Workers/link-worker.js (22275B)
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
26import ogImage from "./og.png";
27
28const ICLOUD_SHARE_BASE = "https://www.icloud.com/share/";
29
30
31
32
33const APPLE_APP_SITE_ASSOCIATION = JSON.stringify({
34 applinks: {
35 details: [
36 { appIDs: ["7TD7PZBNXP.net.inqk.crossmate"], components: [{ "/": "/s/*" }] }
37 ]
38 }
39});
40
41
42
43
44
45const TOKEN_PATTERN = /^[A-Za-z0-9._~-]{8,128}$/;
46
47
48
49
50
51const TITLE_SEGMENT_PATTERN = /^[A-Za-z0-9_-]{1,256}$/;
52const MAX_TITLE_LENGTH = 80;
53
54
55
56
57
58const CRAWLER_PATTERN = new RegExp(
59 [
60 "facebookexternalhit", "facebot", "twitterbot", "slackbot", "discordbot",
61 "telegrambot", "whatsapp", "linkedinbot", "pinterest", "redditbot",
62 "applebot", "googlebot", "bingbot", "duckduckbot", "yandex", "mastodon",
63 "bluesky", "skypeuripreview", "iframely", "embedly", "snapchat", "vkshare",
64 "bot", "crawler", "spider", "preview"
65 ].join("|"),
66 "i"
67);
68
69export default {
70 async fetch(request) {
71 if (request.method !== "GET" && request.method !== "HEAD") {
72 return new Response("Method not allowed", { status: 405 });
73 }
74
75 const url = new URL(request.url);
76
77
78
79
80
81 if (
82 url.pathname === "/apple-app-site-association" ||
83 url.pathname === "/.well-known/apple-app-site-association"
84 ) {
85 return new Response(APPLE_APP_SITE_ASSOCIATION, {
86 headers: {
87 "Content-Type": "application/json",
88 "Cache-Control": "public, max-age=3600"
89 }
90 });
91 }
92
93 if (url.pathname === "/og.png") {
94 return new Response(ogImage, {
95 headers: {
96 "Content-Type": "image/png",
97 "Cache-Control": "public, max-age=86400"
98 }
99 });
100 }
101
102
103
104
105
106
107 const imageMatch = url.pathname.match(/^\/g\/([A-Za-z0-9_-]+)\.png$/);
108 if (imageMatch) {
109 const grid = decodeSilhouette(imageMatch[1]);
110 if (!grid || !meetsDynamicMinimum(grid)) {
111 return new Response(ogImage, {
112 headers: { "Content-Type": "image/png", "Cache-Control": "public, max-age=86400" }
113 });
114 }
115 return new Response(await renderSilhouettePNG(grid.width, grid.height, grid.blocks), {
116 headers: {
117 "Content-Type": "image/png",
118 "Cache-Control": "public, max-age=86400, immutable"
119 }
120 });
121 }
122
123 const match = url.pathname.match(/^\/s\/([^/]+)(?:\/([^/]+))?(?:\/([^/]+))?$/);
124 if (!match) {
125 return new Response("Not found", { status: 404 });
126 }
127
128 const token = match[1];
129 if (!TOKEN_PATTERN.test(token)) {
130 return new Response("Not found", { status: 404 });
131 }
132 const target = ICLOUD_SHARE_BASE + token;
133
134 const userAgent = request.headers.get("User-Agent") || "";
135 if (!CRAWLER_PATTERN.test(userAgent)) {
136
137
138 return new Response(null, {
139 status: 302,
140 headers: {
141 "Location": target,
142 "Cache-Control": "public, max-age=3600"
143 }
144 });
145 }
146
147 return new Response(previewPage(url.origin, token, target, [match[2], match[3]]), {
148 status: 200,
149 headers: {
150 "Content-Type": "text/html; charset=utf-8",
151 "Cache-Control": "public, max-age=3600"
152 }
153 });
154 }
155};
156
157function previewPage(origin, token, target, segments) {
158 const { title: gameTitle, shape } = classifySegments(segments);
159 const title = gameTitle
160 ? `Solve ‘${gameTitle}’ together on Crossmate`
161 : "Solve a crossword together on Crossmate";
162 const description =
163 "You’re invited to a collaborative crossword. " +
164 "Open the link to join the puzzle in Crossmate.";
165
166
167
168
169 const grid = shape ? decodeSilhouette(shape) : null;
170 const usesDynamicImage = grid && meetsDynamicMinimum(grid);
171 const imageURL = usesDynamicImage ? `${origin}/g/${shape}.png` : `${origin}/og.png`;
172 const imageWidth = usesDynamicImage ? silhouettePixelSize(grid.width) : null;
173 const imageHeight = usesDynamicImage ? silhouettePixelSize(grid.height) : null;
174
175
176
177
178
179
180 return `<!doctype html>
181<html lang="en">
182<head>
183<meta charset="utf-8">
184<meta name="viewport" content="width=device-width, initial-scale=1">
185<title>${escapeHTML(title)}</title>
186<meta property="og:type" content="website">
187<meta property="og:site_name" content="Crossmate">
188<meta property="og:title" content="${escapeHTML(title)}">
189<meta property="og:description" content="${description}">
190<meta property="og:image" content="${imageURL}">${imageWidth ? `
191<meta property="og:image:width" content="${imageWidth}">
192<meta property="og:image:height" content="${imageHeight}">` : ""}
193<meta property="og:url" content="${origin}/s/${token}">
194<meta name="twitter:card" content="summary">
195<meta http-equiv="refresh" content="0; url=${target}">
196</head>
197<body>
198<p><a href="${target}">Open the puzzle in Crossmate</a></p>
199</body>
200</html>
201`;
202}
203
204const WEEKDAYS = [
205 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
206];
207
208
209
210
211
212
213
214
215function parseShape(seg) {
216 if (seg.length < 2) return null;
217 const tag = seg[0];
218 let width;
219 let height;
220 let payload;
221 if (tag === "s" || tag === "f") {
222 width = height = parseInt(seg[1], 36);
223 payload = seg.slice(2);
224 } else if (tag === "S" || tag === "F") {
225 if (seg.length < 3) return null;
226 width = parseInt(seg[1], 36);
227 height = parseInt(seg[2], 36);
228 payload = seg.slice(3);
229 } else {
230 return null;
231 }
232 if (!(width >= 2 && width <= 35) || !(height >= 2 && height <= 35)) return null;
233 if (!/^[A-Za-z0-9_-]+$/.test(payload)) return null;
234
235 const symmetric = tag === "s" || tag === "S";
236 const cells = width * height;
237 const bits = symmetric ? Math.ceil(cells / 2) : cells;
238 const payloadBytes = Math.floor((payload.length * 6) / 8);
239 if (payloadBytes < Math.ceil(bits / 8)) return null;
240 return { symmetric, width, height, payload };
241}
242
243function isShapeSegment(seg) {
244 return parseShape(seg) !== null;
245}
246
247
248
249
250function classifySegments(segments) {
251 let title = null;
252 let shape = null;
253 for (const seg of segments) {
254 if (!seg) continue;
255 if (shape === null && isShapeSegment(seg)) {
256 shape = seg;
257 continue;
258 }
259 if (title === null) {
260 title = decodeTitleSegment(seg);
261 }
262 }
263 return { title, shape };
264}
265
266function decodeTitleSegment(seg) {
267 if (/^[0-6]$/.test(seg)) {
268 return `${WEEKDAYS[Number(seg)]} Crossword`;
269 }
270
271
272 return seg[0] === "t" ? decodeBase64Title(seg.slice(1)) : decodeBase64Title(seg);
273}
274
275
276
277
278
279function decodeBase64Title(encoded) {
280 if (!encoded || !TITLE_SEGMENT_PATTERN.test(encoded)) {
281 return null;
282 }
283 try {
284 const base64 = encoded.replaceAll("-", "+").replaceAll("_", "/");
285 const padded = base64 + "=".repeat((4 - (base64.length % 4)) % 4);
286 const bytes = Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
287 const text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
288 const cleaned = text.replace(/[\x00-\x1f\x7f]/g, "").trim();
289 return cleaned ? cleaned.slice(0, MAX_TITLE_LENGTH) : null;
290 } catch {
291 return null;
292 }
293}
294
295function escapeHTML(text) {
296 return text
297 .replaceAll("&", "&")
298 .replaceAll("<", "<")
299 .replaceAll(">", ">")
300 .replaceAll('"', """);
301}
302
303
304
305
306
307
308
309
310function decodeSilhouette(seg) {
311 const parsed = parseShape(seg);
312 if (!parsed) return null;
313 const { symmetric, width, height, payload } = parsed;
314 const n = width * height;
315 const storedCount = symmetric ? Math.ceil(n / 2) : n;
316 const bytes = base64urlToBytes(payload);
317 if (!bytes || bytes.length * 8 < storedCount) return null;
318
319 const stored = new Array(storedCount);
320 for (let i = 0; i < storedCount; i++) {
321 stored[i] = (bytes[i >> 3] & (0x80 >> (i & 7))) !== 0;
322 }
323 const blocks = new Array(n);
324 for (let k = 0; k < n; k++) {
325 blocks[k] = k < storedCount ? stored[k] : stored[n - 1 - k];
326 }
327 return { width, height, blocks };
328}
329
330function base64urlToBytes(s) {
331 try {
332 const base64 = s.replaceAll("-", "+").replaceAll("_", "/");
333 const padded = base64 + "=".repeat((4 - (base64.length % 4)) % 4);
334 return Uint8Array.from(atob(padded), (c) => c.charCodeAt(0));
335 } catch {
336 return null;
337 }
338}
339
340
341
342
343const CELL = 22;
344const MARGIN = 36;
345const BG = [255, 255, 255];
346const LINE = [205, 205, 205];
347const BLOCK = [0, 0, 0];
348const DYNAMIC_IMAGE_MIN_SIDE = 10;
349
350const LOGO_SOURCE_SIZE = 512;
351const LOGO_MIN_SIZE = 64;
352const LOGO_MAX_SIZE = 152;
353const LOGO_SCALE = 0.22;
354const LOGO_PADDING_SCALE = 0.035;
355const LOGO_MIN_PADDING = 10;
356const LOGO_DARK = [42, 42, 42];
357const LOGO_CORAL = [255, 135, 132];
358const LOGO_BLUE = [109, 173, 240];
359const LOGO_PALE_BLUE = [207, 226, 246];
360const LOGO_WHITE = BG;
361const LOGO_BLACK = BLOCK;
362const SHADOW_OFFSET_X = 0.035;
363const SHADOW_OFFSET_Y = 0.055;
364const SHADOW_BLUR = 0.150;
365const SHADOW_ALPHA = 0.26;
366
367function silhouettePixelSize(cells) {
368 return MARGIN * 2 + cells * CELL;
369}
370
371
372
373function meetsDynamicMinimum(grid) {
374 return Math.min(grid.width, grid.height) >= DYNAMIC_IMAGE_MIN_SIDE;
375}
376
377
378
379async function renderSilhouettePNG(cols, rows, blocks) {
380 const dimW = silhouettePixelSize(cols);
381 const dimH = silhouettePixelSize(rows);
382 const pixels = new Uint8Array(dimW * dimH * 3).fill(255);
383 const set = (x, y, color) => setRGB(pixels, dimW, x, y, color);
384
385
386
387 const extentX = cols * CELL;
388 const extentY = rows * CELL;
389 for (let i = 0; i <= cols; i++) {
390 const at = MARGIN + i * CELL;
391 for (let t = 0; t <= extentY; t++) set(at, MARGIN + t, LINE);
392 }
393 for (let i = 0; i <= rows; i++) {
394 const at = MARGIN + i * CELL;
395 for (let t = 0; t <= extentX; t++) set(MARGIN + t, at, LINE);
396 }
397
398 for (let r = 0; r < rows; r++) {
399 for (let c = 0; c < cols; c++) {
400 if (!blocks[r * cols + c]) continue;
401 const x0 = MARGIN + c * CELL;
402 const y0 = MARGIN + r * CELL;
403 for (let y = 1; y < CELL; y++) {
404 for (let x = 1; x < CELL; x++) set(x0 + x, y0 + y, BLOCK);
405 }
406 }
407 }
408
409 drawCrossmateIcon(pixels, dimW, dimH);
410 return await encodeTruecolorPNG(dimW, dimH, pixels);
411}
412
413function setRGB(pixels, width, x, y, color) {
414 const offset = (y * width + x) * 3;
415 pixels[offset] = color[0];
416 pixels[offset + 1] = color[1];
417 pixels[offset + 2] = color[2];
418}
419
420function drawCrossmateIcon(pixels, dimW, dimH) {
421
422
423 const basis = Math.min(dimW, dimH);
424 const size = clamp(Math.round(basis * LOGO_SCALE), LOGO_MIN_SIZE, LOGO_MAX_SIZE);
425 const padding = Math.max(LOGO_MIN_PADDING, Math.round(basis * LOGO_PADDING_SCALE));
426 const originX = dimW - padding - size;
427 const originY = dimH - padding - size;
428 const radius = Math.round(size * 0.135);
429 drawRoundedShadow(pixels, dimW, dimH, originX, originY, size, radius);
430
431 for (let y = 0; y < size; y++) {
432 const sourceY = Math.floor((y * LOGO_SOURCE_SIZE) / size);
433 for (let x = 0; x < size; x++) {
434 const coverage = roundedRectCoverage(x, y, size, radius);
435 if (coverage <= 0) continue;
436 const sourceX = Math.floor((x * LOGO_SOURCE_SIZE) / size);
437 blendPixel(pixels, dimW, originX + x, originY + y, crossmateIconColor(sourceX, sourceY), coverage);
438 }
439 }
440}
441
442function drawRoundedShadow(pixels, dimW, dimH, iconOriginX, iconOriginY, size, radius) {
443 const blur = Math.max(10, Math.round(size * SHADOW_BLUR));
444 const shadowX = iconOriginX + Math.round(size * SHADOW_OFFSET_X);
445 const shadowY = iconOriginY + Math.round(size * SHADOW_OFFSET_Y);
446 const minX = shadowX - blur;
447 const minY = shadowY - blur;
448 const maxX = shadowX + size + blur;
449 const maxY = shadowY + size + blur;
450
451 for (let py = minY; py < maxY; py++) {
452 if (py < 0 || py >= dimH) continue;
453 for (let px = minX; px < maxX; px++) {
454 if (px < 0 || px >= dimW) continue;
455
456 const distance = distanceToRoundedRect(px - shadowX, py - shadowY, size, radius);
457 if (distance > blur) continue;
458
459 const strength = Math.pow(1 - Math.max(distance, 0) / blur, 1.85);
460 blendPixel(pixels, dimW, px, py, BLOCK, strength * SHADOW_ALPHA);
461 }
462 }
463}
464
465function blendPixel(pixels, width, x, y, color, alpha) {
466 const offset = (y * width + x) * 3;
467 const keep = 1 - alpha;
468 pixels[offset] = Math.round(pixels[offset] * keep + color[0] * alpha);
469 pixels[offset + 1] = Math.round(pixels[offset + 1] * keep + color[1] * alpha);
470 pixels[offset + 2] = Math.round(pixels[offset + 2] * keep + color[2] * alpha);
471}
472
473function distanceToRoundedRect(x, y, size, radius) {
474 const half = size / 2;
475 const qx = Math.abs(x - half + 0.5) - (half - radius);
476 const qy = Math.abs(y - half + 0.5) - (half - radius);
477 const outsideX = Math.max(qx, 0);
478 const outsideY = Math.max(qy, 0);
479 return Math.hypot(outsideX, outsideY) + Math.min(Math.max(qx, qy), 0) - radius;
480}
481
482function roundedRectCoverage(x, y, size, radius) {
483 return clamp(0.5 - distanceToRoundedRect(x, y, size, radius), 0, 1);
484}
485
486function crossmateIconColor(x, y) {
487 if (x >= 252 && x <= 263) return LOGO_BLACK;
488 if (y >= 124 && y <= 134) return LOGO_BLACK;
489 if (y >= 390 && y <= 400) return LOGO_BLACK;
490
491 const right = x >= 264;
492 if (y < 124) return right ? LOGO_CORAL : LOGO_DARK;
493 if (y < 390) return right ? LOGO_WHITE : LOGO_BLUE;
494 return right ? LOGO_DARK : LOGO_PALE_BLUE;
495}
496
497function clamp(value, min, max) {
498 return Math.min(Math.max(value, min), max);
499}
500
501
502
503async function encodeTruecolorPNG(width, height, pixels) {
504
505 const stride = width * 3;
506 const raw = new Uint8Array(height * (stride + 1));
507 for (let y = 0; y < height; y++) {
508 raw[y * (stride + 1)] = 0;
509 raw.set(pixels.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
510 }
511
512 const ihdr = new Uint8Array(13);
513 const dv = new DataView(ihdr.buffer);
514 dv.setUint32(0, width);
515 dv.setUint32(4, height);
516 ihdr[8] = 8;
517 ihdr[9] = 2;
518
519
520 const signature = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
521 return concatBytes([
522 signature,
523 pngChunk("IHDR", ihdr),
524 pngChunk("IDAT", await zlibDeflate(raw)),
525 pngChunk("IEND", new Uint8Array(0))
526 ]);
527}
528
529async function zlibDeflate(raw) {
530 if (typeof CompressionStream === "undefined") {
531 return zlibStore(raw);
532 }
533 try {
534 const stream = new Blob([raw]).stream().pipeThrough(new CompressionStream("deflate"));
535 return new Uint8Array(await new Response(stream).arrayBuffer());
536 } catch {
537 return zlibStore(raw);
538 }
539}
540
541function pngChunk(type, data) {
542 const out = new Uint8Array(12 + data.length);
543 const dv = new DataView(out.buffer);
544 dv.setUint32(0, data.length);
545 for (let i = 0; i < 4; i++) out[4 + i] = type.charCodeAt(i);
546 out.set(data, 8);
547 dv.setUint32(8 + data.length, crc32(out.subarray(4, 8 + data.length)));
548 return out;
549}
550
551
552
553
554function zlibStore(raw) {
555 const parts = [new Uint8Array([0x78, 0x01])];
556 for (let off = 0; off < raw.length; off += 65535) {
557 const len = Math.min(65535, raw.length - off);
558 const final = off + len >= raw.length ? 1 : 0;
559 const header = new Uint8Array(5);
560 header[0] = final;
561 header[1] = len & 0xff;
562 header[2] = (len >> 8) & 0xff;
563 const nlen = ~len & 0xffff;
564 header[3] = nlen & 0xff;
565 header[4] = (nlen >> 8) & 0xff;
566 parts.push(header, raw.subarray(off, off + len));
567 }
568 const adler = new Uint8Array(4);
569 new DataView(adler.buffer).setUint32(0, adler32(raw));
570 parts.push(adler);
571 return concatBytes(parts);
572}
573
574function crc32(bytes) {
575 let crc = 0xffffffff;
576 for (let i = 0; i < bytes.length; i++) {
577 crc ^= bytes[i];
578 for (let j = 0; j < 8; j++) {
579 crc = crc & 1 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1;
580 }
581 }
582 return (crc ^ 0xffffffff) >>> 0;
583}
584
585function adler32(bytes) {
586 let a = 1;
587 let b = 0;
588 for (let i = 0; i < bytes.length; i++) {
589 a = (a + bytes[i]) % 65521;
590 b = (b + a) % 65521;
591 }
592 return ((b << 16) | a) >>> 0;
593}
594
595function concatBytes(chunks) {
596 const total = chunks.reduce((sum, c) => sum + c.length, 0);
597 const out = new Uint8Array(total);
598 let off = 0;
599 for (const c of chunks) {
600 out.set(c, off);
601 off += c.length;
602 }
603 return out;
604}