Skip to content
Open
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: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@grpc/proto-loader": "^0.3.0",
"grpc": "^1.15.1"
"@grpc/grpc-js": "^1.11.1",
"@grpc/proto-loader": "^0.7.13"
},
"devDependencies": {
"@types/node": "^10.11.3"
Expand Down
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import * as grpc from "grpc";
import * as http from "http";
import {GrpcObject, ServiceClientConstructor, ProtobufTypeDefinition, loadPackageDefinition, credentials, ServiceError} from "@grpc/grpc-js";
import { loadSync } from "@grpc/proto-loader";

const GRPC_SERVER = process.env.DWARF_GRPC_SERVER;
const PROTO_PATH = __dirname + "./../dwarf.proto";
let dwarfProto: grpc.GrpcObject;
let dwarfProto: GrpcObject | ServiceClientConstructor | ProtobufTypeDefinition;

if (!GRPC_SERVER) {
console.warn("You must set a GRPC server. Dwarf will not work.");
Expand All @@ -13,26 +12,25 @@ if (!GRPC_SERVER) {
export interface ServerResponse {
urls: string[];
}

export function grpcObj(): grpc.GrpcObject {
function grpcObj(): GrpcObject | ServiceClientConstructor | ProtobufTypeDefinition {
if (dwarfProto) {
return dwarfProto;
}

dwarfProto = grpc.loadPackageDefinition(loadSync(PROTO_PATH)).pb;
dwarfProto = loadPackageDefinition(loadSync(PROTO_PATH)).pb;

return dwarfProto;
}

export function shorten(urls: string[]) {
return new Promise((resolve, reject) => {
const Proto = grpcObj();
const client = new Proto.Dwarf(
const client = new (Proto as any).Dwarf(
GRPC_SERVER,
grpc.credentials.createInsecure()
credentials.createInsecure()
);

client.Create({ urls }, (err: Error, res: ServerResponse) => {
client.Create({ urls }, (err: ServiceError | null, res: ServerResponse) => {
if (err) {
reject(err);
} else {
Expand Down