Skip to content

Commit

Permalink
✨ Allow ending a non-file-serve Conduit response with a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Dec 14, 2023
1 parent 3514c29 commit c52cf55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.22 (WIP)

* Allow ending a non-file-serve `Conduit` response with a stream

## 1.3.21 (2023-11-27)

* Add `Schema#addComputedField()` method
Expand Down
68 changes: 53 additions & 15 deletions lib/class/conduit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,10 @@ Conduit.setMethod(function end(message) {

if (typeof message !== 'string') {

if (message && message instanceof Classes.Stream.Stream) {
return this._endWithStream(message);
}

// Use regular JSON if DRY has been disabled in settings
if (alchemy.settings.json_dry_response === false || this.json_dry === false) {
json_type = 'json';
Expand Down Expand Up @@ -1878,12 +1882,29 @@ Conduit.setMethod(function end(message) {
this._end(message, 'utf-8');
});

/**
* End with a stream
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.22
* @version 1.3.22
*/
Conduit.setMethod(function _endWithStream(stream) {

this.ended = new Date();
this.emit('ending');

this.flushHeaders();

stream.pipe(this.response);
});

/**
* Call the actual end method
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 0.2.0
* @version 1.3.14
* @version 1.3.22
*/
Conduit.setMethod(function _end(message, encoding = 'utf-8') {

Expand All @@ -1906,18 +1927,42 @@ Conduit.setMethod(function _end(message, encoding = 'utf-8') {
return;
}

let headers = [],
value,
key;
// Set the content-length if it hasn't been set yet
if (arguments.length > 0 && !this.response_headers['content-length']) {
this.response_headers['content-length'] = Buffer.byteLength(message);
}

this.flushHeaders();

if (arguments.length === 0) {
return this.response.end();
}

// End the response
return this.response.end(message, encoding);
});

/**
* Flush the headers
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.22
* @version 1.3.22
*/
Conduit.setMethod(function flushHeaders() {

if (this._flushed) {
return;
}

this._flushed = true;

if (this.status) {
this.response.statusCode = this.status;
}

// Set the content-length if it hasn't been set yet
if (arguments.length > 0 && !this.response_headers['content-length']) {
this.response_headers['content-length'] = Buffer.byteLength(message);
}
let value,
key;

for (key in this.response_headers) {
value = this.response_headers[key];
Expand All @@ -1935,13 +1980,6 @@ Conduit.setMethod(function _end(message, encoding = 'utf-8') {

// Write the actual headers
this.response.writeHead(this.status);

if (arguments.length === 0) {
return this.response.end();
}

// End the response
return this.response.end(message, encoding);
});

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alchemymvc",
"description": "MVC framework for Node.js",
"version": "1.3.21",
"version": "1.3.22-alpha",
"author": "Jelle De Loecker <jelle@elevenways.be>",
"keywords": [
"alchemy",
Expand Down

0 comments on commit c52cf55

Please sign in to comment.