11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  RegExpPrototypeExec,
51cb0ef41Sopenharmony_ci  Uint8Array,
61cb0ef41Sopenharmony_ci} = primordials;
71cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { closeSync, openSync, readSync } = require('fs');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst extensionFormatMap = {
141cb0ef41Sopenharmony_ci  '__proto__': null,
151cb0ef41Sopenharmony_ci  '.cjs': 'commonjs',
161cb0ef41Sopenharmony_ci  '.js': 'module',
171cb0ef41Sopenharmony_ci  '.json': 'json',
181cb0ef41Sopenharmony_ci  '.mjs': 'module',
191cb0ef41Sopenharmony_ci};
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst legacyExtensionFormatMap = {
221cb0ef41Sopenharmony_ci  '__proto__': null,
231cb0ef41Sopenharmony_ci  '.cjs': 'commonjs',
241cb0ef41Sopenharmony_ci  '.js': 'commonjs',
251cb0ef41Sopenharmony_ci  '.json': 'commonjs',
261cb0ef41Sopenharmony_ci  '.mjs': 'module',
271cb0ef41Sopenharmony_ci  '.node': 'commonjs',
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciif (experimentalWasmModules) {
311cb0ef41Sopenharmony_ci  extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci/**
351cb0ef41Sopenharmony_ci * @param {string} mime
361cb0ef41Sopenharmony_ci * @returns {string | null}
371cb0ef41Sopenharmony_ci */
381cb0ef41Sopenharmony_cifunction mimeToFormat(mime) {
391cb0ef41Sopenharmony_ci  if (
401cb0ef41Sopenharmony_ci    RegExpPrototypeExec(
411cb0ef41Sopenharmony_ci      /^\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?$/i,
421cb0ef41Sopenharmony_ci      mime,
431cb0ef41Sopenharmony_ci    ) !== null
441cb0ef41Sopenharmony_ci  ) { return 'module'; }
451cb0ef41Sopenharmony_ci  if (mime === 'application/json') { return 'json'; }
461cb0ef41Sopenharmony_ci  if (experimentalWasmModules && mime === 'application/wasm') { return 'wasm'; }
471cb0ef41Sopenharmony_ci  return null;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cifunction getLegacyExtensionFormat(ext) {
511cb0ef41Sopenharmony_ci  return legacyExtensionFormatMap[ext];
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci/**
551cb0ef41Sopenharmony_ci * For extensionless files in a `module` package scope, or a default `module` scope enabled by the
561cb0ef41Sopenharmony_ci * `--experimental-default-type` flag, we check the file contents to disambiguate between ES module JavaScript and Wasm.
571cb0ef41Sopenharmony_ci * We do this by taking advantage of the fact that all Wasm files start with the header `0x00 0x61 0x73 0x6d` (`_asm`).
581cb0ef41Sopenharmony_ci * @param {URL} url
591cb0ef41Sopenharmony_ci */
601cb0ef41Sopenharmony_cifunction getFormatOfExtensionlessFile(url) {
611cb0ef41Sopenharmony_ci  if (!experimentalWasmModules) { return 'module'; }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const magic = new Uint8Array(4);
641cb0ef41Sopenharmony_ci  let fd;
651cb0ef41Sopenharmony_ci  try {
661cb0ef41Sopenharmony_ci    // TODO(@anonrig): Optimize the following by having a single C++ call
671cb0ef41Sopenharmony_ci    fd = openSync(url);
681cb0ef41Sopenharmony_ci    readSync(fd, magic, 0, 4); // Only read the first four bytes
691cb0ef41Sopenharmony_ci    if (magic[0] === 0x00 && magic[1] === 0x61 && magic[2] === 0x73 && magic[3] === 0x6d) {
701cb0ef41Sopenharmony_ci      return 'wasm';
711cb0ef41Sopenharmony_ci    }
721cb0ef41Sopenharmony_ci  } finally {
731cb0ef41Sopenharmony_ci    if (fd !== undefined) { closeSync(fd); }
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  return 'module';
771cb0ef41Sopenharmony_ci}
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_cimodule.exports = {
801cb0ef41Sopenharmony_ci  extensionFormatMap,
811cb0ef41Sopenharmony_ci  getFormatOfExtensionlessFile,
821cb0ef41Sopenharmony_ci  getLegacyExtensionFormat,
831cb0ef41Sopenharmony_ci  legacyExtensionFormatMap,
841cb0ef41Sopenharmony_ci  mimeToFormat,
851cb0ef41Sopenharmony_ci};
86