4 ms·
16 years after launch, the JS runtime centered around network requests now supports network requests out of the box.
by andai 1y ago
16 years after launch, the JS runtime centered around network requests now supports network requests out of the box.
- snickerdoodle12 1y agoObviously it supported network requests, the fetch api didn't even exist back then, and XMLHttpRequest which was the standard at the time is insane.
- 8n4vidtmkvmk 1y agoInsane but worked well. At least we could get download progress.
- CSSer 1y agoYou can get download progress with fetch. You can't get upload progress. Edit: Actually, you can even get upload progress, but the implementation seems fraught due to scant documentation. You may be better off using XMLHttpRequest for that. I'm going to try a simple implementation now. This has piqued my curiosity.
- pests 1y agoSniped
- kookamamie 1y agoNerd
- CSSer 1y agoMainly by the fact that all the LLMs were saying it's not possible in addition to GP, and I just couldn't believe that was true...
- CSSer 1y agoIt took me a couple hours, but I got it working for both uploads and downloads with a nice progress bar. My uploadFile method is about 40 lines of formatted code, and my downloadFile method is about 28 lines. It's pretty simple once you figure it out! Note that a key detail is that your server (and any intermediate servers, such as a reverse-proxy) must support HTTP/2 or QUIC. I spent much more time on that than the frontend code. In 2025, this isn't a problem for any modern client and hasn't been for a few years. However, that may not be true for your backend depending on how mature your codebase is. For example, Express doesn't support http/2 without another dependency. After fussing with it for a bit I threw it out and just used Fastify instead (built-in http/2 and high-level streaming). So I understand any apprehension/reservations there. Overall, I'm pretty satisfied knowing that fetch has wide support for easy progress tracking.
- rikafurude21 1y agocan we see a gist?
- CSSer 1y agohttps://github.com/hu0p/fetch-transfer-progress-demo/ https://github.com/hu0p/fetch-transfer-progress-demo/
- throwitaway1123 1y agoWhich browsers have you tested this in? I ran the feature detection script from the Chrome docs and neither Safari nor Firefox seem to support fetch upload streaming: https://developer.chrome.com/docs/capabilities/web-apis/fetch-streaming-requests#feature_detection https://developer.chrome.com/docs/capabilities/web-apis/fetc... const supportsRequestStreams = (() => { let duplexAccessed = false; const hasContentType = new Request('http://localhost', { body: new ReadableStream(), method: 'POST', get duplex() { duplexAccessed = true; return 'half'; }, }).headers.has('Content-Type'); return duplexAccessed && !hasContentType; })(); Safari doesn't appear to support the duplex option (the duplex getter is never triggered), and Firefox can't even handle a stream being used as the body of a Request object, and ends up converting the body to a string, and then setting the content type header to 'text/plain'.
- zachrip 1y agoThe thing I'm unsure about is if the streams approach is the same as the xhr one. I've no idea how the xhr one was accomplished or if it was even standards based in terms of impl - so my question is: Does xhr track if the packet made it to the destination, or only that it was queued to be sent by the OS?
- stevage 1y agoIt always did: https://nodejs.org/docs/latest-v0.10.x/api/http.html#http_http_request_options_callback https://nodejs.org/docs/latest-v0.10.x/api/http.html#http_ht...
- zero_shift 1y agoWhat a strange comment. You could always do network calls. Fetch is an API that has similar semantics across browser and server, using Promises.
- epolanski 1y agoNode always had a lower level http module.
- pmbanugo 1y agotell me you're not a Node.js developer :)