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

feat(types): improve JSONParsed #3074

Merged
merged 2 commits into from
Jul 6, 2024
Merged

Conversation

m-shaka
Copy link
Contributor

@m-shaka m-shaka commented Jul 1, 2024

I've improved JSONParsed in order to follow the spec of JSON.stringify as much as possible.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description

Here is major improvement:

  • support tuple type
  • handle undefined, symbol, and function type correctly(in array or object). For example
    • convert undefined to null in an array
    • drop keys whose value is undefined

You can see some actual behaviour in TS playground

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Jul 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.86%. Comparing base (d038d82) to head (4ec9c4a).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3074      +/-   ##
==========================================
- Coverage   95.87%   95.86%   -0.02%     
==========================================
  Files         137      137              
  Lines       13466    13492      +26     
  Branches     2381     2382       +1     
==========================================
+ Hits        12911    12934      +23     
- Misses        555      558       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

yusukebe commented Jul 4, 2024

Hi @m-shaka

Awesome! Following the spec of JSON.stringify is great.

One thing we have to consider is performance. This change will introduce significant performance degradation, especially in RPC.

Try to create an app that has many routes with this script:

import { writeFile } from 'fs'

const count = 200

const generateRoutes = (count: number) => {
  const imports = `
import { Hono } from 'hono';
import { z } from 'zod';
import { zValidator } from '@hono/zod-validator';
`

  const schema = `
const schema = z.object({
  q: z.string()
})\n
`

  let routes = imports + schema + 'export const app = new Hono()\n'
  for (let i = 1; i <= count; i++) {
    routes += `  .get('/route${i}/:id', zValidator('query', schema), (c) => {
    return c.json({
      query: c.req.valid('query'),
      id: c.req.param('id'),
      ok: true
    })
  })\n`
  }
  return routes
}

const routes = generateRoutes(count)

writeFile('app.ts', routes, (err) => {
  if (err) throw err
  console.log(`${count} routes have been written to app.ts`)
})

Load this app in another script with hc:

import { app } from './app'
import { hc } from 'hono/client'

const client = hc<typeof app>('/')

If with current version 4.4.10, it can handle over 200 routes in my IDE:

CleanShot 2024-07-04 at 15 07 06@2x

But with this PR, it will throw the error:

CleanShot 2024-07-04 at 15 07 41@2x

Performance issues are often reported in this project, so we must address them. We want a good idea to support both the following specs and better performance. We may be able to ignore this matter and refactor other points (like hc) to ensure good performance for RPC. However, we don't want to decrease the performance as much as possible with these JSON things.

@m-shaka @NamesMT Any thoughts?

@yusukebe
Copy link
Member

yusukebe commented Jul 4, 2024

Ah, the script includes a validator, so it's better to use this script:

import { writeFile } from 'fs'

const count = 200

const generateRoutes = (count: number) => {
  let routes = `import { Hono } from 'hono'
  export const app = new Hono()`
  for (let i = 1; i <= count; i++) {
    routes += `  .get('/route${i}/:id', (c) => {
    return c.json({
      ok: true
    })
  })\n`
  }
  return routes
}

const routes = generateRoutes(count)

writeFile('app.ts', routes, (err) => {
  if (err) throw err
  console.log(`${count} routes have been written to app.ts`)
})

@m-shaka
Copy link
Contributor Author

m-shaka commented Jul 4, 2024

Oh, I'll have a look later 👀

@m-shaka
Copy link
Contributor Author

m-shaka commented Jul 4, 2024

@yusukebe While I've changed the behaviour of optional keys, it seems to work now.
Now a?: string gonna be converted to a: string | undefined, but I don't think it's a problem in practical use
{ a: string | undefined } will remain required, instead of being converted to optional like { a?: string | undefined }

@yusukebe
Copy link
Member

yusukebe commented Jul 4, 2024

@m-shaka Thanks! I'll check it later.

@yusukebe yusukebe changed the title feat: improve JSONParsed feat(types): improve JSONParsed Jul 5, 2024
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Jul 5, 2024

@m-shaka

Great! There is little or no performance degradation. Looks good! I wonder if we should merge this into the current main or the next branch(for the next minor release, v4.5). I'll merge later. Thanks!

@yusukebe
Copy link
Member

yusukebe commented Jul 6, 2024

Hi @m-shaka

This is a feat but does not introduce a new future for users. So, I'll merge this now into the main and include it in a patch release. Thanks!

@yusukebe yusukebe merged commit 2a4f257 into honojs:main Jul 6, 2024
14 checks passed
adamnolte referenced this pull request in autoblocksai/cli Jul 24, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [hono](https://hono.dev/) ([source](https://togithub.com/honojs/hono))
| [`4.4.7` ->
`4.5.1`](https://renovatebot.com/diffs/npm/hono/4.4.7/4.5.1) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/hono/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/hono/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/hono/4.4.7/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/hono/4.4.7/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/hono (hono)</summary>

### [`v4.5.1`](https://togithub.com/honojs/hono/releases/tag/v4.5.1)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.5.0...v4.5.1)

#### What's Changed

- chore: remove rimraf and use bun shell by
[@&#8203;nakasyou](https://togithub.com/nakasyou) in
[https://github.com/honojs/hono/pull/3146](https://togithub.com/honojs/hono/pull/3146)
- chore: moving the setup file of vitest by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/3157](https://togithub.com/honojs/hono/pull/3157)
- fix(middleware/jwt): Changed the jwt-secret type to SignatureKey by
[@&#8203;JulesVerner](https://togithub.com/JulesVerner) in
[https://github.com/honojs/hono/pull/3167](https://togithub.com/honojs/hono/pull/3167)
- feat(bearer-auth): Allow empty bearer-auth middleware prefixes by
[@&#8203;prevostc](https://togithub.com/prevostc) in
[https://github.com/honojs/hono/pull/3161](https://togithub.com/honojs/hono/pull/3161)
- chore(factory): remove `@experimental` from `createApp` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3164](https://togithub.com/honojs/hono/pull/3164)
- fix(client): support array values for `query` in `ws` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3169](https://togithub.com/honojs/hono/pull/3169)
- fix(validator): ignore content-type mismatches by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3165](https://togithub.com/honojs/hono/pull/3165)

#### New Contributors

- [@&#8203;JulesVerner](https://togithub.com/JulesVerner) made their
first contribution in
[https://github.com/honojs/hono/pull/3167](https://togithub.com/honojs/hono/pull/3167)
- [@&#8203;prevostc](https://togithub.com/prevostc) made their first
contribution in
[https://github.com/honojs/hono/pull/3161](https://togithub.com/honojs/hono/pull/3161)

**Full Changelog**:
honojs/hono@v4.5.0...v4.5.1

###
[`v4.5.0`](https://togithub.com/honojs/hono/compare/v4.4.13...v4.5.0)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.13...v4.5.0)

### [`v4.4.13`](https://togithub.com/honojs/hono/releases/tag/v4.4.13)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.12...v4.4.13)

##### What's Changed

- chore: update benchmark by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3102](https://togithub.com/honojs/hono/pull/3102)
- chore: replace tsx with Bun by
[@&#8203;nakasyou](https://togithub.com/nakasyou) in
[https://github.com/honojs/hono/pull/3103](https://togithub.com/honojs/hono/pull/3103)
- refactor(http-status): remove unnecessary line of types and use common
types by [@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/3110](https://togithub.com/honojs/hono/pull/3110)
- fix(jsx): redefine scope attribute as enum type by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/3118](https://togithub.com/honojs/hono/pull/3118)
- fix(types): allow `string[] | File[]` for RPC form value by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3117](https://togithub.com/honojs/hono/pull/3117)
- fix(validator-types): type Alignment with Web Standards by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/3120](https://togithub.com/honojs/hono/pull/3120)
- fix(types): `app.use(path, mw)` return correct schema type by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3128](https://togithub.com/honojs/hono/pull/3128)

**Full Changelog**:
honojs/hono@v4.4.12...v4.4.13

### [`v4.4.12`](https://togithub.com/honojs/hono/releases/tag/v4.4.12)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.11...v4.4.12)

#### What's Changed

- fix(aws-lambda): set cookies with comma is bugged by
[@&#8203;NamesMT](https://togithub.com/NamesMT) in
[https://github.com/honojs/hono/pull/3084](https://togithub.com/honojs/hono/pull/3084)
- fix(types): infer `path` when chaining after `use` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3087](https://togithub.com/honojs/hono/pull/3087)
- chore: update outdated links in JSDoc by
[@&#8203;ryuapp](https://togithub.com/ryuapp) in
[https://github.com/honojs/hono/pull/3089](https://togithub.com/honojs/hono/pull/3089)
- fix(jsx): changes behavior when `download` attribute is set to a
boolean value. by [@&#8203;oon00b](https://togithub.com/oon00b) in
[https://github.com/honojs/hono/pull/3094](https://togithub.com/honojs/hono/pull/3094)
- chore: add the triage label by
[@&#8203;mvares](https://togithub.com/mvares) in
[https://github.com/honojs/hono/pull/3092](https://togithub.com/honojs/hono/pull/3092)
- feat(types): improve JSONParsed by
[@&#8203;m-shaka](https://togithub.com/m-shaka) in
[https://github.com/honojs/hono/pull/3074](https://togithub.com/honojs/hono/pull/3074)
- fix(helper/streaming): remove slow types by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3100](https://togithub.com/honojs/hono/pull/3100)
- chore(utils/jwt): add `@module` docs by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3101](https://togithub.com/honojs/hono/pull/3101)

#### New Contributors

- [@&#8203;oon00b](https://togithub.com/oon00b) made their first
contribution in
[https://github.com/honojs/hono/pull/3094](https://togithub.com/honojs/hono/pull/3094)

**Full Changelog**:
honojs/hono@v4.4.11...v4.4.12

### [`v4.4.11`](https://togithub.com/honojs/hono/releases/tag/v4.4.11)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.10...v4.4.11)

#### What's Changed

- refactor: remove unnecessary async keyword from router tests by
[@&#8203;K-tecchan](https://togithub.com/K-tecchan) in
[https://github.com/honojs/hono/pull/3061](https://togithub.com/honojs/hono/pull/3061)
- fix(validator): don't return a FormData if formData is cached by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3067](https://togithub.com/honojs/hono/pull/3067)
- fix(client): Add Query Parameter Support to WebSocket Client in
`hono/client` by [@&#8203;naporin0624](https://togithub.com/naporin0624)
in
[https://github.com/honojs/hono/pull/3066](https://togithub.com/honojs/hono/pull/3066)
- refactor(types): move `HandlerInterface`'s `(path, handler)`s
overloads down by [@&#8203;NamesMT](https://togithub.com/NamesMT) in
[https://github.com/honojs/hono/pull/3072](https://togithub.com/honojs/hono/pull/3072)
- test(helper/dev): fix typo of test case name by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/3073](https://togithub.com/honojs/hono/pull/3073)
- fix(stream): Fixed a problem that onAbort() is called even if request
is normally closed in deno by
[@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/hono/pull/3079](https://togithub.com/honojs/hono/pull/3079)

#### New Contributors

- [@&#8203;K-tecchan](https://togithub.com/K-tecchan) made their first
contribution in
[https://github.com/honojs/hono/pull/3061](https://togithub.com/honojs/hono/pull/3061)

**Full Changelog**:
honojs/hono@v4.4.10...v4.4.11

### [`v4.4.10`](https://togithub.com/honojs/hono/releases/tag/v4.4.10)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.9...v4.4.10)

#### What's Changed

- chore(jsr): export JWT utils by
[@&#8203;ryuapp](https://togithub.com/ryuapp) in
[https://github.com/honojs/hono/pull/3056](https://togithub.com/honojs/hono/pull/3056)
- fix(streaming): call stream.abort() explicitly when request is aborted
by [@&#8203;usualoma](https://togithub.com/usualoma) in
[https://github.com/honojs/hono/pull/3042](https://togithub.com/honojs/hono/pull/3042)
- fix(client): set Path as the default of Original by
[@&#8203;m-shaka](https://togithub.com/m-shaka) in
[https://github.com/honojs/hono/pull/3058](https://togithub.com/honojs/hono/pull/3058)

#### New Contributors

- [@&#8203;m-shaka](https://togithub.com/m-shaka) made their first
contribution in
[https://github.com/honojs/hono/pull/3058](https://togithub.com/honojs/hono/pull/3058)

**Full Changelog**:
honojs/hono@v4.4.9...v4.4.10

### [`v4.4.9`](https://togithub.com/honojs/hono/releases/tag/v4.4.9)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.8...v4.4.9)

#### What's Changed

- perf(context): improve initializing `Context` by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3046](https://togithub.com/honojs/hono/pull/3046)
- fix(types): correct inferring env when routes channing by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3051](https://togithub.com/honojs/hono/pull/3051)
- docs: update the description of `package.json` and README by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3052](https://togithub.com/honojs/hono/pull/3052)
- fix(timing): prevent duplicate applications by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3054](https://togithub.com/honojs/hono/pull/3054)

**Full Changelog**:
honojs/hono@v4.4.8...v4.4.9

### [`v4.4.8`](https://togithub.com/honojs/hono/releases/tag/v4.4.8)

[Compare
Source](https://togithub.com/honojs/hono/compare/v4.4.7...v4.4.8)

#### What's Changed

- fix(jsx): add an explicit type by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3007](https://togithub.com/honojs/hono/pull/3007)
- ci: use `env` for codecov GitHub Actions by
[@&#8203;yusukebe](https://togithub.com/yusukebe) in
[https://github.com/honojs/hono/pull/3010](https://togithub.com/honojs/hono/pull/3010)
- chore: Fix typos in JSDoc by
[@&#8203;NicoPlyley](https://togithub.com/NicoPlyley) in
[https://github.com/honojs/hono/pull/3002](https://togithub.com/honojs/hono/pull/3002)
- fix: change to allow use of websocket options by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/2999](https://togithub.com/honojs/hono/pull/2999)
- perf: parseAccept without spread operator by
[@&#8203;Jayllyz](https://togithub.com/Jayllyz) in
[https://github.com/honojs/hono/pull/3003](https://togithub.com/honojs/hono/pull/3003)
- test: add tests for buffer.ts by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/3004](https://togithub.com/honojs/hono/pull/3004)
- chore: upload bun test coverage to CodeCov by
[@&#8203;exoego](https://togithub.com/exoego) in
[https://github.com/honojs/hono/pull/3022](https://togithub.com/honojs/hono/pull/3022)
- refactor: remove unneeded import statements by
[@&#8203;EdamAme-x](https://togithub.com/EdamAme-x) in
[https://github.com/honojs/hono/pull/3014](https://togithub.com/honojs/hono/pull/3014)
- perf(utils/buffer): use promise all for better performance by
[@&#8203;yasuaki640](https://togithub.com/yasuaki640) in
[https://github.com/honojs/hono/pull/3031](https://togithub.com/honojs/hono/pull/3031)

**Full Changelog**:
honojs/hono@v4.4.7...v4.4.8

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job log](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQzOC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants