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

Commit

Permalink
feat(examples): add dynamic poison registering example
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Oct 3, 2017
1 parent eac790e commit 83ed74d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,12 @@ Disable a poison by name identifier
#### toxy#remove(poison)
Return: `boolean`

Remove poison by name identifier.
Remove an incoming traffic poison by name identifier or object reference.

#### toxy#removeOutgoing(poison)
Return: `boolean`

Remove an outgoing traffic poison by name identifier or object reference.

#### toxy#isEnabled(poison)
Return: `boolean`
Expand Down Expand Up @@ -1115,7 +1120,7 @@ Return an array of registered `outgoing` poisons.
#### toxy#flush()
Alias: `flushPoisons`

Remove all the registered poisons.
Remove all the registered poisons for both incoming and outgoing traffic flows.

#### toxy#enableRule(rule)

Expand Down
32 changes: 32 additions & 0 deletions examples/dynamic-poisons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const toxy = require('..')

const proxy = toxy()
const rules = proxy.rules
const poisons = proxy.poisons

const createPoison = () => poisons.throttle({ chunk: 1024, threshold: 10 })

// Configure proxy
proxy
.forward('http://httpbin.org')
.poison(createPoison())
.withRule(rules.method('GET'))
.withRule(rules.probability(90))

// Redefine poison every
setInterval(() => {
console.log('Replacing poison...')
// Remove by name
proxy.remove('throttle')
// Or alternatively remove all poisons
proxy.flushPoisons()
// Re-register poison with custom rules
toxy.poison(createPoison())
.withRule(rules.method('GET'))
.withRule(rules.probability(90))
}, 1000)

proxy.all('/*')

proxy.listen(3000)
console.log('Server listening on port:', 3000)
6 changes: 4 additions & 2 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ RockyBase.prototype.disableOutgoing = function (poison) {
return this.disable(poison, 'outgoing')
}

RockyBase.prototype.remove = function (poison, phase) {
RockyBase.prototype.remove =
RockyBase.prototype.removePoison = function (poison, phase) {
const stack = getPoisonsStack(this, phase || 'incoming')
return this._remove(stack, poison)
}

RockyBase.prototype.removeOutgoing = function (poison) {
RockyBase.prototype.removeOutgoing =
RockyBase.prototype.removeOutgoingPoison = function (poison) {
return this.remove(poison, 'outgoing')
}

Expand Down

0 comments on commit 83ed74d

Please sign in to comment.