Skip to content

Commit 983cd97

Browse files
authored
Merge pull request #1 from jkyberneees/initial-commit
initial commit
2 parents c879772 + 48a24b9 commit 983cd97

File tree

7 files changed

+3260
-0
lines changed

7 files changed

+3260
-0
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
language: node_js
3+
node_js:
4+
- "10"
5+
6+
install: npm install

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
11
# on-http-end
22
Allows to capture HTTP response content and headers on request end.
3+
> Inspired by: https://github.com/kwhitley/apicache/blob/master/src/apicache.js
4+
5+
## Install
6+
```bash
7+
npm i on-http-end
8+
```
9+
10+
## Usage
11+
```js
12+
const onEnd = require('on-http-end')
13+
const http = require('http')
14+
15+
const server = http.createServer((req, res) => {
16+
onEnd(res, (payload) => {
17+
console.log(payload)
18+
})
19+
20+
res.setHeader('my-header', 'value')
21+
res.end('Hello Word!', 'utf-8')
22+
})
23+
24+
server.listen(3000)
25+
```
26+
27+
Output:
28+
```bash
29+
{
30+
status: 200,
31+
headers: [Object: null prototype] { 'my-header': 'value' },
32+
data: 'Hello Word!',
33+
encoding: 'utf-8'
34+
}
35+
```
36+
37+
## Want to contribute?
38+
This is your repo ;)
39+
40+
> Note: We aim to be 100% code coverage, please consider it on your pull requests.

demos/basic.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const onEnd = require('./../index')
2+
const http = require('http')
3+
4+
const server = http.createServer((req, res) => {
5+
onEnd(res, (payload) => {
6+
console.log(payload)
7+
})
8+
9+
res.setHeader('my-header', 'value')
10+
res.end('Hello Word!', 'utf-8')
11+
})
12+
13+
server.listen(3000)

index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module.exports = (res, cb) => {
2+
res._parent = {
3+
write: res.write,
4+
end: res.end,
5+
content: undefined,
6+
ended: false
7+
}
8+
9+
res.write = function (content) {
10+
accumulate(res, content)
11+
return res._parent.write.apply(res, arguments)
12+
}
13+
14+
res.end = function (content, encoding) {
15+
if (!res._parent.ended) {
16+
res._parent.ended = true
17+
18+
accumulate(res, content)
19+
const headers = res.getHeaders()
20+
const payload = map(res.statusCode, headers, res._parent.content, encoding)
21+
22+
setImmediate(() => {
23+
cb(payload)
24+
})
25+
}
26+
27+
return res._parent.end.apply(res, arguments)
28+
}
29+
}
30+
31+
function map (status, headers, data, encoding) {
32+
return {
33+
status: status,
34+
headers: headers,
35+
data: data,
36+
encoding: encoding
37+
}
38+
}
39+
40+
function accumulate (res, content) {
41+
if (content) {
42+
if (typeof content === 'string') {
43+
res._parent.content = (res._parent.content || '') + content
44+
} else if (Buffer.isBuffer(content)) {
45+
let oldContent = res._parent.content
46+
47+
if (typeof oldContent === 'string') {
48+
oldContent = Buffer.from(oldContent)
49+
} else if (!oldContent) {
50+
oldContent = Buffer.alloc(0)
51+
}
52+
53+
res._parent.content = Buffer.concat([oldContent, content], oldContent.length + content.length)
54+
} else {
55+
res._parent.content = content
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)