Skip to content

Commit

Permalink
chore: fix type errors in blob store live download tests (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn authored May 7, 2024
1 parent 57cfe72 commit 0d3d8d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
27 changes: 20 additions & 7 deletions tests/blob-store/live-download.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
import assert from 'node:assert/strict'
import { DriveLiveDownload } from '../../src/blob-store/live-download.js'
import Hyperdrive from 'hyperdrive'
import Corestore from 'corestore'
Expand All @@ -7,6 +8,8 @@ import test from 'brittle'
import { setTimeout } from 'node:timers/promises'
import { once } from 'node:events'
import { randomBytes } from 'node:crypto'
/** @typedef {import('../../src/blob-store/live-download.js').BlobDownloadState} BlobDownloadState */
/** @typedef {import('../../src/blob-store/live-download.js').BlobDownloadStateError} BlobDownloadStateError */

// Test with buffers that are 3 times the default blockSize for hyperblobs
const TEST_BUF_SIZE = 3 * 64 * 1024
Expand All @@ -15,12 +18,15 @@ test('live download', async (t) => {
const { drive1, drive2, replicate } = await testEnv()

await drive1.put('/foo', randomBytes(TEST_BUF_SIZE))
const drive1Entry = await drive1.entry('/foo')
assert(drive1Entry)
const {
value: { blob: blob1 },
} = await drive1.entry('/foo')
} = drive1Entry

const stream = replicate()
const blobCore2 = (await drive2.getBlobs()).core
const blobCore2 = (await drive2.getBlobs())?.core
assert(blobCore2)

const download = new DriveLiveDownload(drive2)
await waitForState(download, 'downloaded')
Expand Down Expand Up @@ -254,17 +260,20 @@ test('live download started before initial replication', async (t) => {
const { drive1, drive2, replicate } = await testEnv()

await drive1.put('/foo', randomBytes(TEST_BUF_SIZE))
const drive1Entry = await drive1.entry('/foo')
assert(drive1Entry)
const {
value: { blob: blob1 },
} = await drive1.entry('/foo')
} = drive1Entry

const download = new DriveLiveDownload(drive2)
await waitForState(download, 'downloaded')
// initially drive2 is not replicating and empty, so we expect a 'downloaded' status
t.is(download.state.status, 'downloaded')

const stream = replicate()
const blobCore2 = (await drive2.getBlobs()).core
const blobCore2 = (await drive2.getBlobs())?.core
assert(blobCore2)
await waitForState(download, 'downloaded')

// Can't use `drive2.get()` here because connected to replication stream, so
Expand All @@ -288,7 +297,11 @@ test('live download started before initial replication', async (t) => {
t.alike(await drive2.get('/bar'), expected, 'Second blob is downloaded')
})

/** @returns {Promise<void>} */
/**
* @param {DriveLiveDownload} download
* @param {(BlobDownloadState | BlobDownloadStateError)['status']} status
* @returns {Promise<void>}
*/
async function waitForState(download, status) {
return new Promise((res) => {
download.on('state', function onState(state) {
Expand All @@ -301,8 +314,8 @@ async function waitForState(download, status) {
}

async function testEnv() {
const store1 = new Corestore(RAM)
const store2 = new Corestore(RAM)
const store1 = new Corestore(() => new RAM())
const store2 = new Corestore(() => new RAM())
const drive1 = new Hyperdrive(store1)
await drive1.ready()
const drive2 = new Hyperdrive(store2, drive1.key)
Expand Down
10 changes: 7 additions & 3 deletions types/hyperdrive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ declare module 'hyperdrive' {
}

class Hyperdrive extends TypedEmitter<HyperdriveEvents> {
constructor(corestore: Corestore, key: Buffer, opts: HyperdriveOptions)
constructor(corestore: Corestore, opts: HyperdriveOptions)
constructor(
corestore: Corestore,
key?: null | Buffer,
opts?: HyperdriveOptions
)
constructor(corestore: Corestore, opts?: HyperdriveOptions)
readonly id: null | string // String containing the id (z-base-32 of the public key) identifying this drive.
readonly core: Hypercore // Hypercore used for drive.db
readonly blobs: null | Hyperblobs
Expand Down Expand Up @@ -89,7 +93,7 @@ declare module 'hyperdrive' {
blobRanges: Range
): { done: Promise<void>; destroy: () => void }
list(folder: string, opts?: { recursive?: boolean }): Readable
download(folder: string, opts?: { recursive?: boolean }): Readable
download(folder?: string, opts?: { recursive?: boolean }): Readable
readdir(folder: string): Readable
mirror(): any
batch(): any
Expand Down

0 comments on commit 0d3d8d9

Please sign in to comment.