@dxos/codec-protobuf
Advanced tools
Comparing version 2.12.7 to 2.12.8
@@ -7,2 +7,3 @@ export * from './common'; | ||
export * from './buffer-patch'; | ||
export * from './stream'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -22,2 +22,3 @@ "use strict"; | ||
__exportStar(require("./buffer-patch"), exports); | ||
__exportStar(require("./stream"), exports); | ||
//# sourceMappingURL=index.js.map |
import pb from 'protobufjs'; | ||
import type { Schema } from './schema'; | ||
import { Stream } from './stream'; | ||
export interface ServiceBackend { | ||
call(method: string, request: Uint8Array): Promise<Uint8Array>; | ||
callStream(method: string, request: Uint8Array): Stream<Uint8Array>; | ||
} | ||
@@ -22,3 +24,5 @@ export declare class ServiceDescriptor<S> { | ||
call(methodName: string, request: Uint8Array): Promise<Uint8Array>; | ||
callStream(methodName: string, request: Uint8Array): Stream<Uint8Array>; | ||
private _getMethodInfo; | ||
} | ||
//# sourceMappingURL=service.d.ts.map |
@@ -11,2 +11,3 @@ "use strict"; | ||
const assert_1 = __importDefault(require("assert")); | ||
const stream_1 = require("./stream"); | ||
class ServiceDescriptor { | ||
@@ -32,11 +33,22 @@ constructor(_service, _schema) { | ||
assert_1.default(!method.requestStream, 'Streaming RPC requests are not supported.'); | ||
assert_1.default(!method.responseStream, 'Streaming RPC responses are not supported.'); | ||
// TODO(marik-d): What about primitive types. | ||
const requestCodec = schema.tryGetCodecForType(method.resolvedRequestType.fullName); | ||
const responseCodec = schema.tryGetCodecForType(method.resolvedResponseType.fullName); | ||
this[method.name] = async (request) => { | ||
const encoded = requestCodec.encode(request); | ||
const response = await backend.call(method.name, encoded); | ||
return responseCodec.decode(response); | ||
}; | ||
if (!method.responseStream) { | ||
this[method.name] = async (request) => { | ||
const encoded = requestCodec.encode(request); | ||
const response = await backend.call(method.name, encoded); | ||
return responseCodec.decode(response); | ||
}; | ||
} | ||
else { | ||
this[method.name] = (request) => { | ||
const encoded = requestCodec.encode(request); | ||
return new stream_1.Stream(({ next, close }) => { | ||
const stream = backend.callStream(method.name, encoded); | ||
stream.subscribe(data => next(responseCodec.decode(data)), close); | ||
return () => stream.close(); | ||
}); | ||
}; | ||
} | ||
} | ||
@@ -53,2 +65,26 @@ } | ||
async call(methodName, request) { | ||
const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName); | ||
assert_1.default(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.'); | ||
assert_1.default(!method.responseStream, 'Invalid RPC method call: response streaming mismatch.'); | ||
const requestDecoded = requestCodec.decode(request); | ||
const handler = this._handlers[methodName]; | ||
assert_1.default(handler, `Handler is missing: ${methodName}`); | ||
const response = await handler(requestDecoded); | ||
const responseEncoded = responseCodec.encode(response); | ||
return responseEncoded; | ||
} | ||
callStream(methodName, request) { | ||
const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName); | ||
assert_1.default(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.'); | ||
assert_1.default(method.responseStream, 'Invalid RPC method call: response streaming mismatch.'); | ||
const requestDecoded = requestCodec.decode(request); | ||
const handler = this._handlers[methodName]; | ||
assert_1.default(handler, `Handler is missing: ${methodName}`); | ||
const responseStream = handler(requestDecoded); | ||
return new stream_1.Stream(({ next, close }) => { | ||
responseStream.subscribe(data => next(responseCodec.encode(data)), close); | ||
return () => responseStream.close(); | ||
}); | ||
} | ||
_getMethodInfo(methodName) { | ||
const method = this._service.methods[methodName]; | ||
@@ -61,8 +97,3 @@ assert_1.default(!!method, `Method not found: ${methodName}`); | ||
const responseCodec = this._schema.tryGetCodecForType(method.resolvedResponseType.fullName); | ||
const requestDecoded = requestCodec.decode(request); | ||
const handler = this._handlers[methodName]; | ||
assert_1.default(handler, `Handler is missing: ${methodName}`); | ||
const response = await handler(requestDecoded); | ||
const responseEncoded = responseCodec.encode(response); | ||
return responseEncoded; | ||
return { method, requestCodec, responseCodec }; | ||
} | ||
@@ -69,0 +100,0 @@ } |
{ | ||
"name": "@dxos/codec-protobuf", | ||
"version": "2.12.7", | ||
"version": "2.12.8", | ||
"license": "MIT", | ||
@@ -13,4 +13,4 @@ "main": "dist/src/index.js", | ||
"build": "tsc", | ||
"build:test": "pnpm run build && pnpm run test", | ||
"test": "pnpm run lint && jest 2>&1", | ||
"build:test": "pnpm run build && pnpm run lint && pnpm run test", | ||
"test": "jest 2>&1", | ||
"lint": "eslint '{src,test}/**/*.ts'" | ||
@@ -35,3 +35,3 @@ }, | ||
"devDependencies": { | ||
"@dxos/eslint-plugin": "~1.0.11", | ||
"@dxos/eslint-plugin": "~1.0.14", | ||
"@types/assert": "^1.5.4", | ||
@@ -38,0 +38,0 @@ "@types/jest": "^26.0.7", |
@@ -11,1 +11,2 @@ // | ||
export * from './buffer-patch'; | ||
export * from './stream'; |
@@ -9,5 +9,7 @@ // | ||
import type { Schema } from './schema'; | ||
import { Stream } from './stream'; | ||
export interface ServiceBackend { | ||
call (method: string, request: Uint8Array): Promise<Uint8Array>; | ||
callStream (method: string, request: Uint8Array): Stream<Uint8Array>; | ||
} | ||
@@ -41,13 +43,24 @@ | ||
assert(!method.requestStream, 'Streaming RPC requests are not supported.'); | ||
assert(!method.responseStream, 'Streaming RPC responses are not supported.'); | ||
// TODO(marik-d): What about primitive types. | ||
const requestCodec = schema.tryGetCodecForType(method.resolvedRequestType.fullName); | ||
const responseCodec = schema.tryGetCodecForType(method.resolvedResponseType.fullName) | ||
const responseCodec = schema.tryGetCodecForType(method.resolvedResponseType.fullName); | ||
; (this as any)[method.name] = async (request: unknown) => { | ||
const encoded = requestCodec.encode(request); | ||
const response = await backend.call(method.name, encoded); | ||
return responseCodec.decode(response); | ||
}; | ||
if (!method.responseStream) { | ||
(this as any)[method.name] = async (request: unknown) => { | ||
const encoded = requestCodec.encode(request); | ||
const response = await backend.call(method.name, encoded); | ||
return responseCodec.decode(response); | ||
}; | ||
} else { | ||
(this as any)[method.name] = (request: unknown) => { | ||
const encoded = requestCodec.encode(request); | ||
return new Stream(({ next, close }) => { | ||
const stream = backend.callStream(method.name, encoded); | ||
stream.subscribe(data => next(responseCodec.decode(data)), close); | ||
return () => stream.close(); | ||
}); | ||
}; | ||
} | ||
} | ||
@@ -65,12 +78,6 @@ } | ||
async call (methodName: string, request: Uint8Array): Promise<Uint8Array> { | ||
const method = this._service.methods[methodName]; | ||
assert(!!method, `Method not found: ${methodName}`); | ||
const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName); | ||
assert(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.'); | ||
assert(!method.responseStream, 'Invalid RPC method call: response streaming mismatch.'); | ||
method.resolve(); | ||
assert(method.resolvedRequestType); | ||
assert(method.resolvedResponseType); | ||
const requestCodec = this._schema.tryGetCodecForType(method.resolvedRequestType.fullName); | ||
const responseCodec = this._schema.tryGetCodecForType(method.resolvedResponseType.fullName); | ||
const requestDecoded = requestCodec.decode(request); | ||
@@ -87,2 +94,33 @@ | ||
} | ||
callStream (methodName: string, request: Uint8Array): Stream<Uint8Array> { | ||
const { method, requestCodec, responseCodec } = this._getMethodInfo(methodName); | ||
assert(!method.requestStream, 'Invalid RPC method call: request streaming mismatch.'); | ||
assert(method.responseStream, 'Invalid RPC method call: response streaming mismatch.'); | ||
const requestDecoded = requestCodec.decode(request); | ||
const handler = this._handlers[methodName as keyof S]; | ||
assert(handler, `Handler is missing: ${methodName}`); | ||
const responseStream = (handler as any)(requestDecoded) as Stream<unknown>; | ||
return new Stream<Uint8Array>(({ next, close }) => { | ||
responseStream.subscribe(data => next(responseCodec.encode(data)), close); | ||
return () => responseStream.close(); | ||
}); | ||
} | ||
private _getMethodInfo (methodName: string) { | ||
const method = this._service.methods[methodName]; | ||
assert(!!method, `Method not found: ${methodName}`); | ||
method.resolve(); | ||
assert(method.resolvedRequestType); | ||
assert(method.resolvedResponseType); | ||
const requestCodec = this._schema.tryGetCodecForType(method.resolvedRequestType.fullName); | ||
const responseCodec = this._schema.tryGetCodecForType(method.resolvedResponseType.fullName); | ||
return { method, requestCodec, responseCodec }; | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
91022
57
1157