nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
151 lines (133 loc) • 3.99 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/blocklist.js
import { BlockList as BlockListHandle } from "nstdlib/stub/binding/block_list";
import { customInspectSymbol as kInspect } from "nstdlib/lib/internal/util";
import {
SocketAddress,
kHandle as kSocketAddressHandle,
} from "nstdlib/lib/internal/socketaddress";
import {
markTransferMode,
kClone,
kDeserialize,
} from "nstdlib/lib/internal/worker/js_transferable";
import { inspect } from "nstdlib/lib/internal/util/inspect";
import { owner_symbol } from "nstdlib/stub/binding/symbols";
import { codes as __codes__ } from "nstdlib/lib/internal/errors";
import { validateInt32, validateString } from "nstdlib/lib/internal/validators";
const kHandle = Symbol("kHandle");
const { ERR_INVALID_ARG_VALUE } = __codes__;
class BlockList {
constructor() {
markTransferMode(this, true, false);
this[kHandle] = new BlockListHandle();
this[kHandle][owner_symbol] = this;
}
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1,
};
return `BlockList ${inspect(
{
rules: this.rules,
},
opts,
)}`;
}
addAddress(address, family = "ipv4") {
if (!SocketAddress.isSocketAddress(address)) {
validateString(address, "address");
validateString(family, "family");
address = new SocketAddress({
address,
family,
});
}
this[kHandle].addAddress(address[kSocketAddressHandle]);
}
addRange(start, end, family = "ipv4") {
if (!SocketAddress.isSocketAddress(start)) {
validateString(start, "start");
validateString(family, "family");
start = new SocketAddress({
address: start,
family,
});
}
if (!SocketAddress.isSocketAddress(end)) {
validateString(end, "end");
validateString(family, "family");
end = new SocketAddress({
address: end,
family,
});
}
const ret = this[kHandle].addRange(
start[kSocketAddressHandle],
end[kSocketAddressHandle],
);
if (ret === false)
throw new ERR_INVALID_ARG_VALUE("start", start, "must come before end");
}
addSubnet(network, prefix, family = "ipv4") {
if (!SocketAddress.isSocketAddress(network)) {
validateString(network, "network");
validateString(family, "family");
network = new SocketAddress({
address: network,
family,
});
}
switch (network.family) {
case "ipv4":
validateInt32(prefix, "prefix", 0, 32);
break;
case "ipv6":
validateInt32(prefix, "prefix", 0, 128);
break;
}
this[kHandle].addSubnet(network[kSocketAddressHandle], prefix);
}
check(address, family = "ipv4") {
if (!SocketAddress.isSocketAddress(address)) {
validateString(address, "address");
validateString(family, "family");
try {
address = new SocketAddress({
address,
family,
});
} catch {
// Ignore the error. If it's not a valid address, return false.
return false;
}
}
return Boolean(this[kHandle].check(address[kSocketAddressHandle]));
}
get rules() {
return this[kHandle].getRules();
}
[kClone]() {
const handle = this[kHandle];
return {
data: { handle },
deserializeInfo: "internal/blocklist:InternalBlockList",
};
}
[kDeserialize]({ handle }) {
this[kHandle] = handle;
this[kHandle][owner_symbol] = this;
}
}
class InternalBlockList {
constructor(handle) {
markTransferMode(this, true, false);
this[kHandle] = handle;
if (handle !== undefined) handle[owner_symbol] = this;
}
}
InternalBlockList.prototype.constructor = BlockList.prototype.constructor;
Object.setPrototypeOf(InternalBlockList.prototype, BlockList.prototype);
export { BlockList };
export { InternalBlockList };