bare-buffer 2.5.11 → 2.6.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/CMakeLists.txt CHANGED
@@ -20,9 +20,11 @@ target_sources(
20
20
  target_link_libraries(
21
21
  ${bare_buffer}
22
22
  PRIVATE
23
+ $<TARGET_OBJECTS:utf>
23
24
  $<TARGET_OBJECTS:base64>
24
25
  $<TARGET_OBJECTS:hex>
25
26
  PUBLIC
27
+ utf
26
28
  base64
27
29
  hex
28
30
  )
package/binding.c CHANGED
@@ -38,17 +38,7 @@ bare_buffer_set_zero_fill_enabled (js_env_t *env, js_callback_info_t *info) {
38
38
 
39
39
  static uint32_t
40
40
  bare_buffer_byte_length_utf8_fast (js_ffi_receiver_t *receiver, js_ffi_string_t *str) {
41
- const uint8_t *data = (const uint8_t *) str->data;
42
-
43
- uint32_t len = 0;
44
-
45
- for (uint32_t i = 0, n = str->len; i < n; i++) {
46
- // The string data is latin1 so we add an additional byte count for all
47
- // non-ASCII characters.
48
- len += 1 + (data[i] >> 7);
49
- }
50
-
51
- return len;
41
+ return utf8_length_from_latin1((const latin1_t *) str->data, str->len);
52
42
  }
53
43
 
54
44
  static js_value_t *
package/index.js CHANGED
@@ -6,11 +6,21 @@ const utf8 = require('./lib/utf8')
6
6
  const utf16le = require('./lib/utf16le')
7
7
  const binding = require('./binding')
8
8
 
9
+ let poolSize = 0
10
+
9
11
  const Buffer = module.exports = exports = class Buffer extends Uint8Array {
10
12
  static {
11
13
  binding.tag(this)
12
14
  }
13
15
 
16
+ static get poolSize () {
17
+ return poolSize
18
+ }
19
+
20
+ static set poolSize (value) {
21
+ poolSize = Math.max(0, value)
22
+ }
23
+
14
24
  [Symbol.species] () {
15
25
  return Buffer
16
26
  }
@@ -225,169 +235,83 @@ const Buffer = module.exports = exports = class Buffer extends Uint8Array {
225
235
  return codecFor(encoding).write(buffer, string)
226
236
  }
227
237
 
228
- writeUInt8 (value, offset = 0) {
229
- const view = new DataView(this.buffer, this.byteoffset, this.bytelength)
230
- view.setUint8(offset, value, true)
238
+ readBigInt64BE (offset = 0) { return viewOf(this).getBigInt64(offset, false) }
239
+ readBigInt64LE (offset = 0) { return viewOf(this).getBigInt64(offset, true) }
231
240
 
232
- return offset + 1
233
- }
241
+ readBigUint64BE (offset = 0) { return viewOf(this).getBigUint64(offset, false) }
242
+ readBigUint64LE (offset = 0) { return viewOf(this).getBigUint64(offset, true) }
234
243
 
235
- writeUint8 (...args) {
236
- return this.writeUInt8(...args)
237
- }
244
+ readDoubleBE (offset = 0) { return viewOf(this).getFloat64(offset, false) }
245
+ readDoubleLE (offset = 0) { return viewOf(this).getFloat64(offset, true) }
238
246
 
239
- writeInt8 (value, offset = 0) {
240
- const view = new DataView(this.buffer, this.byteoffset, this.bytelength)
241
- view.setInt8(offset, value)
247
+ readFloatBE (offset = 0) { return viewOf(this).getFloat32(offset, false) }
248
+ readFloatLE (offset = 0) { return viewOf(this).getFloat32(offset, true) }
242
249
 
243
- return offset + 1
244
- }
250
+ readInt8 (offset = 0) { return viewOf(this).getInt8(offset) }
245
251
 
246
- writeDoubleLE (value, offset = 0) {
247
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
248
- view.setFloat64(offset, value, true)
252
+ readInt16BE (offset = 0) { return viewOf(this).getInt16(offset, false) }
253
+ readInt16LE (offset = 0) { return viewOf(this).getInt16(offset, true) }
249
254
 
250
- return offset + 8
251
- }
255
+ readInt32BE (offset = 0) { return viewOf(this).getInt32(offset, false) }
256
+ readInt32LE (offset = 0) { return viewOf(this).getInt32(offset, true) }
252
257
 
253
- writeFloatLE (value, offset = 0) {
254
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
255
- view.setFloat32(offset, value, true)
258
+ readUint8 (offset = 0) { return viewOf(this).getUint8(offset) }
256
259
 
257
- return offset + 4
258
- }
260
+ readUint16BE (offset = 0) { return viewOf(this).getUint16(offset, false) }
261
+ readUint16LE (offset = 0) { return viewOf(this).getUint16(offset, true) }
259
262
 
260
- writeUInt16LE (value, offset = 0) {
261
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
262
- view.setUint16(offset, value, true)
263
+ readUint32BE (offset = 0) { return viewOf(this).getUint32(offset, false) }
264
+ readUint32LE (offset = 0) { return viewOf(this).getUint32(offset, true) }
263
265
 
264
- return offset + 2
265
- }
266
+ readBigUInt64BE (...args) { return this.readBigUint64BE(...args) }
267
+ readBigUInt64LE (...args) { return this.readBigUint64LE(...args) }
266
268
 
267
- writeUInt32LE (value, offset = 0) {
268
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
269
- view.setUint32(offset, value, true)
269
+ readUInt8 (...args) { return this.readUint8(...args) }
270
270
 
271
- return offset + 4
272
- }
271
+ readUInt16BE (...args) { return this.readUint16BE(...args) }
272
+ readUInt16LE (...args) { return this.readUint16LE(...args) }
273
273
 
274
- writeInt32LE (value, offset = 0) {
275
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
276
- view.setInt32(offset, value, true)
274
+ readUInt32BE (...args) { return this.readUint32BE(...args) }
275
+ readUInt32LE (...args) { return this.readUint32LE(...args) }
277
276
 
278
- return offset + 4
279
- }
277
+ writeBigInt64BE (value, offset = 0) { viewOf(this).setBigInt64(offset, value, false); return offset + 8 }
278
+ writeBigInt64LE (value, offset = 0) { viewOf(this).setBigInt64(offset, value, true); return offset + 8 }
280
279
 
281
- readInt8 (offset = 0) {
282
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
280
+ writeBigUint64BE (value, offset = 0) { viewOf(this).setBigUint64(offset, value, false); return offset + 8 }
281
+ writeBigUint64LE (value, offset = 0) { viewOf(this).setBigUint64(offset, value, true); return offset + 8 }
283
282
 
284
- return view.getInt8(offset)
285
- }
283
+ writeDoubleBE (value, offset = 0) { viewOf(this).setFloat64(offset, value, false); return offset + 8 }
284
+ writeDoubleLE (value, offset = 0) { viewOf(this).setFloat64(offset, value, true); return offset + 8 }
286
285
 
287
- readUInt8 (offset = 0) {
288
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
286
+ writeFloatBE (value, offset = 0) { viewOf(this).setFloat32(offset, value, false); return offset + 4 }
287
+ writeFloatLE (value, offset = 0) { viewOf(this).setFloat32(offset, value, true); return offset + 4 }
289
288
 
290
- return view.getUint8(offset)
291
- }
289
+ writeInt8 (value, offset = 0) { viewOf(this).setInt8(offset, value); return offset + 1 }
292
290
 
293
- readUint8 (...args) {
294
- return this.readUInt8(...args)
295
- }
296
-
297
- readDoubleLE (offset = 0) {
298
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
299
-
300
- return view.getFloat64(offset, true)
301
- }
302
-
303
- readFloatLE (offset = 0) {
304
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
305
-
306
- return view.getFloat32(offset, true)
307
- }
291
+ writeInt16BE (value, offset = 0) { viewOf(this).setInt16(offset, value, false); return offset + 2 }
292
+ writeInt16LE (value, offset = 0) { viewOf(this).setInt16(offset, value, true); return offset + 2 }
308
293
 
309
- readUInt16LE (offset = 0) {
310
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
294
+ writeInt32BE (value, offset = 0) { viewOf(this).setInt32(offset, value, false); return offset + 4 }
295
+ writeInt32LE (value, offset = 0) { viewOf(this).setInt32(offset, value, true); return offset + 4 }
311
296
 
312
- return view.getUint16(offset, true)
313
- }
297
+ writeUint8 (value, offset = 0) { viewOf(this).setUint8(offset, value, true); return offset + 1 }
314
298
 
315
- readUInt32LE (offset = 0) {
316
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
299
+ writeUint16BE (value, offset = 0) { viewOf(this).setUint16(offset, value, false); return offset + 2 }
300
+ writeUint16LE (value, offset = 0) { viewOf(this).setUint16(offset, value, true); return offset + 2 }
317
301
 
318
- return view.getUint32(offset, true)
319
- }
302
+ writeUint32LE (value, offset = 0) { viewOf(this).setUint32(offset, value, true); return offset + 4 }
303
+ writeUint32BE (value, offset = 0) { viewOf(this).setUint32(offset, value, false); return offset + 4 }
320
304
 
321
- readInt32LE (offset = 0) {
322
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
305
+ writeBigUInt64BE (...args) { return this.writeBigUint64BE(...args) }
306
+ writeBigUInt64LE (...args) { return this.writeBigUint64LE(...args) }
323
307
 
324
- return view.getInt32(offset, true)
325
- }
308
+ writeUInt8 (...args) { return this.writeUint8(...args) }
326
309
 
327
- writeDoubleBE (value, offset = 0) {
328
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
329
- view.setFloat64(offset, value, false)
310
+ writeUInt16BE (...args) { return this.writeUint16BE(...args) }
311
+ writeUInt16LE (...args) { return this.writeUint16LE(...args) }
330
312
 
331
- return offset + 8
332
- }
333
-
334
- writeFloatBE (value, offset = 0) {
335
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
336
- view.setFloat32(offset, value, false)
337
-
338
- return offset + 4
339
- }
340
-
341
- writeUInt16BE (value, offset = 0) {
342
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
343
- view.setUint16(offset, value, false)
344
-
345
- return offset + 2
346
- }
347
-
348
- writeUInt32BE (value, offset = 0) {
349
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
350
- view.setUint32(offset, value, false)
351
-
352
- return offset + 4
353
- }
354
-
355
- writeInt32BE (value, offset = 0) {
356
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
357
- view.setInt32(offset, value, false)
358
-
359
- return offset + 4
360
- }
361
-
362
- readDoubleBE (offset = 0) {
363
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
364
-
365
- return view.getFloat64(offset, false)
366
- }
367
-
368
- readFloatBE (offset = 0) {
369
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
370
-
371
- return view.getFloat32(offset, false)
372
- }
373
-
374
- readUInt16BE (offset = 0) {
375
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
376
-
377
- return view.getUint16(offset, false)
378
- }
379
-
380
- readUInt32BE (offset = 0) {
381
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
382
-
383
- return view.getUint32(offset, false)
384
- }
385
-
386
- readInt32BE (offset = 0) {
387
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
388
-
389
- return view.getInt32(offset, false)
390
- }
313
+ writeUInt32BE (...args) { return this.writeUint32BE(...args) }
314
+ writeUInt32LE (...args) { return this.writeUint32LE(...args) }
391
315
  }
392
316
 
393
317
  exports.Buffer = exports
@@ -412,6 +336,17 @@ function codecFor (encoding = 'utf8') {
412
336
  throw new Error(`Unknown encoding: ${encoding}`)
413
337
  }
414
338
 
339
+ const views = new WeakMap()
340
+
341
+ function viewOf (buffer) {
342
+ let view = views.get(buffer)
343
+ if (view === undefined) {
344
+ view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
345
+ views.set(buffer, view)
346
+ }
347
+ return view
348
+ }
349
+
415
350
  exports.isBuffer = function isBuffer (value) {
416
351
  if (typeof value !== 'object' || value === null) return false
417
352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "2.5.11",
3
+ "version": "2.6.0",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",