Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 11ebf86

Browse files
committed
feat(#29): add CORS support
1 parent f0c69b0 commit 11ebf86

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ toxy.poison(toxy.poisons.abort())
499499
// Abort after a delay
500500
toxy.poison(toxy.poisons.abort(1000))
501501
// In this case, the socket will be closed if
502-
// the target server doesn't replies with
503-
// a response after 2 seconds
502+
// the target server takes more than
503+
// 2 seconds to respond
504504
toxy.poison(toxy.poisons.abort({ delay: 2000, next: true }))
505505
```
506506

@@ -1183,7 +1183,7 @@ For a featured use case, see the [admin server](examples/admin.js) example.
11831183
const toxy = require('toxy')
11841184

11851185
// Create the toxy admin server
1186-
var admin = toxy.admin()
1186+
var admin = toxy.admin({ cors: true })
11871187
admin.listen(9000)
11881188

11891189
// Create the toxy proxy
@@ -1408,8 +1408,10 @@ Returns: `Admin`
14081408

14091409
**Supported options**:
14101410

1411-
- **apiKey** - Optional API key to protect the server
1412-
- **port** - Optional. TCP port to listen
1411+
- **apiKey** `string` - Optional API key to protect the server
1412+
- **port** `number` - Optional. TCP port to listen
1413+
- **cors** `boolean` - Enable CORS for web browser access
1414+
- **middleware** `array<function>` - Plug in additional middleware
14131415

14141416
##### Admin#listen([ port, host ])
14151417

examples/admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ proxy.listen(3000)
3434
console.log('Server listening on port:', 3000)
3535

3636
// Enable the admin HTTP server
37-
var admin = toxy.admin(/* { apiKey: 's3cr3t' } */)
37+
var admin = toxy.admin({ cors: true })
3838

3939
// Add the toxy proxy instance
4040
admin.manage(proxy)

lib/admin/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ module.exports = function (opts) {
1919
admin.use(middleware.reply)
2020
admin.use(middleware.authorization)
2121

22+
if (opts.cors) {
23+
admin.use(middleware.cors)
24+
}
25+
26+
if (opts.middleware) {
27+
opts.middleware.forEach(function (middleware) {
28+
admin.use(middleware)
29+
})
30+
}
31+
2232
/**
2333
* Configure param-based middleware
2434
*/

lib/admin/middleware/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
body: require('./body'),
3+
cors: require('./cors'),
34
admin: require('./admin'),
45
reply: require('./reply'),
56
authorization: require('./authorization')

lib/proxy.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ function getPoisonsStack(proxy, phase) {
162162
}
163163

164164
function createPoison(poison) {
165-
return (poison instanceof Poison)
166-
? poison
167-
: new Poison(poison)
165+
if (poison instanceof Poison) {
166+
return poison
167+
}
168+
return new Poison(poison)
168169
}

0 commit comments

Comments
 (0)