WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@usualoma
Copy link
Member

#269

What is this?

Stop generating heavy global.Request just by accessing req.headers.

This eliminates the memory consumption that until now was caused by simply applying the simple middleware called cors(), which generated a global.Request and was hard to free up until GC was executed.

Cases not resolved

In situations where we accept a lot of lightweight POST requests, this pull request will not improve on memory usage.

@usualoma usualoma force-pushed the keep-lightweight-req-headers branch from 6d5187a to f12919f Compare August 29, 2025 22:35
@usualoma
Copy link
Member Author

@yusukebe

It won't solve every situation, but I believe it will improve things for very simple apps like the one below (which involves accessing headers).

https://github.com/yusukebe/hono-nodejs-adapter-memory-leak

Shall we add this?

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!

headerRecord.push([key, value])
}
}
return new Headers(headerRecord)
Copy link
Member

Choose a reason for hiding this comment

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

Using new Headers instead of creating Request is nice.

It's just an idea, maybe overwork, we should not add it in this PR, but is creating LightweightHeaders a good idea?

class LightweightHeaders {
  constructor(rawHeaders) {} // or constructor(headerRecord)

  get(name) {
    // find the value from rawHeaders or headerRecord
  }

  has(name) {
    // find the value from rawHeaders or headerRecord
  }

  set(name, value) {
    this.toHeaders.set(name, value)
  }

  toHeaders() {
    const headers = new Headers(this.entries())
    // cache headers
    return headers
  }
}

It's for readonly, so it returns the value by .get() from rawHeaders or headerRecord, and if accessing .set(), do new Headers().

Anyway, we can merge this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

LightweightHeaders is a great idea! However, I'm concerned that using it like fetch(url, {headers: c.req.raw.headers}) would introduce more compatibility issues to worry about, making it not worth the maintenance cost.

@yusukebe
Copy link
Member

@usualoma

Shall we add this?

Yes! Thank you!

@yusukebe yusukebe changed the title Keep lightweight request even if accessing method, url or headers fix: Keep lightweight request even if accessing method, url or headers Aug 30, 2025
@yusukebe
Copy link
Member

I wondered if we should label fix, feat, or others, but I labeled it fix.

@yusukebe yusukebe merged commit 12bc0c0 into honojs:main Aug 30, 2025
5 checks passed
@usualoma
Copy link
Member Author

@yusukebe

Thank you!

I wondered if we should label fix, feat, or others, but I labeled it fix.

@usualoma usualoma deleted the keep-lightweight-req-headers branch August 30, 2025 13:18
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