@nxtedition/shared 2.0.1 → 2.0.4
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 +38 -29
- 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,22 +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
|
|
30
|
-
|
|
28
|
+
const tag = buffer.readInt32LE(readPos)
|
|
29
|
+
readPos += 4
|
|
31
30
|
|
|
32
|
-
if (
|
|
31
|
+
if (tag === -1) {
|
|
33
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)
|
|
34
48
|
} else {
|
|
35
|
-
|
|
36
|
-
readPos += len
|
|
37
|
-
if (cb) {
|
|
38
|
-
const thenable = cb(raw)
|
|
39
|
-
if (thenable && typeof thenable.then === 'function') {
|
|
40
|
-
await thenable
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
yield raw
|
|
44
|
-
}
|
|
49
|
+
yield raw
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
Atomics.store(state, READ_INDEX, readPos)
|
|
@@ -68,12 +73,12 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
68
73
|
let writePos = 0
|
|
69
74
|
|
|
70
75
|
function tryWrite(maxLen, fn, opaque) {
|
|
71
|
-
|
|
76
|
+
assert(maxLen < size - 12)
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
readPos = Atomics.load(state, READ_INDEX)
|
|
74
79
|
|
|
75
|
-
if (size - writePos < maxLen +
|
|
76
|
-
if (readPos < maxLen +
|
|
80
|
+
if (size - writePos < maxLen + 12) {
|
|
81
|
+
if (readPos < maxLen + 12) {
|
|
77
82
|
return false
|
|
78
83
|
}
|
|
79
84
|
|
|
@@ -82,17 +87,19 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
82
87
|
} else {
|
|
83
88
|
const available = writePos >= readPos ? size - writePos : readPos - writePos
|
|
84
89
|
|
|
85
|
-
if (available < maxLen +
|
|
90
|
+
if (available < maxLen + 12) {
|
|
86
91
|
return false
|
|
87
92
|
}
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
|
|
95
|
+
buffer.writeInt32LE(-2, writePos)
|
|
91
96
|
writePos += 4
|
|
92
97
|
|
|
93
|
-
|
|
98
|
+
const lenPos = writePos
|
|
99
|
+
writePos += 4
|
|
94
100
|
|
|
95
|
-
const len = writePos
|
|
101
|
+
const len = fn(buffer.subarray(writePos, writePos + maxLen), opaque)
|
|
102
|
+
writePos += len
|
|
96
103
|
|
|
97
104
|
assert(len <= maxLen)
|
|
98
105
|
|
|
@@ -105,8 +112,8 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
105
112
|
}
|
|
106
113
|
|
|
107
114
|
async function _write(len, fn, opaque) {
|
|
108
|
-
|
|
109
|
-
buf.subarray(0, fn(buf, opaque))
|
|
115
|
+
let buf = Buffer.allocUnsafe(len)
|
|
116
|
+
buf = buf.subarray(0, fn(buf, opaque))
|
|
110
117
|
|
|
111
118
|
while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
|
|
112
119
|
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
@@ -130,15 +137,17 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
130
137
|
fn = args[1]
|
|
131
138
|
opaque = args[2]
|
|
132
139
|
|
|
133
|
-
assert(len
|
|
140
|
+
assert(len >= 0)
|
|
134
141
|
assert(typeof fn === 'function')
|
|
135
142
|
} else {
|
|
136
143
|
if (Array.isArray(args[0])) {
|
|
137
144
|
args = args[0]
|
|
138
145
|
}
|
|
139
146
|
|
|
147
|
+
const data = args
|
|
148
|
+
|
|
140
149
|
len = 0
|
|
141
|
-
for (const buf of
|
|
150
|
+
for (const buf of data) {
|
|
142
151
|
len += buf.byteLength ?? buf.length * 3
|
|
143
152
|
}
|
|
144
153
|
|
|
@@ -148,14 +157,14 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
148
157
|
if (typeof buf === 'string') {
|
|
149
158
|
pos += dst.write(buf, pos)
|
|
150
159
|
} else {
|
|
151
|
-
|
|
152
|
-
pos += buf.byteLength
|
|
160
|
+
pos += buf.copy(dst, pos)
|
|
153
161
|
}
|
|
154
162
|
}
|
|
163
|
+
assert(pos <= len)
|
|
155
164
|
return pos
|
|
156
165
|
}
|
|
157
166
|
|
|
158
|
-
opaque =
|
|
167
|
+
opaque = data
|
|
159
168
|
}
|
|
160
169
|
|
|
161
170
|
if (!tryWrite(len, fn, opaque)) {
|