Skip to content
Myr

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

5 files+7 added−35 removed

modified:src/middleware/logger/index.test.ts+4 added-4 removed
modified:src/middleware/logger/index.ts+3 added-7 removed
modified:src/middleware/serve-static/index.test.ts-4 removed
modified:src/context.test.ts-7 removed
82 unmodified lines
83 const obj: { [key: string]: string } = await res.json()84 expect(obj['hono']).toBe('great app')85 })8687 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 })9387 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 lines
modified:src/context.ts-13 removed
65 unmodified lines
66 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)7070 init.headers = { ...this.#headers, ...init.headers }7173 // 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() } }8472 return new Response(data, init)73 }7475 body(data: Data, status: StatusCode = this.#status, headers: Headers = this.#headers): Response {
50 unmodified lines