-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
16 changed files
with
212 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/plugins/data/common/search/aggs/utils/ip_address.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { IpAddress } from './ip_address'; | ||
|
||
describe('IpAddress', () => { | ||
describe('constructor', () => { | ||
it.each` | ||
address | ||
${''} | ||
${'hello, world'} | ||
${'256.0.0.0'} | ||
${'-1.0.0.0'} | ||
${Number.MAX_SAFE_INTEGER} | ||
${'fffff::'} | ||
${'ffff:0:0:0:0:0:0:0:0'} | ||
`('should throw an error on $address', ({ address }) => { | ||
expect(() => new IpAddress(address)).toThrowError(); | ||
}); | ||
|
||
it.each` | ||
address | expected | ||
${'192.168.257'} | ${'192.168.1.1'} | ||
${2116932386} | ${'126.45.211.34'} | ||
${'126.45.211.34'} | ${'126.45.211.34'} | ||
${[126, 45, 211, 34]} | ${'126.45.211.34'} | ||
${'ffff:0:0:0:0:0:0:0'} | ${'ffff::'} | ||
${'ffff::'} | ${'ffff::'} | ||
${[0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]} | ${'ffff::'} | ||
`('should parse $address', ({ address, expected }) => { | ||
expect(new IpAddress(address).toString()).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('valueOf', () => { | ||
it.each` | ||
address | expected | ||
${'0.0.0.0'} | ${'0'} | ||
${'0.0.0.1'} | ${'1'} | ||
${'126.45.211.34'} | ${'2116932386'} | ||
${'ffff::'} | ${'340277174624079928635746076935438991360'} | ||
`( | ||
'should return $expected as a decimal representation of $address', | ||
({ address, expected }) => { | ||
expect(new IpAddress(address).valueOf().toString()).toBe(expected); | ||
} | ||
); | ||
}); | ||
|
||
describe('toString()', () => { | ||
it.each` | ||
address | expected | ||
${'0.000.00000.1'} | ${'0.0.0.1'} | ||
${'192.168.257'} | ${'192.168.1.1'} | ||
${'ffff:0:0:0:0:0:0:0'} | ${'ffff::'} | ||
${'0:0:0:0:0:0:0:ffff'} | ${'::ffff'} | ||
${'f:0:0:0:0:0:0:f'} | ${'f::f'} | ||
`('should serialize $address as $expected', ({ address, expected }) => { | ||
expect(new IpAddress(address).toString()).toBe(expected); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import ipaddr, { IPv4, IPv6 } from 'ipaddr.js'; | ||
|
||
function isIPv6(value: IPv4 | IPv6): value is IPv6 { | ||
return value.kind() === 'ipv6'; | ||
} | ||
|
||
export class IpAddress { | ||
private value: IPv4 | IPv6; | ||
|
||
constructor(ipAddress: string | number | number[]) { | ||
try { | ||
this.value = Array.isArray(ipAddress) | ||
? ipaddr.fromByteArray(ipAddress) | ||
: ipaddr.parse(`${ipAddress}`); | ||
} catch { | ||
throw Error('Invalid IP address: ' + ipAddress); | ||
} | ||
} | ||
|
||
toString() { | ||
if (isIPv6(this.value)) { | ||
return this.value.toRFC5952String(); | ||
} | ||
|
||
return this.value.toString(); | ||
} | ||
|
||
valueOf(): number | bigint { | ||
const value = this.value | ||
.toByteArray() | ||
.reduce((result, octet) => result * 256n + BigInt(octet), 0n); | ||
|
||
if (value > Number.MAX_SAFE_INTEGER) { | ||
return value; | ||
} | ||
|
||
return Number(value); | ||
} | ||
} |
54 changes: 0 additions & 54 deletions
54
src/plugins/data/common/search/aggs/utils/ipv4_address.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.