@gjsify/stream 0.0.4 → 0.1.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/README.md CHANGED
@@ -1,10 +1,34 @@
1
1
  # @gjsify/stream
2
2
 
3
- Node.js stream module for Gjs
3
+ GJS implementation of the Node.js `stream` module. Provides Readable, Writable, Duplex, Transform, and PassThrough streams.
4
+
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/stream
11
+ # or
12
+ yarn add @gjsify/stream
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { Readable, Writable, Transform } from '@gjsify/stream';
19
+
20
+ const readable = Readable.from(['hello', 'world']);
21
+ readable.on('data', (chunk) => console.log(chunk));
22
+ ```
4
23
 
5
24
  ## Inspirations and credits
25
+
6
26
  - https://github.com/browserify/stream-browserify
7
27
  - https://github.com/geut/brode/blob/main/packages/browser-node-core/src/stream.js
8
28
  - https://github.com/exogee-technology/readable-stream
9
29
  - https://github.com/nodejs/readable-stream
10
- - https://github.com/denoland/deno_std/blob/main/node/stream.ts
30
+ - https://github.com/denoland/deno_std/blob/main/node/stream.ts
31
+
32
+ ## License
33
+
34
+ MIT
package/cjs-compat.cjs ADDED
@@ -0,0 +1,7 @@
1
+ // CJS compatibility wrapper for npm packages that require('stream')
2
+ // In Node.js CJS, require('stream') returns the Stream constructor directly.
3
+ // When esbuild bundles ESM @gjsify/stream for CJS consumers, it returns a
4
+ // namespace object instead. This wrapper extracts the default export (the
5
+ // Stream class with all properties like Readable, Writable, etc. attached).
6
+ const mod = require('./lib/esm/index.js');
7
+ module.exports = mod.default || mod;
@@ -1,6 +1,39 @@
1
- export * from "@gjsify/deno_std/node/stream/consumers";
2
- import consumers from "@gjsify/deno_std/node/stream/consumers";
3
- var consumers_default = consumers;
1
+ async function arrayBuffer(stream) {
2
+ const chunks = [];
3
+ for await (const chunk of stream) {
4
+ chunks.push(chunk instanceof Uint8Array ? chunk : new TextEncoder().encode(String(chunk)));
5
+ }
6
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
7
+ const result = new Uint8Array(totalLength);
8
+ let offset = 0;
9
+ for (const chunk of chunks) {
10
+ result.set(chunk, offset);
11
+ offset += chunk.length;
12
+ }
13
+ return result.buffer;
14
+ }
15
+ async function blob(stream) {
16
+ const buf = await arrayBuffer(stream);
17
+ return new Blob([buf]);
18
+ }
19
+ async function buffer(stream) {
20
+ const buf = await arrayBuffer(stream);
21
+ return new Uint8Array(buf);
22
+ }
23
+ async function text(stream) {
24
+ const buf = await arrayBuffer(stream);
25
+ return new TextDecoder().decode(buf);
26
+ }
27
+ async function json(stream) {
28
+ const str = await text(stream);
29
+ return JSON.parse(str);
30
+ }
31
+ var consumers_default = { arrayBuffer, blob, buffer, text, json };
4
32
  export {
5
- consumers_default as default
33
+ arrayBuffer,
34
+ blob,
35
+ buffer,
36
+ consumers_default as default,
37
+ json,
38
+ text
6
39
  };