@nxtedition/shared 2.0.6 → 2.0.9

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 +30 -39
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -33,13 +33,11 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
33
33
  continue
34
34
  }
35
35
 
36
- assert.equal(tag, -2)
36
+ assert(tag === -2, `tag: ${tag} === -2`)
37
37
 
38
38
  const len = buffer.readInt32LE(readPos)
39
39
  readPos += 4
40
40
 
41
- assert(len >= 0)
42
-
43
41
  const raw = buffer.subarray(readPos, readPos + len)
44
42
  readPos += len
45
43
 
@@ -50,9 +48,8 @@ async function* _reader({ sharedState, sharedBuffer }, cb) {
50
48
  }
51
49
 
52
50
  Atomics.store(state, READ_INDEX, readPos)
51
+ Atomics.notify(state, READ_INDEX)
53
52
  }
54
-
55
- Atomics.notify(state, READ_INDEX)
56
53
  }
57
54
  }
58
55
 
@@ -67,18 +64,20 @@ export function reader(options, cb) {
67
64
  export function writer({ sharedState, sharedBuffer }) {
68
65
  const state = new Int32Array(sharedState)
69
66
  const buffer = Buffer.from(sharedBuffer)
70
- const size = buffer.byteLength
67
+ const size = buffer.byteLength - 64
71
68
 
72
69
  let readPos = 0
73
70
  let writePos = 0
74
71
 
75
- function tryWrite(maxLen, fn, opaque) {
76
- assert(maxLen < size - 12)
72
+ function _tryWrite(maxLen, fn, opaque) {
73
+ maxLen += 4
74
+
75
+ assert(maxLen <= size)
77
76
 
78
77
  readPos = Atomics.load(state, READ_INDEX)
79
78
 
80
- if (size - writePos < maxLen + 12) {
81
- if (readPos < maxLen + 12) {
79
+ if (size - writePos < maxLen) {
80
+ if (readPos < maxLen) {
82
81
  return false
83
82
  }
84
83
 
@@ -87,7 +86,7 @@ export function writer({ sharedState, sharedBuffer }) {
87
86
  } else {
88
87
  const available = writePos >= readPos ? size - writePos : readPos - writePos
89
88
 
90
- if (available < maxLen + 12) {
89
+ if (available < maxLen) {
91
90
  return false
92
91
  }
93
92
  }
@@ -95,15 +94,13 @@ export function writer({ sharedState, sharedBuffer }) {
95
94
  buffer.writeInt32LE(-2, writePos)
96
95
  writePos += 4
97
96
 
98
- const lenPos = writePos
99
- writePos += 4
100
-
101
- const len = fn(buffer.subarray(writePos, writePos + maxLen), opaque)
102
- writePos += len
103
-
104
- assert(len <= maxLen)
97
+ const len = fn(buffer.subarray(writePos + 4, writePos + 4 + maxLen), opaque)
98
+ assert(len <= maxLen - 4, `len: ${len} <= maxLen: ${maxLen - 4}`)
99
+ assert(len <= size, `len: ${len} <= size: ${size}`)
105
100
 
106
- buffer.writeInt32LE(len, lenPos)
101
+ buffer.writeInt32LE(len, writePos)
102
+ writePos += 4 + len
103
+ buffer.writeInt32LE(-3, writePos)
107
104
 
108
105
  Atomics.store(state, WRITE_INDEX, writePos)
109
106
  Atomics.notify(state, WRITE_INDEX)
@@ -111,11 +108,13 @@ export function writer({ sharedState, sharedBuffer }) {
111
108
  return true
112
109
  }
113
110
 
114
- async function _write(len, fn, opaque) {
115
- let buf = Buffer.allocUnsafe(len)
116
- buf = buf.subarray(0, fn(buf, opaque))
111
+ async function _write(maxLen, fn, opaque) {
112
+ assert(maxLen <= size)
117
113
 
118
- while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
114
+ const buf = Buffer.allocUnsafe(maxLen)
115
+ const len = fn(buf, opaque)
116
+
117
+ while (!_tryWrite(len, (dst, buf) => buf.copy(dst, 0, 0, len))) {
119
118
  const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
120
119
  if (async) {
121
120
  await value
@@ -128,29 +127,23 @@ export function writer({ sharedState, sharedBuffer }) {
128
127
  return
129
128
  }
130
129
 
131
- let len
130
+ let maxLen
132
131
  let fn
133
132
  let opaque
134
133
 
135
134
  if (Number.isInteger(args[0])) {
136
- len = args[0]
135
+ maxLen = args[0]
137
136
  fn = args[1]
138
137
  opaque = args[2]
139
138
 
140
- assert(len >= 0)
141
- assert(typeof fn === 'function')
139
+ assert(maxLen >= 0, `maxLen: ${maxLen} >= 0`)
140
+ assert(typeof fn === 'function', `fn: ${typeof fn} === 'function`)
142
141
  } else {
143
142
  if (Array.isArray(args[0])) {
144
143
  args = args[0]
145
144
  }
146
145
 
147
- const data = args
148
-
149
- len = 0
150
- for (const buf of data) {
151
- len += buf.byteLength ?? buf.length * 3
152
- }
153
-
146
+ maxLen = args.reduce((len, buf) => len + Buffer.byteLength(buf), 0)
154
147
  fn = (dst, data) => {
155
148
  let pos = 0
156
149
  for (const buf of data) {
@@ -160,15 +153,13 @@ export function writer({ sharedState, sharedBuffer }) {
160
153
  pos += buf.copy(dst, pos)
161
154
  }
162
155
  }
163
- assert(pos <= len)
164
156
  return pos
165
157
  }
166
-
167
- opaque = data
158
+ opaque = args
168
159
  }
169
160
 
170
- if (!tryWrite(len, fn, opaque)) {
171
- return _write(len, fn, opaque)
161
+ if (!_tryWrite(maxLen, fn, opaque)) {
162
+ return _write(maxLen, fn, opaque)
172
163
  }
173
164
  }
174
165
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "2.0.6",
3
+ "version": "2.0.9",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {