Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@furystack/websocket-api

Package Overview
Dependencies
Maintainers
1
Versions
235
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 12.0.3 to 12.0.4

94

CHANGELOG.md

@@ -10,7 +10,2 @@ # Change Log

### [7.0.61](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.60...@furystack/websocket-api@7.0.61) (2022-02-02)

@@ -20,7 +15,2 @@

### [7.0.60](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.58...@furystack/websocket-api@7.0.60) (2022-01-10)

@@ -30,7 +20,2 @@

### [7.0.59](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.58...@furystack/websocket-api@7.0.59) (2022-01-10)

@@ -40,7 +25,2 @@

### [7.0.58](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.57...@furystack/websocket-api@7.0.58) (2021-12-20)

@@ -50,7 +30,2 @@

### [7.0.57](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.56...@furystack/websocket-api@7.0.57) (2021-12-08)

@@ -60,7 +35,2 @@

### [7.0.56](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.55...@furystack/websocket-api@7.0.56) (2021-11-30)

@@ -70,7 +40,2 @@

### [7.0.55](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.54...@furystack/websocket-api@7.0.55) (2021-11-29)

@@ -80,7 +45,2 @@

### [7.0.54](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.53...@furystack/websocket-api@7.0.54) (2021-11-19)

@@ -90,7 +50,2 @@

### [7.0.53](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.52...@furystack/websocket-api@7.0.53) (2021-11-17)

@@ -100,7 +55,2 @@

### [7.0.52](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.51...@furystack/websocket-api@7.0.52) (2021-11-09)

@@ -110,7 +60,2 @@

### [7.0.51](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.50...@furystack/websocket-api@7.0.51) (2021-10-15)

@@ -120,7 +65,2 @@

### [7.0.50](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.49...@furystack/websocket-api@7.0.50) (2021-10-05)

@@ -130,7 +70,2 @@

### [7.0.49](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.48...@furystack/websocket-api@7.0.49) (2021-09-16)

@@ -140,7 +75,2 @@

### [7.0.48](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.47...@furystack/websocket-api@7.0.48) (2021-08-27)

@@ -150,7 +80,2 @@

### [7.0.47](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.46...@furystack/websocket-api@7.0.47) (2021-08-25)

@@ -160,7 +85,2 @@

### [7.0.46](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.45...@furystack/websocket-api@7.0.46) (2021-08-19)

@@ -170,22 +90,12 @@

### [7.0.45](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.15...@furystack/websocket-api@7.0.45) (2021-08-19)
### 🐛 Bug Fixes
* **websocket:** open constant ([94e10ff](https://github.com/furystack/furystack/commit/94e10ffb564876e0e8b77078db5d6b6673e83c74))
- **websocket:** open constant ([94e10ff](https://github.com/furystack/furystack/commit/94e10ffb564876e0e8b77078db5d6b6673e83c74))
### [7.0.44](https://github.com/furystack/furystack/compare/@furystack/websocket-api@7.0.15...@furystack/websocket-api@7.0.44) (2021-07-30)
### 🐛 Bug Fixes
* **websocket:** open constant ([94e10ff](https://github.com/furystack/furystack/commit/94e10ffb564876e0e8b77078db5d6b6673e83c74))
- **websocket:** open constant ([94e10ff](https://github.com/furystack/furystack/commit/94e10ffb564876e0e8b77078db5d6b6673e83c74))

9

esm/actions/whoami.js

@@ -20,3 +20,10 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

static canExecute(options) {
return options.data.toString() === 'whoami' || options.data.toString() === 'whoami /claims';
const stringifiedValue = typeof options.data === 'string'
? options.data
: options.data instanceof Buffer
? options.data.toString()
: options.data instanceof ArrayBuffer
? Buffer.from(options.data).toString()
: options.data.map((buf) => buf.toString()).join('');
return stringifiedValue === 'whoami' || stringifiedValue === 'whoami /claims';
}

@@ -23,0 +30,0 @@ async execute(options) {

@@ -1,2 +0,2 @@

import { usingAsync } from '@furystack/utils';
import { using } from '@furystack/utils';
import { Injector } from '@furystack/inject';

@@ -8,4 +8,4 @@ import { useWebsockets } from './helpers.js';

describe('WebSocket Helpers', () => {
it('Should register the settings', async () => {
await usingAsync(new Injector(), async (i) => {
it('Should register the settings', () => {
using(new Injector(), (i) => {
const port = getPort();

@@ -12,0 +12,0 @@ useWebsockets(i, { port });

@@ -21,3 +21,3 @@ /// <reference path="../../rest-service/esm/incoming-message-extensions.d.ts" />

private isInitialized;
init(): void;
init(): Promise<void>;
dispose(): Promise<void>;

@@ -29,4 +29,4 @@ broadcast(callback: (options: {

}) => void | Promise<void>): Promise<void>;
execute(data: Data, request: IncomingMessage, injector: Injector, socket: WebSocket): Promise<void>;
execute(data: Data, request: IncomingMessage, injector: Injector, socket: WebSocket): void;
}
//# sourceMappingURL=websocket-api.d.ts.map

@@ -14,3 +14,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

import { Injectable, Injected } from '@furystack/inject';
import { usingAsync } from '@furystack/utils';
import { using } from '@furystack/utils';
import { WebSocketServer } from 'ws';

@@ -27,3 +27,3 @@ import ws from 'ws';

isInitialized = false;
init() {
async init() {
if (!this.isInitialized) {

@@ -48,3 +48,5 @@ this.socket.on('connection', (websocket, msg) => {

});
this.serverManager.getOrCreate({ port: this.settings.port, hostName: this.settings.host }).then((server) => {
await this.serverManager
.getOrCreate({ port: this.settings.port, hostName: this.settings.host })
.then((server) => {
server.server.on('upgrade', (request, socket, head) => {

@@ -85,7 +87,7 @@ const { pathname } = new URL(request.url, `http://${request.headers.host}`);

}
async execute(data, request, injector, socket) {
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 });
using(injector.getInstance(Action), (action) => {
action.execute({ data, request, socket });
});

@@ -92,0 +94,0 @@ }

@@ -19,3 +19,3 @@ import { Injector } from '@furystack/inject';

port = getPort();
useRestService({
await useRestService({
injector: i,

@@ -30,3 +30,3 @@ api: {},

useWebsockets(i, { actions: [WhoAmI], path, port, host });
await new Promise((done, reject) => {
await new Promise((resolve, reject) => {
i.getInstance(ServerManager)

@@ -38,6 +38,7 @@ .getOrCreate({ port })

.on('open', () => {
done();
resolve();
})
.on('error', reject);
});
})
.catch(reject);
});

@@ -65,3 +66,3 @@ });

const userStore = i.getInstance(StoreManager).getStoreFor(User, 'username');
userStore.add(testUser);
await userStore.add(testUser);
const userCtx = i.getInstance(HttpUserContext);

@@ -72,3 +73,2 @@ let cookie = '';

cookie = cookieValue;
return {};
},

@@ -86,3 +86,3 @@ });

expect(whoAmIResult.currentUser).toEqual(testUser);
userStore.update(testUser.username, { ...testUser, roles: ['newFancyRole'] });
await userStore.update(testUser.username, { ...testUser, roles: ['newFancyRole'] });
const updatedWhoAmIResult = await getWhoAmIResult(authenticatedClient);

@@ -89,0 +89,0 @@ expect(updatedWhoAmIResult.currentUser.roles).toEqual(['newFancyRole']);

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

@@ -37,6 +37,6 @@ "type": "module",

"dependencies": {
"@furystack/core": "^14.0.3",
"@furystack/inject": "^11.0.2",
"@furystack/rest-service": "^9.0.3",
"@furystack/utils": "^7.0.1",
"@furystack/core": "^14.0.4",
"@furystack/inject": "^11.0.3",
"@furystack/rest-service": "^9.0.4",
"@furystack/utils": "^7.0.2",
"ws": "^8.17.0"

@@ -43,0 +43,0 @@ },

@@ -10,7 +10,6 @@ # websocket-api

```ts
const myInjector = new Injector()
.useWebsockets({
path: '/api/sockets',
actions: [WhoAmI],
})
const myInjector = new Injector().useWebsockets({
path: '/api/sockets',
actions: [WhoAmI],
})
```

@@ -44,4 +43,7 @@

constructor(private httpUserContext: HttpUserContext<User>, private websocket: ws) {}
constructor(
private httpUserContext: HttpUserContext<User>,
private websocket: ws,
) {}
}
```

@@ -15,5 +15,5 @@ import { WhoAmI } from './whoami.js'

const wsMock: ws = {
const wsMock = {
send: vi.fn(() => undefined),
} as unknown as ws
} as unknown as ws & { send: (this: void, ...args: unknown[]) => void }

@@ -20,0 +20,0 @@ it('cannot be executed if data does not match', () => {

@@ -16,3 +16,12 @@ import { HttpUserContext } from '@furystack/rest-service'

public static canExecute(options: { data: Data; request: IncomingMessage }): boolean {
return options.data.toString() === 'whoami' || options.data.toString() === 'whoami /claims'
const stringifiedValue =
typeof options.data === 'string'
? options.data
: options.data instanceof Buffer
? options.data.toString()
: options.data instanceof ArrayBuffer
? Buffer.from(options.data).toString()
: options.data.map((buf) => buf.toString()).join('')
return stringifiedValue === 'whoami' || stringifiedValue === 'whoami /claims'
}

@@ -19,0 +28,0 @@

@@ -1,2 +0,2 @@

import { usingAsync } from '@furystack/utils'
import { using } from '@furystack/utils'
import { Injector } from '@furystack/inject'

@@ -9,4 +9,4 @@ import { useWebsockets } from './helpers.js'

describe('WebSocket Helpers', () => {
it('Should register the settings', async () => {
await usingAsync(new Injector(), async (i) => {
it('Should register the settings', () => {
using(new Injector(), (i) => {
const port = getPort()

@@ -13,0 +13,0 @@ useWebsockets(i, { port })

import { Injector, Injectable } from '@furystack/inject'
import { usingAsync } from '@furystack/utils'
import { WebSocketApi } from './websocket-api.js'
import { WebSocket } from 'ws'
import { WebSocket, type Data } from 'ws'
import type { WebSocketAction } from './models/websocket-action.js'

@@ -82,3 +82,3 @@ import { useWebsockets } from './helpers.js'

public async execute(incomingData: any) {
public async execute(incomingData: { data: Data }) {
expect(JSON.parse(incomingData.data.toString())).toEqual(data)

@@ -85,0 +85,0 @@ }

@@ -7,3 +7,3 @@ import { URL } from 'url'

import { Injectable, Injected } from '@furystack/inject'
import { usingAsync, type Disposable } from '@furystack/utils'
import { using, type Disposable } from '@furystack/utils'
import type { Data } from 'ws'

@@ -35,3 +35,3 @@ import type WebSocket from 'ws'

private isInitialized = false
public init() {
public async init() {
if (!this.isInitialized) {

@@ -63,12 +63,14 @@ this.socket.on('connection', (websocket, msg) => {

this.serverManager.getOrCreate({ port: this.settings.port, hostName: this.settings.host }).then((server) => {
server.server.on('upgrade', (request: IncomingMessage, socket: Socket, head: Buffer) => {
const { pathname } = new URL(request.url as string, `http://${request.headers.host}`)
if (pathname === this.settings.path) {
this.socket.handleUpgrade(request, socket, head, (websocket) => {
this.socket.emit('connection', websocket, request)
})
}
await this.serverManager
.getOrCreate({ port: this.settings.port, hostName: this.settings.host })
.then((server) => {
server.server.on('upgrade', (request: IncomingMessage, socket: Socket, head: Buffer) => {
const { pathname } = new URL(request.url as string, `http://${request.headers.host}`)
if (pathname === this.settings.path) {
this.socket.handleUpgrade(request, socket, head, (websocket) => {
this.socket.emit('connection', websocket, request)
})
}
})
})
})
} else {

@@ -104,7 +106,7 @@ throw Error('WebSocket API is already initialized')

public async execute(data: Data, request: IncomingMessage, injector: Injector, socket: WebSocket) {
public 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 })
using(injector.getInstance<WebSocketAction>(Action), (action) => {
action.execute({ data, request, socket })
})

@@ -111,0 +113,0 @@ }

@@ -21,3 +21,3 @@ import { Injector } from '@furystack/inject'

port = getPort()
useRestService({
await useRestService({
injector: i,

@@ -35,3 +35,3 @@ api: {},

await new Promise<void>((done, reject) => {
await new Promise<void>((resolve, reject) => {
i.getInstance(ServerManager)

@@ -43,6 +43,7 @@ .getOrCreate({ port })

.on('open', () => {
done()
resolve()
})
.on('error', reject)
})
.catch(reject)
})

@@ -56,4 +57,4 @@ })

return new Promise<{ currentUser: User }>((resolve, reject) => {
subjectClient.once('message', (data: any) => {
resolve(JSON.parse(data.toString()))
subjectClient.once('message', (data: Buffer) => {
resolve(JSON.parse(data.toString()) as { currentUser: User })
})

@@ -75,3 +76,3 @@ subjectClient.once('error', reject)

const userStore = i.getInstance(StoreManager).getStoreFor(User, 'username')
userStore.add(testUser)
await userStore.add(testUser)

@@ -83,4 +84,3 @@ const userCtx = i.getInstance(HttpUserContext)

setHeader: (_setCookie, cookieValue) => {
cookie = cookieValue as string
return {} as any
cookie = cookieValue
},

@@ -100,3 +100,3 @@ })

userStore.update(testUser.username, { ...testUser, roles: ['newFancyRole'] })
await userStore.update(testUser.username, { ...testUser, roles: ['newFancyRole'] })

@@ -103,0 +103,0 @@ const updatedWhoAmIResult = await getWhoAmIResult(authenticatedClient)

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

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