@eleven-am/pondsocket 0.1.9 → 0.1.11
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 +1 -1
- package/pondBase/baseClass.js +32 -51
- package/pondBase/baseClass.test.js +34 -34
- package/pondBase/pondBase.js +34 -95
- package/pondBase/pondBase.test.js +39 -67
- package/pondBase/pubSub.d.ts +8 -9
- package/pondBase/pubSub.js +65 -121
- package/pondBase/pubSub.test.js +119 -110
- package/pondBase/simpleBase.js +92 -166
- package/pondBase/simpleBase.test.js +66 -124
- package/pondClient/channel.d.ts +14 -3
- package/pondClient/channel.js +71 -46
- package/pondClient/socket.js +35 -38
- package/pondSocket/channel.js +123 -176
- package/pondSocket/channel.test.js +78 -89
- package/pondSocket/channelMiddleWare.js +16 -43
- package/pondSocket/endpoint.js +93 -188
- package/pondSocket/endpoint.test.js +447 -632
- package/pondSocket/pondChannel.js +81 -142
- package/pondSocket/pondChannel.test.js +40 -40
- package/pondSocket/pondResponse.js +52 -87
- package/pondSocket/pondSocket.js +35 -55
- package/pondSocket/server.test.js +88 -142
- package/pondSocket/socketMiddleWare.js +14 -16
|
@@ -1,46 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.EndpointResponse = exports.PondChannelResponse = exports.ChannelResponse = exports.PondResponse = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
return PondResponse;
|
|
24
|
-
}());
|
|
4
|
+
const enums_1 = require("./enums");
|
|
5
|
+
class PondResponse {
|
|
6
|
+
}
|
|
25
7
|
exports.PondResponse = PondResponse;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
8
|
+
class ChannelResponse extends PondResponse {
|
|
9
|
+
constructor(clientId, channel, resolver) {
|
|
10
|
+
super();
|
|
11
|
+
this._clientId = clientId;
|
|
12
|
+
this._channel = channel;
|
|
13
|
+
this._resolver = resolver;
|
|
14
|
+
this._hasExecuted = false;
|
|
15
|
+
}
|
|
16
|
+
get hasExecuted() {
|
|
17
|
+
return this._hasExecuted;
|
|
35
18
|
}
|
|
36
|
-
|
|
37
|
-
get: function () {
|
|
38
|
-
return this._hasExecuted;
|
|
39
|
-
},
|
|
40
|
-
enumerable: false,
|
|
41
|
-
configurable: true
|
|
42
|
-
});
|
|
43
|
-
ChannelResponse.prototype.accept = function (assigns) {
|
|
19
|
+
accept(assigns) {
|
|
44
20
|
if (this._hasExecuted)
|
|
45
21
|
throw new Error('Response has already been sent');
|
|
46
22
|
this._hasExecuted = true;
|
|
@@ -51,8 +27,8 @@ var ChannelResponse = /** @class */ (function (_super) {
|
|
|
51
27
|
this._channel.data = assigns.channelData || {};
|
|
52
28
|
}
|
|
53
29
|
this._resolver(false);
|
|
54
|
-
}
|
|
55
|
-
|
|
30
|
+
}
|
|
31
|
+
reject(message, errorCode) {
|
|
56
32
|
if (this._hasExecuted)
|
|
57
33
|
throw new Error('Response has already been sent');
|
|
58
34
|
this._hasExecuted = true;
|
|
@@ -62,8 +38,8 @@ var ChannelResponse = /** @class */ (function (_super) {
|
|
|
62
38
|
throw new Error('Cannot reject a message sent by the server');
|
|
63
39
|
this._channel.respondToClient('error', { message: message, code: errorCode }, this._clientId, enums_1.ServerActions.ERROR);
|
|
64
40
|
this._resolver(true);
|
|
65
|
-
}
|
|
66
|
-
|
|
41
|
+
}
|
|
42
|
+
send(event, payload, assigns) {
|
|
67
43
|
if (this._hasExecuted)
|
|
68
44
|
throw new Error('Response has already been sent');
|
|
69
45
|
this._hasExecuted = true;
|
|
@@ -75,38 +51,31 @@ var ChannelResponse = /** @class */ (function (_super) {
|
|
|
75
51
|
}
|
|
76
52
|
this._channel.respondToClient(event, payload, this._clientId);
|
|
77
53
|
this._resolver(false);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
}(PondResponse));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
81
56
|
exports.ChannelResponse = ChannelResponse;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
57
|
+
class PondChannelResponse extends PondResponse {
|
|
58
|
+
constructor(user, handler) {
|
|
59
|
+
super();
|
|
60
|
+
this._handler = handler;
|
|
61
|
+
this._user = user;
|
|
62
|
+
this._executed = false;
|
|
63
|
+
}
|
|
64
|
+
get isResolved() {
|
|
65
|
+
return this._executed;
|
|
90
66
|
}
|
|
91
|
-
|
|
92
|
-
get: function () {
|
|
93
|
-
return this._executed;
|
|
94
|
-
},
|
|
95
|
-
enumerable: false,
|
|
96
|
-
configurable: true
|
|
97
|
-
});
|
|
98
|
-
PondChannelResponse.prototype.accept = function (assigns) {
|
|
67
|
+
accept(assigns) {
|
|
99
68
|
if (this._executed)
|
|
100
69
|
throw new Error('Response has already been sent');
|
|
101
70
|
this._executed = true;
|
|
102
|
-
|
|
71
|
+
const newAssigns = this._mergeAssigns(assigns);
|
|
103
72
|
this._handler(newAssigns);
|
|
104
|
-
}
|
|
105
|
-
|
|
73
|
+
}
|
|
74
|
+
reject(message, errorCode) {
|
|
106
75
|
if (this._executed)
|
|
107
76
|
throw new Error('Response has already been sent');
|
|
108
77
|
this._executed = true;
|
|
109
|
-
|
|
78
|
+
const newMessage = {
|
|
110
79
|
action: enums_1.ServerActions.ERROR,
|
|
111
80
|
event: "JOIN_REQUEST_ERROR",
|
|
112
81
|
channelName: enums_1.PondSenders.POND_CHANNEL,
|
|
@@ -116,40 +85,36 @@ var PondChannelResponse = /** @class */ (function (_super) {
|
|
|
116
85
|
}
|
|
117
86
|
};
|
|
118
87
|
this._user.socket.send(JSON.stringify(newMessage));
|
|
119
|
-
}
|
|
120
|
-
|
|
88
|
+
}
|
|
89
|
+
send(event, payload, assigns) {
|
|
121
90
|
if (this._executed)
|
|
122
91
|
throw new Error('Response has already been sent');
|
|
123
92
|
this._executed = true;
|
|
124
|
-
|
|
93
|
+
const newAssigns = this._mergeAssigns(assigns);
|
|
125
94
|
this._handler(newAssigns, { event: event, payload: payload });
|
|
126
|
-
}
|
|
127
|
-
|
|
95
|
+
}
|
|
96
|
+
_mergeAssigns(assigns) {
|
|
128
97
|
return {
|
|
129
98
|
presence: (assigns === null || assigns === void 0 ? void 0 : assigns.presence) || {},
|
|
130
99
|
channelData: (assigns === null || assigns === void 0 ? void 0 : assigns.channelData) || {},
|
|
131
100
|
assigns: Object.assign({}, this._user.assigns, (assigns === null || assigns === void 0 ? void 0 : assigns.assigns) || {}),
|
|
132
101
|
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
}(PondResponse));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
136
104
|
exports.PondChannelResponse = PondChannelResponse;
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
_this._handler = handler;
|
|
142
|
-
return _this;
|
|
105
|
+
class EndpointResponse extends PondResponse {
|
|
106
|
+
constructor(handler) {
|
|
107
|
+
super();
|
|
108
|
+
this._handler = handler;
|
|
143
109
|
}
|
|
144
|
-
|
|
110
|
+
accept(assigns) {
|
|
145
111
|
this._handler((assigns === null || assigns === void 0 ? void 0 : assigns.assigns) || {}, {});
|
|
146
|
-
}
|
|
147
|
-
|
|
112
|
+
}
|
|
113
|
+
reject(message, errorCode) {
|
|
148
114
|
this._handler({}, { error: { message: message || 'Message rejected', code: errorCode || 403 } });
|
|
149
|
-
}
|
|
150
|
-
|
|
115
|
+
}
|
|
116
|
+
send(event, payload, assigns) {
|
|
151
117
|
this._handler((assigns === null || assigns === void 0 ? void 0 : assigns.assigns) || {}, { message: { event: event, payload: payload } });
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
}(PondResponse));
|
|
118
|
+
}
|
|
119
|
+
}
|
|
155
120
|
exports.EndpointResponse = EndpointResponse;
|
package/pondSocket/pondSocket.js
CHANGED
|
@@ -1,51 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.PondSocket = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
_this._init();
|
|
32
|
-
return _this;
|
|
4
|
+
const http_1 = require("http");
|
|
5
|
+
const ws_1 = require("ws");
|
|
6
|
+
const pondBase_1 = require("../pondBase");
|
|
7
|
+
const endpoint_1 = require("./endpoint");
|
|
8
|
+
const socketMiddleWare_1 = require("./socketMiddleWare");
|
|
9
|
+
class PondSocket extends pondBase_1.BaseClass {
|
|
10
|
+
constructor(server, socketServer) {
|
|
11
|
+
super();
|
|
12
|
+
this._server = server || new http_1.Server();
|
|
13
|
+
this._socketServer = socketServer || new ws_1.WebSocketServer({ noServer: true });
|
|
14
|
+
this._socketChain = new socketMiddleWare_1.SocketMiddleWare(this._server);
|
|
15
|
+
this._init();
|
|
33
16
|
}
|
|
34
17
|
/**
|
|
35
18
|
* @desc Specifies the port to listen on
|
|
36
19
|
* @param port - the port to listen on
|
|
37
20
|
* @param callback - the callback to call when the server is listening
|
|
38
21
|
*/
|
|
39
|
-
|
|
22
|
+
listen(port, callback) {
|
|
40
23
|
return this._server.listen(port, callback);
|
|
41
|
-
}
|
|
24
|
+
}
|
|
42
25
|
/**
|
|
43
26
|
* @desc adds a middleware to the server
|
|
44
27
|
* @param middleware - the middleware to add
|
|
45
28
|
*/
|
|
46
|
-
|
|
29
|
+
useOnUpgrade(middleware) {
|
|
47
30
|
this._socketChain.use(middleware);
|
|
48
|
-
}
|
|
31
|
+
}
|
|
49
32
|
/**
|
|
50
33
|
* @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
|
|
51
34
|
* @param path - the pattern to accept || can also be a regex
|
|
@@ -64,51 +47,48 @@ var PondSocket = /** @class */ (function (_super) {
|
|
|
64
47
|
* });
|
|
65
48
|
* })
|
|
66
49
|
*/
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
var dataEndpoint = _this.generateEventRequest(path, address);
|
|
50
|
+
createEndpoint(path, handler) {
|
|
51
|
+
const endpoint = new endpoint_1.Endpoint(this._socketServer, handler);
|
|
52
|
+
this._socketChain.use((req, socket, head, next) => {
|
|
53
|
+
const address = req.url || "";
|
|
54
|
+
const dataEndpoint = this.generateEventRequest(path, address);
|
|
73
55
|
if (!dataEndpoint)
|
|
74
56
|
return next();
|
|
75
57
|
endpoint.authoriseConnection(req, socket, head, dataEndpoint);
|
|
76
58
|
});
|
|
77
59
|
return endpoint;
|
|
78
|
-
}
|
|
60
|
+
}
|
|
79
61
|
/**
|
|
80
62
|
* @desc Makes sure that every client is still connected to the pond
|
|
81
63
|
* @param server - WebSocket server
|
|
82
64
|
*/
|
|
83
|
-
|
|
84
|
-
server.on("connection",
|
|
65
|
+
_pingClients(server) {
|
|
66
|
+
server.on("connection", (ws) => {
|
|
85
67
|
ws.isAlive = true;
|
|
86
|
-
ws.on("pong",
|
|
68
|
+
ws.on("pong", () => {
|
|
87
69
|
ws.isAlive = true;
|
|
88
70
|
});
|
|
89
71
|
});
|
|
90
|
-
|
|
91
|
-
server.clients.forEach(
|
|
72
|
+
const interval = setInterval(() => {
|
|
73
|
+
server.clients.forEach((ws) => {
|
|
92
74
|
if (ws.isAlive === false)
|
|
93
75
|
return ws.terminate();
|
|
94
76
|
ws.isAlive = false;
|
|
95
77
|
ws.ping();
|
|
96
78
|
});
|
|
97
79
|
}, 30 * 1000);
|
|
98
|
-
server.on("close",
|
|
99
|
-
}
|
|
80
|
+
server.on("close", () => clearInterval(interval));
|
|
81
|
+
}
|
|
100
82
|
/**
|
|
101
83
|
* @desc Initializes the server
|
|
102
84
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this._server.on("error", function (error) {
|
|
85
|
+
_init() {
|
|
86
|
+
this._server.on("error", (error) => {
|
|
106
87
|
throw new Error(error.message);
|
|
107
88
|
});
|
|
108
|
-
this._server.on("listening",
|
|
109
|
-
|
|
89
|
+
this._server.on("listening", () => {
|
|
90
|
+
this._pingClients(this._socketServer);
|
|
110
91
|
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
}(pondBase_1.BaseClass));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
114
94
|
exports.PondSocket = PondSocket;
|
|
@@ -8,85 +8,58 @@ 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 __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
13
|
};
|
|
41
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
describe('server',
|
|
49
|
-
it('should be defined',
|
|
15
|
+
const endpoint_1 = require("./endpoint");
|
|
16
|
+
const pondSocket_1 = require("./pondSocket");
|
|
17
|
+
const superwstest_1 = __importDefault(require("superwstest"));
|
|
18
|
+
const enums_1 = require("./enums");
|
|
19
|
+
const http_1 = require("http");
|
|
20
|
+
const ws_1 = require("ws");
|
|
21
|
+
describe('server', () => {
|
|
22
|
+
it('should be defined', () => {
|
|
50
23
|
expect(pondSocket_1.PondSocket).toBeDefined();
|
|
51
24
|
});
|
|
52
|
-
it('should be instantiable',
|
|
25
|
+
it('should be instantiable', () => {
|
|
53
26
|
expect(new pondSocket_1.PondSocket()).toBeInstanceOf(pondSocket_1.PondSocket);
|
|
54
27
|
});
|
|
55
|
-
it('should ba able to create its own server and websocket server if none is provided',
|
|
56
|
-
|
|
28
|
+
it('should ba able to create its own server and websocket server if none is provided', () => {
|
|
29
|
+
const socket = new pondSocket_1.PondSocket();
|
|
57
30
|
expect(socket['_server']).toBeDefined();
|
|
58
31
|
expect(socket['_socketServer']).toBeDefined();
|
|
59
32
|
});
|
|
60
|
-
it('should take a server and websocket server if provided',
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
33
|
+
it('should take a server and websocket server if provided', () => {
|
|
34
|
+
const server = (0, http_1.createServer)();
|
|
35
|
+
const socketServer = new ws_1.Server({ noServer: true });
|
|
36
|
+
const socket = new pondSocket_1.PondSocket(server, socketServer);
|
|
64
37
|
expect(socket['_server']).toBe(server);
|
|
65
38
|
expect(socket['_socketServer']).toBe(socketServer);
|
|
66
39
|
});
|
|
67
|
-
it('should be able to listen on a port',
|
|
68
|
-
|
|
69
|
-
expect(socket.listen(3001,
|
|
40
|
+
it('should be able to listen on a port', () => {
|
|
41
|
+
const socket = new pondSocket_1.PondSocket();
|
|
42
|
+
expect(socket.listen(3001, () => {
|
|
70
43
|
console.log('socket');
|
|
71
44
|
})).toBeDefined();
|
|
72
45
|
socket['_server'].close();
|
|
73
46
|
});
|
|
74
|
-
it('should be able to create an endpoint',
|
|
75
|
-
|
|
76
|
-
|
|
47
|
+
it('should be able to create an endpoint', () => {
|
|
48
|
+
const socket = new pondSocket_1.PondSocket();
|
|
49
|
+
const endpoint = socket.createEndpoint('/api/socket', () => {
|
|
77
50
|
console.log('socket');
|
|
78
51
|
});
|
|
79
52
|
expect(endpoint).toBeInstanceOf(endpoint_1.Endpoint);
|
|
80
53
|
expect(endpoint['_handler']).toEqual(expect.any(Function));
|
|
81
54
|
});
|
|
82
|
-
it('should be able to create multiple endpoints',
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
55
|
+
it('should be able to create multiple endpoints', () => {
|
|
56
|
+
const server = (0, http_1.createServer)();
|
|
57
|
+
const socketServer = new ws_1.Server({ noServer: true });
|
|
58
|
+
const socket = new pondSocket_1.PondSocket(server, socketServer);
|
|
59
|
+
const endpoint = socket.createEndpoint('/api/socket', () => {
|
|
87
60
|
console.log('socket');
|
|
88
61
|
});
|
|
89
|
-
|
|
62
|
+
const endpoint2 = socket.createEndpoint('/api/socket2', () => {
|
|
90
63
|
console.log('socket2');
|
|
91
64
|
});
|
|
92
65
|
expect(endpoint).toBeInstanceOf(endpoint_1.Endpoint);
|
|
@@ -94,15 +67,15 @@ describe('server', function () {
|
|
|
94
67
|
expect(endpoint2).toBeInstanceOf(endpoint_1.Endpoint);
|
|
95
68
|
expect(endpoint2['_handler']).toEqual(expect.any(Function));
|
|
96
69
|
});
|
|
97
|
-
it('should be able to reject a socket',
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
70
|
+
it('should be able to reject a socket', () => {
|
|
71
|
+
const server = (0, http_1.createServer)();
|
|
72
|
+
const socketServer = new ws_1.Server({ noServer: true });
|
|
73
|
+
const socket = new pondSocket_1.PondSocket(server, socketServer);
|
|
74
|
+
const socketClient = {
|
|
102
75
|
write: jest.fn(),
|
|
103
76
|
destroy: jest.fn(),
|
|
104
77
|
};
|
|
105
|
-
socket.listen(3001,
|
|
78
|
+
socket.listen(3001, () => {
|
|
106
79
|
console.log('server listening');
|
|
107
80
|
});
|
|
108
81
|
server.emit('upgrade', {}, socketClient);
|
|
@@ -111,89 +84,62 @@ describe('server', function () {
|
|
|
111
84
|
expect(socketClient.write).toHaveBeenCalled();
|
|
112
85
|
expect(socketClient.destroy).toHaveBeenCalled();
|
|
113
86
|
});
|
|
114
|
-
it('should be able to accept a socket if a handler is provided',
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
87
|
+
it('should be able to accept a socket if a handler is provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
const socket = new pondSocket_1.PondSocket();
|
|
89
|
+
const server = socket.listen(3001, () => {
|
|
90
|
+
console.log('server listening');
|
|
91
|
+
});
|
|
92
|
+
expect(server).toBeDefined();
|
|
93
|
+
socket.createEndpoint('/api/hello', () => {
|
|
94
|
+
console.log('server listening');
|
|
95
|
+
});
|
|
96
|
+
socket.createEndpoint('/api/:path', (req, res) => {
|
|
97
|
+
expect(req.params.path).toBe('socket');
|
|
98
|
+
res.accept();
|
|
99
|
+
});
|
|
100
|
+
yield (0, superwstest_1.default)(server)
|
|
101
|
+
.ws('/api/socket')
|
|
102
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
103
|
+
.close()
|
|
104
|
+
.expectClosed();
|
|
105
|
+
server.close();
|
|
106
|
+
}));
|
|
107
|
+
it('should be able to reject a socket if the handler rejects', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
108
|
+
const socket = new pondSocket_1.PondSocket();
|
|
109
|
+
const server = socket.listen(3001, () => {
|
|
110
|
+
console.log('server listening');
|
|
111
|
+
});
|
|
112
|
+
expect(server).toBeDefined();
|
|
113
|
+
socket.createEndpoint('/api/:path', (req, res) => {
|
|
114
|
+
expect(req.params.path).toBe('socket');
|
|
115
|
+
res.reject();
|
|
141
116
|
});
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
});
|
|
152
|
-
expect(server).toBeDefined();
|
|
153
|
-
socket.createEndpoint('/api/:path', function (req, res) {
|
|
154
|
-
expect(req.params.path).toBe('socket');
|
|
155
|
-
res.reject();
|
|
156
|
-
});
|
|
157
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
158
|
-
.ws('/api/socket')
|
|
159
|
-
.expectConnectionError()];
|
|
160
|
-
case 1:
|
|
161
|
-
_a.sent();
|
|
162
|
-
server.close();
|
|
163
|
-
return [2 /*return*/];
|
|
164
|
-
}
|
|
117
|
+
yield (0, superwstest_1.default)(server)
|
|
118
|
+
.ws('/api/socket')
|
|
119
|
+
.expectConnectionError();
|
|
120
|
+
server.close();
|
|
121
|
+
}));
|
|
122
|
+
it('should be able to send a message after connection', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
123
|
+
const socket = new pondSocket_1.PondSocket();
|
|
124
|
+
const server = socket.listen(3001, () => {
|
|
125
|
+
console.log('server listening');
|
|
165
126
|
});
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
switch (_a.label) {
|
|
171
|
-
case 0:
|
|
172
|
-
socket = new pondSocket_1.PondSocket();
|
|
173
|
-
server = socket.listen(3001, function () {
|
|
174
|
-
console.log('server listening');
|
|
175
|
-
});
|
|
176
|
-
expect(server).toBeDefined();
|
|
177
|
-
socket.createEndpoint('/api/:path', function (req, res) {
|
|
178
|
-
expect(req.params.path).toBe('socket');
|
|
179
|
-
res.send('testEvent', { test: 'test' });
|
|
180
|
-
});
|
|
181
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
182
|
-
.ws('/api/socket')
|
|
183
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
184
|
-
.expectJson({
|
|
185
|
-
action: enums_1.ServerActions.MESSAGE,
|
|
186
|
-
event: 'testEvent',
|
|
187
|
-
channelName: 'SERVER',
|
|
188
|
-
payload: { test: 'test' },
|
|
189
|
-
})
|
|
190
|
-
.close()
|
|
191
|
-
.expectClosed()];
|
|
192
|
-
case 1:
|
|
193
|
-
_a.sent();
|
|
194
|
-
server.close();
|
|
195
|
-
return [2 /*return*/];
|
|
196
|
-
}
|
|
127
|
+
expect(server).toBeDefined();
|
|
128
|
+
socket.createEndpoint('/api/:path', (req, res) => {
|
|
129
|
+
expect(req.params.path).toBe('socket');
|
|
130
|
+
res.send('testEvent', { test: 'test' });
|
|
197
131
|
});
|
|
198
|
-
|
|
132
|
+
yield (0, superwstest_1.default)(server)
|
|
133
|
+
.ws('/api/socket')
|
|
134
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
135
|
+
.expectJson({
|
|
136
|
+
action: enums_1.ServerActions.MESSAGE,
|
|
137
|
+
event: 'testEvent',
|
|
138
|
+
channelName: 'SERVER',
|
|
139
|
+
payload: { test: 'test' },
|
|
140
|
+
})
|
|
141
|
+
.close()
|
|
142
|
+
.expectClosed();
|
|
143
|
+
server.close();
|
|
144
|
+
}));
|
|
199
145
|
});
|