Skip to content

Commit 65cd34e

Browse files
committed
linted
Signed-off-by: Matteo Collina <[email protected]>
1 parent a409c2a commit 65cd34e

File tree

7 files changed

+81
-60
lines changed

7 files changed

+81
-60
lines changed

doc/api/worker_threads.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,12 +1550,12 @@ const { Worker, makeSync } = require('node:worker_threads');
15501550

15511551
// Create a SharedArrayBuffer for communication
15521552
const buffer = new SharedArrayBuffer(1024, {
1553-
maxByteLength: 64 * 1024 * 1024
1553+
maxByteLength: 64 * 1024 * 1024,
15541554
});
15551555

15561556
// Create a worker, passing the buffer
15571557
const worker = new Worker('worker-script.js', {
1558-
workerData: { buffer }
1558+
workerData: { buffer },
15591559
});
15601560

15611561
// Create a synchronous API facade
@@ -1591,7 +1591,7 @@ wire(workerData.buffer, {
15911591
syncMethod(arg) {
15921592
// Do synchronous work
15931593
return result;
1594-
}
1594+
},
15951595
});
15961596
```
15971597
Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
'use strict';
22

3+
const {
4+
AtomicsNotify,
5+
AtomicsStore,
6+
AtomicsWait,
7+
AtomicsWaitAsync,
8+
Int32Array,
9+
ObjectKeys,
10+
} = primordials;
11+
12+
const {
13+
codes: {
14+
ERR_WORKER_MESSAGING_TIMEOUT,
15+
},
16+
} = require('internal/errors');
17+
318
const { read, write } = require('internal/worker/everysync/objects');
419
const {
520
OFFSET,
@@ -11,18 +26,17 @@ const {
1126
* Creates a synchronous API facade from a shared memory buffer.
1227
* This function is meant to be used in the main thread to communicate with
1328
* a worker thread that has called `wire()` on the same shared memory.
14-
*
1529
* @param {SharedArrayBuffer} data - The shared memory buffer for communication
16-
* @param {Object} [opts={}] - Options object
30+
* @param {object} [opts={}] - Options object
1731
* @param {number} [opts.timeout=1000] - Timeout in milliseconds for synchronous operations
18-
* @returns {Object} - An object with methods that match the ones exposed by the worker
32+
* @returns {object} - An object with methods that match the ones exposed by the worker
1933
*/
2034
function makeSync(data, opts = {}) {
2135
const timeout = opts.timeout || 1000;
2236
const metaView = new Int32Array(data);
2337

24-
const res = Atomics.wait(metaView, TO_WORKER, 0, timeout);
25-
Atomics.store(metaView, TO_WORKER, 0);
38+
const res = AtomicsWait(metaView, TO_WORKER, 0, timeout);
39+
AtomicsStore(metaView, TO_WORKER, 0);
2640

2741
if (res === 'ok') {
2842
const obj = read(data, OFFSET);
@@ -31,59 +45,56 @@ function makeSync(data, opts = {}) {
3145
for (const key of obj) {
3246
api[key] = (...args) => {
3347
write(data, { key, args }, OFFSET);
34-
Atomics.store(metaView, TO_MAIN, 1);
35-
Atomics.notify(metaView, TO_MAIN, 1);
36-
const res = Atomics.wait(metaView, TO_WORKER, 0, timeout);
37-
Atomics.store(metaView, TO_WORKER, 0);
48+
AtomicsStore(metaView, TO_MAIN, 1);
49+
AtomicsNotify(metaView, TO_MAIN, 1);
50+
const res = AtomicsWait(metaView, TO_WORKER, 0, timeout);
51+
AtomicsStore(metaView, TO_WORKER, 0);
3852
if (res === 'ok') {
3953
const obj = read(data, OFFSET);
4054
return obj;
41-
} else {
42-
throw new Error(`The response timed out after ${timeout}ms`);
4355
}
56+
throw new ERR_WORKER_MESSAGING_TIMEOUT();
4457
};
4558
}
4659

4760
return api;
48-
} else {
49-
throw new Error(`The initialization timed out after ${timeout}ms`);
5061
}
62+
throw new ERR_WORKER_MESSAGING_TIMEOUT();
5163
}
5264

5365
/**
5466
* Wires up a shared memory buffer to invoke methods on an object.
5567
* This function is meant to be used in a worker thread to expose methods
5668
* to the main thread that has called `makeSync()` on the same shared memory.
57-
*
5869
* @param {SharedArrayBuffer} data - The shared memory buffer for communication
59-
* @param {Object} obj - Object with methods to expose to the main thread
70+
* @param {object} obj - Object with methods to expose to the main thread
6071
* @returns {Promise<void>} - A promise that never resolves unless there's an error
6172
*/
6273
async function wire(data, obj) {
63-
write(data, Object.keys(obj), OFFSET);
74+
write(data, ObjectKeys(obj), OFFSET);
6475

6576
const metaView = new Int32Array(data);
6677

67-
Atomics.store(metaView, TO_WORKER, 1);
68-
Atomics.notify(metaView, TO_WORKER);
78+
AtomicsStore(metaView, TO_WORKER, 1);
79+
AtomicsNotify(metaView, TO_WORKER);
6980

7081
while (true) {
71-
const waitAsync = Atomics.waitAsync(metaView, TO_MAIN, 0);
82+
const waitAsync = AtomicsWaitAsync(metaView, TO_MAIN, 0);
7283
const res = await waitAsync.value;
73-
Atomics.store(metaView, TO_MAIN, 0);
84+
AtomicsStore(metaView, TO_MAIN, 0);
7485

7586
if (res === 'ok') {
7687
const { key, args } = read(data, OFFSET);
7788
// This is where the magic happens - invoke the requested method
7889
const result = await obj[key](...args);
7990
write(data, result, OFFSET);
80-
Atomics.store(metaView, TO_WORKER, 1);
81-
Atomics.notify(metaView, TO_WORKER, 1);
91+
AtomicsStore(metaView, TO_WORKER, 1);
92+
AtomicsNotify(metaView, TO_WORKER, 1);
8293
}
8394
}
8495
}
8596

8697
module.exports = {
8798
makeSync,
88-
wire
89-
};
99+
wire,
100+
};

lib/internal/worker/everysync/indexes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ const TO_MAIN = 1;
2323
module.exports = {
2424
OFFSET,
2525
TO_WORKER,
26-
TO_MAIN
27-
};
26+
TO_MAIN,
27+
};

lib/internal/worker/everysync/objects.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
'use strict';
22

3+
const {
4+
DataView,
5+
Uint8Array,
6+
} = primordials;
7+
8+
const {
9+
codes: {
10+
ERR_INVALID_BUFFER_SIZE,
11+
},
12+
} = require('internal/errors');
13+
314
const { serialize, deserialize } = require('v8');
415

516
/**
617
* Reads an object from a shared memory buffer
7-
*
818
* @param {SharedArrayBuffer} buffer - The shared memory buffer containing serialized data
919
* @param {number} [byteOffset=0] - Byte offset where the data begins
1020
* @returns {any} - The deserialized object
@@ -18,7 +28,6 @@ function read(buffer, byteOffset = 0) {
1828

1929
/**
2030
* Writes an object to a shared memory buffer
21-
*
2231
* @param {SharedArrayBuffer} buffer - The shared memory buffer to write to
2332
* @param {any} object - The object to serialize and write
2433
* @param {number} [byteOffset=0] - Byte offset where to write the data
@@ -30,18 +39,17 @@ function write(buffer, object, byteOffset = 0) {
3039
if (buffer.byteLength < data.byteLength + 4 + byteOffset) {
3140
// Check if buffer is growable (has grow method from ShareArrayBuffer.prototype)
3241
if (typeof buffer.grow !== 'function') {
33-
throw new Error('Buffer is too small and not growable');
42+
throw new ERR_INVALID_BUFFER_SIZE('Buffer is too small and not growable');
3443
}
35-
3644
buffer.grow(data.byteLength + 4 + byteOffset);
3745
}
38-
46+
3947
const view = new DataView(buffer, byteOffset);
4048
view.setUint32(0, data.byteLength, true);
4149
new Uint8Array(buffer, byteOffset + 4).set(data);
4250
}
4351

4452
module.exports = {
4553
read,
46-
write
47-
};
54+
write,
55+
};

lib/worker_threads.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {
3131

3232
const {
3333
makeSync,
34-
wire
34+
wire,
3535
} = require('internal/worker/everysync/index');
3636

3737
module.exports = {
@@ -55,5 +55,5 @@ module.exports = {
5555
setEnvironmentData,
5656
getEnvironmentData,
5757
makeSync,
58-
wire
58+
wire,
5959
};

test/parallel/test-worker-everysync-base.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
// eslint-disable-next-line no-unused-vars
34
const common = require('../common');
45
const assert = require('assert');
56
const { join } = require('path');
@@ -39,7 +40,7 @@ const { Worker, makeSync } = require('worker_threads');
3940
const api = makeSync(buffer, { timeout: 100 });
4041

4142
assert.throws(() => api.fail(), {
42-
message: 'The response timed out after 100ms'
43+
code: 'ERR_WORKER_MESSAGING_TIMEOUT'
4344
});
4445

4546
worker.terminate();
@@ -52,6 +53,6 @@ const { Worker, makeSync } = require('worker_threads');
5253
});
5354

5455
assert.throws(() => makeSync(buffer, { timeout: 100 }), {
55-
message: 'The initialization timed out after 100ms'
56+
code: 'ERR_WORKER_MESSAGING_TIMEOUT'
5657
});
5758
}

test/parallel/test-worker-everysync-objects.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Flags: --expose-internals
22
'use strict';
33

4+
// eslint-disable-next-line no-unused-vars
45
const common = require('../common');
56
const assert = require('assert');
67
const { read, write } = require('internal/worker/everysync/objects');
@@ -32,27 +33,27 @@ const { read, write } = require('internal/worker/everysync/objects');
3233
});
3334
}
3435

35-
// Test with growable buffer
36-
{
37-
const obj = { foo: 'bar' };
38-
const buffer = new SharedArrayBuffer(2, {
39-
maxByteLength: 1024,
40-
});
41-
write(buffer, obj);
42-
const obj2 = read(buffer);
43-
assert.deepStrictEqual(obj, obj2);
44-
}
36+
// Test with growable buffer
37+
{
38+
const obj = { foo: 'bar' };
39+
const buffer = new SharedArrayBuffer(2, {
40+
maxByteLength: 1024,
41+
});
42+
write(buffer, obj);
43+
const obj2 = read(buffer);
44+
assert.deepStrictEqual(obj, obj2);
45+
}
4546

46-
// Test with growable buffer and offset
47-
{
48-
const obj = { foo: 'bar' };
49-
const buffer = new SharedArrayBuffer(2, {
50-
maxByteLength: 1024,
51-
});
52-
write(buffer, obj, 4);
53-
const obj2 = read(buffer, 4);
54-
assert.deepStrictEqual(obj, obj2);
55-
}
47+
// Test with growable buffer and offset
48+
{
49+
const obj = { foo: 'bar' };
50+
const buffer = new SharedArrayBuffer(2, {
51+
maxByteLength: 1024,
52+
});
53+
write(buffer, obj, 4);
54+
const obj2 = read(buffer, 4);
55+
assert.deepStrictEqual(obj, obj2);
56+
}
5657

5758
// Test complex objects
5859
{

0 commit comments

Comments
 (0)