Skip to content

Commit

Permalink
replace circular-json with flatted; adapt ts to updates
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Kozjak <kozjakm1@gmail.com>
  • Loading branch information
mkozjak committed Feb 11, 2022
1 parent c2cce27 commit c15daf6
Show file tree
Hide file tree
Showing 17 changed files with 249 additions and 347 deletions.
2 changes: 1 addition & 1 deletion build-ts/lib/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class CommonClient extends EventEmitter {
* @param {Object} params - optional method parameters
* @return {Promise}
*/
notify(method: string, params?: IWSRequestParams): Promise<unknown>;
notify(method: string, params?: IWSRequestParams): Promise<void>;
/**
* Subscribes for a defined event.
* @method
Expand Down
8 changes: 4 additions & 4 deletions build-ts/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var __rest = (this && this.__rest) || function (s, e) {
};
// @ts-ignore
import { EventEmitter } from "eventemitter3";
import CircularJSON from "circular-json";
import { stringify, parse } from "flatted";
export default class CommonClient extends EventEmitter {
/**
* Instantiate a Client class.
Expand Down Expand Up @@ -80,7 +80,7 @@ export default class CommonClient extends EventEmitter {
params: params || null,
id: rpc_id
};
this.socket.send(JSON.stringify(message), ws_opts, (error) => {
this.socket.send(stringify(message), ws_opts, (error) => {
if (error)
return reject(error);
this.queue[rpc_id] = { promise: [resolve, reject] };
Expand Down Expand Up @@ -129,7 +129,7 @@ export default class CommonClient extends EventEmitter {
method: method,
params: params || null
};
this.socket.send(JSON.stringify(message), (error) => {
this.socket.send(stringify(message), (error) => {
if (error)
return reject(error);
resolve();
Expand Down Expand Up @@ -195,7 +195,7 @@ export default class CommonClient extends EventEmitter {
if (message instanceof ArrayBuffer)
message = Buffer.from(message).toString();
try {
message = CircularJSON.parse(message);
message = parse(message);
}
catch (error) {
return;
Expand Down
2 changes: 1 addition & 1 deletion build-ts/lib/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export default class Server extends EventEmitter {
* @method
* @return {Promise}
*/
close(): Promise<unknown>;
close(): Promise<void>;
/**
* Handles all WebSocket JSON RPC 2.0 requests.
* @private
Expand Down
14 changes: 7 additions & 7 deletions build-ts/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EventEmitter } from "eventemitter3";
import { Server as WebSocketServer } from "ws";
import { v1 as uuidv1 } from "uuid";
import url from "url";
import CircularJSON from "circular-json";
import { stringify } from "flatted";
import * as utils from "./utils";
export default class Server extends EventEmitter {
/**
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class Server extends EventEmitter {
const socket = this.namespaces[ns].clients.get(socket_id);
if (!socket)
continue;
socket.send(CircularJSON.stringify({
socket.send(stringify({
notification: name,
params: params || null
}));
Expand Down Expand Up @@ -237,7 +237,7 @@ export default class Server extends EventEmitter {
emit(event, ...params) {
const socket_ids = [...self.namespaces[name].clients.keys()];
for (let i = 0, id; id = socket_ids[i]; ++i) {
self.namespaces[name].clients.get(id).send(CircularJSON.stringify({
self.namespaces[name].clients.get(id).send(stringify({
notification: event,
params: params || []
}));
Expand Down Expand Up @@ -339,15 +339,15 @@ export default class Server extends EventEmitter {
parsedData = JSON.parse(data);
}
catch (error) {
return socket.send(JSON.stringify({
return socket.send(stringify({
jsonrpc: "2.0",
error: utils.createError(-32700, error.toString()),
id: null
}), msg_options);
}
if (Array.isArray(parsedData)) {
if (!parsedData.length)
return socket.send(JSON.stringify({
return socket.send(stringify({
jsonrpc: "2.0",
error: utils.createError(-32600, "Invalid array"),
id: null
Expand All @@ -361,12 +361,12 @@ export default class Server extends EventEmitter {
}
if (!responses.length)
return;
return socket.send(CircularJSON.stringify(responses), msg_options);
return socket.send(stringify(responses), msg_options);
}
const response = await this._runMethod(parsedData, socket._id, ns);
if (!response)
return;
return socket.send(CircularJSON.stringify(response), msg_options);
return socket.send(stringify(response), msg_options);
});
}
/**
Expand Down
Loading

0 comments on commit c15daf6

Please sign in to comment.