Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Buffer<ArrayBufferLike> type in the parameter of c.body #3729

Open
rtritto opened this issue Dec 5, 2024 · 7 comments
Open

Missing Buffer<ArrayBufferLike> type in the parameter of c.body #3729

rtritto opened this issue Dec 5, 2024 · 7 comments
Labels

Comments

@rtritto
Copy link

rtritto commented Dec 5, 2024

What version of Hono are you using?

4.6.12

What runtime/platform is your app running on? (with version if possible)

Node.js 22

What steps can reproduce the bug?

import { readFileSync } from 'node:fs'
import { serve } from '@hono/node-server'
import { Hono } from 'hono'

const app = new Hono()

const ZIP_NAME = '<FILENAME>.zip'
const ZIP_FILEPATH = '/' + ZIP_NAME

app.get('/downloadZip', (c) => {
  c.header('Content-Disposition', `attachment; filename="${ZIP_NAME}"`)
  c.header('Content-Type', 'application/zip-compressed')

  return c.body(readFileSync(ZIP_FILEPATH))
})

const port = 3000

serve(
  {
    fetch: app.fetch,
    port
  },
  () => console.log(`Server running at http://localhost:${port}`)
)

readFileSync(zipPath) is of type Buffer<ArrayBufferLike>.
c.body accept data having string | ArrayBuffer | ReadableStream types.

Maybe Buffer<ArrayBufferLike> type should be added to data parameter of c.body.

What is the expected behavior?

No TypeScript error

What do you see instead?

TypeScript error:

No overload matches this call.
  Overload 1 of 2, '(data: Data | null, status?: StatusCode | undefined, headers?: HeaderRecord | undefined): Response', gave the following error.
    Argument of type 'Buffer<ArrayBufferLike>' is not assignable to parameter of type 'Data | null'.
      Type 'Buffer<ArrayBufferLike>' is missing the following properties from type 'ArrayBuffer': maxByteLength, resizable, resize, detached, and 2 more.
  Overload 2 of 2, '(data: Data | null, init?: ResponseInit | undefined): Response', gave the following error.
    Argument of type 'Buffer<ArrayBufferLike>' is not assignable to parameter of type 'Data | null'.
      Type 'Buffer<ArrayBufferLike>' is missing the following properties from type 'ArrayBuffer': maxByteLength, resizable, resize, detached, and 2 more.ts(2769)

Additional information

Workaround:

-return c.body(readFileSync(ZIP_FILEPATH))
+return c.body(readFileSync(ZIP_FILEPATH) as unknown as ReadableStream)`

Maybe related:

Question

There is any better/right alternative to download a zip file from browser?

@rtritto
Copy link
Author

rtritto commented Dec 6, 2024

@EdamAme-x

[email protected]

Maybe it's related to tsconfig.json:

{
  "compilerOptions": {
    "allowImportingTsExtensions": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "noEmit": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "ESNext",
    "verbatimModuleSyntax": true
  }
}

I created a reproducion: https://github.com/rtritto/template-yarn-berry/tree/repro-hono-3729

@EdamAme-x
Copy link
Contributor

okay, I'll check

@EdamAme-x
Copy link
Contributor

image
hmm

@rtritto
Copy link
Author

rtritto commented Dec 6, 2024

The types of readFileSync are related to @types/node (v22.10.1).
Can you try to run tsc (eg yarn tsc)?

@EdamAme-x
Copy link
Contributor

image
oh yes

@itztiva
Copy link

itztiva commented Dec 6, 2024

i submitted a pull request for something that might fix this, if you want you can try https://github.com/itztiva/hono

@rtritto
Copy link
Author

rtritto commented Dec 7, 2024

i submitted a pull request for something that might fix this, if you want you can try https://github.com/itztiva/hono

Yes, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants