@nxtedition/shared 2.0.17 → 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.
- package/index.js +10 -29
- 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(
|
|
78
|
-
if (!
|
|
77
|
+
return function write(len, fn, opaque) {
|
|
78
|
+
if (!len) {
|
|
79
79
|
return
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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}`)
|