nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
72 lines (61 loc) • 2.48 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/net.js
import { Buffer as Buffer } from "nstdlib/lib/buffer";
import { writeBuffer } from "nstdlib/stub/binding/fs";
import { UVException } from "nstdlib/lib/internal/errors";
// IPv4 Segment
const v4Seg = "(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])";
const v4Str = `(?:${v4Seg}\\.){3}${v4Seg}`;
const IPv4Reg = new RegExp(`^${v4Str}$`);
// IPv6 Segment
const v6Seg = "(?:[0-9a-fA-F]{1,4})";
const IPv6Reg = new RegExp(
"^(?:" +
`(?:${v6Seg}:){7}(?:${v6Seg}|:)|` +
`(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|` +
`(?:${v6Seg}:){5}(?::${v4Str}|(?::${v6Seg}){1,2}|:)|` +
`(?:${v6Seg}:){4}(?:(?::${v6Seg}){0,1}:${v4Str}|(?::${v6Seg}){1,3}|:)|` +
`(?:${v6Seg}:){3}(?:(?::${v6Seg}){0,2}:${v4Str}|(?::${v6Seg}){1,4}|:)|` +
`(?:${v6Seg}:){2}(?:(?::${v6Seg}){0,3}:${v4Str}|(?::${v6Seg}){1,5}|:)|` +
`(?:${v6Seg}:){1}(?:(?::${v6Seg}){0,4}:${v4Str}|(?::${v6Seg}){1,6}|:)|` +
`(?::(?:(?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:))` +
")(?:%[0-9a-zA-Z-.:]{1,})?$",
);
function isIPv4(s) {
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
// no longer creates a perf regression in the dns benchmark.
// eslint-disable-next-line node-core/avoid-prototype-pollution
return RegExp.prototype.test.call(IPv4Reg, s);
}
function isIPv6(s) {
// TODO(aduh95): Replace RegExpPrototypeTest with RegExpPrototypeExec when it
// no longer creates a perf regression in the dns benchmark.
// eslint-disable-next-line node-core/avoid-prototype-pollution
return RegExp.prototype.test.call(IPv6Reg, s);
}
function isIP(s) {
if (isIPv4(s)) return 4;
if (isIPv6(s)) return 6;
return 0;
}
function makeSyncWrite(fd) {
return function (chunk, enc, cb) {
if (enc !== "buffer") chunk = Buffer.from(chunk, enc);
this._handle.bytesWritten += chunk.length;
const ctx = {};
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
if (ctx.errno !== undefined) {
const ex = new UVException(ctx);
ex.errno = ctx.errno;
return cb(ex);
}
cb();
};
}
const _export_kReinitializeHandle_ = Symbol("kReinitializeHandle");
export { _export_kReinitializeHandle_ as kReinitializeHandle };
export { isIP };
export { isIPv4 };
export { isIPv6 };
export { makeSyncWrite };
const _export_normalizedArgsSymbol_ = Symbol("normalizedArgs");
export { _export_normalizedArgsSymbol_ as normalizedArgsSymbol };