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