Skip to content

Commit

Permalink
Initial scaffolding w/ proto build
Browse files Browse the repository at this point in the history
  • Loading branch information
mschristensen committed Feb 5, 2020
1 parent faf4ab6 commit 1552b74
Show file tree
Hide file tree
Showing 14 changed files with 2,276 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/proto
11 changes: 11 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"@typescript-eslint/camelcase": 0,
"@typescript-eslint/no-var-requires": 0
},
"extends": ["plugin:@typescript-eslint/recommended"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 4,
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120
}
24 changes: 24 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# USAGE:
# $1 <PROTO_PATH> path to the .proto file
# $2 <BUILD_PATH> path into which to build the .proto files

PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
PROTOC_GEN_GRPC_PATH="./node_modules/.bin/grpc_tools_node_protoc_plugin"
PROTO_PATH=$1
BUILD_PATH=$2

echo "Building $PROTO_PATH into $BUILD_PATH"
rm -rf $BUILD_PATH
mkdir -p $BUILD_PATH

protoc \
--plugin="protoc-gen-ts=${PROTOC_GEN_TS_PATH}" \
--plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} \
--js_out="import_style=commonjs,binary:$BUILD_PATH" \
--ts_out="service=grpc-node:$BUILD_PATH" \
--grpc_out="$BUILD_PATH" \
$PROTO_PATH

echo "Done!"
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "grpc-ts-demo",
"version": "1.0.0",
"author": "Mike Christensen",
"license": "MIT",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"eslint": "^6.8.0",
"grpc": "^1.24.2",
"grpc-tools": "^1.8.1",
"ts-protoc-gen": "^0.12.0",
"typescript": "^3.7.5"
},
"scripts": {
"lint": "yarn run eslint --fix --ext .ts src",
"prebuild": "yarn run lint",
"build": "sh build.sh ./songs.proto ./src/proto && yarn run tsc",
"start": "node ./dist/index.js"
}
}
25 changes: 25 additions & 0 deletions songs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package songs;

import "google/protobuf/empty.proto";

message Song {
int32 id = 1;
string title = 2;
string artist = 3;
}

message Comment {
string username = 1;
string body = 2;
}

message Reaction { bool like = 1; }

service Songs {
rpc GetSong(google.protobuf.Empty) returns (Song);
rpc AddSongs(stream Song) returns (google.protobuf.Empty);
rpc GetComments(Song) returns (stream Comment);
rpc LiveReactions(stream Reaction) returns (stream Reaction);
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function main(): void {
console.log('Hello, world!');
}

main();
31 changes: 31 additions & 0 deletions src/proto/songs_grpc_pb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// GENERATED CODE -- DO NOT EDIT!

// package: songs
// file: songs.proto

import * as songs_pb from "./songs_pb";
import * as google_protobuf_empty_pb from "google-protobuf/google/protobuf/empty_pb";
import * as grpc from "grpc";

interface ISongsService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
getSong: grpc.MethodDefinition<google_protobuf_empty_pb.Empty, songs_pb.Song>;
addSongs: grpc.MethodDefinition<songs_pb.Song, google_protobuf_empty_pb.Empty>;
getComments: grpc.MethodDefinition<songs_pb.Song, songs_pb.Comment>;
liveReactions: grpc.MethodDefinition<songs_pb.Reaction, songs_pb.Reaction>;
}

export const SongsService: ISongsService;

export class SongsClient extends grpc.Client {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
getSong(argument: google_protobuf_empty_pb.Empty, callback: grpc.requestCallback<songs_pb.Song>): grpc.ClientUnaryCall;
getSong(argument: google_protobuf_empty_pb.Empty, metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback<songs_pb.Song>): grpc.ClientUnaryCall;
getSong(argument: google_protobuf_empty_pb.Empty, metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback<songs_pb.Song>): grpc.ClientUnaryCall;
addSongs(callback: grpc.requestCallback<google_protobuf_empty_pb.Empty>): grpc.ClientWritableStream<songs_pb.Song>;
addSongs(metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback<google_protobuf_empty_pb.Empty>): grpc.ClientWritableStream<songs_pb.Song>;
addSongs(metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback<google_protobuf_empty_pb.Empty>): grpc.ClientWritableStream<songs_pb.Song>;
getComments(argument: songs_pb.Song, metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientReadableStream<songs_pb.Comment>;
getComments(argument: songs_pb.Song, metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientReadableStream<songs_pb.Comment>;
liveReactions(metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientDuplexStream<songs_pb.Reaction, songs_pb.Reaction>;
liveReactions(metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientDuplexStream<songs_pb.Reaction, songs_pb.Reaction>;
}
100 changes: 100 additions & 0 deletions src/proto/songs_grpc_pb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// GENERATED CODE -- DO NOT EDIT!

'use strict';
var grpc = require('grpc');
var songs_pb = require('./songs_pb.js');
var google_protobuf_empty_pb = require('google-protobuf/google/protobuf/empty_pb.js');

function serialize_google_protobuf_Empty(arg) {
if (!(arg instanceof google_protobuf_empty_pb.Empty)) {
throw new Error('Expected argument of type google.protobuf.Empty');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_google_protobuf_Empty(buffer_arg) {
return google_protobuf_empty_pb.Empty.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_songs_Comment(arg) {
if (!(arg instanceof songs_pb.Comment)) {
throw new Error('Expected argument of type songs.Comment');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_songs_Comment(buffer_arg) {
return songs_pb.Comment.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_songs_Reaction(arg) {
if (!(arg instanceof songs_pb.Reaction)) {
throw new Error('Expected argument of type songs.Reaction');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_songs_Reaction(buffer_arg) {
return songs_pb.Reaction.deserializeBinary(new Uint8Array(buffer_arg));
}

function serialize_songs_Song(arg) {
if (!(arg instanceof songs_pb.Song)) {
throw new Error('Expected argument of type songs.Song');
}
return Buffer.from(arg.serializeBinary());
}

function deserialize_songs_Song(buffer_arg) {
return songs_pb.Song.deserializeBinary(new Uint8Array(buffer_arg));
}


var SongsService = exports.SongsService = {
getSong: {
path: '/songs.Songs/GetSong',
requestStream: false,
responseStream: false,
requestType: google_protobuf_empty_pb.Empty,
responseType: songs_pb.Song,
requestSerialize: serialize_google_protobuf_Empty,
requestDeserialize: deserialize_google_protobuf_Empty,
responseSerialize: serialize_songs_Song,
responseDeserialize: deserialize_songs_Song,
},
addSongs: {
path: '/songs.Songs/AddSongs',
requestStream: true,
responseStream: false,
requestType: songs_pb.Song,
responseType: google_protobuf_empty_pb.Empty,
requestSerialize: serialize_songs_Song,
requestDeserialize: deserialize_songs_Song,
responseSerialize: serialize_google_protobuf_Empty,
responseDeserialize: deserialize_google_protobuf_Empty,
},
getComments: {
path: '/songs.Songs/GetComments',
requestStream: false,
responseStream: true,
requestType: songs_pb.Song,
responseType: songs_pb.Comment,
requestSerialize: serialize_songs_Song,
requestDeserialize: deserialize_songs_Song,
responseSerialize: serialize_songs_Comment,
responseDeserialize: deserialize_songs_Comment,
},
liveReactions: {
path: '/songs.Songs/LiveReactions',
requestStream: true,
responseStream: true,
requestType: songs_pb.Reaction,
responseType: songs_pb.Reaction,
requestSerialize: serialize_songs_Reaction,
requestDeserialize: deserialize_songs_Reaction,
responseSerialize: serialize_songs_Reaction,
responseDeserialize: deserialize_songs_Reaction,
},
};

exports.SongsClient = grpc.makeGenericClientConstructor(SongsService);
78 changes: 78 additions & 0 deletions src/proto/songs_pb.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// package: songs
// file: songs.proto

import * as jspb from "google-protobuf";
import * as google_protobuf_empty_pb from "google-protobuf/google/protobuf/empty_pb";

export class Song extends jspb.Message {
getId(): number;
setId(value: number): void;

getTitle(): string;
setTitle(value: string): void;

getArtist(): string;
setArtist(value: string): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Song.AsObject;
static toObject(includeInstance: boolean, msg: Song): Song.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: Song, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): Song;
static deserializeBinaryFromReader(message: Song, reader: jspb.BinaryReader): Song;
}

export namespace Song {
export type AsObject = {
id: number,
title: string,
artist: string,
}
}

export class Comment extends jspb.Message {
getUsername(): string;
setUsername(value: string): void;

getBody(): string;
setBody(value: string): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Comment.AsObject;
static toObject(includeInstance: boolean, msg: Comment): Comment.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: Comment, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): Comment;
static deserializeBinaryFromReader(message: Comment, reader: jspb.BinaryReader): Comment;
}

export namespace Comment {
export type AsObject = {
username: string,
body: string,
}
}

export class Reaction extends jspb.Message {
getLike(): boolean;
setLike(value: boolean): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Reaction.AsObject;
static toObject(includeInstance: boolean, msg: Reaction): Reaction.AsObject;
static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
static serializeBinaryToWriter(message: Reaction, writer: jspb.BinaryWriter): void;
static deserializeBinary(bytes: Uint8Array): Reaction;
static deserializeBinaryFromReader(message: Reaction, reader: jspb.BinaryReader): Reaction;
}

export namespace Reaction {
export type AsObject = {
like: boolean,
}
}

Loading

0 comments on commit 1552b74

Please sign in to comment.