import * as cryptoStar from "crypto"; import * as grafastStar from "grafast"; import * as graphqlStar from "grafast/graphql"; import * as utilStar from "util"; import type { ExportOptions } from "./interfaces.ts"; interface $$Export { moduleName: string; exportName: string | "default" | "*" | string[]; } function isPojo(obj: any): obj is object { if (obj == null) return false; if (typeof obj !== "object") return false; const proto = Object.getPrototypeOf(obj); return proto === null || proto === Object.prototype; } function makeWellKnownFromOptions(options: ExportOptions) { const namespaces = Object.create(null); const wellKnownMap = new Map(); function exportAll( moduleStar: Record, moduleName: string, preferViaDefault = false, ) { namespaces[moduleName] = moduleStar; for (const exportName of Object.keys(moduleStar)) { if (!wellKnownMap.has(moduleStar[exportName])) { /** * ESM is still a bit flaky, so though `import { foo } from 'bar';` may * work in some contexts, in raw Node it's often required to do * `import bar from 'bar'; const foo = bar.foo;`. This code determines * if this latter approach is desired. */ const viaDefault = preferViaDefault && exportName !== "default" && moduleStar[exportName] === moduleStar["default"]?.[exportName]; wellKnownMap.set(moduleStar[exportName], { moduleName, exportName: viaDefault ? ["default", exportName] : exportName, }); } } if (!wellKnownMap.has(moduleStar)) { wellKnownMap.set(moduleStar, { moduleName, exportName: "*" }); } // Node's ESM support is... interesting. Let's handle stuff hanging off of default too if (isPojo(moduleStar.default)) { for (const [exportName, value] of Object.entries(moduleStar.default)) { if (!wellKnownMap.has(value)) { wellKnownMap.set(value, { moduleName, exportName: ["default", exportName], }); } } } } exportAll(cryptoStar, "crypto"); exportAll(grafastStar, "grafast"); exportAll(graphqlStar, "graphql"); exportAll(utilStar, "util"); // When defining custom scalars it's often useful to copy the implementation from builtins for (const builtinScalarName of [ "GraphQLBoolean", "GraphQLInt", "GraphQLFloat", "GraphQLString", "GraphQLID", ] as const) { for (const method of ["serialize", "parseValue", "parseLiteral"] as const) { wellKnownMap.set(graphqlStar[builtinScalarName][method], { moduleName: "graphql", exportName: [builtinScalarName, method], }); } } // Now process options if (options.modules) { for (const [moduleName, moduleStar] of Object.entries(options.modules)) { exportAll(moduleStar, moduleName, true); } } return { namespaces, wellKnownMap }; } const $$wellKnown = Symbol("wellKnown"); declare module "./interfaces.js" { interface ExportOptions { /** @internal */ [$$wellKnown]?: ReturnType; } } function getWellKnownFromOptions( options: ExportOptions, ): ReturnType { if (!options[$$wellKnown]) { options[$$wellKnown] = makeWellKnownFromOptions(options); } return options[$$wellKnown]; } /** * Determines if the thing is something well known (like a Node.js builtin); if * so, returns the export description of it. * * @internal */ export function wellKnown( options: ExportOptions, thing: unknown, ): $$Export | undefined { const { wellKnownMap, namespaces } = getWellKnownFromOptions(options); // Straight imports are relatively easy: const simple = wellKnownMap.get(thing); if (simple) { return simple; } // Checking for namespace matches is a bit tougher for (const moduleName in namespaces) { if (isSameNamespace(thing, namespaces[moduleName])) { return { moduleName, exportName: "*" }; } } return undefined; } function isSameNamespace>( thing: unknown, namespace: TNamespace, ): thing is TNamespace { if (typeof thing !== "object" || thing == null) { return false; } const thingKeys = Object.keys(thing); const nspKeys = Object.keys(namespace); if (thingKeys.length !== nspKeys.length) { return false; } for (const key of nspKeys) { if ((thing as Record)[key] !== namespace[key]) { return false; } } return true; }