Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript typings #6

Merged
merged 10 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
import EventEmitter, { EventMap } from 'bare-events'
import Buffer, { BufferEncoding } from 'bare-buffer'

type StreamEncoding = BufferEncoding | 'buffer'

interface StreamCallback {
(err: Error | null): void
}

interface StreamEvents extends EventMap {
close: []
error: [err: Error]
}

interface StreamOptions<S extends Stream> {
eagerOpen?: boolean
signal?: AbortSignal
open?(this: S, cb: StreamCallback): void
predestroy?(this: S): void
destroy?(this: S, err: Error | null, cb: StreamCallback): void
}

interface Stream<M extends StreamEvents = StreamEvents>
extends EventEmitter<M> {
_open(cb: StreamCallback): void
_predestroy(): void
_destroy(err: Error | null, cb: StreamCallback): void

readonly readable: boolean
readonly writable: boolean
readonly destroyed: boolean
readonly destroying: boolean

destroy(err?: Error | null): void
}

declare class Stream {}

interface ReadableEvents extends StreamEvents {
data: [data: Buffer | string]
end: []
readable: []
piping: [dest: Writable]
}

interface ReadableOptions<S extends Readable = Readable>
extends StreamOptions<S> {
encoding?: BufferEncoding
highWaterMark?: number
read?(this: S, size: number): void
}

interface Readable<M extends ReadableEvents = ReadableEvents>
extends Stream<M>,
AsyncIterable<Buffer> {
_read(size: number): void

push(data: string, encoding?: BufferEncoding): boolean
push(data: Buffer | null): boolean

unshift(data: string, encoding?: BufferEncoding): boolean
unshift(data: Buffer | null): boolean

read(): Buffer | string | null

resume(): this
pause(): this

pipe<S extends Writable>(dest: S, cb?: StreamCallback): S

setEncoding(encoding: BufferEncoding): void
}

declare class Readable<
M extends ReadableEvents = ReadableEvents
> extends Stream<M> {
constructor(opts?: ReadableOptions)

static from(
data: (Buffer | string)[] | AsyncIterable<Buffer | string>,
opts?: ReadableOptions
): Readable

static isBackpressured(rs: Readable): boolean

static isPaused(rs: Readable): boolean
}

interface WritableEvents extends StreamEvents {
drain: []
finish: []
pipe: [src: Readable]
}

interface WritableOptions<S extends Writable = Writable>
extends StreamOptions<S> {
write?(
this: S,
data: Buffer,
encoding: StreamEncoding,
cb: StreamCallback
): void
writev?(
this: S,
batch: { chunk: Buffer; encoding: StreamEncoding }[],
cb: StreamCallback
): void
final?(this: S, cb: StreamCallback): void
}

interface Writable<M extends WritableEvents = WritableEvents>
extends Stream<M> {
_write(data: Buffer, encoding: StreamEncoding, cb: StreamCallback): void
_writev(
batch: { chunk: Buffer; encoding: StreamEncoding }[],
cb: StreamCallback
): void
_final(cb: StreamCallback): void

readonly destroyed: boolean

write(data: string, encoding?: BufferEncoding, cb?: StreamCallback): boolean
write(data: Buffer, cb?: StreamCallback): boolean

end(cb?: StreamCallback): this
end(data: string, encoding?: BufferEncoding, cb?: StreamCallback): this
end(data: Buffer, cb?: StreamCallback): this

cork(): void
uncork(): void
}

declare class Writable<
M extends WritableEvents = WritableEvents
> extends Stream<M> {
constructor(opts?: WritableOptions)

static isBackpressured(ws: Writable): boolean

static drained(ws: Writable): Promise<boolean>
}

interface DuplexEvents extends ReadableEvents, WritableEvents {}

interface DuplexOptions<S extends Duplex = Duplex>
extends ReadableOptions<S>,
WritableOptions<S> {}

interface Duplex<M extends DuplexEvents = DuplexEvents>
extends Readable<M>,
Writable<M> {}

declare class Duplex<M extends DuplexEvents = DuplexEvents> extends Stream<M> {
constructor(opts?: DuplexOptions)
}

interface TransformEvents extends DuplexEvents {}

interface TransformOptions<S extends Transform = Transform>
extends DuplexOptions<S> {
transform?(
this: S,
data: Buffer,
encoding: StreamEncoding,
cb: StreamCallback
): void
flush?(this: S, cb: StreamCallback): void
}

interface Transform<M extends TransformEvents = TransformEvents>
extends Duplex<M> {
_transform(data: Buffer, encoding: StreamEncoding, cb: StreamCallback): void
_flush(cb: StreamCallback): void
}

declare class Transform<
M extends TransformEvents = TransformEvents
> extends Duplex<M> {
constructor(opts?: TransformOptions)
}

declare namespace Stream {
export {
Stream,
Readable,
Writable,
Duplex,
Transform,
Transform as PassThrough
}

export function finished(
stream: Stream,
opts: { cleanup?: boolean },
cb: StreamCallback
): () => void

export function finished(stream: Stream, cb: Stream): () => void

export function isStream(stream: unknown): stream is Stream

export function isEnded(stream: Stream): boolean

export function isFinished(stream: Stream): boolean

export function isDisturbed(stream: Stream): boolean

export function getStreamError(
stream: Stream,
opts?: { all?: boolean }
): Error | null
}

export = Stream
48 changes: 38 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ exports.Stream = exports

exports.Readable = class Readable extends stream.Readable {
constructor(opts = {}) {
super(opts)
super({
...opts,
byteLength: null,
byteLengthReadable: null,
map: null,
mapReadable: null
})

if (this._construct) this._open = this._construct

Expand Down Expand Up @@ -49,7 +55,13 @@ exports.Readable = class Readable extends stream.Readable {

exports.Writable = class Writable extends stream.Writable {
constructor(opts = {}) {
super({ ...opts, byteLengthWritable })
super({
...opts,
byteLength: null,
byteLengthWritable,
map: null,
mapWritable: null
})

if (this._construct) this._open = this._construct

Expand Down Expand Up @@ -77,7 +89,7 @@ exports.Writable = class Writable extends stream.Writable {

const result = super.write({ chunk, encoding })

if (cb) stream.Writable.drained(this).then(cb, cb)
if (cb) stream.Writable.drained(this).then(() => cb(null), cb)

return result
}
Expand All @@ -103,15 +115,23 @@ exports.Writable = class Writable extends stream.Writable {
? super.end({ chunk, encoding })
: super.end()

if (cb) this.once('end', cb)
if (cb) this.once('end', () => cb(null))

return result
}
}

exports.Duplex = class Duplex extends stream.Duplex {
constructor(opts = {}) {
super({ ...opts, byteLengthWritable })
super({
...opts,
byteLength: null,
byteLengthReadable: null,
byteLengthWritable,
map: null,
mapReadable: null,
mapWritable: null
})

if (this._construct) this._open = this._construct

Expand Down Expand Up @@ -159,7 +179,7 @@ exports.Duplex = class Duplex extends stream.Duplex {

const result = super.write({ chunk, encoding })

if (cb) stream.Writable.drained(this).then(cb, cb)
if (cb) stream.Writable.drained(this).then(() => cb(null), cb)

return result
}
Expand All @@ -185,15 +205,23 @@ exports.Duplex = class Duplex extends stream.Duplex {
? super.end({ chunk, encoding })
: super.end()

if (cb) this.once('end', cb)
if (cb) this.once('end', () => cb(null))

return result
}
}

exports.Transform = class Transform extends stream.Transform {
constructor(opts = {}) {
super({ ...opts, byteLengthWritable })
super({
...opts,
byteLength: null,
byteLengthReadable: null,
byteLengthWritable,
map: null,
mapReadable: null,
mapWritable: null
})

if (this._transform !== stream.Transform.prototype._transform) {
this._transform = transform.bind(this, this._transform)
Expand Down Expand Up @@ -233,7 +261,7 @@ exports.Transform = class Transform extends stream.Transform {

const result = super.write({ chunk, encoding })

if (cb) stream.Writable.drained(this).then(cb, cb)
if (cb) stream.Writable.drained(this).then(() => cb(null), cb)

return result
}
Expand All @@ -259,7 +287,7 @@ exports.Transform = class Transform extends stream.Transform {
? super.end({ chunk, encoding })
: super.end()

if (cb) this.once('end', cb)
if (cb) this.once('end', () => cb(null))

return result
}
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
"version": "2.6.1",
"description": "Streaming data for JavaScript",
"exports": {
".": "./index.js",
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./package": "./package.json",
"./promises": "./promises.js",
"./web": "./web.js",
"./global": "./global.js"
},
"files": [
"index.js",
"index.d.ts",
"promises.js",
"web.js",
"global.js"
],
"scripts": {
"test": "prettier . --check && bare test/all.js"
"test": "prettier . --check && bare test.js"
},
"repository": {
"type": "git",
Expand All @@ -32,8 +36,14 @@
"streamx": "^2.21.0"
},
"devDependencies": {
"bare-buffer": "^3.0.0",
"bare-events": "^2.5.4",
"brittle": "^3.5.2",
"prettier": "^3.3.3",
"prettier-config-standard": "^7.0.0"
},
"peerDependencies": {
"bare-buffer": "*",
"bare-events": "*"
}
}
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./test/basic')
require('./test/web')
2 changes: 0 additions & 2 deletions test/all.js

This file was deleted.

Loading