@helpers4/node 2.0.2 → 2.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.
package/README.md CHANGED
@@ -13,6 +13,8 @@ A set of helpers for working with URLs.
13
13
 
14
14
  <!-- AUTOMATIC-METHODS -->
15
15
  - isBuffer
16
+ - isNodeStream
17
+ - isSharedArrayBuffer
16
18
  <!-- /AUTOMATIC-METHODS -->
17
19
 
18
20
  ## See
package/lib/index.d.ts CHANGED
@@ -23,4 +23,51 @@
23
23
  */
24
24
  declare function isBuffer(value: unknown): value is Buffer;
25
25
 
26
- export { isBuffer };
26
+ /**
27
+ * This file is part of helpers4.
28
+ * Copyright (C) 2025 baxyz
29
+ * SPDX-License-Identifier: LGPL-3.0-or-later
30
+ */
31
+ /**
32
+ * Checks if a value is a Node.js stream (has a `.pipe()` method).
33
+ *
34
+ * Uses duck-typing: any object with a `pipe` function qualifies, covering
35
+ * `Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible
36
+ * objects without importing from `node:stream`.
37
+ *
38
+ * @param value - The value to check
39
+ * @returns `true` if value is a Node.js stream
40
+ * @example
41
+ * import { Readable } from 'node:stream';
42
+ * isNodeStream(new Readable()) // => true
43
+ * isNodeStream({}) // => false
44
+ * isNodeStream(null) // => false
45
+ * @since 2.0.3
46
+ */
47
+ declare function isNodeStream(value: unknown): value is {
48
+ pipe: (...args: unknown[]) => unknown;
49
+ };
50
+
51
+ /**
52
+ * This file is part of helpers4.
53
+ * Copyright (C) 2025 baxyz
54
+ * SPDX-License-Identifier: LGPL-3.0-or-later
55
+ */
56
+ /**
57
+ * Checks if a value is a `SharedArrayBuffer` instance.
58
+ *
59
+ * `SharedArrayBuffer` enables shared memory between the main thread and worker
60
+ * threads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be
61
+ * unavailable; this function returns `false` in that case.
62
+ *
63
+ * @param value - The value to check
64
+ * @returns `true` if value is a SharedArrayBuffer
65
+ * @example
66
+ * isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true
67
+ * isSharedArrayBuffer(new ArrayBuffer(8)) // => false
68
+ * isSharedArrayBuffer(null) // => false
69
+ * @since 2.0.3
70
+ */
71
+ declare function isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer;
72
+
73
+ export { isBuffer, isNodeStream, isSharedArrayBuffer };
package/lib/index.js CHANGED
@@ -26,6 +26,57 @@ function isBuffer(value) {
26
26
  return typeof Buffer !== "undefined" && value instanceof Buffer;
27
27
  }
28
28
  //#endregion
29
- export { isBuffer };
29
+ //#region helpers/node/isNodeStream.ts
30
+ /**
31
+ * This file is part of helpers4.
32
+ * Copyright (C) 2025 baxyz
33
+ * SPDX-License-Identifier: LGPL-3.0-or-later
34
+ */
35
+ /**
36
+ * Checks if a value is a Node.js stream (has a `.pipe()` method).
37
+ *
38
+ * Uses duck-typing: any object with a `pipe` function qualifies, covering
39
+ * `Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible
40
+ * objects without importing from `node:stream`.
41
+ *
42
+ * @param value - The value to check
43
+ * @returns `true` if value is a Node.js stream
44
+ * @example
45
+ * import { Readable } from 'node:stream';
46
+ * isNodeStream(new Readable()) // => true
47
+ * isNodeStream({}) // => false
48
+ * isNodeStream(null) // => false
49
+ * @since 2.0.3
50
+ */
51
+ function isNodeStream(value) {
52
+ return value !== null && typeof value === "object" && typeof value["pipe"] === "function";
53
+ }
54
+ //#endregion
55
+ //#region helpers/node/isSharedArrayBuffer.ts
56
+ /**
57
+ * This file is part of helpers4.
58
+ * Copyright (C) 2025 baxyz
59
+ * SPDX-License-Identifier: LGPL-3.0-or-later
60
+ */
61
+ /**
62
+ * Checks if a value is a `SharedArrayBuffer` instance.
63
+ *
64
+ * `SharedArrayBuffer` enables shared memory between the main thread and worker
65
+ * threads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be
66
+ * unavailable; this function returns `false` in that case.
67
+ *
68
+ * @param value - The value to check
69
+ * @returns `true` if value is a SharedArrayBuffer
70
+ * @example
71
+ * isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true
72
+ * isSharedArrayBuffer(new ArrayBuffer(8)) // => false
73
+ * isSharedArrayBuffer(null) // => false
74
+ * @since 2.0.3
75
+ */
76
+ function isSharedArrayBuffer(value) {
77
+ return typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
78
+ }
79
+ //#endregion
80
+ export { isBuffer, isNodeStream, isSharedArrayBuffer };
30
81
 
31
82
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../helpers/node/isBuffer.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if a value is a Node.js Buffer instance.\n *\n * `Buffer` extends `Uint8Array` and is specific to Node.js, Bun, and Deno.\n * In browser-only environments where `Buffer` is not defined, this function\n * always returns `false`.\n *\n * Useful for filtering or type-narrowing in a functional pipeline:\n * `values.filter(isBuffer)`\n *\n * @param value - The value to check\n * @returns True if value is a Buffer\n * @example\n * isBuffer(Buffer.from('hello')) // => true\n * isBuffer(new Uint8Array(8)) // => false\n * isBuffer('hello') // => false\n * @since 2.0.0\n */\nexport function isBuffer(value: unknown): value is Buffer {\n return typeof Buffer !== 'undefined' && value instanceof Buffer;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,SAAS,OAAiC;CACxD,OAAO,OAAO,WAAW,eAAe,iBAAiB;AAC3D"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../helpers/node/isBuffer.ts","../../../helpers/node/isNodeStream.ts","../../../helpers/node/isSharedArrayBuffer.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if a value is a Node.js Buffer instance.\n *\n * `Buffer` extends `Uint8Array` and is specific to Node.js, Bun, and Deno.\n * In browser-only environments where `Buffer` is not defined, this function\n * always returns `false`.\n *\n * Useful for filtering or type-narrowing in a functional pipeline:\n * `values.filter(isBuffer)`\n *\n * @param value - The value to check\n * @returns True if value is a Buffer\n * @example\n * isBuffer(Buffer.from('hello')) // => true\n * isBuffer(new Uint8Array(8)) // => false\n * isBuffer('hello') // => false\n * @since 2.0.0\n */\nexport function isBuffer(value: unknown): value is Buffer {\n return typeof Buffer !== 'undefined' && value instanceof Buffer;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if a value is a Node.js stream (has a `.pipe()` method).\n *\n * Uses duck-typing: any object with a `pipe` function qualifies, covering\n * `Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible\n * objects without importing from `node:stream`.\n *\n * @param value - The value to check\n * @returns `true` if value is a Node.js stream\n * @example\n * import { Readable } from 'node:stream';\n * isNodeStream(new Readable()) // => true\n * isNodeStream({}) // => false\n * isNodeStream(null) // => false\n * @since 2.0.3\n */\nexport function isNodeStream(value: unknown): value is { pipe: (...args: unknown[]) => unknown } {\n return (\n // Intentionally `object`-only, unlike `isPromiseLike` (see type/isPromiseLike.ts):\n // a callable function exposing `.pipe()` would be an unusual, contrived stream shape.\n value !== null &&\n typeof value === 'object' &&\n typeof (value as Record<string, unknown>)['pipe'] === 'function'\n );\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if a value is a `SharedArrayBuffer` instance.\n *\n * `SharedArrayBuffer` enables shared memory between the main thread and worker\n * threads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be\n * unavailable; this function returns `false` in that case.\n *\n * @param value - The value to check\n * @returns `true` if value is a SharedArrayBuffer\n * @example\n * isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true\n * isSharedArrayBuffer(new ArrayBuffer(8)) // => false\n * isSharedArrayBuffer(null) // => false\n * @since 2.0.3\n */\nexport function isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer {\n return typeof SharedArrayBuffer !== 'undefined' && value instanceof SharedArrayBuffer;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,SAAS,OAAiC;CACxD,OAAO,OAAO,WAAW,eAAe,iBAAiB;AAC3D;;;;;;;;;;;;;;;;;;;;;;;;ACJA,SAAgB,aAAa,OAAoE;CAC/F,OAGE,UAAU,QACV,OAAO,UAAU,YACjB,OAAQ,MAAkC,YAAY;AAE1D;;;;;;;;;;;;;;;;;;;;;;;ACTA,SAAgB,oBAAoB,OAA4C;CAC9E,OAAO,OAAO,sBAAsB,eAAe,iBAAiB;AACtE"}
package/llms.txt CHANGED
@@ -1,7 +1,7 @@
1
1
  # @helpers4/node
2
2
 
3
3
  > Tree-shakable TypeScript utility functions for the `node` domain.
4
- > Package: `@helpers4/node` — Version: 2.0.2
4
+ > Package: `@helpers4/node` — Version: 2.0.3
5
5
  > License: LGPL-3.0-or-later
6
6
 
7
7
  ## Installation
@@ -15,7 +15,7 @@ pnpm add @helpers4/node
15
15
  ## Usage
16
16
 
17
17
  ```typescript
18
- import { isBuffer } from '@helpers4/node';
18
+ import { isBuffer, isNodeStream, isSharedArrayBuffer } from '@helpers4/node';
19
19
  ```
20
20
 
21
21
  ## Functions
@@ -23,6 +23,8 @@ import { isBuffer } from '@helpers4/node';
23
23
  | Function | Description |
24
24
  |---|---|
25
25
  | `isBuffer` | Checks if a value is a Node.js Buffer instance. `Buffer` extends `Uint8Array` and is specific to No |
26
+ | `isNodeStream` | Checks if a value is a Node.js stream (has a `.pipe()` method). Uses duck-typing: any object with a |
27
+ | `isSharedArrayBuffer` | Checks if a value is a `SharedArrayBuffer` instance. `SharedArrayBuffer` enables shared memory betw |
26
28
 
27
29
  ---
28
30
 
@@ -74,3 +76,100 @@ values.filter(isBuffer)
74
76
  ```
75
77
 
76
78
  ---
79
+
80
+ ### `isNodeStream`
81
+
82
+ Checks if a value is a Node.js stream (has a `.pipe()` method).
83
+
84
+ Uses duck-typing: any object with a `pipe` function qualifies, covering
85
+ `Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible
86
+ objects without importing from `node:stream`.
87
+
88
+ ```typescript
89
+ import { isNodeStream } from '@helpers4/node';
90
+
91
+ isNodeStream(value: unknown): value is object
92
+ ```
93
+
94
+ **Parameters:**
95
+
96
+ - `value: unknown` — The value to check
97
+
98
+ **Returns:** `value is object` — `true` if value is a Node.js stream
99
+
100
+ **Examples:**
101
+
102
+ *Detect a Node.js stream*
103
+
104
+ Returns true for any object with a .pipe() method (Readable, Writable, Transform, etc.).
105
+
106
+ ```typescript
107
+ import { Readable } from 'node:stream';
108
+ isNodeStream(new Readable({ read() {} })) // => true
109
+ isNodeStream({}) // => false
110
+ isNodeStream(null) // => false
111
+ ```
112
+
113
+ *Guard before piping an unknown value*
114
+
115
+ Use isNodeStream to safely pipe only known streams.
116
+
117
+ ```typescript
118
+ import { Writable } from 'node:stream';
119
+ function pipeToOutput(source: unknown, dest: Writable): void {
120
+ if (isNodeStream(source)) {
121
+ source.pipe(dest);
122
+ }
123
+ }
124
+ ```
125
+
126
+ ---
127
+
128
+ ### `isSharedArrayBuffer`
129
+
130
+ Checks if a value is a `SharedArrayBuffer` instance.
131
+
132
+ `SharedArrayBuffer` enables shared memory between the main thread and worker
133
+ threads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be
134
+ unavailable; this function returns `false` in that case.
135
+
136
+ ```typescript
137
+ import { isSharedArrayBuffer } from '@helpers4/node';
138
+
139
+ isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer
140
+ ```
141
+
142
+ **Parameters:**
143
+
144
+ - `value: unknown` — The value to check
145
+
146
+ **Returns:** `value is SharedArrayBuffer` — `true` if value is a SharedArrayBuffer
147
+
148
+ **Examples:**
149
+
150
+ *Distinguish SharedArrayBuffer from ArrayBuffer*
151
+
152
+ Returns true only for SharedArrayBuffer instances, not plain ArrayBuffers.
153
+
154
+ ```typescript
155
+ isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true
156
+ isSharedArrayBuffer(new ArrayBuffer(8)) // => false
157
+ isSharedArrayBuffer(null) // => false
158
+ ```
159
+
160
+ *Safe shared memory check before worker communication*
161
+
162
+ Use as a guard to ensure a buffer can be transferred to a Worker.
163
+
164
+ ```typescript
165
+ function sendToWorker(buffer: unknown): void {
166
+ if (isSharedArrayBuffer(buffer)) {
167
+ // buffer is SharedArrayBuffer — can be shared directly
168
+ // worker.postMessage({ buffer });
169
+ } else {
170
+ // must transfer or copy
171
+ }
172
+ }
173
+ ```
174
+
175
+ ---
package/meta/api.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "category": "node",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "functions": [
5
5
  {
6
6
  "name": "isBuffer",
@@ -37,6 +37,78 @@
37
37
  }
38
38
  ],
39
39
  "sourceFile": "isBuffer.ts"
40
+ },
41
+ {
42
+ "name": "isNodeStream",
43
+ "kind": "function",
44
+ "description": "Checks if a value is a Node.js stream (has a `.pipe()` method).\n\nUses duck-typing: any object with a `pipe` function qualifies, covering\n`Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible\nobjects without importing from `node:stream`.",
45
+ "since": "2.0.3",
46
+ "signatures": [
47
+ {
48
+ "signature": "isNodeStream(value: unknown): value is object",
49
+ "description": "Checks if a value is a Node.js stream (has a `.pipe()` method).\n\nUses duck-typing: any object with a `pipe` function qualifies, covering\n`Readable`, `Writable`, `Duplex`, `Transform`, and custom stream-compatible\nobjects without importing from `node:stream`.",
50
+ "params": [
51
+ {
52
+ "name": "value",
53
+ "type": "unknown",
54
+ "description": "The value to check"
55
+ }
56
+ ],
57
+ "returns": {
58
+ "type": "value is object",
59
+ "description": "`true` if value is a Node.js stream"
60
+ }
61
+ }
62
+ ],
63
+ "examples": [
64
+ {
65
+ "title": "Detect a Node.js stream",
66
+ "description": "Returns true for any object with a .pipe() method (Readable, Writable, Transform, etc.).",
67
+ "code": "import { Readable } from 'node:stream';\nisNodeStream(new Readable({ read() {} })) // => true\nisNodeStream({}) // => false\nisNodeStream(null) // => false"
68
+ },
69
+ {
70
+ "title": "Guard before piping an unknown value",
71
+ "description": "Use isNodeStream to safely pipe only known streams.",
72
+ "code": "import { Writable } from 'node:stream';\nfunction pipeToOutput(source: unknown, dest: Writable): void {\n if (isNodeStream(source)) {\n source.pipe(dest);\n }\n}"
73
+ }
74
+ ],
75
+ "sourceFile": "isNodeStream.ts"
76
+ },
77
+ {
78
+ "name": "isSharedArrayBuffer",
79
+ "kind": "function",
80
+ "description": "Checks if a value is a `SharedArrayBuffer` instance.\n\n`SharedArrayBuffer` enables shared memory between the main thread and worker\nthreads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be\nunavailable; this function returns `false` in that case.",
81
+ "since": "2.0.3",
82
+ "signatures": [
83
+ {
84
+ "signature": "isSharedArrayBuffer(value: unknown): value is SharedArrayBuffer",
85
+ "description": "Checks if a value is a `SharedArrayBuffer` instance.\n\n`SharedArrayBuffer` enables shared memory between the main thread and worker\nthreads. In browsers without COOP/COEP headers, `SharedArrayBuffer` may be\nunavailable; this function returns `false` in that case.",
86
+ "params": [
87
+ {
88
+ "name": "value",
89
+ "type": "unknown",
90
+ "description": "The value to check"
91
+ }
92
+ ],
93
+ "returns": {
94
+ "type": "value is SharedArrayBuffer",
95
+ "description": "`true` if value is a SharedArrayBuffer"
96
+ }
97
+ }
98
+ ],
99
+ "examples": [
100
+ {
101
+ "title": "Distinguish SharedArrayBuffer from ArrayBuffer",
102
+ "description": "Returns true only for SharedArrayBuffer instances, not plain ArrayBuffers.",
103
+ "code": "isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true\nisSharedArrayBuffer(new ArrayBuffer(8)) // => false\nisSharedArrayBuffer(null) // => false"
104
+ },
105
+ {
106
+ "title": "Safe shared memory check before worker communication",
107
+ "description": "Use as a guard to ensure a buffer can be transferred to a Worker.",
108
+ "code": "function sendToWorker(buffer: unknown): void {\n if (isSharedArrayBuffer(buffer)) {\n // buffer is SharedArrayBuffer — can be shared directly\n // worker.postMessage({ buffer });\n } else {\n // must transfer or copy\n }\n}"
109
+ }
110
+ ],
111
+ "sourceFile": "isSharedArrayBuffer.ts"
40
112
  }
41
113
  ]
42
114
  }
@@ -15,6 +15,36 @@
15
15
  "code": "const values = [Buffer.from('a'), 'text', Buffer.alloc(4), 42];\nvalues.filter(isBuffer)\n// => [Buffer, Buffer]"
16
16
  }
17
17
  ]
18
+ },
19
+ {
20
+ "name": "isNodeStream",
21
+ "examples": [
22
+ {
23
+ "title": "Detect a Node.js stream",
24
+ "description": "Returns true for any object with a .pipe() method (Readable, Writable, Transform, etc.).",
25
+ "code": "import { Readable } from 'node:stream';\nisNodeStream(new Readable({ read() {} })) // => true\nisNodeStream({}) // => false\nisNodeStream(null) // => false"
26
+ },
27
+ {
28
+ "title": "Guard before piping an unknown value",
29
+ "description": "Use isNodeStream to safely pipe only known streams.",
30
+ "code": "import { Writable } from 'node:stream';\nfunction pipeToOutput(source: unknown, dest: Writable): void {\n if (isNodeStream(source)) {\n source.pipe(dest);\n }\n}"
31
+ }
32
+ ]
33
+ },
34
+ {
35
+ "name": "isSharedArrayBuffer",
36
+ "examples": [
37
+ {
38
+ "title": "Distinguish SharedArrayBuffer from ArrayBuffer",
39
+ "description": "Returns true only for SharedArrayBuffer instances, not plain ArrayBuffers.",
40
+ "code": "isSharedArrayBuffer(new SharedArrayBuffer(8)) // => true\nisSharedArrayBuffer(new ArrayBuffer(8)) // => false\nisSharedArrayBuffer(null) // => false"
41
+ },
42
+ {
43
+ "title": "Safe shared memory check before worker communication",
44
+ "description": "Use as a guard to ensure a buffer can be transferred to a Worker.",
45
+ "code": "function sendToWorker(buffer: unknown): void {\n if (isSharedArrayBuffer(buffer)) {\n // buffer is SharedArrayBuffer — can be shared directly\n // worker.postMessage({ buffer });\n } else {\n // must transfer or copy\n }\n}"
46
+ }
47
+ ]
18
48
  }
19
49
  ]
20
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helpers4/node",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "A set of helpers in TS/JS, compatible with tree-shaking, for node.",
5
5
  "author": "baxyz <baxy@etik.com>",
6
6
  "license": "LGPL-3.0",
@@ -27,7 +27,9 @@
27
27
  "keywords": [
28
28
  "helpers",
29
29
  "node",
30
- "isBuffer"
30
+ "isBuffer",
31
+ "isNodeStream",
32
+ "isSharedArrayBuffer"
31
33
  ],
32
34
  "files": [
33
35
  "lib/index.js",