Skip to content

Commit

Permalink
chore: set up prettier (honojs#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Feb 17, 2022
1 parent fca3dde commit 9c397fb
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 78 deletions.
33 changes: 12 additions & 21 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = defineConfig({
'eslint:recommended',
'plugin:node/recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand All @@ -31,22 +32,18 @@ module.exports = defineConfig({
destructuring: 'all',
},
],
'@typescript-eslint/ban-types': ['error', {
types: {
'Function': false,
}
}],
'@typescript-eslint/ban-types': [
'error',
{
types: {
Function: false,
},
},
],
'node/no-missing-import': [
'error',
{
allowModules: [
'types',
'estree',
'testUtils',
'less',
'sass',
'stylus',
],
allowModules: ['types', 'estree', 'testUtils', 'less', 'sass', 'stylus'],
tryExtensions: ['.ts', '.js', '.jsx', '.tsx', '.d.ts'],
},
],
Expand All @@ -61,16 +58,10 @@ module.exports = defineConfig({
'node/no-unpublished-require': 'off',
'node/no-unsupported-features/es-syntax': 'off',

'@typescript-eslint/no-empty-function': [
'error',
{ allow: ['arrowFunctions'] },
],
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/consistent-type-imports': [
'error',
{ prefer: 'type-imports' },
],
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
},
})
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"printWidth": 100,
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"endOfLine": "lf"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"scripts": {
"test": "jest",
"lint": "eslint --ext js,ts src .eslintrc.js test",
"lint": "eslint --ext js,ts src .eslintrc.js test && prettier --check src",
"build": "rimraf dist && tsc",
"watch": "tsc -w",
"prepublishOnly": "yarn build"
Expand Down Expand Up @@ -82,18 +82,18 @@
"@typescript-eslint/eslint-plugin": "^5.9.0",
"@typescript-eslint/parser": "^5.9.0",
"eslint": "^7.26.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-define-config": "^1.2.1",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-flowtype": "^5.7.2",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"form-data": "^4.0.0",
"jest": "^27.4.5",
"jest-environment-miniflare": "^2.0.0",
"mustache": "^4.2.0",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.2",
"typescript": "^4.5.5"
Expand Down
11 changes: 9 additions & 2 deletions src/middleware/cors/cors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ describe('CORS by Middleware', () => {

expect(res.status).toBe(204)
expect(res.headers.get('Access-Control-Allow-Methods').split(',')[0]).toBe('GET')
expect(res.headers.get('Access-Control-Allow-Headers').split(',')).toEqual(['X-PINGOTHER', 'Content-Type'])
expect(res.headers.get('Access-Control-Allow-Headers').split(',')).toEqual([
'X-PINGOTHER',
'Content-Type',
])
})

it('Preflight with options', async () => {
Expand All @@ -52,7 +55,11 @@ describe('CORS by Middleware', () => {
'X-Custom-Header',
'Upgrade-Insecure-Requests',
])
expect(res.headers.get('Access-Control-Allow-Methods').split(/\s*,\s*/)).toEqual(['POST', 'GET', 'OPTIONS'])
expect(res.headers.get('Access-Control-Allow-Methods').split(/\s*,\s*/)).toEqual([
'POST',
'GET',
'OPTIONS',
])
expect(res.headers.get('Access-Control-Expose-Headers').split(/\s*,\s*/)).toEqual([
'Content-Length',
'X-Kuma-Revision',
Expand Down
31 changes: 6 additions & 25 deletions src/middleware/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getPathFromURL } from '../../utils/url'
import type { Context } from '../../context'

const humanize = (
n: string[],
opts?: { delimiter?: string; separator?: string }
) => {
const humanize = (n: string[], opts?: { delimiter?: string; separator?: string }) => {
const options = opts || {}
const d = options.delimiter || ','
const s = options.separator || '.'
Expand All @@ -15,9 +12,7 @@ const humanize = (

const time = (start: number) => {
const delta = Date.now() - start
return humanize([
delta < 10000 ? delta + 'ms' : Math.round(delta / 1000) + 's',
])
return humanize([delta < 10000 ? delta + 'ms' : Math.round(delta / 1000) + 's'])
}

const LogPrefix = {
Expand All @@ -38,7 +33,7 @@ const colorStatus = (status: number = 0) => {
}
return out[(status / 100) | 0]
}
type PrintFunc = (str: string, ...rest: string[]) => void;
type PrintFunc = (str: string, ...rest: string[]) => void

function log(
fn: PrintFunc,
Expand All @@ -52,9 +47,7 @@ function log(
const out =
prefix === LogPrefix.Incoming
? ` ${prefix} ${method} ${path}`
: ` ${prefix} ${method} ${path} ${colorStatus(
status
)} ${elasped} ${contentLength}`
: ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elasped} ${contentLength}`
fn(out)
}

Expand All @@ -75,20 +68,8 @@ export const logger = (fn = console.log) => {
}

const len = parseFloat(c.res.headers.get('Content-Length'))
const contentLength = isNaN(len)
? '0'
: len < 1024
? `${len}b`
: `${len / 1024}kB`
const contentLength = isNaN(len) ? '0' : len < 1024 ? `${len}b` : `${len / 1024}kB`

log(
fn,
LogPrefix.Outgoing,
method,
path,
c.res.status,
time(start),
contentLength
)
log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start), contentLength)
}
}
6 changes: 5 additions & 1 deletion src/middleware/serve-static/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const serveStatic = (opt: Options = { root: '' }) => {
await next()
const url = new URL(c.req.url)

const path = getKVFilePath({ filename: url.pathname, root: opt.root, defaultDocument: DEFAULT_DOCUMENT })
const path = getKVFilePath({
filename: url.pathname,
root: opt.root,
defaultDocument: DEFAULT_DOCUMENT,
})

const content = await getContentFromKVAsset(path)
if (content) {
Expand Down
16 changes: 12 additions & 4 deletions src/utils/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ describe('buffer', () => {
})

it('sha256', async () => {
expect(await sha256('hono')).toBe('8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23')
expect(await sha256('炎')).toBe('1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169')
expect(await sha256('hono')).toBe(
'8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23'
)
expect(await sha256('炎')).toBe(
'1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169'
)

expect(await sha256('abcdedf')).not.toBe('abcdef')
})
Expand All @@ -39,8 +43,12 @@ describe('buffer', () => {

it('negative', async () => {
expect(await timingSafeEqual('a', 'b')).toBe(false)
expect(await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toBe(false)
expect(await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')).toBe(false)
expect(
await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
).toBe(false)
expect(
await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')
).toBe(false)
expect(await timingSafeEqual('alpha', 'beta')).toBe(false)
expect(await timingSafeEqual(false, true)).toBe(false)
expect(await timingSafeEqual(false, undefined)).toBe(false)
Expand Down
9 changes: 7 additions & 2 deletions src/utils/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export const sha256 = async (a: string | object | boolean): Promise<string> => {
},
new TextEncoder().encode(String(a))
)
const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('')
const hash = Array.prototype.map
.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))
.join('')
return hash
}

Expand All @@ -62,7 +64,10 @@ export const sha256 = async (a: string | object | boolean): Promise<string> => {
}
}

export const timingSafeEqual = async (a: string | object | boolean, b: string | object | boolean) => {
export const timingSafeEqual = async (
a: string | object | boolean,
b: string | object | boolean
) => {
const sa = await sha256(a)
const sb = await sha256(b)
return sa === sb && a === b
Expand Down
4 changes: 3 additions & 1 deletion src/utils/cloudflare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ describe('Utils for Cloudflare Workers', () => {
expect(getKVFilePath({ filename: 'foo.txt', root: 'bar' })).toBe('bar/foo.txt')

expect(getKVFilePath({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt')
expect(getKVFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe('bar/foo/index.txt')
expect(getKVFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe(
'bar/foo/index.txt'
)

expect(getKVFilePath({ filename: './foo' })).toBe('foo/index.html')
expect(getKVFilePath({ filename: 'foo', root: './bar' })).toBe('bar/foo/index.html')
Expand Down
24 changes: 5 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ escodegen@^2.0.0:
optionalDependencies:
source-map "~0.6.1"

eslint-config-prettier@^8.1.0:
eslint-config-prettier@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.3.0.tgz#f7471b20b6fe8a9a9254cc684454202886a2dd7a"
integrity sha512-BgZuLUSeKzvlL/VUjx/Yb787VQ26RU3gGjA3iiFvdsp/2bMfVIWUVP7tjxtjS0e+HP409cPlPvNkQloz8C91ew==
Expand Down Expand Up @@ -1609,13 +1609,6 @@ eslint-plugin-node@^11.1.0:
resolve "^1.10.1"
semver "^6.1.0"

eslint-plugin-prettier@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz#8b99d1e4b8b24a762472b4567992023619cb98e0"
integrity sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==
dependencies:
prettier-linter-helpers "^1.0.0"

eslint-scope@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
Expand Down Expand Up @@ -1777,11 +1770,6 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-diff@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==

fast-glob@^3.2.9:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
Expand Down Expand Up @@ -3189,12 +3177,10 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier-linter-helpers@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b"
integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==
dependencies:
fast-diff "^1.1.2"
prettier@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==

pretty-format@^27.0.0, pretty-format@^27.4.6:
version "27.4.6"
Expand Down

0 comments on commit 9c397fb

Please sign in to comment.