@squiz/render-runtime-lib 1.2.1-alpha.100

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.
Files changed (56) hide show
  1. package/README.md +11 -0
  2. package/lib/component-runner/component-runner.spec.d.ts +1 -0
  3. package/lib/component-runner/index.d.ts +22 -0
  4. package/lib/component-runner/worker/WorkerPool.d.ts +50 -0
  5. package/lib/component-runner/worker/getRuntimeModules.d.ts +1 -0
  6. package/lib/component-runner/worker/worker-root.d.ts +1 -0
  7. package/lib/index.d.ts +25 -0
  8. package/lib/index.js +112791 -0
  9. package/lib/index.js.map +7 -0
  10. package/lib/migrations/20220704054051_initial.sql +19 -0
  11. package/lib/migrations/20220718172237_adding_component_sets.sql +23 -0
  12. package/lib/migrations/20220728113941_add_env_vars_field.sql +1 -0
  13. package/lib/migrations/20220817113300_removing_null_props_from_jsonb.sql +41 -0
  14. package/lib/render-runtime-lib.spec.d.ts +1 -0
  15. package/lib/test/helpers/fixtures.d.ts +20 -0
  16. package/lib/test/helpers/stack.d.ts +6 -0
  17. package/lib/test/index.d.ts +2 -0
  18. package/lib/utils/convertFunctionStaticFilesToFqdn.d.ts +1 -0
  19. package/lib/utils/convertFunctionStaticFilesToFqdn.spec.d.ts +1 -0
  20. package/lib/utils/getFunctionDefinitionFromManifest.d.ts +2 -0
  21. package/lib/utils/getManifestPath.d.ts +1 -0
  22. package/lib/utils/getManifestPath.spec.d.ts +1 -0
  23. package/lib/utils/getPreviewFilePath.d.ts +1 -0
  24. package/lib/utils/getPreviewFilePath.spec.d.ts +1 -0
  25. package/lib/utils/isInProductionMode.d.ts +1 -0
  26. package/lib/utils/isInProductionMode.spec.d.ts +1 -0
  27. package/lib/utils/resolvePreviewOutput.d.ts +2 -0
  28. package/lib/utils/resolvePreviewOutput.spec.d.ts +1 -0
  29. package/lib/webserver/app.d.ts +4 -0
  30. package/lib/webserver/controllers/core.d.ts +3 -0
  31. package/lib/webserver/controllers/core.spec.d.ts +1 -0
  32. package/lib/webserver/controllers/definition.d.ts +3 -0
  33. package/lib/webserver/controllers/definition.spec.d.ts +1 -0
  34. package/lib/webserver/controllers/index.d.ts +4 -0
  35. package/lib/webserver/controllers/render.d.ts +3 -0
  36. package/lib/webserver/controllers/render.spec.d.ts +1 -0
  37. package/lib/webserver/controllers/static.d.ts +3 -0
  38. package/lib/webserver/controllers/static.spec.d.ts +1 -0
  39. package/lib/webserver/controllers/test/definition-route-tests.d.ts +1 -0
  40. package/lib/webserver/controllers/test/render-route-tests.d.ts +1 -0
  41. package/lib/webserver/controllers/test/static-route-tests.d.ts +1 -0
  42. package/lib/webserver/index.d.ts +22 -0
  43. package/lib/worker/bridge.js +1010 -0
  44. package/lib/worker/compiler.js +87 -0
  45. package/lib/worker/events.js +977 -0
  46. package/lib/worker/nodevm.js +503 -0
  47. package/lib/worker/resolver-compat.js +342 -0
  48. package/lib/worker/resolver.js +882 -0
  49. package/lib/worker/script.js +388 -0
  50. package/lib/worker/setup-node-sandbox.js +469 -0
  51. package/lib/worker/setup-sandbox.js +456 -0
  52. package/lib/worker/transformer.js +180 -0
  53. package/lib/worker/vm.js +539 -0
  54. package/lib/worker/worker-root.js +41743 -0
  55. package/lib/worker/worker-root.js.map +7 -0
  56. package/package.json +60 -0
@@ -0,0 +1,388 @@
1
+ 'use strict';
2
+
3
+ const {Script} = require('vm');
4
+ const {
5
+ lookupCompiler,
6
+ removeShebang
7
+ } = require('./compiler');
8
+ const {
9
+ transformer
10
+ } = require('./transformer');
11
+
12
+ const objectDefineProperties = Object.defineProperties;
13
+
14
+ const MODULE_PREFIX = '(function (exports, require, module, __filename, __dirname) { ';
15
+ const STRICT_MODULE_PREFIX = MODULE_PREFIX + '"use strict"; ';
16
+ const MODULE_SUFFIX = '\n});';
17
+
18
+ /**
19
+ * Class Script
20
+ *
21
+ * @public
22
+ */
23
+ class VMScript {
24
+
25
+ /**
26
+ * The script code with wrapping. If set will invalidate the cache.<br>
27
+ * Writable only for backwards compatibility.
28
+ *
29
+ * @public
30
+ * @readonly
31
+ * @member {string} code
32
+ * @memberOf VMScript#
33
+ */
34
+
35
+ /**
36
+ * The filename used for this script.
37
+ *
38
+ * @public
39
+ * @readonly
40
+ * @since v3.9.0
41
+ * @member {string} filename
42
+ * @memberOf VMScript#
43
+ */
44
+
45
+ /**
46
+ * The line offset use for stack traces.
47
+ *
48
+ * @public
49
+ * @readonly
50
+ * @since v3.9.0
51
+ * @member {number} lineOffset
52
+ * @memberOf VMScript#
53
+ */
54
+
55
+ /**
56
+ * The column offset use for stack traces.
57
+ *
58
+ * @public
59
+ * @readonly
60
+ * @since v3.9.0
61
+ * @member {number} columnOffset
62
+ * @memberOf VMScript#
63
+ */
64
+
65
+ /**
66
+ * The compiler to use to get the JavaScript code.
67
+ *
68
+ * @public
69
+ * @readonly
70
+ * @since v3.9.0
71
+ * @member {(string|compileCallback)} compiler
72
+ * @memberOf VMScript#
73
+ */
74
+
75
+ /**
76
+ * The prefix for the script.
77
+ *
78
+ * @private
79
+ * @member {string} _prefix
80
+ * @memberOf VMScript#
81
+ */
82
+
83
+ /**
84
+ * The suffix for the script.
85
+ *
86
+ * @private
87
+ * @member {string} _suffix
88
+ * @memberOf VMScript#
89
+ */
90
+
91
+ /**
92
+ * The compiled vm.Script for the VM or if not compiled <code>null</code>.
93
+ *
94
+ * @private
95
+ * @member {?vm.Script} _compiledVM
96
+ * @memberOf VMScript#
97
+ */
98
+
99
+ /**
100
+ * The compiled vm.Script for the NodeVM or if not compiled <code>null</code>.
101
+ *
102
+ * @private
103
+ * @member {?vm.Script} _compiledNodeVM
104
+ * @memberOf VMScript#
105
+ */
106
+
107
+ /**
108
+ * The compiled vm.Script for the NodeVM in strict mode or if not compiled <code>null</code>.
109
+ *
110
+ * @private
111
+ * @member {?vm.Script} _compiledNodeVMStrict
112
+ * @memberOf VMScript#
113
+ */
114
+
115
+ /**
116
+ * The resolved compiler to use to get the JavaScript code.
117
+ *
118
+ * @private
119
+ * @readonly
120
+ * @member {compileCallback} _compiler
121
+ * @memberOf VMScript#
122
+ */
123
+
124
+ /**
125
+ * The script to run without wrapping.
126
+ *
127
+ * @private
128
+ * @member {string} _code
129
+ * @memberOf VMScript#
130
+ */
131
+
132
+ /**
133
+ * Whether or not the script contains async functions.
134
+ *
135
+ * @private
136
+ * @member {boolean} _hasAsync
137
+ * @memberOf VMScript#
138
+ */
139
+
140
+ /**
141
+ * Create VMScript instance.
142
+ *
143
+ * @public
144
+ * @param {string} code - Code to run.
145
+ * @param {(string|Object)} [options] - Options map or filename.
146
+ * @param {string} [options.filename="vm.js"] - Filename that shows up in any stack traces produced from this script.
147
+ * @param {number} [options.lineOffset=0] - Passed to vm.Script options.
148
+ * @param {number} [options.columnOffset=0] - Passed to vm.Script options.
149
+ * @param {(string|compileCallback)} [options.compiler="javascript"] - The compiler to use.
150
+ * @throws {VMError} If the compiler is unknown or if coffee-script was requested but the module not found.
151
+ */
152
+ constructor(code, options) {
153
+ const sCode = `${code}`;
154
+ let useFileName;
155
+ let useOptions;
156
+ if (arguments.length === 2) {
157
+ if (typeof options === 'object') {
158
+ useOptions = options || {__proto__: null};
159
+ useFileName = useOptions.filename;
160
+ } else {
161
+ useOptions = {__proto__: null};
162
+ useFileName = options;
163
+ }
164
+ } else if (arguments.length > 2) {
165
+ // We do it this way so that there are no more arguments in the function.
166
+ // eslint-disable-next-line prefer-rest-params
167
+ useOptions = arguments[2] || {__proto__: null};
168
+ useFileName = options || useOptions.filename;
169
+ } else {
170
+ useOptions = {__proto__: null};
171
+ }
172
+
173
+ const {
174
+ compiler = 'javascript',
175
+ lineOffset = 0,
176
+ columnOffset = 0
177
+ } = useOptions;
178
+
179
+ // Throw if the compiler is unknown.
180
+ const resolvedCompiler = lookupCompiler(compiler);
181
+
182
+ objectDefineProperties(this, {
183
+ __proto__: null,
184
+ code: {
185
+ __proto__: null,
186
+ // Put this here so that it is enumerable, and looks like a property.
187
+ get() {
188
+ return this._prefix + this._code + this._suffix;
189
+ },
190
+ set(value) {
191
+ const strNewCode = String(value);
192
+ if (strNewCode === this._code && this._prefix === '' && this._suffix === '') return;
193
+ this._code = strNewCode;
194
+ this._prefix = '';
195
+ this._suffix = '';
196
+ this._compiledVM = null;
197
+ this._compiledNodeVM = null;
198
+ this._compiledCode = null;
199
+ },
200
+ enumerable: true
201
+ },
202
+ filename: {
203
+ __proto__: null,
204
+ value: useFileName || 'vm.js',
205
+ enumerable: true
206
+ },
207
+ lineOffset: {
208
+ __proto__: null,
209
+ value: lineOffset,
210
+ enumerable: true
211
+ },
212
+ columnOffset: {
213
+ __proto__: null,
214
+ value: columnOffset,
215
+ enumerable: true
216
+ },
217
+ compiler: {
218
+ __proto__: null,
219
+ value: compiler,
220
+ enumerable: true
221
+ },
222
+ _code: {
223
+ __proto__: null,
224
+ value: sCode,
225
+ writable: true
226
+ },
227
+ _prefix: {
228
+ __proto__: null,
229
+ value: '',
230
+ writable: true
231
+ },
232
+ _suffix: {
233
+ __proto__: null,
234
+ value: '',
235
+ writable: true
236
+ },
237
+ _compiledVM: {
238
+ __proto__: null,
239
+ value: null,
240
+ writable: true
241
+ },
242
+ _compiledNodeVM: {
243
+ __proto__: null,
244
+ value: null,
245
+ writable: true
246
+ },
247
+ _compiledNodeVMStrict: {
248
+ __proto__: null,
249
+ value: null,
250
+ writable: true
251
+ },
252
+ _compiledCode: {
253
+ __proto__: null,
254
+ value: null,
255
+ writable: true
256
+ },
257
+ _hasAsync: {
258
+ __proto__: null,
259
+ value: false,
260
+ writable: true
261
+ },
262
+ _compiler: {__proto__: null, value: resolvedCompiler}
263
+ });
264
+ }
265
+
266
+ /**
267
+ * Wraps the code.<br>
268
+ * This will replace the old wrapping.<br>
269
+ * Will invalidate the code cache.
270
+ *
271
+ * @public
272
+ * @deprecated Since v3.9.0. Wrap your code before passing it into the VMScript object.
273
+ * @param {string} prefix - String that will be appended before the script code.
274
+ * @param {script} suffix - String that will be appended behind the script code.
275
+ * @return {this} This for chaining.
276
+ * @throws {TypeError} If prefix or suffix is a Symbol.
277
+ */
278
+ wrap(prefix, suffix) {
279
+ const strPrefix = `${prefix}`;
280
+ const strSuffix = `${suffix}`;
281
+ if (this._prefix === strPrefix && this._suffix === strSuffix) return this;
282
+ this._prefix = strPrefix;
283
+ this._suffix = strSuffix;
284
+ this._compiledVM = null;
285
+ this._compiledNodeVM = null;
286
+ this._compiledNodeVMStrict = null;
287
+ return this;
288
+ }
289
+
290
+ /**
291
+ * Compile this script. <br>
292
+ * This is useful to detect syntax errors in the script.
293
+ *
294
+ * @public
295
+ * @return {this} This for chaining.
296
+ * @throws {SyntaxError} If there is a syntax error in the script.
297
+ */
298
+ compile() {
299
+ this._compileVM();
300
+ return this;
301
+ }
302
+
303
+ /**
304
+ * Get the compiled code.
305
+ *
306
+ * @private
307
+ * @return {string} The code.
308
+ */
309
+ getCompiledCode() {
310
+ if (!this._compiledCode) {
311
+ const comp = this._compiler(this._prefix + removeShebang(this._code) + this._suffix, this.filename);
312
+ const res = transformer(null, comp, false, false, this.filename);
313
+ this._compiledCode = res.code;
314
+ this._hasAsync = res.hasAsync;
315
+ }
316
+ return this._compiledCode;
317
+ }
318
+
319
+ /**
320
+ * Compiles this script to a vm.Script.
321
+ *
322
+ * @private
323
+ * @param {string} prefix - JavaScript code that will be used as prefix.
324
+ * @param {string} suffix - JavaScript code that will be used as suffix.
325
+ * @return {vm.Script} The compiled vm.Script.
326
+ * @throws {SyntaxError} If there is a syntax error in the script.
327
+ */
328
+ _compile(prefix, suffix) {
329
+ return new Script(prefix + this.getCompiledCode() + suffix, {
330
+ __proto__: null,
331
+ filename: this.filename,
332
+ displayErrors: false,
333
+ lineOffset: this.lineOffset,
334
+ columnOffset: this.columnOffset
335
+ });
336
+ }
337
+
338
+ /**
339
+ * Will return the cached version of the script intended for VM or compile it.
340
+ *
341
+ * @private
342
+ * @return {vm.Script} The compiled script
343
+ * @throws {SyntaxError} If there is a syntax error in the script.
344
+ */
345
+ _compileVM() {
346
+ let script = this._compiledVM;
347
+ if (!script) {
348
+ this._compiledVM = script = this._compile('', '');
349
+ }
350
+ return script;
351
+ }
352
+
353
+ /**
354
+ * Will return the cached version of the script intended for NodeVM or compile it.
355
+ *
356
+ * @private
357
+ * @return {vm.Script} The compiled script
358
+ * @throws {SyntaxError} If there is a syntax error in the script.
359
+ */
360
+ _compileNodeVM() {
361
+ let script = this._compiledNodeVM;
362
+ if (!script) {
363
+ this._compiledNodeVM = script = this._compile(MODULE_PREFIX, MODULE_SUFFIX);
364
+ }
365
+ return script;
366
+ }
367
+
368
+ /**
369
+ * Will return the cached version of the script intended for NodeVM in strict mode or compile it.
370
+ *
371
+ * @private
372
+ * @return {vm.Script} The compiled script
373
+ * @throws {SyntaxError} If there is a syntax error in the script.
374
+ */
375
+ _compileNodeVMStrict() {
376
+ let script = this._compiledNodeVMStrict;
377
+ if (!script) {
378
+ this._compiledNodeVMStrict = script = this._compile(STRICT_MODULE_PREFIX, MODULE_SUFFIX);
379
+ }
380
+ return script;
381
+ }
382
+
383
+ }
384
+
385
+ exports.MODULE_PREFIX = MODULE_PREFIX;
386
+ exports.STRICT_MODULE_PREFIX = STRICT_MODULE_PREFIX;
387
+ exports.MODULE_SUFFIX = MODULE_SUFFIX;
388
+ exports.VMScript = VMScript;