From 11012cb386d2bb81c5433ea5b1271e0cbc10badf Mon Sep 17 00:00:00 2001 From: dojyorin Date: Fri, 15 Dec 2023 16:20:02 +0900 Subject: [PATCH] feat: stream. --- mod.test.ts | 1 + mod.ts | 1 + mod.universal.ts | 1 + src/deflate.ts | 8 +++----- src/stream.ts | 28 ++++++++++++++++++++++++++++ test/stream.test.ts | 19 +++++++++++++++++++ 6 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/stream.ts create mode 100644 test/stream.test.ts diff --git a/mod.test.ts b/mod.test.ts index 595d6af..44d2d4b 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -12,5 +12,6 @@ import "./test/minipack.test.ts"; import "./test/path.deno.test.ts"; import "./test/platform.deno.test.ts"; import "./test/process.deno.test.ts"; +import "./test/stream.test.ts"; import "./test/text.test.ts"; import "./test/time.test.ts"; \ No newline at end of file diff --git a/mod.ts b/mod.ts index 980c692..b1fd6ed 100644 --- a/mod.ts +++ b/mod.ts @@ -6,6 +6,7 @@ export * from "./src/deflate.ts"; export * from "./src/fetch.ts"; export * from "./src/import.ts"; export * from "./src/minipack.ts"; +export * from "./src/stream.ts"; export * from "./src/text.ts"; export * from "./src/time.ts"; diff --git a/mod.universal.ts b/mod.universal.ts index bf2552d..51d84c2 100644 --- a/mod.universal.ts +++ b/mod.universal.ts @@ -6,5 +6,6 @@ export * from "./src/deflate.ts"; export * from "./src/fetch.ts"; export * from "./src/import.ts"; export * from "./src/minipack.ts"; +export * from "./src/stream.ts"; export * from "./src/text.ts"; export * from "./src/time.ts"; \ No newline at end of file diff --git a/src/deflate.ts b/src/deflate.ts index e3cfb83..b06a221 100644 --- a/src/deflate.ts +++ b/src/deflate.ts @@ -1,6 +1,4 @@ -async function streamConvert(data:Uint8Array, ts:TransformStream){ - return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(ts)).arrayBuffer()); -} +import {streamEncode, streamDecode} from "./stream.ts"; /** * Compress binary with "deflate" format. @@ -13,7 +11,7 @@ async function streamConvert(data:Uint8Array, ts:TransformStream{ - return await streamConvert(data, new CompressionStream("deflate-raw")); + return await streamDecode(streamEncode(data).pipeThrough(new CompressionStream("deflate-raw"))); } /** @@ -27,5 +25,5 @@ export async function deflateEncode(data:Uint8Array):Promise{ * ``` */ export async function deflateDecode(data:Uint8Array):Promise{ - return await streamConvert(data, new DecompressionStream("deflate-raw")); + return await streamDecode(streamEncode(data).pipeThrough(new DecompressionStream("deflate-raw"))); } \ No newline at end of file diff --git a/src/stream.ts b/src/stream.ts new file mode 100644 index 0000000..c0328a3 --- /dev/null +++ b/src/stream.ts @@ -0,0 +1,28 @@ +/** +* Convert from binary to stream. +* @example +* ```ts +* const rs = streamEncode(new Uint8Array([0x00, 0x01, 0x02])); +* ``` +*/ +export function streamEncode(data:Uint8Array):ReadableStream{ + const {body} = new Response(data); + + if(!body){ + throw new Error(); + } + + return body; +} + +/** +* Convert from stream to binary. +* @example +* ```ts +* const rs = streamEncode(new Uint8Array([0x00, 0x01, 0x02])); +* const data = await streamDecode(rs); +* ``` +*/ +export async function streamDecode(rs:ReadableStream):Promise{ + return new Uint8Array(await new Response(rs).arrayBuffer()); +} \ No newline at end of file diff --git a/test/stream.test.ts b/test/stream.test.ts new file mode 100644 index 0000000..8cfb360 --- /dev/null +++ b/test/stream.test.ts @@ -0,0 +1,19 @@ +import {assertEquals} from "../deps.test.ts"; +import {streamEncode, streamDecode} from "../src/stream.ts"; + +const sample = new Uint8Array([ + 0x71, 0xD6, 0xFB, 0x3D, 0xF9, 0xD9, 0x41, 0x07, + 0x38, 0x4D, 0xC9, 0x72, 0xE4, 0xA5, 0x63, 0x37, + 0xD6, 0x8D, 0x12, 0x75, 0x08, 0x62, 0xA1, 0xB6, + 0xAC, 0x3B, 0xEC, 0x12, 0x5A, 0xBF, 0x4F, 0x3B +]); + +Deno.test({ + name: "Stream: Encode and Decode", + async fn(){ + const encode = streamEncode(sample); + const decode = await streamDecode(encode); + + assertEquals(decode, sample); + } +}); \ No newline at end of file