Comparing version 1.7.2 to 1.7.3
@@ -60,5 +60,7 @@ "use strict"; | ||
var grpc = require("grpc"); | ||
var protobuf = require("protobufjs"); | ||
var _a = require("grpc/src/protobuf_js_6_common"), deserializeCls = _a.deserializeCls, serializeCls = _a.serializeCls; | ||
var defaultGrpcOptions = require("grpc/src/common").defaultGrpcOptions; | ||
function lcFirst(name) { | ||
return name[0].toLowerCase() + name.substr(1); | ||
} | ||
var Server = /** @class */ (function () { | ||
@@ -82,5 +84,5 @@ // address - grpc://0.0.0.0:12345 | ||
for (var name_1 in meta) { | ||
var lcName = protobuf.util.lcFirst(name_1); | ||
var m = meta[name_1]; | ||
service[name_1] = { | ||
var lcName = lcFirst(name_1); | ||
service[lcName] = { | ||
path: m.path, | ||
@@ -95,20 +97,16 @@ originalName: name_1, | ||
}; | ||
var impl = factory[lcName] || factory[name_1]; | ||
if (!impl) { | ||
continue; | ||
} | ||
var func = impl.bind(factory); | ||
if (!m.requestStream && !m.responseStream) { | ||
implementation[name_1] = mapUnaryUnary(func); | ||
implementation[lcName] = mapUnaryUnary(factory, lcName); | ||
} | ||
else if (!m.requestStream && m.responseStream) { | ||
implementation[name_1] = mapUnaryStream(func); | ||
implementation[lcName] = mapUnaryStream(factory, lcName); | ||
} | ||
else if (m.requestStream && !m.responseStream) { | ||
implementation[name_1] = mapStreamUnary(func); | ||
implementation[lcName] = mapStreamUnary(factory, lcName); | ||
} | ||
else if (m.requestStream && m.responseStream) { | ||
implementation[name_1] = mapStreamStream(func); | ||
implementation[lcName] = mapStreamStream(factory, lcName); | ||
} | ||
} | ||
console.log("service, implementation:", service, implementation); | ||
this.server.addService(service, implementation); | ||
@@ -119,20 +117,43 @@ }; | ||
exports.Server = Server; | ||
function mapUnaryUnary(func) { | ||
function makeServiceMethod(call, factory, method) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var service, handler; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, factory(call)]; | ||
case 1: | ||
service = _a.sent(); | ||
if (!service) { | ||
throw new Error("create service object fail"); | ||
} | ||
handler = factory[method]; | ||
if (!handler) { | ||
throw new Error("service method not implemeted: " + method); | ||
} | ||
return [2 /*return*/, handler.bind(service)]; | ||
} | ||
}); | ||
}); | ||
} | ||
function mapUnaryUnary(factory, method) { | ||
return function (call, callback) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var res, err_1; | ||
var handler, res, err_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, func(call.request)]; | ||
_a.trys.push([0, 3, , 4]); | ||
return [4 /*yield*/, makeServiceMethod(call, factory, method)]; | ||
case 1: | ||
handler = _a.sent(); | ||
return [4 /*yield*/, handler(call.request)]; | ||
case 2: | ||
res = _a.sent(); | ||
callback(null, res); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
err_1 = _a.sent(); | ||
callback(err_1, null); | ||
return [3 /*break*/, 3]; | ||
case 3: return [2 /*return*/]; | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
@@ -143,50 +164,53 @@ }); | ||
} | ||
function mapUnaryStream(func) { | ||
function mapUnaryStream(factory, method) { | ||
return function (call) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var e_1, _a, resList, resList_1, resList_1_1, res, e_1_1, err_2; | ||
var e_1, _a, handler, resList, resList_1, resList_1_1, res, e_1_1, err_2; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
_b.trys.push([0, 14, 15, 16]); | ||
return [4 /*yield*/, func(call.request)]; | ||
_b.trys.push([0, 15, 16, 17]); | ||
return [4 /*yield*/, makeServiceMethod(call, factory, method)]; | ||
case 1: | ||
handler = _b.sent(); | ||
return [4 /*yield*/, handler(call.request)]; | ||
case 2: | ||
resList = _b.sent(); | ||
_b.label = 2; | ||
case 2: | ||
_b.trys.push([2, 7, 8, 13]); | ||
_b.label = 3; | ||
case 3: | ||
_b.trys.push([3, 8, 9, 14]); | ||
resList_1 = __asyncValues(resList); | ||
_b.label = 3; | ||
case 3: return [4 /*yield*/, resList_1.next()]; | ||
case 4: | ||
if (!(resList_1_1 = _b.sent(), !resList_1_1.done)) return [3 /*break*/, 6]; | ||
_b.label = 4; | ||
case 4: return [4 /*yield*/, resList_1.next()]; | ||
case 5: | ||
if (!(resList_1_1 = _b.sent(), !resList_1_1.done)) return [3 /*break*/, 7]; | ||
res = resList_1_1.value; | ||
call.write(res); | ||
_b.label = 5; | ||
case 5: return [3 /*break*/, 3]; | ||
case 6: return [3 /*break*/, 13]; | ||
case 7: | ||
_b.label = 6; | ||
case 6: return [3 /*break*/, 4]; | ||
case 7: return [3 /*break*/, 14]; | ||
case 8: | ||
e_1_1 = _b.sent(); | ||
e_1 = { error: e_1_1 }; | ||
return [3 /*break*/, 13]; | ||
case 8: | ||
_b.trys.push([8, , 11, 12]); | ||
if (!(resList_1_1 && !resList_1_1.done && (_a = resList_1.return))) return [3 /*break*/, 10]; | ||
return [3 /*break*/, 14]; | ||
case 9: | ||
_b.trys.push([9, , 12, 13]); | ||
if (!(resList_1_1 && !resList_1_1.done && (_a = resList_1.return))) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, _a.call(resList_1)]; | ||
case 9: | ||
case 10: | ||
_b.sent(); | ||
_b.label = 10; | ||
case 10: return [3 /*break*/, 12]; | ||
case 11: | ||
_b.label = 11; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: | ||
if (e_1) throw e_1.error; | ||
return [7 /*endfinally*/]; | ||
case 12: return [7 /*endfinally*/]; | ||
case 13: return [3 /*break*/, 16]; | ||
case 14: | ||
case 13: return [7 /*endfinally*/]; | ||
case 14: return [3 /*break*/, 17]; | ||
case 15: | ||
err_2 = _b.sent(); | ||
return [3 /*break*/, 16]; | ||
case 15: | ||
return [3 /*break*/, 17]; | ||
case 16: | ||
call.end(); | ||
return [7 /*endfinally*/]; | ||
case 16: return [2 /*return*/]; | ||
case 17: return [2 /*return*/]; | ||
} | ||
@@ -197,6 +221,6 @@ }); | ||
} | ||
function mapStreamUnary(func) { | ||
function mapStreamUnary(factory, method) { | ||
return function (call, callback) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var request, res, err_3; | ||
var request, handler, res, err_3; | ||
return __generator(this, function (_a) { | ||
@@ -208,13 +232,16 @@ switch (_a.label) { | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, func(request)]; | ||
_a.trys.push([1, 4, , 5]); | ||
return [4 /*yield*/, makeServiceMethod(call, factory, method)]; | ||
case 2: | ||
handler = _a.sent(); | ||
return [4 /*yield*/, handler(request)]; | ||
case 3: | ||
res = _a.sent(); | ||
callback(null, res); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
err_3 = _a.sent(); | ||
callback(err_3, null); | ||
return [3 /*break*/, 4]; | ||
case 4: return [2 /*return*/]; | ||
return [3 /*break*/, 5]; | ||
case 5: return [2 /*return*/]; | ||
} | ||
@@ -225,6 +252,6 @@ }); | ||
} | ||
function mapStreamStream(func) { | ||
function mapStreamStream(factory, method) { | ||
return function (call, callback) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var e_2, _a, request, resList, resList_2, resList_2_1, res, e_2_1, err_4; | ||
var e_2, _a, request, handler, resList, resList_2, resList_2_1, res, e_2_1, err_4; | ||
return __generator(this, function (_b) { | ||
@@ -236,43 +263,46 @@ switch (_b.label) { | ||
case 1: | ||
_b.trys.push([1, 15, 16, 17]); | ||
return [4 /*yield*/, func(request)]; | ||
_b.trys.push([1, 16, 17, 18]); | ||
return [4 /*yield*/, makeServiceMethod(call, factory, method)]; | ||
case 2: | ||
handler = _b.sent(); | ||
return [4 /*yield*/, handler(request)]; | ||
case 3: | ||
resList = _b.sent(); | ||
_b.label = 3; | ||
case 3: | ||
_b.trys.push([3, 8, 9, 14]); | ||
_b.label = 4; | ||
case 4: | ||
_b.trys.push([4, 9, 10, 15]); | ||
resList_2 = __asyncValues(resList); | ||
_b.label = 4; | ||
case 4: return [4 /*yield*/, resList_2.next()]; | ||
case 5: | ||
if (!(resList_2_1 = _b.sent(), !resList_2_1.done)) return [3 /*break*/, 7]; | ||
_b.label = 5; | ||
case 5: return [4 /*yield*/, resList_2.next()]; | ||
case 6: | ||
if (!(resList_2_1 = _b.sent(), !resList_2_1.done)) return [3 /*break*/, 8]; | ||
res = resList_2_1.value; | ||
call.write(res); | ||
_b.label = 6; | ||
case 6: return [3 /*break*/, 4]; | ||
case 7: return [3 /*break*/, 14]; | ||
case 8: | ||
_b.label = 7; | ||
case 7: return [3 /*break*/, 5]; | ||
case 8: return [3 /*break*/, 15]; | ||
case 9: | ||
e_2_1 = _b.sent(); | ||
e_2 = { error: e_2_1 }; | ||
return [3 /*break*/, 14]; | ||
case 9: | ||
_b.trys.push([9, , 12, 13]); | ||
if (!(resList_2_1 && !resList_2_1.done && (_a = resList_2.return))) return [3 /*break*/, 11]; | ||
return [3 /*break*/, 15]; | ||
case 10: | ||
_b.trys.push([10, , 13, 14]); | ||
if (!(resList_2_1 && !resList_2_1.done && (_a = resList_2.return))) return [3 /*break*/, 12]; | ||
return [4 /*yield*/, _a.call(resList_2)]; | ||
case 10: | ||
case 11: | ||
_b.sent(); | ||
_b.label = 11; | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: | ||
_b.label = 12; | ||
case 12: return [3 /*break*/, 14]; | ||
case 13: | ||
if (e_2) throw e_2.error; | ||
return [7 /*endfinally*/]; | ||
case 13: return [7 /*endfinally*/]; | ||
case 14: return [3 /*break*/, 17]; | ||
case 15: | ||
case 14: return [7 /*endfinally*/]; | ||
case 15: return [3 /*break*/, 18]; | ||
case 16: | ||
err_4 = _b.sent(); | ||
return [3 /*break*/, 17]; | ||
case 16: | ||
return [3 /*break*/, 18]; | ||
case 17: | ||
call.end(); | ||
return [7 /*endfinally*/]; | ||
case 17: return [2 /*return*/]; | ||
case 18: return [2 /*return*/]; | ||
} | ||
@@ -279,0 +309,0 @@ }); |
{ | ||
"name": "wsgrpc", | ||
"version": "1.7.2", | ||
"version": "1.7.3", | ||
"description": "Using WebSocket as gRPC transport", | ||
@@ -5,0 +5,0 @@ "author": "tsangpo", |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
302685
7445
9
83