-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.js
More file actions
542 lines (441 loc) · 15.4 KB
/
worker.js
File metadata and controls
542 lines (441 loc) · 15.4 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* Cloudflare Worker - Self-Hosted URL Shortener
*
* Features:
* - Short URL redirects: /s/{code} → full URL with UTM parameters
* - Full CRUD API with Bearer token authentication
* - Click analytics tracking per source
* - Soft delete + restore + permanent delete
* - Campaign-based UTM injection
*
* Deploy: wrangler deploy
* Companion article: https://www.iamdevbox.com/posts/building-self-hosted-url-shortener-cloudflare-workers/
*/
// KV namespace binding: URL_MAPPINGS
// Configure in wrangler.toml
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
};
export default {
async fetch(request, env) {
const url = new URL(request.url);
// Handle CORS preflight
if (request.method === 'OPTIONS') {
return new Response(null, { headers: CORS_HEADERS });
}
// Short URL redirect: /s/{code}
if (url.pathname.startsWith('/s/')) {
return handleShortUrl(url, env);
}
// API: Create short URL (authenticated)
if (url.pathname === '/api/shorten' && request.method === 'POST') {
return handleCreateShortUrl(request, env);
}
// API: Get click statistics
if (url.pathname.startsWith('/api/stats/')) {
return handleGetStats(url, env);
}
// API: List all active URLs (authenticated)
if (url.pathname === '/api/urls' && request.method === 'GET') {
return handleListUrls(request, env);
}
// API: Update URL (authenticated)
if (url.pathname.startsWith('/api/urls/') && request.method === 'PUT') {
return handleUpdateUrl(request, url, env);
}
// API: Restore deleted URL (authenticated) - must be before generic DELETE
if (url.pathname.match(/^\/api\/urls\/[^/]+\/restore$/) && request.method === 'POST') {
return handleRestoreUrl(request, url, env);
}
// API: Permanently delete URL (authenticated) - must be before generic DELETE
if (url.pathname.match(/^\/api\/urls\/[^/]+\/permanent$/) && request.method === 'DELETE') {
return handlePermanentDelete(request, url, env);
}
// API: List deleted URLs (authenticated)
if (url.pathname === '/api/urls/deleted' && request.method === 'GET') {
return handleListDeletedUrls(request, env);
}
// API: Soft delete URL (authenticated)
if (url.pathname.startsWith('/api/urls/') && request.method === 'DELETE') {
return handleDeleteUrl(request, url, env);
}
return new Response('URL Shortener - see /api/* for endpoints', { status: 200 });
},
};
/**
* Redirect /s/{code} to the target URL with UTM parameters
*/
async function handleShortUrl(url, env) {
const code = url.pathname.split('/s/')[1];
if (!code) {
return new Response('Invalid short URL', { status: 400 });
}
try {
const mapping = await env.URL_MAPPINGS.get(code, { type: 'json' });
if (!mapping || mapping.deleted) {
return new Response(null, { status: 404 });
}
const targetUrl = new URL(mapping.url);
const source = url.searchParams.get('s') || 'short';
// Inject UTM parameters for analytics tracking
targetUrl.searchParams.set('utm_source', source);
targetUrl.searchParams.set('utm_medium', 'shortlink');
targetUrl.searchParams.set('utm_campaign', mapping.campaign || 'general');
targetUrl.searchParams.set('utm_content', code);
// Track asynchronously (non-blocking)
incrementStats(code, env, source);
// 302 temporary redirect (avoids browser caching, ensures deleted URLs stop working immediately)
return Response.redirect(targetUrl.toString(), 302);
} catch (error) {
console.error('Error handling short URL:', error);
return new Response('Internal Server Error', { status: 500 });
}
}
/**
* POST /api/shorten — Create a new short URL
* Body: { url, code?, campaign?, notes? }
*/
async function handleCreateShortUrl(request, env) {
try {
if (!verifyApiKey(request, env)) {
return new Response('Unauthorized', { status: 401 });
}
const body = await request.json();
const { url: longUrl, code, campaign, notes } = body;
if (!longUrl) {
return jsonResponse({ error: 'url is required' }, 400);
}
if (notes !== undefined && notes !== null && notes !== '') {
if (typeof notes !== 'string') {
return jsonResponse({ error: 'notes must be a string' }, 400);
}
if (notes.length > 500) {
return jsonResponse({ error: 'notes must be 500 characters or less' }, 400);
}
}
const shortCode = code || await generateShortCode(env);
const existing = await env.URL_MAPPINGS.get(shortCode);
if (existing) {
return jsonResponse({ error: 'Short code already exists' }, 409);
}
const mapping = {
url: longUrl,
campaign: campaign || 'general',
created: new Date().toISOString(),
};
if (notes && notes.trim()) {
mapping.notes = notes.trim();
}
await env.URL_MAPPINGS.put(shortCode, JSON.stringify(mapping));
// Initialize stats
await env.URL_MAPPINGS.put(`stats:${shortCode}`, JSON.stringify({
total: 0,
sources: {},
}));
const baseUrl = new URL(request.url).origin;
return jsonResponse({
shortUrl: `${baseUrl}/s/${shortCode}`,
code: shortCode,
longUrl,
});
} catch (error) {
console.error('Error creating short URL:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* GET /api/stats/{code} — Get click statistics for a short URL
*/
async function handleGetStats(url, env) {
const code = url.pathname.split('/api/stats/')[1];
if (!code) {
return jsonResponse({ error: 'Invalid code' }, 400);
}
try {
const stats = await env.URL_MAPPINGS.get(`stats:${code}`, { type: 'json' });
if (!stats) {
return jsonResponse({ error: 'Stats not found' }, 404);
}
return jsonResponse(stats);
} catch (error) {
console.error('Error getting stats:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* GET /api/urls — List all active (non-deleted) URLs
* Excludes blog_post campaign URLs (auto-generated by publish pipeline)
*/
async function handleListUrls(request, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
try {
const urls = [];
let cursor = null;
do {
const listResult = await env.URL_MAPPINGS.list({ cursor });
for (const key of listResult.keys) {
if (key.name.startsWith('stats:')) continue;
const mapping = await env.URL_MAPPINGS.get(key.name, { type: 'json' });
if (!mapping || mapping.deleted) continue;
// Optionally skip auto-generated blog_post campaign URLs
// Remove this filter if you want to see all URLs
// if (mapping.campaign === 'blog_post') continue;
const stats = await env.URL_MAPPINGS.get(`stats:${key.name}`, { type: 'json' }) || {
total: 0,
sources: {},
};
const urlObj = {
code: key.name,
url: mapping.url,
campaign: mapping.campaign,
created: mapping.created,
stats,
};
if (mapping.notes) urlObj.notes = mapping.notes;
urls.push(urlObj);
}
cursor = listResult.list_complete ? null : listResult.cursor;
} while (cursor);
urls.sort((a, b) => new Date(b.created) - new Date(a.created));
return jsonResponse({ urls, total: urls.length });
} catch (error) {
console.error('Error listing URLs:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* PUT /api/urls/{code} — Update URL, campaign, code, or notes
*/
async function handleUpdateUrl(request, url, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
const oldCode = url.pathname.split('/api/urls/')[1];
if (!oldCode) {
return jsonResponse({ error: 'Invalid code' }, 400);
}
try {
const existing = await env.URL_MAPPINGS.get(oldCode, { type: 'json' });
if (!existing) {
return jsonResponse({ error: 'Short URL not found' }, 404);
}
const body = await request.json();
const { url: newUrl, campaign, code: newCode, notes } = body;
if (notes !== undefined && notes !== null) {
if (typeof notes !== 'string') {
return jsonResponse({ error: 'notes must be a string' }, 400);
}
if (notes.length > 500) {
return jsonResponse({ error: 'notes must be 500 characters or less' }, 400);
}
}
const updated = {
url: newUrl || existing.url,
campaign: campaign !== undefined ? campaign : existing.campaign,
created: existing.created,
updated: new Date().toISOString(),
};
if (notes !== undefined) {
if (notes.trim()) updated.notes = notes.trim();
} else if (existing.notes) {
updated.notes = existing.notes;
}
if (newCode && newCode !== oldCode) {
const existingNew = await env.URL_MAPPINGS.get(newCode);
if (existingNew) {
return jsonResponse({ error: 'New code already exists' }, 409);
}
await env.URL_MAPPINGS.put(newCode, JSON.stringify(updated));
const stats = await env.URL_MAPPINGS.get(`stats:${oldCode}`, { type: 'json' });
if (stats) {
await env.URL_MAPPINGS.put(`stats:${newCode}`, JSON.stringify(stats));
await env.URL_MAPPINGS.delete(`stats:${oldCode}`);
}
await env.URL_MAPPINGS.delete(oldCode);
return jsonResponse({ code: newCode, oldCode, ...updated });
}
await env.URL_MAPPINGS.put(oldCode, JSON.stringify(updated));
return jsonResponse({ code: oldCode, ...updated });
} catch (error) {
console.error('Error updating URL:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* DELETE /api/urls/{code} — Soft delete (marks deleted=true, preserves data)
*/
async function handleDeleteUrl(request, url, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
const code = url.pathname.split('/api/urls/')[1];
if (!code) {
return jsonResponse({ error: 'Invalid code' }, 400);
}
try {
const existing = await env.URL_MAPPINGS.get(code, { type: 'json' });
if (!existing) {
return jsonResponse({ error: 'Short URL not found' }, 404);
}
const updated = {
...existing,
deleted: true,
deletedAt: new Date().toISOString(),
};
await env.URL_MAPPINGS.put(code, JSON.stringify(updated));
return jsonResponse({ deleted: true, code });
} catch (error) {
console.error('Error deleting URL:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* POST /api/urls/{code}/restore — Restore a soft-deleted URL
*/
async function handleRestoreUrl(request, url, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
const match = url.pathname.match(/^\/api\/urls\/([^/]+)\/restore$/);
const code = match ? match[1] : null;
if (!code) {
return jsonResponse({ error: 'Invalid code' }, 400);
}
try {
const existing = await env.URL_MAPPINGS.get(code, { type: 'json' });
if (!existing) {
return jsonResponse({ error: 'Short URL not found' }, 404);
}
if (!existing.deleted) {
return jsonResponse({ error: 'Short URL is not deleted' }, 400);
}
const restored = { ...existing, deleted: false, restoredAt: new Date().toISOString() };
delete restored.deletedAt;
await env.URL_MAPPINGS.put(code, JSON.stringify(restored));
return jsonResponse({ restored: true, code, url: restored.url });
} catch (error) {
console.error('Error restoring URL:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* GET /api/urls/deleted — List all soft-deleted URLs
*/
async function handleListDeletedUrls(request, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
try {
const urls = [];
let cursor = null;
do {
const listResult = await env.URL_MAPPINGS.list({ cursor });
for (const key of listResult.keys) {
if (key.name.startsWith('stats:')) continue;
const mapping = await env.URL_MAPPINGS.get(key.name, { type: 'json' });
if (mapping && mapping.deleted) {
const urlObj = {
code: key.name,
url: mapping.url,
campaign: mapping.campaign,
created: mapping.created,
deletedAt: mapping.deletedAt,
};
if (mapping.notes) urlObj.notes = mapping.notes;
urls.push(urlObj);
}
}
cursor = listResult.list_complete ? null : listResult.cursor;
} while (cursor);
urls.sort((a, b) => new Date(b.deletedAt) - new Date(a.deletedAt));
return jsonResponse({ urls, total: urls.length });
} catch (error) {
console.error('Error listing deleted URLs:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* DELETE /api/urls/{code}/permanent — Permanently delete from KV (irreversible)
*/
async function handlePermanentDelete(request, url, env) {
if (!verifyApiKey(request, env)) {
return jsonResponse({ error: 'Unauthorized' }, 401);
}
const match = url.pathname.match(/^\/api\/urls\/([^/]+)\/permanent$/);
const code = match ? match[1] : null;
if (!code) {
return jsonResponse({ error: 'Invalid code' }, 400);
}
try {
const existing = await env.URL_MAPPINGS.get(code, { type: 'json' });
if (!existing) {
return jsonResponse({ error: 'Short URL not found' }, 404);
}
if (!existing.deleted) {
return jsonResponse({ error: 'Only soft-deleted URLs can be permanently removed' }, 400);
}
await env.URL_MAPPINGS.delete(code);
await env.URL_MAPPINGS.delete(`stats:${code}`);
return jsonResponse({ permanentlyDeleted: true, code });
} catch (error) {
console.error('Error permanently deleting URL:', error);
return jsonResponse({ error: 'Internal Server Error' }, 500);
}
}
/**
* Increment click stats asynchronously (non-blocking)
*/
async function incrementStats(code, env, source) {
try {
const statsKey = `stats:${code}`;
const stats = await env.URL_MAPPINGS.get(statsKey, { type: 'json' }) || {
total: 0,
sources: {},
};
stats.total += 1;
stats.sources[source] = (stats.sources[source] || 0) + 1;
stats.lastAccess = new Date().toISOString();
await env.URL_MAPPINGS.put(statsKey, JSON.stringify(stats));
} catch (error) {
console.error('Error incrementing stats:', error);
}
}
/**
* Generate a unique numeric short code (starts at 10, increments)
* Codes 0-9 are reserved for manual use
*/
async function generateShortCode(env) {
let num = 10;
const maxAttempts = 10000;
while (num < maxAttempts) {
const code = String(num);
const existing = await env.URL_MAPPINGS.get(code);
if (!existing) return code;
num++;
}
throw new Error('Failed to generate unique short code');
}
/**
* Verify Bearer token API key
*/
function verifyApiKey(request, env) {
const authHeader = request.headers.get('Authorization');
const apiKey = env.API_KEY;
return authHeader && authHeader === `Bearer ${apiKey}`;
}
/**
* Return JSON response with CORS headers
*/
function jsonResponse(data, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: {
'Content-Type': 'application/json',
...CORS_HEADERS,
},
});
}