Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit 66aebb1

Browse files
guybedfordnodejs-ci
authored andcommitted
irp type implementation
1 parent 96c35ce commit 66aebb1

File tree

11 files changed

+423
-105
lines changed

11 files changed

+423
-105
lines changed

lib/internal/errors.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -864,13 +864,6 @@ E('ERR_MISSING_ARGS',
864864
E('ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK',
865865
'The ES Module loader may not return a format of \'dynamic\' when no ' +
866866
'dynamicInstantiate function was provided', Error);
867-
E('ERR_MODULE_NOT_FOUND', (module, base, legacyResolution) => {
868-
let msg = `Cannot find module '${module}' imported from ${base}.`;
869-
if (legacyResolution)
870-
msg += ' Legacy behavior in require() would have found it at ' +
871-
legacyResolution;
872-
return msg;
873-
}, Error);
874867
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
875868
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
876869
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
@@ -973,10 +966,10 @@ E('ERR_UNKNOWN_CREDENTIAL', '%s identifier does not exist: %s', Error);
973966
E('ERR_UNKNOWN_ENCODING', 'Unknown encoding: %s', TypeError);
974967

975968
// This should probably be a `TypeError`.
976-
E('ERR_UNKNOWN_FILE_EXTENSION', 'Unknown file extension: \'%s\' imported ' +
977-
'from %s', Error);
978969
E('ERR_UNKNOWN_MODULE_FORMAT', 'Unknown module format: %s', RangeError);
979970
E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
971+
E('ERR_UNSUPPORTED_FILE_EXTENSION', 'Unsupported file extension: \'%s\' ' +
972+
'imported from %s', Error);
980973

981974
E('ERR_V8BREAKITERATOR',
982975
'Full ICU data not installed. See https://github.com/nodejs/node/wiki/Intl',

lib/internal/modules/esm/default_resolve.js

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
11
'use strict';
22

3-
const { URL } = require('url');
4-
const CJSmodule = require('internal/modules/cjs/loader');
53
const internalFS = require('internal/fs/utils');
64
const { NativeModule } = require('internal/bootstrap/loaders');
75
const { extname } = require('path');
86
const { realpathSync } = require('fs');
97
const { getOptionValue } = require('internal/options');
108
const preserveSymlinks = getOptionValue('--preserve-symlinks');
119
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
12-
const {
13-
ERR_MODULE_NOT_FOUND,
14-
ERR_UNKNOWN_FILE_EXTENSION
15-
} = require('internal/errors').codes;
10+
const { ERR_UNSUPPORTED_FILE_EXTENSION } = require('internal/errors').codes;
1611
const { resolve: moduleWrapResolve } = internalBinding('module_wrap');
1712
const { pathToFileURL, fileURLToPath } = require('internal/url');
1813

1914
const realpathCache = new Map();
2015

21-
function search(target, base) {
22-
try {
23-
return moduleWrapResolve(target, base);
24-
} catch (e) {
25-
e.stack; // cause V8 to generate stack before rethrow
26-
let error = e;
27-
try {
28-
const questionedBase = new URL(base);
29-
const tmpMod = new CJSmodule(questionedBase.pathname, null);
30-
tmpMod.paths = CJSmodule._nodeModulePaths(
31-
new URL('./', questionedBase).pathname);
32-
const found = CJSmodule._resolveFilename(target, tmpMod);
33-
error = new ERR_MODULE_NOT_FOUND(target, fileURLToPath(base), found);
34-
} catch {
35-
// ignore
36-
}
37-
throw error;
38-
}
39-
}
40-
4116
const extensionFormatMap = {
4217
'__proto__': null,
43-
'.mjs': 'esm'
18+
'.mjs': 'esm',
19+
'.js': 'esm'
20+
};
21+
22+
const legacyExtensionFormatMap = {
23+
'__proto__': null,
24+
'.js': 'cjs',
25+
'.json': 'cjs',
26+
'.mjs': 'esm',
27+
'.node': 'cjs'
4428
};
4529

4630
function resolve(specifier, parentURL) {
@@ -51,10 +35,14 @@ function resolve(specifier, parentURL) {
5135
};
5236
}
5337

54-
let url = search(specifier,
55-
parentURL || pathToFileURL(`${process.cwd()}/`).href);
56-
5738
const isMain = parentURL === undefined;
39+
if (isMain)
40+
parentURL = pathToFileURL(`${process.cwd()}/`).href;
41+
42+
const resolved = moduleWrapResolve(specifier, parentURL, isMain);
43+
44+
let url = resolved.url;
45+
const legacy = resolved.legacy;
5846

5947
if (isMain ? !preserveSymlinksMain : !preserveSymlinks) {
6048
const real = realpathSync(fileURLToPath(url), {
@@ -67,14 +55,14 @@ function resolve(specifier, parentURL) {
6755
}
6856

6957
const ext = extname(url.pathname);
58+
let format = (legacy ? legacyExtensionFormatMap : extensionFormatMap)[ext];
7059

71-
let format = extensionFormatMap[ext];
7260
if (!format) {
7361
if (isMain)
74-
format = 'cjs';
62+
format = legacy ? 'cjs' : 'esm';
7563
else
76-
throw new ERR_UNKNOWN_FILE_EXTENSION(url.pathname,
77-
fileURLToPath(parentURL));
64+
throw new ERR_UNSUPPORTED_FILE_EXTENSION(fileURLToPath(url),
65+
fileURLToPath(parentURL));
7866
}
7967

8068
return { url: `${url}`, format };

lib/internal/modules/esm/loader.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const ModuleJob = require('internal/modules/esm/module_job');
1414
const defaultResolve = require('internal/modules/esm/default_resolve');
1515
const createDynamicModule = require(
1616
'internal/modules/esm/create_dynamic_module');
17-
const translators = require('internal/modules/esm/translators');
17+
const { translators } = require('internal/modules/esm/translators');
1818

1919
const FunctionBind = Function.call.bind(Function.prototype.bind);
2020

@@ -32,6 +32,9 @@ class Loader {
3232
// Registry of loaded modules, akin to `require.cache`
3333
this.moduleMap = new ModuleMap();
3434

35+
// map of already-loaded CJS modules to use
36+
this.cjsCache = new Map();
37+
3538
// The resolver has the signature
3639
// (specifier : string, parentURL : string, defaultResolve)
3740
// -> Promise<{ url : string, format: string }>

lib/internal/modules/esm/module_job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ModuleJob {
2323

2424
// This is a Promise<{ module, reflect }>, whose fields will be copied
2525
// onto `this` by `link()` below once it has been resolved.
26-
this.modulePromise = moduleProvider(url, isMain);
26+
this.modulePromise = moduleProvider.call(loader, url, isMain);
2727
this.module = undefined;
2828
this.reflect = undefined;
2929

lib/internal/modules/esm/translators.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const StringReplace = Function.call.bind(String.prototype.replace);
2323
const debug = debuglog('esm');
2424

2525
const translators = new SafeMap();
26-
module.exports = translators;
26+
exports.translators = translators;
2727

2828
function initializeImportMeta(meta, { url }) {
2929
meta.url = url;
@@ -35,7 +35,7 @@ async function importModuleDynamically(specifier, { url }) {
3535
}
3636

3737
// Strategy for loading a standard JavaScript module
38-
translators.set('esm', async (url) => {
38+
translators.set('esm', async function(url) {
3939
const source = `${await readFileAsync(new URL(url))}`;
4040
debug(`Translating StandardModule ${url}`);
4141
const module = new ModuleWrap(stripShebang(source), url);
@@ -52,9 +52,14 @@ translators.set('esm', async (url) => {
5252
// Strategy for loading a node-style CommonJS module
5353
const isWindows = process.platform === 'win32';
5454
const winSepRegEx = /\//g;
55-
translators.set('cjs', async (url, isMain) => {
55+
translators.set('cjs', async function(url, isMain) {
5656
debug(`Translating CJSModule ${url}`);
5757
const pathname = internalURLModule.fileURLToPath(new URL(url));
58+
const cached = this.cjsCache.get(url);
59+
if (cached) {
60+
this.cjsCache.delete(url);
61+
return cached;
62+
}
5863
const module = CJSModule._cache[
5964
isWindows ? StringReplace(pathname, winSepRegEx, '\\') : pathname];
6065
if (module && module.loaded) {
@@ -74,7 +79,7 @@ translators.set('cjs', async (url, isMain) => {
7479

7580
// Strategy for loading a node builtin CommonJS module that isn't
7681
// through normal resolution
77-
translators.set('builtin', async (url) => {
82+
translators.set('builtin', async function(url) {
7883
debug(`Translating BuiltinModule ${url}`);
7984
// slice 'node:' scheme
8085
const id = url.slice(5);

lib/internal/process/esm_loader.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
3333
};
3434

3535
let loaderResolve;
36-
exports.loaderPromise = new Promise((resolve, reject) => {
37-
loaderResolve = resolve;
38-
});
36+
exports.loaderPromise = new Promise((resolve) => loaderResolve = resolve);
3937

4038
exports.ESMLoader = undefined;
4139

src/env.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@ namespace loader {
7373
class ModuleWrap;
7474

7575
struct PackageConfig {
76-
enum class Exists { Yes, No };
77-
enum class IsValid { Yes, No };
78-
enum class HasMain { Yes, No };
79-
80-
Exists exists;
81-
IsValid is_valid;
82-
HasMain has_main;
83-
std::string main;
76+
struct Exists {
77+
enum Bool { No, Yes };
78+
};
79+
struct IsValid {
80+
enum Bool { No, Yes };
81+
};
82+
struct HasMain {
83+
enum Bool { No, Yes };
84+
};
85+
struct IsESM {
86+
enum Bool { No, Yes };
87+
};
88+
const Exists::Bool exists;
89+
const IsValid::Bool is_valid;
90+
const HasMain::Bool has_main;
91+
const std::string main;
92+
const IsESM::Bool esm;
8493
};
8594
} // namespace loader
8695

@@ -168,6 +177,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
168177
V(env_var_settings_string, "envVarSettings") \
169178
V(errno_string, "errno") \
170179
V(error_string, "error") \
180+
V(esm_string, "esm") \
171181
V(exchange_string, "exchange") \
172182
V(exit_code_string, "exitCode") \
173183
V(expire_string, "expire") \
@@ -206,6 +216,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
206216
V(kill_signal_string, "killSignal") \
207217
V(kind_string, "kind") \
208218
V(library_string, "library") \
219+
V(legacy_string, "legacy") \
209220
V(mac_string, "mac") \
210221
V(main_string, "main") \
211222
V(max_buffer_string, "maxBuffer") \

0 commit comments

Comments
 (0)