chore: set up prettier (#106)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent fca3dde · Browse files · 10 files·+71 added−78 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+71 added−78 removed
- tests:+24 added−7 removed
- other:+47 added−71 removed
Provenance
- Commit
9c397fb- Tree
c15d96f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
fca3dde
10 files changed+71 added−78 removed
modified:src/middleware/cors/cors.test.ts+9 added-2 removed
37 unmodified lines383940414242434445464748494 unmodified lines54555657555859606162636465664 unmodified lines37 unmodified lines const res = await app.dispatch(req)
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 () => { const req = new Request('https://localhost/api2/abc', { method: 'OPTIONS' })4 unmodified lines expect(res.headers.get('Access-Control-Allow-Headers').split(/\s*,\s*/)).toEqual([ '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', ])4 unmodified linesmodified:src/middleware/logger/logger.ts+6 added-25 removed
1234567456782 unmodified lines11121314181920151617181912 unmodified lines323334354136373839405 unmodified lines46474849555657505152535412 unmodified lines6768697078798081827172848586878889909192737475import { 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 || '.' n = n.toString().split('.')2 unmodified lines}
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 = { Outgoing: '-->',12 unmodified lines 0: `\x1b[33m${status}\x1b[0m`, } return out[(status / 100) | 0]}type PrintFunc = (str: string, ...rest: string[]) => void;type PrintFunc = (str: string, ...rest: string[]) => void
function log( fn: PrintFunc, prefix: string,5 unmodified lines) { const out = prefix === LogPrefix.Incoming ? ` ${prefix} ${method} ${path}` : ` ${prefix} ${method} ${path} ${colorStatus( status )} ${elasped} ${contentLength}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elasped} ${contentLength}` fn(out)}
export const logger = (fn = console.log) => {12 unmodified lines throw e }
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) }}modified:src/middleware/serve-static/serve-static.ts+5 added-1 removed
12 unmodified lines13141516171718192021222324259 unmodified lines12 unmodified lines return async (c: Context, next: Function) => { 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) { const mimeType = getMimeType(path)9 unmodified linesmodified:src/utils/buffer.test.ts+12 added-4 removed
15 unmodified lines16 expect(await decodeBase64('abcdedf')).not.toBe('abcdef')17 })18 19 it('sha256', async () => {20- expect(await sha256('hono')).toBe('8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23')21- expect(await sha256('炎')).toBe('1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169')20+ expect(await sha256('hono')).toBe(21+ '8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23'22+ )23+ expect(await sha256('炎')).toBe(24+ '1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169'25+ )26 27 expect(await sha256('abcdedf')).not.toBe('abcdef')28 })29 12 unmodified lines42 })43 44 it('negative', async () => {45 expect(await timingSafeEqual('a', 'b')).toBe(false)42- expect(await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toBe(false)43- expect(await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')).toBe(false)46+ expect(47+ await timingSafeEqual('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')48+ ).toBe(false)49+ expect(50+ await timingSafeEqual('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')51+ ).toBe(false)52 expect(await timingSafeEqual('alpha', 'beta')).toBe(false)53 expect(await timingSafeEqual(false, true)).toBe(false)54 expect(await timingSafeEqual(false, undefined)).toBe(false)55 expect(12 unmodified linesmodified:src/utils/buffer.ts+7 added-2 removed
46 unmodified lines47 name: 'SHA-256',48 },49 new TextEncoder().encode(String(a))50 )51- const hash = Array.prototype.map.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2)).join('')51+ const hash = Array.prototype.map52+ .call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))53+ .join('')54 return hash55 }56 57 try {5 unmodified lines63 throw e64 }65 }66 65-export const timingSafeEqual = async (a: string | object | boolean, b: string | object | boolean) => {67+export const timingSafeEqual = async (68+ a: string | object | boolean,69+ b: string | object | boolean70+) => {71 const sa = await sha256(a)72 const sb = await sha256(b)73 return sa === sb && a === b74 }modified:src/utils/cloudflare.test.ts+3 added-1 removed
37 unmodified lines38 expect(getKVFilePath({ filename: 'foo', root: 'bar' })).toBe('bar/foo/index.html')39 expect(getKVFilePath({ filename: 'foo.txt', root: 'bar' })).toBe('bar/foo.txt')40 41 expect(getKVFilePath({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt')42- expect(getKVFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe('bar/foo/index.txt')42+ expect(getKVFilePath({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe(43+ 'bar/foo/index.txt'44+ )45 46 expect(getKVFilePath({ filename: './foo' })).toBe('foo/index.html')47 expect(getKVFilePath({ filename: 'foo', root: './bar' })).toBe('bar/foo/index.html')48 })1 unmodified linemodified:.eslintrc.js+12 added-21 removed
4 unmodified lines5 extends: [6 'eslint:recommended',7 'plugin:node/recommended',8 'plugin:@typescript-eslint/recommended',9+ 'prettier',10 ],11 parser: '@typescript-eslint/parser',12 parserOptions: {13 sourceType: 'module',17 unmodified lines31 {32 destructuring: 'all',33 },34 ],34- '@typescript-eslint/ban-types': ['error', {35- types: {36- 'Function': false,37- }38- }],35+ '@typescript-eslint/ban-types': [36+ 'error',37+ {38+ types: {39+ Function: false,40+ },41+ },42+ ],43 'node/no-missing-import': [44 'error',45 {42- allowModules: [43- 'types',44- 'estree',45- 'testUtils',46- 'less',47- 'sass',48- 'stylus',49- ],46+ allowModules: ['types', 'estree', 'testUtils', 'less', 'sass', 'stylus'],47 tryExtensions: ['.ts', '.js', '.jsx', '.tsx', '.d.ts'],48 },49 ],50 'node/no-missing-require': [6 unmodified lines57 'node/no-unpublished-import': 'off',58 'node/no-unpublished-require': 'off',59 'node/no-unsupported-features/es-syntax': 'off',60 64- '@typescript-eslint/no-empty-function': [65- 'error',66- { allow: ['arrowFunctions'] },67- ],61+ '@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],62 '@typescript-eslint/no-empty-interface': 'off',63 '@typescript-eslint/no-inferrable-types': 'off',64 '@typescript-eslint/no-var-requires': 'off',71- '@typescript-eslint/consistent-type-imports': [72- 'error',73- { prefer: 'type-imports' },74- ],65+ '@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],66 },67 })added:.prettierrc+9 added
1+{2+ "printWidth": 100,3+ "trailingComma": "es5",4+ "tabWidth": 2,5+ "semi": false,6+ "singleQuote": true,7+ "jsxSingleQuote": true,8+ "endOfLine": "lf"9+}modified:package.json+3 added-3 removed
48 unmodified lines49 }50 },51 "scripts": {52 "test": "jest",53- "lint": "eslint --ext js,ts src .eslintrc.js test",53+ "lint": "eslint --ext js,ts src .eslintrc.js test && prettier --check src",54 "build": "rimraf dist && tsc",55 "watch": "tsc -w",56 "prepublishOnly": "yarn build"57 },23 unmodified lines81 "@types/node": "^17.0.8",82 "@typescript-eslint/eslint-plugin": "^5.9.0",83 "@typescript-eslint/parser": "^5.9.0",84 "eslint": "^7.26.0",85- "eslint-config-prettier": "^8.1.0",85+ "eslint-config-prettier": "^8.3.0",86 "eslint-define-config": "^1.2.1",87 "eslint-import-resolver-typescript": "^2.0.0",88 "eslint-plugin-eslint-comments": "^3.2.0",89 "eslint-plugin-flowtype": "^5.7.2",90 "eslint-plugin-import": "^2.20.2",91 "eslint-plugin-node": "^11.1.0",92- "eslint-plugin-prettier": "^4.0.0",92 "form-data": "^4.0.0",93 "jest": "^27.4.5",94 "jest-environment-miniflare": "^2.0.0",95 "mustache": "^4.2.0",96+ "prettier": "^2.5.1",97 "rimraf": "^3.0.2",98 "ts-jest": "^27.1.2",99 "typescript": "^4.5.5"100 },4 unmodified linesmodified:yarn.lock+5 added-19 removed
1515 unmodified lines1516 optionator "^0.8.1"1517 optionalDependencies:1518 source-map "~0.6.1"1519 1520-eslint-config-prettier@^8.1.0:1520+eslint-config-prettier@^8.3.0:1521 version "8.3.0"1522 resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.3.0.tgz#f7471b20b6fe8a9a9254cc684454202886a2dd7a"1523 integrity sha512-BgZuLUSeKzvlL/VUjx/Yb787VQ26RU3gGjA3iiFvdsp/2bMfVIWUVP7tjxtjS0e+HP409cPlPvNkQloz8C91ew==1524 83 unmodified lines1608 minimatch "^3.0.4"1609 resolve "^1.10.1"1610 semver "^6.1.0"1611 1612-eslint-plugin-prettier@^4.0.0:1613- version "4.0.0"1614- resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz#8b99d1e4b8b24a762472b4567992023619cb98e0"1615- integrity sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==1616- dependencies:1617- prettier-linter-helpers "^1.0.0"1618-1612 eslint-scope@^5.1.1:1613 version "5.1.1"1614 resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"1615 integrity sha512-2NxwbF/hZ0KpepYN0cNbo+FN6XoK7GaHlQhgx/hIZl6Va0bF45RQOOwhLIy8lQDbuCiadSLCBnH2CFYquit5bw==153 unmodified lines1769 version "3.1.3"1770 resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"1771 integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==1772 1780-fast-diff@^1.1.2:1781- version "1.2.0"1782- resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"1783- integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==1784-1773 fast-glob@^3.2.9:1774 version "3.2.11"1775 resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"1776 integrity sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==1399 unmodified lines3176 version "1.1.2"3177 resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"3178 integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=3179 3192-prettier-linter-helpers@^1.0.0:3193- version "1.0.0"3194- resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b"3195- integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==3196- dependencies:3197- fast-diff "^1.1.2"3180+prettier@^2.5.1:3181+ version "2.5.1"3182+ resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"3183+ integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==3184 3185 pretty-format@^27.0.0, pretty-format@^27.4.6:3186 version "27.4.6"3187 resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.4.6.tgz#1b784d2f53c68db31797b2348fa39b49e31846b7"689 unmodified lines