Skip to content

Commit

Permalink
stricter signature, parent url tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 3, 2017
1 parent 6e328d0 commit 5496f77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
20 changes: 13 additions & 7 deletions lib/internal/loader/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ class Loader {
resolve = resolver;
}

async resolve(specifier, parentUrlOrString = this.base) {
const { url, format } = await resolve(specifier, parentUrlOrString);
async resolve(specifier, parentURLOrString = this.base) {
if (typeof parentURLOrString === 'string') {
parentURLOrString = new URL(parentURLOrString);
}
else if (parentURLOrString instanceof URL === false) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'parentURLOrString', 'URL');
}
const { url, format } = await resolve(specifier, parentURLOrString);
if (typeof url === 'string') {
url = new URL(url);
}
Expand All @@ -56,19 +62,19 @@ class Loader {
return { url, format };
}

async getModuleJob(specifier, parentUrlOrString = this.base) {
const { url, format } = await this.resolve(specifier, parentUrlOrString);
async getModuleJob(specifier, parentURLOrString = this.base) {
const { url, format } = await this.resolve(specifier, parentURLOrString);
const urlString = `${url}`;
let job = this.moduleMap.get(urlString);
if (job === undefined) {
job = new ModuleJob(this, url, urlString, formatProviders.get(format));
job = new ModuleJob(this, url, formatProviders.get(format));
this.moduleMap.set(urlString, job);
}
return job;
}

async import(specifier, parentUrlOrString = this.base) {
const job = await this.getModuleJob(specifier, parentUrlOrString);
async import(specifier, parentURLOrString = this.base) {
const job = await this.getModuleJob(specifier, parentURLOrString);
const module = await job.run();
return getNamespaceOfModuleWrap(module);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/loader/ModuleJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ModuleJob {
/**
* @param {module: ModuleWrap?, compiled: Promise} moduleProvider
*/
constructor(loader, url, urlString, moduleProvider) {
constructor(loader, url, moduleProvider) {
this.url = url;
this.loader = loader;
this.error = null;
Expand All @@ -26,7 +26,7 @@ class ModuleJob {
this.module = await this.modulePromise;
this.module.link(async (dependencySpecifier) => {
const dependencyJobPromise =
this.loader.getModuleJob(dependencySpecifier, urlString);
this.loader.getModuleJob(dependencySpecifier, url);
dependencyJobs.push(dependencyJobPromise);
const dependencyJob = await dependencyJobPromise;
return dependencyJob.modulePromise;
Expand Down
24 changes: 8 additions & 16 deletions lib/internal/loader/ModuleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ formatProviders.set('cjs', async (url) => {
// through normal resolution
formatProviders.set('native', async (url) => {
const ctx = createDynamicModule(['default'], url, (reflect) => {
debug(`Loading CJSModule ${url.pathname}`);
const CJSModule = require('module');
const pathname = internalURLModule.getPathFromURL(url);
ctx.reflect.exports.default.set(CJSModule._load(pathname));
debug(`Loading NativeModule ${url.pathname}`);
const exports = NativeModule.require(url.pathname);
reflect.exports.default.set(exports);
});
return ctx.module;
});
Expand All @@ -66,31 +65,24 @@ formatProviders.set('json', async (url) => {

// TODO: make this native binary handling only
formatProviders.set('binary', async (url) => {
const source = `${await asyncReadFile(url)}`;
const ctx = createDynamicModule(['default'], url, (reflect) => {
debug(`Loading JSONModule ${url.pathname}`);
const json = JSON.parse(source);
ctx.reflect.exports.default.set(json);
debug(`Loading CJSModule ${url.pathname}`);
const CJSModule = require('module');
const pathname = internalURLModule.getPathFromURL(url);
ctx.reflect.exports.default.set(CJSModule._load(pathname));
});
return ctx.module;
});

function normalizeBaseURL(baseURLOrString) {
if (baseURLOrString instanceof URL) return baseURLOrString;
if (typeof baseURLOrString === 'string') return new URL(baseURLOrString);
return undefined;
}

exports.resolve = resolve;
function resolve(specifier, parentURLOrString) {
function resolve(specifier, parentURL) {
if (NativeModule.nonInternalExists(specifier)) {
return {
url: new URL(`node:${specifier}`),
format: 'native'
};
}

const parentURL = normalizeBaseURL(parentURLOrString);
let url = search(specifier, parentURL);

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

0 comments on commit 5496f77

Please sign in to comment.