@nxtedition/shared 2.0.3 → 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.
Files changed (2) hide show
  1. package/index.js +10 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -38,7 +38,7 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
38
38
  const len = buffer.readInt32LE(readPos)
39
39
  readPos += 4
40
40
 
41
- assert(len > 0)
41
+ assert(len >= 0)
42
42
 
43
43
  const raw = buffer.subarray(readPos, readPos + len)
44
44
  readPos += len
@@ -112,8 +112,8 @@ export function writer({ sharedState, sharedBuffer }) {
112
112
  }
113
113
 
114
114
  async function _write(len, fn, opaque) {
115
- const buf = Buffer.allocUnsafe(len)
116
- buf.subarray(0, fn(buf, opaque))
115
+ let buf = Buffer.allocUnsafe(len)
116
+ buf = buf.subarray(0, fn(buf, opaque))
117
117
 
118
118
  while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
119
119
  const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
@@ -137,15 +137,17 @@ export function writer({ sharedState, sharedBuffer }) {
137
137
  fn = args[1]
138
138
  opaque = args[2]
139
139
 
140
- assert(len > 0)
140
+ assert(len >= 0)
141
141
  assert(typeof fn === 'function')
142
142
  } else {
143
143
  if (Array.isArray(args[0])) {
144
144
  args = args[0]
145
145
  }
146
146
 
147
+ const data = args
148
+
147
149
  len = 0
148
- for (const buf of args) {
150
+ for (const buf of data) {
149
151
  len += buf.byteLength ?? buf.length * 3
150
152
  }
151
153
 
@@ -155,14 +157,14 @@ export function writer({ sharedState, sharedBuffer }) {
155
157
  if (typeof buf === 'string') {
156
158
  pos += dst.write(buf, pos)
157
159
  } else {
158
- dst.set(buf, pos)
159
- pos += buf.byteLength
160
+ pos += buf.copy(dst, pos)
160
161
  }
161
162
  }
163
+ assert(pos <= len)
162
164
  return pos
163
165
  }
164
166
 
165
- opaque = args
167
+ opaque = data
166
168
  }
167
169
 
168
170
  if (!tryWrite(len, fn, opaque)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {