Skip to content

Commit 5496f77

Browse files
committed
stricter signature, parent url tweaks
1 parent 6e328d0 commit 5496f77

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

lib/internal/loader/Loader.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ class Loader {
3838
resolve = resolver;
3939
}
4040

41-
async resolve(specifier, parentUrlOrString = this.base) {
42-
const { url, format } = await resolve(specifier, parentUrlOrString);
41+
async resolve(specifier, parentURLOrString = this.base) {
42+
if (typeof parentURLOrString === 'string') {
43+
parentURLOrString = new URL(parentURLOrString);
44+
}
45+
else if (parentURLOrString instanceof URL === false) {
46+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'parentURLOrString', 'URL');
47+
}
48+
const { url, format } = await resolve(specifier, parentURLOrString);
4349
if (typeof url === 'string') {
4450
url = new URL(url);
4551
}
@@ -56,19 +62,19 @@ class Loader {
5662
return { url, format };
5763
}
5864

59-
async getModuleJob(specifier, parentUrlOrString = this.base) {
60-
const { url, format } = await this.resolve(specifier, parentUrlOrString);
65+
async getModuleJob(specifier, parentURLOrString = this.base) {
66+
const { url, format } = await this.resolve(specifier, parentURLOrString);
6167
const urlString = `${url}`;
6268
let job = this.moduleMap.get(urlString);
6369
if (job === undefined) {
64-
job = new ModuleJob(this, url, urlString, formatProviders.get(format));
70+
job = new ModuleJob(this, url, formatProviders.get(format));
6571
this.moduleMap.set(urlString, job);
6672
}
6773
return job;
6874
}
6975

70-
async import(specifier, parentUrlOrString = this.base) {
71-
const job = await this.getModuleJob(specifier, parentUrlOrString);
76+
async import(specifier, parentURLOrString = this.base) {
77+
const job = await this.getModuleJob(specifier, parentURLOrString);
7278
const module = await job.run();
7379
return getNamespaceOfModuleWrap(module);
7480
}

lib/internal/loader/ModuleJob.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class ModuleJob {
99
/**
1010
* @param {module: ModuleWrap?, compiled: Promise} moduleProvider
1111
*/
12-
constructor(loader, url, urlString, moduleProvider) {
12+
constructor(loader, url, moduleProvider) {
1313
this.url = url;
1414
this.loader = loader;
1515
this.error = null;
@@ -26,7 +26,7 @@ class ModuleJob {
2626
this.module = await this.modulePromise;
2727
this.module.link(async (dependencySpecifier) => {
2828
const dependencyJobPromise =
29-
this.loader.getModuleJob(dependencySpecifier, urlString);
29+
this.loader.getModuleJob(dependencySpecifier, url);
3030
dependencyJobs.push(dependencyJobPromise);
3131
const dependencyJob = await dependencyJobPromise;
3232
return dependencyJob.modulePromise;

lib/internal/loader/ModuleRequest.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ formatProviders.set('cjs', async (url) => {
4646
// through normal resolution
4747
formatProviders.set('native', async (url) => {
4848
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));
49+
debug(`Loading NativeModule ${url.pathname}`);
50+
const exports = NativeModule.require(url.pathname);
51+
reflect.exports.default.set(exports);
5352
});
5453
return ctx.module;
5554
});
@@ -66,31 +65,24 @@ formatProviders.set('json', async (url) => {
6665

6766
// TODO: make this native binary handling only
6867
formatProviders.set('binary', async (url) => {
69-
const source = `${await asyncReadFile(url)}`;
7068
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);
69+
debug(`Loading CJSModule ${url.pathname}`);
70+
const CJSModule = require('module');
71+
const pathname = internalURLModule.getPathFromURL(url);
72+
ctx.reflect.exports.default.set(CJSModule._load(pathname));
7473
});
7574
return ctx.module;
7675
});
7776

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-
8477
exports.resolve = resolve;
85-
function resolve(specifier, parentURLOrString) {
78+
function resolve(specifier, parentURL) {
8679
if (NativeModule.nonInternalExists(specifier)) {
8780
return {
8881
url: new URL(`node:${specifier}`),
8982
format: 'native'
9083
};
9184
}
9285

93-
const parentURL = normalizeBaseURL(parentURLOrString);
9486
let url = search(specifier, parentURL);
9587

9688
if (url.protocol !== 'file:') {

0 commit comments

Comments
 (0)