Skip to content

Commit

Permalink
Utilize node-readable-to-web-readable-stream for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Feb 2, 2025
1 parent 9b80908 commit 86e9076
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 57 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"chai-as-promised": "^8.0.1",
"del-cli": "^6.0.0",
"mocha": "^11.1.0",
"node-readable-to-web-readable-stream": "^0.1.4",
"remark-cli": "^12.0.1",
"remark-preset-lint-recommended": "^7.0.0",
"source-map-support": "^0.5.21",
Expand Down
17 changes: 3 additions & 14 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Matrix', () => {
}];

streamFactories
// .filter((q, n) => n===1)
//.filter((q, n) => n===2)
.forEach(factory => {
describe(factory.description, () => {

Expand Down Expand Up @@ -92,15 +92,7 @@ describe('Matrix', () => {
return uint8Array[0];
}

it('should support concurrent reads', async function() {

if (process.versions.bun) {
this.skip(); // Fails with Bun 1.2
}

if (factory.isDefaultWebReader) {
this.skip(); // Default web reader does not support concurrent reads
}
it.skip('should support concurrent reads', async () => {

const streamReader = factory.fromString('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09');

Expand Down Expand Up @@ -231,10 +223,7 @@ describe('Matrix', () => {

describe('Handle delayed read', () => {

it('handle delay', async function(){
if (process.versions.bun) {
this.skip();
}
it('handle delay', async ()=> {
const fileReadStream = factory.fromString('123', 500);
const res = new Uint8Array(3);
const promise = fileReadStream.read(res, 0, 3);
Expand Down
47 changes: 4 additions & 43 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Utilities for testing

import { Readable } from 'node:stream';
import { ReadableStream } from 'node:stream/web';
import type { ReadableStream } from 'node:stream/web';
import { makeByteReadableStreamFromNodeReadable } from 'node-readable-to-web-readable-stream';

/**
* A mock Node.js readable-stream, using string to read from
Expand All @@ -24,52 +25,12 @@ export class SourceStream extends Readable {
}
}


// Function to convert a string to a BYOB ReadableStream
function stringReadableStream(inputString: string, delay = 0): ReadableStream<Uint8Array> {
// Convert the string to a Uint8Array using TextEncoder
const encoder = new TextEncoder();
const uint8Array = encoder.encode(inputString);

let position = 0;

// Create a BYOBReadableStream
return new ReadableStream({
type: 'bytes',
async pull(controller) {
// Check if there is data left to be pushed
if (position < uint8Array.length) {
const remaining = uint8Array.length - position;

if (controller.byobRequest) {
// BYOB path
const view = (controller.byobRequest as ReadableStreamBYOBRequest).view as Uint8Array;
const bytesRead = Math.min(remaining, view.byteLength);
view.set(uint8Array.subarray(position, position + bytesRead));
position += bytesRead;
(controller.byobRequest as ReadableStreamBYOBRequest).respond(bytesRead);
} else {
// Non-BYOB path
const chunk = uint8Array.subarray(position, position + remaining);
position += remaining;

if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
controller.enqueue(chunk);
}
}

// Close the stream if all data has been pushed
if (position >= uint8Array.length) {
controller.close();
}
},
cancel() {
// Handle stream cancellation
position = uint8Array.length;
}
});
const nodeReadable = new SourceStream(inputString, delay);
return makeByteReadableStreamFromNodeReadable(nodeReadable);
}

export function stringToReadableStream(inputString: string, forceDefault: boolean, delay?: number): ReadableStream<Uint8Array> {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,13 @@ __metadata:
languageName: node
linkType: hard

"node-readable-to-web-readable-stream@npm:^0.1.4":
version: 0.1.4
resolution: "node-readable-to-web-readable-stream@npm:0.1.4"
checksum: 10c0/e063c0c3d96e08733693c2a3d7e5c7e10d2956f67f9d707f08e0e14a217597951cb668a6ae65bd3b7768c60f08f24914d43b9856356df50944a5d6c8f3d80f0c
languageName: node
linkType: hard

"nopt@npm:^7.2.1":
version: 7.2.1
resolution: "nopt@npm:7.2.1"
Expand Down Expand Up @@ -2390,6 +2397,7 @@ __metadata:
chai-as-promised: "npm:^8.0.1"
del-cli: "npm:^6.0.0"
mocha: "npm:^11.1.0"
node-readable-to-web-readable-stream: "npm:^0.1.4"
remark-cli: "npm:^12.0.1"
remark-preset-lint-recommended: "npm:^7.0.0"
source-map-support: "npm:^0.5.21"
Expand Down

0 comments on commit 86e9076

Please sign in to comment.