Skip to content

Commit

Permalink
feat(import-bundle): Support per-user source map caches
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jul 21, 2023
1 parent c0b3c5e commit cb481b4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/import-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"engines": {
"node": ">=12"
},
"exports": {
".": "./src/index.js",
"./source-map-node.js": "./source-map-node.js",
"./source-map-node-powers.js": "./source-map-node-powers.js",
"./package.json": "./package.json"
},
"scripts": {
"test": "ava",
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
Expand All @@ -22,7 +28,9 @@
},
"dependencies": {
"@endo/base64": "^0.2.32",
"@endo/compartment-mapper": "^0.8.5"
"@endo/compartment-mapper": "^0.8.5",
"@endo/where": "^0.3.2",
"ses": "^0.18.4"
},
"devDependencies": {
"@endo/bundle-source": "^2.5.2",
Expand Down
1 change: 1 addition & 0 deletions packages/import-bundle/source-map-node-powers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/source-map-node-powers.js';
1 change: 1 addition & 0 deletions packages/import-bundle/source-map-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/source-map-node.js';
16 changes: 14 additions & 2 deletions packages/import-bundle/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ const { Fail } = assert;
// importBundle takes the output of bundle-source, and returns a namespace
// object (with .default, and maybe other properties for named exports)

export async function importBundle(bundle, options = {}) {
export async function importBundle(bundle, options = {}, powers = {}) {
const {
bundleUrl = undefined,
filePrefix,
endowments: optEndowments = {},
// transforms are indeed __shimTransforms__, intended to apply to both
// evaluated programs and modules shimmed to programs.
transforms = [],
inescapableTransforms = [],
inescapableGlobalProperties = {},
expectedSha512 = undefined,
} = options;
const {
computeSha512 = undefined,
computeSourceLocation = undefined,
computeSourceMapLocation = undefined,
} = powers;
const endowments = {
TextEncoder,
TextDecoder,
Expand Down Expand Up @@ -51,7 +58,12 @@ export async function importBundle(bundle, options = {}) {
if (moduleFormat === 'endoZipBase64') {
const { endoZipBase64 } = bundle;
const bytes = decodeBase64(endoZipBase64);
const archive = await parseArchive(bytes);
const archive = await parseArchive(bytes, bundleUrl, {
computeSha512,
expectedSha512,
computeSourceLocation,
computeSourceMapLocation,
});
// Call import by property to bypass SES censoring for dynamic import.
// eslint-disable-next-line dot-notation
const { namespace } = await archive['import']({
Expand Down
35 changes: 35 additions & 0 deletions packages/import-bundle/src/source-map-node-powers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { whereEndoCache } from '@endo/where';

/**
* @typedef {object} Process
* @property {Record<string, string | undefined>} env
* @property {string} platform
*/

/**
* @param {object} powers
* @param {typeof import('node:url')} powers.url
* @param {typeof import('node:os')} powers.os
* @param {Process} powers.process
*/
export const makeEndoSourceMapLocator = powers => {
const { url, os, process } = powers;

const home = os.userInfo().homedir;
const cacheDirectory = whereEndoCache(process.platform, process.env, {
home,
});
const cacheLocation = url.pathToFileURL(cacheDirectory);

/**
* @param {object} details
* @param {string} details.sha512
*/
const whereSourceMap = ({ sha512 }) => {
const sha512Head = sha512.slice(0, 2);
const sha512Tail = sha512.slice(2);
return `${cacheLocation}/source-map/${sha512Head}/${sha512Tail}.map.json`;
};

return whereSourceMap;
};
10 changes: 10 additions & 0 deletions packages/import-bundle/src/source-map-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* global process */
import url from 'node:url';
import os from 'node:os';
import { makeEndoSourceMapLocator } from './source-map-node-powers.js';

export const computeSourceMapLocation = makeEndoSourceMapLocator({
url,
os,
process,
});

0 comments on commit cb481b4

Please sign in to comment.