stableArtifactFileName function
A stable cache file name for url: a short hash of the full URL plus a
sanitized tail of its last path segment (so the file stays recognizably
e.g. …-gemma-4b-it-q4_k_m.gguf).
Implementation
String stableArtifactFileName(Uri url) {
final text = url.toString();
// djb2 kept within 32 bits; the multiplier is small enough that the
// intermediate stays exact in JS doubles on the web backend.
var hash = 5381;
for (final unit in text.codeUnits) {
hash = (hash * 33 + unit) & 0xffffffff;
}
final tail = url.pathSegments.isEmpty ? 'model' : url.pathSegments.last;
return '${hash.toRadixString(16)}-${_safeTail(tail)}';
}