@bluefields/primitives 0.2.0
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/LICENSE +202 -0
- package/dist/canonicalize.d.ts +37 -0
- package/dist/canonicalize.js +196 -0
- package/dist/canonicalize.js.map +1 -0
- package/dist/env.d.ts +7 -0
- package/dist/env.js +15 -0
- package/dist/env.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +63 -0
- package/dist/logger.js +118 -0
- package/dist/logger.js.map +1 -0
- package/dist/robots-txt.d.ts +65 -0
- package/dist/robots-txt.js +250 -0
- package/dist/robots-txt.js.map +1 -0
- package/dist/ssrf.d.ts +77 -0
- package/dist/ssrf.js +432 -0
- package/dist/ssrf.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* URL canonicalization v1 (Review #2).
|
|
3
|
+
*
|
|
4
|
+
* Turns a customer-submitted URL into a stable identifier used everywhere
|
|
5
|
+
* we key on URL: snapshots, extractions cache, subscriptions index, cost
|
|
6
|
+
* records. Get it wrong and we either fail to dedup (paying twice for the
|
|
7
|
+
* same content) or over-dedup (treating different pages as the same).
|
|
8
|
+
*
|
|
9
|
+
* The schema records `canon_version` on every row that carries a
|
|
10
|
+
* canonicalized URL, so we can roll a v2 ruleset later without rewriting
|
|
11
|
+
* history. NEVER edit this file in place — if rules change, copy to
|
|
12
|
+
* canonicalize.v2.ts, bump CANON_VERSION, and route via a version
|
|
13
|
+
* dispatcher.
|
|
14
|
+
*
|
|
15
|
+
* Inputs: a URL string as submitted by a customer. Must parse under the
|
|
16
|
+
* WHATWG URL Standard.
|
|
17
|
+
*
|
|
18
|
+
* Outputs three values:
|
|
19
|
+
* - url_canonical : canonical form for display + indexing
|
|
20
|
+
* - url_surt : sort-friendly reverse-domain key for host clustering
|
|
21
|
+
* - canon_version : integer ruleset version (1 for this file)
|
|
22
|
+
*/
|
|
23
|
+
export declare const CANON_VERSION: 1;
|
|
24
|
+
export declare class InvalidUrlError extends Error {
|
|
25
|
+
constructor(message: string);
|
|
26
|
+
}
|
|
27
|
+
export interface CanonicalUrl {
|
|
28
|
+
url_canonical: string;
|
|
29
|
+
url_surt: string;
|
|
30
|
+
canon_version: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Canonicalize a URL. Throws InvalidUrlError on parse failure or
|
|
34
|
+
* unsupported scheme. The returned object is safe to persist as
|
|
35
|
+
* (url_canonical, url_surt, canon_version) into the schema.
|
|
36
|
+
*/
|
|
37
|
+
export declare function canonicalizeUrl(input: string): CanonicalUrl;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* URL canonicalization v1 (Review #2).
|
|
4
|
+
*
|
|
5
|
+
* Turns a customer-submitted URL into a stable identifier used everywhere
|
|
6
|
+
* we key on URL: snapshots, extractions cache, subscriptions index, cost
|
|
7
|
+
* records. Get it wrong and we either fail to dedup (paying twice for the
|
|
8
|
+
* same content) or over-dedup (treating different pages as the same).
|
|
9
|
+
*
|
|
10
|
+
* The schema records `canon_version` on every row that carries a
|
|
11
|
+
* canonicalized URL, so we can roll a v2 ruleset later without rewriting
|
|
12
|
+
* history. NEVER edit this file in place — if rules change, copy to
|
|
13
|
+
* canonicalize.v2.ts, bump CANON_VERSION, and route via a version
|
|
14
|
+
* dispatcher.
|
|
15
|
+
*
|
|
16
|
+
* Inputs: a URL string as submitted by a customer. Must parse under the
|
|
17
|
+
* WHATWG URL Standard.
|
|
18
|
+
*
|
|
19
|
+
* Outputs three values:
|
|
20
|
+
* - url_canonical : canonical form for display + indexing
|
|
21
|
+
* - url_surt : sort-friendly reverse-domain key for host clustering
|
|
22
|
+
* - canon_version : integer ruleset version (1 for this file)
|
|
23
|
+
*/
|
|
24
|
+
import { isIPv4, isIPv6 } from 'node:net';
|
|
25
|
+
export const CANON_VERSION = 1;
|
|
26
|
+
export class InvalidUrlError extends Error {
|
|
27
|
+
constructor(message) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'InvalidUrlError';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// ── Tracking-parameter strip lists ──────────────────────────────────────
|
|
33
|
+
const STRIP_PARAMS_GLOBAL = new Set([
|
|
34
|
+
// Google Analytics / Ads
|
|
35
|
+
'utm_source',
|
|
36
|
+
'utm_medium',
|
|
37
|
+
'utm_campaign',
|
|
38
|
+
'utm_term',
|
|
39
|
+
'utm_content',
|
|
40
|
+
'utm_id',
|
|
41
|
+
'_ga',
|
|
42
|
+
'_gl',
|
|
43
|
+
'gclid',
|
|
44
|
+
'gclsrc',
|
|
45
|
+
'dclid',
|
|
46
|
+
// Facebook
|
|
47
|
+
'fbclid',
|
|
48
|
+
'fb_action_ids',
|
|
49
|
+
'fb_action_types',
|
|
50
|
+
'fb_source',
|
|
51
|
+
// Microsoft
|
|
52
|
+
'msclkid',
|
|
53
|
+
// Mailchimp
|
|
54
|
+
'mc_eid',
|
|
55
|
+
'mc_cid',
|
|
56
|
+
// Instagram
|
|
57
|
+
'igshid',
|
|
58
|
+
// YouTube — `t` is a timestamp deep-link, kept; `si` is tracking, stripped
|
|
59
|
+
'feature',
|
|
60
|
+
'si',
|
|
61
|
+
// Generic tracking
|
|
62
|
+
'ref',
|
|
63
|
+
'ref_src',
|
|
64
|
+
'ref_url',
|
|
65
|
+
'referrer',
|
|
66
|
+
'share',
|
|
67
|
+
'share_id',
|
|
68
|
+
'_branch_match_id',
|
|
69
|
+
]);
|
|
70
|
+
// Host-conditional strip rules. `s` and `t` are tracking on Twitter/X but
|
|
71
|
+
// meaningful (e.g. timestamps) elsewhere; only strip them on these hosts.
|
|
72
|
+
const TWITTER_HOSTS = new Set([
|
|
73
|
+
'twitter.com',
|
|
74
|
+
'www.twitter.com',
|
|
75
|
+
'x.com',
|
|
76
|
+
'www.x.com',
|
|
77
|
+
't.co',
|
|
78
|
+
]);
|
|
79
|
+
const STRIP_PARAMS_TWITTER = new Set(['s', 't']);
|
|
80
|
+
// ── Public API ──────────────────────────────────────────────────────────
|
|
81
|
+
/**
|
|
82
|
+
* Canonicalize a URL. Throws InvalidUrlError on parse failure or
|
|
83
|
+
* unsupported scheme. The returned object is safe to persist as
|
|
84
|
+
* (url_canonical, url_surt, canon_version) into the schema.
|
|
85
|
+
*/
|
|
86
|
+
export function canonicalizeUrl(input) {
|
|
87
|
+
let parsed;
|
|
88
|
+
try {
|
|
89
|
+
parsed = new URL(input);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new InvalidUrlError(`could not parse: ${input}`);
|
|
93
|
+
}
|
|
94
|
+
// Rule 2: scheme allowlist (http/https only, lowercased).
|
|
95
|
+
const scheme = parsed.protocol.slice(0, -1).toLowerCase();
|
|
96
|
+
if (scheme !== 'http' && scheme !== 'https') {
|
|
97
|
+
throw new InvalidUrlError(`unsupported scheme: ${scheme}`);
|
|
98
|
+
}
|
|
99
|
+
// Rule 3: WHATWG URL already lowercases the host and converts IDN to
|
|
100
|
+
// Punycode. WHATWG also returns IPv6 hostnames in `[...]` form; strip
|
|
101
|
+
// brackets here so SURT computation gets the bare host.
|
|
102
|
+
const rawHost = parsed.hostname.toLowerCase();
|
|
103
|
+
const hostNoBrackets = rawHost.startsWith('[') && rawHost.endsWith(']') ? rawHost.slice(1, -1) : rawHost;
|
|
104
|
+
// Rule 4: drop default ports.
|
|
105
|
+
const isDefaultPort = (scheme === 'http' && (parsed.port === '' || parsed.port === '80')) ||
|
|
106
|
+
(scheme === 'https' && (parsed.port === '' || parsed.port === '443'));
|
|
107
|
+
const portPart = isDefaultPort ? '' : `:${parsed.port}`;
|
|
108
|
+
// Rules 5-7: preserve path case + trailing slash; decode unreserved %xx;
|
|
109
|
+
// uppercase remaining %xx hex.
|
|
110
|
+
const path = normalizePathPercentEncoding(parsed.pathname);
|
|
111
|
+
// Rule 9: query handling — strip tracking params, sort the rest.
|
|
112
|
+
const queryString = canonicalizeQuery(parsed.searchParams, hostNoBrackets);
|
|
113
|
+
// Rule 10: reassemble. Drop fragment (Rule 8) by simply not appending it.
|
|
114
|
+
const url_canonical = `${scheme}://${rawHost}${portPart}${path}${queryString}`;
|
|
115
|
+
// SURT for host-level clustering and ops. See computeSurt() docstring.
|
|
116
|
+
const url_surt = computeSurt(hostNoBrackets, portPart, path, queryString);
|
|
117
|
+
return { url_canonical, url_surt, canon_version: CANON_VERSION };
|
|
118
|
+
}
|
|
119
|
+
// ── Internals ──────────────────────────────────────────────────────────
|
|
120
|
+
/**
|
|
121
|
+
* Decode percent-encoded unreserved characters per RFC 3986 §6.2.2.2,
|
|
122
|
+
* then uppercase any remaining percent-encoded hex. WHATWG's URL parser
|
|
123
|
+
* normalizes some of this but is inconsistent across runtimes; we
|
|
124
|
+
* normalize explicitly so the output is deterministic.
|
|
125
|
+
*
|
|
126
|
+
* Unreserved per RFC 3986: A-Z a-z 0-9 - . _ ~
|
|
127
|
+
*/
|
|
128
|
+
function normalizePathPercentEncoding(path) {
|
|
129
|
+
return path.replace(/%([0-9A-Fa-f]{2})/g, (_match, hex) => {
|
|
130
|
+
const code = Number.parseInt(hex, 16);
|
|
131
|
+
if (
|
|
132
|
+
// 0-9
|
|
133
|
+
(code >= 0x30 && code <= 0x39) ||
|
|
134
|
+
// A-Z
|
|
135
|
+
(code >= 0x41 && code <= 0x5a) ||
|
|
136
|
+
// a-z
|
|
137
|
+
(code >= 0x61 && code <= 0x7a) ||
|
|
138
|
+
// - . _ ~
|
|
139
|
+
code === 0x2d ||
|
|
140
|
+
code === 0x2e ||
|
|
141
|
+
code === 0x5f ||
|
|
142
|
+
code === 0x7e) {
|
|
143
|
+
return String.fromCharCode(code);
|
|
144
|
+
}
|
|
145
|
+
return `%${hex.toUpperCase()}`;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function canonicalizeQuery(params, host) {
|
|
149
|
+
const isTwitterHost = TWITTER_HOSTS.has(host);
|
|
150
|
+
// Collect kept (key, value) pairs, drop strip-listed.
|
|
151
|
+
const kept = [];
|
|
152
|
+
for (const [key, value] of params) {
|
|
153
|
+
if (STRIP_PARAMS_GLOBAL.has(key))
|
|
154
|
+
continue;
|
|
155
|
+
if (isTwitterHost && STRIP_PARAMS_TWITTER.has(key))
|
|
156
|
+
continue;
|
|
157
|
+
kept.push([key, value]);
|
|
158
|
+
}
|
|
159
|
+
if (kept.length === 0)
|
|
160
|
+
return '';
|
|
161
|
+
// Stable sort: primary by key, secondary by original order (preserved
|
|
162
|
+
// because Array.prototype.sort is stable in Node 12+).
|
|
163
|
+
kept.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
164
|
+
const sp = new URLSearchParams();
|
|
165
|
+
for (const [k, v] of kept)
|
|
166
|
+
sp.append(k, v);
|
|
167
|
+
return `?${sp.toString()}`;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* SURT (Sort-friendly URI Reordering Transform) — IA's canonical key for
|
|
171
|
+
* host-level clustering. Reverses the dotted hostname into a sortable
|
|
172
|
+
* comma-separated key. Example:
|
|
173
|
+
*
|
|
174
|
+
* https://www.example.com/path?q=1
|
|
175
|
+
* → com,example,www,/path?q=1
|
|
176
|
+
*
|
|
177
|
+
* For IP-literal hosts (IPv4 or IPv6) there's no hierarchical structure
|
|
178
|
+
* to reverse, so we use the literal host as-is. IPv6 is wrapped in
|
|
179
|
+
* brackets for unambiguous parsing.
|
|
180
|
+
*/
|
|
181
|
+
function computeSurt(host, portPart, path, queryString) {
|
|
182
|
+
let surtHost;
|
|
183
|
+
if (isIPv4(host)) {
|
|
184
|
+
surtHost = host;
|
|
185
|
+
}
|
|
186
|
+
else if (isIPv6(host)) {
|
|
187
|
+
surtHost = `[${host}]`;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
// Reverse the dotted segments. "www.example.com" -> "com,example,www"
|
|
191
|
+
const segments = host.split('.').filter((s) => s.length > 0);
|
|
192
|
+
surtHost = segments.reverse().join(',');
|
|
193
|
+
}
|
|
194
|
+
return `${surtHost}${portPart},${path}${queryString}`;
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=canonicalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"canonicalize.js","sourceRoot":"","sources":["../src/canonicalize.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,CAAC,MAAM,aAAa,GAAG,CAAU,CAAC;AAExC,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACxC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAQD,2EAA2E;AAE3E,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAS;IAC1C,yBAAyB;IACzB,YAAY;IACZ,YAAY;IACZ,cAAc;IACd,UAAU;IACV,aAAa;IACb,QAAQ;IACR,KAAK;IACL,KAAK;IACL,OAAO;IACP,QAAQ;IACR,OAAO;IACP,WAAW;IACX,QAAQ;IACR,eAAe;IACf,iBAAiB;IACjB,WAAW;IACX,YAAY;IACZ,SAAS;IACT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AC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,MAAM,EAAE,GAAG,IAAI,eAAe,EAAE,CAAC;IACjC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,IAAI;QAAE,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC3C,OAAO,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC;AAC7B,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,WAAW,CAAC,IAAY,EAAE,QAAgB,EAAE,IAAY,EAAE,WAAmB;IACpF,IAAI,QAAgB,CAAC;IACrB,IAAI,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACjB,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC;SAAM,IAAI,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,QAAQ,GAAG,IAAI,IAAI,GAAG,CAAC;IACzB,CAAC;SAAM,CAAC;QACN,sEAAsE;QACtE,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAC7D,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,GAAG,QAAQ,GAAG,QAAQ,IAAI,IAAI,GAAG,WAAW,EAAE,CAAC;AACxD,CAAC"}
|
package/dist/env.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read a positive-integer operational knob from the environment, falling back
|
|
3
|
+
* to the compiled default. Lets an operator tune timeouts / concurrency /
|
|
4
|
+
* thresholds per-deploy without a code change; a missing, non-numeric, or
|
|
5
|
+
* non-positive value uses the fallback (these knobs are never legitimately 0).
|
|
6
|
+
*/
|
|
7
|
+
export declare function envInt(name: string, fallback: number): number;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Read a positive-integer operational knob from the environment, falling back
|
|
4
|
+
* to the compiled default. Lets an operator tune timeouts / concurrency /
|
|
5
|
+
* thresholds per-deploy without a code change; a missing, non-numeric, or
|
|
6
|
+
* non-positive value uses the fallback (these knobs are never legitimately 0).
|
|
7
|
+
*/
|
|
8
|
+
export function envInt(name, fallback) {
|
|
9
|
+
const raw = process.env[name];
|
|
10
|
+
if (raw === undefined || raw === '')
|
|
11
|
+
return fallback;
|
|
12
|
+
const n = Number(raw);
|
|
13
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,IAAY,EAAE,QAAgB;IACnD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9B,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,EAAE;QAAE,OAAO,QAAQ,CAAC;IACrD,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACtB,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;AAChE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @bluefields/primitives — pure, secret-free infrastructure primitives shared
|
|
3
|
+
* by the open engine (fetcher/extractor) and the private control plane.
|
|
4
|
+
*
|
|
5
|
+
* Carved out of @bluefields/control so the engine can be open-sourced without
|
|
6
|
+
* dragging in billing/customers/quotas. Everything here is deterministic
|
|
7
|
+
* library code with no database, no secrets, and no business/policy: URL
|
|
8
|
+
* canonicalization, env parsing, structured logging, SSRF-safe fetching, and
|
|
9
|
+
* robots.txt parsing/evaluation. (Plan-based robots POLICY stays in control —
|
|
10
|
+
* that is business logic, not a primitive.)
|
|
11
|
+
*/
|
|
12
|
+
export * from './canonicalize.js';
|
|
13
|
+
export * from './env.js';
|
|
14
|
+
export * from './logger.js';
|
|
15
|
+
export * from './ssrf.js';
|
|
16
|
+
export * from './robots-txt.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* @bluefields/primitives — pure, secret-free infrastructure primitives shared
|
|
4
|
+
* by the open engine (fetcher/extractor) and the private control plane.
|
|
5
|
+
*
|
|
6
|
+
* Carved out of @bluefields/control so the engine can be open-sourced without
|
|
7
|
+
* dragging in billing/customers/quotas. Everything here is deterministic
|
|
8
|
+
* library code with no database, no secrets, and no business/policy: URL
|
|
9
|
+
* canonicalization, env parsing, structured logging, SSRF-safe fetching, and
|
|
10
|
+
* robots.txt parsing/evaluation. (Plan-based robots POLICY stays in control —
|
|
11
|
+
* that is business logic, not a primitive.)
|
|
12
|
+
*/
|
|
13
|
+
export * from './canonicalize.js';
|
|
14
|
+
export * from './env.js';
|
|
15
|
+
export * from './logger.js';
|
|
16
|
+
export * from './ssrf.js';
|
|
17
|
+
export * from './robots-txt.js';
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC;;;;;;;;;;GAUG;AAEH,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC"}
|
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured logging (pino, JSON output).
|
|
3
|
+
*
|
|
4
|
+
* One root logger; child loggers per layer to attach consistent bindings
|
|
5
|
+
* without callers having to remember them.
|
|
6
|
+
*
|
|
7
|
+
* ## Conventions
|
|
8
|
+
*
|
|
9
|
+
* Every log call passes a structured object as the first argument with these
|
|
10
|
+
* fields when relevant:
|
|
11
|
+
* - layer: 'poll' | 'fetcher' | 'extractor' | 'differ' | 'events' | 'api' | 'control'
|
|
12
|
+
* - subscription_id: UUID
|
|
13
|
+
* - event_id: UUID
|
|
14
|
+
* - customer_id: UUID
|
|
15
|
+
* - duration_ms: int
|
|
16
|
+
* - outcome: 'ok' | 'cached' | 'blocked' | 'skipped' | 'error' | 'partial'
|
|
17
|
+
*
|
|
18
|
+
* The message string is the second argument and stays short — searchable
|
|
19
|
+
* tokens, not prose.
|
|
20
|
+
*
|
|
21
|
+
* ## What NOT to log
|
|
22
|
+
*
|
|
23
|
+
* Configured via pino's `redact` so accidental inclusion in a bindings
|
|
24
|
+
* object or a log argument silently becomes `[Redacted]`:
|
|
25
|
+
* - webhook secrets (current + previous)
|
|
26
|
+
* - API keys (full + hash)
|
|
27
|
+
* - Stripe webhook signing secret
|
|
28
|
+
* - Authorization headers + bearer tokens
|
|
29
|
+
* - Anthropic API key
|
|
30
|
+
* - Resend API key
|
|
31
|
+
* - Raw HTML / extraction body — caller's responsibility (we just don't log it)
|
|
32
|
+
*
|
|
33
|
+
* Raw HTML and raw extraction output aren't redactable by path (the field
|
|
34
|
+
* names vary by call site); the convention is: log size + hash, never bytes.
|
|
35
|
+
* Reviewers should grep `logger.*(.*html|.*markdown|.*structuredData|.*body:)`
|
|
36
|
+
* before merging changes that touch logging.
|
|
37
|
+
*/
|
|
38
|
+
import { type Logger as PinoLogger } from 'pino';
|
|
39
|
+
export type Logger = PinoLogger;
|
|
40
|
+
/** Layers the logger knows about — keep in sync with the four-layer rule. */
|
|
41
|
+
export type LogLayer = 'poll' | 'fetcher' | 'extractor' | 'differ' | 'events' | 'api' | 'control' | 'crawl';
|
|
42
|
+
/** Outcome enum — same set used in PollOutcome and DeliveryHttpResult. */
|
|
43
|
+
export type LogOutcome = 'ok' | 'cached' | 'blocked' | 'skipped' | 'error' | 'partial' | 'timeout' | 'non_2xx';
|
|
44
|
+
/**
|
|
45
|
+
* Build the root logger. Idempotent — subsequent calls return the same
|
|
46
|
+
* instance. Tests can pass a custom `level` override.
|
|
47
|
+
*/
|
|
48
|
+
export declare function getRootLogger(env?: NodeJS.ProcessEnv): Logger;
|
|
49
|
+
/**
|
|
50
|
+
* Build a child logger pre-bound to a layer (and optional subscription /
|
|
51
|
+
* customer / event ids). Pass through callers so log calls don't have to
|
|
52
|
+
* repeat the same bindings.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createLayerLogger(layer: LogLayer, bindings?: LayerBindings, env?: NodeJS.ProcessEnv): Logger;
|
|
55
|
+
export interface LayerBindings {
|
|
56
|
+
subscription_id?: string;
|
|
57
|
+
event_id?: string;
|
|
58
|
+
customer_id?: string;
|
|
59
|
+
/** Inngest function id for grouping per-function logs. */
|
|
60
|
+
inngest_function?: string;
|
|
61
|
+
}
|
|
62
|
+
/** Test-only — reset the cached root logger. */
|
|
63
|
+
export declare function _resetLoggerForTests(): void;
|