source: trip-planner-front/node_modules/engine.io/build/userver.js@ 84d0fbb

Last change on this file since 84d0fbb was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 7.0 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.uServer = void 0;
4const debug_1 = require("debug");
5const server_1 = require("./server");
6const transports_uws_1 = require("./transports-uws");
7const debug = (0, debug_1.default)("engine:uws");
8class uServer extends server_1.BaseServer {
9 init() { }
10 cleanup() { }
11 /**
12 * Prepares a request by processing the query string.
13 *
14 * @api private
15 */
16 prepare(req, res) {
17 req.method = req.getMethod().toUpperCase();
18 const params = new URLSearchParams(req.getQuery());
19 req._query = Object.fromEntries(params.entries());
20 req.headers = {};
21 req.forEach((key, value) => {
22 req.headers[key] = value;
23 });
24 req.connection = {
25 remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString()
26 };
27 }
28 createTransport(transportName, req) {
29 return new transports_uws_1.default[transportName](req);
30 }
31 /**
32 * Attach the engine to a µWebSockets.js server
33 * @param app
34 * @param options
35 */
36 attach(app /* : TemplatedApp */, options = {}) {
37 const path = (options.path || "/engine.io").replace(/\/$/, "") + "/";
38 app
39 .any(path, this.handleRequest.bind(this))
40 //
41 .ws(path, {
42 maxPayloadLength: this.opts.maxHttpBufferSize,
43 upgrade: this.handleUpgrade.bind(this),
44 open: ws => {
45 ws.transport.socket = ws;
46 ws.transport.writable = true;
47 ws.transport.emit("drain");
48 },
49 message: (ws, message, isBinary) => {
50 ws.transport.onData(isBinary ? message : Buffer.from(message).toString());
51 },
52 close: (ws, code, message) => {
53 ws.transport.onClose(code, message);
54 }
55 });
56 }
57 handleRequest(res, req) {
58 debug('handling "%s" http request "%s"', req.getMethod(), req.getUrl());
59 this.prepare(req, res);
60 req.res = res;
61 const callback = (errorCode, errorContext) => {
62 if (errorCode !== undefined) {
63 this.emit("connection_error", {
64 req,
65 code: errorCode,
66 message: server_1.Server.errorMessages[errorCode],
67 context: errorContext
68 });
69 this.abortRequest(req.res, errorCode, errorContext);
70 return;
71 }
72 if (req._query.sid) {
73 debug("setting new request for existing client");
74 this.clients[req._query.sid].transport.onRequest(req);
75 }
76 else {
77 const closeConnection = (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext);
78 this.handshake(req._query.transport, req, closeConnection);
79 }
80 };
81 if (this.corsMiddleware) {
82 // needed to buffer headers until the status is computed
83 req.res = new ResponseWrapper(res);
84 this.corsMiddleware.call(null, req, req.res, () => {
85 this.verify(req, false, callback);
86 });
87 }
88 else {
89 this.verify(req, false, callback);
90 }
91 }
92 handleUpgrade(res, req, context) {
93 debug("on upgrade");
94 this.prepare(req, res);
95 // @ts-ignore
96 req.res = res;
97 this.verify(req, true, async (errorCode, errorContext) => {
98 if (errorCode) {
99 this.emit("connection_error", {
100 req,
101 code: errorCode,
102 message: server_1.Server.errorMessages[errorCode],
103 context: errorContext
104 });
105 this.abortRequest(res, errorCode, errorContext);
106 return;
107 }
108 const id = req._query.sid;
109 let transport;
110 if (id) {
111 const client = this.clients[id];
112 if (!client) {
113 debug("upgrade attempt for closed client");
114 res.close();
115 }
116 else if (client.upgrading) {
117 debug("transport has already been trying to upgrade");
118 res.close();
119 }
120 else if (client.upgraded) {
121 debug("transport had already been upgraded");
122 res.close();
123 }
124 else {
125 debug("upgrading existing transport");
126 transport = this.createTransport(req._query.transport, req);
127 client.maybeUpgrade(transport);
128 }
129 }
130 else {
131 transport = await this.handshake(req._query.transport, req, (errorCode, errorContext) => this.abortRequest(res, errorCode, errorContext));
132 if (!transport) {
133 return;
134 }
135 }
136 res.upgrade({
137 transport
138 }, req.getHeader("sec-websocket-key"), req.getHeader("sec-websocket-protocol"), req.getHeader("sec-websocket-extensions"), context);
139 });
140 }
141 abortRequest(res, errorCode, errorContext) {
142 const statusCode = errorCode === server_1.Server.errors.FORBIDDEN
143 ? "403 Forbidden"
144 : "400 Bad Request";
145 const message = errorContext && errorContext.message
146 ? errorContext.message
147 : server_1.Server.errorMessages[errorCode];
148 res.writeStatus(statusCode);
149 res.writeHeader("Content-Type", "application/json");
150 res.end(JSON.stringify({
151 code: errorCode,
152 message
153 }));
154 }
155}
156exports.uServer = uServer;
157class ResponseWrapper {
158 constructor(res) {
159 this.res = res;
160 this.statusWritten = false;
161 this.headers = [];
162 }
163 set statusCode(status) {
164 this.writeStatus(status === 200 ? "200 OK" : "204 No Content");
165 }
166 setHeader(key, value) {
167 this.writeHeader(key, value);
168 }
169 // needed by vary: https://github.com/jshttp/vary/blob/5d725d059b3871025cf753e9dfa08924d0bcfa8f/index.js#L134
170 getHeader() { }
171 writeStatus(status) {
172 this.res.writeStatus(status);
173 this.statusWritten = true;
174 this.writeBufferedHeaders();
175 }
176 writeHeader(key, value) {
177 if (key === "Content-Length") {
178 // the content length is automatically added by uWebSockets.js
179 return;
180 }
181 if (this.statusWritten) {
182 this.res.writeHeader(key, value);
183 }
184 else {
185 this.headers.push([key, value]);
186 }
187 }
188 writeBufferedHeaders() {
189 this.headers.forEach(([key, value]) => {
190 this.res.writeHeader(key, value);
191 });
192 }
193 end(data) {
194 if (!this.statusWritten) {
195 // status will be inferred as "200 OK"
196 this.writeBufferedHeaders();
197 }
198 this.res.end(data);
199 }
200 onAborted(fn) {
201 this.res.onAborted(fn);
202 }
203}
Note: See TracBrowser for help on using the repository browser.