-
Notifications
You must be signed in to change notification settings - Fork 83
/
Response.js
1007 lines (879 loc) · 34.8 KB
/
Response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const Server = require('../Server.js'); // lgtm [js/unused-local-variable]
const cookie = require('cookie');
const signature = require('cookie-signature');
const status_codes = require('../../constants/status_codes.json');
const mime_types = require('mime-types');
const { Readable, Writable } = require('stream');
const SSEventStream = require('../plugins/SSEventStream.js');
const LiveFile = require('../plugins/LiveFile.js');
const FilePool = {};
class Response extends Writable {
locals = {};
#streaming = false;
#initiated = false;
#completed = false;
#type_written = false;
#custom_content_length;
#wrapped_request;
#middleware_cursor;
#raw_response;
#master_context;
#upgrade_socket;
#status_code = 200;
#headers;
#cookies;
#sse;
constructor(stream_options = {}, wrapped_request, raw_response, socket, master_context) {
// Initialize the writable stream for this response
super(stream_options);
// Store the provided parameter properties for later use
this.#wrapped_request = wrapped_request;
this.#raw_response = raw_response;
this.#upgrade_socket = socket || null;
this.#master_context = master_context;
// Bind the abort handler as required by uWebsockets.js
this._bind_abort_handler();
// Bind a finish/close handler which will end the response once writable has closed
super.once('finish', () => (this.#streaming ? this.send() : undefined));
}
/**
* @private
* INTERNAL METHOD! This method is an internal method and should NOT be called manually.
* This method binds an abort handler which will update completed field to lock appropriate operations in Response
*/
_bind_abort_handler() {
const reference = this;
this.#raw_response.onAborted(() => {
reference.#completed = true;
reference.#wrapped_request._stop_streaming();
reference.emit('abort', this.#wrapped_request, this);
reference.emit('close', this.#wrapped_request, this);
});
}
/**
* Tracks middleware cursor position over a request's lifetime.
* This is so we can detect any double middleware iterations and throw an error.
* @private
* @param {Number} position - Cursor position
*/
_track_middleware_cursor(position) {
// Initialize cursor on first invocation
if (this.#middleware_cursor == undefined) return (this.#middleware_cursor = position);
// Check if position is greater than last cursor and update
if (position > this.#middleware_cursor) return (this.#middleware_cursor = position);
// If position is not greater than last cursor then we likely have a double middleware execution
this.throw(
new Error(
'HyperExpress: Double middleware execution detected! You have a bug where one of your middlewares is calling both the next() callback and also resolving from a Promise/async middleware. You must only use one of these not both.'
)
);
}
/**
* Resume the associated request if it is paused.
* @private
*/
_resume_if_paused() {
// Unpause the request if it is paused
if (this.#wrapped_request.paused) this.#wrapped_request.resume();
}
/* Response Methods/Operators */
/**
* This method can be used to improve Network IO performance by executing
* all network operations in a singular atomic structure.
*
* @param {Function} handler
*/
atomic(handler) {
if (typeof handler !== 'function')
this.throw(new Error('HyperExpress: atomic(handler) -> handler must be a Javascript function'));
this._resume_if_paused();
return this.#raw_response.cork(handler);
}
/**
* This method is used to set a custom response code.
*
* @param {Number} code Example: response.status(403)
* @returns {Response} Response (Chainable)
*/
status(code) {
// Throw expection if a status change is attempted after response has been initiated
if (this.initiated) {
this.throw(
new Error(
'HyperExpress: Response.status(code) -> HTTP Status Code cannot be changed once a response has been initiated.'
)
);
}
// Set the numeric status code. Status text is appended before writing status to uws
this.#status_code = code;
return this;
}
/**
* This method is used to set the response content type header based on the provided mime type. Example: type('json')
*
* @param {String} mime_type Mime type
* @returns {Response} Response (Chainable)
*/
type(mime_type) {
// Remove leading dot from mime type if present
if (mime_type.startsWith('.')) mime_type = mime_type.substring(1);
// Determine proper mime type and send response
let mime_header = mime_types.lookup(mime_type) || 'text/plain';
if (!this.#completed) {
this.#type_written = true;
this.header('content-type', mime_header);
}
return this;
}
/**
* This method can be used to write a response header and supports chaining.
*
* @param {String} name Header Name
* @param {String|Array<String>} value Header Value
* @returns {Response} Response (Chainable)
*/
header(name, value) {
// Throw expection if a header write is attempted after response has been initiated
if (this.initiated)
this.throw(
new Error(
'HyperExpress: Response.header(name, value) -> Headers cannot be written after a response has already been initiated.'
)
);
// Call self for all specified values in values array
if (Array.isArray(value)) {
value.forEach((item) => this.header(name, item));
return this;
}
// Initialize headers container object if it does not exist
if (this.#headers == undefined) this.#headers = {};
// Initialize header values as an array to allow for multiple values
if (this.#headers[name] == undefined) this.#headers[name] = [];
// Ensure that the value is always a string type
if (typeof value !== 'string')
this.throw(
new Error('HyperExpress: header(name, value) -> value candidates must always be of type string')
);
// Determine if the header being written is a "content-length" header and if so, set the length
if (name.toLowerCase() === 'content-length') this.#custom_content_length = parseInt(value);
// Push current header value onto values array
this.#headers[name].push(value);
return this;
}
/**
* @typedef {Object} CookieOptions
* @property {String} domain
* @property {String} path
* @property {Number} maxAge
* @property {Boolean} secure
* @property {Boolean} httpOnly
* @property {Boolean|'none'|'lax'|'strict'} sameSite
* @property {String} secret
*/
/**
* This method is used to write a cookie to incoming request.
* To delete a cookie, set the value to null.
*
* @param {String} name Cookie Name
* @param {String} value Cookie Value
* @param {Number} expiry In milliseconds
* @param {CookieOptions} options Cookie Options
* @param {Boolean} sign_cookie Enables/Disables Cookie Signing
* @returns {Response} Response (Chainable)
*/
cookie(
name,
value,
expiry,
options = {
secure: true,
sameSite: 'none',
path: '/',
},
sign_cookie = true
) {
// Determine if this is a delete operation and recursively call self with appropriate options
if (name && value === null)
return this.cookie(name, '', null, {
maxAge: 0,
});
// Convert expiry to a valid Date object or delete expiry altogether
if (typeof expiry == 'number') options.expires = new Date(Date.now() + expiry);
// Sign cookie value if signing is enabled and a valid secret is provided
if (sign_cookie && typeof options.secret == 'string') {
options.encode = false; // Turn off encoding to prevent loss of signature structure
value = signature.sign(value, options.secret);
}
// Initialize cookies holder and store cookie value
if (this.#cookies == undefined) this.#cookies = {};
this.#cookies[name] = value;
// Serialize the cookie options and write the 'Set-Cookie' header
return this.header('set-cookie', cookie.serialize(name, value, options));
}
/**
* This method is used to upgrade an incoming upgrade HTTP request to a Websocket connection.
* @param {Object=} context Store information about the websocket connection
*/
upgrade(context) {
if (!this.#completed) {
// Ensure a upgrade_socket exists before upgrading ensuring only upgrade handler requests are handled
if (this.#upgrade_socket == null)
this.throw(
new Error(
'HyperExpress: You cannot upgrade a request that does not come from an upgrade handler. No upgrade socket was found.'
)
);
// Ensure our request is not paused for whatever reason
this._resume_if_paused();
// Call uWS.Response.upgrade() method with user data, protocol headers and uWS upgrade socket
const headers = this.#wrapped_request.headers;
this.#raw_response.upgrade(
{
context,
},
headers['sec-websocket-key'],
headers['sec-websocket-protocol'],
headers['sec-websocket-extensions'],
this.#upgrade_socket
);
// Mark request as complete so no more operations can be performed
this.#completed = true;
}
}
/**
* @private
* Initiates response process by writing HTTP status code and then writing the appropriate headers.
*/
_initiate_response() {
// Halt execution if response has already been initiated or completed
if (this.#initiated || this.#completed) return;
// Emit the 'prepare' event to allow for any last minute response modifications
this.emit('prepare', this.#wrapped_request, this);
// Mark the instance as initiated signifying that no more status/header based operations can be performed
this.#initiated = true;
// Ensure we are not in a paused state as uWS requires us to be a in a flowing state to be able to write status and headers
this._resume_if_paused();
// Write the appropriate status code to the response along with mapped status code message
if (this.#status_code)
this.#raw_response.writeStatus(this.#status_code + ' ' + status_codes[this.#status_code]);
// Iterate through all headers and write them to uWS
if (this.#headers)
Object.keys(this.#headers).forEach((name) =>
this.#headers[name].forEach((value) => this.#raw_response.writeHeader(name, value))
);
}
/**
* Binds a drain handler which gets called with a byte offset that can be used to try a failed chunk write.
* You MUST perform a write call inside the handler for uWS chunking to work properly.
* You MUST return a boolean value indicating if the write was successful or not.
*
* @param {function(number):boolean} handler Synchronous callback only
*/
drain(handler) {
// Ensure handler is a function type
if (typeof handler !== 'function')
this.throw(new Error('HyperExpress: Response.drain(handler) -> handler must be a Function.'));
// Bind a writable handler with a fallback return value to true as uWS expects a Boolean
this.#raw_response.onWritable((offset) => {
// Retrieve the write result from the handler
const output = handler(offset);
// Throw an exception if the handler did not return a boolean value as that is an improper implementation
if (typeof output !== 'boolean')
this.throw(
new Error(
'HyperExpress: Response.drain(handler) -> handler must return a boolean value stating if the write was successful or not.'
)
);
// Return the boolean value to uWS as required by uWS documentation
return output;
});
}
/**
* Writes the provided chunk to the client over uWS with backpressure handling if a callback is provided.
*
* @private
* @param {String|Buffer|ArrayBuffer} chunk
* @param {String=} encoding
* @param {Function=} callback
* @returns {Boolean} 'false' signifies that the chunk was not sent due to built up backpressure.
*/
_write(chunk, encoding, callback) {
// Ensure the client is still connected and request is pending
if (!this.#completed) {
// Mark this response as streaming
this.#streaming = true;
// Ensure response has been initiated before writing any chunks
this._initiate_response();
// Attempt to write the chunk to the client
const written = this.#raw_response.write(chunk);
if (written) {
// If chunk write was a success, we can move onto consuming the next chunk
if (callback) callback();
// Return true here to signify that this chunk was written successfully
return true;
} else if (callback) {
// Wait for this chunk to be written to the client
let drained = false;
return this.drain(() => {
// Call the callback once the chunk is drained
if (!drained) {
drained = true;
callback();
}
// Return true here to signify that this chunk was written successfully to uWS
return drained;
});
}
}
// Trigger callback with an error if a write() is performed after response has completed
if (callback) callback(new Error('Response is already completed/aborted'));
return false;
}
/**
* Writes multiples chunks for the response to the client over uWS with backpressure handling if a callback is provided.
*
* @private
* @param {Array<Buffer>} chunks
* @param {Function} callback
*/
_writev(chunks, callback) {
// Serve the first chunk in the array
const reference = this;
this._write(chunks[0], null, (error) => {
// Pass the error to the callback if one was provided
if (error) return callback(error);
// Determine if we have more chunks after the first chunk we just served
if (chunks.length > 1) {
// Recursively serve the remaining chunks
reference._writev(chunks.slice(1), callback);
} else {
// Trigger the callback as all chunks have been served
callback();
}
});
}
/**
* This method is used to end the current request and send response with specified body and headers.
*
* @param {String|Buffer|ArrayBuffer=} body Optional
* @param {Boolean=} close_connection
* @returns {Response}
*/
send(body, close_connection) {
// Ensure response connection is still active
if (!this.#completed) {
// Initiate response to write status code and headers
this._initiate_response();
// Stop downloading further body chunks as we are done with the response
this.#wrapped_request._stop_streaming();
// Wait for any expected request body data to be fully received to prevent an ECONNRESET error
if (!this.#wrapped_request.received)
return this.#wrapped_request.once('received', () => this.send(body, close_connection));
// Attempt to write the body to the client and end the response
if (!this.#streaming && !body && !isNaN(this.#custom_content_length)) {
// Send the response with the uWS.HttpResponse.endWithoutBody(length, close_connection) method as we have no body data
// NOTE: This method is completely undocumented by uWS but exists in the source code to solve the problem of no body being sent with a custom content-length
this.#raw_response.endWithoutBody();
} else {
// Send the response with the uWS.HttpResponse.end(body, close_connection) method as we have some body data
this.#raw_response.end(body, close_connection);
}
// Emit the 'finish' event to signify that the response has been sent without streaming
if (!this.#streaming) this.emit('finish', this.#wrapped_request, this);
// Call any bound hooks for type 'complete' if no backpressure was built up
if (!this.#completed) {
// Mark request as completed if we were able to send response properly
this.#completed = true;
// Emit the 'close' event to signify that the response has been completed
this.emit('close', this.#wrapped_request, this);
}
}
return this;
}
/**
* @private
* Streams individual chunk from a stream.
* Delivers with chunked transfer without content-length header when no total_size is specified.
* Delivers with backpressure handling and content-length header when a total_size is specified.
*
* @param {Readable} stream
* @param {Buffer} chunk
* @param {Number=} total_size
*/
_stream_chunk(stream, chunk, total_size) {
// Ensure the client is still connected and request is pending
if (!this.#completed) {
// Attempt to stream the chunk using appropriate uWS.Response chunk serving method
// This will depend on whether a total_size is specified or not
let sent, finished;
if (total_size) {
// Attempt to stream the current chunk using uWS.tryEnd with a total size
const [ok, done] = this.#raw_response.tryEnd(chunk, total_size);
sent = ok;
finished = done;
} else {
// Attempt to stream the current chunk uWS.write()
sent = this.#raw_response.write(chunk);
// Since we are streaming without a total size, we are not finished
finished = false;
}
if (finished) {
// If streaming has finished, we can destroy the readable stream just to be safe
if (!stream.destroyed) stream.destroy();
} else if (!sent) {
// Pause the readable stream to prevent any further data from being read
stream.pause();
// Bind a drain handler which will resume the once the backpressure is cleared
this.drain(() => {
// Resume the stream if it is paused
if (stream.isPaused()) stream.resume();
// Return the write boolean to uWS based on whether the stream is active or not
return !stream.isPaused();
});
}
}
}
/**
* This method is used to serve a readable stream as response body and send response.
* By default, this method will use chunked encoding transfer to stream data.
* If your use-case requires a content-length header, you must specify the total payload size.
*
* @param {Readable} readable A Readable stream which will be consumed as response body
* @param {Number=} total_size Total size of the Readable stream source in bytes (Optional)
*/
stream(readable, total_size) {
// Ensure readable is an instance of a stream.Readable
if (!(readable instanceof Readable))
this.throw(
new Error('HyperExpress: Response.stream(readable, total_size) -> readable must be a Readable stream.')
);
// Do not allow streaming if response has already been aborted or completed
if (!this.#completed) {
// Bind an 'abort' event handler which will destroy the consumed stream if request is aborted
this.on('abort', () => {
if (!readable.destroyed) readable.destroy();
});
// Initiate response as we will begin writing body chunks
this._initiate_response();
// Bind a listener for the 'data' event to consume chunks
readable.on('data', (chunk) => this._stream_chunk(readable, chunk, total_size));
// Bind listeners to end request on stream closure if no total size was specified and thus we delivered with chunked transfer
if (total_size === undefined) {
const end_request = () => this.send();
readable.once('end', end_request);
}
}
}
/**
* Instantly aborts/closes current request without writing a status response code.
* Use this to instantly abort a request where a proper response with an HTTP status code is not neccessary.
*/
close() {
if (!this.#completed) {
this.#completed = true;
this._resume_if_paused();
this.#wrapped_request._stop_streaming();
this.#raw_response.close();
}
}
/**
* This method is used to redirect an incoming request to a different url.
*
* @param {String} url Redirect URL
* @returns {Boolean} Boolean
*/
redirect(url) {
if (!this.#completed) return this.status(302).header('location', url).send();
return false;
}
/**
* This method is an alias of send() method except it accepts an object and automatically stringifies the passed payload object.
*
* @param {Object} body JSON body
* @returns {Boolean} Boolean
*/
json(body) {
return this.type('json').send(JSON.stringify(body));
}
/**
* This method is an alias of send() method except it accepts an object
* and automatically stringifies the passed payload object with a callback name.
* Note! This method uses 'callback' query parameter by default but you can specify 'name' to use something else.
*
* @param {Object} body
* @param {String=} name
* @returns {Boolean} Boolean
*/
jsonp(body, name) {
let query_parameters = this.#wrapped_request.query_parameters;
let method_name = query_parameters['callback'] || name;
return this.type('js').send(`${method_name}(${JSON.stringify(body)})`);
}
/**
* This method is an alias of send() method except it automatically sets
* html as the response content type and sends provided html response body.
*
* @param {String} body
* @returns {Boolean} Boolean
*/
html(body) {
return this.type('html').send(body);
}
/**
* @private
* Sends file content with appropriate content-type header based on file extension from LiveFile.
*
* @param {LiveFile} live_file
* @param {function(Object):void} callback
*/
async _send_file(live_file, callback) {
// Wait for LiveFile to be ready before serving
if (!live_file.is_ready) await live_file.ready();
// Write appropriate extension type if one has not been written yet
if (!this.#type_written) this.type(live_file.extension);
// Send response with file buffer as body
this.send(live_file.buffer);
// Execute callback with cache pool, so user can expire as they wish.
if (callback) setImmediate(() => callback(FilePool));
}
/**
* This method is an alias of send() method except it sends the file at specified path.
* This method automatically writes the appropriate content-type header if one has not been specified yet.
* This method also maintains its own cache pool in memory allowing for fast performance.
* Avoid using this method to a send a large file as it will be kept in memory.
*
* @param {String} path
* @param {function(Object):void=} callback Executed after file has been served with the parameter being the cache pool.
*/
file(path, callback) {
// Send file from local cache pool if available
if (FilePool[path]) return this._send_file(FilePool[path], callback);
// Create new LiveFile instance in local cache pool for new file path
FilePool[path] = new LiveFile({
path,
});
// Assign error handler to live file
FilePool[path].on('error', (error) => this.throw(error));
// Serve file as response
this._send_file(FilePool[path], callback);
}
/**
* Writes approriate headers to signify that file at path has been attached.
*
* @param {String} path
* @param {String=} name
* @returns {Response}
*/
attachment(path, name) {
// Attach a blank content-disposition header when no filename is defined
if (path == undefined) return this.header('Content-Disposition', 'attachment');
// Parses path in to file name and extension to write appropriate attachment headers
let chunks = path.split('/');
let final_name = name || chunks[chunks.length - 1];
let name_chunks = final_name.split('.');
let extension = name_chunks[name_chunks.length - 1];
return this.header('content-disposition', `attachment; filename="${final_name}"`).type(extension);
}
/**
* Writes appropriate attachment headers and sends file content for download on user browser.
* This method combined Response.attachment() and Response.file() under the hood, so be sure to follow the same guidelines for usage.
*
* @param {String} path
* @param {String=} filename
*/
download(path, filename) {
return this.attachment(path, filename).file(path);
}
/**
* This method allows you to throw an error which will be caught by the global error handler (If one was setup with the Server instance).
*
* @param {Error} error
*/
throw(error) {
// Ensure error is an instance of Error
if (error instanceof Error) return this.#master_context.handlers.on_error(this.#wrapped_request, this, error);
// If error is not an instance of Error, throw a warning error
throw new Error('HyperExpress: Response.throw() expects an instance of an Error.');
}
/* Response Getters */
/**
* Returns the underlying raw uWS.Response object.
* @returns {uWebsockets.Response}
*/
get raw() {
return this.#raw_response;
}
/**
* Returns the HyperExpress.Server instance this Response object originated from.
*
* @returns {Server}
*/
get app() {
return this.#master_context;
}
/**
* Returns whether response has been initiated by writing the HTTP status code and headers.
* Note! No changes can be made to the HTTP status code or headers after a response has been initiated.
* @returns {Boolean}
*/
get initiated() {
return this.#initiated;
}
/**
* Returns current state of request in regards to whether the source is still connected.
* @returns {Boolean}
*/
get aborted() {
return this.#completed;
}
/**
* Alias of aborted property as they both represent the same request state in terms of inaccessibility.
* @returns {Boolean}
*/
get completed() {
return this.#completed;
}
/**
* Upgrade socket context for upgrade requests.
* @returns {uWebsockets.ux_socket_context}
*/
get upgrade_socket() {
return this.#upgrade_socket;
}
/**
* Returns a "Server-Sent Events" connection object to allow for SSE functionality.
* This property will only be available for GET requests as per the SSE specification.
*
* @returns {SSEventStream=}
*/
get sse() {
// Return a new SSE instance if one has not been created yet
if (this.#wrapped_request.method === 'GET') {
// Create new SSE instance if one has not been created yet
if (this.#sse === undefined) this.#sse = new SSEventStream(this);
return this.#sse;
}
}
/**
* Returns the current response body content write offset in bytes.
* Use in conjunction with the drain() offset handler to retry writing failed chunks.
* @returns {Number}
*/
get write_offset() {
return this.#completed ? -1 : this.#raw_response.getWriteOffset();
}
/* ExpressJS compatibility properties & methods */
/**
* Throws a descriptive error when an unsupported ExpressJS property/method is invocated.
* @private
* @param {String} name
*/
_throw_unsupported(name) {
this.throw(
new Error(
`HyperExpress: One of your middlewares or logic tried to call Response.${name} which is unsupported with HyperExpress.`
)
);
}
/**
* ExpressJS: Alias of Response.initated
*/
get headersSent() {
return this.#initiated;
}
/**
* ExpressJS: Alias of Response.status_code to expose response status code
*/
get statusCode() {
return this.#completed ? this.#status_code : undefined;
}
/**
* ExpressJS: Alias of Response.status_code to expose setter response status code
*/
set statusCode(code) {
this.status(code);
}
/**
* ExpressJS: Alias of header() method
* @param {String} name
* @param {String|Array} values
*/
append(name, values) {
return this.header(name, values);
}
/**
* ExpressJS: Alias of Response.append()
*/
setHeader(name, values) {
return this.append(name, values);
}
/**
* ExpressJS: Writes multiple headers in form of an object
* @param {Object} headers
*/
writeHeaders(headers) {
Object.keys(headers).forEach((name) => this.header(name, headers[name]));
}
/**
* ExpressJS: Alias of Response.writeHeaders
* @param {Object} headers
*/
setHeaders(headers) {
this.writeHeaders(headers);
}
/**
* ExpressJS: Writes multiple header values for a single name
* @param {String} name
* @param {Array} values
*/
writeHeaderValues(name, values) {
values.forEach((value) => this.header(name, value));
}
/**
* ExpressJS: Returns pending header from this response
* @param {String} name
* @returns {String|Array|undefined}
*/
getHeader(name) {
return this.#headers ? this.#headers[name] : undefined;
}
/**
* ExpressJS: Returns all pending headers from this response
* @returns {Object|undefined}
*/
getHeaders() {
const headers = {};
Object.keys(this.#headers).forEach((key) => {
headers[key] = this.#headers[key].join(',');
});
return headers;
}
/**
* ExpressJS: Removes header from this response
* @param {String} name
*/
removeHeader(name) {
if (this.#headers) delete this.#headers[name];
}
/**
* ExpressJS: Alias of Response.cookie()
* @param {String} name
* @param {String} value
* @param {Object} options
*/
setCookie(name, value, options) {
return this.cookie(name, value, null, options);
}
/**
* ExpressJS: checks if a cookie exists
* @param {String} name
* @returns {Boolean}
*/
hasCookie(name) {
return this.#cookies && this.#cookies[name] !== undefined;
}
/**
* ExpressJS: Alias of Response.cookie(name, null) method.
* @param {String} name
*/
removeCookie(name) {
return this.cookie(name, null);
}
/**
* ExpressJS: Alias of Response.cookie(name, null) method.
* @param {String} name
*/
clearCookie(name) {
return this.cookie(name, null);
}
/**
* ExpressJS: Alias of Response.send()
*/
end(data) {
return this.send(data);
}
/**
* Unsupported method
*/
format() {
this._throw_unsupported('format()');
}
/**
* ExpressJS: Returns the HTTP response header specified by field. The match is case-insensitive.
* @param {String} name
* @returns {String|Array}
*/
get(name) {
if (this.#headers) {
let values = this.#headers[name];
if (values) return values.length == 0 ? values[0] : values;
}
}
/**
* ExpressJS: Joins the links provided as properties of the parameter to populate the response’s Link HTTP header field.
* @param {Object} links
* @returns {String}
*/
links(links) {
if (typeof links !== 'object' || links == null)
this.throw(new Error('HyperExpress: Response.links(links) -> links must be an Object'));
// Build chunks of links and combine into header spec
let chunks = [];
Object.keys(links).forEach((rel) => {
let url = links[rel];
chunks.push(`<${url}>; rel="${rel}"`);
});
return chunks.join(', ');
}
/**
* ExpressJS: Sets the response Location HTTP header to the specified path parameter.
* @param {String} path
*/
location(path) {
return this.header('location', path);
}
/**
* Unsupported method
*/
render() {
this._throw_unsupported('render()');
}
/**
* ExpressJS: Alias of Response.file()
* @param {String} path
*/
sendFile(path) {
return this.file(path);
}
/**
* ExpressJS: Alias of Response.status()
* @param {Number} status_code
*/
sendStatus(status_code) {
return this.status(status_code);
}
/**
* ExpressJS: Sets the response’s HTTP header field to value. To set multiple fields at once, pass an object as the parameter.
* @param {String|Object} object
* @param {(String|Array)=} value
*/
set(field, value) {
if (typeof field == 'object') {
const reference = this;
Object.keys(field).forEach((name) => {
let value = field[name];
reference.header(field, value);
});
} else {
this.header(field, value);
}
}
/**
* ExpressJS: Adds the field to the Vary response header, if it is not there already.
* @param {String} name