@nxtedition/shared 2.0.15 → 3.0.0

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 +10 -29
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -74,29 +74,21 @@ export function writer({ sharedState, sharedBuffer }) {
74
74
  let readPos = 0
75
75
  let writePos = 0
76
76
 
77
- return function write(...args) {
78
- if (!args.length) {
77
+ return function write(len, fn, opaque) {
78
+ if (!len) {
79
79
  return
80
80
  }
81
81
 
82
- let len
83
- let fn
84
-
85
- if (Number.isInteger(args[0])) {
86
- len = args.shift()
87
- fn = args.shift()
88
-
82
+ if (Number.isInteger(len)) {
89
83
  assert(len >= 0, `len: ${len} >= 0`)
90
84
  assert(typeof fn === 'function', `fn: ${typeof fn} === 'function`)
85
+ } else if (Buffer.isBuffer(len)) {
86
+ const buf = len
87
+ len = buf.byteLength
88
+ fn = (dst, buf) => buf.copy(dst)
89
+ opaque = buf
91
90
  } else {
92
- if (Array.isArray(args[0])) {
93
- args = args[0]
94
- }
95
-
96
- len = 0
97
- for (const buf of args) {
98
- len += Buffer.byteLength(buf)
99
- }
91
+ throw new Error('invalid argument')
100
92
  }
101
93
 
102
94
  assert(len <= size)
@@ -123,18 +115,7 @@ export function writer({ sharedState, sharedBuffer }) {
123
115
 
124
116
  buffer.writeInt32LE(-3, writePos)
125
117
 
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
- }
118
+ const pos = fn(buffer.subarray(writePos + 4, writePos + 4 + len), opaque)
138
119
 
139
120
  assert(pos > 0, `pos: ${pos} > 0`)
140
121
  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": "3.0.0",
4
4
  "description": "Ring Buffer for NodeJS cross Worker communication",
5
5
  "main": "index.js",
6
6
  "repository": {