-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |