@eleven-am/pondsocket-nest 0.0.133 → 0.0.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/constants.d.ts +12 -0
- package/constants.js +2 -1
- package/context/context.d.ts +21 -0
- package/decorators/channel.d.ts +1 -0
- package/decorators/channelInstance.d.ts +1 -0
- package/decorators/endpoint.d.ts +1 -0
- package/decorators/endpointInstance.d.ts +1 -0
- package/decorators/getChannel.d.ts +1 -0
- package/decorators/getChannel.js +2 -8
- package/decorators/getConnectionContext.d.ts +1 -0
- package/decorators/getConnectionContext.js +2 -8
- package/decorators/getConnectionHeaders.d.ts +1 -0
- package/decorators/getConnectionHeaders.js +2 -8
- package/decorators/getContext.d.ts +1 -0
- package/decorators/getEventContext.d.ts +1 -0
- package/decorators/getEventContext.js +2 -8
- package/decorators/getEventParams.d.ts +1 -0
- package/decorators/getEventParams.js +2 -8
- package/decorators/getEventPayload.d.ts +1 -0
- package/decorators/getEventPayload.js +2 -8
- package/decorators/getEventQuery.d.ts +1 -0
- package/decorators/getEventQuery.js +2 -8
- package/decorators/getJoinContext.d.ts +1 -0
- package/decorators/getJoinContext.js +2 -8
- package/decorators/getJoinParams.d.ts +1 -0
- package/decorators/getJoinParams.js +2 -8
- package/decorators/getLeaveEvent.d.ts +1 -0
- package/decorators/getLeaveEvent.js +2 -8
- package/decorators/getUserAssigns.d.ts +1 -0
- package/decorators/getUserAssigns.js +2 -8
- package/decorators/getUserData.d.ts +1 -0
- package/decorators/getUserData.js +2 -8
- package/decorators/getUserPresence.d.ts +1 -0
- package/decorators/getUserPresence.js +2 -8
- package/decorators/index.d.ts +25 -0
- package/decorators/index.js +1 -0
- package/decorators/onConnectionRequest.d.ts +1 -0
- package/decorators/onEvent.d.ts +1 -0
- package/decorators/onJoinRequest.d.ts +1 -0
- package/decorators/onLeave.d.ts +1 -0
- package/decorators/onLeave.js +3 -1
- package/decorators/onOutgoingEvent.d.ts +1 -0
- package/decorators/onOutgoingEvent.js +22 -0
- package/decorators/useGuards.d.ts +2 -0
- package/decorators/usePipes.d.ts +3 -0
- package/helpers/createContextParamDecorator.d.ts +2 -0
- package/helpers/createContextParamDecorator.js +13 -0
- package/helpers/createParamDecorator.d.ts +3 -0
- package/helpers/misc.d.ts +3 -0
- package/helpers/misc.js +2 -1
- package/index.d.ts +5 -287
- package/managers/channel.d.ts +4 -0
- package/managers/channelInstance.d.ts +5 -0
- package/managers/class.d.ts +5 -0
- package/managers/connection.d.ts +5 -0
- package/managers/createMetadataManager.d.ts +8 -0
- package/managers/createMetadataManager.js +36 -0
- package/managers/endpoint.d.ts +4 -0
- package/managers/endpointInstance.d.ts +5 -0
- package/managers/event.d.ts +5 -0
- package/managers/guards.d.ts +6 -0
- package/managers/guards.js +5 -31
- package/managers/handlers.d.ts +5 -0
- package/managers/join.d.ts +5 -0
- package/managers/leave.d.ts +5 -0
- package/managers/method.d.ts +5 -0
- package/managers/outgoingEvent.d.ts +5 -0
- package/managers/outgoingEvent.js +8 -0
- package/managers/parametres.d.ts +9 -0
- package/managers/pipes.d.ts +6 -0
- package/managers/pipes.js +5 -31
- package/managers/property.d.ts +4 -0
- package/modules/pondSocket.d.ts +7 -0
- package/modules/pondSocket.js +2 -3
- package/package.json +26 -12
- package/performers/action.d.ts +5 -0
- package/performers/action.js +7 -4
- package/performers/errors.d.ts +2 -0
- package/performers/errors.js +0 -1
- package/performers/narrow.d.ts +7 -0
- package/performers/response.d.ts +3 -0
- package/performers/response.js +29 -40
- package/services/pondSocket.d.ts +24 -0
- package/services/pondSocket.js +23 -8
- package/types.d.ts +64 -0
package/performers/action.js
CHANGED
|
@@ -42,9 +42,12 @@ function performGuards(moduleRef, globalGuards, context) {
|
|
|
42
42
|
const guards = globalGuards
|
|
43
43
|
.concat(classGuards, methodGuards)
|
|
44
44
|
.map((Guard) => (0, misc_1.retrieveInstance)(moduleRef, Guard));
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
for (const guard of guards) {
|
|
46
|
+
if (!(yield guard.canActivate(context))) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
48
51
|
});
|
|
49
52
|
}
|
|
50
53
|
function getNestContext(context) {
|
|
@@ -78,7 +81,7 @@ function performAction(instance, moduleRef, globalGuards, globalPipes, originalM
|
|
|
78
81
|
const data = yield originalMethod.apply(instance, yield retrieveParameters(context, globalPipes, moduleRef));
|
|
79
82
|
(0, response_1.performResponse)(socketId, channel, data, input);
|
|
80
83
|
}
|
|
81
|
-
else if ((0, narrow_1.isJoinContext)(input) || (0, narrow_1.
|
|
84
|
+
else if ((0, narrow_1.isJoinContext)(input) || (0, narrow_1.isConnectionContext)(input)) {
|
|
82
85
|
input.decline('Unauthorized', 403);
|
|
83
86
|
}
|
|
84
87
|
});
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { ConnectionContext, JoinContext, EventContext } from '@eleven-am/pondsocket/types';
|
|
2
|
+
export declare function performErrors(error: unknown, response: ConnectionContext<string> | JoinContext<string> | EventContext<string>): ConnectionContext<string> | JoinContext<string> | EventContext<string> | undefined;
|
package/performers/errors.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.performErrors = performErrors;
|
|
4
|
-
// eslint-disable-next-line import/no-unresolved
|
|
5
4
|
const common_1 = require("@nestjs/common");
|
|
6
5
|
const narrow_1 = require("./narrow");
|
|
7
6
|
function performErrors(error, response) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { JoinContext, EventContext, ConnectionContext, LeaveEvent } from '@eleven-am/pondsocket/types';
|
|
2
|
+
type Context = JoinContext<string> | EventContext<string> | ConnectionContext<string> | LeaveEvent;
|
|
3
|
+
export declare function isEventContext(response: Context): response is EventContext<string>;
|
|
4
|
+
export declare function isJoinContext(response: Context): response is JoinContext<string>;
|
|
5
|
+
export declare function isConnectionContext(response: Context): response is ConnectionContext<string>;
|
|
6
|
+
export declare function isLeaveEvent(response: Context): response is LeaveEvent;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { Channel, JoinContext, EventContext, ConnectionContext, LeaveEvent } from '@eleven-am/pondsocket/types';
|
|
2
|
+
import { PondResponse } from '../types';
|
|
3
|
+
export declare function performResponse(socketId: string, channel: Channel | null, data: PondResponse | null | undefined, context: JoinContext<string> | EventContext<string> | ConnectionContext<string> | LeaveEvent): void;
|
package/performers/response.js
CHANGED
|
@@ -14,55 +14,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
14
14
|
exports.performResponse = performResponse;
|
|
15
15
|
const narrow_1 = require("./narrow");
|
|
16
16
|
function isNotEmpty(value) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
if (value === null || value === undefined) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'object') {
|
|
21
|
+
return Object.keys(value).length !== 0;
|
|
22
|
+
}
|
|
23
|
+
return value !== '';
|
|
21
24
|
}
|
|
22
25
|
function performResponse(socketId, channel, data, context) {
|
|
23
26
|
if (((0, narrow_1.isLeaveEvent)(context) || !isNotEmpty(data)) || (!(0, narrow_1.isEventContext)(context) && context.hasResponded)) {
|
|
24
27
|
return;
|
|
25
28
|
}
|
|
26
29
|
const { event, presence, assigns, broadcast, broadcastFrom, broadcastTo } = data, rest = __rest(data, ["event", "presence", "assigns", "broadcast", "broadcastFrom", "broadcastTo"]);
|
|
27
|
-
if (context) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
if ((0, narrow_1.isConnectionContext)(context) || (0, narrow_1.isJoinContext)(context)) {
|
|
31
|
+
context
|
|
32
|
+
.assign(typeof assigns === 'object' ? assigns : {})
|
|
33
|
+
.accept();
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
context
|
|
37
|
+
.assign(typeof assigns === 'object' ? assigns : {});
|
|
38
|
+
}
|
|
39
|
+
const payload = isNotEmpty(rest) ? rest : {};
|
|
40
|
+
if (event) {
|
|
41
|
+
context.reply(event, payload);
|
|
42
|
+
}
|
|
43
|
+
if ((0, narrow_1.isJoinContext)(context) || (0, narrow_1.isEventContext)(context)) {
|
|
44
|
+
if (broadcast) {
|
|
45
|
+
context.broadcast(broadcast, payload);
|
|
32
46
|
}
|
|
33
|
-
|
|
34
|
-
context
|
|
35
|
-
.assign(typeof assigns === 'object' ? assigns : {});
|
|
47
|
+
if (broadcastFrom) {
|
|
48
|
+
context.broadcastFrom(broadcastFrom, payload);
|
|
36
49
|
}
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
context.reply(event, rest);
|
|
40
|
-
}
|
|
41
|
-
if ((0, narrow_1.isJoinContext)(context) || (0, narrow_1.isEventContext)(context)) {
|
|
42
|
-
if (broadcast) {
|
|
43
|
-
context.broadcast(broadcast, rest);
|
|
44
|
-
}
|
|
45
|
-
if (broadcastFrom) {
|
|
46
|
-
context.broadcastFrom(broadcastFrom, rest);
|
|
47
|
-
}
|
|
48
|
-
if (broadcastTo) {
|
|
49
|
-
context.broadcastTo(broadcastTo.event, rest, broadcastTo.users);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
50
|
+
if (broadcastTo) {
|
|
51
|
+
context.broadcastTo(broadcastTo.event, payload, broadcastTo.users);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
if (channel) {
|
|
55
|
-
|
|
56
|
-
if (broadcast || event) {
|
|
57
|
-
const newEvent = (broadcast || event);
|
|
58
|
-
channel.broadcast(newEvent, rest);
|
|
59
|
-
}
|
|
60
|
-
if (broadcastTo) {
|
|
61
|
-
channel.broadcastTo(broadcastTo.users, broadcastTo.event, rest);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (isNotEmpty(presence)) {
|
|
65
|
-
channel.upsertPresence(socketId, presence);
|
|
66
|
-
}
|
|
54
|
+
if (channel && isNotEmpty(presence)) {
|
|
55
|
+
channel.upsertPresence(socketId, presence);
|
|
67
56
|
}
|
|
68
57
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { IDistributedBackend } from '@eleven-am/pondsocket/types';
|
|
2
|
+
import { DiscoveryService } from '@golevelup/nestjs-discovery';
|
|
3
|
+
import { OnModuleInit, OnModuleDestroy, PipeTransform } from '@nestjs/common';
|
|
4
|
+
import { HttpAdapterHost, ModuleRef } from '@nestjs/core';
|
|
5
|
+
import { CanActivate, Constructor } from '../types';
|
|
6
|
+
export declare class PondSocketService implements OnModuleInit, OnModuleDestroy {
|
|
7
|
+
private readonly moduleRef;
|
|
8
|
+
private readonly discovery;
|
|
9
|
+
private readonly adapterHost;
|
|
10
|
+
private readonly globalGuards;
|
|
11
|
+
private readonly globalPipes;
|
|
12
|
+
private readonly isExclusiveSocketServer;
|
|
13
|
+
private readonly distributedBackend?;
|
|
14
|
+
private readonly maxMessageSize?;
|
|
15
|
+
private readonly heartbeatInterval?;
|
|
16
|
+
private readonly logger;
|
|
17
|
+
private socket;
|
|
18
|
+
constructor(moduleRef: ModuleRef, discovery: DiscoveryService, adapterHost: HttpAdapterHost, globalGuards: Constructor<CanActivate>[], globalPipes: Constructor<PipeTransform>[], isExclusiveSocketServer: boolean, distributedBackend?: IDistributedBackend | undefined, maxMessageSize?: number | undefined, heartbeatInterval?: number | undefined);
|
|
19
|
+
onModuleInit(): Promise<void>;
|
|
20
|
+
onModuleDestroy(): void;
|
|
21
|
+
private manageEndpoint;
|
|
22
|
+
private manageChannel;
|
|
23
|
+
private getGroupedInstances;
|
|
24
|
+
}
|
package/services/pondSocket.js
CHANGED
|
@@ -8,13 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.PondSocketService = void 0;
|
|
16
|
-
const pondsocket_1 =
|
|
17
|
-
// eslint-disable-next-line import/no-unresolved
|
|
13
|
+
const pondsocket_1 = require("@eleven-am/pondsocket");
|
|
18
14
|
const common_1 = require("@nestjs/common");
|
|
19
15
|
const constants_1 = require("../constants");
|
|
20
16
|
const channel_1 = require("../managers/channel");
|
|
@@ -25,8 +21,9 @@ const endpointInstance_1 = require("../managers/endpointInstance");
|
|
|
25
21
|
const event_1 = require("../managers/event");
|
|
26
22
|
const join_1 = require("../managers/join");
|
|
27
23
|
const leave_1 = require("../managers/leave");
|
|
24
|
+
const outgoingEvent_1 = require("../managers/outgoingEvent");
|
|
28
25
|
class PondSocketService {
|
|
29
|
-
constructor(moduleRef, discovery, adapterHost, globalGuards, globalPipes, isExclusiveSocketServer, distributedBackend) {
|
|
26
|
+
constructor(moduleRef, discovery, adapterHost, globalGuards, globalPipes, isExclusiveSocketServer, distributedBackend, maxMessageSize, heartbeatInterval) {
|
|
30
27
|
this.moduleRef = moduleRef;
|
|
31
28
|
this.discovery = discovery;
|
|
32
29
|
this.adapterHost = adapterHost;
|
|
@@ -34,19 +31,29 @@ class PondSocketService {
|
|
|
34
31
|
this.globalPipes = globalPipes;
|
|
35
32
|
this.isExclusiveSocketServer = isExclusiveSocketServer;
|
|
36
33
|
this.distributedBackend = distributedBackend;
|
|
34
|
+
this.maxMessageSize = maxMessageSize;
|
|
35
|
+
this.heartbeatInterval = heartbeatInterval;
|
|
37
36
|
this.logger = new common_1.Logger(PondSocketService.name);
|
|
37
|
+
this.socket = null;
|
|
38
38
|
}
|
|
39
39
|
onModuleInit() {
|
|
40
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
41
|
const instances = yield this.getGroupedInstances();
|
|
42
|
-
|
|
42
|
+
this.socket = new pondsocket_1.PondSocket({
|
|
43
43
|
server: this.adapterHost.httpAdapter.getHttpServer(),
|
|
44
44
|
exclusiveServer: this.isExclusiveSocketServer,
|
|
45
45
|
distributedBackend: this.distributedBackend,
|
|
46
|
+
maxMessageSize: this.maxMessageSize,
|
|
47
|
+
heartbeatInterval: this.heartbeatInterval,
|
|
46
48
|
});
|
|
47
|
-
instances.forEach((instance) => this.manageEndpoint(socket, instance));
|
|
49
|
+
instances.forEach((instance) => this.manageEndpoint(this.socket, instance));
|
|
48
50
|
});
|
|
49
51
|
}
|
|
52
|
+
onModuleDestroy() {
|
|
53
|
+
var _a;
|
|
54
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
55
|
+
this.socket = null;
|
|
56
|
+
}
|
|
50
57
|
manageEndpoint(socket, groupedInstance) {
|
|
51
58
|
const instance = groupedInstance.endpoint.instance;
|
|
52
59
|
const constructor = instance.constructor;
|
|
@@ -106,6 +113,14 @@ class PondSocketService {
|
|
|
106
113
|
const newEventPath = `${newPath}/${handler.path}`.replace(/\/+/g, '/');
|
|
107
114
|
this.logger.log(`Mapped {${newEventPath}, EVENT}`);
|
|
108
115
|
});
|
|
116
|
+
const { get: getOutgoingHandlers } = (0, outgoingEvent_1.manageOutgoingEvent)(instance);
|
|
117
|
+
getOutgoingHandlers().forEach((handler) => {
|
|
118
|
+
channelInstance.handleOutgoingEvent(handler.path, (context) => __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
yield handler.value(instance, this.moduleRef, this.globalGuards, this.globalPipes, context);
|
|
120
|
+
}));
|
|
121
|
+
const newOutgoingPath = `${newPath}/${handler.path}`.replace(/\/+/g, '/');
|
|
122
|
+
this.logger.log(`Mapped {${newOutgoingPath}, OUTGOING}`);
|
|
123
|
+
});
|
|
109
124
|
const [leaveHandler] = getLeaveHandlers();
|
|
110
125
|
if (leaveHandler) {
|
|
111
126
|
channelInstance.onLeave((event) => __awaiter(this, void 0, void 0, function* () {
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ConnectionContext, EventContext, IDistributedBackend, JoinContext, LeaveEvent, PondAssigns, PondMessage, PondPresence } from '@eleven-am/pondsocket/types';
|
|
2
|
+
import type { DiscoveredClass } from '@golevelup/nestjs-discovery/lib/discovery.interfaces';
|
|
3
|
+
import type { ModuleMetadata, PipeTransform } from '@nestjs/common';
|
|
4
|
+
import type { ModuleRef } from '@nestjs/core';
|
|
5
|
+
import type { Context } from './context/context';
|
|
6
|
+
export interface NestContext {
|
|
7
|
+
connection?: ConnectionContext<string>;
|
|
8
|
+
join?: JoinContext<string>;
|
|
9
|
+
event?: EventContext<string>;
|
|
10
|
+
leave?: LeaveEvent;
|
|
11
|
+
}
|
|
12
|
+
export type ParamDecoratorCallback<Input> = (data: Input, context: Context, type: unknown) => unknown | Promise<unknown>;
|
|
13
|
+
export interface ParamDecoratorMetadata {
|
|
14
|
+
index: number;
|
|
15
|
+
callback: (context: Context, globalPipes: Constructor<PipeTransform>[], moduleRef: ModuleRef) => Promise<unknown>;
|
|
16
|
+
}
|
|
17
|
+
export type HandlerFunction<Context> = (instance: unknown, moduleRef: ModuleRef, globalGuards: Constructor<CanActivate>[], globalPipes: Constructor<PipeTransform>[], ctx: Context) => Promise<void>;
|
|
18
|
+
export type HandlerData<Context> = {
|
|
19
|
+
path: string;
|
|
20
|
+
value: HandlerFunction<Context>;
|
|
21
|
+
};
|
|
22
|
+
export type Constructor<T, Parameters extends any[] = any[]> = new (...args: Parameters) => T;
|
|
23
|
+
export interface CanActivate {
|
|
24
|
+
/**
|
|
25
|
+
* @desc Whether the client can continue with the request
|
|
26
|
+
* @param context - The context of the request
|
|
27
|
+
*/
|
|
28
|
+
canActivate(context: Context): boolean | Promise<boolean>;
|
|
29
|
+
}
|
|
30
|
+
export type GroupedInstances = {
|
|
31
|
+
endpoint: DiscoveredClass;
|
|
32
|
+
channels: DiscoveredClass[];
|
|
33
|
+
};
|
|
34
|
+
export interface Metadata extends Omit<ModuleMetadata, 'controllers'> {
|
|
35
|
+
guards?: Constructor<CanActivate>[];
|
|
36
|
+
pipes?: Constructor<PipeTransform>[];
|
|
37
|
+
isExclusiveSocketServer?: boolean;
|
|
38
|
+
backend?: IDistributedBackend;
|
|
39
|
+
maxMessageSize?: number;
|
|
40
|
+
heartbeatInterval?: number;
|
|
41
|
+
isGlobal?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface AsyncFactoryResult {
|
|
44
|
+
backend?: IDistributedBackend;
|
|
45
|
+
maxMessageSize?: number;
|
|
46
|
+
heartbeatInterval?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface AsyncMetadata extends Omit<Metadata, 'backend' | 'maxMessageSize' | 'heartbeatInterval'> {
|
|
49
|
+
isGlobal?: boolean;
|
|
50
|
+
inject?: any[];
|
|
51
|
+
imports?: any[];
|
|
52
|
+
useFactory: (...args: any[]) => Promise<AsyncFactoryResult> | AsyncFactoryResult;
|
|
53
|
+
}
|
|
54
|
+
export type PondResponse<Event extends string = string, Payload extends PondMessage = PondMessage, Presence extends PondPresence = PondPresence, Assigns extends PondAssigns = PondAssigns> = {
|
|
55
|
+
event?: Event;
|
|
56
|
+
broadcast?: Event;
|
|
57
|
+
broadcastFrom?: Event;
|
|
58
|
+
assigns?: Partial<Assigns>;
|
|
59
|
+
presence?: Partial<Presence>;
|
|
60
|
+
broadcastTo?: {
|
|
61
|
+
event: Event;
|
|
62
|
+
users: string[];
|
|
63
|
+
};
|
|
64
|
+
} & Payload;
|