@nxtedition/shared 2.0.16 → 2.0.17

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