-
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.
* update deps. * remove configload. * update version. * feat: env * typo. * v3 -> v4 * feat: stream.
- Loading branch information
Showing
18 changed files
with
126 additions
and
45 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 |
---|---|---|
@@ -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"; |
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 |
---|---|---|
@@ -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"; |
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,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(); | ||
} | ||
} |
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 {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); | ||
} | ||
}); |
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,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); | ||
} | ||
}); |