honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
17.74k stars 498 forks source link

natively support Structured Field Values #2654

Open Jxck opened 3 months ago

Jxck commented 3 months ago

What is the feature you are proposing?

Structured Field Values is the standard spec for serialize structured object into http header value.

It already referrered by tons of newly defined http specs.

And also, IETF HTTP Working Group currently working on retrofit SFV with existing headers.

So we can say it's a best practice to use SFV for headers, not JSON in any headers. For avoiding developer using JSON or any other format, or SFV like format via hand-made-string-concat, Hono can natively support SFV encoder/decoder and guide developer to use it.

for example.

c.res.headers.append('Cache-Status', 'OriginCache; hit; ttl=1100, "CDN Company Here"; hit; ttl=545')

can be type gurded, by require SFList for value when key is Cache-Status.

c.res.headers.append('Cache-Status',  sfv.encodeList([{
  "value": "Symbol(OriginCache)",
  "params": {
   "hit": true,
   "ttl": 1100
  }
 },
 {
  "value": "CDN Company Here",
  "params": {
   "hit": true,
   "ttl": 545
  }
 }
]))

FYI: I published typed SFV library to npm https://github.com/jxck/structured-field-values

Thanks.

yusukebe commented 3 months ago

Hi @Jxck !

It's very interesting, and your library seems very good. If we implement it, we will add the feature to c.header(). I'll try to investigate the SFV more. Thanks.

mvares commented 2 months ago

Hi @yusukebe

What do you think about implementing structured field values (SFV) following the RFC 8941 for our headers? It could streamline our communication, making it more standardized and efficient.

Some points to think about:

  1. Standardization: Aligning with RFC 8941 ensures our headers adhere to established standards, improving interoperability
  2. Efficiency: Structured field values keep data representation compact, reducing bandwidth usage
  3. Clarity: The structured syntax enhances readability and simplifies debugging

However, there is a con. This may take a while because many programmers don't know SFV.