@api3/commons 0.1.0 → 0.3.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/README.md +17 -19
- package/dist/eslint/jest.d.ts +1 -0
- package/dist/eslint/jest.js +1 -0
- package/dist/eslint/jest.js.map +1 -1
- package/dist/eslint/react.d.ts +0 -1
- package/dist/eslint/react.js +0 -1
- package/dist/eslint/react.js.map +1 -1
- package/dist/eslint/universal.d.ts +1 -5
- package/dist/eslint/universal.js +2 -19
- package/dist/eslint/universal.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -2
- package/dist/index.js.map +1 -1
- package/dist/logger/index.d.ts +9 -20
- package/dist/logger/index.d.ts.map +1 -1
- package/dist/logger/index.js +28 -12
- package/dist/logger/index.js.map +1 -1
- package/dist/processing/index.d.ts +4 -0
- package/dist/processing/index.d.ts.map +1 -0
- package/dist/processing/index.js +20 -0
- package/dist/processing/index.js.map +1 -0
- package/dist/processing/processing.d.ts +39 -0
- package/dist/processing/processing.d.ts.map +1 -0
- package/dist/processing/processing.js +122 -0
- package/dist/processing/processing.js.map +1 -0
- package/dist/processing/schema.d.ts +3 -0
- package/dist/processing/schema.d.ts.map +1 -0
- package/dist/processing/schema.js +12 -0
- package/dist/processing/schema.js.map +1 -0
- package/dist/processing/unsafe-evaluate.d.ts +50 -0
- package/dist/processing/unsafe-evaluate.d.ts.map +1 -0
- package/dist/processing/unsafe-evaluate.js +178 -0
- package/dist/processing/unsafe-evaluate.js.map +1 -0
- package/dist/processing/vm-timers.d.ts +21 -0
- package/dist/processing/vm-timers.d.ts.map +1 -0
- package/dist/processing/vm-timers.js +54 -0
- package/dist/processing/vm-timers.js.map +1 -0
- package/package.json +8 -11
- package/src/eslint/README.md +0 -5
- package/src/eslint/jest.js +1 -0
- package/src/eslint/react.js +0 -1
- package/src/eslint/universal.js +2 -20
- package/src/index.ts +3 -2
- package/src/logger/README.md +1 -2
- package/src/logger/index.ts +37 -15
- package/src/processing/README.md +45 -0
- package/src/processing/index.ts +3 -0
- package/src/processing/processing.test.ts +272 -0
- package/src/processing/processing.ts +160 -0
- package/src/processing/schema.ts +10 -0
- package/src/processing/unsafe-evaluate.test.ts +103 -0
- package/src/processing/unsafe-evaluate.ts +178 -0
- package/src/processing/vm-timers.ts +58 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evaluates the provided code in a new VM context with the specified global variables.
|
|
3
|
+
*
|
|
4
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
5
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
6
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
7
|
+
* reviewed.
|
|
8
|
+
*
|
|
9
|
+
* @param code The JavaScript code to evaluate.
|
|
10
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
11
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
12
|
+
*
|
|
13
|
+
* @returns The result of the evaluated code.
|
|
14
|
+
*
|
|
15
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
*
|
|
19
|
+
*const result = unsafeEvaluate('const output = input + 1;', { input: 1 }, 1000);
|
|
20
|
+
*console.log(result); // Outputs: 2
|
|
21
|
+
*/
|
|
22
|
+
export declare const unsafeEvaluate: (code: string, globalVariables: Record<string, unknown>, timeout: number) => unknown;
|
|
23
|
+
/**
|
|
24
|
+
* Asynchronously evaluates the provided code in a new VM context with the specified global variables.
|
|
25
|
+
*
|
|
26
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
27
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
28
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
29
|
+
* reviewed.
|
|
30
|
+
*
|
|
31
|
+
* @param code The JavaScript code to evaluate. The code should call the `resolve` method to return the result of the
|
|
32
|
+
* evaluation. You may use async/await syntax in the code.
|
|
33
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
34
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
35
|
+
*
|
|
36
|
+
* @returns The result of the evaluated code wrapped in a Promise.
|
|
37
|
+
*
|
|
38
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
*const result = await unsafeEvaluateAsync(
|
|
43
|
+
* "const output = {...input, c: 'some-value'}; resolve(output);",
|
|
44
|
+
* { input: { a: true, b: 123 } },
|
|
45
|
+
* 5000
|
|
46
|
+
*);
|
|
47
|
+
*console.log(result); // Outputs: { a: true, b: 123, c: 'some-value' }
|
|
48
|
+
*/
|
|
49
|
+
export declare const unsafeEvaluateAsync: (code: string, globalVariables: Record<string, unknown>, timeout: number) => Promise<unknown>;
|
|
50
|
+
//# sourceMappingURL=unsafe-evaluate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unsafe-evaluate.d.ts","sourceRoot":"","sources":["../../src/processing/unsafe-evaluate.ts"],"names":[],"mappings":"AA+EA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,cAAc,SAAU,MAAM,mBAAmB,OAAO,MAAM,EAAE,OAAO,CAAC,WAAW,MAAM,YAarG,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,mBAAmB,SAAgB,MAAM,mBAAmB,OAAO,MAAM,EAAE,OAAO,CAAC,WAAW,MAAM,qBAoChH,CAAC"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.unsafeEvaluateAsync = exports.unsafeEvaluate = void 0;
|
|
7
|
+
/* eslint-disable camelcase */
|
|
8
|
+
const node_assert_1 = __importDefault(require("node:assert"));
|
|
9
|
+
const node_async_hooks_1 = __importDefault(require("node:async_hooks"));
|
|
10
|
+
const node_buffer_1 = __importDefault(require("node:buffer"));
|
|
11
|
+
const node_child_process_1 = __importDefault(require("node:child_process"));
|
|
12
|
+
const node_cluster_1 = __importDefault(require("node:cluster"));
|
|
13
|
+
const node_console_1 = __importDefault(require("node:console"));
|
|
14
|
+
const node_constants_1 = __importDefault(require("node:constants"));
|
|
15
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
16
|
+
const node_dgram_1 = __importDefault(require("node:dgram"));
|
|
17
|
+
const node_dns_1 = __importDefault(require("node:dns"));
|
|
18
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
19
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
20
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
21
|
+
const node_http2_1 = __importDefault(require("node:http2"));
|
|
22
|
+
const node_https_1 = __importDefault(require("node:https"));
|
|
23
|
+
const node_inspector_1 = __importDefault(require("node:inspector"));
|
|
24
|
+
const node_module_1 = __importDefault(require("node:module"));
|
|
25
|
+
const node_net_1 = __importDefault(require("node:net"));
|
|
26
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
27
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
28
|
+
const node_perf_hooks_1 = __importDefault(require("node:perf_hooks"));
|
|
29
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
30
|
+
const node_readline_1 = __importDefault(require("node:readline"));
|
|
31
|
+
const node_repl_1 = __importDefault(require("node:repl"));
|
|
32
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
33
|
+
const node_string_decoder_1 = __importDefault(require("node:string_decoder"));
|
|
34
|
+
const node_timers_1 = __importDefault(require("node:timers"));
|
|
35
|
+
const node_tls_1 = __importDefault(require("node:tls"));
|
|
36
|
+
const node_trace_events_1 = __importDefault(require("node:trace_events"));
|
|
37
|
+
const node_tty_1 = __importDefault(require("node:tty"));
|
|
38
|
+
const node_url_1 = __importDefault(require("node:url"));
|
|
39
|
+
const node_util_1 = __importDefault(require("node:util"));
|
|
40
|
+
const node_v8_1 = __importDefault(require("node:v8"));
|
|
41
|
+
const node_vm_1 = __importDefault(require("node:vm"));
|
|
42
|
+
const node_worker_threads_1 = __importDefault(require("node:worker_threads"));
|
|
43
|
+
const node_zlib_1 = __importDefault(require("node:zlib"));
|
|
44
|
+
const vm_timers_1 = require("./vm-timers");
|
|
45
|
+
const builtInNodeModules = {
|
|
46
|
+
assert: node_assert_1.default,
|
|
47
|
+
async_hooks: node_async_hooks_1.default,
|
|
48
|
+
buffer: node_buffer_1.default,
|
|
49
|
+
child_process: node_child_process_1.default,
|
|
50
|
+
cluster: node_cluster_1.default,
|
|
51
|
+
console: node_console_1.default,
|
|
52
|
+
constants: node_constants_1.default,
|
|
53
|
+
crypto: node_crypto_1.default,
|
|
54
|
+
dgram: node_dgram_1.default,
|
|
55
|
+
dns: node_dns_1.default,
|
|
56
|
+
events: node_events_1.default,
|
|
57
|
+
fs: node_fs_1.default,
|
|
58
|
+
http: node_http_1.default,
|
|
59
|
+
http2: node_http2_1.default,
|
|
60
|
+
https: node_https_1.default,
|
|
61
|
+
inspector: node_inspector_1.default,
|
|
62
|
+
module: node_module_1.default,
|
|
63
|
+
net: node_net_1.default,
|
|
64
|
+
os: node_os_1.default,
|
|
65
|
+
path: node_path_1.default,
|
|
66
|
+
perf_hooks: node_perf_hooks_1.default,
|
|
67
|
+
process: node_process_1.default,
|
|
68
|
+
readline: node_readline_1.default,
|
|
69
|
+
repl: node_repl_1.default,
|
|
70
|
+
stream: node_stream_1.default,
|
|
71
|
+
string_decoder: node_string_decoder_1.default,
|
|
72
|
+
timers: node_timers_1.default,
|
|
73
|
+
tls: node_tls_1.default,
|
|
74
|
+
trace_events: node_trace_events_1.default,
|
|
75
|
+
tty: node_tty_1.default,
|
|
76
|
+
url: node_url_1.default,
|
|
77
|
+
util: node_util_1.default,
|
|
78
|
+
v8: node_v8_1.default,
|
|
79
|
+
vm: node_vm_1.default,
|
|
80
|
+
worker_threads: node_worker_threads_1.default,
|
|
81
|
+
zlib: node_zlib_1.default,
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Evaluates the provided code in a new VM context with the specified global variables.
|
|
85
|
+
*
|
|
86
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
87
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
88
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
89
|
+
* reviewed.
|
|
90
|
+
*
|
|
91
|
+
* @param code The JavaScript code to evaluate.
|
|
92
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
93
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
94
|
+
*
|
|
95
|
+
* @returns The result of the evaluated code.
|
|
96
|
+
*
|
|
97
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
*
|
|
101
|
+
*const result = unsafeEvaluate('const output = input + 1;', { input: 1 }, 1000);
|
|
102
|
+
*console.log(result); // Outputs: 2
|
|
103
|
+
*/
|
|
104
|
+
const unsafeEvaluate = (code, globalVariables, timeout) => {
|
|
105
|
+
const vmContext = {
|
|
106
|
+
...globalVariables,
|
|
107
|
+
...builtInNodeModules,
|
|
108
|
+
deferredOutput: undefined,
|
|
109
|
+
};
|
|
110
|
+
node_vm_1.default.runInNewContext(`${code}; deferredOutput = output;`, vmContext, {
|
|
111
|
+
displayErrors: true,
|
|
112
|
+
timeout,
|
|
113
|
+
});
|
|
114
|
+
return vmContext.deferredOutput;
|
|
115
|
+
};
|
|
116
|
+
exports.unsafeEvaluate = unsafeEvaluate;
|
|
117
|
+
/**
|
|
118
|
+
* Asynchronously evaluates the provided code in a new VM context with the specified global variables.
|
|
119
|
+
*
|
|
120
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
121
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
122
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
123
|
+
* reviewed.
|
|
124
|
+
*
|
|
125
|
+
* @param code The JavaScript code to evaluate. The code should call the `resolve` method to return the result of the
|
|
126
|
+
* evaluation. You may use async/await syntax in the code.
|
|
127
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
128
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
129
|
+
*
|
|
130
|
+
* @returns The result of the evaluated code wrapped in a Promise.
|
|
131
|
+
*
|
|
132
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
*
|
|
136
|
+
*const result = await unsafeEvaluateAsync(
|
|
137
|
+
* "const output = {...input, c: 'some-value'}; resolve(output);",
|
|
138
|
+
* { input: { a: true, b: 123 } },
|
|
139
|
+
* 5000
|
|
140
|
+
*);
|
|
141
|
+
*console.log(result); // Outputs: { a: true, b: 123, c: 'some-value' }
|
|
142
|
+
*/
|
|
143
|
+
const unsafeEvaluateAsync = async (code, globalVariables, timeout) => {
|
|
144
|
+
let vmReject;
|
|
145
|
+
// Make sure the timeout is applied. When the processing snippet uses setTimeout or setInterval, the timeout option
|
|
146
|
+
// from VM is broken. See: https://github.com/nodejs/node/issues/3020.
|
|
147
|
+
//
|
|
148
|
+
// We need to manually clear all timers and reject the processing manually.
|
|
149
|
+
const timeoutTimer = setTimeout(() => {
|
|
150
|
+
vmReject(new Error('Timeout exceeded'));
|
|
151
|
+
}, timeout);
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
const timers = (0, vm_timers_1.createTimers)();
|
|
154
|
+
const vmResolve = (value) => {
|
|
155
|
+
timers.clearAll();
|
|
156
|
+
clearTimeout(timeoutTimer);
|
|
157
|
+
resolve(value);
|
|
158
|
+
};
|
|
159
|
+
vmReject = (reason) => {
|
|
160
|
+
timers.clearAll();
|
|
161
|
+
clearTimeout(timeoutTimer);
|
|
162
|
+
reject(reason);
|
|
163
|
+
};
|
|
164
|
+
const vmContext = {
|
|
165
|
+
...globalVariables,
|
|
166
|
+
...builtInNodeModules,
|
|
167
|
+
resolve: vmResolve,
|
|
168
|
+
reject: vmReject,
|
|
169
|
+
setTimeout: timers.customSetTimeout,
|
|
170
|
+
setInterval: timers.customSetInterval,
|
|
171
|
+
clearTimeout: timers.customClearTimeout,
|
|
172
|
+
clearInterval: timers.customClearInterval,
|
|
173
|
+
};
|
|
174
|
+
node_vm_1.default.runInNewContext(code, vmContext, { displayErrors: true, timeout });
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
exports.unsafeEvaluateAsync = unsafeEvaluateAsync;
|
|
178
|
+
//# sourceMappingURL=unsafe-evaluate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unsafe-evaluate.js","sourceRoot":"","sources":["../../src/processing/unsafe-evaluate.ts"],"names":[],"mappings":";;;;;;AAAA,8BAA8B;AAC9B,8DAAiC;AACjC,wEAA2C;AAC3C,8DAAiC;AACjC,4EAA+C;AAC/C,gEAAmC;AACnC,gEAAmC;AACnC,oEAAuC;AACvC,8DAAiC;AACjC,4DAA+B;AAC/B,wDAA2B;AAC3B,8DAAiC;AACjC,sDAAyB;AACzB,0DAA6B;AAC7B,4DAA+B;AAC/B,4DAA+B;AAC/B,oEAAuC;AACvC,8DAAiC;AACjC,wDAA2B;AAC3B,sDAAyB;AACzB,0DAA6B;AAC7B,sEAAyC;AACzC,gEAAmC;AACnC,kEAAqC;AACrC,0DAA6B;AAC7B,8DAAiC;AACjC,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AC,gBAAgB;YACnC,WAAW,EAAE,MAAM,CAAC,iBAAiB;YACrC,YAAY,EAAE,MAAM,CAAC,kBAAkB;YACvC,aAAa,EAAE,MAAM,CAAC,mBAAmB;SAC1C,CAAC;QACF,iBAAE,CAAC,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,aAAa,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AApCW,QAAA,mBAAmB,uBAoC9B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
/// <reference types="node" />
|
|
5
|
+
/// <reference types="node" />
|
|
6
|
+
/**
|
|
7
|
+
* Timers (setTimeout, setInterval) do not work in Node.js vm, see: https://github.com/nodejs/help/issues/1875
|
|
8
|
+
*
|
|
9
|
+
* The API is wrapped in a "create" function so that every processing snippet keeps track of its timers and properly
|
|
10
|
+
* cleans them up after use.
|
|
11
|
+
*/
|
|
12
|
+
export declare const createTimers: () => {
|
|
13
|
+
customSetTimeout: (fn: () => void, ms: number) => void;
|
|
14
|
+
customClearTimeout: (id: NodeJS.Timeout) => void;
|
|
15
|
+
clearAllTimeouts: () => void;
|
|
16
|
+
customSetInterval: (fn: () => void, ms: number) => void;
|
|
17
|
+
customClearInterval: (id: NodeJS.Timeout) => void;
|
|
18
|
+
clearAllIntervals: () => void;
|
|
19
|
+
clearAll: () => void;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=vm-timers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vm-timers.d.ts","sourceRoot":"","sources":["../../src/processing/vm-timers.ts"],"names":[],"mappings":";;;;;AAAA;;;;;GAKG;AACH,eAAO,MAAM,YAAY;2BAGO,MAAM,IAAI,MAAM,MAAM;6BAIpB,OAAO,OAAO;;4BAcf,MAAM,IAAI,MAAM,MAAM;8BAIpB,OAAO,OAAO;;;CA0BhD,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTimers = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Timers (setTimeout, setInterval) do not work in Node.js vm, see: https://github.com/nodejs/help/issues/1875
|
|
6
|
+
*
|
|
7
|
+
* The API is wrapped in a "create" function so that every processing snippet keeps track of its timers and properly
|
|
8
|
+
* cleans them up after use.
|
|
9
|
+
*/
|
|
10
|
+
const createTimers = () => {
|
|
11
|
+
let timeouts = [];
|
|
12
|
+
const customSetTimeout = (fn, ms) => {
|
|
13
|
+
timeouts.push(setTimeout(fn, ms));
|
|
14
|
+
};
|
|
15
|
+
const customClearTimeout = (id) => {
|
|
16
|
+
timeouts = timeouts.filter((timeoutId) => timeoutId !== id);
|
|
17
|
+
clearTimeout(id);
|
|
18
|
+
};
|
|
19
|
+
const clearAllTimeouts = () => {
|
|
20
|
+
for (const element of timeouts) {
|
|
21
|
+
clearTimeout(element);
|
|
22
|
+
}
|
|
23
|
+
timeouts = [];
|
|
24
|
+
};
|
|
25
|
+
let intervals = [];
|
|
26
|
+
const customSetInterval = (fn, ms) => {
|
|
27
|
+
intervals.push(setInterval(fn, ms));
|
|
28
|
+
};
|
|
29
|
+
const customClearInterval = (id) => {
|
|
30
|
+
intervals = intervals.filter((intervalId) => intervalId !== id);
|
|
31
|
+
clearInterval(id);
|
|
32
|
+
};
|
|
33
|
+
const clearAllIntervals = () => {
|
|
34
|
+
for (const element of intervals) {
|
|
35
|
+
clearInterval(element);
|
|
36
|
+
}
|
|
37
|
+
intervals = [];
|
|
38
|
+
};
|
|
39
|
+
const clearAll = () => {
|
|
40
|
+
clearAllTimeouts();
|
|
41
|
+
clearAllIntervals();
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
customSetTimeout,
|
|
45
|
+
customClearTimeout,
|
|
46
|
+
clearAllTimeouts,
|
|
47
|
+
customSetInterval,
|
|
48
|
+
customClearInterval,
|
|
49
|
+
clearAllIntervals,
|
|
50
|
+
clearAll,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
exports.createTimers = createTimers;
|
|
54
|
+
//# sourceMappingURL=vm-timers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vm-timers.js","sourceRoot":"","sources":["../../src/processing/vm-timers.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACI,MAAM,YAAY,GAAG,GAAG,EAAE;IAC/B,IAAI,QAAQ,GAAqB,EAAE,CAAC;IAEpC,MAAM,gBAAgB,GAAG,CAAC,EAAc,EAAE,EAAU,EAAE,EAAE;QACtD,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IACpC,CAAC,CAAC;IAEF,MAAM,kBAAkB,GAAG,CAAC,EAAkB,EAAE,EAAE;QAChD,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,KAAK,EAAE,CAAC,CAAC;QAC5D,YAAY,CAAC,EAAE,CAAC,CAAC;IACnB,CAAC,CAAC;IAEF,MAAM,gBAAgB,GAAG,GAAG,EAAE;QAC5B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;YAC9B,YAAY,CAAC,OAAO,CAAC,CAAC;SACvB;QACD,QAAQ,GAAG,EAAE,CAAC;IAChB,CAAC,CAAC;IAEF,IAAI,SAAS,GAAqB,EAAE,CAAC;IAErC,MAAM,iBAAiB,GAAG,CAAC,EAAc,EAAE,EAAU,EAAE,EAAE;QACvD,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;IACtC,CAAC,CAAC;IAEF,MAAM,mBAAmB,GAAG,CAAC,EAAkB,EAAE,EAAE;QACjD,SAAS,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,KAAK,EAAE,CAAC,CAAC;QAChE,aAAa,CAAC,EAAE,CAAC,CAAC;IACpB,CAAC,CAAC;IAEF,MAAM,iBAAiB,GAAG,GAAG,EAAE;QAC7B,KAAK,MAAM,OAAO,IAAI,SAAS,EAAE;YAC/B,aAAa,CAAC,OAAO,CAAC,CAAC;SACxB;QACD,SAAS,GAAG,EAAE,CAAC;IACjB,CAAC,CAAC;IAEF,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,gBAAgB,EAAE,CAAC;QACnB,iBAAiB,EAAE,CAAC;IACtB,CAAC,CAAC;IAEF,OAAO;QACL,gBAAgB;QAChB,kBAAkB;QAClB,gBAAgB;QAChB,iBAAiB;QACjB,mBAAmB;QACnB,iBAAiB;QACjB,QAAQ;KACT,CAAC;AACJ,CAAC,CAAC;AAnDW,QAAA,YAAY,gBAmDvB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@api3/commons",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
"dist",
|
|
12
12
|
"src"
|
|
13
13
|
],
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"./eslint": "./dist/logger/index.js"
|
|
17
|
-
},
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"exports": "./dist/index.js",
|
|
18
16
|
"dependencies": {
|
|
17
|
+
"@api3/ois": "^2.2.1",
|
|
18
|
+
"@api3/promise-utils": "^0.4.0",
|
|
19
19
|
"@typescript-eslint/eslint-plugin": "^6.2.1",
|
|
20
20
|
"@typescript-eslint/parser": "^6.2.1",
|
|
21
21
|
"eslint-config-next": "^13.1.6",
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
"eslint-plugin-jest-formatting": "^3.1.0",
|
|
29
29
|
"eslint-plugin-lodash": "^7.4.0",
|
|
30
30
|
"eslint-plugin-no-only-tests": "^3.1.0",
|
|
31
|
-
"eslint-plugin-prefer-arrow": "^1.2.3",
|
|
32
31
|
"eslint-plugin-promise": "^6.1.1",
|
|
33
32
|
"eslint-plugin-react": "^7.32.1",
|
|
34
33
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
@@ -49,12 +48,10 @@
|
|
|
49
48
|
"rimraf": "^5.0.5",
|
|
50
49
|
"ts-jest": "^29.1.1",
|
|
51
50
|
"ts-node": "^10.9.1",
|
|
52
|
-
"typescript": "^5.2.2"
|
|
53
|
-
"zod": "^3.22.2"
|
|
51
|
+
"typescript": "^5.2.2"
|
|
54
52
|
},
|
|
55
53
|
"peerDependencies": {
|
|
56
|
-
"eslint": "^8.50.0"
|
|
57
|
-
"zod": "^3.22.2"
|
|
54
|
+
"eslint": "^8.50.0"
|
|
58
55
|
},
|
|
59
56
|
"scripts": {
|
|
60
57
|
"build": "tsc --project tsconfig.build.json",
|
|
@@ -64,7 +61,7 @@
|
|
|
64
61
|
"eslint:fix": "pnpm run eslint:check --fix",
|
|
65
62
|
"prettier:check": "prettier --check \"./**/*.{js,ts,md,json}\"",
|
|
66
63
|
"prettier:fix": "prettier --write \"./**/*.{js,ts,md,json}\"",
|
|
67
|
-
"test": "jest
|
|
64
|
+
"test": "jest",
|
|
68
65
|
"tsc": "tsc --project ."
|
|
69
66
|
}
|
|
70
67
|
}
|
package/src/eslint/README.md
CHANGED
|
@@ -58,11 +58,6 @@ The configurations are a collection of various rulesets and the config is quite
|
|
|
58
58
|
- Fix outdated stuff (avoid `!` ts operator when not necessary)
|
|
59
59
|
- Avoid vulnerabilities and errors (Number.parseInt without radix)
|
|
60
60
|
|
|
61
|
-
But some stylistical rules are too strict for certain projects, for example:
|
|
62
|
-
|
|
63
|
-
- Enforce `camelCase` for variables and functions, but `kebab-case` for filenames.
|
|
64
|
-
- Prefer arrow functions everywhere instead of regular `function` functions.
|
|
65
|
-
|
|
66
61
|
Tip: Some rules do have fixer with multiple variants of the fixes. You need to use the IDE to prompt the fixes and
|
|
67
62
|
choose the one you want.
|
|
68
63
|
|
package/src/eslint/jest.js
CHANGED
package/src/eslint/react.js
CHANGED
|
@@ -42,7 +42,6 @@ module.exports = {
|
|
|
42
42
|
'react/destructuring-assignment': ['error', 'always', { destructureInSignature: 'ignore' }],
|
|
43
43
|
'react/forbid-component-props': ['error', { forbid: [] }],
|
|
44
44
|
'react/forbid-dom-props': ['error', { forbid: [] }],
|
|
45
|
-
'react/function-component-definition': 'off', // Arrow functions are managed by other rules.
|
|
46
45
|
'react/jsx-curly-brace-presence': ['error', { props: 'never', children: 'never', propElementValues: 'always' }],
|
|
47
46
|
'react/jsx-curly-newline': 'off', // Conflicts with prettier.
|
|
48
47
|
'react/jsx-filename-extension': 'off', // We use .tsx extension.
|
package/src/eslint/universal.js
CHANGED
|
@@ -20,16 +20,7 @@ module.exports = {
|
|
|
20
20
|
'plugin:promise/recommended',
|
|
21
21
|
'plugin:lodash/recommended',
|
|
22
22
|
],
|
|
23
|
-
plugins: [
|
|
24
|
-
'@typescript-eslint',
|
|
25
|
-
'deprecation',
|
|
26
|
-
'functional',
|
|
27
|
-
'prefer-arrow',
|
|
28
|
-
'unicorn',
|
|
29
|
-
'check-file',
|
|
30
|
-
'import',
|
|
31
|
-
'lodash',
|
|
32
|
-
],
|
|
23
|
+
plugins: ['@typescript-eslint', 'deprecation', 'functional', 'unicorn', 'check-file', 'import', 'lodash'],
|
|
33
24
|
rules: {
|
|
34
25
|
/* Rule definitions and overrides for standard ESLint rules */
|
|
35
26
|
camelcase: 'error',
|
|
@@ -78,16 +69,6 @@ module.exports = {
|
|
|
78
69
|
},
|
|
79
70
|
],
|
|
80
71
|
|
|
81
|
-
/* Rules to setup enforcement of arrow functions over regular functions */
|
|
82
|
-
'prefer-arrow/prefer-arrow-functions': [
|
|
83
|
-
'error',
|
|
84
|
-
{
|
|
85
|
-
disallowPrototype: true,
|
|
86
|
-
singleReturnOnly: false,
|
|
87
|
-
classPropertiesAllowed: false,
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
|
|
91
72
|
/* Rules to enforce kebab-case folder structure */
|
|
92
73
|
'check-file/folder-naming-convention': [
|
|
93
74
|
'error',
|
|
@@ -110,6 +91,7 @@ module.exports = {
|
|
|
110
91
|
'unicorn/no-array-reduce': 'off', // We are OK with using reduce occasionally, but I agree with the author that the code using reduce can easily get complex.
|
|
111
92
|
'unicorn/no-nested-ternary': 'off', // This rule is smarter than the standard ESLint rule, but conflicts with prettier so it needs to be turned off. Nested ternaries are very unreadable so it's OK if all of them are flagged.
|
|
112
93
|
'unicorn/no-null': 'off', // We use both null and undefined for representing three state objects. We could use a string union instead, but using combination of null and undefined is less verbose.
|
|
94
|
+
'unicorn/no-object-as-default-parameter': 'off', // Too restrictive. TypeScript can ensure that the default value matches the type.
|
|
113
95
|
'unicorn/no-useless-undefined': ['error', { checkArguments: false }], // We need to disable "checkArguments", because if a function expects a value of type "T | undefined" the undefined value needs to be passed explicitly.
|
|
114
96
|
'unicorn/prefer-module': 'off', // We use CJS for configuration files and tests. There is no rush to migrate to ESM and the configuration files are probably not yet ready for ESM yet.
|
|
115
97
|
'unicorn/prevent-abbreviations': 'off', // This rule reports many false positives and leads to more verbose code.
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
1
|
+
// NOTE: Not exporting ESLint rules because they need to be imported in a special way inside .eslintrc.js config.
|
|
2
|
+
export * from './logger';
|
|
3
|
+
export * from './processing';
|
package/src/logger/README.md
CHANGED
|
@@ -6,8 +6,7 @@ Backend-only logger for Node.js packages based on Winston logger.
|
|
|
6
6
|
|
|
7
7
|
## Getting started
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
2. Use `createLogger` function to create a logger instance.
|
|
9
|
+
Import `createLogger` function to create a logger instance.
|
|
11
10
|
|
|
12
11
|
## Configuration
|
|
13
12
|
|
package/src/logger/index.ts
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import winston from 'winston';
|
|
2
2
|
import { consoleFormat } from 'winston-console-format';
|
|
3
|
-
import { z } from 'zod';
|
|
4
3
|
|
|
5
|
-
export const
|
|
4
|
+
export const logFormatOptions = ['json', 'pretty'] as const;
|
|
6
5
|
|
|
7
|
-
export type
|
|
6
|
+
export type LogFormat = (typeof logFormatOptions)[number];
|
|
8
7
|
|
|
9
|
-
export const
|
|
8
|
+
export const logLevelOptions = ['debug', 'info', 'warn', 'error'] as const;
|
|
10
9
|
|
|
11
|
-
export type LogLevel =
|
|
10
|
+
export type LogLevel = (typeof logLevelOptions)[number];
|
|
12
11
|
|
|
13
|
-
export
|
|
14
|
-
colorize:
|
|
15
|
-
enabled:
|
|
16
|
-
format:
|
|
17
|
-
minLevel:
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type LogConfig = z.infer<typeof logConfigSchema>;
|
|
12
|
+
export interface LogConfig {
|
|
13
|
+
colorize: boolean;
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
format: LogFormat;
|
|
16
|
+
minLevel: LogLevel;
|
|
17
|
+
}
|
|
21
18
|
|
|
22
19
|
const createConsoleTransport = (config: LogConfig) => {
|
|
23
20
|
const { colorize, enabled, format } = config;
|
|
@@ -104,7 +101,32 @@ const wrapper = (logger: Logger): Logger => {
|
|
|
104
101
|
} as Logger;
|
|
105
102
|
};
|
|
106
103
|
|
|
104
|
+
export const validateLogConfig = (config: unknown): LogConfig => {
|
|
105
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
106
|
+
if (typeof config !== 'object' || config === null) {
|
|
107
|
+
throw new Error('Invalid logger configuration');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { colorize, enabled, format, minLevel } = config as Partial<LogConfig>;
|
|
111
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
112
|
+
if (typeof colorize !== 'boolean') {
|
|
113
|
+
throw new TypeError('Invalid logger configuration: colorize must be a boolean');
|
|
114
|
+
}
|
|
115
|
+
// eslint-disable-next-line lodash/prefer-lodash-typecheck
|
|
116
|
+
if (typeof enabled !== 'boolean') {
|
|
117
|
+
throw new TypeError('Invalid logger configuration: enabled must be a boolean');
|
|
118
|
+
}
|
|
119
|
+
if (!logFormatOptions.includes(format as any)) {
|
|
120
|
+
throw new TypeError('Invalid logger configuration: format must be one of "json" or "pretty"');
|
|
121
|
+
}
|
|
122
|
+
if (!logLevelOptions.includes(minLevel as any)) {
|
|
123
|
+
throw new TypeError('Invalid logger configuration: minLevel must be one of "debug", "info", "warn" or "error"');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return config as LogConfig;
|
|
127
|
+
};
|
|
128
|
+
|
|
107
129
|
export const createLogger = (config: LogConfig) => {
|
|
108
|
-
// Ensure that the configuration is valid
|
|
109
|
-
return wrapper(createBaseLogger(
|
|
130
|
+
// Ensure that the logger configuration is valid.
|
|
131
|
+
return wrapper(createBaseLogger(validateLogConfig(config)));
|
|
110
132
|
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Processing
|
|
2
|
+
|
|
3
|
+
> Implementation of [OIS processing](https://docs.api3.org/reference/ois/latest/processing.html).
|
|
4
|
+
|
|
5
|
+
The pre/post processing is only supported for Node.js environments and uses internal Node.js modules.
|
|
6
|
+
|
|
7
|
+
## Documentation
|
|
8
|
+
|
|
9
|
+
The processing module exports two main functions:
|
|
10
|
+
|
|
11
|
+
<!-- NOTE: These are copied over from "processing.d.ts" from "dist" file. -->
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
/**
|
|
15
|
+
* Pre-processes API call parameters based on the provided endpoint's processing specifications.
|
|
16
|
+
*
|
|
17
|
+
* @param endpoint The endpoint containing processing specifications.
|
|
18
|
+
* @param apiCallParameters The parameters to be pre-processed.
|
|
19
|
+
* @param processingOptions Options to control the async processing behavior like retries and timeouts.
|
|
20
|
+
*
|
|
21
|
+
* @returns A promise that resolves to the pre-processed parameters.
|
|
22
|
+
*/
|
|
23
|
+
export declare const preProcessApiCallParameters: (
|
|
24
|
+
endpoint: Endpoint,
|
|
25
|
+
apiCallParameters: ApiCallParameters,
|
|
26
|
+
processingOptions?: GoAsyncOptions
|
|
27
|
+
) => Promise<ApiCallParameters>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Post-processes the API call response based on the provided endpoint's processing specifications.
|
|
31
|
+
*
|
|
32
|
+
* @param apiCallResponse The raw response obtained from the API call.
|
|
33
|
+
* @param endpoint The endpoint containing processing specifications.
|
|
34
|
+
* @param apiCallParameters The parameters used in the API call.
|
|
35
|
+
* @param processingOptions Options to control the async processing behavior like retries and timeouts.
|
|
36
|
+
*
|
|
37
|
+
* @returns A promise that resolves to the post-processed API call response.
|
|
38
|
+
*/
|
|
39
|
+
export declare const postProcessApiCallResponse: (
|
|
40
|
+
apiCallResponse: unknown,
|
|
41
|
+
endpoint: Endpoint,
|
|
42
|
+
apiCallParameters: ApiCallParameters,
|
|
43
|
+
processingOptions?: GoAsyncOptions
|
|
44
|
+
) => Promise<unknown>;
|
|
45
|
+
```
|