-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add (TeeTo|ReadFrom)ReadableStreamLink links (#387)
* add `(TeeTo|ReadFrom)ReadableStreamLink` links * add `skipDataTransport` function * also export type `ReadableStreamLinkEvent` * forgot file * feedback from code review * changeset * ensure tests run * Revert "ensure tests run" This reverts commit 31b9d01.
- Loading branch information
Showing
8 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client-react-streaming": minor | ||
--- | ||
|
||
add `TeeToReadableStreamLink` and `ReadFromReadableStreamLink` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/client-react-streaming/src/DataTransportAbstraction/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/client-react-streaming/src/ReadableStreamLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { ApolloLink, Observable } from "@apollo/client/index.js"; | ||
import type { FetchResult } from "@apollo/client/index.js"; | ||
|
||
export type ReadableStreamLinkEvent = | ||
| { type: "next"; value: FetchResult } | ||
| { type: "completed" } | ||
| { type: "error" }; | ||
|
||
interface InternalContext { | ||
[teeToReadableStreamKey]?: ReadableStreamDefaultController<ReadableStreamLinkEvent>; | ||
[readFromReadableStreamKey]?: ReadableStream<ReadableStreamLinkEvent>; | ||
} | ||
|
||
const teeToReadableStreamKey = Symbol.for( | ||
"apollo.tee.readableStreamController" | ||
); | ||
const readFromReadableStreamKey = Symbol.for("apollo.read.readableStream"); | ||
|
||
/** | ||
* Apply to a context that will be passed to a link chain containing `TeeToReadableStreamLink`. | ||
* @param controller | ||
* @param context | ||
* @returns | ||
*/ | ||
export function teeToReadableStream<T extends Record<string, any>>( | ||
controller: ReadableStreamDefaultController<ReadableStreamLinkEvent>, | ||
context: T | ||
): T & InternalContext { | ||
return Object.assign(context, { | ||
[teeToReadableStreamKey]: controller, | ||
}); | ||
} | ||
|
||
/** | ||
* Apply to a context that will be passed to a link chain containing `ReadFromReadableStreamLink`. | ||
* @param readableStream | ||
* @param context | ||
* @returns | ||
*/ | ||
export function readFromReadableStream<T extends Record<string, any>>( | ||
readableStream: ReadableStream<ReadableStreamLinkEvent>, | ||
context: T | ||
): T & InternalContext { | ||
return Object.assign(context, { | ||
[readFromReadableStreamKey]: readableStream, | ||
}); | ||
} | ||
|
||
/** | ||
* A link that allows the request to be cloned into a readable stream, e.g. for | ||
* transport of multipart responses from RSC or a server loader to the browser. | ||
*/ | ||
export const TeeToReadableStreamLink = new ApolloLink((operation, forward) => { | ||
const context = operation.getContext() as InternalContext; | ||
|
||
const controller = context[teeToReadableStreamKey]; | ||
|
||
if (controller) { | ||
return new Observable((observer) => { | ||
const subscription = forward(operation).subscribe({ | ||
next(result) { | ||
controller.enqueue({ type: "next", value: result }); | ||
observer.next(result); | ||
}, | ||
error(error) { | ||
controller.enqueue({ type: "error" }); | ||
controller.close(); | ||
observer.error(error); | ||
}, | ||
complete() { | ||
controller.enqueue({ type: "completed" }); | ||
controller.close(); | ||
observer.complete(); | ||
}, | ||
}); | ||
|
||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
}); | ||
} | ||
|
||
return forward(operation); | ||
}); | ||
|
||
/** | ||
* A link that allows the response to be read from a readable stream, e.g. for | ||
* hydration of a multipart response from RSC or a server loader in the browser. | ||
*/ | ||
export const ReadFromReadableStreamLink = new ApolloLink( | ||
(operation, forward) => { | ||
const context = operation.getContext() as InternalContext; | ||
|
||
const eventSteam = context[readFromReadableStreamKey]; | ||
if (eventSteam) { | ||
return new Observable((observer) => { | ||
let aborted = false as boolean; | ||
const reader = eventSteam.getReader(); | ||
consumeReader(); | ||
|
||
return () => { | ||
aborted = true; | ||
reader.cancel(); | ||
}; | ||
|
||
async function consumeReader() { | ||
let event: | ||
| ReadableStreamReadResult<ReadableStreamLinkEvent> | ||
| undefined = undefined; | ||
while (!aborted && !event?.done) { | ||
event = await reader.read(); | ||
if (aborted) break; | ||
if (event.value) { | ||
switch (event.value.type) { | ||
case "next": | ||
observer.next(event.value.value); | ||
break; | ||
case "completed": | ||
observer.complete(); | ||
break; | ||
case "error": | ||
observer.error( | ||
new Error( | ||
"Error from event stream. Redacted for security concerns." | ||
) | ||
); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return forward(operation); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters