@mattiasbuelens/web-streams-adapter
Advanced tools
Comparing version 0.1.0-alpha.1 to 0.1.0-alpha.2
{ | ||
"name": "@mattiasbuelens/web-streams-adapter", | ||
"version": "0.1.0-alpha.1", | ||
"description": "", | ||
"main": "dist/web-streams-adapter.js", | ||
"version": "0.1.0-alpha.2", | ||
"description": "Adapters for converting between different implementations of WHATWG Streams", | ||
"main": "dist/web-streams-adapter", | ||
"module": "dist/web-streams-adapter.mjs", | ||
@@ -22,4 +22,4 @@ "types": "types/index.d.ts", | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"author": "Mattias Buelens <mattias@buelens.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -29,7 +29,7 @@ "url": "https://github.com/MattiasBuelens/web-streams-adapter/issues" | ||
"homepage": "https://github.com/MattiasBuelens/web-streams-adapter#readme", | ||
"peerDependencies": { | ||
"@mattiasbuelens/web-streams-polyfill": "^0.1.0-alpha.3" | ||
"dependencies": { | ||
"@types/whatwg-streams": "^0.0.7" | ||
}, | ||
"devDependencies": { | ||
"@mattiasbuelens/web-streams-polyfill": "^0.1.0-alpha.3", | ||
"@mattiasbuelens/web-streams-polyfill": "^0.1.0", | ||
"@types/minimatch": "^3.0.3", | ||
@@ -36,0 +36,0 @@ "@types/node": "^9.6.7", |
# web-streams-adapter | ||
Adapters for converting between different implementations of [WHATWG Streams][spec]. | ||
## Why? | ||
When you've got a `ReadableStream` from a native web API, you might be disappointed to find out | ||
that not all browser support the latest and greatest features from the streams spec yet: | ||
```js | ||
const response = await fetch('http://example.com/data.txt'); | ||
const readable = response.body; | ||
const writable = new WritableStream({ write(chunk) { console.log(chunk) } }); | ||
await readable.pipeTo(writable); // TypeError: Object doesn't support property or method 'pipeTo' | ||
``` | ||
This is because although many browsers have already started implementing streams, | ||
most of them are not yet fully up-to-date with the latest specification: | ||
* Chrome 59 supports `ReadableStream` and `WritableStream`, | ||
and `TransformStream` [will arrive in Chrome 67][ts-chrome-status]. | ||
Readable byte streams are [not yet supported][byte-stream-chrome-bug]. | ||
* Firefox 57 supports `ReadableStream`, but only behind an experimental flag. | ||
* Edge 16 supports `ReadableStream`, but does not yet support developer-created streams using `new ReadableStream`. | ||
`WritableStream` however is fully supported. | ||
* Safari supports `ReadableStream`, but no readable byte streams or writable streams. | ||
For up-to-date information, check [caniuse.com][caniuse] | ||
and the browser compatibility tables on MDN for [`ReadableStream`][rs-compat] and [`WritableStream`][ws-compat]. | ||
## What? | ||
`web-streams-adapter` provides adapter functions that take any readable/writable/transform stream | ||
and wraps it into a different readable/writable/stream with a different (more complete) implementation of your choice, | ||
for example [`web-streams-polyfill`][creatorrr-polyfill]. | ||
```js | ||
// setup | ||
import { ReadableStream as PolyfillReadableStream } from 'web-streams-polyfill'; | ||
import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'; | ||
const toPolyfillReadable = createReadableStreamWrapper(PolyfillReadableStream); | ||
// when handling a fetch response | ||
const response = await fetch('http://example.com/data.txt'); | ||
const readable = toPolyfillReadable(response.body); | ||
console.log(readable instanceof PolyfillReadableStream); // -> true | ||
await readable.pipeTo(writable); // works! | ||
``` | ||
You can also use an adapter to convert from your polyfilled stream back to a native stream: | ||
```js | ||
// setup | ||
const toNativeReadable = createReadableStreamWrapper(self.ReadableStream); | ||
// when starting a fetch with a streaming POST body | ||
const readable = new PolyfillReadableStream({ /* amazingness */ }); | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
body: toNativeReadable(readable) // works! | ||
}); | ||
``` | ||
## How? | ||
For readable streams, `web-streams-adapter` creates an underlying source that pulls from the given readable stream | ||
using the primitive reader API. This source can then be used by *any* other readable stream implementation, | ||
both native and polyfilled ones. | ||
For writable and transform streams, it uses a very similar approach to create an underlying sink or transformer | ||
using primitive reader and writer APIs on the given stream. | ||
[spec]: https://streams.spec.whatwg.org/ | ||
[ts-chrome-status]: https://www.chromestatus.com/feature/5466425791610880 | ||
[byte-stream-chrome-bug]: https://bugs.chromium.org/p/chromium/issues/detail?id=614302 | ||
[caniuse]: https://www.caniuse.com/#feat=streams | ||
[rs-compat]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#Browser_Compatibility | ||
[ws-compat]: https://developer.mozilla.org/en-US/docs/Web/API/WritableStream#Browser_Compatibility | ||
[creatorrr-polyfill]: https://github.com/creatorrr/web-streams-polyfill |
@@ -1,3 +0,1 @@ | ||
export declare class AssertionError extends Error { | ||
} | ||
export default function assert(test: boolean): void | never; |
@@ -0,3 +1,5 @@ | ||
export * from './stream-like'; | ||
export * from './wrappers'; | ||
export { createReadableStreamWrapper, createWrappingReadableSource } from './readable-wrapper'; | ||
export { createWritableStreamWrapper, createWrappingWritableSink } from './writable-wrapper'; | ||
export { createTransformStreamWrapper, createWrappingTransformer } from './transform-wrapper'; |
@@ -0,5 +1,5 @@ | ||
import { ReadableByteStreamSource, ReadableStreamSource } from 'whatwg-streams'; | ||
import { ReadableStreamLike, ReadableStreamLikeConstructor } from './stream-like'; | ||
import { ReadableStreamWrapper, WrappingReadableSourceOptions } from './wrappers'; | ||
import { ReadableStreamUnderlyingSource } from '@mattiasbuelens/web-streams-polyfill'; | ||
export declare function createReadableStreamWrapper(ctor: ReadableStreamLikeConstructor): ReadableStreamWrapper; | ||
export declare function createWrappingReadableSource<R = any>(readable: ReadableStreamLike<R>, {type}?: WrappingReadableSourceOptions): ReadableStreamUnderlyingSource<R>; | ||
export declare function createWrappingReadableSource<R = any>(readable: ReadableStreamLike<R>, {type}?: WrappingReadableSourceOptions): ReadableStreamSource<R> | ReadableByteStreamSource; |
@@ -1,49 +0,31 @@ | ||
import { QueuingStrategy, ReadableStreamUnderlyingSource, TransformStreamTransformer, WritableStreamUnderlyingSink } from '@mattiasbuelens/web-streams-polyfill'; | ||
import { QueuingStrategy, ReadableByteStreamSource, ReadableStreamBYOBReader, ReadableStreamDefaultReader, ReadableStreamSource, TransformStreamTransformer, WritableStreamDefaultWriter, WritableStreamSink } from 'whatwg-streams'; | ||
export interface ReadableStreamLikeConstructor { | ||
new <R = any>(underlyingSource?: ReadableStreamUnderlyingSource<R>, strategy?: Partial<QueuingStrategy>): ReadableStreamLike<R>; | ||
new <R extends ArrayBufferView = ArrayBufferView>(underlyingSource?: ReadableByteStreamSource, strategy?: QueuingStrategy<R>): ReadableStreamLike<R>; | ||
new <R = any>(underlyingSource?: ReadableStreamSource<R> | ReadableByteStreamSource, strategy?: QueuingStrategy<R>): ReadableStreamLike<R>; | ||
} | ||
export interface ReadableStreamLike<R = any> { | ||
readonly locked: boolean; | ||
getReader(options: { | ||
getReader(): ReadableStreamDefaultReader<R>; | ||
getReader({mode}: { | ||
mode: 'byob'; | ||
}): ReadableStreamLikeBYOBReader; | ||
getReader(options?: { | ||
mode?: undefined; | ||
}): ReadableStreamLikeDefaultReader<R>; | ||
}): ReadableStreamBYOBReader; | ||
} | ||
export declare type ReadableByteStreamLike = ReadableStreamLike<Uint8Array>; | ||
export interface ReadableStreamLikeReaderBase { | ||
readonly closed?: Promise<void>; | ||
cancel(reason: any): Promise<void>; | ||
releaseLock(): void; | ||
} | ||
export interface ReadableStreamLikeDefaultReader<R = any> extends ReadableStreamLikeReaderBase { | ||
read(): Promise<IteratorResult<R>>; | ||
} | ||
export interface ReadableStreamLikeBYOBReader extends ReadableStreamLikeReaderBase { | ||
read<T extends ArrayBufferView>(view: T): Promise<IteratorResult<T>>; | ||
} | ||
export declare type ReadableByteStreamLike = ReadableStreamLike<ArrayBufferView>; | ||
export interface WritableStreamLikeConstructor { | ||
new <W = any>(underlyingSink?: WritableStreamUnderlyingSink<W>, strategy?: Partial<QueuingStrategy>): WritableStreamLike<W>; | ||
new <W = any>(underlyingSink?: WritableStreamSink<W>, strategy?: QueuingStrategy<W>): WritableStreamLike<W>; | ||
} | ||
export interface WritableStreamLike<W = any> { | ||
readonly locked: boolean; | ||
getWriter(): WritableStreamLikeDefaultWriter<W>; | ||
getWriter(): WritableStreamDefaultWriter<W>; | ||
} | ||
export interface WritableStreamLikeDefaultWriter<W = any> { | ||
readonly closed: Promise<void>; | ||
readonly desiredSize: number | null; | ||
readonly ready: Promise<void>; | ||
abort(reason: any): Promise<void>; | ||
close(): Promise<void>; | ||
releaseLock(): void; | ||
write(chunk: W): Promise<void>; | ||
export interface WritableReadableStreamLikePair<W extends WritableStreamLike<any>, R extends ReadableStreamLike<any>> { | ||
readonly readable: R; | ||
readonly writable: W; | ||
} | ||
export interface ReadableWritableStreamLikePair<R = any, W = any> { | ||
export interface TransformStreamLikeConstructor { | ||
new <R = any, W = any>(transformer?: TransformStreamTransformer<R, W>, writableStrategy?: QueuingStrategy<W>, readableStrategy?: QueuingStrategy<R>): TransformStreamLike<R, W>; | ||
} | ||
export interface TransformStreamLike<R = any, W = any> extends WritableReadableStreamLikePair<WritableStreamLike<W>, ReadableStreamLike<R>> { | ||
readonly readable: ReadableStreamLike<R>; | ||
readonly writable: WritableStreamLike<W>; | ||
} | ||
export interface TransformStreamLikeConstructor { | ||
new <I = any, O = any>(transformer?: TransformStreamTransformer<I, O>, writableStrategy?: Partial<QueuingStrategy>, readableStrategy?: Partial<QueuingStrategy>): TransformStreamLike<I, O>; | ||
} | ||
export declare type TransformStreamLike<I = any, O = any> = ReadableWritableStreamLikePair<O, I>; |
@@ -0,5 +1,5 @@ | ||
import { TransformStreamTransformer } from 'whatwg-streams'; | ||
import { TransformStreamLike, TransformStreamLikeConstructor } from './stream-like'; | ||
import { TransformStreamWrapper } from './wrappers'; | ||
import { TransformStreamTransformer } from '@mattiasbuelens/web-streams-polyfill'; | ||
export declare function createTransformStreamWrapper(ctor: TransformStreamLikeConstructor): TransformStreamWrapper; | ||
export declare function createWrappingTransformer<I = any, O = any>(transform: TransformStreamLike<I, O>): TransformStreamTransformer<I, O>; | ||
export declare function createWrappingTransformer<R = any, W = any>(transform: TransformStreamLike<R, W>): TransformStreamTransformer<R, W>; |
@@ -0,5 +1,5 @@ | ||
import { WritableStreamSink } from 'whatwg-streams'; | ||
import { WritableStreamLike, WritableStreamLikeConstructor } from './stream-like'; | ||
import { WritableStreamWrapper } from './wrappers'; | ||
import { WritableStreamUnderlyingSink } from '@mattiasbuelens/web-streams-polyfill'; | ||
export declare function createWritableStreamWrapper(ctor: WritableStreamLikeConstructor): WritableStreamWrapper; | ||
export declare function createWrappingWritableSink<W = any>(writable: WritableStreamLike<W>): WritableStreamUnderlyingSink<W>; | ||
export declare function createWrappingWritableSink<W = any>(writable: WritableStreamLike<W>): WritableStreamSink<W>; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
55942
17
0
72
621
+ Added@types/whatwg-streams@^0.0.7
+ Added@types/whatwg-streams@0.0.7(transitive)
- Removed@mattiasbuelens/web-streams-polyfill@0.1.0(transitive)
- Removed@types/whatwg-streams@0.0.6(transitive)