Socket
Socket
Sign inDemoInstall

@furystack/websocket-api

Package Overview
Dependencies
Maintainers
1
Versions
232
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@furystack/websocket-api - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

2

dist/ActionResolver.d.ts

@@ -7,4 +7,4 @@ import { Injector } from "@furystack/inject";

readonly actions: IWebSocketAction[];
execute(data: Data, context: WebSocketContext, injector: Injector, send: (data: any) => Promise<void>): void;
execute(data: Data, context: WebSocketContext, injector: Injector): void;
constructor(actions: IWebSocketAction[]);
}

@@ -7,5 +7,5 @@ "use strict";

}
execute(data, context, injector, send) {
execute(data, context, injector) {
const action = this.actions.find((a) => a.canExecute(data));
action && action.execute(data, context, send);
action && action.execute(data, context, injector);
}

@@ -12,0 +12,0 @@ }

@@ -0,3 +1,4 @@

import { Injector } from "@furystack/inject";
import { Data } from "ws";
import { WebSocketContext } from "../WebSocketContext";
import { IWebSocketContext } from "./IWebSocketContext";
export interface IWebSocketAction {

@@ -7,3 +8,3 @@ authenticate: boolean;

canExecute: (data: Data) => boolean;
execute: (data: Data, context: WebSocketContext, send: (data: any) => Promise<void>) => void;
execute: (data: Data, context: IWebSocketContext, injector: Injector) => void;
}
import { IContext } from "@furystack/core";
import { Data } from "ws";
export interface IWebSocketContext extends IContext {
send(data: Data): Promise<void>;
}

@@ -0,7 +1,9 @@

import { Injector } from "@furystack/inject";
import { Data } from "ws";
export declare abstract class WebSocketAction {
import { IWebSocketAction, IWebSocketContext } from "./models";
export declare abstract class WebSocketAction implements IWebSocketAction {
readonly authenticate: boolean;
readonly authorize: never[];
abstract canExecute(data: Data): boolean;
abstract execute(data: Data): any;
abstract execute(data: Data, context: IWebSocketContext, injector: Injector): any;
}
/// <reference types="node" />
import { IApi, LoggerCollection } from "@furystack/core";
import { IdentityService } from "@furystack/http-api";
import { Injector } from "@furystack/inject";
import { IncomingMessage } from "http";
import * as Ws from "ws";
import { WebSocketContext } from ".";
import { IdentityService } from "../node_modules/@furystack/http-api";
import { IWebSocketApiConfiguration, IWebSocketContext } from "./models";

@@ -12,3 +13,3 @@ export declare const defaultOptions: IWebSocketApiConfiguration;

injector: Injector;
contextFactory: (identityService: IdentityService<import("@furystack/http-api/dist/IdentityService").ILoginUser<import("@furystack/core/dist/Models/IUser").IUser>>, incomingMessage: IncomingMessage, injector?: Injector | undefined) => WebSocketContext;
contextFactory: (identityService: IdentityService<import("@furystack/http-api/dist/IdentityService").ILoginUser<import("@furystack/core/dist/Models/IUser").IUser>>, incomingMessage: IncomingMessage, webSocket: Ws, injector?: Injector | undefined) => WebSocketContext;
activate: () => Promise<void>;

@@ -15,0 +16,0 @@ dispose: () => void;

@@ -28,3 +28,3 @@ "use strict";

this.injector = new inject_1.Injector();
this.contextFactory = (identityService, incomingMessage, injector) => new _1.WebSocketContext(identityService, incomingMessage, injector);
this.contextFactory = (identityService, incomingMessage, webSocket, injector) => new _1.WebSocketContext(identityService, incomingMessage, webSocket, injector);
this.activate = () => __awaiter(this, void 0, void 0, function* () { });

@@ -35,23 +35,19 @@ this.dispose = () => __awaiter(this, void 0, void 0, function* () { });

this.resolver = new ActionResolver_1.ActionResolver(this.options.actions);
this.socket.on("connection", (ws, msg) => __awaiter(this, void 0, void 0, function* () {
this.loggers.trace("Connected to WebSocket");
const context = this.contextFactory(this.options.identityService, msg);
this.socket.on("connection", (ws, msg) => {
this.loggers.trace(this.options.logScope, "Client connected to WebSocket");
const context = this.contextFactory(this.options.identityService, msg, ws, this.injector);
ws.on("message", (message) => {
this.resolver.execute(message, context, this.injector, (data) => {
return new Promise((success, reject) => {
ws.send(data, (error) => {
error ? reject(error) : success();
});
});
});
this.loggers.trace(this.options.logScope, "Client Message received");
this.resolver.execute(message, context, this.injector);
});
ws.on("close", () => {
this.loggers.trace("Disconnected");
this.loggers.trace(this.options.logScope, "Client disconnected");
});
}));
this.options.server.on("upgrade", (req, socket, head) => {
const pathname = url_1.parse(req.url).pathname;
});
this.options.server.on("upgrade", (request, socket, head) => {
const pathname = url_1.parse(request.url).pathname;
if (pathname === this.options.path) {
this.socket.handleUpgrade(req, socket, head, (ws) => {
ws.emit("connection", ws, req);
this.socket.handleUpgrade(request, socket, head, (ws) => {
this.loggers.trace(this.options.logScope, `Client connected to socket at '${this.options.path}'.`);
this.socket.emit("connection", ws, request);
});

@@ -58,0 +54,0 @@ }

@@ -6,2 +6,4 @@ /// <reference types="node" />

import { IncomingMessage } from "http";
import { Data } from "ws";
import * as Ws from "ws";
import { IWebSocketContext } from "./models/IWebSocketContext";

@@ -11,3 +13,5 @@ export declare class WebSocketContext implements IWebSocketContext {

private readonly incomingMessage;
private ws;
private readonly injector;
send(data: Data): Promise<void>;
isAuthenticated(): Promise<boolean>;

@@ -18,3 +22,3 @@ isAuthorized(...claims: string[]): Promise<boolean>;

getInjector: () => Injector;
constructor(identityService: IdentityService, incomingMessage: IncomingMessage, injector?: Injector);
constructor(identityService: IdentityService, incomingMessage: IncomingMessage, ws: Ws, injector?: Injector);
}

@@ -14,8 +14,16 @@ "use strict";

class WebSocketContext {
constructor(identityService, incomingMessage, injector = new inject_1.Injector()) {
constructor(identityService, incomingMessage, ws, injector = new inject_1.Injector()) {
this.identityService = identityService;
this.incomingMessage = incomingMessage;
this.ws = ws;
this.injector = injector;
this.getInjector = () => this.injector;
}
send(data) {
return new Promise((success, reject) => {
this.ws.send(data, (error) => {
error ? reject(error) : success();
});
});
}
isAuthenticated() {

@@ -22,0 +30,0 @@ return __awaiter(this, void 0, void 0, function* () {

{
"name": "@furystack/websocket-api",
"version": "0.1.0",
"version": "0.1.1",
"description": "HTTP Api FuryStack package",

@@ -84,2 +84,2 @@ "main": "dist/index.js",

"typings": "./dist/index.d.ts"
}
}

@@ -7,5 +7,5 @@ import { Injector } from "@furystack/inject";

export class ActionResolver {
public execute(data: Data, context: WebSocketContext, injector: Injector, send: (data: any) => Promise<void>) {
public execute(data: Data, context: WebSocketContext, injector: Injector) {
const action = this.actions.find((a) => a.canExecute(data));
action && action.execute(data, context, send);
action && action.execute(data, context, injector);
}

@@ -12,0 +12,0 @@

@@ -0,3 +1,4 @@

import { Injector } from "@furystack/inject";
import { Data } from "ws";
import { WebSocketContext } from "../WebSocketContext";
import { IWebSocketContext } from "./IWebSocketContext";

@@ -8,3 +9,3 @@ export interface IWebSocketAction {

canExecute: (data: Data) => boolean;
execute: (data: Data, context: WebSocketContext, send: (data: any) => Promise<void>) => void;
execute: (data: Data, context: IWebSocketContext, injector: Injector) => void;
}
import { IContext } from "@furystack/core";
import { Data } from "ws";
// tslint:disable-next-line:no-empty-interface
export interface IWebSocketContext extends IContext {
send(data: Data): Promise<void>;
}

@@ -0,7 +1,9 @@

import { Injector } from "@furystack/inject";
import { Data } from "ws";
export abstract class WebSocketAction {
import { IWebSocketAction, IWebSocketContext } from "./models";
export abstract class WebSocketAction implements IWebSocketAction {
public readonly authenticate = false;
public readonly authorize = [];
public abstract canExecute(data: Data): boolean;
public abstract execute(data: Data): any;
public abstract execute(data: Data, context: IWebSocketContext, injector: Injector): any;
}
import { IApi, LoggerCollection } from "@furystack/core";
import { IdentityService } from "@furystack/http-api";
import { Injector } from "@furystack/inject";

@@ -6,4 +7,4 @@ import { IncomingMessage } from "http";

import { Server as WebSocketServer } from "ws";
import * as Ws from "ws";
import { WebSocketContext } from ".";
import { IdentityService } from "../node_modules/@furystack/http-api";
import { ActionResolver } from "./ActionResolver";

@@ -23,3 +24,3 @@ import { IWebSocketApiConfiguration, IWebSocketContext } from "./models";

public injector: Injector = new Injector();
public contextFactory = (identityService: IdentityService, incomingMessage: IncomingMessage, injector?: Injector) => new WebSocketContext(identityService, incomingMessage, injector);
public contextFactory = (identityService: IdentityService, incomingMessage: IncomingMessage, webSocket: Ws, injector?: Injector) => new WebSocketContext(identityService, incomingMessage, webSocket, injector);
public activate: () => Promise<void> = async () => { /** */ };

@@ -34,25 +35,21 @@ public dispose: () => void = async () => { /** */ };

this.resolver = new ActionResolver(this.options.actions);
this.socket.on("connection", async (ws, msg) => {
this.loggers.trace("Connected to WebSocket");
const context = this.contextFactory(this.options.identityService, msg);
this.socket.on("connection", (ws, msg) => {
this.loggers.trace(this.options.logScope, "Client connected to WebSocket");
const context = this.contextFactory(this.options.identityService, msg, ws, this.injector);
ws.on("message", (message) => {
this.resolver.execute(message, context, this.injector, (data) => {
return new Promise((success, reject) => {
ws.send(data, (error) => {
error ? reject(error) : success();
});
});
});
this.loggers.trace(this.options.logScope, "Client Message received");
this.resolver.execute(message, context, this.injector);
});
ws.on("close", () => {
this.loggers.trace("Disconnected");
this.loggers.trace(this.options.logScope, "Client disconnected");
});
});
this.options.server.on("upgrade", (req, socket, head) => {
const pathname = parse(req.url).pathname;
this.options.server.on("upgrade", (request, socket, head) => {
const pathname = parse(request.url).pathname;
if (pathname === this.options.path) {
this.socket.handleUpgrade(req, socket, head, (ws) => {
ws.emit("connection", ws, req);
this.socket.handleUpgrade(request, socket, head, (ws) => {
this.loggers.trace(this.options.logScope, `Client connected to socket at '${this.options.path}'.`);
this.socket.emit("connection", ws, request);
});

@@ -59,0 +56,0 @@ }

@@ -5,5 +5,16 @@ import { IUser, visitorUser } from "@furystack/core";

import { IncomingMessage } from "http";
import { Data } from "ws";
import * as Ws from "ws";
import { IWebSocketContext } from "./models/IWebSocketContext";
export class WebSocketContext implements IWebSocketContext {
public send(data: Data) {
return new Promise<void>((success, reject) => {
this.ws.send(data, (error) => {
error ? reject(error) : success();
});
});
}
public async isAuthenticated(): Promise<boolean> {

@@ -36,3 +47,3 @@ const currentUser = await this.identityService.authenticateRequest(this.incomingMessage);

constructor(private readonly identityService: IdentityService, private readonly incomingMessage: IncomingMessage, private readonly injector = new Injector()) {
constructor(private readonly identityService: IdentityService, private readonly incomingMessage: IncomingMessage, private ws: Ws, private readonly injector = new Injector()) {

@@ -39,0 +50,0 @@ }

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc