From 02308d817a0f30dd4544a08e0ace190c2bed2334 Mon Sep 17 00:00:00 2001 From: Borewit Date: Sun, 2 Feb 2025 19:47:53 +0100 Subject: [PATCH] Utilize `node-readable-to-web-readable-stream` for unit testing --- package.json | 1 + test/test.ts | 17 +++-------------- test/util.ts | 47 ++++------------------------------------------- yarn.lock | 8 ++++++++ 4 files changed, 16 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 220a56b..c9cc784 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.ts b/test/test.ts index 0b27b39..f0dcdc2 100644 --- a/test/test.ts +++ b/test/test.ts @@ -44,7 +44,7 @@ describe('Matrix', () => { }]; streamFactories - // .filter((q, n) => n===1) + //.filter((q, n) => n===2) .forEach(factory => { describe(factory.description, () => { @@ -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'); @@ -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); diff --git a/test/util.ts b/test/util.ts index b1589bc..199d239 100644 --- a/test/util.ts +++ b/test/util.ts @@ -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 @@ -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 { // 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 { diff --git a/yarn.lock b/yarn.lock index c35582a..1127053 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"