@nxtedition/shared 0.0.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -10
- package/index.js +17 -8
- package/package.json +1 -1
- package/package-lock.json +0 -1513
package/README.md
CHANGED
|
@@ -25,13 +25,12 @@ const worker = new Worker(new URL('worker.js', import.meta.url), {
|
|
|
25
25
|
|
|
26
26
|
const writeToWorker = shared.writer(reader)
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
writeToWorker(Buffer.from('ping'))
|
|
29
|
+
|
|
30
|
+
for await (const buffer of shared.reader(writer)) {
|
|
29
31
|
console.log(`From worker ${buffer}`)
|
|
30
32
|
await tp.setTimeout(1e3) // Backpressure
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
while (true) {
|
|
34
|
-
await writeToParent(Buffer.from('Hello from parent')
|
|
33
|
+
writeToWorker(Buffer.from('pong'))
|
|
35
34
|
}
|
|
36
35
|
```
|
|
37
36
|
|
|
@@ -43,12 +42,9 @@ import tp from 'timers/promise'
|
|
|
43
42
|
|
|
44
43
|
const writeToParent = shared.writer(workerData.writer)
|
|
45
44
|
|
|
46
|
-
shared.reader(workerData.reader
|
|
45
|
+
for await (const buffer of shared.reader(workerData.reader)) {
|
|
47
46
|
console.log(`From parent ${buffer}`)
|
|
48
47
|
await tp.setTimeout(1e3) // Backpressure
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while (true) {
|
|
52
|
-
await writeToParent(Buffer.from('Hello from worker')
|
|
48
|
+
writeToWorker(Buffer.from('pong'))
|
|
53
49
|
}
|
|
54
50
|
```
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const WRITE_INDEX = 0
|
|
2
2
|
const READ_INDEX = 1
|
|
3
|
+
const END_OF_PACKET = -1
|
|
3
4
|
|
|
4
5
|
export function alloc(size) {
|
|
5
6
|
return {
|
|
@@ -8,7 +9,7 @@ export function alloc(size) {
|
|
|
8
9
|
}
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
12
13
|
const state = new Int32Array(sharedState)
|
|
13
14
|
const buffer = Buffer.from(sharedBuffer)
|
|
14
15
|
|
|
@@ -25,15 +26,15 @@ export async function reader({ sharedState, sharedBuffer }, cb) {
|
|
|
25
26
|
while (readPos !== writePos) {
|
|
26
27
|
const len = buffer.readInt32LE(readPos)
|
|
27
28
|
|
|
28
|
-
if (len ===
|
|
29
|
+
if (len === END_OF_PACKET) {
|
|
29
30
|
readPos = 0
|
|
30
31
|
} else {
|
|
31
32
|
const raw = buffer.slice(readPos + 4, readPos + len)
|
|
32
33
|
readPos += len
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
if (cb) {
|
|
35
|
+
cb(raw)
|
|
36
|
+
} else {
|
|
37
|
+
yield raw
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
|
|
@@ -44,6 +45,14 @@ export async function reader({ sharedState, sharedBuffer }, cb) {
|
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
export function reader(options, cb) {
|
|
49
|
+
if (cb) {
|
|
50
|
+
_reader(options, cb).next()
|
|
51
|
+
} else {
|
|
52
|
+
return _reader(options)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
47
56
|
export function writer({ sharedState, sharedBuffer }) {
|
|
48
57
|
const state = new Int32Array(sharedState)
|
|
49
58
|
const buffer = Buffer.from(sharedBuffer)
|
|
@@ -102,14 +111,14 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
102
111
|
flushing = null
|
|
103
112
|
}
|
|
104
113
|
|
|
105
|
-
|
|
114
|
+
function write(...raw) {
|
|
106
115
|
if (!queue.length && tryWrite(...raw)) {
|
|
107
116
|
return
|
|
108
117
|
}
|
|
109
118
|
|
|
110
119
|
queue.push(Buffer.concat(raw))
|
|
111
120
|
|
|
112
|
-
|
|
121
|
+
return (flushing ??= flush())
|
|
113
122
|
}
|
|
114
123
|
|
|
115
124
|
return write
|