feat: add typed readable

This commit is contained in:
Joachim Van Herwegen 2020-06-10 11:37:04 +02:00
parent aaf3f8e3aa
commit e0d74fd68a
3 changed files with 38 additions and 0 deletions

View File

@ -1,8 +1,10 @@
import { Representation } from './Representation'; import { Representation } from './Representation';
import { TypedReadable } from '../../util/TypedReadable';
/** /**
* A representation containing binary data. * A representation containing binary data.
*/ */
export interface BinaryRepresentation extends Representation { export interface BinaryRepresentation extends Representation {
dataType: 'binary'; dataType: 'binary';
data: TypedReadable<Buffer>;
} }

View File

@ -1,8 +1,11 @@
import { Quad } from 'rdf-js';
import { Representation } from './Representation'; import { Representation } from './Representation';
import { TypedReadable } from '../../util/TypedReadable';
/** /**
* A representation containing quads as data. * A representation containing quads as data.
*/ */
export interface QuadRepresentation extends Representation { export interface QuadRepresentation extends Representation {
dataType: 'quad'; dataType: 'quad';
data: TypedReadable<Quad>;
} }

33
src/util/TypedReadable.ts Normal file
View File

@ -0,0 +1,33 @@
import { Readable } from 'stream';
/**
* Interface providing typed functions for Readable streams.
*/
export interface TypedReadable<T> extends Readable {
read(size?: number): any;
unshift(chunk: any, encoding?: BufferEncoding): void;
push(chunk: any, encoding?: BufferEncoding): boolean;
addListener(event: 'data', listener: (chunk: T) => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
emit(event: 'data', chunk: T): boolean;
emit(event: string | symbol, ...args: any[]): boolean;
on(event: 'data', listener: (chunk: T) => void): this;
on(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: 'data', listener: (chunk: T) => void): TypedReadable<T>;
once(event: string | symbol, listener: (...args: any[]) => void): this;
prependListener(event: 'data', listener: (chunk: T) => void): TypedReadable<T>;
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
prependOnceListener(event: 'data', listener: (chunk: T) => void): TypedReadable<T>;
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
removeListener(event: 'data', listener: (chunk: T) => void): TypedReadable<T>;
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
[Symbol.asyncIterator](): AsyncIterableIterator<T>;
}