diff --git a/index.d.ts b/index.d.ts index 00d592e..897980a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,6 +4,7 @@ import https = require('https'); import http = require('http'); import events = require('events'); import Stream = require('stream'); +import WebSocket = require('isomorphic-ws'); import * as connect from 'connect'; export interface UtilsJSONParseOptions { @@ -221,6 +222,7 @@ export declare class Server extends events.EventEmitter { https(options?: HttpsServerOptions): HttpsServer; tcp(options?: TcpServerOptions): TcpServer; tls(options?: TlsServerOptions): TlsServer; + websocket(options?: WebsocketServerOptions): WebsocketServer; middleware(options?: MiddlewareServerOptions): connect.HandleFunction; method(name: string, definition: MethodLike): void; @@ -265,6 +267,14 @@ declare class TlsServer extends tls.Server { constructor(server: Server, options?: TlsServerOptions); } +export interface WebsocketServerOptions extends ServerOptions, WebSocket.ServerOptions { + wss?: WebSocket.Server; +} + +declare class WebsocketServer { + constructor(server: Server, options?: WebsocketServerOptions); +} + type JSONParseReviver = (key: string, value: any) => any; type JSONStringifyReplacer = (key: string, value: any) => any; @@ -306,6 +316,16 @@ declare class HttpsClient extends Client { constructor(options?: HttpsClientOptions); } +export interface WebsocketClientOptions extends ClientOptions { + url?: string; + ws?: WebSocket; + timeout?: number; +} + +declare class WebsocketClient extends Client { + constructor(options?: WebsocketClientOptions); +} + export declare class Client extends events.EventEmitter { constructor(server: Server, options?: ClientOptions); constructor(options: ClientOptions); @@ -314,6 +334,7 @@ export declare class Client extends events.EventEmitter { static https(options?: HttpsClientOptions): HttpsClient; static tcp(options?: TcpClientOptions): TcpClient; static tls(options?: TlsClientOptions): TlsClient; + static websocket(options?: WebsocketClientOptions): WebsocketClient; request(method: string, params: RequestParamsLike, id?: string | null, callback?: JSONRPCCallbackType): JSONRPCRequest; request(method: string, params: RequestParamsLike, callback?: JSONRPCCallbackType): JSONRPCRequest; diff --git a/package-lock.json b/package-lock.json index ec500ca..9146f76 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@types/express-serve-static-core": "^4.17.9", "@types/lodash": "^4.14.159", "@types/node": "^12.12.54", + "@types/ws": "^7.4.4", "commander": "^2.20.3", "delay": "^5.0.0", "es6-promisify": "^5.0.0", @@ -253,6 +254,14 @@ "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" }, + "node_modules/@types/ws": { + "version": "7.4.4", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.4.4.tgz", + "integrity": "sha512-d/7W23JAXPodQNbOZNXvl2K+bqAQrCMwlh/nuQsPSQk6Fq0opHoPrUw43aHsvSbIiQPr8Of2hkFbnz1XBFVyZQ==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/accepts": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", @@ -4176,6 +4185,14 @@ "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" }, + "@types/ws": { + "version": "7.4.4", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-7.4.4.tgz", + "integrity": "sha512-d/7W23JAXPodQNbOZNXvl2K+bqAQrCMwlh/nuQsPSQk6Fq0opHoPrUw43aHsvSbIiQPr8Of2hkFbnz1XBFVyZQ==", + "requires": { + "@types/node": "*" + } + }, "accepts": { "version": "1.3.7", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", diff --git a/package.json b/package.json index 23eb964..1adca02 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@types/express-serve-static-core": "^4.17.9", "@types/lodash": "^4.14.159", "@types/node": "^12.12.54", + "@types/ws": "^7.4.4", "commander": "^2.20.3", "delay": "^5.0.0", "es6-promisify": "^5.0.0", diff --git a/promise/index.d.ts b/promise/index.d.ts index c65fb0f..e07e56e 100644 --- a/promise/index.d.ts +++ b/promise/index.d.ts @@ -4,6 +4,7 @@ import https = require('https'); import http = require('http'); import events = require('events'); import Stream = require('stream'); +import WebSocket = require('isomorphic-ws'); import * as connect from 'connect'; export interface UtilsJSONParseOptions { @@ -222,6 +223,7 @@ export declare class Server extends events.EventEmitter { https(options?: HttpsServerOptions): HttpsServer; tcp(options?: TcpServerOptions): TcpServer; tls(options?: TlsServerOptions): TlsServer; + websocket(options?: WebsocketServerOptions): WebsocketServer; middleware(options?: MiddlewareServerOptions): connect.HandleFunction; method(name: string, definition: MethodLike): void; @@ -266,6 +268,14 @@ declare class TlsServer extends tls.Server { constructor(server: Server, options?: TlsServerOptions); } +export interface WebsocketServerOptions extends ServerOptions, WebSocket.ServerOptions { + wss?: WebSocket.Server; +} + +declare class WebsocketServer { + constructor(server: Server, options?: WebsocketServerOptions); +} + type JSONParseReviver = (key: string, value: any) => any; type JSONStringifyReplacer = (key: string, value: any) => any; @@ -307,6 +317,16 @@ declare class HttpsClient extends Client { constructor(options?: HttpsClientOptions); } +export interface WebsocketClientOptions extends ClientOptions { + url?: string; + ws?: WebSocket; + timeout?: number; +} + +declare class WebsocketClient extends Client { + constructor(options?: WebsocketClientOptions); +} + export declare class Client extends events.EventEmitter { constructor(server: Server, options?: ClientOptions); constructor(options: ClientOptions); @@ -315,6 +335,7 @@ export declare class Client extends events.EventEmitter { static https(options?: HttpsClientOptions): HttpsClient; static tcp(options?: TcpClientOptions): TcpClient; static tls(options?: TlsClientOptions): TlsClient; + static websocket(options?: WebsocketClientOptions): WebsocketClient; request(method:string, params:RequestParamsLike, id:JSONRPCIDLike | undefined, shouldCall:false): JSONRPCRequest; request(method:string, params:RequestParamsLike, id?:JSONRPCIDLike): Promise; diff --git a/typescript/test.ts b/typescript/test.ts index 5ac9f42..62f82d3 100644 --- a/typescript/test.ts +++ b/typescript/test.ts @@ -4,6 +4,7 @@ import jaysonBrowserClient from './../lib/client/browser'; import jaysonPromiseBrowserClient from './../promise/lib/client/browser'; import { reduce, isArray } from 'lodash'; import { Express } from 'express-serve-static-core'; +import WebSocket from 'isomorphic-ws'; /** * This file contains tests for the typescript type definitions. @@ -804,3 +805,31 @@ export function test_server_and_method_call () { } }); } + +export function test_websocket () { + const wss = new WebSocket.Server({port: 12345}); + const server = new jayson.Server(); + const websocketServer = server.websocket({wss}); + + const ws = new WebSocket('ws://localhost:12345'); + const websocketClient = jayson.Client.websocket({ + url: 'ws://localhost:12345', + ws, + timeout: 5000, + }); +} + +export async function test_websocket_Promise () { + const wss = new WebSocket.Server({port: 12345}); + const server = new jaysonPromise.Server(); + const websocketServer = server.websocket({wss}); + + const ws = new WebSocket('ws://localhost:12345'); + const websocketClient = jaysonPromise.Client.websocket({ + url: 'ws://localhost:12345', + ws, + timeout: 5000, + }); + + const result = await websocketClient.request('add', [1,2,3]); +}