@blotout/qinto-cli 0.1.16 → 0.1.17
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/README.md +13 -2
- package/dist/index.js +4188 -3802
- package/dist/index.js.map +1 -1
- package/dist/worker-artifact/app-runtime/index.js +89 -13
- package/dist/worker-artifact/main/index.js +2906 -1568
- package/dist/worker-artifact/proxy/index.js +22410 -0
- package/dist/worker-artifact/proxy-wfp/index.js +1311 -0
- package/dist/worker-artifact/version.json +40 -4
- package/package.json +6 -6
- package/templates/project/package.json +2 -1
- package/templates/project/tsconfig.json +1 -1
|
@@ -0,0 +1,1311 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
try {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
} catch (e) {
|
|
12
|
+
throw mod = 0, e;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
|
|
32
|
+
// ../../node_modules/.pnpm/ipaddr.js@2.5.0/node_modules/ipaddr.js/lib/ipaddr.js
|
|
33
|
+
var require_ipaddr = __commonJS({
|
|
34
|
+
"../../node_modules/.pnpm/ipaddr.js@2.5.0/node_modules/ipaddr.js/lib/ipaddr.js"(exports, module) {
|
|
35
|
+
(function(root) {
|
|
36
|
+
"use strict";
|
|
37
|
+
const ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
|
|
38
|
+
const ipv4Regexes = {
|
|
39
|
+
fourOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`, "i"),
|
|
40
|
+
threeOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}$`, "i"),
|
|
41
|
+
twoOctet: new RegExp(`^${ipv4Part}\\.${ipv4Part}$`, "i"),
|
|
42
|
+
longValue: new RegExp(`^${ipv4Part}$`, "i")
|
|
43
|
+
};
|
|
44
|
+
const octalRegex = new RegExp(`^0[0-7]+$`, "i");
|
|
45
|
+
const hexRegex = new RegExp(`^0x[a-f0-9]+$`, "i");
|
|
46
|
+
const zoneIndex = "%[0-9a-z]{1,}";
|
|
47
|
+
const ipv6Part = "(?:[0-9a-f]+::?)+";
|
|
48
|
+
const ipv6Regexes = {
|
|
49
|
+
zoneIndex: new RegExp(zoneIndex, "i"),
|
|
50
|
+
"native": new RegExp(`^(::)?(${ipv6Part})?([0-9a-f]+)?(::)?(${zoneIndex})?$`, "i"),
|
|
51
|
+
deprecatedTransitional: new RegExp(`^(?:::)(${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?)$`, "i"),
|
|
52
|
+
transitional: new RegExp(`^((?:${ipv6Part})|(?:::)(?:${ipv6Part})?)${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}\\.${ipv4Part}(${zoneIndex})?$`, "i")
|
|
53
|
+
};
|
|
54
|
+
function expandIPv6(string, parts) {
|
|
55
|
+
if (string.indexOf("::") !== string.lastIndexOf("::")) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
let colonCount = 0;
|
|
59
|
+
let lastColon = -1;
|
|
60
|
+
let zoneId = (string.match(ipv6Regexes.zoneIndex) || [])[0];
|
|
61
|
+
let replacement, replacementCount;
|
|
62
|
+
if (zoneId) {
|
|
63
|
+
zoneId = zoneId.substring(1);
|
|
64
|
+
string = string.replace(/%.+$/, "");
|
|
65
|
+
}
|
|
66
|
+
while ((lastColon = string.indexOf(":", lastColon + 1)) >= 0) {
|
|
67
|
+
colonCount++;
|
|
68
|
+
}
|
|
69
|
+
if (string.substr(0, 2) === "::") {
|
|
70
|
+
colonCount--;
|
|
71
|
+
}
|
|
72
|
+
if (string.substr(-2, 2) === "::") {
|
|
73
|
+
colonCount--;
|
|
74
|
+
}
|
|
75
|
+
if (colonCount >= parts) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
replacementCount = parts - colonCount;
|
|
79
|
+
replacement = ":";
|
|
80
|
+
while (replacementCount--) {
|
|
81
|
+
replacement += "0:";
|
|
82
|
+
}
|
|
83
|
+
string = string.replace("::", replacement);
|
|
84
|
+
if (string[0] === ":") {
|
|
85
|
+
string = string.slice(1);
|
|
86
|
+
}
|
|
87
|
+
if (string[string.length - 1] === ":") {
|
|
88
|
+
string = string.slice(0, -1);
|
|
89
|
+
}
|
|
90
|
+
parts = (function() {
|
|
91
|
+
const ref = string.split(":");
|
|
92
|
+
const results = [];
|
|
93
|
+
for (let i = 0; i < ref.length; i++) {
|
|
94
|
+
results.push(ref[i].length > 4 ? NaN : parseInt(ref[i], 16));
|
|
95
|
+
}
|
|
96
|
+
return results;
|
|
97
|
+
})();
|
|
98
|
+
return {
|
|
99
|
+
parts,
|
|
100
|
+
zoneId
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
__name(expandIPv6, "expandIPv6");
|
|
104
|
+
function matchCIDR(first, second, partSize, cidrBits) {
|
|
105
|
+
if (first.length !== second.length) {
|
|
106
|
+
throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
|
|
107
|
+
}
|
|
108
|
+
let part = 0;
|
|
109
|
+
let shift;
|
|
110
|
+
while (cidrBits > 0) {
|
|
111
|
+
shift = partSize - cidrBits;
|
|
112
|
+
if (shift < 0) {
|
|
113
|
+
shift = 0;
|
|
114
|
+
}
|
|
115
|
+
if (first[part] >> shift !== second[part] >> shift) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
cidrBits -= partSize;
|
|
119
|
+
part += 1;
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
__name(matchCIDR, "matchCIDR");
|
|
124
|
+
function parseIntAuto(string) {
|
|
125
|
+
if (hexRegex.test(string)) {
|
|
126
|
+
return parseInt(string, 16);
|
|
127
|
+
}
|
|
128
|
+
if (string[0] === "0" && !isNaN(parseInt(string[1], 10))) {
|
|
129
|
+
if (octalRegex.test(string)) {
|
|
130
|
+
return parseInt(string, 8);
|
|
131
|
+
}
|
|
132
|
+
throw new Error(`ipaddr: cannot parse ${string} as octal`);
|
|
133
|
+
}
|
|
134
|
+
return parseInt(string, 10);
|
|
135
|
+
}
|
|
136
|
+
__name(parseIntAuto, "parseIntAuto");
|
|
137
|
+
function padPart(part, length) {
|
|
138
|
+
while (part.length < length) {
|
|
139
|
+
part = `0${part}`;
|
|
140
|
+
}
|
|
141
|
+
return part;
|
|
142
|
+
}
|
|
143
|
+
__name(padPart, "padPart");
|
|
144
|
+
const ipaddr2 = {};
|
|
145
|
+
ipaddr2.IPv4 = (function() {
|
|
146
|
+
function IPv4(octets) {
|
|
147
|
+
if (octets.length !== 4) {
|
|
148
|
+
throw new Error("ipaddr: ipv4 octet count should be 4");
|
|
149
|
+
}
|
|
150
|
+
let i, octet;
|
|
151
|
+
for (i = 0; i < octets.length; i++) {
|
|
152
|
+
octet = octets[i];
|
|
153
|
+
if (!(0 <= octet && octet <= 255)) {
|
|
154
|
+
throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
this.octets = octets;
|
|
158
|
+
}
|
|
159
|
+
__name(IPv4, "IPv4");
|
|
160
|
+
IPv4.prototype.SpecialRanges = {
|
|
161
|
+
unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
|
|
162
|
+
broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
|
|
163
|
+
// RFC3171
|
|
164
|
+
multicast: [[new IPv4([224, 0, 0, 0]), 4]],
|
|
165
|
+
// RFC3927
|
|
166
|
+
linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
|
|
167
|
+
// RFC5735
|
|
168
|
+
loopback: [[new IPv4([127, 0, 0, 0]), 8]],
|
|
169
|
+
// RFC6598
|
|
170
|
+
carrierGradeNat: [[new IPv4([100, 64, 0, 0]), 10]],
|
|
171
|
+
// RFC1918
|
|
172
|
+
"private": [
|
|
173
|
+
[new IPv4([10, 0, 0, 0]), 8],
|
|
174
|
+
[new IPv4([172, 16, 0, 0]), 12],
|
|
175
|
+
[new IPv4([192, 168, 0, 0]), 16]
|
|
176
|
+
],
|
|
177
|
+
// Reserved and testing-only ranges; RFCs 5735, 5737, 2544, 1700
|
|
178
|
+
reserved: [
|
|
179
|
+
[new IPv4([192, 0, 0, 0]), 24],
|
|
180
|
+
[new IPv4([192, 0, 2, 0]), 24],
|
|
181
|
+
[new IPv4([192, 88, 99, 0]), 24],
|
|
182
|
+
[new IPv4([198, 18, 0, 0]), 15],
|
|
183
|
+
[new IPv4([198, 51, 100, 0]), 24],
|
|
184
|
+
[new IPv4([203, 0, 113, 0]), 24],
|
|
185
|
+
[new IPv4([240, 0, 0, 0]), 4]
|
|
186
|
+
],
|
|
187
|
+
// RFC7534, RFC7535
|
|
188
|
+
as112: [
|
|
189
|
+
[new IPv4([192, 175, 48, 0]), 24],
|
|
190
|
+
[new IPv4([192, 31, 196, 0]), 24]
|
|
191
|
+
],
|
|
192
|
+
// RFC7450
|
|
193
|
+
amt: [
|
|
194
|
+
[new IPv4([192, 52, 193, 0]), 24]
|
|
195
|
+
]
|
|
196
|
+
};
|
|
197
|
+
IPv4.prototype.kind = function() {
|
|
198
|
+
return "ipv4";
|
|
199
|
+
};
|
|
200
|
+
IPv4.prototype.match = function(other, cidrRange) {
|
|
201
|
+
let ref;
|
|
202
|
+
if (cidrRange === void 0) {
|
|
203
|
+
ref = other;
|
|
204
|
+
other = ref[0];
|
|
205
|
+
cidrRange = ref[1];
|
|
206
|
+
}
|
|
207
|
+
if (other.kind() !== "ipv4") {
|
|
208
|
+
throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
|
|
209
|
+
}
|
|
210
|
+
return matchCIDR(this.octets, other.octets, 8, cidrRange);
|
|
211
|
+
};
|
|
212
|
+
IPv4.prototype.prefixLengthFromSubnetMask = function() {
|
|
213
|
+
let cidr = 0;
|
|
214
|
+
let stop = false;
|
|
215
|
+
const zerotable = {
|
|
216
|
+
0: 8,
|
|
217
|
+
128: 7,
|
|
218
|
+
192: 6,
|
|
219
|
+
224: 5,
|
|
220
|
+
240: 4,
|
|
221
|
+
248: 3,
|
|
222
|
+
252: 2,
|
|
223
|
+
254: 1,
|
|
224
|
+
255: 0
|
|
225
|
+
};
|
|
226
|
+
let i, octet, zeros;
|
|
227
|
+
for (i = 3; i >= 0; i -= 1) {
|
|
228
|
+
octet = this.octets[i];
|
|
229
|
+
if (octet in zerotable) {
|
|
230
|
+
zeros = zerotable[octet];
|
|
231
|
+
if (stop && zeros !== 0) {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
if (zeros !== 8) {
|
|
235
|
+
stop = true;
|
|
236
|
+
}
|
|
237
|
+
cidr += zeros;
|
|
238
|
+
} else {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return 32 - cidr;
|
|
243
|
+
};
|
|
244
|
+
IPv4.prototype.range = function() {
|
|
245
|
+
return ipaddr2.subnetMatch(this, this.SpecialRanges);
|
|
246
|
+
};
|
|
247
|
+
IPv4.prototype.toByteArray = function() {
|
|
248
|
+
return this.octets.slice(0);
|
|
249
|
+
};
|
|
250
|
+
IPv4.prototype.toIPv4MappedAddress = function() {
|
|
251
|
+
return ipaddr2.IPv6.parse(`::ffff:${this.toString()}`);
|
|
252
|
+
};
|
|
253
|
+
IPv4.prototype.toNormalizedString = function() {
|
|
254
|
+
return this.toString();
|
|
255
|
+
};
|
|
256
|
+
IPv4.prototype.toString = function() {
|
|
257
|
+
return this.octets.join(".");
|
|
258
|
+
};
|
|
259
|
+
return IPv4;
|
|
260
|
+
})();
|
|
261
|
+
ipaddr2.IPv4.broadcastAddressFromCIDR = function(string) {
|
|
262
|
+
try {
|
|
263
|
+
const cidr = this.parseCIDR(string);
|
|
264
|
+
const ipInterfaceOctets = cidr[0].toByteArray();
|
|
265
|
+
const subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
|
|
266
|
+
const octets = [];
|
|
267
|
+
let i = 0;
|
|
268
|
+
while (i < 4) {
|
|
269
|
+
octets.push(parseInt(ipInterfaceOctets[i], 10) | parseInt(subnetMaskOctets[i], 10) ^ 255);
|
|
270
|
+
i++;
|
|
271
|
+
}
|
|
272
|
+
return new this(octets);
|
|
273
|
+
} catch (e) {
|
|
274
|
+
throw new Error("ipaddr: the address does not have IPv4 CIDR format", { cause: e });
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
ipaddr2.IPv4.isIPv4 = function(string) {
|
|
278
|
+
return this.parser(string) !== null;
|
|
279
|
+
};
|
|
280
|
+
ipaddr2.IPv4.isValid = function(string) {
|
|
281
|
+
try {
|
|
282
|
+
new this(this.parser(string));
|
|
283
|
+
return true;
|
|
284
|
+
} catch {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
ipaddr2.IPv4.isValidCIDR = function(string) {
|
|
289
|
+
try {
|
|
290
|
+
this.parseCIDR(string);
|
|
291
|
+
return true;
|
|
292
|
+
} catch {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
ipaddr2.IPv4.isValidFourPartDecimal = function(string) {
|
|
297
|
+
if (ipaddr2.IPv4.isValid(string) && string.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/)) {
|
|
298
|
+
return true;
|
|
299
|
+
} else {
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
ipaddr2.IPv4.isValidCIDRFourPartDecimal = function(string) {
|
|
304
|
+
const match = string.match(/^(.+)\/(\d+)$/);
|
|
305
|
+
if (!ipaddr2.IPv4.isValidCIDR(string) || !match) {
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
return ipaddr2.IPv4.isValidFourPartDecimal(match[1]);
|
|
309
|
+
};
|
|
310
|
+
ipaddr2.IPv4.networkAddressFromCIDR = function(string) {
|
|
311
|
+
let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;
|
|
312
|
+
try {
|
|
313
|
+
cidr = this.parseCIDR(string);
|
|
314
|
+
ipInterfaceOctets = cidr[0].toByteArray();
|
|
315
|
+
subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
|
|
316
|
+
octets = [];
|
|
317
|
+
i = 0;
|
|
318
|
+
while (i < 4) {
|
|
319
|
+
octets.push(parseInt(ipInterfaceOctets[i], 10) & parseInt(subnetMaskOctets[i], 10));
|
|
320
|
+
i++;
|
|
321
|
+
}
|
|
322
|
+
return new this(octets);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
throw new Error("ipaddr: the address does not have IPv4 CIDR format", { cause: e });
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
ipaddr2.IPv4.parse = function(string) {
|
|
328
|
+
const parts = this.parser(string);
|
|
329
|
+
if (parts === null) {
|
|
330
|
+
throw new Error("ipaddr: string is not formatted like an IPv4 Address");
|
|
331
|
+
}
|
|
332
|
+
return new this(parts);
|
|
333
|
+
};
|
|
334
|
+
ipaddr2.IPv4.parseCIDR = function(string) {
|
|
335
|
+
let match;
|
|
336
|
+
if (match = string.match(/^(.+)\/(\d+)$/)) {
|
|
337
|
+
const maskLength = parseInt(match[2]);
|
|
338
|
+
if (maskLength >= 0 && maskLength <= 32) {
|
|
339
|
+
const parsed = [this.parse(match[1]), maskLength];
|
|
340
|
+
Object.defineProperty(parsed, "toString", {
|
|
341
|
+
value: /* @__PURE__ */ __name(function() {
|
|
342
|
+
return this.join("/");
|
|
343
|
+
}, "value")
|
|
344
|
+
});
|
|
345
|
+
return parsed;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range");
|
|
349
|
+
};
|
|
350
|
+
ipaddr2.IPv4.parser = function(string) {
|
|
351
|
+
let match, part, value;
|
|
352
|
+
if (match = string.match(ipv4Regexes.fourOctet)) {
|
|
353
|
+
return (function() {
|
|
354
|
+
const ref = match.slice(1, 6);
|
|
355
|
+
const results = [];
|
|
356
|
+
for (let i = 0; i < ref.length; i++) {
|
|
357
|
+
part = ref[i];
|
|
358
|
+
results.push(parseIntAuto(part));
|
|
359
|
+
}
|
|
360
|
+
return results;
|
|
361
|
+
})();
|
|
362
|
+
} else if (match = string.match(ipv4Regexes.longValue)) {
|
|
363
|
+
value = parseIntAuto(match[1]);
|
|
364
|
+
if (value > 4294967295 || value < 0) {
|
|
365
|
+
throw new Error("ipaddr: address outside defined range");
|
|
366
|
+
}
|
|
367
|
+
return (function() {
|
|
368
|
+
const results = [];
|
|
369
|
+
let shift;
|
|
370
|
+
for (shift = 0; shift <= 24; shift += 8) {
|
|
371
|
+
results.push(value >> shift & 255);
|
|
372
|
+
}
|
|
373
|
+
return results;
|
|
374
|
+
})().reverse();
|
|
375
|
+
} else if (match = string.match(ipv4Regexes.twoOctet)) {
|
|
376
|
+
return (function() {
|
|
377
|
+
const ref = match.slice(1, 4);
|
|
378
|
+
const results = [];
|
|
379
|
+
value = parseIntAuto(ref[1]);
|
|
380
|
+
if (value > 16777215 || value < 0) {
|
|
381
|
+
throw new Error("ipaddr: address outside defined range");
|
|
382
|
+
}
|
|
383
|
+
results.push(parseIntAuto(ref[0]));
|
|
384
|
+
results.push(value >> 16 & 255);
|
|
385
|
+
results.push(value >> 8 & 255);
|
|
386
|
+
results.push(value & 255);
|
|
387
|
+
return results;
|
|
388
|
+
})();
|
|
389
|
+
} else if (match = string.match(ipv4Regexes.threeOctet)) {
|
|
390
|
+
return (function() {
|
|
391
|
+
const ref = match.slice(1, 5);
|
|
392
|
+
const results = [];
|
|
393
|
+
value = parseIntAuto(ref[2]);
|
|
394
|
+
if (value > 65535 || value < 0) {
|
|
395
|
+
throw new Error("ipaddr: address outside defined range");
|
|
396
|
+
}
|
|
397
|
+
results.push(parseIntAuto(ref[0]));
|
|
398
|
+
results.push(parseIntAuto(ref[1]));
|
|
399
|
+
results.push(value >> 8 & 255);
|
|
400
|
+
results.push(value & 255);
|
|
401
|
+
return results;
|
|
402
|
+
})();
|
|
403
|
+
} else {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
ipaddr2.IPv4.subnetMaskFromPrefixLength = function(prefix) {
|
|
408
|
+
prefix = parseInt(prefix);
|
|
409
|
+
if (Number.isNaN(prefix) || prefix < 0 || prefix > 32) {
|
|
410
|
+
throw new Error("ipaddr: invalid IPv4 prefix length");
|
|
411
|
+
}
|
|
412
|
+
const octets = [0, 0, 0, 0];
|
|
413
|
+
let j = 0;
|
|
414
|
+
const filledOctetCount = Math.floor(prefix / 8);
|
|
415
|
+
while (j < filledOctetCount) {
|
|
416
|
+
octets[j] = 255;
|
|
417
|
+
j++;
|
|
418
|
+
}
|
|
419
|
+
if (filledOctetCount < 4) {
|
|
420
|
+
octets[filledOctetCount] = Math.pow(2, prefix % 8) - 1 << 8 - prefix % 8;
|
|
421
|
+
}
|
|
422
|
+
return new this(octets);
|
|
423
|
+
};
|
|
424
|
+
ipaddr2.IPv6 = (function() {
|
|
425
|
+
function IPv6(parts, zoneId) {
|
|
426
|
+
let i, part;
|
|
427
|
+
if (parts.length === 16) {
|
|
428
|
+
this.parts = [];
|
|
429
|
+
for (i = 0; i <= 14; i += 2) {
|
|
430
|
+
this.parts.push(parts[i] << 8 | parts[i + 1]);
|
|
431
|
+
}
|
|
432
|
+
} else if (parts.length === 8) {
|
|
433
|
+
this.parts = parts;
|
|
434
|
+
} else {
|
|
435
|
+
throw new Error("ipaddr: ipv6 part count should be 8 or 16");
|
|
436
|
+
}
|
|
437
|
+
for (i = 0; i < this.parts.length; i++) {
|
|
438
|
+
part = this.parts[i];
|
|
439
|
+
if (!(0 <= part && part <= 65535)) {
|
|
440
|
+
throw new Error("ipaddr: ipv6 part should fit in 16 bits");
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (zoneId) {
|
|
444
|
+
this.zoneId = zoneId;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
__name(IPv6, "IPv6");
|
|
448
|
+
IPv6.prototype.SpecialRanges = {
|
|
449
|
+
// RFC4291, here and after
|
|
450
|
+
unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
|
|
451
|
+
linkLocal: [new IPv6([65152, 0, 0, 0, 0, 0, 0, 0]), 10],
|
|
452
|
+
multicast: [new IPv6([65280, 0, 0, 0, 0, 0, 0, 0]), 8],
|
|
453
|
+
loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
|
|
454
|
+
uniqueLocal: [new IPv6([64512, 0, 0, 0, 0, 0, 0, 0]), 7],
|
|
455
|
+
ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 65535, 0, 0]), 96],
|
|
456
|
+
// RFC3879
|
|
457
|
+
deprecatedSiteLocal: [new IPv6([65216, 0, 0, 0, 0, 0, 0, 0]), 10],
|
|
458
|
+
// RFC6666
|
|
459
|
+
discard: [new IPv6([256, 0, 0, 0, 0, 0, 0, 0]), 64],
|
|
460
|
+
// RFC6145
|
|
461
|
+
rfc6145: [new IPv6([0, 0, 0, 0, 65535, 0, 0, 0]), 96],
|
|
462
|
+
rfc6052: [
|
|
463
|
+
// RFC6052
|
|
464
|
+
[new IPv6([100, 65435, 0, 0, 0, 0, 0, 0]), 96],
|
|
465
|
+
// RFC8215
|
|
466
|
+
[new IPv6([100, 65435, 1, 0, 0, 0, 0, 0]), 48]
|
|
467
|
+
],
|
|
468
|
+
// RFC3056
|
|
469
|
+
"6to4": [new IPv6([8194, 0, 0, 0, 0, 0, 0, 0]), 16],
|
|
470
|
+
// RFC6052, RFC6146
|
|
471
|
+
teredo: [new IPv6([8193, 0, 0, 0, 0, 0, 0, 0]), 32],
|
|
472
|
+
// RFC5180
|
|
473
|
+
benchmarking: [new IPv6([8193, 2, 0, 0, 0, 0, 0, 0]), 48],
|
|
474
|
+
// RFC7450
|
|
475
|
+
amt: [new IPv6([8193, 3, 0, 0, 0, 0, 0, 0]), 32],
|
|
476
|
+
as112v6: [
|
|
477
|
+
// RFC7535
|
|
478
|
+
[new IPv6([8193, 4, 274, 0, 0, 0, 0, 0]), 48],
|
|
479
|
+
// RFC7534
|
|
480
|
+
[new IPv6([9760, 79, 32768, 0, 0, 0, 0, 0]), 48]
|
|
481
|
+
],
|
|
482
|
+
// RFC4843
|
|
483
|
+
deprecatedOrchid: [new IPv6([8193, 16, 0, 0, 0, 0, 0, 0]), 28],
|
|
484
|
+
// RFC7343
|
|
485
|
+
orchid2: [new IPv6([8193, 32, 0, 0, 0, 0, 0, 0]), 28],
|
|
486
|
+
// RFC9374
|
|
487
|
+
droneRemoteIdProtocolEntityTags: [new IPv6([8193, 48, 0, 0, 0, 0, 0, 0]), 28],
|
|
488
|
+
// RFC9602
|
|
489
|
+
segmentRouting: [new IPv6([24320, 0, 0, 0, 0, 0, 0, 0]), 16],
|
|
490
|
+
reserved: [
|
|
491
|
+
// RFC3849
|
|
492
|
+
[new IPv6([8193, 0, 0, 0, 0, 0, 0, 0]), 23],
|
|
493
|
+
// RFC2928
|
|
494
|
+
[new IPv6([8193, 3512, 0, 0, 0, 0, 0, 0]), 32],
|
|
495
|
+
// RFC9637
|
|
496
|
+
[new IPv6([16383, 0, 0, 0, 0, 0, 0, 0]), 20]
|
|
497
|
+
]
|
|
498
|
+
};
|
|
499
|
+
IPv6.prototype.isIPv4MappedAddress = function() {
|
|
500
|
+
return this.range() === "ipv4Mapped";
|
|
501
|
+
};
|
|
502
|
+
IPv6.prototype.kind = function() {
|
|
503
|
+
return "ipv6";
|
|
504
|
+
};
|
|
505
|
+
IPv6.prototype.match = function(other, cidrRange) {
|
|
506
|
+
let ref;
|
|
507
|
+
if (cidrRange === void 0) {
|
|
508
|
+
ref = other;
|
|
509
|
+
other = ref[0];
|
|
510
|
+
cidrRange = ref[1];
|
|
511
|
+
}
|
|
512
|
+
if (other.kind() !== "ipv6") {
|
|
513
|
+
throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
|
|
514
|
+
}
|
|
515
|
+
return matchCIDR(this.parts, other.parts, 16, cidrRange);
|
|
516
|
+
};
|
|
517
|
+
IPv6.prototype.prefixLengthFromSubnetMask = function() {
|
|
518
|
+
let cidr = 0;
|
|
519
|
+
let stop = false;
|
|
520
|
+
const zerotable = {
|
|
521
|
+
0: 16,
|
|
522
|
+
32768: 15,
|
|
523
|
+
49152: 14,
|
|
524
|
+
57344: 13,
|
|
525
|
+
61440: 12,
|
|
526
|
+
63488: 11,
|
|
527
|
+
64512: 10,
|
|
528
|
+
65024: 9,
|
|
529
|
+
65280: 8,
|
|
530
|
+
65408: 7,
|
|
531
|
+
65472: 6,
|
|
532
|
+
65504: 5,
|
|
533
|
+
65520: 4,
|
|
534
|
+
65528: 3,
|
|
535
|
+
65532: 2,
|
|
536
|
+
65534: 1,
|
|
537
|
+
65535: 0
|
|
538
|
+
};
|
|
539
|
+
let part, zeros;
|
|
540
|
+
for (let i = 7; i >= 0; i -= 1) {
|
|
541
|
+
part = this.parts[i];
|
|
542
|
+
if (part in zerotable) {
|
|
543
|
+
zeros = zerotable[part];
|
|
544
|
+
if (stop && zeros !== 0) {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
if (zeros !== 16) {
|
|
548
|
+
stop = true;
|
|
549
|
+
}
|
|
550
|
+
cidr += zeros;
|
|
551
|
+
} else {
|
|
552
|
+
return null;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return 128 - cidr;
|
|
556
|
+
};
|
|
557
|
+
IPv6.prototype.range = function() {
|
|
558
|
+
return ipaddr2.subnetMatch(this, this.SpecialRanges);
|
|
559
|
+
};
|
|
560
|
+
IPv6.prototype.toByteArray = function() {
|
|
561
|
+
let part;
|
|
562
|
+
const bytes = [];
|
|
563
|
+
const ref = this.parts;
|
|
564
|
+
for (let i = 0; i < ref.length; i++) {
|
|
565
|
+
part = ref[i];
|
|
566
|
+
bytes.push(part >> 8);
|
|
567
|
+
bytes.push(part & 255);
|
|
568
|
+
}
|
|
569
|
+
return bytes;
|
|
570
|
+
};
|
|
571
|
+
IPv6.prototype.toFixedLengthString = function() {
|
|
572
|
+
const addr = (function() {
|
|
573
|
+
const results = [];
|
|
574
|
+
for (let i = 0; i < this.parts.length; i++) {
|
|
575
|
+
results.push(padPart(this.parts[i].toString(16), 4));
|
|
576
|
+
}
|
|
577
|
+
return results;
|
|
578
|
+
}).call(this).join(":");
|
|
579
|
+
let suffix = "";
|
|
580
|
+
if (this.zoneId) {
|
|
581
|
+
suffix = `%${this.zoneId}`;
|
|
582
|
+
}
|
|
583
|
+
return addr + suffix;
|
|
584
|
+
};
|
|
585
|
+
IPv6.prototype.toIPv4Address = function() {
|
|
586
|
+
if (!this.isIPv4MappedAddress()) {
|
|
587
|
+
throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
|
|
588
|
+
}
|
|
589
|
+
const ref = this.parts.slice(-2);
|
|
590
|
+
const high = ref[0];
|
|
591
|
+
const low = ref[1];
|
|
592
|
+
return new ipaddr2.IPv4([high >> 8, high & 255, low >> 8, low & 255]);
|
|
593
|
+
};
|
|
594
|
+
IPv6.prototype.toNormalizedString = function() {
|
|
595
|
+
const addr = (function() {
|
|
596
|
+
const results = [];
|
|
597
|
+
for (let i = 0; i < this.parts.length; i++) {
|
|
598
|
+
results.push(this.parts[i].toString(16));
|
|
599
|
+
}
|
|
600
|
+
return results;
|
|
601
|
+
}).call(this).join(":");
|
|
602
|
+
let suffix = "";
|
|
603
|
+
if (this.zoneId) {
|
|
604
|
+
suffix = `%${this.zoneId}`;
|
|
605
|
+
}
|
|
606
|
+
return addr + suffix;
|
|
607
|
+
};
|
|
608
|
+
IPv6.prototype.toRFC5952String = function() {
|
|
609
|
+
const regex = /((^|:)(0(:|$)){2,})/g;
|
|
610
|
+
let suffix = "";
|
|
611
|
+
if (this.zoneId) {
|
|
612
|
+
suffix = `%${this.zoneId}`;
|
|
613
|
+
}
|
|
614
|
+
const normalized = this.toNormalizedString();
|
|
615
|
+
const string = normalized.slice(0, normalized.length - suffix.length);
|
|
616
|
+
let bestMatchIndex = 0;
|
|
617
|
+
let bestMatchLength = -1;
|
|
618
|
+
let bestMatchGroups = -1;
|
|
619
|
+
let match;
|
|
620
|
+
while (match = regex.exec(string)) {
|
|
621
|
+
const groups = (match[0].match(/0/g) || []).length;
|
|
622
|
+
if (groups > bestMatchGroups) {
|
|
623
|
+
bestMatchGroups = groups;
|
|
624
|
+
bestMatchIndex = match.index;
|
|
625
|
+
bestMatchLength = match[0].length;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (bestMatchLength < 0) {
|
|
629
|
+
return string + suffix;
|
|
630
|
+
}
|
|
631
|
+
return `${string.substring(0, bestMatchIndex)}::${string.substring(bestMatchIndex + bestMatchLength)}${suffix}`;
|
|
632
|
+
};
|
|
633
|
+
IPv6.prototype.toString = function() {
|
|
634
|
+
return this.toRFC5952String();
|
|
635
|
+
};
|
|
636
|
+
return IPv6;
|
|
637
|
+
})();
|
|
638
|
+
ipaddr2.IPv6.broadcastAddressFromCIDR = function(string) {
|
|
639
|
+
try {
|
|
640
|
+
const cidr = this.parseCIDR(string);
|
|
641
|
+
const ipInterfaceOctets = cidr[0].toByteArray();
|
|
642
|
+
const subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
|
|
643
|
+
const octets = [];
|
|
644
|
+
let i = 0;
|
|
645
|
+
while (i < 16) {
|
|
646
|
+
octets.push(parseInt(ipInterfaceOctets[i], 10) | parseInt(subnetMaskOctets[i], 10) ^ 255);
|
|
647
|
+
i++;
|
|
648
|
+
}
|
|
649
|
+
return new this(octets);
|
|
650
|
+
} catch (e) {
|
|
651
|
+
throw new Error("ipaddr: the address does not have IPv6 CIDR format", { cause: e });
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
ipaddr2.IPv6.isIPv6 = function(string) {
|
|
655
|
+
return this.parser(string) !== null;
|
|
656
|
+
};
|
|
657
|
+
ipaddr2.IPv6.isValid = function(string) {
|
|
658
|
+
if (typeof string === "string" && string.indexOf(":") === -1) {
|
|
659
|
+
return false;
|
|
660
|
+
}
|
|
661
|
+
try {
|
|
662
|
+
const addr = this.parser(string);
|
|
663
|
+
new this(addr.parts, addr.zoneId);
|
|
664
|
+
return true;
|
|
665
|
+
} catch {
|
|
666
|
+
return false;
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
ipaddr2.IPv6.isValidCIDR = function(string) {
|
|
670
|
+
if (typeof string === "string" && string.indexOf(":") === -1) {
|
|
671
|
+
return false;
|
|
672
|
+
}
|
|
673
|
+
try {
|
|
674
|
+
this.parseCIDR(string);
|
|
675
|
+
return true;
|
|
676
|
+
} catch {
|
|
677
|
+
return false;
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
ipaddr2.IPv6.networkAddressFromCIDR = function(string) {
|
|
681
|
+
let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;
|
|
682
|
+
try {
|
|
683
|
+
cidr = this.parseCIDR(string);
|
|
684
|
+
ipInterfaceOctets = cidr[0].toByteArray();
|
|
685
|
+
subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
|
|
686
|
+
octets = [];
|
|
687
|
+
i = 0;
|
|
688
|
+
while (i < 16) {
|
|
689
|
+
octets.push(parseInt(ipInterfaceOctets[i], 10) & parseInt(subnetMaskOctets[i], 10));
|
|
690
|
+
i++;
|
|
691
|
+
}
|
|
692
|
+
return new this(octets);
|
|
693
|
+
} catch (e) {
|
|
694
|
+
throw new Error("ipaddr: the address does not have IPv6 CIDR format", { cause: e });
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
ipaddr2.IPv6.parse = function(string) {
|
|
698
|
+
const addr = this.parser(string);
|
|
699
|
+
if (addr === null) {
|
|
700
|
+
throw new Error("ipaddr: string is not formatted like an IPv6 Address");
|
|
701
|
+
}
|
|
702
|
+
return new this(addr.parts, addr.zoneId);
|
|
703
|
+
};
|
|
704
|
+
ipaddr2.IPv6.parseCIDR = function(string) {
|
|
705
|
+
let maskLength, match, parsed;
|
|
706
|
+
if (match = string.match(/^(.+)\/(\d+)$/)) {
|
|
707
|
+
maskLength = parseInt(match[2]);
|
|
708
|
+
if (maskLength >= 0 && maskLength <= 128) {
|
|
709
|
+
parsed = [this.parse(match[1]), maskLength];
|
|
710
|
+
Object.defineProperty(parsed, "toString", {
|
|
711
|
+
value: /* @__PURE__ */ __name(function() {
|
|
712
|
+
return this.join("/");
|
|
713
|
+
}, "value")
|
|
714
|
+
});
|
|
715
|
+
return parsed;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range");
|
|
719
|
+
};
|
|
720
|
+
ipaddr2.IPv6.parser = function(string) {
|
|
721
|
+
let addr, i, match, octet, octets, zoneId;
|
|
722
|
+
if (match = string.match(ipv6Regexes.deprecatedTransitional)) {
|
|
723
|
+
return this.parser(`::ffff:${match[1]}`);
|
|
724
|
+
}
|
|
725
|
+
if (ipv6Regexes.native.test(string)) {
|
|
726
|
+
return expandIPv6(string, 8);
|
|
727
|
+
}
|
|
728
|
+
if (match = string.match(ipv6Regexes.transitional)) {
|
|
729
|
+
zoneId = match[6] || "";
|
|
730
|
+
addr = match[1];
|
|
731
|
+
if (!match[1].endsWith("::")) {
|
|
732
|
+
addr = addr.slice(0, -1);
|
|
733
|
+
}
|
|
734
|
+
addr = expandIPv6(addr + zoneId, 6);
|
|
735
|
+
if (addr && addr.parts) {
|
|
736
|
+
octets = [
|
|
737
|
+
parseInt(match[2]),
|
|
738
|
+
parseInt(match[3]),
|
|
739
|
+
parseInt(match[4]),
|
|
740
|
+
parseInt(match[5])
|
|
741
|
+
];
|
|
742
|
+
for (i = 0; i < octets.length; i++) {
|
|
743
|
+
octet = octets[i];
|
|
744
|
+
if (!(0 <= octet && octet <= 255)) {
|
|
745
|
+
return null;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
addr.parts.push(octets[0] << 8 | octets[1]);
|
|
749
|
+
addr.parts.push(octets[2] << 8 | octets[3]);
|
|
750
|
+
return {
|
|
751
|
+
parts: addr.parts,
|
|
752
|
+
zoneId: addr.zoneId
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
return null;
|
|
757
|
+
};
|
|
758
|
+
ipaddr2.IPv6.subnetMaskFromPrefixLength = function(prefix) {
|
|
759
|
+
prefix = parseInt(prefix);
|
|
760
|
+
if (Number.isNaN(prefix) || prefix < 0 || prefix > 128) {
|
|
761
|
+
throw new Error("ipaddr: invalid IPv6 prefix length");
|
|
762
|
+
}
|
|
763
|
+
const octets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
764
|
+
let j = 0;
|
|
765
|
+
const filledOctetCount = Math.floor(prefix / 8);
|
|
766
|
+
while (j < filledOctetCount) {
|
|
767
|
+
octets[j] = 255;
|
|
768
|
+
j++;
|
|
769
|
+
}
|
|
770
|
+
if (filledOctetCount < 16) {
|
|
771
|
+
octets[filledOctetCount] = Math.pow(2, prefix % 8) - 1 << 8 - prefix % 8;
|
|
772
|
+
}
|
|
773
|
+
return new this(octets);
|
|
774
|
+
};
|
|
775
|
+
ipaddr2.fromByteArray = function(bytes) {
|
|
776
|
+
const length = bytes.length;
|
|
777
|
+
if (length === 4) {
|
|
778
|
+
return new ipaddr2.IPv4(bytes);
|
|
779
|
+
} else if (length === 16) {
|
|
780
|
+
return new ipaddr2.IPv6(bytes);
|
|
781
|
+
} else {
|
|
782
|
+
throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address");
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
ipaddr2.isValid = function(string) {
|
|
786
|
+
return ipaddr2.IPv6.isValid(string) || ipaddr2.IPv4.isValid(string);
|
|
787
|
+
};
|
|
788
|
+
ipaddr2.isValidCIDR = function(string) {
|
|
789
|
+
return ipaddr2.IPv6.isValidCIDR(string) || ipaddr2.IPv4.isValidCIDR(string);
|
|
790
|
+
};
|
|
791
|
+
ipaddr2.parse = function(string) {
|
|
792
|
+
if (ipaddr2.IPv6.isValid(string)) {
|
|
793
|
+
return ipaddr2.IPv6.parse(string);
|
|
794
|
+
} else if (ipaddr2.IPv4.isValid(string)) {
|
|
795
|
+
return ipaddr2.IPv4.parse(string);
|
|
796
|
+
} else {
|
|
797
|
+
throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
ipaddr2.parseCIDR = function(string) {
|
|
801
|
+
try {
|
|
802
|
+
return ipaddr2.IPv6.parseCIDR(string);
|
|
803
|
+
} catch {
|
|
804
|
+
try {
|
|
805
|
+
return ipaddr2.IPv4.parseCIDR(string);
|
|
806
|
+
} catch (e) {
|
|
807
|
+
throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format", { cause: e });
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
ipaddr2.process = function(string) {
|
|
812
|
+
const addr = this.parse(string);
|
|
813
|
+
if (addr.kind() === "ipv6" && addr.isIPv4MappedAddress()) {
|
|
814
|
+
return addr.toIPv4Address();
|
|
815
|
+
} else {
|
|
816
|
+
return addr;
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
ipaddr2.subnetMatch = function(address, rangeList, defaultName) {
|
|
820
|
+
let i, rangeName, rangeSubnets, subnet;
|
|
821
|
+
if (defaultName === void 0 || defaultName === null) {
|
|
822
|
+
defaultName = "unicast";
|
|
823
|
+
}
|
|
824
|
+
for (rangeName in rangeList) {
|
|
825
|
+
if (Object.prototype.hasOwnProperty.call(rangeList, rangeName)) {
|
|
826
|
+
rangeSubnets = rangeList[rangeName];
|
|
827
|
+
if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
|
|
828
|
+
rangeSubnets = [rangeSubnets];
|
|
829
|
+
}
|
|
830
|
+
for (i = 0; i < rangeSubnets.length; i++) {
|
|
831
|
+
subnet = rangeSubnets[i];
|
|
832
|
+
if (address.kind() === subnet[0].kind() && address.match.apply(address, subnet)) {
|
|
833
|
+
return rangeName;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return defaultName;
|
|
839
|
+
};
|
|
840
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
841
|
+
module.exports = ipaddr2;
|
|
842
|
+
} else {
|
|
843
|
+
root.ipaddr = ipaddr2;
|
|
844
|
+
}
|
|
845
|
+
})(exports);
|
|
846
|
+
}
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
// ../../libs/deploy-id/src/deploy-id.ts
|
|
850
|
+
var MAX_DEPLOY_ID = 63;
|
|
851
|
+
var HASH_LENGTH = 16;
|
|
852
|
+
var hostnameHash = /* @__PURE__ */ __name(async (hostname) => {
|
|
853
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(hostname));
|
|
854
|
+
return Array.from(new Uint8Array(digest)).map((byte) => byte.toString(16).padStart(2, "0")).join("").slice(0, HASH_LENGTH);
|
|
855
|
+
}, "hostnameHash");
|
|
856
|
+
var deployIdFromHostname = /* @__PURE__ */ __name(async (hostname) => {
|
|
857
|
+
const canonical = hostname.toLowerCase();
|
|
858
|
+
const budget = MAX_DEPLOY_ID - HASH_LENGTH - 1;
|
|
859
|
+
const prefix = canonical.replace(/[^a-z0-9-]/g, "").slice(0, budget).replace(/-+$/, "");
|
|
860
|
+
return `${prefix}-${await hostnameHash(canonical)}`;
|
|
861
|
+
}, "deployIdFromHostname");
|
|
862
|
+
|
|
863
|
+
// ../../libs/honeycomb/src/ids.ts
|
|
864
|
+
var HEX = "0123456789abcdef";
|
|
865
|
+
var randomHex = /* @__PURE__ */ __name((bytes) => {
|
|
866
|
+
const buffer = new Uint8Array(bytes);
|
|
867
|
+
crypto.getRandomValues(buffer);
|
|
868
|
+
let out = "";
|
|
869
|
+
for (const byte of buffer) {
|
|
870
|
+
out += HEX[byte >> 4];
|
|
871
|
+
out += HEX[byte & 15];
|
|
872
|
+
}
|
|
873
|
+
return out;
|
|
874
|
+
}, "randomHex");
|
|
875
|
+
var newSpanId = /* @__PURE__ */ __name(() => {
|
|
876
|
+
return randomHex(8);
|
|
877
|
+
}, "newSpanId");
|
|
878
|
+
var nowUnixNano = /* @__PURE__ */ __name(() => {
|
|
879
|
+
return String(BigInt(Date.now()) * 1000000n);
|
|
880
|
+
}, "nowUnixNano");
|
|
881
|
+
|
|
882
|
+
// ../../libs/honeycomb/src/metric.ts
|
|
883
|
+
var HISTOGRAM_BOUNDS = [5e-3, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10];
|
|
884
|
+
var streamKey = /* @__PURE__ */ __name((name, attributes) => {
|
|
885
|
+
const pairs = Object.entries(attributes).map(([key, value]) => `${key}=${String(value)}`).sort();
|
|
886
|
+
return JSON.stringify([name, pairs]);
|
|
887
|
+
}, "streamKey");
|
|
888
|
+
var bucketFor = /* @__PURE__ */ __name((value) => {
|
|
889
|
+
const index = HISTOGRAM_BOUNDS.findIndex((bound) => value <= bound);
|
|
890
|
+
return index === -1 ? HISTOGRAM_BOUNDS.length : index;
|
|
891
|
+
}, "bucketFor");
|
|
892
|
+
var observed = /* @__PURE__ */ __name((bucketCounts, value) => {
|
|
893
|
+
const target = bucketFor(value);
|
|
894
|
+
return bucketCounts.map((count, index) => index === target ? count + 1 : count);
|
|
895
|
+
}, "observed");
|
|
896
|
+
var recordHistogram = /* @__PURE__ */ __name((buffer, instrument, value) => {
|
|
897
|
+
const key = streamKey(instrument.name, instrument.attributes);
|
|
898
|
+
const existing = buffer.histograms.get(key);
|
|
899
|
+
if (existing === void 0) {
|
|
900
|
+
const empty = new Array(HISTOGRAM_BOUNDS.length + 1).fill(0);
|
|
901
|
+
buffer.histograms.set(key, { ...instrument, count: 1, sum: value, min: value, max: value, bucketCounts: observed(empty, value) });
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
existing.count += 1;
|
|
905
|
+
existing.sum += value;
|
|
906
|
+
existing.min = Math.min(existing.min, value);
|
|
907
|
+
existing.max = Math.max(existing.max, value);
|
|
908
|
+
existing.bucketCounts = observed(existing.bucketCounts, value);
|
|
909
|
+
}, "recordHistogram");
|
|
910
|
+
|
|
911
|
+
// ../../libs/honeycomb/src/scope.ts
|
|
912
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
913
|
+
var scopes = new AsyncLocalStorage();
|
|
914
|
+
var runInScope = /* @__PURE__ */ __name((scope, run) => {
|
|
915
|
+
return scopes.run(scope, run);
|
|
916
|
+
}, "runInScope");
|
|
917
|
+
var currentScope = /* @__PURE__ */ __name(() => {
|
|
918
|
+
return scopes.getStore();
|
|
919
|
+
}, "currentScope");
|
|
920
|
+
var currentSpan = /* @__PURE__ */ __name(() => {
|
|
921
|
+
return scopes.getStore()?.span;
|
|
922
|
+
}, "currentSpan");
|
|
923
|
+
|
|
924
|
+
// ../../libs/honeycomb/src/span.ts
|
|
925
|
+
var startSpan = /* @__PURE__ */ __name(({ traceId, parentSpanId, name, kind, attributes }) => {
|
|
926
|
+
return {
|
|
927
|
+
traceId,
|
|
928
|
+
spanId: newSpanId(),
|
|
929
|
+
parentSpanId,
|
|
930
|
+
name,
|
|
931
|
+
kind,
|
|
932
|
+
startTimeUnixNano: nowUnixNano(),
|
|
933
|
+
endTimeUnixNano: nowUnixNano(),
|
|
934
|
+
attributes: { ...attributes },
|
|
935
|
+
events: [],
|
|
936
|
+
failed: false,
|
|
937
|
+
statusMessage: void 0
|
|
938
|
+
};
|
|
939
|
+
}, "startSpan");
|
|
940
|
+
var MAX_CAUSE_DEPTH = 5;
|
|
941
|
+
var causeChain = /* @__PURE__ */ __name((error) => {
|
|
942
|
+
const chain = [error];
|
|
943
|
+
let current = error?.cause;
|
|
944
|
+
while (current !== void 0 && current !== null && chain.length < MAX_CAUSE_DEPTH) {
|
|
945
|
+
chain.push(current);
|
|
946
|
+
current = current.cause;
|
|
947
|
+
}
|
|
948
|
+
return chain;
|
|
949
|
+
}, "causeChain");
|
|
950
|
+
var linkMessage = /* @__PURE__ */ __name((error) => {
|
|
951
|
+
if (!(error instanceof Error)) {
|
|
952
|
+
return String(error);
|
|
953
|
+
}
|
|
954
|
+
const code = error.code;
|
|
955
|
+
return typeof code === "string" ? `${error.message} [${code}]` : error.message;
|
|
956
|
+
}, "linkMessage");
|
|
957
|
+
var errorMessage = /* @__PURE__ */ __name((error) => {
|
|
958
|
+
return causeChain(error).map(linkMessage).join(": ");
|
|
959
|
+
}, "errorMessage");
|
|
960
|
+
var exceptionType = /* @__PURE__ */ __name((error) => {
|
|
961
|
+
return error instanceof Error ? error.name : typeof error;
|
|
962
|
+
}, "exceptionType");
|
|
963
|
+
var exceptionAttributes = /* @__PURE__ */ __name((error) => {
|
|
964
|
+
const attributes = {
|
|
965
|
+
"exception.type": exceptionType(error),
|
|
966
|
+
"exception.message": errorMessage(error)
|
|
967
|
+
};
|
|
968
|
+
const stack = error instanceof Error ? error.stack : void 0;
|
|
969
|
+
if (stack !== void 0) {
|
|
970
|
+
attributes["exception.stacktrace"] = stack;
|
|
971
|
+
}
|
|
972
|
+
return attributes;
|
|
973
|
+
}, "exceptionAttributes");
|
|
974
|
+
var exceptionEvent = /* @__PURE__ */ __name((error) => {
|
|
975
|
+
return { name: "exception", timeUnixNano: nowUnixNano(), attributes: exceptionAttributes(error) };
|
|
976
|
+
}, "exceptionEvent");
|
|
977
|
+
|
|
978
|
+
// ../../libs/honeycomb/src/trace.ts
|
|
979
|
+
var durationSeconds = /* @__PURE__ */ __name((span) => {
|
|
980
|
+
return Number(BigInt(span.endTimeUnixNano) - BigInt(span.startTimeUnixNano)) / 1e9;
|
|
981
|
+
}, "durationSeconds");
|
|
982
|
+
var measure = /* @__PURE__ */ __name((scope, span, options, errorType) => {
|
|
983
|
+
const seconds = durationSeconds(span);
|
|
984
|
+
const attributes = errorType === void 0 ? options.attributes : { ...options.attributes, "error.type": errorType };
|
|
985
|
+
recordHistogram(scope.buffer.metrics, { name: options.metric, unit: options.unit, attributes }, seconds);
|
|
986
|
+
return span.failed || seconds >= options.slowSeconds || scope.keepMeasuredSpans;
|
|
987
|
+
}, "measure");
|
|
988
|
+
var traced = /* @__PURE__ */ __name(async (name, options, run) => {
|
|
989
|
+
const scope = currentScope();
|
|
990
|
+
if (scope === void 0) {
|
|
991
|
+
return await run();
|
|
992
|
+
}
|
|
993
|
+
const span = startSpan({
|
|
994
|
+
traceId: scope.traceId,
|
|
995
|
+
parentSpanId: scope.span?.spanId,
|
|
996
|
+
name,
|
|
997
|
+
kind: options.kind ?? "internal",
|
|
998
|
+
attributes: options.attributes ?? {}
|
|
999
|
+
});
|
|
1000
|
+
let errorType;
|
|
1001
|
+
try {
|
|
1002
|
+
return await runInScope({ ...scope, span }, run);
|
|
1003
|
+
} catch (error) {
|
|
1004
|
+
span.events.push(exceptionEvent(error));
|
|
1005
|
+
span.attributes["error"] = true;
|
|
1006
|
+
span.failed = true;
|
|
1007
|
+
span.statusMessage = errorMessage(error);
|
|
1008
|
+
errorType = exceptionType(error);
|
|
1009
|
+
throw error;
|
|
1010
|
+
} finally {
|
|
1011
|
+
span.endTimeUnixNano = nowUnixNano();
|
|
1012
|
+
if (options.measure === void 0 || measure(scope, span, options.measure, errorType)) {
|
|
1013
|
+
scope.buffer.spans.push(span);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}, "traced");
|
|
1017
|
+
var setSpanAttributes = /* @__PURE__ */ __name((attributes) => {
|
|
1018
|
+
const span = currentSpan();
|
|
1019
|
+
if (span === void 0) {
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
Object.assign(span.attributes, attributes);
|
|
1023
|
+
}, "setSpanAttributes");
|
|
1024
|
+
|
|
1025
|
+
// ../../libs/honeycomb/src/fetch.ts
|
|
1026
|
+
var targetUrl = /* @__PURE__ */ __name((input) => {
|
|
1027
|
+
if (typeof input === "string") {
|
|
1028
|
+
return input;
|
|
1029
|
+
}
|
|
1030
|
+
return input instanceof URL ? input.href : input.url;
|
|
1031
|
+
}, "targetUrl");
|
|
1032
|
+
var targetMethod = /* @__PURE__ */ __name((input, init) => {
|
|
1033
|
+
if (init?.method !== void 0) {
|
|
1034
|
+
return init.method;
|
|
1035
|
+
}
|
|
1036
|
+
return typeof input === "string" || input instanceof URL ? "GET" : input.method;
|
|
1037
|
+
}, "targetMethod");
|
|
1038
|
+
var tracedFetch = /* @__PURE__ */ __name(async (input, init, options) => {
|
|
1039
|
+
const url = targetUrl(input);
|
|
1040
|
+
const method = targetMethod(input, init);
|
|
1041
|
+
const parsed = new URL(url);
|
|
1042
|
+
const urlFull = options?.traceQuery === false ? `${parsed.origin}${parsed.pathname}` : url;
|
|
1043
|
+
return await traced(
|
|
1044
|
+
`${method} ${parsed.host}`,
|
|
1045
|
+
{ kind: "client", attributes: { "http.request.method": method, "url.full": urlFull } },
|
|
1046
|
+
async () => {
|
|
1047
|
+
const response = await fetch(input, init);
|
|
1048
|
+
setSpanAttributes({ "http.response.status_code": response.status });
|
|
1049
|
+
return response;
|
|
1050
|
+
}
|
|
1051
|
+
);
|
|
1052
|
+
}, "tracedFetch");
|
|
1053
|
+
|
|
1054
|
+
// ../../libs/honeycomb/src/redact.ts
|
|
1055
|
+
var SENSITIVE_URL_PARAMS = /* @__PURE__ */ new Set([
|
|
1056
|
+
"access_token",
|
|
1057
|
+
"api_key",
|
|
1058
|
+
"api_secret",
|
|
1059
|
+
"client_secret",
|
|
1060
|
+
"code",
|
|
1061
|
+
"code_verifier",
|
|
1062
|
+
"key",
|
|
1063
|
+
"password",
|
|
1064
|
+
"refresh_token",
|
|
1065
|
+
"secret",
|
|
1066
|
+
"session",
|
|
1067
|
+
"session_id",
|
|
1068
|
+
"token"
|
|
1069
|
+
]);
|
|
1070
|
+
var SENSITIVE_URL_PARAM_SUFFIXES = ["token", "secret", "password"];
|
|
1071
|
+
var PARAM_MATCHER = /(^|[?&])([^=&?]+)=[^&]*/g;
|
|
1072
|
+
var redactUrlParams = /* @__PURE__ */ __name((value) => {
|
|
1073
|
+
return value.replaceAll(PARAM_MATCHER, (match, separator, param) => {
|
|
1074
|
+
const name = param.toLowerCase();
|
|
1075
|
+
const sensitive = SENSITIVE_URL_PARAMS.has(name) || SENSITIVE_URL_PARAM_SUFFIXES.some((suffix) => name.endsWith(suffix));
|
|
1076
|
+
return sensitive ? `${separator}${param}=[REDACTED]` : match;
|
|
1077
|
+
});
|
|
1078
|
+
}, "redactUrlParams");
|
|
1079
|
+
|
|
1080
|
+
// ../../libs/honeycomb/src/logger.ts
|
|
1081
|
+
var append = /* @__PURE__ */ __name((severity, body, attributes) => {
|
|
1082
|
+
const scope = currentScope();
|
|
1083
|
+
if (scope === void 0) {
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
const span = currentSpan();
|
|
1087
|
+
scope.buffer.logs.push({
|
|
1088
|
+
timeUnixNano: nowUnixNano(),
|
|
1089
|
+
severity,
|
|
1090
|
+
body: redactUrlParams(body),
|
|
1091
|
+
attributes,
|
|
1092
|
+
traceId: scope.traceId,
|
|
1093
|
+
spanId: span?.spanId
|
|
1094
|
+
});
|
|
1095
|
+
}, "append");
|
|
1096
|
+
var logger = {
|
|
1097
|
+
/**
|
|
1098
|
+
* Logs at debug severity.
|
|
1099
|
+
* @param message The log body; sensitive URL parameters in it are redacted before export.
|
|
1100
|
+
* @param attributes Extra attributes to set on the log record.
|
|
1101
|
+
*/
|
|
1102
|
+
debug: /* @__PURE__ */ __name((message, attributes = {}) => {
|
|
1103
|
+
console.debug(message);
|
|
1104
|
+
append("debug", message, attributes);
|
|
1105
|
+
}, "debug"),
|
|
1106
|
+
/**
|
|
1107
|
+
* Logs at info severity.
|
|
1108
|
+
* @param message The log body; sensitive URL parameters in it are redacted before export.
|
|
1109
|
+
* @param attributes Extra attributes to set on the log record.
|
|
1110
|
+
*/
|
|
1111
|
+
info: /* @__PURE__ */ __name((message, attributes = {}) => {
|
|
1112
|
+
console.info(message);
|
|
1113
|
+
append("info", message, attributes);
|
|
1114
|
+
}, "info"),
|
|
1115
|
+
/**
|
|
1116
|
+
* Logs at warn severity.
|
|
1117
|
+
* @param message The log body; sensitive URL parameters in it are redacted before export.
|
|
1118
|
+
* @param attributes Extra attributes to set on the log record.
|
|
1119
|
+
*/
|
|
1120
|
+
warn: /* @__PURE__ */ __name((message, attributes = {}) => {
|
|
1121
|
+
console.warn(message);
|
|
1122
|
+
append("warn", message, attributes);
|
|
1123
|
+
}, "warn"),
|
|
1124
|
+
/**
|
|
1125
|
+
* Logs at error severity.
|
|
1126
|
+
* @param message The log body; sensitive URL parameters in it are redacted before export.
|
|
1127
|
+
* @param error The error to report — its message joins the body, and it is attached to the active span as an exception when there is one.
|
|
1128
|
+
* @param attributes Extra attributes to set on the log record.
|
|
1129
|
+
*/
|
|
1130
|
+
error: /* @__PURE__ */ __name((message, error, attributes = {}) => {
|
|
1131
|
+
if (error === void 0) {
|
|
1132
|
+
console.error(message);
|
|
1133
|
+
append("error", message, attributes);
|
|
1134
|
+
return;
|
|
1135
|
+
}
|
|
1136
|
+
console.error(message, error);
|
|
1137
|
+
const span = currentSpan();
|
|
1138
|
+
if (span !== void 0) {
|
|
1139
|
+
span.events.push(exceptionEvent(error));
|
|
1140
|
+
span.attributes["error"] = true;
|
|
1141
|
+
}
|
|
1142
|
+
append("error", `${message}: ${errorMessage(error)}`, { ...attributes, ...exceptionAttributes(error) });
|
|
1143
|
+
}, "error")
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
// ../../libs/proxy-ingress/src/ingress.ts
|
|
1147
|
+
var PROXY_INGRESS_HEADER = "x-qinto-proxy-ingress";
|
|
1148
|
+
var PROXY_ORIGIN_HEADER = "x-qinto-proxy-origin";
|
|
1149
|
+
var encodeProxyIngress = /* @__PURE__ */ __name((ingress) => {
|
|
1150
|
+
const cf = ingress.cf;
|
|
1151
|
+
return JSON.stringify({
|
|
1152
|
+
requestId: ingress.requestId,
|
|
1153
|
+
traceId: ingress.traceId,
|
|
1154
|
+
rayId: ingress.rayId,
|
|
1155
|
+
visitorIp: ingress.visitorIp,
|
|
1156
|
+
cf: {
|
|
1157
|
+
country: cf.country,
|
|
1158
|
+
continent: cf.continent,
|
|
1159
|
+
city: cf.city,
|
|
1160
|
+
region: cf.region,
|
|
1161
|
+
regionCode: cf.regionCode,
|
|
1162
|
+
timezone: cf.timezone,
|
|
1163
|
+
postalCode: cf.postalCode,
|
|
1164
|
+
latitude: cf.latitude,
|
|
1165
|
+
longitude: cf.longitude,
|
|
1166
|
+
isEUCountry: cf.isEUCountry,
|
|
1167
|
+
colo: cf.colo,
|
|
1168
|
+
asn: cf.asn,
|
|
1169
|
+
asOrganization: cf.asOrganization,
|
|
1170
|
+
botManagement: { score: cf.botManagement?.score, verifiedBot: cf.botManagement?.verifiedBot }
|
|
1171
|
+
}
|
|
1172
|
+
}).replace(/[\u0080-\uffff]/g, (character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
1173
|
+
}, "encodeProxyIngress");
|
|
1174
|
+
|
|
1175
|
+
// ../../libs/proxy-rules/src/cidr.ts
|
|
1176
|
+
var import_ipaddr = __toESM(require_ipaddr(), 1);
|
|
1177
|
+
var normalizeIp = /* @__PURE__ */ __name((raw) => {
|
|
1178
|
+
if (raw.includes("%") || raw.includes(".") && !import_ipaddr.default.IPv4.isValidFourPartDecimal(raw.slice(raw.lastIndexOf(":") + 1)) || !(import_ipaddr.default.IPv4.isValidFourPartDecimal(raw) || import_ipaddr.default.IPv6.isValid(raw))) {
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
const address = import_ipaddr.default.process(raw);
|
|
1182
|
+
return { family: address.kind() === "ipv4" ? 4 : 6, bytes: address.toByteArray(), text: address.toString() };
|
|
1183
|
+
}, "normalizeIp");
|
|
1184
|
+
|
|
1185
|
+
// src/visitor-ip.ts
|
|
1186
|
+
var visitorIpFrom = /* @__PURE__ */ __name((headers) => {
|
|
1187
|
+
const raw = headers.get("cf-connecting-ip");
|
|
1188
|
+
if (raw === "2a06:98c0:3600::103") {
|
|
1189
|
+
return { kind: "unavailable", reason: "synthetic_ip" };
|
|
1190
|
+
}
|
|
1191
|
+
if (headers.has("cf-worker") || headers.has("cf-connecting-o2o")) {
|
|
1192
|
+
return { kind: "unavailable", reason: "untrusted_source" };
|
|
1193
|
+
}
|
|
1194
|
+
const address = raw === null ? null : normalizeIp(raw);
|
|
1195
|
+
if (address === null) {
|
|
1196
|
+
return { kind: "unavailable", reason: "not_provided" };
|
|
1197
|
+
}
|
|
1198
|
+
if (address.family === 4 && address.bytes[0] >= 240) {
|
|
1199
|
+
return { kind: "unavailable", reason: "untrusted_source" };
|
|
1200
|
+
}
|
|
1201
|
+
return { kind: "value", value: address.text };
|
|
1202
|
+
}, "visitorIpFrom");
|
|
1203
|
+
|
|
1204
|
+
// src/dispatch.ts
|
|
1205
|
+
var dispatchRequest = /* @__PURE__ */ __name(async (request, env) => {
|
|
1206
|
+
const hostname = new URL(request.url).hostname;
|
|
1207
|
+
const workerId = await deployIdFromHostname(hostname);
|
|
1208
|
+
const ingress = {
|
|
1209
|
+
requestId: crypto.randomUUID(),
|
|
1210
|
+
traceId: crypto.randomUUID().replaceAll("-", ""),
|
|
1211
|
+
rayId: request.headers.get("cf-ray"),
|
|
1212
|
+
visitorIp: visitorIpFrom(request.headers),
|
|
1213
|
+
cf: request.cf ?? {}
|
|
1214
|
+
};
|
|
1215
|
+
const headers = new Headers(request.headers);
|
|
1216
|
+
headers.set(PROXY_INGRESS_HEADER, encodeProxyIngress(ingress));
|
|
1217
|
+
const forwarded = new Request(request, { headers, redirect: "manual" });
|
|
1218
|
+
try {
|
|
1219
|
+
return await env.PROXY.get(workerId).fetch(forwarded);
|
|
1220
|
+
} catch (error) {
|
|
1221
|
+
const missing = error instanceof Error && error.message.includes("Worker not found");
|
|
1222
|
+
const code = missing ? "worker_missing" : "dispatch_error";
|
|
1223
|
+
try {
|
|
1224
|
+
env.DISPATCH_ANALYTICS.writeDataPoint({
|
|
1225
|
+
indexes: [workerId],
|
|
1226
|
+
blobs: [hostname, code, ingress.requestId, ingress.traceId]
|
|
1227
|
+
});
|
|
1228
|
+
} catch {
|
|
1229
|
+
logger.error("Proxy dispatcher measurement failed", void 0, { requestId: ingress.requestId, code });
|
|
1230
|
+
}
|
|
1231
|
+
return new Response(missing ? "Not found" : "Proxy unavailable", { status: missing ? 404 : 503 });
|
|
1232
|
+
}
|
|
1233
|
+
}, "dispatchRequest");
|
|
1234
|
+
|
|
1235
|
+
// src/origin-connection.ts
|
|
1236
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
1237
|
+
|
|
1238
|
+
// src/origin.ts
|
|
1239
|
+
var transportFor = /* @__PURE__ */ __name((kind) => {
|
|
1240
|
+
switch (kind) {
|
|
1241
|
+
case "custom": {
|
|
1242
|
+
return { preserveHost: true, adaptLocation: false };
|
|
1243
|
+
}
|
|
1244
|
+
case "external": {
|
|
1245
|
+
return { preserveHost: false, adaptLocation: false };
|
|
1246
|
+
}
|
|
1247
|
+
case "shopify":
|
|
1248
|
+
case "webflow": {
|
|
1249
|
+
return { preserveHost: false, adaptLocation: true };
|
|
1250
|
+
}
|
|
1251
|
+
default: {
|
|
1252
|
+
throw new Error(`Unsupported origin kind: ${String(kind)}`);
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
}, "transportFor");
|
|
1256
|
+
var adaptProviderLocation = /* @__PURE__ */ __name((response, origin, publicHost) => {
|
|
1257
|
+
const location = response.headers.get("location");
|
|
1258
|
+
if (location === null || !/^(https?:)?\/\//i.test(location) || !URL.canParse(location, origin.href)) {
|
|
1259
|
+
return response;
|
|
1260
|
+
}
|
|
1261
|
+
const target = new URL(location, origin);
|
|
1262
|
+
if (target.host !== origin.host || target.username !== "" || target.password !== "") {
|
|
1263
|
+
return response;
|
|
1264
|
+
}
|
|
1265
|
+
const headers = new Headers(response.headers);
|
|
1266
|
+
headers.set("location", location.replace(/^((?:https?:)?\/\/)[^/?#]+/i, `$1${publicHost}`));
|
|
1267
|
+
return new Response(response.body, { status: response.status, statusText: response.statusText, headers });
|
|
1268
|
+
}, "adaptProviderLocation");
|
|
1269
|
+
var forwardOrigin = /* @__PURE__ */ __name(async (request, destination) => {
|
|
1270
|
+
const origin = new URL(destination.baseUrl);
|
|
1271
|
+
const { preserveHost, adaptLocation } = transportFor(destination.kind);
|
|
1272
|
+
if (preserveHost && (origin.port !== "" || new URL(request.url).port !== "")) {
|
|
1273
|
+
throw new Error("Custom origin transport requires default HTTP/HTTPS ports");
|
|
1274
|
+
}
|
|
1275
|
+
const url = preserveHost ? request.url.replace(/^http:/, "https:") : request.url.replace(/^https?:\/\/[^/]+/, origin.origin);
|
|
1276
|
+
const upstream = new Request(url, request);
|
|
1277
|
+
upstream.headers.delete(PROXY_ORIGIN_HEADER);
|
|
1278
|
+
if (!preserveHost) {
|
|
1279
|
+
upstream.headers.set("host", origin.host);
|
|
1280
|
+
}
|
|
1281
|
+
const response = await tracedFetch(upstream, {
|
|
1282
|
+
redirect: "manual",
|
|
1283
|
+
signal: request.signal,
|
|
1284
|
+
cf: {
|
|
1285
|
+
...preserveHost ? { resolveOverride: origin.hostname } : {},
|
|
1286
|
+
cacheTtlByStatus: { "100-599": -1 }
|
|
1287
|
+
}
|
|
1288
|
+
});
|
|
1289
|
+
if (!adaptLocation || response.status === 101) {
|
|
1290
|
+
return response;
|
|
1291
|
+
}
|
|
1292
|
+
return adaptProviderLocation(response, origin, new URL(request.url).host);
|
|
1293
|
+
}, "forwardOrigin");
|
|
1294
|
+
|
|
1295
|
+
// src/origin-connection.ts
|
|
1296
|
+
var OriginConnection = class extends WorkerEntrypoint {
|
|
1297
|
+
static {
|
|
1298
|
+
__name(this, "OriginConnection");
|
|
1299
|
+
}
|
|
1300
|
+
fetch(request) {
|
|
1301
|
+
const destination = JSON.parse(request.headers.get(PROXY_ORIGIN_HEADER) ?? "");
|
|
1302
|
+
return forwardOrigin(request, destination);
|
|
1303
|
+
}
|
|
1304
|
+
};
|
|
1305
|
+
|
|
1306
|
+
// src/index.ts
|
|
1307
|
+
var index_default = { fetch: dispatchRequest };
|
|
1308
|
+
export {
|
|
1309
|
+
OriginConnection,
|
|
1310
|
+
index_default as default
|
|
1311
|
+
};
|