@nxtedition/shared 2.0.7 → 2.0.8

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 +25 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -66,18 +66,20 @@ export function reader(options, cb) {
66
66
  export function writer({ sharedState, sharedBuffer }) {
67
67
  const state = new Int32Array(sharedState)
68
68
  const buffer = Buffer.from(sharedBuffer)
69
- const size = buffer.byteLength
69
+ const size = buffer.byteLength - 64
70
70
 
71
71
  let readPos = 0
72
72
  let writePos = 0
73
73
 
74
- function tryWrite(maxLen, fn, opaque) {
75
- assert(maxLen < size - 12)
74
+ function _tryWrite(maxLen, fn, opaque) {
75
+ maxLen += 4
76
+
77
+ assert(maxLen <= size)
76
78
 
77
79
  readPos = Atomics.load(state, READ_INDEX)
78
80
 
79
- if (size - writePos < maxLen + 12) {
80
- if (readPos < maxLen + 12) {
81
+ if (size - writePos < maxLen) {
82
+ if (readPos < maxLen) {
81
83
  return false
82
84
  }
83
85
 
@@ -86,7 +88,7 @@ export function writer({ sharedState, sharedBuffer }) {
86
88
  } else {
87
89
  const available = writePos >= readPos ? size - writePos : readPos - writePos
88
90
 
89
- if (available < maxLen + 12) {
91
+ if (available < maxLen) {
90
92
  return false
91
93
  }
92
94
  }
@@ -98,7 +100,10 @@ export function writer({ sharedState, sharedBuffer }) {
98
100
  writePos += 4
99
101
 
100
102
  const len = fn(buffer.subarray(writePos, writePos + maxLen), opaque)
103
+ assert(len <= size)
104
+
101
105
  writePos += len
106
+ buffer.writeInt32LE(-3, writePos)
102
107
 
103
108
  assert(len <= maxLen)
104
109
 
@@ -110,11 +115,13 @@ export function writer({ sharedState, sharedBuffer }) {
110
115
  return true
111
116
  }
112
117
 
113
- async function _write(len, fn, opaque) {
114
- let buf = Buffer.allocUnsafe(len)
115
- buf = buf.subarray(0, fn(buf, opaque))
118
+ async function _write(maxLen, fn, opaque) {
119
+ assert(maxLen <= size)
116
120
 
117
- while (!tryWrite(len, (dst, buf) => buf.copy(dst), buf)) {
121
+ const buf = Buffer.allocUnsafe(maxLen)
122
+ const len = fn(buf, opaque)
123
+
124
+ while (!_tryWrite(len, (dst, buf) => buf.copy(dst, 0, 0, len))) {
118
125
  const { async, value } = Atomics.waitAsync(state, READ_INDEX, readPos)
119
126
  if (async) {
120
127
  await value
@@ -127,29 +134,23 @@ export function writer({ sharedState, sharedBuffer }) {
127
134
  return
128
135
  }
129
136
 
130
- let len
137
+ let maxLen
131
138
  let fn
132
139
  let opaque
133
140
 
134
141
  if (Number.isInteger(args[0])) {
135
- len = args[0]
142
+ maxLen = args[0]
136
143
  fn = args[1]
137
144
  opaque = args[2]
138
145
 
139
- assert(len >= 0)
146
+ assert(maxLen >= 0)
140
147
  assert(typeof fn === 'function')
141
148
  } else {
142
149
  if (Array.isArray(args[0])) {
143
150
  args = args[0]
144
151
  }
145
152
 
146
- const data = args
147
-
148
- len = 0
149
- for (const buf of data) {
150
- len += buf.byteLength ?? buf.length * 3
151
- }
152
-
153
+ maxLen = args.reduce((len, buf) => len + Buffer.byteLength(buf), 0)
153
154
  fn = (dst, data) => {
154
155
  let pos = 0
155
156
  for (const buf of data) {
@@ -159,15 +160,14 @@ export function writer({ sharedState, sharedBuffer }) {
159
160
  pos += buf.copy(dst, pos)
160
161
  }
161
162
  }
162
- assert(pos <= len)
163
+ assert(pos < maxLen)
163
164
  return pos
164
165
  }
165
-
166
- opaque = data
166
+ opaque = args
167
167
  }
168
168
 
169
- if (!tryWrite(len, fn, opaque)) {
170
- return _write(len, fn, opaque)
169
+ if (!_tryWrite(maxLen, fn, opaque)) {
170
+ return _write(maxLen, fn, opaque)
171
171
  }
172
172
  }
173
173
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {