Skip to content

Commit

Permalink
chore: add linter (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Dec 13, 2023
1 parent e4d0902 commit 1e5f648
Show file tree
Hide file tree
Showing 17 changed files with 1,260 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['@hono/eslint-config'],
}
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"watch": "tsup --watch",
"postbuild": "publint",
"prerelease": "yarn build && yarn test",
"release": "np"
"release": "np",
"lint": "eslint --ext js,ts src test",
"lint:fix": "eslint --ext js,ts src test --fix"
},
"license": "MIT",
"repository": {
Expand All @@ -60,9 +62,11 @@
"node": ">=18.14.1"
},
"devDependencies": {
"@hono/eslint-config": "^0.0.1",
"@types/jest": "^29.5.3",
"@types/node": "^20.10.0",
"@types/supertest": "^2.0.12",
"eslint": "^8.55.0",
"hono": "^3.9.2",
"jest": "^29.6.1",
"np": "^7.7.0",
Expand Down
7 changes: 4 additions & 3 deletions src/listener.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'node:http'
import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2'
import type { FetchCallback } from './types'
import './globals'
import { cacheKey } from './response'
import { newRequest } from './request'
import { cacheKey } from './response'
import type { FetchCallback } from './types'
import { writeFromReadableStream, buildOutgoingHttpHeaders } from './utils'
import './globals'

const regBuffer = /^no$/i
const regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i
Expand Down Expand Up @@ -33,6 +33,7 @@ const responseViaCache = (
res: Response,
outgoing: ServerResponse | Http2ServerResponse
): undefined | Promise<undefined> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const [status, body, header] = (res as any)[cacheKey]
if (typeof body === 'string') {
header['Content-Length'] = Buffer.byteLength(body)
Expand Down
5 changes: 3 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// Define prototype for lightweight pseudo Request object

import { Readable } from 'node:stream'
import type { IncomingMessage } from 'node:http'
import type { Http2ServerRequest } from 'node:http2'
import { Readable } from 'node:stream'

const newRequestFromIncoming = (
method: string,
Expand Down Expand Up @@ -80,4 +81,4 @@ export const newRequest = (incoming: IncomingMessage | Http2ServerRequest) => {
const req = Object.create(requestPrototype)
req[incomingKey] = incoming
return req
};
}
1 change: 1 addition & 0 deletions src/response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// Define lightweight pseudo Response class and replace global.Response with it.

import type { OutgoingHttpHeaders } from 'node:http'
Expand Down
3 changes: 2 additions & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReadStream} from 'fs'
import { createReadStream, existsSync, lstatSync } from 'fs'
import type { MiddlewareHandler } from 'hono'
import { ReadStream, createReadStream, existsSync, lstatSync } from 'fs'
import { getFilePath } from 'hono/utils/filepath'
import { getMimeType } from 'hono/utils/mime'

Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createServer as createServerHTTP } from 'node:http'
import type { AddressInfo } from 'node:net'
import type { Options, ServerType } from './types'
import { getRequestListener } from './listener'
import type { Options, ServerType } from './types'

export const createAdaptorServer = (options: Options): ServerType => {
const fetchCallback = options.fetch
const requestListener = getRequestListener(fetchCallback)
// ts will complain about createServerHTTP and createServerHTTP2 not being callable, which works just fine
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const createServer: any = options.createServer || createServerHTTP
const server: ServerType = createServer(options.serverOptions || {}, requestListener)
return server
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type { createServer, Server, ServerOptions as HttpServerOptions } from 'node:http'
import type {
createServer as createHttpsServer,
ServerOptions as HttpsServerOptions,
} from 'node:https'
import type {
createSecureServer as createSecureHttp2Server,
createServer as createHttp2Server,
Expand All @@ -11,6 +7,10 @@ import type {
SecureServerOptions as SecureHttp2ServerOptions,
ServerOptions as Http2ServerOptions,
} from 'node:http2'
import type {
createServer as createHttpsServer,
ServerOptions as HttpsServerOptions,
} from 'node:https'

export type FetchCallback = (request: Request) => Promise<unknown> | unknown

Expand Down
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Writable } from 'node:stream'
import type { OutgoingHttpHeaders } from 'node:http'
import type { Writable } from 'node:stream'

export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writable: Writable) {
if (stream.locked) {
throw new TypeError('ReadableStream is locked.')
} else if (writable.destroyed) {
stream.cancel();
stream.cancel()
return
}
const reader = stream.getReader()
Expand All @@ -16,6 +16,7 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
writable.off('close', cancel)
writable.off('error', cancel)
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function cancel(error?: any) {
reader.cancel(error).catch(() => {})
if (error) writable.destroy(error)
Expand All @@ -28,7 +29,7 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
if (done) {
writable.end()
} else if (!writable.write(value)) {
writable.once("drain", onDrain);
writable.once('drain', onDrain)
} else {
return reader.read().then(flow, cancel)
}
Expand Down
1 change: 1 addition & 0 deletions src/vercel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Hono } from 'hono'
import { getRequestListener } from './listener'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const handle = (app: Hono<any, any, any>) => {
return getRequestListener(app.fetch)
}
2 changes: 1 addition & 1 deletion test/request.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingMessage } from 'node:http'
import type { IncomingMessage } from 'node:http'
import { newRequest } from '../src/request'

describe('Request', () => {
Expand Down
3 changes: 2 additions & 1 deletion test/response.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createServer, type Server } from 'node:http'
import { AddressInfo } from 'node:net'
import type { AddressInfo } from 'node:net'
import { GlobalResponse } from '../src/response'

class NextResponse extends Response {}
Expand Down Expand Up @@ -56,6 +56,7 @@ describe('Response', () => {

// can only use new operator
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(Response as any)()
}).toThrow()

Expand Down
2 changes: 1 addition & 1 deletion test/serve-static.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createAdaptorServer } from './../src/server'
import { Hono } from 'hono'
import request from 'supertest'
import { serveStatic } from './../src/serve-static'
import { createAdaptorServer } from './../src/server'

describe('Serve Static Middleware', () => {
const app = new Hono()
Expand Down
24 changes: 12 additions & 12 deletions test/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createAdaptorServer } from '../src/server'
import request from 'supertest'
import fs from 'node:fs'
import { createServer as createHttp2Server } from 'node:http2'
import { createServer as createHTTPSServer } from 'node:https'
import { Hono } from 'hono'
import { basicAuth } from 'hono/basic-auth'
import { compress } from 'hono/compress'
import { poweredBy } from 'hono/powered-by'
import { basicAuth } from 'hono/basic-auth'
import { createServer as createHTTPSServer } from 'node:https'
import { createServer as createHttp2Server } from 'node:http2'
import fs from 'node:fs'
import request from 'supertest'
import { createAdaptorServer } from '../src/server'

describe('Basic', () => {
const app = new Hono()
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Basic', () => {
it('Should return 201 response - DELETE /posts/123', async () => {
const res = await request(server).delete('/posts/123')
expect(res.status).toBe(200)
expect(res.text).toBe(`DELETE 123`)
expect(res.text).toBe('DELETE 123')
})
})

Expand All @@ -77,15 +77,15 @@ describe('Routing', () => {
it('Should return responses from `/book/*`', async () => {
let res = await request(server).get('/book')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /book`)
expect(res.text).toBe('get /book')

res = await request(server).get('/book/123')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /book/123`)
expect(res.text).toBe('get /book/123')

res = await request(server).post('/book')
expect(res.status).toBe(200)
expect(res.text).toBe(`post /book`)
expect(res.text).toBe('post /book')
})
})

Expand All @@ -106,11 +106,11 @@ describe('Routing', () => {
it('Should return responses from `/chained/*`', async () => {
let res = await request(server).get('/chained/abc')
expect(res.status).toBe(200)
expect(res.text).toBe(`GET for abc`)
expect(res.text).toBe('GET for abc')

res = await request(server).post('/chained/abc')
expect(res.status).toBe(200)
expect(res.text).toBe(`POST for abc`)
expect(res.text).toBe('POST for abc')

res = await request(server).put('/chained/abc')
expect(res.status).toBe(404)
Expand Down
10 changes: 5 additions & 5 deletions test/vercel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import request from 'supertest'
import { Hono } from 'hono'
import request from 'supertest'
import { handle } from '../src/vercel'

describe('Basic', () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('Basic', () => {
it('Should return 201 response - DELETE /api/posts/123', async () => {
const res = await request(server).delete('/api/posts/123')
expect(res.status).toBe(200)
expect(res.text).toBe(`DELETE 123`)
expect(res.text).toBe('DELETE 123')
})
})

Expand All @@ -63,15 +63,15 @@ describe('Routing', () => {
it('Should return responses from `/v2/api/*`', async () => {
let res = await request(server).get('/v2/api')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /api`)
expect(res.text).toBe('get /api')

res = await request(server).get('/v2/api/123')
expect(res.status).toBe(200)
expect(res.text).toBe(`get /api/123`)
expect(res.text).toBe('get /api/123')

res = await request(server).post('/v2/api')
expect(res.status).toBe(200)
expect(res.text).toBe(`post /api`)
expect(res.text).toBe('post /api')
})
})
})
Expand Down
Loading

0 comments on commit 1e5f648

Please sign in to comment.