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