init project
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.isInSubnet = isInSubnet;
|
||||
exports.isHostInSubnet = isHostInSubnet;
|
||||
exports.isCorrect = isCorrect;
|
||||
exports.prefixLengthFromMask = prefixLengthFromMask;
|
||||
exports.assertByteArray = assertByteArray;
|
||||
exports.numberToPaddedHex = numberToPaddedHex;
|
||||
exports.stringToPaddedHex = stringToPaddedHex;
|
||||
exports.testBit = testBit;
|
||||
const address_error_1 = require("./address-error");
|
||||
/**
|
||||
* Returns whether this address's *network* is contained within `address`,
|
||||
* i.e. whether every address this one can represent also falls inside
|
||||
* `address`. A network wider than `address` is not contained in it, so
|
||||
* `10.0.0.0/8` is not in `10.0.0.0/16`.
|
||||
*
|
||||
* To ask whether the address itself falls inside a range, ignoring any CIDR
|
||||
* suffix it was written with, use {@link isHostInSubnet} instead. That is the
|
||||
* question the special-use classifiers ask.
|
||||
*/
|
||||
function isInSubnet(address) {
|
||||
if (this.subnetMask < address.subnetMask) {
|
||||
return false;
|
||||
}
|
||||
return isHostInSubnet.call(this, address);
|
||||
}
|
||||
/**
|
||||
* Returns whether this address's host bits fall inside `address`, ignoring
|
||||
* this address's own subnet mask.
|
||||
*
|
||||
* This is the primitive the special-use classifiers (`isLoopback`,
|
||||
* `isPrivate`, `isLinkLocal`, `getType`, …) are built on: they answer a
|
||||
* question about the address, so the answer must not change with the CIDR
|
||||
* suffix the caller happened to write. Use this rather than
|
||||
* {@link isInSubnet} when classifying a single address — notably when the
|
||||
* address came from untrusted input and the result backs a trust-boundary
|
||||
* decision such as an SSRF allow/deny filter.
|
||||
*/
|
||||
function isHostInSubnet(address) {
|
||||
return this.mask(address.subnetMask) === address.mask();
|
||||
}
|
||||
function isCorrect(defaultBits) {
|
||||
return function isCorrectForm() {
|
||||
if (this.addressMinusSuffix !== this.correctForm()) {
|
||||
return false;
|
||||
}
|
||||
if (this.subnetMask === defaultBits && !this.parsedSubnet) {
|
||||
return true;
|
||||
}
|
||||
return this.parsedSubnet === String(this.subnetMask);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Returns the prefix length (number of leading 1 bits) of a contiguous
|
||||
* subnet mask. Throws `AddressError` if the mask is non-contiguous (e.g.
|
||||
* `255.0.255.0`).
|
||||
*/
|
||||
function prefixLengthFromMask(value, totalBits) {
|
||||
const binary = value.toString(2).padStart(totalBits, '0');
|
||||
if (binary.length > totalBits) {
|
||||
throw new address_error_1.AddressError('Invalid subnet mask.');
|
||||
}
|
||||
const firstZero = binary.indexOf('0');
|
||||
if (firstZero === -1) {
|
||||
return totalBits;
|
||||
}
|
||||
if (binary.slice(firstZero).includes('1')) {
|
||||
throw new address_error_1.AddressError('Invalid subnet mask.');
|
||||
}
|
||||
return firstZero;
|
||||
}
|
||||
/**
|
||||
* Throws `AddressError` unless `bytes` holds exactly `byteCount` integers,
|
||||
* each from `minimum` to 255. Pass a `minimum` of `-128` where signed bytes
|
||||
* are accepted and folded to unsigned, and `0` where they are not.
|
||||
*/
|
||||
function assertByteArray(bytes, byteCount, family, minimum) {
|
||||
if (bytes.length !== byteCount) {
|
||||
throw new address_error_1.AddressError(`${family} addresses require exactly ${byteCount} bytes`);
|
||||
}
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
if (!Number.isInteger(bytes[i]) || bytes[i] < minimum || bytes[i] > 255) {
|
||||
throw new address_error_1.AddressError(`All bytes must be integers between ${minimum} and 255`);
|
||||
}
|
||||
}
|
||||
}
|
||||
function numberToPaddedHex(number) {
|
||||
return number.toString(16).padStart(2, '0');
|
||||
}
|
||||
function stringToPaddedHex(numberString) {
|
||||
return numberToPaddedHex(parseInt(numberString, 10));
|
||||
}
|
||||
/**
|
||||
* @param binaryValue Binary representation of a value (e.g. `10`)
|
||||
* @param position Byte position, where 0 is the least significant bit
|
||||
*/
|
||||
function testBit(binaryValue, position) {
|
||||
const { length } = binaryValue;
|
||||
if (position > length) {
|
||||
return false;
|
||||
}
|
||||
const positionInString = length - position;
|
||||
return binaryValue.substring(positionInString, positionInString + 1) === '1';
|
||||
}
|
||||
//# sourceMappingURL=common.js.map
|
||||
Reference in New Issue
Block a user