@edge-runtime/vm
Advanced tools
Comparing version 1.1.0-beta.11 to 1.1.0-beta.12
@@ -1,4 +0,3 @@ | ||
import type { ExtendedDictionary } from './types'; | ||
import type { Primitives } from '@edge-runtime/primitives'; | ||
import type { VMOptions } from './vm'; | ||
import type * as EdgePrimitives from '@edge-runtime/primitives'; | ||
import type { VMContext, VMOptions } from './vm'; | ||
import { VM } from './vm'; | ||
@@ -15,3 +14,3 @@ export interface EdgeVMOptions<T> { | ||
*/ | ||
extend?: (context: ExtendedDictionary<Primitives>) => ExtendedDictionary<T>; | ||
extend?: (context: EdgeContext) => EdgeContext & T; | ||
/** | ||
@@ -27,4 +26,44 @@ * Provides an initial map to the require cache. | ||
*/ | ||
export declare class EdgeVM<T extends Primitives> extends VM<T> { | ||
export declare class EdgeVM<T extends EdgeContext> extends VM<T> { | ||
constructor(options?: EdgeVMOptions<T>); | ||
} | ||
export declare type EdgeContext = VMContext & { | ||
self: EdgeContext; | ||
globalThis: EdgeContext; | ||
AbortController: typeof EdgePrimitives.AbortController; | ||
AbortSignal: typeof EdgePrimitives.AbortSignal; | ||
atob: typeof EdgePrimitives.atob; | ||
Blob: typeof EdgePrimitives.Blob; | ||
btoa: typeof EdgePrimitives.btoa; | ||
Cache: typeof EdgePrimitives.Cache; | ||
caches: typeof EdgePrimitives.caches; | ||
CacheStorage: typeof EdgePrimitives.CacheStorage; | ||
console: typeof EdgePrimitives.console; | ||
createCaches: typeof EdgePrimitives.createCaches; | ||
crypto: typeof EdgePrimitives.crypto; | ||
Crypto: typeof EdgePrimitives.Crypto; | ||
CryptoKey: typeof EdgePrimitives.CryptoKey; | ||
Event: typeof EdgePrimitives.Event; | ||
EventTarget: typeof EdgePrimitives.EventTarget; | ||
fetch: typeof EdgePrimitives.fetch; | ||
FetchEvent: typeof EdgePrimitives.FetchEvent; | ||
File: typeof EdgePrimitives.File; | ||
FormData: typeof EdgePrimitives.FormData; | ||
Headers: typeof EdgePrimitives.Headers; | ||
PromiseRejectionEvent: typeof EdgePrimitives.PromiseRejectionEvent; | ||
ReadableStream: typeof EdgePrimitives.ReadableStream; | ||
ReadableStreamBYOBReader: typeof EdgePrimitives.ReadableStreamBYOBReader; | ||
ReadableStreamDefaultReader: typeof EdgePrimitives.ReadableStreamDefaultReader; | ||
Request: typeof EdgePrimitives.Request; | ||
Response: typeof EdgePrimitives.Response; | ||
structuredClone: typeof EdgePrimitives.structuredClone; | ||
SubtleCrypto: typeof EdgePrimitives.SubtleCrypto; | ||
TextDecoder: typeof EdgePrimitives.TextDecoder; | ||
TextEncoder: typeof EdgePrimitives.TextEncoder; | ||
TransformStream: typeof EdgePrimitives.TransformStream; | ||
URL: typeof EdgePrimitives.URL; | ||
URLPattern: typeof EdgePrimitives.URLPattern; | ||
URLSearchParams: typeof EdgePrimitives.URLSearchParams; | ||
WritableStream: typeof EdgePrimitives.WritableStream; | ||
WritableStreamDefaultWriter: typeof EdgePrimitives.WritableStreamDefaultWriter; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EdgeVM = void 0; | ||
const primitives_1 = require("@edge-runtime/primitives"); | ||
const buffer_1 = require("buffer"); | ||
const require_1 = require("./require"); | ||
const vm_1 = require("./vm"); | ||
@@ -16,4 +17,4 @@ /** | ||
return options.extend | ||
? options.extend((0, primitives_1.addPrimitives)(context)) | ||
: (0, primitives_1.addPrimitives)(context); | ||
? options.extend(addPrimitives(context)) | ||
: addPrimitives(context); | ||
}, | ||
@@ -24,2 +25,209 @@ }); | ||
exports.EdgeVM = EdgeVM; | ||
function addPrimitives(context) { | ||
defineProperty(context, 'self', { enumerable: true, value: context }); | ||
defineProperty(context, 'globalThis', { value: context }); | ||
defineProperty(context, 'Symbol', { value: Symbol }); | ||
defineProperty(context, 'clearInterval', { value: clearInterval }); | ||
defineProperty(context, 'clearTimeout', { value: clearTimeout }); | ||
defineProperty(context, 'setInterval', { value: setInterval }); | ||
defineProperty(context, 'setTimeout', { value: setTimeout }); | ||
// Console | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/console'), | ||
scopedContext: { console: console }, | ||
}), | ||
nonenumerable: ['console'], | ||
}); | ||
// Encoding APIs | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/encoding'), | ||
scopedContext: { Buffer: buffer_1.Buffer }, | ||
}), | ||
nonenumerable: ['atob', 'btoa', 'TextEncoder', 'TextDecoder'], | ||
}); | ||
const streams = (0, require_1.requireWithCache)({ | ||
path: require.resolve('@edge-runtime/primitives/streams'), | ||
context, | ||
}); | ||
// Streams | ||
defineProperties(context, { | ||
exports: streams, | ||
nonenumerable: [ | ||
'ReadableStream', | ||
'ReadableStreamBYOBReader', | ||
'ReadableStreamDefaultReader', | ||
'TransformStream', | ||
'WritableStream', | ||
'WritableStreamDefaultWriter', | ||
], | ||
}); | ||
const abort = (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/abort-controller'), | ||
}); | ||
// AbortController | ||
defineProperties(context, { | ||
exports: abort, | ||
nonenumerable: ['AbortController', 'AbortSignal'], | ||
}); | ||
// URL | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
cache: new Map([ | ||
['tr46', { exports: require('tr46') }], | ||
['punycode', { exports: require('punycode') }], | ||
]), | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/url'), | ||
}), | ||
nonenumerable: ['URL', 'URLSearchParams', 'URLPattern'], | ||
}); | ||
const blob = (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/blob'), | ||
}); | ||
// Blob | ||
defineProperties(context, { | ||
exports: blob, | ||
nonenumerable: ['Blob'], | ||
}); | ||
const webFetch = (0, require_1.requireWithCache)({ | ||
context, | ||
cache: new Map([ | ||
['abort-controller', { exports: abort }], | ||
['assert', { exports: require('assert') }], | ||
['buffer', { exports: require('buffer') }], | ||
['events', { exports: require('events') }], | ||
['http', { exports: require('http') }], | ||
['net', { exports: require('net') }], | ||
['perf_hooks', { exports: require('perf_hooks') }], | ||
['stream', { exports: require('stream') }], | ||
['tls', { exports: require('tls') }], | ||
['util', { exports: require('util') }], | ||
['zlib', { exports: require('zlib') }], | ||
[ | ||
require.resolve('@edge-runtime/primitives/streams'), | ||
{ exports: streams }, | ||
], | ||
[require.resolve('@edge-runtime/primitives/blob'), { exports: blob }], | ||
]), | ||
path: require.resolve('@edge-runtime/primitives/fetch'), | ||
scopedContext: { | ||
Buffer: buffer_1.Buffer, | ||
FinalizationRegistry: function () { | ||
return { register: function () { } }; | ||
}, | ||
global: {}, | ||
queueMicrotask: queueMicrotask, | ||
}, | ||
}); | ||
// Fetch APIs | ||
defineProperties(context, { | ||
exports: webFetch, | ||
nonenumerable: [ | ||
'fetch', | ||
'File', | ||
'FormData', | ||
'Headers', | ||
'Request', | ||
'Response', | ||
], | ||
}); | ||
// Cache | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
cache: new Map([ | ||
[ | ||
require.resolve('@edge-runtime/primitives/fetch'), | ||
{ exports: webFetch }, | ||
], | ||
]), | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/cache'), | ||
scopedContext: { global: {} }, | ||
}), | ||
enumerable: ['caches'], | ||
nonenumerable: ['Cache', 'CacheStorage'], | ||
}); | ||
// Crypto | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
context, | ||
cache: new Map([ | ||
[ | ||
'@peculiar/asn1-schema', | ||
{ exports: require('@peculiar/asn1-schema') }, | ||
], | ||
[ | ||
'@peculiar/json-schema', | ||
{ exports: require('@peculiar/json-schema') }, | ||
], | ||
['crypto', { exports: require('crypto') }], | ||
['process', { exports: require('process') }], | ||
['pvtsutils', { exports: require('pvtsutils') }], | ||
['tslib', { exports: require('tslib') }], | ||
['webcrypto-core', { exports: require('webcrypto-core') }], | ||
]), | ||
path: require.resolve('@edge-runtime/primitives/crypto'), | ||
scopedContext: { Buffer: buffer_1.Buffer }, | ||
}), | ||
enumerable: ['crypto'], | ||
nonenumerable: ['Crypto', 'CryptoKey', 'SubtleCrypto'], | ||
}); | ||
// Events | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/events'), | ||
}), | ||
nonenumerable: [ | ||
'Event', | ||
'EventTarget', | ||
'FetchEvent', | ||
'PromiseRejectionEvent', | ||
], | ||
}); | ||
// Structured Clone | ||
defineProperties(context, { | ||
exports: (0, require_1.requireWithCache)({ | ||
context, | ||
path: require.resolve('@edge-runtime/primitives/structured-clone'), | ||
}), | ||
nonenumerable: ['structuredClone'], | ||
}); | ||
return context; | ||
} | ||
function defineProperty(obj, prop, attrs) { | ||
var _a, _b, _c; | ||
Object.defineProperty(obj, prop, { | ||
configurable: (_a = attrs.configurable) !== null && _a !== void 0 ? _a : false, | ||
enumerable: (_b = attrs.enumerable) !== null && _b !== void 0 ? _b : false, | ||
value: attrs.value, | ||
writable: (_c = attrs.writable) !== null && _c !== void 0 ? _c : true, | ||
}); | ||
} | ||
function defineProperties(context, options) { | ||
var _a, _b; | ||
for (const property of (_a = options.enumerable) !== null && _a !== void 0 ? _a : []) { | ||
if (!options.exports[property]) { | ||
throw new Error(`Attempt to export a nullable value for "${property}"`); | ||
} | ||
defineProperty(context, property, { | ||
enumerable: true, | ||
value: options.exports[property], | ||
}); | ||
} | ||
for (const property of (_b = options.nonenumerable) !== null && _b !== void 0 ? _b : []) { | ||
if (!options.exports[property]) { | ||
throw new Error(`Attempt to export a nullable value for "${property}"`); | ||
} | ||
defineProperty(context, property, { | ||
value: options.exports[property], | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=edge-vm.js.map |
@@ -1,6 +0,4 @@ | ||
export type { EdgeVMOptions } from './edge-vm'; | ||
export type { ExtendedDictionary } from './types'; | ||
export type { Primitives } from '@edge-runtime/primitives'; | ||
export type { VMOptions } from './vm'; | ||
export type { EdgeVMOptions, EdgeContext } from './edge-vm'; | ||
export type { VMOptions, VMContext } from './vm'; | ||
export { EdgeVM } from './edge-vm'; | ||
export { VM } from './vm'; |
@@ -19,2 +19,9 @@ /// <reference types="node" /> | ||
}): void; | ||
export declare function createRequire(context: Context, cache: Map<string, any>): (referrer: string, specifier: string) => any; | ||
export declare function createRequire(context: Context, cache: Map<string, any>, references?: Set<string>, scopedContext?: Record<any, any>): (referrer: string, specifier: string) => any; | ||
export declare function requireWithCache(params: { | ||
cache?: Map<string, any>; | ||
context: Context; | ||
path: string; | ||
references?: Set<string>; | ||
scopedContext?: Record<string, any>; | ||
}): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRequire = exports.requireDependencies = void 0; | ||
exports.requireWithCache = exports.createRequire = exports.requireDependencies = void 0; | ||
const fs_1 = require("fs"); | ||
@@ -23,3 +23,3 @@ const vm_1 = require("vm"); | ||
exports.requireDependencies = requireDependencies; | ||
function createRequire(context, cache) { | ||
function createRequire(context, cache, references, scopedContext = {}) { | ||
return function requireFn(referrer, specifier) { | ||
@@ -29,4 +29,4 @@ const resolved = require.resolve(specifier, { | ||
}); | ||
const cached = cache.get(resolved); | ||
if (cached !== undefined) { | ||
const cached = cache.get(specifier) || cache.get(resolved); | ||
if (cached !== undefined && cached !== null) { | ||
return cached.exports; | ||
@@ -40,8 +40,10 @@ } | ||
cache.set(resolved, module); | ||
const fn = (0, vm_1.runInContext)(`(function(module,exports,require,__dirname,__filename) {${(0, fs_1.readFileSync)(resolved, 'utf-8')}\n})`, context); | ||
references === null || references === void 0 ? void 0 : references.add(resolved); | ||
const fn = (0, vm_1.runInContext)(`(function(module,exports,require,__dirname,__filename,${Object.keys(scopedContext).join(',')}) {${(0, fs_1.readFileSync)(resolved, 'utf-8')}\n})`, context); | ||
try { | ||
fn(module, module.exports, requireFn.bind(null, resolved), (0, path_1.dirname)(resolved), resolved); | ||
fn(module, module.exports, requireFn.bind(null, resolved), (0, path_1.dirname)(resolved), resolved, ...Object.values(scopedContext)); | ||
} | ||
finally { | ||
catch (error) { | ||
cache.delete(resolved); | ||
throw error; | ||
} | ||
@@ -53,2 +55,7 @@ module.loaded = true; | ||
exports.createRequire = createRequire; | ||
function requireWithCache(params) { | ||
var _a; | ||
return createRequire(params.context, (_a = params.cache) !== null && _a !== void 0 ? _a : new Map(), params.references, params.scopedContext).call(null, params.path, params.path); | ||
} | ||
exports.requireWithCache = requireWithCache; | ||
//# sourceMappingURL=require.js.map |
@@ -7,7 +7,6 @@ "use strict"; | ||
exports.tempFile = void 0; | ||
const crypto_1 = require("@edge-runtime/primitives/crypto"); | ||
const path_1 = __importDefault(require("path")); | ||
const fs_1 = __importDefault(require("fs")); | ||
const os_1 = __importDefault(require("os")); | ||
const path_1 = __importDefault(require("path")); | ||
const primitives_1 = __importDefault(require("@edge-runtime/primitives")); | ||
const { crypto } = primitives_1.default; | ||
/** | ||
@@ -19,3 +18,3 @@ * Creates a temporary file with the provided content and returns the | ||
function tempFile(code) { | ||
const filepath = path_1.default.join(os_1.default.tmpdir(), crypto.randomUUID()); | ||
const filepath = path_1.default.join(os_1.default.tmpdir(), crypto_1.crypto.randomUUID()); | ||
fs_1.default.writeFileSync(filepath, code); | ||
@@ -22,0 +21,0 @@ return { |
@@ -8,9 +8,1 @@ /** | ||
} | ||
/** | ||
* Extends the Dictionary type to allow having arbitrary keys that can hold | ||
* any type of data. This would represent an object where we can add extra | ||
* properties what would not be typed. | ||
*/ | ||
export declare type ExtendedDictionary<T extends Dictionary> = T & { | ||
[key: string | number]: any; | ||
}; |
/// <reference types="node" /> | ||
import type { Context as VMContext, CreateContextOptions } from 'vm'; | ||
import type { Dictionary, ExtendedDictionary } from './types'; | ||
import type { CreateContextOptions } from 'vm'; | ||
import type { Dictionary } from './types'; | ||
export interface VMOptions<T> { | ||
@@ -14,3 +14,3 @@ /** | ||
*/ | ||
extend?: (context: VMContext) => ExtendedDictionary<T>; | ||
extend?: (context: VMContext) => VMContext & T; | ||
/** | ||
@@ -30,3 +30,3 @@ * Provides an initial map to the require cache. | ||
readonly requireCache: Map<string, Dictionary>; | ||
readonly context: ExtendedDictionary<T>; | ||
readonly context: VMContext & T; | ||
constructor(options?: VMOptions<T>); | ||
@@ -55,1 +55,56 @@ /** | ||
} | ||
export interface VMContext { | ||
Array: typeof Array; | ||
ArrayBuffer: typeof ArrayBuffer; | ||
Atomics: typeof Atomics; | ||
BigInt: typeof BigInt; | ||
BigInt64Array: typeof BigInt64Array; | ||
BigUint64Array: typeof BigUint64Array; | ||
Boolean: typeof Boolean; | ||
DataView: typeof DataView; | ||
Date: typeof Date; | ||
decodeURI: typeof decodeURI; | ||
decodeURIComponent: typeof decodeURIComponent; | ||
encodeURI: typeof encodeURI; | ||
encodeURIComponent: typeof encodeURIComponent; | ||
Error: typeof Error; | ||
EvalError: typeof EvalError; | ||
Float32Array: typeof Float32Array; | ||
Float64Array: typeof Float64Array; | ||
Function: typeof Function; | ||
Infinity: typeof Infinity; | ||
Int8Array: typeof Int8Array; | ||
Int16Array: typeof Int16Array; | ||
Int32Array: typeof Int32Array; | ||
Intl: typeof Intl; | ||
isFinite: typeof isFinite; | ||
isNaN: typeof isNaN; | ||
JSON: typeof JSON; | ||
Map: typeof Map; | ||
Math: typeof Math; | ||
Number: typeof Number; | ||
Object: typeof Object; | ||
parseFloat: typeof parseFloat; | ||
parseInt: typeof parseInt; | ||
Promise: typeof Promise; | ||
Proxy: typeof Proxy; | ||
RangeError: typeof RangeError; | ||
ReferenceError: typeof ReferenceError; | ||
Reflect: typeof Reflect; | ||
RegExp: typeof RegExp; | ||
Set: typeof Set; | ||
SharedArrayBuffer: typeof SharedArrayBuffer; | ||
String: typeof String; | ||
Symbol: typeof Symbol; | ||
SyntaxError: typeof SyntaxError; | ||
TypeError: typeof TypeError; | ||
Uint8Array: typeof Uint8Array; | ||
Uint8ClampedArray: typeof Uint8ClampedArray; | ||
Uint16Array: typeof Uint16Array; | ||
Uint32Array: typeof Uint32Array; | ||
URIError: typeof URIError; | ||
WeakMap: typeof WeakMap; | ||
WeakSet: typeof WeakSet; | ||
WebAssembly: typeof WebAssembly; | ||
[key: string | number]: any; | ||
} |
@@ -19,3 +19,3 @@ "use strict"; | ||
strings: false, | ||
wasm: false, | ||
wasm: true, | ||
}, | ||
@@ -22,0 +22,0 @@ }); |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://edge-runtime.vercel.app/packages/vm", | ||
"version": "1.1.0-beta.11", | ||
"version": "1.1.0-beta.12", | ||
"main": "dist/index.js", | ||
@@ -26,3 +26,3 @@ "repository": { | ||
"dependencies": { | ||
"@edge-runtime/primitives": "^1.1.0-beta.11" | ||
"@edge-runtime/primitives": "^1.1.0-beta.12" | ||
}, | ||
@@ -34,2 +34,5 @@ "files": [ | ||
"build": "tsc --project ./tsconfig.prod.json", | ||
"clean": "pnpm run clean:node && pnpm run clean:build", | ||
"clean:build": "rm -rf dist", | ||
"clean:node": "rm -rf node_modules", | ||
"test": "jest" | ||
@@ -53,3 +56,3 @@ }, | ||
"types": "dist/index.d.ts", | ||
"gitHead": "5381871804e3d5c58a61154a82296e0a7096afb9" | ||
"gitHead": "0e0c9d189a83b070551b04c094631f9e0b2cc8d1" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 3 instances in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69079
600
6
3