11cb0ef41Sopenharmony_ciimport module from 'node:module';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci/** @type {string} */
41cb0ef41Sopenharmony_cilet GET_BUILTIN;
51cb0ef41Sopenharmony_ciexport function initialize(data) {
61cb0ef41Sopenharmony_ci  GET_BUILTIN = data.GET_BUILTIN;
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciexport async function resolve(specifier, context, next) {
101cb0ef41Sopenharmony_ci  const def = await next(specifier, context);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  if (def.url.startsWith('node:')) {
131cb0ef41Sopenharmony_ci    return {
141cb0ef41Sopenharmony_ci      shortCircuit: true,
151cb0ef41Sopenharmony_ci      url: `custom-${def.url}`,
161cb0ef41Sopenharmony_ci      importAttributes: context.importAttributes,
171cb0ef41Sopenharmony_ci    };
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci  return def;
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciexport function load(url, context, next) {
231cb0ef41Sopenharmony_ci  if (url.startsWith('custom-node:')) {
241cb0ef41Sopenharmony_ci    const urlObj = new URL(url);
251cb0ef41Sopenharmony_ci    return {
261cb0ef41Sopenharmony_ci      shortCircuit: true,
271cb0ef41Sopenharmony_ci      source: generateBuiltinModule(urlObj.pathname),
281cb0ef41Sopenharmony_ci      format: 'module',
291cb0ef41Sopenharmony_ci    };
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci  return next(url);
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cifunction generateBuiltinModule(builtinName) {
351cb0ef41Sopenharmony_ci  const builtinInstance = module._load(builtinName);
361cb0ef41Sopenharmony_ci  const builtinExports = [
371cb0ef41Sopenharmony_ci    ...Object.keys(builtinInstance),
381cb0ef41Sopenharmony_ci  ];
391cb0ef41Sopenharmony_ci  return `\
401cb0ef41Sopenharmony_ciconst $builtinInstance = ${GET_BUILTIN}(${JSON.stringify(builtinName)});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciexport const __fromLoader = true;
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciexport default $builtinInstance;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci${
471cb0ef41Sopenharmony_ci  builtinExports
481cb0ef41Sopenharmony_ci    .map(name => `export const ${name} = $builtinInstance.${name};`)
491cb0ef41Sopenharmony_ci    .join('\n')
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci`;
521cb0ef41Sopenharmony_ci}
53