bare-module 7.2.2 → 7.4.0

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.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import Buffer from 'bare-buffer'
2
+ import Bundle from 'bare-bundle'
2
3
  import URL from 'bare-url'
3
4
  import { ResolutionsMap } from 'bare-module-resolve'
4
5
  import { constants } from 'bare-module-traverse'
@@ -151,7 +152,7 @@ declare namespace Module {
151
152
  export function load(url: URL, opts: LoadOptions): Promise<Module>
152
153
  export function load(
153
154
  url: URL,
154
- source?: Buffer | string | null,
155
+ source?: Buffer | Bundle | string | null,
155
156
  opts?: LoadOptions
156
157
  ): Promise<Module>
157
158
 
@@ -169,7 +170,11 @@ declare namespace Module {
169
170
  * the requested `type`.
170
171
  */
171
172
  export function loadSync(url: URL, opts: LoadOptions): Module
172
- export function loadSync(url: URL, source?: Buffer | string | null, opts?: LoadOptions): Module
173
+ export function loadSync(
174
+ url: URL,
175
+ source?: Buffer | Bundle | string | null,
176
+ opts?: LoadOptions
177
+ ): Module
173
178
 
174
179
  /**
175
180
  * Resolve the module `specifier` relative to the `parentURL`. `specifier` is a string and
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const { isURL, pathToFileURL } = require('bare-url')
2
+ const Bundle = require('bare-bundle')
2
3
  const { constants } = require('bare-module-traverse')
3
4
  const Module = require('./lib/module')
4
5
  const ModuleProtocol = require('./lib/protocol')
@@ -15,7 +16,7 @@ exports.constants = constants
15
16
  exports.load = async function load(url, source = null, opts = {}) {
16
17
  if (typeof url === 'string') url = new URL(url)
17
18
 
18
- if (source !== null && typeof source !== 'string' && !ArrayBuffer.isView(source)) {
19
+ if (source !== null && !isSource(source)) {
19
20
  opts = source
20
21
  source = null
21
22
  }
@@ -32,7 +33,7 @@ exports.load = async function load(url, source = null, opts = {}) {
32
33
  exports.loadSync = function loadSync(url, source = null, opts = {}) {
33
34
  if (typeof url === 'string') url = new URL(url)
34
35
 
35
- if (source !== null && typeof source !== 'string' && !ArrayBuffer.isView(source)) {
36
+ if (source !== null && !isSource(source)) {
36
37
  opts = source
37
38
  source = null
38
39
  }
@@ -102,6 +103,10 @@ function assertURL(url, name) {
102
103
  throw new TypeError(`${name} must be a URL. Received type ${typeof url} (${url})`)
103
104
  }
104
105
 
106
+ function isSource(value) {
107
+ return typeof value === 'string' || ArrayBuffer.isView(value) || Bundle.isBundle(value)
108
+ }
109
+
105
110
  function optionsFor(opts) {
106
111
  if (opts === undefined || opts === null) return {}
107
112
 
package/lib/loader.d.ts CHANGED
@@ -65,6 +65,12 @@ declare namespace ModuleLoader {
65
65
  * type: 'json' }`.
66
66
  */
67
67
  attributes?: Attributes
68
+
69
+ /**
70
+ * The URL to mount the entry at if it's a bundle, defaulting to the URL of the bundle followed
71
+ * by a `/`. The bundle serves only the modules under it, so `file:///` lets it serve any file.
72
+ */
73
+ mount?: URL | string
68
74
  }
69
75
 
70
76
  export interface ImportOptions extends LinkOptions {}
package/lib/loader.js CHANGED
@@ -190,6 +190,7 @@ module.exports = class ModuleLoader {
190
190
  this._resolutions = resolutionsFor(resolutions)
191
191
  this._manifests = Object.create(null)
192
192
  this._bundles = []
193
+ this._mounts = new Map()
193
194
  this._visited = new Set()
194
195
  this._packages = new Map()
195
196
  this._prefixes = new Map()
@@ -302,6 +303,8 @@ module.exports = class ModuleLoader {
302
303
  async link(entry, source = null, opts = {}) {
303
304
  assertURL(entry, 'Entry')
304
305
 
306
+ if (opts.mount !== undefined) this._mounts.set(entry.href, mountFor(opts.mount))
307
+
305
308
  if (entry.protocol === 'builtin:') this._lookup(entry)
306
309
  else await this._link(this._root(entry, source, opts), this._protocolFor(entry))
307
310
 
@@ -313,6 +316,8 @@ module.exports = class ModuleLoader {
313
316
  linkSync(entry, source = null, opts = {}) {
314
317
  assertURL(entry, 'Entry')
315
318
 
319
+ if (opts.mount !== undefined) this._mounts.set(entry.href, mountFor(opts.mount))
320
+
316
321
  if (entry.protocol === 'builtin:') this._lookup(entry)
317
322
  else this._linkSync(this._root(entry, source, opts), this._protocolFor(entry))
318
323
 
@@ -430,6 +435,10 @@ module.exports = class ModuleLoader {
430
435
  })
431
436
  }
432
437
 
438
+ _mountFor(url) {
439
+ return this._mounts.get(url.href) || url.href + '/'
440
+ }
441
+
433
442
  _bundleFor(url) {
434
443
  for (const bundle of this._bundles) {
435
444
  if (bundleOwns(bundle, url)) return bundle
@@ -1386,6 +1395,16 @@ function toURL(value, base) {
1386
1395
  return URL.parse(value, base) || pathToFileURL(value)
1387
1396
  }
1388
1397
 
1398
+ function mountFor(mount) {
1399
+ const url = typeof mount === 'string' ? URL.parse(mount) : mount
1400
+
1401
+ if (!isURL(url)) {
1402
+ throw new TypeError(`Mount must be a URL. Received type ${typeof mount} (${mount})`)
1403
+ }
1404
+
1405
+ return url.href.endsWith('/') ? url.href : url.href + '/'
1406
+ }
1407
+
1389
1408
  function bundleCovers(bundle, href) {
1390
1409
  const root = bundle.root
1391
1410
 
@@ -14,7 +14,7 @@ module.exports = class BundleModule extends SyntheticModule {
14
14
  _open() {
15
15
  if (this._bundle !== null) return this._bundle
16
16
 
17
- const bundle = Bundle.from(this._source.bytes).mount(this.url.href + '/')
17
+ const bundle = Bundle.from(this._source.bytes).mount(this._loader._mountFor(this.url))
18
18
 
19
19
  this._bundle = bundle
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "7.2.2",
3
+ "version": "7.4.0",
4
4
  "description": "The module system that powers Bare",
5
5
  "exports": {
6
6
  "./package": "./package.json",