bare-module 1.14.7 → 2.0.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/binding.js +1 -1
- package/index.js +98 -101
- package/lib/protocol.js +3 -1
- package/package.json +5 -3
- package/prebuilds/darwin-arm64/bare_module.bare +0 -0
package/binding.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports =
|
|
1
|
+
module.exports = require.addon()
|
package/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
/* global Bare */
|
|
2
|
+
const path = require('bare-path')
|
|
3
|
+
const os = require('bare-os')
|
|
4
4
|
const Bundle = require('bare-bundle')
|
|
5
5
|
const Protocol = require('./lib/protocol')
|
|
6
6
|
const constants = require('./lib/constants')
|
|
7
7
|
const errors = require('./lib/errors')
|
|
8
8
|
const binding = require('./binding')
|
|
9
|
+
const addon = require.addon.bind(require)
|
|
9
10
|
|
|
10
|
-
module.exports = exports = class Module {
|
|
11
|
+
const Module = module.exports = exports = class Module {
|
|
11
12
|
constructor (filename) {
|
|
12
13
|
this._filename = filename
|
|
13
14
|
this._state = 0
|
|
@@ -87,7 +88,6 @@ module.exports = exports = class Module {
|
|
|
87
88
|
|
|
88
89
|
static _extensions = Object.create(null)
|
|
89
90
|
static _protocols = Object.create(null)
|
|
90
|
-
static _builtins = Object.create(null)
|
|
91
91
|
static _imports = Object.create(null)
|
|
92
92
|
static _cache = Object.create(null)
|
|
93
93
|
static _bundles = Object.create(null)
|
|
@@ -170,14 +170,6 @@ module.exports = exports = class Module {
|
|
|
170
170
|
return this._cache
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
static get builtinModules () {
|
|
174
|
-
return Object.keys(this._builtins)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
static isBuiltin (name) {
|
|
178
|
-
return name in this._builtins
|
|
179
|
-
}
|
|
180
|
-
|
|
181
173
|
static createRequire (filename, opts = {}) {
|
|
182
174
|
const {
|
|
183
175
|
imports = this._imports,
|
|
@@ -216,6 +208,7 @@ module.exports = exports = class Module {
|
|
|
216
208
|
require.main = module._main
|
|
217
209
|
require.cache = this._cache
|
|
218
210
|
require.resolve = resolve
|
|
211
|
+
require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
|
|
219
212
|
|
|
220
213
|
return require
|
|
221
214
|
}
|
|
@@ -266,33 +259,29 @@ module.exports = exports = class Module {
|
|
|
266
259
|
|
|
267
260
|
if (protocol.exists(pkg)) {
|
|
268
261
|
try {
|
|
269
|
-
module._info =
|
|
262
|
+
module._info = this.load(pkg, { protocol })._exports
|
|
270
263
|
} catch {}
|
|
271
264
|
break
|
|
272
265
|
}
|
|
273
266
|
|
|
274
267
|
dirname = path.dirname(dirname)
|
|
275
|
-
} while (dirname !==
|
|
268
|
+
} while (dirname !== path.sep && dirname !== '.')
|
|
276
269
|
|
|
277
|
-
|
|
278
|
-
module._exports = this._builtins[specifier]
|
|
279
|
-
} else {
|
|
280
|
-
module._main = main || module
|
|
270
|
+
module._main = main || module
|
|
281
271
|
|
|
282
|
-
|
|
272
|
+
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
283
273
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (extension === '.bundle' && path.extname(specifier) !== extension) {
|
|
290
|
-
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
291
|
-
}
|
|
274
|
+
if (extension in this._extensions === false) {
|
|
275
|
+
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
|
|
276
|
+
else extension = '.js'
|
|
277
|
+
}
|
|
292
278
|
|
|
293
|
-
|
|
279
|
+
if (extension === '.bundle' && path.extname(specifier) !== extension) {
|
|
280
|
+
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
294
281
|
}
|
|
295
282
|
|
|
283
|
+
this._extensions[extension].call(this, module, source, referrer, protocol, imports)
|
|
284
|
+
|
|
296
285
|
return this._transform(module, referrer, dynamic)
|
|
297
286
|
}
|
|
298
287
|
|
|
@@ -349,10 +338,6 @@ module.exports = exports = class Module {
|
|
|
349
338
|
|
|
350
339
|
yield * protocol.resolve(specifier, dirname, imports)
|
|
351
340
|
|
|
352
|
-
if (this.isBuiltin(specifier)) {
|
|
353
|
-
yield specifier
|
|
354
|
-
}
|
|
355
|
-
|
|
356
341
|
if (path.isAbsolute(specifier)) {
|
|
357
342
|
yield * this._resolveFile(specifier, protocol)
|
|
358
343
|
yield * this._resolveDirectory(specifier, protocol)
|
|
@@ -529,7 +514,7 @@ module.exports = exports = class Module {
|
|
|
529
514
|
}
|
|
530
515
|
|
|
531
516
|
name = path.dirname(name)
|
|
532
|
-
} while (name !==
|
|
517
|
+
} while (name !== path.sep && name !== '.')
|
|
533
518
|
|
|
534
519
|
if (path.extname(name) !== '.bundle') return null
|
|
535
520
|
|
|
@@ -606,7 +591,7 @@ module.exports = exports = class Module {
|
|
|
606
591
|
}
|
|
607
592
|
}
|
|
608
593
|
|
|
609
|
-
|
|
594
|
+
Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
|
|
610
595
|
const isESM = (
|
|
611
596
|
// The default type is ES modules.
|
|
612
597
|
(module._defaultType === constants.types.MODULE) ||
|
|
@@ -623,99 +608,118 @@ exports._extensions['.js'] = function (module, source, referrer, protocol, impor
|
|
|
623
608
|
return loader.call(this, module, source, referrer, protocol, imports)
|
|
624
609
|
}
|
|
625
610
|
|
|
626
|
-
|
|
627
|
-
|
|
611
|
+
Module._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
|
|
612
|
+
module._type = constants.types.SCRIPT
|
|
613
|
+
module._protocol = protocol
|
|
614
|
+
module._imports = imports
|
|
628
615
|
|
|
629
|
-
if (
|
|
616
|
+
if (protocol.load) {
|
|
617
|
+
module._exports = protocol.load(module._filename)
|
|
618
|
+
} else {
|
|
619
|
+
if (source === null) source = protocol.read(module._filename)
|
|
630
620
|
|
|
631
|
-
|
|
621
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
632
622
|
|
|
633
|
-
|
|
634
|
-
return this.resolve(specifier, path.dirname(module._filename), {
|
|
635
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
636
|
-
imports,
|
|
637
|
-
referrer
|
|
638
|
-
})
|
|
639
|
-
}
|
|
623
|
+
referrer = module
|
|
640
624
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
625
|
+
const resolve = (specifier) => {
|
|
626
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
627
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
628
|
+
imports,
|
|
629
|
+
referrer
|
|
630
|
+
})
|
|
631
|
+
}
|
|
647
632
|
|
|
648
|
-
|
|
649
|
-
|
|
633
|
+
const require = (specifier) => {
|
|
634
|
+
const module = this.load(resolve(specifier), {
|
|
635
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
636
|
+
imports,
|
|
637
|
+
referrer
|
|
638
|
+
})
|
|
650
639
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
module._imports = imports
|
|
654
|
-
module._exports = {}
|
|
655
|
-
|
|
656
|
-
require.main = module._main
|
|
657
|
-
require.cache = this._cache
|
|
658
|
-
require.resolve = resolve
|
|
659
|
-
|
|
660
|
-
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
661
|
-
require,
|
|
662
|
-
module,
|
|
663
|
-
module._exports,
|
|
664
|
-
module._filename,
|
|
665
|
-
path.dirname(module._filename)
|
|
666
|
-
)
|
|
667
|
-
}
|
|
640
|
+
return module._exports
|
|
641
|
+
}
|
|
668
642
|
|
|
669
|
-
|
|
670
|
-
if (source === null) source = protocol.read(module._filename)
|
|
643
|
+
module._exports = {}
|
|
671
644
|
|
|
672
|
-
|
|
645
|
+
require.main = module._main
|
|
646
|
+
require.cache = this._cache
|
|
647
|
+
require.resolve = resolve
|
|
648
|
+
require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
|
|
673
649
|
|
|
650
|
+
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
651
|
+
require,
|
|
652
|
+
module,
|
|
653
|
+
module._exports,
|
|
654
|
+
module._filename,
|
|
655
|
+
path.dirname(module._filename)
|
|
656
|
+
)
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
Module._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
|
|
674
661
|
module._type = constants.types.MODULE
|
|
675
662
|
module._protocol = protocol
|
|
676
663
|
module._imports = imports
|
|
677
|
-
module._handle = binding.createModule(module._filename, source, 0, this._handle)
|
|
678
|
-
}
|
|
679
664
|
|
|
680
|
-
|
|
681
|
-
|
|
665
|
+
if (protocol.load) {
|
|
666
|
+
module._exports = protocol.load(module._filename)
|
|
667
|
+
} else {
|
|
668
|
+
if (source === null) source = protocol.read(module._filename)
|
|
669
|
+
|
|
670
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
682
671
|
|
|
683
|
-
|
|
672
|
+
module._handle = binding.createModule(module._filename, source, 0, this._handle)
|
|
673
|
+
}
|
|
674
|
+
}
|
|
684
675
|
|
|
676
|
+
Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
|
|
685
677
|
module._type = constants.types.JSON
|
|
686
678
|
module._protocol = protocol
|
|
687
679
|
module._imports = imports
|
|
688
|
-
|
|
680
|
+
|
|
681
|
+
if (protocol.load) {
|
|
682
|
+
module._exports = protocol.load(module._filename)
|
|
683
|
+
} else {
|
|
684
|
+
if (source === null) source = protocol.read(module._filename)
|
|
685
|
+
|
|
686
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
687
|
+
|
|
688
|
+
module._exports = JSON.parse(source)
|
|
689
|
+
}
|
|
689
690
|
}
|
|
690
691
|
|
|
691
|
-
|
|
692
|
+
Module._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
|
|
692
693
|
module._type = constants.types.ADDON
|
|
693
694
|
module._protocol = protocol
|
|
694
695
|
module._imports = imports
|
|
695
|
-
|
|
696
|
+
|
|
697
|
+
module._exports = addon(module._filename)
|
|
696
698
|
}
|
|
697
699
|
|
|
698
|
-
|
|
700
|
+
Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
|
|
699
701
|
module._type = constants.types.ADDON
|
|
700
702
|
module._protocol = protocol
|
|
701
703
|
module._imports = imports
|
|
702
|
-
|
|
704
|
+
|
|
705
|
+
module._exports = addon(module._filename)
|
|
703
706
|
}
|
|
704
707
|
|
|
705
|
-
|
|
708
|
+
Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
|
|
709
|
+
module._type = constants.types.BUNDLE
|
|
710
|
+
module._protocol = protocol
|
|
711
|
+
module._imports = imports
|
|
712
|
+
|
|
706
713
|
if (source === null) source = protocol.read(module._filename)
|
|
707
714
|
|
|
708
715
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
709
716
|
|
|
710
717
|
const bundle = this._bundleFor(module._filename, protocol, source)
|
|
711
718
|
|
|
712
|
-
module.
|
|
713
|
-
module._protocol = protocol
|
|
714
|
-
module._imports = imports
|
|
715
|
-
module._exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer })._exports
|
|
719
|
+
module._exports = this.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer })._exports
|
|
716
720
|
}
|
|
717
721
|
|
|
718
|
-
|
|
722
|
+
Module._protocols['file:'] = new Protocol({
|
|
719
723
|
preresolve (specifier, dirname) {
|
|
720
724
|
specifier = specifier.replace(/^file:/, '')
|
|
721
725
|
|
|
@@ -726,7 +730,6 @@ exports._protocols['file:'] = new Protocol({
|
|
|
726
730
|
},
|
|
727
731
|
|
|
728
732
|
postresolve (specifier) {
|
|
729
|
-
if (exports.isBuiltin(specifier)) return specifier
|
|
730
733
|
return binding.realpath(specifier)
|
|
731
734
|
},
|
|
732
735
|
|
|
@@ -739,13 +742,7 @@ exports._protocols['file:'] = new Protocol({
|
|
|
739
742
|
}
|
|
740
743
|
})
|
|
741
744
|
|
|
742
|
-
|
|
743
|
-
preresolve (specifier) {
|
|
744
|
-
return specifier.replace(/^node:/, '')
|
|
745
|
-
}
|
|
746
|
-
})
|
|
747
|
-
|
|
748
|
-
exports._protocols['data:'] = new Protocol({
|
|
745
|
+
Module._protocols['data:'] = new Protocol({
|
|
749
746
|
* resolve (specifier) {
|
|
750
747
|
yield specifier
|
|
751
748
|
},
|
|
@@ -757,11 +754,11 @@ exports._protocols['data:'] = new Protocol({
|
|
|
757
754
|
}
|
|
758
755
|
})
|
|
759
756
|
|
|
760
|
-
|
|
757
|
+
Bare
|
|
761
758
|
.prependListener('teardown', () => {
|
|
762
|
-
for (const module of
|
|
759
|
+
for (const module of Module._modules) {
|
|
763
760
|
module.destroy()
|
|
764
761
|
}
|
|
765
762
|
|
|
766
|
-
binding.destroy(
|
|
763
|
+
binding.destroy(Module._handle)
|
|
767
764
|
})
|
package/lib/protocol.js
CHANGED
|
@@ -6,7 +6,8 @@ module.exports = class ModuleProtocol {
|
|
|
6
6
|
postresolve = null,
|
|
7
7
|
resolve = null,
|
|
8
8
|
exists = null,
|
|
9
|
-
read = null
|
|
9
|
+
read = null,
|
|
10
|
+
load = null
|
|
10
11
|
} = opts
|
|
11
12
|
|
|
12
13
|
this.imports = imports
|
|
@@ -16,6 +17,7 @@ module.exports = class ModuleProtocol {
|
|
|
16
17
|
if (resolve) this.resolve = resolve.bind(this)
|
|
17
18
|
if (exists) this.exists = exists.bind(this)
|
|
18
19
|
if (read) this.read = read.bind(this)
|
|
20
|
+
if (load) this.load = load.bind(this)
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
preresolve (specifier, dirname) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/holepunchto/bare-module.git"
|
|
19
|
+
"url": "git+https://github.com/holepunchto/bare-module.git"
|
|
20
20
|
},
|
|
21
21
|
"author": "Holepunch",
|
|
22
22
|
"license": "Apache-2.0",
|
|
@@ -25,7 +25,9 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/holepunchto/bare-module#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"bare-bundle": "^0.
|
|
28
|
+
"bare-bundle": "^0.3.0",
|
|
29
|
+
"bare-os": "^2.0.0",
|
|
30
|
+
"bare-path": "^2.0.0"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"brittle": "^3.1.1",
|
|
Binary file
|