@stardazed/streams-fetch-adapter
Advanced tools
Comparing version 1.0.2 to 2.0.0
# @stardazed/streams-fetch-adapter changelog | ||
## 2.0.0 | ||
_2018-10-01_ | ||
* BREAKING: changed external APIs to support proper Response cloning for fetches ([#2](https://github.com/stardazed/sd-streams/issues/2)) | ||
## 1.0.2 | ||
@@ -4,0 +8,0 @@ _2018-09-18_ |
@@ -18,2 +18,13 @@ /** | ||
interface PipeToOptions { | ||
preventClose?: boolean; | ||
preventAbort?: boolean; | ||
preventCancel?: boolean; | ||
} | ||
interface StreamTransform { | ||
readable: ReadableStream; | ||
writable: ws.WritableStream; | ||
} | ||
interface ReadableStream { | ||
@@ -23,2 +34,4 @@ cancel(reason?: any): Promise<void>; | ||
tee(): ReadableStream[]; | ||
pipeThrough(transform: StreamTransform, options?: PipeToOptions): ReadableStream; | ||
pipeTo(dest: WritableStream, options?: PipeToOptions): Promise<void>; | ||
readonly locked: boolean; | ||
@@ -32,2 +45,4 @@ } | ||
type ReadableStreamTeeFunction = (stream: ReadableStream, cloneForBranch2: boolean) => ReadableStream[]; | ||
interface ResponseConstructor { | ||
@@ -58,13 +73,27 @@ new(body?: Blob | BufferSource | FormData | ReadableStream | string | null, init?: ResponseInit): Response; | ||
* @param nativeFetch A reference to the browser native fetch function to patch | ||
* @param nativeResponse The constructor function of the browser's built in Response class | ||
* @param nativeReadableStream The constructor function of the browser's built in ReadableStream class, if available | ||
* @param customReadableStream The constructor function of your custom ReadableStream | ||
* @param customReadableStreamTee The `ReadableStreamTee` method implementation for the custom ReadableStream | ||
*/ | ||
export declare function createAdaptedFetch(nativeFetch: GlobalFetch["fetch"], nativeReadableStream: ReadableStreamConstructor | undefined, customReadableStream: ReadableStreamConstructor): AdaptedFetch | ||
export declare function createAdaptedFetch( | ||
nativeFetch: GlobalFetch["fetch"], | ||
nativeResponse: ResponseConstructor, | ||
nativeReadableStream: ReadableStreamConstructor | undefined, | ||
customReadableStream: ReadableStreamConstructor, | ||
customReadableStreamTee: ReadableStreamTeeFunction | ||
): AdaptedFetch; | ||
/** | ||
* Wrap the Response constructor to add or patch handling of ReadableStream | ||
* body objects. | ||
* Wrap the Response constructor to add or patch handling of ReadableStream body objects. | ||
* @param nativeResponse The constructor function of the browser's built in Response class | ||
* @param nativeReadableStream The constructor function of the browser's built in ReadableStream class, if available | ||
* @param customReadableStream The constructor function of your custom ReadableStream | ||
* @param customReadableStreamTee The `ReadableStreamTee` method implementation for the custom ReadableStream | ||
*/ | ||
export declare function createAdaptedResponse(nativeResponse: ResponseConstructor, nativeReadableStream: ReadableStreamConstructor | undefined, customReadableStream: ReadableStreamConstructor): ResponseConstructor; | ||
export declare function createAdaptedResponse( | ||
nativeResponse: ResponseConstructor, | ||
nativeReadableStream: ReadableStreamConstructor | undefined, | ||
customReadableStream: ReadableStreamConstructor, | ||
customReadableStreamTee: ReadableStreamTeeFunction | ||
): ResponseConstructor; |
@@ -71,3 +71,3 @@ /** | ||
} | ||
function createAdaptedFetch(nativeFetch, nativeReadableStream, customReadableStream) { | ||
function createAdaptedFetch(nativeFetch, nativeResponse, nativeReadableStream, customReadableStream, customReadableStreamTee) { | ||
return function fetch(input, init) { | ||
@@ -86,2 +86,7 @@ return resolveRequestInitStream(init, nativeReadableStream, customReadableStream).then(resolvedInit => nativeFetch.call(undefined, input, resolvedInit).then((response) => { | ||
}); | ||
response.clone = function () { | ||
const [body1, body2] = customReadableStreamTee(response.body, true); | ||
response.body = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
}; | ||
} | ||
@@ -91,4 +96,6 @@ else { | ||
let wrappedBody; | ||
let customClone; | ||
response = new Proxy(origResponse, { | ||
get(target, prop, _receiver) { | ||
let value; | ||
if (prop === "body") { | ||
@@ -98,13 +105,23 @@ if (wrappedBody === undefined) { | ||
} | ||
return wrappedBody; | ||
value = wrappedBody; | ||
} | ||
else { | ||
const value = target[prop]; | ||
if (typeof value === "function") { | ||
return function (...args) { | ||
return value.apply(target, args); | ||
else if (prop === "clone") { | ||
if (customClone === undefined) { | ||
customClone = function () { | ||
const [body1, body2] = customReadableStreamTee(response.body, true); | ||
wrappedBody = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
}; | ||
} | ||
return value; | ||
value = customClone; | ||
} | ||
else { | ||
value = target[prop]; | ||
} | ||
if (typeof value === "function") { | ||
return function (...args) { | ||
return value.apply(target, args); | ||
}; | ||
} | ||
return value; | ||
}, | ||
@@ -148,3 +165,3 @@ }); | ||
} | ||
function createResponseProxyWithStreamBody(nativeResponse, body, init) { | ||
function createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body, init) { | ||
const tempResponse = new nativeResponse("fake", init); | ||
@@ -181,5 +198,5 @@ const mimeType = getMIMETypeFromHeadersInit(tempResponse.headers); | ||
clone() { | ||
const [body1, body2] = body.tee(); | ||
const [body1, body2] = customReadableStreamTee(body, true); | ||
body = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, body2, init); | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
} | ||
@@ -195,7 +212,7 @@ get body() { return body; } | ||
} | ||
function createAdaptedResponse(nativeResponse, nativeReadableStream, customReadableStream) { | ||
function createAdaptedResponse(nativeResponse, nativeReadableStream, customReadableStream, customReadableStreamTee) { | ||
const wrappedResponse = function (body, init) { | ||
if (body instanceof customReadableStream) { | ||
if (nativeReadableStream === undefined || !("body" in nativeResponse)) { | ||
return createResponseProxyWithStreamBody(nativeResponse, body, init); | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body, init); | ||
} | ||
@@ -202,0 +219,0 @@ body = wrapReadableStream(body, nativeReadableStream); |
@@ -77,3 +77,3 @@ (function (global, factory) { | ||
} | ||
function createAdaptedFetch(nativeFetch, nativeReadableStream, customReadableStream) { | ||
function createAdaptedFetch(nativeFetch, nativeResponse, nativeReadableStream, customReadableStream, customReadableStreamTee) { | ||
return function fetch(input, init) { | ||
@@ -92,2 +92,7 @@ return resolveRequestInitStream(init, nativeReadableStream, customReadableStream).then(resolvedInit => nativeFetch.call(undefined, input, resolvedInit).then((response) => { | ||
}); | ||
response.clone = function () { | ||
const [body1, body2] = customReadableStreamTee(response.body, true); | ||
response.body = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
}; | ||
} | ||
@@ -97,4 +102,6 @@ else { | ||
let wrappedBody; | ||
let customClone; | ||
response = new Proxy(origResponse, { | ||
get(target, prop, _receiver) { | ||
let value; | ||
if (prop === "body") { | ||
@@ -104,13 +111,23 @@ if (wrappedBody === undefined) { | ||
} | ||
return wrappedBody; | ||
value = wrappedBody; | ||
} | ||
else { | ||
const value = target[prop]; | ||
if (typeof value === "function") { | ||
return function (...args) { | ||
return value.apply(target, args); | ||
else if (prop === "clone") { | ||
if (customClone === undefined) { | ||
customClone = function () { | ||
const [body1, body2] = customReadableStreamTee(response.body, true); | ||
wrappedBody = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
}; | ||
} | ||
return value; | ||
value = customClone; | ||
} | ||
else { | ||
value = target[prop]; | ||
} | ||
if (typeof value === "function") { | ||
return function (...args) { | ||
return value.apply(target, args); | ||
}; | ||
} | ||
return value; | ||
}, | ||
@@ -154,3 +171,3 @@ }); | ||
} | ||
function createResponseProxyWithStreamBody(nativeResponse, body, init) { | ||
function createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body, init) { | ||
const tempResponse = new nativeResponse("fake", init); | ||
@@ -187,5 +204,5 @@ const mimeType = getMIMETypeFromHeadersInit(tempResponse.headers); | ||
clone() { | ||
const [body1, body2] = body.tee(); | ||
const [body1, body2] = customReadableStreamTee(body, true); | ||
body = body1; | ||
return createResponseProxyWithStreamBody(nativeResponse, body2, init); | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body2, init); | ||
} | ||
@@ -201,7 +218,7 @@ get body() { return body; } | ||
} | ||
function createAdaptedResponse(nativeResponse, nativeReadableStream, customReadableStream) { | ||
function createAdaptedResponse(nativeResponse, nativeReadableStream, customReadableStream, customReadableStreamTee) { | ||
const wrappedResponse = function (body, init) { | ||
if (body instanceof customReadableStream) { | ||
if (nativeReadableStream === undefined || !("body" in nativeResponse)) { | ||
return createResponseProxyWithStreamBody(nativeResponse, body, init); | ||
return createResponseProxyWithStreamBody(nativeResponse, customReadableStreamTee, body, init); | ||
} | ||
@@ -208,0 +225,0 @@ body = wrapReadableStream(body, nativeReadableStream); |
{ | ||
"name": "@stardazed/streams-fetch-adapter", | ||
"description": "Patch fetch and Response to work with custom stream implementations", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"author": { | ||
@@ -18,3 +18,3 @@ "name": "Arthur Langereis" | ||
"scripts": { | ||
"build": "rollup -c", | ||
"build": "tsc --noEmit -p . && rollup -c", | ||
"prepublishOnly": "npm run build" | ||
@@ -27,6 +27,6 @@ }, | ||
"devDependencies": { | ||
"rollup": "^0.65.2", | ||
"rollup": "^0.66.2", | ||
"rollup-plugin-typescript": "^0.8.1", | ||
"typescript": "^3.0.3" | ||
"typescript": "^3.1.1" | ||
} | ||
} |
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
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
27700
526