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,CAAC,CAAC;QAEhE,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;QACxB,MAAM,MAAM,GAAG,IAAA,WAAQ,EAAC,GAAG,CAAC,CAAC;QAE7B,IAAI,CAAC,6BAA6B,CAAC,MAAM,CAAC,EAAE;YAC1C,OAAO,KAAK,EAAE,CAAC;SAChB;QACD,MAAM,UAAU,GAAG,IAAA,mBAAa,EAAC,GAAG,CAAC,CAAC;QAEtC,IAAI,aAAa,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;YACrC,OAAO,KAAK,EAAE,CAAC;SAChB;QAED,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;QAEpE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IAC/B,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AA7RD,wCA6RC;AAED,KAAK,UAAU,mBAAmB,CAAI,EAAoB;IACxD,MAAM,GAAG,GAAG,MAAM,EAAE,EAAE,CAAC;IACvB,yEAAyE;IACzE,IAAI,GAAG,IAAI,IAAI;QAAE,OAAO,GAAG,CAAC;IAC5B,OAAO;QACL,GAAG,GAAG;QACN,YAAY,EAAE,IAAI;KACnB,CAAC;AACJ,CAAC","sourcesContent":["import { register, RegisterOptions, Service } from './index';\nimport {\n parse as parseUrl,\n format as formatUrl,\n UrlWithStringQuery,\n fileURLToPath,\n pathToFileURL,\n} from 'url';\nimport { extname } from 'path';\nimport * as assert from 'assert';\nimport { normalizeSlashes, versionGteLt } from './util';\nimport { createRequire } from 'module';\n\n// Note: On Windows, URLs look like this: file:///D:/dev/@TypeStrong/ts-node-examples/foo.ts\n\n// NOTE ABOUT MULTIPLE EXPERIMENTAL LOADER APIS\n//\n// At the time of writing, this file implements 2x different loader APIs.\n// Node made a breaking change to the loader API in https://github.com/nodejs/node/pull/37468\n//\n// We check the node version number and export either the *old* or the *new* API.\n//\n// Today, we are implementing the *new* API on top of our implementation of the *old* API,\n// which relies on copy-pasted code from the *old* hooks implementation in node.\n//\n// In the future, we will likely invert this: we will copy-paste the *new* API implementation\n// from node, build our implementation of the *new* API on top of it, and implement the *old*\n// hooks API as a shim to the *new* API.\n\nexport interface NodeLoaderHooksAPI1 {\n resolve: NodeLoaderHooksAPI1.ResolveHook;\n getFormat: NodeLoaderHooksAPI1.GetFormatHook;\n transformSource: NodeLoaderHooksAPI1.TransformSourceHook;\n}\nexport namespace NodeLoaderHooksAPI1 {\n export type ResolveHook = NodeLoaderHooksAPI2.ResolveHook;\n export type GetFormatHook = (\n url: string,\n context: {},\n defaultGetFormat: GetFormatHook\n ) => Promise<{ format: NodeLoaderHooksFormat }>;\n export type TransformSourceHook = (\n source: string | Buffer,\n context: { url: string; format: NodeLoaderHooksFormat },\n defaultTransformSource: NodeLoaderHooksAPI1.TransformSourceHook\n ) => Promise<{ source: string | Buffer }>;\n}\n\nexport interface NodeLoaderHooksAPI2 {\n resolve: NodeLoaderHooksAPI2.ResolveHook;\n load: NodeLoaderHooksAPI2.LoadHook;\n}\nexport namespace NodeLoaderHooksAPI2 {\n export type ResolveHook = (\n specifier: string,\n context: {\n conditions?: NodeImportConditions;\n importAssertions?: NodeImportAssertions;\n parentURL: string;\n },\n defaultResolve: ResolveHook\n ) => Promise<{\n url: string;\n format?: NodeLoaderHooksFormat;\n shortCircuit?: boolean;\n }>;\n export type LoadHook = (\n url: string,\n context: {\n format: NodeLoaderHooksFormat | null | undefined;\n importAssertions?: NodeImportAssertions;\n },\n defaultLoad: NodeLoaderHooksAPI2['load']\n ) => Promise<{\n format: NodeLoaderHooksFormat;\n source: string | Buffer | undefined;\n shortCircuit?: boolean;\n }>;\n export type NodeImportConditions = unknown;\n export interface NodeImportAssertions {\n type?: 'json';\n }\n}\n\nexport type NodeLoaderHooksFormat =\n | 'builtin'\n | 'commonjs'\n | 'dynamic'\n | 'json'\n | 'module'\n | 'wasm';\n\nexport type NodeImportConditions = unknown;\nexport interface NodeImportAssertions {\n type?: 'json';\n}\n\n// The hooks API changed in node version X so we need to check for backwards compatibility.\nconst newHooksAPI = versionGteLt(process.versions.node, '16.12.0');\n\n/** @internal */\nexport function filterHooksByAPIVersion(\n hooks: NodeLoaderHooksAPI1 & NodeLoaderHooksAPI2\n): NodeLoaderHooksAPI1 | NodeLoaderHooksAPI2 {\n const { getFormat, load, resolve, transformSource } = hooks;\n // Explicit return type to avoid TS's non-ideal inferred type\n const hooksAPI: NodeLoaderHooksAPI1 | NodeLoaderHooksAPI2 = newHooksAPI\n ? { resolve, load, getFormat: undefined, transformSource: undefined }\n : { resolve, getFormat, transformSource, load: undefined };\n return hooksAPI;\n}\n\n/** @internal */\nexport function registerAndCreateEsmHooks(opts?: RegisterOptions) {\n // Automatically performs registration just like `-r ts-node/register`\n const tsNodeInstance = register(opts);\n\n return createEsmHooks(tsNodeInstance);\n}\n\nexport function createEsmHooks(tsNodeService: Service) {\n tsNodeService.enableExperimentalEsmLoaderInterop();\n\n // Custom implementation that considers additional file extensions and automatically adds file extensions\n const nodeResolveImplementation = tsNodeService.getNodeEsmResolver();\n const nodeGetFormatImplementation = tsNodeService.getNodeEsmGetFormat();\n const extensions = tsNodeService.extensions;\n\n const hooksAPI = filterHooksByAPIVersion({\n resolve,\n load,\n getFormat,\n transformSource,\n });\n\n function isFileUrlOrNodeStyleSpecifier(parsed: UrlWithStringQuery) {\n // We only understand file:// URLs, but in node, the specifier can be a node-style `./foo` or `foo`\n const { protocol } = parsed;\n return protocol === null || protocol === 'file:';\n }\n\n /**\n * Named \"probably\" as a reminder that this is a guess.\n * node does not explicitly tell us if we're resolving the entrypoint or not.\n */\n function isProbablyEntrypoint(specifier: string, parentURL: string) {\n return parentURL === undefined && specifier.startsWith('file://');\n }\n // Side-channel between `resolve()` and `load()` hooks\n const rememberIsProbablyEntrypoint = new Set();\n const rememberResolvedViaCommonjsFallback = new Set();\n\n async function resolve(\n specifier: string,\n context: { parentURL: string },\n defaultResolve: typeof resolve\n ): Promise<{ url: string; format?: NodeLoaderHooksFormat }> {\n const defer = async () => {\n const r = await defaultResolve(specifier, context, defaultResolve);\n return r;\n };\n // See: https://github.com/nodejs/node/discussions/41711\n // nodejs will likely implement a similar fallback. Till then, we can do our users a favor and fallback today.\n async function entrypointFallback(\n cb: () => ReturnType<typeof resolve> | Awaited<ReturnType<typeof resolve>>\n ): ReturnType<typeof resolve> {\n try {\n const resolution = await cb();\n if (\n resolution?.url &&\n isProbablyEntrypoint(specifier, context.parentURL)\n )\n rememberIsProbablyEntrypoint.add(resolution.url);\n return resolution;\n } catch (esmResolverError) {\n if (!isProbablyEntrypoint(specifier, context.parentURL))\n throw esmResolverError;\n try {\n let cjsSpecifier = specifier;\n // Attempt to convert from ESM file:// to CommonJS path\n try {\n if (specifier.startsWith('file://'))\n cjsSpecifier = fileURLToPath(specifier);\n } catch {}\n const resolution = pathToFileURL(\n createRequire(process.cwd()).resolve(cjsSpecifier)\n ).toString();\n rememberIsProbablyEntrypoint.add(resolution);\n rememberResolvedViaCommonjsFallback.add(resolution);\n return { url: resolution, format: 'commonjs' };\n } catch (commonjsResolverError) {\n throw esmResolverError;\n }\n }\n }\n\n return addShortCircuitFlag(async () => {\n const parsed = parseUrl(specifier);\n const { pathname, protocol, hostname } = parsed;\n\n if (!isFileUrlOrNodeStyleSpecifier(parsed)) {\n return entrypointFallback(defer);\n }\n\n if (protocol !== null && protocol !== 'file:') {\n return entrypointFallback(defer);\n }\n\n // Malformed file:// URL? We should always see `null` or `''`\n if (hostname) {\n // TODO file://./foo sets `hostname` to `'.'`. Perhaps we should special-case this.\n return entrypointFallback(defer);\n }\n\n // pathname is the path to be resolved\n\n return entrypointFallback(() =>\n nodeResolveImplementation.defaultResolve(\n specifier,\n context,\n defaultResolve\n )\n );\n });\n }\n\n // `load` from new loader hook API (See description at the top of this file)\n async function load(\n url: string,\n context: {\n format: NodeLoaderHooksFormat | null | undefined;\n importAssertions?: NodeLoaderHooksAPI2.NodeImportAssertions;\n },\n defaultLoad: typeof load\n ): Promise<{\n format: NodeLoaderHooksFormat;\n source: string | Buffer | undefined;\n }> {\n return addShortCircuitFlag(async () => {\n // If we get a format hint from resolve() on the context then use it\n // otherwise call the old getFormat() hook using node's old built-in defaultGetFormat() that ships with ts-node\n const format =\n context.format ??\n (\n await getFormat(\n url,\n context,\n nodeGetFormatImplementation.defaultGetFormat\n )\n ).format;\n\n let source = undefined;\n if (format !== 'builtin' && format !== 'commonjs') {\n // Call the new defaultLoad() to get the source\n const { source: rawSource } = await defaultLoad(\n url,\n {\n ...context,\n format,\n },\n defaultLoad\n );\n\n if (rawSource === undefined || rawSource === null) {\n throw new Error(\n `Failed to load raw source: Format was '${format}' and url was '${url}''.`\n );\n }\n\n // Emulate node's built-in old defaultTransformSource() so we can re-use the old transformSource() hook\n const defaultTransformSource: typeof transformSource = async (\n source,\n _context,\n _defaultTransformSource\n ) => ({ source });\n\n // Call the old hook\n const { source: transformedSource } = await transformSource(\n rawSource,\n { url, format },\n defaultTransformSource\n );\n source = transformedSource;\n }\n\n return { format, source };\n });\n }\n\n async function getFormat(\n url: string,\n context: {},\n defaultGetFormat: typeof getFormat\n ): Promise<{ format: NodeLoaderHooksFormat }> {\n const defer = (overrideUrl: string = url) =>\n defaultGetFormat(overrideUrl, context, defaultGetFormat);\n\n // See: https://github.com/nodejs/node/discussions/41711\n // nodejs will likely implement a similar fallback. Till then, we can do our users a favor and fallback today.\n async function entrypointFallback(\n cb: () => ReturnType<typeof getFormat>\n ): ReturnType<typeof getFormat> {\n try {\n return await cb();\n } catch (getFormatError) {\n if (!rememberIsProbablyEntrypoint.has(url)) throw getFormatError;\n return { format: 'commonjs' };\n }\n }\n\n const parsed = parseUrl(url);\n\n if (!isFileUrlOrNodeStyleSpecifier(parsed)) {\n return entrypointFallback(defer);\n }\n\n const { pathname } = parsed;\n assert(\n pathname !== null,\n 'ESM getFormat() hook: URL should never have null pathname'\n );\n\n const nativePath = fileURLToPath(url);\n\n let nodeSays: { format: NodeLoaderHooksFormat };\n\n // If file has extension not understood by node, then ask node how it would treat the emitted extension.\n // E.g. .mts compiles to .mjs, so ask node how to classify an .mjs file.\n const ext = extname(nativePath);\n const tsNodeIgnored = tsNodeService.ignored(nativePath);\n const nodeEquivalentExt = extensions.nodeEquivalents.get(ext);\n if (nodeEquivalentExt && !tsNodeIgnored) {\n nodeSays = await entrypointFallback(() =>\n defer(formatUrl(pathToFileURL(nativePath + nodeEquivalentExt)))\n );\n } else {\n try {\n nodeSays = await entrypointFallback(defer);\n } catch (e) {\n if (\n e instanceof Error &&\n tsNodeIgnored &&\n extensions.nodeDoesNotUnderstand.includes(ext)\n ) {\n e.message +=\n `\\n\\n` +\n `Hint:\\n` +\n `ts-node is configured to ignore this file.\\n` +\n `If you want ts-node to handle this file, consider enabling the \"skipIgnore\" option or adjusting your \"ignore\" patterns.\\n` +\n `https://typestrong.org/ts-node/docs/scope\\n`;\n }\n throw e;\n }\n }\n // For files compiled by ts-node that node believes are either CJS or ESM, check if we should override that classification\n if (\n !tsNodeService.ignored(nativePath) &&\n (nodeSays.format === 'commonjs' || nodeSays.format === 'module')\n ) {\n const { moduleType } =\n tsNodeService.moduleTypeClassifier.classifyModuleByModuleTypeOverrides(\n normalizeSlashes(nativePath)\n );\n if (moduleType === 'cjs') {\n return { format: 'commonjs' };\n } else if (moduleType === 'esm') {\n return { format: 'module' };\n }\n }\n return nodeSays;\n }\n\n async function transformSource(\n source: string | Buffer,\n context: { url: string; format: NodeLoaderHooksFormat },\n defaultTransformSource: typeof transformSource\n ): Promise<{ source: string | Buffer }> {\n if (source === null || source === undefined) {\n throw new Error('No source');\n }\n\n const defer = () =>\n defaultTransformSource(source, context, defaultTransformSource);\n\n const sourceAsString =\n typeof source === 'string' ? source : source.toString('utf8');\n\n const { url } = context;\n const parsed = parseUrl(url);\n\n if (!isFileUrlOrNodeStyleSpecifier(parsed)) {\n return defer();\n }\n const nativePath = fileURLToPath(url);\n\n if (tsNodeService.ignored(nativePath)) {\n return defer();\n }\n\n const emittedJs = tsNodeService.compile(sourceAsString, nativePath);\n\n return { source: emittedJs };\n }\n\n return hooksAPI;\n}\n\nasync function addShortCircuitFlag<T>(fn: () => Promise<T>) {\n const ret = await fn();\n // Not sure if this is necessary; being lazy. Can revisit in the future.\n if (ret == null) return ret;\n return {\n ...ret,\n shortCircuit: true,\n };\n}\n"]}
\No newline at end of file