Offline pack
Check an offline pack file by file before using it: size, then sha256.
Code
html
<!doctype html>
<html lang="mn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Offline pack</title>
<link href="https://dock.mn/sdk/v1/dock.css" rel="stylesheet">
<script src="https://dock.mn/sdk/v1/dock.js"></script>
<style>
body { margin: 0; font: 14px system-ui, sans-serif; }
#map { position: absolute; inset: 0; }
.panel { position: absolute; top: 10px; left: 10px; z-index: 1; max-width: 300px; padding: 8px 10px;
background: #fff; border-radius: 6px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25); }
</style>
</head>
<body>
<div id="map"></div>
<div class="panel" id="out"></div>
<script>
const client = new dock.Client({ key: 'dk_...' });
const map = dock.createMap(client, { container: 'map', center: [106.9165, 47.8880], zoom: 13 });
const enc = (s) => new TextEncoder().encode(s);
async function sha256(bytes) {
const h = await crypto.subtle.digest('SHA-256', bytes);
return [...new Uint8Array(h)].map((b) => b.toString(16).padStart(2, '0')).join('');
}
(async () => {
// A pack folder as an app keeps it (in a browser: the Cache API or IndexedDB).
const folder = { 'pois.geojson': enc('{}'), 'water.geojson': enc('{"type":"FeatureCollection","features":[]}') };
const manifest = { files: await Promise.all(Object.entries(folder).map(async ([name, b]) =>
({ name, bytes: b.length, sha256: await sha256(b) }))) };
// The same size, other bytes: a file damaged in the download.
folder['water.geojson'] = enc('{"type":"FeatureCollection","features":{}}');
const check = await dock.checkPack(manifest, async (name) => folder[name] ?? null);
document.getElementById('out').textContent =
check.files.map((f) => `${f.name}: ${f.status}`).join(', ') + (check.ok ? '' : ` | дахин татах: ${check.bad.join(' ')}`);
})();
</script>
</body>
</html>