Skip to content

Commit 6599bc3

Browse files
authored
Fixing scope (#7)
* fixing scope to support multiple registrations * adding github actions
1 parent bfbe934 commit 6599bc3

File tree

8 files changed

+136
-18
lines changed

8 files changed

+136
-18
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
custom: https://www.paypal.me/kyberneees
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/custom.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Have you contributed with at least a little donation to support the development of this project?**
11+
- Paypal: https://www.paypal.me/kyberneees
12+
- [TRON](https://www.binance.com/en/buy-TRON) Wallet: `TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus`
13+
14+
**Is your feature request related to a problem? Please describe.**
15+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
16+
17+
**Describe the solution you'd like**
18+
A clear and concise description of what you want to happen.
19+
20+
**Describe alternatives you've considered**
21+
A clear and concise description of any alternative solutions or features you've considered.
22+
23+
**Additional context**
24+
Add any other context or screenshots about the feature request here.

.github/workflows/tests.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
testing:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
- name: Setup Environment (Using NodeJS 16.x)
10+
uses: actions/setup-node@v1
11+
with:
12+
node-version: 16.x
13+
14+
- name: Install dependencies
15+
run: npm install
16+
17+
- name: Linting
18+
run: npx standard
19+
20+
- name: Run tests
21+
run: npm run test

index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
module.exports = (res, cb) => {
2-
res._parent = {
2+
const parent = {
33
write: res.write,
44
end: res.end,
55
content: undefined,
66
ended: false
77
}
88

99
res.write = function (content) {
10-
accumulate(res, content)
11-
return res._parent.write.apply(res, arguments)
10+
accumulate(parent, content)
11+
return parent.write.apply(res, arguments)
1212
}
1313

1414
res.end = function (content, encoding) {
15-
if (!res._parent.ended) {
16-
res._parent.ended = true
15+
if (!parent.ended) {
16+
parent.ended = true
1717

18-
accumulate(res, content)
18+
accumulate(parent, content)
1919
const headers = res.getHeaders()
20-
const payload = map(res.statusCode, headers, res._parent.content, encoding)
20+
const payload = map(res.statusCode, headers, parent.content, encoding)
2121

2222
setImmediate(() => {
2323
cb(payload)
2424
})
2525
}
2626

27-
return res._parent.end.apply(res, arguments)
27+
return parent.end.apply(res, arguments)
2828
}
2929
}
3030

3131
function map (status, headers, data, encoding) {
3232
return {
33-
status: status,
34-
headers: headers,
35-
data: data,
33+
status,
34+
headers,
35+
data,
3636
encoding: typeof encoding === 'string' ? encoding : null
3737
}
3838
}
3939

40-
function accumulate (res, content) {
40+
function accumulate (parent, content) {
4141
if (content) {
4242
if (typeof content === 'string') {
43-
res._parent.content = (res._parent.content || '') + content
43+
parent.content = (parent.content || '') + content
4444
} else if (Buffer.isBuffer(content)) {
45-
let oldContent = res._parent.content
45+
let oldContent = parent.content
4646

4747
if (typeof oldContent === 'string') {
4848
oldContent = Buffer.from(oldContent)
4949
} else if (!oldContent) {
5050
oldContent = Buffer.alloc(0)
5151
}
5252

53-
res._parent.content = Buffer.concat([oldContent, content], oldContent.length + content.length)
53+
parent.content = Buffer.concat([oldContent, content], oldContent.length + content.length)
5454
} else {
55-
res._parent.content = content
55+
parent.content = content
5656
}
5757
}
5858
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Allows to capture HTTP response content and headers on request end.",
55
"main": "index.js",
66
"scripts": {
7+
"format": "standard --fix",
78
"test": "nyc mocha test/*.test.js --exit"
89
},
910
"repository": {
@@ -25,7 +26,6 @@
2526
"devDependencies": {
2627
"chai": "^4.3.7",
2728
"mocha": "^10.2.0",
28-
"nyc": "^15.1.0",
29-
"standard": "^17.0.0"
29+
"nyc": "^15.1.0"
3030
}
3131
}

test/smoke.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ describe('on-http-end', () => {
3939
res.end('world')
4040
})
4141

42+
it('should support multiple callback registrations', function (done) {
43+
onFinished(res, (payload) => {
44+
expect(payload.data).to.equal('hello world')
45+
expect(data).to.equal('hello world')
46+
})
47+
onFinished(res, (payload) => {
48+
expect(payload.data).to.equal('hello world')
49+
expect(data).to.equal('hello world')
50+
})
51+
onFinished(res, (payload) => {
52+
expect(payload.data).to.equal('hello world')
53+
expect(data).to.equal('hello world')
54+
done()
55+
})
56+
57+
res.write(undefined)
58+
res.write('h')
59+
res.write(Buffer.from('ello'))
60+
res.write(' ')
61+
res.end('world')
62+
})
63+
4264
it('should accumulate content and encoding', function (done) {
4365
onFinished(res, (payload) => {
4466
expect(payload.encoding).to.equal('utf-8')

0 commit comments

Comments
 (0)