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

Removed Node transport from automatic detection #204

Closed
wants to merge 8 commits into from
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ This library is tested against:

## Node.js Support

`grpc-web-client` also [supports Node.js through a transport](ts/docs/transport.md#node-http-only-available-in-a-nodejs-environment) that uses the `http` and `https` packages. Usage does not vary from browser usage as transport is determined at runtime.
`grpc-web-client` also [supports Node.js through a transport](ts/docs/transport.md#node-http-only-available-in-a-nodejs-environment) that uses the `http` and `https` packages. In order to ensure the library is tree-shakable, you must explicitly import and specify the Node transport like so:

```
import nodeHttpRequest from "grpc-web-client/dist/transports/nodeHttp";
grpc.unary(BookService.QueryBooks, {
transport: nodeHttpRequest
});
```

If you want to use `grpc-web-client` in a node.js environment with Typescript, you must include `dom` in the `"lib"` Array in your `tsconfig.json` otherwise `tsc` will be unable to find some type declarations to compile. Note that `dom` will be included automatically if you do not declare `lib` in your configration and your target is one of `es5` or `es6`. (See [Typescript compiler options](https://www.typescriptlang.org/docs/handbook/compiler-options.html)).

Expand Down
3 changes: 2 additions & 1 deletion test/ts/src/testRpcCombinations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
corsHost
} from "../../hosts-config";
import {grpc} from "../../../ts/src/index";
import nodeHttpRequest, {detectNodeHTTPSupport} from "../../../ts/src/transports/nodeHttp";

type TestConfig = {
testHostUrl: string,
Expand Down Expand Up @@ -54,7 +55,7 @@ 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": detectNodeHTTPSupport() ? nodeHttpRequest : undefined
};

if (!process.env.DISABLE_WEBSOCKET_TESTS) {
Expand Down
8 changes: 2 additions & 6 deletions ts/src/transports/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {Metadata} from "../metadata";
import fetchRequest, {detectFetchSupport} from "./fetch";
import xhrRequest, {detectXHRSupport} from "./xhr";
import mozXhrRequest, {detectMozXHRSupport} from "./mozXhr";
import httpNodeRequest, {detectNodeHTTPSupport} from "./nodeHttp";
import {MethodDefinition} from "../service";
import {ProtobufMessage} from "../message";
import websocketRequest from "./websocket";
Expand Down Expand Up @@ -54,11 +53,8 @@ function detectTransport(): TransportConstructor {
return xhrRequest;
}

if (detectNodeHTTPSupport()) {
return httpNodeRequest;
}

throw new Error("No suitable transport found for gRPC-Web");
throw new Error("No suitable transport found for gRPC-Web. If running in " +
"Node, import and set the `transport` option to nodeHttpRequest");
}

export function WebsocketTransportFactory(transportOptions: TransportOptions): Transport | Error {
Expand Down