Skip to content

Commit b8ef1b4

Browse files
authoredMar 8, 2023
test: add coverage for custom loader hooks with permission model
PR-URL: #46977 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 0c46051 commit b8ef1b4

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
 

‎test/es-module/test-esm-loader-hooks.mjs

+52
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,56 @@ describe('Loader hooks', { concurrency: true }, () => {
113113
assert.strictEqual(signal, null);
114114
});
115115
});
116+
117+
it('should work without worker permission', async () => {
118+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
119+
'--no-warnings',
120+
'--experimental-permission',
121+
'--allow-fs-read',
122+
'*',
123+
'--experimental-loader',
124+
fixtures.fileURL('empty.js'),
125+
fixtures.path('es-modules/esm-top-level-await.mjs'),
126+
]);
127+
128+
assert.strictEqual(stderr, '');
129+
assert.match(stdout, /^1\r?\n2\r?\n$/);
130+
assert.strictEqual(code, 0);
131+
assert.strictEqual(signal, null);
132+
});
133+
134+
it('should allow loader hooks to spawn workers when allowed by the CLI flags', async () => {
135+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
136+
'--no-warnings',
137+
'--experimental-permission',
138+
'--allow-worker',
139+
'--allow-fs-read',
140+
'*',
141+
'--experimental-loader',
142+
`data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`,
143+
fixtures.path('es-modules/esm-top-level-await.mjs'),
144+
]);
145+
146+
assert.strictEqual(stderr, '');
147+
assert.match(stdout, /^1\r?\n2\r?\n$/);
148+
assert.strictEqual(code, 0);
149+
assert.strictEqual(signal, null);
150+
});
151+
152+
it('should not allow loader hooks to spawn workers if restricted by the CLI flags', async () => {
153+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
154+
'--no-warnings',
155+
'--experimental-permission',
156+
'--allow-fs-read',
157+
'*',
158+
'--experimental-loader',
159+
`data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`,
160+
fixtures.path('es-modules/esm-top-level-await.mjs'),
161+
]);
162+
163+
assert.match(stderr, /code: 'ERR_ACCESS_DENIED'/);
164+
assert.strictEqual(stdout, '');
165+
assert.strictEqual(code, 1);
166+
assert.strictEqual(signal, null);
167+
});
116168
});

0 commit comments

Comments
 (0)
Please sign in to comment.