@nxtedition/shared 2.0.0 → 2.0.3
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/index.js +32 -21
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,7 +2,6 @@ import assert from 'node:assert'
|
|
|
2
2
|
|
|
3
3
|
const WRITE_INDEX = 0
|
|
4
4
|
const READ_INDEX = 1
|
|
5
|
-
const END_OF_PACKET = -1
|
|
6
5
|
|
|
7
6
|
export function alloc(size) {
|
|
8
7
|
return {
|
|
@@ -26,21 +25,28 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
|
26
25
|
writePos = Atomics.load(state, WRITE_INDEX)
|
|
27
26
|
|
|
28
27
|
while (readPos !== writePos) {
|
|
29
|
-
const
|
|
28
|
+
const tag = buffer.readInt32LE(readPos)
|
|
29
|
+
readPos += 4
|
|
30
30
|
|
|
31
|
-
if (
|
|
31
|
+
if (tag === -1) {
|
|
32
32
|
readPos = 0
|
|
33
|
+
continue
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
assert.equal(tag, -2)
|
|
37
|
+
|
|
38
|
+
const len = buffer.readInt32LE(readPos)
|
|
39
|
+
readPos += 4
|
|
40
|
+
|
|
41
|
+
assert(len > 0)
|
|
42
|
+
|
|
43
|
+
const raw = buffer.subarray(readPos, readPos + len)
|
|
44
|
+
readPos += len
|
|
45
|
+
|
|
46
|
+
if (cb) {
|
|
47
|
+
await cb(raw)
|
|
33
48
|
} else {
|
|
34
|
-
|
|
35
|
-
readPos += len
|
|
36
|
-
if (cb) {
|
|
37
|
-
const thenable = cb(raw)
|
|
38
|
-
if (thenable && typeof thenable.then === 'function') {
|
|
39
|
-
await thenable
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
yield raw
|
|
43
|
-
}
|
|
49
|
+
yield raw
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
Atomics.store(state, READ_INDEX, readPos)
|
|
@@ -67,12 +73,12 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
67
73
|
let writePos = 0
|
|
68
74
|
|
|
69
75
|
function tryWrite(maxLen, fn, opaque) {
|
|
70
|
-
|
|
76
|
+
assert(maxLen < size - 12)
|
|
71
77
|
|
|
72
|
-
|
|
78
|
+
readPos = Atomics.load(state, READ_INDEX)
|
|
73
79
|
|
|
74
|
-
if (size - writePos < maxLen +
|
|
75
|
-
if (readPos < maxLen +
|
|
80
|
+
if (size - writePos < maxLen + 12) {
|
|
81
|
+
if (readPos < maxLen + 12) {
|
|
76
82
|
return false
|
|
77
83
|
}
|
|
78
84
|
|
|
@@ -81,17 +87,19 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
81
87
|
} else {
|
|
82
88
|
const available = writePos >= readPos ? size - writePos : readPos - writePos
|
|
83
89
|
|
|
84
|
-
if (available < maxLen +
|
|
90
|
+
if (available < maxLen + 12) {
|
|
85
91
|
return false
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
94
|
|
|
89
|
-
|
|
95
|
+
buffer.writeInt32LE(-2, writePos)
|
|
90
96
|
writePos += 4
|
|
91
97
|
|
|
92
|
-
|
|
98
|
+
const lenPos = writePos
|
|
99
|
+
writePos += 4
|
|
93
100
|
|
|
94
|
-
const len = writePos
|
|
101
|
+
const len = fn(buffer.subarray(writePos, writePos + maxLen), opaque)
|
|
102
|
+
writePos += len
|
|
95
103
|
|
|
96
104
|
assert(len <= maxLen)
|
|
97
105
|
|
|
@@ -128,6 +136,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
128
136
|
len = args[0]
|
|
129
137
|
fn = args[1]
|
|
130
138
|
opaque = args[2]
|
|
139
|
+
|
|
140
|
+
assert(len > 0)
|
|
141
|
+
assert(typeof fn === 'function')
|
|
131
142
|
} else {
|
|
132
143
|
if (Array.isArray(args[0])) {
|
|
133
144
|
args = args[0]
|