@pbkit/runtime
Advanced tools
Comparing version 0.0.24 to 0.0.25
export declare function fromSingle<T>(value: T): AsyncGenerator<T>; | ||
export declare function first<T>(generator: AsyncGenerator<T>): Promise<T>; | ||
export declare function map<T>(asyncGenerator: AsyncGenerator<T>, fn: (value: T) => T | Promise<T>): AsyncGenerator<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.first = exports.fromSingle = void 0; | ||
exports.map = exports.first = exports.fromSingle = void 0; | ||
async function* fromSingle(value) { | ||
@@ -14,1 +14,6 @@ yield value; | ||
exports.first = first; | ||
async function* map(asyncGenerator, fn) { | ||
for await (const value of asyncGenerator) | ||
yield await fn(value); | ||
} | ||
exports.map = map; |
@@ -1,19 +0,41 @@ | ||
import type { MethodDescriptor } from "./rpc"; | ||
export interface DevtoolsConfig { | ||
services: Set<Service>; | ||
subscribe: () => AsyncGenerator<Request>; | ||
emit: (request: Request) => void; | ||
import type { RpcClientImpl } from "./rpc"; | ||
import { EventEmitter } from "./async/event-emitter"; | ||
export declare const devtoolsKey = "@pbkit/devtools"; | ||
export declare function getDevtoolsConfig(): DevtoolsConfig; | ||
export interface DevtoolsConfig extends EventEmitter<Events> { | ||
requestIdCounter: number; | ||
} | ||
export interface Service { | ||
export interface WrapRpcClientImplConfig { | ||
rpcClientImpl: RpcClientImpl; | ||
devtoolsConfig: DevtoolsConfig; | ||
tags: string[]; | ||
methodDescriptors: MethodDescriptors; | ||
} | ||
export interface Request { | ||
service: Service; | ||
methodName: string; | ||
export declare function wrapRpcClientImpl<TMetadata, THeader, TTrailer>(config: WrapRpcClientImplConfig): RpcClientImpl<TMetadata, THeader, TTrailer>; | ||
interface Events { | ||
"request": { | ||
requestId: number; | ||
servicePath: string; | ||
rpcName: string; | ||
metadataJson: string; | ||
tags: string[]; | ||
}; | ||
"request-payload": { | ||
requestId: number; | ||
payloadJson: string; | ||
payloadProto: Uint8Array; | ||
}; | ||
"response": { | ||
requestId: number; | ||
headerJson: string; | ||
}; | ||
"response-payload": { | ||
requestId: number; | ||
payloadJson: string; | ||
payloadProto: Uint8Array; | ||
}; | ||
"response-trailer": { | ||
requestId: number; | ||
trailerJson: string; | ||
}; | ||
} | ||
export interface MethodDescriptors { | ||
[methodName: string]: MethodDescriptor<any, any>; | ||
} | ||
export declare const devtoolsKey = "@pbkit/devtools"; | ||
export declare function getDevtoolsConfig(): DevtoolsConfig; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDevtoolsConfig = exports.devtoolsKey = void 0; | ||
exports.wrapRpcClientImpl = exports.getDevtoolsConfig = exports.devtoolsKey = void 0; | ||
const async_generator_1 = require("./async/async-generator"); | ||
const event_emitter_1 = require("./async/event-emitter"); | ||
exports.devtoolsKey = "@pbkit/devtools"; | ||
function getDevtoolsConfig() { | ||
const global = globalThis; | ||
return global[exports.devtoolsKey] = global[exports.devtoolsKey] || createDevtoolsConfig(); | ||
var _a; | ||
return (_a = globalThis)[exports.devtoolsKey] || (_a[exports.devtoolsKey] = createDevtoolsConfig()); | ||
} | ||
exports.getDevtoolsConfig = getDevtoolsConfig; | ||
function createDevtoolsConfig() { | ||
const listeners = new Set(); | ||
const services = new Set(); | ||
function subscribe() { | ||
const asyncIterator = { | ||
[Symbol.asyncIterator]: () => asyncIterator, | ||
next() { | ||
return new Promise((resolve) => { | ||
const listener = (value) => { | ||
listeners.delete(listener); | ||
resolve({ done: false, value }); | ||
}; | ||
listeners.add(listener); | ||
const devtoolsConfig = { | ||
requestIdCounter: 0, | ||
...(0, event_emitter_1.createEventEmitter)(), | ||
}; | ||
return devtoolsConfig; | ||
} | ||
function wrapRpcClientImpl(config) { | ||
return function devtoolsRpcClientImpl(methodDescriptor) { | ||
const { rpcClientImpl, devtoolsConfig, tags } = config; | ||
const rpcMethodImpl = rpcClientImpl(methodDescriptor); | ||
return function devtoolsRpcMethodImpl(req, metadata) { | ||
const requestId = devtoolsConfig.requestIdCounter++; | ||
devtoolsConfig.emit("request", { | ||
requestId, | ||
servicePath: methodDescriptor.service.serviceName, | ||
rpcName: methodDescriptor.methodName, | ||
metadataJson: toJson(metadata), | ||
tags, | ||
}); | ||
const rpcMethodResult = rpcMethodImpl((0, async_generator_1.map)(req, (payload) => { | ||
devtoolsConfig.emit("request-payload", { | ||
requestId, | ||
payloadJson: toJson(payload), | ||
payloadProto: methodDescriptor.requestType.serializeBinary(payload), | ||
}); | ||
}, | ||
return(value) { | ||
return Promise.resolve({ done: true, value }); | ||
}, | ||
throw(error) { | ||
return Promise.reject(error); | ||
}, | ||
return payload; | ||
}), metadata); | ||
const resAsyncGenerator = (0, async_generator_1.map)(rpcMethodResult[0], (payload) => { | ||
devtoolsConfig.emit("response-payload", { | ||
requestId, | ||
payloadJson: toJson(payload), | ||
payloadProto: methodDescriptor.responseType.serializeBinary(payload), | ||
}); | ||
return payload; | ||
}); | ||
const headerPromise = rpcMethodResult[1].then((header) => { | ||
devtoolsConfig.emit("response", { | ||
requestId, | ||
headerJson: toJson(header), | ||
}); | ||
return header; | ||
}); | ||
const trailerPromise = rpcMethodResult[2].then((trailer) => { | ||
devtoolsConfig.emit("response-trailer", { | ||
requestId, | ||
trailerJson: toJson(trailer), | ||
}); | ||
return trailer; | ||
}); | ||
return [resAsyncGenerator, headerPromise, trailerPromise]; | ||
}; | ||
return asyncIterator; | ||
} | ||
function emit(request) { | ||
for (const listener of listeners) | ||
listener(request); | ||
} | ||
return { services, subscribe, emit }; | ||
}; | ||
} | ||
exports.wrapRpcClientImpl = wrapRpcClientImpl; | ||
function toJson(value) { | ||
if ((!value) || (typeof value !== "object")) | ||
return "{}"; | ||
return JSON.stringify(value); | ||
} |
{ | ||
"name": "@pbkit/runtime", | ||
"version": "0.0.24", | ||
"version": "0.0.25", | ||
"author": "JongChan Choi <jong@chan.moe>", | ||
@@ -5,0 +5,0 @@ "license": "(MIT OR Apache-2.0)", |
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
44110
33
1197