media.ts (4929B)
1 import { boundedUtf8 } from './text.ts'; 2 export type ImageDiagnostic = 3 | 'food_image_shape_invalid' 4 | 'food_image_url_invalid' 5 | 'food_image_dimensions_missing' 6 | 'food_image_dimensions_invalid' 7 | 'food_image_duplicate_url' 8 | 'food_image_duplicate_digest' 9 | 'food_image_count_exceeded'; 10 export type InboundImage = Readonly<{ 11 raw_tag: readonly string[]; 12 url: string | null; 13 dimensions: Readonly<{ width: number; height: number }> | null; 14 diagnostics: readonly ImageDiagnostic[]; 15 qualifies: boolean; 16 }>; 17 function structuralUrl(value: string): URL | undefined { 18 if ( 19 !boundedUtf8(value, 4096) || 20 !value.includes('://') || 21 /[\p{White_Space}\p{Cc}\p{Cf}]/u.test(value) 22 ) 23 return undefined; 24 try { 25 const url = new URL(value); 26 if ( 27 !['http:', 'https:'].includes(url.protocol) || 28 url.username || 29 url.password 30 ) 31 return undefined; 32 const rest = value.slice(value.indexOf('://') + 3); 33 const end = rest.search(/[/?#]/u); 34 const authority = end < 0 ? rest : rest.slice(0, end); 35 if (!authority || authority.includes('@')) return undefined; 36 const suffix = end < 0 ? '' : rest.slice(end); 37 const path = suffix.split(/[?#]/u)[0]; 38 if (!path.startsWith('/')) return undefined; 39 let host: string; 40 if (authority.startsWith('[')) { 41 const close = authority.indexOf(']'); 42 if (close < 0) return undefined; 43 const after = authority.slice(close + 1); 44 if (after && !after.startsWith(':')) return undefined; 45 host = authority.slice(1, close); 46 } else { 47 const colon = authority.lastIndexOf(':'); 48 host = colon < 0 ? authority : authority.slice(0, colon); 49 if (host.includes(':')) return undefined; 50 } 51 if ( 52 !host || 53 Array.from(host).some((character) => character.codePointAt(0)! > 127) 54 ) 55 return undefined; 56 const ip = 57 url.hostname.startsWith('[') || 58 /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/u.test(url.hostname); 59 if ( 60 !ip && 61 (host.length > 253 || 62 !host 63 .split('.') 64 .every( 65 (label) => 66 label.length <= 63 && 67 /^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/u.test(label) 68 )) 69 ) 70 return undefined; 71 return url; 72 } catch { 73 return undefined; 74 } 75 } 76 function dimension(value: string): number | undefined { 77 if ( 78 value.length === 0 || 79 value.length > 10 || 80 !/^[1-9][0-9]*$/u.test(value) || 81 (value.length === 10 && value > '4294967295') 82 ) 83 return undefined; 84 return Number(value); 85 } 86 function dimensions(value: string): { width: number; height: number } | null { 87 if (value.length > 21) return null; 88 const parts = value.split('x'); 89 if (parts.length !== 2) return null; 90 const width = dimension(parts[0]); 91 const height = dimension(parts[1]); 92 return width === undefined || height === undefined ? null : { width, height }; 93 } 94 function digest(url: URL): string | undefined { 95 // Public tolerant reader extracts only a structural root Blossom hash path. 96 // Query/fragment do not change the path; no download or byte proof occurs. 97 const match = /^\/([0-9a-f]{64})(?:\.([a-zA-Z0-9_.-]+))?$/u.exec( 98 url.pathname 99 ); 100 if ( 101 !match || 102 (match[2] && match[2].split('.').some((part) => part.length === 0)) 103 ) 104 return undefined; 105 return match[1]; 106 } 107 export function projectImages(tags: readonly (readonly string[])[]): Readonly<{ 108 images: readonly InboundImage[]; 109 diagnostics: readonly ImageDiagnostic[]; 110 }> { 111 const bounded = tags.slice(0, 64); 112 const images: InboundImage[] = bounded.map((tag, index) => { 113 const row: ImageDiagnostic[] = []; 114 if (tag.length !== 3) row.push('food_image_shape_invalid'); 115 const parsed = tag[1] === undefined ? undefined : structuralUrl(tag[1]); 116 if (!parsed) row.push('food_image_url_invalid'); 117 const size = tag[2] === undefined ? null : dimensions(tag[2]); 118 if (tag[2] === undefined) row.push('food_image_dimensions_missing'); 119 else if (!size) row.push('food_image_dimensions_invalid'); 120 const previous = bounded.slice(0, index); 121 if (tag[1] !== undefined) { 122 if (previous.some((old) => old[1] === tag[1])) 123 row.push('food_image_duplicate_url'); 124 const hash = parsed ? digest(parsed) : undefined; 125 if ( 126 hash && 127 previous.some((old) => { 128 const url = old[1] === undefined ? undefined : structuralUrl(old[1]); 129 return url !== undefined && digest(url) === hash; 130 }) 131 ) 132 row.push('food_image_duplicate_digest'); 133 } 134 return { 135 raw_tag: [...tag], 136 url: parsed ? tag[1] : null, 137 dimensions: size, 138 diagnostics: row, 139 qualifies: row.length === 0 140 }; 141 }); 142 const overflow: ImageDiagnostic[] = 143 tags.length > 64 ? ['food_image_count_exceeded'] : []; 144 return { 145 images, 146 diagnostics: [...overflow, ...images.flatMap((image) => image.diagnostics)] 147 }; 148 }