Skip to content

Commit

Permalink
refactor: remove response stream in favor of res.end() (#225)
Browse files Browse the repository at this point in the history
* refactor: remove response stream in favor of res.end()

* refactor: responder should return a curried function
  • Loading branch information
zacharygolba authored Jul 15, 2016
1 parent a8a5406 commit d8ef797
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 71 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This isn't another wrapper around [Express](http://expressjs.com/) or a framewor
* Automatic pagination, sorting, filtering via query params in controllers
* CLI for eliminating boiler plate
* [JSON API](http://jsonapi.org/) 1.0 compliant out of the box
* Chunked/Streamed JSON responses to client over http
* Optimized database queries based on serialized attributes and associations
* Highly extensible - just write reusable JavaScript functions
* Pairs nicely with client-side JavaScript applications 🍷
Expand Down
21 changes: 4 additions & 17 deletions src/packages/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import http from 'http';
import { parse as parseURL } from 'url';

import responder from './responder';
import { createResponder } from './responder';

import entries from '../../utils/entries';
import tryCatch from '../../utils/try-catch';
Expand Down Expand Up @@ -70,6 +70,7 @@ class Server {

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

tryCatch(async () => {
const { logger } = this;
Expand Down Expand Up @@ -108,22 +109,8 @@ class Server {
startTime
});

this.sendResponse(req, res, await this.router.visit(req, res));
}, err => {
this.sendResponse(req, res, err);
});
}

sendResponse(
req: IncomingMessage,
res: ServerResponse,
data: void | ?mixed
): void {
if (data && typeof data.pipe === 'function') {
data.pipe(res);
} else {
responder.resolve(req, res, data);
}
respond(await this.router.visit(req, res));
}, respond);
}
}

Expand Down
38 changes: 17 additions & 21 deletions src/packages/server/responder/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
// @flow
import Response from './response';

import normalize from './utils/normalize';

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

export function resolve(
export function createResponder(
req: IncomingMessage,
res: ServerResponse,
data: ?mixed | void
): void {
new Response()
.once('ready', (stream: Response) => {
const { normalized, ...meta } = normalize(data);
let { statusCode } = meta;
res: ServerResponse
): (data: ?mixed | void) => void {
return function respond(data: ?mixed | void): void {
const { normalized, ...meta } = normalize(data);
let { statusCode } = meta;

if (statusCode === 200 && req.method === 'POST') {
statusCode++;
}
if (statusCode === 200 && req.method === 'POST') {
statusCode++;
}

res.statusCode = statusCode;
stream.end(normalized);
})
.pipe(res);
}
res.statusCode = statusCode;

export default {
resolve
};
if (typeof normalized === 'string') {
res.end(normalized);
} else {
res.end(JSON.stringify(normalized));
}
};
}
32 changes: 0 additions & 32 deletions src/packages/server/responder/response/index.js

This file was deleted.

0 comments on commit d8ef797

Please sign in to comment.