@push-rpc/core
Advanced tools
Comparing version 1.0.18 to 1.0.19
@@ -1,2 +0,2 @@ | ||
import { RpcConnectionContext } from "./rpc"; | ||
import { Middleware, RpcConnectionContext } from "./rpc"; | ||
import { Socket } from "./transport"; | ||
@@ -23,3 +23,4 @@ export interface RpcClientListeners { | ||
createContext(): RpcConnectionContext; | ||
localMiddleware: (ctx: any, next: any) => Promise<any>; | ||
localMiddleware: Middleware; | ||
remoteMiddleware: Middleware; | ||
messageParser(data: any): any[]; | ||
@@ -26,0 +27,0 @@ keepAlivePeriod: number; |
@@ -29,3 +29,4 @@ "use strict"; | ||
createContext: function () { return ({ remoteId: null }); }, | ||
localMiddleware: function (ctx, next) { return next(); }, | ||
localMiddleware: function (ctx, next, params) { return next(params); }, | ||
remoteMiddleware: function (ctx, next, params) { return next(params); }, | ||
messageParser: function (data) { return JSON.parse(data, utils_1.dateReviver); }, | ||
@@ -38,3 +39,3 @@ keepAlivePeriod: null, | ||
var opts = __assign(__assign({}, defaultOptions), options); | ||
var session = new RpcSession_1.RpcSession(opts.local, level, opts.listeners, opts.createContext(), opts.localMiddleware, opts.messageParser, opts.keepAlivePeriod, opts.syncRemoteCalls); | ||
var session = new RpcSession_1.RpcSession(opts.local, level, opts.listeners, opts.createContext(), opts.localMiddleware, opts.remoteMiddleware, opts.messageParser, opts.keepAlivePeriod, opts.syncRemoteCalls); | ||
var client = { | ||
@@ -41,0 +42,0 @@ remote: session.remote, |
@@ -7,2 +7,2 @@ export { Topic, RpcContext, RpcConnectionContext } from "./rpc"; | ||
export { setLogger } from "./logger"; | ||
export { dateReviver } from "./utils"; | ||
export { dateReviver, composeMiddleware } from "./utils"; |
@@ -13,1 +13,2 @@ "use strict"; | ||
exports.dateReviver = utils_1.dateReviver; | ||
exports.composeMiddleware = utils_1.composeMiddleware; |
@@ -53,1 +53,2 @@ /** | ||
} | ||
export declare type Middleware = (ctx: RpcContext, next: (params: any) => Promise<any>, params: any) => Promise<any>; |
@@ -1,2 +0,2 @@ | ||
import { MessageType, RpcConnectionContext, RpcContext } from "./rpc"; | ||
import { MessageType, Middleware, RpcConnectionContext, RpcContext } from "./rpc"; | ||
import { Socket } from "./transport"; | ||
@@ -15,6 +15,7 @@ export declare function setCallTimeout(v: any): void; | ||
private localMiddleware; | ||
private remoteMiddleware; | ||
private messageParser; | ||
private keepAlivePeriod; | ||
private syncRemoteCalls; | ||
constructor(local: any, remoteLevel: number, listeners: RpcSessionListeners, connectionContext: RpcConnectionContext, localMiddleware: (ctx: any, next: any) => Promise<any>, messageParser: (data: any) => any[], keepAlivePeriod: number, syncRemoteCalls: boolean); | ||
constructor(local: any, remoteLevel: number, listeners: RpcSessionListeners, connectionContext: RpcConnectionContext, localMiddleware: Middleware, remoteMiddleware: Middleware, messageParser: (data: any) => any[], keepAlivePeriod: number, syncRemoteCalls: boolean); | ||
remote: any; | ||
@@ -31,3 +32,3 @@ open(socket: Socket): void; | ||
private timeoutCalls; | ||
callRemote(name: any, params: any, type: any): Promise<unknown>; | ||
callRemote(name: any, params: any, type: any): Promise<any>; | ||
private sendCall; | ||
@@ -34,0 +35,0 @@ /** Creates call context - context to be used in calls */ |
@@ -67,3 +67,3 @@ "use strict"; | ||
var RpcSession = /** @class */ (function () { | ||
function RpcSession(local, remoteLevel, listeners, connectionContext, localMiddleware, messageParser, keepAlivePeriod, syncRemoteCalls) { | ||
function RpcSession(local, remoteLevel, listeners, connectionContext, localMiddleware, remoteMiddleware, messageParser, keepAlivePeriod, syncRemoteCalls) { | ||
var _this = this; | ||
@@ -74,2 +74,3 @@ this.local = local; | ||
this.localMiddleware = localMiddleware; | ||
this.remoteMiddleware = remoteMiddleware; | ||
this.messageParser = messageParser; | ||
@@ -235,12 +236,15 @@ this.keepAlivePeriod = keepAlivePeriod; | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
_this.queue.push({ | ||
type: type, | ||
name: name, | ||
params: params, | ||
resolve: resolve, | ||
reject: reject, | ||
var sendMessage = function (p) { | ||
return new Promise(function (resolve, reject) { | ||
_this.queue.push({ | ||
type: type, | ||
name: name, | ||
params: p, | ||
resolve: resolve, | ||
reject: reject, | ||
}); | ||
_this.sendCall(); | ||
}); | ||
_this.sendCall(); | ||
}); | ||
}; | ||
return this.remoteMiddleware(null, sendMessage, params); | ||
}; | ||
@@ -294,3 +298,3 @@ RpcSession.prototype.sendCall = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var callContext_1, r, e_2; | ||
var callContext_1, invokeLocalMethod, r, e_2; | ||
return __generator(this, function (_a) { | ||
@@ -301,5 +305,7 @@ switch (_a.label) { | ||
callContext_1 = this.createContext(id, name, localMethod); | ||
return [4 /*yield*/, this.localMiddleware(callContext_1, function () { | ||
return localMethod.call(localMethodObject, params, callContext_1); | ||
})]; | ||
invokeLocalMethod = function (p) { | ||
if (p === void 0) { p = params; } | ||
return localMethod.call(localMethodObject, p, callContext_1); | ||
}; | ||
return [4 /*yield*/, this.localMiddleware(callContext_1, invokeLocalMethod, params)]; | ||
case 1: | ||
@@ -306,0 +312,0 @@ r = _a.sent(); |
@@ -1,6 +0,7 @@ | ||
import { RpcConnectionContext } from "./rpc"; | ||
import { Middleware, RpcConnectionContext } from "./rpc"; | ||
import { Socket, SocketServer } from "./transport"; | ||
export interface RpcServerOptions { | ||
createConnectionContext?(socket: Socket, transportDetails: any): Promise<RpcConnectionContext>; | ||
localMiddleware?: (ctx: any, next: any) => Promise<any>; | ||
localMiddleware?: Middleware; | ||
remoteMiddleware?: Middleware; | ||
clientLevel?: number; | ||
@@ -7,0 +8,0 @@ messageParser?(data: any): any[]; |
@@ -64,3 +64,4 @@ "use strict"; | ||
}); }, | ||
localMiddleware: function (ctx, next) { return next(); }, | ||
localMiddleware: function (ctx, next, params) { return next(params); }, | ||
remoteMiddleware: function (ctx, next, params) { return next(params); }, | ||
clientLevel: 0, | ||
@@ -119,3 +120,3 @@ keepAlivePeriod: 50 * 1000, | ||
unsubscribed: function () { return opts.listeners.unsubscribed(getTotalSubscriptions()); }, | ||
}, connectionContext, opts.localMiddleware, opts.messageParser, opts.keepAlivePeriod, opts.syncRemoteCalls); | ||
}, connectionContext, opts.localMiddleware, opts.remoteMiddleware, opts.messageParser, opts.keepAlivePeriod, opts.syncRemoteCalls); | ||
session.open(socket); | ||
@@ -122,0 +123,0 @@ if (sessions[remoteId]) { |
@@ -1,2 +0,2 @@ | ||
import { MessageType } from "./rpc"; | ||
import { MessageType, Middleware } from "./rpc"; | ||
export declare function dateReviver(key: any, val: any): any; | ||
@@ -9,1 +9,2 @@ export declare const ISO8601: RegExp; | ||
export declare function getClassMethodNames(obj: any): string[]; | ||
export declare function composeMiddleware(...middleware: Middleware[]): Middleware; |
@@ -73,1 +73,28 @@ "use strict"; | ||
exports.getClassMethodNames = getClassMethodNames; | ||
function composeMiddleware() { | ||
var middleware = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
middleware[_i] = arguments[_i]; | ||
} | ||
return function (ctx, next, params) { | ||
var index = -1; | ||
return dispatch(0, params); | ||
function dispatch(i, p) { | ||
if (i <= index) | ||
return Promise.reject(new Error("next() called multiple times")); | ||
index = i; | ||
try { | ||
if (i === middleware.length) { | ||
return Promise.resolve(next(p)); | ||
} | ||
else { | ||
return Promise.resolve(middleware[i](ctx, dispatch.bind(null, i + 1), p)); | ||
} | ||
} | ||
catch (err) { | ||
return Promise.reject(err); | ||
} | ||
} | ||
}; | ||
} | ||
exports.composeMiddleware = composeMiddleware; |
{ | ||
"name": "@push-rpc/core", | ||
"version": "1.0.18", | ||
"version": "1.0.19", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
import {RpcSession} from "./RpcSession" | ||
import {log} from "./logger" | ||
import {dateReviver} from "./utils" | ||
import {RpcConnectionContext} from "./rpc" | ||
import {Middleware, RpcConnectionContext} from "./rpc" | ||
import {Socket} from "./transport" | ||
@@ -27,3 +27,4 @@ | ||
createContext(): RpcConnectionContext | ||
localMiddleware: (ctx, next) => Promise<any> | ||
localMiddleware: Middleware | ||
remoteMiddleware: Middleware | ||
messageParser(data): any[] | ||
@@ -47,3 +48,4 @@ keepAlivePeriod: number | ||
createContext: () => ({remoteId: null}), | ||
localMiddleware: (ctx, next) => next(), | ||
localMiddleware: (ctx, next, params) => next(params), | ||
remoteMiddleware: (ctx, next, params) => next(params), | ||
messageParser: data => JSON.parse(data, dateReviver), | ||
@@ -67,2 +69,3 @@ keepAlivePeriod: null, | ||
opts.localMiddleware, | ||
opts.remoteMiddleware, | ||
opts.messageParser, | ||
@@ -69,0 +72,0 @@ opts.keepAlivePeriod, |
@@ -7,2 +7,2 @@ export {Topic, RpcContext, RpcConnectionContext} from "./rpc" | ||
export {setLogger} from "./logger" | ||
export {dateReviver} from "./utils" | ||
export {dateReviver, composeMiddleware} from "./utils" |
@@ -88,1 +88,9 @@ /** | ||
export class TopicImpl {} | ||
// Middleware - an local or remote call interceptor | ||
// ctx would be null for remote interceptors | ||
export type Middleware = ( | ||
ctx: RpcContext, | ||
next: (params: any) => Promise<any>, | ||
params: any | ||
) => Promise<any> |
import {log} from "./logger" | ||
import {createMessageId, message} from "./utils" | ||
import {getServiceItem, MessageType, Method, RpcConnectionContext, RpcContext} from "./rpc" | ||
import { | ||
getServiceItem, | ||
MessageType, | ||
Method, | ||
Middleware, | ||
RpcConnectionContext, | ||
RpcContext, | ||
} from "./rpc" | ||
import {LocalTopicImpl} from "./local" | ||
@@ -27,3 +34,4 @@ import {createRemote, RemoteTopicImpl} from "./remote" | ||
private connectionContext: RpcConnectionContext, | ||
private localMiddleware: (ctx, next) => Promise<any>, | ||
private localMiddleware: Middleware, | ||
private remoteMiddleware: Middleware, | ||
private messageParser: (data) => any[], | ||
@@ -199,13 +207,17 @@ private keepAlivePeriod: number, | ||
callRemote(name, params, type) { | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ | ||
type, | ||
name: name, | ||
params: params, | ||
resolve, | ||
reject, | ||
const sendMessage = p => { | ||
return new Promise((resolve, reject) => { | ||
this.queue.push({ | ||
type, | ||
name: name, | ||
params: p, | ||
resolve, | ||
reject, | ||
}) | ||
this.sendCall() | ||
}) | ||
} | ||
this.sendCall() | ||
}) | ||
return this.remoteMiddleware(null, sendMessage, params) | ||
} | ||
@@ -270,6 +282,6 @@ | ||
const callContext = this.createContext(id, name, localMethod) | ||
const r = await this.localMiddleware(callContext, () => | ||
localMethod.call(localMethodObject, params, callContext) | ||
) | ||
const invokeLocalMethod = (p = params) => localMethod.call(localMethodObject, p, callContext) | ||
const r = await this.localMiddleware(callContext, invokeLocalMethod, params) | ||
this.send(MessageType.Result, id, r) | ||
@@ -276,0 +288,0 @@ } catch (e) { |
@@ -7,3 +7,3 @@ import * as UUID from "uuid-js" | ||
import {dateReviver} from "./utils" | ||
import {RpcConnectionContext} from "./rpc" | ||
import {Middleware, RpcConnectionContext} from "./rpc" | ||
import {Socket, SocketServer} from "./transport" | ||
@@ -13,3 +13,4 @@ | ||
createConnectionContext?(socket: Socket, transportDetails: any): Promise<RpcConnectionContext> | ||
localMiddleware?: (ctx, next) => Promise<any> | ||
localMiddleware?: Middleware | ||
remoteMiddleware?: Middleware | ||
clientLevel?: number | ||
@@ -34,3 +35,4 @@ messageParser?(data): any[] | ||
}), | ||
localMiddleware: (ctx, next) => next(), | ||
localMiddleware: (ctx, next, params) => next(params), | ||
remoteMiddleware: (ctx, next, params) => next(params), | ||
clientLevel: 0, | ||
@@ -109,2 +111,3 @@ keepAlivePeriod: 50 * 1000, | ||
opts.localMiddleware, | ||
opts.remoteMiddleware, | ||
opts.messageParser, | ||
@@ -111,0 +114,0 @@ opts.keepAlivePeriod, |
import * as UUID from "uuid-js" | ||
import {MessageType} from "./rpc" | ||
import {MessageType, Middleware} from "./rpc" | ||
@@ -53,3 +53,3 @@ export function dateReviver(key, val) { | ||
export const ISO8601 = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/ | ||
export const ISO8601 = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/ | ||
export const ISO8601_secs = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$/ | ||
@@ -72,3 +72,25 @@ export const ISO8601_date = /^\d\d\d\d-\d\d-\d\d$/ | ||
.filter(m => obj[m].name != "constructor") | ||
} | ||
} | ||
export function composeMiddleware(...middleware: Middleware[]): Middleware { | ||
return function(ctx, next, params) { | ||
let index = -1 | ||
return dispatch(0, params) | ||
function dispatch(i, p) { | ||
if (i <= index) return Promise.reject(new Error("next() called multiple times")) | ||
index = i | ||
try { | ||
if (i === middleware.length) { | ||
return Promise.resolve(next(p)) | ||
} else { | ||
return Promise.resolve(middleware[i](ctx, dispatch.bind(null, i + 1), p)) | ||
} | ||
} catch (err) { | ||
return Promise.reject(err) | ||
} | ||
} | ||
} | ||
} |
@@ -28,5 +28,5 @@ import {assert} from "chai" | ||
createConnectionContext: async () => ({sessionId, remoteId: "555"}), | ||
localMiddleware: (ctx, next) => { | ||
localMiddleware: (ctx, next, params) => { | ||
ctx.callId = callId | ||
return next() | ||
return next(params) | ||
}, | ||
@@ -33,0 +33,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import {createRpcClient, createRpcServer} from "../src" | ||
import {createRpcClient, createRpcServer, RpcClientOptions} from "../src" | ||
import {RpcServerOptions} from "../src/server" | ||
@@ -37,4 +37,6 @@ import {createWebsocket, createWebsocketServer} from "../../websocket/src/server" | ||
export async function createTestClient(level = 1) { | ||
return (await createRpcClient(level, () => createWebsocket(`ws://localhost:${TEST_PORT}`))).remote | ||
export async function createTestClient(level = 1, options: Partial<RpcClientOptions> = {}) { | ||
return ( | ||
await createRpcClient(level, () => createWebsocket(`ws://localhost:${TEST_PORT}`), options) | ||
).remote | ||
} |
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
101339
38
2699