bare-module 3.1.9 → 3.1.11

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
@@ -465,6 +465,25 @@ bare_module_run_module (js_env_t *env, js_callback_info_t *info) {
465
465
  err = js_run_module(env, module, &result);
466
466
  if (err < 0) return NULL;
467
467
 
468
+ bool is_promise;
469
+ err = js_is_promise(env, result, &is_promise);
470
+ assert(err == 0);
471
+
472
+ if (is_promise) {
473
+ js_promise_state_t state;
474
+ err = js_get_promise_state(env, result, &state);
475
+ assert(err == 0);
476
+
477
+ if (state == js_promise_rejected) {
478
+ js_value_t *error;
479
+ err = js_get_promise_result(env, result, &error);
480
+ if (err < 0) return NULL;
481
+
482
+ err = js_set_named_property(env, result, "error", error);
483
+ if (err < 0) return NULL;
484
+ }
485
+ }
486
+
468
487
  return result;
469
488
  }
470
489
 
package/index.js CHANGED
@@ -162,7 +162,7 @@ const Module = module.exports = exports = class Module {
162
162
  break
163
163
  }
164
164
 
165
- case constants.types.MODULE: {
165
+ case constants.types.MODULE:
166
166
  module._evaluate()
167
167
 
168
168
  for (const name of Object.keys(module._exports)) {
@@ -170,13 +170,11 @@ const Module = module.exports = exports = class Module {
170
170
  }
171
171
 
172
172
  break
173
- }
174
173
 
175
- case constants.types.JSON: {
174
+ case constants.types.JSON:
176
175
  for (const name of Object.keys(module._exports)) {
177
176
  names.push(name)
178
177
  }
179
- }
180
178
  }
181
179
  }
182
180
 
@@ -190,12 +188,16 @@ const Module = module.exports = exports = class Module {
190
188
 
191
189
  this._state |= constants.states.EVALUATED
192
190
 
191
+ let result
192
+
193
193
  if (this._type === constants.types.SCRIPT) {
194
194
  const require = createRequire(this._url, { module: this })
195
195
 
196
196
  this._exports = {}
197
197
 
198
- this._function(
198
+ const fn = this._function // Bind to variable to ensure proper stack trace
199
+
200
+ fn(
199
201
  require,
200
202
  this,
201
203
  this._exports,
@@ -203,17 +205,23 @@ const Module = module.exports = exports = class Module {
203
205
  urlToDirname(this._url)
204
206
  )
205
207
 
206
- if (eagerRun) binding.runModule(this._handle, Module._handle)
208
+ if (eagerRun) result = binding.runModule(this._handle, Module._handle)
207
209
  }
208
210
 
209
211
  if (this._type === constants.types.MODULE) {
210
- binding.runModule(this._handle, Module._handle)
212
+ result = binding.runModule(this._handle, Module._handle)
211
213
 
212
214
  this._exports = binding.getNamespace(this._handle)
213
215
  }
214
216
 
215
217
  if (this._type === constants.types.ADDON) {
216
- if (eagerRun) binding.runModule(this._handle, Module._handle)
218
+ if (eagerRun) result = binding.runModule(this._handle, Module._handle)
219
+ }
220
+
221
+ if (result && result.error) {
222
+ result.catch(() => {}) // Handle the promise rejection
223
+
224
+ throw result.error
217
225
  }
218
226
  }
219
227
 
@@ -365,33 +373,39 @@ const Module = module.exports = exports = class Module {
365
373
 
366
374
  const module = cache[url.href] = new Module(url)
367
375
 
368
- switch (url.protocol) {
369
- case 'builtin:':
370
- module._exports = builtins[url.pathname]
371
- break
376
+ try {
377
+ switch (url.protocol) {
378
+ case 'builtin:':
379
+ module._exports = builtins[url.pathname]
380
+ break
372
381
 
373
- default: {
374
- module._defaultType = defaultType
375
- module._cache = cache
376
- module._main = main || module
377
- module._protocol = protocol
378
- module._imports = imports
379
- module._resolutions = resolutions
380
- module._builtins = builtins
381
- module._conditions = conditions
382
-
383
- let extension = self._extensionFor(type) || path.extname(url.pathname)
384
-
385
- if (extension in self._extensions === false) {
386
- if (defaultType) extension = self._extensionFor(defaultType) || '.js'
387
- else extension = '.js'
388
- }
382
+ default: {
383
+ module._defaultType = defaultType
384
+ module._cache = cache
385
+ module._main = main || module
386
+ module._protocol = protocol
387
+ module._imports = imports
388
+ module._resolutions = resolutions
389
+ module._builtins = builtins
390
+ module._conditions = conditions
391
+
392
+ let extension = self._extensionFor(type) || path.extname(url.pathname)
393
+
394
+ if (extension in self._extensions === false) {
395
+ if (defaultType) extension = self._extensionFor(defaultType) || '.js'
396
+ else extension = '.js'
397
+ }
389
398
 
390
- self._extensions[extension](module, source, referrer)
399
+ self._extensions[extension](module, source, referrer)
400
+ }
391
401
  }
392
- }
393
402
 
394
- return module._transform(isImport, isDynamicImport)
403
+ return module._transform(isImport, isDynamicImport)
404
+ } catch (err) {
405
+ delete cache[url.href]
406
+
407
+ throw err
408
+ }
395
409
  }
396
410
 
397
411
  static resolve (specifier, parentURL, opts = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "3.1.9",
3
+ "version": "3.1.11",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [