{"version":3,"file":"mirror-sync-DPMtQC3A.mjs","sources":["../.rollup-tmp/client/mirror-sync.js"],"sourcesContent":["/** Post-confirmation readback stays outside the ordinary offchain client bundle. */\nimport { makeApiRequest } from '../utils/api';\n/** Look a concrete path up in a path-keyed map, tolerating a leading slash. */\nfunction pickPathDocument(source, path) {\n    if (Object.prototype.hasOwnProperty.call(source, path))\n        return source[path];\n    const withSlash = path.startsWith('/') ? path : `/${path}`;\n    if (Object.prototype.hasOwnProperty.call(source, withSlash))\n        return source[withSlash];\n    const withoutSlash = path.startsWith('/') ? path.slice(1) : path;\n    if (Object.prototype.hasOwnProperty.call(source, withoutSlash))\n        return source[withoutSlash];\n    return undefined;\n}\n/**\n * `observed: true` only when EVERY requested concrete path was read back as a\n * real document. The sync endpoint answers `null` for a document that is\n * absent, lagging, or read-denied, and a deleted path reads back as `null` too\n * - none of those are evidence of synchronization.\n */\nfunction validatePathKeyedDocuments(paths, source) {\n    if (!source || typeof source !== 'object' || Array.isArray(source)) {\n        return { observed: false, reason: 'the synchronization response was not a path-keyed document map' };\n    }\n    const map = source;\n    const documents = [];\n    const unobserved = [];\n    for (const path of paths) {\n        const document = pickPathDocument(map, path);\n        if (document == null || typeof document !== 'object' || Array.isArray(document)) {\n            unobserved.push(path);\n            documents.push({ path, document: null });\n            continue;\n        }\n        documents.push({ path, document: document });\n    }\n    if (unobserved.length > 0) {\n        return {\n            observed: false,\n            reason: `no readable document was returned for ${unobserved.join(', ')} ` +\n                '(absent, deleted, lagging, read-denied, or a passthrough write)',\n        };\n    }\n    return { observed: true, documents };\n}\n/**\n * Best-effort chain synchronization. A confirmed transaction must NEVER be lost to a\n * synchronization failure, so this reports its outcome in `mirrorSync` and\n * never rejects. The signature lets the server index the exact transaction,\n * including hook-mutated documents beyond the requested destination paths.\n */\nexport async function attemptMirrorSync(paths, options, transactionSignature) {\n    try {\n        const synced = await syncItems(paths, options, transactionSignature);\n        // The sync endpoint answers `{ data: <path-keyed map>, status }`; a document\n        // path always contains a `/`, so a top-level `data` key is never one.\n        const map = (synced && typeof synced === 'object' && !Array.isArray(synced) && 'data' in synced)\n            ? synced.data\n            : synced;\n        return validatePathKeyedDocuments(paths, map);\n    }\n    catch (error) {\n        const message = typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' ? error.message : String(error);\n        return { observed: false, reason: `mirror synchronization failed: ${message}` };\n    }\n}\n/** Refresh concrete paths, or index a confirmed transaction and read its requested paths. */\nexport async function syncItems(paths, options, transactionSignature) {\n    var _a, _b;\n    const response = await makeApiRequest('PUT', 'items/sync', Object.assign({ paths }, (transactionSignature !== undefined ? { transactionSignature } : {})), options === null || options === void 0 ? void 0 : options._overrides);\n    if (transactionSignature !== undefined && (response.status !== 200\n        || ((_a = response.data) === null || _a === void 0 ? void 0 : _a.synchronized) !== true\n        || ((_b = response.data) === null || _b === void 0 ? void 0 : _b.transactionSignature) !== transactionSignature)) {\n        // An older server's local-read stub can return 200 with stale documents.\n        // Only the matching server acknowledgment proves this transaction was indexed.\n        throw new Error('the synchronization response did not acknowledge the confirmed transaction');\n    }\n    return response.data;\n}\n"],"names":[],"mappings":";;;;;AAAA;AAEA;AACA,SAAS,gBAAgB,CAAC,MAAM,EAAE,IAAI,EAAE;AACxC,IAAI,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC;AAC1D,QAAQ,OAAO,MAAM,CAAC,IAAI,CAAC;AAC3B,IAAI,MAAM,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AAC9D,IAAI,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC;AAC/D,QAAQ,OAAO,MAAM,CAAC,SAAS,CAAC;AAChC,IAAI,MAAM,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CA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