Skip to content

Commit d8ef797

Browse files
authored
refactor: remove response stream in favor of res.end() (#225)
* refactor: remove response stream in favor of res.end() * refactor: responder should return a curried function
1 parent a8a5406 commit d8ef797

File tree

4 files changed

+21
-71
lines changed

4 files changed

+21
-71
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ This isn't another wrapper around [Express](http://expressjs.com/) or a framewor
2020
* Automatic pagination, sorting, filtering via query params in controllers
2121
* CLI for eliminating boiler plate
2222
* [JSON API](http://jsonapi.org/) 1.0 compliant out of the box
23-
* Chunked/Streamed JSON responses to client over http
2423
* Optimized database queries based on serialized attributes and associations
2524
* Highly extensible - just write reusable JavaScript functions
2625
* Pairs nicely with client-side JavaScript applications 🍷

src/packages/server/index.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import http from 'http';
33
import { parse as parseURL } from 'url';
44

5-
import responder from './responder';
5+
import { createResponder } from './responder';
66

77
import entries from '../../utils/entries';
88
import tryCatch from '../../utils/try-catch';
@@ -70,6 +70,7 @@ class Server {
7070

7171
receiveRequest(req: IncomingMessage, res: ServerResponse): void {
7272
const startTime = Date.now();
73+
const respond = createResponder(req, res);
7374

7475
tryCatch(async () => {
7576
const { logger } = this;
@@ -108,22 +109,8 @@ class Server {
108109
startTime
109110
});
110111

111-
this.sendResponse(req, res, await this.router.visit(req, res));
112-
}, err => {
113-
this.sendResponse(req, res, err);
114-
});
115-
}
116-
117-
sendResponse(
118-
req: IncomingMessage,
119-
res: ServerResponse,
120-
data: void | ?mixed
121-
): void {
122-
if (data && typeof data.pipe === 'function') {
123-
data.pipe(res);
124-
} else {
125-
responder.resolve(req, res, data);
126-
}
112+
respond(await this.router.visit(req, res));
113+
}, respond);
127114
}
128115
}
129116

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
// @flow
2-
import Response from './response';
3-
42
import normalize from './utils/normalize';
53

64
import type { IncomingMessage, ServerResponse } from 'http';
75

8-
export function resolve(
6+
export function createResponder(
97
req: IncomingMessage,
10-
res: ServerResponse,
11-
data: ?mixed | void
12-
): void {
13-
new Response()
14-
.once('ready', (stream: Response) => {
15-
const { normalized, ...meta } = normalize(data);
16-
let { statusCode } = meta;
8+
res: ServerResponse
9+
): (data: ?mixed | void) => void {
10+
return function respond(data: ?mixed | void): void {
11+
const { normalized, ...meta } = normalize(data);
12+
let { statusCode } = meta;
1713

18-
if (statusCode === 200 && req.method === 'POST') {
19-
statusCode++;
20-
}
14+
if (statusCode === 200 && req.method === 'POST') {
15+
statusCode++;
16+
}
2117

22-
res.statusCode = statusCode;
23-
stream.end(normalized);
24-
})
25-
.pipe(res);
26-
}
18+
res.statusCode = statusCode;
2719

28-
export default {
29-
resolve
30-
};
20+
if (typeof normalized === 'string') {
21+
res.end(normalized);
22+
} else {
23+
res.end(JSON.stringify(normalized));
24+
}
25+
};
26+
}

src/packages/server/responder/response/index.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)