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

feat: Allow overriding default stream constructor options #52

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 9 additions & 5 deletions src/ObjectMultiplex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Duplex, finished } from 'readable-stream';
import { Duplex, finished, type DuplexOptions } from 'readable-stream';
import once from 'once';
import { Substream } from './Substream';

Expand All @@ -12,15 +12,15 @@ interface Chunk {
export class ObjectMultiplex extends Duplex {
private _substreams: Record<string, Substream | typeof IGNORE_SUBSTREAM>;

constructor(opts: Record<string, unknown> = {}) {
constructor(opts: DuplexOptions = {}) {
super({
...opts,
objectMode: true,
...opts,
});
this._substreams = {};
}

createStream(name: string): Substream {
createStream(name: string, opts: DuplexOptions = {}): Substream {
// guard stream against destroyed already
if (this.destroyed) {
throw new Error(
Expand All @@ -47,7 +47,11 @@ export class ObjectMultiplex extends Duplex {
}

// create substream
const substream = new Substream({ parent: this, name });
const substream = new Substream({
name,
parent: this,
...opts,
});
this._substreams[name] = substream;

// listen for parent stream to end
Expand Down
11 changes: 7 additions & 4 deletions src/Substream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Duplex } from 'readable-stream';
import { Duplex, type DuplexOptions } from 'readable-stream';
import { ObjectMultiplex } from './ObjectMultiplex';

export interface SubstreamOptions {
export interface SubstreamOptions extends DuplexOptions {
parent: ObjectMultiplex;
name: string;
}
Expand All @@ -11,8 +11,11 @@ export class Substream extends Duplex {

private readonly _name: string;

constructor({ parent, name }: SubstreamOptions) {
super({ objectMode: true });
constructor({ parent, name, ...streamOptions }: SubstreamOptions) {
super({
objectMode: true,
...streamOptions,
});
this._parent = parent;
this._name = name;
}
Expand Down
Loading