@edge-runtime/vm
Advanced tools
Comparing version 2.2.0-beta.2 to 2.2.0-beta.3
@@ -1,2 +0,2 @@ | ||
import * as EdgePrimitives from '@edge-runtime/primitives'; | ||
import type * as EdgePrimitives from '@edge-runtime/primitives'; | ||
import type { DispatchFetch } from './types'; | ||
@@ -3,0 +3,0 @@ import { VM, type VMContext, type VMOptions } from './vm'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EdgeVM = void 0; | ||
const EdgePrimitives = __importStar(require("@edge-runtime/primitives")); | ||
const load_1 = require("@edge-runtime/primitives/load"); | ||
const vm_1 = require("vm"); | ||
@@ -68,4 +45,4 @@ const vm_2 = require("./vm"); | ||
this.dispatchFetch = this.evaluate(getDispatchFetchCode()); | ||
for (const item of constructorsToPatchInstanceOf) { | ||
patchInstanceOf(item, this.context); | ||
for (const name of transferableConstructors) { | ||
patchInstanceOf(name, this.context); | ||
} | ||
@@ -79,9 +56,16 @@ if (options === null || options === void 0 ? void 0 : options.initialCode) { | ||
/** | ||
* A list of constructors that need to be patched to make sure that the | ||
* `instanceof` operator works as expected from within the vm context, | ||
* even when passing it objects that were created in the Node.js realm. | ||
* Transferable constructors are the constructors that we expect to be | ||
* "shared" between the realms. | ||
* | ||
* Example: the return value from `new TextEncoder().encode("hello")` is a | ||
* Uint8Array. If `TextEncoder` is coming from the Node.js realm, then the | ||
* following will be false, which doesn't fit the expectation of the user: | ||
* When a user creates an instance of one of these constructors, we want | ||
* to make sure that the `instanceof` operator works as expected: | ||
* | ||
* * If the instance was created in the Node.js realm, then `instanceof` | ||
* should return true when used in the EdgeVM realm. | ||
* * If the instance was created in the EdgeVM realm, then `instanceof` | ||
* should return true when used in the EdgeVM realm. | ||
* | ||
* For example, the return value from `new TextEncoder().encode("hello")` is a | ||
* Uint8Array. Since `TextEncoder` implementation is coming from the Node.js realm, | ||
* therefore the following will be false, which doesn't fit the expectation of the user: | ||
* ```ts | ||
@@ -96,4 +80,8 @@ * new TextEncoder().encode("hello") instanceof Uint8Array | ||
* function, and this is the list of constructors that need to be patched. | ||
* | ||
* These constructors are also being injected as "globals" when the VM is | ||
* constructed, by passing them as arguments to the {@link loadPrimitives} | ||
* function. | ||
*/ | ||
const constructorsToPatchInstanceOf = [ | ||
const transferableConstructors = [ | ||
'Object', | ||
@@ -254,4 +242,8 @@ 'Array', | ||
defineProperty(context, 'EdgeRuntime', { value: 'edge-runtime' }); | ||
const transferables = getTransferablePrimitivesFromContext(context); | ||
defineProperties(context, { | ||
exports: EdgePrimitives, | ||
exports: (0, load_1.load)({ | ||
...transferables, | ||
WeakRef: (0, vm_1.runInContext)(`WeakRef`, context), | ||
}), | ||
enumerable: ['crypto'], | ||
@@ -337,2 +329,11 @@ nonenumerable: [ | ||
} | ||
/** | ||
* Create an object that contains all the {@link transferableConstructors} | ||
* implemented in the provided context. | ||
*/ | ||
function getTransferablePrimitivesFromContext(context) { | ||
const keys = transferableConstructors.join(','); | ||
const stringifedObject = `({${keys}})`; | ||
return (0, vm_1.runInContext)(stringifedObject, context); | ||
} | ||
//# sourceMappingURL=edge-vm.js.map |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://edge-runtime.vercel.app/packages/vm", | ||
"version": "2.2.0-beta.2", | ||
"version": "2.2.0-beta.3", | ||
"main": "dist/index.js", | ||
@@ -38,3 +38,3 @@ "repository": { | ||
"dependencies": { | ||
"@edge-runtime/primitives": "2.2.0-beta.2" | ||
"@edge-runtime/primitives": "2.2.0-beta.3" | ||
}, | ||
@@ -41,0 +41,0 @@ "devDependencies": { |
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
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
76609
696
+ Added@edge-runtime/primitives@2.2.0-beta.3(transitive)
- Removed@edge-runtime/primitives@2.2.0-beta.2(transitive)