@shopify/ui-extensions-server-kit 5.2.0 → 5.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.
Files changed (169) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/ExtensionServerClient/ExtensionServerClient.cjs.js +1 -1
  3. package/dist/ExtensionServerClient/ExtensionServerClient.d.ts +1 -0
  4. package/dist/ExtensionServerClient/ExtensionServerClient.es.js +1 -1
  5. package/dist/ExtensionServerClient/ExtensionServerClient.test.d.ts +8 -0
  6. package/dist/ExtensionServerClient/server-types.d.ts +42 -0
  7. package/dist/context/constants.cjs.js +1 -1
  8. package/dist/context/constants.d.ts +0 -1
  9. package/dist/context/constants.es.js +0 -1
  10. package/dist/context/types.d.ts +1 -0
  11. package/dist/hooks/index.d.ts +0 -2
  12. package/dist/i18n.cjs.js +1 -1
  13. package/dist/i18n.d.ts +1 -20
  14. package/dist/i18n.es.js +0 -1
  15. package/dist/index.cjs.js +1 -1
  16. package/dist/index.es.js +40 -48
  17. package/dist/testing/extensions.cjs.js +1 -1
  18. package/dist/testing/extensions.es.js +2 -2
  19. package/dist/types.cjs.js +1 -1
  20. package/dist/types.d.ts +10 -4
  21. package/dist/types.es.js +2 -2
  22. package/dist/utilities/index.d.ts +0 -1
  23. package/node_modules/@shopify/ui-extensions-test-utils/dist/index.d.ts +3 -0
  24. package/node_modules/@shopify/ui-extensions-test-utils/dist/render.d.ts +2 -0
  25. package/node_modules/@shopify/ui-extensions-test-utils/dist/renderHook.d.ts +17 -0
  26. package/node_modules/@shopify/ui-extensions-test-utils/dist/withProviders.d.ts +9 -0
  27. package/node_modules/@shopify/ui-extensions-test-utils/dist/withProviders.js +1 -0
  28. package/node_modules/@shopify/ui-extensions-test-utils/package.json +2 -3
  29. package/package.json +7 -6
  30. package/project.json +0 -2
  31. package/src/ExtensionServerClient/ExtensionServerClient.test.ts +837 -330
  32. package/src/ExtensionServerClient/ExtensionServerClient.ts +10 -8
  33. package/src/ExtensionServerClient/server-types.ts +55 -0
  34. package/src/ExtensionServerClient/types.ts +2 -0
  35. package/src/context/ExtensionServerProvider.test.tsx +202 -39
  36. package/src/context/ExtensionServerProvider.tsx +1 -0
  37. package/src/context/constants.ts +3 -2
  38. package/src/context/types.ts +1 -0
  39. package/src/hooks/index.ts +0 -2
  40. package/src/i18n.ts +3 -3
  41. package/src/state/reducers/extensionServerReducer.test.ts +2 -2
  42. package/src/testing/extensions.ts +2 -2
  43. package/src/types.ts +10 -4
  44. package/src/utilities/index.ts +0 -1
  45. package/src/utilities/replaceUpdated.ts +1 -0
  46. package/src/utilities/set.ts +1 -0
  47. package/dist/hooks/useExtensionClient.cjs.js +0 -1
  48. package/dist/hooks/useExtensionClient.d.ts +0 -1
  49. package/dist/hooks/useExtensionClient.es.js +0 -8
  50. package/dist/hooks/useExtensionServerEvent.cjs.js +0 -1
  51. package/dist/hooks/useExtensionServerEvent.d.ts +0 -1
  52. package/dist/hooks/useExtensionServerEvent.es.js +0 -9
  53. package/dist/utilities/groupByKey.cjs.js +0 -1
  54. package/dist/utilities/groupByKey.d.ts +0 -3
  55. package/dist/utilities/groupByKey.es.js +0 -6
  56. package/node_modules/@types/node/LICENSE +0 -21
  57. package/node_modules/@types/node/README.md +0 -15
  58. package/node_modules/@types/node/assert/strict.d.ts +0 -8
  59. package/node_modules/@types/node/assert.d.ts +0 -985
  60. package/node_modules/@types/node/async_hooks.d.ts +0 -522
  61. package/node_modules/@types/node/buffer.d.ts +0 -2321
  62. package/node_modules/@types/node/child_process.d.ts +0 -1544
  63. package/node_modules/@types/node/cluster.d.ts +0 -432
  64. package/node_modules/@types/node/console.d.ts +0 -412
  65. package/node_modules/@types/node/constants.d.ts +0 -19
  66. package/node_modules/@types/node/crypto.d.ts +0 -4451
  67. package/node_modules/@types/node/dgram.d.ts +0 -586
  68. package/node_modules/@types/node/diagnostics_channel.d.ts +0 -192
  69. package/node_modules/@types/node/dns/promises.d.ts +0 -381
  70. package/node_modules/@types/node/dns.d.ts +0 -809
  71. package/node_modules/@types/node/dom-events.d.ts +0 -122
  72. package/node_modules/@types/node/domain.d.ts +0 -170
  73. package/node_modules/@types/node/events.d.ts +0 -803
  74. package/node_modules/@types/node/fs/promises.d.ts +0 -1205
  75. package/node_modules/@types/node/fs.d.ts +0 -4211
  76. package/node_modules/@types/node/globals.d.ts +0 -377
  77. package/node_modules/@types/node/globals.global.d.ts +0 -1
  78. package/node_modules/@types/node/http.d.ts +0 -1801
  79. package/node_modules/@types/node/http2.d.ts +0 -2386
  80. package/node_modules/@types/node/https.d.ts +0 -544
  81. package/node_modules/@types/node/index.d.ts +0 -88
  82. package/node_modules/@types/node/inspector.d.ts +0 -2739
  83. package/node_modules/@types/node/module.d.ts +0 -298
  84. package/node_modules/@types/node/net.d.ts +0 -913
  85. package/node_modules/@types/node/os.d.ts +0 -473
  86. package/node_modules/@types/node/package.json +0 -235
  87. package/node_modules/@types/node/path.d.ts +0 -191
  88. package/node_modules/@types/node/perf_hooks.d.ts +0 -626
  89. package/node_modules/@types/node/process.d.ts +0 -1531
  90. package/node_modules/@types/node/punycode.d.ts +0 -117
  91. package/node_modules/@types/node/querystring.d.ts +0 -141
  92. package/node_modules/@types/node/readline/promises.d.ts +0 -143
  93. package/node_modules/@types/node/readline.d.ts +0 -666
  94. package/node_modules/@types/node/repl.d.ts +0 -430
  95. package/node_modules/@types/node/stream/consumers.d.ts +0 -12
  96. package/node_modules/@types/node/stream/promises.d.ts +0 -83
  97. package/node_modules/@types/node/stream/web.d.ts +0 -336
  98. package/node_modules/@types/node/stream.d.ts +0 -1731
  99. package/node_modules/@types/node/string_decoder.d.ts +0 -67
  100. package/node_modules/@types/node/test.d.ts +0 -1113
  101. package/node_modules/@types/node/timers/promises.d.ts +0 -93
  102. package/node_modules/@types/node/timers.d.ts +0 -126
  103. package/node_modules/@types/node/tls.d.ts +0 -1203
  104. package/node_modules/@types/node/trace_events.d.ts +0 -171
  105. package/node_modules/@types/node/ts4.8/assert/strict.d.ts +0 -8
  106. package/node_modules/@types/node/ts4.8/assert.d.ts +0 -985
  107. package/node_modules/@types/node/ts4.8/async_hooks.d.ts +0 -522
  108. package/node_modules/@types/node/ts4.8/buffer.d.ts +0 -2321
  109. package/node_modules/@types/node/ts4.8/child_process.d.ts +0 -1544
  110. package/node_modules/@types/node/ts4.8/cluster.d.ts +0 -432
  111. package/node_modules/@types/node/ts4.8/console.d.ts +0 -412
  112. package/node_modules/@types/node/ts4.8/constants.d.ts +0 -19
  113. package/node_modules/@types/node/ts4.8/crypto.d.ts +0 -4450
  114. package/node_modules/@types/node/ts4.8/dgram.d.ts +0 -586
  115. package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +0 -192
  116. package/node_modules/@types/node/ts4.8/dns/promises.d.ts +0 -381
  117. package/node_modules/@types/node/ts4.8/dns.d.ts +0 -809
  118. package/node_modules/@types/node/ts4.8/dom-events.d.ts +0 -122
  119. package/node_modules/@types/node/ts4.8/domain.d.ts +0 -170
  120. package/node_modules/@types/node/ts4.8/events.d.ts +0 -754
  121. package/node_modules/@types/node/ts4.8/fs/promises.d.ts +0 -1205
  122. package/node_modules/@types/node/ts4.8/fs.d.ts +0 -4211
  123. package/node_modules/@types/node/ts4.8/globals.d.ts +0 -377
  124. package/node_modules/@types/node/ts4.8/globals.global.d.ts +0 -1
  125. package/node_modules/@types/node/ts4.8/http.d.ts +0 -1801
  126. package/node_modules/@types/node/ts4.8/http2.d.ts +0 -2386
  127. package/node_modules/@types/node/ts4.8/https.d.ts +0 -544
  128. package/node_modules/@types/node/ts4.8/index.d.ts +0 -88
  129. package/node_modules/@types/node/ts4.8/inspector.d.ts +0 -2739
  130. package/node_modules/@types/node/ts4.8/module.d.ts +0 -298
  131. package/node_modules/@types/node/ts4.8/net.d.ts +0 -913
  132. package/node_modules/@types/node/ts4.8/os.d.ts +0 -473
  133. package/node_modules/@types/node/ts4.8/path.d.ts +0 -191
  134. package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +0 -626
  135. package/node_modules/@types/node/ts4.8/process.d.ts +0 -1531
  136. package/node_modules/@types/node/ts4.8/punycode.d.ts +0 -117
  137. package/node_modules/@types/node/ts4.8/querystring.d.ts +0 -141
  138. package/node_modules/@types/node/ts4.8/readline/promises.d.ts +0 -143
  139. package/node_modules/@types/node/ts4.8/readline.d.ts +0 -666
  140. package/node_modules/@types/node/ts4.8/repl.d.ts +0 -430
  141. package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +0 -12
  142. package/node_modules/@types/node/ts4.8/stream/promises.d.ts +0 -83
  143. package/node_modules/@types/node/ts4.8/stream/web.d.ts +0 -336
  144. package/node_modules/@types/node/ts4.8/stream.d.ts +0 -1731
  145. package/node_modules/@types/node/ts4.8/string_decoder.d.ts +0 -67
  146. package/node_modules/@types/node/ts4.8/test.d.ts +0 -1113
  147. package/node_modules/@types/node/ts4.8/timers/promises.d.ts +0 -93
  148. package/node_modules/@types/node/ts4.8/timers.d.ts +0 -126
  149. package/node_modules/@types/node/ts4.8/tls.d.ts +0 -1203
  150. package/node_modules/@types/node/ts4.8/trace_events.d.ts +0 -171
  151. package/node_modules/@types/node/ts4.8/tty.d.ts +0 -206
  152. package/node_modules/@types/node/ts4.8/url.d.ts +0 -937
  153. package/node_modules/@types/node/ts4.8/util.d.ts +0 -2075
  154. package/node_modules/@types/node/ts4.8/v8.d.ts +0 -541
  155. package/node_modules/@types/node/ts4.8/vm.d.ts +0 -667
  156. package/node_modules/@types/node/ts4.8/wasi.d.ts +0 -158
  157. package/node_modules/@types/node/ts4.8/worker_threads.d.ts +0 -692
  158. package/node_modules/@types/node/ts4.8/zlib.d.ts +0 -517
  159. package/node_modules/@types/node/tty.d.ts +0 -206
  160. package/node_modules/@types/node/url.d.ts +0 -937
  161. package/node_modules/@types/node/util.d.ts +0 -2075
  162. package/node_modules/@types/node/v8.d.ts +0 -541
  163. package/node_modules/@types/node/vm.d.ts +0 -667
  164. package/node_modules/@types/node/wasi.d.ts +0 -158
  165. package/node_modules/@types/node/worker_threads.d.ts +0 -692
  166. package/node_modules/@types/node/zlib.d.ts +0 -517
  167. package/src/hooks/useExtensionClient.ts +0 -6
  168. package/src/hooks/useExtensionServerEvent.ts +0 -11
  169. package/src/utilities/groupByKey.ts +0 -3
@@ -1,667 +0,0 @@
1
- /**
2
- * The `vm` module enables compiling and running code within V8 Virtual
3
- * Machine contexts.
4
- *
5
- * **The `vm` module is not a security**
6
- * **mechanism. Do not use it to run untrusted code.**
7
- *
8
- * JavaScript code can be compiled and run immediately or
9
- * compiled, saved, and run later.
10
- *
11
- * A common use case is to run the code in a different V8 Context. This means
12
- * invoked code has a different global object than the invoking code.
13
- *
14
- * One can provide the context by `contextifying` an
15
- * object. The invoked code treats any property in the context like a
16
- * global variable. Any changes to global variables caused by the invoked
17
- * code are reflected in the context object.
18
- *
19
- * ```js
20
- * const vm = require('vm');
21
- *
22
- * const x = 1;
23
- *
24
- * const context = { x: 2 };
25
- * vm.createContext(context); // Contextify the object.
26
- *
27
- * const code = 'x += 40; var y = 17;';
28
- * // `x` and `y` are global variables in the context.
29
- * // Initially, x has the value 2 because that is the value of context.x.
30
- * vm.runInContext(code, context);
31
- *
32
- * console.log(context.x); // 42
33
- * console.log(context.y); // 17
34
- *
35
- * console.log(x); // 1; y is not defined.
36
- * ```
37
- * @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/vm.js)
38
- */
39
- declare module "vm" {
40
- import { ImportAttributes } from "node:module";
41
- interface Context extends NodeJS.Dict<any> {}
42
- interface BaseOptions {
43
- /**
44
- * Specifies the filename used in stack traces produced by this script.
45
- * Default: `''`.
46
- */
47
- filename?: string | undefined;
48
- /**
49
- * Specifies the line number offset that is displayed in stack traces produced by this script.
50
- * Default: `0`.
51
- */
52
- lineOffset?: number | undefined;
53
- /**
54
- * Specifies the column number offset that is displayed in stack traces produced by this script.
55
- * @default 0
56
- */
57
- columnOffset?: number | undefined;
58
- }
59
- interface ScriptOptions extends BaseOptions {
60
- /**
61
- * V8's code cache data for the supplied source.
62
- */
63
- cachedData?: Buffer | NodeJS.ArrayBufferView | undefined;
64
- /** @deprecated in favor of `script.createCachedData()` */
65
- produceCachedData?: boolean | undefined;
66
- /**
67
- * Called during evaluation of this module when `import()` is called.
68
- * If this option is not specified, calls to `import()` will reject with `ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`.
69
- */
70
- importModuleDynamically?:
71
- | ((specifier: string, script: Script, importAttributes: ImportAttributes) => Module)
72
- | undefined;
73
- }
74
- interface RunningScriptOptions extends BaseOptions {
75
- /**
76
- * When `true`, if an `Error` occurs while compiling the `code`, the line of code causing the error is attached to the stack trace.
77
- * Default: `true`.
78
- */
79
- displayErrors?: boolean | undefined;
80
- /**
81
- * Specifies the number of milliseconds to execute code before terminating execution.
82
- * If execution is terminated, an `Error` will be thrown. This value must be a strictly positive integer.
83
- */
84
- timeout?: number | undefined;
85
- /**
86
- * If `true`, the execution will be terminated when `SIGINT` (Ctrl+C) is received.
87
- * Existing handlers for the event that have been attached via `process.on('SIGINT')` will be disabled during script execution, but will continue to work after that.
88
- * If execution is terminated, an `Error` will be thrown.
89
- * Default: `false`.
90
- */
91
- breakOnSigint?: boolean | undefined;
92
- }
93
- interface RunningScriptInNewContextOptions extends RunningScriptOptions {
94
- /**
95
- * Human-readable name of the newly created context.
96
- */
97
- contextName?: CreateContextOptions["name"];
98
- /**
99
- * Origin corresponding to the newly created context for display purposes. The origin should be formatted like a URL,
100
- * but with only the scheme, host, and port (if necessary), like the value of the `url.origin` property of a `URL` object.
101
- * Most notably, this string should omit the trailing slash, as that denotes a path.
102
- */
103
- contextOrigin?: CreateContextOptions["origin"];
104
- contextCodeGeneration?: CreateContextOptions["codeGeneration"];
105
- /**
106
- * If set to `afterEvaluate`, microtasks will be run immediately after the script has run.
107
- */
108
- microtaskMode?: CreateContextOptions["microtaskMode"];
109
- }
110
- interface RunningCodeOptions extends RunningScriptOptions {
111
- cachedData?: ScriptOptions["cachedData"];
112
- importModuleDynamically?: ScriptOptions["importModuleDynamically"];
113
- }
114
- interface RunningCodeInNewContextOptions extends RunningScriptInNewContextOptions {
115
- cachedData?: ScriptOptions["cachedData"];
116
- importModuleDynamically?: ScriptOptions["importModuleDynamically"];
117
- }
118
- interface CompileFunctionOptions extends BaseOptions {
119
- /**
120
- * Provides an optional data with V8's code cache data for the supplied source.
121
- */
122
- cachedData?: Buffer | undefined;
123
- /**
124
- * Specifies whether to produce new cache data.
125
- * Default: `false`,
126
- */
127
- produceCachedData?: boolean | undefined;
128
- /**
129
- * The sandbox/context in which the said function should be compiled in.
130
- */
131
- parsingContext?: Context | undefined;
132
- /**
133
- * An array containing a collection of context extensions (objects wrapping the current scope) to be applied while compiling
134
- */
135
- contextExtensions?: Object[] | undefined;
136
- }
137
- interface CreateContextOptions {
138
- /**
139
- * Human-readable name of the newly created context.
140
- * @default 'VM Context i' Where i is an ascending numerical index of the created context.
141
- */
142
- name?: string | undefined;
143
- /**
144
- * Corresponds to the newly created context for display purposes.
145
- * The origin should be formatted like a `URL`, but with only the scheme, host, and port (if necessary),
146
- * like the value of the `url.origin` property of a URL object.
147
- * Most notably, this string should omit the trailing slash, as that denotes a path.
148
- * @default ''
149
- */
150
- origin?: string | undefined;
151
- codeGeneration?:
152
- | {
153
- /**
154
- * If set to false any calls to eval or function constructors (Function, GeneratorFunction, etc)
155
- * will throw an EvalError.
156
- * @default true
157
- */
158
- strings?: boolean | undefined;
159
- /**
160
- * If set to false any attempt to compile a WebAssembly module will throw a WebAssembly.CompileError.
161
- * @default true
162
- */
163
- wasm?: boolean | undefined;
164
- }
165
- | undefined;
166
- /**
167
- * If set to `afterEvaluate`, microtasks will be run immediately after the script has run.
168
- */
169
- microtaskMode?: "afterEvaluate" | undefined;
170
- }
171
- type MeasureMemoryMode = "summary" | "detailed";
172
- interface MeasureMemoryOptions {
173
- /**
174
- * @default 'summary'
175
- */
176
- mode?: MeasureMemoryMode | undefined;
177
- /**
178
- * @default 'default'
179
- */
180
- execution?: "default" | "eager" | undefined;
181
- }
182
- interface MemoryMeasurement {
183
- total: {
184
- jsMemoryEstimate: number;
185
- jsMemoryRange: [number, number];
186
- };
187
- }
188
- /**
189
- * Instances of the `vm.Script` class contain precompiled scripts that can be
190
- * executed in specific contexts.
191
- * @since v0.3.1
192
- */
193
- class Script {
194
- constructor(code: string, options?: ScriptOptions | string);
195
- /**
196
- * Runs the compiled code contained by the `vm.Script` object within the given`contextifiedObject` and returns the result. Running code does not have access
197
- * to local scope.
198
- *
199
- * The following example compiles code that increments a global variable, sets
200
- * the value of another global variable, then execute the code multiple times.
201
- * The globals are contained in the `context` object.
202
- *
203
- * ```js
204
- * const vm = require('vm');
205
- *
206
- * const context = {
207
- * animal: 'cat',
208
- * count: 2
209
- * };
210
- *
211
- * const script = new vm.Script('count += 1; name = "kitty";');
212
- *
213
- * vm.createContext(context);
214
- * for (let i = 0; i < 10; ++i) {
215
- * script.runInContext(context);
216
- * }
217
- *
218
- * console.log(context);
219
- * // Prints: { animal: 'cat', count: 12, name: 'kitty' }
220
- * ```
221
- *
222
- * Using the `timeout` or `breakOnSigint` options will result in new event loops
223
- * and corresponding threads being started, which have a non-zero performance
224
- * overhead.
225
- * @since v0.3.1
226
- * @param contextifiedObject A `contextified` object as returned by the `vm.createContext()` method.
227
- * @return the result of the very last statement executed in the script.
228
- */
229
- runInContext(contextifiedObject: Context, options?: RunningScriptOptions): any;
230
- /**
231
- * First contextifies the given `contextObject`, runs the compiled code contained
232
- * by the `vm.Script` object within the created context, and returns the result.
233
- * Running code does not have access to local scope.
234
- *
235
- * The following example compiles code that sets a global variable, then executes
236
- * the code multiple times in different contexts. The globals are set on and
237
- * contained within each individual `context`.
238
- *
239
- * ```js
240
- * const vm = require('vm');
241
- *
242
- * const script = new vm.Script('globalVar = "set"');
243
- *
244
- * const contexts = [{}, {}, {}];
245
- * contexts.forEach((context) => {
246
- * script.runInNewContext(context);
247
- * });
248
- *
249
- * console.log(contexts);
250
- * // Prints: [{ globalVar: 'set' }, { globalVar: 'set' }, { globalVar: 'set' }]
251
- * ```
252
- * @since v0.3.1
253
- * @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
254
- * @return the result of the very last statement executed in the script.
255
- */
256
- runInNewContext(contextObject?: Context, options?: RunningScriptInNewContextOptions): any;
257
- /**
258
- * Runs the compiled code contained by the `vm.Script` within the context of the
259
- * current `global` object. Running code does not have access to local scope, but _does_ have access to the current `global` object.
260
- *
261
- * The following example compiles code that increments a `global` variable then
262
- * executes that code multiple times:
263
- *
264
- * ```js
265
- * const vm = require('vm');
266
- *
267
- * global.globalVar = 0;
268
- *
269
- * const script = new vm.Script('globalVar += 1', { filename: 'myfile.vm' });
270
- *
271
- * for (let i = 0; i < 1000; ++i) {
272
- * script.runInThisContext();
273
- * }
274
- *
275
- * console.log(globalVar);
276
- *
277
- * // 1000
278
- * ```
279
- * @since v0.3.1
280
- * @return the result of the very last statement executed in the script.
281
- */
282
- runInThisContext(options?: RunningScriptOptions): any;
283
- /**
284
- * Creates a code cache that can be used with the `Script` constructor's`cachedData` option. Returns a `Buffer`. This method may be called at any
285
- * time and any number of times.
286
- *
287
- * ```js
288
- * const script = new vm.Script(`
289
- * function add(a, b) {
290
- * return a + b;
291
- * }
292
- *
293
- * const x = add(1, 2);
294
- * `);
295
- *
296
- * const cacheWithoutX = script.createCachedData();
297
- *
298
- * script.runInThisContext();
299
- *
300
- * const cacheWithX = script.createCachedData();
301
- * ```
302
- * @since v10.6.0
303
- */
304
- createCachedData(): Buffer;
305
- /** @deprecated in favor of `script.createCachedData()` */
306
- cachedDataProduced?: boolean | undefined;
307
- cachedDataRejected?: boolean | undefined;
308
- cachedData?: Buffer | undefined;
309
- /**
310
- * When the script is compiled from a source that contains a source map magic comment, this property will be set to the URL of the source map.
311
- */
312
- sourceMapURL?: string | undefined;
313
- }
314
- /**
315
- * If given a `contextObject`, the `vm.createContext()` method will `prepare
316
- * that object` so that it can be used in calls to {@link runInContext} or `script.runInContext()`. Inside such scripts,
317
- * the `contextObject` will be the global object, retaining all of its existing
318
- * properties but also having the built-in objects and functions any standard [global object](https://es5.github.io/#x15.1) has. Outside of scripts run by the vm module, global variables
319
- * will remain unchanged.
320
- *
321
- * ```js
322
- * const vm = require('vm');
323
- *
324
- * global.globalVar = 3;
325
- *
326
- * const context = { globalVar: 1 };
327
- * vm.createContext(context);
328
- *
329
- * vm.runInContext('globalVar *= 2;', context);
330
- *
331
- * console.log(context);
332
- * // Prints: { globalVar: 2 }
333
- *
334
- * console.log(global.globalVar);
335
- * // Prints: 3
336
- * ```
337
- *
338
- * If `contextObject` is omitted (or passed explicitly as `undefined`), a new,
339
- * empty `contextified` object will be returned.
340
- *
341
- * The `vm.createContext()` method is primarily useful for creating a single
342
- * context that can be used to run multiple scripts. For instance, if emulating a
343
- * web browser, the method can be used to create a single context representing a
344
- * window's global object, then run all `<script>` tags together within that
345
- * context.
346
- *
347
- * The provided `name` and `origin` of the context are made visible through the
348
- * Inspector API.
349
- * @since v0.3.1
350
- * @return contextified object.
351
- */
352
- function createContext(sandbox?: Context, options?: CreateContextOptions): Context;
353
- /**
354
- * Returns `true` if the given `object` object has been `contextified` using {@link createContext}.
355
- * @since v0.11.7
356
- */
357
- function isContext(sandbox: Context): boolean;
358
- /**
359
- * The `vm.runInContext()` method compiles `code`, runs it within the context of
360
- * the `contextifiedObject`, then returns the result. Running code does not have
361
- * access to the local scope. The `contextifiedObject` object _must_ have been
362
- * previously `contextified` using the {@link createContext} method.
363
- *
364
- * If `options` is a string, then it specifies the filename.
365
- *
366
- * The following example compiles and executes different scripts using a single `contextified` object:
367
- *
368
- * ```js
369
- * const vm = require('vm');
370
- *
371
- * const contextObject = { globalVar: 1 };
372
- * vm.createContext(contextObject);
373
- *
374
- * for (let i = 0; i < 10; ++i) {
375
- * vm.runInContext('globalVar *= 2;', contextObject);
376
- * }
377
- * console.log(contextObject);
378
- * // Prints: { globalVar: 1024 }
379
- * ```
380
- * @since v0.3.1
381
- * @param code The JavaScript code to compile and run.
382
- * @param contextifiedObject The `contextified` object that will be used as the `global` when the `code` is compiled and run.
383
- * @return the result of the very last statement executed in the script.
384
- */
385
- function runInContext(code: string, contextifiedObject: Context, options?: RunningCodeOptions | string): any;
386
- /**
387
- * The `vm.runInNewContext()` first contextifies the given `contextObject` (or
388
- * creates a new `contextObject` if passed as `undefined`), compiles the `code`,
389
- * runs it within the created context, then returns the result. Running code
390
- * does not have access to the local scope.
391
- *
392
- * If `options` is a string, then it specifies the filename.
393
- *
394
- * The following example compiles and executes code that increments a global
395
- * variable and sets a new one. These globals are contained in the `contextObject`.
396
- *
397
- * ```js
398
- * const vm = require('vm');
399
- *
400
- * const contextObject = {
401
- * animal: 'cat',
402
- * count: 2
403
- * };
404
- *
405
- * vm.runInNewContext('count += 1; name = "kitty"', contextObject);
406
- * console.log(contextObject);
407
- * // Prints: { animal: 'cat', count: 3, name: 'kitty' }
408
- * ```
409
- * @since v0.3.1
410
- * @param code The JavaScript code to compile and run.
411
- * @param contextObject An object that will be `contextified`. If `undefined`, a new object will be created.
412
- * @return the result of the very last statement executed in the script.
413
- */
414
- function runInNewContext(
415
- code: string,
416
- contextObject?: Context,
417
- options?: RunningCodeInNewContextOptions | string,
418
- ): any;
419
- /**
420
- * `vm.runInThisContext()` compiles `code`, runs it within the context of the
421
- * current `global` and returns the result. Running code does not have access to
422
- * local scope, but does have access to the current `global` object.
423
- *
424
- * If `options` is a string, then it specifies the filename.
425
- *
426
- * The following example illustrates using both `vm.runInThisContext()` and
427
- * the JavaScript [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) function to run the same code:
428
- *
429
- * ```js
430
- * const vm = require('vm');
431
- * let localVar = 'initial value';
432
- *
433
- * const vmResult = vm.runInThisContext('localVar = "vm";');
434
- * console.log(`vmResult: '${vmResult}', localVar: '${localVar}'`);
435
- * // Prints: vmResult: 'vm', localVar: 'initial value'
436
- *
437
- * const evalResult = eval('localVar = "eval";');
438
- * console.log(`evalResult: '${evalResult}', localVar: '${localVar}'`);
439
- * // Prints: evalResult: 'eval', localVar: 'eval'
440
- * ```
441
- *
442
- * Because `vm.runInThisContext()` does not have access to the local scope,`localVar` is unchanged. In contrast,
443
- * [`eval()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval) _does_ have access to the
444
- * local scope, so the value `localVar` is changed. In this way`vm.runInThisContext()` is much like an [indirect `eval()` call](https://es5.github.io/#x10.4.2), e.g.`(0,eval)('code')`.
445
- *
446
- * ## Example: Running an HTTP server within a VM
447
- *
448
- * When using either `script.runInThisContext()` or {@link runInThisContext}, the code is executed within the current V8 global
449
- * context. The code passed to this VM context will have its own isolated scope.
450
- *
451
- * In order to run a simple web server using the `http` module the code passed to
452
- * the context must either call `require('http')` on its own, or have a reference
453
- * to the `http` module passed to it. For instance:
454
- *
455
- * ```js
456
- * 'use strict';
457
- * const vm = require('vm');
458
- *
459
- * const code = `
460
- * ((require) => {
461
- * const http = require('http');
462
- *
463
- * http.createServer((request, response) => {
464
- * response.writeHead(200, { 'Content-Type': 'text/plain' });
465
- * response.end('Hello World\\n');
466
- * }).listen(8124);
467
- *
468
- * console.log('Server running at http://127.0.0.1:8124/');
469
- * })`;
470
- *
471
- * vm.runInThisContext(code)(require);
472
- * ```
473
- *
474
- * The `require()` in the above case shares the state with the context it is
475
- * passed from. This may introduce risks when untrusted code is executed, e.g.
476
- * altering objects in the context in unwanted ways.
477
- * @since v0.3.1
478
- * @param code The JavaScript code to compile and run.
479
- * @return the result of the very last statement executed in the script.
480
- */
481
- function runInThisContext(code: string, options?: RunningCodeOptions | string): any;
482
- /**
483
- * Compiles the given code into the provided context (if no context is
484
- * supplied, the current context is used), and returns it wrapped inside a
485
- * function with the given `params`.
486
- * @since v10.10.0
487
- * @param code The body of the function to compile.
488
- * @param params An array of strings containing all parameters for the function.
489
- */
490
- function compileFunction(
491
- code: string,
492
- params?: readonly string[],
493
- options?: CompileFunctionOptions,
494
- ): Function & {
495
- cachedData?: Script["cachedData"] | undefined;
496
- cachedDataProduced?: Script["cachedDataProduced"] | undefined;
497
- cachedDataRejected?: Script["cachedDataRejected"] | undefined;
498
- };
499
- /**
500
- * Measure the memory known to V8 and used by all contexts known to the
501
- * current V8 isolate, or the main context.
502
- *
503
- * The format of the object that the returned Promise may resolve with is
504
- * specific to the V8 engine and may change from one version of V8 to the next.
505
- *
506
- * The returned result is different from the statistics returned by`v8.getHeapSpaceStatistics()` in that `vm.measureMemory()` measure the
507
- * memory reachable by each V8 specific contexts in the current instance of
508
- * the V8 engine, while the result of `v8.getHeapSpaceStatistics()` measure
509
- * the memory occupied by each heap space in the current V8 instance.
510
- *
511
- * ```js
512
- * const vm = require('vm');
513
- * // Measure the memory used by the main context.
514
- * vm.measureMemory({ mode: 'summary' })
515
- * // This is the same as vm.measureMemory()
516
- * .then((result) => {
517
- * // The current format is:
518
- * // {
519
- * // total: {
520
- * // jsMemoryEstimate: 2418479, jsMemoryRange: [ 2418479, 2745799 ]
521
- * // }
522
- * // }
523
- * console.log(result);
524
- * });
525
- *
526
- * const context = vm.createContext({ a: 1 });
527
- * vm.measureMemory({ mode: 'detailed', execution: 'eager' })
528
- * .then((result) => {
529
- * // Reference the context here so that it won't be GC'ed
530
- * // until the measurement is complete.
531
- * console.log(context.a);
532
- * // {
533
- * // total: {
534
- * // jsMemoryEstimate: 2574732,
535
- * // jsMemoryRange: [ 2574732, 2904372 ]
536
- * // },
537
- * // current: {
538
- * // jsMemoryEstimate: 2438996,
539
- * // jsMemoryRange: [ 2438996, 2768636 ]
540
- * // },
541
- * // other: [
542
- * // {
543
- * // jsMemoryEstimate: 135736,
544
- * // jsMemoryRange: [ 135736, 465376 ]
545
- * // }
546
- * // ]
547
- * // }
548
- * console.log(result);
549
- * });
550
- * ```
551
- * @since v13.10.0
552
- * @experimental
553
- */
554
- function measureMemory(options?: MeasureMemoryOptions): Promise<MemoryMeasurement>;
555
-
556
- interface ModuleEvaluateOptions {
557
- timeout?: RunningScriptOptions["timeout"] | undefined;
558
- breakOnSigint?: RunningScriptOptions["breakOnSigint"] | undefined;
559
- }
560
- type ModuleLinker = (
561
- specifier: string,
562
- referencingModule: Module,
563
- extra: {
564
- /** @deprecated Use `attributes` instead */
565
- assert: ImportAttributes;
566
- attributes: ImportAttributes;
567
- },
568
- ) => Module | Promise<Module>;
569
- type ModuleStatus = "unlinked" | "linking" | "linked" | "evaluating" | "evaluated" | "errored";
570
- class Module {
571
- /**
572
- * The specifiers of all dependencies of this module.
573
- */
574
- dependencySpecifiers: readonly string[];
575
- /**
576
- * If the `module.status` is `'errored'`, this property contains the exception thrown by the module during evaluation.
577
- * If the status is anything else, accessing this property will result in a thrown exception.
578
- */
579
- error: any;
580
- /**
581
- * The identifier of the current module, as set in the constructor.
582
- */
583
- identifier: string;
584
- context: Context;
585
- /**
586
- * The namespace object of the module. This is only available after linking (`module.link()`) has completed.
587
- */
588
- namespace: Object;
589
- /**
590
- * The current status of the module.
591
- */
592
- status: ModuleStatus;
593
- /**
594
- * Evaluate the module.
595
- *
596
- * This must be called after the module has been linked; otherwise it will reject
597
- * It could be called also when the module has already been evaluated, in which case it will either do nothing
598
- * if the initial evaluation ended in success (`module.status` is `'evaluated'`) or it will re-throw the exception
599
- * that the initial evaluation resulted in (`module.status` is `'errored'`).
600
- *
601
- * This method cannot be called while the module is being evaluated (`module.status` is `'evaluating'`).
602
- */
603
- evaluate(options?: ModuleEvaluateOptions): Promise<void>;
604
- /**
605
- * Link module dependencies. This method must be called before evaluation, and can only be called once per module.
606
- */
607
- link(linker: ModuleLinker): Promise<void>;
608
- }
609
-
610
- interface SourceTextModuleOptions {
611
- /**
612
- * String used in stack traces.
613
- * @default 'vm:module(i)' where i is a context-specific ascending index.
614
- */
615
- identifier?: string | undefined;
616
- cachedData?: ScriptOptions["cachedData"] | undefined;
617
- context?: Context | undefined;
618
- lineOffset?: BaseOptions["lineOffset"] | undefined;
619
- columnOffset?: BaseOptions["columnOffset"] | undefined;
620
- /**
621
- * Called during evaluation of this module to initialize the `import.meta`.
622
- */
623
- initializeImportMeta?: ((meta: ImportMeta, module: SourceTextModule) => void) | undefined;
624
- importModuleDynamically?: ScriptOptions["importModuleDynamically"] | undefined;
625
- }
626
- class SourceTextModule extends Module {
627
- /**
628
- * Creates a new `SourceTextModule` instance.
629
- * @param code JavaScript Module code to parse
630
- */
631
- constructor(code: string, options?: SourceTextModuleOptions);
632
- }
633
-
634
- interface SyntheticModuleOptions {
635
- /**
636
- * String used in stack traces.
637
- * @default 'vm:module(i)' where i is a context-specific ascending index.
638
- */
639
- identifier?: string | undefined;
640
- /**
641
- * The contextified object as returned by the `vm.createContext()` method, to compile and evaluate this module in.
642
- */
643
- context?: Context | undefined;
644
- }
645
- class SyntheticModule extends Module {
646
- /**
647
- * Creates a new `SyntheticModule` instance.
648
- * @param exportNames Array of names that will be exported from the module.
649
- * @param evaluateCallback Called when the module is evaluated.
650
- */
651
- constructor(
652
- exportNames: string[],
653
- evaluateCallback: (this: SyntheticModule) => void,
654
- options?: SyntheticModuleOptions,
655
- );
656
- /**
657
- * This method is used after the module is linked to set the values of exports.
658
- * If it is called before the module is linked, an `ERR_VM_MODULE_STATUS` error will be thrown.
659
- * @param name
660
- * @param value
661
- */
662
- setExport(name: string, value: any): void;
663
- }
664
- }
665
- declare module "node:vm" {
666
- export * from "vm";
667
- }