worker: fix error imports and receiver check in Web Locks

Signed-off-by: Yaroslav Chuhai <yaroslavchuhai@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67717
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Yaroslav Chuhai authored and GitHub committed 2026-11-26 19:16:17 +00:00
1 parent fa9600147d
commit c698e79e2e
2 files changed
+37 -1

No files matched your search

+5 -1
View File
@@ -13,7 +13,7 @@ const {
const {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_THIS,
} = require('internal/errors');
} = require('internal/errors').codes;
const {
kEmptyObject,
lazyDOMException,
@@ -165,6 +165,10 @@ class LockManager {
*/
// https://w3c.github.io/web-locks/#api-lock-manager-request
async request(name, options, callback = undefined) {
if (!(this instanceof LockManager)) {
throw new ERR_INVALID_THIS('LockManager');
}
if (callback === undefined) {
callback = options;
options = undefined;
+32
View File
@@ -190,6 +190,38 @@ describe('Web Locks with worker threads', () => {
}));
});
it('should throw proper errors on illegal constructor and invalid receiver', async () => {
const LockManager = navigator.locks.constructor;
assert.throws(
() => new LockManager(),
{ code: 'ERR_ILLEGAL_CONSTRUCTOR' },
);
await assert.rejects(
LockManager.prototype.request.call({}, 'test', common.mustNotCall()),
{ code: 'ERR_INVALID_THIS' },
);
await assert.rejects(
LockManager.prototype.query.call({}),
{ code: 'ERR_INVALID_THIS' },
);
await navigator.locks.request('receiver-test', common.mustCall((lock) => {
const Lock = lock.constructor;
assert.throws(
() => new Lock(),
{ code: 'ERR_ILLEGAL_CONSTRUCTOR' },
);
assert.throws(
() => Reflect.get(Lock.prototype, 'name', {}),
{ code: 'ERR_INVALID_THIS' },
);
assert.throws(
() => Reflect.get(Lock.prototype, 'mode', {}),
{ code: 'ERR_INVALID_THIS' },
);
}));
});
it('should clean up when worker is terminated with a pending lock', async () => {
// Acquire the lock in the main thread so that the worker's request will be pending
await navigator.locks.request('cleanup-test', common.mustCall(async () => {