-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathimport-archive.js
More file actions
95 lines (88 loc) · 2.56 KB
/
import-archive.js
File metadata and controls
95 lines (88 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Provides functions for evaluating modules in an archive (a zip file
* with a `compartment-map.json` and a file for a module and each of its
* transitive dependencies.)
*
* These functions accept the URL of an entry module and find its transitive
* dependencies through the Node.js `node_modules` conventions.
*
* These functions use the default parsers in `import-archive-parsers.js`,
* which support only pre-compiled ESM and CommonJS.
*
* See `import-archive-lite.js` for functions that are not coupled to these
* parsers or the `node_modules` conventions without necessarily entraining a
* dependency on Babel.
*
* @module
*/
/**
* @import {
* Application,
* ExecuteOptions,
* LoadArchiveOptions,
* ReadPowers,
* ParserForLanguage,
* ParseArchiveOptions,
* ReadFn,
* } from './types.js'
*/
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;
/**
* Add the default parserForLanguage option.
* @param {ParseArchiveOptions} [options]
* @returns {ParseArchiveOptions}
*/
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 {ParseArchiveOptions} [options]
* @returns {Promise<Application>}
*/
export const parseArchive = async (
archiveBytes,
archiveLocation = '<unknown>',
options = {},
) =>
parseArchiveLite(
archiveBytes,
archiveLocation,
assignParserForLanguage(options),
);
/**
* @param {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 {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),
);