From f24d71bfe5be343e65d084d23073c3686a7f4d18 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Sun, 27 Aug 2023 13:47:32 +1000 Subject: [PATCH] Replace the test utility function `streamToString` with `text` from `node:stream/consumers`. --- changelog.md | 1 + processRequest.test.mjs | 29 +++++++++++++---------------- test/streamToString.mjs | 18 ------------------ 3 files changed, 14 insertions(+), 34 deletions(-) delete mode 100644 test/streamToString.mjs diff --git a/changelog.md b/changelog.md index 654f84c..6842bc5 100644 --- a/changelog.md +++ b/changelog.md @@ -7,6 +7,7 @@ - Updated Node.js support to `^18.15.0 || >=20.4.0`. - Updated dev dependencies, some of which require newer Node.js versions than previously supported. - Refactored tests to use the standard `AbortController`, `fetch`, `File`, and `FormData` APIs available in modern Node.js and removed the dev dependencies [`node-abort-controller`](https://npm.im/node-abort-controller) and [`node-fetch`](https://npm.im/node-fetch). +- Replaced the test utility function `streamToString` with the function `text` from `node:stream/consumers` that’s available in modern Node.js. ### Patch diff --git a/processRequest.test.mjs b/processRequest.test.mjs index 82db4ed..b69f5fc 100644 --- a/processRequest.test.mjs +++ b/processRequest.test.mjs @@ -11,6 +11,7 @@ import { throws, } from "node:assert"; import { createServer } from "node:http"; +import { text } from "node:stream/consumers"; import { ReadStream } from "fs-capacitor"; @@ -18,7 +19,6 @@ import processRequest from "./processRequest.mjs"; import abortingMultipartRequest from "./test/abortingMultipartRequest.mjs"; import Deferred from "./test/Deferred.mjs"; import listen from "./test/listen.mjs"; -import streamToString from "./test/streamToString.mjs"; import Upload from "./Upload.mjs"; /** @@ -86,7 +86,7 @@ export default (tests) => { ok(stream instanceof ReadStream); strictEqual(stream.readableEncoding, null); strictEqual(stream.readableHighWaterMark, 16384); - strictEqual(await streamToString(stream), "a"); + strictEqual(await text(stream), "a"); } catch (error) { serverError = error; } finally { @@ -147,7 +147,7 @@ export default (tests) => { ok(stream instanceof ReadStream); strictEqual(stream.readableEncoding, null); strictEqual(stream.readableHighWaterMark, 16384); - strictEqual(await streamToString(stream), "a"); + strictEqual(await text(stream), "a"); } catch (error) { serverError = error; } finally { @@ -208,10 +208,7 @@ export default (tests) => { ok(stream instanceof ReadStream); strictEqual(stream.readableEncoding, encoding); strictEqual(stream.readableHighWaterMark, highWaterMark); - strictEqual( - await streamToString(stream), - Buffer.from("a").toString(encoding), - ); + strictEqual(await text(stream), Buffer.from("a").toString(encoding)); } catch (error) { serverError = error; } finally { @@ -266,7 +263,7 @@ export default (tests) => { const streamA = uploadA.createReadStream(); ok(streamA instanceof ReadStream); - strictEqual(await streamToString(streamA), "a"); + strictEqual(await text(streamA), "a"); ok(operations[1].variables.file instanceof Upload); @@ -279,7 +276,7 @@ export default (tests) => { const streamB = uploadB.createReadStream(); ok(streamB instanceof ReadStream); - strictEqual(await streamToString(streamB), "b"); + strictEqual(await text(streamB), "b"); } catch (error) { serverError = error; } finally { @@ -351,8 +348,8 @@ export default (tests) => { ok(stream2 instanceof ReadStream); const [content1, content2] = await Promise.all([ - streamToString(stream1), - streamToString(stream2), + text(stream1), + text(stream2), ]); strictEqual(content1, "a"); @@ -408,7 +405,7 @@ export default (tests) => { const uploadB = await operation.variables.fileB.promise; const streamB = uploadB.createReadStream(); - await streamToString(streamB); + await text(streamB); } catch (error) { serverError = error; } finally { @@ -468,7 +465,7 @@ export default (tests) => { const stream = upload.createReadStream(); ok(stream instanceof ReadStream); - strictEqual(await streamToString(stream), "a"); + strictEqual(await text(stream), "a"); } catch (error) { serverError = error; } finally { @@ -622,7 +619,7 @@ export default (tests) => { const streamA = uploadA.createReadStream(); ok(streamA instanceof ReadStream); - strictEqual(await streamToString(streamA), "a"); + strictEqual(await text(streamA), "a"); ok(operation.variables.files[1] instanceof Upload); await rejects(operation.variables.files[1].promise, { name: "PayloadTooLargeError", @@ -717,7 +714,7 @@ export default (tests) => { const streamB = uploadB.createReadStream(); ok(streamB instanceof ReadStream); - strictEqual(await streamToString(streamB), "b"); + strictEqual(await text(streamB), "b"); } catch (error) { serverError = error; } finally { @@ -832,7 +829,7 @@ export default (tests) => { const stream = upload.createReadStream(); ok(stream instanceof ReadStream); - strictEqual(await streamToString(stream), "a"); + strictEqual(await text(stream), "a"); }; const testUploadB = async () => { diff --git a/test/streamToString.mjs b/test/streamToString.mjs deleted file mode 100644 index ca01a41..0000000 --- a/test/streamToString.mjs +++ /dev/null @@ -1,18 +0,0 @@ -// @ts-check - -/** - * Converts a Node.js readable stream to a string. - * @param {import("node:stream").Readable} stream Node.js readable stream. - * @returns {Promise} Resolves the final string. - */ -export default function streamToString(stream) { - return new Promise((resolve, reject) => { - let data = ""; - stream - .on("error", reject) - .on("data", (chunk) => { - data += chunk; - }) - .on("end", () => resolve(data)); - }); -}