.
This commit is contained in:
+437
@@ -0,0 +1,437 @@
|
||||
import { AsyncLocalStorage } from 'async_hooks';
|
||||
import { COMPILER_NAMES, EDGE_UNSUPPORTED_NODE_APIS } from '../../../shared/lib/constants';
|
||||
import { EdgeRuntime } from 'next/dist/compiled/edge-runtime';
|
||||
import { readFileSync, promises as fs } from 'fs';
|
||||
import { validateURL } from '../utils';
|
||||
import { pick } from '../../../lib/pick';
|
||||
import { fetchInlineAsset } from './fetch-inline-assets';
|
||||
import { runInContext } from 'vm';
|
||||
import BufferImplementation from 'node:buffer';
|
||||
import EventsImplementation from 'node:events';
|
||||
import AssertImplementation from 'node:assert';
|
||||
import UtilImplementation from 'node:util';
|
||||
import AsyncHooksImplementation from 'node:async_hooks';
|
||||
import { intervalsManager, timeoutsManager } from './resource-managers';
|
||||
import { createLocalRequestContext } from '../../after/builtin-request-context';
|
||||
import { patchErrorInspectEdgeLite, patchErrorInspectNodeJS } from '../../patch-error-inspect';
|
||||
let getServerError;
|
||||
let decorateServerError;
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
getServerError = require('../../dev/node-stack-frames').getServerError;
|
||||
decorateServerError = require('../../../shared/lib/error-source').decorateServerError;
|
||||
} else {
|
||||
getServerError = (error)=>error;
|
||||
decorateServerError = ()=>{};
|
||||
}
|
||||
/**
|
||||
* A Map of cached module contexts indexed by the module name. It allows
|
||||
* to have a different cache scoped per module name or depending on the
|
||||
* provided module key on creation.
|
||||
*/ const moduleContexts = new Map();
|
||||
const pendingModuleCaches = new Map();
|
||||
/**
|
||||
* Same as clearModuleContext but for all module contexts.
|
||||
*/ export async function clearAllModuleContexts() {
|
||||
intervalsManager.removeAll();
|
||||
timeoutsManager.removeAll();
|
||||
moduleContexts.clear();
|
||||
pendingModuleCaches.clear();
|
||||
}
|
||||
/**
|
||||
* For a given path a context, this function checks if there is any module
|
||||
* context that contains the path with an older content and, if that's the
|
||||
* case, removes the context from the cache.
|
||||
*
|
||||
* This function also clears all intervals and timeouts created by the
|
||||
* module context.
|
||||
*/ export async function clearModuleContext(path) {
|
||||
intervalsManager.removeAll();
|
||||
timeoutsManager.removeAll();
|
||||
const handleContext = (key, cache, context)=>{
|
||||
if (cache == null ? void 0 : cache.paths.has(path)) {
|
||||
context.delete(key);
|
||||
}
|
||||
};
|
||||
for (const [key, cache] of moduleContexts){
|
||||
handleContext(key, cache, moduleContexts);
|
||||
}
|
||||
for (const [key, cache] of pendingModuleCaches){
|
||||
handleContext(key, await cache, pendingModuleCaches);
|
||||
}
|
||||
}
|
||||
async function loadWasm(wasm) {
|
||||
const modules = {};
|
||||
await Promise.all(wasm.map(async (binding)=>{
|
||||
const module = await WebAssembly.compile(// @ts-expect-error - Argument of type 'Buffer<ArrayBufferLike>' is not assignable to parameter of type 'BufferSource'.
|
||||
await fs.readFile(binding.filePath));
|
||||
modules[binding.name] = module;
|
||||
}));
|
||||
return modules;
|
||||
}
|
||||
function buildEnvironmentVariablesFrom(injectedEnvironments) {
|
||||
let env = Object.fromEntries([
|
||||
...Object.entries(process.env),
|
||||
...Object.entries(injectedEnvironments),
|
||||
[
|
||||
'NEXT_RUNTIME',
|
||||
'edge'
|
||||
]
|
||||
]);
|
||||
return env;
|
||||
}
|
||||
function throwUnsupportedAPIError(name) {
|
||||
const error = Object.defineProperty(new Error(`A Node.js API is used (${name}) which is not supported in the Edge Runtime.
|
||||
Learn more: https://nextjs.org/docs/api-reference/edge-runtime`), "__NEXT_ERROR_CODE", {
|
||||
value: "E97",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
decorateServerError(error, COMPILER_NAMES.edgeServer);
|
||||
throw error;
|
||||
}
|
||||
function createProcessPolyfill(env) {
|
||||
const processPolyfill = {
|
||||
env: buildEnvironmentVariablesFrom(env)
|
||||
};
|
||||
const overriddenValue = {};
|
||||
for (const key of Object.keys(process)){
|
||||
if (key === 'env') continue;
|
||||
Object.defineProperty(processPolyfill, key, {
|
||||
get () {
|
||||
if (overriddenValue[key] !== undefined) {
|
||||
return overriddenValue[key];
|
||||
}
|
||||
if (typeof process[key] === 'function') {
|
||||
return ()=>throwUnsupportedAPIError(`process.${key}`);
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
set (value) {
|
||||
overriddenValue[key] = value;
|
||||
},
|
||||
enumerable: false
|
||||
});
|
||||
}
|
||||
return processPolyfill;
|
||||
}
|
||||
function addStub(context, name) {
|
||||
Object.defineProperty(context, name, {
|
||||
get () {
|
||||
return function() {
|
||||
throwUnsupportedAPIError(name);
|
||||
};
|
||||
},
|
||||
enumerable: false
|
||||
});
|
||||
}
|
||||
function getDecorateUnhandledError(runtime) {
|
||||
const EdgeRuntimeError = runtime.evaluate(`Error`);
|
||||
return (error)=>{
|
||||
if (error instanceof EdgeRuntimeError) {
|
||||
decorateServerError(error, COMPILER_NAMES.edgeServer);
|
||||
}
|
||||
};
|
||||
}
|
||||
function getDecorateUnhandledRejection(runtime) {
|
||||
const EdgeRuntimeError = runtime.evaluate(`Error`);
|
||||
return (rejected)=>{
|
||||
if (rejected.reason instanceof EdgeRuntimeError) {
|
||||
decorateServerError(rejected.reason, COMPILER_NAMES.edgeServer);
|
||||
}
|
||||
};
|
||||
}
|
||||
const NativeModuleMap = (()=>{
|
||||
const mods = {
|
||||
'node:buffer': pick(BufferImplementation, [
|
||||
'constants',
|
||||
'kMaxLength',
|
||||
'kStringMaxLength',
|
||||
'Buffer',
|
||||
'SlowBuffer'
|
||||
]),
|
||||
'node:events': pick(EventsImplementation, [
|
||||
'EventEmitter',
|
||||
'captureRejectionSymbol',
|
||||
'defaultMaxListeners',
|
||||
'errorMonitor',
|
||||
'listenerCount',
|
||||
'on',
|
||||
'once'
|
||||
]),
|
||||
'node:async_hooks': pick(AsyncHooksImplementation, [
|
||||
'AsyncLocalStorage',
|
||||
'AsyncResource'
|
||||
]),
|
||||
'node:assert': pick(AssertImplementation, [
|
||||
'AssertionError',
|
||||
'deepEqual',
|
||||
'deepStrictEqual',
|
||||
'doesNotMatch',
|
||||
'doesNotReject',
|
||||
'doesNotThrow',
|
||||
'equal',
|
||||
'fail',
|
||||
'ifError',
|
||||
'match',
|
||||
'notDeepEqual',
|
||||
'notDeepStrictEqual',
|
||||
'notEqual',
|
||||
'notStrictEqual',
|
||||
'ok',
|
||||
'rejects',
|
||||
'strict',
|
||||
'strictEqual',
|
||||
'throws'
|
||||
]),
|
||||
'node:util': pick(UtilImplementation, [
|
||||
'_extend',
|
||||
'callbackify',
|
||||
'format',
|
||||
'inherits',
|
||||
'promisify',
|
||||
'types'
|
||||
])
|
||||
};
|
||||
return new Map(Object.entries(mods));
|
||||
})();
|
||||
export const requestStore = new AsyncLocalStorage();
|
||||
export const edgeSandboxNextRequestContext = createLocalRequestContext();
|
||||
/**
|
||||
* Create a module cache specific for the provided parameters. It includes
|
||||
* a runtime context, require cache and paths cache.
|
||||
*/ async function createModuleContext(options) {
|
||||
const warnedEvals = new Set();
|
||||
const warnedWasmCodegens = new Set();
|
||||
const { edgeFunctionEntry } = options;
|
||||
const wasm = await loadWasm(edgeFunctionEntry.wasm ?? []);
|
||||
const runtime = new EdgeRuntime({
|
||||
codeGeneration: process.env.NODE_ENV !== 'production' ? {
|
||||
strings: true,
|
||||
wasm: true
|
||||
} : undefined,
|
||||
extend: (context)=>{
|
||||
context.process = createProcessPolyfill(edgeFunctionEntry.env);
|
||||
Object.defineProperty(context, 'require', {
|
||||
enumerable: false,
|
||||
value: (id)=>{
|
||||
const value = NativeModuleMap.get(id);
|
||||
if (!value) {
|
||||
throw Object.defineProperty(new TypeError('Native module not found: ' + id), "__NEXT_ERROR_CODE", {
|
||||
value: "E546",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
return value;
|
||||
}
|
||||
});
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
context.__next_log_error__ = function(err) {
|
||||
options.onError(err);
|
||||
};
|
||||
}
|
||||
context.__next_eval__ = function __next_eval__(fn) {
|
||||
const key = fn.toString();
|
||||
if (!warnedEvals.has(key)) {
|
||||
const warning = getServerError(Object.defineProperty(new Error(`Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime
|
||||
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), "__NEXT_ERROR_CODE", {
|
||||
value: "E149",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
}), COMPILER_NAMES.edgeServer);
|
||||
warning.name = 'DynamicCodeEvaluationWarning';
|
||||
Error.captureStackTrace(warning, __next_eval__);
|
||||
warnedEvals.add(key);
|
||||
options.onWarning(warning);
|
||||
}
|
||||
return fn();
|
||||
};
|
||||
context.__next_webassembly_compile__ = function __next_webassembly_compile__(fn) {
|
||||
const key = fn.toString();
|
||||
if (!warnedWasmCodegens.has(key)) {
|
||||
const warning = getServerError(Object.defineProperty(new Error(`Dynamic WASM code generation (e. g. 'WebAssembly.compile') not allowed in Edge Runtime.
|
||||
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), "__NEXT_ERROR_CODE", {
|
||||
value: "E184",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
}), COMPILER_NAMES.edgeServer);
|
||||
warning.name = 'DynamicWasmCodeGenerationWarning';
|
||||
Error.captureStackTrace(warning, __next_webassembly_compile__);
|
||||
warnedWasmCodegens.add(key);
|
||||
options.onWarning(warning);
|
||||
}
|
||||
return fn();
|
||||
};
|
||||
context.__next_webassembly_instantiate__ = async function __next_webassembly_instantiate__(fn) {
|
||||
const result = await fn();
|
||||
// If a buffer is given, WebAssembly.instantiate returns an object
|
||||
// containing both a module and an instance while it returns only an
|
||||
// instance if a WASM module is given. Utilize the fact to determine
|
||||
// if the WASM code generation happens.
|
||||
//
|
||||
// https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#primary_overload_%E2%80%94_taking_wasm_binary_code
|
||||
const instantiatedFromBuffer = result.hasOwnProperty('module');
|
||||
const key = fn.toString();
|
||||
if (instantiatedFromBuffer && !warnedWasmCodegens.has(key)) {
|
||||
const warning = getServerError(Object.defineProperty(new Error(`Dynamic WASM code generation ('WebAssembly.instantiate' with a buffer parameter) not allowed in Edge Runtime.
|
||||
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), "__NEXT_ERROR_CODE", {
|
||||
value: "E40",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
}), COMPILER_NAMES.edgeServer);
|
||||
warning.name = 'DynamicWasmCodeGenerationWarning';
|
||||
Error.captureStackTrace(warning, __next_webassembly_instantiate__);
|
||||
warnedWasmCodegens.add(key);
|
||||
options.onWarning(warning);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const __fetch = context.fetch;
|
||||
context.fetch = async (input, init = {})=>{
|
||||
const callingError = Object.defineProperty(new Error('[internal]'), "__NEXT_ERROR_CODE", {
|
||||
value: "E5",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
const assetResponse = await fetchInlineAsset({
|
||||
input,
|
||||
assets: options.edgeFunctionEntry.assets,
|
||||
distDir: options.distDir,
|
||||
context
|
||||
});
|
||||
if (assetResponse) {
|
||||
return assetResponse;
|
||||
}
|
||||
init.headers = new Headers(init.headers ?? {});
|
||||
if (!init.headers.has('user-agent')) {
|
||||
init.headers.set(`user-agent`, `Next.js Middleware`);
|
||||
}
|
||||
const response = typeof input === 'object' && 'url' in input ? __fetch(input.url, {
|
||||
...pick(input, [
|
||||
'method',
|
||||
'body',
|
||||
'cache',
|
||||
'credentials',
|
||||
'integrity',
|
||||
'keepalive',
|
||||
'mode',
|
||||
'redirect',
|
||||
'referrer',
|
||||
'referrerPolicy',
|
||||
'signal'
|
||||
]),
|
||||
...init,
|
||||
headers: {
|
||||
...Object.fromEntries(input.headers),
|
||||
...Object.fromEntries(init.headers)
|
||||
}
|
||||
}) : __fetch(String(input), init);
|
||||
return await response.catch((err)=>{
|
||||
callingError.message = err.message;
|
||||
err.stack = callingError.stack;
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
const __Request = context.Request;
|
||||
context.Request = class extends __Request {
|
||||
constructor(input, init){
|
||||
const url = typeof input !== 'string' && 'url' in input ? input.url : String(input);
|
||||
if (typeof input === 'string') {
|
||||
validateURL(url);
|
||||
super(input, init);
|
||||
} else {
|
||||
super(input, init);
|
||||
validateURL(url);
|
||||
}
|
||||
this.next = init == null ? void 0 : init.next;
|
||||
}
|
||||
};
|
||||
const __redirect = context.Response.redirect.bind(context.Response);
|
||||
context.Response.redirect = (...args)=>{
|
||||
validateURL(args[0]);
|
||||
return __redirect(...args);
|
||||
};
|
||||
for (const name of EDGE_UNSUPPORTED_NODE_APIS){
|
||||
addStub(context, name);
|
||||
}
|
||||
Object.assign(context, wasm);
|
||||
context.performance = performance;
|
||||
context.AsyncLocalStorage = AsyncLocalStorage;
|
||||
// @ts-ignore the timeouts have weird types in the edge runtime
|
||||
context.setInterval = (...args)=>intervalsManager.add(args);
|
||||
// @ts-ignore the timeouts have weird types in the edge runtime
|
||||
context.clearInterval = (interval)=>intervalsManager.remove(interval);
|
||||
// @ts-ignore the timeouts have weird types in the edge runtime
|
||||
context.setTimeout = (...args)=>timeoutsManager.add(args);
|
||||
// @ts-ignore the timeouts have weird types in the edge runtime
|
||||
context.clearTimeout = (timeout)=>timeoutsManager.remove(timeout);
|
||||
// Duplicated from packages/next/src/server/after/builtin-request-context.ts
|
||||
// because we need to use the sandboxed `Symbol.for`, not the one from the outside
|
||||
const NEXT_REQUEST_CONTEXT_SYMBOL = context.Symbol.for('@next/request-context');
|
||||
Object.defineProperty(context, NEXT_REQUEST_CONTEXT_SYMBOL, {
|
||||
enumerable: false,
|
||||
value: edgeSandboxNextRequestContext
|
||||
});
|
||||
return context;
|
||||
}
|
||||
});
|
||||
const decorateUnhandledError = getDecorateUnhandledError(runtime);
|
||||
runtime.context.addEventListener('error', decorateUnhandledError);
|
||||
const decorateUnhandledRejection = getDecorateUnhandledRejection(runtime);
|
||||
runtime.context.addEventListener('unhandledrejection', decorateUnhandledRejection);
|
||||
patchErrorInspectEdgeLite(runtime.context.Error);
|
||||
// An Error from within the Edge Runtime could also bubble up into the Node.js process.
|
||||
// For example, uncaught errors are handled in the Node.js runtime.
|
||||
patchErrorInspectNodeJS(runtime.context.Error);
|
||||
return {
|
||||
runtime,
|
||||
paths: new Map(),
|
||||
warnedEvals: new Set()
|
||||
};
|
||||
}
|
||||
function getModuleContextShared(options) {
|
||||
let deferredModuleContext = pendingModuleCaches.get(options.moduleName);
|
||||
if (!deferredModuleContext) {
|
||||
deferredModuleContext = createModuleContext(options);
|
||||
pendingModuleCaches.set(options.moduleName, deferredModuleContext);
|
||||
}
|
||||
return deferredModuleContext;
|
||||
}
|
||||
/**
|
||||
* For a given module name this function will get a cached module
|
||||
* context or create it. It will return the module context along
|
||||
* with a function that allows to run some code from a given
|
||||
* filepath within the context.
|
||||
*/ export async function getModuleContext(options) {
|
||||
let lazyModuleContext;
|
||||
if (options.useCache) {
|
||||
lazyModuleContext = moduleContexts.get(options.moduleName) || await getModuleContextShared(options);
|
||||
}
|
||||
if (!lazyModuleContext) {
|
||||
lazyModuleContext = await createModuleContext(options);
|
||||
moduleContexts.set(options.moduleName, lazyModuleContext);
|
||||
}
|
||||
const moduleContext = lazyModuleContext;
|
||||
const evaluateInContext = (filepath)=>{
|
||||
if (!moduleContext.paths.has(filepath)) {
|
||||
const content = readFileSync(filepath, 'utf-8');
|
||||
try {
|
||||
runInContext(content, moduleContext.runtime.context, {
|
||||
filename: filepath
|
||||
});
|
||||
moduleContext.paths.set(filepath, content);
|
||||
} catch (error) {
|
||||
if (options.useCache) {
|
||||
moduleContext == null ? void 0 : moduleContext.paths.delete(filepath);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
return {
|
||||
...moduleContext,
|
||||
evaluateInContext
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=context.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+29
@@ -0,0 +1,29 @@
|
||||
import { createReadStream, promises as fs } from 'fs';
|
||||
import { requestToBodyStream } from '../../body-streams';
|
||||
import { resolve } from 'path';
|
||||
/**
|
||||
* Short-circuits the `fetch` function
|
||||
* to return a stream for a given asset, if a user used `new URL("file", import.meta.url)`.
|
||||
* This allows to embed assets in Edge Runtime.
|
||||
*/ export async function fetchInlineAsset(options) {
|
||||
const inputString = String(options.input);
|
||||
if (!inputString.startsWith('blob:')) {
|
||||
return;
|
||||
}
|
||||
const name = inputString.replace('blob:', '');
|
||||
const asset = options.assets ? options.assets.find((x)=>x.name === name) : {
|
||||
name,
|
||||
filePath: name
|
||||
};
|
||||
if (!asset) {
|
||||
return;
|
||||
}
|
||||
const filePath = resolve(options.distDir, asset.filePath);
|
||||
const fileIsReadable = await fs.access(filePath).then(()=>true, ()=>false);
|
||||
if (fileIsReadable) {
|
||||
const readStream = createReadStream(filePath);
|
||||
return new options.context.Response(requestToBodyStream(options.context, Uint8Array, readStream));
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=fetch-inline-assets.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/sandbox/fetch-inline-assets.ts"],"sourcesContent":["import type { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'\nimport { createReadStream, promises as fs } from 'fs'\nimport { requestToBodyStream } from '../../body-streams'\nimport { resolve } from 'path'\n\n/**\n * Short-circuits the `fetch` function\n * to return a stream for a given asset, if a user used `new URL(\"file\", import.meta.url)`.\n * This allows to embed assets in Edge Runtime.\n */\nexport async function fetchInlineAsset(options: {\n input: RequestInfo | URL\n distDir: string\n assets: EdgeFunctionDefinition['assets']\n context: { Response: typeof Response; ReadableStream: typeof ReadableStream }\n}): Promise<Response | undefined> {\n const inputString = String(options.input)\n if (!inputString.startsWith('blob:')) {\n return\n }\n\n const name = inputString.replace('blob:', '')\n const asset = options.assets\n ? options.assets.find((x) => x.name === name)\n : {\n name,\n filePath: name,\n }\n if (!asset) {\n return\n }\n\n const filePath = resolve(options.distDir, asset.filePath)\n const fileIsReadable = await fs.access(filePath).then(\n () => true,\n () => false\n )\n\n if (fileIsReadable) {\n const readStream = createReadStream(filePath)\n return new options.context.Response(\n requestToBodyStream(options.context, Uint8Array, readStream)\n )\n }\n}\n"],"names":["createReadStream","promises","fs","requestToBodyStream","resolve","fetchInlineAsset","options","inputString","String","input","startsWith","name","replace","asset","assets","find","x","filePath","distDir","fileIsReadable","access","then","readStream","context","Response","Uint8Array"],"mappings":"AACA,SAASA,gBAAgB,EAAEC,YAAYC,EAAE,QAAQ,KAAI;AACrD,SAASC,mBAAmB,QAAQ,qBAAoB;AACxD,SAASC,OAAO,QAAQ,OAAM;AAE9B;;;;CAIC,GACD,OAAO,eAAeC,iBAAiBC,OAKtC;IACC,MAAMC,cAAcC,OAAOF,QAAQG,KAAK;IACxC,IAAI,CAACF,YAAYG,UAAU,CAAC,UAAU;QACpC;IACF;IAEA,MAAMC,OAAOJ,YAAYK,OAAO,CAAC,SAAS;IAC1C,MAAMC,QAAQP,QAAQQ,MAAM,GACxBR,QAAQQ,MAAM,CAACC,IAAI,CAAC,CAACC,IAAMA,EAAEL,IAAI,KAAKA,QACtC;QACEA;QACAM,UAAUN;IACZ;IACJ,IAAI,CAACE,OAAO;QACV;IACF;IAEA,MAAMI,WAAWb,QAAQE,QAAQY,OAAO,EAAEL,MAAMI,QAAQ;IACxD,MAAME,iBAAiB,MAAMjB,GAAGkB,MAAM,CAACH,UAAUI,IAAI,CACnD,IAAM,MACN,IAAM;IAGR,IAAIF,gBAAgB;QAClB,MAAMG,aAAatB,iBAAiBiB;QACpC,OAAO,IAAIX,QAAQiB,OAAO,CAACC,QAAQ,CACjCrB,oBAAoBG,QAAQiB,OAAO,EAAEE,YAAYH;IAErD;AACF","ignoreList":[0]}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
export * from './sandbox';
|
||||
export { clearModuleContext } from './context';
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/sandbox/index.ts"],"sourcesContent":["export * from './sandbox'\nexport { clearModuleContext } from './context'\n"],"names":["clearModuleContext"],"mappings":"AAAA,cAAc,YAAW;AACzB,SAASA,kBAAkB,QAAQ,YAAW","ignoreList":[0]}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
class ResourceManager {
|
||||
add(resourceArgs) {
|
||||
const resource = this.create(resourceArgs);
|
||||
this.resources.push(resource);
|
||||
return resource;
|
||||
}
|
||||
remove(resource) {
|
||||
this.resources = this.resources.filter((r)=>r !== resource);
|
||||
this.destroy(resource);
|
||||
}
|
||||
removeAll() {
|
||||
this.resources.forEach(this.destroy);
|
||||
this.resources = [];
|
||||
}
|
||||
constructor(){
|
||||
this.resources = [];
|
||||
}
|
||||
}
|
||||
class IntervalsManager extends ResourceManager {
|
||||
create(args) {
|
||||
// TODO: use the edge runtime provided `setInterval` instead
|
||||
return webSetIntervalPolyfill(...args);
|
||||
}
|
||||
destroy(interval) {
|
||||
clearInterval(interval);
|
||||
}
|
||||
}
|
||||
class TimeoutsManager extends ResourceManager {
|
||||
create(args) {
|
||||
// TODO: use the edge runtime provided `setTimeout` instead
|
||||
return webSetTimeoutPolyfill(...args);
|
||||
}
|
||||
destroy(timeout) {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
function webSetIntervalPolyfill(callback, ms, ...args) {
|
||||
return setInterval(()=>{
|
||||
// node's `setInterval` sets `this` to the `Timeout` instance it returned,
|
||||
// but web `setInterval` always sets `this` to `window`
|
||||
// see: https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval#the_this_problem
|
||||
return callback.apply(globalThis, args);
|
||||
}, ms)[Symbol.toPrimitive]();
|
||||
}
|
||||
function webSetTimeoutPolyfill(callback, ms, ...args) {
|
||||
const wrappedCallback = ()=>{
|
||||
try {
|
||||
// node's `setTimeout` sets `this` to the `Timeout` instance it returned,
|
||||
// but web `setTimeout` always sets `this` to `window`
|
||||
// see: https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#the_this_problem
|
||||
return callback.apply(globalThis, args);
|
||||
} finally{
|
||||
// On certain older node versions (<20.16.0, <22.4.0),
|
||||
// a `setTimeout` whose Timeout was converted to a primitive will leak.
|
||||
// See: https://github.com/nodejs/node/issues/53335
|
||||
// We can work around this by explicitly calling `clearTimeout` after the callback runs.
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
};
|
||||
const timeout = setTimeout(wrappedCallback, ms);
|
||||
return timeout[Symbol.toPrimitive]();
|
||||
}
|
||||
export const intervalsManager = new IntervalsManager();
|
||||
export const timeoutsManager = new TimeoutsManager();
|
||||
|
||||
//# sourceMappingURL=resource-managers.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../../src/server/web/sandbox/resource-managers.ts"],"sourcesContent":["abstract class ResourceManager<T, Args> {\n private resources: T[] = []\n\n abstract create(resourceArgs: Args): T\n abstract destroy(resource: T): void\n\n add(resourceArgs: Args) {\n const resource = this.create(resourceArgs)\n this.resources.push(resource)\n return resource\n }\n\n remove(resource: T) {\n this.resources = this.resources.filter((r) => r !== resource)\n this.destroy(resource)\n }\n\n removeAll() {\n this.resources.forEach(this.destroy)\n this.resources = []\n }\n}\n\nclass IntervalsManager extends ResourceManager<\n number,\n Parameters<typeof setInterval>\n> {\n create(args: Parameters<typeof setInterval>) {\n // TODO: use the edge runtime provided `setInterval` instead\n return webSetIntervalPolyfill(...args)\n }\n\n destroy(interval: number) {\n clearInterval(interval)\n }\n}\n\nclass TimeoutsManager extends ResourceManager<\n number,\n Parameters<typeof setTimeout>\n> {\n create(args: Parameters<typeof setTimeout>) {\n // TODO: use the edge runtime provided `setTimeout` instead\n return webSetTimeoutPolyfill(...args)\n }\n\n destroy(timeout: number) {\n clearTimeout(timeout)\n }\n}\n\nfunction webSetIntervalPolyfill<TArgs extends any[]>(\n callback: (...args: TArgs) => void,\n ms?: number,\n ...args: TArgs\n): number {\n return setInterval(() => {\n // node's `setInterval` sets `this` to the `Timeout` instance it returned,\n // but web `setInterval` always sets `this` to `window`\n // see: https://developer.mozilla.org/en-US/docs/Web/API/Window/setInterval#the_this_problem\n return callback.apply(globalThis, args)\n }, ms)[Symbol.toPrimitive]()\n}\n\nfunction webSetTimeoutPolyfill<TArgs extends any[]>(\n callback: (...args: TArgs) => void,\n ms?: number,\n ...args: TArgs\n): number {\n const wrappedCallback = () => {\n try {\n // node's `setTimeout` sets `this` to the `Timeout` instance it returned,\n // but web `setTimeout` always sets `this` to `window`\n // see: https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#the_this_problem\n return callback.apply(globalThis, args)\n } finally {\n // On certain older node versions (<20.16.0, <22.4.0),\n // a `setTimeout` whose Timeout was converted to a primitive will leak.\n // See: https://github.com/nodejs/node/issues/53335\n // We can work around this by explicitly calling `clearTimeout` after the callback runs.\n clearTimeout(timeout)\n }\n }\n const timeout = setTimeout(wrappedCallback, ms)\n return timeout[Symbol.toPrimitive]()\n}\n\nexport const intervalsManager = new IntervalsManager()\nexport const timeoutsManager = new TimeoutsManager()\n"],"names":["ResourceManager","add","resourceArgs","resource","create","resources","push","remove","filter","r","destroy","removeAll","forEach","IntervalsManager","args","webSetIntervalPolyfill","interval","clearInterval","TimeoutsManager","webSetTimeoutPolyfill","timeout","clearTimeout","callback","ms","setInterval","apply","globalThis","Symbol","toPrimitive","wrappedCallback","setTimeout","intervalsManager","timeoutsManager"],"mappings":"AAAA,MAAeA;IAMbC,IAAIC,YAAkB,EAAE;QACtB,MAAMC,WAAW,IAAI,CAACC,MAAM,CAACF;QAC7B,IAAI,CAACG,SAAS,CAACC,IAAI,CAACH;QACpB,OAAOA;IACT;IAEAI,OAAOJ,QAAW,EAAE;QAClB,IAAI,CAACE,SAAS,GAAG,IAAI,CAACA,SAAS,CAACG,MAAM,CAAC,CAACC,IAAMA,MAAMN;QACpD,IAAI,CAACO,OAAO,CAACP;IACf;IAEAQ,YAAY;QACV,IAAI,CAACN,SAAS,CAACO,OAAO,CAAC,IAAI,CAACF,OAAO;QACnC,IAAI,CAACL,SAAS,GAAG,EAAE;IACrB;;aAnBQA,YAAiB,EAAE;;AAoB7B;AAEA,MAAMQ,yBAAyBb;IAI7BI,OAAOU,IAAoC,EAAE;QAC3C,4DAA4D;QAC5D,OAAOC,0BAA0BD;IACnC;IAEAJ,QAAQM,QAAgB,EAAE;QACxBC,cAAcD;IAChB;AACF;AAEA,MAAME,wBAAwBlB;IAI5BI,OAAOU,IAAmC,EAAE;QAC1C,2DAA2D;QAC3D,OAAOK,yBAAyBL;IAClC;IAEAJ,QAAQU,OAAe,EAAE;QACvBC,aAAaD;IACf;AACF;AAEA,SAASL,uBACPO,QAAkC,EAClCC,EAAW,EACX,GAAGT,IAAW;IAEd,OAAOU,YAAY;QACjB,0EAA0E;QAC1E,uDAAuD;QACvD,4FAA4F;QAC5F,OAAOF,SAASG,KAAK,CAACC,YAAYZ;IACpC,GAAGS,GAAG,CAACI,OAAOC,WAAW,CAAC;AAC5B;AAEA,SAAST,sBACPG,QAAkC,EAClCC,EAAW,EACX,GAAGT,IAAW;IAEd,MAAMe,kBAAkB;QACtB,IAAI;YACF,yEAAyE;YACzE,sDAAsD;YACtD,2FAA2F;YAC3F,OAAOP,SAASG,KAAK,CAACC,YAAYZ;QACpC,SAAU;YACR,sDAAsD;YACtD,uEAAuE;YACvE,mDAAmD;YACnD,wFAAwF;YACxFO,aAAaD;QACf;IACF;IACA,MAAMA,UAAUU,WAAWD,iBAAiBN;IAC5C,OAAOH,OAAO,CAACO,OAAOC,WAAW,CAAC;AACpC;AAEA,OAAO,MAAMG,mBAAmB,IAAIlB,mBAAkB;AACtD,OAAO,MAAMmB,kBAAkB,IAAId,kBAAiB","ignoreList":[0]}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
import { getModuleContext, requestStore, edgeSandboxNextRequestContext } from './context';
|
||||
import { requestToBodyStream } from '../../body-streams';
|
||||
import { getBuiltinRequestContext } from '../../after/builtin-request-context';
|
||||
import { RouterServerContextSymbol, routerServerGlobal } from '../../lib/router-utils/router-server-context';
|
||||
export const ErrorSource = Symbol('SandboxError');
|
||||
const FORBIDDEN_HEADERS = [
|
||||
'content-length',
|
||||
'content-encoding',
|
||||
'transfer-encoding'
|
||||
];
|
||||
/**
|
||||
* Decorates the runner function making sure all errors it can produce are
|
||||
* tagged with `edge-server` so they can properly be rendered in dev.
|
||||
*/ function withTaggedErrors(fn) {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const { getServerError } = require('../../dev/node-stack-frames');
|
||||
return (params)=>fn(params).then((result)=>{
|
||||
var _result_waitUntil;
|
||||
return {
|
||||
...result,
|
||||
waitUntil: result == null ? void 0 : (_result_waitUntil = result.waitUntil) == null ? void 0 : _result_waitUntil.catch((error)=>{
|
||||
// TODO: used COMPILER_NAMES.edgeServer instead. Verify that it does not increase the runtime size.
|
||||
throw getServerError(error, 'edge-server');
|
||||
})
|
||||
};
|
||||
}).catch((error)=>{
|
||||
// TODO: used COMPILER_NAMES.edgeServer instead
|
||||
throw getServerError(error, 'edge-server');
|
||||
});
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
export async function getRuntimeContext(params) {
|
||||
const { runtime, evaluateInContext } = await getModuleContext({
|
||||
moduleName: params.name,
|
||||
onWarning: params.onWarning ?? (()=>{}),
|
||||
onError: params.onError ?? (()=>{}),
|
||||
useCache: params.useCache !== false,
|
||||
edgeFunctionEntry: params.edgeFunctionEntry,
|
||||
distDir: params.distDir
|
||||
});
|
||||
if (params.incrementalCache) {
|
||||
runtime.context.globalThis.__incrementalCacheShared = true;
|
||||
runtime.context.globalThis.__incrementalCache = params.incrementalCache;
|
||||
}
|
||||
// expose router server context for access to dev handlers like
|
||||
// logErrorWithOriginalStack
|
||||
;
|
||||
runtime.context.globalThis[RouterServerContextSymbol] = routerServerGlobal[RouterServerContextSymbol];
|
||||
if (params.serverComponentsHmrCache) {
|
||||
runtime.context.globalThis.__serverComponentsHmrCache = params.serverComponentsHmrCache;
|
||||
}
|
||||
if (params.clientAssetToken) {
|
||||
runtime.context.globalThis.NEXT_CLIENT_ASSET_SUFFIX = params.clientAssetToken ? `?dpl=${params.clientAssetToken}` : '';
|
||||
}
|
||||
for (const paramPath of params.paths){
|
||||
evaluateInContext(paramPath);
|
||||
}
|
||||
return runtime;
|
||||
}
|
||||
export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
|
||||
var _params_request_body;
|
||||
const runtime = await getRuntimeContext(params);
|
||||
const edgeFunction = (await runtime.context._ENTRIES[`middleware_${params.name}`]).default;
|
||||
const cloned = ![
|
||||
'HEAD',
|
||||
'GET'
|
||||
].includes(params.request.method) ? (_params_request_body = params.request.body) == null ? void 0 : _params_request_body.cloneBodyStream() : undefined;
|
||||
const KUint8Array = runtime.evaluate('Uint8Array');
|
||||
const urlInstance = new URL(params.request.url);
|
||||
params.request.url = urlInstance.toString();
|
||||
const headers = new Headers();
|
||||
for (const [key, value] of Object.entries(params.request.headers)){
|
||||
headers.set(key, (value == null ? void 0 : value.toString()) ?? '');
|
||||
}
|
||||
try {
|
||||
let result = undefined;
|
||||
const builtinRequestCtx = {
|
||||
...getBuiltinRequestContext(),
|
||||
// FIXME(after):
|
||||
// arguably, this is an abuse of "@next/request-context" --
|
||||
// it'd make more sense to simply forward its existing value into the sandbox (in `createModuleContext`)
|
||||
// but here we're using it to just pass in `waitUntil` regardless if we were running in this context or not.
|
||||
waitUntil: params.request.waitUntil
|
||||
};
|
||||
await edgeSandboxNextRequestContext.run(builtinRequestCtx, ()=>requestStore.run({
|
||||
headers
|
||||
}, async ()=>{
|
||||
result = await edgeFunction({
|
||||
request: {
|
||||
...params.request,
|
||||
body: cloned && requestToBodyStream(runtime.context, KUint8Array, cloned)
|
||||
}
|
||||
});
|
||||
for (const headerName of FORBIDDEN_HEADERS){
|
||||
result.response.headers.delete(headerName);
|
||||
}
|
||||
}));
|
||||
if (!result) throw Object.defineProperty(new Error('Edge function did not return a response'), "__NEXT_ERROR_CODE", {
|
||||
value: "E332",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
return result;
|
||||
} finally{
|
||||
var _params_request_body1;
|
||||
await ((_params_request_body1 = params.request.body) == null ? void 0 : _params_request_body1.finalize());
|
||||
}
|
||||
});
|
||||
|
||||
//# sourceMappingURL=sandbox.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user