Skip to content

Commit

Permalink
New feat (#85)
Browse files Browse the repository at this point in the history
* update deps.

* remove configload.

* update version.

* feat: env

* typo.

* v3 -> v4

* feat: stream.
  • Loading branch information
dojyorin authored Dec 15, 2023
1 parent a631a4c commit f6a30c5
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"runs-on": "ubuntu-latest",
"steps": [{
"name": "clone repository",
"uses": "actions/checkout@v3"
"uses": "actions/checkout@v4"
}, {
"name": "dispatch release",
"uses": "softprops/action-gh-release@v1",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: dispatch release
uses: softprops/action-gh-release@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"steps": [{
"name": "clone repository",
"uses": "actions/checkout@v3"
"uses": "actions/checkout@v4"
}, {
"name": "install deno",
"uses": "denoland/setup-deno@v1",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- windows-latest
steps:
- name: clone repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: install deno
uses: denoland/setup-deno@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions deps.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {assertEquals} from "https://deno.land/std@0.207.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.207.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.207.0/fs/mod.ts";
export {assertEquals} from "https://deno.land/std@0.209.0/assert/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.209.0/path/mod.ts";
export {exists} from "https://deno.land/std@0.209.0/fs/mod.ts";
6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export {dirname, fromFileUrl} from "https://deno.land/std@0.207.0/path/mod.ts";
export {Logger, handlers} from "https://deno.land/std@0.207.0/log/mod.ts";
export {format} from "https://deno.land/std@0.207.0/datetime/mod.ts";
export {dirname, fromFileUrl} from "https://deno.land/std@0.209.0/path/mod.ts";
export {Logger, handlers} from "https://deno.land/std@0.209.0/log/mod.ts";
export {format} from "https://deno.land/std@0.209.0/datetime/mod.ts";
2 changes: 2 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "./test/byte.test.ts";
import "./test/crypto.test.ts";
import "./test/deep.test.ts";
import "./test/deflate.test.ts";
import "./test/env.deno.test.ts";
import "./test/fetch.test.ts";
import "./test/import.test.ts";
import "./test/json.deno.test.ts";
Expand All @@ -11,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";
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ 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";

export * from "./src/env.deno.ts";
export * from "./src/json.deno.ts";
export * from "./src/log.deno.ts";
export * from "./src/path.deno.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")));
}
35 changes: 35 additions & 0 deletions src/env.deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Map of env value type and string specify them.
*/
export interface EnvType{
"string": string;
"number": number;
"boolean": boolean;
}

/**
* Convert environment variable to specified type and get.
* @example
* ```ts
* const port = envGet("SERVER_PORT", "number", true);
* ```
*/
export function envGet<T extends keyof EnvType, U extends boolean>(key:string, type:T, required:U):U extends true ? EnvType[T] : EnvType[T] | undefined{
const env = Deno.env.get(key);

if(env === undefined){
if(required){
throw new Error(key);
}
else{
return <U extends true ? EnvType[T] : EnvType[T] | undefined>env;
}
}

switch(type){
case "string": return <EnvType[T]>env;
case "number": return <EnvType[T]>Number(env);
case "boolean": return <EnvType[T]>(env === "true");
default: throw new Error();
}
}
14 changes: 0 additions & 14 deletions src/json.deno.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {type Opt} from "./deep.ts";
import {mainPath} from "./path.deno.ts";

/**
* Read JSON file and convert to object.
Expand Down Expand Up @@ -49,17 +48,4 @@ export async function jsonLoad<T extends Opt<T>>(path:string, def:T):Promise<T>{
}

return def;
}

/**
* Wrapper function of `jsonLoad()`.
* Config file path is fixed `${Deno.mainModule}/config.json`.
* @example
* ```ts
* import dconfig from "./config.json" assert {type: "json"};
* const config = await configLoad(dconfig);
* ```
*/
export async function configLoad<T extends Opt<T>>(def:T):Promise<T>{
return await jsonLoad(`${mainPath()}/config.json`, def);
}
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/env.deno.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {assertEquals} from "../deps.test.ts";
import {envGet} from "../src/env.deno.ts";

Deno.env.set("TEST_ENV_1", "abc");
Deno.env.set("TEST_ENV_2", "123");
Deno.env.set("TEST_ENV_3", "true");

Deno.test({
name: "Env: Get",
fn(){
const env1 = envGet("TEST_ENV_1", "string", true);
const env2 = envGet("TEST_ENV_2", "number", true);
const env3 = envGet("TEST_ENV_3", "boolean", true);

assertEquals(env1, "abc");
assertEquals(env2, 123);
assertEquals(env3, true);
}
});
7 changes: 1 addition & 6 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ const sample = new Uint8Array([
Deno.test({
name: "Fetch: Get",
async fn(){
const ac = new AbortController();

const server = Deno.serve({
hostname: "127.0.0.1",
port: 62000,
signal: ac.signal
}, () => new Response(sample));

const result = await fetchExtend("http://127.0.0.1:62000", "byte");
await server.shutdown();

assertEquals(result, sample);

ac.abort();
await server.finished;
}
});
14 changes: 5 additions & 9 deletions test/json.deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../deps.test.ts";
import {jsonRead, jsonWrite, jsonLoad, configLoad} from "../src/json.deno.ts";
import {jsonRead, jsonWrite, jsonLoad} from "../src/json.deno.ts";

const object = <const>{
id: "22d8b040-8a63-46a0-8df6-0f508a778689",
Expand All @@ -25,17 +25,13 @@ Deno.test({
async fn(){
const path = "./resource.json";

const result01 = await jsonLoad(path, object);
const result02 = await configLoad(object);
assertEquals(result01, object);
assertEquals(result02, object);
const result1 = await jsonLoad(path, object);
assertEquals(result1, object);

const modify = structuredClone(object);
modify.name = "Ninja Slayer";

const result11 = await jsonLoad(path, modify);
const result12 = await configLoad(modify);
assertEquals(result11, object);
assertEquals(result12, object);
const result2 = await jsonLoad(path, modify);
assertEquals(result2, object);
}
});
2 changes: 1 addition & 1 deletion test/platform.deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Deno.test({

Deno.test({
ignore: Deno.build.os === "windows",
name: "Platform: Posix",
name: "Platform: Unix",
fn(){
assertEquals(isWin(), false);
}
Expand Down
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 f6a30c5

Please sign in to comment.