| 604 | const segmentAttempts = 3 |
| 605 | |
| 606 | const makeEncryptedCompaction = ({ client, encryption, session: { framed, request } }: { |
| 607 | readonly session: RemoteSession |
| 608 | readonly client: CompactionClient |
| 609 | readonly encryption: EventLogEncryption["Service"] |
| 610 | }): EventLogRemoteCompaction => { |
| 611 | const stageRequest = (stage: CompactionStage) => ({ |
| 612 | publicKey: stage.identity.publicKey, |
| 613 | storeId: stage.storeId, |
| 614 | stageId: stage.stageId |
| 615 | }) |
| 616 | |
| 617 | return { |
| 618 | status: ({ identity, storeId }) => |
| 619 | request( |
| 620 | "compactionStatus", |
| 621 | identity, |
| 622 | client["EventLog.CompactionStatus"]({ publicKey: identity.publicKey, storeId }) |
| 623 | ), |
| 624 | begin: ({ identity, storeId }) => |
| 625 | request( |
| 626 | "compactionBegin", |
| 627 | identity, |
| 628 | client["EventLog.CompactionBegin"]({ publicKey: identity.publicKey, storeId }) |
| 629 | ), |
| 630 | input: (stage) => |
| 631 | framed( |
| 632 | "compactionInput", |
| 633 | stage.identity, |
| 634 | () => client["EventLog.CompactionInput"](stageRequest(stage)), |
| 635 | CompactionInputRpc.decodeHeaders |
| 636 | ), |
| 637 | fetch: (stage, sequences) => |
| 638 | framed( |
| 639 | "compactionFetch", |
| 640 | stage.identity, |
| 641 | () => client["EventLog.CompactionFetch"]({ ...stageRequest(stage), batches: sequences }), |
| 642 | (data) => |
| 643 | Effect.flatMap( |
| 644 | ChangesRpc.decodeEncrypted(data), |
| 645 | (batches) => encryption.decrypt(stage.identity, stage.storeId, batches) |
| 646 | ) |
| 647 | ), |
| 648 | upload: (stage, entries) => |
| 649 | Effect.suspend(() => { |
| 650 | let segment: Array<EncryptedBatch> = [] |
| 651 | let segmentBytes = 0 |
| 652 | let segmentCount = 0 |
| 653 | |
| 654 | // The server forgets a segment's pieces when the connection drops, so a |
| 655 | // retry resends all of them. |
| 656 | const sendSegment = Effect.suspend(() => { |
| 657 | if (!Arr.isArrayNonEmpty(segment)) return Effect.void |
| 658 | const batches = segment |
| 659 | const id = segmentCount++ |
| 660 | segment = [] |
| 661 | segmentBytes = 0 |
| 662 | return Effect.flatMap(CompactionPartRpc.encodeSegment(batches), (data) => { |
| 663 | const pieces = ChunkedMessage.split(id, data) |