From f616ed9abe3e3cc28d3c96894020897796bfbde2 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 29 Jan 2024 22:21:09 +0900 Subject: [PATCH] chore(lint): set `curly` rule as `all` (#2112) * chore(lint): set `curly` rule as `all` * denoify --- deno_dist/adapter/deno/serve-static.ts | 4 ++- deno_dist/client/client.ts | 8 ++++-- deno_dist/helper/adapter/index.ts | 28 ++++++++++++++----- deno_dist/helper/cookie/index.ts | 16 ++++++++--- deno_dist/hono-base.ts | 8 ++++-- deno_dist/request.ts | 16 ++++++++--- deno_dist/router/reg-exp-router/router.ts | 4 ++- deno_dist/router/trie-router/node.ts | 8 ++++-- deno_dist/utils/cookie.ts | 26 ++++++++++++----- deno_dist/utils/filepath.ts | 4 ++- deno_dist/utils/mime.ts | 4 ++- deno_dist/utils/url.ts | 4 ++- package.json | 2 +- runtime_tests/lambda/index.test.ts | 4 ++- src/adapter/aws-lambda/handler.ts | 4 ++- src/adapter/bun/serve-static.ts | 4 ++- .../cloudflare-workers/serve-static.ts | 4 ++- src/adapter/deno/serve-static.ts | 4 ++- src/client/client.ts | 8 ++++-- src/helper/adapter/index.ts | 28 ++++++++++++++----- src/helper/cookie/index.test.ts | 4 ++- src/helper/cookie/index.ts | 16 ++++++++--- src/hono-base.ts | 8 ++++-- src/request.ts | 16 ++++++++--- src/router/reg-exp-router/router.ts | 4 ++- src/router/trie-router/node.ts | 8 ++++-- src/utils/cookie.ts | 26 ++++++++++++----- src/utils/filepath.ts | 4 ++- src/utils/mime.ts | 4 ++- src/utils/url.ts | 4 ++- yarn.lock | 8 +++--- 31 files changed, 214 insertions(+), 76 deletions(-) diff --git a/deno_dist/adapter/deno/serve-static.ts b/deno_dist/adapter/deno/serve-static.ts index 8cecd4fc5..7b572b9ca 100644 --- a/deno_dist/adapter/deno/serve-static.ts +++ b/deno_dist/adapter/deno/serve-static.ts @@ -34,7 +34,9 @@ export const serveStatic = ( defaultDocument: DEFAULT_DOCUMENT, }) - if (!path) return await next() + if (!path) { + return await next() + } path = `./${path}` diff --git a/deno_dist/client/client.ts b/deno_dist/client/client.ts index 08a22422a..eaf652e91 100644 --- a/deno_dist/client/client.ts +++ b/deno_dist/client/client.ts @@ -8,7 +8,9 @@ import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './util const createProxy = (callback: Callback, path: string[]) => { const proxy: unknown = new Proxy(() => {}, { get(_obj, key) { - if (typeof key !== 'string' || key === 'then') return undefined + if (typeof key !== 'string' || key === 'then') { + return undefined + } return createProxy(callback, [...path, key]) }, apply(_1, _2, args) { @@ -100,7 +102,9 @@ class ClientRequestImpl { headerValues['Cookie'] = cookies.join(',') } - if (this.cType) headerValues['Content-Type'] = this.cType + if (this.cType) { + headerValues['Content-Type'] = this.cType + } const headers = new Headers(headerValues ?? undefined) let url = this.url diff --git a/deno_dist/helper/adapter/index.ts b/deno_dist/helper/adapter/index.ts index 4c7141100..7d9a5f2c1 100644 --- a/deno_dist/helper/adapter/index.ts +++ b/deno_dist/helper/adapter/index.ts @@ -43,13 +43,27 @@ export const getRuntimeKey = () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any - if (global?.Deno !== undefined) return 'deno' - if (global?.Bun !== undefined) return 'bun' - if (typeof global?.WebSocketPair === 'function') return 'workerd' - if (typeof global?.EdgeRuntime === 'string') return 'edge-light' - if (global?.fastly !== undefined) return 'fastly' - if (global?.__lagon__ !== undefined) return 'lagon' - if (global?.process?.release?.name === 'node') return 'node' + if (global?.Deno !== undefined) { + return 'deno' + } + if (global?.Bun !== undefined) { + return 'bun' + } + if (typeof global?.WebSocketPair === 'function') { + return 'workerd' + } + if (typeof global?.EdgeRuntime === 'string') { + return 'edge-light' + } + if (global?.fastly !== undefined) { + return 'fastly' + } + if (global?.__lagon__ !== undefined) { + return 'lagon' + } + if (global?.process?.release?.name === 'node') { + return 'node' + } return 'other' } diff --git a/deno_dist/helper/cookie/index.ts b/deno_dist/helper/cookie/index.ts index 2d4ce36e2..b75d7f372 100644 --- a/deno_dist/helper/cookie/index.ts +++ b/deno_dist/helper/cookie/index.ts @@ -15,11 +15,15 @@ interface GetSignedCookie { export const getCookie: GetCookie = (c, key?) => { const cookie = c.req.raw.headers.get('Cookie') if (typeof key === 'string') { - if (!cookie) return undefined + if (!cookie) { + return undefined + } const obj = parse(cookie, key) return obj[key] } - if (!cookie) return {} + if (!cookie) { + return {} + } const obj = parse(cookie) // eslint-disable-next-line @typescript-eslint/no-explicit-any return obj as any @@ -28,11 +32,15 @@ export const getCookie: GetCookie = (c, key?) => { export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => { const cookie = c.req.raw.headers.get('Cookie') if (typeof key === 'string') { - if (!cookie) return undefined + if (!cookie) { + return undefined + } const obj = await parseSigned(cookie, secret, key) return obj[key] } - if (!cookie) return {} + if (!cookie) { + return {} + } const obj = await parseSigned(cookie, secret) // eslint-disable-next-line @typescript-eslint/no-explicit-any return obj as any diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index c42b3361f..6ad4920e1 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -101,7 +101,9 @@ class Hono< // Implementation of app.on(method, path, ...handlers[]) this.on = (method: string | string[], path: string, ...handlers: H[]) => { - if (!method) return this + if (!method) { + return this + } this.#path = path for (const m of [method].flat()) { handlers.map((handler) => { @@ -236,7 +238,9 @@ class Hono< ...optionsArray ) - if (res) return res + if (res) { + return res + } await next() } diff --git a/deno_dist/request.ts b/deno_dist/request.ts index 12194e57a..1faba9af1 100644 --- a/deno_dist/request.ts +++ b/deno_dist/request.ts @@ -94,7 +94,9 @@ export class HonoRequest

{ header(name: string): string | undefined header(): Record header(name?: string) { - if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined + if (name) { + return this.raw.headers.get(name.toLowerCase()) ?? undefined + } const headerData: Record = {} this.raw.headers.forEach((value, key) => { @@ -127,7 +129,9 @@ export class HonoRequest

{ cookie(key?: string) { const cookie = this.raw.headers.get('Cookie') - if (!cookie) return + if (!cookie) { + return + } const obj = parse(cookie) if (key) { const value = obj[key] @@ -138,7 +142,9 @@ export class HonoRequest

{ } async parseBody(options?: ParseBodyOptions): Promise { - if (this.bodyCache.parsedBody) return this.bodyCache.parsedBody as T + if (this.bodyCache.parsedBody) { + return this.bodyCache.parsedBody as T + } const parsedBody = await parseBody(this, options) this.bodyCache.parsedBody = parsedBody return parsedBody @@ -147,7 +153,9 @@ export class HonoRequest

{ private cachedBody = (key: keyof Body) => { const { bodyCache, raw } = this const cachedBody = bodyCache[key] - if (cachedBody) return cachedBody + if (cachedBody) { + return cachedBody + } /** * If an arrayBuffer cache is exist, * use it for creating a text, json, and others. diff --git a/deno_dist/router/reg-exp-router/router.ts b/deno_dist/router/reg-exp-router/router.ts index 4c65a55b2..b43e8280f 100644 --- a/deno_dist/router/reg-exp-router/router.ts +++ b/deno_dist/router/reg-exp-router/router.ts @@ -137,7 +137,9 @@ export class RegExpRouter implements Router { throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT) } - if (methodNames.indexOf(method) === -1) methodNames.push(method) + if (methodNames.indexOf(method) === -1) { + methodNames.push(method) + } if (!middleware[method]) { ;[middleware, routes].forEach((handlerMap) => { handlerMap[method] = {} diff --git a/deno_dist/router/trie-router/node.ts b/deno_dist/router/trie-router/node.ts index cb6c46a43..fd5b16bfb 100644 --- a/deno_dist/router/trie-router/node.ts +++ b/deno_dist/router/trie-router/node.ts @@ -53,7 +53,9 @@ export class Node { parentPatterns.push(...curNode.patterns) curNode = curNode.children[p] const pattern = getPattern(p) - if (pattern) possibleKeys.push(pattern[1]) + if (pattern) { + possibleKeys.push(pattern[1]) + } continue } @@ -160,7 +162,9 @@ export class Node { continue } - if (part === '') continue + if (part === '') { + continue + } const [key, name, matcher] = pattern diff --git a/deno_dist/utils/cookie.ts b/deno_dist/utils/cookie.ts index 476c60404..f7c5c72e1 100644 --- a/deno_dist/utils/cookie.ts +++ b/deno_dist/utils/cookie.ts @@ -36,7 +36,9 @@ const verifySignature = async ( try { const signatureBinStr = atob(base64Signature) const signature = new Uint8Array(signatureBinStr.length) - for (let i = 0; i < signatureBinStr.length; i++) signature[i] = signatureBinStr.charCodeAt(i) + for (let i = 0; i < signatureBinStr.length; i++) { + signature[i] = signatureBinStr.charCodeAt(i) + } return await crypto.subtle.verify(algorithm, secret, signature, new TextEncoder().encode(value)) } catch (e) { return false @@ -59,16 +61,22 @@ export const parse = (cookie: string, name?: string): Cookie => { return pairs.reduce((parsedCookie, pairStr) => { pairStr = pairStr.trim() const valueStartPos = pairStr.indexOf('=') - if (valueStartPos === -1) return parsedCookie + if (valueStartPos === -1) { + return parsedCookie + } const cookieName = pairStr.substring(0, valueStartPos).trim() - if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) return parsedCookie + if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) { + return parsedCookie + } let cookieValue = pairStr.substring(valueStartPos + 1).trim() - if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) + if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) { cookieValue = cookieValue.slice(1, -1) - if (validCookieValueRegEx.test(cookieValue)) + } + if (validCookieValueRegEx.test(cookieValue)) { parsedCookie[cookieName] = decodeURIComponent_(cookieValue) + } return parsedCookie }, {} as Cookie) @@ -84,11 +92,15 @@ export const parseSigned = async ( for (const [key, value] of Object.entries(parse(cookie, name))) { const signatureStartPos = value.lastIndexOf('.') - if (signatureStartPos < 1) continue + if (signatureStartPos < 1) { + continue + } const signedValue = value.substring(0, signatureStartPos) const signature = value.substring(signatureStartPos + 1) - if (signature.length !== 44 || !signature.endsWith('=')) continue + if (signature.length !== 44 || !signature.endsWith('=')) { + continue + } const isVerified = await verifySignature(signature, signedValue, secretKey) parsedCookie[key] = isVerified ? signedValue : false diff --git a/deno_dist/utils/filepath.ts b/deno_dist/utils/filepath.ts index 8ac30685a..3a9edefd0 100644 --- a/deno_dist/utils/filepath.ts +++ b/deno_dist/utils/filepath.ts @@ -6,7 +6,9 @@ type FilePathOptions = { export const getFilePath = (options: FilePathOptions): string | undefined => { let filename = options.filename - if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) return + if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) { + return + } let root = options.root || '' const defaultDocument = options.defaultDocument || 'index.html' diff --git a/deno_dist/utils/mime.ts b/deno_dist/utils/mime.ts index 5b55663f5..b28ed8ae5 100644 --- a/deno_dist/utils/mime.ts +++ b/deno_dist/utils/mime.ts @@ -1,7 +1,9 @@ export const getMimeType = (filename: string): string | undefined => { const regexp = /\.([a-zA-Z0-9]+?)$/ const match = filename.match(regexp) - if (!match) return + if (!match) { + return + } let mimeType = mimes[match[1]] if ((mimeType && mimeType.startsWith('text')) || mimeType === 'application/json') { mimeType += '; charset=utf-8' diff --git a/deno_dist/utils/url.ts b/deno_dist/utils/url.ts index 785948ee0..86daf2bf5 100644 --- a/deno_dist/utils/url.ts +++ b/deno_dist/utils/url.ts @@ -126,7 +126,9 @@ export const checkOptionalParameter = (path: string): string[] | null => { in other cases it will return null */ - if (!path.match(/\:.+\?$/)) return null + if (!path.match(/\:.+\?$/)) { + return null + } const segments = path.split('/') const results: string[] = [] diff --git a/package.json b/package.json index 73e8bc443..9005c1060 100644 --- a/package.json +++ b/package.json @@ -472,7 +472,7 @@ ], "devDependencies": { "@cloudflare/workers-types": "^4.20231121.0", - "@hono/eslint-config": "^0.0.3", + "@hono/eslint-config": "^0.0.4", "@hono/node-server": "^1.3.3", "@types/crypto-js": "^4.1.1", "@types/glob": "^8.0.0", diff --git a/runtime_tests/lambda/index.test.ts b/runtime_tests/lambda/index.test.ts index e674e7706..fdc2c0c1d 100644 --- a/runtime_tests/lambda/index.test.ts +++ b/runtime_tests/lambda/index.test.ts @@ -133,7 +133,9 @@ describe('AWS Lambda Adapter for Hono', () => { const validCookies = getCookie(c, testCookie1.key) === testCookie1.value && getCookie(c, testCookie2.key) === testCookie2.value - if (!validCookies) return c.text('Invalid Cookies') + if (!validCookies) { + return c.text('Invalid Cookies') + } return c.text('Valid Cookies') }) diff --git a/src/adapter/aws-lambda/handler.ts b/src/adapter/aws-lambda/handler.ts index 9dafd4292..e340289be 100644 --- a/src/adapter/aws-lambda/handler.ts +++ b/src/adapter/aws-lambda/handler.ts @@ -204,7 +204,9 @@ const createRequest = (event: LambdaEvent) => { const headers = new Headers() getCookies(event, headers) for (const [k, v] of Object.entries(event.headers)) { - if (v) headers.set(k, v) + if (v) { + headers.set(k, v) + } } const method = isProxyEventV2(event) ? event.requestContext.http.method : event.httpMethod diff --git a/src/adapter/bun/serve-static.ts b/src/adapter/bun/serve-static.ts index 479718f9a..77946c8ae 100644 --- a/src/adapter/bun/serve-static.ts +++ b/src/adapter/bun/serve-static.ts @@ -36,7 +36,9 @@ export const serveStatic = ( defaultDocument: DEFAULT_DOCUMENT, }) - if (!path) return await next() + if (!path) { + return await next() + } path = `./${path}` diff --git a/src/adapter/cloudflare-workers/serve-static.ts b/src/adapter/cloudflare-workers/serve-static.ts index 91cec143f..fe16b24ac 100644 --- a/src/adapter/cloudflare-workers/serve-static.ts +++ b/src/adapter/cloudflare-workers/serve-static.ts @@ -36,7 +36,9 @@ export const serveStatic = ( defaultDocument: DEFAULT_DOCUMENT, }) - if (!path) return await next() + if (!path) { + return await next() + } const content = await getContentFromKVAsset(path, { manifest: options.manifest, diff --git a/src/adapter/deno/serve-static.ts b/src/adapter/deno/serve-static.ts index 8add7e715..f0110df60 100644 --- a/src/adapter/deno/serve-static.ts +++ b/src/adapter/deno/serve-static.ts @@ -34,7 +34,9 @@ export const serveStatic = ( defaultDocument: DEFAULT_DOCUMENT, }) - if (!path) return await next() + if (!path) { + return await next() + } path = `./${path}` diff --git a/src/client/client.ts b/src/client/client.ts index f937a5f3d..5b8e1d115 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -8,7 +8,9 @@ import { deepMerge, mergePath, removeIndexString, replaceUrlParam } from './util const createProxy = (callback: Callback, path: string[]) => { const proxy: unknown = new Proxy(() => {}, { get(_obj, key) { - if (typeof key !== 'string' || key === 'then') return undefined + if (typeof key !== 'string' || key === 'then') { + return undefined + } return createProxy(callback, [...path, key]) }, apply(_1, _2, args) { @@ -100,7 +102,9 @@ class ClientRequestImpl { headerValues['Cookie'] = cookies.join(',') } - if (this.cType) headerValues['Content-Type'] = this.cType + if (this.cType) { + headerValues['Content-Type'] = this.cType + } const headers = new Headers(headerValues ?? undefined) let url = this.url diff --git a/src/helper/adapter/index.ts b/src/helper/adapter/index.ts index 0d1f1ead5..29dcc808b 100644 --- a/src/helper/adapter/index.ts +++ b/src/helper/adapter/index.ts @@ -43,13 +43,27 @@ export const getRuntimeKey = () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any - if (global?.Deno !== undefined) return 'deno' - if (global?.Bun !== undefined) return 'bun' - if (typeof global?.WebSocketPair === 'function') return 'workerd' - if (typeof global?.EdgeRuntime === 'string') return 'edge-light' - if (global?.fastly !== undefined) return 'fastly' - if (global?.__lagon__ !== undefined) return 'lagon' - if (global?.process?.release?.name === 'node') return 'node' + if (global?.Deno !== undefined) { + return 'deno' + } + if (global?.Bun !== undefined) { + return 'bun' + } + if (typeof global?.WebSocketPair === 'function') { + return 'workerd' + } + if (typeof global?.EdgeRuntime === 'string') { + return 'edge-light' + } + if (global?.fastly !== undefined) { + return 'fastly' + } + if (global?.__lagon__ !== undefined) { + return 'lagon' + } + if (global?.process?.release?.name === 'node') { + return 'node' + } return 'other' } diff --git a/src/helper/cookie/index.test.ts b/src/helper/cookie/index.test.ts index 8cee5859a..d3a2c4e50 100644 --- a/src/helper/cookie/index.test.ts +++ b/src/helper/cookie/index.test.ts @@ -122,7 +122,9 @@ describe('Cookie Middleware', () => { app.get('/cookie', (c) => { const yummyCookie = getCookie(c, 'yummy_cookie') const res = new Response('Good cookie') - if (yummyCookie) res.headers.set('Yummy-Cookie', yummyCookie) + if (yummyCookie) { + res.headers.set('Yummy-Cookie', yummyCookie) + } return res }) diff --git a/src/helper/cookie/index.ts b/src/helper/cookie/index.ts index cd7f638e9..418092ac7 100644 --- a/src/helper/cookie/index.ts +++ b/src/helper/cookie/index.ts @@ -15,11 +15,15 @@ interface GetSignedCookie { export const getCookie: GetCookie = (c, key?) => { const cookie = c.req.raw.headers.get('Cookie') if (typeof key === 'string') { - if (!cookie) return undefined + if (!cookie) { + return undefined + } const obj = parse(cookie, key) return obj[key] } - if (!cookie) return {} + if (!cookie) { + return {} + } const obj = parse(cookie) // eslint-disable-next-line @typescript-eslint/no-explicit-any return obj as any @@ -28,11 +32,15 @@ export const getCookie: GetCookie = (c, key?) => { export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => { const cookie = c.req.raw.headers.get('Cookie') if (typeof key === 'string') { - if (!cookie) return undefined + if (!cookie) { + return undefined + } const obj = await parseSigned(cookie, secret, key) return obj[key] } - if (!cookie) return {} + if (!cookie) { + return {} + } const obj = await parseSigned(cookie, secret) // eslint-disable-next-line @typescript-eslint/no-explicit-any return obj as any diff --git a/src/hono-base.ts b/src/hono-base.ts index 564e40baf..9d444d21c 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -101,7 +101,9 @@ class Hono< // Implementation of app.on(method, path, ...handlers[]) this.on = (method: string | string[], path: string, ...handlers: H[]) => { - if (!method) return this + if (!method) { + return this + } this.#path = path for (const m of [method].flat()) { handlers.map((handler) => { @@ -236,7 +238,9 @@ class Hono< ...optionsArray ) - if (res) return res + if (res) { + return res + } await next() } diff --git a/src/request.ts b/src/request.ts index 3c799dbd0..4b4b962fb 100644 --- a/src/request.ts +++ b/src/request.ts @@ -94,7 +94,9 @@ export class HonoRequest

{ header(name: string): string | undefined header(): Record header(name?: string) { - if (name) return this.raw.headers.get(name.toLowerCase()) ?? undefined + if (name) { + return this.raw.headers.get(name.toLowerCase()) ?? undefined + } const headerData: Record = {} this.raw.headers.forEach((value, key) => { @@ -127,7 +129,9 @@ export class HonoRequest

{ cookie(key?: string) { const cookie = this.raw.headers.get('Cookie') - if (!cookie) return + if (!cookie) { + return + } const obj = parse(cookie) if (key) { const value = obj[key] @@ -138,7 +142,9 @@ export class HonoRequest

{ } async parseBody(options?: ParseBodyOptions): Promise { - if (this.bodyCache.parsedBody) return this.bodyCache.parsedBody as T + if (this.bodyCache.parsedBody) { + return this.bodyCache.parsedBody as T + } const parsedBody = await parseBody(this, options) this.bodyCache.parsedBody = parsedBody return parsedBody @@ -147,7 +153,9 @@ export class HonoRequest

{ private cachedBody = (key: keyof Body) => { const { bodyCache, raw } = this const cachedBody = bodyCache[key] - if (cachedBody) return cachedBody + if (cachedBody) { + return cachedBody + } /** * If an arrayBuffer cache is exist, * use it for creating a text, json, and others. diff --git a/src/router/reg-exp-router/router.ts b/src/router/reg-exp-router/router.ts index 51ab473ec..8e2cf056b 100644 --- a/src/router/reg-exp-router/router.ts +++ b/src/router/reg-exp-router/router.ts @@ -137,7 +137,9 @@ export class RegExpRouter implements Router { throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT) } - if (methodNames.indexOf(method) === -1) methodNames.push(method) + if (methodNames.indexOf(method) === -1) { + methodNames.push(method) + } if (!middleware[method]) { ;[middleware, routes].forEach((handlerMap) => { handlerMap[method] = {} diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 5929459de..3f7bd9e2e 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -53,7 +53,9 @@ export class Node { parentPatterns.push(...curNode.patterns) curNode = curNode.children[p] const pattern = getPattern(p) - if (pattern) possibleKeys.push(pattern[1]) + if (pattern) { + possibleKeys.push(pattern[1]) + } continue } @@ -160,7 +162,9 @@ export class Node { continue } - if (part === '') continue + if (part === '') { + continue + } const [key, name, matcher] = pattern diff --git a/src/utils/cookie.ts b/src/utils/cookie.ts index 8456a8dd9..fc047b641 100644 --- a/src/utils/cookie.ts +++ b/src/utils/cookie.ts @@ -36,7 +36,9 @@ const verifySignature = async ( try { const signatureBinStr = atob(base64Signature) const signature = new Uint8Array(signatureBinStr.length) - for (let i = 0; i < signatureBinStr.length; i++) signature[i] = signatureBinStr.charCodeAt(i) + for (let i = 0; i < signatureBinStr.length; i++) { + signature[i] = signatureBinStr.charCodeAt(i) + } return await crypto.subtle.verify(algorithm, secret, signature, new TextEncoder().encode(value)) } catch (e) { return false @@ -59,16 +61,22 @@ export const parse = (cookie: string, name?: string): Cookie => { return pairs.reduce((parsedCookie, pairStr) => { pairStr = pairStr.trim() const valueStartPos = pairStr.indexOf('=') - if (valueStartPos === -1) return parsedCookie + if (valueStartPos === -1) { + return parsedCookie + } const cookieName = pairStr.substring(0, valueStartPos).trim() - if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) return parsedCookie + if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) { + return parsedCookie + } let cookieValue = pairStr.substring(valueStartPos + 1).trim() - if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) + if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) { cookieValue = cookieValue.slice(1, -1) - if (validCookieValueRegEx.test(cookieValue)) + } + if (validCookieValueRegEx.test(cookieValue)) { parsedCookie[cookieName] = decodeURIComponent_(cookieValue) + } return parsedCookie }, {} as Cookie) @@ -84,11 +92,15 @@ export const parseSigned = async ( for (const [key, value] of Object.entries(parse(cookie, name))) { const signatureStartPos = value.lastIndexOf('.') - if (signatureStartPos < 1) continue + if (signatureStartPos < 1) { + continue + } const signedValue = value.substring(0, signatureStartPos) const signature = value.substring(signatureStartPos + 1) - if (signature.length !== 44 || !signature.endsWith('=')) continue + if (signature.length !== 44 || !signature.endsWith('=')) { + continue + } const isVerified = await verifySignature(signature, signedValue, secretKey) parsedCookie[key] = isVerified ? signedValue : false diff --git a/src/utils/filepath.ts b/src/utils/filepath.ts index 8ac30685a..3a9edefd0 100644 --- a/src/utils/filepath.ts +++ b/src/utils/filepath.ts @@ -6,7 +6,9 @@ type FilePathOptions = { export const getFilePath = (options: FilePathOptions): string | undefined => { let filename = options.filename - if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) return + if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) { + return + } let root = options.root || '' const defaultDocument = options.defaultDocument || 'index.html' diff --git a/src/utils/mime.ts b/src/utils/mime.ts index 5b55663f5..b28ed8ae5 100644 --- a/src/utils/mime.ts +++ b/src/utils/mime.ts @@ -1,7 +1,9 @@ export const getMimeType = (filename: string): string | undefined => { const regexp = /\.([a-zA-Z0-9]+?)$/ const match = filename.match(regexp) - if (!match) return + if (!match) { + return + } let mimeType = mimes[match[1]] if ((mimeType && mimeType.startsWith('text')) || mimeType === 'application/json') { mimeType += '; charset=utf-8' diff --git a/src/utils/url.ts b/src/utils/url.ts index 785948ee0..86daf2bf5 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -126,7 +126,9 @@ export const checkOptionalParameter = (path: string): string[] | null => { in other cases it will return null */ - if (!path.match(/\:.+\?$/)) return null + if (!path.match(/\:.+\?$/)) { + return null + } const segments = path.split('/') const results: string[] = [] diff --git a/yarn.lock b/yarn.lock index 175654b43..ab5901cf5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -496,10 +496,10 @@ dependencies: "@hapi/hoek" "^9.0.0" -"@hono/eslint-config@^0.0.3": - version "0.0.3" - resolved "https://registry.yarnpkg.com/@hono/eslint-config/-/eslint-config-0.0.3.tgz#15978d401c53c5497156f0a80dc2b261e94139c9" - integrity sha512-A4pbMilmx+HpY9TPZuC7+1eEhdTfTWKo7wJjWULOkJiH3pf580ZhaRZ+GvArCDPW6YkvTVvaGxr0wkt8VApzDg== +"@hono/eslint-config@^0.0.4": + version "0.0.4" + resolved "https://registry.yarnpkg.com/@hono/eslint-config/-/eslint-config-0.0.4.tgz#11bf12fd606b63302b4c4f5880f6965cc33ddcd2" + integrity sha512-CaL3LBdI2tw7Luo4vGDhhrdntA02Nsts6P0FjAz3U5piy3UhWzgEvEN+L9M0fsG7Mm1cgd+kM3hF8CRJOH0aGA== dependencies: "@typescript-eslint/eslint-plugin" "^6.14.0" "@typescript-eslint/parser" "^6.14.0"