Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transport options (variant B) #261

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/ts/src/testRpcCombinations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export function runWithSupportedTransports(cb: (transport: grpc.TransportConstru
"defaultTransport": undefined
};

if (typeof window !== "undefined") {
transports["fetchWithOptions"] = grpc.transports.fetchRequestWithOptions({})
}

if (!process.env.DISABLE_WEBSOCKET_TESTS) {
transports["websocketTransport"] = grpc.WebsocketTransportFactory
}
Expand Down
6 changes: 6 additions & 0 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as impClient from "./client";
import * as impService from "./service";
import * as impMessage from "./message";

import * as impTransportFetch from "./transports/fetch";

export namespace grpc {
export interface ProtobufMessageClass<T extends ProtobufMessage> extends impMessage.ProtobufMessageClass<T> {}
export interface ProtobufMessage extends impMessage.ProtobufMessage {}
Expand Down Expand Up @@ -37,4 +39,8 @@ export namespace grpc {
export const unary = impUnary.unary;
export interface UnaryOutput<TResponse extends ProtobufMessage> extends impUnary.UnaryOutput<TResponse> {}
export interface UnaryRpcOptions<TRequest extends ProtobufMessage, TResponse extends ProtobufMessage> extends impUnary.UnaryRpcOptions<TRequest, TResponse> {}

export namespace transports {
export const fetchRequestWithOptions = impTransportFetch.fetchRequestWithOptions;
}
}
21 changes: 16 additions & 5 deletions ts/src/transports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ import {Transport, TransportOptions} from "./Transport";
import {debug} from "../debug";
import detach from "../detach";

interface FetchOptions {
credentials?: RequestCredentials;
}

/* fetchRequestWithOptions provides a way to construct a Fetch transport with custom FetchOptions */
export function fetchRequestWithOptions(fetchOptions: FetchOptions = {}): (options: TransportOptions) => Transport {
return function(options: TransportOptions) {
return fetchRequest(options, fetchOptions);
}
}

/* fetchRequest uses Fetch (with ReadableStream) to read response chunks without buffering the entire response. */
export default function fetchRequest(options: TransportOptions): Transport {
options.debug && debug("fetchRequest", options);
return new Fetch(options);
export default function fetchRequest(options: TransportOptions, fetchOptions: FetchOptions = {}): Transport {
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
options.debug && debug("fetchRequest", options, fetchOptions);
return new Fetch(options, fetchOptions);
}

declare const Response: any;
Expand All @@ -19,7 +30,7 @@ class Fetch implements Transport {
metadata: Metadata;
controller: AbortController | undefined = (window as any).AbortController && new AbortController();

constructor(transportOptions: TransportOptions) {
constructor(transportOptions: TransportOptions, private readonly fetchOptions: FetchOptions) {
this.options = transportOptions;
}

Expand Down Expand Up @@ -63,7 +74,7 @@ class Fetch implements Transport {
headers: this.metadata.toHeaders(),
method: "POST",
body: msgBytes,
credentials: "same-origin",
credentials: this.fetchOptions.credentials || "same-origin",
signal: this.controller && this.controller.signal
}).then((res: Response) => {
this.options.debug && debug("Fetch.response", res);
Expand Down