This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure codebase (#189)
* refactor: restructure codebase * fix test suite * refactor: remove store * fmt
- Loading branch information
1 parent
09cd49a
commit 29f5fe8
Showing
14 changed files
with
169 additions
and
280 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { parse } from 'https://deno.land/std@0.198.0/flags/mod.ts' | ||
import { | ||
brightGreen, | ||
brightRed, | ||
brightYellow, | ||
gray, | ||
} from 'https://deno.land/std@0.198.0/fmt/colors.ts' | ||
import byte from 'https://deno.land/x/byte@v3.3.0/byte.ts' | ||
import { logError } from '../../utils.ts' | ||
import { bundle } from './bundle.ts' | ||
|
||
export async function bundleCommand(args: ReturnType<typeof parse>) { | ||
const { _, target, runtime } = args | ||
|
||
const input = _[1] && typeof _[1] === 'string' ? _[1] : undefined | ||
const output = _[2] && typeof _[2] === 'string' ? _[2] : undefined | ||
|
||
try { | ||
const { outputSize } = await bundle({ | ||
input, | ||
output, | ||
// @ts-ignore: | ||
target: typeof target === 'string' ? target : undefined, | ||
// @ts-ignore: | ||
runtime: typeof runtime === 'string' ? runtime : undefined, | ||
}) | ||
|
||
console.info( | ||
gray( | ||
`file size - ${ | ||
outputSize < 1_000_000 | ||
? brightGreen(byte(outputSize)) | ||
: outputSize < 5_000_000 | ||
? brightYellow(byte(outputSize)) | ||
: brightRed(byte(outputSize)) | ||
}`, | ||
), | ||
) | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
logError(err.message) | ||
} else { | ||
logError('something went wrong trying to bundle your app.') | ||
} | ||
} | ||
} |
File renamed without changes.
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,14 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { encode } from 'https://deno.land/std@0.198.0/encoding/base64.ts' | ||
|
||
export async function createCryptoKey() { | ||
const key = await crypto.subtle.generateKey( | ||
{ name: 'AES-GCM', hash: 'SHA-512', length: 256 }, | ||
true, | ||
['encrypt', 'decrypt'], | ||
) | ||
|
||
const exportedKey = await crypto.subtle.exportKey('raw', key) | ||
|
||
return encode(exportedKey) | ||
} |
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,14 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { encode } from 'https://deno.land/std@0.198.0/encoding/base64.ts' | ||
|
||
export async function createJwtSecret() { | ||
const key = await crypto.subtle.generateKey( | ||
{ name: 'HMAC', hash: 'SHA-512' }, | ||
true, | ||
['sign', 'verify'], | ||
) | ||
|
||
const exportedKey = await crypto.subtle.exportKey('raw', key) | ||
|
||
return encode(exportedKey) | ||
} |
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,8 +1,6 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { brightRed, gray } from 'https://deno.land/std@0.198.0/fmt/colors.ts' | ||
|
||
export const log = { | ||
error(message: string) { | ||
console.error(gray(`${brightRed('error')} - ${message}`)) | ||
}, | ||
export function logError(message: string) { | ||
console.error(gray(`${brightRed('error')} - ${message}`)) | ||
} |
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,56 @@ | ||
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license. | ||
import { decode } from 'https://deno.land/std@0.198.0/encoding/base64.ts' | ||
import { Context } from './context.ts' | ||
|
||
export async function encrypt(c: Context, message: string) { | ||
const key = (c.env('crypto_key') ?? c.env('CRYPTO_KEY')) as string | ||
|
||
const iv = crypto.getRandomValues(new Uint8Array(12)) | ||
const ivStr = Array.from(iv) | ||
.map((byte) => String.fromCharCode(byte)) | ||
.join('') | ||
const alg = { name: 'AES-GCM', iv } | ||
const cryptoKey = await crypto.subtle.importKey( | ||
'raw', | ||
decode(key).buffer, | ||
alg, | ||
true, | ||
['encrypt', 'decrypt'], | ||
) | ||
const cipherBuf = await crypto.subtle.encrypt( | ||
alg, | ||
cryptoKey, | ||
new TextEncoder().encode(message), | ||
) | ||
const cipherArr = Array.from(new Uint8Array(cipherBuf)) | ||
const cipherStr = cipherArr.map((byte) => String.fromCharCode(byte)) | ||
.join('') | ||
|
||
return btoa(ivStr + cipherStr) | ||
} | ||
|
||
export async function decrypt(c: Context, message: string) { | ||
const key = (c.env('crypto_key') ?? c.env('CRYPTO_KEY')) as string | ||
|
||
const iv = atob(message).slice(0, 12) | ||
const alg = { | ||
name: 'AES-GCM', | ||
iv: new Uint8Array( | ||
Array.from(iv).map((char) => char.charCodeAt(0)), | ||
), | ||
} | ||
const cryptoKey = await crypto.subtle.importKey( | ||
'raw', | ||
decode(key).buffer, | ||
alg, | ||
true, | ||
['encrypt', 'decrypt'], | ||
) | ||
const cipherStr = atob(message).slice(12) | ||
const cipherBuf = new Uint8Array( | ||
Array.from(cipherStr).map((char) => char.charCodeAt(0)), | ||
) | ||
const buf = await crypto.subtle.decrypt(alg, cryptoKey, cipherBuf) | ||
|
||
return new TextDecoder().decode(buf) | ||
} |
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
Oops, something went wrong.