@eggjs/mock 6.0.7 → 6.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -56
- package/README.zh_CN.md +30 -50
- package/dist/_virtual/rolldown_runtime.js +30 -0
- package/dist/app/extend/agent.d.ts +39 -0
- package/dist/app/extend/agent.js +48 -0
- package/dist/app/extend/application.d.ts +175 -0
- package/dist/app/extend/application.js +385 -0
- package/dist/app/middleware/cluster_app_mock.d.ts +6 -0
- package/dist/app/middleware/cluster_app_mock.js +97 -0
- package/dist/app.d.ts +10 -0
- package/dist/app.js +17 -0
- package/dist/bootstrap.d.ts +9 -0
- package/dist/bootstrap.js +14 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +47 -0
- package/dist/inject_mocha.d.ts +1 -0
- package/dist/inject_mocha.js +35 -0
- package/dist/lib/agent_handler.d.ts +7 -0
- package/dist/lib/agent_handler.js +26 -0
- package/dist/lib/app.d.ts +7 -0
- package/dist/lib/app.js +251 -0
- package/dist/lib/app_handler.d.ts +9 -0
- package/dist/lib/app_handler.js +56 -0
- package/dist/lib/cluster.d.ts +126 -0
- package/dist/lib/cluster.js +283 -0
- package/dist/lib/context.d.ts +4 -0
- package/dist/lib/context.js +12 -0
- package/dist/lib/format_options.d.ts +10 -0
- package/dist/lib/format_options.js +65 -0
- package/dist/lib/inject_context.d.ts +9 -0
- package/dist/lib/inject_context.js +106 -0
- package/dist/lib/mock_agent.d.ts +9 -0
- package/dist/lib/mock_agent.js +45 -0
- package/dist/lib/mock_custom_loader.d.ts +4 -0
- package/dist/lib/mock_custom_loader.js +35 -0
- package/dist/lib/mock_http_server.d.ts +6 -0
- package/dist/lib/mock_http_server.js +17 -0
- package/dist/lib/mock_httpclient.d.ts +40 -0
- package/dist/lib/mock_httpclient.js +110 -0
- package/dist/lib/parallel/agent.d.ts +24 -0
- package/dist/lib/parallel/agent.js +111 -0
- package/dist/lib/parallel/app.d.ts +24 -0
- package/dist/lib/parallel/app.js +99 -0
- package/dist/lib/parallel/util.d.ts +6 -0
- package/dist/lib/parallel/util.js +59 -0
- package/dist/lib/prerequire.d.ts +1 -0
- package/dist/lib/prerequire.js +1 -0
- package/dist/lib/request_call_function.d.ts +1 -0
- package/dist/lib/request_call_function.js +37 -0
- package/dist/lib/restore.d.ts +4 -0
- package/dist/lib/restore.js +16 -0
- package/dist/lib/start-cluster.d.ts +1 -0
- package/dist/lib/start-cluster.js +19 -0
- package/dist/lib/supertest.d.ts +16 -0
- package/dist/lib/supertest.js +39 -0
- package/dist/lib/tmp/empty.d.ts +1 -0
- package/dist/lib/tmp/empty.js +1 -0
- package/{src/lib/types.ts → dist/lib/types.d.ts} +12 -29
- package/dist/lib/types.js +1 -0
- package/dist/lib/utils.d.ts +12 -0
- package/dist/lib/utils.js +40 -0
- package/dist/node_modules/.pnpm/ansi-styles@4.3.0/node_modules/ansi-styles/index.js +142 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/index.js +157 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/templates.js +97 -0
- package/dist/node_modules/.pnpm/chalk@4.1.2/node_modules/chalk/source/util.js +40 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/conversions.js +814 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/index.js +50 -0
- package/dist/node_modules/.pnpm/color-convert@2.0.1/node_modules/color-convert/route.js +68 -0
- package/dist/node_modules/.pnpm/color-name@1.1.4/node_modules/color-name/index.js +752 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/browser.js +203 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/common.js +209 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/index.js +18 -0
- package/dist/node_modules/.pnpm/debug@4.4.3_supports-color@8.1.1/node_modules/debug/src/node.js +215 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/dmp.js +24 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/convert/xml.js +33 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/array.js +29 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/base.js +185 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/character.js +23 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/css.js +26 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/json.js +83 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/line.js +51 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/sentence.js +26 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/diff/word.js +151 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/index.js +178 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/apply.js +168 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/create.js +301 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/line-endings.js +137 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/merge.js +367 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/parse.js +91 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/patch/reverse.js +99 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/array.js +23 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/distance-iterator.js +30 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/params.js +22 -0
- package/dist/node_modules/.pnpm/diff@7.0.0/node_modules/diff/lib/util/string.js +97 -0
- package/dist/node_modules/.pnpm/escape-string-regexp@4.0.0/node_modules/escape-string-regexp/index.js +14 -0
- package/dist/node_modules/.pnpm/has-flag@4.0.0/node_modules/has-flag/index.js +16 -0
- package/dist/node_modules/.pnpm/he@1.2.0/node_modules/he/he.js +4093 -0
- package/dist/node_modules/.pnpm/is-unicode-supported@0.1.0/node_modules/is-unicode-supported/index.js +14 -0
- package/dist/node_modules/.pnpm/log-symbols@4.1.0/node_modules/log-symbols/index.js +27 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/index.js +12 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/context.js +83 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/error-constants.js +35 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/errors.js +392 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/hook.js +86 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/bdd.js +104 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/common.js +99 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/exports.js +65 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/index.js +18 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/qunit.js +95 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/interfaces/tdd.js +105 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocha.js +1037 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/mocharc.js +24 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/buffered-worker-pool.js +152 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/esm-utils.js +74 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/file-unloader.js +22 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/parallel-buffered-runner.js +329 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/nodejs/serializer.js +314 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/pending.js +22 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/base.js +417 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/doc.js +79 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/dot.js +75 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/html.js +283 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/index.js +40 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json-stream.js +90 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/json.js +145 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/landing.js +107 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/list.js +77 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/markdown.js +106 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/min.js +57 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/nyan.js +233 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/progress.js +92 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/spec.js +91 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/tap.js +261 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/reporters/xunit.js +166 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runnable.js +346 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/runner.js +922 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/stats-collector.js +72 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/suite.js +517 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/test.js +105 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/lib/utils.js +505 -0
- package/dist/node_modules/.pnpm/mocha@11.7.2/node_modules/mocha/package.js +225 -0
- package/dist/node_modules/.pnpm/ms@2.1.3/node_modules/ms/index.js +124 -0
- package/dist/node_modules/.pnpm/randombytes@2.1.0/node_modules/randombytes/index.js +11 -0
- package/dist/node_modules/.pnpm/serialize-javascript@6.0.2/node_modules/serialize-javascript/index.js +114 -0
- package/dist/node_modules/.pnpm/supports-color@7.2.0/node_modules/supports-color/index.js +76 -0
- package/dist/node_modules/.pnpm/supports-color@8.1.1/node_modules/supports-color/index.js +89 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Pool.js +366 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Promise.js +248 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/WorkerHandler.js +419 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/debug-port-allocator.js +27 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/environment.js +18 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/generated/embeddedWorker.js +16 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/index.js +63 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/transfer.js +21 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/validateOptions.js +9 -0
- package/dist/node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/worker.js +277 -0
- package/dist/register.d.ts +10 -0
- package/dist/register.js +40 -0
- package/dist/typings/index.d.ts +1 -0
- package/package.json +71 -98
- package/dist/commonjs/app/extend/agent.d.ts +0 -34
- package/dist/commonjs/app/extend/agent.js +0 -49
- package/dist/commonjs/app/extend/application.d.ts +0 -172
- package/dist/commonjs/app/extend/application.js +0 -450
- package/dist/commonjs/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/commonjs/app/middleware/cluster_app_mock.js +0 -101
- package/dist/commonjs/app.d.ts +0 -6
- package/dist/commonjs/app.js +0 -20
- package/dist/commonjs/bootstrap.d.ts +0 -5
- package/dist/commonjs/bootstrap.js +0 -59
- package/dist/commonjs/index.d.ts +0 -77
- package/dist/commonjs/index.js +0 -114
- package/dist/commonjs/lib/agent_handler.d.ts +0 -3
- package/dist/commonjs/lib/agent_handler.js +0 -28
- package/dist/commonjs/lib/app.d.ts +0 -3
- package/dist/commonjs/lib/app.js +0 -301
- package/dist/commonjs/lib/app_handler.d.ts +0 -5
- package/dist/commonjs/lib/app_handler.js +0 -71
- package/dist/commonjs/lib/cluster.d.ts +0 -114
- package/dist/commonjs/lib/cluster.js +0 -337
- package/dist/commonjs/lib/context.d.ts +0 -1
- package/dist/commonjs/lib/context.js +0 -16
- package/dist/commonjs/lib/format_options.d.ts +0 -5
- package/dist/commonjs/lib/format_options.js +0 -100
- package/dist/commonjs/lib/inject_context.d.ts +0 -6
- package/dist/commonjs/lib/inject_context.js +0 -132
- package/dist/commonjs/lib/mock_agent.d.ts +0 -5
- package/dist/commonjs/lib/mock_agent.js +0 -49
- package/dist/commonjs/lib/mock_custom_loader.d.ts +0 -1
- package/dist/commonjs/lib/mock_custom_loader.js +0 -37
- package/dist/commonjs/lib/mock_http_server.d.ts +0 -2
- package/dist/commonjs/lib/mock_http_server.js +0 -24
- package/dist/commonjs/lib/mock_httpclient.d.ts +0 -36
- package/dist/commonjs/lib/mock_httpclient.js +0 -147
- package/dist/commonjs/lib/parallel/agent.d.ts +0 -20
- package/dist/commonjs/lib/parallel/agent.js +0 -125
- package/dist/commonjs/lib/parallel/app.d.ts +0 -20
- package/dist/commonjs/lib/parallel/app.js +0 -115
- package/dist/commonjs/lib/parallel/util.d.ts +0 -3
- package/dist/commonjs/lib/parallel/util.js +0 -77
- package/dist/commonjs/lib/prerequire.d.ts +0 -1
- package/dist/commonjs/lib/prerequire.js +0 -26
- package/dist/commonjs/lib/request_call_function.d.ts +0 -1
- package/dist/commonjs/lib/request_call_function.js +0 -52
- package/dist/commonjs/lib/restore.d.ts +0 -1
- package/dist/commonjs/lib/restore.js +0 -16
- package/dist/commonjs/lib/start-cluster.d.ts +0 -2
- package/dist/commonjs/lib/start-cluster.js +0 -23
- package/dist/commonjs/lib/supertest.d.ts +0 -11
- package/dist/commonjs/lib/supertest.js +0 -48
- package/dist/commonjs/lib/tmp/empty.d.ts +0 -1
- package/dist/commonjs/lib/tmp/empty.js +0 -3
- package/dist/commonjs/lib/types.d.ts +0 -68
- package/dist/commonjs/lib/types.js +0 -3
- package/dist/commonjs/lib/utils.d.ts +0 -9
- package/dist/commonjs/lib/utils.js +0 -80
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/register.d.ts +0 -8
- package/dist/commonjs/register.js +0 -80
- package/dist/esm/app/extend/agent.d.ts +0 -34
- package/dist/esm/app/extend/agent.js +0 -46
- package/dist/esm/app/extend/application.d.ts +0 -172
- package/dist/esm/app/extend/application.js +0 -444
- package/dist/esm/app/middleware/cluster_app_mock.d.ts +0 -3
- package/dist/esm/app/middleware/cluster_app_mock.js +0 -99
- package/dist/esm/app.d.ts +0 -6
- package/dist/esm/app.js +0 -17
- package/dist/esm/bootstrap.d.ts +0 -5
- package/dist/esm/bootstrap.js +0 -15
- package/dist/esm/index.d.ts +0 -77
- package/dist/esm/index.js +0 -91
- package/dist/esm/lib/agent_handler.d.ts +0 -3
- package/dist/esm/lib/agent_handler.js +0 -24
- package/dist/esm/lib/app.d.ts +0 -3
- package/dist/esm/lib/app.js +0 -295
- package/dist/esm/lib/app_handler.d.ts +0 -5
- package/dist/esm/lib/app_handler.js +0 -65
- package/dist/esm/lib/cluster.d.ts +0 -114
- package/dist/esm/lib/cluster.js +0 -328
- package/dist/esm/lib/context.d.ts +0 -1
- package/dist/esm/lib/context.js +0 -13
- package/dist/esm/lib/format_options.d.ts +0 -5
- package/dist/esm/lib/format_options.js +0 -94
- package/dist/esm/lib/inject_context.d.ts +0 -6
- package/dist/esm/lib/inject_context.js +0 -126
- package/dist/esm/lib/mock_agent.d.ts +0 -5
- package/dist/esm/lib/mock_agent.js +0 -45
- package/dist/esm/lib/mock_custom_loader.d.ts +0 -1
- package/dist/esm/lib/mock_custom_loader.js +0 -34
- package/dist/esm/lib/mock_http_server.d.ts +0 -2
- package/dist/esm/lib/mock_http_server.js +0 -18
- package/dist/esm/lib/mock_httpclient.d.ts +0 -36
- package/dist/esm/lib/mock_httpclient.js +0 -144
- package/dist/esm/lib/parallel/agent.d.ts +0 -20
- package/dist/esm/lib/parallel/agent.js +0 -117
- package/dist/esm/lib/parallel/app.d.ts +0 -20
- package/dist/esm/lib/parallel/app.js +0 -110
- package/dist/esm/lib/parallel/util.d.ts +0 -3
- package/dist/esm/lib/parallel/util.js +0 -73
- package/dist/esm/lib/prerequire.d.ts +0 -1
- package/dist/esm/lib/prerequire.js +0 -25
- package/dist/esm/lib/request_call_function.d.ts +0 -1
- package/dist/esm/lib/request_call_function.js +0 -47
- package/dist/esm/lib/restore.d.ts +0 -1
- package/dist/esm/lib/restore.js +0 -13
- package/dist/esm/lib/start-cluster.d.ts +0 -2
- package/dist/esm/lib/start-cluster.js +0 -18
- package/dist/esm/lib/supertest.d.ts +0 -11
- package/dist/esm/lib/supertest.js +0 -40
- package/dist/esm/lib/tmp/empty.d.ts +0 -1
- package/dist/esm/lib/tmp/empty.js +0 -2
- package/dist/esm/lib/types.d.ts +0 -68
- package/dist/esm/lib/types.js +0 -2
- package/dist/esm/lib/utils.d.ts +0 -9
- package/dist/esm/lib/utils.js +0 -69
- package/dist/esm/package.json +0 -3
- package/dist/esm/register.d.ts +0 -8
- package/dist/esm/register.js +0 -75
- package/dist/package.json +0 -4
- package/src/app/extend/agent.ts +0 -57
- package/src/app/extend/application.ts +0 -512
- package/src/app/middleware/cluster_app_mock.ts +0 -102
- package/src/app.ts +0 -18
- package/src/bootstrap.ts +0 -25
- package/src/index.ts +0 -112
- package/src/lib/agent_handler.ts +0 -28
- package/src/lib/app.ts +0 -314
- package/src/lib/app_handler.ts +0 -77
- package/src/lib/cluster.ts +0 -363
- package/src/lib/context.ts +0 -14
- package/src/lib/format_options.ts +0 -103
- package/src/lib/inject_context.ts +0 -134
- package/src/lib/mock_agent.ts +0 -57
- package/src/lib/mock_custom_loader.ts +0 -36
- package/src/lib/mock_http_server.ts +0 -19
- package/src/lib/mock_httpclient.ts +0 -183
- package/src/lib/parallel/agent.ts +0 -128
- package/src/lib/parallel/app.ts +0 -123
- package/src/lib/parallel/util.ts +0 -66
- package/src/lib/prerequire.ts +0 -25
- package/src/lib/request_call_function.ts +0 -49
- package/src/lib/restore.ts +0 -14
- package/src/lib/start-cluster.ts +0 -23
- package/src/lib/supertest.ts +0 -45
- package/src/lib/tmp/.gitkeep +0 -0
- package/src/lib/tmp/empty.ts +0 -0
- package/src/lib/utils.ts +0 -82
- package/src/register.ts +0 -80
- package/src/typings/index.d.ts +0 -4
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { __commonJS } from "../../../../../../_virtual/rolldown_runtime.js";
|
|
2
|
+
import { require_environment } from "./environment.js";
|
|
3
|
+
import { require_Promise } from "./Promise.js";
|
|
4
|
+
import { require_WorkerHandler } from "./WorkerHandler.js";
|
|
5
|
+
import { require_debug_port_allocator } from "./debug-port-allocator.js";
|
|
6
|
+
|
|
7
|
+
//#region ../../node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Pool.js
|
|
8
|
+
var require_Pool = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Pool.js": ((exports, module) => {
|
|
9
|
+
var { Promise: Promise$1 } = require_Promise();
|
|
10
|
+
var WorkerHandler = require_WorkerHandler();
|
|
11
|
+
var environment = require_environment();
|
|
12
|
+
var DEBUG_PORT_ALLOCATOR = new (require_debug_port_allocator())();
|
|
13
|
+
/**
|
|
14
|
+
* A pool to manage workers, which can be created using the function workerpool.pool.
|
|
15
|
+
*
|
|
16
|
+
* @param {String} [script] Optional worker script
|
|
17
|
+
* @param {import('./types.js').WorkerPoolOptions} [options] See docs
|
|
18
|
+
* @constructor
|
|
19
|
+
*/
|
|
20
|
+
function Pool(script, options) {
|
|
21
|
+
if (typeof script === "string")
|
|
22
|
+
/** @readonly */
|
|
23
|
+
this.script = script || null;
|
|
24
|
+
else {
|
|
25
|
+
this.script = null;
|
|
26
|
+
options = script;
|
|
27
|
+
}
|
|
28
|
+
/** @private */
|
|
29
|
+
this.workers = [];
|
|
30
|
+
/** @private */
|
|
31
|
+
this.tasks = [];
|
|
32
|
+
options = options || {};
|
|
33
|
+
/** @readonly */
|
|
34
|
+
this.forkArgs = Object.freeze(options.forkArgs || []);
|
|
35
|
+
/** @readonly */
|
|
36
|
+
this.forkOpts = Object.freeze(options.forkOpts || {});
|
|
37
|
+
/** @readonly */
|
|
38
|
+
this.workerOpts = Object.freeze(options.workerOpts || {});
|
|
39
|
+
/** @readonly */
|
|
40
|
+
this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {});
|
|
41
|
+
/** @private */
|
|
42
|
+
this.debugPortStart = options.debugPortStart || 43210;
|
|
43
|
+
/** @readonly @deprecated */
|
|
44
|
+
this.nodeWorker = options.nodeWorker;
|
|
45
|
+
/** @readonly
|
|
46
|
+
* @type {'auto' | 'web' | 'process' | 'thread'}
|
|
47
|
+
*/
|
|
48
|
+
this.workerType = options.workerType || options.nodeWorker || "auto";
|
|
49
|
+
/** @readonly */
|
|
50
|
+
this.maxQueueSize = options.maxQueueSize || Infinity;
|
|
51
|
+
/** @readonly */
|
|
52
|
+
this.workerTerminateTimeout = options.workerTerminateTimeout || 1e3;
|
|
53
|
+
/** @readonly */
|
|
54
|
+
this.onCreateWorker = options.onCreateWorker || (() => null);
|
|
55
|
+
/** @readonly */
|
|
56
|
+
this.onTerminateWorker = options.onTerminateWorker || (() => null);
|
|
57
|
+
/** @readonly */
|
|
58
|
+
this.emitStdStreams = options.emitStdStreams || false;
|
|
59
|
+
if (options && "maxWorkers" in options) {
|
|
60
|
+
validateMaxWorkers(options.maxWorkers);
|
|
61
|
+
/** @readonly */
|
|
62
|
+
this.maxWorkers = options.maxWorkers;
|
|
63
|
+
} else this.maxWorkers = Math.max((environment.cpus || 4) - 1, 1);
|
|
64
|
+
if (options && "minWorkers" in options) {
|
|
65
|
+
if (options.minWorkers === "max")
|
|
66
|
+
/** @readonly */
|
|
67
|
+
this.minWorkers = this.maxWorkers;
|
|
68
|
+
else {
|
|
69
|
+
validateMinWorkers(options.minWorkers);
|
|
70
|
+
this.minWorkers = options.minWorkers;
|
|
71
|
+
this.maxWorkers = Math.max(this.minWorkers, this.maxWorkers);
|
|
72
|
+
}
|
|
73
|
+
this._ensureMinWorkers();
|
|
74
|
+
}
|
|
75
|
+
/** @private */
|
|
76
|
+
this._boundNext = this._next.bind(this);
|
|
77
|
+
if (this.workerType === "thread") WorkerHandler.ensureWorkerThreads();
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Execute a function on a worker.
|
|
81
|
+
*
|
|
82
|
+
* Example usage:
|
|
83
|
+
*
|
|
84
|
+
* var pool = new Pool()
|
|
85
|
+
*
|
|
86
|
+
* // call a function available on the worker
|
|
87
|
+
* pool.exec('fibonacci', [6])
|
|
88
|
+
*
|
|
89
|
+
* // offload a function
|
|
90
|
+
* function add(a, b) {
|
|
91
|
+
* return a + b
|
|
92
|
+
* };
|
|
93
|
+
* pool.exec(add, [2, 4])
|
|
94
|
+
* .then(function (result) {
|
|
95
|
+
* console.log(result); // outputs 6
|
|
96
|
+
* })
|
|
97
|
+
* .catch(function(error) {
|
|
98
|
+
* console.log(error);
|
|
99
|
+
* });
|
|
100
|
+
* @template { (...args: any[]) => any } T
|
|
101
|
+
* @param {String | T} method Function name or function.
|
|
102
|
+
* If `method` is a string, the corresponding
|
|
103
|
+
* method on the worker will be executed
|
|
104
|
+
* If `method` is a Function, the function
|
|
105
|
+
* will be stringified and executed via the
|
|
106
|
+
* workers built-in function `run(fn, args)`.
|
|
107
|
+
* @param {Parameters<T> | null} [params] Function arguments applied when calling the function
|
|
108
|
+
* @param {import('./types.js').ExecOptions} [options] Options
|
|
109
|
+
* @return {Promise<ReturnType<T>>}
|
|
110
|
+
*/
|
|
111
|
+
Pool.prototype.exec = function(method, params, options) {
|
|
112
|
+
if (params && !Array.isArray(params)) throw new TypeError("Array expected as argument \"params\"");
|
|
113
|
+
if (typeof method === "string") {
|
|
114
|
+
var resolver = Promise$1.defer();
|
|
115
|
+
if (this.tasks.length >= this.maxQueueSize) throw new Error("Max queue size of " + this.maxQueueSize + " reached");
|
|
116
|
+
var tasks = this.tasks;
|
|
117
|
+
var task = {
|
|
118
|
+
method,
|
|
119
|
+
params,
|
|
120
|
+
resolver,
|
|
121
|
+
timeout: null,
|
|
122
|
+
options
|
|
123
|
+
};
|
|
124
|
+
tasks.push(task);
|
|
125
|
+
var originalTimeout = resolver.promise.timeout;
|
|
126
|
+
resolver.promise.timeout = function timeout(delay) {
|
|
127
|
+
if (tasks.indexOf(task) !== -1) {
|
|
128
|
+
task.timeout = delay;
|
|
129
|
+
return resolver.promise;
|
|
130
|
+
} else return originalTimeout.call(resolver.promise, delay);
|
|
131
|
+
};
|
|
132
|
+
this._next();
|
|
133
|
+
return resolver.promise;
|
|
134
|
+
} else if (typeof method === "function") return this.exec("run", [String(method), params], options);
|
|
135
|
+
else throw new TypeError("Function or string expected as argument \"method\"");
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Create a proxy for current worker. Returns an object containing all
|
|
139
|
+
* methods available on the worker. All methods return promises resolving the methods result.
|
|
140
|
+
* @template { { [k: string]: (...args: any[]) => any } } T
|
|
141
|
+
* @return {Promise<import('./types.js').Proxy<T>, Error>} Returns a promise which resolves with a proxy object
|
|
142
|
+
*/
|
|
143
|
+
Pool.prototype.proxy = function() {
|
|
144
|
+
if (arguments.length > 0) throw new Error("No arguments expected");
|
|
145
|
+
var pool = this;
|
|
146
|
+
return this.exec("methods").then(function(methods) {
|
|
147
|
+
var proxy = {};
|
|
148
|
+
methods.forEach(function(method) {
|
|
149
|
+
proxy[method] = function() {
|
|
150
|
+
return pool.exec(method, Array.prototype.slice.call(arguments));
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
return proxy;
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Creates new array with the results of calling a provided callback function
|
|
158
|
+
* on every element in this array.
|
|
159
|
+
* @param {Array} array
|
|
160
|
+
* @param {function} callback Function taking two arguments:
|
|
161
|
+
* `callback(currentValue, index)`
|
|
162
|
+
* @return {Promise.<Array>} Returns a promise which resolves with an Array
|
|
163
|
+
* containing the results of the callback function
|
|
164
|
+
* executed for each of the array elements.
|
|
165
|
+
*/
|
|
166
|
+
/**
|
|
167
|
+
* Grab the first task from the queue, find a free worker, and assign the
|
|
168
|
+
* worker to the task.
|
|
169
|
+
* @private
|
|
170
|
+
*/
|
|
171
|
+
Pool.prototype._next = function() {
|
|
172
|
+
if (this.tasks.length > 0) {
|
|
173
|
+
var worker = this._getWorker();
|
|
174
|
+
if (worker) {
|
|
175
|
+
var me = this;
|
|
176
|
+
var task = this.tasks.shift();
|
|
177
|
+
if (task.resolver.promise.pending) {
|
|
178
|
+
var promise = worker.exec(task.method, task.params, task.resolver, task.options).then(me._boundNext).catch(function() {
|
|
179
|
+
if (worker.terminated) return me._removeWorker(worker);
|
|
180
|
+
}).then(function() {
|
|
181
|
+
me._next();
|
|
182
|
+
});
|
|
183
|
+
if (typeof task.timeout === "number") promise.timeout(task.timeout);
|
|
184
|
+
} else me._next();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Get an available worker. If no worker is available and the maximum number
|
|
190
|
+
* of workers isn't yet reached, a new worker will be created and returned.
|
|
191
|
+
* If no worker is available and the maximum number of workers is reached,
|
|
192
|
+
* null will be returned.
|
|
193
|
+
*
|
|
194
|
+
* @return {WorkerHandler | null} worker
|
|
195
|
+
* @private
|
|
196
|
+
*/
|
|
197
|
+
Pool.prototype._getWorker = function() {
|
|
198
|
+
var workers = this.workers;
|
|
199
|
+
for (var i = 0; i < workers.length; i++) {
|
|
200
|
+
var worker = workers[i];
|
|
201
|
+
if (worker.busy() === false) return worker;
|
|
202
|
+
}
|
|
203
|
+
if (workers.length < this.maxWorkers) {
|
|
204
|
+
worker = this._createWorkerHandler();
|
|
205
|
+
workers.push(worker);
|
|
206
|
+
return worker;
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Remove a worker from the pool.
|
|
212
|
+
* Attempts to terminate worker if not already terminated, and ensures the minimum
|
|
213
|
+
* pool size is met.
|
|
214
|
+
* @param {WorkerHandler} worker
|
|
215
|
+
* @return {Promise<WorkerHandler>}
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
Pool.prototype._removeWorker = function(worker) {
|
|
219
|
+
var me = this;
|
|
220
|
+
DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort);
|
|
221
|
+
this._removeWorkerFromList(worker);
|
|
222
|
+
this._ensureMinWorkers();
|
|
223
|
+
return new Promise$1(function(resolve, reject) {
|
|
224
|
+
worker.terminate(false, function(err) {
|
|
225
|
+
me.onTerminateWorker({
|
|
226
|
+
forkArgs: worker.forkArgs,
|
|
227
|
+
forkOpts: worker.forkOpts,
|
|
228
|
+
workerThreadOpts: worker.workerThreadOpts,
|
|
229
|
+
script: worker.script
|
|
230
|
+
});
|
|
231
|
+
if (err) reject(err);
|
|
232
|
+
else resolve(worker);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Remove a worker from the pool list.
|
|
238
|
+
* @param {WorkerHandler} worker
|
|
239
|
+
* @private
|
|
240
|
+
*/
|
|
241
|
+
Pool.prototype._removeWorkerFromList = function(worker) {
|
|
242
|
+
var index = this.workers.indexOf(worker);
|
|
243
|
+
if (index !== -1) this.workers.splice(index, 1);
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Close all active workers. Tasks currently being executed will be finished first.
|
|
247
|
+
* @param {boolean} [force=false] If false (default), the workers are terminated
|
|
248
|
+
* after finishing all tasks currently in
|
|
249
|
+
* progress. If true, the workers will be
|
|
250
|
+
* terminated immediately.
|
|
251
|
+
* @param {number} [timeout] If provided and non-zero, worker termination promise will be rejected
|
|
252
|
+
* after timeout if worker process has not been terminated.
|
|
253
|
+
* @return {Promise.<void, Error>}
|
|
254
|
+
*/
|
|
255
|
+
Pool.prototype.terminate = function(force, timeout) {
|
|
256
|
+
var me = this;
|
|
257
|
+
this.tasks.forEach(function(task) {
|
|
258
|
+
task.resolver.reject(/* @__PURE__ */ new Error("Pool terminated"));
|
|
259
|
+
});
|
|
260
|
+
this.tasks.length = 0;
|
|
261
|
+
var f = function(worker) {
|
|
262
|
+
DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort);
|
|
263
|
+
this._removeWorkerFromList(worker);
|
|
264
|
+
};
|
|
265
|
+
var removeWorker = f.bind(this);
|
|
266
|
+
var promises = [];
|
|
267
|
+
this.workers.slice().forEach(function(worker) {
|
|
268
|
+
var termPromise = worker.terminateAndNotify(force, timeout).then(removeWorker).always(function() {
|
|
269
|
+
me.onTerminateWorker({
|
|
270
|
+
forkArgs: worker.forkArgs,
|
|
271
|
+
forkOpts: worker.forkOpts,
|
|
272
|
+
workerThreadOpts: worker.workerThreadOpts,
|
|
273
|
+
script: worker.script
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
promises.push(termPromise);
|
|
277
|
+
});
|
|
278
|
+
return Promise$1.all(promises);
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* Retrieve statistics on tasks and workers.
|
|
282
|
+
* @return {{totalWorkers: number, busyWorkers: number, idleWorkers: number, pendingTasks: number, activeTasks: number}} Returns an object with statistics
|
|
283
|
+
*/
|
|
284
|
+
Pool.prototype.stats = function() {
|
|
285
|
+
var totalWorkers = this.workers.length;
|
|
286
|
+
var busyWorkers = this.workers.filter(function(worker) {
|
|
287
|
+
return worker.busy();
|
|
288
|
+
}).length;
|
|
289
|
+
return {
|
|
290
|
+
totalWorkers,
|
|
291
|
+
busyWorkers,
|
|
292
|
+
idleWorkers: totalWorkers - busyWorkers,
|
|
293
|
+
pendingTasks: this.tasks.length,
|
|
294
|
+
activeTasks: busyWorkers
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Ensures that a minimum of minWorkers is up and running
|
|
299
|
+
* @private
|
|
300
|
+
*/
|
|
301
|
+
Pool.prototype._ensureMinWorkers = function() {
|
|
302
|
+
if (this.minWorkers) for (var i = this.workers.length; i < this.minWorkers; i++) this.workers.push(this._createWorkerHandler());
|
|
303
|
+
};
|
|
304
|
+
/**
|
|
305
|
+
* Helper function to create a new WorkerHandler and pass all options.
|
|
306
|
+
* @return {WorkerHandler}
|
|
307
|
+
* @private
|
|
308
|
+
*/
|
|
309
|
+
Pool.prototype._createWorkerHandler = function() {
|
|
310
|
+
const overriddenParams = this.onCreateWorker({
|
|
311
|
+
forkArgs: this.forkArgs,
|
|
312
|
+
forkOpts: this.forkOpts,
|
|
313
|
+
workerOpts: this.workerOpts,
|
|
314
|
+
workerThreadOpts: this.workerThreadOpts,
|
|
315
|
+
script: this.script
|
|
316
|
+
}) || {};
|
|
317
|
+
return new WorkerHandler(overriddenParams.script || this.script, {
|
|
318
|
+
forkArgs: overriddenParams.forkArgs || this.forkArgs,
|
|
319
|
+
forkOpts: overriddenParams.forkOpts || this.forkOpts,
|
|
320
|
+
workerOpts: overriddenParams.workerOpts || this.workerOpts,
|
|
321
|
+
workerThreadOpts: overriddenParams.workerThreadOpts || this.workerThreadOpts,
|
|
322
|
+
debugPort: DEBUG_PORT_ALLOCATOR.nextAvailableStartingAt(this.debugPortStart),
|
|
323
|
+
workerType: this.workerType,
|
|
324
|
+
workerTerminateTimeout: this.workerTerminateTimeout,
|
|
325
|
+
emitStdStreams: this.emitStdStreams
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
/**
|
|
329
|
+
* Ensure that the maxWorkers option is an integer >= 1
|
|
330
|
+
* @param {*} maxWorkers
|
|
331
|
+
* @returns {boolean} returns true maxWorkers has a valid value
|
|
332
|
+
*/
|
|
333
|
+
function validateMaxWorkers(maxWorkers) {
|
|
334
|
+
if (!isNumber(maxWorkers) || !isInteger(maxWorkers) || maxWorkers < 1) throw new TypeError("Option maxWorkers must be an integer number >= 1");
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Ensure that the minWorkers option is an integer >= 0
|
|
338
|
+
* @param {*} minWorkers
|
|
339
|
+
* @returns {boolean} returns true when minWorkers has a valid value
|
|
340
|
+
*/
|
|
341
|
+
function validateMinWorkers(minWorkers) {
|
|
342
|
+
if (!isNumber(minWorkers) || !isInteger(minWorkers) || minWorkers < 0) throw new TypeError("Option minWorkers must be an integer number >= 0");
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Test whether a variable is a number
|
|
346
|
+
* @param {*} value
|
|
347
|
+
* @returns {boolean} returns true when value is a number
|
|
348
|
+
*/
|
|
349
|
+
function isNumber(value) {
|
|
350
|
+
return typeof value === "number";
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Test whether a number is an integer
|
|
354
|
+
* @param {number} value
|
|
355
|
+
* @returns {boolean} Returns true if value is an integer
|
|
356
|
+
*/
|
|
357
|
+
function isInteger(value) {
|
|
358
|
+
return Math.round(value) == value;
|
|
359
|
+
}
|
|
360
|
+
module.exports = Pool;
|
|
361
|
+
}) });
|
|
362
|
+
|
|
363
|
+
//#endregion
|
|
364
|
+
export default require_Pool();
|
|
365
|
+
|
|
366
|
+
export { require_Pool };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { __commonJS } from "../../../../../../_virtual/rolldown_runtime.js";
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Promise.js
|
|
4
|
+
var require_Promise = /* @__PURE__ */ __commonJS({ "../../node_modules/.pnpm/workerpool@9.3.4/node_modules/workerpool/src/Promise.js": ((exports) => {
|
|
5
|
+
/**
|
|
6
|
+
* Promise
|
|
7
|
+
*
|
|
8
|
+
* Inspired by https://gist.github.com/RubaXa/8501359 from RubaXa <trash@rubaxa.org>
|
|
9
|
+
* @template T
|
|
10
|
+
* @template [E=Error]
|
|
11
|
+
* @param {Function} handler Called as handler(resolve: Function, reject: Function)
|
|
12
|
+
* @param {Promise} [parent] Parent promise for propagation of cancel and timeout
|
|
13
|
+
*/
|
|
14
|
+
function Promise$1(handler, parent) {
|
|
15
|
+
var me = this;
|
|
16
|
+
if (!(this instanceof Promise$1)) throw new SyntaxError("Constructor must be called with the new operator");
|
|
17
|
+
if (typeof handler !== "function") throw new SyntaxError("Function parameter handler(resolve, reject) missing");
|
|
18
|
+
var _onSuccess = [];
|
|
19
|
+
var _onFail = [];
|
|
20
|
+
/**
|
|
21
|
+
* @readonly
|
|
22
|
+
*/
|
|
23
|
+
this.resolved = false;
|
|
24
|
+
/**
|
|
25
|
+
* @readonly
|
|
26
|
+
*/
|
|
27
|
+
this.rejected = false;
|
|
28
|
+
/**
|
|
29
|
+
* @readonly
|
|
30
|
+
*/
|
|
31
|
+
this.pending = true;
|
|
32
|
+
/**
|
|
33
|
+
* @readonly
|
|
34
|
+
*/
|
|
35
|
+
this[Symbol.toStringTag] = "Promise";
|
|
36
|
+
/**
|
|
37
|
+
* Process onSuccess and onFail callbacks: add them to the queue.
|
|
38
|
+
* Once the promise is resolved, the function _promise is replace.
|
|
39
|
+
* @param {Function} onSuccess
|
|
40
|
+
* @param {Function} onFail
|
|
41
|
+
* @private
|
|
42
|
+
*/
|
|
43
|
+
var _process = function(onSuccess, onFail) {
|
|
44
|
+
_onSuccess.push(onSuccess);
|
|
45
|
+
_onFail.push(onFail);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Add an onSuccess callback and optionally an onFail callback to the Promise
|
|
49
|
+
* @template TT
|
|
50
|
+
* @template [TE=never]
|
|
51
|
+
* @param {(r: T) => TT | PromiseLike<TT>} onSuccess
|
|
52
|
+
* @param {(r: E) => TE | PromiseLike<TE>} [onFail]
|
|
53
|
+
* @returns {Promise<TT | TE, any>} promise
|
|
54
|
+
*/
|
|
55
|
+
this.then = function(onSuccess, onFail) {
|
|
56
|
+
return new Promise$1(function(resolve, reject) {
|
|
57
|
+
var s = onSuccess ? _then(onSuccess, resolve, reject) : resolve;
|
|
58
|
+
var f = onFail ? _then(onFail, resolve, reject) : reject;
|
|
59
|
+
_process(s, f);
|
|
60
|
+
}, me);
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Resolve the promise
|
|
64
|
+
* @param {*} result
|
|
65
|
+
* @type {Function}
|
|
66
|
+
*/
|
|
67
|
+
var _resolve = function(result) {
|
|
68
|
+
me.resolved = true;
|
|
69
|
+
me.rejected = false;
|
|
70
|
+
me.pending = false;
|
|
71
|
+
_onSuccess.forEach(function(fn) {
|
|
72
|
+
fn(result);
|
|
73
|
+
});
|
|
74
|
+
_process = function(onSuccess, onFail) {
|
|
75
|
+
onSuccess(result);
|
|
76
|
+
};
|
|
77
|
+
_resolve = _reject = function() {};
|
|
78
|
+
return me;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Reject the promise
|
|
82
|
+
* @param {Error} error
|
|
83
|
+
* @type {Function}
|
|
84
|
+
*/
|
|
85
|
+
var _reject = function(error) {
|
|
86
|
+
me.resolved = false;
|
|
87
|
+
me.rejected = true;
|
|
88
|
+
me.pending = false;
|
|
89
|
+
_onFail.forEach(function(fn) {
|
|
90
|
+
fn(error);
|
|
91
|
+
});
|
|
92
|
+
_process = function(onSuccess, onFail) {
|
|
93
|
+
onFail(error);
|
|
94
|
+
};
|
|
95
|
+
_resolve = _reject = function() {};
|
|
96
|
+
return me;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Cancel the promise. This will reject the promise with a CancellationError
|
|
100
|
+
* @returns {this} self
|
|
101
|
+
*/
|
|
102
|
+
this.cancel = function() {
|
|
103
|
+
if (parent) parent.cancel();
|
|
104
|
+
else _reject(new CancellationError());
|
|
105
|
+
return me;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Set a timeout for the promise. If the promise is not resolved within
|
|
109
|
+
* the time, the promise will be cancelled and a TimeoutError is thrown.
|
|
110
|
+
* If the promise is resolved in time, the timeout is removed.
|
|
111
|
+
* @param {number} delay Delay in milliseconds
|
|
112
|
+
* @returns {this} self
|
|
113
|
+
*/
|
|
114
|
+
this.timeout = function(delay) {
|
|
115
|
+
if (parent) parent.timeout(delay);
|
|
116
|
+
else {
|
|
117
|
+
var timer = setTimeout(function() {
|
|
118
|
+
_reject(new TimeoutError("Promise timed out after " + delay + " ms"));
|
|
119
|
+
}, delay);
|
|
120
|
+
me.always(function() {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return me;
|
|
125
|
+
};
|
|
126
|
+
handler(function(result) {
|
|
127
|
+
_resolve(result);
|
|
128
|
+
}, function(error) {
|
|
129
|
+
_reject(error);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Execute given callback, then call resolve/reject based on the returned result
|
|
134
|
+
* @param {Function} callback
|
|
135
|
+
* @param {Function} resolve
|
|
136
|
+
* @param {Function} reject
|
|
137
|
+
* @returns {Function}
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
function _then(callback, resolve, reject) {
|
|
141
|
+
return function(result) {
|
|
142
|
+
try {
|
|
143
|
+
var res = callback(result);
|
|
144
|
+
if (res && typeof res.then === "function" && typeof res["catch"] === "function") res.then(resolve, reject);
|
|
145
|
+
else resolve(res);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
reject(error);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Add an onFail callback to the Promise
|
|
153
|
+
* @template TT
|
|
154
|
+
* @param {(error: E) => TT | PromiseLike<TT>} onFail
|
|
155
|
+
* @returns {Promise<T | TT>} promise
|
|
156
|
+
*/
|
|
157
|
+
Promise$1.prototype["catch"] = function(onFail) {
|
|
158
|
+
return this.then(null, onFail);
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Execute given callback when the promise either resolves or rejects.
|
|
162
|
+
* @template TT
|
|
163
|
+
* @param {() => Promise<TT>} fn
|
|
164
|
+
* @returns {Promise<TT>} promise
|
|
165
|
+
*/
|
|
166
|
+
Promise$1.prototype.always = function(fn) {
|
|
167
|
+
return this.then(fn, fn);
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Execute given callback when the promise either resolves or rejects.
|
|
171
|
+
* Same semantics as Node's Promise.finally()
|
|
172
|
+
* @param {Function | null | undefined} [fn]
|
|
173
|
+
* @returns {Promise} promise
|
|
174
|
+
*/
|
|
175
|
+
Promise$1.prototype.finally = function(fn) {
|
|
176
|
+
const me = this;
|
|
177
|
+
const final = function() {
|
|
178
|
+
return new Promise$1((resolve) => resolve()).then(fn).then(() => me);
|
|
179
|
+
};
|
|
180
|
+
return this.then(final, final);
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Create a promise which resolves when all provided promises are resolved,
|
|
184
|
+
* and fails when any of the promises resolves.
|
|
185
|
+
* @param {Promise[]} promises
|
|
186
|
+
* @returns {Promise<any[], any>} promise
|
|
187
|
+
*/
|
|
188
|
+
Promise$1.all = function(promises) {
|
|
189
|
+
return new Promise$1(function(resolve, reject) {
|
|
190
|
+
var remaining = promises.length, results = [];
|
|
191
|
+
if (remaining) promises.forEach(function(p, i) {
|
|
192
|
+
p.then(function(result) {
|
|
193
|
+
results[i] = result;
|
|
194
|
+
remaining--;
|
|
195
|
+
if (remaining == 0) resolve(results);
|
|
196
|
+
}, function(error) {
|
|
197
|
+
remaining = 0;
|
|
198
|
+
reject(error);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
else resolve(results);
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Create a promise resolver
|
|
206
|
+
* @returns {{promise: Promise, resolve: Function, reject: Function}} resolver
|
|
207
|
+
*/
|
|
208
|
+
Promise$1.defer = function() {
|
|
209
|
+
var resolver = {};
|
|
210
|
+
resolver.promise = new Promise$1(function(resolve, reject) {
|
|
211
|
+
resolver.resolve = resolve;
|
|
212
|
+
resolver.reject = reject;
|
|
213
|
+
});
|
|
214
|
+
return resolver;
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* Create a cancellation error
|
|
218
|
+
* @param {String} [message]
|
|
219
|
+
* @extends Error
|
|
220
|
+
*/
|
|
221
|
+
function CancellationError(message) {
|
|
222
|
+
this.message = message || "promise cancelled";
|
|
223
|
+
this.stack = (/* @__PURE__ */ new Error()).stack;
|
|
224
|
+
}
|
|
225
|
+
CancellationError.prototype = /* @__PURE__ */ new Error();
|
|
226
|
+
CancellationError.prototype.constructor = Error;
|
|
227
|
+
CancellationError.prototype.name = "CancellationError";
|
|
228
|
+
Promise$1.CancellationError = CancellationError;
|
|
229
|
+
/**
|
|
230
|
+
* Create a timeout error
|
|
231
|
+
* @param {String} [message]
|
|
232
|
+
* @extends Error
|
|
233
|
+
*/
|
|
234
|
+
function TimeoutError(message) {
|
|
235
|
+
this.message = message || "timeout exceeded";
|
|
236
|
+
this.stack = (/* @__PURE__ */ new Error()).stack;
|
|
237
|
+
}
|
|
238
|
+
TimeoutError.prototype = /* @__PURE__ */ new Error();
|
|
239
|
+
TimeoutError.prototype.constructor = Error;
|
|
240
|
+
TimeoutError.prototype.name = "TimeoutError";
|
|
241
|
+
Promise$1.TimeoutError = TimeoutError;
|
|
242
|
+
exports.Promise = Promise$1;
|
|
243
|
+
}) });
|
|
244
|
+
|
|
245
|
+
//#endregion
|
|
246
|
+
export default require_Promise();
|
|
247
|
+
|
|
248
|
+
export { require_Promise };
|