bare-module 1.13.4 → 1.14.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.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
@@ -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
 
@@ -270,6 +287,10 @@ module.exports = exports = class Module {
270
287
  else extension = '.js'
271
288
  }
272
289
 
290
+ if (extension === '.bundle' && path.extname(specifier) !== extension) {
291
+ throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
292
+ }
293
+
273
294
  this._extensions[extension].call(this, module, source, referrer, protocol, imports)
274
295
  }
275
296
 
@@ -667,8 +688,4 @@ exports._protocols['data:'] = new Protocol({
667
688
  }
668
689
  })
669
690
 
670
- process.on('exit', () => binding.destroy(exports._context))
671
-
672
- if (process.thread) {
673
- process.thread.on('exit', () => binding.destroy(exports._context))
674
- }
691
+ process.on('exit', exports._ondestroy.bind(exports))
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.4",
3
+ "version": "1.14.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [