diff --git a/package.json b/package.json index abb40e7..0d6fecc 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ "license": "ISC", "devDependencies": { "@biomejs/biome": "^1.3.3", + "@edge-runtime/vm": "^3.1.7", + "happy-dom": "^12.10.3", "vitest": "^0.34.6" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e78965a..627a0d1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,15 @@ devDependencies: '@biomejs/biome': specifier: ^1.3.3 version: 1.3.3 + '@edge-runtime/vm': + specifier: ^3.1.7 + version: 3.1.7 + happy-dom: + specifier: ^12.10.3 + version: 12.10.3 vitest: specifier: ^0.34.6 - version: 0.34.6 + version: 0.34.6(@edge-runtime/vm@3.1.7)(happy-dom@12.10.3) packages: @@ -82,6 +88,18 @@ packages: dev: true optional: true + /@edge-runtime/primitives@4.0.5: + resolution: {integrity: sha512-t7QiN5d/KpXgCvIfSt6Nm9Hj3WVdNgc5CpOD73jasY+9EvTI7Ngdj5cXvjcHrPcmYWJZMySPgeEeoL/1N/Llag==} + engines: {node: '>=16'} + dev: true + + /@edge-runtime/vm@3.1.7: + resolution: {integrity: sha512-hUMFbDQ/nZN+1TLMi6iMO1QFz9RSV8yGG8S42WFPFma1d7VSNE0eMdJUmwjmtav22/iQkzHMmu6oTSfAvRGS8g==} + engines: {node: '>=16'} + dependencies: + '@edge-runtime/primitives': 4.0.5 + dev: true + /@esbuild/android-arm64@0.19.7: resolution: {integrity: sha512-YEDcw5IT7hW3sFKZBkCAQaOCJQLONVcD4bOyTXMZz5fr66pTHnAet46XAtbXAkJRfIn2YVhdC6R9g4xa27jQ1w==} engines: {node: '>=12'} @@ -489,6 +507,10 @@ packages: get-func-name: 2.0.2 dev: true + /css.escape@1.5.1: + resolution: {integrity: sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg==} + dev: true + /debug@4.3.4: resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} engines: {node: '>=6.0'} @@ -513,6 +535,11 @@ packages: engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} dev: true + /entities@4.5.0: + resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==} + engines: {node: '>=0.12'} + dev: true + /esbuild@0.19.7: resolution: {integrity: sha512-6brbTZVqxhqgbpqBR5MzErImcpA0SQdoKOkcWK/U30HtQxnokIpG3TX2r0IJqbFUzqLjhU/zC1S5ndgakObVCQ==} engines: {node: '>=12'} @@ -555,6 +582,24 @@ packages: resolution: {integrity: sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==} dev: true + /happy-dom@12.10.3: + resolution: {integrity: sha512-JzUXOh0wdNGY54oKng5hliuBkq/+aT1V3YpTM+lrN/GoLQTANZsMaIvmHiHe612rauHvPJnDZkZ+5GZR++1Abg==} + dependencies: + css.escape: 1.5.1 + entities: 4.5.0 + iconv-lite: 0.6.3 + webidl-conversions: 7.0.0 + whatwg-encoding: 2.0.0 + whatwg-mimetype: 3.0.0 + dev: true + + /iconv-lite@0.6.3: + resolution: {integrity: sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==} + engines: {node: '>=0.10.0'} + dependencies: + safer-buffer: 2.1.2 + dev: true + /jsonc-parser@3.2.0: resolution: {integrity: sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==} dev: true @@ -665,6 +710,10 @@ packages: fsevents: 2.3.3 dev: true + /safer-buffer@2.1.2: + resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} + dev: true + /siginfo@2.0.0: resolution: {integrity: sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==} dev: true @@ -773,7 +822,7 @@ packages: fsevents: 2.3.3 dev: true - /vitest@0.34.6: + /vitest@0.34.6(@edge-runtime/vm@3.1.7)(happy-dom@12.10.3): resolution: {integrity: sha512-+5CALsOvbNKnS+ZHMXtuUC7nL8/7F1F2DnHGjSsszX8zCjWSSviphCb/NuS9Nzf4Q03KyyDRBAXhF/8lffME4Q==} engines: {node: '>=v14.18.0'} hasBin: true @@ -804,6 +853,7 @@ packages: webdriverio: optional: true dependencies: + '@edge-runtime/vm': 3.1.7 '@types/chai': 4.3.11 '@types/chai-subset': 1.3.5 '@types/node': 20.10.0 @@ -817,6 +867,7 @@ packages: cac: 6.7.14 chai: 4.3.10 debug: 4.3.4 + happy-dom: 12.10.3 local-pkg: 0.4.3 magic-string: 0.30.5 pathe: 1.1.1 @@ -838,6 +889,23 @@ packages: - terser dev: true + /webidl-conversions@7.0.0: + resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==} + engines: {node: '>=12'} + dev: true + + /whatwg-encoding@2.0.0: + resolution: {integrity: sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg==} + engines: {node: '>=12'} + dependencies: + iconv-lite: 0.6.3 + dev: true + + /whatwg-mimetype@3.0.0: + resolution: {integrity: sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==} + engines: {node: '>=12'} + dev: true + /why-is-node-running@2.2.2: resolution: {integrity: sha512-6tSwToZxTOcotxHeA+qGCq1mVzKR3CwcJGmVcY+QE8SHy6TnpFnh8PAvPNHYr7EcuVeG0QSMxtYCuO1ta/G/oA==} engines: {node: '>=8'} diff --git a/src/fromArray.ts b/src/fromArray.ts new file mode 100644 index 0000000..6ca3a8c --- /dev/null +++ b/src/fromArray.ts @@ -0,0 +1,11 @@ +export const fromArray = (array: T[]): ReadableStream => { + const arrayLength = array.length + return new ReadableStream({ + start(controller) { + for (let i = 0; i < arrayLength; i++) { + controller.enqueue(array[i]) + } + controller.close() + } + }) +} \ No newline at end of file diff --git a/test/fromArray.test.ts b/test/fromArray.test.ts new file mode 100644 index 0000000..d8fcdee --- /dev/null +++ b/test/fromArray.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest' +import { fromArray } from '../src/fromArray' + +describe('fromArray', () => { + it('should create a readable stream from an array', async () => { + const array = [1, 2, 3] + + const reader = fromArray(array).getReader() + + for (let i = 0; i < array.length; i++) { + const result = await reader.read() + expect(result.value).toEqual(array[i]) + expect(result.done).toEqual(false) + } + + expect(await reader.read()).toEqual({ done: true, value: undefined }) + }) +}) \ No newline at end of file