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

Metadata alias for BrowserHeaders + Docs update #65

Merged
merged 7 commits into from
Jul 16, 2017
Merged
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: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

[gRPC-Web](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-WEB.md) is a cutting-edge spec that enables invoking gRPC services from *modern* browsers.

*__Please note - Whilst this package supports Node.js, there is an [official Node.js gRPC library](https://www.npmjs.com/package/grpc) that does not require the server to support gRPC-Web__*

Components of the stack are based on Golang and TypeScript:
* [`grpcweb`](go/grpcweb) - a Go package that wraps an existing `grpc.Server` as a gRPC-Web `http.Handler` for both HTTP2 and HTTP/1.1
* [`grpcwebproxy`](go/grpcwebproxy) - a Go-based stand-alone reverse proxy for classic gRPC servers (e.g. in Java or C++) that exposes their services over gRPC-Web to modern browsers
Expand Down Expand Up @@ -133,7 +135,7 @@ This library is tested against:

`grpc-web-client` also supports Node.js through a transport that uses the `http` and `https` packages. Usage does not vary from browser usage as transport is determined at runtime.

*__Please note - There is an [official Node.js gRPC library](https://github.com/grpc/grpc/tree/9a69478498232b6b42169f8a1a389b51fb4e03ec/src/node) that does not require the server to support gRPC-Web__*
*__Please note - There is an [official Node.js gRPC library](https://www.npmjs.com/package/grpc) that does not require the server to support gRPC-Web__*

### Client-side streaming

Expand Down
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"dependencies": {
"@types/google-protobuf": "^3.2.5",
"google-protobuf": "^3.2.0",
"grpc-web-client": "0.2.0"
"grpc-web-client": "0.3.0"
},
"devDependencies": {
"concurrently": "^3.4.0",
"ts-loader": "^2.0.1",
"ts-protoc-gen": "^0.2.2",
"typescript": "^2.2.1",
"ts-protoc-gen": "0.3.2",
"typescript": "2.2.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.2"
}
Expand Down
24 changes: 12 additions & 12 deletions example/ts/_proto/examplecom/library/book_service_pb_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ export class BookService {
}
export namespace BookService {
export class GetBook {
static methodName = "GetBook";
static service = BookService;
static requestStream = false;
static responseStream = false;
static requestType = examplecom_library_book_service_pb.GetBookRequest;
static responseType = examplecom_library_book_service_pb.Book;
static readonly methodName = "GetBook";
static readonly service = BookService;
static readonly requestStream = false;
static readonly responseStream = false;
static readonly requestType = examplecom_library_book_service_pb.GetBookRequest;
static readonly responseType = examplecom_library_book_service_pb.Book;
}
export class QueryBooks {
static methodName = "QueryBooks";
static service = BookService;
static requestStream = false;
static responseStream = true;
static requestType = examplecom_library_book_service_pb.QueryBooksRequest;
static responseType = examplecom_library_book_service_pb.Book;
static readonly methodName = "QueryBooks";
static readonly service = BookService;
static readonly requestStream = false;
static readonly responseStream = true;
static readonly requestType = examplecom_library_book_service_pb.QueryBooksRequest;
static readonly responseType = examplecom_library_book_service_pb.Book;
}
}
25 changes: 12 additions & 13 deletions example/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {grpc, BrowserHeaders} from "grpc-web-client";
import {grpc, Code, Metadata} from "grpc-web-client";
import {BookService} from "../_proto/examplecom/library/book_service_pb_service";
import {QueryBooksRequest, Book, GetBookRequest} from "../_proto/examplecom/library/book_service_pb";

Expand All @@ -8,18 +8,17 @@ const host = USE_TLS ? "https://localhost:9091" : "http://localhost:9090";
function getBook() {
const getBookRequest = new GetBookRequest();
getBookRequest.setIsbn(60929871);
grpc.invoke(BookService.GetBook, {
grpc.unary(BookService.GetBook, {
request: getBookRequest,
host: host,
onHeaders: (headers: BrowserHeaders) => {
console.log("getBook.onHeaders", headers);
},
onMessage: (message: Book) => {
console.log("getBook.onMessage", message.toObject());
},
onEnd: (code: grpc.Code, msg: string, trailers: BrowserHeaders) => {
console.log("getBook.onEnd", code, msg, trailers);

onEnd: res => {
const { status, statusMessage, headers, message, trailers } = res;
console.log("getBook.onEnd.status", status, statusMessage);
console.log("getBook.onEnd.headers", headers);
if (status === Code.OK && message) {
console.log("getBook.onEnd.message", message.toObject());
}
console.log("getBook.onEnd.trailers", trailers);
queryBooks();
}
});
Expand All @@ -33,13 +32,13 @@ function queryBooks() {
grpc.invoke(BookService.QueryBooks, {
request: queryBooksRequest,
host: host,
onHeaders: (headers: BrowserHeaders) => {
onHeaders: (headers: Metadata) => {
console.log("queryBooks.onHeaders", headers);
},
onMessage: (message: Book) => {
console.log("queryBooks.onMessage", message.toObject());
},
onEnd: (code: grpc.Code, msg: string, trailers: BrowserHeaders) => {
onEnd: (code: Code, msg: string, trailers: Metadata) => {
console.log("queryBooks.onEnd", code, msg, trailers);
}
});
Expand Down
4 changes: 2 additions & 2 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"lodash": "^4.17.4",
"ts-loader": "^2.0.1",
"ts-node": "^3.0.4",
"ts-protoc-gen": "^0.3.0",
"ts-protoc-gen": "0.3.2",
"tslint": "^4.4.2",
"typescript": "^2.2.1",
"typescript": "2.2.1",
"wd": "^1.2.0",
"webpack": "^2.2.1"
}
Expand Down
54 changes: 27 additions & 27 deletions test/ts/src/grpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const DEBUG: boolean = (global as any).DEBUG;
// gRPC-Web library
import {
grpc,
Code,
BrowserHeaders,
} from "../../../ts/src/index";
import Code = grpc.Code;
import UnaryMethodDefinition = grpc.UnaryMethodDefinition;

// Generated Test Classes
Expand Down Expand Up @@ -95,9 +95,9 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
assert.deepEqual(message.getValue(), "hello world");
assert.deepEqual(message.getCounter(), 252);
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withTrailers) {
assert.deepEqual(trailers.get("TrailerTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -141,9 +141,9 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
assert.deepEqual(message.getValue(), "hello world");
assert.deepEqual(message.getCounter(), 252);
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withTrailers) {
assert.deepEqual(trailers.get("TrailerTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -184,9 +184,9 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
assert.ok(message instanceof PingResponse);
assert.strictEqual(message.getCounter(), onMessageId++);
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withTrailers) {
assert.deepEqual(trailers.get("TrailerTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -241,9 +241,9 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
}
lastMessageTime = thisTime;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withTrailers) {
assert.deepEqual(trailers.get("TrailerTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -284,9 +284,9 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
assert.ok(message instanceof PingResponse);
assert.strictEqual(message.getCounter(), onMessageId++);
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withTrailers) {
assert.deepEqual(trailers.get("TrailerTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -322,11 +322,11 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: Empty) => {
didGetOnMessage = true;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.deepEqual(trailers.get("grpc-status"), ["12"]);
assert.deepEqual(trailers.get("grpc-message"), ["Intentionally returning error for PingError"]);
assert.strictEqual(status, grpc.Code.Unimplemented);
assert.strictEqual(status, Code.Unimplemented);
assert.strictEqual(statusMessage, "Intentionally returning error for PingError");
assert.ok(didGetOnHeaders);
assert.ok(!didGetOnMessage);
Expand Down Expand Up @@ -358,11 +358,11 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: Empty) => {
didGetOnMessage = true;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
// Some browsers return empty Headers for failed requests
assert.strictEqual(statusMessage, "Response closed without headers");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
assert.ok(!didGetOnMessage);
done();
}
Expand Down Expand Up @@ -390,10 +390,10 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: Empty) => {
didGetOnMessage = true;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(statusMessage, "Response closed without grpc-status (Headers only)");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
assert.ok(!didGetOnMessage);
done();
}
Expand All @@ -417,10 +417,10 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: Empty) => {
didGetOnMessage = true;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(statusMessage, "Response closed without headers");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
assert.ok(!didGetOnMessage);
done();
}
Expand All @@ -446,7 +446,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: Empty) => {
didGetOnMessage = true;
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "trailers", trailers);
assert.strictEqual(statusMessage, "unknown service improbable.grpcweb.test.FailService");
assert.strictEqual(status, 12);
Expand Down Expand Up @@ -484,7 +484,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onMessage: (message: PingResponse) => {
throw new Error("onMessage exception");
},
onEnd: (status: grpc.Code, statusMessage: string, trailers: BrowserHeaders) => {
onEnd: (status: Code, statusMessage: string, trailers: BrowserHeaders) => {
setTimeout(() => {
uncaughtHandler.detach();
const exceptionsCaught = uncaughtHandler.getMessages();
Expand Down Expand Up @@ -532,7 +532,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
host: testHostUrl,
onEnd: ({status, statusMessage, headers, message, trailers}) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withHeaders) {
assert.deepEqual(headers.get("HeaderTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -567,7 +567,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
host: testHostUrl,
onEnd: ({status, statusMessage, headers, message, trailers}) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
assert.strictEqual(status, grpc.Code.OK, "expected OK (0)");
assert.strictEqual(status, Code.OK, "expected OK (0)");
assert.strictEqual(statusMessage, undefined, "expected no message");
if (withHeaders) {
assert.deepEqual(headers.get("HeaderTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -601,7 +601,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
host: testHostUrl,
onEnd: ({status, statusMessage, headers, message, trailers}) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
assert.strictEqual(status, grpc.Code.Unimplemented);
assert.strictEqual(status, Code.Unimplemented);
assert.strictEqual(statusMessage, "Intentionally returning error for PingError");
if (withHeaders) {
assert.deepEqual(headers.get("HeaderTestKey1"), ["ServerValue1"]);
Expand Down Expand Up @@ -636,7 +636,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
// Some browsers return empty Headers for failed requests
assert.strictEqual(statusMessage, "Response closed without headers");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
done();
}
});
Expand All @@ -654,7 +654,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onEnd: ({status, statusMessage, headers, message, trailers}) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
assert.strictEqual(statusMessage, "Response closed without grpc-status (Headers only)");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
assert.deepEqual(headers.get("grpc-status"), []);
assert.deepEqual(headers.get("grpc-message"), []);
done();
Expand All @@ -673,7 +673,7 @@ function runTests({testHostUrl, corsHostUrl, unavailableHost, emptyHost}: TestCo
onEnd: ({status, statusMessage, headers, message, trailers}) => {
DEBUG && debug("status", status, "statusMessage", statusMessage, "headers", headers, "res", message, "trailers", trailers);
assert.strictEqual(statusMessage, "Response closed without headers");
assert.strictEqual(status, grpc.Code.Internal);
assert.strictEqual(status, Code.Internal);
assert.isNull(message);
done();
}
Expand Down
Loading