@furystack/websocket-api
Advanced tools
Comparing version 1.0.0 to 2.0.1
@@ -1,4 +0,6 @@ | ||
export * from "./WebSocketApi"; | ||
export * from "./models"; | ||
export * from "./WebSocketApi"; | ||
export * from './WebSocketApi'; | ||
export * from './models'; | ||
export * from './WebSocketApi'; | ||
export * from './InjectorExtension'; | ||
import './InjectorExtension'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./WebSocketApi")); | ||
__export(require("./WebSocketApi")); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./WebSocketApi"), exports); | ||
tslib_1.__exportStar(require("./WebSocketApi"), exports); | ||
require("./InjectorExtension"); | ||
//# sourceMappingURL=index.js.map |
@@ -13,7 +13,4 @@ import { Disposable } from '@sensenet/client-utils'; | ||
export interface IWebSocketAction extends Disposable { | ||
new: (...args: any[]) => IWebSocketActionStatic; | ||
authenticate: boolean; | ||
authorize: string[]; | ||
execute(data: Data): void; | ||
} | ||
//# sourceMappingURL=IWebSocketAction.d.ts.map |
/// <reference types="node" /> | ||
import { IApi, LoggerCollection } from '@furystack/core'; | ||
import { Constructable, Injector } from '@furystack/inject'; | ||
import { ServerManager } from '@furystack/core'; | ||
import { Injector } from '@furystack/inject'; | ||
import { LoggerCollection } from '@furystack/logging'; | ||
import { IncomingMessage } from 'http'; | ||
import { Data } from 'ws'; | ||
import * as ws from 'ws'; | ||
import { IWebSocketAction, IWebSocketActionStatic } from './models/IWebSocketAction'; | ||
import ws from 'ws'; | ||
import { WebSocketApiSettings } from './WebSocketApiSettings'; | ||
@@ -12,16 +12,12 @@ /** | ||
*/ | ||
export declare class WebSocketApi implements IApi { | ||
export declare class WebSocketApi { | ||
private readonly logger; | ||
activate(): Promise<void>; | ||
dispose(): Promise<void>; | ||
private settings; | ||
serverManager: ServerManager; | ||
private readonly socket; | ||
private readonly injector; | ||
private readonly logScope; | ||
actions: Array<Constructable<IWebSocketAction> & IWebSocketActionStatic>; | ||
path: string; | ||
private settings; | ||
setup(settings: Partial<WebSocketApiSettings>): void; | ||
constructor(logger: LoggerCollection, parentInjector: Injector); | ||
constructor(logger: LoggerCollection, settings: WebSocketApiSettings, serverManager: ServerManager, parentInjector: Injector); | ||
execute(data: Data, msg: IncomingMessage, websocket: ws): void; | ||
} | ||
//# sourceMappingURL=WebSocketApi.d.ts.map |
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const core_1 = require("@furystack/core"); | ||
const inject_1 = require("@furystack/inject"); | ||
const logging_1 = require("@furystack/logging"); | ||
const client_utils_1 = require("@sensenet/client-utils"); | ||
const http_1 = require("http"); | ||
const url_1 = require("url"); | ||
const ws_1 = require("ws"); | ||
const ws_2 = tslib_1.__importDefault(require("ws")); | ||
const WebSocketApiSettings_1 = require("./WebSocketApiSettings"); | ||
@@ -22,8 +17,12 @@ /** | ||
let WebSocketApi = class WebSocketApi { | ||
constructor(logger, parentInjector) { | ||
constructor(logger, settings, serverManager, parentInjector) { | ||
this.logger = logger; | ||
this.logScope = '@furystack/websocket-api' + this.constructor.name; | ||
this.actions = []; | ||
this.path = '/socket'; | ||
this.settings = WebSocketApiSettings_1.defaultWebSocketApiSettings; | ||
this.settings = settings; | ||
this.serverManager = serverManager; | ||
this.logScope = '@furystack/websocket-api/' + this.constructor.name; | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: 'Initializating WebSocket API', | ||
data: this.settings, | ||
}); | ||
this.socket = new ws_1.Server({ noServer: true }); | ||
@@ -36,3 +35,4 @@ this.injector = parentInjector.createChild({ owner: this }); | ||
data: { | ||
address: msg.connection.address, | ||
url: msg.url, | ||
remoteAddress: msg.socket.remoteAddress, | ||
}, | ||
@@ -45,4 +45,3 @@ }); | ||
data: { | ||
message: message.toString(), | ||
address: msg.connection.address, | ||
message, | ||
}, | ||
@@ -62,33 +61,23 @@ }); | ||
}); | ||
this.settings.server.on('upgrade', (request, socket, head) => { | ||
const pathname = url_1.parse(request.url).pathname; | ||
if (pathname === this.path) { | ||
this.socket.handleUpgrade(request, socket, head, websocket => { | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: `Client connected to socket at '${this.path}'.`, | ||
data: { | ||
path: this.path, | ||
}, | ||
for (const server of this.serverManager.getServers()) { | ||
server.on('upgrade', (request, socket, head) => { | ||
const pathname = url_1.parse(request.url).pathname; | ||
if (pathname === this.settings.path) { | ||
this.socket.handleUpgrade(request, socket, head, websocket => { | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: `Client connected to socket at '${this.settings.path}'.`, | ||
}); | ||
this.socket.emit('connection', websocket, request); | ||
}); | ||
this.socket.emit('connection', websocket, request); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
async activate() { | ||
/** */ | ||
} | ||
async dispose() { | ||
/** */ | ||
} | ||
setup(settings) { | ||
this.settings = { ...this.settings, ...settings }; | ||
} | ||
execute(data, msg, websocket) { | ||
const action = this.actions.find(a => a.canExecute(data)); | ||
const action = this.settings.actions.find(a => a.canExecute(data)); | ||
if (action) { | ||
client_utils_1.usingAsync(this.injector.createChild({ owner: msg }), async (i) => { | ||
i.setExplicitInstance(msg); | ||
i.setExplicitInstance(websocket); | ||
i.setExplicitInstance(msg, http_1.IncomingMessage); | ||
i.setExplicitInstance(websocket, ws_2.default); | ||
const actionInstance = i.getInstance(action); | ||
@@ -100,7 +89,10 @@ actionInstance.execute(data); | ||
}; | ||
WebSocketApi = __decorate([ | ||
inject_1.Injectable(), | ||
__metadata("design:paramtypes", [core_1.LoggerCollection, inject_1.Injector]) | ||
WebSocketApi = tslib_1.__decorate([ | ||
inject_1.Injectable({ lifetime: 'scoped' }), | ||
tslib_1.__metadata("design:paramtypes", [logging_1.LoggerCollection, | ||
WebSocketApiSettings_1.WebSocketApiSettings, | ||
core_1.ServerManager, | ||
inject_1.Injector]) | ||
], WebSocketApi); | ||
exports.WebSocketApi = WebSocketApi; | ||
//# sourceMappingURL=WebSocketApi.js.map |
@@ -1,16 +0,10 @@ | ||
/// <reference types="node" /> | ||
import { Constructable } from '@furystack/inject'; | ||
import { Server } from 'net'; | ||
import { IWebSocketAction, IWebSocketActionStatic } from '.'; | ||
/** | ||
* A configuration object for FuryStack WebSocket API | ||
*/ | ||
export interface WebSocketApiSettings { | ||
export declare class WebSocketApiSettings { | ||
path: string; | ||
server: Server; | ||
perActionServices: Array<Constructable<any>>; | ||
actions: Array<Constructable<IWebSocketAction> & IWebSocketActionStatic>; | ||
} | ||
/** | ||
* default settings for WebSocket API | ||
*/ | ||
export declare const defaultWebSocketApiSettings: WebSocketApiSettings; | ||
//# sourceMappingURL=WebSocketApiSettings.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http_api_1 = require("@furystack/http-api"); | ||
const http_1 = require("http"); | ||
const Whoami_1 = require("./Actions/Whoami"); | ||
/** | ||
* default settings for WebSocket API | ||
* A configuration object for FuryStack WebSocket API | ||
*/ | ||
exports.defaultWebSocketApiSettings = { | ||
path: '/socket', | ||
server: http_1.createServer(), | ||
perActionServices: [http_api_1.HttpUserContext], | ||
}; | ||
class WebSocketApiSettings { | ||
constructor() { | ||
this.path = '/socket'; | ||
this.actions = [Whoami_1.WhoAmI]; | ||
} | ||
} | ||
exports.WebSocketApiSettings = WebSocketApiSettings; | ||
//# sourceMappingURL=WebSocketApiSettings.js.map |
{ | ||
"name": "@furystack/websocket-api", | ||
"version": "1.0.0", | ||
"version": "2.0.1", | ||
"description": "HTTP Api FuryStack package", | ||
@@ -58,16 +58,17 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@furystack/core": "^2.0.0", | ||
"@furystack/http-api": "^1.0.0", | ||
"@furystack/inject": "^2.0.0", | ||
"@sensenet/client-utils": "^1.2.1", | ||
"ws": "^6.0.0" | ||
"@furystack/core": "^4.0.0", | ||
"@furystack/http-api": "^3.0.0", | ||
"@furystack/inject": "^3.0.1", | ||
"@furystack/logging": "^1.0.0", | ||
"@sensenet/client-utils": "^1.5.1", | ||
"ws": "6.1.4" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^23.3.11", | ||
"@types/ws": "^6.0.1", | ||
"jest": "^23.6.0", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^23.10.4", | ||
"tslint": "^5.11.0", | ||
"typescript": "^3.1.6" | ||
"@types/jest": "24.0.9", | ||
"@types/ws": "6.0.1", | ||
"jest": "24.1.0", | ||
"rimraf": "2.6.3", | ||
"ts-jest": "24.0.0", | ||
"tslint": "5.13.0", | ||
"typescript": "^3.4.3" | ||
}, | ||
@@ -80,3 +81,3 @@ "config": { | ||
"typings": "./dist/index.d.ts", | ||
"gitHead": "d0452632dfb2b8de2dcb1c47d88fff62997c738d" | ||
"gitHead": "2fa8429190fca702d037ea9da26cdc9ded16a8de" | ||
} |
@@ -1,3 +0,5 @@ | ||
export * from "./WebSocketApi"; | ||
export * from "./models"; | ||
export * from "./WebSocketApi"; | ||
export * from './WebSocketApi' | ||
export * from './models' | ||
export * from './WebSocketApi' | ||
export * from './InjectorExtension' | ||
import './InjectorExtension' |
@@ -15,6 +15,3 @@ import { Disposable } from '@sensenet/client-utils' | ||
export interface IWebSocketAction extends Disposable { | ||
new: (...args: any[]) => IWebSocketActionStatic | ||
authenticate: boolean | ||
authorize: string[] | ||
execute(data: Data): void | ||
} |
@@ -1,3 +0,4 @@ | ||
import { IApi, LoggerCollection } from '@furystack/core' | ||
import { Constructable, Injectable, Injector } from '@furystack/inject' | ||
import { ServerManager } from '@furystack/core' | ||
import { Injectable, Injector } from '@furystack/inject' | ||
import { LoggerCollection } from '@furystack/logging' | ||
import { usingAsync } from '@sensenet/client-utils' | ||
@@ -7,5 +8,5 @@ import { IncomingMessage } from 'http' | ||
import { Data, Server as WebSocketServer } from 'ws' | ||
import * as ws from 'ws' | ||
import { IWebSocketAction, IWebSocketActionStatic } from './models/IWebSocketAction' | ||
import { defaultWebSocketApiSettings, WebSocketApiSettings } from './WebSocketApiSettings' | ||
import ws from 'ws' | ||
import { IWebSocketAction } from './models/IWebSocketAction' | ||
import { WebSocketApiSettings } from './WebSocketApiSettings' | ||
@@ -15,24 +16,18 @@ /** | ||
*/ | ||
@Injectable() | ||
export class WebSocketApi implements IApi { | ||
public async activate() { | ||
/** */ | ||
} | ||
public async dispose() { | ||
/** */ | ||
} | ||
@Injectable({ lifetime: 'scoped' }) | ||
export class WebSocketApi { | ||
private readonly socket: WebSocketServer | ||
private readonly injector: Injector | ||
private readonly logScope: string = '@furystack/websocket-api' + this.constructor.name | ||
public actions: Array<Constructable<IWebSocketAction> & IWebSocketActionStatic> = [] | ||
public path: string = '/socket' | ||
private settings: WebSocketApiSettings = defaultWebSocketApiSettings | ||
public setup(settings: Partial<WebSocketApiSettings>) { | ||
this.settings = { ...this.settings, ...settings } | ||
} | ||
constructor(private readonly logger: LoggerCollection, parentInjector: Injector) { | ||
private readonly logScope: string = '@furystack/websocket-api/' + this.constructor.name | ||
constructor( | ||
private readonly logger: LoggerCollection, | ||
private settings: WebSocketApiSettings, | ||
public serverManager: ServerManager, | ||
parentInjector: Injector, | ||
) { | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: 'Initializating WebSocket API', | ||
data: this.settings, | ||
}) | ||
this.socket = new WebSocketServer({ noServer: true }) | ||
@@ -45,3 +40,4 @@ this.injector = parentInjector.createChild({ owner: this }) | ||
data: { | ||
address: msg.connection.address, | ||
url: msg.url, | ||
remoteAddress: msg.socket.remoteAddress, | ||
}, | ||
@@ -54,4 +50,3 @@ }) | ||
data: { | ||
message: message.toString(), | ||
address: msg.connection.address, | ||
message, | ||
}, | ||
@@ -73,25 +68,24 @@ }) | ||
this.settings.server.on('upgrade', (request, socket, head) => { | ||
const pathname = parse(request.url).pathname | ||
if (pathname === this.path) { | ||
this.socket.handleUpgrade(request, socket, head, websocket => { | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: `Client connected to socket at '${this.path}'.`, | ||
data: { | ||
path: this.path, | ||
}, | ||
for (const server of this.serverManager.getServers()) { | ||
server.on('upgrade', (request, socket, head) => { | ||
const pathname = parse(request.url).pathname | ||
if (pathname === this.settings.path) { | ||
this.socket.handleUpgrade(request, socket, head, websocket => { | ||
this.logger.verbose({ | ||
scope: this.logScope, | ||
message: `Client connected to socket at '${this.settings.path}'.`, | ||
}) | ||
this.socket.emit('connection', websocket, request) | ||
}) | ||
this.socket.emit('connection', websocket, request) | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
public execute(data: Data, msg: IncomingMessage, websocket: ws) { | ||
const action = this.actions.find(a => a.canExecute(data)) | ||
const action = this.settings.actions.find(a => a.canExecute(data)) | ||
if (action) { | ||
usingAsync(this.injector.createChild({ owner: msg }), async i => { | ||
i.setExplicitInstance(msg) | ||
i.setExplicitInstance(websocket) | ||
i.setExplicitInstance(msg, IncomingMessage) | ||
i.setExplicitInstance(websocket, ws) | ||
const actionInstance = i.getInstance<IWebSocketAction>(action) | ||
@@ -98,0 +92,0 @@ actionInstance.execute(data) |
@@ -1,5 +0,4 @@ | ||
import { HttpUserContext } from '@furystack/http-api' | ||
import { Constructable } from '@furystack/inject' | ||
import { createServer } from 'http' | ||
import { Server } from 'net' | ||
import { IWebSocketAction, IWebSocketActionStatic } from '.' | ||
import { WhoAmI } from './Actions/Whoami' | ||
@@ -9,15 +8,5 @@ /** | ||
*/ | ||
export interface WebSocketApiSettings { | ||
path: string | ||
server: Server | ||
perActionServices: Array<Constructable<any>> | ||
export class WebSocketApiSettings { | ||
public path: string = '/socket' | ||
public actions: Array<Constructable<IWebSocketAction> & IWebSocketActionStatic> = [WhoAmI] | ||
} | ||
/** | ||
* default settings for WebSocket API | ||
*/ | ||
export const defaultWebSocketApiSettings: WebSocketApiSettings = { | ||
path: '/socket', | ||
server: createServer(), | ||
perActionServices: [HttpUserContext], | ||
} |
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
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2
43137
6
38
388
+ Added@furystack/logging@^1.0.0
+ Added@furystack/core@4.0.8(transitive)
+ Added@furystack/http-api@3.1.4(transitive)
+ Added@furystack/inject@3.0.8(transitive)
+ Added@furystack/logging@1.1.7(transitive)
+ Addedws@6.1.4(transitive)
- Removed@furystack/core@2.0.0(transitive)
- Removed@furystack/http-api@1.0.0(transitive)
- Removed@furystack/inject@2.0.0(transitive)
- Removed@types/node@10.17.60(transitive)
- Removedws@6.2.3(transitive)
Updated@furystack/core@^4.0.0
Updated@furystack/http-api@^3.0.0
Updated@furystack/inject@^3.0.1
Updatedws@6.1.4