@push-rpc/next
Advanced tools
Comparing version 2.0.0-beta.3 to 2.0.0-beta.4
import { RpcContext, Services } from "../rpc.js"; | ||
import { ServicesWithSubscriptions } from "./remote.js"; | ||
import WebSocket from "ws"; | ||
import type WebSocket from "ws"; | ||
import { Middleware } from "../utils/middleware.js"; | ||
@@ -13,6 +13,6 @@ export type RpcClient = { | ||
callTimeout: number; | ||
subscribe: boolean; | ||
reconnectDelay: number; | ||
errorDelayMaxDuration: number; | ||
pingInterval: number; | ||
subscriptions: boolean; | ||
middleware: Middleware<RpcContext>[]; | ||
@@ -19,0 +19,0 @@ }; |
@@ -22,8 +22,8 @@ "use strict"; | ||
callTimeout: 5 * 1000, | ||
subscribe: true, | ||
reconnectDelay: 0, | ||
errorDelayMaxDuration: 15 * 1000, | ||
pingInterval: 30 * 1000, // should be in-sync with server | ||
subscriptions: true, | ||
middleware: [], | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -23,3 +23,3 @@ "use strict"; | ||
} | ||
if (this.options.subscribe) { | ||
if (this.options.subscriptions) { | ||
this.connection.connect().catch((e) => { | ||
@@ -26,0 +26,0 @@ // ignored |
@@ -1,2 +0,2 @@ | ||
import WebSocket from "ws"; | ||
import type WebSocket from "ws"; | ||
export declare class WebSocketConnection { | ||
@@ -3,0 +3,0 @@ private readonly url; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.WebSocketConnection = void 0; | ||
const ws_1 = __importDefault(require("ws")); | ||
const logger_js_1 = require("../logger.js"); | ||
@@ -86,3 +105,4 @@ const json_js_1 = require("../utils/json.js"); | ||
try { | ||
const socket = new ws_1.default(this.url, this.clientId); | ||
const { WebSocket } = await Promise.resolve().then(() => __importStar(require("ws"))); | ||
const socket = new WebSocket(this.url, this.clientId); | ||
let connected = false; | ||
@@ -89,0 +109,0 @@ socket.on("open", () => { |
@@ -24,2 +24,3 @@ /// <reference types="node" /> | ||
pingInterval: number; | ||
subscriptions: boolean; | ||
createConnectionContext(req: IncomingMessage): Promise<RpcConnectionContext>; | ||
@@ -26,0 +27,0 @@ } & ({ |
@@ -25,2 +25,3 @@ "use strict"; | ||
pingInterval: 30 * 1000, // should be in-sync with client | ||
subscriptions: true, | ||
async createConnectionContext(req) { | ||
@@ -27,0 +28,0 @@ return { |
@@ -10,3 +10,4 @@ /// <reference types="node" /> | ||
constructor(services: S, options: PublishServicesOptions<C>); | ||
start(): Promise<void> | undefined; | ||
private createConnectionsServer; | ||
start(): Promise<void>; | ||
close(): Promise<void>; | ||
@@ -17,3 +18,3 @@ createServicesWithTriggers(): ServicesWithTriggers<S>; | ||
private readonly invocationCache; | ||
private readonly connectionsServer; | ||
private connectionsServer; | ||
readonly httpServer: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>; | ||
@@ -20,0 +21,0 @@ private call; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -9,3 +32,2 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const http_1 = __importDefault(require("http")); | ||
const ConnectionsServer_js_1 = require("./ConnectionsServer.js"); | ||
const promises_js_1 = require("../utils/promises.js"); | ||
@@ -24,2 +46,3 @@ const http_js_1 = require("./http.js"); | ||
this.invocationCache = new promises_js_1.PromiseCache(); | ||
this.connectionsServer = null; | ||
this.call = async (connectionContext, itemName, parameters) => { | ||
@@ -54,3 +77,3 @@ const item = this.getRemoteFunction(itemName); | ||
lastDataJson = newDataJson; | ||
this.connectionsServer.publish(connectionContext.clientId, itemName, parameters, newData); | ||
this.connectionsServer?.publish(connectionContext.clientId, itemName, parameters, newData); | ||
} | ||
@@ -93,5 +116,2 @@ } | ||
} | ||
this.connectionsServer = new ConnectionsServer_js_1.ConnectionsServer(this.httpServer, { pingInterval: options.pingInterval, path: options.path }, (clientId) => { | ||
this.localSubscriptions.unsubscribeAll(clientId); | ||
}, !("server" in this.options)); | ||
this.httpServer.addListener("request", (req, res) => (0, http_js_1.serveHttpRequest)(req, res, options.path, { | ||
@@ -103,3 +123,12 @@ call: this.call, | ||
} | ||
start() { | ||
async createConnectionsServer() { | ||
const { ConnectionsServer } = await Promise.resolve().then(() => __importStar(require("./ConnectionsServer.js"))); | ||
return new ConnectionsServer(this.httpServer, { pingInterval: this.options.pingInterval, path: this.options.path }, (clientId) => { | ||
this.localSubscriptions.unsubscribeAll(clientId); | ||
}, !("server" in this.options)); | ||
} | ||
async start() { | ||
this.connectionsServer = this.options.subscriptions | ||
? await this.createConnectionsServer() | ||
: null; | ||
if ("server" in this.options) { | ||
@@ -121,3 +150,3 @@ return Promise.resolve(); | ||
async close() { | ||
await this.connectionsServer.close(); | ||
await this.connectionsServer?.close(); | ||
await new Promise((resolve, reject) => { | ||
@@ -124,0 +153,0 @@ this.httpServer.closeIdleConnections(); |
{ | ||
"name": "@push-rpc/next", | ||
"version": "2.0.0-beta.3", | ||
"version": "2.0.0-beta.4", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
import {RpcContext, Services} from "../rpc.js" | ||
import {ServicesWithSubscriptions} from "./remote.js" | ||
import WebSocket from "ws" | ||
import type WebSocket from "ws" | ||
import {RpcClientImpl} from "./RpcClientImpl.js" | ||
@@ -18,6 +18,6 @@ import {Middleware} from "../utils/middleware.js" | ||
callTimeout: number | ||
subscribe: boolean | ||
reconnectDelay: number | ||
errorDelayMaxDuration: number | ||
pingInterval: number | ||
subscriptions: boolean | ||
middleware: Middleware<RpcContext>[] | ||
@@ -52,7 +52,7 @@ } | ||
callTimeout: 5 * 1000, | ||
subscribe: true, | ||
reconnectDelay: 0, | ||
errorDelayMaxDuration: 15 * 1000, | ||
pingInterval: 30 * 1000, // should be in-sync with server | ||
subscriptions: true, | ||
middleware: [], | ||
} |
@@ -105,3 +105,3 @@ import {CallOptions, InvocationType, RpcContext, Services} from "../rpc.js" | ||
if (this.options.subscribe) { | ||
if (this.options.subscriptions) { | ||
this.connection.connect().catch((e) => { | ||
@@ -108,0 +108,0 @@ // ignored |
@@ -1,2 +0,2 @@ | ||
import WebSocket from "ws" | ||
import type WebSocket from "ws" | ||
import {log} from "../logger.js" | ||
@@ -100,2 +100,4 @@ import {safeParseJson} from "../utils/json.js" | ||
try { | ||
const {WebSocket} = await import("ws") | ||
const socket = new WebSocket(this.url, this.clientId) | ||
@@ -102,0 +104,0 @@ |
@@ -39,6 +39,6 @@ import {CLIENT_ID_HEADER, RpcConnectionContext, RpcContext, Services} from "../rpc.js" | ||
host: string | ||
path: string | ||
middleware: Middleware<C>[] | ||
pingInterval: number | ||
subscriptions: boolean | ||
createConnectionContext(req: IncomingMessage): Promise<RpcConnectionContext> | ||
@@ -60,2 +60,3 @@ } & ( | ||
pingInterval: 30 * 1000, // should be in-sync with client | ||
subscriptions: true, | ||
@@ -62,0 +63,0 @@ async createConnectionContext(req: IncomingMessage): Promise<RpcConnectionContext> { |
import {PublishServicesOptions, RpcServer} from "./index.js" | ||
import {LocalSubscriptions} from "./LocalSubscriptions.js" | ||
import http from "http" | ||
import {ConnectionsServer} from "./ConnectionsServer.js" | ||
import type {ConnectionsServer} from "./ConnectionsServer.js" | ||
import {PromiseCache} from "../utils/promises.js" | ||
@@ -41,11 +41,2 @@ import {serveHttpRequest} from "./http.js" | ||
this.connectionsServer = new ConnectionsServer( | ||
this.httpServer, | ||
{pingInterval: options.pingInterval, path: options.path}, | ||
(clientId) => { | ||
this.localSubscriptions.unsubscribeAll(clientId) | ||
}, | ||
!("server" in this.options) | ||
) | ||
this.httpServer.addListener("request", (req, res) => | ||
@@ -66,3 +57,20 @@ serveHttpRequest( | ||
start() { | ||
private async createConnectionsServer() { | ||
const {ConnectionsServer} = await import("./ConnectionsServer.js") | ||
return new ConnectionsServer( | ||
this.httpServer, | ||
{pingInterval: this.options.pingInterval, path: this.options.path}, | ||
(clientId) => { | ||
this.localSubscriptions.unsubscribeAll(clientId) | ||
}, | ||
!("server" in this.options) | ||
) | ||
} | ||
async start() { | ||
this.connectionsServer = this.options.subscriptions | ||
? await this.createConnectionsServer() | ||
: null | ||
if ("server" in this.options) { | ||
@@ -88,3 +96,3 @@ return Promise.resolve() | ||
async close() { | ||
await this.connectionsServer.close() | ||
await this.connectionsServer?.close() | ||
await new Promise<void>((resolve, reject) => { | ||
@@ -109,3 +117,3 @@ this.httpServer.closeIdleConnections() | ||
private readonly invocationCache = new PromiseCache() | ||
private readonly connectionsServer: ConnectionsServer | ||
private connectionsServer: ConnectionsServer | null = null | ||
readonly httpServer | ||
@@ -176,3 +184,3 @@ | ||
lastDataJson = newDataJson | ||
this.connectionsServer.publish( | ||
this.connectionsServer?.publish( | ||
connectionContext.clientId, | ||
@@ -179,0 +187,0 @@ itemName, |
@@ -20,3 +20,3 @@ import {assert} from "chai" | ||
await client.test.item.subscribe(() => { | ||
await client.test.item.subscribe((item) => { | ||
receivedItem = item | ||
@@ -29,2 +29,35 @@ }) | ||
it("disabled subscribe", async () => { | ||
const item = {r: "1"} | ||
const services = await startTestServer( | ||
{ | ||
test: { | ||
item: async () => item, | ||
}, | ||
}, | ||
{ | ||
subscriptions: false, | ||
} | ||
) | ||
const client = await createTestClient<typeof services>({ | ||
subscriptions: false, | ||
}) | ||
let receivedItem | ||
await client.test.item.subscribe((item) => { | ||
receivedItem = item | ||
}) | ||
receivedItem = null | ||
item.r = "2" | ||
services.test.item.trigger() | ||
await adelay(20) | ||
assert.equal(receivedItem, null) | ||
}) | ||
it("error in supplier breaks subscribe", async () => { | ||
@@ -31,0 +64,0 @@ const services = await startTestServer({ |
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
187939
4206