diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 81f6bb5..15cb4bb 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,5 +1,5 @@ -name: Release -"on": +name: release +on: push: tags: v[0-9]+.[0-9]+.[0-9]+ jobs: @@ -14,7 +14,7 @@ jobs: with: deno-version: v1.x - name: install esbuild - run: curl -L $(curl -L https://esbuild.github.io/getting-started | grep -o -E 'https:[^"]+linux-x64[^"]+\.tgz' | uniq) | tar x -z --strip-components 2 + run: curl -L $(curl -L https://esbuild.github.io/getting-started | grep -oE -e 'https:[^"]+linux-x64[^"]+\.tgz' | uniq) | tar x -z --strip-components 2 - name: bundle universal module run: deno bundle ./mod.universal.ts | ./esbuild --minify | head -c -1 > ./mod.universal.min.js - name: dispatch release diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 13896e0..4d276e8 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,5 +1,5 @@ -name: Test -"on": +name: test +on: push: branches: - dev diff --git a/.github/workflows_json/release.json b/.github/workflows_json/release.json index 411cc7b..d4e9687 100644 --- a/.github/workflows_json/release.json +++ b/.github/workflows_json/release.json @@ -1,5 +1,5 @@ { - "name": "Release", + "name": "release", "on": { "push": { "tags": "v[0-9]+.[0-9]+.[0-9]+" @@ -20,7 +20,7 @@ } }, { "name": "install esbuild", - "run": "curl -L $(curl -L https://esbuild.github.io/getting-started | grep -o -E 'https:[^\"]+linux-x64[^\"]+\\.tgz' | uniq) | tar x -z --strip-components 2" + "run": "curl -L $(curl -L https://esbuild.github.io/getting-started | grep -oE -e 'https:[^\"]+linux-x64[^\"]+\\.tgz' | uniq) | tar x -z --strip-components 2" }, { "name": "bundle universal module", "run": "deno bundle ./mod.universal.ts | ./esbuild --minify | head -c -1 > ./mod.universal.min.js" diff --git a/.github/workflows_json/test.json b/.github/workflows_json/test.json index 1b6ecd4..226d42a 100644 --- a/.github/workflows_json/test.json +++ b/.github/workflows_json/test.json @@ -1,5 +1,5 @@ { - "name": "Test", + "name": "test", "on": { "push": { "branches": [ diff --git a/.github/workflows_json/to_yaml.sh b/.github/workflows_json/to_yaml.sh index c052906..4878624 100644 --- a/.github/workflows_json/to_yaml.sh +++ b/.github/workflows_json/to_yaml.sh @@ -3,4 +3,4 @@ set -eu cd ${0%/*} -yq -P -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml \ No newline at end of file +yq -o y -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml \ No newline at end of file diff --git a/mod.ts b/mod.ts index ccb57bc..35f3f99 100644 --- a/mod.ts +++ b/mod.ts @@ -8,6 +8,4 @@ export * from "./src/time.ts"; export * from "./src/path.deno.ts"; export * from "./src/platform.deno.ts"; -export * from "./src/process.deno.ts"; - -export type * from "./src/core.d.ts"; \ No newline at end of file +export * from "./src/process.deno.ts"; \ No newline at end of file diff --git a/src/core.d.ts b/src/core.d.ts deleted file mode 100644 index 434b75c..0000000 --- a/src/core.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** -* Possible types of JSON. -*/ -export type JsonStruct = string | number | boolean | null | JsonStruct[] | {[key: string]: JsonStruct}; - -/** -* ECMAScript primitive types. -*/ -export type PrimitiveValue = string | number | bigint | boolean | symbol; - -/** -* Simple `id` and `password` pair. -*/ -export interface IdCredential{ - id: string; - pw: string; -} \ No newline at end of file diff --git a/src/crypto.ts b/src/crypto.ts index 385d4d6..2d07d51 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -1,12 +1,7 @@ -/** -* Serialized `CryptoKey`. -*/ -export type PortableCryptoKey = Uint8Array; - /** * Serialized `CryptoKeyPair`. */ -export type PortableCryptoKeyPair = Record; +export type PortableCryptoKeyPair = Record; async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ return await crypto.subtle.deriveKey({ @@ -28,10 +23,10 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ * Generate UUIDv4 string. * @example * ```ts -* const uuid = cryptoUuid(); +* const uuid = randomUuid(); * ``` */ -export function cryptoUuid():string{ +export function randomUuid():string{ return crypto.randomUUID(); } @@ -39,10 +34,10 @@ export function cryptoUuid():string{ * Generate random binary with any number of bytes. * @example * ```ts -* const random = cryptoRandom(16); +* const random = randomBin(16); * ``` */ -export function cryptoRandom(n:number):Uint8Array{ +export function randomBin(n:number):Uint8Array{ return crypto.getRandomValues(new Uint8Array(n)); } @@ -51,10 +46,10 @@ export function cryptoRandom(n:number):Uint8Array{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const hash = await cryptoHash(256, bin); +* const hash = await hashValue(256, bin); * ``` */ -export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise{ +export async function hashValue(bit:256 | 384 | 512, data:Uint8Array):Promise{ return new Uint8Array(await crypto.subtle.digest(`SHA-${bit}`, data)); } @@ -64,15 +59,15 @@ export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise{ +export async function pubkeyGen(usage:"ECDH" | "ECDSA"):Promise{ const {publicKey, privateKey} = await crypto.subtle.generateKey({ - name: isECDH ? "ECDH" : "ECDSA", + name: usage, namedCurve: "P-521" - }, true, isECDH ? ["deriveKey", "deriveBits"] : ["sign", "verify"]); + }, true, usage === "ECDH" ? ["deriveKey", "deriveBits"] : ["sign", "verify"]); return { publicKey: new Uint8Array(await crypto.subtle.exportKey("spki", publicKey)), @@ -87,20 +82,20 @@ export async function cryptoGenerateKey(isECDH:boolean):Promise{ - const iv = cryptoRandom(12); +export async function pubkeyEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ + const iv = randomBin(12); const enc = await crypto.subtle.encrypt({ name: "AES-GCM", tagLength: 128, @@ -121,19 +116,19 @@ export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPai * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key1 = await cryptoGenerateKey(true); -* const key2 = await cryptoGenerateKey(true); -* const converted = await cryptoEncrypt({ +* const key1 = await pubkeyGen("ECDH"); +* const key2 = await pubkeyGen("ECDH"); +* const converted = await pubkeyEncrypt({ * publicKey: key1.publicKey, * privateKey: key2.privateKey * }, bin); -* const restored = await cryptoDecrypt({ +* const restored = await pubkeyDecrypt({ * publicKey: key2.publicKey, * privateKey: key1.privateKey * }, converted); * ``` */ -export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ +export async function pubkeyDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ const iv = data.subarray(0, 12); const dec = await crypto.subtle.decrypt({ name: "AES-GCM", @@ -149,12 +144,12 @@ export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPai * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key = await cryptoGenerateKey(false); -* const signature = await cryptoSign(key.privateKey, bin); -* const verified = await cryptoVerify(key.publicKey, signature, bin); +* const key = await pubkeyGen("ECDSA"); +* const signature = await pubkeySign(key.privateKey, bin); +* const verified = await pubkeyVerify(key.publicKey, signature, bin); * ``` */ -export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array):Promise{ +export async function pubkeySign(privateKey:Uint8Array, data:Uint8Array):Promise{ const sign = await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-512" @@ -171,12 +166,12 @@ export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array): * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key = await cryptoGenerateKey(false); -* const signature = await cryptoSign(key.privateKey, bin); -* const verified = await cryptoVerify(key.publicKey, signature, bin); +* const key = await pubkeyGen("ECDSA"); +* const signature = await pubkeySign(key.privateKey, bin); +* const verified = await pubkeyVerify(key.publicKey, signature, bin); * ``` */ -export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array):Promise{ +export async function pubkeyVerify(publicKey:Uint8Array, signature:Uint8Array, data:Uint8Array):Promise{ return await crypto.subtle.verify({ name: "ECDSA", hash: "SHA-512" diff --git a/src/fetch.ts b/src/fetch.ts index a831001..76545d2 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,5 +1,3 @@ -import {type JsonStruct} from "./core.d.ts"; - /** * Possible input for `URLSearchParams`. */ @@ -17,7 +15,7 @@ export interface FetchInit extends Omit{ */ export interface ResponseType{ "text": string; - "json": JsonStruct; + "json": unknown; "form": FormData; "byte": Uint8Array; "buffer": ArrayBuffer; diff --git a/src/process.deno.ts b/src/process.deno.ts index 3d09f01..bb3ac23 100644 --- a/src/process.deno.ts +++ b/src/process.deno.ts @@ -2,7 +2,7 @@ * Run command as subprocess. * @example * ```ts -* const success = executeCommand(["echo", "foobar"]); +* const success = await runCommand(["echo", "foobar"]); * ``` */ export async function runCommand(...commands:string[]):Promise{ diff --git a/test/crypto.test.ts b/test/crypto.test.ts index ee03f95..929f02b 100644 --- a/test/crypto.test.ts +++ b/test/crypto.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {cryptoUuid, cryptoRandom, cryptoHash, cryptoGenerateKey, cryptoEncrypt, cryptoDecrypt, cryptoSign, cryptoVerify} from "../src/crypto.ts"; +import {randomUuid, randomBin, hashValue, pubkeyGen, pubkeyEncrypt, pubkeyDecrypt, pubkeySign, pubkeyVerify} from "../src/crypto.ts"; const sample = new Uint8Array([0x02, 0xF2, 0x5D, 0x1F, 0x1C, 0x34, 0xB9, 0x2F]); @@ -17,7 +17,7 @@ const hashResult = new Uint8Array([ Deno.test({ name: "Crypto: UUID", fn(){ - const uuid = cryptoUuid(); + const uuid = randomUuid(); assertEquals(uuid.length, 36); } @@ -26,7 +26,7 @@ Deno.test({ Deno.test({ name: "Crypto: Random", fn(){ - const random = cryptoRandom(16); + const random = randomBin(16); assertEquals(random.byteLength, 16); } @@ -35,7 +35,7 @@ Deno.test({ Deno.test({ name: "Crypto: Hash", async fn(){ - const hash = await cryptoHash(512, sample); + const hash = await hashValue(512, sample); assertEquals(hash, hashResult); } @@ -45,15 +45,15 @@ Deno.test({ ignore: true, name: "Crypto: Encrypt and Decrypt", async fn(){ - const key1 = await cryptoGenerateKey(true); - const key2 = await cryptoGenerateKey(true); + const key1 = await pubkeyGen("ECDH"); + const key2 = await pubkeyGen("ECDH"); - const encrypt = await cryptoEncrypt({ + const encrypt = await pubkeyEncrypt({ publicKey: key1.publicKey, privateKey: key2.privateKey }, sample); - const decrypt = await cryptoDecrypt({ + const decrypt = await pubkeyDecrypt({ publicKey: key2.publicKey, privateKey: key1.privateKey }, encrypt); @@ -66,9 +66,9 @@ Deno.test({ ignore: true, name: "Crypto: Sign and Verify", async fn(){ - const key = await cryptoGenerateKey(false); - const signature = await cryptoSign(key.privateKey, sample); - const result = await cryptoVerify(key.publicKey, signature, sample); + const key = await pubkeyGen("ECDSA"); + const signature = await pubkeySign(key.privateKey, sample); + const result = await pubkeyVerify(key.publicKey, signature, sample); assertEquals(result, true); }