fix(api): address PR comments
MODIFY
README.md
README.md
@@ -11,10 +11,10 @@ When an importer is created, it has the following API:
```js
/**
- * spec - Module specifier string provided to import
- * url - referrer URL
+ * specifier {string} Module specifier string provided to import
+ * referrer {AbsoluteSpecifier} How to find the referrer module
*/
-function importer({ spec, url }): Promise<ModuleNamespace>;
+function importer({ specifier, referrer }): Promise<ModuleNamespace>;
```
## Using
@@ -31,6 +31,10 @@ const importer = makeImporter({
locate, // (absSpecifer: AbsoluteSpecifier) => Promise<ModuleLocation> // cached
retrieve, // (loc: ModuleLocation) => Promise<ResourceStream> // (not cached)
analyze, // (rs: ResourceStream) => Promise<ModuleStaticRecord> // cached by ModuleLocation
+ // interface ModuleLinkageRecord extends ModuleStaticRecord {
+ // moduleLocation: ModuleLocation,
+ // moduleLocations: Record<string, ModuleLocation>,
+ // }
rootLinker, // see below:
// {
// link: (lr: ModuleLinkageRecord, recursiveLink, preEndowments) => ModuleInstance
@@ -45,7 +49,7 @@ const importer = makeImporter({
One ModuleLocation per ModuleStaticRecord.
-One ModuleLinkageRecord per ModuleLocation (in a given Linker).
+One ModuleLinkageRecord per ModuleLocation (in a given Linker's instanceCache).
Multiple Linkers per Evaluator. One Evaluator per Linker.

MODIFY
src/analyze.js
src/analyze.js
-export const makeTypeAnalyzer = typeMap => async resourceStream => {
+export const makeTypeAnalyzer = typeMap => resourceStream =>
+ new Promise(resolve => {
const { type } = resourceStream;
const handler = typeMap[type];
if (handler === undefined) {
throw TypeError(`Type analyzer for '${type}' not specified`);
}
- return handler(resourceStream);
-};
+ resolve(handler(resourceStream));
+ });

MODIFY
src/locate.js
src/locate.js
export const makeSuffixLocator = (suffix = '.js') => {
- return async scopedRef => {
+ return async absoluteSpecifier => {
// This constructor throws on anything but absolute URLs.
// TODO: Ensure this is powerless, or inline string-manipulation code.
- const url = new URL(scopedRef);
+ const url = new URL(absoluteSpecifier);
let path = url.pathname;
// Translate trailing slash to an index reference.

MODIFY
src/moduleInstance.js
src/moduleInstance.js
+/* global SES */
// This file needs to provide the API that the transforms
// target.
+const harden = (typeof SES !== 'undefined' && SES.harden) || Object.freeze;
+
const {
create,
entries,
+ keys,
defineProperty: defProp,
- freeze: harden,
getOwnPropertyDescriptors: getProps,
} = Object;
@@ -244,7 +247,7 @@ export function makeModuleInstance(
}
}
- for (const [importName, notify] of Object.entries(candidateAll)) {
+ for (const [importName, notify] of entries(candidateAll)) {
if (!notifiers[importName] && notify !== false) {
notifiers[importName] = notify;
@@ -265,7 +268,7 @@ export function makeModuleInstance(
// Sort the module namespace as per spec.
// TODO should create something more like a
// "Module Namespace Exotic Object".
- Object.keys(moduleNSProps)
+ keys(moduleNSProps)
.sort()
.forEach(k => defProp(moduleNS, k, moduleNSProps[k]));
}

MODIFY
src/pipeline.js
src/pipeline.js
@@ -20,7 +20,7 @@ export const makeImporter = (importHooks, moduleCache = new Map()) => {
// Translate the linkage record into a module instance, and cache it.
const linkageRecord = moduleCache.get(moduleLocation);
moduleInstance = subLinker.link(
- linkageRecord, // has url
+ linkageRecord, // has moduleLocation
recursiveLink,
preEndowments,
);
@@ -88,8 +88,8 @@ export const makeImporter = (importHooks, moduleCache = new Map()) => {
}
const importer = async (srcSpec, preEndowments) => {
- const { spec, staticRecord, url } = srcSpec;
- const moduleLocation = await loadOne(spec, staticRecord, url);
+ const { specifier, staticRecord, referrer } = srcSpec;
+ const moduleLocation = await loadOne(specifier, staticRecord, referrer);
// Begin initialization of the linked modules.
const moduleInstance = recursiveLink(

MODIFY
src/retrieve.js
src/retrieve.js
export const makeProtocolRetriever = protoHandlers => {
- return async absoluteSpecifier => {
+ const protoMap = new Map(Object.entries(protoHandlers));
+ return absoluteSpecifier =>
+ new Promise(resolve => {
const url = new URL(absoluteSpecifier);
const { protocol } = url;
- const handler = protoHandlers[protocol];
+ const handler = protoMap.get(protocol);
if (handler === undefined) {
throw TypeError(
`Protocol retriever for ${absoluteSpecifier} not specified`,
);
}
- return handler(url);
- };
+ resolve(handler(url));
+ });
};

MODIFY
test/test-moddir.js
test/test-moddir.js
@@ -39,7 +39,7 @@ test('moddir index.js', async t => {
const rootUrl = `file://${path.join(__dirname, 'moddir')}`;
const { importer, endowments } = setup(rootUrl);
t.deepEqual(
- await importer({ spec: '.', url: `${rootUrl}/` }, endowments),
+ await importer({ specifier: '.', referrer: `${rootUrl}/` }, endowments),
{
default: 42,
mu: 89,
@@ -66,7 +66,7 @@ test('moddir function.js', async t => {
const rootUrl = `file://${path.join(__dirname, 'moddir')}`;
const { importer, endowments } = setup(rootUrl);
const ns = await importer(
- { spec: './function', url: `${rootUrl}/` },
+ { specifier: './function', referrer: `${rootUrl}/` },
endowments,
);
t.is(typeof ns.fn1, 'function', `function fn1 is exported`);
@@ -85,7 +85,10 @@ test('moddir exports', async t => {
const rootUrl = `file://${path.join(__dirname, 'moddir')}`;
const { importer, endowments } = setup(rootUrl);
t.deepEqual(
- await importer({ spec: './exportNS', url: `${rootUrl}/` }, endowments),
+ await importer(
+ { specifier: './exportNS', referrer: `${rootUrl}/` },
+ endowments,
+ ),
{
ns2: {
f: 'f',
@@ -96,7 +99,10 @@ test('moddir exports', async t => {
);
t.deepEqual(
- await importer({ spec: './exportAll', url: `${rootUrl}/` }, endowments),
+ await importer(
+ { specifier: './exportAll', referrer: `${rootUrl}/` },
+ endowments,
+ ),
{},
're-exporting nothing',
);
@@ -112,7 +118,7 @@ test('invalid export all', async t => {
const rootUrl = `file://${path.join(__dirname, 'invalid')}`;
const { importer, endowments } = setup(rootUrl);
await t.rejects(
- importer({ spec: './index', url: `${rootUrl}/` }, endowments),
+ importer({ specifier: './index', referrer: `${rootUrl}/` }, endowments),
SyntaxError,
'exporting all default fails',
);
@@ -129,7 +135,7 @@ test('moddir export recursive', async t => {
const { importer, endowments } = setup(rootUrl);
t.deepEqual(
await importer(
- { spec: './exportRecursive', url: `${rootUrl}/` },
+ { specifier: './exportRecursive', referrer: `${rootUrl}/` },
endowments,
),
{

MODIFY
test/test-pipeline.js
test/test-pipeline.js
@@ -73,7 +73,10 @@ export default 123;
});
t.deepEquals(
- await importer({ spec: './hello/', url: 'https://example.com/' }),
+ await importer({
+ specifier: './hello/',
+ referrer: 'https://example.com/',
+ }),
{ default: indexSource },
'importer works',
);
@@ -127,7 +130,8 @@ test('import cached specifier', async t => {
moduleCache,
);
- const imp = spec => importer({ spec, url: 'file:///some/where/over' });
+ const imp = specifier =>
+ importer({ specifier, referrer: 'file:///some/where/over' });
t.deepEquals(
(await imp('@agoric/hello'))('you'),
'Hello, you!',

MODIFY
test/test-test262.js
test/test-test262.js
@@ -324,7 +324,7 @@ function executeTest(testInfo, rootPath, relativePath) {
const globalObject = evaluateProgram('Function("return this")()');
injectTest262Harness(globalObject, t);
- await importer({ spec: relativePath, url: `${rootUrl}/` }, {});
+ await importer({ specifier: relativePath, referrer: `${rootUrl}/` }, {});
} catch (e) {
if (testInfo.negative) {
if (e.constructor.name !== testInfo.negative.type) {

fix(api): address PR comments