11cb0ef41Sopenharmony_ciimport { URL } from 'url';
21cb0ef41Sopenharmony_ciimport path from 'path';
31cb0ef41Sopenharmony_ciimport process from 'process';
41cb0ef41Sopenharmony_ciimport { builtinModules } from 'module';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst JS_EXTENSIONS = new Set(['.js', '.mjs']);
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst baseURL = new URL('file://');
91cb0ef41Sopenharmony_cibaseURL.pathname = process.cwd() + '/';
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciexport function resolve(specifier, { parentURL = baseURL }, next) {
121cb0ef41Sopenharmony_ci  if (builtinModules.includes(specifier)) {
131cb0ef41Sopenharmony_ci    return {
141cb0ef41Sopenharmony_ci      shortCircuit: true,
151cb0ef41Sopenharmony_ci      url: 'node:' + specifier
161cb0ef41Sopenharmony_ci    };
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci  if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
191cb0ef41Sopenharmony_ci    // For node_modules support:
201cb0ef41Sopenharmony_ci    // return next(specifier);
211cb0ef41Sopenharmony_ci    throw new Error(
221cb0ef41Sopenharmony_ci      `imports must be URLs or begin with './', or '../'; '${specifier}' does not`);
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  const resolved = new URL(specifier, parentURL);
251cb0ef41Sopenharmony_ci  return {
261cb0ef41Sopenharmony_ci    shortCircuit: true,
271cb0ef41Sopenharmony_ci    url: resolved.href
281cb0ef41Sopenharmony_ci  };
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciexport function getFormat(url, context, defaultGetFormat) {
321cb0ef41Sopenharmony_ci  if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) {
331cb0ef41Sopenharmony_ci    return {
341cb0ef41Sopenharmony_ci      format: 'builtin'
351cb0ef41Sopenharmony_ci    };
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci  const { pathname } = new URL(url);
381cb0ef41Sopenharmony_ci  const ext = path.extname(pathname);
391cb0ef41Sopenharmony_ci  if (!JS_EXTENSIONS.has(ext)) {
401cb0ef41Sopenharmony_ci    throw new Error(
411cb0ef41Sopenharmony_ci      `Cannot load file with non-JavaScript file extension ${ext}.`);
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  return {
441cb0ef41Sopenharmony_ci    format: 'module'
451cb0ef41Sopenharmony_ci  };
461cb0ef41Sopenharmony_ci}
47