@eleven-am/pondsocket 0.1.57 → 0.1.58
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/package.json +3 -3
- package/.eslintrc.json +0 -387
- package/dist/LICENSE +0 -674
- package/dist/README.md +0 -139
- package/dist/package.json +0 -51
- package/jest.config.js +0 -11
- package/src/abstracts/abstractRequest.test.ts +0 -49
- package/src/abstracts/abstractRequest.ts +0 -56
- package/src/abstracts/abstractResponse.ts +0 -26
- package/src/abstracts/middleware.test.ts +0 -75
- package/src/abstracts/middleware.ts +0 -50
- package/src/channel/channel.test.ts +0 -501
- package/src/channel/channel.ts +0 -305
- package/src/channel/eventRequest.test.ts +0 -37
- package/src/channel/eventRequest.ts +0 -27
- package/src/channel/eventResponse.test.ts +0 -249
- package/src/channel/eventResponse.ts +0 -172
- package/src/client/channel.test.ts +0 -799
- package/src/client/channel.ts +0 -342
- package/src/client.ts +0 -124
- package/src/endpoint/endpoint.test.ts +0 -825
- package/src/endpoint/endpoint.ts +0 -304
- package/src/endpoint/response.ts +0 -106
- package/src/enums.ts +0 -52
- package/src/errors/pondError.ts +0 -32
- package/src/express.ts +0 -58
- package/src/index.ts +0 -3
- package/src/lobby/JoinRequest.test.ts +0 -48
- package/src/lobby/JoinResponse.test.ts +0 -162
- package/src/lobby/joinRequest.ts +0 -32
- package/src/lobby/joinResponse.ts +0 -146
- package/src/lobby/lobby.ts +0 -182
- package/src/matcher/matcher.test.ts +0 -103
- package/src/matcher/matcher.ts +0 -105
- package/src/node.ts +0 -33
- package/src/presence/presence.ts +0 -127
- package/src/presence/presenceEngine.test.ts +0 -143
- package/src/server/pondSocket.ts +0 -153
- package/src/subjects/subject.test.ts +0 -163
- package/src/subjects/subject.ts +0 -137
- package/src/typedefs.d.ts +0 -451
- package/src/types.d.ts +0 -89
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -12
- /package/{dist/abstracts → abstracts}/abstractRequest.js +0 -0
- /package/{dist/abstracts → abstracts}/abstractResponse.js +0 -0
- /package/{dist/abstracts → abstracts}/middleware.js +0 -0
- /package/{dist/channel → channel}/channel.js +0 -0
- /package/{dist/channel → channel}/eventRequest.js +0 -0
- /package/{dist/channel → channel}/eventResponse.js +0 -0
- /package/{dist/client → client}/channel.js +0 -0
- /package/{dist/client.d.ts → client.d.ts} +0 -0
- /package/{dist/client.js → client.js} +0 -0
- /package/{dist/endpoint → endpoint}/endpoint.js +0 -0
- /package/{dist/endpoint → endpoint}/response.js +0 -0
- /package/{dist/enums.js → enums.js} +0 -0
- /package/{dist/errors → errors}/pondError.js +0 -0
- /package/{dist/express.d.ts → express.d.ts} +0 -0
- /package/{dist/express.js → express.js} +0 -0
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/lobby → lobby}/joinRequest.js +0 -0
- /package/{dist/lobby → lobby}/joinResponse.js +0 -0
- /package/{dist/lobby → lobby}/lobby.js +0 -0
- /package/{dist/matcher → matcher}/matcher.js +0 -0
- /package/{dist/node.d.ts → node.d.ts} +0 -0
- /package/{dist/node.js → node.js} +0 -0
- /package/{dist/presence → presence}/presence.js +0 -0
- /package/{dist/server → server}/pondSocket.js +0 -0
- /package/{dist/subjects → subjects}/subject.js +0 -0
- /package/{dist/types.d.ts → types.d.ts} +0 -0
package/dist/README.md
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
# PondSocket
|
|
3
|
-
|
|
4
|
-
PondSocket is a fast, minimalist and bidirectional socket framework for NodeJS. Pond allows you to think of each action during a sockets lifetime as a request instead of a huge callback that exists inside the connection event.
|
|
5
|
-
## Documentation
|
|
6
|
-
|
|
7
|
-
This is a Node.js module available through the npm registry.
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @eleven-am/pondsocket
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
PondSocket usage depends on the environment in which it is being used.
|
|
14
|
-
|
|
15
|
-
#### On the server
|
|
16
|
-
|
|
17
|
-
When using PondSocket, an endpoint is created. The endpoint is the gateway by which sockets actually connect to the server.
|
|
18
|
-
Multiple endpoints can be created but every endpoint is independent of the other, ie sockets on one endpoint cannot communicate with sockets on another endpoint.
|
|
19
|
-
|
|
20
|
-
```js
|
|
21
|
-
import { PondSocket } from "@eleven-am/pondsocket";
|
|
22
|
-
|
|
23
|
-
const pond = new PondSocket();
|
|
24
|
-
|
|
25
|
-
const endpoint = pond.createEndpoint('/api/socket', (req, res, _endpoint) => {
|
|
26
|
-
const token = req.query.token;
|
|
27
|
-
if (!token)
|
|
28
|
-
return res.reject('No token provided');
|
|
29
|
-
res.accept({
|
|
30
|
-
assign: {
|
|
31
|
-
token
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
})
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
While sockets connect through the endpoint, communication between sockets cannot occur on the endpoint level. Sockets have to join a channel to communicate
|
|
38
|
-
between themselves.
|
|
39
|
-
|
|
40
|
-
```js
|
|
41
|
-
const channel = endpoint.createChannel(/^channel(.*?)/, (req, res, channel) => {
|
|
42
|
-
const isAdmin = req.clientAssigns.admin;
|
|
43
|
-
if (!isAdmin)
|
|
44
|
-
return res.reject('You are not an admin');
|
|
45
|
-
|
|
46
|
-
res.accept({
|
|
47
|
-
assign: {
|
|
48
|
-
admin: true,
|
|
49
|
-
joinedDate: new Date()
|
|
50
|
-
},
|
|
51
|
-
presence: {
|
|
52
|
-
state: 'online'
|
|
53
|
-
},
|
|
54
|
-
channelData: {
|
|
55
|
-
locked: true,
|
|
56
|
-
numberOfUsers: channel.presence.length
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
A user goes through the createChannel function to join a channel.
|
|
63
|
-
When a user joins a channel, some private information can be assigned to the user. This assign could be viewed as a cookie that is only available serverside.
|
|
64
|
-
The presence is the current state of the user. When you reassign a new presence information to a user, all other users connected to the same channel are informed of the change.
|
|
65
|
-
This could be used as *user is typing*, *user is away*, etc. The channelData is information that is stored on the channel and accessible from anywhere the channel is available.
|
|
66
|
-
It can be anything from a boolean to an instance of a class. This data cannot be accessed from another channel as it is private to the channel.
|
|
67
|
-
|
|
68
|
-
```js
|
|
69
|
-
channel.on('hello', (req, res, channel) => {
|
|
70
|
-
const users = channel.presence;
|
|
71
|
-
res.assign({
|
|
72
|
-
assign: {
|
|
73
|
-
pingDate: new Date(),
|
|
74
|
-
users: users.length
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// res.reject('curse words are not allowed on a child friendly channel')
|
|
79
|
-
// channel.closeFromChannel(req.client.clientId);
|
|
80
|
-
})
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
When a message is sent on a channel by a user, an event is triggered. The *on* function can be used to listen for these events. If the function is specified, it is called when the message is received.
|
|
84
|
-
You can choose to decline the message being sent, or you can allow the message to be sent as usual. You can also do all the normal assigns to the channel, or user.
|
|
85
|
-
In case there is no *on* function, the message will be sent without any action being taken.
|
|
86
|
-
|
|
87
|
-
#### On the browser
|
|
88
|
-
|
|
89
|
-
```js
|
|
90
|
-
import { PondClient } from "@eleven-am/pondsocket/client";
|
|
91
|
-
|
|
92
|
-
export const socket = new PondClient('/api/socket', {});
|
|
93
|
-
socket.connect();
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
The browser compatible package can be imported from @eleven-am/pondsocket/client.
|
|
97
|
-
AN url string is provided to the class along with other url params, like token.
|
|
98
|
-
|
|
99
|
-
Multiple classes can be created, but it is advised to use a single class throughout the application.
|
|
100
|
-
You can just create multiple channels and maintain the single socket connection.
|
|
101
|
-
|
|
102
|
-
```js
|
|
103
|
-
const channelTopic = 'channel:one';
|
|
104
|
-
const options = {
|
|
105
|
-
username: 'eleven-am'
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const channel = socket.createChannel(channelTopic, options);
|
|
109
|
-
channel.join();
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
When connected to the channel you can subscribe to the events from the channel.
|
|
113
|
-
|
|
114
|
-
```js
|
|
115
|
-
const subscriptionPresence = channel.onPresenceUpdate(presence => {
|
|
116
|
-
// handle the presence changes of the channel
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
const subscriptionMessage = channel.onMessage((event, data) => {
|
|
120
|
-
// handle the message being received
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// When done with the channel remember to unsubscribe from these listeners
|
|
124
|
-
subscriptionPresence.unsubscribe();
|
|
125
|
-
subscriptionMessage.unsubscribe();
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
There are many other features available on the channel object. Since the application is completely typed,
|
|
129
|
-
suggestions should be provided by your IDE.
|
|
130
|
-
|
|
131
|
-
```js
|
|
132
|
-
channel.broadcast('hello', {
|
|
133
|
-
name: 'eleven-am',
|
|
134
|
-
message: 'I am the man, man'
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
// channel.broadcastFrom broadcasts a message to everyone but the client that emitted the message
|
|
138
|
-
// channel.sendMessage sends a message to clients specified in the function
|
|
139
|
-
```
|
package/dist/package.json
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@eleven-am/pondsocket",
|
|
3
|
-
"version": "0.1.56",
|
|
4
|
-
"description": "PondSocket is a fast simple socket server",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"socket",
|
|
7
|
-
"server",
|
|
8
|
-
"ws",
|
|
9
|
-
"websocket",
|
|
10
|
-
"pubsub",
|
|
11
|
-
"presence",
|
|
12
|
-
"realtime",
|
|
13
|
-
"realtime server"
|
|
14
|
-
],
|
|
15
|
-
"publishConfig": {
|
|
16
|
-
"access": "public"
|
|
17
|
-
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "jest --coverage --verbose --watchAll",
|
|
20
|
-
"build": "tsc --project tsconfig.build.json",
|
|
21
|
-
"publishToNpm": "npm version patch && npm publish"
|
|
22
|
-
},
|
|
23
|
-
"author": "Roy OSSAI",
|
|
24
|
-
"license": "GPL-3.0",
|
|
25
|
-
"main": "index.js",
|
|
26
|
-
"types": "index.d.ts",
|
|
27
|
-
"repository": {
|
|
28
|
-
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/Eleven-am/pondSocket.git"
|
|
30
|
-
},
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"websocket": "^1.0.34",
|
|
33
|
-
"ws": "^8.12.0"
|
|
34
|
-
},
|
|
35
|
-
"devDependencies": {
|
|
36
|
-
"@types/express": "^4.17.14",
|
|
37
|
-
"@types/jest": "^29.5.0",
|
|
38
|
-
"@types/node": "^16.10.3",
|
|
39
|
-
"@types/websocket": "^1.0.5",
|
|
40
|
-
"@types/ws": "^8.5.3",
|
|
41
|
-
"@typescript-eslint/eslint-plugin": "^5.58.0",
|
|
42
|
-
"eslint": "^8.38.0",
|
|
43
|
-
"eslint-plugin-file-progress": "^1.3.0",
|
|
44
|
-
"eslint-plugin-import": "^2.27.5",
|
|
45
|
-
"jest": "^29.0.1",
|
|
46
|
-
"superwstest": "^2.0.3",
|
|
47
|
-
"ts-jest": "^29.1.0",
|
|
48
|
-
"ts-node": "^10.9.1",
|
|
49
|
-
"typescript": "^4.9.4"
|
|
50
|
-
}
|
|
51
|
-
}
|
package/jest.config.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { AbstractRequest } from './abstractRequest';
|
|
2
|
-
|
|
3
|
-
const createMockChannelEngine = () => ({
|
|
4
|
-
name: 'test',
|
|
5
|
-
getAssigns: () => ({}),
|
|
6
|
-
getPresence: () => ({}),
|
|
7
|
-
} as any);
|
|
8
|
-
|
|
9
|
-
describe('AbstractRequest', () => {
|
|
10
|
-
it('should be able to be instantiated', () => {
|
|
11
|
-
const request = new AbstractRequest('/test', createMockChannelEngine(), {});
|
|
12
|
-
|
|
13
|
-
expect(request).toBeTruthy();
|
|
14
|
-
expect(request.channelNme).toBe('test');
|
|
15
|
-
expect(request.assigns).toEqual({});
|
|
16
|
-
expect(request.presence).toEqual({});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should be able to parse queries', () => {
|
|
20
|
-
const request = new AbstractRequest('/1234?choke=balls', createMockChannelEngine(), {});
|
|
21
|
-
|
|
22
|
-
expect(() => request.event).toThrowError('Event was not parsed');
|
|
23
|
-
expect(request['_parseQueries']('/:id')).toBe(true);
|
|
24
|
-
expect(request.event).toEqual({
|
|
25
|
-
event: '/1234?choke=balls',
|
|
26
|
-
params: { id: '1234' },
|
|
27
|
-
query: { choke: 'balls' },
|
|
28
|
-
payload: {},
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should be return the value of the payload', () => {
|
|
33
|
-
const request = new AbstractRequest('/test', createMockChannelEngine(), { test: 'test' });
|
|
34
|
-
|
|
35
|
-
expect(request['_parseQueries']('/:id')).toBe(true);
|
|
36
|
-
expect(request.event.payload).toEqual({ test: 'test' });
|
|
37
|
-
|
|
38
|
-
const request2 = new AbstractRequest('/test', createMockChannelEngine(), {
|
|
39
|
-
test: 'test',
|
|
40
|
-
test2: 'test2',
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
expect(request2['_parseQueries']('/:id')).toBe(true);
|
|
44
|
-
expect(request2.event.payload).toEqual({
|
|
45
|
-
test: 'test',
|
|
46
|
-
test2: 'test2',
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { ChannelEngine } from '../channel/channel';
|
|
2
|
-
import { parseAddress } from '../matcher/matcher';
|
|
3
|
-
// eslint-disable-next-line import/no-unresolved
|
|
4
|
-
import { PondMessage, UserPresences, UserAssigns, PondPath, PondEvent, EventParams } from '../types';
|
|
5
|
-
|
|
6
|
-
export class AbstractRequest<Path extends string> {
|
|
7
|
-
protected readonly _engine: ChannelEngine;
|
|
8
|
-
|
|
9
|
-
#eventObject: EventParams<Path> | null;
|
|
10
|
-
|
|
11
|
-
readonly #event: string;
|
|
12
|
-
|
|
13
|
-
readonly #payload: PondMessage;
|
|
14
|
-
|
|
15
|
-
constructor (event: string, engine: ChannelEngine, payload: PondMessage) {
|
|
16
|
-
this._engine = engine;
|
|
17
|
-
this.#event = event;
|
|
18
|
-
this.#eventObject = null;
|
|
19
|
-
this.#payload = payload;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public get event (): PondEvent<Path> {
|
|
23
|
-
if (this.#eventObject === null) {
|
|
24
|
-
throw new Error('Event was not parsed');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
event: this.#event,
|
|
29
|
-
params: this.#eventObject.params || {},
|
|
30
|
-
query: this.#eventObject.query || {},
|
|
31
|
-
payload: this.#payload,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public get channelNme (): string {
|
|
36
|
-
return this._engine.name;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public get assigns (): UserAssigns {
|
|
40
|
-
return this._engine.getAssigns();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public get presence (): UserPresences {
|
|
44
|
-
return this._engine.presenceEngine?.getPresence() || {};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @desc Parses the event and returns true if the event matches the path
|
|
49
|
-
* @param path - the path to match
|
|
50
|
-
*/
|
|
51
|
-
public _parseQueries (path: PondPath<Path>): boolean {
|
|
52
|
-
this.#eventObject = parseAddress(path, this.#event);
|
|
53
|
-
|
|
54
|
-
return this.#eventObject !== null;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line import/no-unresolved
|
|
2
|
-
import { PondAssigns, PondMessage } from '../types';
|
|
3
|
-
|
|
4
|
-
export abstract class PondResponse {
|
|
5
|
-
/**
|
|
6
|
-
* @desc Rejects the request with the given error message
|
|
7
|
-
* @param message - the error message
|
|
8
|
-
* @param errorCode - the error code
|
|
9
|
-
* @param assigns - the data to assign to the client
|
|
10
|
-
*/
|
|
11
|
-
public abstract reject(message?: string, errorCode?: number, assigns?: PondAssigns): void
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @desc Emits a direct message to the client
|
|
15
|
-
* @param event - the event name
|
|
16
|
-
* @param payload - the payload to send
|
|
17
|
-
* @param assigns - the data to assign to the client
|
|
18
|
-
*/
|
|
19
|
-
public abstract send(event: string, payload: PondMessage, assigns?: PondAssigns): void
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @desc Accepts the request and optionally assigns data to the client
|
|
23
|
-
* @param assigns - the data to assign to the client
|
|
24
|
-
*/
|
|
25
|
-
public abstract accept(assigns?: PondAssigns): void
|
|
26
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { Middleware } from './middleware';
|
|
2
|
-
|
|
3
|
-
describe('Middleware', () => {
|
|
4
|
-
it('should be able to add middleware to the stack', () => {
|
|
5
|
-
const middleware = new Middleware();
|
|
6
|
-
|
|
7
|
-
middleware.use(() => {});
|
|
8
|
-
expect(middleware.length).toBe(1);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it('should be able to run middleware', async () => {
|
|
12
|
-
const middleware = new Middleware();
|
|
13
|
-
const mock = jest.fn();
|
|
14
|
-
|
|
15
|
-
middleware.use(mock);
|
|
16
|
-
await middleware.run({}, {} as any, () => {});
|
|
17
|
-
expect(mock).toHaveBeenCalled();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('should be able to run multiple middleware', async () => {
|
|
21
|
-
const middleware = new Middleware();
|
|
22
|
-
const mock = jest.fn();
|
|
23
|
-
|
|
24
|
-
middleware.use((_, __, next) => {
|
|
25
|
-
mock('first');
|
|
26
|
-
next();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
middleware.use((_, __, next) => {
|
|
30
|
-
mock('second');
|
|
31
|
-
next();
|
|
32
|
-
});
|
|
33
|
-
await middleware.run({}, {} as any, () => {});
|
|
34
|
-
expect(mock).toHaveBeenCalledTimes(2);
|
|
35
|
-
expect(mock.mock.calls[0][2]).toBe(mock.mock.calls[1][2]);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should be merge middleware in the correct order', async () => {
|
|
39
|
-
const middleware = new Middleware();
|
|
40
|
-
const mock = jest.fn();
|
|
41
|
-
|
|
42
|
-
middleware.use(mock);
|
|
43
|
-
middleware.use(mock);
|
|
44
|
-
const middleware2 = new Middleware(middleware);
|
|
45
|
-
|
|
46
|
-
expect(middleware2.length).toBe(2);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should call the final function when the middleware stack is empty', async () => {
|
|
50
|
-
const middleware = new Middleware();
|
|
51
|
-
const mock = jest.fn();
|
|
52
|
-
|
|
53
|
-
await middleware.run({}, {} as any, mock);
|
|
54
|
-
expect(mock).toHaveBeenCalled();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should be able to run middleware in the correct order when using a second middleware', async () => {
|
|
58
|
-
const middleware = new Middleware();
|
|
59
|
-
const mock = jest.fn();
|
|
60
|
-
|
|
61
|
-
middleware.use((_, __, next) => {
|
|
62
|
-
mock('first');
|
|
63
|
-
next();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
middleware.use((_, __, next) => {
|
|
67
|
-
mock('second');
|
|
68
|
-
next();
|
|
69
|
-
});
|
|
70
|
-
const middleware2 = new Middleware(middleware);
|
|
71
|
-
|
|
72
|
-
await middleware2.run({}, {} as any, () => {});
|
|
73
|
-
expect(mock.mock.calls[0][2]).toBe(mock.mock.calls[1][2]);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
type NextFunction = () => void;
|
|
2
|
-
|
|
3
|
-
export type MiddlewareFunction<Request, Response> = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
4
|
-
|
|
5
|
-
export class Middleware<Request, Response> {
|
|
6
|
-
readonly #stack: MiddlewareFunction<Request, Response>[] = [];
|
|
7
|
-
|
|
8
|
-
constructor (second?: Middleware<Request, Response>) {
|
|
9
|
-
if (second) {
|
|
10
|
-
this.#stack.push(...second.#stack);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @desc Adds a middleware function to the middleware stack
|
|
16
|
-
* @param middleware - the middleware function to add
|
|
17
|
-
*/
|
|
18
|
-
public use (middleware: MiddlewareFunction<Request, Response>) {
|
|
19
|
-
this.#stack.push(middleware);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @desc Runs the middleware stack
|
|
24
|
-
* @param req - the request object
|
|
25
|
-
* @param res - the response object
|
|
26
|
-
* @param final - the final function to call
|
|
27
|
-
*/
|
|
28
|
-
public run (req: Request, res: Response, final: NextFunction) {
|
|
29
|
-
const temp = this.#stack.concat();
|
|
30
|
-
|
|
31
|
-
const nextFunction = () => {
|
|
32
|
-
const middleware = temp.shift();
|
|
33
|
-
|
|
34
|
-
if (middleware) {
|
|
35
|
-
middleware(req, res, nextFunction);
|
|
36
|
-
} else {
|
|
37
|
-
final();
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
nextFunction();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @desc Returns the middleware stack length
|
|
46
|
-
*/
|
|
47
|
-
public get length () {
|
|
48
|
-
return this.#stack.length;
|
|
49
|
-
}
|
|
50
|
-
}
|