@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,178 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import async_hooks from 'node:async_hooks';
|
|
4
|
+
import buffer from 'node:buffer';
|
|
5
|
+
import child_process from 'node:child_process';
|
|
6
|
+
import cluster from 'node:cluster';
|
|
7
|
+
import console from 'node:console';
|
|
8
|
+
import constants from 'node:constants';
|
|
9
|
+
import crypto from 'node:crypto';
|
|
10
|
+
import dgram from 'node:dgram';
|
|
11
|
+
import dns from 'node:dns';
|
|
12
|
+
import events from 'node:events';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import http from 'node:http';
|
|
15
|
+
import http2 from 'node:http2';
|
|
16
|
+
import https from 'node:https';
|
|
17
|
+
import inspector from 'node:inspector';
|
|
18
|
+
import module from 'node:module';
|
|
19
|
+
import net from 'node:net';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import perf_hooks from 'node:perf_hooks';
|
|
23
|
+
import process from 'node:process';
|
|
24
|
+
import readline from 'node:readline';
|
|
25
|
+
import repl from 'node:repl';
|
|
26
|
+
import stream from 'node:stream';
|
|
27
|
+
import string_decoder from 'node:string_decoder';
|
|
28
|
+
import timers from 'node:timers';
|
|
29
|
+
import tls from 'node:tls';
|
|
30
|
+
import trace_events from 'node:trace_events';
|
|
31
|
+
import tty from 'node:tty';
|
|
32
|
+
import url from 'node:url';
|
|
33
|
+
import util from 'node:util';
|
|
34
|
+
import v8 from 'node:v8';
|
|
35
|
+
import vm from 'node:vm';
|
|
36
|
+
import worker_threads from 'node:worker_threads';
|
|
37
|
+
import zlib from 'node:zlib';
|
|
38
|
+
|
|
39
|
+
import { createTimers } from './vm-timers';
|
|
40
|
+
|
|
41
|
+
const builtInNodeModules = {
|
|
42
|
+
assert,
|
|
43
|
+
async_hooks,
|
|
44
|
+
buffer,
|
|
45
|
+
child_process,
|
|
46
|
+
cluster,
|
|
47
|
+
console,
|
|
48
|
+
constants,
|
|
49
|
+
crypto,
|
|
50
|
+
dgram,
|
|
51
|
+
dns,
|
|
52
|
+
events,
|
|
53
|
+
fs,
|
|
54
|
+
http,
|
|
55
|
+
http2,
|
|
56
|
+
https,
|
|
57
|
+
inspector,
|
|
58
|
+
module,
|
|
59
|
+
net,
|
|
60
|
+
os,
|
|
61
|
+
path,
|
|
62
|
+
perf_hooks,
|
|
63
|
+
process,
|
|
64
|
+
readline,
|
|
65
|
+
repl,
|
|
66
|
+
stream,
|
|
67
|
+
string_decoder,
|
|
68
|
+
timers,
|
|
69
|
+
tls,
|
|
70
|
+
trace_events,
|
|
71
|
+
tty,
|
|
72
|
+
url,
|
|
73
|
+
util,
|
|
74
|
+
v8,
|
|
75
|
+
vm,
|
|
76
|
+
worker_threads,
|
|
77
|
+
zlib,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Evaluates the provided code in a new VM context with the specified global variables.
|
|
82
|
+
*
|
|
83
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
84
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
85
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
86
|
+
* reviewed.
|
|
87
|
+
*
|
|
88
|
+
* @param code The JavaScript code to evaluate.
|
|
89
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
90
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
91
|
+
*
|
|
92
|
+
* @returns The result of the evaluated code.
|
|
93
|
+
*
|
|
94
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
*const result = unsafeEvaluate('const output = input + 1;', { input: 1 }, 1000);
|
|
99
|
+
*console.log(result); // Outputs: 2
|
|
100
|
+
*/
|
|
101
|
+
export const unsafeEvaluate = (code: string, globalVariables: Record<string, unknown>, timeout: number) => {
|
|
102
|
+
const vmContext = {
|
|
103
|
+
...globalVariables,
|
|
104
|
+
...builtInNodeModules,
|
|
105
|
+
deferredOutput: undefined as unknown,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
vm.runInNewContext(`${code}; deferredOutput = output;`, vmContext, {
|
|
109
|
+
displayErrors: true,
|
|
110
|
+
timeout,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return vmContext.deferredOutput;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Asynchronously evaluates the provided code in a new VM context with the specified global variables.
|
|
118
|
+
*
|
|
119
|
+
* **Security Warning:** This function executes the provided code and can have unintended side effects or
|
|
120
|
+
* vulnerabilities if used with untrusted or malicious input. It's imperative to use this function only with code you
|
|
121
|
+
* trust completely. Avoid using this function with user-generated code or third-party code that hasn't been thoroughly
|
|
122
|
+
* reviewed.
|
|
123
|
+
*
|
|
124
|
+
* @param code The JavaScript code to evaluate. The code should call the `resolve` method to return the result of the
|
|
125
|
+
* evaluation. You may use async/await syntax in the code.
|
|
126
|
+
* @param globalVariables A key-value pair of variables to be made available in the context of the executed code.
|
|
127
|
+
* @param timeout Duration in milliseconds to wait before terminating the execution.
|
|
128
|
+
*
|
|
129
|
+
* @returns The result of the evaluated code wrapped in a Promise.
|
|
130
|
+
*
|
|
131
|
+
* @throws Throws an error if the execution exceeds the provided timeout or if there's a problem with the code.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
*
|
|
135
|
+
*const result = await unsafeEvaluateAsync(
|
|
136
|
+
* "const output = {...input, c: 'some-value'}; resolve(output);",
|
|
137
|
+
* { input: { a: true, b: 123 } },
|
|
138
|
+
* 5000
|
|
139
|
+
*);
|
|
140
|
+
*console.log(result); // Outputs: { a: true, b: 123, c: 'some-value' }
|
|
141
|
+
*/
|
|
142
|
+
export const unsafeEvaluateAsync = async (code: string, globalVariables: Record<string, unknown>, timeout: number) => {
|
|
143
|
+
let vmReject: (reason: unknown) => void;
|
|
144
|
+
|
|
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
|
+
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
const timers = createTimers();
|
|
155
|
+
const vmResolve = (value: unknown) => {
|
|
156
|
+
timers.clearAll();
|
|
157
|
+
clearTimeout(timeoutTimer);
|
|
158
|
+
resolve(value);
|
|
159
|
+
};
|
|
160
|
+
vmReject = (reason: unknown) => {
|
|
161
|
+
timers.clearAll();
|
|
162
|
+
clearTimeout(timeoutTimer);
|
|
163
|
+
reject(reason);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const vmContext = {
|
|
167
|
+
...globalVariables,
|
|
168
|
+
...builtInNodeModules,
|
|
169
|
+
resolve: vmResolve,
|
|
170
|
+
reject: vmReject,
|
|
171
|
+
setTimeout: timers.customSetTimeout,
|
|
172
|
+
setInterval: timers.customSetInterval,
|
|
173
|
+
clearTimeout: timers.customClearTimeout,
|
|
174
|
+
clearInterval: timers.customClearInterval,
|
|
175
|
+
};
|
|
176
|
+
vm.runInNewContext(code, vmContext, { displayErrors: true, timeout });
|
|
177
|
+
});
|
|
178
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timers (setTimeout, setInterval) do not work in Node.js vm, see: https://github.com/nodejs/help/issues/1875
|
|
3
|
+
*
|
|
4
|
+
* The API is wrapped in a "create" function so that every processing snippet keeps track of its timers and properly
|
|
5
|
+
* cleans them up after use.
|
|
6
|
+
*/
|
|
7
|
+
export const createTimers = () => {
|
|
8
|
+
let timeouts: NodeJS.Timeout[] = [];
|
|
9
|
+
|
|
10
|
+
const customSetTimeout = (fn: () => void, ms: number) => {
|
|
11
|
+
timeouts.push(setTimeout(fn, ms));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const customClearTimeout = (id: NodeJS.Timeout) => {
|
|
15
|
+
timeouts = timeouts.filter((timeoutId) => timeoutId !== id);
|
|
16
|
+
clearTimeout(id);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const clearAllTimeouts = () => {
|
|
20
|
+
for (const element of timeouts) {
|
|
21
|
+
clearTimeout(element);
|
|
22
|
+
}
|
|
23
|
+
timeouts = [];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
let intervals: NodeJS.Timeout[] = [];
|
|
27
|
+
|
|
28
|
+
const customSetInterval = (fn: () => void, ms: number) => {
|
|
29
|
+
intervals.push(setInterval(fn, ms));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const customClearInterval = (id: NodeJS.Timeout) => {
|
|
33
|
+
intervals = intervals.filter((intervalId) => intervalId !== id);
|
|
34
|
+
clearInterval(id);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const clearAllIntervals = () => {
|
|
38
|
+
for (const element of intervals) {
|
|
39
|
+
clearInterval(element);
|
|
40
|
+
}
|
|
41
|
+
intervals = [];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const clearAll = () => {
|
|
45
|
+
clearAllTimeouts();
|
|
46
|
+
clearAllIntervals();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
customSetTimeout,
|
|
51
|
+
customClearTimeout,
|
|
52
|
+
clearAllTimeouts,
|
|
53
|
+
customSetInterval,
|
|
54
|
+
customClearInterval,
|
|
55
|
+
clearAllIntervals,
|
|
56
|
+
clearAll,
|
|
57
|
+
};
|
|
58
|
+
};
|