Multilingual Engine: polyfill timeouts and intervals
grant execute on javascript to [APEXワークスペース・スキーマ];
grant execute dynamic mle to [APEXワークスペース・スキーマ];
grant create mle to [APEXワークスペース・スキーマ];
npm -v
mkdir demo-jimp
cd demo-jimp
npm init -y
% npm -v
11.3.0
% mkdir demo-jimp
% cd demo-jimp
demo-jimp % npm init -y
Wrote to /Users/**********/Documents/demo-jimp/package.json:
{
"name": "demo-jimp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "commonjs"
}
demo-jimp %
demo-jimp % npm install jimp
added 70 packages, and audited 71 packages in 6s
10 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
demo-jimp %
npm install --save-exact --save-dev esbuild
demo-jimp % npm install --save-exact --save-dev esbuild
added 26 packages, and audited 97 packages in 2s
10 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
demo-jimp %
Martin BackさんのGitHubのリポジトリよりdemo-mle.jsとpolyfill.jsをダウンロードし、src/javascript以下に配置します。
curl -OL https://raw.githubusercontent.com/martincarstenbach/javascript-blogposts/refs/heads/main/timeout-polyfill/src/javascript/demo-mle.js
curl -OL https://raw.githubusercontent.com/martincarstenbach/javascript-blogposts/refs/heads/main/timeout-polyfill/src/javascript/polyfill.js
mkdir -p src/javascript
mv demo-mle.js polyfill.js src/javascript
ls src/javascript
demo-jimp % curl -OL https://raw.githubusercontent.com/martincarstenbach/javascript-blogposts/refs/heads/main/timeout-polyfill/src/javascript/demo-mle.js
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1023 100 1023 0 0 4903 0 --:--:-- --:--:-- --:--:-- 4918
demo-jimp % curl -OL https://raw.githubusercontent.com/martincarstenbach/javascript-blogposts/refs/heads/main/timeout-polyfill/src/javascript/polyfill.js
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2996 100 2996 0 0 7682 0 --:--:-- --:--:-- --:--:-- 7701
demo-jimp % mkdir -p src/javascript
demo-jimp % mv demo-mle.js polyfill.js src/javascript
demo-jimp % ls src/javascript
demo-mle.js polyfill.js
demo-jimp %
npx esbuild src/javascript/demo-mle.js --bundle --outfile=dist/bundle.js --format=esm
demo-jimp % npx esbuild src/javascript/demo-mle.js --bundle --outfile=dist/bundle.js --format=esm
dist/bundle.js 1008.8kb
⚡ Done in 44ms
demo-jimp %
SQLclでAPEXのワークスペース・スキーマに接続し、MLEモジュールを作成します。
demo-jimp % sql wksp_apexdev/********@localhost/freepdb1
SQLcl: 水 6月 25 12:31:27 2025のリリース25.1 Production
Copyright (c) 1982, 2025, Oracle. All rights reserved.
接続先:
Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free
Version 23.8.0.25.04
SQL>
demo-mle.jsにJIMPをバンドルしたdist/bundle.jsを、MLEモジュールJIMP_TEST_MODULEとして作成します。
mle create-module -replace -module-name jimp_test_module -filename dist/bundle.js
SQL> mle create-module -replace -module-name jimp_test_module -filename dist/bundle.js
MLEモジュールjimp_test_moduleが作成されました
SQL>
setTimeout()のpolyfillを、MLEモジュールTIMERS_POLYFILLとして作成します。
mle create-module -replace -module-name timers_polyfill -filename src/javascript/polyfill.js
SQL> mle create-module -replace -module-name timers_polyfill -filename src/javascript/polyfill.js
MLEモジュールtimers_polyfillが作成されました
SQL>
以上で、demo-mle.jsに含まれているファンクションimage2Greyscaleを、データベース内で呼び出せるようになりました。
create table ebaj_demo_images (
id number generated by default on null as identity
constraint ebaj_demo_images_id_pk primary key,
filename varchar2(80 char),
mime_type varchar2(80 char),
operation varchar2(80 char),
source_image blob,
target_image blob
);
create mle env jimp_env imports (
'jimp' module jimp_test_module,
'polyfill' module timers_polyfill
);
新規に画像がアップロードされる場合、ページ・アイテムP1_IDには空白(null)が渡されます。ページ送信時に実行されるプロセスでは、画像を含む行をデータベースに挿入する際に、INSERT INTO ... RETURNING id INTO :P1_ID という構文を用いて主キーの値を取得し、それをP1_IDに設定します。
しかし、セッション・ステートのストレージがリクエストごと(メモリのみ)になっていると、プロセスの処理が完了してフォームのページが再描画される際に、P1_IDの値がセッション・ステートに保存されておらず、結果としてP1_IDは空白のままになります。このため、直前にアップロードされた画像をP1_IDの値を用いて参照できません。
通常、フォームはレポート画面から開かれ、フォームを閉じるとレポート画面に戻る構成であるため、このような問題は発生しません。
しかし、今回のアプリケーションでは、フォームを閉じた後に再び同じフォームに戻る動作となっているため、P1_IDの値がページ遷移を超えて維持される必要があります。この対応として、セッション・ステートのストレージをセッションごと(永続)に変更します。
ページ・アイテムP1_OPERATIONに計算を作成します。
select source_image, null, filename, mime_type
from ebaj_demo_images
where id = :P1_ID
select target_image, null, filename, mime_type
from ebaj_demo_images
where id = :P1_ID