From a9f79194609cdedb13e17de143eeb96497cd3b55 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Sat, 18 Feb 2023 13:02:58 -0600 Subject: [PATCH] refactor: type abstract-chunk-store and idb-chunk-store --- src/react-app-env.d.ts | 61 +++++++++++++++++++++++ src/services/FileTransfer/FileTransfer.ts | 1 - 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/react-app-env.d.ts b/src/react-app-env.d.ts index 56c759b..73b8adf 100644 --- a/src/react-app-env.d.ts +++ b/src/react-app-env.d.ts @@ -145,3 +145,64 @@ consumes the data in `encryptedStream` and returns a plaintext version. */ export function encryptedSize(plaintextSize: number): number } + +declare module 'abstract-chunk-store' { + type GetCallback = (err: Error | null, buffer: Buffer) => void + + export interface ChunkStore { + /** + * Create a new chunk store. Chunks must have a length of `chunkLength`. + */ + constructor(chunkLength: number) + + /** + * Add a new chunk to the storage. `index` should be an integer. + */ + put( + index: number, + chunkBuffer: Buffer, + cb: (err: Error | null) => void + ): void + + /** + * Retrieve a chunk stored. `index` should be an integer. + */ + get(index: number, cb: GetCallback): void + get( + index: number, + options: { offset?: number; length?: number }, + cb: GetCallback + ): void + + /** + * Close the underlying resource, e.g. if the store is backed by a file, this would close the file descriptor. + */ + close(cb: (err: Error | null) => void) + + /** + * Destroy the file data, e.g. if the store is backed by a file, this would delete the file from the filesystem. + */ + destroy(cb: (err: Error | null) => void) + + /** + * Expose the chunk length from the constructor so that code that receives a chunk store can know what size of chunks to write. + */ + chunkLength: number + } +} + +declare module 'idb-chunk-store' { + import { TorrentOptions } from 'webtorrent' + import { ChunkStore } from 'abstract-chunk-store' + + export default function idbChunkStore( + chunkLength: number, + opts?: Partial<{ + /** + * A name to separate the contents of different stores + */ + name: string + }> & + TorrentOptions.store + ): ChunkStore +} diff --git a/src/services/FileTransfer/FileTransfer.ts b/src/services/FileTransfer/FileTransfer.ts index 8bc56e5..ebed0c4 100644 --- a/src/services/FileTransfer/FileTransfer.ts +++ b/src/services/FileTransfer/FileTransfer.ts @@ -1,7 +1,6 @@ import WebTorrent, { Torrent, TorrentFile } from 'webtorrent' import streamSaver from 'streamsaver' import { Keychain, plaintextSize, encryptedSize } from 'wormhole-crypto' -// @ts-ignore import idbChunkStore from 'idb-chunk-store' import { detectIncognito } from 'detectincognitojs'