요청 본문은 한 번만 읽을 수 있다.1 누가 미리 읽었는지는 읽기 메서드를 Proxy 로 감싸서 찾는다.

const bodyReadingMethods = ['arrayBuffer', 'blob', 'formData', 'text', 'json']

bodyReadingMethods.forEach((methodName) => {
  request[methodName] = new Proxy(request[methodName], {
    apply(...args) {
      console.trace(`Premature "request.${methodName}" call!`)
      return Reflect.apply(...args)
    },
  })
})

https://redd.one/blog/debugging-like-a-pro-xy


Footnotes

  1. Using the Fetch API - MDN: “if any content has been read from the stream, then the stream is disturbed, and nothing else can read from the stream.” / “This means it’s not possible to read the same response (or request) body more than once” ↩

`#294`