feat: [BREAKING] `Content-Length` is not added automatically (#183)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #179 Calculating Content-Length makes returning a response slowly. But Wrangler, Miniflare, or Cloudflare calculate Content-Length and add to a header automatically. So, we do not have to do it by ourselves.
authored by Yusuke Wada · committed by GitHub · parent 7cd3c70 · Browse files · 5 files·+7 added−35 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+7 added−35 removed
- tests:+4 added−15 removed
- other:+3 added−20 removed
Provenance
- Commit
501854f- Tree
a93d39f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
7cd3c70
5 files changed+7 added−35 removed
modified:src/middleware/logger/index.test.ts+4 added-4 removed
20 unmodified lines212223242525262728293031323333343536373839404141424344453 unmodified lines495051525353545520 unmodified lines const res = await app.request('http://localhost/empty') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(log.startsWith(' --> GET /empty \x1b[32m200\x1b[0m')).toBe(true) expect(log.endsWith(' 0b')).toBe(true) expect(log).toMatch(/m?s$/) })
it('Log status 200 with small body', async () => { const res = await app.request('http://localhost/short') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(log.startsWith(' --> GET /short \x1b[32m200\x1b[0m')).toBe(true) expect(log.endsWith(` ${shortRandomString.length}b`)).toBe(true) expect(log).toMatch(/m?s$/) })
it('Log status 200 with big body', async () => { const res = await app.request('http://localhost/long') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(log.startsWith(' --> GET /long \x1b[32m200\x1b[0m')).toBe(true) expect(log.endsWith(` ${longRandomString.length / 1024}kB`)).toBe(true) expect(log).toMatch(/m?s$/) })
it('Log status 404', async () => { const msg = 'Default 404 Not Found'3 unmodified lines const res = await app.request('http://localhost/notfound') expect(res).not.toBeNull() expect(res.status).toBe(404) expect(log.startsWith(' --> GET /notfound \x1b[33m404\x1b[0m')).toBe(true) expect(log.endsWith(` ${msg.length}b`)).toBe(true) expect(log).toMatch(/m?s$/) })})modified:src/middleware/logger/index.ts+3 added-7 removed
40 unmodified lines41424344454645464748495150515253546 unmodified lines616263646667686965666740 unmodified lines prefix: string, method: string, path: string, status?: number, elapsed?: string, contentLength?: string elapsed?: string) { const out = prefix === LogPrefix.Incoming ? ` ${prefix} ${method} ${path}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed} ${contentLength}` : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}` fn(out)}
export const logger = (fn = console.log) => {6 unmodified lines const start = Date.now()
await next()
const len = parseFloat(c.res.headers.get('Content-Length')) 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)) }}modified:src/middleware/serve-static/index.test.ts-4 removed
32 unmodified lines33343536373738394041424345444546474 unmodified lines52535455585657585960616266636432 unmodified lines const res = await app.request('http://localhost/static/plain.txt') expect(res.status).toBe(200) expect(await res.text()).toBe('This is plain.txt') expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8') expect(res.headers.get('Content-Length')).toBe('17') })
it('Should return hono.html', async () => { const res = await app.request('http://localhost/static/hono.html') expect(res.status).toBe(200) expect(await res.text()).toBe('<h1>Hono!</h1>') expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8') expect(res.headers.get('Content-Length')).toBe('14') })
it('Should return 404 response', async () => { const res = await app.request('http://localhost/static/not-found.html')4 unmodified lines const res = await app.request('http://localhost/static-no-root/plain.txt') expect(res.status).toBe(200) expect(await res.text()).toBe('That is plain.txt') expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8') expect(res.headers.get('Content-Length')).toBe('17') })
it('Should return index.html', async () => { const res = await app.request('http://localhost/static/top') expect(res.status).toBe(200) expect(await res.text()).toBe('<h1>Top</h1>') expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8') expect(res.headers.get('Content-Length')).toBe('12') })})modified:src/context.test.ts-7 removed
82 unmodified lines83 const obj: { [key: string]: string } = await res.json()84 expect(obj['hono']).toBe('great app')85 })86 87- it('Should return `Content-Length`', async () => {88- let res = c.text('abcdefg')89- expect(res.headers.get('Content-Length')).toBe('7')90- res = c.text('炎')91- expect(res.headers.get('Content-Length')).toBe('3')92- })93-87 it('Has headers, status, and statusText', async () => {88 c.header('X-Custom1', 'Message1')89 c.header('X-Custom2', 'Message2')90 c.status(200)28 unmodified linesmodified:src/context.ts-13 removed
65 unmodified lines66 newResponse(data: Data, init: ResponseInit = {}): Response {67 init.status = init.status || this.#status || 20068 init.statusText =69 init.statusText || this.#statusText || getStatusText(init.status as StatusCode)70-70 init.headers = { ...this.#headers, ...init.headers }71 73- // Content-Length74- let length = 075- if (data) {76- if (data instanceof ArrayBuffer) {77- length = data.byteLength78- } else if (typeof data == 'string') {79- const Encoder = new TextEncoder()80- length = Encoder.encode(data).byteLength || 081- }82- }83- init.headers = { ...init.headers, ...{ 'Content-Length': length.toString() } }84-72 return new Response(data, init)73 }74 75 body(data: Data, status: StatusCode = this.#status, headers: Headers = this.#headers): Response {50 unmodified lines