From c4b1f061775c0fe16e47db906ca00ecae4370e1a Mon Sep 17 00:00:00 2001 From: Joe Ma Date: Thu, 2 Nov 2023 17:22:10 +0800 Subject: [PATCH] Format Code Signed-off-by: Joe Ma --- src/index.ts | 133 ++++++++++++++++--------------- src/types.d.ts | 10 +++ {web => static}/.eslintrc.json | 0 {web => static}/v1/paste.html | 0 {web => static}/v2/css/paste.css | 0 {web => static}/v2/js/paste.js | 0 {web => static}/v2/paste.html | 0 7 files changed, 80 insertions(+), 63 deletions(-) create mode 100644 src/types.d.ts rename {web => static}/.eslintrc.json (100%) rename {web => static}/v1/paste.html (100%) rename {web => static}/v2/css/paste.css (100%) rename {web => static}/v2/js/paste.js (100%) rename {web => static}/v2/paste.html (100%) diff --git a/src/index.ts b/src/index.ts index bb67099..6560087 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,15 +16,16 @@ * along with this program. If not, see . */ -import {AwsClient} from 'aws4fetch'; -import {customAlphabet} from 'nanoid'; -import {sha256} from 'js-sha256'; +import { AwsClient } from 'aws4fetch'; +import { customAlphabet } from 'nanoid'; +import { sha256 } from 'js-sha256'; import dedent from 'dedent-js'; +import { PasteIndexEntry } from './types'; // Constants const SERVICE_URL = 'pb.nekoid.cc'; -const PASTE_WEB_URL_v1 = 'https://raw.githubusercontent.com/rikkaneko/paste/main/web/v1'; -const PASTE_WEB_URL = 'https://raw.githubusercontent.com/rikkaneko/paste/main/web/v2'; +const PASTE_WEB_URL_v1 = 'https://raw.githubusercontent.com/rikkaneko/paste/main/static/v1'; +const PASTE_WEB_URL = 'https://raw.githubusercontent.com/rikkaneko/paste/main/static/v2'; const UUID_LENGTH = 4; export interface Env { @@ -35,23 +36,20 @@ export interface Env { ENDPOINT: string; } -const gen_id = customAlphabet( - '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', UUID_LENGTH); +const gen_id = customAlphabet('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', UUID_LENGTH); export default { - async fetch( - request: Request, - env: Env, - ctx: ExecutionContext, - ): Promise { - const {url, method, headers} = request; - const {pathname, searchParams} = new URL(url); + async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + const { url, method, headers } = request; + const { pathname, searchParams } = new URL(url); const path = pathname.replace(/\/+$/, '') || '/'; let cache = caches.default; const agent = headers.get('user-agent') ?? ''; // Detect if request from browsers - const is_browser = ['Chrome', 'Mozilla', 'AppleWebKit', 'Safari', 'Gecko', 'Chromium'].some(v => agent.includes(v)); + const is_browser = ['Chrome', 'Mozilla', 'AppleWebKit', 'Safari', 'Gecko', 'Chromium'].some((v) => + agent.includes(v) + ); const s3 = new AwsClient({ accessKeyId: env.AWS_ACCESS_KEY_ID, @@ -200,10 +198,12 @@ export default { // Check password rules if (password && !check_password_rules(password)) { - return new Response('Invalid password. ' + - 'Password must contain alphabets and digits only, and has a length of 4 or more.', { - status: 422, - }); + return new Response( + 'Invalid password. ' + 'Password must contain alphabets and digits only, and has a length of 4 or more.', + { + status: 422, + } + ); } // Check request.body size <= 25MB @@ -239,16 +239,14 @@ export default { }; // Key will be expired after 28 day if unmodified - ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {expirationTtl: 2419200})); + ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), { expirationTtl: 2419200 })); return await get_paste_info(uuid, descriptor, env, is_browser, need_qrcode, reply_json); } else { return new Response('Unable to upload the paste.\n', { status: 500, }); } - } - } else if (path.length >= UUID_LENGTH + 1) { // RegExpr to match //