Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/internal/locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
const {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_THIS,
} = require('internal/errors');
} = require('internal/errors').codes;
const {
kEmptyObject,
lazyDOMException,
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-web-locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down