bare-module 1.8.6 → 1.9.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/index.js CHANGED
@@ -36,16 +36,27 @@ const Module = module.exports = class Module {
36
36
  let protocol, imports
37
37
 
38
38
  if (referrer) {
39
+ protocol = this._protocolFor(specifier, referrer._protocol)
40
+
41
+ imports = referrer._imports
42
+
39
43
  specifier = this.resolve(specifier, referrer.dirname, {
40
- protocol: protocol = referrer._protocol,
41
- imports: imports = referrer._imports,
44
+ protocol,
45
+ imports,
42
46
  referrer
43
47
  })
44
48
  } else {
45
49
  specifier = this.resolve(specifier)
46
50
  }
47
51
 
48
- return this.load(specifier, { protocol, imports, referrer, dynamic })._handle
52
+ const module = this.load(specifier, {
53
+ protocol: this._protocolFor(specifier, protocol),
54
+ imports,
55
+ referrer,
56
+ dynamic
57
+ })
58
+
59
+ return module._handle
49
60
  }
50
61
 
51
62
  static _onevaluate (specifier) {
@@ -141,6 +152,10 @@ const Module = module.exports = class Module {
141
152
 
142
153
  specifier = protocol.map(specifier, dirname)
143
154
 
155
+ if (protocol.resolve) {
156
+ yield * protocol.resolve(specifier, dirname, imports)
157
+ }
158
+
144
159
  if (this.isBuiltin(specifier)) {
145
160
  yield specifier
146
161
  }
@@ -163,8 +178,8 @@ const Module = module.exports = class Module {
163
178
  if (protocol.exists(f + '.cjs')) yield f + '.cjs'
164
179
  if (protocol.exists(f + '.mjs')) yield f + '.mjs'
165
180
  if (protocol.exists(f + '.json')) yield f + '.json'
166
- if (protocol.exists(f + '.node')) yield f + '.node'
167
181
  if (protocol.exists(f + '.bare')) yield f + '.bare'
182
+ if (protocol.exists(f + '.node')) yield f + '.node'
168
183
  }
169
184
 
170
185
  static * _resolveIndex (dirname, protocol) {
@@ -214,7 +229,7 @@ const Module = module.exports = class Module {
214
229
  }
215
230
  }
216
231
 
217
- static _protocolFor (specifier, fallback) {
232
+ static _protocolFor (specifier, fallback = null) {
218
233
  const i = specifier.indexOf(':')
219
234
 
220
235
  if (i < 2) return fallback // Allow drive letters in Windows paths
@@ -222,7 +237,11 @@ const Module = module.exports = class Module {
222
237
  const protocol = specifier.slice(0, i + 1)
223
238
 
224
239
  if (!this._protocols[protocol]) {
225
- throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${protocol}' in specifier '${specifier}'`)
240
+ if (fallback === null) {
241
+ throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${protocol}' in specifier '${specifier}'`)
242
+ }
243
+
244
+ return fallback
226
245
  }
227
246
 
228
247
  return this._protocols[protocol]
@@ -276,11 +295,15 @@ const Module = module.exports = class Module {
276
295
  }
277
296
 
278
297
  Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
279
- const loader = this._extensions[
280
- module._info && module._info.type === 'module'
281
- ? '.mjs'
282
- : '.cjs'
283
- ]
298
+ const isESM = (
299
+ // The package is explicitly declared as an ES module.
300
+ (module._info && module._info.type === 'module') ||
301
+
302
+ // The referrer is itself an ES module.
303
+ (referrer && referrer._type === 'esm')
304
+ )
305
+
306
+ const loader = this._extensions[isESM ? '.mjs' : '.cjs']
284
307
 
285
308
  return loader.call(this, module, source, referrer, protocol, imports)
286
309
  }
@@ -293,11 +316,21 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
293
316
  referrer = module
294
317
 
295
318
  const resolve = (specifier) => {
296
- return this.resolve(specifier, module.dirname, { protocol, imports, referrer })
319
+ return this.resolve(specifier, module.dirname, {
320
+ protocol: this._protocolFor(specifier, protocol),
321
+ imports,
322
+ referrer
323
+ })
297
324
  }
298
325
 
299
326
  const require = (specifier) => {
300
- return this.load(resolve(specifier), { protocol, imports, referrer }).exports
327
+ const module = this.load(resolve(specifier), {
328
+ protocol: this._protocolFor(specifier, protocol),
329
+ imports,
330
+ referrer
331
+ })
332
+
333
+ return module.exports
301
334
  }
302
335
 
303
336
  module._type = 'cjs'
@@ -404,4 +437,16 @@ Module._protocols['node:'] = new Protocol({
404
437
  }
405
438
  })
406
439
 
440
+ Module._protocols['data:'] = new Protocol({
441
+ * resolve (specifier) {
442
+ yield specifier
443
+ },
444
+
445
+ read (specifier) {
446
+ const [, , , base64, data] = specifier.match(/data:(?:([^/]+\/[^;,]+)(;[^=]+=[^;,]+)*)?(;base64)?,(.*)/)
447
+
448
+ return Buffer.from(decodeURIComponent(data), base64 ? 'base64' : 'ascii')
449
+ }
450
+ })
451
+
407
452
  process.once('exit', () => binding.destroy(Module._context))
package/lib/protocol.js CHANGED
@@ -2,11 +2,14 @@ module.exports = class Protocol {
2
2
  constructor (opts = {}) {
3
3
  const {
4
4
  map = null,
5
+ resolve = null,
5
6
  exists = null,
6
7
  read = null
7
8
  } = opts
8
9
 
9
10
  if (map) this.map = map.bind(this)
11
+ if (resolve) this.resolve = resolve.bind(this)
12
+ else this.resolve = null
10
13
  if (exists) this.exists = exists.bind(this)
11
14
  if (read) this.read = read.bind(this)
12
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.8.6",
3
+ "version": "1.9.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [