-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(compartment-mapper): Extract lite from hevy
- Loading branch information
Showing
13 changed files
with
246 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { parserForLanguage } from './src/archive-parsers.js'; | ||
export { defaultParserForLanguage } from './src/archive-parsers.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { parserForLanguage } from './src/import-archive-parsers.js'; | ||
export { defaultParserForLanguage } from './src/import-archive-parsers.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { parserForLanguage } from './src/import-parsers.js'; | ||
export { defaultParserForLanguage } from './src/import-parsers.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// @ts-check | ||
|
||
import { defaultParserForLanguage } from './archive-parsers.js'; | ||
import { | ||
makeAndHashArchive as makeAndHashArchiveLite, | ||
makeArchive as makeArchiveLite, | ||
mapLocation as mapLocationLite, | ||
hashLocation as hashLocationLite, | ||
writeArchive as writeArchiveLite, | ||
} from './archive-lite.js'; | ||
|
||
export { makeArchiveCompartmentMap } from './archive-lite.js'; | ||
|
||
const { assign, create, freeze } = Object; | ||
|
||
/** @import {ArchiveOptions} from './types.js' */ | ||
/** @import {ReadFn} from './types.js' */ | ||
/** @import {ReadPowers} from './types.js' */ | ||
/** @import {HashPowers} from './types.js' */ | ||
/** @import {WriteFn} from './types.js' */ | ||
|
||
// Add the default parserForLanguage option. | ||
const assignParserForLanguage = (options = {}) => { | ||
const { parserForLanguage: parserForLanguageOption, ...rest } = options; | ||
const parserForLanguage = freeze( | ||
assign(create(null), defaultParserForLanguage, parserForLanguageOption), | ||
); | ||
return { ...rest, parserForLanguage }; | ||
}; | ||
|
||
/** | ||
* @param {ReadFn | ReadPowers} powers | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
* @returns {Promise<{bytes: Uint8Array, sha512?: string}>} | ||
*/ | ||
export const makeAndHashArchive = async (powers, moduleLocation, options) => | ||
makeAndHashArchiveLite( | ||
powers, | ||
moduleLocation, | ||
assignParserForLanguage(options), | ||
); | ||
|
||
/** | ||
* @param {ReadFn | ReadPowers} powers | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
* @returns {Promise<Uint8Array>} | ||
*/ | ||
export const makeArchive = async (powers, moduleLocation, options) => | ||
makeArchiveLite(powers, moduleLocation, assignParserForLanguage(options)); | ||
|
||
/** | ||
* @param {ReadFn | ReadPowers} powers | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
* @returns {Promise<Uint8Array>} | ||
*/ | ||
export const mapLocation = async (powers, moduleLocation, options) => | ||
mapLocationLite(powers, moduleLocation, assignParserForLanguage(options)); | ||
|
||
/** | ||
* @param {HashPowers} powers | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
* @returns {Promise<string>} | ||
*/ | ||
export const hashLocation = async (powers, moduleLocation, options) => | ||
hashLocationLite(powers, moduleLocation, assignParserForLanguage(options)); | ||
|
||
/** | ||
* @param {WriteFn} write | ||
* @param {ReadFn | ReadPowers} readPowers | ||
* @param {string} archiveLocation | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
*/ | ||
export const writeArchive = async ( | ||
write, | ||
readPowers, | ||
archiveLocation, | ||
moduleLocation, | ||
options, | ||
) => | ||
writeArchiveLite( | ||
write, | ||
readPowers, | ||
archiveLocation, | ||
moduleLocation, | ||
assignParserForLanguage(options), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @ts-check | ||
|
||
import { defaultParserForLanguage } from './import-archive-parsers.js'; | ||
import { | ||
parseArchive as parseArchiveLite, | ||
loadArchive as loadArchiveLite, | ||
importArchive as importArchiveLite, | ||
} from './import-archive-lite.js'; | ||
|
||
const { assign, create, freeze } = Object; | ||
|
||
/** @import {Application, ComputeSourceLocationHook, ComputeSourceMapLocationHook, ExecuteOptions, ExitModuleImportHook, HashFn, LoadArchiveOptions, ReadPowers} from './types.js' */ | ||
/** @import {ParserForLanguage} from './types.js' */ | ||
|
||
// Have to give it a name to capture the external meaning of Compartment | ||
// Otherwise @param {typeof Compartment} takes the Compartment to mean | ||
// the const variable defined within the function. | ||
// | ||
/** @typedef {typeof Compartment} CompartmentConstructor */ | ||
|
||
// Add the default parserForLanguage option. | ||
const assignParserForLanguage = (options = {}) => { | ||
const { parserForLanguage: parserForLanguageOption, ...rest } = options; | ||
/** @type {ParserForLanguage} */ | ||
const parserForLanguage = freeze( | ||
assign(create(null), defaultParserForLanguage, parserForLanguageOption), | ||
); | ||
return { ...rest, parserForLanguage }; | ||
}; | ||
|
||
/** | ||
* @param {Uint8Array} archiveBytes | ||
* @param {string} [archiveLocation] | ||
* @param {object} [options] | ||
* @param {string} [options.expectedSha512] | ||
* @param {HashFn} [options.computeSha512] | ||
* @param {Record<string, unknown>} [options.modules] | ||
* @param {ExitModuleImportHook} [options.importHook] | ||
* @param {CompartmentConstructor} [options.Compartment] | ||
* @param {ComputeSourceLocationHook} [options.computeSourceLocation] | ||
* @param {ComputeSourceMapLocationHook} [options.computeSourceMapLocation] | ||
* @param {ParserForLanguage} [options.parserForLanguage] | ||
* @returns {Promise<Application>} | ||
*/ | ||
export const parseArchive = async ( | ||
archiveBytes, | ||
archiveLocation = '<unknown>', | ||
options = {}, | ||
) => | ||
parseArchiveLite( | ||
archiveBytes, | ||
archiveLocation, | ||
assignParserForLanguage(options), | ||
); | ||
|
||
/** | ||
* @param {import('@endo/zip').ReadFn | ReadPowers} readPowers | ||
* @param {string} archiveLocation | ||
* @param {LoadArchiveOptions} [options] | ||
* @returns {Promise<Application>} | ||
*/ | ||
export const loadArchive = async (readPowers, archiveLocation, options) => | ||
loadArchiveLite( | ||
readPowers, | ||
archiveLocation, | ||
assignParserForLanguage(options), | ||
); | ||
|
||
/** | ||
* @param {import('@endo/zip').ReadFn | ReadPowers} readPowers | ||
* @param {string} archiveLocation | ||
* @param {ExecuteOptions & LoadArchiveOptions} options | ||
* @returns {Promise<object>} | ||
*/ | ||
export const importArchive = async (readPowers, archiveLocation, options) => | ||
importArchiveLite( | ||
readPowers, | ||
archiveLocation, | ||
assignParserForLanguage(options), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// @ts-check | ||
|
||
import { defaultParserForLanguage } from './import-parsers.js'; | ||
import { | ||
loadLocation as loadLocationLite, | ||
importLocation as importLocationLite, | ||
} from './import-lite.js'; | ||
|
||
const { assign, create, freeze } = Object; | ||
|
||
/** @import {Application} from './types.js' */ | ||
/** @import {ArchiveOptions} from './types.js' */ | ||
/** @import {ExecuteOptions} from './types.js' */ | ||
/** @import {ReadFn} from './types.js' */ | ||
/** @import {ReadPowers} from './types.js' */ | ||
|
||
// Add the default parserForLanguage option. | ||
const assignParserForLanguage = (options = {}) => { | ||
const { parserForLanguage: parserForLanguageOption, ...rest } = options; | ||
const parserForLanguage = freeze( | ||
assign(create(null), defaultParserForLanguage, parserForLanguageOption), | ||
); | ||
return { ...rest, parserForLanguage }; | ||
}; | ||
|
||
/** | ||
* @param {ReadFn | ReadPowers} readPowers | ||
* @param {string} moduleLocation | ||
* @param {ArchiveOptions} [options] | ||
* @returns {Promise<Application>} | ||
*/ | ||
export const loadLocation = async (readPowers, moduleLocation, options) => | ||
loadLocationLite( | ||
readPowers, | ||
moduleLocation, | ||
assignParserForLanguage(options), | ||
); | ||
|
||
/** | ||
* @param {ReadFn | ReadPowers} readPowers | ||
* @param {string} moduleLocation | ||
* @param {ExecuteOptions & ArchiveOptions} [options] | ||
* @returns {Promise<import('./types.js').SomeObject>} the object of the imported modules exported | ||
* names. | ||
*/ | ||
export const importLocation = async (readPowers, moduleLocation, options) => | ||
importLocationLite( | ||
readPowers, | ||
moduleLocation, | ||
assignParserForLanguage(options), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+3 Bytes
(100%)
packages/compartment-mapper/test/snapshots/error-handling.test.js.snap
Binary file not shown.
Binary file modified
BIN
-2 Bytes
(100%)
packages/compartment-mapper/test/snapshots/policy-format.test.js.snap
Binary file not shown.
Binary file modified
BIN
-6 Bytes
(99%)
packages/compartment-mapper/test/snapshots/policy.test.js.snap
Binary file not shown.