-
Notifications
You must be signed in to change notification settings - Fork 436
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
Configurable Transports and Extraction of the Node HTTP Transport #265
Changes from 3 commits
fbebb1f
dcda21d
0774e58
17066a9
dbf695b
f3d41e7
b08e5a8
f35d8de
84e10ba
e671411
a0cab8f
3f668d7
dbe3fbd
afc8caf
b5c274b
ee85f3c
86b3a6f
ebcb001
bf08173
26b0c07
43ab1e9
ca16ed5
d66c8af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import {Metadata} from "../metadata"; | ||
import fetchRequest, {detectFetchSupport} from "./fetch"; | ||
import xhrRequest, {detectXHRSupport} from "./xhr"; | ||
import fetchRequest, {detectFetchSupport, FetchTransportInit} from "./fetch"; | ||
import xhrRequest, {XhrTransportInit} from "./xhr"; | ||
import mozXhrRequest, {detectMozXHRSupport} from "./mozXhr"; | ||
import httpNodeRequest, {detectNodeHTTPSupport} from "./nodeHttp"; | ||
import {MethodDefinition} from "../service"; | ||
import {ProtobufMessage} from "../message"; | ||
import websocketRequest from "./websocket"; | ||
|
@@ -14,8 +13,8 @@ export interface Transport { | |
start(metadata: Metadata): void | ||
} | ||
|
||
export interface TransportConstructor { | ||
(options: TransportOptions): Transport | Error; | ||
export interface HttpTransportConstructor { | ||
(options: TransportOptions, config: HttpTransportInit): Transport; | ||
} | ||
|
||
export interface TransportOptions { | ||
|
@@ -27,40 +26,42 @@ export interface TransportOptions { | |
onEnd: (err?: Error) => void; | ||
} | ||
|
||
let selectedTransport: TransportConstructor; | ||
export function DefaultTransportFactory(transportOptions: TransportOptions): Transport | Error { | ||
// The transports provided by DefaultTransportFactory do not support client-streaming | ||
if (transportOptions.methodDefinition.requestStream) { | ||
return new Error("No transport available for client-streaming (requestStream) method"); | ||
} | ||
export function DefaultHttpTransport(transportOptions: TransportOptions): Transport { | ||
return HttpTransport({ withCredentials: false })(transportOptions); | ||
} | ||
|
||
if (!selectedTransport) { | ||
selectedTransport = detectTransport(); | ||
} | ||
export interface HttpTransportInit { | ||
withCredentials?: boolean | ||
} | ||
|
||
return selectedTransport(transportOptions); | ||
export interface TransportFactory { | ||
(options: TransportOptions): Transport; | ||
} | ||
|
||
function detectTransport(): TransportConstructor { | ||
export function HttpTransport(init: HttpTransportInit): TransportFactory { | ||
if (detectFetchSupport()) { | ||
return fetchRequest; | ||
} | ||
|
||
if (detectMozXHRSupport()) { | ||
return mozXhrRequest; | ||
return FetchReadableStreamTransport({ credentials: init.withCredentials ? 'include' : 'same-origin' }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you configure this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation allows explicitly using |
||
} | ||
return XhrTransport({ withCredentials: init.withCredentials }); | ||
} | ||
|
||
if (detectXHRSupport()) { | ||
return xhrRequest; | ||
export function XhrTransport(init: XhrTransportInit): TransportFactory { | ||
return (opts: TransportOptions) => { | ||
if (detectMozXHRSupport()) { | ||
return mozXhrRequest(opts, init); | ||
} | ||
return xhrRequest(opts, init); | ||
} | ||
} | ||
|
||
if (detectNodeHTTPSupport()) { | ||
return httpNodeRequest; | ||
export function FetchReadableStreamTransport(init: FetchTransportInit): TransportFactory { | ||
return (opts: TransportOptions) => { | ||
return fetchRequest(opts, init); | ||
} | ||
|
||
throw new Error("No suitable transport found for gRPC-Web"); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you view this file it's easy to notice a lot of empty lines - we should probably clean them up. |
||
export function WebsocketTransportFactory(transportOptions: TransportOptions): Transport | Error { | ||
return websocketRequest(transportOptions); | ||
export function WebsocketTransport(): TransportFactory { | ||
return (opts: TransportOptions) => { | ||
return websocketRequest(opts); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change, but whilst I was here I wanted to fix this.