Skip to content

Commit

Permalink
only rename driveId to driveDiscoveryId for attachment reference
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Oct 2, 2023
1 parent 43d39a1 commit 6547df3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 171 deletions.
10 changes: 5 additions & 5 deletions src/blob-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export class BlobApi {
* @returns {Promise<string>}
*/
async getUrl(blobId) {
const { driveDiscoveryId, type, variant, name } = blobId
const { driveId, type, variant, name } = blobId
const port = await getPort(this.blobServer.server)
return `http://127.0.0.1:${port}/${this.projectId}/${driveDiscoveryId}/${type}/${variant}/${name}`
return `http://127.0.0.1:${port}/${this.projectId}/${driveId}/${type}/${variant}/${name}`
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ export class BlobApi {
}

return {
driveDiscoveryId: this.blobStore.writerDriveDiscoveryId,
driveDiscoveryId: this.blobStore.writerDriveId,
name,
type: blobType,
hash: contentHash.digest('hex'),
Expand All @@ -95,7 +95,7 @@ export class BlobApi {

/**
* @param {string} filepath
* @param {Omit<BlobId, 'driveDiscoveryId'>} options
* @param {Omit<BlobId, 'driveId'>} options
* @param {object} metadata
* @param {string} metadata.mimeType
* @param {import('node:crypto').Hash} [hash]
Expand All @@ -107,7 +107,7 @@ export class BlobApi {
fs.createReadStream(filepath),
hash,

// @ts-ignore TODO: remove driveDiscoveryId property from createWriteStream
// @ts-ignore TODO: remove driveId property from createWriteStream
this.blobStore.createWriteStream({ type, variant, name }, { metadata })
)

Expand Down
21 changes: 7 additions & 14 deletions src/blob-server/fastify-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const HEX_STRING_32_BYTES = T.String({ pattern: HEX_REGEX_32_BYTES })

const PARAMS_JSON_SCHEMA = T.Object({
projectId: HEX_STRING_32_BYTES,
driveDiscoveryId: HEX_STRING_32_BYTES,
driveId: HEX_STRING_32_BYTES,
type: T.Union(
BLOB_TYPES.map((type) => {
return T.Literal(type)
Expand Down Expand Up @@ -57,7 +57,7 @@ async function routes(fastify, options) {
const { getBlobStore } = options

fastify.get(
'/:projectId/:driveDiscoveryId/:type/:variant/:name',
'/:projectId/:driveId/:type/:variant/:name',
{ schema: { params: PARAMS_JSON_SCHEMA } },
async (request, reply) => {
const { projectId, ...blobId } = request.params
Expand All @@ -68,7 +68,7 @@ async function routes(fastify, options) {
`Unsupported variant "${blobId.variant}" for ${blobId.type}`
)
}
const { driveDiscoveryId: driveDiscoveryId } = blobId
const { driveId } = blobId

let blobStore
try {
Expand All @@ -95,10 +95,7 @@ async function routes(fastify, options) {

let blobStream
try {
blobStream = await blobStore.createEntryReadStream(
driveDiscoveryId,
entry
)
blobStream = await blobStore.createEntryReadStream(driveId, entry)
} catch (e) {
reply.code(404)
throw e
Expand All @@ -113,13 +110,9 @@ async function routes(fastify, options) {
reply.header('Content-Type', metadata.mimeType)
} else {
// Attempt to guess the MIME type based on the blob contents
const blobSlice = await blobStore.getEntryBlob(
driveDiscoveryId,
entry,
{
length: 20,
}
)
const blobSlice = await blobStore.getEntryBlob(driveId, entry, {
length: 20,
})

if (!blobSlice) {
reply.code(404)
Expand Down
69 changes: 29 additions & 40 deletions src/blob-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,28 @@ export class BlobStore {
}
coreManager.on('add-core', ({ key, namespace }) => {
if (namespace !== 'blobIndex') return
const discoveryId = getDiscoveryId(key)
if (this.#hyperdrives.has(discoveryId)) return
const driveId = getDiscoveryId(key)
if (this.#hyperdrives.has(driveId)) return
// @ts-ignore - we know pretendCorestore is not actually a Corestore
const drive = new Hyperdrive(corestore, key)
this.#hyperdrives.set(discoveryId, drive)
this.#hyperdrives.set(driveId, drive)
this.#driveEmitter.emit('add-drive', drive)
})
// This shouldn't happen, but this check ensures this.#writer is typed to exist
if (!this.#writer)
throw new Error('Could not find a writer for the blobIndex namespace')
}

get writerDriveDiscoveryId() {
get writerDriveId() {
return getDiscoveryId(this.#writer.key)
}

/**
* @param {string} driveDiscoveryId
* @param {string} driveId hex-encoded discovery key
*/
#getDrive(driveDiscoveryId) {
const drive = this.#hyperdrives.get(driveDiscoveryId)
if (!drive)
throw new Error('Drive not found ' + driveDiscoveryId.slice(0, 7))
#getDrive(driveId) {
const drive = this.#hyperdrives.get(driveId)
if (!drive) throw new Error('Drive not found ' + driveId.slice(0, 7))
return drive
}

Expand All @@ -88,11 +87,8 @@ export class BlobStore {
* @param {false} [opts.wait=false] Set to `true` to wait for a blob to download, otherwise will throw if blob is not available locally
* @param {never} [opts.timeout] Optional timeout to wait for a blob to download
*/
async get(
{ type, variant, name, driveDiscoveryId },
{ wait = false, timeout } = {}
) {
const drive = this.#getDrive(driveDiscoveryId)
async get({ type, variant, name, driveId }, { wait = false, timeout } = {}) {
const drive = this.#getDrive(driveId)
const path = makePath({ type, variant, name })
const blob = await drive.get(path, { wait, timeout })
if (!blob) throw new ErrNotFound()
Expand Down Expand Up @@ -126,11 +122,11 @@ export class BlobStore {
* @param {number} [options.timeout] Optional timeout to wait for a blob to download
*/
createReadStream(
{ type, variant, name, driveDiscoveryId },
{ type, variant, name, driveId },
options = { wait: false }
) {
// TODO: Error thrown from this be an emit error on the returned stream?
const drive = this.#getDrive(driveDiscoveryId)
const drive = this.#getDrive(driveId)
const path = makePath({ type, variant, name })

// @ts-ignore - TODO: update @digidem/types to include wait/timeout options
Expand All @@ -140,52 +136,46 @@ export class BlobStore {
/**
* Optimization for creating the blobs read stream when you have
* previously read the entry from Hyperdrive using `drive.entry`
* @param {BlobId['driveDiscoveryId']} driveDiscoveryId Hyperdrive drive discovery id
* @param {BlobId['driveId']} driveId Hyperdrive drive discovery id
* @param {import('hyperdrive').HyperdriveEntry} entry Hyperdrive entry
* @param {object} [options]
* @param {boolean} [options.wait=false] Set to `true` to wait for a blob to download, otherwise will throw if blob is not available locally
*/
async createEntryReadStream(
driveDiscoveryId,
entry,
options = { wait: false }
) {
const drive = this.#getDrive(driveDiscoveryId)
async createEntryReadStream(driveId, entry, options = { wait: false }) {
const drive = this.#getDrive(driveId)
const blobs = await drive.getBlobs()

if (!blobs)
throw new Error(
'Hyperblobs instance not found for drive ' +
driveDiscoveryId.slice(0, 7)
'Hyperblobs instance not found for drive ' + driveId.slice(0, 7)
)

return blobs.createReadStream(entry.value.blob, options)
}

/**
* @param {BlobId['driveDiscoveryId']} driveDiscoveryId Hyperdrive drive id
* @param {BlobId['driveId']} driveId Hyperdrive drive id
* @param {import('hyperdrive').HyperdriveEntry} entry Hyperdrive entry
* @param {object} [opts]
* @param {number} [opts.length]
*
* @returns {Promise<Buffer | null>}
*/
async getEntryBlob(driveDiscoveryId, entry, { length } = {}) {
const drive = this.#getDrive(driveDiscoveryId)
async getEntryBlob(driveId, entry, { length } = {}) {
const drive = this.#getDrive(driveId)
const blobs = await drive.getBlobs()

if (!blobs)
throw new Error(
'Hyperblobs instance not found for drive ' +
driveDiscoveryId.slice(0, 7)
'Hyperblobs instance not found for drive ' + driveId.slice(0, 7)
)

return blobs.get(entry.value.blob, { wait: false, start: 0, length })
}

/**
*
* @param {Omit<BlobId, 'driveDiscoveryId'>} blobId
* @param {Omit<BlobId, 'driveId'>} blobId
* @param {Buffer} blob
* @param {object} [options]
* @param {{mimeType: string}} [options.metadata] Metadata to store with the blob
Expand All @@ -194,19 +184,19 @@ export class BlobStore {
async put({ type, variant, name }, blob, options) {
const path = makePath({ type, variant, name })
await this.#writer.put(path, blob, options)
return this.writerDriveDiscoveryId
return this.writerDriveId
}

/**
* @param {Omit<BlobId, 'driveDiscoveryId'>} blobId
* @param {Omit<BlobId, 'driveId'>} blobId
* @param {object} [options]
* @param {{mimeType: string}} [options.metadata] Metadata to store with the blob
*/
createWriteStream({ type, variant, name }, options) {
const path = makePath({ type, variant, name })
const stream = this.#writer.createWriteStream(path, options)
return proxyProps(stream, {
driveDiscoveryId: this.writerDriveDiscoveryId,
driveId: this.writerDriveId,
})
}

Expand All @@ -219,12 +209,11 @@ export class BlobStore {
* @returns {Promise<import('hyperdrive').HyperdriveEntry | null>}
*/
async entry(
{ type, variant, name, driveDiscoveryId },
{ type, variant, name, driveId },
options = { follow: false, wait: false }
) {
const drive = this.#hyperdrives.get(driveDiscoveryId)
if (!drive)
throw new Error('Drive not found ' + driveDiscoveryId.slice(0, 7))
const drive = this.#hyperdrives.get(driveId)
if (!drive) throw new Error('Drive not found ' + driveId.slice(0, 7))
const path = makePath({ type, variant, name })
const entry = await drive.entry(path, options)
return entry
Expand All @@ -236,9 +225,9 @@ export class BlobStore {
* @param {boolean} [options.diff=false] Enable to return an object with a `block` property with number of bytes removed
* @return {Promise<{ blocks: number } | null>}
*/
async clear({ type, variant, name, driveDiscoveryId }, options = {}) {
async clear({ type, variant, name, driveId }, options = {}) {
const path = makePath({ type, variant, name })
const drive = this.#getDrive(driveDiscoveryId)
const drive = this.#getDrive(driveId)

return drive.clear(path, options)
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type BlobIdBase<T extends BlobType> = {
/** unique identifier for blob (e.g. hash of content) */
name: string
/** discovery key as hex string of hyperdrive where blob is stored */
driveDiscoveryId: string
driveId: string
}

// Ugly, but the only way I could figure out how to get what I wanted
Expand Down
6 changes: 3 additions & 3 deletions tests/blob-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ test('get url from blobId', async (t) => {
})

const url = await blobApi.getUrl({
driveDiscoveryId: blobStore.writerDriveDiscoveryId,
driveId: blobStore.writerDriveId,
type,
variant,
name,
Expand All @@ -76,7 +76,7 @@ test('get url from blobId', async (t) => {
t.is(
url,
`http://127.0.0.1:${blobServer.server.address().port}/${projectId}/${
blobStore.writerDriveDiscoveryId
blobStore.writerDriveId
}/${type}/${variant}/${name}`
)
t.teardown(async () => {
Expand Down Expand Up @@ -114,7 +114,7 @@ test('create blobs', async (t) => {
}
)

t.is(attachment.driveDiscoveryId, blobStore.writerDriveDiscoveryId)
t.is(attachment.driveDiscoveryId, blobStore.writerDriveId)
t.is(attachment.type, 'photo')
t.alike(attachment.hash, hash.digest('hex'))

Expand Down
Loading

0 comments on commit 6547df3

Please sign in to comment.