pipeline.js: separate rewrite from retrieve
MODIFY
package-lock.json
package-lock.json
{
- "name": "???",
+ "name": "@agoric/make-importer",
"version": "0.0.1",
"lockfileVersion": 1,
"requires": true,
"requireindex": "^1.2.0"
}
},
+ "node-fetch": {
+ "version": "2.6.0",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
+ "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==",
+ "dev": true
+ },
"requireindex": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/requireindex/-/requireindex-1.2.0.tgz",

MODIFY
package.json
package.json
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.0",
+ "node-fetch": "^2.6.0",
"prettier": "^1.18.2",
"tap-spec": "^5.0.0",
"tape": "^4.11.0",

MODIFY
src/index.js
src/index.js
export { makeRootedResolver } from './resolve';
+export { makeSuffixLocator } from './locate';
+export { makeFetchRetriever } from './retrieve';
export { makeImportPipeline as default } from './pipeline';

MODIFY
src/pipeline.js
src/pipeline.js
-export const makeImportPipeline = (
- importHooks,
- moduleCache = new Map(),
- rewriters = new Map(),
-) => {
+export const makeImportPipeline = (importHooks, moduleCache = new Map()) => {
const {
// Functions for the pipeline.
resolve,
locate,
retrieve,
+ rewrite,
rootContainer,
} = importHooks;
@@ -31,13 +28,12 @@ export const makeImportPipeline = (
makeImporter = referrer => {
return async specifier => {
- const moduleId = resolve(specifier, referrer);
+ const scopedRef = resolve(specifier, referrer);
+ const moduleId = await locate(scopedRef);
if (!moduleCache.has(moduleId)) {
// This is sequential because we are beginning an import cycle.
-
- // Get a location, and retrieve it with rewriting.
- const location = await locate(moduleId);
- const linkageRecord = await retrieve(location, rewriters);
+ const body = await retrieve(moduleId);
+ const linkageRecord = await rewrite(moduleId, body);
moduleCache.set(moduleId, linkageRecord);
}

MODIFY
src/resolve.js
src/resolve.js
@@ -31,8 +31,7 @@ export const makeRootedResolver = root => {
// Find the subpath elements.
const appendEls = [];
- let i = 0;
- while (i < specEls.length) {
+ for (let i = 0; i < specEls.length; i += 1) {
if (specEls[i] === '.') {
// Do nothing.
} else if (specEls[i] === '..') {
@@ -47,7 +46,6 @@ export const makeRootedResolver = root => {
// Navigate down according to the specifier.
appendEls.push(specEls[i]);
}
- i += 1;
}
return rootSlash + appendEls.join('/');

MODIFY
test/test-pipeline.js
test/test-pipeline.js
@@ -2,7 +2,7 @@ import { test } from 'tape-promise/tape';
import makeImportPipeline from '../src';
-test.only('import magic specifier', async t => {
+test('import magic specifier', async t => {
try {
const MAGIC_SPECIFIER = {
toString() {
@@ -18,6 +18,9 @@ test.only('import magic specifier', async t => {
}
throw TypeError(`Not a magical referrer`);
},
+ locate(scopedRef) {
+ return scopedRef;
+ },
rootContainer: {
link(mlr, _mimp) {
return {

pipeline.js: separate rewrite from retrieve