| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | async function query(sql, params) { | 
					
						
							|  |  |  | 	let results = []; | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | 	await ssb.sqlAsync(sql, params, function (row) { | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 		results.push(row); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return results; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function guess_content_type(name) { | 
					
						
							|  |  |  | 	if (name.endsWith('.html')) { | 
					
						
							|  |  |  | 		return 'text/html; charset=UTF-8'; | 
					
						
							|  |  |  | 	} else if (name.endsWith('.js') || name.endsWith('.mjs')) { | 
					
						
							|  |  |  | 		return 'text/javascript; charset=UTF-8'; | 
					
						
							|  |  |  | 	} else if (name.endsWith('.css')) { | 
					
						
							|  |  |  | 		return 'text/stylesheet; charset=UTF-8'; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return 'application/binary'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function main() { | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | 	let path = request.path.replaceAll(/(%[0-9a-fA-F]{2})/g, (x) => | 
					
						
							|  |  |  | 		String.fromCharCode(parseInt(x.substring(1), 16)) | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 	let match = path.match(/^(%.{44}\.sha256)(?:\/)?(.*)$/); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let content_type = guess_content_type(request.path); | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | 	let root = await query( | 
					
						
							|  |  |  | 		`
 | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 		SELECT root.content ->> 'root' AS root | 
					
						
							|  |  |  | 		FROM messages site | 
					
						
							|  |  |  | 		JOIN messages root | 
					
						
							|  |  |  | 		ON site.id = ? AND root.author = site.author AND root.content ->> 'site' = site.id | 
					
						
							|  |  |  | 		ORDER BY root.sequence DESC LIMIT 1 | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | 	`,
 | 
					
						
							|  |  |  | 		[match[1]] | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 	let root_id = root[0]['root']; | 
					
						
							|  |  |  | 	let last_id = root_id; | 
					
						
							|  |  |  | 	let blob = await ssb.blobGet(root_id); | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		for (let part of match[2]?.split('/')) { | 
					
						
							|  |  |  | 			let dir = JSON.parse(utf8Decode(blob)); | 
					
						
							|  |  |  | 			last_id = dir?.links[part]; | 
					
						
							|  |  |  | 			blob = await ssb.blobGet(dir?.links[part]); | 
					
						
							|  |  |  | 			content_type = guess_content_type(part); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | 	} catch {} | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	respond({ | 
					
						
							|  |  |  | 		status_code: 200, | 
					
						
							|  |  |  | 		data: blob ? utf8Decode(blob) : `${last_id} not found`, | 
					
						
							|  |  |  | 		content_type: content_type, | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | main().catch(function (e) { | 
					
						
							| 
									
										
										
										
											2025-04-02 18:07:25 -04:00
										 |  |  | 	respond({ | 
					
						
							|  |  |  | 		status_code: 200, | 
					
						
							|  |  |  | 		data: `${e.message}\n${e.stack}`, | 
					
						
							|  |  |  | 		content_type: 'text/plain', | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2025-04-05 22:05:26 -04:00
										 |  |  | }); |