Skip to content

Commit

Permalink
Support web-streams (WHATWG standard streams)
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Jul 5, 2024
1 parent 289da08 commit 7567fe0
Show file tree
Hide file tree
Showing 5 changed files with 715 additions and 707 deletions.
8 changes: 2 additions & 6 deletions lib/ReadStreamTokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { AbstractTokenizer } from './AbstractTokenizer.js';
import { EndOfStreamError, StreamReader } from 'peek-readable';
import { Readable } from 'node:stream';
import { EndOfStreamError, IStreamReader } from 'peek-readable';
import { IFileInfo, IReadChunkOptions } from './types.js';

const maxBufferSize = 256000;

export class ReadStreamTokenizer extends AbstractTokenizer {

private streamReader: StreamReader;

public constructor(stream: Readable, fileInfo?: IFileInfo) {
public constructor(private streamReader: IStreamReader, fileInfo?: IFileInfo) {
super(fileInfo);
this.streamReader = new StreamReader(stream);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
import { Readable } from 'node:stream';
import { BufferTokenizer } from './BufferTokenizer.js';
import { IFileInfo } from './types.js';
import { StreamReader, WebStreamReader} from "peek-readable";
import type { ReadableStream } from 'node:stream/web';
export { EndOfStreamError } from 'peek-readable';
export { ITokenizer, IFileInfo } from './types.js';
export { IToken, IGetToken } from '@tokenizer/token';
Expand All @@ -15,7 +17,19 @@ export { IToken, IGetToken } from '@tokenizer/token';
*/
export function fromStream(stream: Readable, fileInfo?: IFileInfo): ReadStreamTokenizer {
fileInfo = fileInfo ? fileInfo : {};
return new ReadStreamTokenizer(stream, fileInfo);
return new ReadStreamTokenizer(new StreamReader(stream), fileInfo);
}

/**
* Construct ReadStreamTokenizer from given ReadableStream (WebStream API).
* Will set fileSize, if provided given Stream has set the .path property/
* @param webStream - Read from Node.js Stream.Readable
* @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
* @returns ReadStreamTokenizer
*/
export function fromWebStream(webStream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo): ReadStreamTokenizer {
fileInfo = fileInfo ? fileInfo : {};
return new ReadStreamTokenizer(new WebStreamReader(webStream), fileInfo);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
import * as core from './core.js';

export { fromFile } from './FileTokenizer.js';
export { ITokenizer, EndOfStreamError, fromBuffer, IFileInfo } from './core.js';
export { ITokenizer, EndOfStreamError, fromBuffer, fromWebStream, IFileInfo } from './core.js';
export { IToken, IGetToken } from '@tokenizer/token';

/**
Expand Down
Loading

0 comments on commit 7567fe0

Please sign in to comment.