Comparing version 0.13.0 to 0.13.1
@@ -0,1 +1,9 @@ | ||
# Version 0.13.1 - 2020/04/04 | ||
## Features | ||
- Added support for Connector Initializer | ||
- Allows using the Buttplug connector for auth or other communication before | ||
spinning up the protocol itself. | ||
# Version 0.13.0 - 2020/03/29 | ||
@@ -2,0 +10,0 @@ |
@@ -8,18 +8,13 @@ /*! | ||
*/ | ||
/// <reference types="node" /> | ||
import { EventEmitter } from "events"; | ||
import { IButtplugClientConnector } from "./IButtplugClientConnector"; | ||
import { ButtplugMessage } from "../core/Messages"; | ||
export declare class ButtplugBrowserWebsocketClientConnector extends EventEmitter implements IButtplugClientConnector { | ||
private _url; | ||
private _shouldUseSorter; | ||
import { ButtplugBrowserWebsocketConnector } from "../utils/ButtplugBrowserWebsocketConnector"; | ||
export declare class ButtplugBrowserWebsocketClientConnector extends ButtplugBrowserWebsocketConnector implements IButtplugClientConnector { | ||
private _sorter; | ||
private _ws; | ||
constructor(_url: string, _shouldUseSorter?: boolean); | ||
protected _ws: WebSocket | undefined; | ||
constructor(_url: string); | ||
get Connected(): boolean; | ||
Connect: () => Promise<void>; | ||
Disconnect: () => Promise<void>; | ||
Send: (aMsg: ButtplugMessage) => Promise<ButtplugMessage>; | ||
private ParseIncomingMessage; | ||
private OnReaderLoad; | ||
protected ParseIncomingMessage: (aEvent: MessageEvent) => void; | ||
protected OnReaderLoad(aEvent: Event): void; | ||
} |
@@ -19,38 +19,9 @@ /*! | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const events_1 = require("events"); | ||
const MessageUtils_1 = require("../core/MessageUtils"); | ||
const ButtplugMessageSorter_1 = require("../utils/ButtplugMessageSorter"); | ||
class ButtplugBrowserWebsocketClientConnector extends events_1.EventEmitter { | ||
constructor(_url, _shouldUseSorter = true) { | ||
super(); | ||
this._url = _url; | ||
this._shouldUseSorter = _shouldUseSorter; | ||
const ButtplugBrowserWebsocketConnector_1 = require("../utils/ButtplugBrowserWebsocketConnector"); | ||
class ButtplugBrowserWebsocketClientConnector extends ButtplugBrowserWebsocketConnector_1.ButtplugBrowserWebsocketConnector { | ||
constructor(_url) { | ||
super(_url); | ||
this._sorter = new ButtplugMessageSorter_1.ButtplugMessageSorter(true); | ||
this.Connect = () => __awaiter(this, void 0, void 0, function* () { | ||
const ws = new WebSocket(this._url); | ||
let res; | ||
let rej; | ||
const p = new Promise((resolve, reject) => { res = resolve; rej = reject; }); | ||
// In websockets, our error rarely tells us much, as for security reasons | ||
// browsers usually only throw Error Code 1006. It's up to those using this | ||
// library to state what the problem might be. | ||
const conErrorCallback = (ev) => rej(); | ||
ws.addEventListener("open", (ev) => __awaiter(this, void 0, void 0, function* () { | ||
this._ws = ws; | ||
this._ws.addEventListener("message", (aMsg) => { this.ParseIncomingMessage(aMsg); }); | ||
this._ws.removeEventListener("close", conErrorCallback); | ||
this._ws.addEventListener("close", this.Disconnect); | ||
res(); | ||
})); | ||
ws.addEventListener("close", conErrorCallback); | ||
return p; | ||
}); | ||
this.Disconnect = () => __awaiter(this, void 0, void 0, function* () { | ||
if (!this.Connected) { | ||
return; | ||
} | ||
this._ws.close(); | ||
this._ws = undefined; | ||
this.emit("disconnect"); | ||
}); | ||
this.Send = (aMsg) => __awaiter(this, void 0, void 0, function* () { | ||
@@ -60,4 +31,4 @@ if (!this.Connected) { | ||
} | ||
const p = this._shouldUseSorter ? this._sorter.PrepareOutgoingMessage(aMsg) : aMsg; | ||
this._ws.send("[" + aMsg.toJSON() + "]"); | ||
const p = this._sorter.PrepareOutgoingMessage(aMsg); | ||
this.SendMessage(aMsg); | ||
return yield p; | ||
@@ -64,0 +35,0 @@ }); |
/// <reference types="node" /> | ||
import { ButtplugClientDevice } from "../client/ButtplugClientDevice"; | ||
import { ButtplugDeviceMessage, DeviceAdded, DeviceRemoved, Ok, Error } from "../core/Messages"; | ||
import { ButtplugDeviceMessage, DeviceAdded, DeviceRemoved, Ok, Error, ButtplugMessage } from "../core/Messages"; | ||
import { EventEmitter } from "events"; | ||
export interface ButtplugClientForwarderConnector extends EventEmitter { | ||
import { ButtplugBrowserWebsocketConnector } from "../utils/ButtplugBrowserWebsocketConnector"; | ||
export interface IButtplugClientForwarderConnector extends EventEmitter { | ||
Connect(): Promise<void>; | ||
Disconnect(): Promise<void>; | ||
SendMessage(message: DeviceAdded | DeviceRemoved | Ok | Error): Promise<void>; | ||
SendForwardedMessage(message: DeviceAdded | DeviceRemoved | Ok | Error): Promise<void>; | ||
} | ||
export declare class ButtplugClientForwarderBrowserWebsocketConnector extends EventEmitter implements ButtplugClientForwarderConnector { | ||
private _serverAddress; | ||
private _connector; | ||
export declare class ButtplugClientForwarderBrowserWebsocketConnector extends ButtplugBrowserWebsocketConnector implements IButtplugClientForwarderConnector { | ||
constructor(serverAddress: string); | ||
Connect: () => Promise<void>; | ||
Disconnect: () => Promise<void>; | ||
SendMessage: (message: Error | Ok | DeviceAdded | DeviceRemoved) => Promise<void>; | ||
SendForwardedMessage(message: ButtplugMessage): Promise<void>; | ||
} | ||
@@ -22,3 +19,3 @@ export declare class ButtplugClientForwarder { | ||
private _devices; | ||
constructor(clientName: string, connector: ButtplugClientForwarderConnector); | ||
constructor(clientName: string, connector: IButtplugClientForwarderConnector); | ||
Connect: () => Promise<void>; | ||
@@ -25,0 +22,0 @@ Disconnect: () => Promise<void>; |
@@ -13,27 +13,15 @@ "use strict"; | ||
const Messages_1 = require("../core/Messages"); | ||
const events_1 = require("events"); | ||
const Exceptions_1 = require("../core/Exceptions"); | ||
const ButtplugBrowserWebsocketClientConnector_1 = require("./ButtplugBrowserWebsocketClientConnector"); | ||
const Utils_1 = require("../utils/Utils"); | ||
class ButtplugClientForwarderBrowserWebsocketConnector extends events_1.EventEmitter { | ||
const ButtplugBrowserWebsocketConnector_1 = require("../utils/ButtplugBrowserWebsocketConnector"); | ||
class ButtplugClientForwarderBrowserWebsocketConnector extends ButtplugBrowserWebsocketConnector_1.ButtplugBrowserWebsocketConnector { | ||
constructor(serverAddress) { | ||
super(); | ||
this.Connect = () => __awaiter(this, void 0, void 0, function* () { | ||
// Connect to the websocket, hook up events. | ||
this._connector.addListener("message", (msg) => __awaiter(this, void 0, void 0, function* () { | ||
for (const m of msg) { | ||
this.emit("message", m); | ||
} | ||
})); | ||
yield this._connector.Connect(); | ||
}); | ||
this.Disconnect = () => __awaiter(this, void 0, void 0, function* () { | ||
// Disconnect from the websocket | ||
yield this._connector.Disconnect(); | ||
}); | ||
this.SendMessage = (message) => __awaiter(this, void 0, void 0, function* () { | ||
super(serverAddress); | ||
} | ||
SendForwardedMessage(message) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Expect that we'll just get Ok or Error back. | ||
yield this._connector.Send(message); | ||
this.SendMessage(message); | ||
return Promise.resolve(); | ||
}); | ||
this._connector = new ButtplugBrowserWebsocketClientConnector_1.ButtplugBrowserWebsocketClientConnector(serverAddress, false); | ||
} | ||
@@ -47,4 +35,6 @@ } | ||
yield this._connector.Connect(); | ||
this._connector.addListener("message", (msg) => __awaiter(this, void 0, void 0, function* () { | ||
yield this.ReceiveDeviceCommand(msg); | ||
this._connector.addListener("message", (msgs) => __awaiter(this, void 0, void 0, function* () { | ||
for (const m of msgs) { | ||
yield this.ReceiveDeviceCommand(m); | ||
} | ||
})); | ||
@@ -58,7 +48,7 @@ }); | ||
device.addListener("deviceremoved", () => this.RemoveDevice(device)); | ||
yield this._connector.SendMessage(msg); | ||
yield this._connector.SendForwardedMessage(msg); | ||
}); | ||
this.RemoveDevice = (device) => __awaiter(this, void 0, void 0, function* () { | ||
const msg = this.CreateDeviceRemoved(device); | ||
yield this._connector.SendMessage(msg); | ||
yield this._connector.SendForwardedMessage(msg); | ||
}); | ||
@@ -79,3 +69,3 @@ this.ReceiveDeviceCommand = (message) => __awaiter(this, void 0, void 0, function* () { | ||
// Id match. | ||
yield this._connector.SendMessage(new Messages_1.Ok(message.Id)); | ||
yield this._connector.SendForwardedMessage(new Messages_1.Ok(message.Id)); | ||
}); | ||
@@ -82,0 +72,0 @@ // We should prepend the client name to our devices before sending them out. |
@@ -18,2 +18,3 @@ /*! | ||
get Server(): ButtplugServer | null; | ||
Initialize(): Promise<void>; | ||
get Connected(): boolean; | ||
@@ -20,0 +21,0 @@ Connect: () => Promise<void>; |
@@ -66,2 +66,7 @@ /*! | ||
} | ||
Initialize() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return Promise.resolve(); | ||
}); | ||
} | ||
get Connected() { | ||
@@ -68,0 +73,0 @@ return this._connected; |
@@ -14,4 +14,5 @@ /*! | ||
Disconnect: () => Promise<void>; | ||
Initialize: () => Promise<void>; | ||
Send: (aMsg: ButtplugMessage) => Promise<ButtplugMessage>; | ||
readonly Connected: boolean; | ||
} |
{ | ||
"name": "buttplug", | ||
"version": "0.13.0", | ||
"version": "0.13.1", | ||
"description": "Javascript library for creating or accessing Buttplug Intimate Hardware Protocol servers/clients, for node or web", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/buttplugio/buttplug-js/packages/buttplug#readme", |
@@ -11,3 +11,2 @@ /*! | ||
import { EventEmitter } from "events"; | ||
import { IButtplugClientConnector } from "./IButtplugClientConnector"; | ||
@@ -17,10 +16,11 @@ import { ButtplugMessage } from "../core/Messages"; | ||
import { ButtplugMessageSorter } from "../utils/ButtplugMessageSorter"; | ||
import { ButtplugBrowserWebsocketConnector } from "../utils/ButtplugBrowserWebsocketConnector"; | ||
export class ButtplugBrowserWebsocketClientConnector extends EventEmitter implements IButtplugClientConnector { | ||
export class ButtplugBrowserWebsocketClientConnector extends ButtplugBrowserWebsocketConnector implements IButtplugClientConnector { | ||
private _sorter: ButtplugMessageSorter = new ButtplugMessageSorter(true); | ||
private _ws: WebSocket | undefined; | ||
protected _ws: WebSocket | undefined; | ||
public constructor(private _url: string, private _shouldUseSorter: boolean = true) { | ||
super(); | ||
public constructor(_url: string) { | ||
super(_url); | ||
} | ||
@@ -32,31 +32,2 @@ | ||
public Connect = async (): Promise<void> => { | ||
const ws = new WebSocket(this._url); | ||
let res; | ||
let rej; | ||
const p = new Promise<void>((resolve, reject) => { res = resolve; rej = reject; }); | ||
// In websockets, our error rarely tells us much, as for security reasons | ||
// browsers usually only throw Error Code 1006. It's up to those using this | ||
// library to state what the problem might be. | ||
const conErrorCallback = (ev) => rej(); | ||
ws.addEventListener("open", async (ev) => { | ||
this._ws = ws; | ||
this._ws.addEventListener("message", (aMsg) => { this.ParseIncomingMessage(aMsg); }); | ||
this._ws.removeEventListener("close", conErrorCallback); | ||
this._ws.addEventListener("close", this.Disconnect); | ||
res(); | ||
}); | ||
ws.addEventListener("close", conErrorCallback); | ||
return p; | ||
} | ||
public Disconnect = async (): Promise<void> => { | ||
if (!this.Connected) { | ||
return; | ||
} | ||
this._ws!.close(); | ||
this._ws = undefined; | ||
this.emit("disconnect"); | ||
} | ||
public Send = async (aMsg: ButtplugMessage): Promise<ButtplugMessage> => { | ||
@@ -66,8 +37,8 @@ if (!this.Connected) { | ||
} | ||
const p = this._shouldUseSorter? this._sorter.PrepareOutgoingMessage(aMsg) : aMsg; | ||
this._ws!.send("[" + aMsg.toJSON() + "]"); | ||
const p = this._sorter.PrepareOutgoingMessage(aMsg); | ||
this.SendMessage(aMsg); | ||
return await p; | ||
} | ||
private ParseIncomingMessage = (aEvent: MessageEvent) => { | ||
protected ParseIncomingMessage = (aEvent: MessageEvent) => { | ||
if (typeof (aEvent.data) === "string") { | ||
@@ -84,3 +55,3 @@ const msgs = FromJSON(aEvent.data); | ||
private OnReaderLoad(aEvent: Event) { | ||
protected OnReaderLoad(aEvent: Event) { | ||
const msgs = FromJSON((aEvent.target as FileReader).result); | ||
@@ -87,0 +58,0 @@ const emitMsgs = this._sorter.ParseIncomingMessages(msgs); |
@@ -5,39 +5,20 @@ import { ButtplugClientDevice } from "../client/ButtplugClientDevice"; | ||
import { ButtplugDeviceException } from "../core/Exceptions"; | ||
import { ButtplugBrowserWebsocketClientConnector } from "./ButtplugBrowserWebsocketClientConnector"; | ||
import { getRandomInt } from "../utils/Utils"; | ||
import { ButtplugBrowserWebsocketConnector } from "../utils/ButtplugBrowserWebsocketConnector"; | ||
export interface ButtplugClientForwarderConnector extends EventEmitter { | ||
export interface IButtplugClientForwarderConnector extends EventEmitter { | ||
Connect(): Promise<void>; | ||
Disconnect(): Promise<void>; | ||
SendMessage(message: DeviceAdded | DeviceRemoved | Ok | Error): Promise<void>; | ||
SendForwardedMessage(message: DeviceAdded | DeviceRemoved | Ok | Error): Promise<void>; | ||
} | ||
export class ButtplugClientForwarderBrowserWebsocketConnector extends EventEmitter implements ButtplugClientForwarderConnector { | ||
private _serverAddress: string; | ||
private _connector: ButtplugBrowserWebsocketClientConnector; | ||
export class ButtplugClientForwarderBrowserWebsocketConnector extends ButtplugBrowserWebsocketConnector implements IButtplugClientForwarderConnector { | ||
public constructor(serverAddress: string) { | ||
super(); | ||
this._connector = new ButtplugBrowserWebsocketClientConnector(serverAddress, false); | ||
super(serverAddress); | ||
} | ||
public Connect = async (): Promise<void> => { | ||
// Connect to the websocket, hook up events. | ||
this._connector.addListener("message", async (msg: ButtplugDeviceMessage[]) => { | ||
for (const m of msg) { | ||
this.emit("message", m); | ||
} | ||
}); | ||
await this._connector.Connect(); | ||
} | ||
public Disconnect = async (): Promise<void> => { | ||
// Disconnect from the websocket | ||
await this._connector.Disconnect(); | ||
} | ||
public SendMessage = async (message: DeviceAdded | DeviceRemoved | Ok | Error): Promise<void> => { | ||
public async SendForwardedMessage(message: ButtplugMessage): Promise<void> { | ||
// Expect that we'll just get Ok or Error back. | ||
await this._connector.Send(message); | ||
this.SendMessage(message); | ||
return Promise.resolve(); | ||
} | ||
@@ -48,6 +29,6 @@ } | ||
private _clientName: string; | ||
private _connector: ButtplugClientForwarderConnector; | ||
private _connector: IButtplugClientForwarderConnector; | ||
private _devices: Map<number, ButtplugClientDevice> = new Map<number, ButtplugClientDevice>(); | ||
public constructor(clientName: string, connector: ButtplugClientForwarderConnector) { | ||
public constructor(clientName: string, connector: IButtplugClientForwarderConnector) { | ||
// We should prepend the client name to our devices before sending them out. | ||
@@ -60,4 +41,6 @@ this._clientName = clientName; | ||
await this._connector.Connect(); | ||
this._connector.addListener("message", async (msg: ButtplugDeviceMessage) => { | ||
await this.ReceiveDeviceCommand(msg); | ||
this._connector.addListener("message", async (msgs: ButtplugDeviceMessage[]) => { | ||
for (const m of msgs) { | ||
await this.ReceiveDeviceCommand(m); | ||
} | ||
}); | ||
@@ -73,3 +56,3 @@ } | ||
device.addListener("deviceremoved", () => this.RemoveDevice(device)); | ||
await this._connector.SendMessage(msg); | ||
await this._connector.SendForwardedMessage(msg); | ||
} | ||
@@ -79,3 +62,3 @@ | ||
const msg = this.CreateDeviceRemoved(device); | ||
await this._connector.SendMessage(msg); | ||
await this._connector.SendForwardedMessage(msg); | ||
} | ||
@@ -97,3 +80,3 @@ | ||
// Id match. | ||
await this._connector.SendMessage(new Ok(message.Id)); | ||
await this._connector.SendForwardedMessage(new Ok(message.Id)); | ||
} | ||
@@ -100,0 +83,0 @@ |
@@ -29,2 +29,6 @@ /*! | ||
public async Initialize(): Promise<void> { | ||
return Promise.resolve(); | ||
} | ||
public get Connected(): boolean { | ||
@@ -31,0 +35,0 @@ return this._connected; |
@@ -15,4 +15,5 @@ /*! | ||
Disconnect: () => Promise<void>; | ||
Initialize: () => Promise<void>; | ||
Send: (aMsg: ButtplugMessage) => Promise<ButtplugMessage>; | ||
readonly Connected: boolean; | ||
} |
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 too big to display
Sorry, the diff of this file is too big to display
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
4001702
257
31719