{"version":3,"file":"write-files.mjs","names":[],"sources":["../../src/flows/write-files.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Signer } from '@mysten/sui/cryptography';\nimport type { Transaction } from '@mysten/sui/transactions';\n\nimport { WalrusClientError } from '../error.js';\nimport type {\n\tWriteBlobFlowOptions,\n\tWriteBlobStep,\n\tWriteBlobStepEncoded,\n\tWriteFilesFlow,\n\tWriteFilesFlowOptions,\n\tWriteFilesFlowRegisterOptions,\n\tWriteFilesFlowRunOptions,\n} from '../types.js';\nimport { encodeQuiltPatchId } from '../utils/quilts.js';\nimport type { WalrusClient } from '../client.js';\n\nexport function createWriteFilesFlow(\n\tclient: WalrusClient,\n\t{ files, resume }: WriteFilesFlowOptions,\n): WriteFilesFlow {\n\tlet quiltBytes: Uint8Array | undefined;\n\tlet quiltIndex: Awaited<ReturnType<typeof client.encodeQuilt>>['index'] | undefined;\n\n\tconst blobFlow = client.writeBlobFlow({\n\t\tget blob(): Uint8Array {\n\t\t\tif (!quiltBytes) {\n\t\t\t\tthrow new Error('encode must be executed before accessing blob');\n\t\t\t}\n\t\t\treturn quiltBytes;\n\t\t},\n\t\tresume,\n\t} as WriteBlobFlowOptions); // Cast needed: blob is a lazy getter populated by encode()\n\n\tconst encodeQuilt = async () => {\n\t\tif (!quiltBytes) {\n\t\t\tconst { quilt, index } = await client.encodeQuilt({\n\t\t\t\tblobs: await Promise.all(\n\t\t\t\t\tfiles.map(async (file, i) => ({\n\t\t\t\t\t\tcontents: await file.bytes(),\n\t\t\t\t\t\tidentifier: (await file.getIdentifier()) ?? `file-${i}`,\n\t\t\t\t\t\ttags: (await file.getTags()) ?? {},\n\t\t\t\t\t})),\n\t\t\t\t),\n\t\t\t});\n\t\t\tif (resume?.step === 'uploaded' || resume?.step === 'certified') {\n\t\t\t\tconst { blobId } = await client.computeBlobMetadata({ bytes: quilt });\n\t\t\t\tif (blobId !== resume.blobId) {\n\t\t\t\t\tthrow new WalrusClientError(\n\t\t\t\t\t\t`Resume blobId mismatch: expected ${resume.blobId}, got ${blobId}. The blob content may have changed.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t\tquiltBytes = quilt;\n\t\t\tquiltIndex = index;\n\t\t}\n\t};\n\n\tconst encode = async (): Promise<WriteBlobStepEncoded> => {\n\t\tawait encodeQuilt();\n\t\treturn blobFlow.encode();\n\t};\n\n\tconst register = (options: WriteFilesFlowRegisterOptions): Transaction => {\n\t\tif (!quiltBytes) {\n\t\t\tthrow new Error('encode must be executed before calling register');\n\t\t}\n\n\t\treturn blobFlow.register({\n\t\t\t...options,\n\t\t\tattributes: {\n\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t...options.attributes,\n\t\t\t},\n\t\t});\n\t};\n\n\tconst listFiles = async () => {\n\t\tif (!quiltIndex) {\n\t\t\tthrow new Error('encode must be executed before calling listFiles');\n\t\t}\n\n\t\tconst certResult = resume?.step === 'certified' ? resume : await blobFlow.getBlob();\n\t\treturn quiltIndex.patches.map((patch) => ({\n\t\t\tid: encodeQuiltPatchId({\n\t\t\t\tquiltId: certResult.blobId,\n\t\t\t\tpatchId: {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tstartIndex: patch.startIndex,\n\t\t\t\t\tendIndex: patch.endIndex,\n\t\t\t\t},\n\t\t\t}),\n\t\t\tblobId: certResult.blobId,\n\t\t\tblobObject: certResult.blobObject,\n\t\t}));\n\t};\n\n\t/** @yields {WriteBlobStep} */\n\tasync function* run(options: WriteFilesFlowRunOptions): AsyncGenerator<WriteBlobStep> {\n\t\t// The quilt index is needed by listFiles() even when resuming after the upload, where the blob\n\t\t// flow itself doesn't need to encode again\n\t\tawait encodeQuilt();\n\n\t\t// The blob flow's run() handles every resume point, including restoring the upload certificate\n\t\t// when resuming from the 'uploaded' step\n\t\tyield* blobFlow.run({\n\t\t\t...options,\n\t\t\tattributes: {\n\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t...options.attributes,\n\t\t\t},\n\t\t});\n\t}\n\n\treturn {\n\t\tencode: encode,\n\t\tregister: register,\n\t\tupload: blobFlow.upload,\n\t\tcertify: blobFlow.certify,\n\t\tlistFiles: listFiles,\n\t\texecuteRegister: async (options: WriteFilesFlowRegisterOptions & { signer: Signer }) => {\n\t\t\tconst { signer, ...rest } = options;\n\t\t\treturn blobFlow.executeRegister({\n\t\t\t\tsigner,\n\t\t\t\t...rest,\n\t\t\t\tattributes: {\n\t\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t\t...rest.attributes,\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\texecuteCertify: blobFlow.executeCertify,\n\t\trun,\n\t};\n}\n"],"mappings":";;;;AAmBA,SAAgB,qBACf,QACA,EAAE,OAAO,UACQ;CACjB,IAAI;CACJ,IAAI;CAEJ,MAAM,WAAW,OAAO,cAAc;EACrC,IAAI,OAAmB;AACtB,OAAI,CAAC,WACJ,OAAM,IAAI,MAAM,gDAAgD;AAEjE,UAAO;;EAER;EACA,CAAyB;CAE1B,MAAM,cAAc,YAAY;AAC/B,MAAI,CAAC,YAAY;GAChB,MAAM,EAAE,OAAO,UAAU,MAAM,OAAO,YAAY,EACjD,OAAO,MAAM,QAAQ,IACpB,MAAM,IAAI,OAAO,MAAM,OAAO;IAC7B,UAAU,MAAM,KAAK,OAAO;IAC5B,YAAa,MAAM,KAAK,eAAe,IAAK,QAAQ;IACpD,MAAO,MAAM,KAAK,SAAS,IAAK,EAAE;IAClC,EAAE,CACH,EACD,CAAC;AACF,OAAI,QAAQ,SAAS,cAAc,QAAQ,SAAS,aAAa;IAChE,MAAM,EAAE,WAAW,MAAM,OAAO,oBAAoB,EAAE,OAAO,OAAO,CAAC;AACrE,QAAI,WAAW,OAAO,OACrB,OAAM,IAAI,kBACT,oCAAoC,OAAO,OAAO,QAAQ,OAAO,sCACjE;;AAGH,gBAAa;AACb,gBAAa;;;CAIf,MAAM,SAAS,YAA2C;AACzD,QAAM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