Comparing version 0.1.3 to 0.1.4
import { Metadata } from '@grpc/grpc-js'; | ||
import { AbortSignal } from 'node-abort-controller'; | ||
export declare type CallOptions = { | ||
@@ -3,0 +4,0 @@ deadline?: Date; |
@@ -7,12 +7,7 @@ "use strict"; | ||
function createChannel(address, credentials, options = {}) { | ||
const parts = address.split(/:\/\/(.*)/); | ||
let protocol; | ||
let host; | ||
if (parts.length === 1) { | ||
protocol = 'http'; | ||
host = address; | ||
const match = /^(?:([^:]+):\/\/)?(.*?)(?::(\d+))?$/.exec(address); | ||
if (match == null) { | ||
throw new Error(`Invalid address: '${address}'`); | ||
} | ||
else { | ||
[protocol, host] = parts; | ||
} | ||
const [, protocol = 'http', host, port = protocol === 'http' ? '80' : '443',] = match; | ||
if (protocol === 'http') { | ||
@@ -27,3 +22,3 @@ credentials !== null && credentials !== void 0 ? credentials : (credentials = grpc_js_1.ChannelCredentials.createInsecure()); | ||
} | ||
return new grpc_js_1.Channel(host, credentials, options); | ||
return new grpc_js_1.Channel(`${host}:${port}`, credentials, options); | ||
} | ||
@@ -30,0 +25,0 @@ exports.createChannel = createChannel; |
import { Metadata } from '@grpc/grpc-js'; | ||
import { AbortSignal } from 'node-abort-controller'; | ||
/** | ||
@@ -3,0 +4,0 @@ * Call context passed to server methods. |
@@ -8,3 +8,10 @@ import { ChannelOptions, ServerCredentials, ServiceDefinition } from '@grpc/grpc-js'; | ||
add<Service extends ServiceDefinition>(definition: Service, implementation: ServiceImplementation<Service, CallContextExt>): void; | ||
listen(address: string, credentials?: ServerCredentials): Promise<void>; | ||
/** | ||
* Start listening on given 'host:port'. | ||
* | ||
* Use 'localhost:0' to bind to a random port. | ||
* | ||
* Returns port that the server is bound to. | ||
*/ | ||
listen(address: string, credentials?: ServerCredentials): Promise<number>; | ||
shutdown(): Promise<void>; | ||
@@ -11,0 +18,0 @@ forceShutdown(): void; |
@@ -73,4 +73,4 @@ "use strict"; | ||
} | ||
await new Promise((resolve, reject) => { | ||
server.bindAsync(address, credentials !== null && credentials !== void 0 ? credentials : grpc_js_1.ServerCredentials.createInsecure(), err => { | ||
const port = await new Promise((resolve, reject) => { | ||
server.bindAsync(address, credentials !== null && credentials !== void 0 ? credentials : grpc_js_1.ServerCredentials.createInsecure(), (err, port) => { | ||
if (err != null) { | ||
@@ -80,3 +80,3 @@ reject(err); | ||
else { | ||
resolve(); | ||
resolve(port); | ||
} | ||
@@ -86,2 +86,3 @@ }); | ||
server.start(); | ||
return port; | ||
}, | ||
@@ -88,0 +89,0 @@ async shutdown() { |
{ | ||
"name": "nice-grpc", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A gRPC library that is nice to you", | ||
@@ -47,5 +47,5 @@ "keywords": [ | ||
"@grpc/grpc-js": "^1.2.6", | ||
"abort-controller-x": "^0.2.0", | ||
"node-abort-controller": "^1.1.0" | ||
"abort-controller-x": "^0.2.4", | ||
"node-abort-controller": "^1.2.1" | ||
} | ||
} |
@@ -12,2 +12,4 @@ # nice-grpc [![npm version][npm-image]][npm-url] | ||
- [Compiling Protobuf files](#compiling-protobuf-files) | ||
- [Using `ts-proto`](#using-ts-proto) | ||
- [Using `google-protobuf`](#using-google-protobuf) | ||
- [Server](#server) | ||
@@ -25,2 +27,3 @@ - [Errors](#errors) | ||
- [Example: Authentication](#example-authentication) | ||
- [Server Reflection](#server-reflection) | ||
- [Client](#client) | ||
@@ -54,4 +57,3 @@ - [Channels](#channels) | ||
``` | ||
npm install nice-grpc google-protobuf @grpc/grpc-js | ||
npm install --save-dev @types/google-protobuf | ||
npm install nice-grpc @grpc/grpc-js | ||
``` | ||
@@ -63,10 +65,33 @@ | ||
This works the same way as you would do for `grpc-js`. | ||
The recommended way is to use | ||
[`ts-proto`](https://github.com/stephenh/ts-proto). | ||
#### Using `ts-proto` | ||
Install necessary tools: | ||
``` | ||
npm install --save-dev grpc-tools grpc_tools_node_protoc_ts | ||
npm install protobufjs long | ||
npm install --save-dev grpc-tools ts-proto | ||
``` | ||
Given a Protobuf file `./proto/example.proto`, generate TypeScript code into | ||
directory `./compiled_proto`: | ||
``` | ||
./node_modules/.bin/grpc_tools_node_protoc \ | ||
--ts_proto_out=./compiled_proto \ | ||
--ts_proto_opt=outputServices=grpc-js \ | ||
./proto/example.proto | ||
``` | ||
#### Using `google-protobuf` | ||
Install necessary tools: | ||
``` | ||
npm install google-protobuf | ||
npm install --save-dev grpc-tools grpc_tools_node_protoc_ts @types/google-protobuf | ||
``` | ||
Given a Protobuf file `./proto/example.proto`, generate JS code and TypeScript | ||
@@ -77,4 +102,2 @@ definitions into directory `./compiled_proto`: | ||
./node_modules/.bin/grpc_tools_node_protoc \ | ||
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \ | ||
--plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin \ | ||
--js_out=import_style=commonjs,binary:./compiled_proto \ | ||
@@ -86,5 +109,2 @@ --ts_out=grpc_js:./compiled_proto \ | ||
Alternative methods include [Buf](https://github.com/bufbuild/buf) and | ||
[Prototool](https://github.com/uber/prototool). | ||
### Server | ||
@@ -538,2 +558,7 @@ | ||
#### Server Reflection | ||
See | ||
[deeplay-io/nice-grpc-server-reflection](https://github.com/deeplay-io/nice-grpc-server-reflection). | ||
### Client | ||
@@ -587,3 +612,3 @@ | ||
```ts | ||
client.close(); | ||
channel.close(); | ||
``` | ||
@@ -611,2 +636,5 @@ | ||
If port is omitted, it defaults to `80` for insecure connections, and `443` for | ||
secure connections. | ||
#### Metadata | ||
@@ -613,0 +641,0 @@ |
import {Metadata} from '@grpc/grpc-js'; | ||
import {AbortSignal} from 'node-abort-controller'; | ||
@@ -3,0 +4,0 @@ export type CallOptions = { |
@@ -9,14 +9,15 @@ import {Channel, ChannelCredentials, ChannelOptions} from '@grpc/grpc-js'; | ||
): Channel { | ||
const parts = address.split(/:\/\/(.*)/); | ||
const match = /^(?:([^:]+):\/\/)?(.*?)(?::(\d+))?$/.exec(address); | ||
let protocol: string; | ||
let host: string; | ||
if (parts.length === 1) { | ||
protocol = 'http'; | ||
host = address; | ||
} else { | ||
[protocol, host] = parts; | ||
if (match == null) { | ||
throw new Error(`Invalid address: '${address}'`); | ||
} | ||
const [ | ||
, | ||
protocol = 'http', | ||
host, | ||
port = protocol === 'http' ? '80' : '443', | ||
] = match; | ||
if (protocol === 'http') { | ||
@@ -32,3 +33,3 @@ credentials ??= ChannelCredentials.createInsecure(); | ||
return new Channel(host, credentials, options); | ||
return new Channel(`${host}:${port}`, credentials, options); | ||
} | ||
@@ -35,0 +36,0 @@ |
@@ -8,5 +8,5 @@ import { | ||
import {isAbortError, throwIfAborted, waitForEvent} from 'abort-controller-x'; | ||
import AbortController from 'node-abort-controller'; | ||
import AbortController, {AbortSignal} from 'node-abort-controller'; | ||
import {isAsyncIterable} from '../utils/isAsyncIterable'; | ||
import { patchClientWritableStream } from '../utils/patchClientWritableStream'; | ||
import {patchClientWritableStream} from '../utils/patchClientWritableStream'; | ||
import {readableToAsyncIterable} from '../utils/readableToAsyncIterable'; | ||
@@ -13,0 +13,0 @@ import {CallOptions} from './CallOptions'; |
@@ -13,5 +13,5 @@ import { | ||
} from 'abort-controller-x'; | ||
import AbortController from 'node-abort-controller'; | ||
import AbortController, {AbortSignal} from 'node-abort-controller'; | ||
import {isAsyncIterable} from '../utils/isAsyncIterable'; | ||
import { patchClientWritableStream } from '../utils/patchClientWritableStream' | ||
import {patchClientWritableStream} from '../utils/patchClientWritableStream'; | ||
import {CallOptions} from './CallOptions'; | ||
@@ -18,0 +18,0 @@ import {ClientStreamingClientMethod} from './Client'; |
import {Metadata} from '@grpc/grpc-js'; | ||
import {ServerSurfaceCall} from '@grpc/grpc-js/build/src/server-call'; | ||
import AbortController from 'node-abort-controller'; | ||
import AbortController, {AbortSignal} from 'node-abort-controller'; | ||
@@ -5,0 +5,0 @@ /** |
@@ -29,3 +29,10 @@ import { | ||
listen(address: string, credentials?: ServerCredentials): Promise<void>; | ||
/** | ||
* Start listening on given 'host:port'. | ||
* | ||
* Use 'localhost:0' to bind to a random port. | ||
* | ||
* Returns port that the server is bound to. | ||
*/ | ||
listen(address: string, credentials?: ServerCredentials): Promise<number>; | ||
@@ -160,11 +167,11 @@ shutdown(): Promise<void>; | ||
await new Promise<void>((resolve, reject) => { | ||
const port = await new Promise<number>((resolve, reject) => { | ||
server!.bindAsync( | ||
address, | ||
credentials ?? ServerCredentials.createInsecure(), | ||
err => { | ||
(err, port) => { | ||
if (err != null) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
resolve(port); | ||
} | ||
@@ -176,2 +183,4 @@ }, | ||
server.start(); | ||
return port; | ||
}, | ||
@@ -178,0 +187,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
162515
2901
911
Updatedabort-controller-x@^0.2.4
Updatednode-abort-controller@^1.2.1