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

[http.IncomingMessage] req.toWeb() #42529

Open
jimmywarting opened this issue Mar 30, 2022 · 2 comments
Open

[http.IncomingMessage] req.toWeb() #42529

jimmywarting opened this issue Mar 30, 2022 · 2 comments
Labels
feature request http

Comments

@jimmywarting
Copy link

@jimmywarting jimmywarting commented Mar 30, 2022

What is the problem this feature will solve?

Reading the body out of the http.IncomingMessage is troublesome without any 3th party package...

Request on the other hand has features such as blob, arrayBuffer, formData and body for parsing.

What is the feature you are proposing to solve the problem?

It would be a convenient to have something like a req.toWeb() utility added onto the simple http server 🚀

something like the stream.Readable.toWeb() but for the http.IncomingRequest to web Request

import http from 'node:http'

const app = http.createServer(async (req, res) => {
  const request = req.toWeb()
  
  await request.blob()
  await request.arrayBuffer()
  await request.formData()
  await request.json()
  await request.body.pipeTo(dest)
  
  request.headers.has('content-length')
  
  request.url // full url (populated with host from request headers and http(s) based on using http or https)
  
  request.signal // a abort signal that aborts when user cancel the request
})

What alternatives have you considered?

currently doing some simplier variant like this one:

const app = http.createServer((r, res) => {
  const req = new Request(r.url, {
    headers: r.headers,
    method: r.method,
    body: r
  })
})

having this req.toWeb() built in would be very handy

@jimmywarting jimmywarting added the feature request label Mar 30, 2022
@VoltrexMaster VoltrexMaster added the http label Mar 31, 2022
@ronag
Copy link
Member

@ronag ronag commented Apr 2, 2022

Doesn't the alternative you propose already work?

@jimmywarting
Copy link
Author

@jimmywarting jimmywarting commented Apr 2, 2022

it's too verbose and having to do it in every project.

would have been nice to have that built in doe... there is also some signaling stuff, url construction and referrer that i would to set up also to get a complete Request object.

const app = http.createServer((r, res) => {
  const ctrl = new AbortController()
  const headers = new Headers(r.headers)
  const url = `http://${headers.get('host')}${r.url}`
  
  req.once('aborted', () => ctrl.abort())
 
  const req = new Request(url, {
    headers,
    method: r.method,
    body: r,
    signal: ctrl.signal,
    referrer: headers.get(referrer)
  })
})

i just thought this toWeb() would be better than having to add all of the other stuff Request has to make IncomingMessage be more web-like.

a alternative could be to have a getter function like:

class IncomingMessage {
  get request () {
    if (this._request) return this._request
    return (this._request = new Request(...))
  }
}

// and then later be able to do something like:
const app = http.createServer(async (x, res) => {
  const json = await x.request.json()
})

on another note it would also be nice to have a respondWith on the IncomingMessage as well so you can use respondWith a standardlized response object and combine it with the new fetch api and have it be very similar to a service worker and be able to have some isomorphic code shared between server and the browser.

class IncomingMessage {
  get request () { ... }
  respondWith (x) { ... }
}

// and then later be able to do something like:
const app = http.createServer(async evt => {
  const json = await evt.request.json()
  evt.respondWith(fetch('https://httpbin.org/get'))
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request http
Projects
Status: Pending Triage
Development

No branches or pull requests

3 participants