@nxtedition/shared 1.1.1 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +16 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import assert from 'node:assert'
2
+
1
3
  const WRITE_INDEX = 0
2
4
  const READ_INDEX = 1
3
5
  const END_OF_PACKET = -1
@@ -77,13 +79,15 @@ export function writer({ sharedState, sharedBuffer }) {
77
79
 
78
80
  readPos = Atomics.load(state, READ_INDEX)
79
81
 
80
- let len = 4
82
+ let maxLen = 4
81
83
  for (const buf of raw) {
82
- len += buf.byteLength ?? Buffer.byteLength(buf)
84
+ maxLen += buf.byteLength ?? buf.length * 3
83
85
  }
84
86
 
85
- if (size - writePos < len + 4) {
86
- if (readPos < len + 4) {
87
+ assert(maxLen < size / 2)
88
+
89
+ if (size - writePos < maxLen + 4) {
90
+ if (readPos < maxLen + 4) {
87
91
  return false
88
92
  }
89
93
 
@@ -92,12 +96,12 @@ export function writer({ sharedState, sharedBuffer }) {
92
96
  } else {
93
97
  const available = writePos >= readPos ? size - writePos : readPos - writePos
94
98
 
95
- if (available < len + 4) {
99
+ if (available < maxLen + 4) {
96
100
  return false
97
101
  }
98
102
  }
99
103
 
100
- buffer.writeInt32LE(len, writePos)
104
+ const lenPos = writePos
101
105
  writePos += 4
102
106
 
103
107
  for (const buf of raw) {
@@ -109,6 +113,12 @@ export function writer({ sharedState, sharedBuffer }) {
109
113
  }
110
114
  }
111
115
 
116
+ const len = writePos - lenPos
117
+
118
+ assert(len <= maxLen)
119
+
120
+ buffer.writeInt32LE(len, lenPos)
121
+
112
122
  Atomics.store(state, WRITE_INDEX, writePos)
113
123
  Atomics.notify(state, WRITE_INDEX)
114
124
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {