bare-module 1.8.5 → 1.9.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 +13 -21
- package/index.js +57 -12
- package/lib/protocol.js +3 -0
- package/package.json +1 -1
package/binding.c
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
#include <stddef.h>
|
|
6
6
|
#include <stdint.h>
|
|
7
7
|
#include <stdlib.h>
|
|
8
|
+
#include <utf.h>
|
|
8
9
|
#include <uv.h>
|
|
9
10
|
|
|
10
11
|
typedef struct {
|
|
@@ -32,7 +33,7 @@ on_static_import (js_env_t *env, js_value_t *specifier, js_value_t *assertions,
|
|
|
32
33
|
|
|
33
34
|
js_value_t *args[4] = {specifier, assertions};
|
|
34
35
|
|
|
35
|
-
err = js_create_string_utf8(env, name, -1, &args[2]);
|
|
36
|
+
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[2]);
|
|
36
37
|
if (err < 0) return NULL;
|
|
37
38
|
|
|
38
39
|
err = js_get_boolean(env, false, &args[3]);
|
|
@@ -99,7 +100,7 @@ on_evaluate (js_env_t *env, js_module_t *module, void *data) {
|
|
|
99
100
|
|
|
100
101
|
js_value_t *args[1];
|
|
101
102
|
|
|
102
|
-
err = js_create_string_utf8(env, name, -1, &args[0]);
|
|
103
|
+
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
|
|
103
104
|
if (err < 0) return;
|
|
104
105
|
|
|
105
106
|
js_value_t *result;
|
|
@@ -175,7 +176,7 @@ bare_module_create_function (js_env_t *env, js_callback_info_t *info) {
|
|
|
175
176
|
assert(argc == 4);
|
|
176
177
|
|
|
177
178
|
size_t file_len;
|
|
178
|
-
|
|
179
|
+
utf8_t file[1024];
|
|
179
180
|
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
180
181
|
if (err < 0) return NULL;
|
|
181
182
|
|
|
@@ -197,7 +198,7 @@ bare_module_create_function (js_env_t *env, js_callback_info_t *info) {
|
|
|
197
198
|
if (err < 0) goto err;
|
|
198
199
|
|
|
199
200
|
js_value_t *result;
|
|
200
|
-
err = js_create_function_with_source(env, NULL, 0, file, file_len, args, args_len, 0, source, &result);
|
|
201
|
+
err = js_create_function_with_source(env, NULL, 0, (char *) file, file_len, args, args_len, 0, source, &result);
|
|
201
202
|
if (err < 0) goto err;
|
|
202
203
|
|
|
203
204
|
free(args);
|
|
@@ -223,7 +224,7 @@ bare_module_create_module (js_env_t *env, js_callback_info_t *info) {
|
|
|
223
224
|
assert(argc == 3);
|
|
224
225
|
|
|
225
226
|
size_t file_len;
|
|
226
|
-
|
|
227
|
+
utf8_t file[1024];
|
|
227
228
|
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
228
229
|
if (err < 0) return NULL;
|
|
229
230
|
|
|
@@ -234,7 +235,7 @@ bare_module_create_module (js_env_t *env, js_callback_info_t *info) {
|
|
|
234
235
|
if (err < 0) return NULL;
|
|
235
236
|
|
|
236
237
|
js_module_t *module;
|
|
237
|
-
err = js_create_module(env, file, file_len, offset, source, &module);
|
|
238
|
+
err = js_create_module(env, (char *) file, file_len, offset, source, &module);
|
|
238
239
|
if (err < 0) return NULL;
|
|
239
240
|
|
|
240
241
|
js_value_t *result;
|
|
@@ -257,7 +258,7 @@ bare_module_create_synthetic_module (js_env_t *env, js_callback_info_t *info) {
|
|
|
257
258
|
assert(argc == 3);
|
|
258
259
|
|
|
259
260
|
size_t file_len;
|
|
260
|
-
|
|
261
|
+
utf8_t file[1024];
|
|
261
262
|
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
262
263
|
if (err < 0) return NULL;
|
|
263
264
|
|
|
@@ -277,7 +278,7 @@ bare_module_create_synthetic_module (js_env_t *env, js_callback_info_t *info) {
|
|
|
277
278
|
if (err < 0) goto err;
|
|
278
279
|
|
|
279
280
|
js_module_t *module;
|
|
280
|
-
err = js_create_synthetic_module(env, file, file_len, export_names, names_len, on_evaluate, (void *) context, &module);
|
|
281
|
+
err = js_create_synthetic_module(env, (char *) file, file_len, export_names, names_len, on_evaluate, (void *) context, &module);
|
|
281
282
|
if (err < 0) goto err;
|
|
282
283
|
|
|
283
284
|
js_value_t *result;
|
|
@@ -382,12 +383,12 @@ bare_module_exists (js_env_t *env, js_callback_info_t *info) {
|
|
|
382
383
|
|
|
383
384
|
assert(argc == 1);
|
|
384
385
|
|
|
385
|
-
|
|
386
|
+
utf8_t path[4096];
|
|
386
387
|
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
387
388
|
assert(err == 0);
|
|
388
389
|
|
|
389
390
|
uv_fs_t req;
|
|
390
|
-
uv_fs_stat(loop, &req, path, NULL);
|
|
391
|
+
uv_fs_stat(loop, &req, (char *) path, NULL);
|
|
391
392
|
|
|
392
393
|
uv_stat_t *st = req.result < 0 ? NULL : req.ptr;
|
|
393
394
|
|
|
@@ -416,12 +417,12 @@ bare_module_read (js_env_t *env, js_callback_info_t *info) {
|
|
|
416
417
|
|
|
417
418
|
assert(argc == 1);
|
|
418
419
|
|
|
419
|
-
|
|
420
|
+
utf8_t path[4096];
|
|
420
421
|
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
421
422
|
assert(err == 0);
|
|
422
423
|
|
|
423
424
|
uv_fs_t req;
|
|
424
|
-
uv_fs_open(loop, &req, path, UV_FS_O_RDONLY, 0, NULL);
|
|
425
|
+
uv_fs_open(loop, &req, (char *) path, UV_FS_O_RDONLY, 0, NULL);
|
|
425
426
|
|
|
426
427
|
int fd = req.result;
|
|
427
428
|
uv_fs_req_cleanup(&req);
|
|
@@ -481,55 +482,46 @@ init (js_env_t *env, js_value_t *exports) {
|
|
|
481
482
|
js_create_function(env, "init", -1, bare_module_init, NULL, &fn);
|
|
482
483
|
js_set_named_property(env, exports, "init", fn);
|
|
483
484
|
}
|
|
484
|
-
|
|
485
485
|
{
|
|
486
486
|
js_value_t *fn;
|
|
487
487
|
js_create_function(env, "destroy", -1, bare_module_destroy, NULL, &fn);
|
|
488
488
|
js_set_named_property(env, exports, "destroy", fn);
|
|
489
489
|
}
|
|
490
|
-
|
|
491
490
|
{
|
|
492
491
|
js_value_t *fn;
|
|
493
492
|
js_create_function(env, "createFunction", -1, bare_module_create_function, NULL, &fn);
|
|
494
493
|
js_set_named_property(env, exports, "createFunction", fn);
|
|
495
494
|
}
|
|
496
|
-
|
|
497
495
|
{
|
|
498
496
|
js_value_t *fn;
|
|
499
497
|
js_create_function(env, "createModule", -1, bare_module_create_module, NULL, &fn);
|
|
500
498
|
js_set_named_property(env, exports, "createModule", fn);
|
|
501
499
|
}
|
|
502
|
-
|
|
503
500
|
{
|
|
504
501
|
js_value_t *fn;
|
|
505
502
|
js_create_function(env, "createSyntheticModule", -1, bare_module_create_synthetic_module, NULL, &fn);
|
|
506
503
|
js_set_named_property(env, exports, "createSyntheticModule", fn);
|
|
507
504
|
}
|
|
508
|
-
|
|
509
505
|
{
|
|
510
506
|
js_value_t *fn;
|
|
511
507
|
js_create_function(env, "setExport", -1, bare_module_set_export, NULL, &fn);
|
|
512
508
|
js_set_named_property(env, exports, "setExport", fn);
|
|
513
509
|
}
|
|
514
|
-
|
|
515
510
|
{
|
|
516
511
|
js_value_t *fn;
|
|
517
512
|
js_create_function(env, "runModule", -1, bare_module_run_module, NULL, &fn);
|
|
518
513
|
js_set_named_property(env, exports, "runModule", fn);
|
|
519
514
|
}
|
|
520
|
-
|
|
521
515
|
{
|
|
522
516
|
js_value_t *fn;
|
|
523
517
|
js_create_function(env, "getNamespace", -1, bare_module_get_namespace, NULL, &fn);
|
|
524
518
|
js_set_named_property(env, exports, "getNamespace", fn);
|
|
525
519
|
}
|
|
526
|
-
|
|
527
520
|
{
|
|
528
521
|
js_value_t *fn;
|
|
529
522
|
js_create_function(env, "exists", -1, bare_module_exists, NULL, &fn);
|
|
530
523
|
js_set_named_property(env, exports, "exists", fn);
|
|
531
524
|
}
|
|
532
|
-
|
|
533
525
|
{
|
|
534
526
|
js_value_t *fn;
|
|
535
527
|
js_create_function(env, "read", -1, bare_module_read, NULL, &fn);
|
package/index.js
CHANGED
|
@@ -36,16 +36,27 @@ const Module = module.exports = class Module {
|
|
|
36
36
|
let protocol, imports
|
|
37
37
|
|
|
38
38
|
if (referrer) {
|
|
39
|
+
protocol = this._protocolFor(specifier, referrer._protocol)
|
|
40
|
+
|
|
41
|
+
imports = referrer._imports
|
|
42
|
+
|
|
39
43
|
specifier = this.resolve(specifier, referrer.dirname, {
|
|
40
|
-
protocol
|
|
41
|
-
imports
|
|
44
|
+
protocol,
|
|
45
|
+
imports,
|
|
42
46
|
referrer
|
|
43
47
|
})
|
|
44
48
|
} else {
|
|
45
49
|
specifier = this.resolve(specifier)
|
|
46
50
|
}
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
const module = this.load(specifier, {
|
|
53
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
54
|
+
imports,
|
|
55
|
+
referrer,
|
|
56
|
+
dynamic
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
return module._handle
|
|
49
60
|
}
|
|
50
61
|
|
|
51
62
|
static _onevaluate (specifier) {
|
|
@@ -141,6 +152,10 @@ const Module = module.exports = class Module {
|
|
|
141
152
|
|
|
142
153
|
specifier = protocol.map(specifier, dirname)
|
|
143
154
|
|
|
155
|
+
if (protocol.resolve) {
|
|
156
|
+
yield * protocol.resolve(specifier, dirname, imports)
|
|
157
|
+
}
|
|
158
|
+
|
|
144
159
|
if (this.isBuiltin(specifier)) {
|
|
145
160
|
yield specifier
|
|
146
161
|
}
|
|
@@ -214,7 +229,7 @@ const Module = module.exports = class Module {
|
|
|
214
229
|
}
|
|
215
230
|
}
|
|
216
231
|
|
|
217
|
-
static _protocolFor (specifier, fallback) {
|
|
232
|
+
static _protocolFor (specifier, fallback = null) {
|
|
218
233
|
const i = specifier.indexOf(':')
|
|
219
234
|
|
|
220
235
|
if (i < 2) return fallback // Allow drive letters in Windows paths
|
|
@@ -222,7 +237,11 @@ const Module = module.exports = class Module {
|
|
|
222
237
|
const protocol = specifier.slice(0, i + 1)
|
|
223
238
|
|
|
224
239
|
if (!this._protocols[protocol]) {
|
|
225
|
-
|
|
240
|
+
if (fallback === null) {
|
|
241
|
+
throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${protocol}' in specifier '${specifier}'`)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return fallback
|
|
226
245
|
}
|
|
227
246
|
|
|
228
247
|
return this._protocols[protocol]
|
|
@@ -276,11 +295,15 @@ const Module = module.exports = class Module {
|
|
|
276
295
|
}
|
|
277
296
|
|
|
278
297
|
Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
298
|
+
const isESM = (
|
|
299
|
+
// The package is explicitly declared as an ES module.
|
|
300
|
+
(module._info && module._info.type === 'module') ||
|
|
301
|
+
|
|
302
|
+
// The referrer is itself an ES module.
|
|
303
|
+
(referrer && referrer._type === 'esm')
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
const loader = this._extensions[isESM ? '.mjs' : '.cjs']
|
|
284
307
|
|
|
285
308
|
return loader.call(this, module, source, referrer, protocol, imports)
|
|
286
309
|
}
|
|
@@ -293,11 +316,21 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
293
316
|
referrer = module
|
|
294
317
|
|
|
295
318
|
const resolve = (specifier) => {
|
|
296
|
-
return this.resolve(specifier, module.dirname, {
|
|
319
|
+
return this.resolve(specifier, module.dirname, {
|
|
320
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
321
|
+
imports,
|
|
322
|
+
referrer
|
|
323
|
+
})
|
|
297
324
|
}
|
|
298
325
|
|
|
299
326
|
const require = (specifier) => {
|
|
300
|
-
|
|
327
|
+
const module = this.load(resolve(specifier), {
|
|
328
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
329
|
+
imports,
|
|
330
|
+
referrer
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
return module.exports
|
|
301
334
|
}
|
|
302
335
|
|
|
303
336
|
module._type = 'cjs'
|
|
@@ -404,4 +437,16 @@ Module._protocols['node:'] = new Protocol({
|
|
|
404
437
|
}
|
|
405
438
|
})
|
|
406
439
|
|
|
440
|
+
Module._protocols['data:'] = new Protocol({
|
|
441
|
+
* resolve (specifier) {
|
|
442
|
+
yield specifier
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
read (specifier) {
|
|
446
|
+
const [, , , base64, data] = specifier.match(/data:(?:([^/]+\/[^;,]+)(;[^=]+=[^;,]+)*)?(;base64)?,(.*)/)
|
|
447
|
+
|
|
448
|
+
return Buffer.from(decodeURIComponent(data), base64 ? 'base64' : 'ascii')
|
|
449
|
+
}
|
|
450
|
+
})
|
|
451
|
+
|
|
407
452
|
process.once('exit', () => binding.destroy(Module._context))
|
package/lib/protocol.js
CHANGED
|
@@ -2,11 +2,14 @@ module.exports = class Protocol {
|
|
|
2
2
|
constructor (opts = {}) {
|
|
3
3
|
const {
|
|
4
4
|
map = null,
|
|
5
|
+
resolve = null,
|
|
5
6
|
exists = null,
|
|
6
7
|
read = null
|
|
7
8
|
} = opts
|
|
8
9
|
|
|
9
10
|
if (map) this.map = map.bind(this)
|
|
11
|
+
if (resolve) this.resolve = resolve.bind(this)
|
|
12
|
+
else this.resolve = null
|
|
10
13
|
if (exists) this.exists = exists.bind(this)
|
|
11
14
|
if (read) this.read = read.bind(this)
|
|
12
15
|
}
|