bare-module 1.13.3 → 1.13.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/binding.c CHANGED
@@ -343,6 +343,28 @@ err:
343
343
  return NULL;
344
344
  }
345
345
 
346
+ static js_value_t *
347
+ bare_module_delete_module (js_env_t *env, js_callback_info_t *info) {
348
+ int err;
349
+
350
+ size_t argc = 1;
351
+ js_value_t *argv[1];
352
+
353
+ err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
354
+ assert(err == 0);
355
+
356
+ assert(argc == 1);
357
+
358
+ js_module_t *module;
359
+ err = js_get_value_external(env, argv[0], (void **) &module);
360
+ if (err < 0) return NULL;
361
+
362
+ err = js_delete_module(env, module);
363
+ assert(err == 0);
364
+
365
+ return NULL;
366
+ }
367
+
346
368
  static js_value_t *
347
369
  bare_module_set_export (js_env_t *env, js_callback_info_t *info) {
348
370
  int err;
@@ -596,6 +618,11 @@ init (js_env_t *env, js_value_t *exports) {
596
618
  js_create_function(env, "createSyntheticModule", -1, bare_module_create_synthetic_module, NULL, &fn);
597
619
  js_set_named_property(env, exports, "createSyntheticModule", fn);
598
620
  }
621
+ {
622
+ js_value_t *fn;
623
+ js_create_function(env, "deleteModule", -1, bare_module_delete_module, NULL, &fn);
624
+ js_set_named_property(env, exports, "deleteModule", fn);
625
+ }
599
626
  {
600
627
  js_value_t *fn;
601
628
  js_create_function(env, "setExport", -1, bare_module_set_export, NULL, &fn);
package/index.js CHANGED
@@ -8,12 +8,12 @@ const errors = require('./lib/errors')
8
8
  const binding = require('./binding')
9
9
 
10
10
  module.exports = exports = class Module {
11
- constructor (filename, main) {
11
+ constructor (filename) {
12
+ this._filename = filename
12
13
  this._state = 0
13
14
  this._type = 0
14
15
  this._defaultType = this._type
15
- this._filename = filename
16
- this._main = main || this
16
+ this._main = null
17
17
  this._exports = null
18
18
  this._imports = null
19
19
  this._info = null
@@ -59,6 +59,15 @@ module.exports = exports = class Module {
59
59
  return this.dirname
60
60
  }
61
61
 
62
+ destroy () {
63
+ this._state |= constants.states.DESTROYED
64
+
65
+ if (this._handle) {
66
+ binding.deleteModule(this._handle)
67
+ this._handle = null
68
+ }
69
+ }
70
+
62
71
  [Symbol.for('bare.inspect')] () {
63
72
  return {
64
73
  __proto__: { constructor: Module },
@@ -72,8 +81,6 @@ module.exports = exports = class Module {
72
81
  }
73
82
  }
74
83
 
75
- static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
76
-
77
84
  static _extensions = Object.create(null)
78
85
  static _protocols = Object.create(null)
79
86
  static _builtins = Object.create(null)
@@ -81,6 +88,16 @@ module.exports = exports = class Module {
81
88
  static _cache = Object.create(null)
82
89
  static _bundles = Object.create(null)
83
90
 
91
+ static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
92
+
93
+ static _ondestroy () {
94
+ for (const specifier in this._cache) {
95
+ this._cache[specifier].destroy()
96
+ }
97
+
98
+ binding.destroy(this._context)
99
+ }
100
+
84
101
  static _onimport (specifier, assertions, referrerFilename, dynamic) {
85
102
  const referrer = this._cache[referrerFilename]
86
103
 
@@ -164,18 +181,35 @@ module.exports = exports = class Module {
164
181
  return name in this._builtins
165
182
  }
166
183
 
167
- static createRequire (filename) {
184
+ static createRequire (filename, opts = {}) {
185
+ const {
186
+ imports = this._imports,
187
+ protocol = this._protocolFor(filename, this._protocols['file:']),
188
+ type = constants.types.SCRIPT,
189
+ defaultType = constants.types.SCRIPT
190
+ } = opts
191
+
168
192
  const module = new Module(filename)
193
+
194
+ module._type = type
195
+ module._defaultType = defaultType
196
+ module._imports = imports
197
+ module._protocol = protocol
198
+
169
199
  const referrer = module
170
200
 
171
201
  const resolve = (specifier) => {
172
202
  return this.resolve(specifier, path.dirname(module._filename), {
203
+ protocol: this._protocolFor(specifier, protocol),
204
+ imports,
173
205
  referrer
174
206
  })
175
207
  }
176
208
 
177
209
  const require = (specifier) => {
178
210
  const module = this.load(resolve(specifier), {
211
+ protocol: this._protocolFor(specifier, protocol),
212
+ imports,
179
213
  referrer
180
214
  })
181
215
 
@@ -223,7 +257,7 @@ module.exports = exports = class Module {
223
257
  })
224
258
  }
225
259
 
226
- const module = this._cache[specifier] = new this(specifier, main)
260
+ const module = this._cache[specifier] = new this(specifier)
227
261
 
228
262
  module._defaultType = defaultType
229
263
 
@@ -244,6 +278,8 @@ module.exports = exports = class Module {
244
278
  if (specifier in this._builtins) {
245
279
  module._exports = this._builtins[specifier]
246
280
  } else {
281
+ module._main = main || module
282
+
247
283
  let extension = this._extensionFor(type) || path.extname(specifier)
248
284
 
249
285
  if (extension in this._extensions === false) {
@@ -251,6 +287,10 @@ module.exports = exports = class Module {
251
287
  else extension = '.js'
252
288
  }
253
289
 
290
+ if (extension === '.bundle' && path.extname(specifier) !== extension) {
291
+ throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
292
+ }
293
+
254
294
  this._extensions[extension].call(this, module, source, referrer, protocol, imports)
255
295
  }
256
296
 
@@ -648,8 +688,8 @@ exports._protocols['data:'] = new Protocol({
648
688
  }
649
689
  })
650
690
 
651
- process.on('exit', () => binding.destroy(exports._context))
691
+ process.on('exit', exports._ondestroy.bind(exports))
652
692
 
653
693
  if (process.thread) {
654
- process.thread.on('exit', () => binding.destroy(exports._context))
694
+ process.thread.on('exit', exports._ondestroy.bind(exports))
655
695
  }
package/lib/constants.js CHANGED
@@ -1,7 +1,8 @@
1
1
  module.exports = {
2
2
  states: {
3
3
  EVALUATED: 1,
4
- SYNTHESIZED: 2
4
+ SYNTHESIZED: 2,
5
+ DESTROYED: 4
5
6
  },
6
7
 
7
8
  types: {
package/lib/errors.js CHANGED
@@ -19,4 +19,8 @@ module.exports = class ModuleError extends Error {
19
19
  static UNKNOWN_PROTOCOL (msg) {
20
20
  return new ModuleError(msg, 'UNKNOWN_PROTOCOL', ModuleError.UNKNOWN_PROTOCOL)
21
21
  }
22
+
23
+ static INVALID_BUNDLE_EXTENSION (msg) {
24
+ return new ModuleError(msg, 'INVALID_BUNDLE_EXTENSION', ModuleError.INVALID_BUNDLE_EXTENSION)
25
+ }
22
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.13.3",
3
+ "version": "1.13.5",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [