@furystack/websocket-api
Advanced tools
Comparing version 11.0.0 to 12.0.0
/// <reference path="../../../rest-service/esm/incoming-message-extensions.d.ts" /> | ||
/// <reference path="../../../rest-service/esm/server-response-extensions.d.ts" /> | ||
/// <reference types="node/http.js" /> | ||
import type { Data } from 'ws'; | ||
import type { Data, WebSocket } from 'ws'; | ||
import type { WebSocketAction } from '../models/websocket-action.js'; | ||
@@ -19,6 +19,6 @@ import type { IncomingMessage } from 'http'; | ||
request: IncomingMessage; | ||
socket: WebSocket; | ||
}): Promise<void>; | ||
private readonly httpUserContext; | ||
private readonly websocket; | ||
} | ||
//# sourceMappingURL=whoami.d.ts.map |
@@ -12,3 +12,2 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { Injectable, Injected } from '@furystack/inject'; | ||
import ws from 'ws'; | ||
/** | ||
@@ -27,10 +26,8 @@ * Example action that returns the current user instance | ||
const currentUser = await this.httpUserContext.getCurrentUser(options.request); | ||
this.websocket.send(JSON.stringify({ currentUser })); | ||
options.socket.send(JSON.stringify({ currentUser })); | ||
} | ||
catch (error) { | ||
this.websocket.send(JSON.stringify({ currentUser: null })); | ||
options.socket.send(JSON.stringify({ currentUser: null })); | ||
} | ||
} | ||
httpUserContext; | ||
websocket; | ||
}; | ||
@@ -41,6 +38,2 @@ __decorate([ | ||
], WhoAmI.prototype, "httpUserContext", void 0); | ||
__decorate([ | ||
Injected(ws), | ||
__metadata("design:type", ws) | ||
], WhoAmI.prototype, "websocket", void 0); | ||
WhoAmI = __decorate([ | ||
@@ -47,0 +40,0 @@ Injectable({ lifetime: 'transient' }) |
@@ -28,3 +28,3 @@ import { WhoAmI } from './whoami.js'; | ||
const instance = injector.getInstance(WhoAmI); | ||
await instance.execute({ request, data: '' }); | ||
await instance.execute({ request, data: '', socket: wsMock }); | ||
expect(wsMock.send).toBeCalledWith(JSON.stringify({ currentUser })); | ||
@@ -31,0 +31,0 @@ }); |
@@ -0,3 +1,7 @@ | ||
/// <reference path="../../../rest-service/esm/incoming-message-extensions.d.ts" /> | ||
/// <reference path="../../../rest-service/esm/server-response-extensions.d.ts" /> | ||
/// <reference types="node/http.js" /> | ||
import type { Disposable } from '@furystack/utils'; | ||
import type { Data } from 'ws'; | ||
import type { Data, WebSocket } from 'ws'; | ||
import type { IncomingMessage } from 'http'; | ||
/** | ||
@@ -9,2 +13,4 @@ * Static methods of a WebSocket Action | ||
data: Data; | ||
request: IncomingMessage; | ||
socket: WebSocket; | ||
}): boolean; | ||
@@ -18,4 +24,6 @@ } | ||
data: Data; | ||
request: IncomingMessage; | ||
socket: WebSocket; | ||
}): void; | ||
} | ||
//# sourceMappingURL=websocket-action.d.ts.map |
@@ -6,5 +6,6 @@ /// <reference path="../../rest-service/esm/incoming-message-extensions.d.ts" /> | ||
import { IncomingMessage } from 'http'; | ||
import { Injector } from '@furystack/inject'; | ||
import type { Disposable } from '@furystack/utils'; | ||
import type { Injector } from '@furystack/inject'; | ||
import { type Disposable } from '@furystack/utils'; | ||
import type { Data } from 'ws'; | ||
import type WebSocket from 'ws'; | ||
import ws from 'ws'; | ||
@@ -15,3 +16,3 @@ /** | ||
export declare class WebSocketApi implements Disposable { | ||
readonly socket: ws.Server<typeof ws, typeof IncomingMessage>; | ||
readonly socket: WebSocket.Server<typeof WebSocket, typeof IncomingMessage>; | ||
private clients; | ||
@@ -29,4 +30,4 @@ private readonly settings; | ||
}) => void | Promise<void>): Promise<void>; | ||
execute(data: Data, injector: Injector): void; | ||
execute(data: Data, request: IncomingMessage, injector: Injector, socket: WebSocket): Promise<void>; | ||
} | ||
//# sourceMappingURL=websocket-api.d.ts.map |
@@ -12,4 +12,5 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { IncomingMessage } from 'http'; | ||
import { ServerManager } from '@furystack/rest-service'; | ||
import { Injectable, Injected, Injector } from '@furystack/inject'; | ||
import { HttpUserContext, ServerManager } from '@furystack/rest-service'; | ||
import { Injectable, Injected } from '@furystack/inject'; | ||
import { usingAsync } from '@furystack/utils'; | ||
import { WebSocketServer } from 'ws'; | ||
@@ -19,3 +20,2 @@ import ws from 'ws'; | ||
import { AggregatedError, IdentityContext } from '@furystack/core'; | ||
import { WebsocketUserContext } from './websocket-user-context.js'; | ||
/** | ||
@@ -27,5 +27,2 @@ * A WebSocket API implementation for FuryStack | ||
clients = new Map(); | ||
settings; | ||
serverManager; | ||
injector; | ||
isInitialized = false; | ||
@@ -38,6 +35,6 @@ init() { | ||
connectionInjector.setExplicitInstance(msg, IncomingMessage); | ||
connectionInjector.setExplicitInstance(connectionInjector.getInstance(WebsocketUserContext), IdentityContext); | ||
connectionInjector.setExplicitInstance(connectionInjector.getInstance(HttpUserContext), IdentityContext); | ||
this.clients.set(websocket, { injector: connectionInjector, message: msg, ws: websocket }); | ||
websocket.on('message', (message) => { | ||
this.execute(message, connectionInjector); | ||
this.execute(message, msg, connectionInjector, websocket); | ||
}); | ||
@@ -84,7 +81,8 @@ websocket.on('close', () => { | ||
} | ||
execute(data, injector) { | ||
const action = this.settings.actions.find((a) => a.canExecute({ data })); | ||
if (action) { | ||
const actionInstance = injector.getInstance(action); | ||
actionInstance.execute({ data }); | ||
async execute(data, request, injector, socket) { | ||
const Action = this.settings.actions.find((a) => a.canExecute({ data, request, socket })); | ||
if (Action) { | ||
await usingAsync(injector.getInstance(Action), async (action) => { | ||
await action.execute({ data, request, socket }); | ||
}); | ||
} | ||
@@ -101,6 +99,2 @@ } | ||
], WebSocketApi.prototype, "serverManager", void 0); | ||
__decorate([ | ||
Injected(Injector), | ||
__metadata("design:type", Injector) | ||
], WebSocketApi.prototype, "injector", void 0); | ||
WebSocketApi = __decorate([ | ||
@@ -107,0 +101,0 @@ Injectable({ lifetime: 'scoped' }) |
{ | ||
"name": "@furystack/websocket-api", | ||
"version": "11.0.0", | ||
"version": "12.0.0", | ||
"description": "HTTP Api FuryStack package", | ||
@@ -37,6 +37,6 @@ "type": "module", | ||
"dependencies": { | ||
"@furystack/core": "^14.0.0", | ||
"@furystack/inject": "^10.0.0", | ||
"@furystack/rest-service": "^9.0.0", | ||
"@furystack/utils": "^6.0.0", | ||
"@furystack/core": "^14.0.1", | ||
"@furystack/inject": "^11.0.0", | ||
"@furystack/rest-service": "^9.0.1", | ||
"@furystack/utils": "^6.0.1", | ||
"ws": "^8.16.0" | ||
@@ -46,3 +46,3 @@ }, | ||
"@types/ws": "^8.5.10", | ||
"typescript": "^5.4.2", | ||
"typescript": "^5.4.3", | ||
"vitest": "^1.4.0" | ||
@@ -49,0 +49,0 @@ }, |
@@ -36,4 +36,3 @@ import { WhoAmI } from './whoami.js' | ||
const instance = injector.getInstance(WhoAmI) | ||
await instance.execute({ request, data: '' }) | ||
await instance.execute({ request, data: '', socket: wsMock }) | ||
expect(wsMock.send).toBeCalledWith(JSON.stringify({ currentUser })) | ||
@@ -40,0 +39,0 @@ }) |
import { HttpUserContext } from '@furystack/rest-service' | ||
import { Injectable, Injected } from '@furystack/inject' | ||
import type { Data } from 'ws' | ||
import ws from 'ws' | ||
import type { Data, WebSocket } from 'ws' | ||
import type { WebSocketAction } from '../models/websocket-action.js' | ||
@@ -20,8 +19,8 @@ import type { IncomingMessage } from 'http' | ||
public async execute(options: { data: Data; request: IncomingMessage }) { | ||
public async execute(options: { data: Data; request: IncomingMessage; socket: WebSocket }) { | ||
try { | ||
const currentUser = await this.httpUserContext.getCurrentUser(options.request) | ||
this.websocket.send(JSON.stringify({ currentUser })) | ||
options.socket.send(JSON.stringify({ currentUser })) | ||
} catch (error) { | ||
this.websocket.send(JSON.stringify({ currentUser: null })) | ||
options.socket.send(JSON.stringify({ currentUser: null })) | ||
} | ||
@@ -31,6 +30,3 @@ } | ||
@Injected(HttpUserContext) | ||
private readonly httpUserContext!: HttpUserContext | ||
@Injected(ws) | ||
private readonly websocket!: ws | ||
private declare readonly httpUserContext: HttpUserContext | ||
} |
import type { Disposable } from '@furystack/utils' | ||
import type { Data } from 'ws' | ||
import type { Data, WebSocket } from 'ws' | ||
import type { IncomingMessage } from 'http' | ||
@@ -8,3 +9,3 @@ /** | ||
export interface WebSocketActionStatic { | ||
canExecute(options: { data: Data }): boolean | ||
canExecute(options: { data: Data; request: IncomingMessage; socket: WebSocket }): boolean | ||
} | ||
@@ -16,3 +17,3 @@ | ||
export interface WebSocketAction extends Disposable { | ||
execute(options: { data: Data }): void | ||
execute(options: { data: Data; request: IncomingMessage; socket: WebSocket }): void | ||
} |
import { URL } from 'url' | ||
import type { Socket } from 'net' | ||
import { IncomingMessage } from 'http' | ||
import { ServerManager } from '@furystack/rest-service' | ||
import { Injectable, Injected, Injector } from '@furystack/inject' | ||
import type { Disposable } from '@furystack/utils' | ||
import { HttpUserContext, ServerManager } from '@furystack/rest-service' | ||
import type { Injector } from '@furystack/inject' | ||
import { Injectable, Injected } from '@furystack/inject' | ||
import { usingAsync, type Disposable } from '@furystack/utils' | ||
import type { Data } from 'ws' | ||
import type WebSocket from 'ws' | ||
import { WebSocketServer } from 'ws' | ||
@@ -13,3 +15,2 @@ import ws from 'ws' | ||
import { AggregatedError, IdentityContext } from '@furystack/core' | ||
import { WebsocketUserContext } from './websocket-user-context.js' | ||
@@ -26,9 +27,8 @@ /** | ||
@Injected(WebSocketApiSettings) | ||
private readonly settings!: WebSocketApiSettings | ||
private declare readonly settings: WebSocketApiSettings | ||
@Injected(ServerManager) | ||
private readonly serverManager!: ServerManager | ||
private declare readonly serverManager: ServerManager | ||
@Injected(Injector) | ||
private readonly injector!: Injector | ||
private declare readonly injector: Injector | ||
@@ -42,6 +42,6 @@ private isInitialized = false | ||
connectionInjector.setExplicitInstance(msg, IncomingMessage) | ||
connectionInjector.setExplicitInstance(connectionInjector.getInstance(WebsocketUserContext), IdentityContext) | ||
connectionInjector.setExplicitInstance(connectionInjector.getInstance(HttpUserContext), IdentityContext) | ||
this.clients.set(websocket, { injector: connectionInjector, message: msg, ws: websocket }) | ||
websocket.on('message', (message) => { | ||
this.execute(message, connectionInjector) | ||
this.execute(message, msg, connectionInjector, websocket) | ||
}) | ||
@@ -94,9 +94,10 @@ | ||
public execute(data: Data, injector: Injector) { | ||
const action = this.settings.actions.find((a) => a.canExecute({ data })) | ||
if (action) { | ||
const actionInstance = injector.getInstance<WebSocketAction>(action) | ||
actionInstance.execute({ data }) | ||
public async execute(data: Data, request: IncomingMessage, injector: Injector, socket: WebSocket) { | ||
const Action = this.settings.actions.find((a) => a.canExecute({ data, request, socket })) | ||
if (Action) { | ||
await usingAsync(injector.getInstance<WebSocketAction>(Action), async (action) => { | ||
await action.execute({ data, request, socket }) | ||
}) | ||
} | ||
} | ||
} |
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
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
1
77050
59
826
- Removed@furystack/inject@10.0.0(transitive)
Updated@furystack/core@^14.0.1
Updated@furystack/inject@^11.0.0
Updated@furystack/utils@^6.0.1