@naturalcycles/js-lib 15.94.1 → 15.95.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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Returns an ArrayBuffer that contains exactly the bytes of the given view (e.g Uint8Array or Buffer).
3
+ *
4
+ * Safe alternative to `view.buffer`, which returns the whole underlying ArrayBuffer.
5
+ * It can be larger than the view, and the view may start at a non-zero offset:
6
+ * e.g Node.js allocates small Buffers from a shared pool (`Buffer.from(string)`,
7
+ * `fs.readFileSync` of small files), or the view was created with `subarray()`.
8
+ *
9
+ * Zero-copy when the view covers the whole ArrayBuffer, otherwise copies exactly its bytes.
10
+ */
11
+ export declare function _toArrayBuffer<T extends ArrayBufferLike>(view: ArrayBufferView<T>): T;
12
+ /**
13
+ * Same as _chunk, but for Uint8Array (or Buffer).
14
+ * Chunks are zero-copy views (subarray) into the input, and preserve its type
15
+ * (e.g Buffer in - Buffer[] out).
16
+ */
17
+ export declare function _chunkBuffer<T extends Uint8Array>(buf: T, size: number): T[];
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Returns an ArrayBuffer that contains exactly the bytes of the given view (e.g Uint8Array or Buffer).
3
+ *
4
+ * Safe alternative to `view.buffer`, which returns the whole underlying ArrayBuffer.
5
+ * It can be larger than the view, and the view may start at a non-zero offset:
6
+ * e.g Node.js allocates small Buffers from a shared pool (`Buffer.from(string)`,
7
+ * `fs.readFileSync` of small files), or the view was created with `subarray()`.
8
+ *
9
+ * Zero-copy when the view covers the whole ArrayBuffer, otherwise copies exactly its bytes.
10
+ */
11
+ export function _toArrayBuffer(view) {
12
+ const { buffer, byteOffset, byteLength } = view;
13
+ if (byteOffset === 0 && byteLength === buffer.byteLength)
14
+ return buffer;
15
+ return buffer.slice(byteOffset, byteOffset + byteLength);
16
+ }
17
+ /**
18
+ * Same as _chunk, but for Uint8Array (or Buffer).
19
+ * Chunks are zero-copy views (subarray) into the input, and preserve its type
20
+ * (e.g Buffer in - Buffer[] out).
21
+ */
22
+ export function _chunkBuffer(buf, size) {
23
+ const out = [];
24
+ for (let i = 0; i < buf.length; i += size) {
25
+ // subarray preserves the subclass at runtime (Buffer#subarray returns a Buffer)
26
+ out.push(buf.subarray(i, i + size));
27
+ }
28
+ return out;
29
+ }
@@ -0,0 +1 @@
1
+ export * from './buffer.util.js';
@@ -0,0 +1 @@
1
+ export * from './buffer.util.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.94.1",
4
+ "version": "15.95.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -28,6 +28,8 @@
28
28
  "./array/*.js": "./dist/array/*.js",
29
29
  "./browser": "./dist/browser/index.js",
30
30
  "./browser/*.js": "./dist/browser/*.js",
31
+ "./buffer": "./dist/buffer/index.js",
32
+ "./buffer/*.js": "./dist/buffer/*.js",
31
33
  "./datetime": "./dist/datetime/index.js",
32
34
  "./datetime/*.js": "./dist/datetime/*.js",
33
35
  "./decorators": "./dist/decorators/index.js",
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Returns an ArrayBuffer that contains exactly the bytes of the given view (e.g Uint8Array or Buffer).
3
+ *
4
+ * Safe alternative to `view.buffer`, which returns the whole underlying ArrayBuffer.
5
+ * It can be larger than the view, and the view may start at a non-zero offset:
6
+ * e.g Node.js allocates small Buffers from a shared pool (`Buffer.from(string)`,
7
+ * `fs.readFileSync` of small files), or the view was created with `subarray()`.
8
+ *
9
+ * Zero-copy when the view covers the whole ArrayBuffer, otherwise copies exactly its bytes.
10
+ */
11
+ export function _toArrayBuffer<T extends ArrayBufferLike>(view: ArrayBufferView<T>): T {
12
+ const { buffer, byteOffset, byteLength } = view
13
+ if (byteOffset === 0 && byteLength === buffer.byteLength) return buffer
14
+ return buffer.slice(byteOffset, byteOffset + byteLength) as T
15
+ }
16
+
17
+ /**
18
+ * Same as _chunk, but for Uint8Array (or Buffer).
19
+ * Chunks are zero-copy views (subarray) into the input, and preserve its type
20
+ * (e.g Buffer in - Buffer[] out).
21
+ */
22
+ export function _chunkBuffer<T extends Uint8Array>(buf: T, size: number): T[] {
23
+ const out: T[] = []
24
+
25
+ for (let i = 0; i < buf.length; i += size) {
26
+ // subarray preserves the subclass at runtime (Buffer#subarray returns a Buffer)
27
+ out.push(buf.subarray(i, i + size) as T)
28
+ }
29
+
30
+ return out
31
+ }
@@ -0,0 +1 @@
1
+ export * from './buffer.util.js'