From df6432861f575e43fb12d56b23628e70c04124db Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Wed, 29 Nov 2023 22:31:25 +0100 Subject: [PATCH] fix: fetch-n --- README.md | 12 ++++++++++++ package.json | 2 +- pnpm-lock.yaml | 44 +++++++++++++++++++++--------------------- src/fetchText.ts | 3 +++ test/fetchText.test.ts | 17 ++++++++++++++++ 5 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 src/fetchText.ts create mode 100644 test/fetchText.test.ts diff --git a/README.md b/README.md index 944dea9..3dd797f 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,18 @@ concat( ``` +
+fetchText + +```javascript +import { fetchText } from '@ilteoood/re-flusso/fetchText'; + +const response = await fetch('...') + +fetchText(response) +``` +
+
filter diff --git a/package.json b/package.json index 3a9e6c3..d1a9012 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "streams" ], "devDependencies": { - "@biomejs/biome": "^1.3.3", + "@biomejs/biome": "^1.4.0", "@edge-runtime/vm": "^3.1.7", "happy-dom": "^12.10.3", "tsup": "^8.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 84c6554..78ac15a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6,8 +6,8 @@ settings: devDependencies: '@biomejs/biome': - specifier: ^1.3.3 - version: 1.3.3 + specifier: ^1.4.0 + version: 1.4.0 '@edge-runtime/vm': specifier: ^3.1.7 version: 3.1.7 @@ -26,22 +26,22 @@ devDependencies: packages: - /@biomejs/biome@1.3.3: - resolution: {integrity: sha512-vTJn7RBzLWIabUuUIoEopO860YyBrbPEu4Pztfd28jRU5QD074hKZ9IQs24pFO6A2R296gaeYmN62f4u7pUruQ==} + /@biomejs/biome@1.4.0: + resolution: {integrity: sha512-/rDlao6ra38nhxo4IYCqWCzfTJcpMk4YHjSVBI9yN/ifdhnzSwirL25xDVH7G9hZdNhpF9g78FaPJhFa9DX0Cw==} engines: {node: '>=14.*'} hasBin: true requiresBuild: true optionalDependencies: - '@biomejs/cli-darwin-arm64': 1.3.3 - '@biomejs/cli-darwin-x64': 1.3.3 - '@biomejs/cli-linux-arm64': 1.3.3 - '@biomejs/cli-linux-x64': 1.3.3 - '@biomejs/cli-win32-arm64': 1.3.3 - '@biomejs/cli-win32-x64': 1.3.3 + '@biomejs/cli-darwin-arm64': 1.4.0 + '@biomejs/cli-darwin-x64': 1.4.0 + '@biomejs/cli-linux-arm64': 1.4.0 + '@biomejs/cli-linux-x64': 1.4.0 + '@biomejs/cli-win32-arm64': 1.4.0 + '@biomejs/cli-win32-x64': 1.4.0 dev: true - /@biomejs/cli-darwin-arm64@1.3.3: - resolution: {integrity: sha512-2X87ZfbmWwe4NGukrUvnoYdI//muSgjNUCAHJ2DO+kS1sB7kDy1s6PN/IYyTJuqRcJtDuOnSpaUDE7KxR1YhtA==} + /@biomejs/cli-darwin-arm64@1.4.0: + resolution: {integrity: sha512-nBrtVRwr4IlTtxLOHwBwLv1sWvggf9/DnT5/ALIANJZOpoING6u8jHWipods69wK8kGa8Ld7iwHm3W5BrJJFFQ==} engines: {node: '>=14.*'} cpu: [arm64] os: [darwin] @@ -49,8 +49,8 @@ packages: dev: true optional: true - /@biomejs/cli-darwin-x64@1.3.3: - resolution: {integrity: sha512-t+7DWTCbSgHOBcPsGKuwS1qh1z9zbXFK8i8ktE18yW7iF/W0zI62k44fYqYeFJKlb0Q08aqUvez3L+AQJFsn+w==} + /@biomejs/cli-darwin-x64@1.4.0: + resolution: {integrity: sha512-nny0VgOj3ksUGzU5GblgtQEvrAZFgFe1IJBoYOP978OQdDrg7BpS+GX5udfof87Dl4ZlHPRBU951ceHOxF7BTg==} engines: {node: '>=14.*'} cpu: [x64] os: [darwin] @@ -58,8 +58,8 @@ packages: dev: true optional: true - /@biomejs/cli-linux-arm64@1.3.3: - resolution: {integrity: sha512-D8CvXaB8lkXXBQ6B3n0MXSSZFiE60+aNHorBLimVTtKiMod8QvAP425oQFZFul5wMXZqPLGTKFjXbAi/rvnc1A==} + /@biomejs/cli-linux-arm64@1.4.0: + resolution: {integrity: sha512-gyLkT/Yh9xfW1T9yjQs/2txkCeG0e+LRs0adLugMwN0ptcNTRyusBvUoiHnpB+9rS6hWu9ZCedGMNmKQ8v2GSw==} engines: {node: '>=14.*'} cpu: [arm64] os: [linux] @@ -67,8 +67,8 @@ packages: dev: true optional: true - /@biomejs/cli-linux-x64@1.3.3: - resolution: {integrity: sha512-bqB05fwJnRZwRlcm/BS/s4qPickqiXZkiU/nOYvHApfsPeqgSHgv5HWoBYuSUjgqBbX3XZJArsC5dCcVW7vAJw==} + /@biomejs/cli-linux-x64@1.4.0: + resolution: {integrity: sha512-LIxTuU2zSbIHM9XDYjQphJ5UU8h2eS7yR8uIvGYSba7Qt9AKqfbenyVJTsVnoj1CXxxgKNVSc/wVmlOlGz5DBQ==} engines: {node: '>=14.*'} cpu: [x64] os: [linux] @@ -76,8 +76,8 @@ packages: dev: true optional: true - /@biomejs/cli-win32-arm64@1.3.3: - resolution: {integrity: sha512-muFOjAv1ONMfaJDlo4Ds+Qb9lkdSLM2XaxOe3AJPejSq3Vi0aRr51ZnE02BofMnL2sVsOA9cO54wibsuTcopbw==} + /@biomejs/cli-win32-arm64@1.4.0: + resolution: {integrity: sha512-U2jT1/0wZLJIRqnU8qHAfi/A/+yUwlL3sYJgqs+wO0BbR22WGQZlj03u5FdpEoyLXdsLv1pbeIcjNp+V0NYXWA==} engines: {node: '>=14.*'} cpu: [arm64] os: [win32] @@ -85,8 +85,8 @@ packages: dev: true optional: true - /@biomejs/cli-win32-x64@1.3.3: - resolution: {integrity: sha512-PMkMhS4smmmTMflxuZUx3REFSazEL9xsGscvZO1dKWI4ET23la+KxEM4TlSpjOyO66UerqSkuUlZecn0QhD63A==} + /@biomejs/cli-win32-x64@1.4.0: + resolution: {integrity: sha512-gN6DgyyBxIwoCovAUFJHFWVallb0cLosayDRtNyxU3MDv/atZxSXOWQezfVKBIbgmFPxYWJObd+awvbPYXwwww==} engines: {node: '>=14.*'} cpu: [x64] os: [win32] diff --git a/src/fetchText.ts b/src/fetchText.ts new file mode 100644 index 0000000..ce4f56a --- /dev/null +++ b/src/fetchText.ts @@ -0,0 +1,3 @@ +export const fetchText = (response: Response, label?: string, options?: TextDecoderOptions) => +// biome-ignore lint/style/noNonNullAssertion: For this function, response must be correctly resolved + response.body!.pipeThrough(new TextDecoderStream(label, options)); diff --git a/test/fetchText.test.ts b/test/fetchText.test.ts new file mode 100644 index 0000000..d0d65fb --- /dev/null +++ b/test/fetchText.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, test } from "vitest"; +import { fetchText } from "../src/fetchText"; +import { pipeline } from "../src/pipeline"; +import { toArray } from "../src/toArray"; + +describe("fetchText", () => { + test("should fetch text", async () => { + const destinationArray = []; + const response = await fetch( + "https://gist.githubusercontent.com/rfmcnally/0a5a16e09374da7dd478ffbe6ba52503/raw/095e75121f31a8b7dc88aa89dbd637a944ce264a/ndjson-sample.json", + ); + + await pipeline(fetchText(response), toArray(destinationArray)); + + expect(destinationArray.join("")).toHaveLength(7476); + }); +});