@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.
@@ -1,6 +1,30 @@
1
- export * from "@gjsify/deno_std/node/stream/promises";
2
- import promises from "@gjsify/deno_std/node/stream/promises";
3
- var promises_default = promises;
1
+ import { pipeline as _pipeline, finished as _finished } from "../index.js";
2
+ function pipeline(...streams) {
3
+ return new Promise((resolve, reject) => {
4
+ _pipeline(...streams, (err) => {
5
+ if (err) reject(err);
6
+ else resolve();
7
+ });
8
+ });
9
+ }
10
+ function finished(stream, opts) {
11
+ return new Promise((resolve, reject) => {
12
+ if (opts && typeof opts !== "function") {
13
+ _finished(stream, opts, (err) => {
14
+ if (err) reject(err);
15
+ else resolve();
16
+ });
17
+ } else {
18
+ _finished(stream, (err) => {
19
+ if (err) reject(err);
20
+ else resolve();
21
+ });
22
+ }
23
+ });
24
+ }
25
+ var promises_default = { pipeline, finished };
4
26
  export {
5
- promises_default as default
27
+ promises_default as default,
28
+ finished,
29
+ pipeline
6
30
  };
@@ -1,6 +1,37 @@
1
- export * from "@gjsify/deno_std/node/stream/web";
2
- import web from "@gjsify/deno_std/node/stream/web";
3
- var web_default = web;
1
+ import {
2
+ ReadableStream,
3
+ WritableStream,
4
+ TransformStream,
5
+ ByteLengthQueuingStrategy,
6
+ CountQueuingStrategy,
7
+ TextEncoderStream,
8
+ TextDecoderStream
9
+ } from "@gjsify/web-streams";
10
+ import {
11
+ ReadableStream as ReadableStream2,
12
+ WritableStream as WritableStream2,
13
+ TransformStream as TransformStream2,
14
+ ByteLengthQueuingStrategy as ByteLengthQueuingStrategy2,
15
+ CountQueuingStrategy as CountQueuingStrategy2,
16
+ TextEncoderStream as TextEncoderStream2,
17
+ TextDecoderStream as TextDecoderStream2
18
+ } from "@gjsify/web-streams";
19
+ var web_default = {
20
+ ReadableStream: ReadableStream2,
21
+ WritableStream: WritableStream2,
22
+ TransformStream: TransformStream2,
23
+ ByteLengthQueuingStrategy: ByteLengthQueuingStrategy2,
24
+ CountQueuingStrategy: CountQueuingStrategy2,
25
+ TextEncoderStream: TextEncoderStream2,
26
+ TextDecoderStream: TextDecoderStream2
27
+ };
4
28
  export {
29
+ ByteLengthQueuingStrategy,
30
+ CountQueuingStrategy,
31
+ ReadableStream,
32
+ TextDecoderStream,
33
+ TextEncoderStream,
34
+ TransformStream,
35
+ WritableStream,
5
36
  web_default as default
6
37
  };
@@ -1,3 +1,13 @@
1
- export * from '@gjsify/deno_std/node/stream/consumers';
2
- import consumers from '@gjsify/deno_std/node/stream/consumers';
3
- export default consumers;
1
+ export declare function arrayBuffer(stream: ReadableStream | AsyncIterable<any>): Promise<ArrayBuffer>;
2
+ export declare function blob(stream: ReadableStream | AsyncIterable<any>): Promise<Blob>;
3
+ export declare function buffer(stream: ReadableStream | AsyncIterable<any>): Promise<any>;
4
+ export declare function text(stream: ReadableStream | AsyncIterable<any>): Promise<string>;
5
+ export declare function json(stream: ReadableStream | AsyncIterable<any>): Promise<any>;
6
+ declare const _default: {
7
+ arrayBuffer: typeof arrayBuffer;
8
+ blob: typeof blob;
9
+ buffer: typeof buffer;
10
+ text: typeof text;
11
+ json: typeof json;
12
+ };
13
+ export default _default;
@@ -1,6 +1,182 @@
1
- /// <reference types="node" />
2
- export * from '@gjsify/deno_std/node/stream';
3
- import stream from '@gjsify/deno_std/node/stream';
4
- import type { ReadableOptions } from 'stream';
5
- export type { ReadableOptions };
6
- export default stream;
1
+ import { EventEmitter } from '@gjsify/events';
2
+ import type { ReadableOptions, WritableOptions, DuplexOptions, TransformOptions, FinishedOptions } from 'node:stream';
3
+ export declare function getDefaultHighWaterMark(objectMode: boolean): number;
4
+ export declare function setDefaultHighWaterMark(objectMode: boolean, value: number): void;
5
+ /** Base options accepted by the Stream constructor (superset used by subclass options). */
6
+ export interface StreamOptions {
7
+ highWaterMark?: number;
8
+ objectMode?: boolean;
9
+ signal?: AbortSignal;
10
+ captureRejections?: boolean;
11
+ }
12
+ export type { ReadableOptions, WritableOptions, DuplexOptions, TransformOptions, FinishedOptions };
13
+ /** Tracked pipe destination for unpipe support. */
14
+ interface PipeState {
15
+ dest: Writable;
16
+ ondata: (chunk: unknown) => void;
17
+ ondrain: () => void;
18
+ onend: () => void;
19
+ cleanup: () => void;
20
+ doEnd: boolean;
21
+ }
22
+ export declare class Stream extends EventEmitter {
23
+ constructor(opts?: StreamOptions);
24
+ pipe<T extends Writable>(destination: T, options?: {
25
+ end?: boolean;
26
+ }): T;
27
+ }
28
+ export declare class Readable extends Stream {
29
+ readable: boolean;
30
+ readableFlowing: boolean | null;
31
+ readableLength: number;
32
+ readableHighWaterMark: number;
33
+ readableEncoding: string | null;
34
+ readableObjectMode: boolean;
35
+ readableEnded: boolean;
36
+ readableAborted: boolean;
37
+ destroyed: boolean;
38
+ /** @internal Tracked pipe destinations for unpipe. */
39
+ _pipeDests: PipeState[];
40
+ private _buffer;
41
+ private _readableState;
42
+ private _readablePending;
43
+ private _readImpl;
44
+ private _destroyImpl;
45
+ private _constructImpl;
46
+ constructor(opts?: ReadableOptions);
47
+ _construct(callback: (error?: Error | null) => void): void;
48
+ _read(_size: number): void;
49
+ read(size?: number): any;
50
+ /** @internal Extract exactly `size` bytes from the internal buffer. */
51
+ private _readBytes;
52
+ push(chunk: any, encoding?: string): boolean;
53
+ /** Emit 'end' followed by 'close' (matches Node.js autoDestroy behavior). */
54
+ private _emitEnd;
55
+ /** Schedule a single 'readable' event per microtask cycle (deduplicates multiple pushes). */
56
+ private _scheduleReadable;
57
+ on(event: string | symbol, listener: (...args: any[]) => void): this;
58
+ unshift(chunk: any): void;
59
+ setEncoding(encoding: string): this;
60
+ pause(): this;
61
+ resume(): this;
62
+ private _flowing;
63
+ private _flow;
64
+ isPaused(): boolean;
65
+ unpipe(destination?: Writable): this;
66
+ destroy(error?: Error): this;
67
+ [Symbol.asyncIterator](): AsyncIterableIterator<unknown>;
68
+ static from(iterable: Iterable<unknown> | AsyncIterable<unknown>, opts?: ReadableOptions): Readable;
69
+ }
70
+ export declare class Writable extends Stream {
71
+ writable: boolean;
72
+ writableHighWaterMark: number;
73
+ writableLength: number;
74
+ writableObjectMode: boolean;
75
+ writableEnded: boolean;
76
+ writableFinished: boolean;
77
+ writableCorked: number;
78
+ writableNeedDrain: boolean;
79
+ destroyed: boolean;
80
+ private _writableState;
81
+ private _corkedBuffer;
82
+ private _writeBuffer;
83
+ private _pendingConstruct;
84
+ private _ending;
85
+ private _endCallback?;
86
+ private _pendingEnd;
87
+ private _writeImpl;
88
+ private _writev;
89
+ private _finalImpl;
90
+ private _destroyImpl;
91
+ private _constructImpl;
92
+ private _decodeStrings;
93
+ private _defaultEncoding;
94
+ constructor(opts?: WritableOptions);
95
+ _construct(callback: (error?: Error | null) => void): void;
96
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
97
+ _final(callback: (error?: Error | null) => void): void;
98
+ private _maybeFlush;
99
+ private _doWrite;
100
+ private _drainWriteBuffer;
101
+ private _maybeFinish;
102
+ write(chunk: any, encoding?: string | ((error?: Error | null) => void), callback?: (error?: Error | null) => void): boolean;
103
+ private _doEnd;
104
+ end(chunk?: any, encoding?: string | (() => void), callback?: () => void): this;
105
+ cork(): void;
106
+ uncork(): void;
107
+ private _flushCorkedBuffer;
108
+ setDefaultEncoding(encoding: string): this;
109
+ destroy(error?: Error): this;
110
+ }
111
+ export declare class Duplex extends Readable {
112
+ writable: boolean;
113
+ writableHighWaterMark: number;
114
+ writableLength: number;
115
+ writableObjectMode: boolean;
116
+ writableEnded: boolean;
117
+ writableFinished: boolean;
118
+ writableCorked: number;
119
+ writableNeedDrain: boolean;
120
+ allowHalfOpen: boolean;
121
+ private _decodeStrings;
122
+ private _duplexCorkedBuffer;
123
+ private _writeImpl;
124
+ private _finalImpl;
125
+ private _defaultEncoding;
126
+ private _pendingWrites;
127
+ private _pendingEndCb;
128
+ constructor(opts?: DuplexOptions);
129
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
130
+ _final(callback: (error?: Error | null) => void): void;
131
+ destroy(error?: Error): this;
132
+ write(chunk: any, encoding?: string | ((error?: Error | null) => void), callback?: (error?: Error | null) => void): boolean;
133
+ end(chunk?: any, encoding?: string | (() => void), callback?: () => void): this;
134
+ cork(): void;
135
+ uncork(): void;
136
+ setDefaultEncoding(encoding: string): this;
137
+ }
138
+ export declare class Transform extends Duplex {
139
+ private _transformImpl;
140
+ private _flushImpl;
141
+ constructor(opts?: TransformOptions);
142
+ _transform(chunk: any, encoding: string, callback: (error?: Error | null, data?: any) => void): void;
143
+ _flush(callback: (error?: Error | null, data?: any) => void): void;
144
+ _write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
145
+ _final(callback: (error?: Error | null) => void): void;
146
+ }
147
+ export declare class PassThrough extends Transform {
148
+ constructor(opts?: TransformOptions);
149
+ }
150
+ type PipelineCallback = (err: Error | null) => void;
151
+ /** A stream that can be destroyed (duck-typed for pipeline). */
152
+ interface DestroyableStream extends Stream {
153
+ destroy?(error?: Error): void;
154
+ }
155
+ export declare function pipeline(...args: [...streams: DestroyableStream[], callback: PipelineCallback] | DestroyableStream[]): DestroyableStream;
156
+ export declare function finished(stream: Stream | Readable | Writable, callback: (err?: Error | null) => void): () => void;
157
+ export declare function finished(stream: Stream | Readable | Writable, opts: FinishedOptions, callback: (err?: Error | null) => void): () => void;
158
+ export declare function addAbortSignal(signal: AbortSignal, stream: Stream): typeof stream;
159
+ export declare function isReadable(stream: unknown): boolean;
160
+ export declare function isWritable(stream: unknown): boolean;
161
+ export declare function isDestroyed(stream: unknown): boolean;
162
+ export declare function isDisturbed(stream: unknown): boolean;
163
+ export declare function isErrored(stream: unknown): boolean;
164
+ declare const _default: typeof Stream & {
165
+ Stream: typeof Stream;
166
+ Readable: typeof Readable;
167
+ Writable: typeof Writable;
168
+ Duplex: typeof Duplex;
169
+ Transform: typeof Transform;
170
+ PassThrough: typeof PassThrough;
171
+ pipeline: typeof pipeline;
172
+ finished: typeof finished;
173
+ addAbortSignal: typeof addAbortSignal;
174
+ isReadable: typeof isReadable;
175
+ isWritable: typeof isWritable;
176
+ isDestroyed: typeof isDestroyed;
177
+ isDisturbed: typeof isDisturbed;
178
+ isErrored: typeof isErrored;
179
+ getDefaultHighWaterMark: typeof getDefaultHighWaterMark;
180
+ setDefaultHighWaterMark: typeof setDefaultHighWaterMark;
181
+ };
182
+ export default _default;
@@ -1,3 +1,8 @@
1
- export * from '@gjsify/deno_std/node/stream/promises';
2
- import promises from '@gjsify/deno_std/node/stream/promises';
3
- export default promises;
1
+ import type { Stream, Readable, Writable } from '../index.js';
2
+ export declare function pipeline(...streams: any[]): Promise<void>;
3
+ export declare function finished(stream: Stream | Readable | Writable, opts?: any): Promise<void>;
4
+ declare const _default: {
5
+ pipeline: typeof pipeline;
6
+ finished: typeof finished;
7
+ };
8
+ export default _default;
@@ -1,3 +1,3 @@
1
- export * from '@gjsify/deno_std/node/stream/web';
2
- import web from '@gjsify/deno_std/node/stream/web';
3
- export default web;
1
+ export { ReadableStream, WritableStream, TransformStream, ByteLengthQueuingStrategy, CountQueuingStrategy, TextEncoderStream, TextDecoderStream, } from '@gjsify/web-streams';
2
+ declare const _default: Record<string, any>;
3
+ export default _default;
package/package.json CHANGED
@@ -1,63 +1,39 @@
1
1
  {
2
2
  "name": "@gjsify/stream",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "Node.js stream module for Gjs",
5
5
  "type": "module",
6
- "main": "lib/cjs/index.js",
7
6
  "module": "lib/esm/index.js",
8
- "types": "lib/types/index.d.js",
7
+ "types": "lib/types/index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
- "import": {
12
- "types": "./lib/types/index.d.ts",
13
- "default": "./lib/esm/index.js"
14
- },
15
- "require": {
16
- "types": "./lib/types/index.d.ts",
17
- "default": "./lib/cjs/index.js"
18
- }
10
+ "types": "./lib/types/index.d.ts",
11
+ "require": "./cjs-compat.cjs",
12
+ "default": "./lib/esm/index.js"
19
13
  },
20
14
  "./consumers": {
21
- "import": {
22
- "types": "./lib/types/consumers/index.d.ts",
23
- "default": "./lib/esm/consumers/index.js"
24
- },
25
- "require": {
26
- "types": "./lib/types/consumers/index.d.ts",
27
- "default": "./lib/cjs/consumers/index.js"
28
- }
15
+ "types": "./lib/types/consumers/index.d.ts",
16
+ "default": "./lib/esm/consumers/index.js"
29
17
  },
30
18
  "./promises": {
31
- "import": {
32
- "types": "./lib/types/promises/index.d.ts",
33
- "default": "./lib/esm/promises/index.js"
34
- },
35
- "require": {
36
- "types": "./lib/types/promises/index.d.ts",
37
- "default": "./lib/cjs/promises/index.js"
38
- }
19
+ "types": "./lib/types/promises/index.d.ts",
20
+ "default": "./lib/esm/promises/index.js"
39
21
  },
40
22
  "./web": {
41
- "import": {
42
- "types": "./lib/types/web/index.d.ts",
43
- "default": "./lib/esm/web/index.js"
44
- },
45
- "require": {
46
- "types": "./lib/types/web/index.d.ts",
47
- "default": "./lib/cjs/web/index.js"
48
- }
23
+ "types": "./lib/types/web/index.d.ts",
24
+ "default": "./lib/esm/web/index.js"
49
25
  }
50
26
  },
51
27
  "scripts": {
52
- "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
53
- "print:name": "echo '@gjsify/stream'",
54
- "build": "yarn print:name && yarn build:gjsify && yarn build:types",
28
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
29
+ "check": "tsc --noEmit",
30
+ "build": "yarn build:gjsify && yarn build:types",
55
31
  "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
56
- "build:types": "tsc --project tsconfig.types.json || exit 0",
32
+ "build:types": "tsc",
57
33
  "build:test": "yarn build:test:gjs && yarn build:test:node",
58
34
  "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
59
35
  "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
60
- "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
36
+ "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
61
37
  "test:gjs": "gjs -m test.gjs.mjs",
62
38
  "test:node": "node test.node.mjs"
63
39
  },
@@ -67,12 +43,14 @@
67
43
  "stream"
68
44
  ],
69
45
  "devDependencies": {
70
- "@gjsify/cli": "^0.0.4",
71
- "@gjsify/unit": "^0.0.4",
72
- "@types/node": "^20.10.5",
73
- "typescript": "^5.3.3"
46
+ "@gjsify/cli": "^0.1.0",
47
+ "@gjsify/unit": "^0.1.0",
48
+ "@types/node": "^25.5.0",
49
+ "typescript": "^6.0.2"
74
50
  },
75
51
  "dependencies": {
76
- "@gjsify/deno_std": "^0.0.4"
52
+ "@gjsify/events": "^0.1.0",
53
+ "@gjsify/utils": "^0.1.0",
54
+ "@gjsify/web-streams": "^0.1.0"
77
55
  }
78
56
  }
@@ -0,0 +1,107 @@
1
+ // Tests for stream/consumers module
2
+ // Reference: Node.js lib/stream/consumers.js
3
+
4
+ import { describe, it, expect } from '@gjsify/unit';
5
+ import { Readable } from 'node:stream';
6
+ import { text, json, buffer, arrayBuffer, blob } from 'node:stream/consumers';
7
+
8
+ export default async () => {
9
+ await describe('stream/consumers', async () => {
10
+
11
+ // ==================== text() ====================
12
+ await describe('text', async () => {
13
+ await it('should consume stream as text', async () => {
14
+ const stream = Readable.from(['Hello', ' ', 'World']);
15
+ const result = await text(stream);
16
+ expect(result).toBe('Hello World');
17
+ });
18
+
19
+ await it('should handle empty stream', async () => {
20
+ const stream = Readable.from([]);
21
+ const result = await text(stream);
22
+ expect(result).toBe('');
23
+ });
24
+
25
+ await it('should handle single chunk', async () => {
26
+ const stream = Readable.from(['single']);
27
+ const result = await text(stream);
28
+ expect(result).toBe('single');
29
+ });
30
+ });
31
+
32
+ // ==================== json() ====================
33
+ await describe('json', async () => {
34
+ await it('should consume stream as JSON', async () => {
35
+ const stream = Readable.from(['{"key":', '"value"}']);
36
+ const result = await json(stream);
37
+ expect(result.key).toBe('value');
38
+ });
39
+
40
+ await it('should parse JSON array', async () => {
41
+ const stream = Readable.from(['[1,2,3]']);
42
+ const result = await json(stream);
43
+ expect(Array.isArray(result)).toBe(true);
44
+ expect(result.length).toBe(3);
45
+ expect(result[0]).toBe(1);
46
+ });
47
+
48
+ await it('should parse JSON number', async () => {
49
+ const stream = Readable.from(['42']);
50
+ const result = await json(stream);
51
+ expect(result).toBe(42);
52
+ });
53
+ });
54
+
55
+ // ==================== buffer() ====================
56
+ await describe('buffer', async () => {
57
+ await it('should consume stream as Uint8Array', async () => {
58
+ const stream = Readable.from(['Hello']);
59
+ const result = await buffer(stream);
60
+ expect(result instanceof Uint8Array).toBe(true);
61
+ expect(result.length).toBeGreaterThan(0);
62
+ });
63
+
64
+ await it('should handle empty stream', async () => {
65
+ const stream = Readable.from([]);
66
+ const result = await buffer(stream);
67
+ expect(result instanceof Uint8Array).toBe(true);
68
+ expect(result.length).toBe(0);
69
+ });
70
+ });
71
+
72
+ // ==================== arrayBuffer() ====================
73
+ await describe('arrayBuffer', async () => {
74
+ await it('should consume stream as ArrayBuffer', async () => {
75
+ const stream = Readable.from(['Hello']);
76
+ const result = await arrayBuffer(stream);
77
+ expect(result instanceof ArrayBuffer).toBe(true);
78
+ expect(result.byteLength).toBeGreaterThan(0);
79
+ });
80
+
81
+ await it('should handle multiple chunks', async () => {
82
+ const stream = Readable.from(['AB', 'CD']);
83
+ const result = await arrayBuffer(stream);
84
+ const view = new Uint8Array(result);
85
+ const str = new TextDecoder().decode(view);
86
+ expect(str).toBe('ABCD');
87
+ });
88
+ });
89
+
90
+ // ==================== blob() ====================
91
+ await describe('blob', async () => {
92
+ await it('should consume stream as Blob', async () => {
93
+ const stream = Readable.from(['Hello Blob']);
94
+ const result = await blob(stream);
95
+ expect(result instanceof Blob).toBe(true);
96
+ expect(result.size).toBeGreaterThan(0);
97
+ });
98
+
99
+ await it('should have correct content', async () => {
100
+ const stream = Readable.from(['test']);
101
+ const result = await blob(stream);
102
+ const t = await result.text();
103
+ expect(t).toBe('test');
104
+ });
105
+ });
106
+ });
107
+ };
@@ -1,3 +1,39 @@
1
- export * from '@gjsify/deno_std/node/stream/consumers';
2
- import consumers from '@gjsify/deno_std/node/stream/consumers';
3
- export default consumers;
1
+ // stream/consumers — Utility functions for consuming readable streams
2
+
3
+ export async function arrayBuffer(stream: ReadableStream | AsyncIterable<any>): Promise<ArrayBuffer> {
4
+ const chunks: Uint8Array[] = [];
5
+ for await (const chunk of stream as AsyncIterable<any>) {
6
+ chunks.push(chunk instanceof Uint8Array ? chunk : new TextEncoder().encode(String(chunk)));
7
+ }
8
+ const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
9
+ const result = new Uint8Array(totalLength);
10
+ let offset = 0;
11
+ for (const chunk of chunks) {
12
+ result.set(chunk, offset);
13
+ offset += chunk.length;
14
+ }
15
+ return result.buffer;
16
+ }
17
+
18
+ export async function blob(stream: ReadableStream | AsyncIterable<any>): Promise<Blob> {
19
+ const buf = await arrayBuffer(stream);
20
+ return new Blob([buf]);
21
+ }
22
+
23
+ export async function buffer(stream: ReadableStream | AsyncIterable<any>): Promise<any> {
24
+ const buf = await arrayBuffer(stream);
25
+ // Return as Uint8Array (Buffer-compatible)
26
+ return new Uint8Array(buf);
27
+ }
28
+
29
+ export async function text(stream: ReadableStream | AsyncIterable<any>): Promise<string> {
30
+ const buf = await arrayBuffer(stream);
31
+ return new TextDecoder().decode(buf);
32
+ }
33
+
34
+ export async function json(stream: ReadableStream | AsyncIterable<any>): Promise<any> {
35
+ const str = await text(stream);
36
+ return JSON.parse(str);
37
+ }
38
+
39
+ export default { arrayBuffer, blob, buffer, text, json };