Skip to content

fix: fetch proxy compatible with node 24#333

Merged
mnahkies merged 1 commit intomainfrom
mn/fix/fetch-proxy
May 31, 2025
Merged

fix: fetch proxy compatible with node 24#333
mnahkies merged 1 commit intomainfrom
mn/fix/fetch-proxy

Conversation

@mnahkies
Copy link
Copy Markdown
Owner

In NodeJS v24 undici Response objects are constructed using a mixin pattern that looks like this:

class Test {
  #prop = "foo"

  get prop() {
    return this.#prop
  }

  static getProp(test) {
    return test.#prop
  }
}

function mixin(prototype, getInternal) {
  const methods = {
    status() {
      return getInternal(this)
    },
  }

  Object.assign(prototype.prototype, methods)
}

mixin(Test, Test.getProp)

const test = new Test()
const proxied = new Proxy(test, {
  get(target, prop) {
    const r = Reflect.get(target, prop)

    if (typeof r === "function") {
      return r.bind(target)
    }
    return r
  },
})

console.log(test.prop)
console.log(test.status())
console.log(proxied.prop)
console.log(proxied.status())

If we don't bind functions, then we'll get errors like this:

  return test.#prop
                ^

TypeError: Cannot read private member #prop from an object whose class did not declare it
    at getProp
    at Proxy.status
    at Object.<anonymous>

@mnahkies mnahkies enabled auto-merge (squash) May 31, 2025 10:07
@mnahkies mnahkies merged commit 40acade into main May 31, 2025
5 checks passed
@mnahkies mnahkies deleted the mn/fix/fetch-proxy branch May 31, 2025 10:12
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.

1 participant