forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-module-hooks-load-buffers.js
More file actions
42 lines (35 loc) · 1.15 KB
/
Copy pathtest-module-hooks-load-buffers.js
File metadata and controls
42 lines (35 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const common = require('../common');
const assert = require('assert');
const { registerHooks } = require('module');
// This tests that the source in the load hook can be returned as
// array buffers or array buffer views.
const arrayBufferSource = 'module.exports = "arrayBuffer"';
const arrayBufferViewSource = 'module.exports = "arrayBufferView"';
const encoder = new TextEncoder();
const hook1 = registerHooks({
resolve(specifier, context, nextResolve) {
return { shortCircuit: true, url: `test://${specifier}` };
},
load: common.mustNotCall(),
});
const hook2 = registerHooks({
load(url, context, nextLoad) {
if (url === 'test://array_buffer') {
return {
shortCircuit: true,
source: encoder.encode(arrayBufferSource).buffer,
};
} else if (url === 'test://array_buffer_view') {
return {
shortCircuit: true,
source: encoder.encode(arrayBufferViewSource),
};
}
assert.fail('unreachable');
},
});
assert.strictEqual(require('array_buffer'), 'arrayBuffer');
assert.strictEqual(require('array_buffer_view'), 'arrayBufferView');
hook1.deregister();
hook2.deregister();