{"_id":"fetch-to-node","_rev":"2-5c56ad17cf346c8fddda3e36b640e210","name":"fetch-to-node","dist-tags":{"latest":"2.1.0"},"versions":{"2.0.0":{"name":"fetch-to-node","version":"2.0.0","keywords":["node","fetch","whatwg","wintertc","http","request","response"],"author":{"name":"Michael Hart","email":"mhart@cloudflare.com"},"license":"MIT","_id":"fetch-to-node@2.0.0","maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"homepage":"https://github.com/mhart/fetch-to-node#readme","bugs":{"url":"https://github.com/mhart/fetch-to-node/issues"},"dist":{"shasum":"f19fdcb548809d1ecbc23836941ab8e041c38625","tarball":"https://registry.npmjs.org/fetch-to-node/-/fetch-to-node-2.0.0.tgz","fileCount":23,"integrity":"sha512-eA91GD9hFnvGZYLqlnni//4Wuf3ZgEivZpL+d2/YZjGRVbOvQ+XlrmzPWNDPt7rgjfcKrveq3U8MhlEfwvwcYA==","signatures":[{"sig":"MEUCIQCiscsIq5mKD8tp0bQTEQeIaPzjkpzyHKVhMpsHmwd/CQIgeSP6uqr4rZv626b67raQ0RH0jc56vn8LtXO0xTHhhmY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":99900},"main":"dist/index.js","type":"module","types":"dist/index.d.ts","gitHead":"eb92f4bcf14276db6ffd833f14a03df012528ce0","scripts":{"test":"vitest run","clean":"rm -rf dist","compile":"tsc --build tsconfig.json","prepack":"npm run clean && npm run compile"},"_npmUser":{"name":"hichaelmart","email":"michael.hart.au@gmail.com"},"repository":{"url":"git+https://github.com/mhart/fetch-to-node.git","type":"git"},"_npmVersion":"11.2.0","description":"Node.js-compatible request and response objects for WinterTC runtimes","directories":{},"_nodeVersion":"22.14.0","_hasShrinkwrap":false,"devDependencies":{"vitest":"^3.1.2","typescript":"^5.8.3","@types/node":"^22.14.1"},"_npmOperationalInternal":{"tmp":"tmp/fetch-to-node_2.0.0_1745337053511_0.926304286947883","host":"s3://npm-registry-packages-npm-production"}},"2.1.0":{"name":"fetch-to-node","version":"2.1.0","description":"Node.js-compatible request and response objects for WinterTC runtimes","main":"dist/index.js","types":"dist/index.d.ts","type":"module","scripts":{"prepack":"npm run clean && npm run compile","compile":"tsc --build tsconfig.json","clean":"rm -rf dist","test":"vitest run"},"repository":{"type":"git","url":"git+https://github.com/mhart/fetch-to-node.git"},"keywords":["node","fetch","whatwg","wintertc","http","request","response"],"author":{"name":"Michael Hart","email":"mhart@cloudflare.com"},"license":"MIT","devDependencies":{"@types/node":"^22.14.1","typescript":"^5.8.3","vitest":"^3.1.2"},"_id":"fetch-to-node@2.1.0","gitHead":"7ac0835fecda6d671f95de619914c6bb11eb67db","bugs":{"url":"https://github.com/mhart/fetch-to-node/issues"},"homepage":"https://github.com/mhart/fetch-to-node#readme","_nodeVersion":"22.14.0","_npmVersion":"11.2.0","dist":{"integrity":"sha512-Wq05j6LE1GrWpT2t1YbCkyFY6xKRJq3hx/oRJdWEJpZlik3g25MmdJS6RFm49iiMJw6zpZuBOrgihOgy2jGyAA==","shasum":"e779d699f80aea0b1b34fd057737d151cb1dc19e","tarball":"https://registry.npmjs.org/fetch-to-node/-/fetch-to-node-2.1.0.tgz","fileCount":23,"unpackedSize":114936,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCICEX4gDuBpZldsTM9U/JxuBmFdy/l9WSXbBvA8igfBfWAiAeKuvD+2Dxfoki5Qg4xt+7EEJXmm8Ay3CuAHsZRfHr2w=="}]},"_npmUser":{"name":"hichaelmart","email":"michael.hart.au@gmail.com"},"directories":{},"maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fetch-to-node_2.1.0_1745638355024_0.9744599599705952"},"_hasShrinkwrap":false}},"time":{"created":"2025-04-22T15:50:53.510Z","modified":"2025-04-26T03:32:35.395Z","2.0.0":"2025-04-22T15:50:53.760Z","2.1.0":"2025-04-26T03:32:35.225Z"},"bugs":{"url":"https://github.com/mhart/fetch-to-node/issues"},"author":{"name":"Michael Hart","email":"mhart@cloudflare.com"},"license":"MIT","homepage":"https://github.com/mhart/fetch-to-node#readme","keywords":["node","fetch","whatwg","wintertc","http","request","response"],"repository":{"type":"git","url":"git+https://github.com/mhart/fetch-to-node.git"},"description":"Node.js-compatible request and response objects for WinterTC runtimes","maintainers":[{"name":"hichaelmart","email":"michael.hart.au@gmail.com"}],"readme":"# fetch-to-node\n\nA library providing Node.js-compatible request and response objects for [WinterTC](https://wintertc.org) (`fetch`-like) runtimes,\nsuch as Cloudflare Workers, Bun, Deno and Fastly Compute.\n\nUseful for when you're using a Node.js library in one of these environments that expects Node.js-style `req` and `res` objects,\nfor example [Express](https://expressjs.com/).\n\nThis is basically the inverse of libraries like [`@mjackson/node-fetch-server`](https://github.com/mjackson/remix-the-web/tree/main/packages/node-fetch-server#readme)\nthat allow the use of `Request`/`Response` signatures in Node.js servers.\n\nThis library is a copy/fork of [Katsuyuki Omuro's](https://github.com/harmony7) great [@fastly/http-compute-js](https://github.com/fastly/http-compute-js)\nproject and wouldn't be possible without the hard work put in there. The changes here were largely made to remove dependencies and make the interfaces more generic.\n\nThat said, this library does depend on a certain level of Node.js compatibility (`Readable`, `Writable` from `node:stream` and `Buffer` from `node:buffer`).\nSo please check out [@fastly/http-compute-js](https://github.com/fastly/http-compute-js) if this library doesn't work for you.\n\n## Usage\n\n```ts\nimport { toReqRes, toFetchResponse } from \"fetch-to-node\";\n\nexport default {\n  async fetch(request: Request): Promise<Response> {\n    // Create Node.js-compatible req and res from request\n    const { req, res } = toReqRes(request);\n\n    // Use req/res as you would in a Node.js application\n    res.writeHead(200, { \"Content-Type\": \"application/json\" });\n    res.end(\n      JSON.stringify({\n        data: \"Hello World!\",\n      })\n    );\n\n    // Create a Response object based on res, and return it\n    return await toFetchResponse(res);\n  },\n};\n```\n\nNB: If you're using Cloudflare Workers, be sure to set the [`nodejs_compat` flag](https://developers.cloudflare.com/workers/runtime-apis/nodejs/).\n\n`req` and `res` are implementations of [`IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage) and\n[`ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse), respectively, and\ncan be used as in a Node.js program.\n\n## API\n\n`toReqRes(request)`\n\n- Converts from a `Request` object to a pair of Node.js-compatible request and response objects.\n- Parameters:\n  - `request` - A [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) object. You would\n    typically obtain this from the `request` received by your `fetch` handler.\n- Returns: an object with the following properties.\n  - `req` - An [`http.IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage)\n    object whose `Readable` interface has been wired to the `Request` object's `body`. NOTE: This is an error\n    if the `Request`'s `body` has already been used.\n  - `res` - An [`http.ServerResponse`](https://nodejs.org/api/http.html#class-httpserverresponse)\n    object whose `Writable` interface has been wired to an in-memory buffer.\n\n`toFetchResponse(res)`\n\n- Creates a new `Response` object from the `res` object above, based on the status code, headers, and body that has been\n  written to it.\n- Parameters:\n  - `res` - An `http.ServerResponse` object created by `toReqRes()`.\n- Returns: a promise that resolves to a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object.\n- NOTE: This function returns a `Promise` that resolves to a `Response` once the `res` object emits the\n  [`'finish'`](https://nodejs.org/api/http.html#event-finish) event, which typically happens when you call\n  [`res.end()`](https://nodejs.org/api/http.html#responseenddata-encoding-callback). If your application never signals the\n  end of output, this promise will never resolve, and your application will likely error or time out.\n- If an error occurs, the promise will reject with that error.\n\n### Notes / Known Issues\n\n- HTTP Version is currently always reported as `1.1`.\n- The `socket` property of these objects is always `null`, and cannot be assigned.\n- The `ServerResponse` write stream must be finished before the `Response` object is generated.\n- Trailers are not supported, as [there's no support for these in the fetch API](https://github.com/whatwg/fetch/issues/981).\n- Client APIs not supported: `http.Agent`, `http.ClientRequest`, `http.get()`, `http.request()`, to name a few.\n\n## License\n\n[MIT](./LICENSE).\n\nIn order for this library to function without requiring a direct dependency on Node.js itself,\nportions of the code in this library are adapted / copied from Node.js.\nThose portions are Copyright Joyent, Inc. and other Node contributors.\nSee the LICENSE file for details.\n","readmeFilename":"README.md"}