Comparing version 0.1.0 to 1.0.1
66
index.js
@@ -1,56 +0,32 @@ | ||
var forEach = require("iterators").forEachSync | ||
var Buffer = require("buffer").Buffer | ||
module.exports = send | ||
send.json = sendJson | ||
send.html = sendHtml | ||
function sendJson(req, res, data) { | ||
if (!data.statusCode && !data.headers) { | ||
data = { data: data } | ||
/* type SendData := Buffer | String | { | ||
headers?: Object<String, String>, | ||
body: Buffer | String, | ||
statusCode?: Number | ||
} | ||
data.headers = data.headers || {} | ||
data.data = JSON.stringify(data.data) | ||
data.headers["content-type"] = "application/json" | ||
send(req, res, data) | ||
} | ||
function sendHtml(req, res, data) { | ||
if (typeof data !== "object") { | ||
data = { data: data } | ||
} | ||
data.headers = data.headers || {} | ||
data.headers["content-type"] = "text/html" | ||
send(req, res, data) | ||
} | ||
function send(req, res, data) { | ||
*/ | ||
// send := (HttpRequest, HttpResponse, SendData) | ||
function send(req, res, body) { | ||
var headers | ||
var statusCode | ||
, headers = {} | ||
if (typeof data === "object") { | ||
statusCode = data.statusCode | ||
headers = data.headers || {} | ||
data = data.data | ||
if (typeof body === "object") { | ||
statusCode = body.statusCode | ||
headers = body.headers | ||
body = body.body | ||
} | ||
if (!Buffer.isBuffer(data)) { | ||
data = new Buffer(data) | ||
} | ||
body = Buffer.isBuffer(body) ? body : new Buffer(body) | ||
headers = headers || {} | ||
headers["content-length"] = data.length | ||
headers["Content-Length"] = body.length | ||
res.statusCode = statusCode || res.statusCode | ||
res.statusCode = statusCode || res.statusCode || 200 | ||
forEach(headers, writeHeader, res) | ||
res.end(data) | ||
Object.keys(headers).forEach(function (header) { | ||
res.setHeader(header, headers[header]) | ||
}) | ||
res.end(body) | ||
} | ||
function writeHeader(value, headerName) { | ||
this.setHeader(headerName, value) | ||
} |
{ | ||
"name": "send-data", | ||
"version": "0.1.0", | ||
"version": "1.0.1", | ||
"description": "send data through response", | ||
@@ -19,8 +19,7 @@ "keywords": [], | ||
}, | ||
"dependencies": { | ||
"iterators": "~0.3.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tap": "~0.3.1", | ||
"test-server": "0.0.1" | ||
"test-server": "~0.1.1", | ||
"tape": "~0.3.3" | ||
}, | ||
@@ -34,4 +33,4 @@ "licenses": [ | ||
"scripts": { | ||
"test": "tap --stderr ./test" | ||
"test": "node ./test/integration.js" | ||
} | ||
} |
@@ -9,5 +9,5 @@ # send-data [![build status][1]][2] | ||
var send = require("send-data") | ||
, sendJson = send.json | ||
, sendHtml = send.html | ||
, http = require("http") | ||
var sendJson = require("send-data/json") | ||
var sendHtml = require("send-data/html") | ||
var http = require("http") | ||
@@ -17,3 +17,3 @@ http.createServer(function handleRequest(req, res) { | ||
send(req, res, { | ||
data: "foo" | ||
body: "foo" | ||
, statusCode: 202 | ||
@@ -28,3 +28,3 @@ , headers: { | ||
sendJson(req, res, { | ||
data: { | ||
body: { | ||
foo: "bar" | ||
@@ -40,3 +40,3 @@ } | ||
sendHtml(req, res, { | ||
data: "<div>foo</div>" | ||
body: "<div>foo</div>" | ||
, statusCode: 200 | ||
@@ -65,3 +65,3 @@ , headers: {} | ||
[1]: https://secure.travis-ci.org/Raynos/routil-send.png | ||
[2]: http://travis-ci.org/Raynos/routil-send | ||
[1]: https://secure.travis-ci.org/Raynos/send-data.png | ||
[2]: http://travis-ci.org/Raynos/send-data |
@@ -1,6 +0,6 @@ | ||
var test = require("tap").test | ||
, send = require("..") | ||
, testServer = require("test-server") | ||
, sendJson = send.json | ||
, sendHtml = send.html | ||
var test = require("tape") | ||
var send = require("..") | ||
var testServer = require("test-server") | ||
var sendJson = require("../json") | ||
var sendHtml = require("../html") | ||
@@ -12,5 +12,5 @@ testServer(handleRequest, startTest) | ||
send(req, res, { | ||
data: "foo" | ||
, statusCode: 202 | ||
, headers: { | ||
body: "foo", | ||
statusCode: 202, | ||
headers: { | ||
bar: "baz" | ||
@@ -23,6 +23,6 @@ } | ||
sendJson(req, res, { | ||
data: { | ||
body: { | ||
foo: "bar" | ||
} | ||
, statusCode: 201 | ||
}, | ||
statusCode: 201 | ||
}) | ||
@@ -35,5 +35,5 @@ } else if (req.url === "/json/optional") { | ||
sendHtml(req, res, { | ||
data: "<div>foo</div>" | ||
, statusCode: 200 | ||
, headers: {} | ||
body: "<div>foo</div>", | ||
statusCode: 200, | ||
headers: {} | ||
}) | ||
@@ -110,2 +110,2 @@ } else if (req.url === "/html/optional") { | ||
.on("end", done) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7673
0
9
149
0
63
3
- Removediterators@~0.3.0
- Removediterators@0.3.3(transitive)