bare-module 6.4.0 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +237 -270
- package/binding.c +623 -333
- package/index.d.ts +156 -63
- package/index.js +87 -1002
- package/lib/errors.js +27 -10
- package/lib/loader.d.ts +56 -0
- package/lib/loader.js +1192 -0
- package/lib/module/addon.js +7 -0
- package/lib/module/binary.js +7 -0
- package/lib/module/builtin.js +19 -0
- package/lib/module/bundle.js +47 -0
- package/lib/module/json.js +19 -0
- package/lib/module/script.js +23 -0
- package/lib/module/source-text.js +13 -0
- package/lib/module/synthetic.js +20 -0
- package/lib/module/text.js +7 -0
- package/lib/module/value.js +27 -0
- package/lib/module.js +392 -0
- package/lib/protocol.d.ts +56 -8
- package/lib/protocol.js +93 -26
- package/lib/source.js +18 -0
- package/lib/url.js +52 -0
- package/package.json +18 -8
- package/prebuilds/android-arm/bare-module.bare +0 -0
- package/prebuilds/android-arm64/bare-module.bare +0 -0
- package/prebuilds/android-ia32/bare-module.bare +0 -0
- package/prebuilds/android-x64/bare-module.bare +0 -0
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-arm64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
- package/lib/constants.d.ts +0 -19
- package/lib/constants.js +0 -17
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const SyntheticModule = require('./synthetic')
|
|
2
|
+
|
|
3
|
+
module.exports = class BuiltinModule extends SyntheticModule {
|
|
4
|
+
constructor(loader, source, exports) {
|
|
5
|
+
super(loader, source)
|
|
6
|
+
|
|
7
|
+
this.exports = exports
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
_exportNames(seen = new Set()) {
|
|
11
|
+
const names = super._exportNames(seen)
|
|
12
|
+
|
|
13
|
+
if (this.exports !== null && typeof this.exports === 'object') {
|
|
14
|
+
for (const key of Object.keys(this.exports)) names.add(key)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return names
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const Bundle = require('bare-bundle')
|
|
2
|
+
const SyntheticModule = require('./synthetic')
|
|
3
|
+
|
|
4
|
+
module.exports = class BundleModule extends SyntheticModule {
|
|
5
|
+
constructor(loader, source) {
|
|
6
|
+
super(loader, source)
|
|
7
|
+
|
|
8
|
+
this._bundle = null
|
|
9
|
+
this._mainURL = null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_mount() {
|
|
13
|
+
if (this._bundle !== null) return
|
|
14
|
+
|
|
15
|
+
const bundle = Bundle.from(this._source.bytes).mount(this.url.href + '/')
|
|
16
|
+
|
|
17
|
+
this._bundle = bundle
|
|
18
|
+
|
|
19
|
+
if (bundle.main) {
|
|
20
|
+
this._mainURL = new URL(bundle.main)
|
|
21
|
+
|
|
22
|
+
this._loader._linkBundle(bundle)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
_main() {
|
|
27
|
+
return this._mainURL === null ? null : this._loader.get(this._mainURL)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_exportNames(seen = new Set()) {
|
|
31
|
+
this._mount()
|
|
32
|
+
|
|
33
|
+
const main = this._main()
|
|
34
|
+
|
|
35
|
+
return main === null ? new Set(['default']) : main._exportNames(seen)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_initialize() {
|
|
39
|
+
this._mount()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_execute() {
|
|
43
|
+
const main = this._main()
|
|
44
|
+
|
|
45
|
+
this.exports = main === null ? null : main._evaluate()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const ValueModule = require('./value')
|
|
2
|
+
|
|
3
|
+
module.exports = class JSONModule extends ValueModule {
|
|
4
|
+
_parse() {
|
|
5
|
+
return JSON.parse(this._source.bytes.toString())
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
_exportNames(seen = new Set()) {
|
|
9
|
+
const names = super._exportNames(seen)
|
|
10
|
+
|
|
11
|
+
const value = this._parseOnce()
|
|
12
|
+
|
|
13
|
+
if (value !== null && typeof value === 'object') {
|
|
14
|
+
for (const key of Object.keys(value)) names.add(key)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return names
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const SyntheticModule = require('./synthetic')
|
|
2
|
+
|
|
3
|
+
module.exports = class ScriptModule extends SyntheticModule {
|
|
4
|
+
constructor(loader, source) {
|
|
5
|
+
super(loader, source)
|
|
6
|
+
|
|
7
|
+
this._fn = null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
_initialize() {
|
|
11
|
+
this._fn = this._createFunction(this._text())
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
_execute() {
|
|
15
|
+
this.exports = {}
|
|
16
|
+
|
|
17
|
+
const require = this._loader._createRequire(this)
|
|
18
|
+
|
|
19
|
+
const fn = this._fn // Bind to a variable to ensure a proper stack trace.
|
|
20
|
+
|
|
21
|
+
fn(require, this, this.exports, this.filename, this.dirname)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const Module = require('../module')
|
|
2
|
+
|
|
3
|
+
module.exports = class SyntheticModule extends Module {
|
|
4
|
+
constructor(loader, source) {
|
|
5
|
+
super(loader, source)
|
|
6
|
+
|
|
7
|
+
this._synthesized = false
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
_synthesize() {
|
|
11
|
+
if (this._synthesized) return
|
|
12
|
+
|
|
13
|
+
const names = Array.from(this._exportNames())
|
|
14
|
+
|
|
15
|
+
this._createSyntheticModule(names)
|
|
16
|
+
|
|
17
|
+
this._names = names
|
|
18
|
+
this._synthesized = true
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const SyntheticModule = require('./synthetic')
|
|
2
|
+
|
|
3
|
+
module.exports = class ValueModule extends SyntheticModule {
|
|
4
|
+
constructor(loader, source) {
|
|
5
|
+
super(loader, source)
|
|
6
|
+
|
|
7
|
+
this._value = null
|
|
8
|
+
this._parsed = false
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
_parseOnce() {
|
|
12
|
+
if (this._parsed === false) {
|
|
13
|
+
this._value = this._parse()
|
|
14
|
+
this._parsed = true
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return this._value
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
_parse() {
|
|
21
|
+
return this._source.bytes
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_execute() {
|
|
25
|
+
this.exports = this._parseOnce()
|
|
26
|
+
}
|
|
27
|
+
}
|
package/lib/module.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
const lex = require('bare-module-lexer')
|
|
2
|
+
const strip = require('bare-type-stripper')
|
|
3
|
+
const binding = require('../binding')
|
|
4
|
+
const { urlToPath, urlToDirname } = require('./url')
|
|
5
|
+
const errors = require('./errors')
|
|
6
|
+
|
|
7
|
+
const { REEXPORT, ADDON, ASSET } = lex.constants
|
|
8
|
+
|
|
9
|
+
const DEFERRED_PROTOCOL = 'deferred:'
|
|
10
|
+
|
|
11
|
+
const UNLINKED = 0
|
|
12
|
+
const LINKED = 1
|
|
13
|
+
const EVALUATING = 2
|
|
14
|
+
const EVALUATED = 3
|
|
15
|
+
|
|
16
|
+
// The context never leaves this module. A module graph is no wall, so anything
|
|
17
|
+
// that knows this module's URL reaches the `Module` it exports, and the context
|
|
18
|
+
// must not be among what it reaches; holding it is what lets the binding create
|
|
19
|
+
// and run modules, and holding its records is what would let one graph name a
|
|
20
|
+
// module that another graph, given another protocol, loaded.
|
|
21
|
+
//
|
|
22
|
+
// There may be more than one of these in an environment, as there is whenever
|
|
23
|
+
// this module is bundled twice. Each answers only for the units it created, so
|
|
24
|
+
// neither can see the other's imports.
|
|
25
|
+
class ModuleContext {
|
|
26
|
+
constructor() {
|
|
27
|
+
this.records = new WeakMap()
|
|
28
|
+
|
|
29
|
+
binding.createContext(this, onimport, ondynamicimport, onevaluate, onmeta)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
adopt(module, id) {
|
|
33
|
+
module._id = id
|
|
34
|
+
|
|
35
|
+
this.records.set(id, module)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const context = new ModuleContext()
|
|
40
|
+
|
|
41
|
+
module.exports = exports = class Module {
|
|
42
|
+
constructor(loader, source) {
|
|
43
|
+
this._loader = loader
|
|
44
|
+
this._source = source
|
|
45
|
+
this._status = UNLINKED
|
|
46
|
+
|
|
47
|
+
this._id = null
|
|
48
|
+
this._names = null
|
|
49
|
+
this._promise = null
|
|
50
|
+
this._error = null
|
|
51
|
+
|
|
52
|
+
this.exports = null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get url() {
|
|
56
|
+
return this._source.url
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get protocol() {
|
|
60
|
+
return this._loader._protocolFor(this.url)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// For Node.js compatibility
|
|
64
|
+
get filename() {
|
|
65
|
+
return urlToPath(this.url)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// For Node.js compatibility
|
|
69
|
+
get dirname() {
|
|
70
|
+
return urlToDirname(this.url)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// For Node.js compatibility
|
|
74
|
+
get id() {
|
|
75
|
+
return this.filename
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// For Node.js compatibility
|
|
79
|
+
get path() {
|
|
80
|
+
return this.dirname
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
[Symbol.for('bare.inspect')]() {
|
|
84
|
+
return {
|
|
85
|
+
__proto__: { constructor: Module },
|
|
86
|
+
|
|
87
|
+
url: this.url,
|
|
88
|
+
exports: this.exports
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_resolve(specifier, condition) {
|
|
93
|
+
const entry = this._source.imports[specifier]
|
|
94
|
+
|
|
95
|
+
if (entry === undefined) return null
|
|
96
|
+
|
|
97
|
+
const href = pickCondition(entry, condition)
|
|
98
|
+
|
|
99
|
+
if (href === null) return null
|
|
100
|
+
|
|
101
|
+
const url = new URL(href)
|
|
102
|
+
|
|
103
|
+
// A specifier recorded as deferred was left for whoever names it, which is
|
|
104
|
+
// whoever is asking. Answering nothing sends them to the resolver.
|
|
105
|
+
return url.protocol === DEFERRED_PROTOCOL ? null : url
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_exportNames(seen = new Set()) {
|
|
109
|
+
const names = new Set(['default'])
|
|
110
|
+
|
|
111
|
+
if (seen.has(this)) return names
|
|
112
|
+
|
|
113
|
+
seen.add(this)
|
|
114
|
+
|
|
115
|
+
for (const entry of this._source.lexer.exports) names.add(entry.name)
|
|
116
|
+
|
|
117
|
+
for (const entry of this._source.lexer.imports) {
|
|
118
|
+
if ((entry.type & REEXPORT) === 0) continue
|
|
119
|
+
if ((entry.type & ADDON) !== 0 || (entry.type & ASSET) !== 0) continue
|
|
120
|
+
|
|
121
|
+
const target = this._reexported(entry.specifier)
|
|
122
|
+
|
|
123
|
+
if (target) {
|
|
124
|
+
for (const name of target._exportNames(seen)) names.add(name)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return names
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
_reexported(specifier) {
|
|
132
|
+
let url = this._resolve(specifier, 'import')
|
|
133
|
+
|
|
134
|
+
if (url === null) {
|
|
135
|
+
try {
|
|
136
|
+
url = this._loader._resolveSync(specifier, this.url, 'import')
|
|
137
|
+
} catch {
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return this._loader._lookup(url)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
_instantiate() {
|
|
146
|
+
if (this._status >= LINKED) return
|
|
147
|
+
|
|
148
|
+
this._initialize()
|
|
149
|
+
|
|
150
|
+
this._status = LINKED
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
_evaluate() {
|
|
154
|
+
if (this._error !== null) throw this._error
|
|
155
|
+
|
|
156
|
+
if (this._status >= EVALUATING) return this.exports
|
|
157
|
+
|
|
158
|
+
this._instantiate()
|
|
159
|
+
|
|
160
|
+
this._status = EVALUATING
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
this._execute()
|
|
164
|
+
} catch (err) {
|
|
165
|
+
this._error = err
|
|
166
|
+
|
|
167
|
+
throw err
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this._status = EVALUATED
|
|
171
|
+
|
|
172
|
+
return this.exports
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
_initialize() {}
|
|
176
|
+
|
|
177
|
+
_synthesize() {}
|
|
178
|
+
|
|
179
|
+
_execute() {}
|
|
180
|
+
|
|
181
|
+
_run() {
|
|
182
|
+
if (this._promise === null) {
|
|
183
|
+
this._instantiate()
|
|
184
|
+
|
|
185
|
+
this._promise = this._runModule()
|
|
186
|
+
|
|
187
|
+
if (this._loader._pending !== null) this._loader._pending.push(this._promise)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return this._promise
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
_text() {
|
|
194
|
+
return stripTypeScript(this._source.bytes, this.url).toString()
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
_createModule(source) {
|
|
198
|
+
context.adopt(this, binding.createModule(context, this, this.url.href, source, 0))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
_createSyntheticModule(names) {
|
|
202
|
+
context.adopt(this, binding.createSyntheticModule(context, this, this.url.href, names))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_createFunction(source) {
|
|
206
|
+
const fn = binding.createFunction(
|
|
207
|
+
context,
|
|
208
|
+
this.url.href,
|
|
209
|
+
['require', 'module', 'exports', '__filename', '__dirname'],
|
|
210
|
+
source,
|
|
211
|
+
0
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
context.adopt(this, binding.getFunctionID(fn))
|
|
215
|
+
|
|
216
|
+
return fn
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
_runModule() {
|
|
220
|
+
return binding.runModule(context, this, onrun)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
_moduleNamespace() {
|
|
224
|
+
return binding.getModuleNamespace(context, this)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function onimport(specifier, attributes, id) {
|
|
229
|
+
const referrer = context.records.get(id) || null
|
|
230
|
+
|
|
231
|
+
if (referrer === null) {
|
|
232
|
+
throw errors.MODULE_NOT_FOUND(`Cannot find referrer for module '${specifier}'`, specifier)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const loader = referrer._loader
|
|
236
|
+
|
|
237
|
+
const url = referrer._resolve(specifier, 'import')
|
|
238
|
+
|
|
239
|
+
let child = loader._lookup(url)
|
|
240
|
+
|
|
241
|
+
// A deferred specifier is resolved by whoever names it, and here that is the
|
|
242
|
+
// engine, part way through instantiating the graph.
|
|
243
|
+
if (child === null) child = loader._importSync(specifier, referrer.url)
|
|
244
|
+
|
|
245
|
+
if (!child) {
|
|
246
|
+
throw errors.MODULE_NOT_FOUND(
|
|
247
|
+
`Cannot find module '${specifier}' imported from '${referrer.url.href}'`,
|
|
248
|
+
specifier,
|
|
249
|
+
referrer.url
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
loader._assertType(child, attributes)
|
|
254
|
+
|
|
255
|
+
child._instantiate()
|
|
256
|
+
child._synthesize()
|
|
257
|
+
|
|
258
|
+
return child
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async function ondynamicimport(specifier, attributes, referrerName, id) {
|
|
262
|
+
const referrer = context.records.get(id) || null
|
|
263
|
+
|
|
264
|
+
if (referrer === null) {
|
|
265
|
+
throw errors.MODULE_NOT_FOUND(
|
|
266
|
+
`Cannot find referrer for dynamically imported module '${specifier}'`,
|
|
267
|
+
specifier
|
|
268
|
+
)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const loader = referrer._loader
|
|
272
|
+
|
|
273
|
+
let url = referrer._resolve(specifier, 'import')
|
|
274
|
+
|
|
275
|
+
let record = loader._lookup(url)
|
|
276
|
+
|
|
277
|
+
if (record === null) {
|
|
278
|
+
url = await loader._resolve(specifier, referrer.url, 'import')
|
|
279
|
+
|
|
280
|
+
record = await loader.link(url, null, {
|
|
281
|
+
attributes,
|
|
282
|
+
importType: lex.constants.IMPORT | lex.constants.DYNAMIC
|
|
283
|
+
})
|
|
284
|
+
} else {
|
|
285
|
+
record._instantiate()
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
loader._assertType(record, attributes)
|
|
289
|
+
|
|
290
|
+
record._synthesize()
|
|
291
|
+
|
|
292
|
+
await record._run()
|
|
293
|
+
|
|
294
|
+
return record._moduleNamespace()
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function onevaluate(id) {
|
|
298
|
+
const record = context.records.get(id) || null
|
|
299
|
+
|
|
300
|
+
if (record === null) {
|
|
301
|
+
throw errors.MODULE_NOT_FOUND('Cannot find module being evaluated', null)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
record._evaluate()
|
|
305
|
+
|
|
306
|
+
for (const name of record._names) {
|
|
307
|
+
let value
|
|
308
|
+
|
|
309
|
+
if (
|
|
310
|
+
name === 'default' &&
|
|
311
|
+
(typeof record.exports !== 'object' ||
|
|
312
|
+
record.exports === null ||
|
|
313
|
+
name in record.exports === false)
|
|
314
|
+
) {
|
|
315
|
+
value = record.exports
|
|
316
|
+
} else {
|
|
317
|
+
value = record.exports[name]
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
binding.setModuleExport(context, record, name, value)
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function onmeta(id, meta) {
|
|
325
|
+
const record = context.records.get(id) || null
|
|
326
|
+
|
|
327
|
+
if (record === null) {
|
|
328
|
+
throw errors.MODULE_NOT_FOUND('Cannot find module being initialized', null)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
meta.url = record.url.href
|
|
332
|
+
meta.main = record._loader.main === record
|
|
333
|
+
meta.cache = record._loader.cache
|
|
334
|
+
|
|
335
|
+
// For Node.js compatibility.
|
|
336
|
+
meta.dirname = record.dirname
|
|
337
|
+
meta.filename = record.filename
|
|
338
|
+
|
|
339
|
+
meta.resolve = function (specifier) {
|
|
340
|
+
const url = record._resolve(specifier, 'import')
|
|
341
|
+
|
|
342
|
+
return url === null
|
|
343
|
+
? record._loader._resolveSync(specifier, record.url, 'import').href
|
|
344
|
+
: url.href
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
meta.asset = function (specifier) {
|
|
348
|
+
return record._loader._resolveArtifact(record, specifier, 'asset').href
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
meta.addon = function (specifier = '.') {
|
|
352
|
+
return record._loader._addon(record, specifier)
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
meta.addon.resolve = function (specifier = '.') {
|
|
356
|
+
return record._loader._resolveArtifact(record, specifier, 'addon').href
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
meta.addon.host = Bare.Addon.host
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function onrun(reason, promise, err = reason) {
|
|
363
|
+
if (err) {
|
|
364
|
+
promise.catch(() => {}) // Don't leak the rejection before it is rethrown.
|
|
365
|
+
|
|
366
|
+
throw err
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const entryConditions = ['require', 'import', 'asset', 'addon']
|
|
371
|
+
|
|
372
|
+
function pickCondition(entry, condition) {
|
|
373
|
+
if (typeof entry === 'string') return entry
|
|
374
|
+
|
|
375
|
+
if (entry === null || typeof entry !== 'object') return null
|
|
376
|
+
|
|
377
|
+
if (Object.hasOwn(entry, condition)) return pickCondition(entry[condition], condition)
|
|
378
|
+
|
|
379
|
+
for (const key of Object.keys(entry)) {
|
|
380
|
+
if (key !== 'default' && entryConditions.includes(key) === false) return null
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (Object.hasOwn(entry, 'default')) return pickCondition(entry.default, condition)
|
|
384
|
+
|
|
385
|
+
return null
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const typeScriptExtension = /\.(c|m)?ts$/
|
|
389
|
+
|
|
390
|
+
function stripTypeScript(source, url) {
|
|
391
|
+
return typeScriptExtension.test(url.pathname) ? strip(source) : source
|
|
392
|
+
}
|
package/lib/protocol.d.ts
CHANGED
|
@@ -1,27 +1,75 @@
|
|
|
1
1
|
import URL from 'bare-url'
|
|
2
2
|
import Buffer from 'bare-buffer'
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
declare const kind: unique symbol
|
|
4
5
|
|
|
5
6
|
interface ModuleProtocol {
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Resolve a module `URL` to the `URL` that should be linked. The default implementation returns
|
|
9
|
+
* `url` unchanged.
|
|
10
|
+
* @param url - The `URL` to resolve.
|
|
11
|
+
* @returns The resolved `URL`. A promise must not be returned during synchronous linking.
|
|
12
|
+
*/
|
|
13
|
+
resolve(url: URL): URL | Promise<URL>
|
|
14
|
+
|
|
15
|
+
resolveSync(url: URL): URL
|
|
7
16
|
|
|
8
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Return whether the URL exists.
|
|
19
|
+
* @param url - The `URL` to check for existence.
|
|
20
|
+
* @returns Whether `url` exists. The default implementation returns `false`.
|
|
21
|
+
*/
|
|
22
|
+
exists(url: URL): boolean | Promise<boolean>
|
|
9
23
|
|
|
10
|
-
|
|
24
|
+
existsSync(url: URL): boolean
|
|
11
25
|
|
|
12
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Return the source code of a URL, represented as a string or buffer.
|
|
28
|
+
* @param url - The `URL` to read.
|
|
29
|
+
* @returns The source of `url` as a `Buffer` or `string`, or `null` if it does not exist.
|
|
30
|
+
*/
|
|
31
|
+
read(url: URL): Buffer | string | null | Promise<Buffer | string | null>
|
|
13
32
|
|
|
14
|
-
|
|
33
|
+
readSync(url: URL): Buffer | string | null
|
|
15
34
|
|
|
16
|
-
|
|
35
|
+
list(url: URL): Iterable<URL> | AsyncIterable<URL>
|
|
17
36
|
|
|
18
|
-
|
|
37
|
+
listSync(url: URL): Iterable<URL>
|
|
19
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Create a new protocol that uses this protocol as its context, overriding the given `methods`.
|
|
41
|
+
* @param methods - Protocol method overrides for the new protocol.
|
|
42
|
+
* @returns A new `ModuleProtocol` that uses this protocol as its context, with `methods`
|
|
43
|
+
* overriding.
|
|
44
|
+
*/
|
|
20
45
|
extend(methods: Partial<ModuleProtocol>): ModuleProtocol
|
|
46
|
+
|
|
47
|
+
/** The version of the protocol interface. The same value as on the class. */
|
|
48
|
+
readonly [kind]: number
|
|
21
49
|
}
|
|
22
50
|
|
|
23
51
|
declare class ModuleProtocol {
|
|
52
|
+
/**
|
|
53
|
+
* @param methods - Protocol method overrides; any of `resolve`, `resolveSync`, `exists`,
|
|
54
|
+
* `existsSync`, `read`, `readSync`, `list`, or `listSync`.
|
|
55
|
+
* @param context - An existing protocol to fall back to for any method not provided in `methods`.
|
|
56
|
+
*/
|
|
24
57
|
constructor(methods?: Partial<ModuleProtocol>, context?: ModuleProtocol)
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The version of the protocol interface, read through the well known symbol
|
|
61
|
+
* `Symbol.for('bare.module.protocol.kind')`. An instance reports the same version as its class. A
|
|
62
|
+
* protocol travels between module systems, which may be different copies of this one, so a
|
|
63
|
+
* module system reads this to tell a protocol it can use from one it cannot.
|
|
64
|
+
*/
|
|
65
|
+
static readonly [kind]: number
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param value - The value to test.
|
|
69
|
+
* @returns Whether `value` is a protocol of the same version as this one, whichever copy of
|
|
70
|
+
* `bare-module` created it.
|
|
71
|
+
*/
|
|
72
|
+
static isProtocol(value: unknown): boolean
|
|
25
73
|
}
|
|
26
74
|
|
|
27
75
|
export = ModuleProtocol
|