@nxtedition/shared 1.4.2 → 2.0.2
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 +15 -40
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -27,6 +27,7 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
|
27
27
|
|
|
28
28
|
while (readPos !== writePos) {
|
|
29
29
|
const len = buffer.readInt32LE(readPos)
|
|
30
|
+
assert(len > 0)
|
|
30
31
|
|
|
31
32
|
if (len === END_OF_PACKET) {
|
|
32
33
|
readPos = 0
|
|
@@ -34,10 +35,7 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
|
34
35
|
const raw = buffer.subarray(readPos + 4, readPos + len)
|
|
35
36
|
readPos += len
|
|
36
37
|
if (cb) {
|
|
37
|
-
|
|
38
|
-
if (thenable && typeof thenable.then === 'function') {
|
|
39
|
-
await thenable
|
|
40
|
-
}
|
|
38
|
+
await cb(raw)
|
|
41
39
|
} else {
|
|
42
40
|
yield raw
|
|
43
41
|
}
|
|
@@ -62,11 +60,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
62
60
|
const state = new Int32Array(sharedState)
|
|
63
61
|
const buffer = Buffer.from(sharedBuffer)
|
|
64
62
|
const size = buffer.byteLength
|
|
65
|
-
const queue = []
|
|
66
63
|
|
|
67
64
|
let readPos = 0
|
|
68
65
|
let writePos = 0
|
|
69
|
-
let flushing = null
|
|
70
66
|
|
|
71
67
|
function tryWrite(maxLen, fn, opaque) {
|
|
72
68
|
readPos = Atomics.load(state, READ_INDEX)
|
|
@@ -105,35 +101,16 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
105
101
|
return true
|
|
106
102
|
}
|
|
107
103
|
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
return flushing
|
|
113
|
-
}
|
|
104
|
+
async function _write(len, fn, opaque) {
|
|
105
|
+
const buf = Buffer.allocUnsafe(len)
|
|
106
|
+
buf.subarray(0, fn(buf, opaque))
|
|
114
107
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
!tryWrite(
|
|
120
|
-
buf.byteLength,
|
|
121
|
-
(dst, buf) => {
|
|
122
|
-
dst.set(buf)
|
|
123
|
-
return buf.byteLength
|
|
124
|
-
},
|
|
125
|
-
buf
|
|
126
|
-
)
|
|
127
|
-
) {
|
|
128
|
-
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
129
|
-
if (async) {
|
|
130
|
-
await value
|
|
131
|
-
}
|
|
108
|
+
while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
|
|
109
|
+
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
110
|
+
if (async) {
|
|
111
|
+
await value
|
|
132
112
|
}
|
|
133
|
-
queue.shift()
|
|
134
113
|
}
|
|
135
|
-
|
|
136
|
-
flushing = null
|
|
137
114
|
}
|
|
138
115
|
|
|
139
116
|
function write(...args) {
|
|
@@ -149,6 +126,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
149
126
|
len = args[0]
|
|
150
127
|
fn = args[1]
|
|
151
128
|
opaque = args[2]
|
|
129
|
+
|
|
130
|
+
assert(len > 0)
|
|
131
|
+
assert(typeof fn === 'function')
|
|
152
132
|
} else {
|
|
153
133
|
if (Array.isArray(args[0])) {
|
|
154
134
|
args = args[0]
|
|
@@ -175,18 +155,13 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
175
155
|
opaque = args
|
|
176
156
|
}
|
|
177
157
|
|
|
178
|
-
if (!
|
|
179
|
-
return
|
|
158
|
+
if (!tryWrite(len, fn, opaque)) {
|
|
159
|
+
return _write(len, fn, opaque)
|
|
180
160
|
}
|
|
181
|
-
|
|
182
|
-
const buf = Buffer.allocUnsafe(len)
|
|
183
|
-
queue.push(buf.subarray(0, fn(buf, opaque)))
|
|
184
|
-
|
|
185
|
-
return flush()
|
|
186
161
|
}
|
|
187
162
|
|
|
188
163
|
write.write = write
|
|
189
|
-
write.flush =
|
|
164
|
+
write.flush = () => {}
|
|
190
165
|
|
|
191
166
|
return write
|
|
192
167
|
}
|