Comparing version 2.2.1 to 3.0.1
20
error.js
var sendJson = require("./json") | ||
var isSendObject = require("./is-send-object.js") | ||
module.exports = sendError | ||
function sendError(req, res, opts) { | ||
if (!opts.statusCode || !opts.body || !opts.headers) { | ||
function sendError(req, res, opts, callback) { | ||
if (!isSendObject(opts)) { | ||
opts = { body: opts, statusCode: 500 } | ||
@@ -11,12 +12,9 @@ } | ||
var error = opts.body | ||
var statusCode = opts.statusCode | ||
var headers = opts.headers | ||
var body | ||
if (Array.isArray(error)) { | ||
body = { errors: error } | ||
opts.body = { errors: error } | ||
} else if (typeof error === "string") { | ||
body = { errors: [{ message: error, attribute: "general" }] } | ||
opts.body = { errors: [{ message: error, attribute: "general" }] } | ||
} else if (error && typeof error.message === "string") { | ||
body = { errors: [ | ||
opts.body = { errors: [ | ||
{ message: error.message, attribute: "general" } | ||
@@ -26,7 +24,3 @@ ] } | ||
sendJson(req, res, { | ||
statusCode: statusCode, | ||
body: body, | ||
headers: headers | ||
}) | ||
sendJson(req, res, opts, callback) | ||
} |
12
html.js
@@ -5,11 +5,11 @@ var send = require("./index") | ||
function sendHtml(req, res, html) { | ||
if (typeof html !== "object") { | ||
html = { body: html } | ||
function sendHtml(req, res, opts, callback) { | ||
if (typeof opts === "string") { | ||
opts = { body: opts } | ||
} | ||
html.headers = html.headers || {} | ||
html.headers["Content-Type"] = "text/html" | ||
opts.headers = opts.headers || {} | ||
opts.headers["Content-Type"] = "text/html" | ||
send(req, res, html) | ||
send(req, res, opts, callback) | ||
} |
52
index.js
var Buffer = require("buffer").Buffer | ||
var zlib = require("zlib") | ||
var isGzip = /\bgzip\b/ | ||
module.exports = send | ||
/* type SendData := Buffer | String | { | ||
headers?: Object<String, String>, | ||
body: Buffer | String, | ||
statusCode?: Number | ||
} | ||
*/ | ||
// send := (HttpRequest, HttpResponse, SendData) | ||
function send(req, res, body) { | ||
var headers | ||
var statusCode | ||
function send(req, res, opts, callback) { | ||
var headers = opts.headers || {} | ||
var statusCode = opts.statusCode || null | ||
var body = typeof opts === "object" ? opts.body : opts | ||
var gzip = opts.gzip || false | ||
if (typeof body === "object") { | ||
statusCode = body.statusCode | ||
headers = body.headers | ||
body = body.body | ||
} | ||
body = Buffer.isBuffer(body) ? body : new Buffer(body || "") | ||
headers = headers || {} | ||
headers["Content-Length"] = body.length | ||
res.statusCode = statusCode || res.statusCode | ||
@@ -31,3 +22,28 @@ | ||
}) | ||
res.end(body) | ||
if (gzip && acceptsGzip(req)) { | ||
zlib.gzip(body, function (err, body) { | ||
if (err) { | ||
return callback(err) | ||
} | ||
res.once("finish", callback) | ||
res.setHeader("Content-Length", body.length) | ||
res.end(body) | ||
}) | ||
} else { | ||
if (callback) { | ||
res.once("finish", callback) | ||
} | ||
res.setHeader("Content-Length", body.length) | ||
res.end(body) | ||
} | ||
} | ||
function acceptsGzip(req) { | ||
var acceptEncoding = req.headers["accept-encoding"] || "" | ||
return !!acceptEncoding.match(isGzip) | ||
} |
15
json.js
var send = require("./index") | ||
var isSendObject = require("./is-send-object") | ||
module.exports = sendJson | ||
function sendJson(req, res, value, opts) { | ||
if (!value || (!value.statusCode && !value.headers)) { | ||
value = { body: value } | ||
function sendJson(req, res, opts, callback) { | ||
if (!isSendObject(opts)) { | ||
opts = { body: opts } | ||
} | ||
@@ -15,8 +16,8 @@ | ||
value.headers = value.headers || {} | ||
value.body = JSON.stringify(value.body, | ||
opts.headers = opts.headers || {} | ||
opts.body = JSON.stringify(opts.body, | ||
opts.replacer || null, opts.space || "") | ||
value.headers["Content-Type"] = "application/json" | ||
opts.headers["Content-Type"] = "application/json" | ||
send(req, res, value) | ||
send(req, res, opts, callback) | ||
} |
{ | ||
"name": "send-data", | ||
"version": "2.2.1", | ||
"version": "3.0.1", | ||
"description": "send data through response", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -64,3 +64,2 @@ var test = require("tape") | ||
request("/json", function (err, res, body) { | ||
console.log("wut", res.statusCode) | ||
var data = JSON.parse(body) | ||
@@ -67,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
10030
13
178