GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — worker: restrict supported extensions · nodejs/node@c97fb91

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • errors.js
    • worker.js
  • test/parallel
    • test-worker-unsupported-path.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/errors.js‎

Copy file name to clipboardExpand all lines: lib/internal/errors.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,4 +856,7 @@ E('ERR_WORKER_NEED_ABSOLUTE_PATH',
856856
TypeError);
857857
E('ERR_WORKER_UNSERIALIZABLE_ERROR',
858858
'Serializing an uncaught exception failed', Error);
859+
E('ERR_WORKER_UNSUPPORTED_EXTENSION',
860+
'The worker script extension must be ".js" or ".mjs". Received "%s"',
861+
TypeError);
859862
E('ERR_ZLIB_INITIALIZATION_FAILED', 'Initialization failed', Error);
Collapse file

‎lib/internal/worker.js‎

Copy file name to clipboardExpand all lines: lib/internal/worker.js
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const util = require('util');
88
const {
99
ERR_INVALID_ARG_TYPE,
1010
ERR_WORKER_NEED_ABSOLUTE_PATH,
11-
ERR_WORKER_UNSERIALIZABLE_ERROR
11+
ERR_WORKER_UNSERIALIZABLE_ERROR,
12+
ERR_WORKER_UNSUPPORTED_EXTENSION,
1213
} = require('internal/errors').codes;
1314

1415
const { internalBinding } = require('internal/bootstrap/loaders');
@@ -136,8 +137,14 @@ class Worker extends EventEmitter {
136137
throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename);
137138
}
138139

139-
if (!options.eval && !path.isAbsolute(filename)) {
140-
throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename);
140+
if (!options.eval) {
141+
if (!path.isAbsolute(filename)) {
142+
throw new ERR_WORKER_NEED_ABSOLUTE_PATH(filename);
143+
}
144+
const ext = path.extname(filename);
145+
if (ext !== '.js' && ext !== '.mjs') {
146+
throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext);
147+
}
141148
}
142149

143150
// Set up the C++ handle for the worker, as well as some internal wiring.
Collapse file

‎test/parallel/test-worker-unsupported-path.js‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Flags: --experimental-worker
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const { Worker } = require('worker');
7+
8+
{
9+
const expectedErr = common.expectsError({
10+
code: 'ERR_WORKER_NEED_ABSOLUTE_PATH',
11+
type: TypeError
12+
}, 4);
13+
assert.throws(() => { new Worker('a.js'); }, expectedErr);
14+
assert.throws(() => { new Worker('b'); }, expectedErr);
15+
assert.throws(() => { new Worker('c/d.js'); }, expectedErr);
16+
assert.throws(() => { new Worker('a.mjs'); }, expectedErr);
17+
}
18+
19+
{
20+
const expectedErr = common.expectsError({
21+
code: 'ERR_WORKER_UNSUPPORTED_EXTENSION',
22+
type: TypeError
23+
}, 3);
24+
assert.throws(() => { new Worker('/b'); }, expectedErr);
25+
assert.throws(() => { new Worker('/c.wasm'); }, expectedErr);
26+
assert.throws(() => { new Worker('/d.txt'); }, expectedErr);
27+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.