@sentio/runtime 2.57.12-rc.b → 2.57.12-rc.d
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/lib/chunk-6YBY6GSE.js +22984 -0
- package/lib/chunk-6YBY6GSE.js.map +1 -0
- package/lib/{chunk-HBDDXJ53.js → chunk-CPC37CZL.js} +17792 -37162
- package/lib/chunk-CPC37CZL.js.map +1 -0
- package/lib/index.d.ts +136 -50
- package/lib/index.js +5 -3
- package/lib/index.js.map +1 -1
- package/lib/processor-runner.js +40074 -3
- package/lib/processor-runner.js.map +1 -1
- package/lib/service-worker.d.ts +6 -1
- package/lib/service-worker.js +51 -20
- package/lib/service-worker.js.map +1 -1
- package/lib/test-processor.test.d.ts +2 -0
- package/lib/test-processor.test.js.map +1 -0
- package/package.json +3 -28
- package/src/service-manager.ts +20 -25
- package/src/service-worker.ts +21 -24
- package/src/tsup.config.ts +1 -1
- package/lib/chunk-HBDDXJ53.js.map +0 -1
- package/lib/chunk-SDKBU7PP.js +0 -39674
- package/lib/chunk-SDKBU7PP.js.map +0 -1
package/lib/service-worker.d.ts
CHANGED
package/lib/service-worker.js
CHANGED
@@ -1,24 +1,23 @@
|
|
1
1
|
import { createRequire as createRequireShim } from 'module'; const require = createRequireShim(import.meta.url);
|
2
2
|
import {
|
3
|
-
command_line_args_default,
|
4
|
-
optionDefinitions
|
5
|
-
} from "./chunk-SDKBU7PP.js";
|
6
|
-
import {
|
7
|
-
PluginManager,
|
8
|
-
ProcessConfigResponse,
|
9
|
-
__toESM,
|
10
3
|
errorString,
|
11
4
|
freezeGlobalConfig,
|
5
|
+
recordRuntimeInfo,
|
12
6
|
require_lib2 as require_lib,
|
13
7
|
require_lib3 as require_lib2
|
14
|
-
} from "./chunk-
|
8
|
+
} from "./chunk-CPC37CZL.js";
|
9
|
+
import {
|
10
|
+
PluginManager,
|
11
|
+
ProcessConfigResponse,
|
12
|
+
__toESM
|
13
|
+
} from "./chunk-6YBY6GSE.js";
|
15
14
|
|
16
15
|
// src/service-worker.ts
|
17
16
|
var import_nice_grpc = __toESM(require_lib(), 1);
|
18
17
|
var import_nice_grpc_error_details = __toESM(require_lib2(), 1);
|
19
18
|
import { BroadcastChannel } from "worker_threads";
|
19
|
+
import { Piscina } from "piscina";
|
20
20
|
var started = false;
|
21
|
-
var options = command_line_args_default(optionDefinitions, { partial: true });
|
22
21
|
var unhandled;
|
23
22
|
process.on("uncaughtException", (err) => {
|
24
23
|
console.error("Uncaught Exception, please checking if await is properly used", err);
|
@@ -38,32 +37,64 @@ async function getConfig(request, context) {
|
|
38
37
|
await PluginManager.INSTANCE.configure(newConfig);
|
39
38
|
return newConfig;
|
40
39
|
}
|
41
|
-
var loader = async () => {
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
var loader = async (options) => {
|
41
|
+
if (options.target) {
|
42
|
+
const m = await import(options.target);
|
43
|
+
console.debug("Module loaded", m);
|
44
|
+
return m;
|
45
|
+
}
|
45
46
|
};
|
46
47
|
var configureChannel = new BroadcastChannel("configure_channel");
|
47
48
|
configureChannel.onmessage = (request) => {
|
48
49
|
getConfig(request);
|
49
50
|
};
|
50
|
-
async function start(request) {
|
51
|
+
async function start(request, options) {
|
51
52
|
if (started) {
|
52
53
|
return {};
|
53
54
|
}
|
54
55
|
freezeGlobalConfig();
|
55
56
|
try {
|
56
|
-
await loader();
|
57
|
+
await loader(options);
|
57
58
|
} catch (e) {
|
58
59
|
throw new import_nice_grpc.ServerError(import_nice_grpc.Status.INVALID_ARGUMENT, "Failed to load processor: " + errorString(e));
|
59
60
|
}
|
60
61
|
await PluginManager.INSTANCE.start(request);
|
61
|
-
started = true;
|
62
62
|
return {};
|
63
63
|
}
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
async function service_worker_default({ request }) {
|
65
|
+
if (!started) {
|
66
|
+
const { startRequest, configRequest, options } = Piscina.workerData;
|
67
|
+
if (startRequest) {
|
68
|
+
await start(startRequest, options);
|
69
|
+
}
|
70
|
+
if (configRequest) {
|
71
|
+
await getConfig(configRequest);
|
72
|
+
}
|
73
|
+
started = true;
|
74
|
+
console.info("new processor worker started");
|
75
|
+
}
|
76
|
+
if (unhandled) {
|
77
|
+
throw new import_nice_grpc_error_details.RichServerError(
|
78
|
+
import_nice_grpc.Status.UNAVAILABLE,
|
79
|
+
"Unhandled exception/rejection in previous request: " + errorString(unhandled),
|
80
|
+
[
|
81
|
+
import_nice_grpc_error_details.DebugInfo.fromPartial({
|
82
|
+
detail: unhandled.message,
|
83
|
+
stackEntries: unhandled.stack?.split("\n")
|
84
|
+
})
|
85
|
+
]
|
86
|
+
);
|
87
|
+
}
|
88
|
+
const result = await PluginManager.INSTANCE.processBinding(
|
89
|
+
request,
|
90
|
+
void 0
|
91
|
+
// PluginManager.INSTANCE.dbContextLocalStorage.getStore()
|
92
|
+
);
|
93
|
+
recordRuntimeInfo(result, request.handlerType);
|
94
|
+
return result;
|
95
|
+
}
|
68
96
|
import("node:process").then((p) => p.stdout.write(""));
|
97
|
+
export {
|
98
|
+
service_worker_default as default
|
99
|
+
};
|
69
100
|
//# sourceMappingURL=service-worker.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/service-worker.ts"],"sourcesContent":["import {\n DataBinding,\n Empty,\n HandlerType,\n PreparedData,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest\n} from '@sentio/protos'\nimport { CallContext, ServerError, Status } from 'nice-grpc'\nimport { PluginManager } from './plugin.js'\nimport commandLineArgs from 'command-line-args'\nimport { optionDefinitions } from './processor-runner.js'\nimport { errorString } from './utils.js'\nimport { freezeGlobalConfig } from './global-config.js'\nimport { DebugInfo, RichServerError } from 'nice-grpc-error-details'\nimport { recordRuntimeInfo } from './service.js'\nimport { BroadcastChannel } from 'worker_threads'\n\nlet started = false\
|
1
|
+
{"version":3,"sources":["../src/service-worker.ts"],"sourcesContent":["import {\n DataBinding,\n Empty,\n HandlerType,\n PreparedData,\n ProcessConfigRequest,\n ProcessConfigResponse,\n ProcessResult,\n StartRequest\n} from '@sentio/protos'\nimport { CallContext, ServerError, Status } from 'nice-grpc'\nimport { PluginManager } from './plugin.js'\nimport commandLineArgs from 'command-line-args'\nimport { optionDefinitions } from './processor-runner.js'\nimport { errorString } from './utils.js'\nimport { freezeGlobalConfig } from './global-config.js'\nimport { DebugInfo, RichServerError } from 'nice-grpc-error-details'\nimport { recordRuntimeInfo } from './service.js'\nimport { BroadcastChannel } from 'worker_threads'\nimport { Piscina } from 'piscina'\n\nlet started = false\n\nlet unhandled: Error | undefined\n\nprocess\n .on('uncaughtException', (err) => {\n console.error('Uncaught Exception, please checking if await is properly used', err)\n unhandled = err\n })\n .on('unhandledRejection', (reason, p) => {\n // @ts-ignore ignore invalid ens error\n if (reason?.message.startsWith('invalid ENS name (disallowed character: \"*\"')) {\n return\n }\n console.error('Unhandled Rejection, please checking if await is properly', reason)\n unhandled = reason as Error\n // shutdownServers(1)\n })\n\nasync function getConfig(request: ProcessConfigRequest, context?: CallContext): Promise<ProcessConfigResponse> {\n if (!started) {\n throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')\n }\n\n const newConfig = ProcessConfigResponse.fromPartial({})\n await PluginManager.INSTANCE.configure(newConfig)\n return newConfig\n}\n\nconst loader = async (options: any) => {\n if (options.target) {\n const m = await import(options.target)\n console.debug('Module loaded', m)\n return m\n }\n}\n\nconst configureChannel = new BroadcastChannel('configure_channel')\nconfigureChannel.onmessage = (request: ProcessConfigRequest) => {\n getConfig(request)\n}\n\nasync function start(request: StartRequest, options: any): Promise<Empty> {\n if (started) {\n return {}\n }\n freezeGlobalConfig()\n\n try {\n await loader(options)\n } catch (e) {\n throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))\n }\n\n await PluginManager.INSTANCE.start(request)\n return {}\n}\n\nexport default async function ({ request }: { request: DataBinding }) {\n if (!started) {\n const { startRequest, configRequest, options } = Piscina.workerData\n if (startRequest) {\n await start(startRequest, options)\n }\n if (configRequest) {\n await getConfig(configRequest)\n }\n started = true\n console.info('new processor worker started')\n }\n\n if (unhandled) {\n throw new RichServerError(\n Status.UNAVAILABLE,\n 'Unhandled exception/rejection in previous request: ' + errorString(unhandled),\n [\n DebugInfo.fromPartial({\n detail: unhandled.message,\n stackEntries: unhandled.stack?.split('\\n')\n })\n ]\n )\n }\n\n const result = await PluginManager.INSTANCE.processBinding(\n request,\n undefined\n // PluginManager.INSTANCE.dbContextLocalStorage.getStore()\n )\n recordRuntimeInfo(result, request.handlerType)\n return result\n}\n;import(\"node:process\").then((p) => p.stdout.write(\"\"));"],"mappings":";;;;;;;;;;;;;;;AAUA,uBAAiD;AAMjD,qCAA2C;AAE3C,SAAS,wBAAwB;AACjC,SAAS,eAAe;AAExB,IAAI,UAAU;AAEd,IAAI;AAEJ,QACG,GAAG,qBAAqB,CAAC,QAAQ;AAChC,UAAQ,MAAM,iEAAiE,GAAG;AAClF,cAAY;AACd,CAAC,EACA,GAAG,sBAAsB,CAAC,QAAQ,MAAM;AAEvC,MAAI,QAAQ,QAAQ,WAAW,6CAA6C,GAAG;AAC7E;AAAA,EACF;AACA,UAAQ,MAAM,6DAA6D,MAAM;AACjF,cAAY;AAEd,CAAC;AAEH,eAAe,UAAU,SAA+B,SAAuD;AAC7G,MAAI,CAAC,SAAS;AACZ,UAAM,IAAI,6BAAY,wBAAO,aAAa,sBAAsB;AAAA,EAClE;AAEA,QAAM,YAAY,sBAAsB,YAAY,CAAC,CAAC;AACtD,QAAM,cAAc,SAAS,UAAU,SAAS;AAChD,SAAO;AACT;AAEA,IAAM,SAAS,OAAO,YAAiB;AACrC,MAAI,QAAQ,QAAQ;AAClB,UAAM,IAAI,MAAM,OAAO,QAAQ;AAC/B,YAAQ,MAAM,iBAAiB,CAAC;AAChC,WAAO;AAAA,EACT;AACF;AAEA,IAAM,mBAAmB,IAAI,iBAAiB,mBAAmB;AACjE,iBAAiB,YAAY,CAAC,YAAkC;AAC9D,YAAU,OAAO;AACnB;AAEA,eAAe,MAAM,SAAuB,SAA8B;AACxE,MAAI,SAAS;AACX,WAAO,CAAC;AAAA,EACV;AACA,qBAAmB;AAEnB,MAAI;AACF,UAAM,OAAO,OAAO;AAAA,EACtB,SAAS,GAAG;AACV,UAAM,IAAI,6BAAY,wBAAO,kBAAkB,+BAA+B,YAAY,CAAC,CAAC;AAAA,EAC9F;AAEA,QAAM,cAAc,SAAS,MAAM,OAAO;AAC1C,SAAO,CAAC;AACV;AAEA,eAAO,uBAAwB,EAAE,QAAQ,GAA6B;AACpE,MAAI,CAAC,SAAS;AACZ,UAAM,EAAE,cAAc,eAAe,QAAQ,IAAI,QAAQ;AACzD,QAAI,cAAc;AAChB,YAAM,MAAM,cAAc,OAAO;AAAA,IACnC;AACA,QAAI,eAAe;AACjB,YAAM,UAAU,aAAa;AAAA,IAC/B;AACA,cAAU;AACV,YAAQ,KAAK,8BAA8B;AAAA,EAC7C;AAEA,MAAI,WAAW;AACb,UAAM,IAAI;AAAA,MACR,wBAAO;AAAA,MACP,wDAAwD,YAAY,SAAS;AAAA,MAC7E;AAAA,QACE,yCAAU,YAAY;AAAA,UACpB,QAAQ,UAAU;AAAA,UAClB,cAAc,UAAU,OAAO,MAAM,IAAI;AAAA,QAC3C,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAEA,QAAM,SAAS,MAAM,cAAc,SAAS;AAAA,IAC1C;AAAA,IACA;AAAA;AAAA,EAEF;AACA,oBAAkB,QAAQ,QAAQ,WAAW;AAC7C,SAAO;AACT;AACC,OAAO,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,EAAE,CAAC;","names":[]}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/test-processor.test.ts"],"sourcesContent":["import { Plugin, PluginManager } from './plugin.js'\nimport { DataBinding, HandlerType, ProcessResult } from './gen/processor/protos/processor.js'\n\nclass TestPlugin extends Plugin {\n async processBinding(request: DataBinding): Promise<ProcessResult> {\n return ProcessResult.fromPartial({\n states: {\n configUpdated: true\n }\n })\n }\n supportedHandlers = [HandlerType.UNKNOWN, HandlerType.APT_EVENT]\n}\n\nPluginManager.INSTANCE.plugins = []\nPluginManager.INSTANCE.register(new TestPlugin())\n;import(\"node:process\").then((p) => p.stdout.write(\"\"));"],"mappings":";;;;;;;;AAGA,IAAM,aAAN,cAAyB,OAAO;AAAA,EAC9B,MAAM,eAAe,SAA8C;AACjE,WAAO,cAAc,YAAY;AAAA,MAC/B,QAAQ;AAAA,QACN,eAAe;AAAA,MACjB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EACA,oBAAoB,mCAA2C;AACjE;AAEA,cAAc,SAAS,UAAU,CAAC;AAClC,cAAc,SAAS,SAAS,IAAI,WAAW,CAAC;AAC/C,OAAO,cAAc,EAAE,KAAK,CAAC,MAAM,EAAE,OAAO,MAAM,EAAE,CAAC;","names":[]}
|
package/package.json
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "@sentio/runtime",
|
3
|
-
"version": "2.57.12-rc.
|
3
|
+
"version": "2.57.12-rc.d",
|
4
4
|
"license": "Apache-2.0",
|
5
5
|
"type": "module",
|
6
6
|
"exports": {
|
7
|
-
".": "./lib/index.js"
|
8
|
-
"./service-worker": "./lib/service-worker.js"
|
7
|
+
".": "./lib/index.js"
|
9
8
|
},
|
10
9
|
"bin": {
|
11
10
|
"processor-runner": "./lib/processor-runner.js"
|
@@ -15,31 +14,7 @@
|
|
15
14
|
"!**/*.test.{js,ts}",
|
16
15
|
"!{lib,src}/tests"
|
17
16
|
],
|
18
|
-
"dependencies": {
|
19
|
-
"@grpc/grpc-js": "^1.9.14",
|
20
|
-
"@opentelemetry/exporter-metrics-otlp-grpc": "^0.57.0",
|
21
|
-
"@opentelemetry/exporter-prometheus": "^0.57.0",
|
22
|
-
"@opentelemetry/sdk-node": "^0.57.0",
|
23
|
-
"command-line-args": "^6.0.0",
|
24
|
-
"command-line-usage": "^7.0.1",
|
25
|
-
"ethers": "npm:@sentio/ethers@6.13.1-patch.5",
|
26
|
-
"fs-extra": "^11.2.0",
|
27
|
-
"google-protobuf": "^3.21.2",
|
28
|
-
"ix": "^7.0.0",
|
29
|
-
"long": "^5.2.3",
|
30
|
-
"nice-grpc": "^2.1.10",
|
31
|
-
"nice-grpc-client-middleware-retry": "^3.1.6",
|
32
|
-
"nice-grpc-common": "^2.0.2",
|
33
|
-
"nice-grpc-error-details": "^0.2.4",
|
34
|
-
"nice-grpc-opentelemetry": "^0.1.15",
|
35
|
-
"nice-grpc-prometheus": "^0.2.2",
|
36
|
-
"protobufjs": "^7.2.6",
|
37
|
-
"rxjs": "^7.8.1",
|
38
|
-
"utility-types": "^3.11.0",
|
39
|
-
"winston": "^3.11.0",
|
40
|
-
"piscina": "5.0.0-alpha.0",
|
41
|
-
"@sentio/protos": "2.57.12-rc.b"
|
42
|
-
},
|
17
|
+
"dependencies": {},
|
43
18
|
"devDependencies": {
|
44
19
|
"@types/command-line-args": "^5.2.3",
|
45
20
|
"@types/command-line-usage": "^5.0.4",
|
package/src/service-manager.ts
CHANGED
@@ -42,19 +42,13 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
42
42
|
// When there is unhandled error, stop process and return unavailable error
|
43
43
|
unhandled: Error
|
44
44
|
// private processorConfig: ProcessConfigResponse
|
45
|
-
private
|
45
|
+
private pool: Piscina<any, any>
|
46
46
|
|
47
47
|
constructor(
|
48
48
|
readonly options: any,
|
49
49
|
readonly loader: () => Promise<any>,
|
50
50
|
readonly shutdownHandler?: () => void
|
51
|
-
) {
|
52
|
-
this.pool = new Piscina({
|
53
|
-
maxThreads: options.worker,
|
54
|
-
minThreads: options.worker,
|
55
|
-
filename: new URL('./service-worker.js', import.meta.url).href
|
56
|
-
})
|
57
|
-
}
|
51
|
+
) {}
|
58
52
|
|
59
53
|
async *preprocessBindingsStream(requests: AsyncIterable<PreprocessStreamRequest>, context: CallContext) {
|
60
54
|
throw new Error('not supported')
|
@@ -72,15 +66,13 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
72
66
|
// also configure the main thread
|
73
67
|
const newConfig = ProcessConfigResponse.fromPartial({})
|
74
68
|
await PluginManager.INSTANCE.configure(newConfig)
|
69
|
+
this.pool.options.workerData = {
|
70
|
+
configRequest: request,
|
71
|
+
...this.pool.options.workerData
|
72
|
+
}
|
75
73
|
return newConfig
|
76
74
|
}
|
77
75
|
|
78
|
-
//
|
79
|
-
// async configure() {
|
80
|
-
// this.processorConfig = ProcessConfigResponse.fromPartial({})
|
81
|
-
// await PluginManager.INSTANCE.configure(this.processorConfig)
|
82
|
-
// }
|
83
|
-
|
84
76
|
async start(request: StartRequest, context: CallContext): Promise<Empty> {
|
85
77
|
if (this.started) {
|
86
78
|
return {}
|
@@ -92,9 +84,16 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
92
84
|
throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
|
93
85
|
}
|
94
86
|
|
95
|
-
|
96
|
-
|
97
|
-
|
87
|
+
this.pool = new Piscina({
|
88
|
+
maxThreads: this.options.worker,
|
89
|
+
minThreads: this.options.worker,
|
90
|
+
filename: new URL('./service-worker.js', import.meta.url).href.replaceAll('runtime/src', 'runtime/lib'),
|
91
|
+
argv: process.argv,
|
92
|
+
workerData: {
|
93
|
+
startRequest: request,
|
94
|
+
options: this.options
|
95
|
+
}
|
96
|
+
})
|
98
97
|
|
99
98
|
// also start the processor in main thread
|
100
99
|
await PluginManager.INSTANCE.start(request)
|
@@ -113,7 +112,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
113
112
|
async processBindings(request: ProcessBindingsRequest, options?: CallContext): Promise<ProcessBindingResponse> {
|
114
113
|
const promises = []
|
115
114
|
for (const binding of request.bindings) {
|
116
|
-
const promise = this.processBinding(binding
|
115
|
+
const promise = this.processBinding(binding)
|
117
116
|
if (GLOBAL_CONFIG.execution.sequential) {
|
118
117
|
await promise
|
119
118
|
}
|
@@ -140,11 +139,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
140
139
|
}
|
141
140
|
}
|
142
141
|
|
143
|
-
async processBinding(
|
144
|
-
request: DataBinding,
|
145
|
-
preparedData: PreparedData | undefined,
|
146
|
-
options?: CallContext
|
147
|
-
): Promise<ProcessResult> {
|
142
|
+
async processBinding(request: DataBinding): Promise<ProcessResult> {
|
148
143
|
if (!this.started) {
|
149
144
|
throw new ServerError(Status.UNAVAILABLE, 'Service Not started.')
|
150
145
|
}
|
@@ -161,7 +156,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
161
156
|
)
|
162
157
|
}
|
163
158
|
|
164
|
-
return await this.pool.run({ request
|
159
|
+
return await this.pool.run({ request })
|
165
160
|
}
|
166
161
|
|
167
162
|
async *processBindingsStream(requests: AsyncIterable<ProcessStreamRequest>, context: CallContext) {
|
@@ -206,7 +201,7 @@ export class ServiceManager implements ProcessorServiceImplementation {
|
|
206
201
|
// todo support db request
|
207
202
|
// const dbContext = contexts.new(request.processId, subject)
|
208
203
|
const start = Date.now()
|
209
|
-
await this.processBinding(binding
|
204
|
+
await this.processBinding(binding)
|
210
205
|
.then(async (result) => {
|
211
206
|
// await all pending db requests
|
212
207
|
// await dbContext.awaitPendings()
|
package/src/service-worker.ts
CHANGED
@@ -17,9 +17,9 @@ import { freezeGlobalConfig } from './global-config.js'
|
|
17
17
|
import { DebugInfo, RichServerError } from 'nice-grpc-error-details'
|
18
18
|
import { recordRuntimeInfo } from './service.js'
|
19
19
|
import { BroadcastChannel } from 'worker_threads'
|
20
|
+
import { Piscina } from 'piscina'
|
20
21
|
|
21
22
|
let started = false
|
22
|
-
const options = commandLineArgs(optionDefinitions, { partial: true })
|
23
23
|
|
24
24
|
let unhandled: Error | undefined
|
25
25
|
|
@@ -48,10 +48,12 @@ async function getConfig(request: ProcessConfigRequest, context?: CallContext):
|
|
48
48
|
return newConfig
|
49
49
|
}
|
50
50
|
|
51
|
-
const loader = async () => {
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
const loader = async (options: any) => {
|
52
|
+
if (options.target) {
|
53
|
+
const m = await import(options.target)
|
54
|
+
console.debug('Module loaded', m)
|
55
|
+
return m
|
56
|
+
}
|
55
57
|
}
|
56
58
|
|
57
59
|
const configureChannel = new BroadcastChannel('configure_channel')
|
@@ -59,40 +61,35 @@ configureChannel.onmessage = (request: ProcessConfigRequest) => {
|
|
59
61
|
getConfig(request)
|
60
62
|
}
|
61
63
|
|
62
|
-
async function start(request: StartRequest): Promise<Empty> {
|
64
|
+
async function start(request: StartRequest, options: any): Promise<Empty> {
|
63
65
|
if (started) {
|
64
66
|
return {}
|
65
67
|
}
|
66
68
|
freezeGlobalConfig()
|
67
69
|
|
68
70
|
try {
|
69
|
-
await loader()
|
71
|
+
await loader(options)
|
70
72
|
} catch (e) {
|
71
73
|
throw new ServerError(Status.INVALID_ARGUMENT, 'Failed to load processor: ' + errorString(e))
|
72
74
|
}
|
73
75
|
|
74
76
|
await PluginManager.INSTANCE.start(request)
|
75
|
-
started = true
|
76
|
-
return {}
|
77
|
-
}
|
78
|
-
|
79
|
-
const startChannel = new BroadcastChannel('start_channel')
|
80
|
-
startChannel.onmessage = (request: StartRequest) => {
|
81
|
-
start(request)
|
82
|
-
}
|
83
|
-
|
84
|
-
async function stop(request: Empty, context: CallContext): Promise<Empty> {
|
85
77
|
return {}
|
86
78
|
}
|
87
79
|
|
88
|
-
async function
|
89
|
-
request: DataBinding,
|
90
|
-
preparedData: PreparedData | undefined,
|
91
|
-
options?: CallContext
|
92
|
-
): Promise<ProcessResult> {
|
80
|
+
export default async function ({ request }: { request: DataBinding }) {
|
93
81
|
if (!started) {
|
94
|
-
|
82
|
+
const { startRequest, configRequest, options } = Piscina.workerData
|
83
|
+
if (startRequest) {
|
84
|
+
await start(startRequest, options)
|
85
|
+
}
|
86
|
+
if (configRequest) {
|
87
|
+
await getConfig(configRequest)
|
88
|
+
}
|
89
|
+
started = true
|
90
|
+
console.info('new processor worker started')
|
95
91
|
}
|
92
|
+
|
96
93
|
if (unhandled) {
|
97
94
|
throw new RichServerError(
|
98
95
|
Status.UNAVAILABLE,
|
@@ -108,7 +105,7 @@ async function processBinding(
|
|
108
105
|
|
109
106
|
const result = await PluginManager.INSTANCE.processBinding(
|
110
107
|
request,
|
111
|
-
|
108
|
+
undefined
|
112
109
|
// PluginManager.INSTANCE.dbContextLocalStorage.getStore()
|
113
110
|
)
|
114
111
|
recordRuntimeInfo(result, request.handlerType)
|
package/src/tsup.config.ts
CHANGED
@@ -6,7 +6,7 @@ export default defineConfig({
|
|
6
6
|
js: `import { createRequire as createRequireShim } from 'module'; const require = createRequireShim(import.meta.url);`
|
7
7
|
}
|
8
8
|
},
|
9
|
-
entry: ['src/index.ts', 'src/processor-runner.ts', 'src/service-worker.ts'],
|
9
|
+
entry: ['src/index.ts', 'src/processor-runner.ts', 'src/service-worker.ts', 'src/test-processor.test.ts'],
|
10
10
|
outDir: 'lib',
|
11
11
|
minify: process.env['BRANCH'] === 'release',
|
12
12
|
sourcemap: true,
|