bare-module 3.1.4 → 3.1.5

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 CHANGED
@@ -18,8 +18,22 @@ const Module = require('bare-module')
18
18
 
19
19
  #### `Module.constants.states`
20
20
 
21
+ Constant | Description
22
+ :-- | :--
23
+ `EVALUATED` |
24
+ `SYNTHESIZED` |
25
+ `DESTROYED` |
26
+
21
27
  #### `Module.constants.types`
22
28
 
29
+ Constant | Description
30
+ :-- | :--
31
+ `SCRIPT` |
32
+ `MODULE` |
33
+ `JSON` |
34
+ `BUNDLE` |
35
+ `ADDON` |
36
+
23
37
  #### `Module.cache`
24
38
 
25
39
  #### `const url = Module.resolve(specifier, parentURL[, options])`
@@ -28,6 +42,12 @@ Options include:
28
42
 
29
43
  ```js
30
44
  {
45
+ referrer = null,
46
+ protocol,
47
+ imports,
48
+ resolutions,
49
+ builtins,
50
+ conditions
31
51
  }
32
52
  ```
33
53
 
@@ -37,6 +57,16 @@ Options include:
37
57
 
38
58
  ```js
39
59
  {
60
+ referrer = null,
61
+ type,
62
+ defaultType = constants.types.SCRIPT,
63
+ cache,
64
+ main,
65
+ protocol,
66
+ imports,
67
+ resolutions,
68
+ builtins,
69
+ conditions
40
70
  }
41
71
  ```
42
72
 
@@ -50,12 +80,16 @@ Options include:
50
80
 
51
81
  #### `module.defaultType`
52
82
 
83
+ #### `module.cache`
84
+
53
85
  #### `module.main`
54
86
 
55
87
  #### `module.exports`
56
88
 
57
89
  #### `module.imports`
58
90
 
91
+ #### `module.resolutions`
92
+
59
93
  #### `module.builtins`
60
94
 
61
95
  #### `module.conditions`
@@ -68,6 +102,23 @@ Options include:
68
102
 
69
103
  #### `const require = Module.createRequire(parentURL[, options])`
70
104
 
105
+ Options include:
106
+
107
+ ```js
108
+ {
109
+ referrer = null,
110
+ type = constants.types.SCRIPT,
111
+ defaultType = constants.types.SCRIPT,
112
+ cache,
113
+ main,
114
+ protocol,
115
+ imports,
116
+ resolutions,
117
+ builtins,
118
+ conditions
119
+ }
120
+ ```
121
+
71
122
  ### Protocols
72
123
 
73
124
  #### `const protocol = new Module.Protocol(options)`
@@ -76,6 +127,12 @@ Options include:
76
127
 
77
128
  ```js
78
129
  {
130
+ preresolve,
131
+ postresolve,
132
+ resolve,
133
+ exists,
134
+ read,
135
+ load
79
136
  }
80
137
  ```
81
138
 
@@ -83,6 +140,8 @@ Options include:
83
140
 
84
141
  #### `const bundle = new Module.Bundle()`
85
142
 
143
+ See <https://github.com/holepunchto/bare-bundle>.
144
+
86
145
  ## License
87
146
 
88
147
  Apache-2.0
package/index.js CHANGED
@@ -1,13 +1,15 @@
1
1
  /* global Bare */
2
2
  const path = require('bare-path')
3
- const url = require('bare-url')
4
3
  const resolve = require('bare-module-resolve')
5
4
  const Bundle = require('bare-bundle')
5
+ const { fileURLToPath, pathToFileURL } = require('url-file-url')
6
6
  const Protocol = require('./lib/protocol')
7
7
  const constants = require('./lib/constants')
8
8
  const errors = require('./lib/errors')
9
9
  const binding = require('./binding')
10
10
 
11
+ const isWindows = Bare.platform === 'win32'
12
+
11
13
  const { startsWithWindowsDriveLetter } = resolve
12
14
 
13
15
  const Module = module.exports = exports = class Module {
@@ -544,14 +546,12 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
544
546
 
545
547
  module._exports = {}
546
548
 
547
- const filename = urlToPath(module._url)
548
-
549
549
  binding.createFunction(module._url.href, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
550
550
  require,
551
551
  module,
552
552
  module._exports,
553
- filename,
554
- path.dirname(filename)
553
+ urlToPath(module._url),
554
+ urlToDirname(module._url)
555
555
  )
556
556
 
557
557
  function require (specifier) {
@@ -665,16 +665,16 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
665
665
  }
666
666
 
667
667
  Module._protocols['file:'] = new Protocol({
668
- postresolve (fileURL) {
669
- return url.pathToFileURL(binding.realpath(url.fileURLToPath(fileURL)))
668
+ postresolve (url) {
669
+ return pathToFileURL(binding.realpath(fileURLToPath(url)))
670
670
  },
671
671
 
672
- exists (fileURL) {
673
- return binding.exists(url.fileURLToPath(fileURL))
672
+ exists (url) {
673
+ return binding.exists(fileURLToPath(url))
674
674
  },
675
675
 
676
- read (fileURL) {
677
- return Buffer.from(binding.read(url.fileURLToPath(fileURL)))
676
+ read (url) {
677
+ return Buffer.from(binding.read(fileURLToPath(url)))
678
678
  }
679
679
  })
680
680
 
@@ -687,14 +687,34 @@ Bare
687
687
  binding.destroy(Module._handle)
688
688
  })
689
689
 
690
- function urlToPath (u) {
691
- return u.protocol === 'file:'
692
- ? url.fileURLToPath(u)
693
- : decodeURIComponent(u.pathname)
690
+ function urlToPath (url) {
691
+ if (url.protocol) return fileURLToPath(url)
692
+
693
+ if (isWindows) {
694
+ if (/%2f|%5c/i.test(url.pathname)) {
695
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
696
+ }
697
+ } else {
698
+ if (/%2f/i.test(url.pathname)) {
699
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
700
+ }
701
+ }
702
+
703
+ return decodeURIComponent(url.pathname)
694
704
  }
695
705
 
696
- function urlToDirname (u) {
697
- return u.protocol === 'file:'
698
- ? path.dirname(url.fileURLToPath(u))
699
- : decodeURIComponent((new URL('.', u)).pathname).replace(/\/$/, '')
706
+ function urlToDirname (url) {
707
+ if (url.protocol) return path.dirname(fileURLToPath(url))
708
+
709
+ if (isWindows) {
710
+ if (/%2f|%5c/i.test(url.pathname)) {
711
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded \\ or / characters')
712
+ }
713
+ } else {
714
+ if (/%2f/i.test(url.pathname)) {
715
+ throw errors.INVALID_URL_PATH('The URL path must not include encoded / characters')
716
+ }
717
+ }
718
+
719
+ return decodeURIComponent((new URL('.', url)).pathname).replace(/\/$/, '')
700
720
  }
package/lib/errors.js CHANGED
@@ -23,4 +23,8 @@ module.exports = class ModuleError extends Error {
23
23
  static INVALID_BUNDLE_EXTENSION (msg) {
24
24
  return new ModuleError(msg, 'INVALID_BUNDLE_EXTENSION', ModuleError.INVALID_BUNDLE_EXTENSION)
25
25
  }
26
+
27
+ static INVALID_URL_PATH (msg) {
28
+ return new ModuleError(msg, 'INVALID_URL_PATH', ModuleError.INVALID_URL_PATH)
29
+ }
26
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "3.1.4",
3
+ "version": "3.1.5",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -29,7 +29,7 @@
29
29
  "bare-bundle": "^1.0.0",
30
30
  "bare-module-resolve": "^1.4.4",
31
31
  "bare-path": "^2.0.0",
32
- "bare-url": "^1.0.0"
32
+ "url-file-url": "^1.0.2"
33
33
  },
34
34
  "devDependencies": {
35
35
  "brittle": "^3.1.1",