Skip to content

Commit 52e69ef

Browse files
committed
init files
1 parent a19c04a commit 52e69ef

17 files changed

+3082
-0
lines changed

‎client/all-users.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
2+
import { User } from "../proto/users_pb";
3+
import { client } from "./utils";
4+
5+
export default function allUsers() {
6+
return new Promise<User[]>((resolve, reject) => {
7+
const stream = client.getUsers(new Empty());
8+
const users: User[] = [];
9+
stream.on("data", (user) => users.push(user));
10+
stream.on("error", reject);
11+
stream.on("end", () => resolve(users));
12+
});
13+
}

‎client/create-users.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { User } from "../proto/users_pb";
2+
import { client, noop } from "./utils";
3+
4+
export default function createNewUsers(users: User[]) {
5+
const stream = client.createUser(noop);
6+
for (const user of users) {
7+
stream.write(user);
8+
}
9+
stream.end();
10+
}

‎client/get-user.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { User, UserRequest } from "../proto/users_pb";
2+
import { client } from "./utils";
3+
4+
export default function getUsers(id: number) {
5+
return new Promise<User>((resolve, reject) => {
6+
const request = new UserRequest();
7+
request.setId(id);
8+
9+
client.getUser(request, (err, user) => {
10+
if (err) reject(err);
11+
else resolve(user);
12+
});
13+
});
14+
}

‎client/index.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { User, UserStatus } from "../proto/users_pb";
2+
import getUser from "./get-user";
3+
import createUsers from "./create-users";
4+
import allUsers from "./all-users";
5+
6+
async function run() {
7+
const user = await getUser(1);
8+
console.log(user.toString());
9+
10+
const jim = new User();
11+
jim.setName("Jim");
12+
jim.setAge(10);
13+
jim.setId(20);
14+
jim.setStatus(UserStatus.OFFLINE);
15+
createUsers([jim]);
16+
console.log(`\nCreated user ${jim.toString()}`);
17+
18+
const users = await allUsers();
19+
console.log(`\nListing all ${users.length} users`);
20+
for (const user of users) {
21+
console.log(user.toString());
22+
}
23+
}
24+
25+
run();

‎client/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { UsersClient } from "../proto/users_grpc_pb";
2+
import { credentials } from "grpc";
3+
4+
const port = 3000;
5+
6+
export const client = new UsersClient(
7+
`localhost:${port}`,
8+
credentials.createInsecure()
9+
);
10+
11+
export const noop = () => {};

‎package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "ts-grpc",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"devDependencies": {
7+
"@types/google-protobuf": "^3.7.2",
8+
"grpc-tools": "^1.9.1",
9+
"grpc_tools_node_protoc_ts": "^4.1.2",
10+
"ts-node": "^8.10.2",
11+
"typescript": "^3.9.7"
12+
},
13+
"dependencies": {
14+
"grpc": "^1.24.3"
15+
},
16+
"scripts": {
17+
"build": "sh proto/build.sh",
18+
"server": "ts-node server/index.ts",
19+
"client": "ts-node client/index.ts"
20+
}
21+
}

‎proto/build.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
PROTO_DIR=./proto
4+
5+
# Generate JavaScript code
6+
yarn run grpc_tools_node_protoc \
7+
--js_out=import_style=commonjs,binary:${PROTO_DIR} \
8+
--grpc_out=${PROTO_DIR} \
9+
--plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin \
10+
-I ./proto \
11+
proto/*.proto
12+
13+
# Generate TypeScript code (d.ts)
14+
yarn run grpc_tools_node_protoc \
15+
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
16+
--ts_out=${PROTO_DIR} \
17+
-I ./proto \
18+
proto/*.proto
19+

‎proto/users.proto

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
syntax = "proto3";
2+
3+
package users;
4+
5+
import "google/protobuf/empty.proto";
6+
7+
enum UserStatus {
8+
UNKNOWN = 0;
9+
OFFLINE = 1;
10+
BUSY = 2;
11+
AVAILABLE = 3;
12+
}
13+
14+
message User {
15+
int32 id = 1;
16+
string name = 2;
17+
int32 age = 3;
18+
UserStatus status = 4;
19+
}
20+
21+
message UserRequest {
22+
int32 id = 1;
23+
}
24+
25+
service Users {
26+
rpc GetUser(UserRequest) returns (User) {};
27+
rpc CreateUser(stream User) returns (google.protobuf.Empty) {}
28+
rpc GetUsers(google.protobuf.Empty) returns (stream User) {};
29+
}

‎proto/users_grpc_pb.d.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// package: users
2+
// file: users.proto
3+
4+
/* tslint:disable */
5+
/* eslint-disable */
6+
7+
import * as grpc from "grpc";
8+
import * as users_pb from "./users_pb";
9+
import * as google_protobuf_empty_pb from "google-protobuf/google/protobuf/empty_pb";
10+
11+
interface IUsersService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
12+
getUser: IUsersService_IGetUser;
13+
createUser: IUsersService_ICreateUser;
14+
getUsers: IUsersService_IGetUsers;
15+
}
16+
17+
interface IUsersService_IGetUser extends grpc.MethodDefinition<users_pb.UserRequest, users_pb.User> {
18+
path: string; // "/users.Users/GetUser"
19+
requestStream: false;
20+
responseStream: false;
21+
requestSerialize: grpc.serialize<users_pb.UserRequest>;
22+
requestDeserialize: grpc.deserialize<users_pb.UserRequest>;
23+
responseSerialize: grpc.serialize<users_pb.User>;
24+
responseDeserialize: grpc.deserialize<users_pb.User>;
25+
}
26+
interface IUsersService_ICreateUser extends grpc.MethodDefinition<users_pb.User, google_protobuf_empty_pb.Empty> {
27+
path: string; // "/users.Users/CreateUser"
28+
requestStream: true;
29+
responseStream: false;
30+
requestSerialize: grpc.serialize<users_pb.User>;
31+
requestDeserialize: grpc.deserialize<users_pb.User>;
32+
responseSerialize: grpc.serialize<google_protobuf_empty_pb.Empty>;
33+
responseDeserialize: grpc.deserialize<google_protobuf_empty_pb.Empty>;
34+
}
35+
interface IUsersService_IGetUsers extends grpc.MethodDefinition<google_protobuf_empty_pb.Empty, users_pb.User> {
36+
path: string; // "/users.Users/GetUsers"
37+
requestStream: false;
38+
responseStream: true;
39+
requestSerialize: grpc.serialize<google_protobuf_empty_pb.Empty>;
40+
requestDeserialize: grpc.deserialize<google_protobuf_empty_pb.Empty>;
41+
responseSerialize: grpc.serialize<users_pb.User>;
42+
responseDeserialize: grpc.deserialize<users_pb.User>;
43+
}
44+
45+
export const UsersService: IUsersService;
46+
47+
export interface IUsersServer {
48+
getUser: grpc.handleUnaryCall<users_pb.UserRequest, users_pb.User>;
49+
createUser: grpc.handleClientCall<users_pb.User, google_protobuf_empty_pb.Empty>;
50+
getUsers: grpc.handleServerCall<google_protobuf_empty_pb.Empty, users_pb.User>;
51+
}
52+
53+
export interface IUsersClient {
54+
getUser(request: users_pb.UserRequest, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
55+
getUser(request: users_pb.UserRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
56+
getUser(request: users_pb.UserRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
57+
createUser(callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
58+
createUser(metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
59+
createUser(options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
60+
createUser(metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
61+
getUsers(request: google_protobuf_empty_pb.Empty, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<users_pb.User>;
62+
getUsers(request: google_protobuf_empty_pb.Empty, metadata?: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<users_pb.User>;
63+
}
64+
65+
export class UsersClient extends grpc.Client implements IUsersClient {
66+
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
67+
public getUser(request: users_pb.UserRequest, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
68+
public getUser(request: users_pb.UserRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
69+
public getUser(request: users_pb.UserRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: users_pb.User) => void): grpc.ClientUnaryCall;
70+
public createUser(callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
71+
public createUser(metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
72+
public createUser(options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
73+
public createUser(metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: google_protobuf_empty_pb.Empty) => void): grpc.ClientWritableStream<users_pb.User>;
74+
public getUsers(request: google_protobuf_empty_pb.Empty, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<users_pb.User>;
75+
public getUsers(request: google_protobuf_empty_pb.Empty, metadata?: grpc.Metadata, options?: Partial<grpc.CallOptions>): grpc.ClientReadableStream<users_pb.User>;
76+
}

‎proto/users_grpc_pb.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// GENERATED CODE -- DO NOT EDIT!
2+
3+
'use strict';
4+
var grpc = require('grpc');
5+
var users_pb = require('./users_pb.js');
6+
var google_protobuf_empty_pb = require('google-protobuf/google/protobuf/empty_pb.js');
7+
8+
function serialize_google_protobuf_Empty(arg) {
9+
if (!(arg instanceof google_protobuf_empty_pb.Empty)) {
10+
throw new Error('Expected argument of type google.protobuf.Empty');
11+
}
12+
return Buffer.from(arg.serializeBinary());
13+
}
14+
15+
function deserialize_google_protobuf_Empty(buffer_arg) {
16+
return google_protobuf_empty_pb.Empty.deserializeBinary(new Uint8Array(buffer_arg));
17+
}
18+
19+
function serialize_users_User(arg) {
20+
if (!(arg instanceof users_pb.User)) {
21+
throw new Error('Expected argument of type users.User');
22+
}
23+
return Buffer.from(arg.serializeBinary());
24+
}
25+
26+
function deserialize_users_User(buffer_arg) {
27+
return users_pb.User.deserializeBinary(new Uint8Array(buffer_arg));
28+
}
29+
30+
function serialize_users_UserRequest(arg) {
31+
if (!(arg instanceof users_pb.UserRequest)) {
32+
throw new Error('Expected argument of type users.UserRequest');
33+
}
34+
return Buffer.from(arg.serializeBinary());
35+
}
36+
37+
function deserialize_users_UserRequest(buffer_arg) {
38+
return users_pb.UserRequest.deserializeBinary(new Uint8Array(buffer_arg));
39+
}
40+
41+
42+
var UsersService = exports.UsersService = {
43+
getUser: {
44+
path: '/users.Users/GetUser',
45+
requestStream: false,
46+
responseStream: false,
47+
requestType: users_pb.UserRequest,
48+
responseType: users_pb.User,
49+
requestSerialize: serialize_users_UserRequest,
50+
requestDeserialize: deserialize_users_UserRequest,
51+
responseSerialize: serialize_users_User,
52+
responseDeserialize: deserialize_users_User,
53+
},
54+
createUser: {
55+
path: '/users.Users/CreateUser',
56+
requestStream: true,
57+
responseStream: false,
58+
requestType: users_pb.User,
59+
responseType: google_protobuf_empty_pb.Empty,
60+
requestSerialize: serialize_users_User,
61+
requestDeserialize: deserialize_users_User,
62+
responseSerialize: serialize_google_protobuf_Empty,
63+
responseDeserialize: deserialize_google_protobuf_Empty,
64+
},
65+
getUsers: {
66+
path: '/users.Users/GetUsers',
67+
requestStream: false,
68+
responseStream: true,
69+
requestType: google_protobuf_empty_pb.Empty,
70+
responseType: users_pb.User,
71+
requestSerialize: serialize_google_protobuf_Empty,
72+
requestDeserialize: deserialize_google_protobuf_Empty,
73+
responseSerialize: serialize_users_User,
74+
responseDeserialize: deserialize_users_User,
75+
},
76+
};
77+
78+
exports.UsersClient = grpc.makeGenericClientConstructor(UsersService);

‎proto/users_pb.d.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// package: users
2+
// file: users.proto
3+
4+
/* tslint:disable */
5+
/* eslint-disable */
6+
7+
import * as jspb from "google-protobuf";
8+
import * as google_protobuf_empty_pb from "google-protobuf/google/protobuf/empty_pb";
9+
10+
export class User extends jspb.Message {
11+
getId(): number;
12+
setId(value: number): User;
13+
14+
getName(): string;
15+
setName(value: string): User;
16+
17+
getAge(): number;
18+
setAge(value: number): User;
19+
20+
getStatus(): UserStatus;
21+
setStatus(value: UserStatus): User;
22+
23+
24+
serializeBinary(): Uint8Array;
25+
toObject(includeInstance?: boolean): User.AsObject;
26+
static toObject(includeInstance: boolean, msg: User): User.AsObject;
27+
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
28+
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
29+
static serializeBinaryToWriter(message: User, writer: jspb.BinaryWriter): void;
30+
static deserializeBinary(bytes: Uint8Array): User;
31+
static deserializeBinaryFromReader(message: User, reader: jspb.BinaryReader): User;
32+
}
33+
34+
export namespace User {
35+
export type AsObject = {
36+
id: number,
37+
name: string,
38+
age: number,
39+
status: UserStatus,
40+
}
41+
}
42+
43+
export class UserRequest extends jspb.Message {
44+
getId(): number;
45+
setId(value: number): UserRequest;
46+
47+
48+
serializeBinary(): Uint8Array;
49+
toObject(includeInstance?: boolean): UserRequest.AsObject;
50+
static toObject(includeInstance: boolean, msg: UserRequest): UserRequest.AsObject;
51+
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
52+
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
53+
static serializeBinaryToWriter(message: UserRequest, writer: jspb.BinaryWriter): void;
54+
static deserializeBinary(bytes: Uint8Array): UserRequest;
55+
static deserializeBinaryFromReader(message: UserRequest, reader: jspb.BinaryReader): UserRequest;
56+
}
57+
58+
export namespace UserRequest {
59+
export type AsObject = {
60+
id: number,
61+
}
62+
}
63+
64+
export enum UserStatus {
65+
UNKNOWN = 0,
66+
OFFLINE = 1,
67+
BUSY = 2,
68+
AVAILABLE = 3,
69+
}

0 commit comments

Comments
 (0)