diff --git a/src/base64.ts b/src/base64.ts index 62939fd..7263abc 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -7,7 +7,7 @@ * const restored = base64Decode(converted); * ``` */ -export function base64Encode(data:Uint8Array){ +export function base64Encode(data:Uint8Array):string{ return btoa([...data].map(n => String.fromCharCode(n)).join("")); } @@ -20,6 +20,6 @@ export function base64Encode(data:Uint8Array){ * const restored = base64Decode(converted); * ``` */ -export function base64Decode(data:string){ +export function base64Decode(data:string):Uint8Array{ return new Uint8Array([...atob(data)].map(s => s.charCodeAt(0))); } \ No newline at end of file diff --git a/src/crypto.ts b/src/crypto.ts index ec8b216..20b3f0d 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -50,7 +50,7 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ * const uuid = cryptoUuid(); * ``` */ -export function cryptoUuid(){ +export function cryptoUuid():string{ return crypto.randomUUID(); } @@ -61,7 +61,7 @@ export function cryptoUuid(){ * const random = cryptoRandom(16); * ``` */ -export function cryptoRandom(n:number){ +export function cryptoRandom(n:number):Uint8Array{ return crypto.getRandomValues(new Uint8Array(n)); } @@ -73,7 +73,7 @@ export function cryptoRandom(n:number){ * const hash = await cryptoHash(256, bin); * ``` */ -export async function cryptoHash(bit:256|384|512, data:Uint8Array){ +export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise{ return new Uint8Array(await crypto.subtle.digest(`SHA-${bit}`, data)); } @@ -115,7 +115,7 @@ export async function cryptoGenerateKey(isECDH:boolean):Promise{ const gcm = aesGcmConfig(cryptoRandom(sizeIv)); const output = new Uint8Array((gcm.tagLength ?? 0 / 8) + gcm.iv.byteLength + data.byteLength); output.set(gcm.iv, 0); @@ -143,7 +143,7 @@ export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPai * }, converted); * ``` */ -export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array){ +export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ const gcm = aesGcmConfig(data.subarray(0, sizeIv)); return new Uint8Array(await crypto.subtle.decrypt(gcm, await deriveSecretKey({publicKey, privateKey}), data.subarray(gcm.iv.byteLength))); @@ -159,7 +159,7 @@ export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPai * const verified = await cryptoVerify(key.publicKey, signature, bin); * ``` */ -export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array){ +export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array):Promise{ return new Uint8Array(await crypto.subtle.sign(dsaHash, await crypto.subtle.importKey("pkcs8", privateKey, dsaKey, false, ["sign"]), data)); } @@ -173,6 +173,6 @@ export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array){ * const verified = await cryptoVerify(key.publicKey, signature, bin); * ``` */ -export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array){ +export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array):Promise{ return await crypto.subtle.verify(dsaHash, await crypto.subtle.importKey("spki", publicKey, dsaKey, false, ["verify"]), signature, data); } \ No newline at end of file diff --git a/src/deflate.ts b/src/deflate.ts index 41d4d9f..75f7894 100644 --- a/src/deflate.ts +++ b/src/deflate.ts @@ -12,7 +12,7 @@ async function streamConvert(data:Uint8Array, ts:TransformStream{ return await streamConvert(data, new CompressionStream("deflate-raw")); } @@ -26,6 +26,6 @@ export async function deflateEncode(data:Uint8Array){ * const restored = await deflateDecode(converted); * ``` */ -export async function deflateDecode(data:Uint8Array){ +export async function deflateDecode(data:Uint8Array):Promise{ return await streamConvert(data, new DecompressionStream("deflate-raw")); } \ No newline at end of file diff --git a/src/fetch.ts b/src/fetch.ts index bb70cc3..a831001 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -15,7 +15,7 @@ export interface FetchInit extends Omit{ /** * Map of fetch response type and string specify them. */ -export interface FetchResponseType{ +export interface ResponseType{ "text": string; "json": JsonStruct; "form": FormData; @@ -35,7 +35,7 @@ export interface FetchResponseType{ * const response = await fetchExtend("./asset", "byte"); * ``` */ -export async function fetchExtend(path:string, type:T, option?:FetchInit){ +export async function fetchExtend(path:string, type:T, option?:FetchInit):Promise{ const {origin, pathname} = /^http(s|):\/\//i.test(path) ? new URL(path) : new URL(path, location.href); const query = new URLSearchParams(option?.query).toString(); @@ -56,16 +56,16 @@ export async function fetchExtend(path:string }); switch(type){ - case "text": return await response.text(); - case "json": return await response.json(); - case "form": return await response.formData(); - case "byte": return new Uint8Array(await response.arrayBuffer()); - case "buffer": return await response.arrayBuffer(); - case "blob": return await response.blob(); - case "ok": return response.ok; - case "code": return response.status; - case "header": return response.headers; - case "response": return response; + case "text": return await response.text(); + case "json": return await response.json(); + case "form": return await response.formData(); + case "byte": return new Uint8Array(await response.arrayBuffer()); + case "buffer": return await response.arrayBuffer(); + case "blob": return await response.blob(); + case "ok": return response.ok; + case "code": return response.status; + case "header": return response.headers; + case "response": return response; default: throw new Error(); } } \ No newline at end of file diff --git a/src/minipack.ts b/src/minipack.ts index 9883d57..9282676 100644 --- a/src/minipack.ts +++ b/src/minipack.ts @@ -21,7 +21,7 @@ export type FileInit = [string, Uint8Array]; * const restored = minipackDecode(converted); * ``` */ -export function minipackEncode(files:FileInit[]){ +export function minipackEncode(files:FileInit[]):Uint8Array{ const archive = new Uint8Array(files.reduce((a, [k, v]) => a + sizeName + sizeBody + utfEncode(k).byteLength + v.byteLength, 0)); let i = 0; @@ -59,7 +59,7 @@ export function minipackEncode(files:FileInit[]){ * const restored = minipackDecode(converted); * ``` */ -export function minipackDecode(archive:Uint8Array){ +export function minipackDecode(archive:Uint8Array):FileInit[]{ const files:FileInit[] = []; for(let i = 0; i < archive.byteLength; false){ diff --git a/src/path.deno.ts b/src/path.deno.ts index fc2f6f6..7d7cc73 100644 --- a/src/path.deno.ts +++ b/src/path.deno.ts @@ -9,7 +9,7 @@ import {isWin} from "./platform.deno.ts"; * const path = slashUnix("C:\\file"); * ``` */ -export function slashUnix(path:string){ +export function slashUnix(path:string):string{ return path.replaceAll("\\", "/"); } @@ -21,7 +21,7 @@ export function slashUnix(path:string){ * const path = slashWin("C:/file"); * ``` */ -export function slashWin(path:string){ +export function slashWin(path:string):string{ return path.replaceAll("/", "\\"); } @@ -33,7 +33,7 @@ export function slashWin(path:string){ * const path = tmpPath(); * ``` */ -export function tmpPath(){ +export function tmpPath():string{ return isWin() ? "C:/Windows/Temp" : "/tmp"; } @@ -45,7 +45,7 @@ export function tmpPath(){ * const path = dataPath(); * ``` */ -export function dataPath(){ +export function dataPath():string{ return isWin() ? "C:/ProgramData" : "/var"; } @@ -57,7 +57,7 @@ export function dataPath(){ * const path = homePath(); * ``` */ -export function homePath(){ +export function homePath():string{ const {HOME, USERPROFILE} = Deno.env.toObject(); return isWin() ? slashUnix(USERPROFILE) : HOME; @@ -70,7 +70,7 @@ export function homePath(){ * const path = mainPath(); * ``` */ -export function mainPath(){ +export function mainPath():string{ const path = fromFileUrl(dirname(Deno.mainModule)); return isWin() ? slashUnix(path) : path; diff --git a/src/platform.deno.ts b/src/platform.deno.ts index 2072783..90e12d2 100644 --- a/src/platform.deno.ts +++ b/src/platform.deno.ts @@ -5,6 +5,6 @@ * const runOnWin = isWin(); * ``` */ -export function isWin(){ +export function isWin():boolean{ return Deno.build.os === "windows"; } \ No newline at end of file diff --git a/src/process.deno.ts b/src/process.deno.ts index 6b3bd86..3d09f01 100644 --- a/src/process.deno.ts +++ b/src/process.deno.ts @@ -5,7 +5,7 @@ * const success = executeCommand(["echo", "foobar"]); * ``` */ -export async function runCommand(...commands:string[]){ +export async function runCommand(...commands:string[]):Promise{ const {success} = await new Deno.Command(commands.shift() ?? "", { args: commands, stdin: "null", diff --git a/src/text.ts b/src/text.ts index b70ec16..811e2b6 100644 --- a/src/text.ts +++ b/src/text.ts @@ -7,7 +7,7 @@ * const restored = utfDecode(converted); * ``` */ -export function utfEncode(data:string){ +export function utfEncode(data:string):Uint8Array{ return new TextEncoder().encode(data); } @@ -20,7 +20,7 @@ export function utfEncode(data:string){ * const restored = utfDecode(converted); * ``` */ -export function utfDecode(data:Uint8Array){ +export function utfDecode(data:Uint8Array):string{ return new TextDecoder().decode(data); } @@ -33,7 +33,7 @@ export function utfDecode(data:Uint8Array){ * const restored = hexDecode(converted); * ``` */ -export function hexEncode(data:Uint8Array){ +export function hexEncode(data:Uint8Array):string{ return [...data].map(n => n.toString(16).toUpperCase().padStart(2, "0")).join(""); } @@ -46,7 +46,7 @@ export function hexEncode(data:Uint8Array){ * const restored = hexDecode(converted); * ``` */ -export function hexDecode(data:string){ +export function hexDecode(data:string):Uint8Array{ return new Uint8Array(data.match(/[0-9a-fA-F]{2}/g)?.map(s => parseInt(s, 16)) ?? []); } @@ -58,7 +58,7 @@ export function hexDecode(data:string){ * const formated = trimExtend(text); * ``` */ -export function trimExtend(data:string){ +export function trimExtend(data:string):string{ return data.trim().replace(/\r/g, "").replace(/\t/g, " ").replace(/ +/g, " ").replace(/ +$/mg, ""); } @@ -71,6 +71,6 @@ export function trimExtend(data:string){ * const characters = accurateSegment(text); * ``` */ -export function accurateSegment(data:string){ +export function accurateSegment(data:string):string[]{ return [...new Intl.Segmenter().segment(data)].map(({segment}) => segment); } \ No newline at end of file diff --git a/src/time.ts b/src/time.ts index cbe4c59..4b8a5a0 100644 --- a/src/time.ts +++ b/src/time.ts @@ -7,7 +7,7 @@ * const date = unixtimeDecode(time); * ``` */ -export function unixtimeEncode(date?:Date){ +export function unixtimeEncode(date?:Date):number{ return Math.floor((date ?? new Date()).getTime() / 1000); } @@ -20,7 +20,7 @@ export function unixtimeEncode(date?:Date){ * const date = unixtimeDecode(time); * ``` */ -export function unixtimeDecode(time:number){ +export function unixtimeDecode(time:number):Date{ return new Date(time * 1000); } @@ -31,7 +31,7 @@ export function unixtimeDecode(time:number){ * const time = unixtimeParse("2023-05-18T08:31:32.292Z"); * ``` */ -export function unixtimeParse(ds:string){ +export function unixtimeParse(ds:string):number{ const [y, mo, d, h, mi, s] = ds.split(/[/ :TZ_.-]/i).map(s => Number(s)); return unixtimeEncode(new Date(y, (mo ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0));