@ttmg/cli 0.3.2-beta.8 → 0.3.2-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6,13 +6,18 @@ var inquirer = require('inquirer');
6
6
  var path = require('path');
7
7
  var os = require('os');
8
8
  var axios = require('axios');
9
- var require$$0$1 = require('net');
9
+ var net = require('net');
10
10
  var require$$1$2 = require('tls');
11
11
  var require$$2 = require('url');
12
12
  var require$$3 = require('assert');
13
13
  var require$$1 = require('tty');
14
14
  var require$$1$1 = require('util');
15
15
  var require$$0 = require('events');
16
+ var require$$0$1 = require('buffer');
17
+ var require$$2$1 = require('stream');
18
+ var http = require('http');
19
+ var https = require('https');
20
+ var dns = require('dns');
16
21
  var fs = require('fs');
17
22
  var handlebars = require('handlebars');
18
23
  var esbuild = require('esbuild');
@@ -30,9 +35,7 @@ var worker_threads = require('worker_threads');
30
35
  var glob = require('glob');
31
36
  var got = require('got');
32
37
  var FormData$1 = require('form-data');
33
- var stream = require('stream');
34
38
  var ttmgPack = require('ttmg-pack');
35
- var http = require('http');
36
39
  var expressStaticGzip = require('express-static-gzip');
37
40
  var fileUpload = require('express-fileupload');
38
41
  var fs$1 = require('node:fs');
@@ -60,6 +63,10 @@ function _interopNamespaceDefault(e) {
60
63
 
61
64
  var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
62
65
  var os__namespace = /*#__PURE__*/_interopNamespaceDefault(os);
66
+ var net__namespace = /*#__PURE__*/_interopNamespaceDefault(net);
67
+ var require$$1__namespace = /*#__PURE__*/_interopNamespaceDefault(require$$1$2);
68
+ var http__namespace = /*#__PURE__*/_interopNamespaceDefault(http);
69
+ var dns__namespace = /*#__PURE__*/_interopNamespaceDefault(dns);
63
70
  var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
64
71
  var glob__namespace = /*#__PURE__*/_interopNamespaceDefault(glob);
65
72
 
@@ -89,6 +96,10 @@ async function openUrl(url) {
89
96
  }
90
97
  }
91
98
 
99
+ function getDefaultExportFromCjs (x) {
100
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
101
+ }
102
+
92
103
  var agent = {};
93
104
 
94
105
  var src$1 = {exports: {}};
@@ -266,12 +277,12 @@ function requireMs () {
266
277
  return ms;
267
278
  }
268
279
 
269
- var common;
270
- var hasRequiredCommon;
280
+ var common$1;
281
+ var hasRequiredCommon$1;
271
282
 
272
- function requireCommon () {
273
- if (hasRequiredCommon) return common;
274
- hasRequiredCommon = 1;
283
+ function requireCommon$1 () {
284
+ if (hasRequiredCommon$1) return common$1;
285
+ hasRequiredCommon$1 = 1;
275
286
  /**
276
287
  * This is the common logic for both the Node.js and web browser
277
288
  * implementations of `debug()`.
@@ -562,8 +573,8 @@ function requireCommon () {
562
573
  return createDebug;
563
574
  }
564
575
 
565
- common = setup;
566
- return common;
576
+ common$1 = setup;
577
+ return common$1;
567
578
  }
568
579
 
569
580
  /* eslint-env browser */
@@ -829,7 +840,7 @@ function requireBrowser () {
829
840
  }
830
841
  }
831
842
 
832
- module.exports = requireCommon()(exports$1);
843
+ module.exports = requireCommon$1()(exports$1);
833
844
 
834
845
  const {formatters} = module.exports;
835
846
 
@@ -1254,7 +1265,7 @@ function requireNode () {
1254
1265
  }
1255
1266
  }
1256
1267
 
1257
- module.exports = requireCommon()(exports$1);
1268
+ module.exports = requireCommon$1()(exports$1);
1258
1269
 
1259
1270
  const {formatters} = module.exports;
1260
1271
 
@@ -1631,7 +1642,7 @@ function requireAgent () {
1631
1642
  return (mod && mod.__esModule) ? mod : { "default": mod };
1632
1643
  };
1633
1644
  Object.defineProperty(agent, "__esModule", { value: true });
1634
- const net_1 = __importDefault(require$$0$1);
1645
+ const net_1 = __importDefault(net);
1635
1646
  const tls_1 = __importDefault(require$$1$2);
1636
1647
  const url_1 = __importDefault(require$$2);
1637
1648
  const assert_1 = __importDefault(require$$3);
@@ -1821,52 +1832,4694 @@ function requireDist () {
1821
1832
 
1822
1833
  var distExports = requireDist();
1823
1834
 
1824
- const CONFIG_PATH$1 = process.env.TTMGRC_PATH || path.join(os.homedir(), '.ttmgrc');
1825
- const getTTMGRC = () => {
1826
- // only check one time
1827
- if (!fs.existsSync(CONFIG_PATH$1)) {
1828
- return null;
1829
- }
1830
- else {
1831
- // safe parse
1832
- let res = JSON.parse(fs.readFileSync(CONFIG_PATH$1, 'utf8'));
1833
- return res;
1834
- }
1835
- };
1836
- const setTTMGRC = (config) => {
1837
- // updata cache config
1838
- config = config;
1839
- const originConfig = getTTMGRC() || {};
1840
- fs.writeFileSync(CONFIG_PATH$1, JSON.stringify({ ...originConfig, ...config }));
1841
- };
1842
- const resetTTMGRC = (config = {}) => {
1843
- fs.writeFileSync(CONFIG_PATH$1, JSON.stringify(config));
1844
- };
1845
- const getCurrentUser = () => {
1846
- try {
1847
- const config = getTTMGRC();
1848
- return config;
1849
- }
1850
- catch (err) {
1851
- return null;
1852
- }
1853
- };
1835
+ var build$1 = {};
1854
1836
 
1855
- // ppe_dev_tool
1856
- function getAxiosProxyConfig() {
1857
- const config = getTTMGRC();
1858
- const proxyUrl = config?.proxy; // Only use internal config, let axios handle env vars if not set?
1859
- // User requirement: "内置代理配置".
1860
- // My logic in previous turn: "Priority: Internal Config > Env Vars".
1861
- // If I use `https-proxy-agent`, I should probably handle env vars too if I want consistent behavior,
1862
- // OR just let axios handle env vars if config.proxy is missing.
1863
- // BUT, if I don't set httpsAgent, axios uses env vars by default.
1864
- // SO:
1865
- if (proxyUrl) {
1837
+ var socksclient = {};
1838
+
1839
+ var smartbuffer = {};
1840
+
1841
+ var utils = {};
1842
+
1843
+ var hasRequiredUtils;
1844
+
1845
+ function requireUtils () {
1846
+ if (hasRequiredUtils) return utils;
1847
+ hasRequiredUtils = 1;
1848
+ Object.defineProperty(utils, "__esModule", { value: true });
1849
+ const buffer_1 = require$$0$1;
1850
+ /**
1851
+ * Error strings
1852
+ */
1853
+ const ERRORS = {
1854
+ INVALID_ENCODING: 'Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.',
1855
+ INVALID_SMARTBUFFER_SIZE: 'Invalid size provided. Size must be a valid integer greater than zero.',
1856
+ INVALID_SMARTBUFFER_BUFFER: 'Invalid Buffer provided in SmartBufferOptions.',
1857
+ INVALID_SMARTBUFFER_OBJECT: 'Invalid SmartBufferOptions object supplied to SmartBuffer constructor or factory methods.',
1858
+ INVALID_OFFSET: 'An invalid offset value was provided.',
1859
+ INVALID_OFFSET_NON_NUMBER: 'An invalid offset value was provided. A numeric value is required.',
1860
+ INVALID_LENGTH: 'An invalid length value was provided.',
1861
+ INVALID_LENGTH_NON_NUMBER: 'An invalid length value was provived. A numeric value is required.',
1862
+ INVALID_TARGET_OFFSET: 'Target offset is beyond the bounds of the internal SmartBuffer data.',
1863
+ INVALID_TARGET_LENGTH: 'Specified length value moves cursor beyong the bounds of the internal SmartBuffer data.',
1864
+ INVALID_READ_BEYOND_BOUNDS: 'Attempted to read beyond the bounds of the managed data.',
1865
+ INVALID_WRITE_BEYOND_BOUNDS: 'Attempted to write beyond the bounds of the managed data.'
1866
+ };
1867
+ utils.ERRORS = ERRORS;
1868
+ /**
1869
+ * Checks if a given encoding is a valid Buffer encoding. (Throws an exception if check fails)
1870
+ *
1871
+ * @param { String } encoding The encoding string to check.
1872
+ */
1873
+ function checkEncoding(encoding) {
1874
+ if (!buffer_1.Buffer.isEncoding(encoding)) {
1875
+ throw new Error(ERRORS.INVALID_ENCODING);
1876
+ }
1877
+ }
1878
+ utils.checkEncoding = checkEncoding;
1879
+ /**
1880
+ * Checks if a given number is a finite integer. (Throws an exception if check fails)
1881
+ *
1882
+ * @param { Number } value The number value to check.
1883
+ */
1884
+ function isFiniteInteger(value) {
1885
+ return typeof value === 'number' && isFinite(value) && isInteger(value);
1886
+ }
1887
+ utils.isFiniteInteger = isFiniteInteger;
1888
+ /**
1889
+ * Checks if an offset/length value is valid. (Throws an exception if check fails)
1890
+ *
1891
+ * @param value The value to check.
1892
+ * @param offset True if checking an offset, false if checking a length.
1893
+ */
1894
+ function checkOffsetOrLengthValue(value, offset) {
1895
+ if (typeof value === 'number') {
1896
+ // Check for non finite/non integers
1897
+ if (!isFiniteInteger(value) || value < 0) {
1898
+ throw new Error(offset ? ERRORS.INVALID_OFFSET : ERRORS.INVALID_LENGTH);
1899
+ }
1900
+ }
1901
+ else {
1902
+ throw new Error(offset ? ERRORS.INVALID_OFFSET_NON_NUMBER : ERRORS.INVALID_LENGTH_NON_NUMBER);
1903
+ }
1904
+ }
1905
+ /**
1906
+ * Checks if a length value is valid. (Throws an exception if check fails)
1907
+ *
1908
+ * @param { Number } length The value to check.
1909
+ */
1910
+ function checkLengthValue(length) {
1911
+ checkOffsetOrLengthValue(length, false);
1912
+ }
1913
+ utils.checkLengthValue = checkLengthValue;
1914
+ /**
1915
+ * Checks if a offset value is valid. (Throws an exception if check fails)
1916
+ *
1917
+ * @param { Number } offset The value to check.
1918
+ */
1919
+ function checkOffsetValue(offset) {
1920
+ checkOffsetOrLengthValue(offset, true);
1921
+ }
1922
+ utils.checkOffsetValue = checkOffsetValue;
1923
+ /**
1924
+ * Checks if a target offset value is out of bounds. (Throws an exception if check fails)
1925
+ *
1926
+ * @param { Number } offset The offset value to check.
1927
+ * @param { SmartBuffer } buff The SmartBuffer instance to check against.
1928
+ */
1929
+ function checkTargetOffset(offset, buff) {
1930
+ if (offset < 0 || offset > buff.length) {
1931
+ throw new Error(ERRORS.INVALID_TARGET_OFFSET);
1932
+ }
1933
+ }
1934
+ utils.checkTargetOffset = checkTargetOffset;
1935
+ /**
1936
+ * Determines whether a given number is a integer.
1937
+ * @param value The number to check.
1938
+ */
1939
+ function isInteger(value) {
1940
+ return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
1941
+ }
1942
+ /**
1943
+ * Throws if Node.js version is too low to support bigint
1944
+ */
1945
+ function bigIntAndBufferInt64Check(bufferMethod) {
1946
+ if (typeof BigInt === 'undefined') {
1947
+ throw new Error('Platform does not support JS BigInt type.');
1948
+ }
1949
+ if (typeof buffer_1.Buffer.prototype[bufferMethod] === 'undefined') {
1950
+ throw new Error(`Platform does not support Buffer.prototype.${bufferMethod}.`);
1951
+ }
1952
+ }
1953
+ utils.bigIntAndBufferInt64Check = bigIntAndBufferInt64Check;
1954
+
1955
+ return utils;
1956
+ }
1957
+
1958
+ var hasRequiredSmartbuffer;
1959
+
1960
+ function requireSmartbuffer () {
1961
+ if (hasRequiredSmartbuffer) return smartbuffer;
1962
+ hasRequiredSmartbuffer = 1;
1963
+ Object.defineProperty(smartbuffer, "__esModule", { value: true });
1964
+ const utils_1 = requireUtils();
1965
+ // The default Buffer size if one is not provided.
1966
+ const DEFAULT_SMARTBUFFER_SIZE = 4096;
1967
+ // The default string encoding to use for reading/writing strings.
1968
+ const DEFAULT_SMARTBUFFER_ENCODING = 'utf8';
1969
+ class SmartBuffer {
1970
+ /**
1971
+ * Creates a new SmartBuffer instance.
1972
+ *
1973
+ * @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
1974
+ */
1975
+ constructor(options) {
1976
+ this.length = 0;
1977
+ this._encoding = DEFAULT_SMARTBUFFER_ENCODING;
1978
+ this._writeOffset = 0;
1979
+ this._readOffset = 0;
1980
+ if (SmartBuffer.isSmartBufferOptions(options)) {
1981
+ // Checks for encoding
1982
+ if (options.encoding) {
1983
+ utils_1.checkEncoding(options.encoding);
1984
+ this._encoding = options.encoding;
1985
+ }
1986
+ // Checks for initial size length
1987
+ if (options.size) {
1988
+ if (utils_1.isFiniteInteger(options.size) && options.size > 0) {
1989
+ this._buff = Buffer.allocUnsafe(options.size);
1990
+ }
1991
+ else {
1992
+ throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_SIZE);
1993
+ }
1994
+ // Check for initial Buffer
1995
+ }
1996
+ else if (options.buff) {
1997
+ if (Buffer.isBuffer(options.buff)) {
1998
+ this._buff = options.buff;
1999
+ this.length = options.buff.length;
2000
+ }
2001
+ else {
2002
+ throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_BUFFER);
2003
+ }
2004
+ }
2005
+ else {
2006
+ this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
2007
+ }
2008
+ }
2009
+ else {
2010
+ // If something was passed but it's not a SmartBufferOptions object
2011
+ if (typeof options !== 'undefined') {
2012
+ throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_OBJECT);
2013
+ }
2014
+ // Otherwise default to sane options
2015
+ this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
2016
+ }
2017
+ }
2018
+ /**
2019
+ * Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
2020
+ *
2021
+ * @param size { Number } The size of the internal Buffer.
2022
+ * @param encoding { String } The BufferEncoding to use for strings.
2023
+ *
2024
+ * @return { SmartBuffer }
2025
+ */
2026
+ static fromSize(size, encoding) {
2027
+ return new this({
2028
+ size: size,
2029
+ encoding: encoding
2030
+ });
2031
+ }
2032
+ /**
2033
+ * Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
2034
+ *
2035
+ * @param buffer { Buffer } The Buffer to use as the internal Buffer value.
2036
+ * @param encoding { String } The BufferEncoding to use for strings.
2037
+ *
2038
+ * @return { SmartBuffer }
2039
+ */
2040
+ static fromBuffer(buff, encoding) {
2041
+ return new this({
2042
+ buff: buff,
2043
+ encoding: encoding
2044
+ });
2045
+ }
2046
+ /**
2047
+ * Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
2048
+ *
2049
+ * @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
2050
+ */
2051
+ static fromOptions(options) {
2052
+ return new this(options);
2053
+ }
2054
+ /**
2055
+ * Type checking function that determines if an object is a SmartBufferOptions object.
2056
+ */
2057
+ static isSmartBufferOptions(options) {
2058
+ const castOptions = options;
2059
+ return (castOptions &&
2060
+ (castOptions.encoding !== undefined || castOptions.size !== undefined || castOptions.buff !== undefined));
2061
+ }
2062
+ // Signed integers
2063
+ /**
2064
+ * Reads an Int8 value from the current read position or an optionally provided offset.
2065
+ *
2066
+ * @param offset { Number } The offset to read data from (optional)
2067
+ * @return { Number }
2068
+ */
2069
+ readInt8(offset) {
2070
+ return this._readNumberValue(Buffer.prototype.readInt8, 1, offset);
2071
+ }
2072
+ /**
2073
+ * Reads an Int16BE value from the current read position or an optionally provided offset.
2074
+ *
2075
+ * @param offset { Number } The offset to read data from (optional)
2076
+ * @return { Number }
2077
+ */
2078
+ readInt16BE(offset) {
2079
+ return this._readNumberValue(Buffer.prototype.readInt16BE, 2, offset);
2080
+ }
2081
+ /**
2082
+ * Reads an Int16LE value from the current read position or an optionally provided offset.
2083
+ *
2084
+ * @param offset { Number } The offset to read data from (optional)
2085
+ * @return { Number }
2086
+ */
2087
+ readInt16LE(offset) {
2088
+ return this._readNumberValue(Buffer.prototype.readInt16LE, 2, offset);
2089
+ }
2090
+ /**
2091
+ * Reads an Int32BE value from the current read position or an optionally provided offset.
2092
+ *
2093
+ * @param offset { Number } The offset to read data from (optional)
2094
+ * @return { Number }
2095
+ */
2096
+ readInt32BE(offset) {
2097
+ return this._readNumberValue(Buffer.prototype.readInt32BE, 4, offset);
2098
+ }
2099
+ /**
2100
+ * Reads an Int32LE value from the current read position or an optionally provided offset.
2101
+ *
2102
+ * @param offset { Number } The offset to read data from (optional)
2103
+ * @return { Number }
2104
+ */
2105
+ readInt32LE(offset) {
2106
+ return this._readNumberValue(Buffer.prototype.readInt32LE, 4, offset);
2107
+ }
2108
+ /**
2109
+ * Reads a BigInt64BE value from the current read position or an optionally provided offset.
2110
+ *
2111
+ * @param offset { Number } The offset to read data from (optional)
2112
+ * @return { BigInt }
2113
+ */
2114
+ readBigInt64BE(offset) {
2115
+ utils_1.bigIntAndBufferInt64Check('readBigInt64BE');
2116
+ return this._readNumberValue(Buffer.prototype.readBigInt64BE, 8, offset);
2117
+ }
2118
+ /**
2119
+ * Reads a BigInt64LE value from the current read position or an optionally provided offset.
2120
+ *
2121
+ * @param offset { Number } The offset to read data from (optional)
2122
+ * @return { BigInt }
2123
+ */
2124
+ readBigInt64LE(offset) {
2125
+ utils_1.bigIntAndBufferInt64Check('readBigInt64LE');
2126
+ return this._readNumberValue(Buffer.prototype.readBigInt64LE, 8, offset);
2127
+ }
2128
+ /**
2129
+ * Writes an Int8 value to the current write position (or at optional offset).
2130
+ *
2131
+ * @param value { Number } The value to write.
2132
+ * @param offset { Number } The offset to write the value at.
2133
+ *
2134
+ * @return this
2135
+ */
2136
+ writeInt8(value, offset) {
2137
+ this._writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
2138
+ return this;
2139
+ }
2140
+ /**
2141
+ * Inserts an Int8 value at the given offset value.
2142
+ *
2143
+ * @param value { Number } The value to insert.
2144
+ * @param offset { Number } The offset to insert the value at.
2145
+ *
2146
+ * @return this
2147
+ */
2148
+ insertInt8(value, offset) {
2149
+ return this._insertNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
2150
+ }
2151
+ /**
2152
+ * Writes an Int16BE value to the current write position (or at optional offset).
2153
+ *
2154
+ * @param value { Number } The value to write.
2155
+ * @param offset { Number } The offset to write the value at.
2156
+ *
2157
+ * @return this
2158
+ */
2159
+ writeInt16BE(value, offset) {
2160
+ return this._writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
2161
+ }
2162
+ /**
2163
+ * Inserts an Int16BE value at the given offset value.
2164
+ *
2165
+ * @param value { Number } The value to insert.
2166
+ * @param offset { Number } The offset to insert the value at.
2167
+ *
2168
+ * @return this
2169
+ */
2170
+ insertInt16BE(value, offset) {
2171
+ return this._insertNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
2172
+ }
2173
+ /**
2174
+ * Writes an Int16LE value to the current write position (or at optional offset).
2175
+ *
2176
+ * @param value { Number } The value to write.
2177
+ * @param offset { Number } The offset to write the value at.
2178
+ *
2179
+ * @return this
2180
+ */
2181
+ writeInt16LE(value, offset) {
2182
+ return this._writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
2183
+ }
2184
+ /**
2185
+ * Inserts an Int16LE value at the given offset value.
2186
+ *
2187
+ * @param value { Number } The value to insert.
2188
+ * @param offset { Number } The offset to insert the value at.
2189
+ *
2190
+ * @return this
2191
+ */
2192
+ insertInt16LE(value, offset) {
2193
+ return this._insertNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
2194
+ }
2195
+ /**
2196
+ * Writes an Int32BE value to the current write position (or at optional offset).
2197
+ *
2198
+ * @param value { Number } The value to write.
2199
+ * @param offset { Number } The offset to write the value at.
2200
+ *
2201
+ * @return this
2202
+ */
2203
+ writeInt32BE(value, offset) {
2204
+ return this._writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
2205
+ }
2206
+ /**
2207
+ * Inserts an Int32BE value at the given offset value.
2208
+ *
2209
+ * @param value { Number } The value to insert.
2210
+ * @param offset { Number } The offset to insert the value at.
2211
+ *
2212
+ * @return this
2213
+ */
2214
+ insertInt32BE(value, offset) {
2215
+ return this._insertNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
2216
+ }
2217
+ /**
2218
+ * Writes an Int32LE value to the current write position (or at optional offset).
2219
+ *
2220
+ * @param value { Number } The value to write.
2221
+ * @param offset { Number } The offset to write the value at.
2222
+ *
2223
+ * @return this
2224
+ */
2225
+ writeInt32LE(value, offset) {
2226
+ return this._writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
2227
+ }
2228
+ /**
2229
+ * Inserts an Int32LE value at the given offset value.
2230
+ *
2231
+ * @param value { Number } The value to insert.
2232
+ * @param offset { Number } The offset to insert the value at.
2233
+ *
2234
+ * @return this
2235
+ */
2236
+ insertInt32LE(value, offset) {
2237
+ return this._insertNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
2238
+ }
2239
+ /**
2240
+ * Writes a BigInt64BE value to the current write position (or at optional offset).
2241
+ *
2242
+ * @param value { BigInt } The value to write.
2243
+ * @param offset { Number } The offset to write the value at.
2244
+ *
2245
+ * @return this
2246
+ */
2247
+ writeBigInt64BE(value, offset) {
2248
+ utils_1.bigIntAndBufferInt64Check('writeBigInt64BE');
2249
+ return this._writeNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset);
2250
+ }
2251
+ /**
2252
+ * Inserts a BigInt64BE value at the given offset value.
2253
+ *
2254
+ * @param value { BigInt } The value to insert.
2255
+ * @param offset { Number } The offset to insert the value at.
2256
+ *
2257
+ * @return this
2258
+ */
2259
+ insertBigInt64BE(value, offset) {
2260
+ utils_1.bigIntAndBufferInt64Check('writeBigInt64BE');
2261
+ return this._insertNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset);
2262
+ }
2263
+ /**
2264
+ * Writes a BigInt64LE value to the current write position (or at optional offset).
2265
+ *
2266
+ * @param value { BigInt } The value to write.
2267
+ * @param offset { Number } The offset to write the value at.
2268
+ *
2269
+ * @return this
2270
+ */
2271
+ writeBigInt64LE(value, offset) {
2272
+ utils_1.bigIntAndBufferInt64Check('writeBigInt64LE');
2273
+ return this._writeNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset);
2274
+ }
2275
+ /**
2276
+ * Inserts a Int64LE value at the given offset value.
2277
+ *
2278
+ * @param value { BigInt } The value to insert.
2279
+ * @param offset { Number } The offset to insert the value at.
2280
+ *
2281
+ * @return this
2282
+ */
2283
+ insertBigInt64LE(value, offset) {
2284
+ utils_1.bigIntAndBufferInt64Check('writeBigInt64LE');
2285
+ return this._insertNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset);
2286
+ }
2287
+ // Unsigned Integers
2288
+ /**
2289
+ * Reads an UInt8 value from the current read position or an optionally provided offset.
2290
+ *
2291
+ * @param offset { Number } The offset to read data from (optional)
2292
+ * @return { Number }
2293
+ */
2294
+ readUInt8(offset) {
2295
+ return this._readNumberValue(Buffer.prototype.readUInt8, 1, offset);
2296
+ }
2297
+ /**
2298
+ * Reads an UInt16BE value from the current read position or an optionally provided offset.
2299
+ *
2300
+ * @param offset { Number } The offset to read data from (optional)
2301
+ * @return { Number }
2302
+ */
2303
+ readUInt16BE(offset) {
2304
+ return this._readNumberValue(Buffer.prototype.readUInt16BE, 2, offset);
2305
+ }
2306
+ /**
2307
+ * Reads an UInt16LE value from the current read position or an optionally provided offset.
2308
+ *
2309
+ * @param offset { Number } The offset to read data from (optional)
2310
+ * @return { Number }
2311
+ */
2312
+ readUInt16LE(offset) {
2313
+ return this._readNumberValue(Buffer.prototype.readUInt16LE, 2, offset);
2314
+ }
2315
+ /**
2316
+ * Reads an UInt32BE value from the current read position or an optionally provided offset.
2317
+ *
2318
+ * @param offset { Number } The offset to read data from (optional)
2319
+ * @return { Number }
2320
+ */
2321
+ readUInt32BE(offset) {
2322
+ return this._readNumberValue(Buffer.prototype.readUInt32BE, 4, offset);
2323
+ }
2324
+ /**
2325
+ * Reads an UInt32LE value from the current read position or an optionally provided offset.
2326
+ *
2327
+ * @param offset { Number } The offset to read data from (optional)
2328
+ * @return { Number }
2329
+ */
2330
+ readUInt32LE(offset) {
2331
+ return this._readNumberValue(Buffer.prototype.readUInt32LE, 4, offset);
2332
+ }
2333
+ /**
2334
+ * Reads a BigUInt64BE value from the current read position or an optionally provided offset.
2335
+ *
2336
+ * @param offset { Number } The offset to read data from (optional)
2337
+ * @return { BigInt }
2338
+ */
2339
+ readBigUInt64BE(offset) {
2340
+ utils_1.bigIntAndBufferInt64Check('readBigUInt64BE');
2341
+ return this._readNumberValue(Buffer.prototype.readBigUInt64BE, 8, offset);
2342
+ }
2343
+ /**
2344
+ * Reads a BigUInt64LE value from the current read position or an optionally provided offset.
2345
+ *
2346
+ * @param offset { Number } The offset to read data from (optional)
2347
+ * @return { BigInt }
2348
+ */
2349
+ readBigUInt64LE(offset) {
2350
+ utils_1.bigIntAndBufferInt64Check('readBigUInt64LE');
2351
+ return this._readNumberValue(Buffer.prototype.readBigUInt64LE, 8, offset);
2352
+ }
2353
+ /**
2354
+ * Writes an UInt8 value to the current write position (or at optional offset).
2355
+ *
2356
+ * @param value { Number } The value to write.
2357
+ * @param offset { Number } The offset to write the value at.
2358
+ *
2359
+ * @return this
2360
+ */
2361
+ writeUInt8(value, offset) {
2362
+ return this._writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
2363
+ }
2364
+ /**
2365
+ * Inserts an UInt8 value at the given offset value.
2366
+ *
2367
+ * @param value { Number } The value to insert.
2368
+ * @param offset { Number } The offset to insert the value at.
2369
+ *
2370
+ * @return this
2371
+ */
2372
+ insertUInt8(value, offset) {
2373
+ return this._insertNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
2374
+ }
2375
+ /**
2376
+ * Writes an UInt16BE value to the current write position (or at optional offset).
2377
+ *
2378
+ * @param value { Number } The value to write.
2379
+ * @param offset { Number } The offset to write the value at.
2380
+ *
2381
+ * @return this
2382
+ */
2383
+ writeUInt16BE(value, offset) {
2384
+ return this._writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
2385
+ }
2386
+ /**
2387
+ * Inserts an UInt16BE value at the given offset value.
2388
+ *
2389
+ * @param value { Number } The value to insert.
2390
+ * @param offset { Number } The offset to insert the value at.
2391
+ *
2392
+ * @return this
2393
+ */
2394
+ insertUInt16BE(value, offset) {
2395
+ return this._insertNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
2396
+ }
2397
+ /**
2398
+ * Writes an UInt16LE value to the current write position (or at optional offset).
2399
+ *
2400
+ * @param value { Number } The value to write.
2401
+ * @param offset { Number } The offset to write the value at.
2402
+ *
2403
+ * @return this
2404
+ */
2405
+ writeUInt16LE(value, offset) {
2406
+ return this._writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
2407
+ }
2408
+ /**
2409
+ * Inserts an UInt16LE value at the given offset value.
2410
+ *
2411
+ * @param value { Number } The value to insert.
2412
+ * @param offset { Number } The offset to insert the value at.
2413
+ *
2414
+ * @return this
2415
+ */
2416
+ insertUInt16LE(value, offset) {
2417
+ return this._insertNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
2418
+ }
2419
+ /**
2420
+ * Writes an UInt32BE value to the current write position (or at optional offset).
2421
+ *
2422
+ * @param value { Number } The value to write.
2423
+ * @param offset { Number } The offset to write the value at.
2424
+ *
2425
+ * @return this
2426
+ */
2427
+ writeUInt32BE(value, offset) {
2428
+ return this._writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
2429
+ }
2430
+ /**
2431
+ * Inserts an UInt32BE value at the given offset value.
2432
+ *
2433
+ * @param value { Number } The value to insert.
2434
+ * @param offset { Number } The offset to insert the value at.
2435
+ *
2436
+ * @return this
2437
+ */
2438
+ insertUInt32BE(value, offset) {
2439
+ return this._insertNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
2440
+ }
2441
+ /**
2442
+ * Writes an UInt32LE value to the current write position (or at optional offset).
2443
+ *
2444
+ * @param value { Number } The value to write.
2445
+ * @param offset { Number } The offset to write the value at.
2446
+ *
2447
+ * @return this
2448
+ */
2449
+ writeUInt32LE(value, offset) {
2450
+ return this._writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
2451
+ }
2452
+ /**
2453
+ * Inserts an UInt32LE value at the given offset value.
2454
+ *
2455
+ * @param value { Number } The value to insert.
2456
+ * @param offset { Number } The offset to insert the value at.
2457
+ *
2458
+ * @return this
2459
+ */
2460
+ insertUInt32LE(value, offset) {
2461
+ return this._insertNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
2462
+ }
2463
+ /**
2464
+ * Writes a BigUInt64BE value to the current write position (or at optional offset).
2465
+ *
2466
+ * @param value { Number } The value to write.
2467
+ * @param offset { Number } The offset to write the value at.
2468
+ *
2469
+ * @return this
2470
+ */
2471
+ writeBigUInt64BE(value, offset) {
2472
+ utils_1.bigIntAndBufferInt64Check('writeBigUInt64BE');
2473
+ return this._writeNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset);
2474
+ }
2475
+ /**
2476
+ * Inserts a BigUInt64BE value at the given offset value.
2477
+ *
2478
+ * @param value { Number } The value to insert.
2479
+ * @param offset { Number } The offset to insert the value at.
2480
+ *
2481
+ * @return this
2482
+ */
2483
+ insertBigUInt64BE(value, offset) {
2484
+ utils_1.bigIntAndBufferInt64Check('writeBigUInt64BE');
2485
+ return this._insertNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset);
2486
+ }
2487
+ /**
2488
+ * Writes a BigUInt64LE value to the current write position (or at optional offset).
2489
+ *
2490
+ * @param value { Number } The value to write.
2491
+ * @param offset { Number } The offset to write the value at.
2492
+ *
2493
+ * @return this
2494
+ */
2495
+ writeBigUInt64LE(value, offset) {
2496
+ utils_1.bigIntAndBufferInt64Check('writeBigUInt64LE');
2497
+ return this._writeNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset);
2498
+ }
2499
+ /**
2500
+ * Inserts a BigUInt64LE value at the given offset value.
2501
+ *
2502
+ * @param value { Number } The value to insert.
2503
+ * @param offset { Number } The offset to insert the value at.
2504
+ *
2505
+ * @return this
2506
+ */
2507
+ insertBigUInt64LE(value, offset) {
2508
+ utils_1.bigIntAndBufferInt64Check('writeBigUInt64LE');
2509
+ return this._insertNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset);
2510
+ }
2511
+ // Floating Point
2512
+ /**
2513
+ * Reads an FloatBE value from the current read position or an optionally provided offset.
2514
+ *
2515
+ * @param offset { Number } The offset to read data from (optional)
2516
+ * @return { Number }
2517
+ */
2518
+ readFloatBE(offset) {
2519
+ return this._readNumberValue(Buffer.prototype.readFloatBE, 4, offset);
2520
+ }
2521
+ /**
2522
+ * Reads an FloatLE value from the current read position or an optionally provided offset.
2523
+ *
2524
+ * @param offset { Number } The offset to read data from (optional)
2525
+ * @return { Number }
2526
+ */
2527
+ readFloatLE(offset) {
2528
+ return this._readNumberValue(Buffer.prototype.readFloatLE, 4, offset);
2529
+ }
2530
+ /**
2531
+ * Writes a FloatBE value to the current write position (or at optional offset).
2532
+ *
2533
+ * @param value { Number } The value to write.
2534
+ * @param offset { Number } The offset to write the value at.
2535
+ *
2536
+ * @return this
2537
+ */
2538
+ writeFloatBE(value, offset) {
2539
+ return this._writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
2540
+ }
2541
+ /**
2542
+ * Inserts a FloatBE value at the given offset value.
2543
+ *
2544
+ * @param value { Number } The value to insert.
2545
+ * @param offset { Number } The offset to insert the value at.
2546
+ *
2547
+ * @return this
2548
+ */
2549
+ insertFloatBE(value, offset) {
2550
+ return this._insertNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
2551
+ }
2552
+ /**
2553
+ * Writes a FloatLE value to the current write position (or at optional offset).
2554
+ *
2555
+ * @param value { Number } The value to write.
2556
+ * @param offset { Number } The offset to write the value at.
2557
+ *
2558
+ * @return this
2559
+ */
2560
+ writeFloatLE(value, offset) {
2561
+ return this._writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
2562
+ }
2563
+ /**
2564
+ * Inserts a FloatLE value at the given offset value.
2565
+ *
2566
+ * @param value { Number } The value to insert.
2567
+ * @param offset { Number } The offset to insert the value at.
2568
+ *
2569
+ * @return this
2570
+ */
2571
+ insertFloatLE(value, offset) {
2572
+ return this._insertNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
2573
+ }
2574
+ // Double Floating Point
2575
+ /**
2576
+ * Reads an DoublEBE value from the current read position or an optionally provided offset.
2577
+ *
2578
+ * @param offset { Number } The offset to read data from (optional)
2579
+ * @return { Number }
2580
+ */
2581
+ readDoubleBE(offset) {
2582
+ return this._readNumberValue(Buffer.prototype.readDoubleBE, 8, offset);
2583
+ }
2584
+ /**
2585
+ * Reads an DoubleLE value from the current read position or an optionally provided offset.
2586
+ *
2587
+ * @param offset { Number } The offset to read data from (optional)
2588
+ * @return { Number }
2589
+ */
2590
+ readDoubleLE(offset) {
2591
+ return this._readNumberValue(Buffer.prototype.readDoubleLE, 8, offset);
2592
+ }
2593
+ /**
2594
+ * Writes a DoubleBE value to the current write position (or at optional offset).
2595
+ *
2596
+ * @param value { Number } The value to write.
2597
+ * @param offset { Number } The offset to write the value at.
2598
+ *
2599
+ * @return this
2600
+ */
2601
+ writeDoubleBE(value, offset) {
2602
+ return this._writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
2603
+ }
2604
+ /**
2605
+ * Inserts a DoubleBE value at the given offset value.
2606
+ *
2607
+ * @param value { Number } The value to insert.
2608
+ * @param offset { Number } The offset to insert the value at.
2609
+ *
2610
+ * @return this
2611
+ */
2612
+ insertDoubleBE(value, offset) {
2613
+ return this._insertNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
2614
+ }
2615
+ /**
2616
+ * Writes a DoubleLE value to the current write position (or at optional offset).
2617
+ *
2618
+ * @param value { Number } The value to write.
2619
+ * @param offset { Number } The offset to write the value at.
2620
+ *
2621
+ * @return this
2622
+ */
2623
+ writeDoubleLE(value, offset) {
2624
+ return this._writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
2625
+ }
2626
+ /**
2627
+ * Inserts a DoubleLE value at the given offset value.
2628
+ *
2629
+ * @param value { Number } The value to insert.
2630
+ * @param offset { Number } The offset to insert the value at.
2631
+ *
2632
+ * @return this
2633
+ */
2634
+ insertDoubleLE(value, offset) {
2635
+ return this._insertNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
2636
+ }
2637
+ // Strings
2638
+ /**
2639
+ * Reads a String from the current read position.
2640
+ *
2641
+ * @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
2642
+ * the string (Defaults to instance level encoding).
2643
+ * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
2644
+ *
2645
+ * @return { String }
2646
+ */
2647
+ readString(arg1, encoding) {
2648
+ let lengthVal;
2649
+ // Length provided
2650
+ if (typeof arg1 === 'number') {
2651
+ utils_1.checkLengthValue(arg1);
2652
+ lengthVal = Math.min(arg1, this.length - this._readOffset);
2653
+ }
2654
+ else {
2655
+ encoding = arg1;
2656
+ lengthVal = this.length - this._readOffset;
2657
+ }
2658
+ // Check encoding
2659
+ if (typeof encoding !== 'undefined') {
2660
+ utils_1.checkEncoding(encoding);
2661
+ }
2662
+ const value = this._buff.slice(this._readOffset, this._readOffset + lengthVal).toString(encoding || this._encoding);
2663
+ this._readOffset += lengthVal;
2664
+ return value;
2665
+ }
2666
+ /**
2667
+ * Inserts a String
2668
+ *
2669
+ * @param value { String } The String value to insert.
2670
+ * @param offset { Number } The offset to insert the string at.
2671
+ * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
2672
+ *
2673
+ * @return this
2674
+ */
2675
+ insertString(value, offset, encoding) {
2676
+ utils_1.checkOffsetValue(offset);
2677
+ return this._handleString(value, true, offset, encoding);
2678
+ }
2679
+ /**
2680
+ * Writes a String
2681
+ *
2682
+ * @param value { String } The String value to write.
2683
+ * @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
2684
+ * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
2685
+ *
2686
+ * @return this
2687
+ */
2688
+ writeString(value, arg2, encoding) {
2689
+ return this._handleString(value, false, arg2, encoding);
2690
+ }
2691
+ /**
2692
+ * Reads a null-terminated String from the current read position.
2693
+ *
2694
+ * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
2695
+ *
2696
+ * @return { String }
2697
+ */
2698
+ readStringNT(encoding) {
2699
+ if (typeof encoding !== 'undefined') {
2700
+ utils_1.checkEncoding(encoding);
2701
+ }
2702
+ // Set null character position to the end SmartBuffer instance.
2703
+ let nullPos = this.length;
2704
+ // Find next null character (if one is not found, default from above is used)
2705
+ for (let i = this._readOffset; i < this.length; i++) {
2706
+ if (this._buff[i] === 0x00) {
2707
+ nullPos = i;
2708
+ break;
2709
+ }
2710
+ }
2711
+ // Read string value
2712
+ const value = this._buff.slice(this._readOffset, nullPos);
2713
+ // Increment internal Buffer read offset
2714
+ this._readOffset = nullPos + 1;
2715
+ return value.toString(encoding || this._encoding);
2716
+ }
2717
+ /**
2718
+ * Inserts a null-terminated String.
2719
+ *
2720
+ * @param value { String } The String value to write.
2721
+ * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
2722
+ * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
2723
+ *
2724
+ * @return this
2725
+ */
2726
+ insertStringNT(value, offset, encoding) {
2727
+ utils_1.checkOffsetValue(offset);
2728
+ // Write Values
2729
+ this.insertString(value, offset, encoding);
2730
+ this.insertUInt8(0x00, offset + value.length);
2731
+ return this;
2732
+ }
2733
+ /**
2734
+ * Writes a null-terminated String.
2735
+ *
2736
+ * @param value { String } The String value to write.
2737
+ * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
2738
+ * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
2739
+ *
2740
+ * @return this
2741
+ */
2742
+ writeStringNT(value, arg2, encoding) {
2743
+ // Write Values
2744
+ this.writeString(value, arg2, encoding);
2745
+ this.writeUInt8(0x00, typeof arg2 === 'number' ? arg2 + value.length : this.writeOffset);
2746
+ return this;
2747
+ }
2748
+ // Buffers
2749
+ /**
2750
+ * Reads a Buffer from the internal read position.
2751
+ *
2752
+ * @param length { Number } The length of data to read as a Buffer.
2753
+ *
2754
+ * @return { Buffer }
2755
+ */
2756
+ readBuffer(length) {
2757
+ if (typeof length !== 'undefined') {
2758
+ utils_1.checkLengthValue(length);
2759
+ }
2760
+ const lengthVal = typeof length === 'number' ? length : this.length;
2761
+ const endPoint = Math.min(this.length, this._readOffset + lengthVal);
2762
+ // Read buffer value
2763
+ const value = this._buff.slice(this._readOffset, endPoint);
2764
+ // Increment internal Buffer read offset
2765
+ this._readOffset = endPoint;
2766
+ return value;
2767
+ }
2768
+ /**
2769
+ * Writes a Buffer to the current write position.
2770
+ *
2771
+ * @param value { Buffer } The Buffer to write.
2772
+ * @param offset { Number } The offset to write the Buffer to.
2773
+ *
2774
+ * @return this
2775
+ */
2776
+ insertBuffer(value, offset) {
2777
+ utils_1.checkOffsetValue(offset);
2778
+ return this._handleBuffer(value, true, offset);
2779
+ }
2780
+ /**
2781
+ * Writes a Buffer to the current write position.
2782
+ *
2783
+ * @param value { Buffer } The Buffer to write.
2784
+ * @param offset { Number } The offset to write the Buffer to.
2785
+ *
2786
+ * @return this
2787
+ */
2788
+ writeBuffer(value, offset) {
2789
+ return this._handleBuffer(value, false, offset);
2790
+ }
2791
+ /**
2792
+ * Reads a null-terminated Buffer from the current read poisiton.
2793
+ *
2794
+ * @return { Buffer }
2795
+ */
2796
+ readBufferNT() {
2797
+ // Set null character position to the end SmartBuffer instance.
2798
+ let nullPos = this.length;
2799
+ // Find next null character (if one is not found, default from above is used)
2800
+ for (let i = this._readOffset; i < this.length; i++) {
2801
+ if (this._buff[i] === 0x00) {
2802
+ nullPos = i;
2803
+ break;
2804
+ }
2805
+ }
2806
+ // Read value
2807
+ const value = this._buff.slice(this._readOffset, nullPos);
2808
+ // Increment internal Buffer read offset
2809
+ this._readOffset = nullPos + 1;
2810
+ return value;
2811
+ }
2812
+ /**
2813
+ * Inserts a null-terminated Buffer.
2814
+ *
2815
+ * @param value { Buffer } The Buffer to write.
2816
+ * @param offset { Number } The offset to write the Buffer to.
2817
+ *
2818
+ * @return this
2819
+ */
2820
+ insertBufferNT(value, offset) {
2821
+ utils_1.checkOffsetValue(offset);
2822
+ // Write Values
2823
+ this.insertBuffer(value, offset);
2824
+ this.insertUInt8(0x00, offset + value.length);
2825
+ return this;
2826
+ }
2827
+ /**
2828
+ * Writes a null-terminated Buffer.
2829
+ *
2830
+ * @param value { Buffer } The Buffer to write.
2831
+ * @param offset { Number } The offset to write the Buffer to.
2832
+ *
2833
+ * @return this
2834
+ */
2835
+ writeBufferNT(value, offset) {
2836
+ // Checks for valid numberic value;
2837
+ if (typeof offset !== 'undefined') {
2838
+ utils_1.checkOffsetValue(offset);
2839
+ }
2840
+ // Write Values
2841
+ this.writeBuffer(value, offset);
2842
+ this.writeUInt8(0x00, typeof offset === 'number' ? offset + value.length : this._writeOffset);
2843
+ return this;
2844
+ }
2845
+ /**
2846
+ * Clears the SmartBuffer instance to its original empty state.
2847
+ */
2848
+ clear() {
2849
+ this._writeOffset = 0;
2850
+ this._readOffset = 0;
2851
+ this.length = 0;
2852
+ return this;
2853
+ }
2854
+ /**
2855
+ * Gets the remaining data left to be read from the SmartBuffer instance.
2856
+ *
2857
+ * @return { Number }
2858
+ */
2859
+ remaining() {
2860
+ return this.length - this._readOffset;
2861
+ }
2862
+ /**
2863
+ * Gets the current read offset value of the SmartBuffer instance.
2864
+ *
2865
+ * @return { Number }
2866
+ */
2867
+ get readOffset() {
2868
+ return this._readOffset;
2869
+ }
2870
+ /**
2871
+ * Sets the read offset value of the SmartBuffer instance.
2872
+ *
2873
+ * @param offset { Number } - The offset value to set.
2874
+ */
2875
+ set readOffset(offset) {
2876
+ utils_1.checkOffsetValue(offset);
2877
+ // Check for bounds.
2878
+ utils_1.checkTargetOffset(offset, this);
2879
+ this._readOffset = offset;
2880
+ }
2881
+ /**
2882
+ * Gets the current write offset value of the SmartBuffer instance.
2883
+ *
2884
+ * @return { Number }
2885
+ */
2886
+ get writeOffset() {
2887
+ return this._writeOffset;
2888
+ }
2889
+ /**
2890
+ * Sets the write offset value of the SmartBuffer instance.
2891
+ *
2892
+ * @param offset { Number } - The offset value to set.
2893
+ */
2894
+ set writeOffset(offset) {
2895
+ utils_1.checkOffsetValue(offset);
2896
+ // Check for bounds.
2897
+ utils_1.checkTargetOffset(offset, this);
2898
+ this._writeOffset = offset;
2899
+ }
2900
+ /**
2901
+ * Gets the currently set string encoding of the SmartBuffer instance.
2902
+ *
2903
+ * @return { BufferEncoding } The string Buffer encoding currently set.
2904
+ */
2905
+ get encoding() {
2906
+ return this._encoding;
2907
+ }
2908
+ /**
2909
+ * Sets the string encoding of the SmartBuffer instance.
2910
+ *
2911
+ * @param encoding { BufferEncoding } The string Buffer encoding to set.
2912
+ */
2913
+ set encoding(encoding) {
2914
+ utils_1.checkEncoding(encoding);
2915
+ this._encoding = encoding;
2916
+ }
2917
+ /**
2918
+ * Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
2919
+ *
2920
+ * @return { Buffer } The Buffer value.
2921
+ */
2922
+ get internalBuffer() {
2923
+ return this._buff;
2924
+ }
2925
+ /**
2926
+ * Gets the value of the internal managed Buffer (Includes managed data only)
2927
+ *
2928
+ * @param { Buffer }
2929
+ */
2930
+ toBuffer() {
2931
+ return this._buff.slice(0, this.length);
2932
+ }
2933
+ /**
2934
+ * Gets the String value of the internal managed Buffer
2935
+ *
2936
+ * @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
2937
+ */
2938
+ toString(encoding) {
2939
+ const encodingVal = typeof encoding === 'string' ? encoding : this._encoding;
2940
+ // Check for invalid encoding.
2941
+ utils_1.checkEncoding(encodingVal);
2942
+ return this._buff.toString(encodingVal, 0, this.length);
2943
+ }
2944
+ /**
2945
+ * Destroys the SmartBuffer instance.
2946
+ */
2947
+ destroy() {
2948
+ this.clear();
2949
+ return this;
2950
+ }
2951
+ /**
2952
+ * Handles inserting and writing strings.
2953
+ *
2954
+ * @param value { String } The String value to insert.
2955
+ * @param isInsert { Boolean } True if inserting a string, false if writing.
2956
+ * @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
2957
+ * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
2958
+ */
2959
+ _handleString(value, isInsert, arg3, encoding) {
2960
+ let offsetVal = this._writeOffset;
2961
+ let encodingVal = this._encoding;
2962
+ // Check for offset
2963
+ if (typeof arg3 === 'number') {
2964
+ offsetVal = arg3;
2965
+ // Check for encoding
2966
+ }
2967
+ else if (typeof arg3 === 'string') {
2968
+ utils_1.checkEncoding(arg3);
2969
+ encodingVal = arg3;
2970
+ }
2971
+ // Check for encoding (third param)
2972
+ if (typeof encoding === 'string') {
2973
+ utils_1.checkEncoding(encoding);
2974
+ encodingVal = encoding;
2975
+ }
2976
+ // Calculate bytelength of string.
2977
+ const byteLength = Buffer.byteLength(value, encodingVal);
2978
+ // Ensure there is enough internal Buffer capacity.
2979
+ if (isInsert) {
2980
+ this.ensureInsertable(byteLength, offsetVal);
2981
+ }
2982
+ else {
2983
+ this._ensureWriteable(byteLength, offsetVal);
2984
+ }
2985
+ // Write value
2986
+ this._buff.write(value, offsetVal, byteLength, encodingVal);
2987
+ // Increment internal Buffer write offset;
2988
+ if (isInsert) {
2989
+ this._writeOffset += byteLength;
2990
+ }
2991
+ else {
2992
+ // If an offset was given, check to see if we wrote beyond the current writeOffset.
2993
+ if (typeof arg3 === 'number') {
2994
+ this._writeOffset = Math.max(this._writeOffset, offsetVal + byteLength);
2995
+ }
2996
+ else {
2997
+ // If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
2998
+ this._writeOffset += byteLength;
2999
+ }
3000
+ }
3001
+ return this;
3002
+ }
3003
+ /**
3004
+ * Handles writing or insert of a Buffer.
3005
+ *
3006
+ * @param value { Buffer } The Buffer to write.
3007
+ * @param offset { Number } The offset to write the Buffer to.
3008
+ */
3009
+ _handleBuffer(value, isInsert, offset) {
3010
+ const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
3011
+ // Ensure there is enough internal Buffer capacity.
3012
+ if (isInsert) {
3013
+ this.ensureInsertable(value.length, offsetVal);
3014
+ }
3015
+ else {
3016
+ this._ensureWriteable(value.length, offsetVal);
3017
+ }
3018
+ // Write buffer value
3019
+ value.copy(this._buff, offsetVal);
3020
+ // Increment internal Buffer write offset;
3021
+ if (isInsert) {
3022
+ this._writeOffset += value.length;
3023
+ }
3024
+ else {
3025
+ // If an offset was given, check to see if we wrote beyond the current writeOffset.
3026
+ if (typeof offset === 'number') {
3027
+ this._writeOffset = Math.max(this._writeOffset, offsetVal + value.length);
3028
+ }
3029
+ else {
3030
+ // If no offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
3031
+ this._writeOffset += value.length;
3032
+ }
3033
+ }
3034
+ return this;
3035
+ }
3036
+ /**
3037
+ * Ensures that the internal Buffer is large enough to read data.
3038
+ *
3039
+ * @param length { Number } The length of the data that needs to be read.
3040
+ * @param offset { Number } The offset of the data that needs to be read.
3041
+ */
3042
+ ensureReadable(length, offset) {
3043
+ // Offset value defaults to managed read offset.
3044
+ let offsetVal = this._readOffset;
3045
+ // If an offset was provided, use it.
3046
+ if (typeof offset !== 'undefined') {
3047
+ // Checks for valid numberic value;
3048
+ utils_1.checkOffsetValue(offset);
3049
+ // Overide with custom offset.
3050
+ offsetVal = offset;
3051
+ }
3052
+ // Checks if offset is below zero, or the offset+length offset is beyond the total length of the managed data.
3053
+ if (offsetVal < 0 || offsetVal + length > this.length) {
3054
+ throw new Error(utils_1.ERRORS.INVALID_READ_BEYOND_BOUNDS);
3055
+ }
3056
+ }
3057
+ /**
3058
+ * Ensures that the internal Buffer is large enough to insert data.
3059
+ *
3060
+ * @param dataLength { Number } The length of the data that needs to be written.
3061
+ * @param offset { Number } The offset of the data to be written.
3062
+ */
3063
+ ensureInsertable(dataLength, offset) {
3064
+ // Checks for valid numberic value;
3065
+ utils_1.checkOffsetValue(offset);
3066
+ // Ensure there is enough internal Buffer capacity.
3067
+ this._ensureCapacity(this.length + dataLength);
3068
+ // If an offset was provided and its not the very end of the buffer, copy data into appropriate location in regards to the offset.
3069
+ if (offset < this.length) {
3070
+ this._buff.copy(this._buff, offset + dataLength, offset, this._buff.length);
3071
+ }
3072
+ // Adjust tracked smart buffer length
3073
+ if (offset + dataLength > this.length) {
3074
+ this.length = offset + dataLength;
3075
+ }
3076
+ else {
3077
+ this.length += dataLength;
3078
+ }
3079
+ }
3080
+ /**
3081
+ * Ensures that the internal Buffer is large enough to write data.
3082
+ *
3083
+ * @param dataLength { Number } The length of the data that needs to be written.
3084
+ * @param offset { Number } The offset of the data to be written (defaults to writeOffset).
3085
+ */
3086
+ _ensureWriteable(dataLength, offset) {
3087
+ const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
3088
+ // Ensure enough capacity to write data.
3089
+ this._ensureCapacity(offsetVal + dataLength);
3090
+ // Adjust SmartBuffer length (if offset + length is larger than managed length, adjust length)
3091
+ if (offsetVal + dataLength > this.length) {
3092
+ this.length = offsetVal + dataLength;
3093
+ }
3094
+ }
3095
+ /**
3096
+ * Ensures that the internal Buffer is large enough to write at least the given amount of data.
3097
+ *
3098
+ * @param minLength { Number } The minimum length of the data needs to be written.
3099
+ */
3100
+ _ensureCapacity(minLength) {
3101
+ const oldLength = this._buff.length;
3102
+ if (minLength > oldLength) {
3103
+ let data = this._buff;
3104
+ let newLength = (oldLength * 3) / 2 + 1;
3105
+ if (newLength < minLength) {
3106
+ newLength = minLength;
3107
+ }
3108
+ this._buff = Buffer.allocUnsafe(newLength);
3109
+ data.copy(this._buff, 0, 0, oldLength);
3110
+ }
3111
+ }
3112
+ /**
3113
+ * Reads a numeric number value using the provided function.
3114
+ *
3115
+ * @typeparam T { number | bigint } The type of the value to be read
3116
+ *
3117
+ * @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
3118
+ * @param byteSize { Number } The number of bytes read.
3119
+ * @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
3120
+ *
3121
+ * @returns { T } the number value
3122
+ */
3123
+ _readNumberValue(func, byteSize, offset) {
3124
+ this.ensureReadable(byteSize, offset);
3125
+ // Call Buffer.readXXXX();
3126
+ const value = func.call(this._buff, typeof offset === 'number' ? offset : this._readOffset);
3127
+ // Adjust internal read offset if an optional read offset was not provided.
3128
+ if (typeof offset === 'undefined') {
3129
+ this._readOffset += byteSize;
3130
+ }
3131
+ return value;
3132
+ }
3133
+ /**
3134
+ * Inserts a numeric number value based on the given offset and value.
3135
+ *
3136
+ * @typeparam T { number | bigint } The type of the value to be written
3137
+ *
3138
+ * @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with.
3139
+ * @param byteSize { Number } The number of bytes written.
3140
+ * @param value { T } The number value to write.
3141
+ * @param offset { Number } the offset to write the number at (REQUIRED).
3142
+ *
3143
+ * @returns SmartBuffer this buffer
3144
+ */
3145
+ _insertNumberValue(func, byteSize, value, offset) {
3146
+ // Check for invalid offset values.
3147
+ utils_1.checkOffsetValue(offset);
3148
+ // Ensure there is enough internal Buffer capacity. (raw offset is passed)
3149
+ this.ensureInsertable(byteSize, offset);
3150
+ // Call buffer.writeXXXX();
3151
+ func.call(this._buff, value, offset);
3152
+ // Adjusts internally managed write offset.
3153
+ this._writeOffset += byteSize;
3154
+ return this;
3155
+ }
3156
+ /**
3157
+ * Writes a numeric number value based on the given offset and value.
3158
+ *
3159
+ * @typeparam T { number | bigint } The type of the value to be written
3160
+ *
3161
+ * @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with.
3162
+ * @param byteSize { Number } The number of bytes written.
3163
+ * @param value { T } The number value to write.
3164
+ * @param offset { Number } the offset to write the number at (REQUIRED).
3165
+ *
3166
+ * @returns SmartBuffer this buffer
3167
+ */
3168
+ _writeNumberValue(func, byteSize, value, offset) {
3169
+ // If an offset was provided, validate it.
3170
+ if (typeof offset === 'number') {
3171
+ // Check if we're writing beyond the bounds of the managed data.
3172
+ if (offset < 0) {
3173
+ throw new Error(utils_1.ERRORS.INVALID_WRITE_BEYOND_BOUNDS);
3174
+ }
3175
+ utils_1.checkOffsetValue(offset);
3176
+ }
3177
+ // Default to writeOffset if no offset value was given.
3178
+ const offsetVal = typeof offset === 'number' ? offset : this._writeOffset;
3179
+ // Ensure there is enough internal Buffer capacity. (raw offset is passed)
3180
+ this._ensureWriteable(byteSize, offsetVal);
3181
+ func.call(this._buff, value, offsetVal);
3182
+ // If an offset was given, check to see if we wrote beyond the current writeOffset.
3183
+ if (typeof offset === 'number') {
3184
+ this._writeOffset = Math.max(this._writeOffset, offsetVal + byteSize);
3185
+ }
3186
+ else {
3187
+ // If no numeric offset was given, we wrote to the end of the SmartBuffer so increment writeOffset.
3188
+ this._writeOffset += byteSize;
3189
+ }
3190
+ return this;
3191
+ }
3192
+ }
3193
+ smartbuffer.SmartBuffer = SmartBuffer;
3194
+
3195
+ return smartbuffer;
3196
+ }
3197
+
3198
+ var constants$2 = {};
3199
+
3200
+ var hasRequiredConstants$2;
3201
+
3202
+ function requireConstants$2 () {
3203
+ if (hasRequiredConstants$2) return constants$2;
3204
+ hasRequiredConstants$2 = 1;
3205
+ Object.defineProperty(constants$2, "__esModule", { value: true });
3206
+ constants$2.SOCKS5_NO_ACCEPTABLE_AUTH = constants$2.SOCKS5_CUSTOM_AUTH_END = constants$2.SOCKS5_CUSTOM_AUTH_START = constants$2.SOCKS_INCOMING_PACKET_SIZES = constants$2.SocksClientState = constants$2.Socks5Response = constants$2.Socks5HostType = constants$2.Socks5Auth = constants$2.Socks4Response = constants$2.SocksCommand = constants$2.ERRORS = constants$2.DEFAULT_TIMEOUT = void 0;
3207
+ const DEFAULT_TIMEOUT = 30000;
3208
+ constants$2.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
3209
+ // prettier-ignore
3210
+ const ERRORS = {
3211
+ InvalidSocksCommand: 'An invalid SOCKS command was provided. Valid options are connect, bind, and associate.',
3212
+ InvalidSocksCommandForOperation: 'An invalid SOCKS command was provided. Only a subset of commands are supported for this operation.',
3213
+ InvalidSocksCommandChain: 'An invalid SOCKS command was provided. Chaining currently only supports the connect command.',
3214
+ InvalidSocksClientOptionsDestination: 'An invalid destination host was provided.',
3215
+ InvalidSocksClientOptionsExistingSocket: 'An invalid existing socket was provided. This should be an instance of stream.Duplex.',
3216
+ InvalidSocksClientOptionsProxy: 'Invalid SOCKS proxy details were provided.',
3217
+ InvalidSocksClientOptionsTimeout: 'An invalid timeout value was provided. Please enter a value above 0 (in ms).',
3218
+ InvalidSocksClientOptionsProxiesLength: 'At least two socks proxies must be provided for chaining.',
3219
+ InvalidSocksClientOptionsCustomAuthRange: 'Custom auth must be a value between 0x80 and 0xFE.',
3220
+ InvalidSocksClientOptionsCustomAuthOptions: 'When a custom_auth_method is provided, custom_auth_request_handler, custom_auth_response_size, and custom_auth_response_handler must also be provided and valid.',
3221
+ NegotiationError: 'Negotiation error',
3222
+ SocketClosed: 'Socket closed',
3223
+ ProxyConnectionTimedOut: 'Proxy connection timed out',
3224
+ InternalError: 'SocksClient internal error (this should not happen)',
3225
+ InvalidSocks4HandshakeResponse: 'Received invalid Socks4 handshake response',
3226
+ Socks4ProxyRejectedConnection: 'Socks4 Proxy rejected connection',
3227
+ InvalidSocks4IncomingConnectionResponse: 'Socks4 invalid incoming connection response',
3228
+ Socks4ProxyRejectedIncomingBoundConnection: 'Socks4 Proxy rejected incoming bound connection',
3229
+ InvalidSocks5InitialHandshakeResponse: 'Received invalid Socks5 initial handshake response',
3230
+ InvalidSocks5IntiailHandshakeSocksVersion: 'Received invalid Socks5 initial handshake (invalid socks version)',
3231
+ InvalidSocks5InitialHandshakeNoAcceptedAuthType: 'Received invalid Socks5 initial handshake (no accepted authentication type)',
3232
+ InvalidSocks5InitialHandshakeUnknownAuthType: 'Received invalid Socks5 initial handshake (unknown authentication type)',
3233
+ Socks5AuthenticationFailed: 'Socks5 Authentication failed',
3234
+ InvalidSocks5FinalHandshake: 'Received invalid Socks5 final handshake response',
3235
+ InvalidSocks5FinalHandshakeRejected: 'Socks5 proxy rejected connection',
3236
+ InvalidSocks5IncomingConnectionResponse: 'Received invalid Socks5 incoming connection response',
3237
+ Socks5ProxyRejectedIncomingBoundConnection: 'Socks5 Proxy rejected incoming bound connection',
3238
+ };
3239
+ constants$2.ERRORS = ERRORS;
3240
+ const SOCKS_INCOMING_PACKET_SIZES = {
3241
+ Socks5InitialHandshakeResponse: 2,
3242
+ Socks5UserPassAuthenticationResponse: 2,
3243
+ // Command response + incoming connection (bind)
3244
+ Socks5ResponseHeader: 5, // We need at least 5 to read the hostname length, then we wait for the address+port information.
3245
+ Socks5ResponseIPv4: 10, // 4 header + 4 ip + 2 port
3246
+ Socks5ResponseIPv6: 22, // 4 header + 16 ip + 2 port
3247
+ Socks5ResponseHostname: (hostNameLength) => hostNameLength + 7, // 4 header + 1 host length + host + 2 port
3248
+ // Command response + incoming connection (bind)
3249
+ Socks4Response: 8, // 2 header + 2 port + 4 ip
3250
+ };
3251
+ constants$2.SOCKS_INCOMING_PACKET_SIZES = SOCKS_INCOMING_PACKET_SIZES;
3252
+ var SocksCommand;
3253
+ (function (SocksCommand) {
3254
+ SocksCommand[SocksCommand["connect"] = 1] = "connect";
3255
+ SocksCommand[SocksCommand["bind"] = 2] = "bind";
3256
+ SocksCommand[SocksCommand["associate"] = 3] = "associate";
3257
+ })(SocksCommand || (constants$2.SocksCommand = SocksCommand = {}));
3258
+ var Socks4Response;
3259
+ (function (Socks4Response) {
3260
+ Socks4Response[Socks4Response["Granted"] = 90] = "Granted";
3261
+ Socks4Response[Socks4Response["Failed"] = 91] = "Failed";
3262
+ Socks4Response[Socks4Response["Rejected"] = 92] = "Rejected";
3263
+ Socks4Response[Socks4Response["RejectedIdent"] = 93] = "RejectedIdent";
3264
+ })(Socks4Response || (constants$2.Socks4Response = Socks4Response = {}));
3265
+ var Socks5Auth;
3266
+ (function (Socks5Auth) {
3267
+ Socks5Auth[Socks5Auth["NoAuth"] = 0] = "NoAuth";
3268
+ Socks5Auth[Socks5Auth["GSSApi"] = 1] = "GSSApi";
3269
+ Socks5Auth[Socks5Auth["UserPass"] = 2] = "UserPass";
3270
+ })(Socks5Auth || (constants$2.Socks5Auth = Socks5Auth = {}));
3271
+ const SOCKS5_CUSTOM_AUTH_START = 0x80;
3272
+ constants$2.SOCKS5_CUSTOM_AUTH_START = SOCKS5_CUSTOM_AUTH_START;
3273
+ const SOCKS5_CUSTOM_AUTH_END = 0xfe;
3274
+ constants$2.SOCKS5_CUSTOM_AUTH_END = SOCKS5_CUSTOM_AUTH_END;
3275
+ const SOCKS5_NO_ACCEPTABLE_AUTH = 0xff;
3276
+ constants$2.SOCKS5_NO_ACCEPTABLE_AUTH = SOCKS5_NO_ACCEPTABLE_AUTH;
3277
+ var Socks5Response;
3278
+ (function (Socks5Response) {
3279
+ Socks5Response[Socks5Response["Granted"] = 0] = "Granted";
3280
+ Socks5Response[Socks5Response["Failure"] = 1] = "Failure";
3281
+ Socks5Response[Socks5Response["NotAllowed"] = 2] = "NotAllowed";
3282
+ Socks5Response[Socks5Response["NetworkUnreachable"] = 3] = "NetworkUnreachable";
3283
+ Socks5Response[Socks5Response["HostUnreachable"] = 4] = "HostUnreachable";
3284
+ Socks5Response[Socks5Response["ConnectionRefused"] = 5] = "ConnectionRefused";
3285
+ Socks5Response[Socks5Response["TTLExpired"] = 6] = "TTLExpired";
3286
+ Socks5Response[Socks5Response["CommandNotSupported"] = 7] = "CommandNotSupported";
3287
+ Socks5Response[Socks5Response["AddressNotSupported"] = 8] = "AddressNotSupported";
3288
+ })(Socks5Response || (constants$2.Socks5Response = Socks5Response = {}));
3289
+ var Socks5HostType;
3290
+ (function (Socks5HostType) {
3291
+ Socks5HostType[Socks5HostType["IPv4"] = 1] = "IPv4";
3292
+ Socks5HostType[Socks5HostType["Hostname"] = 3] = "Hostname";
3293
+ Socks5HostType[Socks5HostType["IPv6"] = 4] = "IPv6";
3294
+ })(Socks5HostType || (constants$2.Socks5HostType = Socks5HostType = {}));
3295
+ var SocksClientState;
3296
+ (function (SocksClientState) {
3297
+ SocksClientState[SocksClientState["Created"] = 0] = "Created";
3298
+ SocksClientState[SocksClientState["Connecting"] = 1] = "Connecting";
3299
+ SocksClientState[SocksClientState["Connected"] = 2] = "Connected";
3300
+ SocksClientState[SocksClientState["SentInitialHandshake"] = 3] = "SentInitialHandshake";
3301
+ SocksClientState[SocksClientState["ReceivedInitialHandshakeResponse"] = 4] = "ReceivedInitialHandshakeResponse";
3302
+ SocksClientState[SocksClientState["SentAuthentication"] = 5] = "SentAuthentication";
3303
+ SocksClientState[SocksClientState["ReceivedAuthenticationResponse"] = 6] = "ReceivedAuthenticationResponse";
3304
+ SocksClientState[SocksClientState["SentFinalHandshake"] = 7] = "SentFinalHandshake";
3305
+ SocksClientState[SocksClientState["ReceivedFinalResponse"] = 8] = "ReceivedFinalResponse";
3306
+ SocksClientState[SocksClientState["BoundWaitingForConnection"] = 9] = "BoundWaitingForConnection";
3307
+ SocksClientState[SocksClientState["Established"] = 10] = "Established";
3308
+ SocksClientState[SocksClientState["Disconnected"] = 11] = "Disconnected";
3309
+ SocksClientState[SocksClientState["Error"] = 99] = "Error";
3310
+ })(SocksClientState || (constants$2.SocksClientState = SocksClientState = {}));
3311
+
3312
+ return constants$2;
3313
+ }
3314
+
3315
+ var helpers$1 = {};
3316
+
3317
+ var util = {};
3318
+
3319
+ var hasRequiredUtil;
3320
+
3321
+ function requireUtil () {
3322
+ if (hasRequiredUtil) return util;
3323
+ hasRequiredUtil = 1;
3324
+ Object.defineProperty(util, "__esModule", { value: true });
3325
+ util.shuffleArray = util.SocksClientError = void 0;
3326
+ /**
3327
+ * Error wrapper for SocksClient
3328
+ */
3329
+ class SocksClientError extends Error {
3330
+ constructor(message, options) {
3331
+ super(message);
3332
+ this.options = options;
3333
+ }
3334
+ }
3335
+ util.SocksClientError = SocksClientError;
3336
+ /**
3337
+ * Shuffles a given array.
3338
+ * @param array The array to shuffle.
3339
+ */
3340
+ function shuffleArray(array) {
3341
+ for (let i = array.length - 1; i > 0; i--) {
3342
+ const j = Math.floor(Math.random() * (i + 1));
3343
+ [array[i], array[j]] = [array[j], array[i]];
3344
+ }
3345
+ }
3346
+ util.shuffleArray = shuffleArray;
3347
+
3348
+ return util;
3349
+ }
3350
+
3351
+ var ipAddress = {};
3352
+
3353
+ var ipv4 = {};
3354
+
3355
+ var common = {};
3356
+
3357
+ var hasRequiredCommon;
3358
+
3359
+ function requireCommon () {
3360
+ if (hasRequiredCommon) return common;
3361
+ hasRequiredCommon = 1;
3362
+ Object.defineProperty(common, "__esModule", { value: true });
3363
+ common.isInSubnet = isInSubnet;
3364
+ common.isCorrect = isCorrect;
3365
+ common.numberToPaddedHex = numberToPaddedHex;
3366
+ common.stringToPaddedHex = stringToPaddedHex;
3367
+ common.testBit = testBit;
3368
+ function isInSubnet(address) {
3369
+ if (this.subnetMask < address.subnetMask) {
3370
+ return false;
3371
+ }
3372
+ if (this.mask(address.subnetMask) === address.mask()) {
3373
+ return true;
3374
+ }
3375
+ return false;
3376
+ }
3377
+ function isCorrect(defaultBits) {
3378
+ return function () {
3379
+ if (this.addressMinusSuffix !== this.correctForm()) {
3380
+ return false;
3381
+ }
3382
+ if (this.subnetMask === defaultBits && !this.parsedSubnet) {
3383
+ return true;
3384
+ }
3385
+ return this.parsedSubnet === String(this.subnetMask);
3386
+ };
3387
+ }
3388
+ function numberToPaddedHex(number) {
3389
+ return number.toString(16).padStart(2, '0');
3390
+ }
3391
+ function stringToPaddedHex(numberString) {
3392
+ return numberToPaddedHex(parseInt(numberString, 10));
3393
+ }
3394
+ /**
3395
+ * @param binaryValue Binary representation of a value (e.g. `10`)
3396
+ * @param position Byte position, where 0 is the least significant bit
3397
+ */
3398
+ function testBit(binaryValue, position) {
3399
+ const { length } = binaryValue;
3400
+ if (position > length) {
3401
+ return false;
3402
+ }
3403
+ const positionInString = length - position;
3404
+ return binaryValue.substring(positionInString, positionInString + 1) === '1';
3405
+ }
3406
+
3407
+ return common;
3408
+ }
3409
+
3410
+ var constants$1 = {};
3411
+
3412
+ var hasRequiredConstants$1;
3413
+
3414
+ function requireConstants$1 () {
3415
+ if (hasRequiredConstants$1) return constants$1;
3416
+ hasRequiredConstants$1 = 1;
3417
+ Object.defineProperty(constants$1, "__esModule", { value: true });
3418
+ constants$1.RE_SUBNET_STRING = constants$1.RE_ADDRESS = constants$1.GROUPS = constants$1.BITS = void 0;
3419
+ constants$1.BITS = 32;
3420
+ constants$1.GROUPS = 4;
3421
+ constants$1.RE_ADDRESS = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g;
3422
+ constants$1.RE_SUBNET_STRING = /\/\d{1,2}$/;
3423
+
3424
+ return constants$1;
3425
+ }
3426
+
3427
+ var addressError = {};
3428
+
3429
+ var hasRequiredAddressError;
3430
+
3431
+ function requireAddressError () {
3432
+ if (hasRequiredAddressError) return addressError;
3433
+ hasRequiredAddressError = 1;
3434
+ Object.defineProperty(addressError, "__esModule", { value: true });
3435
+ addressError.AddressError = void 0;
3436
+ class AddressError extends Error {
3437
+ constructor(message, parseMessage) {
3438
+ super(message);
3439
+ this.name = 'AddressError';
3440
+ this.parseMessage = parseMessage;
3441
+ }
3442
+ }
3443
+ addressError.AddressError = AddressError;
3444
+
3445
+ return addressError;
3446
+ }
3447
+
3448
+ var hasRequiredIpv4;
3449
+
3450
+ function requireIpv4 () {
3451
+ if (hasRequiredIpv4) return ipv4;
3452
+ hasRequiredIpv4 = 1;
3453
+ /* eslint-disable no-param-reassign */
3454
+ var __createBinding = (ipv4 && ipv4.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3455
+ if (k2 === undefined) k2 = k;
3456
+ var desc = Object.getOwnPropertyDescriptor(m, k);
3457
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
3458
+ desc = { enumerable: true, get: function() { return m[k]; } };
3459
+ }
3460
+ Object.defineProperty(o, k2, desc);
3461
+ }) : (function(o, m, k, k2) {
3462
+ if (k2 === undefined) k2 = k;
3463
+ o[k2] = m[k];
3464
+ }));
3465
+ var __setModuleDefault = (ipv4 && ipv4.__setModuleDefault) || (Object.create ? (function(o, v) {
3466
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
3467
+ }) : function(o, v) {
3468
+ o["default"] = v;
3469
+ });
3470
+ var __importStar = (ipv4 && ipv4.__importStar) || function (mod) {
3471
+ if (mod && mod.__esModule) return mod;
3472
+ var result = {};
3473
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
3474
+ __setModuleDefault(result, mod);
3475
+ return result;
3476
+ };
3477
+ Object.defineProperty(ipv4, "__esModule", { value: true });
3478
+ ipv4.Address4 = void 0;
3479
+ const common = __importStar(requireCommon());
3480
+ const constants = __importStar(requireConstants$1());
3481
+ const address_error_1 = requireAddressError();
3482
+ /**
3483
+ * Represents an IPv4 address
3484
+ * @class Address4
3485
+ * @param {string} address - An IPv4 address string
3486
+ */
3487
+ class Address4 {
3488
+ constructor(address) {
3489
+ this.groups = constants.GROUPS;
3490
+ this.parsedAddress = [];
3491
+ this.parsedSubnet = '';
3492
+ this.subnet = '/32';
3493
+ this.subnetMask = 32;
3494
+ this.v4 = true;
3495
+ /**
3496
+ * Returns true if the address is correct, false otherwise
3497
+ * @memberof Address4
3498
+ * @instance
3499
+ * @returns {Boolean}
3500
+ */
3501
+ this.isCorrect = common.isCorrect(constants.BITS);
3502
+ /**
3503
+ * Returns true if the given address is in the subnet of the current address
3504
+ * @memberof Address4
3505
+ * @instance
3506
+ * @returns {boolean}
3507
+ */
3508
+ this.isInSubnet = common.isInSubnet;
3509
+ this.address = address;
3510
+ const subnet = constants.RE_SUBNET_STRING.exec(address);
3511
+ if (subnet) {
3512
+ this.parsedSubnet = subnet[0].replace('/', '');
3513
+ this.subnetMask = parseInt(this.parsedSubnet, 10);
3514
+ this.subnet = `/${this.subnetMask}`;
3515
+ if (this.subnetMask < 0 || this.subnetMask > constants.BITS) {
3516
+ throw new address_error_1.AddressError('Invalid subnet mask.');
3517
+ }
3518
+ address = address.replace(constants.RE_SUBNET_STRING, '');
3519
+ }
3520
+ this.addressMinusSuffix = address;
3521
+ this.parsedAddress = this.parse(address);
3522
+ }
3523
+ static isValid(address) {
3524
+ try {
3525
+ // eslint-disable-next-line no-new
3526
+ new Address4(address);
3527
+ return true;
3528
+ }
3529
+ catch (e) {
3530
+ return false;
3531
+ }
3532
+ }
3533
+ /*
3534
+ * Parses a v4 address
3535
+ */
3536
+ parse(address) {
3537
+ const groups = address.split('.');
3538
+ if (!address.match(constants.RE_ADDRESS)) {
3539
+ throw new address_error_1.AddressError('Invalid IPv4 address.');
3540
+ }
3541
+ return groups;
3542
+ }
3543
+ /**
3544
+ * Returns the correct form of an address
3545
+ * @memberof Address4
3546
+ * @instance
3547
+ * @returns {String}
3548
+ */
3549
+ correctForm() {
3550
+ return this.parsedAddress.map((part) => parseInt(part, 10)).join('.');
3551
+ }
3552
+ /**
3553
+ * Converts a hex string to an IPv4 address object
3554
+ * @memberof Address4
3555
+ * @static
3556
+ * @param {string} hex - a hex string to convert
3557
+ * @returns {Address4}
3558
+ */
3559
+ static fromHex(hex) {
3560
+ const padded = hex.replace(/:/g, '').padStart(8, '0');
3561
+ const groups = [];
3562
+ let i;
3563
+ for (i = 0; i < 8; i += 2) {
3564
+ const h = padded.slice(i, i + 2);
3565
+ groups.push(parseInt(h, 16));
3566
+ }
3567
+ return new Address4(groups.join('.'));
3568
+ }
3569
+ /**
3570
+ * Converts an integer into a IPv4 address object
3571
+ * @memberof Address4
3572
+ * @static
3573
+ * @param {integer} integer - a number to convert
3574
+ * @returns {Address4}
3575
+ */
3576
+ static fromInteger(integer) {
3577
+ return Address4.fromHex(integer.toString(16));
3578
+ }
3579
+ /**
3580
+ * Return an address from in-addr.arpa form
3581
+ * @memberof Address4
3582
+ * @static
3583
+ * @param {string} arpaFormAddress - an 'in-addr.arpa' form ipv4 address
3584
+ * @returns {Adress4}
3585
+ * @example
3586
+ * var address = Address4.fromArpa(42.2.0.192.in-addr.arpa.)
3587
+ * address.correctForm(); // '192.0.2.42'
3588
+ */
3589
+ static fromArpa(arpaFormAddress) {
3590
+ // remove ending ".in-addr.arpa." or just "."
3591
+ const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.$/, '');
3592
+ const address = leader.split('.').reverse().join('.');
3593
+ return new Address4(address);
3594
+ }
3595
+ /**
3596
+ * Converts an IPv4 address object to a hex string
3597
+ * @memberof Address4
3598
+ * @instance
3599
+ * @returns {String}
3600
+ */
3601
+ toHex() {
3602
+ return this.parsedAddress.map((part) => common.stringToPaddedHex(part)).join(':');
3603
+ }
3604
+ /**
3605
+ * Converts an IPv4 address object to an array of bytes
3606
+ * @memberof Address4
3607
+ * @instance
3608
+ * @returns {Array}
3609
+ */
3610
+ toArray() {
3611
+ return this.parsedAddress.map((part) => parseInt(part, 10));
3612
+ }
3613
+ /**
3614
+ * Converts an IPv4 address object to an IPv6 address group
3615
+ * @memberof Address4
3616
+ * @instance
3617
+ * @returns {String}
3618
+ */
3619
+ toGroup6() {
3620
+ const output = [];
3621
+ let i;
3622
+ for (i = 0; i < constants.GROUPS; i += 2) {
3623
+ output.push(`${common.stringToPaddedHex(this.parsedAddress[i])}${common.stringToPaddedHex(this.parsedAddress[i + 1])}`);
3624
+ }
3625
+ return output.join(':');
3626
+ }
3627
+ /**
3628
+ * Returns the address as a `bigint`
3629
+ * @memberof Address4
3630
+ * @instance
3631
+ * @returns {bigint}
3632
+ */
3633
+ bigInt() {
3634
+ return BigInt(`0x${this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join('')}`);
3635
+ }
3636
+ /**
3637
+ * Helper function getting start address.
3638
+ * @memberof Address4
3639
+ * @instance
3640
+ * @returns {bigint}
3641
+ */
3642
+ _startAddress() {
3643
+ return BigInt(`0b${this.mask() + '0'.repeat(constants.BITS - this.subnetMask)}`);
3644
+ }
3645
+ /**
3646
+ * The first address in the range given by this address' subnet.
3647
+ * Often referred to as the Network Address.
3648
+ * @memberof Address4
3649
+ * @instance
3650
+ * @returns {Address4}
3651
+ */
3652
+ startAddress() {
3653
+ return Address4.fromBigInt(this._startAddress());
3654
+ }
3655
+ /**
3656
+ * The first host address in the range given by this address's subnet ie
3657
+ * the first address after the Network Address
3658
+ * @memberof Address4
3659
+ * @instance
3660
+ * @returns {Address4}
3661
+ */
3662
+ startAddressExclusive() {
3663
+ const adjust = BigInt('1');
3664
+ return Address4.fromBigInt(this._startAddress() + adjust);
3665
+ }
3666
+ /**
3667
+ * Helper function getting end address.
3668
+ * @memberof Address4
3669
+ * @instance
3670
+ * @returns {bigint}
3671
+ */
3672
+ _endAddress() {
3673
+ return BigInt(`0b${this.mask() + '1'.repeat(constants.BITS - this.subnetMask)}`);
3674
+ }
3675
+ /**
3676
+ * The last address in the range given by this address' subnet
3677
+ * Often referred to as the Broadcast
3678
+ * @memberof Address4
3679
+ * @instance
3680
+ * @returns {Address4}
3681
+ */
3682
+ endAddress() {
3683
+ return Address4.fromBigInt(this._endAddress());
3684
+ }
3685
+ /**
3686
+ * The last host address in the range given by this address's subnet ie
3687
+ * the last address prior to the Broadcast Address
3688
+ * @memberof Address4
3689
+ * @instance
3690
+ * @returns {Address4}
3691
+ */
3692
+ endAddressExclusive() {
3693
+ const adjust = BigInt('1');
3694
+ return Address4.fromBigInt(this._endAddress() - adjust);
3695
+ }
3696
+ /**
3697
+ * Converts a BigInt to a v4 address object
3698
+ * @memberof Address4
3699
+ * @static
3700
+ * @param {bigint} bigInt - a BigInt to convert
3701
+ * @returns {Address4}
3702
+ */
3703
+ static fromBigInt(bigInt) {
3704
+ return Address4.fromHex(bigInt.toString(16));
3705
+ }
3706
+ /**
3707
+ * Convert a byte array to an Address4 object
3708
+ * @memberof Address4
3709
+ * @static
3710
+ * @param {Array<number>} bytes - an array of 4 bytes (0-255)
3711
+ * @returns {Address4}
3712
+ */
3713
+ static fromByteArray(bytes) {
3714
+ if (bytes.length !== 4) {
3715
+ throw new address_error_1.AddressError('IPv4 addresses require exactly 4 bytes');
3716
+ }
3717
+ // Validate that all bytes are within valid range (0-255)
3718
+ for (let i = 0; i < bytes.length; i++) {
3719
+ if (!Number.isInteger(bytes[i]) || bytes[i] < 0 || bytes[i] > 255) {
3720
+ throw new address_error_1.AddressError('All bytes must be integers between 0 and 255');
3721
+ }
3722
+ }
3723
+ return this.fromUnsignedByteArray(bytes);
3724
+ }
3725
+ /**
3726
+ * Convert an unsigned byte array to an Address4 object
3727
+ * @memberof Address4
3728
+ * @static
3729
+ * @param {Array<number>} bytes - an array of 4 unsigned bytes (0-255)
3730
+ * @returns {Address4}
3731
+ */
3732
+ static fromUnsignedByteArray(bytes) {
3733
+ if (bytes.length !== 4) {
3734
+ throw new address_error_1.AddressError('IPv4 addresses require exactly 4 bytes');
3735
+ }
3736
+ const address = bytes.join('.');
3737
+ return new Address4(address);
3738
+ }
3739
+ /**
3740
+ * Returns the first n bits of the address, defaulting to the
3741
+ * subnet mask
3742
+ * @memberof Address4
3743
+ * @instance
3744
+ * @returns {String}
3745
+ */
3746
+ mask(mask) {
3747
+ if (mask === undefined) {
3748
+ mask = this.subnetMask;
3749
+ }
3750
+ return this.getBitsBase2(0, mask);
3751
+ }
3752
+ /**
3753
+ * Returns the bits in the given range as a base-2 string
3754
+ * @memberof Address4
3755
+ * @instance
3756
+ * @returns {string}
3757
+ */
3758
+ getBitsBase2(start, end) {
3759
+ return this.binaryZeroPad().slice(start, end);
3760
+ }
3761
+ /**
3762
+ * Return the reversed ip6.arpa form of the address
3763
+ * @memberof Address4
3764
+ * @param {Object} options
3765
+ * @param {boolean} options.omitSuffix - omit the "in-addr.arpa" suffix
3766
+ * @instance
3767
+ * @returns {String}
3768
+ */
3769
+ reverseForm(options) {
3770
+ if (!options) {
3771
+ options = {};
3772
+ }
3773
+ const reversed = this.correctForm().split('.').reverse().join('.');
3774
+ if (options.omitSuffix) {
3775
+ return reversed;
3776
+ }
3777
+ return `${reversed}.in-addr.arpa.`;
3778
+ }
3779
+ /**
3780
+ * Returns true if the given address is a multicast address
3781
+ * @memberof Address4
3782
+ * @instance
3783
+ * @returns {boolean}
3784
+ */
3785
+ isMulticast() {
3786
+ return this.isInSubnet(new Address4('224.0.0.0/4'));
3787
+ }
3788
+ /**
3789
+ * Returns a zero-padded base-2 string representation of the address
3790
+ * @memberof Address4
3791
+ * @instance
3792
+ * @returns {string}
3793
+ */
3794
+ binaryZeroPad() {
3795
+ return this.bigInt().toString(2).padStart(constants.BITS, '0');
3796
+ }
3797
+ /**
3798
+ * Groups an IPv4 address for inclusion at the end of an IPv6 address
3799
+ * @returns {String}
3800
+ */
3801
+ groupForV6() {
3802
+ const segments = this.parsedAddress;
3803
+ return this.address.replace(constants.RE_ADDRESS, `<span class="hover-group group-v4 group-6">${segments
3804
+ .slice(0, 2)
3805
+ .join('.')}</span>.<span class="hover-group group-v4 group-7">${segments
3806
+ .slice(2, 4)
3807
+ .join('.')}</span>`);
3808
+ }
3809
+ }
3810
+ ipv4.Address4 = Address4;
3811
+
3812
+ return ipv4;
3813
+ }
3814
+
3815
+ var ipv6 = {};
3816
+
3817
+ var constants = {};
3818
+
3819
+ var hasRequiredConstants;
3820
+
3821
+ function requireConstants () {
3822
+ if (hasRequiredConstants) return constants;
3823
+ hasRequiredConstants = 1;
3824
+ Object.defineProperty(constants, "__esModule", { value: true });
3825
+ constants.RE_URL_WITH_PORT = constants.RE_URL = constants.RE_ZONE_STRING = constants.RE_SUBNET_STRING = constants.RE_BAD_ADDRESS = constants.RE_BAD_CHARACTERS = constants.TYPES = constants.SCOPES = constants.GROUPS = constants.BITS = void 0;
3826
+ constants.BITS = 128;
3827
+ constants.GROUPS = 8;
3828
+ /**
3829
+ * Represents IPv6 address scopes
3830
+ * @memberof Address6
3831
+ * @static
3832
+ */
3833
+ constants.SCOPES = {
3834
+ 0: 'Reserved',
3835
+ 1: 'Interface local',
3836
+ 2: 'Link local',
3837
+ 4: 'Admin local',
3838
+ 5: 'Site local',
3839
+ 8: 'Organization local',
3840
+ 14: 'Global',
3841
+ 15: 'Reserved',
3842
+ };
3843
+ /**
3844
+ * Represents IPv6 address types
3845
+ * @memberof Address6
3846
+ * @static
3847
+ */
3848
+ constants.TYPES = {
3849
+ 'ff01::1/128': 'Multicast (All nodes on this interface)',
3850
+ 'ff01::2/128': 'Multicast (All routers on this interface)',
3851
+ 'ff02::1/128': 'Multicast (All nodes on this link)',
3852
+ 'ff02::2/128': 'Multicast (All routers on this link)',
3853
+ 'ff05::2/128': 'Multicast (All routers in this site)',
3854
+ 'ff02::5/128': 'Multicast (OSPFv3 AllSPF routers)',
3855
+ 'ff02::6/128': 'Multicast (OSPFv3 AllDR routers)',
3856
+ 'ff02::9/128': 'Multicast (RIP routers)',
3857
+ 'ff02::a/128': 'Multicast (EIGRP routers)',
3858
+ 'ff02::d/128': 'Multicast (PIM routers)',
3859
+ 'ff02::16/128': 'Multicast (MLDv2 reports)',
3860
+ 'ff01::fb/128': 'Multicast (mDNSv6)',
3861
+ 'ff02::fb/128': 'Multicast (mDNSv6)',
3862
+ 'ff05::fb/128': 'Multicast (mDNSv6)',
3863
+ 'ff02::1:2/128': 'Multicast (All DHCP servers and relay agents on this link)',
3864
+ 'ff05::1:2/128': 'Multicast (All DHCP servers and relay agents in this site)',
3865
+ 'ff02::1:3/128': 'Multicast (All DHCP servers on this link)',
3866
+ 'ff05::1:3/128': 'Multicast (All DHCP servers in this site)',
3867
+ '::/128': 'Unspecified',
3868
+ '::1/128': 'Loopback',
3869
+ 'ff00::/8': 'Multicast',
3870
+ 'fe80::/10': 'Link-local unicast',
3871
+ };
3872
+ /**
3873
+ * A regular expression that matches bad characters in an IPv6 address
3874
+ * @memberof Address6
3875
+ * @static
3876
+ */
3877
+ constants.RE_BAD_CHARACTERS = /([^0-9a-f:/%])/gi;
3878
+ /**
3879
+ * A regular expression that matches an incorrect IPv6 address
3880
+ * @memberof Address6
3881
+ * @static
3882
+ */
3883
+ constants.RE_BAD_ADDRESS = /([0-9a-f]{5,}|:{3,}|[^:]:$|^:[^:]|\/$)/gi;
3884
+ /**
3885
+ * A regular expression that matches an IPv6 subnet
3886
+ * @memberof Address6
3887
+ * @static
3888
+ */
3889
+ constants.RE_SUBNET_STRING = /\/\d{1,3}(?=%|$)/;
3890
+ /**
3891
+ * A regular expression that matches an IPv6 zone
3892
+ * @memberof Address6
3893
+ * @static
3894
+ */
3895
+ constants.RE_ZONE_STRING = /%.*$/;
3896
+ constants.RE_URL = /^\[{0,1}([0-9a-f:]+)\]{0,1}/;
3897
+ constants.RE_URL_WITH_PORT = /\[([0-9a-f:]+)\]:([0-9]{1,5})/;
3898
+
3899
+ return constants;
3900
+ }
3901
+
3902
+ var helpers = {};
3903
+
3904
+ var hasRequiredHelpers$1;
3905
+
3906
+ function requireHelpers$1 () {
3907
+ if (hasRequiredHelpers$1) return helpers;
3908
+ hasRequiredHelpers$1 = 1;
3909
+ Object.defineProperty(helpers, "__esModule", { value: true });
3910
+ helpers.spanAllZeroes = spanAllZeroes;
3911
+ helpers.spanAll = spanAll;
3912
+ helpers.spanLeadingZeroes = spanLeadingZeroes;
3913
+ helpers.simpleGroup = simpleGroup;
3914
+ /**
3915
+ * @returns {String} the string with all zeroes contained in a <span>
3916
+ */
3917
+ function spanAllZeroes(s) {
3918
+ return s.replace(/(0+)/g, '<span class="zero">$1</span>');
3919
+ }
3920
+ /**
3921
+ * @returns {String} the string with each character contained in a <span>
3922
+ */
3923
+ function spanAll(s, offset = 0) {
3924
+ const letters = s.split('');
3925
+ return letters
3926
+ .map((n, i) => `<span class="digit value-${n} position-${i + offset}">${spanAllZeroes(n)}</span>`)
3927
+ .join('');
3928
+ }
3929
+ function spanLeadingZeroesSimple(group) {
3930
+ return group.replace(/^(0+)/, '<span class="zero">$1</span>');
3931
+ }
3932
+ /**
3933
+ * @returns {String} the string with leading zeroes contained in a <span>
3934
+ */
3935
+ function spanLeadingZeroes(address) {
3936
+ const groups = address.split(':');
3937
+ return groups.map((g) => spanLeadingZeroesSimple(g)).join(':');
3938
+ }
3939
+ /**
3940
+ * Groups an address
3941
+ * @returns {String} a grouped address
3942
+ */
3943
+ function simpleGroup(addressString, offset = 0) {
3944
+ const groups = addressString.split(':');
3945
+ return groups.map((g, i) => {
3946
+ if (/group-v4/.test(g)) {
3947
+ return g;
3948
+ }
3949
+ return `<span class="hover-group group-${i + offset}">${spanLeadingZeroesSimple(g)}</span>`;
3950
+ });
3951
+ }
3952
+
3953
+ return helpers;
3954
+ }
3955
+
3956
+ var regularExpressions = {};
3957
+
3958
+ var hasRequiredRegularExpressions;
3959
+
3960
+ function requireRegularExpressions () {
3961
+ if (hasRequiredRegularExpressions) return regularExpressions;
3962
+ hasRequiredRegularExpressions = 1;
3963
+ var __createBinding = (regularExpressions && regularExpressions.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3964
+ if (k2 === undefined) k2 = k;
3965
+ var desc = Object.getOwnPropertyDescriptor(m, k);
3966
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
3967
+ desc = { enumerable: true, get: function() { return m[k]; } };
3968
+ }
3969
+ Object.defineProperty(o, k2, desc);
3970
+ }) : (function(o, m, k, k2) {
3971
+ if (k2 === undefined) k2 = k;
3972
+ o[k2] = m[k];
3973
+ }));
3974
+ var __setModuleDefault = (regularExpressions && regularExpressions.__setModuleDefault) || (Object.create ? (function(o, v) {
3975
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
3976
+ }) : function(o, v) {
3977
+ o["default"] = v;
3978
+ });
3979
+ var __importStar = (regularExpressions && regularExpressions.__importStar) || function (mod) {
3980
+ if (mod && mod.__esModule) return mod;
3981
+ var result = {};
3982
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
3983
+ __setModuleDefault(result, mod);
3984
+ return result;
3985
+ };
3986
+ Object.defineProperty(regularExpressions, "__esModule", { value: true });
3987
+ regularExpressions.ADDRESS_BOUNDARY = void 0;
3988
+ regularExpressions.groupPossibilities = groupPossibilities;
3989
+ regularExpressions.padGroup = padGroup;
3990
+ regularExpressions.simpleRegularExpression = simpleRegularExpression;
3991
+ regularExpressions.possibleElisions = possibleElisions;
3992
+ const v6 = __importStar(requireConstants());
3993
+ function groupPossibilities(possibilities) {
3994
+ return `(${possibilities.join('|')})`;
3995
+ }
3996
+ function padGroup(group) {
3997
+ if (group.length < 4) {
3998
+ return `0{0,${4 - group.length}}${group}`;
3999
+ }
4000
+ return group;
4001
+ }
4002
+ regularExpressions.ADDRESS_BOUNDARY = '[^A-Fa-f0-9:]';
4003
+ function simpleRegularExpression(groups) {
4004
+ const zeroIndexes = [];
4005
+ groups.forEach((group, i) => {
4006
+ const groupInteger = parseInt(group, 16);
4007
+ if (groupInteger === 0) {
4008
+ zeroIndexes.push(i);
4009
+ }
4010
+ });
4011
+ // You can technically elide a single 0, this creates the regular expressions
4012
+ // to match that eventuality
4013
+ const possibilities = zeroIndexes.map((zeroIndex) => groups
4014
+ .map((group, i) => {
4015
+ if (i === zeroIndex) {
4016
+ const elision = i === 0 || i === v6.GROUPS - 1 ? ':' : '';
4017
+ return groupPossibilities([padGroup(group), elision]);
4018
+ }
4019
+ return padGroup(group);
4020
+ })
4021
+ .join(':'));
4022
+ // The simplest case
4023
+ possibilities.push(groups.map(padGroup).join(':'));
4024
+ return groupPossibilities(possibilities);
4025
+ }
4026
+ function possibleElisions(elidedGroups, moreLeft, moreRight) {
4027
+ const left = moreLeft ? '' : ':';
4028
+ const right = moreRight ? '' : ':';
4029
+ const possibilities = [];
4030
+ // 1. elision of everything (::)
4031
+ if (!moreLeft && !moreRight) {
4032
+ possibilities.push('::');
4033
+ }
4034
+ // 2. complete elision of the middle
4035
+ if (moreLeft && moreRight) {
4036
+ possibilities.push('');
4037
+ }
4038
+ if ((moreRight && !moreLeft) || (!moreRight && moreLeft)) {
4039
+ // 3. complete elision of one side
4040
+ possibilities.push(':');
4041
+ }
4042
+ // 4. elision from the left side
4043
+ possibilities.push(`${left}(:0{1,4}){1,${elidedGroups - 1}}`);
4044
+ // 5. elision from the right side
4045
+ possibilities.push(`(0{1,4}:){1,${elidedGroups - 1}}${right}`);
4046
+ // 6. no elision
4047
+ possibilities.push(`(0{1,4}:){${elidedGroups - 1}}0{1,4}`);
4048
+ // 7. elision (including sloppy elision) from the middle
4049
+ for (let groups = 1; groups < elidedGroups - 1; groups++) {
4050
+ for (let position = 1; position < elidedGroups - groups; position++) {
4051
+ possibilities.push(`(0{1,4}:){${position}}:(0{1,4}:){${elidedGroups - position - groups - 1}}0{1,4}`);
4052
+ }
4053
+ }
4054
+ return groupPossibilities(possibilities);
4055
+ }
4056
+
4057
+ return regularExpressions;
4058
+ }
4059
+
4060
+ var hasRequiredIpv6;
4061
+
4062
+ function requireIpv6 () {
4063
+ if (hasRequiredIpv6) return ipv6;
4064
+ hasRequiredIpv6 = 1;
4065
+ /* eslint-disable prefer-destructuring */
4066
+ /* eslint-disable no-param-reassign */
4067
+ var __createBinding = (ipv6 && ipv6.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4068
+ if (k2 === undefined) k2 = k;
4069
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4070
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
4071
+ desc = { enumerable: true, get: function() { return m[k]; } };
4072
+ }
4073
+ Object.defineProperty(o, k2, desc);
4074
+ }) : (function(o, m, k, k2) {
4075
+ if (k2 === undefined) k2 = k;
4076
+ o[k2] = m[k];
4077
+ }));
4078
+ var __setModuleDefault = (ipv6 && ipv6.__setModuleDefault) || (Object.create ? (function(o, v) {
4079
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
4080
+ }) : function(o, v) {
4081
+ o["default"] = v;
4082
+ });
4083
+ var __importStar = (ipv6 && ipv6.__importStar) || function (mod) {
4084
+ if (mod && mod.__esModule) return mod;
4085
+ var result = {};
4086
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
4087
+ __setModuleDefault(result, mod);
4088
+ return result;
4089
+ };
4090
+ Object.defineProperty(ipv6, "__esModule", { value: true });
4091
+ ipv6.Address6 = void 0;
4092
+ const common = __importStar(requireCommon());
4093
+ const constants4 = __importStar(requireConstants$1());
4094
+ const constants6 = __importStar(requireConstants());
4095
+ const helpers = __importStar(requireHelpers$1());
4096
+ const ipv4_1 = requireIpv4();
4097
+ const regular_expressions_1 = requireRegularExpressions();
4098
+ const address_error_1 = requireAddressError();
4099
+ const common_1 = requireCommon();
4100
+ function assert(condition) {
4101
+ if (!condition) {
4102
+ throw new Error('Assertion failed.');
4103
+ }
4104
+ }
4105
+ function addCommas(number) {
4106
+ const r = /(\d+)(\d{3})/;
4107
+ while (r.test(number)) {
4108
+ number = number.replace(r, '$1,$2');
4109
+ }
4110
+ return number;
4111
+ }
4112
+ function spanLeadingZeroes4(n) {
4113
+ n = n.replace(/^(0{1,})([1-9]+)$/, '<span class="parse-error">$1</span>$2');
4114
+ n = n.replace(/^(0{1,})(0)$/, '<span class="parse-error">$1</span>$2');
4115
+ return n;
4116
+ }
4117
+ /*
4118
+ * A helper function to compact an array
4119
+ */
4120
+ function compact(address, slice) {
4121
+ const s1 = [];
4122
+ const s2 = [];
4123
+ let i;
4124
+ for (i = 0; i < address.length; i++) {
4125
+ if (i < slice[0]) {
4126
+ s1.push(address[i]);
4127
+ }
4128
+ else if (i > slice[1]) {
4129
+ s2.push(address[i]);
4130
+ }
4131
+ }
4132
+ return s1.concat(['compact']).concat(s2);
4133
+ }
4134
+ function paddedHex(octet) {
4135
+ return parseInt(octet, 16).toString(16).padStart(4, '0');
4136
+ }
4137
+ function unsignByte(b) {
4138
+ // eslint-disable-next-line no-bitwise
4139
+ return b & 0xff;
4140
+ }
4141
+ /**
4142
+ * Represents an IPv6 address
4143
+ * @class Address6
4144
+ * @param {string} address - An IPv6 address string
4145
+ * @param {number} [groups=8] - How many octets to parse
4146
+ * @example
4147
+ * var address = new Address6('2001::/32');
4148
+ */
4149
+ class Address6 {
4150
+ constructor(address, optionalGroups) {
4151
+ this.addressMinusSuffix = '';
4152
+ this.parsedSubnet = '';
4153
+ this.subnet = '/128';
4154
+ this.subnetMask = 128;
4155
+ this.v4 = false;
4156
+ this.zone = '';
4157
+ // #region Attributes
4158
+ /**
4159
+ * Returns true if the given address is in the subnet of the current address
4160
+ * @memberof Address6
4161
+ * @instance
4162
+ * @returns {boolean}
4163
+ */
4164
+ this.isInSubnet = common.isInSubnet;
4165
+ /**
4166
+ * Returns true if the address is correct, false otherwise
4167
+ * @memberof Address6
4168
+ * @instance
4169
+ * @returns {boolean}
4170
+ */
4171
+ this.isCorrect = common.isCorrect(constants6.BITS);
4172
+ if (optionalGroups === undefined) {
4173
+ this.groups = constants6.GROUPS;
4174
+ }
4175
+ else {
4176
+ this.groups = optionalGroups;
4177
+ }
4178
+ this.address = address;
4179
+ const subnet = constants6.RE_SUBNET_STRING.exec(address);
4180
+ if (subnet) {
4181
+ this.parsedSubnet = subnet[0].replace('/', '');
4182
+ this.subnetMask = parseInt(this.parsedSubnet, 10);
4183
+ this.subnet = `/${this.subnetMask}`;
4184
+ if (Number.isNaN(this.subnetMask) ||
4185
+ this.subnetMask < 0 ||
4186
+ this.subnetMask > constants6.BITS) {
4187
+ throw new address_error_1.AddressError('Invalid subnet mask.');
4188
+ }
4189
+ address = address.replace(constants6.RE_SUBNET_STRING, '');
4190
+ }
4191
+ else if (/\//.test(address)) {
4192
+ throw new address_error_1.AddressError('Invalid subnet mask.');
4193
+ }
4194
+ const zone = constants6.RE_ZONE_STRING.exec(address);
4195
+ if (zone) {
4196
+ this.zone = zone[0];
4197
+ address = address.replace(constants6.RE_ZONE_STRING, '');
4198
+ }
4199
+ this.addressMinusSuffix = address;
4200
+ this.parsedAddress = this.parse(this.addressMinusSuffix);
4201
+ }
4202
+ static isValid(address) {
4203
+ try {
4204
+ // eslint-disable-next-line no-new
4205
+ new Address6(address);
4206
+ return true;
4207
+ }
4208
+ catch (e) {
4209
+ return false;
4210
+ }
4211
+ }
4212
+ /**
4213
+ * Convert a BigInt to a v6 address object
4214
+ * @memberof Address6
4215
+ * @static
4216
+ * @param {bigint} bigInt - a BigInt to convert
4217
+ * @returns {Address6}
4218
+ * @example
4219
+ * var bigInt = BigInt('1000000000000');
4220
+ * var address = Address6.fromBigInt(bigInt);
4221
+ * address.correctForm(); // '::e8:d4a5:1000'
4222
+ */
4223
+ static fromBigInt(bigInt) {
4224
+ const hex = bigInt.toString(16).padStart(32, '0');
4225
+ const groups = [];
4226
+ let i;
4227
+ for (i = 0; i < constants6.GROUPS; i++) {
4228
+ groups.push(hex.slice(i * 4, (i + 1) * 4));
4229
+ }
4230
+ return new Address6(groups.join(':'));
4231
+ }
4232
+ /**
4233
+ * Convert a URL (with optional port number) to an address object
4234
+ * @memberof Address6
4235
+ * @static
4236
+ * @param {string} url - a URL with optional port number
4237
+ * @example
4238
+ * var addressAndPort = Address6.fromURL('http://[ffff::]:8080/foo/');
4239
+ * addressAndPort.address.correctForm(); // 'ffff::'
4240
+ * addressAndPort.port; // 8080
4241
+ */
4242
+ static fromURL(url) {
4243
+ let host;
4244
+ let port = null;
4245
+ let result;
4246
+ // If we have brackets parse them and find a port
4247
+ if (url.indexOf('[') !== -1 && url.indexOf(']:') !== -1) {
4248
+ result = constants6.RE_URL_WITH_PORT.exec(url);
4249
+ if (result === null) {
4250
+ return {
4251
+ error: 'failed to parse address with port',
4252
+ address: null,
4253
+ port: null,
4254
+ };
4255
+ }
4256
+ host = result[1];
4257
+ port = result[2];
4258
+ // If there's a URL extract the address
4259
+ }
4260
+ else if (url.indexOf('/') !== -1) {
4261
+ // Remove the protocol prefix
4262
+ url = url.replace(/^[a-z0-9]+:\/\//, '');
4263
+ // Parse the address
4264
+ result = constants6.RE_URL.exec(url);
4265
+ if (result === null) {
4266
+ return {
4267
+ error: 'failed to parse address from URL',
4268
+ address: null,
4269
+ port: null,
4270
+ };
4271
+ }
4272
+ host = result[1];
4273
+ // Otherwise just assign the URL to the host and let the library parse it
4274
+ }
4275
+ else {
4276
+ host = url;
4277
+ }
4278
+ // If there's a port convert it to an integer
4279
+ if (port) {
4280
+ port = parseInt(port, 10);
4281
+ // squelch out of range ports
4282
+ if (port < 0 || port > 65536) {
4283
+ port = null;
4284
+ }
4285
+ }
4286
+ else {
4287
+ // Standardize `undefined` to `null`
4288
+ port = null;
4289
+ }
4290
+ return {
4291
+ address: new Address6(host),
4292
+ port,
4293
+ };
4294
+ }
4295
+ /**
4296
+ * Create an IPv6-mapped address given an IPv4 address
4297
+ * @memberof Address6
4298
+ * @static
4299
+ * @param {string} address - An IPv4 address string
4300
+ * @returns {Address6}
4301
+ * @example
4302
+ * var address = Address6.fromAddress4('192.168.0.1');
4303
+ * address.correctForm(); // '::ffff:c0a8:1'
4304
+ * address.to4in6(); // '::ffff:192.168.0.1'
4305
+ */
4306
+ static fromAddress4(address) {
4307
+ const address4 = new ipv4_1.Address4(address);
4308
+ const mask6 = constants6.BITS - (constants4.BITS - address4.subnetMask);
4309
+ return new Address6(`::ffff:${address4.correctForm()}/${mask6}`);
4310
+ }
4311
+ /**
4312
+ * Return an address from ip6.arpa form
4313
+ * @memberof Address6
4314
+ * @static
4315
+ * @param {string} arpaFormAddress - an 'ip6.arpa' form address
4316
+ * @returns {Adress6}
4317
+ * @example
4318
+ * var address = Address6.fromArpa(e.f.f.f.3.c.2.6.f.f.f.e.6.6.8.e.1.0.6.7.9.4.e.c.0.0.0.0.1.0.0.2.ip6.arpa.)
4319
+ * address.correctForm(); // '2001:0:ce49:7601:e866:efff:62c3:fffe'
4320
+ */
4321
+ static fromArpa(arpaFormAddress) {
4322
+ // remove ending ".ip6.arpa." or just "."
4323
+ let address = arpaFormAddress.replace(/(\.ip6\.arpa)?\.$/, '');
4324
+ const semicolonAmount = 7;
4325
+ // correct ip6.arpa form with ending removed will be 63 characters
4326
+ if (address.length !== 63) {
4327
+ throw new address_error_1.AddressError("Invalid 'ip6.arpa' form.");
4328
+ }
4329
+ const parts = address.split('.').reverse();
4330
+ for (let i = semicolonAmount; i > 0; i--) {
4331
+ const insertIndex = i * 4;
4332
+ parts.splice(insertIndex, 0, ':');
4333
+ }
4334
+ address = parts.join('');
4335
+ return new Address6(address);
4336
+ }
4337
+ /**
4338
+ * Return the Microsoft UNC transcription of the address
4339
+ * @memberof Address6
4340
+ * @instance
4341
+ * @returns {String} the Microsoft UNC transcription of the address
4342
+ */
4343
+ microsoftTranscription() {
4344
+ return `${this.correctForm().replace(/:/g, '-')}.ipv6-literal.net`;
4345
+ }
4346
+ /**
4347
+ * Return the first n bits of the address, defaulting to the subnet mask
4348
+ * @memberof Address6
4349
+ * @instance
4350
+ * @param {number} [mask=subnet] - the number of bits to mask
4351
+ * @returns {String} the first n bits of the address as a string
4352
+ */
4353
+ mask(mask = this.subnetMask) {
4354
+ return this.getBitsBase2(0, mask);
4355
+ }
4356
+ /**
4357
+ * Return the number of possible subnets of a given size in the address
4358
+ * @memberof Address6
4359
+ * @instance
4360
+ * @param {number} [subnetSize=128] - the subnet size
4361
+ * @returns {String}
4362
+ */
4363
+ // TODO: probably useful to have a numeric version of this too
4364
+ possibleSubnets(subnetSize = 128) {
4365
+ const availableBits = constants6.BITS - this.subnetMask;
4366
+ const subnetBits = Math.abs(subnetSize - constants6.BITS);
4367
+ const subnetPowers = availableBits - subnetBits;
4368
+ if (subnetPowers < 0) {
4369
+ return '0';
4370
+ }
4371
+ return addCommas((BigInt('2') ** BigInt(subnetPowers)).toString(10));
4372
+ }
4373
+ /**
4374
+ * Helper function getting start address.
4375
+ * @memberof Address6
4376
+ * @instance
4377
+ * @returns {bigint}
4378
+ */
4379
+ _startAddress() {
4380
+ return BigInt(`0b${this.mask() + '0'.repeat(constants6.BITS - this.subnetMask)}`);
4381
+ }
4382
+ /**
4383
+ * The first address in the range given by this address' subnet
4384
+ * Often referred to as the Network Address.
4385
+ * @memberof Address6
4386
+ * @instance
4387
+ * @returns {Address6}
4388
+ */
4389
+ startAddress() {
4390
+ return Address6.fromBigInt(this._startAddress());
4391
+ }
4392
+ /**
4393
+ * The first host address in the range given by this address's subnet ie
4394
+ * the first address after the Network Address
4395
+ * @memberof Address6
4396
+ * @instance
4397
+ * @returns {Address6}
4398
+ */
4399
+ startAddressExclusive() {
4400
+ const adjust = BigInt('1');
4401
+ return Address6.fromBigInt(this._startAddress() + adjust);
4402
+ }
4403
+ /**
4404
+ * Helper function getting end address.
4405
+ * @memberof Address6
4406
+ * @instance
4407
+ * @returns {bigint}
4408
+ */
4409
+ _endAddress() {
4410
+ return BigInt(`0b${this.mask() + '1'.repeat(constants6.BITS - this.subnetMask)}`);
4411
+ }
4412
+ /**
4413
+ * The last address in the range given by this address' subnet
4414
+ * Often referred to as the Broadcast
4415
+ * @memberof Address6
4416
+ * @instance
4417
+ * @returns {Address6}
4418
+ */
4419
+ endAddress() {
4420
+ return Address6.fromBigInt(this._endAddress());
4421
+ }
4422
+ /**
4423
+ * The last host address in the range given by this address's subnet ie
4424
+ * the last address prior to the Broadcast Address
4425
+ * @memberof Address6
4426
+ * @instance
4427
+ * @returns {Address6}
4428
+ */
4429
+ endAddressExclusive() {
4430
+ const adjust = BigInt('1');
4431
+ return Address6.fromBigInt(this._endAddress() - adjust);
4432
+ }
4433
+ /**
4434
+ * Return the scope of the address
4435
+ * @memberof Address6
4436
+ * @instance
4437
+ * @returns {String}
4438
+ */
4439
+ getScope() {
4440
+ let scope = constants6.SCOPES[parseInt(this.getBits(12, 16).toString(10), 10)];
4441
+ if (this.getType() === 'Global unicast' && scope !== 'Link local') {
4442
+ scope = 'Global';
4443
+ }
4444
+ return scope || 'Unknown';
4445
+ }
4446
+ /**
4447
+ * Return the type of the address
4448
+ * @memberof Address6
4449
+ * @instance
4450
+ * @returns {String}
4451
+ */
4452
+ getType() {
4453
+ for (const subnet of Object.keys(constants6.TYPES)) {
4454
+ if (this.isInSubnet(new Address6(subnet))) {
4455
+ return constants6.TYPES[subnet];
4456
+ }
4457
+ }
4458
+ return 'Global unicast';
4459
+ }
4460
+ /**
4461
+ * Return the bits in the given range as a BigInt
4462
+ * @memberof Address6
4463
+ * @instance
4464
+ * @returns {bigint}
4465
+ */
4466
+ getBits(start, end) {
4467
+ return BigInt(`0b${this.getBitsBase2(start, end)}`);
4468
+ }
4469
+ /**
4470
+ * Return the bits in the given range as a base-2 string
4471
+ * @memberof Address6
4472
+ * @instance
4473
+ * @returns {String}
4474
+ */
4475
+ getBitsBase2(start, end) {
4476
+ return this.binaryZeroPad().slice(start, end);
4477
+ }
4478
+ /**
4479
+ * Return the bits in the given range as a base-16 string
4480
+ * @memberof Address6
4481
+ * @instance
4482
+ * @returns {String}
4483
+ */
4484
+ getBitsBase16(start, end) {
4485
+ const length = end - start;
4486
+ if (length % 4 !== 0) {
4487
+ throw new Error('Length of bits to retrieve must be divisible by four');
4488
+ }
4489
+ return this.getBits(start, end)
4490
+ .toString(16)
4491
+ .padStart(length / 4, '0');
4492
+ }
4493
+ /**
4494
+ * Return the bits that are set past the subnet mask length
4495
+ * @memberof Address6
4496
+ * @instance
4497
+ * @returns {String}
4498
+ */
4499
+ getBitsPastSubnet() {
4500
+ return this.getBitsBase2(this.subnetMask, constants6.BITS);
4501
+ }
4502
+ /**
4503
+ * Return the reversed ip6.arpa form of the address
4504
+ * @memberof Address6
4505
+ * @param {Object} options
4506
+ * @param {boolean} options.omitSuffix - omit the "ip6.arpa" suffix
4507
+ * @instance
4508
+ * @returns {String}
4509
+ */
4510
+ reverseForm(options) {
4511
+ if (!options) {
4512
+ options = {};
4513
+ }
4514
+ const characters = Math.floor(this.subnetMask / 4);
4515
+ const reversed = this.canonicalForm()
4516
+ .replace(/:/g, '')
4517
+ .split('')
4518
+ .slice(0, characters)
4519
+ .reverse()
4520
+ .join('.');
4521
+ if (characters > 0) {
4522
+ if (options.omitSuffix) {
4523
+ return reversed;
4524
+ }
4525
+ return `${reversed}.ip6.arpa.`;
4526
+ }
4527
+ if (options.omitSuffix) {
4528
+ return '';
4529
+ }
4530
+ return 'ip6.arpa.';
4531
+ }
4532
+ /**
4533
+ * Return the correct form of the address
4534
+ * @memberof Address6
4535
+ * @instance
4536
+ * @returns {String}
4537
+ */
4538
+ correctForm() {
4539
+ let i;
4540
+ let groups = [];
4541
+ let zeroCounter = 0;
4542
+ const zeroes = [];
4543
+ for (i = 0; i < this.parsedAddress.length; i++) {
4544
+ const value = parseInt(this.parsedAddress[i], 16);
4545
+ if (value === 0) {
4546
+ zeroCounter++;
4547
+ }
4548
+ if (value !== 0 && zeroCounter > 0) {
4549
+ if (zeroCounter > 1) {
4550
+ zeroes.push([i - zeroCounter, i - 1]);
4551
+ }
4552
+ zeroCounter = 0;
4553
+ }
4554
+ }
4555
+ // Do we end with a string of zeroes?
4556
+ if (zeroCounter > 1) {
4557
+ zeroes.push([this.parsedAddress.length - zeroCounter, this.parsedAddress.length - 1]);
4558
+ }
4559
+ const zeroLengths = zeroes.map((n) => n[1] - n[0] + 1);
4560
+ if (zeroes.length > 0) {
4561
+ const index = zeroLengths.indexOf(Math.max(...zeroLengths));
4562
+ groups = compact(this.parsedAddress, zeroes[index]);
4563
+ }
4564
+ else {
4565
+ groups = this.parsedAddress;
4566
+ }
4567
+ for (i = 0; i < groups.length; i++) {
4568
+ if (groups[i] !== 'compact') {
4569
+ groups[i] = parseInt(groups[i], 16).toString(16);
4570
+ }
4571
+ }
4572
+ let correct = groups.join(':');
4573
+ correct = correct.replace(/^compact$/, '::');
4574
+ correct = correct.replace(/(^compact)|(compact$)/, ':');
4575
+ correct = correct.replace(/compact/, '');
4576
+ return correct;
4577
+ }
4578
+ /**
4579
+ * Return a zero-padded base-2 string representation of the address
4580
+ * @memberof Address6
4581
+ * @instance
4582
+ * @returns {String}
4583
+ * @example
4584
+ * var address = new Address6('2001:4860:4001:803::1011');
4585
+ * address.binaryZeroPad();
4586
+ * // '0010000000000001010010000110000001000000000000010000100000000011
4587
+ * // 0000000000000000000000000000000000000000000000000001000000010001'
4588
+ */
4589
+ binaryZeroPad() {
4590
+ return this.bigInt().toString(2).padStart(constants6.BITS, '0');
4591
+ }
4592
+ // TODO: Improve the semantics of this helper function
4593
+ parse4in6(address) {
4594
+ const groups = address.split(':');
4595
+ const lastGroup = groups.slice(-1)[0];
4596
+ const address4 = lastGroup.match(constants4.RE_ADDRESS);
4597
+ if (address4) {
4598
+ this.parsedAddress4 = address4[0];
4599
+ this.address4 = new ipv4_1.Address4(this.parsedAddress4);
4600
+ for (let i = 0; i < this.address4.groups; i++) {
4601
+ if (/^0[0-9]+/.test(this.address4.parsedAddress[i])) {
4602
+ throw new address_error_1.AddressError("IPv4 addresses can't have leading zeroes.", address.replace(constants4.RE_ADDRESS, this.address4.parsedAddress.map(spanLeadingZeroes4).join('.')));
4603
+ }
4604
+ }
4605
+ this.v4 = true;
4606
+ groups[groups.length - 1] = this.address4.toGroup6();
4607
+ address = groups.join(':');
4608
+ }
4609
+ return address;
4610
+ }
4611
+ // TODO: Make private?
4612
+ parse(address) {
4613
+ address = this.parse4in6(address);
4614
+ const badCharacters = address.match(constants6.RE_BAD_CHARACTERS);
4615
+ if (badCharacters) {
4616
+ throw new address_error_1.AddressError(`Bad character${badCharacters.length > 1 ? 's' : ''} detected in address: ${badCharacters.join('')}`, address.replace(constants6.RE_BAD_CHARACTERS, '<span class="parse-error">$1</span>'));
4617
+ }
4618
+ const badAddress = address.match(constants6.RE_BAD_ADDRESS);
4619
+ if (badAddress) {
4620
+ throw new address_error_1.AddressError(`Address failed regex: ${badAddress.join('')}`, address.replace(constants6.RE_BAD_ADDRESS, '<span class="parse-error">$1</span>'));
4621
+ }
4622
+ let groups = [];
4623
+ const halves = address.split('::');
4624
+ if (halves.length === 2) {
4625
+ let first = halves[0].split(':');
4626
+ let last = halves[1].split(':');
4627
+ if (first.length === 1 && first[0] === '') {
4628
+ first = [];
4629
+ }
4630
+ if (last.length === 1 && last[0] === '') {
4631
+ last = [];
4632
+ }
4633
+ const remaining = this.groups - (first.length + last.length);
4634
+ if (!remaining) {
4635
+ throw new address_error_1.AddressError('Error parsing groups');
4636
+ }
4637
+ this.elidedGroups = remaining;
4638
+ this.elisionBegin = first.length;
4639
+ this.elisionEnd = first.length + this.elidedGroups;
4640
+ groups = groups.concat(first);
4641
+ for (let i = 0; i < remaining; i++) {
4642
+ groups.push('0');
4643
+ }
4644
+ groups = groups.concat(last);
4645
+ }
4646
+ else if (halves.length === 1) {
4647
+ groups = address.split(':');
4648
+ this.elidedGroups = 0;
4649
+ }
4650
+ else {
4651
+ throw new address_error_1.AddressError('Too many :: groups found');
4652
+ }
4653
+ groups = groups.map((group) => parseInt(group, 16).toString(16));
4654
+ if (groups.length !== this.groups) {
4655
+ throw new address_error_1.AddressError('Incorrect number of groups found');
4656
+ }
4657
+ return groups;
4658
+ }
4659
+ /**
4660
+ * Return the canonical form of the address
4661
+ * @memberof Address6
4662
+ * @instance
4663
+ * @returns {String}
4664
+ */
4665
+ canonicalForm() {
4666
+ return this.parsedAddress.map(paddedHex).join(':');
4667
+ }
4668
+ /**
4669
+ * Return the decimal form of the address
4670
+ * @memberof Address6
4671
+ * @instance
4672
+ * @returns {String}
4673
+ */
4674
+ decimal() {
4675
+ return this.parsedAddress.map((n) => parseInt(n, 16).toString(10).padStart(5, '0')).join(':');
4676
+ }
4677
+ /**
4678
+ * Return the address as a BigInt
4679
+ * @memberof Address6
4680
+ * @instance
4681
+ * @returns {bigint}
4682
+ */
4683
+ bigInt() {
4684
+ return BigInt(`0x${this.parsedAddress.map(paddedHex).join('')}`);
4685
+ }
4686
+ /**
4687
+ * Return the last two groups of this address as an IPv4 address string
4688
+ * @memberof Address6
4689
+ * @instance
4690
+ * @returns {Address4}
4691
+ * @example
4692
+ * var address = new Address6('2001:4860:4001::1825:bf11');
4693
+ * address.to4().correctForm(); // '24.37.191.17'
4694
+ */
4695
+ to4() {
4696
+ const binary = this.binaryZeroPad().split('');
4697
+ return ipv4_1.Address4.fromHex(BigInt(`0b${binary.slice(96, 128).join('')}`).toString(16));
4698
+ }
4699
+ /**
4700
+ * Return the v4-in-v6 form of the address
4701
+ * @memberof Address6
4702
+ * @instance
4703
+ * @returns {String}
4704
+ */
4705
+ to4in6() {
4706
+ const address4 = this.to4();
4707
+ const address6 = new Address6(this.parsedAddress.slice(0, 6).join(':'), 6);
4708
+ const correct = address6.correctForm();
4709
+ let infix = '';
4710
+ if (!/:$/.test(correct)) {
4711
+ infix = ':';
4712
+ }
4713
+ return correct + infix + address4.address;
4714
+ }
4715
+ /**
4716
+ * Return an object containing the Teredo properties of the address
4717
+ * @memberof Address6
4718
+ * @instance
4719
+ * @returns {Object}
4720
+ */
4721
+ inspectTeredo() {
4722
+ /*
4723
+ - Bits 0 to 31 are set to the Teredo prefix (normally 2001:0000::/32).
4724
+ - Bits 32 to 63 embed the primary IPv4 address of the Teredo server that
4725
+ is used.
4726
+ - Bits 64 to 79 can be used to define some flags. Currently only the
4727
+ higher order bit is used; it is set to 1 if the Teredo client is
4728
+ located behind a cone NAT, 0 otherwise. For Microsoft's Windows Vista
4729
+ and Windows Server 2008 implementations, more bits are used. In those
4730
+ implementations, the format for these 16 bits is "CRAAAAUG AAAAAAAA",
4731
+ where "C" remains the "Cone" flag. The "R" bit is reserved for future
4732
+ use. The "U" bit is for the Universal/Local flag (set to 0). The "G" bit
4733
+ is Individual/Group flag (set to 0). The A bits are set to a 12-bit
4734
+ randomly generated number chosen by the Teredo client to introduce
4735
+ additional protection for the Teredo node against IPv6-based scanning
4736
+ attacks.
4737
+ - Bits 80 to 95 contains the obfuscated UDP port number. This is the
4738
+ port number that is mapped by the NAT to the Teredo client with all
4739
+ bits inverted.
4740
+ - Bits 96 to 127 contains the obfuscated IPv4 address. This is the
4741
+ public IPv4 address of the NAT with all bits inverted.
4742
+ */
4743
+ const prefix = this.getBitsBase16(0, 32);
4744
+ const bitsForUdpPort = this.getBits(80, 96);
4745
+ // eslint-disable-next-line no-bitwise
4746
+ const udpPort = (bitsForUdpPort ^ BigInt('0xffff')).toString();
4747
+ const server4 = ipv4_1.Address4.fromHex(this.getBitsBase16(32, 64));
4748
+ const bitsForClient4 = this.getBits(96, 128);
4749
+ // eslint-disable-next-line no-bitwise
4750
+ const client4 = ipv4_1.Address4.fromHex((bitsForClient4 ^ BigInt('0xffffffff')).toString(16));
4751
+ const flagsBase2 = this.getBitsBase2(64, 80);
4752
+ const coneNat = (0, common_1.testBit)(flagsBase2, 15);
4753
+ const reserved = (0, common_1.testBit)(flagsBase2, 14);
4754
+ const groupIndividual = (0, common_1.testBit)(flagsBase2, 8);
4755
+ const universalLocal = (0, common_1.testBit)(flagsBase2, 9);
4756
+ const nonce = BigInt(`0b${flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16)}`).toString(10);
4757
+ return {
4758
+ prefix: `${prefix.slice(0, 4)}:${prefix.slice(4, 8)}`,
4759
+ server4: server4.address,
4760
+ client4: client4.address,
4761
+ flags: flagsBase2,
4762
+ coneNat,
4763
+ microsoft: {
4764
+ reserved,
4765
+ universalLocal,
4766
+ groupIndividual,
4767
+ nonce,
4768
+ },
4769
+ udpPort,
4770
+ };
4771
+ }
4772
+ /**
4773
+ * Return an object containing the 6to4 properties of the address
4774
+ * @memberof Address6
4775
+ * @instance
4776
+ * @returns {Object}
4777
+ */
4778
+ inspect6to4() {
4779
+ /*
4780
+ - Bits 0 to 15 are set to the 6to4 prefix (2002::/16).
4781
+ - Bits 16 to 48 embed the IPv4 address of the 6to4 gateway that is used.
4782
+ */
4783
+ const prefix = this.getBitsBase16(0, 16);
4784
+ const gateway = ipv4_1.Address4.fromHex(this.getBitsBase16(16, 48));
4785
+ return {
4786
+ prefix: prefix.slice(0, 4),
4787
+ gateway: gateway.address,
4788
+ };
4789
+ }
4790
+ /**
4791
+ * Return a v6 6to4 address from a v6 v4inv6 address
4792
+ * @memberof Address6
4793
+ * @instance
4794
+ * @returns {Address6}
4795
+ */
4796
+ to6to4() {
4797
+ if (!this.is4()) {
4798
+ return null;
4799
+ }
4800
+ const addr6to4 = [
4801
+ '2002',
4802
+ this.getBitsBase16(96, 112),
4803
+ this.getBitsBase16(112, 128),
4804
+ '',
4805
+ '/16',
4806
+ ].join(':');
4807
+ return new Address6(addr6to4);
4808
+ }
4809
+ /**
4810
+ * Return a byte array
4811
+ * @memberof Address6
4812
+ * @instance
4813
+ * @returns {Array}
4814
+ */
4815
+ toByteArray() {
4816
+ const valueWithoutPadding = this.bigInt().toString(16);
4817
+ const leadingPad = '0'.repeat(valueWithoutPadding.length % 2);
4818
+ const value = `${leadingPad}${valueWithoutPadding}`;
4819
+ const bytes = [];
4820
+ for (let i = 0, length = value.length; i < length; i += 2) {
4821
+ bytes.push(parseInt(value.substring(i, i + 2), 16));
4822
+ }
4823
+ return bytes;
4824
+ }
4825
+ /**
4826
+ * Return an unsigned byte array
4827
+ * @memberof Address6
4828
+ * @instance
4829
+ * @returns {Array}
4830
+ */
4831
+ toUnsignedByteArray() {
4832
+ return this.toByteArray().map(unsignByte);
4833
+ }
4834
+ /**
4835
+ * Convert a byte array to an Address6 object
4836
+ * @memberof Address6
4837
+ * @static
4838
+ * @returns {Address6}
4839
+ */
4840
+ static fromByteArray(bytes) {
4841
+ return this.fromUnsignedByteArray(bytes.map(unsignByte));
4842
+ }
4843
+ /**
4844
+ * Convert an unsigned byte array to an Address6 object
4845
+ * @memberof Address6
4846
+ * @static
4847
+ * @returns {Address6}
4848
+ */
4849
+ static fromUnsignedByteArray(bytes) {
4850
+ const BYTE_MAX = BigInt('256');
4851
+ let result = BigInt('0');
4852
+ let multiplier = BigInt('1');
4853
+ for (let i = bytes.length - 1; i >= 0; i--) {
4854
+ result += multiplier * BigInt(bytes[i].toString(10));
4855
+ multiplier *= BYTE_MAX;
4856
+ }
4857
+ return Address6.fromBigInt(result);
4858
+ }
4859
+ /**
4860
+ * Returns true if the address is in the canonical form, false otherwise
4861
+ * @memberof Address6
4862
+ * @instance
4863
+ * @returns {boolean}
4864
+ */
4865
+ isCanonical() {
4866
+ return this.addressMinusSuffix === this.canonicalForm();
4867
+ }
4868
+ /**
4869
+ * Returns true if the address is a link local address, false otherwise
4870
+ * @memberof Address6
4871
+ * @instance
4872
+ * @returns {boolean}
4873
+ */
4874
+ isLinkLocal() {
4875
+ // Zeroes are required, i.e. we can't check isInSubnet with 'fe80::/10'
4876
+ if (this.getBitsBase2(0, 64) ===
4877
+ '1111111010000000000000000000000000000000000000000000000000000000') {
4878
+ return true;
4879
+ }
4880
+ return false;
4881
+ }
4882
+ /**
4883
+ * Returns true if the address is a multicast address, false otherwise
4884
+ * @memberof Address6
4885
+ * @instance
4886
+ * @returns {boolean}
4887
+ */
4888
+ isMulticast() {
4889
+ return this.getType() === 'Multicast';
4890
+ }
4891
+ /**
4892
+ * Returns true if the address is a v4-in-v6 address, false otherwise
4893
+ * @memberof Address6
4894
+ * @instance
4895
+ * @returns {boolean}
4896
+ */
4897
+ is4() {
4898
+ return this.v4;
4899
+ }
4900
+ /**
4901
+ * Returns true if the address is a Teredo address, false otherwise
4902
+ * @memberof Address6
4903
+ * @instance
4904
+ * @returns {boolean}
4905
+ */
4906
+ isTeredo() {
4907
+ return this.isInSubnet(new Address6('2001::/32'));
4908
+ }
4909
+ /**
4910
+ * Returns true if the address is a 6to4 address, false otherwise
4911
+ * @memberof Address6
4912
+ * @instance
4913
+ * @returns {boolean}
4914
+ */
4915
+ is6to4() {
4916
+ return this.isInSubnet(new Address6('2002::/16'));
4917
+ }
4918
+ /**
4919
+ * Returns true if the address is a loopback address, false otherwise
4920
+ * @memberof Address6
4921
+ * @instance
4922
+ * @returns {boolean}
4923
+ */
4924
+ isLoopback() {
4925
+ return this.getType() === 'Loopback';
4926
+ }
4927
+ // #endregion
4928
+ // #region HTML
4929
+ /**
4930
+ * @returns {String} the address in link form with a default port of 80
4931
+ */
4932
+ href(optionalPort) {
4933
+ if (optionalPort === undefined) {
4934
+ optionalPort = '';
4935
+ }
4936
+ else {
4937
+ optionalPort = `:${optionalPort}`;
4938
+ }
4939
+ return `http://[${this.correctForm()}]${optionalPort}/`;
4940
+ }
4941
+ /**
4942
+ * @returns {String} a link suitable for conveying the address via a URL hash
4943
+ */
4944
+ link(options) {
4945
+ if (!options) {
4946
+ options = {};
4947
+ }
4948
+ if (options.className === undefined) {
4949
+ options.className = '';
4950
+ }
4951
+ if (options.prefix === undefined) {
4952
+ options.prefix = '/#address=';
4953
+ }
4954
+ if (options.v4 === undefined) {
4955
+ options.v4 = false;
4956
+ }
4957
+ let formFunction = this.correctForm;
4958
+ if (options.v4) {
4959
+ formFunction = this.to4in6;
4960
+ }
4961
+ const form = formFunction.call(this);
4962
+ if (options.className) {
4963
+ return `<a href="${options.prefix}${form}" class="${options.className}">${form}</a>`;
4964
+ }
4965
+ return `<a href="${options.prefix}${form}">${form}</a>`;
4966
+ }
4967
+ /**
4968
+ * Groups an address
4969
+ * @returns {String}
4970
+ */
4971
+ group() {
4972
+ if (this.elidedGroups === 0) {
4973
+ // The simple case
4974
+ return helpers.simpleGroup(this.address).join(':');
4975
+ }
4976
+ assert(typeof this.elidedGroups === 'number');
4977
+ assert(typeof this.elisionBegin === 'number');
4978
+ // The elided case
4979
+ const output = [];
4980
+ const [left, right] = this.address.split('::');
4981
+ if (left.length) {
4982
+ output.push(...helpers.simpleGroup(left));
4983
+ }
4984
+ else {
4985
+ output.push('');
4986
+ }
4987
+ const classes = ['hover-group'];
4988
+ for (let i = this.elisionBegin; i < this.elisionBegin + this.elidedGroups; i++) {
4989
+ classes.push(`group-${i}`);
4990
+ }
4991
+ output.push(`<span class="${classes.join(' ')}"></span>`);
4992
+ if (right.length) {
4993
+ output.push(...helpers.simpleGroup(right, this.elisionEnd));
4994
+ }
4995
+ else {
4996
+ output.push('');
4997
+ }
4998
+ if (this.is4()) {
4999
+ assert(this.address4 instanceof ipv4_1.Address4);
5000
+ output.pop();
5001
+ output.push(this.address4.groupForV6());
5002
+ }
5003
+ return output.join(':');
5004
+ }
5005
+ // #endregion
5006
+ // #region Regular expressions
5007
+ /**
5008
+ * Generate a regular expression string that can be used to find or validate
5009
+ * all variations of this address
5010
+ * @memberof Address6
5011
+ * @instance
5012
+ * @param {boolean} substringSearch
5013
+ * @returns {string}
5014
+ */
5015
+ regularExpressionString(substringSearch = false) {
5016
+ let output = [];
5017
+ // TODO: revisit why this is necessary
5018
+ const address6 = new Address6(this.correctForm());
5019
+ if (address6.elidedGroups === 0) {
5020
+ // The simple case
5021
+ output.push((0, regular_expressions_1.simpleRegularExpression)(address6.parsedAddress));
5022
+ }
5023
+ else if (address6.elidedGroups === constants6.GROUPS) {
5024
+ // A completely elided address
5025
+ output.push((0, regular_expressions_1.possibleElisions)(constants6.GROUPS));
5026
+ }
5027
+ else {
5028
+ // A partially elided address
5029
+ const halves = address6.address.split('::');
5030
+ if (halves[0].length) {
5031
+ output.push((0, regular_expressions_1.simpleRegularExpression)(halves[0].split(':')));
5032
+ }
5033
+ assert(typeof address6.elidedGroups === 'number');
5034
+ output.push((0, regular_expressions_1.possibleElisions)(address6.elidedGroups, halves[0].length !== 0, halves[1].length !== 0));
5035
+ if (halves[1].length) {
5036
+ output.push((0, regular_expressions_1.simpleRegularExpression)(halves[1].split(':')));
5037
+ }
5038
+ output = [output.join(':')];
5039
+ }
5040
+ if (!substringSearch) {
5041
+ output = [
5042
+ '(?=^|',
5043
+ regular_expressions_1.ADDRESS_BOUNDARY,
5044
+ '|[^\\w\\:])(',
5045
+ ...output,
5046
+ ')(?=[^\\w\\:]|',
5047
+ regular_expressions_1.ADDRESS_BOUNDARY,
5048
+ '|$)',
5049
+ ];
5050
+ }
5051
+ return output.join('');
5052
+ }
5053
+ /**
5054
+ * Generate a regular expression that can be used to find or validate all
5055
+ * variations of this address.
5056
+ * @memberof Address6
5057
+ * @instance
5058
+ * @param {boolean} substringSearch
5059
+ * @returns {RegExp}
5060
+ */
5061
+ regularExpression(substringSearch = false) {
5062
+ return new RegExp(this.regularExpressionString(substringSearch), 'i');
5063
+ }
5064
+ }
5065
+ ipv6.Address6 = Address6;
5066
+
5067
+ return ipv6;
5068
+ }
5069
+
5070
+ var hasRequiredIpAddress;
5071
+
5072
+ function requireIpAddress () {
5073
+ if (hasRequiredIpAddress) return ipAddress;
5074
+ hasRequiredIpAddress = 1;
5075
+ (function (exports$1) {
5076
+ var __createBinding = (ipAddress && ipAddress.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5077
+ if (k2 === undefined) k2 = k;
5078
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5079
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5080
+ desc = { enumerable: true, get: function() { return m[k]; } };
5081
+ }
5082
+ Object.defineProperty(o, k2, desc);
5083
+ }) : (function(o, m, k, k2) {
5084
+ if (k2 === undefined) k2 = k;
5085
+ o[k2] = m[k];
5086
+ }));
5087
+ var __setModuleDefault = (ipAddress && ipAddress.__setModuleDefault) || (Object.create ? (function(o, v) {
5088
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
5089
+ }) : function(o, v) {
5090
+ o["default"] = v;
5091
+ });
5092
+ var __importStar = (ipAddress && ipAddress.__importStar) || function (mod) {
5093
+ if (mod && mod.__esModule) return mod;
5094
+ var result = {};
5095
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
5096
+ __setModuleDefault(result, mod);
5097
+ return result;
5098
+ };
5099
+ Object.defineProperty(exports$1, "__esModule", { value: true });
5100
+ exports$1.v6 = exports$1.AddressError = exports$1.Address6 = exports$1.Address4 = void 0;
5101
+ var ipv4_1 = requireIpv4();
5102
+ Object.defineProperty(exports$1, "Address4", { enumerable: true, get: function () { return ipv4_1.Address4; } });
5103
+ var ipv6_1 = requireIpv6();
5104
+ Object.defineProperty(exports$1, "Address6", { enumerable: true, get: function () { return ipv6_1.Address6; } });
5105
+ var address_error_1 = requireAddressError();
5106
+ Object.defineProperty(exports$1, "AddressError", { enumerable: true, get: function () { return address_error_1.AddressError; } });
5107
+ const helpers = __importStar(requireHelpers$1());
5108
+ exports$1.v6 = { helpers };
5109
+
5110
+ } (ipAddress));
5111
+ return ipAddress;
5112
+ }
5113
+
5114
+ var hasRequiredHelpers;
5115
+
5116
+ function requireHelpers () {
5117
+ if (hasRequiredHelpers) return helpers$1;
5118
+ hasRequiredHelpers = 1;
5119
+ Object.defineProperty(helpers$1, "__esModule", { value: true });
5120
+ helpers$1.ipToBuffer = helpers$1.int32ToIpv4 = helpers$1.ipv4ToInt32 = helpers$1.validateSocksClientChainOptions = helpers$1.validateSocksClientOptions = void 0;
5121
+ const util_1 = requireUtil();
5122
+ const constants_1 = requireConstants$2();
5123
+ const stream = require$$2$1;
5124
+ const ip_address_1 = requireIpAddress();
5125
+ const net$1 = net;
5126
+ /**
5127
+ * Validates the provided SocksClientOptions
5128
+ * @param options { SocksClientOptions }
5129
+ * @param acceptedCommands { string[] } A list of accepted SocksProxy commands.
5130
+ */
5131
+ function validateSocksClientOptions(options, acceptedCommands = ['connect', 'bind', 'associate']) {
5132
+ // Check SOCKs command option.
5133
+ if (!constants_1.SocksCommand[options.command]) {
5134
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommand, options);
5135
+ }
5136
+ // Check SocksCommand for acceptable command.
5137
+ if (acceptedCommands.indexOf(options.command) === -1) {
5138
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandForOperation, options);
5139
+ }
5140
+ // Check destination
5141
+ if (!isValidSocksRemoteHost(options.destination)) {
5142
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
5143
+ }
5144
+ // Check SOCKS proxy to use
5145
+ if (!isValidSocksProxy(options.proxy)) {
5146
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
5147
+ }
5148
+ // Validate custom auth (if set)
5149
+ validateCustomProxyAuth(options.proxy, options);
5150
+ // Check timeout
5151
+ if (options.timeout && !isValidTimeoutValue(options.timeout)) {
5152
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
5153
+ }
5154
+ // Check existing_socket (if provided)
5155
+ if (options.existing_socket &&
5156
+ !(options.existing_socket instanceof stream.Duplex)) {
5157
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsExistingSocket, options);
5158
+ }
5159
+ }
5160
+ helpers$1.validateSocksClientOptions = validateSocksClientOptions;
5161
+ /**
5162
+ * Validates the SocksClientChainOptions
5163
+ * @param options { SocksClientChainOptions }
5164
+ */
5165
+ function validateSocksClientChainOptions(options) {
5166
+ // Only connect is supported when chaining.
5167
+ if (options.command !== 'connect') {
5168
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandChain, options);
5169
+ }
5170
+ // Check destination
5171
+ if (!isValidSocksRemoteHost(options.destination)) {
5172
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
5173
+ }
5174
+ // Validate proxies (length)
5175
+ if (!(options.proxies &&
5176
+ Array.isArray(options.proxies) &&
5177
+ options.proxies.length >= 2)) {
5178
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxiesLength, options);
5179
+ }
5180
+ // Validate proxies
5181
+ options.proxies.forEach((proxy) => {
5182
+ if (!isValidSocksProxy(proxy)) {
5183
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
5184
+ }
5185
+ // Validate custom auth (if set)
5186
+ validateCustomProxyAuth(proxy, options);
5187
+ });
5188
+ // Check timeout
5189
+ if (options.timeout && !isValidTimeoutValue(options.timeout)) {
5190
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
5191
+ }
5192
+ }
5193
+ helpers$1.validateSocksClientChainOptions = validateSocksClientChainOptions;
5194
+ function validateCustomProxyAuth(proxy, options) {
5195
+ if (proxy.custom_auth_method !== undefined) {
5196
+ // Invalid auth method range
5197
+ if (proxy.custom_auth_method < constants_1.SOCKS5_CUSTOM_AUTH_START ||
5198
+ proxy.custom_auth_method > constants_1.SOCKS5_CUSTOM_AUTH_END) {
5199
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthRange, options);
5200
+ }
5201
+ // Missing custom_auth_request_handler
5202
+ if (proxy.custom_auth_request_handler === undefined ||
5203
+ typeof proxy.custom_auth_request_handler !== 'function') {
5204
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
5205
+ }
5206
+ // Missing custom_auth_response_size
5207
+ if (proxy.custom_auth_response_size === undefined) {
5208
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
5209
+ }
5210
+ // Missing/invalid custom_auth_response_handler
5211
+ if (proxy.custom_auth_response_handler === undefined ||
5212
+ typeof proxy.custom_auth_response_handler !== 'function') {
5213
+ throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
5214
+ }
5215
+ }
5216
+ }
5217
+ /**
5218
+ * Validates a SocksRemoteHost
5219
+ * @param remoteHost { SocksRemoteHost }
5220
+ */
5221
+ function isValidSocksRemoteHost(remoteHost) {
5222
+ return (remoteHost &&
5223
+ typeof remoteHost.host === 'string' &&
5224
+ Buffer.byteLength(remoteHost.host) < 256 &&
5225
+ typeof remoteHost.port === 'number' &&
5226
+ remoteHost.port >= 0 &&
5227
+ remoteHost.port <= 65535);
5228
+ }
5229
+ /**
5230
+ * Validates a SocksProxy
5231
+ * @param proxy { SocksProxy }
5232
+ */
5233
+ function isValidSocksProxy(proxy) {
5234
+ return (proxy &&
5235
+ (typeof proxy.host === 'string' || typeof proxy.ipaddress === 'string') &&
5236
+ typeof proxy.port === 'number' &&
5237
+ proxy.port >= 0 &&
5238
+ proxy.port <= 65535 &&
5239
+ (proxy.type === 4 || proxy.type === 5));
5240
+ }
5241
+ /**
5242
+ * Validates a timeout value.
5243
+ * @param value { Number }
5244
+ */
5245
+ function isValidTimeoutValue(value) {
5246
+ return typeof value === 'number' && value > 0;
5247
+ }
5248
+ function ipv4ToInt32(ip) {
5249
+ const address = new ip_address_1.Address4(ip);
5250
+ // Convert the IPv4 address parts to an integer
5251
+ return address.toArray().reduce((acc, part) => (acc << 8) + part, 0) >>> 0;
5252
+ }
5253
+ helpers$1.ipv4ToInt32 = ipv4ToInt32;
5254
+ function int32ToIpv4(int32) {
5255
+ // Extract each byte (octet) from the 32-bit integer
5256
+ const octet1 = (int32 >>> 24) & 0xff;
5257
+ const octet2 = (int32 >>> 16) & 0xff;
5258
+ const octet3 = (int32 >>> 8) & 0xff;
5259
+ const octet4 = int32 & 0xff;
5260
+ // Combine the octets into a string in IPv4 format
5261
+ return [octet1, octet2, octet3, octet4].join('.');
5262
+ }
5263
+ helpers$1.int32ToIpv4 = int32ToIpv4;
5264
+ function ipToBuffer(ip) {
5265
+ if (net$1.isIPv4(ip)) {
5266
+ // Handle IPv4 addresses
5267
+ const address = new ip_address_1.Address4(ip);
5268
+ return Buffer.from(address.toArray());
5269
+ }
5270
+ else if (net$1.isIPv6(ip)) {
5271
+ // Handle IPv6 addresses
5272
+ const address = new ip_address_1.Address6(ip);
5273
+ return Buffer.from(address
5274
+ .canonicalForm()
5275
+ .split(':')
5276
+ .map((segment) => segment.padStart(4, '0'))
5277
+ .join(''), 'hex');
5278
+ }
5279
+ else {
5280
+ throw new Error('Invalid IP address format');
5281
+ }
5282
+ }
5283
+ helpers$1.ipToBuffer = ipToBuffer;
5284
+
5285
+ return helpers$1;
5286
+ }
5287
+
5288
+ var receivebuffer = {};
5289
+
5290
+ var hasRequiredReceivebuffer;
5291
+
5292
+ function requireReceivebuffer () {
5293
+ if (hasRequiredReceivebuffer) return receivebuffer;
5294
+ hasRequiredReceivebuffer = 1;
5295
+ Object.defineProperty(receivebuffer, "__esModule", { value: true });
5296
+ receivebuffer.ReceiveBuffer = void 0;
5297
+ class ReceiveBuffer {
5298
+ constructor(size = 4096) {
5299
+ this.buffer = Buffer.allocUnsafe(size);
5300
+ this.offset = 0;
5301
+ this.originalSize = size;
5302
+ }
5303
+ get length() {
5304
+ return this.offset;
5305
+ }
5306
+ append(data) {
5307
+ if (!Buffer.isBuffer(data)) {
5308
+ throw new Error('Attempted to append a non-buffer instance to ReceiveBuffer.');
5309
+ }
5310
+ if (this.offset + data.length >= this.buffer.length) {
5311
+ const tmp = this.buffer;
5312
+ this.buffer = Buffer.allocUnsafe(Math.max(this.buffer.length + this.originalSize, this.buffer.length + data.length));
5313
+ tmp.copy(this.buffer);
5314
+ }
5315
+ data.copy(this.buffer, this.offset);
5316
+ return (this.offset += data.length);
5317
+ }
5318
+ peek(length) {
5319
+ if (length > this.offset) {
5320
+ throw new Error('Attempted to read beyond the bounds of the managed internal data.');
5321
+ }
5322
+ return this.buffer.slice(0, length);
5323
+ }
5324
+ get(length) {
5325
+ if (length > this.offset) {
5326
+ throw new Error('Attempted to read beyond the bounds of the managed internal data.');
5327
+ }
5328
+ const value = Buffer.allocUnsafe(length);
5329
+ this.buffer.slice(0, length).copy(value);
5330
+ this.buffer.copyWithin(0, length, length + this.offset - length);
5331
+ this.offset -= length;
5332
+ return value;
5333
+ }
5334
+ }
5335
+ receivebuffer.ReceiveBuffer = ReceiveBuffer;
5336
+
5337
+ return receivebuffer;
5338
+ }
5339
+
5340
+ var hasRequiredSocksclient;
5341
+
5342
+ function requireSocksclient () {
5343
+ if (hasRequiredSocksclient) return socksclient;
5344
+ hasRequiredSocksclient = 1;
5345
+ (function (exports$1) {
5346
+ var __awaiter = (socksclient && socksclient.__awaiter) || function (thisArg, _arguments, P, generator) {
5347
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5348
+ return new (P || (P = Promise))(function (resolve, reject) {
5349
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5350
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
5351
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
5352
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
5353
+ });
5354
+ };
5355
+ Object.defineProperty(exports$1, "__esModule", { value: true });
5356
+ exports$1.SocksClientError = exports$1.SocksClient = void 0;
5357
+ const events_1 = require$$0;
5358
+ const net$1 = net;
5359
+ const smart_buffer_1 = requireSmartbuffer();
5360
+ const constants_1 = requireConstants$2();
5361
+ const helpers_1 = requireHelpers();
5362
+ const receivebuffer_1 = requireReceivebuffer();
5363
+ const util_1 = requireUtil();
5364
+ Object.defineProperty(exports$1, "SocksClientError", { enumerable: true, get: function () { return util_1.SocksClientError; } });
5365
+ const ip_address_1 = requireIpAddress();
5366
+ class SocksClient extends events_1.EventEmitter {
5367
+ constructor(options) {
5368
+ super();
5369
+ this.options = Object.assign({}, options);
5370
+ // Validate SocksClientOptions
5371
+ (0, helpers_1.validateSocksClientOptions)(options);
5372
+ // Default state
5373
+ this.setState(constants_1.SocksClientState.Created);
5374
+ }
5375
+ /**
5376
+ * Creates a new SOCKS connection.
5377
+ *
5378
+ * Note: Supports callbacks and promises. Only supports the connect command.
5379
+ * @param options { SocksClientOptions } Options.
5380
+ * @param callback { Function } An optional callback function.
5381
+ * @returns { Promise }
5382
+ */
5383
+ static createConnection(options, callback) {
5384
+ return new Promise((resolve, reject) => {
5385
+ // Validate SocksClientOptions
5386
+ try {
5387
+ (0, helpers_1.validateSocksClientOptions)(options, ['connect']);
5388
+ }
5389
+ catch (err) {
5390
+ if (typeof callback === 'function') {
5391
+ callback(err);
5392
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5393
+ return resolve(err); // Resolves pending promise (prevents memory leaks).
5394
+ }
5395
+ else {
5396
+ return reject(err);
5397
+ }
5398
+ }
5399
+ const client = new SocksClient(options);
5400
+ client.connect(options.existing_socket);
5401
+ client.once('established', (info) => {
5402
+ client.removeAllListeners();
5403
+ if (typeof callback === 'function') {
5404
+ callback(null, info);
5405
+ resolve(info); // Resolves pending promise (prevents memory leaks).
5406
+ }
5407
+ else {
5408
+ resolve(info);
5409
+ }
5410
+ });
5411
+ // Error occurred, failed to establish connection.
5412
+ client.once('error', (err) => {
5413
+ client.removeAllListeners();
5414
+ if (typeof callback === 'function') {
5415
+ callback(err);
5416
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5417
+ resolve(err); // Resolves pending promise (prevents memory leaks).
5418
+ }
5419
+ else {
5420
+ reject(err);
5421
+ }
5422
+ });
5423
+ });
5424
+ }
5425
+ /**
5426
+ * Creates a new SOCKS connection chain to a destination host through 2 or more SOCKS proxies.
5427
+ *
5428
+ * Note: Supports callbacks and promises. Only supports the connect method.
5429
+ * Note: Implemented via createConnection() factory function.
5430
+ * @param options { SocksClientChainOptions } Options
5431
+ * @param callback { Function } An optional callback function.
5432
+ * @returns { Promise }
5433
+ */
5434
+ static createConnectionChain(options, callback) {
5435
+ // eslint-disable-next-line no-async-promise-executor
5436
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
5437
+ // Validate SocksClientChainOptions
5438
+ try {
5439
+ (0, helpers_1.validateSocksClientChainOptions)(options);
5440
+ }
5441
+ catch (err) {
5442
+ if (typeof callback === 'function') {
5443
+ callback(err);
5444
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5445
+ return resolve(err); // Resolves pending promise (prevents memory leaks).
5446
+ }
5447
+ else {
5448
+ return reject(err);
5449
+ }
5450
+ }
5451
+ // Shuffle proxies
5452
+ if (options.randomizeChain) {
5453
+ (0, util_1.shuffleArray)(options.proxies);
5454
+ }
5455
+ try {
5456
+ let sock;
5457
+ for (let i = 0; i < options.proxies.length; i++) {
5458
+ const nextProxy = options.proxies[i];
5459
+ // If we've reached the last proxy in the chain, the destination is the actual destination, otherwise it's the next proxy.
5460
+ const nextDestination = i === options.proxies.length - 1
5461
+ ? options.destination
5462
+ : {
5463
+ host: options.proxies[i + 1].host ||
5464
+ options.proxies[i + 1].ipaddress,
5465
+ port: options.proxies[i + 1].port,
5466
+ };
5467
+ // Creates the next connection in the chain.
5468
+ const result = yield SocksClient.createConnection({
5469
+ command: 'connect',
5470
+ proxy: nextProxy,
5471
+ destination: nextDestination,
5472
+ existing_socket: sock,
5473
+ });
5474
+ // If sock is undefined, assign it here.
5475
+ sock = sock || result.socket;
5476
+ }
5477
+ if (typeof callback === 'function') {
5478
+ callback(null, { socket: sock });
5479
+ resolve({ socket: sock }); // Resolves pending promise (prevents memory leaks).
5480
+ }
5481
+ else {
5482
+ resolve({ socket: sock });
5483
+ }
5484
+ }
5485
+ catch (err) {
5486
+ if (typeof callback === 'function') {
5487
+ callback(err);
5488
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5489
+ resolve(err); // Resolves pending promise (prevents memory leaks).
5490
+ }
5491
+ else {
5492
+ reject(err);
5493
+ }
5494
+ }
5495
+ }));
5496
+ }
5497
+ /**
5498
+ * Creates a SOCKS UDP Frame.
5499
+ * @param options
5500
+ */
5501
+ static createUDPFrame(options) {
5502
+ const buff = new smart_buffer_1.SmartBuffer();
5503
+ buff.writeUInt16BE(0);
5504
+ buff.writeUInt8(options.frameNumber || 0);
5505
+ // IPv4/IPv6/Hostname
5506
+ if (net$1.isIPv4(options.remoteHost.host)) {
5507
+ buff.writeUInt8(constants_1.Socks5HostType.IPv4);
5508
+ buff.writeUInt32BE((0, helpers_1.ipv4ToInt32)(options.remoteHost.host));
5509
+ }
5510
+ else if (net$1.isIPv6(options.remoteHost.host)) {
5511
+ buff.writeUInt8(constants_1.Socks5HostType.IPv6);
5512
+ buff.writeBuffer((0, helpers_1.ipToBuffer)(options.remoteHost.host));
5513
+ }
5514
+ else {
5515
+ buff.writeUInt8(constants_1.Socks5HostType.Hostname);
5516
+ buff.writeUInt8(Buffer.byteLength(options.remoteHost.host));
5517
+ buff.writeString(options.remoteHost.host);
5518
+ }
5519
+ // Port
5520
+ buff.writeUInt16BE(options.remoteHost.port);
5521
+ // Data
5522
+ buff.writeBuffer(options.data);
5523
+ return buff.toBuffer();
5524
+ }
5525
+ /**
5526
+ * Parses a SOCKS UDP frame.
5527
+ * @param data
5528
+ */
5529
+ static parseUDPFrame(data) {
5530
+ const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
5531
+ buff.readOffset = 2;
5532
+ const frameNumber = buff.readUInt8();
5533
+ const hostType = buff.readUInt8();
5534
+ let remoteHost;
5535
+ if (hostType === constants_1.Socks5HostType.IPv4) {
5536
+ remoteHost = (0, helpers_1.int32ToIpv4)(buff.readUInt32BE());
5537
+ }
5538
+ else if (hostType === constants_1.Socks5HostType.IPv6) {
5539
+ remoteHost = ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm();
5540
+ }
5541
+ else {
5542
+ remoteHost = buff.readString(buff.readUInt8());
5543
+ }
5544
+ const remotePort = buff.readUInt16BE();
5545
+ return {
5546
+ frameNumber,
5547
+ remoteHost: {
5548
+ host: remoteHost,
5549
+ port: remotePort,
5550
+ },
5551
+ data: buff.readBuffer(),
5552
+ };
5553
+ }
5554
+ /**
5555
+ * Internal state setter. If the SocksClient is in an error state, it cannot be changed to a non error state.
5556
+ */
5557
+ setState(newState) {
5558
+ if (this.state !== constants_1.SocksClientState.Error) {
5559
+ this.state = newState;
5560
+ }
5561
+ }
5562
+ /**
5563
+ * Starts the connection establishment to the proxy and destination.
5564
+ * @param existingSocket Connected socket to use instead of creating a new one (internal use).
5565
+ */
5566
+ connect(existingSocket) {
5567
+ this.onDataReceived = (data) => this.onDataReceivedHandler(data);
5568
+ this.onClose = () => this.onCloseHandler();
5569
+ this.onError = (err) => this.onErrorHandler(err);
5570
+ this.onConnect = () => this.onConnectHandler();
5571
+ // Start timeout timer (defaults to 30 seconds)
5572
+ const timer = setTimeout(() => this.onEstablishedTimeout(), this.options.timeout || constants_1.DEFAULT_TIMEOUT);
5573
+ // check whether unref is available as it differs from browser to NodeJS (#33)
5574
+ if (timer.unref && typeof timer.unref === 'function') {
5575
+ timer.unref();
5576
+ }
5577
+ // If an existing socket is provided, use it to negotiate SOCKS handshake. Otherwise create a new Socket.
5578
+ if (existingSocket) {
5579
+ this.socket = existingSocket;
5580
+ }
5581
+ else {
5582
+ this.socket = new net$1.Socket();
5583
+ }
5584
+ // Attach Socket error handlers.
5585
+ this.socket.once('close', this.onClose);
5586
+ this.socket.once('error', this.onError);
5587
+ this.socket.once('connect', this.onConnect);
5588
+ this.socket.on('data', this.onDataReceived);
5589
+ this.setState(constants_1.SocksClientState.Connecting);
5590
+ this.receiveBuffer = new receivebuffer_1.ReceiveBuffer();
5591
+ if (existingSocket) {
5592
+ this.socket.emit('connect');
5593
+ }
5594
+ else {
5595
+ this.socket.connect(this.getSocketOptions());
5596
+ if (this.options.set_tcp_nodelay !== undefined &&
5597
+ this.options.set_tcp_nodelay !== null) {
5598
+ this.socket.setNoDelay(!!this.options.set_tcp_nodelay);
5599
+ }
5600
+ }
5601
+ // Listen for established event so we can re-emit any excess data received during handshakes.
5602
+ this.prependOnceListener('established', (info) => {
5603
+ setImmediate(() => {
5604
+ if (this.receiveBuffer.length > 0) {
5605
+ const excessData = this.receiveBuffer.get(this.receiveBuffer.length);
5606
+ info.socket.emit('data', excessData);
5607
+ }
5608
+ info.socket.resume();
5609
+ });
5610
+ });
5611
+ }
5612
+ // Socket options (defaults host/port to options.proxy.host/options.proxy.port)
5613
+ getSocketOptions() {
5614
+ return Object.assign(Object.assign({}, this.options.socket_options), { host: this.options.proxy.host || this.options.proxy.ipaddress, port: this.options.proxy.port });
5615
+ }
5616
+ /**
5617
+ * Handles internal Socks timeout callback.
5618
+ * Note: If the Socks client is not BoundWaitingForConnection or Established, the connection will be closed.
5619
+ */
5620
+ onEstablishedTimeout() {
5621
+ if (this.state !== constants_1.SocksClientState.Established &&
5622
+ this.state !== constants_1.SocksClientState.BoundWaitingForConnection) {
5623
+ this.closeSocket(constants_1.ERRORS.ProxyConnectionTimedOut);
5624
+ }
5625
+ }
5626
+ /**
5627
+ * Handles Socket connect event.
5628
+ */
5629
+ onConnectHandler() {
5630
+ this.setState(constants_1.SocksClientState.Connected);
5631
+ // Send initial handshake.
5632
+ if (this.options.proxy.type === 4) {
5633
+ this.sendSocks4InitialHandshake();
5634
+ }
5635
+ else {
5636
+ this.sendSocks5InitialHandshake();
5637
+ }
5638
+ this.setState(constants_1.SocksClientState.SentInitialHandshake);
5639
+ }
5640
+ /**
5641
+ * Handles Socket data event.
5642
+ * @param data
5643
+ */
5644
+ onDataReceivedHandler(data) {
5645
+ /*
5646
+ All received data is appended to a ReceiveBuffer.
5647
+ This makes sure that all the data we need is received before we attempt to process it.
5648
+ */
5649
+ this.receiveBuffer.append(data);
5650
+ // Process data that we have.
5651
+ this.processData();
5652
+ }
5653
+ /**
5654
+ * Handles processing of the data we have received.
5655
+ */
5656
+ processData() {
5657
+ // If we have enough data to process the next step in the SOCKS handshake, proceed.
5658
+ while (this.state !== constants_1.SocksClientState.Established &&
5659
+ this.state !== constants_1.SocksClientState.Error &&
5660
+ this.receiveBuffer.length >= this.nextRequiredPacketBufferSize) {
5661
+ // Sent initial handshake, waiting for response.
5662
+ if (this.state === constants_1.SocksClientState.SentInitialHandshake) {
5663
+ if (this.options.proxy.type === 4) {
5664
+ // Socks v4 only has one handshake response.
5665
+ this.handleSocks4FinalHandshakeResponse();
5666
+ }
5667
+ else {
5668
+ // Socks v5 has two handshakes, handle initial one here.
5669
+ this.handleInitialSocks5HandshakeResponse();
5670
+ }
5671
+ // Sent auth request for Socks v5, waiting for response.
5672
+ }
5673
+ else if (this.state === constants_1.SocksClientState.SentAuthentication) {
5674
+ this.handleInitialSocks5AuthenticationHandshakeResponse();
5675
+ // Sent final Socks v5 handshake, waiting for final response.
5676
+ }
5677
+ else if (this.state === constants_1.SocksClientState.SentFinalHandshake) {
5678
+ this.handleSocks5FinalHandshakeResponse();
5679
+ // Socks BIND established. Waiting for remote connection via proxy.
5680
+ }
5681
+ else if (this.state === constants_1.SocksClientState.BoundWaitingForConnection) {
5682
+ if (this.options.proxy.type === 4) {
5683
+ this.handleSocks4IncomingConnectionResponse();
5684
+ }
5685
+ else {
5686
+ this.handleSocks5IncomingConnectionResponse();
5687
+ }
5688
+ }
5689
+ else {
5690
+ this.closeSocket(constants_1.ERRORS.InternalError);
5691
+ break;
5692
+ }
5693
+ }
5694
+ }
5695
+ /**
5696
+ * Handles Socket close event.
5697
+ * @param had_error
5698
+ */
5699
+ onCloseHandler() {
5700
+ this.closeSocket(constants_1.ERRORS.SocketClosed);
5701
+ }
5702
+ /**
5703
+ * Handles Socket error event.
5704
+ * @param err
5705
+ */
5706
+ onErrorHandler(err) {
5707
+ this.closeSocket(err.message);
5708
+ }
5709
+ /**
5710
+ * Removes internal event listeners on the underlying Socket.
5711
+ */
5712
+ removeInternalSocketHandlers() {
5713
+ // Pauses data flow of the socket (this is internally resumed after 'established' is emitted)
5714
+ this.socket.pause();
5715
+ this.socket.removeListener('data', this.onDataReceived);
5716
+ this.socket.removeListener('close', this.onClose);
5717
+ this.socket.removeListener('error', this.onError);
5718
+ this.socket.removeListener('connect', this.onConnect);
5719
+ }
5720
+ /**
5721
+ * Closes and destroys the underlying Socket. Emits an error event.
5722
+ * @param err { String } An error string to include in error event.
5723
+ */
5724
+ closeSocket(err) {
5725
+ // Make sure only one 'error' event is fired for the lifetime of this SocksClient instance.
5726
+ if (this.state !== constants_1.SocksClientState.Error) {
5727
+ // Set internal state to Error.
5728
+ this.setState(constants_1.SocksClientState.Error);
5729
+ // Destroy Socket
5730
+ this.socket.destroy();
5731
+ // Remove internal listeners
5732
+ this.removeInternalSocketHandlers();
5733
+ // Fire 'error' event.
5734
+ this.emit('error', new util_1.SocksClientError(err, this.options));
5735
+ }
5736
+ }
5737
+ /**
5738
+ * Sends initial Socks v4 handshake request.
5739
+ */
5740
+ sendSocks4InitialHandshake() {
5741
+ const userId = this.options.proxy.userId || '';
5742
+ const buff = new smart_buffer_1.SmartBuffer();
5743
+ buff.writeUInt8(0x04);
5744
+ buff.writeUInt8(constants_1.SocksCommand[this.options.command]);
5745
+ buff.writeUInt16BE(this.options.destination.port);
5746
+ // Socks 4 (IPv4)
5747
+ if (net$1.isIPv4(this.options.destination.host)) {
5748
+ buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
5749
+ buff.writeStringNT(userId);
5750
+ // Socks 4a (hostname)
5751
+ }
5752
+ else {
5753
+ buff.writeUInt8(0x00);
5754
+ buff.writeUInt8(0x00);
5755
+ buff.writeUInt8(0x00);
5756
+ buff.writeUInt8(0x01);
5757
+ buff.writeStringNT(userId);
5758
+ buff.writeStringNT(this.options.destination.host);
5759
+ }
5760
+ this.nextRequiredPacketBufferSize =
5761
+ constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks4Response;
5762
+ this.socket.write(buff.toBuffer());
5763
+ }
5764
+ /**
5765
+ * Handles Socks v4 handshake response.
5766
+ * @param data
5767
+ */
5768
+ handleSocks4FinalHandshakeResponse() {
5769
+ const data = this.receiveBuffer.get(8);
5770
+ if (data[1] !== constants_1.Socks4Response.Granted) {
5771
+ this.closeSocket(`${constants_1.ERRORS.Socks4ProxyRejectedConnection} - (${constants_1.Socks4Response[data[1]]})`);
5772
+ }
5773
+ else {
5774
+ // Bind response
5775
+ if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.bind) {
5776
+ const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
5777
+ buff.readOffset = 2;
5778
+ const remoteHost = {
5779
+ port: buff.readUInt16BE(),
5780
+ host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
5781
+ };
5782
+ // If host is 0.0.0.0, set to proxy host.
5783
+ if (remoteHost.host === '0.0.0.0') {
5784
+ remoteHost.host = this.options.proxy.ipaddress;
5785
+ }
5786
+ this.setState(constants_1.SocksClientState.BoundWaitingForConnection);
5787
+ this.emit('bound', { remoteHost, socket: this.socket });
5788
+ // Connect response
5789
+ }
5790
+ else {
5791
+ this.setState(constants_1.SocksClientState.Established);
5792
+ this.removeInternalSocketHandlers();
5793
+ this.emit('established', { socket: this.socket });
5794
+ }
5795
+ }
5796
+ }
5797
+ /**
5798
+ * Handles Socks v4 incoming connection request (BIND)
5799
+ * @param data
5800
+ */
5801
+ handleSocks4IncomingConnectionResponse() {
5802
+ const data = this.receiveBuffer.get(8);
5803
+ if (data[1] !== constants_1.Socks4Response.Granted) {
5804
+ this.closeSocket(`${constants_1.ERRORS.Socks4ProxyRejectedIncomingBoundConnection} - (${constants_1.Socks4Response[data[1]]})`);
5805
+ }
5806
+ else {
5807
+ const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
5808
+ buff.readOffset = 2;
5809
+ const remoteHost = {
5810
+ port: buff.readUInt16BE(),
5811
+ host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
5812
+ };
5813
+ this.setState(constants_1.SocksClientState.Established);
5814
+ this.removeInternalSocketHandlers();
5815
+ this.emit('established', { remoteHost, socket: this.socket });
5816
+ }
5817
+ }
5818
+ /**
5819
+ * Sends initial Socks v5 handshake request.
5820
+ */
5821
+ sendSocks5InitialHandshake() {
5822
+ const buff = new smart_buffer_1.SmartBuffer();
5823
+ // By default we always support no auth.
5824
+ const supportedAuthMethods = [constants_1.Socks5Auth.NoAuth];
5825
+ // We should only tell the proxy we support user/pass auth if auth info is actually provided.
5826
+ // Note: As of Tor v0.3.5.7+, if user/pass auth is an option from the client, by default it will always take priority.
5827
+ if (this.options.proxy.userId || this.options.proxy.password) {
5828
+ supportedAuthMethods.push(constants_1.Socks5Auth.UserPass);
5829
+ }
5830
+ // Custom auth method?
5831
+ if (this.options.proxy.custom_auth_method !== undefined) {
5832
+ supportedAuthMethods.push(this.options.proxy.custom_auth_method);
5833
+ }
5834
+ // Build handshake packet
5835
+ buff.writeUInt8(0x05);
5836
+ buff.writeUInt8(supportedAuthMethods.length);
5837
+ for (const authMethod of supportedAuthMethods) {
5838
+ buff.writeUInt8(authMethod);
5839
+ }
5840
+ this.nextRequiredPacketBufferSize =
5841
+ constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5InitialHandshakeResponse;
5842
+ this.socket.write(buff.toBuffer());
5843
+ this.setState(constants_1.SocksClientState.SentInitialHandshake);
5844
+ }
5845
+ /**
5846
+ * Handles initial Socks v5 handshake response.
5847
+ * @param data
5848
+ */
5849
+ handleInitialSocks5HandshakeResponse() {
5850
+ const data = this.receiveBuffer.get(2);
5851
+ if (data[0] !== 0x05) {
5852
+ this.closeSocket(constants_1.ERRORS.InvalidSocks5IntiailHandshakeSocksVersion);
5853
+ }
5854
+ else if (data[1] === constants_1.SOCKS5_NO_ACCEPTABLE_AUTH) {
5855
+ this.closeSocket(constants_1.ERRORS.InvalidSocks5InitialHandshakeNoAcceptedAuthType);
5856
+ }
5857
+ else {
5858
+ // If selected Socks v5 auth method is no auth, send final handshake request.
5859
+ if (data[1] === constants_1.Socks5Auth.NoAuth) {
5860
+ this.socks5ChosenAuthType = constants_1.Socks5Auth.NoAuth;
5861
+ this.sendSocks5CommandRequest();
5862
+ // If selected Socks v5 auth method is user/password, send auth handshake.
5863
+ }
5864
+ else if (data[1] === constants_1.Socks5Auth.UserPass) {
5865
+ this.socks5ChosenAuthType = constants_1.Socks5Auth.UserPass;
5866
+ this.sendSocks5UserPassAuthentication();
5867
+ // If selected Socks v5 auth method is the custom_auth_method, send custom handshake.
5868
+ }
5869
+ else if (data[1] === this.options.proxy.custom_auth_method) {
5870
+ this.socks5ChosenAuthType = this.options.proxy.custom_auth_method;
5871
+ this.sendSocks5CustomAuthentication();
5872
+ }
5873
+ else {
5874
+ this.closeSocket(constants_1.ERRORS.InvalidSocks5InitialHandshakeUnknownAuthType);
5875
+ }
5876
+ }
5877
+ }
5878
+ /**
5879
+ * Sends Socks v5 user & password auth handshake.
5880
+ *
5881
+ * Note: No auth and user/pass are currently supported.
5882
+ */
5883
+ sendSocks5UserPassAuthentication() {
5884
+ const userId = this.options.proxy.userId || '';
5885
+ const password = this.options.proxy.password || '';
5886
+ const buff = new smart_buffer_1.SmartBuffer();
5887
+ buff.writeUInt8(0x01);
5888
+ buff.writeUInt8(Buffer.byteLength(userId));
5889
+ buff.writeString(userId);
5890
+ buff.writeUInt8(Buffer.byteLength(password));
5891
+ buff.writeString(password);
5892
+ this.nextRequiredPacketBufferSize =
5893
+ constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5UserPassAuthenticationResponse;
5894
+ this.socket.write(buff.toBuffer());
5895
+ this.setState(constants_1.SocksClientState.SentAuthentication);
5896
+ }
5897
+ sendSocks5CustomAuthentication() {
5898
+ return __awaiter(this, void 0, void 0, function* () {
5899
+ this.nextRequiredPacketBufferSize =
5900
+ this.options.proxy.custom_auth_response_size;
5901
+ this.socket.write(yield this.options.proxy.custom_auth_request_handler());
5902
+ this.setState(constants_1.SocksClientState.SentAuthentication);
5903
+ });
5904
+ }
5905
+ handleSocks5CustomAuthHandshakeResponse(data) {
5906
+ return __awaiter(this, void 0, void 0, function* () {
5907
+ return yield this.options.proxy.custom_auth_response_handler(data);
5908
+ });
5909
+ }
5910
+ handleSocks5AuthenticationNoAuthHandshakeResponse(data) {
5911
+ return __awaiter(this, void 0, void 0, function* () {
5912
+ return data[1] === 0x00;
5913
+ });
5914
+ }
5915
+ handleSocks5AuthenticationUserPassHandshakeResponse(data) {
5916
+ return __awaiter(this, void 0, void 0, function* () {
5917
+ return data[1] === 0x00;
5918
+ });
5919
+ }
5920
+ /**
5921
+ * Handles Socks v5 auth handshake response.
5922
+ * @param data
5923
+ */
5924
+ handleInitialSocks5AuthenticationHandshakeResponse() {
5925
+ return __awaiter(this, void 0, void 0, function* () {
5926
+ this.setState(constants_1.SocksClientState.ReceivedAuthenticationResponse);
5927
+ let authResult = false;
5928
+ if (this.socks5ChosenAuthType === constants_1.Socks5Auth.NoAuth) {
5929
+ authResult = yield this.handleSocks5AuthenticationNoAuthHandshakeResponse(this.receiveBuffer.get(2));
5930
+ }
5931
+ else if (this.socks5ChosenAuthType === constants_1.Socks5Auth.UserPass) {
5932
+ authResult =
5933
+ yield this.handleSocks5AuthenticationUserPassHandshakeResponse(this.receiveBuffer.get(2));
5934
+ }
5935
+ else if (this.socks5ChosenAuthType === this.options.proxy.custom_auth_method) {
5936
+ authResult = yield this.handleSocks5CustomAuthHandshakeResponse(this.receiveBuffer.get(this.options.proxy.custom_auth_response_size));
5937
+ }
5938
+ if (!authResult) {
5939
+ this.closeSocket(constants_1.ERRORS.Socks5AuthenticationFailed);
5940
+ }
5941
+ else {
5942
+ this.sendSocks5CommandRequest();
5943
+ }
5944
+ });
5945
+ }
5946
+ /**
5947
+ * Sends Socks v5 final handshake request.
5948
+ */
5949
+ sendSocks5CommandRequest() {
5950
+ const buff = new smart_buffer_1.SmartBuffer();
5951
+ buff.writeUInt8(0x05);
5952
+ buff.writeUInt8(constants_1.SocksCommand[this.options.command]);
5953
+ buff.writeUInt8(0x00);
5954
+ // ipv4, ipv6, domain?
5955
+ if (net$1.isIPv4(this.options.destination.host)) {
5956
+ buff.writeUInt8(constants_1.Socks5HostType.IPv4);
5957
+ buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
5958
+ }
5959
+ else if (net$1.isIPv6(this.options.destination.host)) {
5960
+ buff.writeUInt8(constants_1.Socks5HostType.IPv6);
5961
+ buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
5962
+ }
5963
+ else {
5964
+ buff.writeUInt8(constants_1.Socks5HostType.Hostname);
5965
+ buff.writeUInt8(this.options.destination.host.length);
5966
+ buff.writeString(this.options.destination.host);
5967
+ }
5968
+ buff.writeUInt16BE(this.options.destination.port);
5969
+ this.nextRequiredPacketBufferSize =
5970
+ constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
5971
+ this.socket.write(buff.toBuffer());
5972
+ this.setState(constants_1.SocksClientState.SentFinalHandshake);
5973
+ }
5974
+ /**
5975
+ * Handles Socks v5 final handshake response.
5976
+ * @param data
5977
+ */
5978
+ handleSocks5FinalHandshakeResponse() {
5979
+ // Peek at available data (we need at least 5 bytes to get the hostname length)
5980
+ const header = this.receiveBuffer.peek(5);
5981
+ if (header[0] !== 0x05 || header[1] !== constants_1.Socks5Response.Granted) {
5982
+ this.closeSocket(`${constants_1.ERRORS.InvalidSocks5FinalHandshakeRejected} - ${constants_1.Socks5Response[header[1]]}`);
5983
+ }
5984
+ else {
5985
+ // Read address type
5986
+ const addressType = header[3];
5987
+ let remoteHost;
5988
+ let buff;
5989
+ // IPv4
5990
+ if (addressType === constants_1.Socks5HostType.IPv4) {
5991
+ // Check if data is available.
5992
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
5993
+ if (this.receiveBuffer.length < dataNeeded) {
5994
+ this.nextRequiredPacketBufferSize = dataNeeded;
5995
+ return;
5996
+ }
5997
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
5998
+ remoteHost = {
5999
+ host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
6000
+ port: buff.readUInt16BE(),
6001
+ };
6002
+ // If given host is 0.0.0.0, assume remote proxy ip instead.
6003
+ if (remoteHost.host === '0.0.0.0') {
6004
+ remoteHost.host = this.options.proxy.ipaddress;
6005
+ }
6006
+ // Hostname
6007
+ }
6008
+ else if (addressType === constants_1.Socks5HostType.Hostname) {
6009
+ const hostLength = header[4];
6010
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(hostLength); // header + host length + host + port
6011
+ // Check if data is available.
6012
+ if (this.receiveBuffer.length < dataNeeded) {
6013
+ this.nextRequiredPacketBufferSize = dataNeeded;
6014
+ return;
6015
+ }
6016
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(5));
6017
+ remoteHost = {
6018
+ host: buff.readString(hostLength),
6019
+ port: buff.readUInt16BE(),
6020
+ };
6021
+ // IPv6
6022
+ }
6023
+ else if (addressType === constants_1.Socks5HostType.IPv6) {
6024
+ // Check if data is available.
6025
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
6026
+ if (this.receiveBuffer.length < dataNeeded) {
6027
+ this.nextRequiredPacketBufferSize = dataNeeded;
6028
+ return;
6029
+ }
6030
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
6031
+ remoteHost = {
6032
+ host: ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm(),
6033
+ port: buff.readUInt16BE(),
6034
+ };
6035
+ }
6036
+ // We have everything we need
6037
+ this.setState(constants_1.SocksClientState.ReceivedFinalResponse);
6038
+ // If using CONNECT, the client is now in the established state.
6039
+ if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.connect) {
6040
+ this.setState(constants_1.SocksClientState.Established);
6041
+ this.removeInternalSocketHandlers();
6042
+ this.emit('established', { remoteHost, socket: this.socket });
6043
+ }
6044
+ else if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.bind) {
6045
+ /* If using BIND, the Socks client is now in BoundWaitingForConnection state.
6046
+ This means that the remote proxy server is waiting for a remote connection to the bound port. */
6047
+ this.setState(constants_1.SocksClientState.BoundWaitingForConnection);
6048
+ this.nextRequiredPacketBufferSize =
6049
+ constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
6050
+ this.emit('bound', { remoteHost, socket: this.socket });
6051
+ /*
6052
+ If using Associate, the Socks client is now Established. And the proxy server is now accepting UDP packets at the
6053
+ given bound port. This initial Socks TCP connection must remain open for the UDP relay to continue to work.
6054
+ */
6055
+ }
6056
+ else if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.associate) {
6057
+ this.setState(constants_1.SocksClientState.Established);
6058
+ this.removeInternalSocketHandlers();
6059
+ this.emit('established', {
6060
+ remoteHost,
6061
+ socket: this.socket,
6062
+ });
6063
+ }
6064
+ }
6065
+ }
6066
+ /**
6067
+ * Handles Socks v5 incoming connection request (BIND).
6068
+ */
6069
+ handleSocks5IncomingConnectionResponse() {
6070
+ // Peek at available data (we need at least 5 bytes to get the hostname length)
6071
+ const header = this.receiveBuffer.peek(5);
6072
+ if (header[0] !== 0x05 || header[1] !== constants_1.Socks5Response.Granted) {
6073
+ this.closeSocket(`${constants_1.ERRORS.Socks5ProxyRejectedIncomingBoundConnection} - ${constants_1.Socks5Response[header[1]]}`);
6074
+ }
6075
+ else {
6076
+ // Read address type
6077
+ const addressType = header[3];
6078
+ let remoteHost;
6079
+ let buff;
6080
+ // IPv4
6081
+ if (addressType === constants_1.Socks5HostType.IPv4) {
6082
+ // Check if data is available.
6083
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
6084
+ if (this.receiveBuffer.length < dataNeeded) {
6085
+ this.nextRequiredPacketBufferSize = dataNeeded;
6086
+ return;
6087
+ }
6088
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
6089
+ remoteHost = {
6090
+ host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
6091
+ port: buff.readUInt16BE(),
6092
+ };
6093
+ // If given host is 0.0.0.0, assume remote proxy ip instead.
6094
+ if (remoteHost.host === '0.0.0.0') {
6095
+ remoteHost.host = this.options.proxy.ipaddress;
6096
+ }
6097
+ // Hostname
6098
+ }
6099
+ else if (addressType === constants_1.Socks5HostType.Hostname) {
6100
+ const hostLength = header[4];
6101
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(hostLength); // header + host length + port
6102
+ // Check if data is available.
6103
+ if (this.receiveBuffer.length < dataNeeded) {
6104
+ this.nextRequiredPacketBufferSize = dataNeeded;
6105
+ return;
6106
+ }
6107
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(5));
6108
+ remoteHost = {
6109
+ host: buff.readString(hostLength),
6110
+ port: buff.readUInt16BE(),
6111
+ };
6112
+ // IPv6
6113
+ }
6114
+ else if (addressType === constants_1.Socks5HostType.IPv6) {
6115
+ // Check if data is available.
6116
+ const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
6117
+ if (this.receiveBuffer.length < dataNeeded) {
6118
+ this.nextRequiredPacketBufferSize = dataNeeded;
6119
+ return;
6120
+ }
6121
+ buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
6122
+ remoteHost = {
6123
+ host: ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm(),
6124
+ port: buff.readUInt16BE(),
6125
+ };
6126
+ }
6127
+ this.setState(constants_1.SocksClientState.Established);
6128
+ this.removeInternalSocketHandlers();
6129
+ this.emit('established', { remoteHost, socket: this.socket });
6130
+ }
6131
+ }
6132
+ get socksClientOptions() {
6133
+ return Object.assign({}, this.options);
6134
+ }
6135
+ }
6136
+ exports$1.SocksClient = SocksClient;
6137
+
6138
+ } (socksclient));
6139
+ return socksclient;
6140
+ }
6141
+
6142
+ var hasRequiredBuild;
6143
+
6144
+ function requireBuild () {
6145
+ if (hasRequiredBuild) return build$1;
6146
+ hasRequiredBuild = 1;
6147
+ (function (exports$1) {
6148
+ var __createBinding = (build$1 && build$1.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6149
+ if (k2 === undefined) k2 = k;
6150
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6151
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6152
+ desc = { enumerable: true, get: function() { return m[k]; } };
6153
+ }
6154
+ Object.defineProperty(o, k2, desc);
6155
+ }) : (function(o, m, k, k2) {
6156
+ if (k2 === undefined) k2 = k;
6157
+ o[k2] = m[k];
6158
+ }));
6159
+ var __exportStar = (build$1 && build$1.__exportStar) || function(m, exports$1) {
6160
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$1, p)) __createBinding(exports$1, m, p);
6161
+ };
6162
+ Object.defineProperty(exports$1, "__esModule", { value: true });
6163
+ __exportStar(requireSocksclient(), exports$1);
6164
+
6165
+ } (build$1));
6166
+ return build$1;
6167
+ }
6168
+
6169
+ var buildExports = requireBuild();
6170
+
6171
+ const INTERNAL = Symbol('AgentBaseInternalState');
6172
+ class Agent extends http__namespace.Agent {
6173
+ constructor(opts) {
6174
+ super(opts);
6175
+ this[INTERNAL] = {};
6176
+ }
6177
+ /**
6178
+ * Determine whether this is an `http` or `https` request.
6179
+ */
6180
+ isSecureEndpoint(options) {
6181
+ if (options) {
6182
+ // First check the `secureEndpoint` property explicitly, since this
6183
+ // means that a parent `Agent` is "passing through" to this instance.
6184
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6185
+ if (typeof options.secureEndpoint === 'boolean') {
6186
+ return options.secureEndpoint;
6187
+ }
6188
+ // If no explicit `secure` endpoint, check if `protocol` property is
6189
+ // set. This will usually be the case since using a full string URL
6190
+ // or `URL` instance should be the most common usage.
6191
+ if (typeof options.protocol === 'string') {
6192
+ return options.protocol === 'https:';
6193
+ }
6194
+ }
6195
+ // Finally, if no `protocol` property was set, then fall back to
6196
+ // checking the stack trace of the current call stack, and try to
6197
+ // detect the "https" module.
6198
+ const { stack } = new Error();
6199
+ if (typeof stack !== 'string')
6200
+ return false;
6201
+ return stack
6202
+ .split('\n')
6203
+ .some((l) => l.indexOf('(https.js:') !== -1 ||
6204
+ l.indexOf('node:https:') !== -1);
6205
+ }
6206
+ // In order to support async signatures in `connect()` and Node's native
6207
+ // connection pooling in `http.Agent`, the array of sockets for each origin
6208
+ // has to be updated synchronously. This is so the length of the array is
6209
+ // accurate when `addRequest()` is next called. We achieve this by creating a
6210
+ // fake socket and adding it to `sockets[origin]` and incrementing
6211
+ // `totalSocketCount`.
6212
+ incrementSockets(name) {
6213
+ // If `maxSockets` and `maxTotalSockets` are both Infinity then there is no
6214
+ // need to create a fake socket because Node.js native connection pooling
6215
+ // will never be invoked.
6216
+ if (this.maxSockets === Infinity && this.maxTotalSockets === Infinity) {
6217
+ return null;
6218
+ }
6219
+ // All instances of `sockets` are expected TypeScript errors. The
6220
+ // alternative is to add it as a private property of this class but that
6221
+ // will break TypeScript subclassing.
6222
+ if (!this.sockets[name]) {
6223
+ // @ts-expect-error `sockets` is readonly in `@types/node`
6224
+ this.sockets[name] = [];
6225
+ }
6226
+ const fakeSocket = new net__namespace.Socket({ writable: false });
6227
+ this.sockets[name].push(fakeSocket);
6228
+ // @ts-expect-error `totalSocketCount` isn't defined in `@types/node`
6229
+ this.totalSocketCount++;
6230
+ return fakeSocket;
6231
+ }
6232
+ decrementSockets(name, socket) {
6233
+ if (!this.sockets[name] || socket === null) {
6234
+ return;
6235
+ }
6236
+ const sockets = this.sockets[name];
6237
+ const index = sockets.indexOf(socket);
6238
+ if (index !== -1) {
6239
+ sockets.splice(index, 1);
6240
+ // @ts-expect-error `totalSocketCount` isn't defined in `@types/node`
6241
+ this.totalSocketCount--;
6242
+ if (sockets.length === 0) {
6243
+ // @ts-expect-error `sockets` is readonly in `@types/node`
6244
+ delete this.sockets[name];
6245
+ }
6246
+ }
6247
+ }
6248
+ // In order to properly update the socket pool, we need to call `getName()` on
6249
+ // the core `https.Agent` if it is a secureEndpoint.
6250
+ getName(options) {
6251
+ const secureEndpoint = this.isSecureEndpoint(options);
6252
+ if (secureEndpoint) {
6253
+ return https.Agent.prototype.getName.call(this, options);
6254
+ }
6255
+ return super.getName(options);
6256
+ }
6257
+ createSocket(req, options, cb) {
6258
+ const connectOpts = {
6259
+ ...options,
6260
+ secureEndpoint: this.isSecureEndpoint(options),
6261
+ };
6262
+ const name = this.getName(connectOpts);
6263
+ const fakeSocket = this.incrementSockets(name);
6264
+ Promise.resolve()
6265
+ .then(() => this.connect(req, connectOpts))
6266
+ .then((socket) => {
6267
+ this.decrementSockets(name, fakeSocket);
6268
+ if (socket instanceof http__namespace.Agent) {
6269
+ try {
6270
+ // @ts-expect-error `addRequest()` isn't defined in `@types/node`
6271
+ return socket.addRequest(req, connectOpts);
6272
+ }
6273
+ catch (err) {
6274
+ return cb(err);
6275
+ }
6276
+ }
6277
+ this[INTERNAL].currentSocket = socket;
6278
+ // @ts-expect-error `createSocket()` isn't defined in `@types/node`
6279
+ super.createSocket(req, options, cb);
6280
+ }, (err) => {
6281
+ this.decrementSockets(name, fakeSocket);
6282
+ cb(err);
6283
+ });
6284
+ }
6285
+ createConnection() {
6286
+ const socket = this[INTERNAL].currentSocket;
6287
+ this[INTERNAL].currentSocket = undefined;
6288
+ if (!socket) {
6289
+ throw new Error('No socket was returned in the `connect()` function');
6290
+ }
6291
+ return socket;
6292
+ }
6293
+ get defaultPort() {
6294
+ return (this[INTERNAL].defaultPort ??
6295
+ (this.protocol === 'https:' ? 443 : 80));
6296
+ }
6297
+ set defaultPort(v) {
6298
+ if (this[INTERNAL]) {
6299
+ this[INTERNAL].defaultPort = v;
6300
+ }
6301
+ }
6302
+ get protocol() {
6303
+ return (this[INTERNAL].protocol ??
6304
+ (this.isSecureEndpoint() ? 'https:' : 'http:'));
6305
+ }
6306
+ set protocol(v) {
6307
+ if (this[INTERNAL]) {
6308
+ this[INTERNAL].protocol = v;
6309
+ }
6310
+ }
6311
+ }
6312
+
6313
+ var srcExports = requireSrc$1();
6314
+ var createDebug = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
6315
+
6316
+ const debug = createDebug('socks-proxy-agent');
6317
+ const setServernameFromNonIpHost = (options) => {
6318
+ if (options.servername === undefined &&
6319
+ options.host &&
6320
+ !net__namespace.isIP(options.host)) {
6321
+ return {
6322
+ ...options,
6323
+ servername: options.host,
6324
+ };
6325
+ }
6326
+ return options;
6327
+ };
6328
+ function parseSocksURL(url) {
6329
+ let lookup = false;
6330
+ let type = 5;
6331
+ const host = url.hostname;
6332
+ // From RFC 1928, Section 3: https://tools.ietf.org/html/rfc1928#section-3
6333
+ // "The SOCKS service is conventionally located on TCP port 1080"
6334
+ const port = parseInt(url.port, 10) || 1080;
6335
+ // figure out if we want socks v4 or v5, based on the "protocol" used.
6336
+ // Defaults to 5.
6337
+ switch (url.protocol.replace(':', '')) {
6338
+ case 'socks4':
6339
+ lookup = true;
6340
+ type = 4;
6341
+ break;
6342
+ // pass through
6343
+ case 'socks4a':
6344
+ type = 4;
6345
+ break;
6346
+ case 'socks5':
6347
+ lookup = true;
6348
+ type = 5;
6349
+ break;
6350
+ // pass through
6351
+ case 'socks': // no version specified, default to 5h
6352
+ type = 5;
6353
+ break;
6354
+ case 'socks5h':
6355
+ type = 5;
6356
+ break;
6357
+ default:
6358
+ throw new TypeError(`A "socks" protocol must be specified! Got: ${String(url.protocol)}`);
6359
+ }
6360
+ const proxy = {
6361
+ host,
6362
+ port,
6363
+ type,
6364
+ };
6365
+ if (url.username) {
6366
+ Object.defineProperty(proxy, 'userId', {
6367
+ value: decodeURIComponent(url.username),
6368
+ enumerable: false,
6369
+ });
6370
+ }
6371
+ if (url.password != null) {
6372
+ Object.defineProperty(proxy, 'password', {
6373
+ value: decodeURIComponent(url.password),
6374
+ enumerable: false,
6375
+ });
6376
+ }
6377
+ return { lookup, proxy };
6378
+ }
6379
+ class SocksProxyAgent extends Agent {
6380
+ constructor(uri, opts) {
6381
+ super(opts);
6382
+ const url = typeof uri === 'string' ? new require$$2.URL(uri) : uri;
6383
+ const { proxy, lookup } = parseSocksURL(url);
6384
+ this.shouldLookup = lookup;
6385
+ this.proxy = proxy;
6386
+ this.timeout = opts?.timeout ?? null;
6387
+ this.socketOptions = opts?.socketOptions ?? null;
6388
+ }
6389
+ /**
6390
+ * Initiates a SOCKS connection to the specified SOCKS proxy server,
6391
+ * which in turn connects to the specified remote host and port.
6392
+ */
6393
+ async connect(req, opts) {
6394
+ const { shouldLookup, proxy, timeout } = this;
6395
+ if (!opts.host) {
6396
+ throw new Error('No `host` defined!');
6397
+ }
6398
+ let { host } = opts;
6399
+ const { port, lookup: lookupFn = dns__namespace.lookup } = opts;
6400
+ if (shouldLookup) {
6401
+ // Client-side DNS resolution for "4" and "5" socks proxy versions.
6402
+ host = await new Promise((resolve, reject) => {
6403
+ // Use the request's custom lookup, if one was configured:
6404
+ lookupFn(host, {}, (err, address) => {
6405
+ if (err) {
6406
+ reject(err);
6407
+ }
6408
+ else {
6409
+ resolve(typeof address === 'string'
6410
+ ? address
6411
+ : address[0].address);
6412
+ }
6413
+ });
6414
+ });
6415
+ }
6416
+ const socksOpts = {
6417
+ proxy,
6418
+ destination: {
6419
+ host,
6420
+ port: typeof port === 'number' ? port : parseInt(port, 10),
6421
+ },
6422
+ command: 'connect',
6423
+ timeout: timeout ?? undefined,
6424
+ // @ts-expect-error the type supplied by socks for socket_options is wider
6425
+ // than necessary since socks will always override the host and port
6426
+ socket_options: this.socketOptions ?? undefined,
6427
+ };
6428
+ const cleanup = (tlsSocket) => {
6429
+ req.destroy();
6430
+ socket.destroy();
6431
+ if (tlsSocket)
6432
+ tlsSocket.destroy();
6433
+ };
6434
+ debug('Creating socks proxy connection: %o', socksOpts);
6435
+ const { socket } = await buildExports.SocksClient.createConnection(socksOpts);
6436
+ debug('Successfully created socks proxy connection');
6437
+ if (timeout !== null) {
6438
+ socket.setTimeout(timeout);
6439
+ socket.on('timeout', () => cleanup());
6440
+ }
6441
+ if (opts.secureEndpoint) {
6442
+ // The proxy is connecting to a TLS server, so upgrade
6443
+ // this socket connection to a TLS connection.
6444
+ debug('Upgrading socket connection to TLS');
6445
+ const tlsSocket = require$$1__namespace.connect({
6446
+ ...omit(setServernameFromNonIpHost(opts), 'host', 'path', 'port'),
6447
+ socket,
6448
+ });
6449
+ tlsSocket.once('error', (error) => {
6450
+ debug('Socket TLS error', error.message);
6451
+ cleanup(tlsSocket);
6452
+ });
6453
+ return tlsSocket;
6454
+ }
6455
+ return socket;
6456
+ }
6457
+ }
6458
+ SocksProxyAgent.protocols = [
6459
+ 'socks',
6460
+ 'socks4',
6461
+ 'socks4a',
6462
+ 'socks5',
6463
+ 'socks5h',
6464
+ ];
6465
+ function omit(obj, ...keys) {
6466
+ const ret = {};
6467
+ let key;
6468
+ for (key in obj) {
6469
+ if (!keys.includes(key)) {
6470
+ ret[key] = obj[key];
6471
+ }
6472
+ }
6473
+ return ret;
6474
+ }
6475
+
6476
+ const CONFIG_PATH$1 = process.env.TTMGRC_PATH || path.join(os.homedir(), '.ttmgrc');
6477
+ const getTTMGRC = () => {
6478
+ // only check one time
6479
+ if (!fs.existsSync(CONFIG_PATH$1)) {
6480
+ return null;
6481
+ }
6482
+ else {
6483
+ // safe parse
6484
+ let res = JSON.parse(fs.readFileSync(CONFIG_PATH$1, 'utf8'));
6485
+ return res;
6486
+ }
6487
+ };
6488
+ const setTTMGRC = (config) => {
6489
+ // updata cache config
6490
+ config = config;
6491
+ const originConfig = getTTMGRC() || {};
6492
+ fs.writeFileSync(CONFIG_PATH$1, JSON.stringify({ ...originConfig, ...config }));
6493
+ };
6494
+ const resetTTMGRC = (config = {}) => {
6495
+ fs.writeFileSync(CONFIG_PATH$1, JSON.stringify(config));
6496
+ };
6497
+ const getCurrentUser = () => {
6498
+ try {
6499
+ const config = getTTMGRC();
6500
+ return config;
6501
+ }
6502
+ catch (err) {
6503
+ return null;
6504
+ }
6505
+ };
6506
+
6507
+ // ppe_dev_tool
6508
+ function getAxiosProxyConfig() {
6509
+ const config = getTTMGRC();
6510
+ const proxyUrl = config?.proxy;
6511
+ if (proxyUrl) {
6512
+ let agent;
6513
+ if (proxyUrl.startsWith('socks')) {
6514
+ agent = new SocksProxyAgent(proxyUrl);
6515
+ }
6516
+ else {
6517
+ agent = new distExports.HttpsProxyAgent(proxyUrl);
6518
+ }
1866
6519
  // 兼容 http/https 协议
1867
6520
  return {
1868
- httpsAgent: new distExports.HttpsProxyAgent(proxyUrl),
1869
- httpAgent: new distExports.HttpsProxyAgent(proxyUrl),
6521
+ httpsAgent: agent,
6522
+ httpAgent: agent,
1870
6523
  proxy: false,
1871
6524
  };
1872
6525
  }
@@ -3816,7 +8469,7 @@ function showTips(context) {
3816
8469
  const zipCwdToBuffer = (customIgnores = []) => {
3817
8470
  return new Promise((resolve, reject) => {
3818
8471
  const chunks = [];
3819
- const output = new stream.Writable({
8472
+ const output = new require$$2$1.Writable({
3820
8473
  write(chunk, encoding, next) {
3821
8474
  chunks.push(chunk);
3822
8475
  next();
@@ -5862,7 +10515,7 @@ const config = {
5862
10515
  }
5863
10516
  };
5864
10517
 
5865
- var version = "0.3.2-beta.8";
10518
+ var version = "0.3.2-beta.9";
5866
10519
  var pkg = {
5867
10520
  version: version};
5868
10521