-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support web-streams (WHATWG standard streams)
- Loading branch information
Showing
10 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,6 @@ jobs: | |
test/**/*.js.map | ||
test: | ||
|
||
runs-on: ubuntu-latest | ||
needs: build | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"cache": false, | ||
"check-coverage": false, | ||
"extension": [ | ||
".ts" | ||
], | ||
"sourceMap": true, | ||
"instrument": true, | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
], | ||
"all": true, | ||
"instrument": true, | ||
"report-dir": "coverage", | ||
"include": "lib/**/*.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @ts-ignore | ||
import { ReadableStream, ReadableStreamBYOBReader } from 'node:stream/web'; | ||
import { EndOfStreamError } from './EndOfFileStream.js'; | ||
|
||
export { EndOfStreamError } from './EndOfFileStream.js'; | ||
|
||
export class WebStreamReader { | ||
|
||
private reader: ReadableStreamBYOBReader; | ||
|
||
/** | ||
* Store peeked data | ||
* @type {Array} | ||
*/ | ||
private peekQueue: Uint8Array[] = []; | ||
|
||
public constructor(stream: ReadableStream) { | ||
this.reader = stream.getReader({mode: 'byob'}) as ReadableStreamBYOBReader; | ||
} | ||
|
||
/** | ||
* Read ahead (peek) from stream. Subsequent read or peeks will return the same data | ||
* @param buffer - Uint8Array (or Buffer) to store data read from stream in | ||
* @param offset - Offset target | ||
* @param length - Number of bytes to read | ||
* @returns Number of bytes peeked | ||
*/ | ||
public async peek(buffer: Uint8Array, offset: number, length: number): Promise<number> { | ||
const bytesRead = await this.read(buffer, offset, length); | ||
this.peekQueue.push(buffer.subarray(offset, offset + bytesRead)); // Put read data back to peek buffer | ||
return bytesRead; | ||
} | ||
|
||
/** | ||
* Read chunk from stream | ||
* @param buffer - Target Uint8Array (or Buffer) to store data read from stream in | ||
* @param offset - Offset target | ||
* @param length - Number of bytes to read | ||
* @returns Number of bytes read | ||
*/ | ||
public async read(buffer: Uint8Array, offset: number, length: number): Promise<number> { | ||
if (length === 0) { | ||
return 0; | ||
} | ||
const result = await this.reader.read(buffer); | ||
|
||
if (result.done) { | ||
throw new EndOfStreamError(); | ||
} | ||
return length; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { EndOfStreamError } from './EndOfFileStream.js'; | ||
export { StreamReader } from './StreamReader.js'; | ||
export { WebStreamReader } from './WebStreamReader.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters