@nxtedition/shared 2.0.15 → 2.0.16

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 +20 -26
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -66,6 +66,18 @@ export function reader(options, cb) {
66
66
  }
67
67
  }
68
68
 
69
+ function arrayWrite(dst, data) {
70
+ let pos = 0
71
+ for (const buf of data) {
72
+ if (typeof buf === 'string') {
73
+ pos += dst.write(buf, pos)
74
+ } else {
75
+ pos += buf.copy(dst, pos)
76
+ }
77
+ }
78
+ return pos
79
+ }
80
+
69
81
  export function writer({ sharedState, sharedBuffer }) {
70
82
  const state = new Int32Array(sharedState)
71
83
  const buffer = Buffer.from(sharedBuffer)
@@ -74,29 +86,23 @@ export function writer({ sharedState, sharedBuffer }) {
74
86
  let readPos = 0
75
87
  let writePos = 0
76
88
 
77
- return function write(...args) {
78
- if (!args.length) {
89
+ return function write(len, fn, opaque) {
90
+ if (!len) {
79
91
  return
80
92
  }
81
93
 
82
- let len
83
- let fn
84
-
85
- if (Number.isInteger(args[0])) {
86
- len = args.shift()
87
- fn = args.shift()
88
-
94
+ if (Number.isInteger(len)) {
89
95
  assert(len >= 0, `len: ${len} >= 0`)
90
96
  assert(typeof fn === 'function', `fn: ${typeof fn} === 'function`)
91
97
  } else {
92
- if (Array.isArray(args[0])) {
93
- args = args[0]
94
- }
98
+ opaque = Array.isArray(len) ? len : arguments
95
99
 
96
100
  len = 0
97
- for (const buf of args) {
101
+ for (const buf of opaque) {
98
102
  len += Buffer.byteLength(buf)
99
103
  }
104
+
105
+ fn = arrayWrite
100
106
  }
101
107
 
102
108
  assert(len <= size)
@@ -120,21 +126,9 @@ export function writer({ sharedState, sharedBuffer }) {
120
126
 
121
127
  buffer.writeInt32LE(-2, writePos)
122
128
  writePos += 4
123
-
124
129
  buffer.writeInt32LE(-3, writePos)
125
130
 
126
- let pos = 0
127
- if (fn) {
128
- pos += fn(buffer.subarray(writePos + 4, writePos + 4 + len), ...args)
129
- } else {
130
- for (const buf of args) {
131
- if (typeof buf === 'string') {
132
- pos += buffer.write(buf, writePos + 4 + pos)
133
- } else {
134
- pos += buf.copy(buffer, writePos + 4 + pos)
135
- }
136
- }
137
- }
131
+ const pos = fn(buffer.subarray(writePos + 4, writePos + 4 + len), opaque)
138
132
 
139
133
  assert(pos > 0, `pos: ${pos} > 0`)
140
134
  assert(pos <= len, `pos: ${pos} <= len: ${len}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/shared",
3
- "version": "2.0.15",
3
+ "version": "2.0.16",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {