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

Configurable Transports and Extraction of the Node HTTP Transport #265

Merged
merged 23 commits into from
Oct 27, 2018
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fbebb1f
WIP to allow configuration of Transports.
jonny-improbable Oct 20, 2018
dcda21d
Fix linting
jonny-improbable Oct 20, 2018
0774e58
Allow independent configuration of XHR and Fetch Transports.
jonny-improbable Oct 21, 2018
17066a9
Fix lint
jonny-improbable Oct 21, 2018
dbf695b
Allow the user to specify the default transport.
jonny-improbable Oct 21, 2018
f3d41e7
Refactor transport package
jonny-improbable Oct 21, 2018
b08e5a8
Drop support for NodeJS transports.
jonny-improbable Oct 21, 2018
f35d8de
Allow extended configuration of FetchInit
jonny-improbable Oct 21, 2018
84e10ba
Add support for referrerPolicy, keepalive and integrity FetchInit opt…
jonny-improbable Oct 21, 2018
e671411
Remove NodeJS typings as they are no longer required.
jonny-improbable Oct 22, 2018
a0cab8f
Rename `HttpTransport` -> `CrossBrowserHttpTransport` to make it clea…
jonny-improbable Oct 22, 2018
3f668d7
Merge branch 'master' into feature/configurable-transports
jonny-improbable Oct 22, 2018
dbe3fbd
Move Node HTTP transport into the repo and include it in the tests.
jonny-improbable Oct 24, 2018
afc8caf
Fix linking script
jonny-improbable Oct 24, 2018
b5c274b
Fix linking
jonny-improbable Oct 24, 2018
ee85f3c
Trying to get linking working on travis...
jonny-improbable Oct 25, 2018
86b3a6f
Give up on using npm link :D
jonny-improbable Oct 25, 2018
ebcb001
Revert "Give up on using npm link :D"
jonny-improbable Oct 25, 2018
bf08173
Make NPM Linking work after praying to the correct gods.
jonny-improbable Oct 25, 2018
26b0c07
Remove duplicate call to `link-npm-modules.sh`
jonny-improbable Oct 25, 2018
43ab1e9
- Reinstate note on NodeJS usage in README
jonny-improbable Oct 25, 2018
ca16ed5
Rewrite transport docs.
jonny-improbable Oct 27, 2018
d66c8af
Fix style issues.
jonny-improbable Oct 27, 2018
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
16 changes: 16 additions & 0 deletions grpc-web-node-http-transport/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# grpc-web-node-http-transport
Node HTTP Transport for use with [grpc-web-client](https://github.com/improbable-eng/grpc-web)

## Usage
When making a gprc request, specify this transport:

```
import { grpc } from 'grpc-web-client';
import { NodeHttpTransport } from 'grpc-web-node-http-transport';

grpc.invoke(MyService.DoQuery, {
host: "https://example.com",
transport: NodeHttpTransport(),
/* ... */
})
```
33 changes: 33 additions & 0 deletions grpc-web-node-http-transport/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions grpc-web-node-http-transport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "grpc-web-node-http-transport",
"version": "0.0.1",
"description": "Node HTTP Transport for use with grpc-web-client",
"main": "lib/index.js",
"repository": {
"type": "git",
"url": "github.com/improbable-eng/grpc-web"
},
"scripts": {
"clean": "rm -rf lib",
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"grpc-web-client": "^0.6.3"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@types/node": "^10.12.0",
"typescript": "^3.1.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import * as http from "http";
import * as https from "https";
import * as url from "url";
import {Transport, TransportOptions} from "./Transport";
import {Metadata} from "../metadata";
import { grpc } from 'grpc-web-client';

/* nodeHttpRequest uses the node http and https modules */
export default function nodeHttpRequest(options: TransportOptions): Transport {
options.debug && console.log("nodeHttpRequest", options);

return new NodeHttp(options);
export function NodeHttpTransport(): grpc.TransportFactory {
return (opts: grpc.TransportOptions) => {
return new NodeHttp(opts);
};
}

class NodeHttp implements Transport {
options: TransportOptions;
class NodeHttp implements grpc.Transport {
options: grpc.TransportOptions;
request: http.ClientRequest;

constructor(transportOptions: TransportOptions) {
constructor(transportOptions: grpc.TransportOptions) {
this.options = transportOptions;
}

Expand All @@ -31,7 +29,7 @@ class NodeHttp implements Transport {
responseCallback(response: http.IncomingMessage) {
this.options.debug && console.log("NodeHttp.response", response.statusCode);
const headers = filterHeadersForUndefined(response.headers);
this.options.onHeaders(new Metadata(headers), response.statusCode!);
this.options.onHeaders(new grpc.Metadata(headers), response.statusCode!);

response.on("data", chunk => {
this.options.debug && console.log("NodeHttp.data", chunk);
Expand All @@ -44,7 +42,7 @@ class NodeHttp implements Transport {
});
};

start(metadata: Metadata) {
start(metadata: grpc.Metadata) {
const headers: { [key: string]: string } = {};
metadata.forEach((key, values) => {
headers[key] = values.join(", ");
Expand Down Expand Up @@ -105,7 +103,3 @@ function toBuffer(ab: Uint8Array): Buffer {
}
return buf;
}

export function detectNodeHTTPSupport(): boolean {
return typeof module !== "undefined" && module.exports;
}
28 changes: 28 additions & 0 deletions grpc-web-node-http-transport/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"compilerOptions": {
"alwaysStrict": true,
"sourceMap": true,
"declaration": true,
"declarationDir": "lib",
"target": "es5",
"removeComments": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"stripInternal": true,
"noFallthroughCasesInSwitch": true,
"outDir": "lib",
"noEmitOnError": true
},
"types": [
"node"
],
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
13 changes: 13 additions & 0 deletions link-npm-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e

# Link the modules present in this repo so the tests run with
# the latest sources instead of those fetched from npm.

# Link local modules into the global store
pushd ts && npm link && popd
pushd grpc-web-node-http-transport && npm link && popd

# Link dependencies from the global store
pushd grpc-web-node-http-transport && npm link grpc-web-client && popd
pushd test && npm link grpc-web-node-http-transport && popd
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "grpc-web-ci",
"scripts": {
"install": "(cd ts && npm install) && (cd test && npm install)",
"install": "(cd ts && npm install) && (cd test && npm install) && (cd grpc-web-node-http-transport && npm install)",
"postinstall": "./link-npm-modules.sh && (cd ts && npm run lib:build) && (cd grpc-web-node-http-transport && npm run build)",
"test": "cd test && npm run test"
},
"author": "Improbable",
Expand Down
Loading