|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { URL } = require('url'); |
| 4 | +const internalCJSModule = require('internal/module'); |
| 5 | +const internalURLModule = require('internal/url'); |
| 6 | +const internalFS = require('internal/fs'); |
| 7 | +const NativeModule = require('native_module'); |
| 8 | +const { extname } = require('path'); |
| 9 | +const { realpathSync } = require('fs'); |
| 10 | +const preserveSymlinks = !!process.binding('config').preserveSymlinks; |
| 11 | +const { |
| 12 | + ModuleWrap, |
| 13 | + createDynamicModule |
| 14 | +} = require('internal/loader/ModuleWrap'); |
| 15 | +const errors = require('internal/errors'); |
| 16 | + |
| 17 | +const search = require('internal/loader/search'); |
| 18 | +const asyncReadFile = require('util').promisify(require('fs').readFile); |
| 19 | +const debug = require('util').debuglog('esm'); |
| 20 | + |
| 21 | +const realpathCache = new Map(); |
| 22 | + |
| 23 | +const formatProviders = new Map(); |
| 24 | +exports.formatProviders = formatProviders; |
| 25 | + |
| 26 | +// Strategy for loading a standard JavaScript module |
| 27 | +formatProviders.set('esm', async (url) => { |
| 28 | + const source = `${await asyncReadFile(url)}`; |
| 29 | + debug(`Loading StandardModule ${url}`); |
| 30 | + return new ModuleWrap(internalCJSModule.stripShebang(source), |
| 31 | + `${url}`); |
| 32 | +}); |
| 33 | + |
| 34 | +// Strategy for loading a node-style CommonJS module |
| 35 | +formatProviders.set('cjs', async (url) => { |
| 36 | + const ctx = createDynamicModule(['default'], url, (reflect) => { |
| 37 | + debug(`Loading CJSModule ${url.pathname}`); |
| 38 | + const CJSModule = require('module'); |
| 39 | + const pathname = internalURLModule.getPathFromURL(url); |
| 40 | + ctx.reflect.exports.default.set(CJSModule._load(pathname)); |
| 41 | + }); |
| 42 | + return ctx.module; |
| 43 | +}); |
| 44 | + |
| 45 | +// Strategy for loading a node builtin CommonJS module that isn't |
| 46 | +// through normal resolution |
| 47 | +formatProviders.set('native', async (url) => { |
| 48 | + const ctx = createDynamicModule(['default'], url, (reflect) => { |
| 49 | + debug(`Loading CJSModule ${url.pathname}`); |
| 50 | + const CJSModule = require('module'); |
| 51 | + const pathname = internalURLModule.getPathFromURL(url); |
| 52 | + ctx.reflect.exports.default.set(CJSModule._load(pathname)); |
| 53 | + }); |
| 54 | + return ctx.module; |
| 55 | +}); |
| 56 | + |
| 57 | +formatProviders.set('json', async (url) => { |
| 58 | + const source = `${await asyncReadFile(url)}`; |
| 59 | + const ctx = createDynamicModule(['default'], url, (reflect) => { |
| 60 | + debug(`Loading JSONModule ${url.pathname}`); |
| 61 | + const json = JSON.parse(source); |
| 62 | + ctx.reflect.exports.default.set(json); |
| 63 | + }); |
| 64 | + return ctx.module; |
| 65 | +}); |
| 66 | + |
| 67 | +// TODO: make this native binary handling only |
| 68 | +formatProviders.set('binary', async (url) => { |
| 69 | + const source = `${await asyncReadFile(url)}`; |
| 70 | + const ctx = createDynamicModule(['default'], url, (reflect) => { |
| 71 | + debug(`Loading JSONModule ${url.pathname}`); |
| 72 | + const json = JSON.parse(source); |
| 73 | + ctx.reflect.exports.default.set(json); |
| 74 | + }); |
| 75 | + return ctx.module; |
| 76 | +}); |
| 77 | + |
| 78 | +function normalizeBaseURL(baseURLOrString) { |
| 79 | + if (baseURLOrString instanceof URL) return baseURLOrString; |
| 80 | + if (typeof baseURLOrString === 'string') return new URL(baseURLOrString); |
| 81 | + return undefined; |
| 82 | +} |
| 83 | + |
| 84 | +exports.resolve = resolve; |
| 85 | +function resolve(specifier, parentURLOrString) { |
| 86 | + if (NativeModule.nonInternalExists(specifier)) { |
| 87 | + return { |
| 88 | + url: new URL(`node:${specifier}`), |
| 89 | + format: 'native' |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + const parentURL = normalizeBaseURL(parentURLOrString); |
| 94 | + let url = search(specifier, parentURL); |
| 95 | + |
| 96 | + if (url.protocol !== 'file:') { |
| 97 | + throw new errors.Error('ERR_INVALID_PROTOCOL', url.protocol, 'file:'); |
| 98 | + } |
| 99 | + |
| 100 | + if (!preserveSymlinks) { |
| 101 | + const real = realpathSync(internalURLModule.getPathFromURL(url), { |
| 102 | + [internalFS.realpathCacheKey]: realpathCache |
| 103 | + }); |
| 104 | + const old = url; |
| 105 | + url = internalURLModule.getURLFromFilePath(real); |
| 106 | + url.search = old.search; |
| 107 | + url.hash = old.hash; |
| 108 | + } |
| 109 | + |
| 110 | + const ext = extname(url.pathname); |
| 111 | + switch (ext) { |
| 112 | + case '.mjs': |
| 113 | + return { url, format: 'esm' }; |
| 114 | + case '.json': |
| 115 | + return { url, format: 'json' }; |
| 116 | + case '.node': |
| 117 | + return { url, format: 'binary' }; |
| 118 | + default: |
| 119 | + return { url, format: 'cjs' }; |
| 120 | + } |
| 121 | +}; |
0 commit comments