From f462503b61dbd5d1f774bd5a779ed0675a48e7c4 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 10:49:08 +0300 Subject: [PATCH 1/6] Added support for specifying custom options for fetch transport --- ts/src/transports/fetch.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ts/src/transports/fetch.ts b/ts/src/transports/fetch.ts index d4c24104..5c0e85ea 100644 --- a/ts/src/transports/fetch.ts +++ b/ts/src/transports/fetch.ts @@ -3,10 +3,14 @@ import {Transport, TransportOptions} from "./Transport"; import {debug} from "../debug"; import detach from "../detach"; +interface FetchOptions { + credentials?: RequestCredentials; +} + /* 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 { + options.debug && debug("fetchRequest", options, fetchOptions); + return new Fetch(options, fetchOptions); } declare const Response: any; @@ -19,7 +23,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; } @@ -63,7 +67,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); From 464f4d5c993d0d973b3a82d1cd162da342c8844a Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 10:54:17 +0300 Subject: [PATCH 2/6] Added a way to construct Fetch transport with custom options --- ts/src/transports/fetch.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ts/src/transports/fetch.ts b/ts/src/transports/fetch.ts index 5c0e85ea..399bfbfa 100644 --- a/ts/src/transports/fetch.ts +++ b/ts/src/transports/fetch.ts @@ -7,6 +7,13 @@ 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, fetchOptions: FetchOptions = {}): Transport { options.debug && debug("fetchRequest", options, fetchOptions); From 2c058f7be1ee035f9ef204a62686dd4d28d11546 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 11:27:41 +0300 Subject: [PATCH 3/6] Testing with fetchWithOptions transport --- test/ts/src/testRpcCombinations.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/ts/src/testRpcCombinations.ts b/test/ts/src/testRpcCombinations.ts index f71416f3..d890d894 100644 --- a/test/ts/src/testRpcCombinations.ts +++ b/test/ts/src/testRpcCombinations.ts @@ -3,6 +3,7 @@ import { corsHost } from "../../hosts-config"; import {grpc} from "../../../ts/src/index"; +import { fetchRequestWithOptions } from "../../../ts/src/transports/fetch"; type TestConfig = { testHostUrl: string, @@ -54,7 +55,8 @@ export function runWithHttp1AndHttp2(cb: (config: TestConfig) => void) { export function runWithSupportedTransports(cb: (transport: grpc.TransportConstructor | undefined) => void) { const transports: {[key: string]: grpc.TransportConstructor | undefined} = { - "defaultTransport": undefined + "defaultTransport": undefined, + "fetchWithOptions": fetchRequestWithOptions({}), }; if (!process.env.DISABLE_WEBSOCKET_TESTS) { From 2640fe0831aa7f4f4256ff99767ff3dba1c781f2 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 12:29:58 +0300 Subject: [PATCH 4/6] Only test fetchWithOptions in a browser environment --- test/ts/src/testRpcCombinations.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/ts/src/testRpcCombinations.ts b/test/ts/src/testRpcCombinations.ts index d890d894..8e983296 100644 --- a/test/ts/src/testRpcCombinations.ts +++ b/test/ts/src/testRpcCombinations.ts @@ -55,10 +55,13 @@ export function runWithHttp1AndHttp2(cb: (config: TestConfig) => void) { export function runWithSupportedTransports(cb: (transport: grpc.TransportConstructor | undefined) => void) { const transports: {[key: string]: grpc.TransportConstructor | undefined} = { - "defaultTransport": undefined, - "fetchWithOptions": fetchRequestWithOptions({}), + "defaultTransport": undefined }; + if (typeof window !== "undefined") { + transports["fetchWithOptions"] = fetchRequestWithOptions({}) + } + if (!process.env.DISABLE_WEBSOCKET_TESTS) { transports["websocketTransport"] = grpc.WebsocketTransportFactory } From 3e3a7376f48f5f2a6b5c136f7778aa756d333e5f Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 13:22:16 +0300 Subject: [PATCH 5/6] Exposed fetchRequestWithOptions in the public API --- ts/src/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ts/src/index.ts b/ts/src/index.ts index 5e1b8cb7..03009a51 100644 --- a/ts/src/index.ts +++ b/ts/src/index.ts @@ -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 extends impMessage.ProtobufMessageClass {} export interface ProtobufMessage extends impMessage.ProtobufMessage {} @@ -37,4 +39,8 @@ export namespace grpc { export const unary = impUnary.unary; export interface UnaryOutput extends impUnary.UnaryOutput {} export interface UnaryRpcOptions extends impUnary.UnaryRpcOptions {} + + export namespace transports { + export const fetchRequestWithOptions = impTransportFetch.fetchRequestWithOptions; + } } From 4a492a40f8feea88f7c6336afb15a055fac41a5e Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Fri, 19 Oct 2018 13:22:38 +0300 Subject: [PATCH 6/6] Switched to using fetchRequestWithOptions from the public API in tests --- test/ts/src/testRpcCombinations.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/ts/src/testRpcCombinations.ts b/test/ts/src/testRpcCombinations.ts index 8e983296..a4b425bb 100644 --- a/test/ts/src/testRpcCombinations.ts +++ b/test/ts/src/testRpcCombinations.ts @@ -3,7 +3,6 @@ import { corsHost } from "../../hosts-config"; import {grpc} from "../../../ts/src/index"; -import { fetchRequestWithOptions } from "../../../ts/src/transports/fetch"; type TestConfig = { testHostUrl: string, @@ -59,7 +58,7 @@ export function runWithSupportedTransports(cb: (transport: grpc.TransportConstru }; if (typeof window !== "undefined") { - transports["fetchWithOptions"] = fetchRequestWithOptions({}) + transports["fetchWithOptions"] = grpc.transports.fetchRequestWithOptions({}) } if (!process.env.DISABLE_WEBSOCKET_TESTS) {