diff --git a/.eslintrc.js b/.eslintrc.js index ec5739754..acce3a24c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -6,6 +6,7 @@ module.exports = defineConfig({ 'eslint:recommended', 'plugin:node/recommended', 'plugin:@typescript-eslint/recommended', + 'prettier', ], parser: '@typescript-eslint/parser', parserOptions: { @@ -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'], }, ], @@ -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' }], }, }) diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..a456dc9af --- /dev/null +++ b/.prettierrc @@ -0,0 +1,9 @@ +{ + "printWidth": 100, + "trailingComma": "es5", + "tabWidth": 2, + "semi": false, + "singleQuote": true, + "jsxSingleQuote": true, + "endOfLine": "lf" +} diff --git a/package.json b/package.json index ac5b2c552..833793e81 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/src/middleware/cors/cors.test.ts b/src/middleware/cors/cors.test.ts index da52b569f..ae6e52dff 100644 --- a/src/middleware/cors/cors.test.ts +++ b/src/middleware/cors/cors.test.ts @@ -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 () => { @@ -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', diff --git a/src/middleware/logger/logger.ts b/src/middleware/logger/logger.ts index beeef4ed2..164ec7b70 100644 --- a/src/middleware/logger/logger.ts +++ b/src/middleware/logger/logger.ts @@ -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 || '.' @@ -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 = { @@ -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, @@ -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) } @@ -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) } } diff --git a/src/middleware/serve-static/serve-static.ts b/src/middleware/serve-static/serve-static.ts index c18c3787b..927a685d4 100644 --- a/src/middleware/serve-static/serve-static.ts +++ b/src/middleware/serve-static/serve-static.ts @@ -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) { diff --git a/src/utils/buffer.test.ts b/src/utils/buffer.test.ts index b1829d493..82a2203be 100644 --- a/src/utils/buffer.test.ts +++ b/src/utils/buffer.test.ts @@ -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') }) @@ -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) diff --git a/src/utils/buffer.ts b/src/utils/buffer.ts index 09af06925..96a10723e 100644 --- a/src/utils/buffer.ts +++ b/src/utils/buffer.ts @@ -48,7 +48,9 @@ export const sha256 = async (a: string | object | boolean): Promise => { }, 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 } @@ -62,7 +64,10 @@ export const sha256 = async (a: string | object | boolean): Promise => { } } -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 diff --git a/src/utils/cloudflare.test.ts b/src/utils/cloudflare.test.ts index a3e8ba869..25260e3d8 100644 --- a/src/utils/cloudflare.test.ts +++ b/src/utils/cloudflare.test.ts @@ -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') diff --git a/yarn.lock b/yarn.lock index ea580fc09..7b27dbc16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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== @@ -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" @@ -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" @@ -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"