@protobuf-ts/grpc-backend
Advanced tools
Comparing version 2.2.1 to 2.2.2-2.2.2-alpha.0.0
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapService = exports.createDefinition = exports.adaptService = void 0; | ||
exports.createContext = exports.mapService = exports.createDefinition = exports.adaptService = void 0; | ||
const runtime_rpc_1 = require("@protobuf-ts/runtime-rpc"); | ||
@@ -21,6 +21,6 @@ const grpc = require("@grpc/grpc-js"); | ||
*/ | ||
function adaptService(serviceInfo, serviceImplementation) { | ||
function adaptService(serviceInfo, serviceImplementation, options = {}) { | ||
return [ | ||
createDefinition(serviceInfo), | ||
mapService(serviceInfo, serviceImplementation) | ||
mapService(serviceInfo, serviceImplementation, options) | ||
]; | ||
@@ -53,18 +53,20 @@ } | ||
*/ | ||
function mapService(serviceInfo, service) { | ||
function mapService(si, service, options) { | ||
var _a; | ||
const grpcImp = {}; | ||
for (let mi of serviceInfo.methods) { | ||
const ctxFactory = (_a = options.contextFactory) !== null && _a !== void 0 ? _a : createContext; | ||
for (let mi of si.methods) { | ||
runtime_1.assert(typeof service[mi.localName] == "function", `implementation is missing method ${mi.localName}()`); | ||
const fn = service[mi.localName].bind(service); | ||
if (mi.serverStreaming && mi.clientStreaming) { | ||
grpcImp[mi.localName] = mapBidi(mi, fn); | ||
grpcImp[mi.localName] = mapBidi(si, mi, ctxFactory, fn); | ||
} | ||
else if (mi.serverStreaming) { | ||
grpcImp[mi.localName] = mapServerStreaming(mi, fn); | ||
grpcImp[mi.localName] = mapServerStreaming(si, mi, ctxFactory, fn); | ||
} | ||
else if (mi.clientStreaming) { | ||
grpcImp[mi.localName] = mapClientStreaming(mi, fn); | ||
grpcImp[mi.localName] = mapClientStreaming(si, mi, ctxFactory, fn); | ||
} | ||
else { | ||
grpcImp[mi.localName] = mapUnary(mi, fn); | ||
grpcImp[mi.localName] = mapUnary(si, mi, ctxFactory, fn); | ||
} | ||
@@ -75,3 +77,3 @@ } | ||
exports.mapService = mapService; | ||
function createContext(methodInfo, call) { | ||
function createContext(serviceInfo, methodInfo, call) { | ||
const deadlineGrpc = call.getDeadline(); | ||
@@ -84,5 +86,6 @@ const deadlineDate = typeof deadlineGrpc === 'number' ? new Date(deadlineGrpc) : deadlineGrpc; | ||
} | ||
function mapUnary(methodInfo, method) { | ||
exports.createContext = createContext; | ||
function mapUnary(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call, callback) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
let response; | ||
@@ -133,5 +136,5 @@ try { | ||
} | ||
function mapServerStreaming(methodInfo, method) { | ||
function mapServerStreaming(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const responseStream = { | ||
@@ -179,5 +182,5 @@ send(message) { | ||
} | ||
function mapClientStreaming(methodInfo, method) { | ||
function mapClientStreaming(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call, callback) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const requestStream = new runtime_rpc_1.RpcOutputStreamController(); | ||
@@ -239,5 +242,5 @@ call.on('data', args => { | ||
} | ||
function mapBidi(methodInfo, method) { | ||
function mapBidi(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const requestStream = new runtime_rpc_1.RpcOutputStreamController(); | ||
@@ -244,0 +247,0 @@ call.on('data', args => { |
@@ -8,1 +8,2 @@ "use strict"; | ||
Object.defineProperty(exports, "adaptService", { enumerable: true, get: function () { return grpc_adapter_1.adaptService; } }); | ||
Object.defineProperty(exports, "createContext", { enumerable: true, get: function () { return grpc_adapter_1.createContext; } }); |
@@ -17,6 +17,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
*/ | ||
export function adaptService(serviceInfo, serviceImplementation) { | ||
export function adaptService(serviceInfo, serviceImplementation, options = {}) { | ||
return [ | ||
createDefinition(serviceInfo), | ||
mapService(serviceInfo, serviceImplementation) | ||
mapService(serviceInfo, serviceImplementation, options) | ||
]; | ||
@@ -47,18 +47,20 @@ } | ||
*/ | ||
export function mapService(serviceInfo, service) { | ||
export function mapService(si, service, options) { | ||
var _a; | ||
const grpcImp = {}; | ||
for (let mi of serviceInfo.methods) { | ||
const ctxFactory = (_a = options.contextFactory) !== null && _a !== void 0 ? _a : createContext; | ||
for (let mi of si.methods) { | ||
assert(typeof service[mi.localName] == "function", `implementation is missing method ${mi.localName}()`); | ||
const fn = service[mi.localName].bind(service); | ||
if (mi.serverStreaming && mi.clientStreaming) { | ||
grpcImp[mi.localName] = mapBidi(mi, fn); | ||
grpcImp[mi.localName] = mapBidi(si, mi, ctxFactory, fn); | ||
} | ||
else if (mi.serverStreaming) { | ||
grpcImp[mi.localName] = mapServerStreaming(mi, fn); | ||
grpcImp[mi.localName] = mapServerStreaming(si, mi, ctxFactory, fn); | ||
} | ||
else if (mi.clientStreaming) { | ||
grpcImp[mi.localName] = mapClientStreaming(mi, fn); | ||
grpcImp[mi.localName] = mapClientStreaming(si, mi, ctxFactory, fn); | ||
} | ||
else { | ||
grpcImp[mi.localName] = mapUnary(mi, fn); | ||
grpcImp[mi.localName] = mapUnary(si, mi, ctxFactory, fn); | ||
} | ||
@@ -68,3 +70,3 @@ } | ||
} | ||
function createContext(methodInfo, call) { | ||
export function createContext(serviceInfo, methodInfo, call) { | ||
const deadlineGrpc = call.getDeadline(); | ||
@@ -77,5 +79,5 @@ const deadlineDate = typeof deadlineGrpc === 'number' ? new Date(deadlineGrpc) : deadlineGrpc; | ||
} | ||
function mapUnary(methodInfo, method) { | ||
function mapUnary(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call, callback) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
let response; | ||
@@ -126,5 +128,5 @@ try { | ||
} | ||
function mapServerStreaming(methodInfo, method) { | ||
function mapServerStreaming(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const responseStream = { | ||
@@ -172,5 +174,5 @@ send(message) { | ||
} | ||
function mapClientStreaming(methodInfo, method) { | ||
function mapClientStreaming(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call, callback) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const requestStream = new RpcOutputStreamController(); | ||
@@ -232,5 +234,5 @@ call.on('data', args => { | ||
} | ||
function mapBidi(methodInfo, method) { | ||
function mapBidi(serviceInfo, methodInfo, ctxFactory, method) { | ||
return (call) => __awaiter(this, void 0, void 0, function* () { | ||
const context = createContext(methodInfo, call); | ||
const context = ctxFactory(serviceInfo, methodInfo, call); | ||
const requestStream = new RpcOutputStreamController(); | ||
@@ -237,0 +239,0 @@ call.on('data', args => { |
// Public API. | ||
// Note: we do not use `export * from ...` to help tree shakers, | ||
// webpack verbose output hints that this should be useful | ||
export { adaptService } from "./grpc-adapter"; | ||
export { adaptService, createContext } from "./grpc-adapter"; |
@@ -1,7 +0,11 @@ | ||
import { ServiceInfo } from "@protobuf-ts/runtime-rpc"; | ||
import { MethodInfo, ServerCallContext, ServiceInfo } from "@protobuf-ts/runtime-rpc"; | ||
import * as grpc from "@grpc/grpc-js"; | ||
declare type ContextFactory = (serviceInfo: ServiceInfo, methodInfo: MethodInfo, call: grpc.ServerUnaryCall<any, any> | grpc.ServerReadableStream<any, any> | grpc.ServerWritableStream<any, any>) => ServerCallContext; | ||
export interface AdaptServiceOptions { | ||
contextFactory?: ContextFactory; | ||
} | ||
/** | ||
* Create a grpc service definition and an implementation for @grpc/grpc-js. | ||
*/ | ||
export declare function adaptService(serviceInfo: ServiceInfo, serviceImplementation: any): [grpc.ServiceDefinition, grpc.UntypedServiceImplementation]; | ||
export declare function adaptService(serviceInfo: ServiceInfo, serviceImplementation: any, options?: AdaptServiceOptions): [grpc.ServiceDefinition, grpc.UntypedServiceImplementation]; | ||
/** | ||
@@ -15,2 +19,4 @@ * Create a service definition for @grpc/grpc-js from service info. | ||
*/ | ||
export declare function mapService(serviceInfo: ServiceInfo, service: any): grpc.UntypedServiceImplementation; | ||
export declare function mapService(si: ServiceInfo, service: any, options: AdaptServiceOptions): grpc.UntypedServiceImplementation; | ||
export declare function createContext(serviceInfo: ServiceInfo, methodInfo: MethodInfo, call: grpc.ServerUnaryCall<any, any> | grpc.ServerReadableStream<any, any> | grpc.ServerWritableStream<any, any>): ServerCallContext; | ||
export {}; |
@@ -1,1 +0,1 @@ | ||
export { adaptService } from "./grpc-adapter"; | ||
export { adaptService, createContext } from "./grpc-adapter"; |
{ | ||
"name": "@protobuf-ts/grpc-backend", | ||
"version": "2.2.1", | ||
"version": "2.2.2-2.2.2-alpha.0.0", | ||
"description": "gRPC backend for servers generated by the protoc plugin \"protobuf-ts\"", | ||
@@ -27,4 +27,4 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@protobuf-ts/runtime": "^2.2.1", | ||
"@protobuf-ts/runtime-rpc": "^2.2.1" | ||
"@protobuf-ts/runtime": "^2.2.2-2.2.2-alpha.0.0", | ||
"@protobuf-ts/runtime-rpc": "^2.2.2-2.2.2-alpha.0.0" | ||
}, | ||
@@ -36,4 +36,4 @@ "peerDependencies": { | ||
"@grpc/grpc-js": "^1.2.2", | ||
"@protobuf-ts/plugin": "^2.2.1", | ||
"@protobuf-ts/protoc": "^2.2.1", | ||
"@protobuf-ts/plugin": "^2.2.2-2.2.2-alpha.0.0", | ||
"@protobuf-ts/protoc": "^2.2.2-2.2.2-alpha.0.0", | ||
"@types/jasmine": "^3.5.10", | ||
@@ -46,3 +46,3 @@ "jasmine": "^3.5.0", | ||
}, | ||
"gitHead": "8cefc7aca9493b3aeb737b37cea2c30c384b157e" | ||
"gitHead": "76e3066858257dc45dc0495fae3b8ab81a113aeb" | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
43139
794
1