This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

3
book/node_modules/body/.npmignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
node_modules
*.log
*.err

3
book/node_modules/body/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,3 @@
language: node_js
node_js:
- 0.10

19
book/node_modules/body/LICENCE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (c) 2012 Raynos.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

303
book/node_modules/body/README.md generated vendored Normal file
View File

@@ -0,0 +1,303 @@
# body [![build status][1]][2]
Body parsing
Originally taken from [npm-www](https://github.com/isaacs/npm-www)
## Example
```js
var textBody = require("body")
var jsonBody = require("body/json")
var formBody = require("body/form")
var anyBody = require("body/any")
var http = require("http")
var sendJson = require("send-data/json")
http.createServer(function handleRequest(req, res) {
function send(err, body) {
sendJson(req, res, body)
}
if (req.url === "/body") {
// all functions can be called with (req, cb)
textBody(req, send)
} else if (req.url === "/form") {
// all functions can be called with (req, opts, cb)
formBody(req, {}, send)
} else if (req.url === "/json") {
// all functions can be called with (req, res, cb)
jsonBody(req, res, send)
} else if (req.url === "/any") {
// all functions can be called with (req, res, opts, cb)
anyBody(req, res, {}, send)
}
})
```
`body` simply parses the request body and returns it in the callback. `jsonBody` and `formBody` call JSON.parse and querystring.parse respectively on the body.
anyBody will detect the content-type of the request and use the appropiate body method.
## Example generators
You can use `body` with generators as the body functions will
return a continuable if you don't pass a callback.
```js
var http = require("http")
var Router = require("routes-router")
var jsonBody = require("body/json")
var formBody = require("body/form")
// async turns a generator into an async function taking a cb
var async = require("gens")
// the router works with normal async functions.
// router automatically handles errors as 500 responses
var app = Router({
// do whatever you want. the jsonBody error would go here
errorHandler: function (req, res, err) {
res.statusCode = 500
res.end(err.message)
}
})
app.addRoute("/json", async(function* (req, res) {
// if jsonBody has an error it just goes to the cb
// in the called in the router. and it does the correct thing
// it shows your 500 page.
var body = yield jsonBody(req, res)
res.setHeader("content-type", "application/json")
res.end(JSON.stringify(body))
}))
app.addRoute("/form", async(function* (req, res) {
var body = yield formBody(req, res)
res.setHeader("content-type", "application/json")
res.end(JSON.stringify(body))
}))
// app returned from the router is just a function(req, res) {}
// that dispatches the req/res to the correct route based on
// the routers routing table & req.url
http.createServer(app).listen(8080)
```
## Documentation
### `textBody(req, res?, opts?, cb<Error, String>)`
```ocaml
textBody := (
req: HttpRequest,
res?: HttpResponse,
opts?: {
limit?: Number,
cache?: Boolean,
encoding?: String
},
cb: Callback<err: Error, bodyPayload: String>
) => void
```
`textBody` allows you to get the body from any readable stream.
It will read the entire content of the stream into memory and
give it back to you in the callback.
- `limit`: You can set `opts.limit` to a custom number to change the
limit at which `textBody` gives up. By default it will only
read a 1MB body, if a stream contains more then 1MB it returns
an error. This prevents someone attacking your HTTP server
with an infinite body causing an out of memory attack.
- `encoding`: You can set `encoding`. All encodings that are valid on a
[`Buffer`](http://nodejs.org/api/buffer.html#buffer_buffer) are
valid options. It defaults to `'utf8'`
```js
var textBody = require("body")
var http = require("http")
http.createServer(function (req, res) {
textBody(req, res, function (err, body) {
// err probably means invalid HTTP protocol or some shiz.
if (err) {
res.statusCode = 500
return res.end("NO U")
}
// I am an echo server
res.end(body)
})
}).listen(8080)
```
### `formBody(req, res?, opts?, cb<Error, Any>)`
```ocaml
formBody := (
req: HttpRequest,
res?: HttpResponse,
opts?: {
limit?: Number,
encoding?: String,
querystring: {
parse: (String, Callback<Error, Any>) => void
}
},
cb: Callback<err: Error, bodyPayload: Any>
) => void
```
`formBody` allows you to get the body of a readable stream. It
does the same as `textBody` but assumes the content is querystring
encoded and parses just like it was a &lt;form&gt; submit.
- `limit`: same as `textBody`
- `encoding`: same as `textBody`
- `querystring`: You can pass a custom querystring parser if
you want. It should have a `parse` method that takes a
string and a callback. It should return the value in the
callback or a parsing error
```js
var formBody = require("body/form")
var http = require("http")
http.createServer(function (req, res) {
formBody(req, res, function (err, body) {
// err probably means invalid HTTP protocol or some shiz.
if (err) {
res.statusCode = 500
return res.end("NO U")
}
// I am an echo server
res.setHeader("content-type", "application/json")
res.end(JSON.stringify(body))
})
}).listen(8080)
```
### `jsonBody(req, res?, opts?, cb<Error, Any>)`
```ocaml
jsonBody := (
req: HttpRequest,
res?: HttpResponse,
opts?: {
limit?: Number,
encoding?: String,
reviver?: (Any) => Any
JSON?: {
parse: (String, reviver?: Function, Callback<Error, Any>) => void
}
},
cb: Callback<err: Error, bodyPayload: Any>
) => void
```
`jsonBody` allows you to get the body of a readable stream. It
does the same as `textbody` but assumes the content it a JSON
value and parses it using `JSON.parse`. If `JSON.parse` throws
an exception then it calls the callback with the exception.
- `limit`: same as `textBody`
- `encoding`: same as `textBody`
- `reviver`: A reviver function that will be passed to `JSON.parse`
as the second argument
- `JSON`: You can pass a custom JSON parser if you want.
It should have a `parse` method that takes a string, an
optional reviver and a callback. It should return the value
in the callback or a parsing error.
```js
var jsonBody = require("body/json")
var http = require("http")
http.createServer(function (req, res) {
jsonBody(req, res, function (err, body) {
// err is probably an invalid json error
if (err) {
res.statusCode = 500
return res.end("NO U")
}
// I am an echo server
res.setHeader("content-type", "application/json")
res.end(JSON.stringify(body))
})
}).listen(8080)
```
### `anyBody(req, res?, opts?, cb<Error, Any>)`
```ocaml
anyBody := (
req: HttpRequest,
res?: HttpResponse,
opts?: {
limit?: Number,
encoding?: String,
reviver?: (Any) => Any
JSON?: {
parse: (String, reviver?: Function, Callback<Error, Any>) => void
},
querystring: {
parse: (String, Callback<Error, Any>) => void
}
},
cb: Callback<err: Error, bodyPayload: Any>
) => void
```
`anyBody` allows you to get the body of a HTTPRequest. It
does the same as `textBody` except it parses the `content-type`
header and uses either the jsonBody or the formBody function.
This allows you to write POST route handlers that work with
both ajax and html form submits.
- `limit`: same as `textBody`
- `encoding`: same as `textBody`
- `reviver`: same as `jsonBody`
- `JSON`: same as `jsonBody`
- `querystring`: same as `formBody`
```js
var anyBody = require("body/any")
var http = require("http")
http.createServer(function (req, res) {
anyBody(req, res, function (err, body) {
// err is probably an invalid json error
if (err) {
res.statusCode = 500
return res.end("NO U")
}
// I am an echo server
res.setHeader("content-type", "application/json")
res.end(JSON.stringify(body))
})
}).listen(8080)
```
## Installation
`npm install body`
## Tests
`npm test`
## Contributors
- Raynos
## MIT Licenced
[1]: https://secure.travis-ci.org/Raynos/body.png
[2]: http://travis-ci.org/Raynos/body

38
book/node_modules/body/any.js generated vendored Normal file
View File

@@ -0,0 +1,38 @@
var TypedError = require("error/typed")
var parseArguments = require("./parse-arguments.js")
var jsonBody = require("./json.js")
var formBody = require("./form.js")
var jsonType = "application/json"
var formType = "application/x-www-form-urlencoded"
var INVALID_CONTENT_TYPE = TypedError({
message: "Could not parse content type header: {contentType}",
type: "invalid.content.type",
statusCode: 415,
contentType: null
})
module.exports = anyBody
function anyBody(req, res, opts, callback) {
var args = parseArguments(req, res, opts, callback)
req = args.req
res = args.res
opts = args.opts
callback = args.callback
if (!callback) {
return anyBody.bind(null, req, res, opts)
}
var contentType = req.headers["content-type"] || ""
if (contentType.indexOf(jsonType) !== -1) {
jsonBody(req, res, opts, callback)
} else if (contentType.indexOf(formType) !== -1) {
formBody(req, res, opts, callback)
} else {
callback(INVALID_CONTENT_TYPE({contentType: contentType}))
}
}

33
book/node_modules/body/form.js generated vendored Normal file
View File

@@ -0,0 +1,33 @@
var querystringParse = require("querystring").parse
var body = require("./index.js")
var parseArguments = require("./parse-arguments.js")
module.exports = formBody
function formBody(req, res, opts, callback) {
var args = parseArguments(req, res, opts, callback)
req = args.req
res = args.res
opts = args.opts
callback = args.callback
if (!callback) {
return formBody.bind(null, req, res, opts)
}
var parse = opts.querystring ?
opts.querystring.parse : defaultQueryStringParse
body(req, res, opts, function (err, body) {
if (err) {
return callback(err)
}
parse(body, callback)
})
}
function defaultQueryStringParse(str, callback) {
callback(null, querystringParse(str))
}

47
book/node_modules/body/index.js generated vendored Normal file
View File

@@ -0,0 +1,47 @@
var rawBody = require("raw-body")
var cache = require("continuable-cache")
var parseArguments = require("./parse-arguments.js")
var ONE_MB = 1024 * 1024
var THUNK_KEY = '__npm_body_thunk_cache__';
module.exports = body
function parseBodyThunk(req, res, opts) {
return function thunk(callback) {
var limit = "limit" in opts ? opts.limit : ONE_MB
var contentLength = req.headers ?
Number(req.headers["content-length"]) : null;
rawBody(req, {
limit: limit,
length: contentLength,
encoding: "encoding" in opts ? opts.encoding : true
}, callback);
};
}
function body(req, res, opts, callback) {
var args = parseArguments(req, res, opts, callback)
req = args.req
res = args.res
opts = args.opts
callback = args.callback
var thunk;
if (opts.cache) {
var thunk = req[THUNK_KEY] ||
cache(parseBodyThunk(req, res, opts));
req[THUNK_KEY] = thunk;
} else {
thunk = parseBodyThunk(req, res, opts);
}
if (!callback) {
return thunk;
}
thunk(callback);
}

29
book/node_modules/body/json.js generated vendored Normal file
View File

@@ -0,0 +1,29 @@
var jsonParse = require("safe-json-parse")
var body = require("./index.js")
var parseArguments = require("./parse-arguments.js")
module.exports = jsonBody
function jsonBody(req, res, opts, callback) {
var args = parseArguments(req, res, opts, callback)
req = args.req
res = args.res
opts = args.opts
callback = args.callback
if (!callback) {
return jsonBody.bind(null, req, res, opts)
}
var parse = opts.JSON ? opts.JSON.parse : jsonParse
var reviver = opts.reviver || null
body(req, res, opts, function (err, body) {
if (err) {
return callback(err)
}
parse(body, reviver, callback)
})
}

42
book/node_modules/body/package.json generated vendored Normal file
View File

@@ -0,0 +1,42 @@
{
"name": "body",
"version": "5.1.0",
"description": "Body parsing",
"keywords": [],
"author": "Raynos <raynos2@gmail.com>",
"repository": "git://github.com/Raynos/body.git",
"main": "index",
"homepage": "https://github.com/Raynos/body",
"contributors": [
{
"name": "Jake Verbaten"
}
],
"bugs": {
"url": "https://github.com/Raynos/body/issues",
"email": "raynos2@gmail.com"
},
"dependencies": {
"continuable-cache": "^0.3.1",
"error": "^7.0.0",
"raw-body": "~1.1.0",
"safe-json-parse": "~1.0.1"
},
"devDependencies": {
"after": "~0.7.0",
"hammock": "^1.0.0",
"test-server": "~0.1.3",
"send-data": "~1.0.1",
"tape": "~2.3.0",
"process": "~0.5.1"
},
"licenses": [
{
"type": "MIT",
"url": "http://github.com/Raynos/body/raw/master/LICENSE"
}
],
"scripts": {
"test": "node ./test/index.js"
}
}

30
book/node_modules/body/parse-arguments.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
module.exports = parseArguments
function isWritable(stream) {
return typeof stream.write === "function" &&
typeof stream.end === "function"
}
function parseArguments(req, res, opts, callback) {
// (req, cb)
if (typeof res === "function") {
callback = res
opts = {}
res = null
}
// (req, res, cb)
if (typeof opts === "function") {
callback = opts
opts = {}
}
// (req, opts, cb)
if (res && !isWritable(res)) {
opts = res
res = null
}
// default (req, res, opts, cb)
return { req: req, res: res, opts: opts, callback: callback }
}

2
book/node_modules/body/test/index.js generated vendored Normal file
View File

@@ -0,0 +1,2 @@
require('./integration.js');
require('./unit.js');

105
book/node_modules/body/test/integration.js generated vendored Normal file
View File

@@ -0,0 +1,105 @@
var testServer = require("test-server")
var test = require("tape")
var sendJson = require("send-data/json")
var after = require("after")
var body = require("../index")
var jsonBody = require("../json")
var formBody = require("../form")
var anyBody = require("../any")
testServer(handleRequest, runTests)
function handleRequest(req, res) {
function send(err, body) {
if (err) {
return sendJson(req, res, err.message)
}
sendJson(req, res, body)
}
if (req.url === "/body") {
body(req, res, {}, send)
} else if (req.url === "/form") {
formBody(req, res, send)
} else if (req.url === "/json") {
jsonBody(req, {}, send)
} else if (req.url === "/any") {
anyBody(req, send)
}
}
function runTests(request, done) {
test("body works", function (t) {
t.end = after(2, t.end.bind(t))
testBody("/body", request, t)
request({
uri: "/any",
body: "foo"
}, function (err, res, body) {
t.equal(err, null)
t.equal(JSON.parse(body), "Could not parse content type header: ")
t.end()
})
})
test("form works", function (t) {
t.end = after(2, t.end.bind(t))
testFormBody("/form", request, t)
testFormBody("/any", request, t)
})
test("json works", function (t) {
t.end = after(2, t.end.bind(t))
testJsonBody("/json", request, t)
testJsonBody("/any", request, t)
})
.on("end", done)
}
function testBody(uri, request, t) {
request({
uri: uri,
body: "foo"
}, function (err, res, body) {
t.equal(err, null, "error is not null")
console.log("body", body, JSON.parse(body))
t.equal(JSON.parse(body), "foo", "body is incorrect")
t.end()
})
}
function testFormBody(uri, request, t) {
request({
uri: uri,
form: {
foo: "bar"
}
}, function (err, res, body) {
t.equal(err, null, "error is not null")
t.equal(JSON.parse(body).foo, "bar", "body is incorrect")
t.end()
})
}
function testJsonBody(uri, request, t) {
request({
uri: uri,
json: {
foo: "bar"
}
}, function (err, res, body) {
t.equal(err, null, "error is not null")
t.equal(body.foo, "bar", "body is incorrect")
t.end()
})
}

60
book/node_modules/body/test/unit.js generated vendored Normal file
View File

@@ -0,0 +1,60 @@
var after = require('after');
var body = require('../index.js');
var hammock = require('hammock');
var test = require('tape');
test('caching works', function t(assert) {
var request = hammock.Request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: '/somewhere'
});
var response = hammock.Response();
var done = after(2, assert.end.bind(assert));
body(request, response, { cache: true }, function onBody(err, body) {
assert.equal(body, 'thisbody', 'raw body has been set');
assert.pass('body is parsed');
done();
});
request.on('end', function() {
body(request, response, { cache: true }, function onBody(err, body) {
assert.equal(body, 'thisbody', 'cached body is provided');
assert.pass('body is parsed');
done();
});
});
request.end('thisbody');
});
test('parallel caching works', function t(assert) {
var request = hammock.Request({
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
url: '/somewhere'
});
request.end('thisbody');
var response = hammock.Response();
var done = after(5, function() {
process.nextTick(function() {
assert.equal(request.listeners('rawBody').length, 0, 'rawBody listeners cleared');
assert.end();
});
});
for (var i = 0; i < 5; ++i) {
body(request, response, { cache: true }, function onBody(err, body) {
assert.equal(body, 'thisbody', 'raw body has been set');
assert.pass('body is parsed');
done();
});
}
});