@zucker-framework/script 1.0.0
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/LICENSE +201 -0
- package/NOTICE +22 -0
- package/THIRD_PARTY_NOTICES +4 -0
- package/dist/index.d.mts +213 -0
- package/dist/index.d.ts +213 -0
- package/dist/index.js +588 -0
- package/dist/index.mjs +549 -0
- package/package.json +39 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var index_exports = {};
|
|
33
|
+
__export(index_exports, {
|
|
34
|
+
CompositeExecutionContext: () => CompositeExecutionContext,
|
|
35
|
+
DefaultExecutionContext: () => DefaultExecutionContext,
|
|
36
|
+
ExposedScriptRegistry: () => ExposedScriptRegistry,
|
|
37
|
+
JavaScriptScriptFactory: () => JavaScriptScriptFactory,
|
|
38
|
+
ScriptUtils: () => ScriptUtils,
|
|
39
|
+
ZuckerScriptModule: () => ZuckerScriptModule,
|
|
40
|
+
bindInterface: () => bindInterface,
|
|
41
|
+
composeContexts: () => composeContexts,
|
|
42
|
+
createExecutionContext: () => createExecutionContext,
|
|
43
|
+
createSandbox: () => createSandbox,
|
|
44
|
+
scriptUtils: () => scriptUtils
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(index_exports);
|
|
47
|
+
|
|
48
|
+
// src/script-factory.ts
|
|
49
|
+
var import_common = require("@nestjs/common");
|
|
50
|
+
|
|
51
|
+
// src/sandbox.ts
|
|
52
|
+
var import_node_vm = __toESM(require("vm"));
|
|
53
|
+
var BLOCKED_GLOBALS = [
|
|
54
|
+
"process",
|
|
55
|
+
"require",
|
|
56
|
+
"module",
|
|
57
|
+
"exports",
|
|
58
|
+
"__filename",
|
|
59
|
+
"__dirname",
|
|
60
|
+
"globalThis",
|
|
61
|
+
"global",
|
|
62
|
+
"root",
|
|
63
|
+
"GLOBAL",
|
|
64
|
+
"setTimeout",
|
|
65
|
+
"setInterval",
|
|
66
|
+
"setImmediate",
|
|
67
|
+
"clearTimeout",
|
|
68
|
+
"clearInterval",
|
|
69
|
+
"clearImmediate",
|
|
70
|
+
"queueMicrotask"
|
|
71
|
+
];
|
|
72
|
+
function createSandbox(options) {
|
|
73
|
+
return {
|
|
74
|
+
execute(code, context = {}) {
|
|
75
|
+
const sandbox = {
|
|
76
|
+
...context,
|
|
77
|
+
console: {
|
|
78
|
+
log: /* @__PURE__ */ __name((...args) => console.log("[sandbox]", ...args), "log"),
|
|
79
|
+
warn: /* @__PURE__ */ __name((...args) => console.warn("[sandbox]", ...args), "warn"),
|
|
80
|
+
error: /* @__PURE__ */ __name((...args) => console.error("[sandbox]", ...args), "error")
|
|
81
|
+
},
|
|
82
|
+
require: createRestrictedRequire(options.allowedModules)
|
|
83
|
+
};
|
|
84
|
+
for (const key of BLOCKED_GLOBALS) {
|
|
85
|
+
sandbox[key] = void 0;
|
|
86
|
+
}
|
|
87
|
+
const vmContext = import_node_vm.default.createContext(sandbox);
|
|
88
|
+
import_node_vm.default.runInContext(`(function() {
|
|
89
|
+
var ObjProto = Object.getPrototypeOf({});
|
|
90
|
+
var FuncProto = Object.getPrototypeOf(function(){});
|
|
91
|
+
Object.defineProperty(ObjProto, 'constructor', { value: undefined, writable: false, configurable: false });
|
|
92
|
+
Object.defineProperty(FuncProto, 'constructor', { value: undefined, writable: false, configurable: false });
|
|
93
|
+
})()`, vmContext, {
|
|
94
|
+
timeout: 1e3
|
|
95
|
+
});
|
|
96
|
+
const wrappedCode = `(function(__args) { ${code} })(typeof __args !== 'undefined' ? __args : [])`;
|
|
97
|
+
return import_node_vm.default.runInContext(wrappedCode, vmContext, {
|
|
98
|
+
timeout: options.timeout,
|
|
99
|
+
filename: "sandbox-script.js"
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
__name(createSandbox, "createSandbox");
|
|
105
|
+
function createRestrictedRequire(allowedModules) {
|
|
106
|
+
if (allowedModules.length === 0) return void 0;
|
|
107
|
+
return (moduleName) => {
|
|
108
|
+
if (!allowedModules.includes(moduleName)) {
|
|
109
|
+
throw new Error(`Module not allowed in sandbox: ${moduleName}`);
|
|
110
|
+
}
|
|
111
|
+
return require(moduleName);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
__name(createRestrictedRequire, "createRestrictedRequire");
|
|
115
|
+
|
|
116
|
+
// src/interface-binding.ts
|
|
117
|
+
function bindInterface(script, interfaceKeys) {
|
|
118
|
+
let cachedExports = null;
|
|
119
|
+
const getExports = /* @__PURE__ */ __name(async () => {
|
|
120
|
+
if (!cachedExports) {
|
|
121
|
+
const result = await script.call();
|
|
122
|
+
if (typeof result !== "object" || result === null) {
|
|
123
|
+
throw new Error("Script must return an object with methods");
|
|
124
|
+
}
|
|
125
|
+
cachedExports = result;
|
|
126
|
+
}
|
|
127
|
+
return cachedExports;
|
|
128
|
+
}, "getExports");
|
|
129
|
+
return new Proxy({}, {
|
|
130
|
+
get(_target, prop) {
|
|
131
|
+
if (!interfaceKeys.includes(prop)) {
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
return async (...args) => {
|
|
135
|
+
const exports2 = await getExports();
|
|
136
|
+
const fn = exports2[prop];
|
|
137
|
+
if (typeof fn !== "function") {
|
|
138
|
+
throw new Error(`Script does not export method: ${prop}`);
|
|
139
|
+
}
|
|
140
|
+
return fn(...args);
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
__name(bindInterface, "bindInterface");
|
|
146
|
+
|
|
147
|
+
// src/script-factory.ts
|
|
148
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
149
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
150
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
151
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
152
|
+
} else {
|
|
153
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
154
|
+
if (d = decorators[i]) {
|
|
155
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
160
|
+
}
|
|
161
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
162
|
+
var JavaScriptScriptFactory = class _JavaScriptScriptFactory {
|
|
163
|
+
static {
|
|
164
|
+
__name(this, "JavaScriptScriptFactory");
|
|
165
|
+
}
|
|
166
|
+
logger = new import_common.Logger(_JavaScriptScriptFactory.name);
|
|
167
|
+
defaultOptions = {
|
|
168
|
+
timeout: 5e3,
|
|
169
|
+
allowedModules: []
|
|
170
|
+
};
|
|
171
|
+
accessControl = {
|
|
172
|
+
allowedModules: /* @__PURE__ */ new Set(),
|
|
173
|
+
deniedPatterns: /* @__PURE__ */ new Set([
|
|
174
|
+
"*"
|
|
175
|
+
]),
|
|
176
|
+
allowedPatterns: /* @__PURE__ */ new Set()
|
|
177
|
+
};
|
|
178
|
+
setDefaultOptions(options) {
|
|
179
|
+
this.defaultOptions = {
|
|
180
|
+
...this.defaultOptions,
|
|
181
|
+
...options
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/** 添加允许的模块 — 对齐 jetlinks allows() */
|
|
185
|
+
allowModule(moduleName) {
|
|
186
|
+
this.accessControl.allowedModules.add(moduleName);
|
|
187
|
+
}
|
|
188
|
+
/** 移除允许的模块 — 对齐 jetlinks denies() */
|
|
189
|
+
denyModule(moduleName) {
|
|
190
|
+
this.accessControl.allowedModules.delete(moduleName);
|
|
191
|
+
}
|
|
192
|
+
/** 添加允许的模式(正则) — 对齐 jetlinks allowsPattern() */
|
|
193
|
+
allowPattern(pattern) {
|
|
194
|
+
this.accessControl.allowedPatterns.add(pattern);
|
|
195
|
+
}
|
|
196
|
+
/** 添加拒绝的模式(正则) — 对齐 jetlinks deniesPattern() */
|
|
197
|
+
denyPattern(pattern) {
|
|
198
|
+
this.accessControl.deniedPatterns.add(pattern);
|
|
199
|
+
}
|
|
200
|
+
/** 检查模块是否被拒绝 — 对齐 jetlinks isDenied() */
|
|
201
|
+
isDenied(moduleName) {
|
|
202
|
+
if (this.accessControl.allowedModules.has(moduleName)) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
for (const pattern of this.accessControl.allowedPatterns) {
|
|
206
|
+
if (new RegExp(pattern).test(moduleName)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (this.accessControl.deniedPatterns.has("*")) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
for (const pattern of this.accessControl.deniedPatterns) {
|
|
214
|
+
if (new RegExp(pattern).test(moduleName)) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* 将脚本返回值转换为纯 JS 类型 — 对齐 jetlinks AbstractScriptFactory.convertToJavaType()。
|
|
222
|
+
* 确保 Map、Set 等非 JSON 安全类型被转为 POJO/Array。
|
|
223
|
+
*/
|
|
224
|
+
convertToJavaType(data) {
|
|
225
|
+
if (data === void 0 || data === null) {
|
|
226
|
+
return data;
|
|
227
|
+
}
|
|
228
|
+
if (data instanceof Map) {
|
|
229
|
+
const obj = {};
|
|
230
|
+
for (const [key, value] of data) {
|
|
231
|
+
obj[String(key)] = this.convertToJavaType(value);
|
|
232
|
+
}
|
|
233
|
+
return obj;
|
|
234
|
+
}
|
|
235
|
+
if (data instanceof Set) {
|
|
236
|
+
return Array.from(data).map((v) => this.convertToJavaType(v));
|
|
237
|
+
}
|
|
238
|
+
if (Array.isArray(data)) {
|
|
239
|
+
return data.map((v) => this.convertToJavaType(v));
|
|
240
|
+
}
|
|
241
|
+
return data;
|
|
242
|
+
}
|
|
243
|
+
async compile(script) {
|
|
244
|
+
return this.compileExpose(script, {});
|
|
245
|
+
}
|
|
246
|
+
async compileExpose(script, expose) {
|
|
247
|
+
this.logger.debug(`Compiling script: ${script.name}`);
|
|
248
|
+
const allowedModules = Array.from(this.accessControl.allowedModules);
|
|
249
|
+
const sandboxOptions = {
|
|
250
|
+
...this.defaultOptions,
|
|
251
|
+
name: script.name,
|
|
252
|
+
allowedModules: [
|
|
253
|
+
...this.defaultOptions.allowedModules,
|
|
254
|
+
...allowedModules
|
|
255
|
+
]
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
call: /* @__PURE__ */ __name(async (...args) => {
|
|
259
|
+
const sandbox = createSandbox(sandboxOptions);
|
|
260
|
+
return sandbox.execute(script.content, {
|
|
261
|
+
...expose,
|
|
262
|
+
__args: args
|
|
263
|
+
});
|
|
264
|
+
}, "call"),
|
|
265
|
+
callWithContext: /* @__PURE__ */ __name(async (context, ...args) => {
|
|
266
|
+
const sandbox = createSandbox(sandboxOptions);
|
|
267
|
+
return sandbox.execute(script.content, {
|
|
268
|
+
...expose,
|
|
269
|
+
...context,
|
|
270
|
+
__args: args
|
|
271
|
+
});
|
|
272
|
+
}, "callWithContext"),
|
|
273
|
+
callWithExecutionContext: /* @__PURE__ */ __name(async (execCtx) => {
|
|
274
|
+
const sandbox = createSandbox(sandboxOptions);
|
|
275
|
+
return sandbox.execute(script.content, {
|
|
276
|
+
...expose,
|
|
277
|
+
...execCtx.toRecord()
|
|
278
|
+
});
|
|
279
|
+
}, "callWithExecutionContext")
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async bind(script, context) {
|
|
283
|
+
return this.compileExpose(script, context);
|
|
284
|
+
}
|
|
285
|
+
async createProxy(script, interfaceKeys) {
|
|
286
|
+
const compiled = await this.compile(script);
|
|
287
|
+
return bindInterface(compiled, interfaceKeys);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
JavaScriptScriptFactory = _ts_decorate([
|
|
291
|
+
(0, import_common.Injectable)()
|
|
292
|
+
], JavaScriptScriptFactory);
|
|
293
|
+
|
|
294
|
+
// src/exposed-script.ts
|
|
295
|
+
var ExposedScriptRegistry = class {
|
|
296
|
+
static {
|
|
297
|
+
__name(this, "ExposedScriptRegistry");
|
|
298
|
+
}
|
|
299
|
+
methods = /* @__PURE__ */ new Map();
|
|
300
|
+
register(name, fn) {
|
|
301
|
+
this.methods.set(name, fn);
|
|
302
|
+
}
|
|
303
|
+
unregister(name) {
|
|
304
|
+
this.methods.delete(name);
|
|
305
|
+
}
|
|
306
|
+
getExposedMethods() {
|
|
307
|
+
const result = {};
|
|
308
|
+
for (const [name, fn] of this.methods) {
|
|
309
|
+
result[name] = fn;
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
has(name) {
|
|
314
|
+
return this.methods.has(name);
|
|
315
|
+
}
|
|
316
|
+
getNames() {
|
|
317
|
+
return Array.from(this.methods.keys());
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/execution-context.ts
|
|
322
|
+
var DefaultExecutionContext = class {
|
|
323
|
+
static {
|
|
324
|
+
__name(this, "DefaultExecutionContext");
|
|
325
|
+
}
|
|
326
|
+
layers;
|
|
327
|
+
selfLayer;
|
|
328
|
+
constructor(...contexts) {
|
|
329
|
+
this.layers = contexts.map((c) => c);
|
|
330
|
+
this.selfLayer = /* @__PURE__ */ new Map();
|
|
331
|
+
}
|
|
332
|
+
getAttribute(name) {
|
|
333
|
+
for (const layer of this.layers) {
|
|
334
|
+
if (layer instanceof Map) {
|
|
335
|
+
if (layer.has(name)) return layer.get(name);
|
|
336
|
+
} else {
|
|
337
|
+
if (name in layer) return layer[name];
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (this.selfLayer.has(name)) return this.selfLayer.get(name);
|
|
341
|
+
return void 0;
|
|
342
|
+
}
|
|
343
|
+
setAttribute(name, value) {
|
|
344
|
+
this.selfLayer.set(name, value);
|
|
345
|
+
}
|
|
346
|
+
removeAttribute(name) {
|
|
347
|
+
const existing = this.selfLayer.get(name);
|
|
348
|
+
this.selfLayer.delete(name);
|
|
349
|
+
return existing;
|
|
350
|
+
}
|
|
351
|
+
hasAttribute(name) {
|
|
352
|
+
for (const layer of this.layers) {
|
|
353
|
+
if (layer instanceof Map) {
|
|
354
|
+
if (layer.has(name)) return true;
|
|
355
|
+
} else {
|
|
356
|
+
if (name in layer) return true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return this.selfLayer.has(name);
|
|
360
|
+
}
|
|
361
|
+
merge(other) {
|
|
362
|
+
return new CompositeExecutionContext(this, other);
|
|
363
|
+
}
|
|
364
|
+
toRecord() {
|
|
365
|
+
const result = {};
|
|
366
|
+
for (const entry of this.selfLayer.entries()) {
|
|
367
|
+
result[entry[0]] = entry[1];
|
|
368
|
+
}
|
|
369
|
+
for (let i = this.layers.length - 1; i >= 0; i--) {
|
|
370
|
+
const layer = this.layers[i];
|
|
371
|
+
if (layer instanceof Map) {
|
|
372
|
+
for (const entry of layer.entries()) {
|
|
373
|
+
result[entry[0]] = entry[1];
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
Object.assign(result, layer);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return result;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
var CompositeExecutionContext = class {
|
|
383
|
+
static {
|
|
384
|
+
__name(this, "CompositeExecutionContext");
|
|
385
|
+
}
|
|
386
|
+
contexts;
|
|
387
|
+
constructor(...contexts) {
|
|
388
|
+
this.contexts = [
|
|
389
|
+
...contexts
|
|
390
|
+
];
|
|
391
|
+
}
|
|
392
|
+
getAttribute(name) {
|
|
393
|
+
for (const ctx of this.contexts) {
|
|
394
|
+
if (ctx.hasAttribute(name)) {
|
|
395
|
+
return ctx.getAttribute(name);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return void 0;
|
|
399
|
+
}
|
|
400
|
+
setAttribute(name, value) {
|
|
401
|
+
if (this.contexts.length > 0) {
|
|
402
|
+
this.contexts[this.contexts.length - 1].setAttribute(name, value);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
removeAttribute(name) {
|
|
406
|
+
for (const ctx of this.contexts) {
|
|
407
|
+
if (ctx.hasAttribute(name)) {
|
|
408
|
+
return ctx.removeAttribute(name);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return void 0;
|
|
412
|
+
}
|
|
413
|
+
hasAttribute(name) {
|
|
414
|
+
return this.contexts.some((ctx) => ctx.hasAttribute(name));
|
|
415
|
+
}
|
|
416
|
+
merge(other) {
|
|
417
|
+
this.contexts = [
|
|
418
|
+
...this.contexts,
|
|
419
|
+
other
|
|
420
|
+
];
|
|
421
|
+
return this;
|
|
422
|
+
}
|
|
423
|
+
toRecord() {
|
|
424
|
+
const result = {};
|
|
425
|
+
for (let i = this.contexts.length - 1; i >= 0; i--) {
|
|
426
|
+
Object.assign(result, this.contexts[i].toRecord());
|
|
427
|
+
}
|
|
428
|
+
return result;
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
function createExecutionContext(...contexts) {
|
|
432
|
+
return new DefaultExecutionContext(...contexts);
|
|
433
|
+
}
|
|
434
|
+
__name(createExecutionContext, "createExecutionContext");
|
|
435
|
+
function composeContexts(...contexts) {
|
|
436
|
+
return new CompositeExecutionContext(...contexts);
|
|
437
|
+
}
|
|
438
|
+
__name(composeContexts, "composeContexts");
|
|
439
|
+
|
|
440
|
+
// src/script-utils.ts
|
|
441
|
+
var import_node_crypto = require("crypto");
|
|
442
|
+
var ScriptUtils = class {
|
|
443
|
+
static {
|
|
444
|
+
__name(this, "ScriptUtils");
|
|
445
|
+
}
|
|
446
|
+
/** 当前时间戳(毫秒) */
|
|
447
|
+
now() {
|
|
448
|
+
return Date.now();
|
|
449
|
+
}
|
|
450
|
+
/** 格式化当前时间 */
|
|
451
|
+
nowFormatted(format) {
|
|
452
|
+
const d = /* @__PURE__ */ new Date();
|
|
453
|
+
if (!format) return d.toISOString();
|
|
454
|
+
return format.replace("yyyy", String(d.getFullYear())).replace("MM", String(d.getMonth() + 1).padStart(2, "0")).replace("dd", String(d.getDate()).padStart(2, "0")).replace("HH", String(d.getHours()).padStart(2, "0")).replace("mm", String(d.getMinutes()).padStart(2, "0")).replace("ss", String(d.getSeconds()).padStart(2, "0"));
|
|
455
|
+
}
|
|
456
|
+
/** 创建新的 Map(对象) */
|
|
457
|
+
newMap() {
|
|
458
|
+
return {};
|
|
459
|
+
}
|
|
460
|
+
/** 创建新的数组 */
|
|
461
|
+
newList() {
|
|
462
|
+
return [];
|
|
463
|
+
}
|
|
464
|
+
/** 序列化为 JSON 字符串 */
|
|
465
|
+
toJsonString(obj) {
|
|
466
|
+
return JSON.stringify(obj);
|
|
467
|
+
}
|
|
468
|
+
/** 解析 JSON 字符串 */
|
|
469
|
+
parseJson(json) {
|
|
470
|
+
return JSON.parse(json);
|
|
471
|
+
}
|
|
472
|
+
/** 转换为 Java/原生类型(identity in JS) */
|
|
473
|
+
toJavaType(obj) {
|
|
474
|
+
return obj;
|
|
475
|
+
}
|
|
476
|
+
/** 求最大值(支持数组、嵌套集合) */
|
|
477
|
+
max(...params) {
|
|
478
|
+
let maxVal = void 0;
|
|
479
|
+
for (let param of params) {
|
|
480
|
+
if (param && typeof param === "object" && !Array.isArray(param)) {
|
|
481
|
+
param = Object.values(param);
|
|
482
|
+
}
|
|
483
|
+
if (Array.isArray(param)) {
|
|
484
|
+
param = this.max(...param);
|
|
485
|
+
}
|
|
486
|
+
if (maxVal === void 0) {
|
|
487
|
+
maxVal = param;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (Number(param) > Number(maxVal)) {
|
|
491
|
+
maxVal = param;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return maxVal;
|
|
495
|
+
}
|
|
496
|
+
/** 求最小值(支持数组、嵌套集合) */
|
|
497
|
+
min(...params) {
|
|
498
|
+
let minVal = void 0;
|
|
499
|
+
for (let param of params) {
|
|
500
|
+
if (param && typeof param === "object" && !Array.isArray(param)) {
|
|
501
|
+
param = Object.values(param);
|
|
502
|
+
}
|
|
503
|
+
if (Array.isArray(param)) {
|
|
504
|
+
param = this.min(...param);
|
|
505
|
+
}
|
|
506
|
+
if (minVal === void 0) {
|
|
507
|
+
minVal = param;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (Number(param) < Number(minVal)) {
|
|
511
|
+
minVal = param;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return minVal;
|
|
515
|
+
}
|
|
516
|
+
/** SHA1 摘要(hex) */
|
|
517
|
+
sha1Hex(str) {
|
|
518
|
+
if (str == null) return null;
|
|
519
|
+
return (0, import_node_crypto.createHash)("sha1").update(str).digest("hex");
|
|
520
|
+
}
|
|
521
|
+
/** MD5 摘要(hex) */
|
|
522
|
+
md5Hex(str) {
|
|
523
|
+
if (str == null) return null;
|
|
524
|
+
return (0, import_node_crypto.createHash)("md5").update(str).digest("hex");
|
|
525
|
+
}
|
|
526
|
+
/** 创建指定大小的 Buffer */
|
|
527
|
+
newByteArray(size) {
|
|
528
|
+
return Buffer.alloc(size);
|
|
529
|
+
}
|
|
530
|
+
/** 创建指定大小的 Int32 数组 — 对齐 jetlinks Utils.newIntArray() */
|
|
531
|
+
newIntArray(size) {
|
|
532
|
+
return new Int32Array(size);
|
|
533
|
+
}
|
|
534
|
+
/** 创建指定大小的 BigInt64 数组 — 对齐 jetlinks Utils.newLongArray() */
|
|
535
|
+
newLongArray(size) {
|
|
536
|
+
return new BigInt64Array(size);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
var scriptUtils = new ScriptUtils();
|
|
540
|
+
|
|
541
|
+
// src/script.module.ts
|
|
542
|
+
var import_common2 = require("@nestjs/common");
|
|
543
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
544
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
545
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
546
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
547
|
+
} else {
|
|
548
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
549
|
+
if (d = decorators[i]) {
|
|
550
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
555
|
+
}
|
|
556
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
557
|
+
var ZuckerScriptModule = class {
|
|
558
|
+
static {
|
|
559
|
+
__name(this, "ZuckerScriptModule");
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
ZuckerScriptModule = _ts_decorate2([
|
|
563
|
+
(0, import_common2.Global)(),
|
|
564
|
+
(0, import_common2.Module)({
|
|
565
|
+
providers: [
|
|
566
|
+
JavaScriptScriptFactory,
|
|
567
|
+
ExposedScriptRegistry
|
|
568
|
+
],
|
|
569
|
+
exports: [
|
|
570
|
+
JavaScriptScriptFactory,
|
|
571
|
+
ExposedScriptRegistry
|
|
572
|
+
]
|
|
573
|
+
})
|
|
574
|
+
], ZuckerScriptModule);
|
|
575
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
576
|
+
0 && (module.exports = {
|
|
577
|
+
CompositeExecutionContext,
|
|
578
|
+
DefaultExecutionContext,
|
|
579
|
+
ExposedScriptRegistry,
|
|
580
|
+
JavaScriptScriptFactory,
|
|
581
|
+
ScriptUtils,
|
|
582
|
+
ZuckerScriptModule,
|
|
583
|
+
bindInterface,
|
|
584
|
+
composeContexts,
|
|
585
|
+
createExecutionContext,
|
|
586
|
+
createSandbox,
|
|
587
|
+
scriptUtils
|
|
588
|
+
});
|