From fe60d9dfedd270e1783deeb55ede02fb88ff8754 Mon Sep 17 00:00:00 2001 From: Sven Reglitzki Date: Sun, 17 Apr 2016 18:07:09 +0200 Subject: [PATCH] Add typings and tests for json-socket (#9026) --- json-socket/json-socket-tests.ts | 13 ++++++ json-socket/json-socket.d.ts | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 json-socket/json-socket-tests.ts create mode 100644 json-socket/json-socket.d.ts diff --git a/json-socket/json-socket-tests.ts b/json-socket/json-socket-tests.ts new file mode 100644 index 0000000000..846fa362a8 --- /dev/null +++ b/json-socket/json-socket-tests.ts @@ -0,0 +1,13 @@ +/// + +import JsonSocket = require("json-socket"); +import {Socket} from "net"; + +JsonSocket.sendSingleMessage(42, "localhost", {foo: "bar"}, (err: Error) => {}); +JsonSocket.sendSingleMessageAndReceive(42, "localhost", {foo: "bar"}, (err: Error, message: any) => {}); + +let jsonSocket = new JsonSocket(new Socket()); +jsonSocket.sendMessage({foo: "bar"}, (err: Error) => {}); +jsonSocket.sendEndMessage({foo: "bar"}, (err: Error) => {}); +jsonSocket.sendError(new Error("foo"), (err: Error) => {}); +jsonSocket.sendEndError(new Error("foo"), (err: Error) => {}); diff --git a/json-socket/json-socket.d.ts b/json-socket/json-socket.d.ts new file mode 100644 index 0000000000..198dbf5f51 --- /dev/null +++ b/json-socket/json-socket.d.ts @@ -0,0 +1,71 @@ +// Type definitions for json-socket v0.1.2 +// Project: https://github.com/sebastianseilund/node-json-socket +// Definitions by: Sven Reglitzki +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module "json-socket" { + + import {Socket} from "net"; + + class JsonSocket extends Socket { + + constructor(socket: Socket); + + /** + * sends a single message and closes the connection instantly. Use this if you need to send a server a message, + * but you don't need any response. + * @param port port to send the message to + * @param host host to send the message to + * @param message the message to send + * @param callback will be called after the message has been sent + */ + static sendSingleMessage(port: number, host: string, message: any, callback: (err: Error) => void): void; + + /** + * sends a single message, waits for a single response message from the server and closes the connection right after. + * Use this if you need to send a server a message, and get a response, but you don't need the connection to stay + * open. + * @param port port to send the message to + * @param host host to send the message to + * @param message the message to send + * @param callback will be called when the response message has been received + */ + static sendSingleMessageAndReceive(port: number, host: string, message: any, callback: (err: Error, message: any) => void): void; + + /** + * Convenience method for sending an error as a message. + * @param err an Error object that should be formatted as a message + * @param callback will be called after the message has been sent + */ + sendError(err: Error, callback: (err: Error) => void): void; + + /** + * Same as {@link JsonSocket.sendError}, except that the socket is closed right after the message has been sent + * using net.end(). + * No more messages can be sent from either the server or client through this socket. + * @param err + * @param callback + */ + sendEndError(err: Error, callback: (err: Error) => void): void; + + /** + * Sends a JSON message over the socket. + * @param message the message to send + * @param callback will be called after the message has been sent + */ + sendMessage(message: any, callback: (err: Error) => void): void; + + /** + * Same as {@link JsonSocket.sendMessage}, except that the socket is closed right after the message has been sent + * using net.end(). + * No more messages can be sent from either the server or client through this socket. + * @param message the message to send + * @param callback will be called after the message has been sent + */ + sendEndMessage(message: any, callback: (err: Error) => void): void; + } + + export = JsonSocket; +}