@nxtedition/lib 28.0.2 → 28.0.3

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/package.json +3 -2
  2. package/slice.js +278 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.2",
3
+ "version": "28.0.3",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -37,6 +37,7 @@
37
37
  "errors.js",
38
38
  "errors.d.ts",
39
39
  "worker.js",
40
+ "slice.js",
40
41
  "stream.js",
41
42
  "transcript.js",
42
43
  "docker-secrets.js",
@@ -91,5 +92,5 @@
91
92
  "pino": ">=7.0.0",
92
93
  "rxjs": "^7.0.0"
93
94
  },
94
- "gitHead": "10cdafd06260ca5abfabbf08f503ccd78819c3e2"
95
+ "gitHead": "15f4fd00f86a558ae56672b1efa3e909b23feb3f"
95
96
  }
package/slice.js ADDED
@@ -0,0 +1,278 @@
1
+ import util from 'node:util'
2
+
3
+ const EMPTY_BUF = Buffer.alloc(0)
4
+ const POOL = []
5
+
6
+ export class Slice {
7
+ buffer
8
+ byteOffset = 0
9
+ byteLength = 0
10
+ maxByteLength = 0
11
+
12
+ static EMPTY_BUF = EMPTY_BUF
13
+
14
+ /**
15
+ *
16
+ * @param {Buffer} [buffer]
17
+ * @param {number} [byteOffset]
18
+ * @param {number} [byteLength]
19
+ * @param {number} [maxByteLength]
20
+ */
21
+ constructor(
22
+ buffer = Slice.EMPTY_BUF,
23
+ byteOffset = 0,
24
+ byteLength = buffer.byteLength,
25
+ maxByteLength = byteLength,
26
+ ) {
27
+ if (byteOffset < 0 || !Number.isInteger(byteOffset)) {
28
+ throw new RangeError(`Invalid byteOffset: ${byteOffset}`)
29
+ }
30
+
31
+ if (byteLength < 0 || !Number.isInteger(byteLength)) {
32
+ throw new RangeError(`Invalid byteLength: ${byteLength}`)
33
+ }
34
+
35
+ if (
36
+ maxByteLength < byteLength ||
37
+ maxByteLength > buffer.byteLength ||
38
+ !Number.isInteger(maxByteLength)
39
+ ) {
40
+ throw new RangeError(`Invalid maxByteLength: ${maxByteLength}`)
41
+ }
42
+
43
+ this.buffer = buffer
44
+ this.byteOffset = byteOffset
45
+ this.byteLength = byteLength
46
+ this.maxByteLength = maxByteLength
47
+ }
48
+
49
+ static create(buffer, byteOffset, byteLength, maxByteLength) {
50
+ if (buffer === undefined) {
51
+ buffer = Slice.EMPTY_BUF
52
+ }
53
+ if (byteOffset === undefined) {
54
+ byteOffset = 0
55
+ }
56
+ if (byteLength === undefined) {
57
+ byteLength = buffer.byteLength
58
+ }
59
+ if (maxByteLength === undefined) {
60
+ maxByteLength = byteLength
61
+ }
62
+
63
+ const slice = POOL.pop()
64
+
65
+ if (slice) {
66
+ slice.buffer = buffer
67
+ slice.byteOffset = byteOffset
68
+ slice.byteLength = byteLength
69
+ slice.maxByteLength = maxByteLength
70
+ return slice
71
+ }
72
+
73
+ return new Slice(buffer, byteOffset, byteLength, maxByteLength)
74
+ }
75
+
76
+ static free(slice) {
77
+ if (slice == null) {
78
+ return
79
+ }
80
+
81
+ slice.reset()
82
+
83
+ if (POOL.length < 16 * 1024) {
84
+ POOL.push(slice)
85
+ }
86
+ }
87
+
88
+ reset() {
89
+ this.buffer = Slice.EMPTY_BUF
90
+ this.byteOffset = 0
91
+ this.byteLength = 0
92
+ this.maxByteLength = 0
93
+ }
94
+
95
+ get length() {
96
+ return this.byteLength
97
+ }
98
+
99
+ /**
100
+ * @param {Buffer|Slice} target
101
+ * @param {number} [targetStart = 0]
102
+ * @param {number} [sourceStart = 0]
103
+ * @param {number} [sourceEnd = this.byteOffset]
104
+ * @returns {number} Number of bytes written.
105
+ */
106
+ copy(target, targetStart, sourceStart, sourceEnd) {
107
+ if (target instanceof Slice) {
108
+ if (targetStart === undefined) {
109
+ targetStart = target.byteOffset
110
+ } else {
111
+ targetStart += target.byteOffset
112
+ }
113
+
114
+ target = target.buffer
115
+ }
116
+
117
+ if (sourceStart === undefined) {
118
+ sourceStart = this.byteOffset
119
+ } else {
120
+ sourceStart += this.byteOffset
121
+ }
122
+
123
+ if (sourceEnd === undefined) {
124
+ sourceEnd = this.byteLength + this.byteOffset
125
+ } else {
126
+ sourceEnd += this.byteOffset
127
+ }
128
+
129
+ return this.buffer.copy(target, targetStart, sourceStart, sourceEnd)
130
+ }
131
+
132
+ /**
133
+ * @param {Buffer|Slice} target
134
+ * @param {number} [targetStart = 0]
135
+ * @param {number} [targetEnd = target.byteOffset]
136
+ * @param {number} [sourceStart = 0]
137
+ * @param {number} [sourceEnd = this.byteOffset]
138
+ * @returns
139
+ */
140
+ compare(target, targetStart, targetEnd, sourceStart, sourceEnd) {
141
+ if (target instanceof Slice) {
142
+ if (targetStart === undefined) {
143
+ targetStart = target.byteOffset
144
+ } else {
145
+ targetStart += target.byteOffset
146
+ }
147
+
148
+ if (targetEnd === undefined) {
149
+ targetEnd = target.byteLength + target.byteOffset
150
+ } else {
151
+ targetEnd += target.byteOffset
152
+ }
153
+
154
+ target = target.buffer
155
+ }
156
+
157
+ if (sourceStart === undefined) {
158
+ sourceStart = this.byteOffset
159
+ } else {
160
+ sourceStart += this.byteOffset
161
+ }
162
+
163
+ if (sourceEnd === undefined) {
164
+ sourceEnd = this.byteLength + this.byteOffset
165
+ } else {
166
+ sourceEnd += this.byteOffset
167
+ }
168
+
169
+ return this.buffer.compare(target, targetStart, targetEnd, sourceStart, sourceEnd)
170
+ }
171
+
172
+ /**
173
+ *
174
+ * @param {string} string
175
+ * @param {number} [offset=0]
176
+ * @param {number} [length=this.byteLength - offset]
177
+ * @param {NodeJS.BufferEncoding} [encoding='utf8']
178
+ * @returns {number} Number of bytes written.
179
+ */
180
+ write(string, offset, length, encoding) {
181
+ if (offset === undefined) {
182
+ offset = this.byteOffset
183
+ } else {
184
+ offset += this.byteOffset
185
+ }
186
+
187
+ if (length === undefined) {
188
+ length = this.byteLength + this.byteOffset - offset
189
+ }
190
+
191
+ return this.buffer.write(string, offset, length, encoding)
192
+ }
193
+
194
+ set(array, offset) {
195
+ if (array == null) {
196
+ return
197
+ }
198
+
199
+ if (offset === undefined) {
200
+ offset = this.byteOffset
201
+ } else {
202
+ offset += this.byteOffset
203
+ }
204
+
205
+ array?.copy(this.buffer, offset)
206
+ }
207
+
208
+ at(index) {
209
+ return index >= 0
210
+ ? this.buffer[this.byteOffset + index]
211
+ : this.buffer[this.byteOffset + this.byteLength + index]
212
+ }
213
+
214
+ test(expr) {
215
+ return expr.test(this.buffer, this.byteOffset, this.byteLength)
216
+ }
217
+
218
+ readBigUInt64BE(offset = 0) {
219
+ if (offset === undefined) {
220
+ offset = this.byteOffset
221
+ } else {
222
+ offset += this.byteOffset
223
+ }
224
+
225
+ return this.buffer.readBigUInt64BE(offset)
226
+ }
227
+
228
+ /**
229
+ *
230
+ * @param {BufferEncoding} [encoding='utf8']
231
+ * @param {number} [start=0]
232
+ * @param {number} [end=this.byteLength]
233
+ * @returns {string}
234
+ */
235
+ toString(encoding, start, end) {
236
+ if (start === undefined) {
237
+ start = this.byteOffset
238
+ } else {
239
+ start += this.byteOffset
240
+ }
241
+
242
+ if (end === undefined) {
243
+ end = this.byteLength + this.byteOffset
244
+ } else {
245
+ end += this.byteOffset
246
+ }
247
+
248
+ return this.buffer.toString(encoding, start, end)
249
+ }
250
+
251
+ toBuffer(start, end) {
252
+ if (start === undefined) {
253
+ start = this.byteOffset
254
+ } else {
255
+ start += this.byteOffset
256
+ }
257
+
258
+ if (end === undefined) {
259
+ end = this.byteLength + this.byteOffset
260
+ } else {
261
+ end += this.byteOffset
262
+ }
263
+
264
+ return this.buffer.subarray(start, end)
265
+ }
266
+
267
+ [Symbol.toStringTag]() {
268
+ return this.toString()
269
+ }
270
+
271
+ [util.inspect.custom](depth, options, inspect) {
272
+ const bytes = []
273
+ for (let i = 0; i < this.byteLength; i++) {
274
+ bytes.push(this.buffer[this.byteOffset + i].toString(16).padStart(2, '0'))
275
+ }
276
+ return `Slice: "${this.toString()}" <${bytes.join(' ')}>`
277
+ }
278
+ }