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

Add support for CBOR #3648

Open
3w36zj6 opened this issue Nov 8, 2024 · 5 comments
Open

Add support for CBOR #3648

3w36zj6 opened this issue Nov 8, 2024 · 5 comments
Labels
enhancement New feature or request.

Comments

@3w36zj6
Copy link

3w36zj6 commented Nov 8, 2024

What is the feature you are proposing?

CBOR is a binary format for serializing objects defined in RFC 8949. It is JSON-compatible and suitable for network communications that require efficient data exchange, as well as for use in resource-constrained environments such as IoT devices.

I would be happy to assist if there is anything I can help with. If there are concerns about supporting CBOR in the Hono core package, I am considering creating a helper as third-party middleware first.

@3w36zj6 3w36zj6 added the enhancement New feature or request. label Nov 8, 2024
@yusukebe
Copy link
Member

yusukebe commented Nov 9, 2024

Hi @3w36zj6

It's interesting. Does it need external libraries?

@EdamAme-x
Copy link
Contributor

EdamAme-x commented Nov 9, 2024

There is quite a bit of lines...
I think seems too big to incorporate as core middleware / helper
https://github.com/paroga/cbor-js/blob/master/cbor.js

@3w36zj6
Copy link
Author

3w36zj6 commented Nov 9, 2024

If implemented according to the definition, I believe it can be implemented without dependencies.

In fact, cbor2, the successor of cbor, has no dependencies. However, the required lines of code are longer.

@yusukebe
Copy link
Member

@3w36zj6

I think we can use c.setRenderer and c.render like the React Renderer.

Code example:

import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'

const app = new Hono()

declare module 'hono' {
  interface ContextRenderer {
    (content: any): Response | Promise<Response>
  }
}

const renderer = createMiddleware(async (c, next) => {
  c.setRenderer((content) => {
    return c.body(encodeCBOR(content))
  })
  await next()
})

app.use(renderer)

app.get('/', (c) => {
  return c.render({ foo: 'bar' })
})

export default app

So, if the implementation is not too complicated, we may not provide the middleware and can write the instructions on our website.

@3w36zj6
Copy link
Author

3w36zj6 commented Nov 15, 2024

As an example of utilizing setRenderer, I have added example code to return a CBOR response on a website: honojs/website#532

IMO: CBOR has related RFCs such as CBOR Sequences (RFC 8742), COSE (RFC 9053), and CWT (RFC 8392). Considering future support for these, I think it would be preferable to manage reusable code and calls to other third-party middleware centrally through CBOR Middleware/Helper.

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

No branches or pull requests

3 participants