@nxtedition/shared 1.2.1 → 1.3.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/index.js +56 -29
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -31,7 +31,7 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
|
|
|
31
31
|
if (len === END_OF_PACKET) {
|
|
32
32
|
readPos = 0
|
|
33
33
|
} else {
|
|
34
|
-
const raw = buffer.
|
|
34
|
+
const raw = buffer.subarray(readPos + 4, readPos + len)
|
|
35
35
|
readPos += len
|
|
36
36
|
if (cb) {
|
|
37
37
|
const thenable = cb(raw)
|
|
@@ -68,24 +68,9 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
68
68
|
let writePos = 0
|
|
69
69
|
let flushing = null
|
|
70
70
|
|
|
71
|
-
function tryWrite(
|
|
72
|
-
if (Array.isArray(raw[0])) {
|
|
73
|
-
raw = raw[0]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (!raw.length) {
|
|
77
|
-
return true
|
|
78
|
-
}
|
|
79
|
-
|
|
71
|
+
function tryWrite(maxLen, fn, opaque) {
|
|
80
72
|
readPos = Atomics.load(state, READ_INDEX)
|
|
81
73
|
|
|
82
|
-
let maxLen = 4
|
|
83
|
-
for (const buf of raw) {
|
|
84
|
-
maxLen += buf.byteLength ?? buf.length * 3
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
assert(maxLen < size / 2)
|
|
88
|
-
|
|
89
74
|
if (size - writePos < maxLen + 4) {
|
|
90
75
|
if (readPos < maxLen + 4) {
|
|
91
76
|
return false
|
|
@@ -104,14 +89,7 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
104
89
|
const lenPos = writePos
|
|
105
90
|
writePos += 4
|
|
106
91
|
|
|
107
|
-
|
|
108
|
-
if (typeof buf === 'string') {
|
|
109
|
-
writePos += buffer.write(buf, writePos)
|
|
110
|
-
} else {
|
|
111
|
-
buffer.set(buf, writePos)
|
|
112
|
-
writePos += buf.byteLength
|
|
113
|
-
}
|
|
114
|
-
}
|
|
92
|
+
writePos += fn(buffer.subarray(writePos, writePos + maxLen), opaque)
|
|
115
93
|
|
|
116
94
|
const len = writePos - lenPos
|
|
117
95
|
|
|
@@ -127,7 +105,17 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
127
105
|
|
|
128
106
|
async function flush() {
|
|
129
107
|
while (queue.length) {
|
|
130
|
-
|
|
108
|
+
const buf = queue[0]
|
|
109
|
+
while (
|
|
110
|
+
!tryWrite(
|
|
111
|
+
buf.byteLength,
|
|
112
|
+
(dst, buf) => {
|
|
113
|
+
dst.set(buf)
|
|
114
|
+
return buf.byteLength
|
|
115
|
+
},
|
|
116
|
+
queue[0]
|
|
117
|
+
)
|
|
118
|
+
) {
|
|
131
119
|
const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
|
|
132
120
|
if (async) {
|
|
133
121
|
await value
|
|
@@ -139,12 +127,51 @@ export function writer({ sharedState, sharedBuffer }) {
|
|
|
139
127
|
flushing = null
|
|
140
128
|
}
|
|
141
129
|
|
|
142
|
-
function write(...
|
|
143
|
-
if (!
|
|
130
|
+
function write(...args) {
|
|
131
|
+
if (!args.length) {
|
|
132
|
+
return true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let len
|
|
136
|
+
let fn
|
|
137
|
+
let opaque
|
|
138
|
+
|
|
139
|
+
if (Number.isInteger(args[0])) {
|
|
140
|
+
len = args[0]
|
|
141
|
+
fn = args[1]
|
|
142
|
+
opaque = args[2]
|
|
143
|
+
} else {
|
|
144
|
+
if (Array.isArray(args[0])) {
|
|
145
|
+
args = args[0]
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
len = 4
|
|
149
|
+
for (const buf of args) {
|
|
150
|
+
len += buf.byteLength ?? buf.length * 3
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
fn = (dst, data) => {
|
|
154
|
+
let pos = 0
|
|
155
|
+
for (const buf of data) {
|
|
156
|
+
if (typeof buf === 'string') {
|
|
157
|
+
pos += dst.write(buf, pos)
|
|
158
|
+
} else {
|
|
159
|
+
dst.set(buf, pos)
|
|
160
|
+
pos += buf.byteLength
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return pos
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
opaque = args
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!queue.length && tryWrite(len, fn, opaque)) {
|
|
144
170
|
return
|
|
145
171
|
}
|
|
146
172
|
|
|
147
|
-
|
|
173
|
+
const buf = Buffer.allocUnsafe(len)
|
|
174
|
+
queue.push(buf.subarray(0, fn(0, buf)))
|
|
148
175
|
|
|
149
176
|
return (flushing ??= flush())
|
|
150
177
|
}
|