Skip to content

Commit

Permalink
refactor(index): use \d over number range character class in regex (#…
Browse files Browse the repository at this point in the history
…56)

* refactor(index): use `\d` over number range in regex

* test: add unicode `u` regex flag
  • Loading branch information
Fdawgs committed Oct 21, 2023
1 parent 4a3a489 commit 2c1a45b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ipaddr = require('ipaddr.js')
* @private
*/

const DIGIT_REGEXP = /^[0-9]+$/
const DIGIT_REGEXP = /^\d+$/u
const isip = ipaddr.isValid
const parseip = ipaddr.parse

Expand Down
2 changes: 1 addition & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require('tap')
const proxyaddr = require('..')

test('argument req should be required', function (t) {
t.throws(proxyaddr.all, /req.*required/)
t.throws(proxyaddr.all, /req.*required/u)
t.end()
})

Expand Down
36 changes: 18 additions & 18 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const { test } = require('tap')
const proxyaddr = require('..')

test('req should be required', function (t) {
t.throws(proxyaddr, /req.*required/)
t.throws(proxyaddr, /req.*required/u)
t.end()
})

test('trust should be required', function (t) {
const req = createReq('127.0.0.1')
t.throws(proxyaddr.bind(null, req), /trust.*required/)
t.throws(proxyaddr.bind(null, req), /trust.*required/u)
t.end()
})

Expand All @@ -34,7 +34,7 @@ test('trust should accept a string', function (t) {

test('trust should reject a number', function (t) {
const req = createReq('127.0.0.1')
t.throws(proxyaddr.bind(null, req, 42), /unsupported trust argument/)
t.throws(proxyaddr.bind(null, req, 42), /unsupported trust argument/u)
t.end()
})

Expand Down Expand Up @@ -78,30 +78,30 @@ test('trust should not alter input array', function (t) {

test('trust should reject non-IP', function (t) {
const req = createReq('127.0.0.1')
t.throws(proxyaddr.bind(null, req, 'blargh'), /invalid IP address/)
t.throws(proxyaddr.bind(null, req, '10.0.300.1'), /invalid IP address/)
t.throws(proxyaddr.bind(null, req, '::ffff:30.168.1.9000'), /invalid IP address/)
t.throws(proxyaddr.bind(null, req, '-1'), /invalid IP address/)
t.throws(proxyaddr.bind(null, req, 'blargh'), /invalid IP address/u)
t.throws(proxyaddr.bind(null, req, '10.0.300.1'), /invalid IP address/u)
t.throws(proxyaddr.bind(null, req, '::ffff:30.168.1.9000'), /invalid IP address/u)
t.throws(proxyaddr.bind(null, req, '-1'), /invalid IP address/u)
t.end()
})

test('trust should reject bad CIDR', function (t) {
const req = createReq('127.0.0.1')
t.throws(proxyaddr.bind(null, req, '10.0.0.1/internet'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/6000'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '::1/6000'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/136'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/-1'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/internet'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/6000'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '::1/6000'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/136'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/-1'), /invalid range on address/u)
t.end()
})

test('trust should reject bad netmask', function (t) {
const req = createReq('127.0.0.1')
t.throws(proxyaddr.bind(null, req, '10.0.0.1/255.0.255.0'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/ffc0::'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, 'fe80::/ffc0::'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, 'fe80::/255.255.255.0'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/255.255.255.0'), /invalid range on address/)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/255.0.255.0'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '10.0.0.1/ffc0::'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, 'fe80::/ffc0::'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, 'fe80::/255.255.255.0'), /invalid range on address/u)
t.throws(proxyaddr.bind(null, req, '::ffff:a00:2/255.255.255.0'), /invalid range on address/u)
t.end()
})

Expand Down Expand Up @@ -414,4 +414,4 @@ function createReq (socketAddr, headers) {

function all () { return true }
function none () { return false }
function trust10x (addr) { return /^10\./.test(addr) }
function trust10x (addr) { return /^10\./u.test(addr) }
16 changes: 8 additions & 8 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { test } = require('tap')
const proxyaddr = require('..')

test('trust arg should be required', function (t) {
t.throws(proxyaddr.compile, /argument.*required/)
t.throws(proxyaddr.compile, /argument.*required/u)
t.end()
})

Expand All @@ -19,7 +19,7 @@ test('trust arg should accept a string', function (t) {
})

test('trust arg should reject a number', function (t) {
t.throws(proxyaddr.compile.bind(null, 42), /unsupported trust argument/)
t.throws(proxyaddr.compile.bind(null, 42), /unsupported trust argument/u)
t.end()
})

Expand Down Expand Up @@ -49,16 +49,16 @@ test('trust arg should accept pre-defined names in array', function (t) {
})

test('trust arg should reject non-IP', function (t) {
t.throws(proxyaddr.compile.bind(null, 'blargh'), /invalid IP address/)
t.throws(proxyaddr.compile.bind(null, '-1'), /invalid IP address/)
t.throws(proxyaddr.compile.bind(null, 'blargh'), /invalid IP address/u)
t.throws(proxyaddr.compile.bind(null, '-1'), /invalid IP address/u)
t.end()
})

test('trust arg should reject bad CIDR', function (t) {
t.throws(proxyaddr.compile.bind(null, '10.0.0.1/6000'), /invalid range on address/)
t.throws(proxyaddr.compile.bind(null, '::1/6000'), /invalid range on address/)
t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/136'), /invalid range on address/)
t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/-46'), /invalid range on address/)
t.throws(proxyaddr.compile.bind(null, '10.0.0.1/6000'), /invalid range on address/u)
t.throws(proxyaddr.compile.bind(null, '::1/6000'), /invalid range on address/u)
t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/136'), /invalid range on address/u)
t.throws(proxyaddr.compile.bind(null, '::ffff:a00:2/-46'), /invalid range on address/u)
t.end()
})

Expand Down

0 comments on commit 2c1a45b

Please sign in to comment.