@push-rpc/core
Advanced tools
Comparing version 1.0.35 to 1.1.2
@@ -14,2 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRpcClient = void 0; | ||
var RpcSession_1 = require("./RpcSession"); | ||
@@ -44,3 +45,3 @@ var logger_1 = require("./logger"); | ||
remote: session.remote, | ||
disconnect: function () { return session.terminate(); }, | ||
disconnect: function () { return session.disconnect(); }, | ||
}; | ||
@@ -96,12 +97,2 @@ return (opts.reconnect ? startConnectionLoop : connect)(session, createSocket, opts.listeners).then(function () { return client; }); | ||
} | ||
socket.onMessage(function (data) { | ||
session.handleMessage(data); | ||
}); | ||
socket.onError(function (e) { | ||
if (!connected) { | ||
reject(e); | ||
} | ||
socket.terminate(); | ||
logger_1.log.warn("RPC connection error", e.message); | ||
}); | ||
socket.onOpen(function () { | ||
@@ -113,4 +104,4 @@ connected = true; | ||
}); | ||
socket.onClose(function (code, reason) { | ||
session.close(); | ||
socket.onDisconnected(function (code, reason) { | ||
session.handleDisconnected(); | ||
if (connected) { | ||
@@ -120,3 +111,15 @@ listeners.disconnected({ code: code, reason: reason }); | ||
}); | ||
socket.onError(function (e) { | ||
if (!connected) { | ||
reject(e); | ||
} | ||
logger_1.log.warn("RPC connection error", e.message); | ||
try { | ||
socket.disconnect(); | ||
} | ||
catch (e) { | ||
// ignore | ||
} | ||
}); | ||
}); | ||
} |
@@ -7,2 +7,3 @@ export { Topic, RpcContext, RpcConnectionContext, MessageType, Middleware, RemoteTopic, LocalTopic, } from "./rpc"; | ||
export { setLogger } from "./logger"; | ||
export { dateReviver, composeMiddleware, createMessageId, setCreateMessageId, mapTopic, } from "./utils"; | ||
export { dateReviver, composeMiddleware, createMessageId, setCreateMessageId, mapTopic, createDomWebsocket, } from "./utils"; | ||
export { PING_MESSAGE, PONG_MESSAGE } from "./RpcSession"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var rpc_1 = require("./rpc"); | ||
exports.MessageType = rpc_1.MessageType; | ||
Object.defineProperty(exports, "MessageType", { enumerable: true, get: function () { return rpc_1.MessageType; } }); | ||
var local_1 = require("./local"); | ||
exports.LocalTopicImpl = local_1.LocalTopicImpl; | ||
Object.defineProperty(exports, "LocalTopicImpl", { enumerable: true, get: function () { return local_1.LocalTopicImpl; } }); | ||
var client_1 = require("./client"); | ||
exports.createRpcClient = client_1.createRpcClient; | ||
Object.defineProperty(exports, "createRpcClient", { enumerable: true, get: function () { return client_1.createRpcClient; } }); | ||
var server_1 = require("./server"); | ||
exports.createRpcServer = server_1.createRpcServer; | ||
Object.defineProperty(exports, "createRpcServer", { enumerable: true, get: function () { return server_1.createRpcServer; } }); | ||
var logger_1 = require("./logger"); | ||
exports.setLogger = logger_1.setLogger; | ||
Object.defineProperty(exports, "setLogger", { enumerable: true, get: function () { return logger_1.setLogger; } }); | ||
var utils_1 = require("./utils"); | ||
exports.dateReviver = utils_1.dateReviver; | ||
exports.composeMiddleware = utils_1.composeMiddleware; | ||
exports.createMessageId = utils_1.createMessageId; | ||
exports.setCreateMessageId = utils_1.setCreateMessageId; | ||
exports.mapTopic = utils_1.mapTopic; | ||
Object.defineProperty(exports, "dateReviver", { enumerable: true, get: function () { return utils_1.dateReviver; } }); | ||
Object.defineProperty(exports, "composeMiddleware", { enumerable: true, get: function () { return utils_1.composeMiddleware; } }); | ||
Object.defineProperty(exports, "createMessageId", { enumerable: true, get: function () { return utils_1.createMessageId; } }); | ||
Object.defineProperty(exports, "setCreateMessageId", { enumerable: true, get: function () { return utils_1.setCreateMessageId; } }); | ||
Object.defineProperty(exports, "mapTopic", { enumerable: true, get: function () { return utils_1.mapTopic; } }); | ||
Object.defineProperty(exports, "createDomWebsocket", { enumerable: true, get: function () { return utils_1.createDomWebsocket; } }); | ||
var RpcSession_1 = require("./RpcSession"); | ||
Object.defineProperty(exports, "PING_MESSAGE", { enumerable: true, get: function () { return RpcSession_1.PING_MESSAGE; } }); | ||
Object.defineProperty(exports, "PONG_MESSAGE", { enumerable: true, get: function () { return RpcSession_1.PONG_MESSAGE; } }); |
@@ -70,2 +70,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.prepareLocal = exports.LocalTopicImpl = exports.groupReducer = void 0; | ||
var rpc_1 = require("./rpc"); | ||
@@ -72,0 +73,0 @@ var throttle_1 = require("./throttle"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.setLogger = exports.log = void 0; | ||
exports.log = console; | ||
@@ -4,0 +5,0 @@ function setLogger(l) { |
@@ -23,2 +23,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRemote = exports.RemoteTopicImpl = void 0; | ||
var rpc_1 = require("./rpc"); | ||
@@ -25,0 +26,0 @@ var utils_1 = require("./utils"); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TopicImpl = exports.MessageType = exports.getServiceItem = void 0; | ||
function getServiceItem(services, name) { | ||
@@ -4,0 +5,0 @@ if (!name) { |
@@ -35,6 +35,6 @@ import { MessageType, Middleware, RpcConnectionContext, RpcContext } from "./rpc"; | ||
private trackMessageReceived; | ||
close(): Promise<void>; | ||
handleDisconnected(): Promise<void>; | ||
sendPing: () => Promise<void>; | ||
private checkKeepAlive; | ||
terminate(): void; | ||
disconnect(): void; | ||
handleMessage(data: any): void; | ||
@@ -54,1 +54,3 @@ sendError(id: any, error: Error): void; | ||
} | ||
export declare const PING_MESSAGE = "PING"; | ||
export declare const PONG_MESSAGE = "PONG"; |
@@ -57,2 +57,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.PONG_MESSAGE = exports.PING_MESSAGE = exports.RpcSession = void 0; | ||
var logger_1 = require("./logger"); | ||
@@ -105,2 +106,5 @@ var utils_1 = require("./utils"); | ||
resubscribeTopics(this.remote); | ||
socket.onMessage(function (message) { | ||
_this.handleMessage(message); | ||
}); | ||
socket.onPing(function () { | ||
@@ -129,3 +133,3 @@ _this.trackMessageReceived("PING"); | ||
}; | ||
RpcSession.prototype.close = function () { | ||
RpcSession.prototype.handleDisconnected = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -161,7 +165,7 @@ var _this = this; | ||
logger_1.log.debug("Keep alive period expired, closing socket " + this.connectionContext.remoteId); | ||
this.terminate(); | ||
this.disconnect(); | ||
} | ||
}; | ||
RpcSession.prototype.terminate = function () { | ||
this.socket.terminate(); | ||
RpcSession.prototype.disconnect = function () { | ||
this.socket.disconnect(); | ||
}; | ||
@@ -171,2 +175,8 @@ RpcSession.prototype.handleMessage = function (data) { | ||
this.trackMessageReceived(data); | ||
// handle emulated PINGs | ||
if (data == exports.PING_MESSAGE) { | ||
this.listeners.messageOut(exports.PONG_MESSAGE); | ||
this.socket.send(exports.PONG_MESSAGE); | ||
return; | ||
} | ||
var message_1 = this.messageParser(data); | ||
@@ -403,1 +413,3 @@ if (message_1[0] == rpc_1.MessageType.Result || message_1[0] == rpc_1.MessageType.Error) { | ||
var PING_MESSAGE_ID = "–ws-ping"; | ||
exports.PING_MESSAGE = "PING"; | ||
exports.PONG_MESSAGE = "PONG"; |
@@ -57,2 +57,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createRpcServer = void 0; | ||
var UUID = require("uuid-js"); | ||
@@ -131,3 +132,3 @@ var logger_1 = require("./logger"); | ||
logger_1.log.warn("Failed to create connection context", e_1); | ||
socket.terminate(); | ||
socket.disconnect(); | ||
return [2 /*return*/]; | ||
@@ -145,13 +146,10 @@ case 3: | ||
logger_1.log.warn("Prev session active, discarding", remoteId); | ||
sessions[remoteId].terminate(); | ||
sessions[remoteId].disconnect(); | ||
} | ||
sessions[remoteId] = session; | ||
opts.listeners.connected(remoteId, Object.keys(sessions).length); | ||
socket.onMessage(function (message) { | ||
session.handleMessage(message); | ||
}); | ||
socket.onClose(function (code, reason) { return __awaiter(_this, void 0, void 0, function () { | ||
socket.onDisconnected(function (code, reason) { return __awaiter(_this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, session.close()]; | ||
case 0: return [4 /*yield*/, session.handleDisconnected()]; | ||
case 1: | ||
@@ -158,0 +156,0 @@ _a.sent(); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.throttle = exports.lastValueReducer = void 0; | ||
function lastValueReducer(prevValue, newValue) { | ||
@@ -4,0 +5,0 @@ return newValue; |
export interface Socket { | ||
onMessage(h: (message: string) => void): any; | ||
onOpen(h: () => void): any; | ||
onClose(h: (code: any, reason: any) => void): any; | ||
onError(h: (e: any) => void): any; | ||
onPong(h: () => void): any; | ||
onPing(h: () => void): any; | ||
terminate(): any; | ||
disconnect(): any; | ||
onDisconnected(h: (code: any, reason: any) => void): any; | ||
send(data: string): any; | ||
onMessage(h: (message: string) => void): any; | ||
ping(data: string): any; | ||
@@ -11,0 +11,0 @@ } |
@@ -12,1 +12,12 @@ import { MessageType, Middleware, RemoteTopic } from "./rpc"; | ||
export declare function mapTopic<D1, P, D2>(t: RemoteTopic<D1, P>, map: (D1: any) => D2): RemoteTopic<D2, P>; | ||
export declare function createDomWebsocket(url: any, protocols?: any): { | ||
onMessage: (h: any) => void; | ||
onOpen: (h: any) => any; | ||
onDisconnected: (h: any) => void; | ||
onError: (h: any) => any; | ||
onPong: (h: any) => void; | ||
onPing: (h: any) => void; | ||
disconnect: () => void; | ||
send: (data: any) => any; | ||
ping: () => void; | ||
}; |
@@ -46,3 +46,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createDomWebsocket = exports.mapTopic = exports.composeMiddleware = exports.getClassMethodNames = exports.message = exports.setCreateMessageId = exports.createMessageId = exports.ISO8601_date = exports.ISO8601_secs = exports.ISO8601 = exports.dateReviver = void 0; | ||
var UUID = require("uuid-js"); | ||
var RpcSession_1 = require("./RpcSession"); | ||
function dateReviver(key, val) { | ||
@@ -164,1 +166,52 @@ if (typeof val == "string") { | ||
exports.mapTopic = mapTopic; | ||
function createDomWebsocket(url, protocols) { | ||
if (protocols === void 0) { protocols = undefined; } | ||
var ws = new WebSocket(url, protocols); | ||
var onPong = function () { }; | ||
var onDisconnected = function (code, reason) { }; | ||
function singleCallDisconnected(code, reason) { | ||
onDisconnected(code, reason); | ||
onDisconnected = function () { }; | ||
} | ||
return { | ||
onMessage: function (h) { | ||
ws.onmessage = function (e) { | ||
var message = e.data.toString(); | ||
if (message == RpcSession_1.PONG_MESSAGE) | ||
onPong(); | ||
else | ||
h(message); | ||
}; | ||
}, | ||
onOpen: function (h) { return (ws.onopen = h); }, | ||
onDisconnected: function (h) { | ||
onDisconnected = h; | ||
ws.onclose = function (_a) { | ||
var code = _a.code, reason = _a.reason; | ||
return void singleCallDisconnected(code, reason); | ||
}; | ||
}, | ||
onError: function (h) { return (ws.onerror = h); }, | ||
onPong: function (h) { | ||
onPong = h; | ||
}, | ||
onPing: function (h) { | ||
// not implemented | ||
}, | ||
disconnect: function () { | ||
try { | ||
ws.close(3000, "forced"); | ||
} | ||
catch (e) { | ||
console.warn("Failed to close socket", e); | ||
} | ||
// we sent close frame, no need to wait for actual close | ||
singleCallDisconnected(3000, "forced"); | ||
}, | ||
send: function (data) { return ws.send(data); }, | ||
ping: function () { | ||
ws.send(RpcSession_1.PING_MESSAGE); | ||
}, | ||
}; | ||
} | ||
exports.createDomWebsocket = createDomWebsocket; |
{ | ||
"name": "@push-rpc/core", | ||
"version": "1.0.35", | ||
"version": "1.1.2", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"prepublishOnly": "tsc", | ||
"prepublishOnly": "yarn build", | ||
"build": "tsc", | ||
@@ -12,12 +12,2 @@ "test": "mocha -r ts-node/register tests/**/*.ts" | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/chai": "^4.2.2", | ||
"@types/mocha": "^5.2.7", | ||
"@types/ws": "^6.0.3", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.0", | ||
"prettier": "^1.19.1", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.6.2" | ||
}, | ||
"dependencies": { | ||
@@ -30,3 +20,4 @@ "uuid-js": "^0.7.5" | ||
"access": "public" | ||
} | ||
}, | ||
"gitHead": "4907513edf8ccc166e61d84e6f6076fff66f689a" | ||
} |
@@ -81,3 +81,3 @@ import {RpcSession} from "./RpcSession" | ||
remote: session.remote, | ||
disconnect: () => session.terminate(), | ||
disconnect: () => session.disconnect(), | ||
} | ||
@@ -177,15 +177,2 @@ | ||
socket.onMessage(data => { | ||
session.handleMessage(data) | ||
}) | ||
socket.onError(e => { | ||
if (!connected) { | ||
reject(e) | ||
} | ||
socket.terminate() | ||
log.warn("RPC connection error", e.message) | ||
}) | ||
socket.onOpen(() => { | ||
@@ -198,4 +185,4 @@ connected = true | ||
socket.onClose((code, reason) => { | ||
session.close() | ||
socket.onDisconnected((code, reason) => { | ||
session.handleDisconnected() | ||
if (connected) { | ||
@@ -205,3 +192,17 @@ listeners.disconnected({code, reason}) | ||
}) | ||
socket.onError(e => { | ||
if (!connected) { | ||
reject(e) | ||
} | ||
log.warn("RPC connection error", e.message) | ||
try { | ||
socket.disconnect() | ||
} catch (e) { | ||
// ignore | ||
} | ||
}) | ||
}) | ||
} |
@@ -21,2 +21,4 @@ export { | ||
mapTopic, | ||
createDomWebsocket, | ||
} from "./utils" | ||
export {PING_MESSAGE, PONG_MESSAGE} from "./RpcSession" |
@@ -58,2 +58,6 @@ import {log} from "./logger" | ||
socket.onMessage(message => { | ||
this.handleMessage(message) | ||
}) | ||
socket.onPing(() => { | ||
@@ -90,3 +94,3 @@ this.trackMessageReceived("PING") | ||
async close() { | ||
async handleDisconnected() { | ||
// stop timers | ||
@@ -126,8 +130,8 @@ clearTimeout(this.pingTimer) | ||
log.debug(`Keep alive period expired, closing socket ${this.connectionContext.remoteId}`) | ||
this.terminate() | ||
this.disconnect() | ||
} | ||
} | ||
terminate() { | ||
this.socket.terminate() | ||
disconnect() { | ||
this.socket.disconnect() | ||
} | ||
@@ -139,2 +143,9 @@ | ||
// handle emulated PINGs | ||
if (data == PING_MESSAGE) { | ||
this.listeners.messageOut(PONG_MESSAGE) | ||
this.socket.send(PONG_MESSAGE) | ||
return | ||
} | ||
const message = this.messageParser(data) | ||
@@ -372,1 +383,4 @@ | ||
const PING_MESSAGE_ID = "–ws-ping" | ||
export const PING_MESSAGE = "PING" | ||
export const PONG_MESSAGE = "PONG" |
@@ -95,3 +95,3 @@ import * as UUID from "uuid-js" | ||
log.warn("Failed to create connection context", e) | ||
socket.terminate() | ||
socket.disconnect() | ||
return | ||
@@ -125,3 +125,3 @@ } | ||
log.warn("Prev session active, discarding", remoteId) | ||
sessions[remoteId].terminate() | ||
sessions[remoteId].disconnect() | ||
} | ||
@@ -132,9 +132,5 @@ sessions[remoteId] = session | ||
socket.onMessage(message => { | ||
session.handleMessage(message) | ||
}) | ||
socket.onDisconnected(async (code, reason) => { | ||
await session.handleDisconnected() | ||
socket.onClose(async (code, reason) => { | ||
await session.close() | ||
if (sessions[remoteId] == session) { | ||
@@ -141,0 +137,0 @@ delete sessions[remoteId] |
export interface Socket { | ||
onMessage(h: (message: string) => void) | ||
onOpen(h: () => void) | ||
onClose(h: (code, reason) => void) | ||
onError(h: (e) => void) | ||
@@ -9,4 +7,7 @@ onPong(h: () => void) | ||
terminate() | ||
disconnect() | ||
onDisconnected(h: (code, reason) => void) | ||
send(data: string) | ||
onMessage(h: (message: string) => void) | ||
ping(data: string) | ||
@@ -13,0 +14,0 @@ } |
import * as UUID from "uuid-js" | ||
import {DataConsumer, MessageType, Middleware, RemoteTopic} from "./rpc" | ||
import {PING_MESSAGE, PONG_MESSAGE} from "./RpcSession" | ||
@@ -118,1 +119,57 @@ export function dateReviver(key, val) { | ||
} | ||
declare var WebSocket | ||
export function createDomWebsocket(url, protocols = undefined) { | ||
const ws = new WebSocket(url, protocols) | ||
let onPong = () => {} | ||
let onDisconnected = (code, reason) => {} | ||
function singleCallDisconnected(code, reason) { | ||
onDisconnected(code, reason) | ||
onDisconnected = () => {} | ||
} | ||
return { | ||
onMessage: h => { | ||
ws.onmessage = e => { | ||
const message = e.data.toString() | ||
if (message == PONG_MESSAGE) | ||
onPong() | ||
else | ||
h(message) | ||
} | ||
}, | ||
onOpen: h => (ws.onopen = h), | ||
onDisconnected: h => { | ||
onDisconnected = h | ||
ws.onclose = ({ code, reason }) => void singleCallDisconnected(code, reason) | ||
}, | ||
onError: h => (ws.onerror = h), | ||
onPong: h => { | ||
onPong = h | ||
}, | ||
onPing: h => { | ||
// not implemented | ||
}, | ||
disconnect: () => { | ||
try { | ||
ws.close(3000, "forced") | ||
} catch (e) { | ||
console.warn("Failed to close socket", e) | ||
} | ||
// we sent close frame, no need to wait for actual close | ||
singleCallDisconnected(3000, "forced") | ||
}, | ||
send: data => ws.send(data), | ||
ping: () => { | ||
ws.send(PING_MESSAGE) | ||
}, | ||
} | ||
} |
import {createRpcClient, createRpcServer, RpcClientOptions, RpcServer} from "../src" | ||
import {RpcServerOptions} from "../src/server" | ||
import {createWebsocket, createWebsocketServer} from "../../websocket/src/server" | ||
import {createNodeWebsocket, createWebsocketServer} from "../../websocket/src/server" | ||
@@ -46,3 +46,3 @@ export const TEST_PORT = 5555 | ||
level, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`, protocol), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`, protocol), | ||
options | ||
@@ -49,0 +49,0 @@ ) |
@@ -5,3 +5,3 @@ import {assert} from "chai" | ||
import {createTestClient, startTestServer, TEST_PORT} from "./testUtils" | ||
import {createWebsocket} from "../../websocket/src/server" | ||
import {createNodeWebsocket} from "../../websocket/src/server" | ||
@@ -38,3 +38,3 @@ describe("Topics", () => { | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -85,3 +85,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -142,3 +142,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -184,3 +184,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -223,3 +223,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -270,3 +270,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -314,3 +314,3 @@ ) | ||
1, | ||
() => createWebsocket(`ws://localhost:${TEST_PORT}`), | ||
() => createNodeWebsocket(`ws://localhost:${TEST_PORT}`), | ||
{reconnect: true} | ||
@@ -317,0 +317,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
131873
0
44
3502
5