| | import type { |
| | InstrumentationModule, |
| | InstrumentationOnRequestError, |
| | } from '../instrumentation/types' |
| |
|
| | declare const _ENTRIES: any |
| |
|
| | export async function getEdgeInstrumentationModule(): Promise< |
| | InstrumentationModule | undefined |
| | > { |
| | const instrumentation = |
| | '_ENTRIES' in globalThis && |
| | _ENTRIES.middleware_instrumentation && |
| | (await _ENTRIES.middleware_instrumentation) |
| |
|
| | return instrumentation |
| | } |
| |
|
| | let instrumentationModulePromise: Promise<any> | null = null |
| | async function registerInstrumentation() { |
| | |
| | if (process.env.NEXT_PHASE === 'phase-production-build') return |
| | if (!instrumentationModulePromise) { |
| | instrumentationModulePromise = getEdgeInstrumentationModule() |
| | } |
| | const instrumentation = await instrumentationModulePromise |
| | if (instrumentation?.register) { |
| | try { |
| | await instrumentation.register() |
| | } catch (err: any) { |
| | err.message = `An error occurred while loading instrumentation hook: ${err.message}` |
| | throw err |
| | } |
| | } |
| | } |
| |
|
| | export async function edgeInstrumentationOnRequestError( |
| | ...args: Parameters<InstrumentationOnRequestError> |
| | ) { |
| | const instrumentation = await getEdgeInstrumentationModule() |
| | try { |
| | await instrumentation?.onRequestError?.(...args) |
| | } catch (err) { |
| | |
| | console.error('Error in instrumentation.onRequestError:', err) |
| | } |
| | } |
| |
|
| | let registerInstrumentationPromise: Promise<void> | null = null |
| | export function ensureInstrumentationRegistered() { |
| | if (!registerInstrumentationPromise) { |
| | registerInstrumentationPromise = registerInstrumentation() |
| | } |
| | return registerInstrumentationPromise |
| | } |
| |
|
| | function getUnsupportedModuleErrorMessage(module: string) { |
| | |
| | return `The edge runtime does not support Node.js '${module}' module. |
| | Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime` |
| | } |
| |
|
| | function __import_unsupported(moduleName: string) { |
| | const proxy: any = new Proxy(function () {}, { |
| | get(_obj, prop) { |
| | if (prop === 'then') { |
| | return {} |
| | } |
| | throw new Error(getUnsupportedModuleErrorMessage(moduleName)) |
| | }, |
| | construct() { |
| | throw new Error(getUnsupportedModuleErrorMessage(moduleName)) |
| | }, |
| | apply(_target, _this, args) { |
| | if (typeof args[0] === 'function') { |
| | return args[0](proxy) |
| | } |
| | throw new Error(getUnsupportedModuleErrorMessage(moduleName)) |
| | }, |
| | }) |
| | return new Proxy({}, { get: () => proxy }) |
| | } |
| |
|
| | function enhanceGlobals() { |
| | if (process.env.NEXT_RUNTIME !== 'edge') { |
| | return |
| | } |
| |
|
| | |
| | if (process !== global.process) { |
| | |
| | process.env = global.process.env |
| | global.process = process |
| | } |
| |
|
| | |
| | |
| | try { |
| | Object.defineProperty(globalThis, '__import_unsupported', { |
| | value: __import_unsupported, |
| | enumerable: false, |
| | configurable: false, |
| | }) |
| | } catch {} |
| |
|
| | |
| | void ensureInstrumentationRegistered() |
| | } |
| |
|
| | enhanceGlobals() |
| |
|