Skip to content

Commit

Permalink
feat: stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Dec 15, 2023
1 parent d4758e6 commit 11012cb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
1 change: 1 addition & 0 deletions mod.universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
8 changes: 3 additions & 5 deletions src/deflate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
async function streamConvert(data:Uint8Array, ts:TransformStream<Uint8Array, Uint8Array>){
return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(ts)).arrayBuffer());
}
import {streamEncode, streamDecode} from "./stream.ts";

/**
* Compress binary with "deflate" format.
Expand All @@ -13,7 +11,7 @@ async function streamConvert(data:Uint8Array, ts:TransformStream<Uint8Array, Uin
* ```
*/
export async function deflateEncode(data:Uint8Array):Promise<Uint8Array>{
return await streamConvert(data, new CompressionStream("deflate-raw"));
return await streamDecode(streamEncode(data).pipeThrough(new CompressionStream("deflate-raw")));
}

/**
Expand All @@ -27,5 +25,5 @@ export async function deflateEncode(data:Uint8Array):Promise<Uint8Array>{
* ```
*/
export async function deflateDecode(data:Uint8Array):Promise<Uint8Array>{
return await streamConvert(data, new DecompressionStream("deflate-raw"));
return await streamDecode(streamEncode(data).pipeThrough(new DecompressionStream("deflate-raw")));
}
28 changes: 28 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -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<Uint8Array>{
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<Uint8Array>):Promise<Uint8Array>{
return new Uint8Array(await new Response(rs).arrayBuffer());
}
19 changes: 19 additions & 0 deletions test/stream.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});

0 comments on commit 11012cb

Please sign in to comment.