-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ts
More file actions
256 lines (228 loc) · 7.41 KB
/
utils.ts
File metadata and controls
256 lines (228 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { createHash } from 'node:crypto';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
export const DEFAULT_TIMEOUT_MS = 30_000;
export const DEFAULT_RETRIES = 1;
export const DEFAULT_RETRY_BACKOFF_MS = 2_000;
export interface FetchOptions extends RequestInit {
timeoutMs?: number;
retries?: number;
retryBackoffMs?: number;
}
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function fetchOnce(url: string, init: RequestInit, timeoutMs: number): Promise<Response> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetch(url, { ...init, signal: controller.signal });
} finally {
clearTimeout(timer);
}
}
export async function fetchWithTimeout(
url: string,
{
timeoutMs = DEFAULT_TIMEOUT_MS,
retries = DEFAULT_RETRIES,
retryBackoffMs = DEFAULT_RETRY_BACKOFF_MS,
...init
}: FetchOptions = {},
): Promise<Response> {
let attempt = 0;
while (true) {
try {
const res = await fetchOnce(url, init, timeoutMs);
const transient = res.status >= 500 && res.status < 600;
if (!transient || attempt >= retries) return res;
} catch (err) {
if (attempt >= retries) throw err;
}
attempt++;
await sleep(retryBackoffMs * attempt);
}
}
export async function fetchJson<T>(url: string, options: FetchOptions = {}): Promise<T> {
const res = await fetchWithTimeout(url, options);
if (!res.ok) {
throw new Error(`HTTP ${res.status} for ${url}`);
}
return (await res.json()) as T;
}
export async function fetchText(url: string, options: FetchOptions = {}): Promise<string> {
const res = await fetchWithTimeout(url, options);
if (!res.ok) {
throw new Error(`HTTP ${res.status} for ${url}`);
}
return await res.text();
}
export function sha1Hex(input: string): string {
return createHash('sha1').update(input).digest('hex');
}
export function stripHtml(input: string | null | undefined): string {
if (!input) return '';
return input
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<br\s*\/?>(\s*)/gi, '\n')
.replace(/<\/p>/gi, '\n')
.replace(/<\/li>/gi, '\n')
.replace(/<[^>]+>/g, ' ')
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/</gi, '<')
.replace(/>/gi, '>')
.replace(/"/gi, '"')
.replace(/'/gi, "'")
.replace(/&#(\d+);/g, (_, code: string) => String.fromCharCode(Number(code)))
.replace(/[ \t]+/g, ' ')
.replace(/\n{3,}/g, '\n\n')
.trim();
}
const ALLOWED_URL_SCHEMES = new Set(['http:', 'https:']);
export function isSafeUrl(url: string | null | undefined): boolean {
if (!url) return false;
try {
const u = new URL(url.trim());
return ALLOWED_URL_SCHEMES.has(u.protocol);
} catch {
return false;
}
}
export function normalizeUrl(url: string): string {
try {
const u = new URL(url.trim());
if (!ALLOWED_URL_SCHEMES.has(u.protocol)) return '';
u.hash = '';
const stripParams = [
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term',
'ref',
'source',
'gh_src',
'gh_jid',
];
for (const p of stripParams) u.searchParams.delete(p);
if (u.hostname.startsWith('www.')) u.hostname = u.hostname.slice(4);
if (u.pathname.endsWith('/') && u.pathname !== '/') {
u.pathname = u.pathname.replace(/\/+$/, '');
}
return u.toString();
} catch {
return '';
}
}
export function normalizeText(input: string | null | undefined): string {
if (!input) return '';
return input
.toLowerCase()
.replace(/[\p{P}\p{S}]/gu, ' ')
.replace(/\s+/g, ' ')
.trim();
}
export function relativeTime(iso: string | null | undefined, now = new Date()): string {
if (!iso) return 'unknown';
const then = new Date(iso);
if (Number.isNaN(then.getTime())) return 'unknown';
const diffMs = now.getTime() - then.getTime();
const minutes = Math.round(diffMs / 60_000);
if (minutes < 1) return 'just now';
if (minutes < 60) return `${minutes}m ago`;
const hours = Math.round(minutes / 60);
if (hours < 24) return `${hours}h ago`;
const days = Math.round(hours / 24);
if (days < 30) return `${days}d ago`;
const months = Math.round(days / 30);
if (months < 12) return `${months}mo ago`;
const years = Math.round(months / 12);
return `${years}y ago`;
}
export function withinDays(
iso: string | null | undefined,
days: number,
now = new Date(),
): boolean {
if (!iso) return false;
const then = new Date(iso);
if (Number.isNaN(then.getTime())) return false;
const diffMs = now.getTime() - then.getTime();
return diffMs >= 0 && diffMs <= days * 24 * 60 * 60 * 1000;
}
export async function writeJson(path: string, value: unknown): Promise<void> {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
}
export async function readJsonOrNull<T>(path: string): Promise<T | null> {
try {
const text = await readFile(path, 'utf8');
return JSON.parse(text) as T;
} catch {
return null;
}
}
export async function writeFileEnsured(path: string, content: string): Promise<void> {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, content, 'utf8');
}
export function isoToday(): string {
return new Date().toISOString().slice(0, 10);
}
const MONTHS = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
] as const;
// Human-readable UTC timestamp: "28 April 2026, 19:59 UTC".
// Day-month-year ordering avoids the US/EU "04/28 vs 28/04" ambiguity, and
// the spelled-out month makes the date unmistakable at a glance.
export function formatDateTimeUTC(iso: string): string {
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return iso;
const day = d.getUTCDate();
const month = MONTHS[d.getUTCMonth()];
const year = d.getUTCFullYear();
const hh = String(d.getUTCHours()).padStart(2, '0');
const mm = String(d.getUTCMinutes()).padStart(2, '0');
return `${day} ${month} ${year}, ${hh}:${mm} UTC`;
}
export function safeIso(value: string | number | undefined | null): string | null {
if (value === undefined || value === null) return null;
const d = typeof value === 'number' ? new Date(value * 1000) : new Date(value);
if (Number.isNaN(d.getTime())) return null;
return d.toISOString();
}
export function asArray<T>(value: T | T[] | undefined | null): T[] {
if (value === undefined || value === null) return [];
return Array.isArray(value) ? value : [value];
}
export function uniq<T>(items: T[]): T[] {
return Array.from(new Set(items));
}
export const DEFAULT_USER_AGENT =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 pupila/0.1';
export const RSS_HEADERS: Record<string, string> = {
'User-Agent': DEFAULT_USER_AGENT,
Accept: 'application/rss+xml, application/xml;q=0.9, text/xml;q=0.8, */*;q=0.5',
};
export const JSON_HEADERS: Record<string, string> = {
'User-Agent': DEFAULT_USER_AGENT,
Accept: 'application/json',
};
export const HTML_HEADERS: Record<string, string> = {
'User-Agent': DEFAULT_USER_AGENT,
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
};