Skip to content

Commit a9ad889

Browse files
authored
feat: Allow overriding default stream constructor options (#52)
* feat: Allow overriding substream options in createStream * chore: Use specific type for constructor options
1 parent 92c21af commit a9ad889

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/ObjectMultiplex.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Duplex, finished } from 'readable-stream';
1+
import { Duplex, finished, type DuplexOptions } from 'readable-stream';
22
import once from 'once';
33
import { Substream } from './Substream';
44

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

15-
constructor(opts: Record<string, unknown> = {}) {
15+
constructor(opts: DuplexOptions = {}) {
1616
super({
17-
...opts,
1817
objectMode: true,
18+
...opts,
1919
});
2020
this._substreams = {};
2121
}
2222

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

4949
// create substream
50-
const substream = new Substream({ parent: this, name });
50+
const substream = new Substream({
51+
name,
52+
parent: this,
53+
...opts,
54+
});
5155
this._substreams[name] = substream;
5256

5357
// listen for parent stream to end

src/Substream.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Duplex } from 'readable-stream';
1+
import { Duplex, type DuplexOptions } from 'readable-stream';
22
import { ObjectMultiplex } from './ObjectMultiplex';
33

4-
export interface SubstreamOptions {
4+
export interface SubstreamOptions extends DuplexOptions {
55
parent: ObjectMultiplex;
66
name: string;
77
}
@@ -11,8 +11,11 @@ export class Substream extends Duplex {
1111

1212
private readonly _name: string;
1313

14-
constructor({ parent, name }: SubstreamOptions) {
15-
super({ objectMode: true });
14+
constructor({ parent, name, ...streamOptions }: SubstreamOptions) {
15+
super({
16+
objectMode: true,
17+
...streamOptions,
18+
});
1619
this._parent = parent;
1720
this._name = name;
1821
}

0 commit comments

Comments
 (0)