1
- // XXX Omit from typecheck for TypeScript packages depending upon
2
- // import-bundle.
3
- // TODO https://github.com/endojs/endo/issues/1254
4
- // @ts -nocheck
5
1
/* global globalThis */
6
2
/// <reference types="ses"/>
7
3
@@ -10,9 +6,27 @@ import { decodeBase64 } from '@endo/base64';
10
6
import { Fail } from '@endo/errors' ;
11
7
import { wrapInescapableCompartment } from './compartment-wrapper.js' ;
12
8
13
- // importBundle takes the output of bundle-source, and returns a namespace
14
- // object (with .default, and maybe other properties for named exports)
9
+ /**
10
+ * The bundle importer is designed for (serializable) source modules but can
11
+ * accomodate native module exports (unserializable) too in a 'test' format.
12
+ * @typedef {import('@endo/bundle-source').ModuleFormat | 'test' } ImportableBundleFormat
13
+ */
15
14
15
+ /**
16
+ * The bundle importer is designed for (serializable) source modules but can
17
+ * accomodate native module exports (unserializable) too in a 'test' format.
18
+ * @typedef {import('@endo/bundle-source').BundleSourceResult<any> | {moduleFormat: 'test'} } ImportableBundle
19
+ */
20
+
21
+ /**
22
+ * importBundle takes the output of `bundleSource` or `bundleTestExports`, and returns a namespace
23
+ * object (with .default, and maybe other properties for named exports)
24
+ *
25
+ * @param {ImportableBundle } bundle
26
+ * @param {object } [options]
27
+ * @param {object } [powers]
28
+ * @returns {Promise<Record<string, any>> }
29
+ */
16
30
export async function importBundle ( bundle , options = { } , powers = { } ) {
17
31
await null ;
18
32
const {
@@ -56,8 +70,10 @@ export async function importBundle(bundle, options = {}, powers = {}) {
56
70
57
71
let compartment ;
58
72
73
+ assert ( 'moduleFormat' in bundle ) ;
59
74
const { moduleFormat } = bundle ;
60
75
if ( moduleFormat === 'endoZipBase64' ) {
76
+ assert ( 'endoZipBase64' in bundle ) ;
61
77
const { endoZipBase64 } = bundle ;
62
78
const bytes = decodeBase64 ( endoZipBase64 ) ;
63
79
const archive = await parseArchive ( bytes , bundleUrl , {
@@ -71,7 +87,6 @@ export async function importBundle(bundle, options = {}, powers = {}) {
71
87
const { namespace } = await archive [ 'import' ] ( {
72
88
globals : endowments ,
73
89
__shimTransforms__ : transforms ,
74
- // @ts -expect-error TS2740 missing properties from type
75
90
Compartment : CompartmentToUse ,
76
91
} ) ;
77
92
// namespace.default has the default export
@@ -131,8 +146,9 @@ export async function importBundle(bundle, options = {}, powers = {}) {
131
146
) ;
132
147
}
133
148
149
+ assert ( 'source' in bundle ) ;
134
150
let { source } = bundle ;
135
- const { sourceMap } = bundle ;
151
+ const sourceMap = ' sourceMap' in bundle ? bundle . sourceMap : '' ;
136
152
if ( moduleFormat === 'getExport' ) {
137
153
// The 'getExport' format is a string which defines a wrapper function
138
154
// named `getExport()`. This function provides a `module` to the
@@ -142,7 +158,7 @@ export async function importBundle(bundle, options = {}, powers = {}) {
142
158
// (making it an expression). We also want to append the `sourceMap`
143
159
// comment so `evaluate` can attach useful debug information. Finally, to
144
160
// extract the namespace object, we need to invoke this function.
145
- source = `(${ source } )\n${ sourceMap || '' } ` ;
161
+ source = `(${ source } )\n${ sourceMap } ` ;
146
162
} else if ( moduleFormat === 'nestedEvaluate' ) {
147
163
// The 'nestedEvaluate' format is similar, except the wrapper function
148
164
// (now named `getExportWithNestedEvaluate`) wraps more than a single
@@ -155,7 +171,7 @@ export async function importBundle(bundle, options = {}, powers = {}) {
155
171
// module. The sourceMap name for other modules will be derived from
156
172
// `filePrefix` and the relative import path of each module.
157
173
endowments . nestedEvaluate = src => compartment . evaluate ( src ) ;
158
- source = `(${ source } )\n${ sourceMap || '' } ` ;
174
+ source = `(${ source } )\n${ sourceMap } ` ;
159
175
} else if ( moduleFormat === 'endoScript' ) {
160
176
// The 'endoScript' format is just a script.
161
177
} else {
0 commit comments