Skip to content

Commit

Permalink
chore(lint): set curly rule as all (#2112)
Browse files Browse the repository at this point in the history
* chore(lint): set `curly` rule as `all`

* denoify
  • Loading branch information
yusukebe authored Jan 29, 2024
1 parent 4031fa8 commit f616ed9
Show file tree
Hide file tree
Showing 31 changed files with 214 additions and 76 deletions.
4 changes: 3 additions & 1 deletion deno_dist/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const serveStatic = <E extends Env = Env>(
defaultDocument: DEFAULT_DOCUMENT,
})

if (!path) return await next()
if (!path) {
return await next()
}

path = `./${path}`

Expand Down
8 changes: 6 additions & 2 deletions deno_dist/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions deno_dist/helper/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
16 changes: 12 additions & 4 deletions deno_dist/helper/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions deno_dist/hono-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -236,7 +238,9 @@ class Hono<
...optionsArray
)

if (res) return res
if (res) {
return res
}

await next()
}
Expand Down
16 changes: 12 additions & 4 deletions deno_dist/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
header(name: string): string | undefined
header(): Record<string, string>
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<string, string | undefined> = {}
this.raw.headers.forEach((value, key) => {
Expand Down Expand Up @@ -127,7 +129,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {

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]
Expand All @@ -138,7 +142,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
}

async parseBody<T extends BodyData = BodyData>(options?: ParseBodyOptions): Promise<T> {
if (this.bodyCache.parsedBody) return this.bodyCache.parsedBody as T
if (this.bodyCache.parsedBody) {
return this.bodyCache.parsedBody as T
}
const parsedBody = await parseBody<T>(this, options)
this.bodyCache.parsedBody = parsedBody
return parsedBody
Expand All @@ -147,7 +153,9 @@ export class HonoRequest<P extends string = '/', I extends Input['out'] = {}> {
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.
Expand Down
4 changes: 3 additions & 1 deletion deno_dist/router/reg-exp-router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ export class RegExpRouter<T> implements Router<T> {
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] = {}
Expand Down
8 changes: 6 additions & 2 deletions deno_dist/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export class Node<T> {
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
}

Expand Down Expand Up @@ -160,7 +162,9 @@ export class Node<T> {
continue
}

if (part === '') continue
if (part === '') {
continue
}

const [key, name, matcher] = pattern

Expand Down
26 changes: 19 additions & 7 deletions deno_dist/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion deno_dist/utils/filepath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion deno_dist/utils/mime.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 3 additions & 1 deletion deno_dist/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion runtime_tests/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})

Expand Down
4 changes: 3 additions & 1 deletion src/adapter/aws-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/adapter/bun/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const serveStatic = <E extends Env = Env>(
defaultDocument: DEFAULT_DOCUMENT,
})

if (!path) return await next()
if (!path) {
return await next()
}

path = `./${path}`

Expand Down
4 changes: 3 additions & 1 deletion src/adapter/cloudflare-workers/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const serveStatic = <E extends Env = Env>(
defaultDocument: DEFAULT_DOCUMENT,
})

if (!path) return await next()
if (!path) {
return await next()
}

const content = await getContentFromKVAsset(path, {
manifest: options.manifest,
Expand Down
4 changes: 3 additions & 1 deletion src/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const serveStatic = <E extends Env = Env>(
defaultDocument: DEFAULT_DOCUMENT,
})

if (!path) return await next()
if (!path) {
return await next()
}

path = `./${path}`

Expand Down
8 changes: 6 additions & 2 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit f616ed9

Please sign in to comment.