web-std / io Goto Github PK
View Code? Open in Web Editor NEWWeb standard IO APIs implemented for nodejs
Web standard IO APIs implemented for nodejs
fetch-blob
The reason this library exists is because fetch-blob chooses to compromise Web API compatibility of blob.stream() by using nodejs native Readable stream. We found this to be problematic when sharing code across nodejs and browser runtimes. Instead this library stays true to the specification by using ReadableStream implementation from web-streams-polyfill library even if that is less convenient in nodejs context.
This is no longer true as fetch-blob@3 uses web stream as well now...
Using rollup to bundle a module that depends on @web-std/fetch
I'm hitting an error:
@web-std/blob (imported by src/platform.js, node_modules/@web-std/fetch/src/index.js, node_modules/@web-std/fetch/src/body.js)
[!] Error: 'default' is not exported by node_modules/web-streams-polyfill/dist/polyfill.mjs, imported by node_modules/@web-std/fetch/src/body.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/@web-std/fetch/src/body.js (12:7)
10:
11: import {Blob} from '@web-std/blob';
12: import WebStreams from 'web-streams-polyfill';
Looking at
Lines 21 to 26 in 68e89a8
it looks like there is no default export from web-streams-polyfill
Headers supports an array of values new Headers({ "foo": ["a", "b"] })
but this isn't reflected in the types.
It results in { "foo": "a,b" }
.
Would you accept a PR?
We got ton of great contributions from @MichaelDeBoey backported from https://github.com/remix-run/web-std-io. I assume fork was created so remix would not be blocked on me.
I think it would be wonderful if remix team felt empowered to drive changes without having to maintain the fork, I would love to support this. I have invited @MichaelDeBoey to the @jacob-ebey contributors team that have maintainers role.
Also starting this thread to discuss whether converging would make sense and if so what should we do to make it happen. For what it's worth my hope was that it would be a community project so getting more teams driving it it more than welcome.
Per the spec the Request.formData
should support parsing body of Content-Type: application/x-www-form-urlencoded
, but it currently only supports multipart/form-data
. I have an update locally and can open a PR shortly
Hello @Gozala, thanks for your answer. I've recreated the issue, since web-file-polyfill
project has been archived.
Problem
I have Angular Universal project and I use this package to create File and Blob polyfills on server side. After updating to Angular v12 I faced some problem with building app.
My server.ts file:
import { createWindow } from 'domino';
import { File, Blob } from 'web-file-polyfill';
const win = createWindow(template);
global.window = win as Window & typeof globalThis;
global.document = win.document;
global.localStorage = localStorage;
global.File = File;
global.Blob = Blob;
When I try to build project, an error throws:
./node_modules/@web-std/blob/src/package.js:4:51-58 - Error: export 'default' (imported as 'streams') was not found in 'web-streams-polyfill' (possible exports: ByteLengthQueuingStrategy, CountQueuingStrategy, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, TransformStream, TransformStreamDefaultController, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter)
If I change import in the @web-std/blob/src/package.js
:
import * as streams from "web-streams-polyfill"
It works well.
Steps to reproduce:
npm i
and npm run dev:ssr
I often find myself needing to load assets e.g. in tests in a platform agnostic way. Today this is tricky because in node you need to perform FS read and in browser you have fetch.
It should be fairly trivial to make support file URLs in node making fetch a universal API for loading assets.
Both the blob
and the stream
package use the not yet stable Blob and Web Streams API from Node. This leads to warnings when used with newer node versions as found in remix-run/remix#1141
It seems unsafe to me to use these APIs yet so I would opt for removing them until they become stable.
io/packages/fetch/src/utils/form-data.js
Line 107 in c88170b
The File
instance being constructed here is not imported from @web-std/file
causing it to fail in node with
ReferenceError: File is not defined
fetch(url, { method: 'POST', headers })
Promise { <pending> }
> f(node:66034) UnhandledPromiseRejectionWarning: FetchError: request to ... failed, reason: connect ENETUNREACH ...:443
at ClientRequest.<anonymous> (file:///Users/gozala/Projects/nft.storage/node_modules/@web-std/fetch/src/index.js:100:11)
at ClientRequest.emit (events.js:315:20)
at ClientRequest.EventEmitter.emit (domain.js:467:12)
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at TLSSocket.EventEmitter.emit (domain.js:467:12)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:66034) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
Promise {
<rejected> FetchError: request to ... failed, reason: connect ENETUNREACH ....:443
at ClientRequest.<anonymous> (file:///Users/gozala/Projects/nft.storage/node_modules/@web-std/fetch/src/index.js:100:11)
at ClientRequest.emit (events.js:315:20)
at ClientRequest.EventEmitter.emit (domain.js:467:12)
at TLSSocket.socketErrorListener (_http_client.js:469:9)
at TLSSocket.emit (events.js:315:20)
at TLSSocket.EventEmitter.emit (domain.js:467:12)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
type: 'system',
errno: 'ENETUNREACH',
code: 'ENETUNREACH',
erroredSysCall: 'connect'
}
}
Instead request should follow a redirect.
Change #35 introduced a regression by leaving out dist/index.cjs from publishing to npm as reported in nftstorage/nft.storage#1166
I have @web-std/fetch
as a sub-dependency. Starting yesterday I started having this issue during build. Downgrading to @web-std/[email protected]
resolves this.
[path]/node_modules/ts-node/dist/index.js:729
return old(m, filename);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module [path]/node_modules/@ssttevee/multipart-parser/lib/index.js from [path]/node_modules/@web-std/fetch/dist/index.cjs not supported.
Instead change the require of index.js in [path]/node_modules/@web-std/fetch/dist/index.cjs to a dynamic import() which is available in all CommonJS modules.
at Object.require.extensions.<computed> [as .js] ([path]/node_modules/ts-node/dist/index.js:729:20)
at Object.<anonymous> ([path]/node_modules/@web-std/fetch/dist/index.cjs:14:25)
at Object.require.extensions.<computed> [as .js] ([path]/node_modules/ts-node/dist/index.js:729:20)
at Object.<anonymous> ([path]/node_modules/nft.storage/dist/src/token.cjs:5:1)
at Object.require.extensions.<computed> [as .js] ([path]/node_modules/ts-node/dist/index.js:729:20)
at Object.<anonymous> ([path]/node_modules/nft.storage/dist/src/lib.cjs:8:13)
at Object.require.extensions.<computed> [as .js] ([path]/node_modules/ts-node/dist/index.js:729:20)
at Object.<anonymous> ([path]/src/services/ipfs.ts:28:23)
at Module.m._compile ([path]/node_modules/ts-node/dist/index.js:735:29)
at Object.require.extensions.<computed> [as .ts] ([path]/node_modules/ts-node/dist/index.js:737:16)
...
The current typs require us to pass a string to @web-std/fetch as the resource parameter. In the browser you can pass a URL instance as the resource, as it defines a stringifier attribute for it's href property https://url.spec.whatwg.org/#idl-index
The typings here are leading us to write fetch(urlInstance.toString())
which is unappealing.
Line 119 in 68e89a8
As per #51 it would be help to use fetch
for loading assets in tests across node/browser. Which in turn would be straight forward if we @web-std/fetch
could be used in test without having to use alternative code paths across node and web.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.