Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow any JSON object to be saved in blob store #901

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/blob-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import util from 'node:util'
import { discoveryKey } from 'hypercore-crypto'
import { TypedEmitter } from 'tiny-typed-emitter'
import { LiveDownload } from './live-download.js'
/** @import { JsonObject } from 'type-fest' */
/** @import { Readable as NodeReadable } from 'node:stream' */
/** @import { Readable as StreamxReadable, Writable } from 'streamx' */
/** @import { BlobId } from '../types.js' */
Expand Down Expand Up @@ -200,7 +201,7 @@ export class BlobStore {
* @param {Omit<BlobId, 'driveId'>} blobId
* @param {Buffer} blob
* @param {object} [options]
* @param {{mimeType: string}} [options.metadata] Metadata to store with the blob
* @param {JsonObject} [options.metadata] Metadata to store with the blob
* @returns {Promise<string>} discovery key as hex string of hyperdrive where blob is stored
*/
async put({ type, variant, name }, blob, options) {
Expand All @@ -212,7 +213,7 @@ export class BlobStore {
/**
* @param {Omit<BlobId, 'driveId'>} blobId
* @param {object} [options]
* @param {{mimeType: string}} [options.metadata] Metadata to store with the blob
* @param {JsonObject} [options.metadata] Metadata to store with the blob
* @returns {Writable & { driveId: string }}
*/
createWriteStream({ type, variant, name }, options) {
Expand Down
1 change: 1 addition & 0 deletions src/fastify-plugins/blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ async function routes(fastify, options) {
// Extract the 'mimeType' property of the metadata and use it for the response header if found
if (
metadata &&
typeof metadata === 'object' &&
'mimeType' in metadata &&
typeof metadata.mimeType === 'string'
) {
Expand Down
26 changes: 26 additions & 0 deletions test/blob-store/blob-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ test('blobStore.createWriteStream(blobId) and blobStore.createReadStream(blobId)
assert.deepEqual(bndlbuf, diskbuf, 'should be equal')
})

test('blobStore.entry includes metadata if present', async () => {
const { blobStore } = testenv()

const blobId = /** @type {const} */ ({
type: 'photo',
variant: 'original',
name: 'test-file',
})
const ws = blobStore.createWriteStream(blobId, {
metadata: {
foo: 'bar',
baz: [1, 2, 3],
},
})
await pipeline(fs.createReadStream(new URL(import.meta.url)), ws)

const entry = await blobStore.entry({
...blobId,
driveId: ws.driveId,
})
assert.deepEqual(entry?.value.metadata, {
foo: 'bar',
baz: [1, 2, 3],
})
})

test('blobStore.createReadStream should not wait', async () => {
const { blobStore } = testenv()
const expected = await readFile(new URL(import.meta.url))
Expand Down
3 changes: 2 additions & 1 deletion types/hyperdrive.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare module 'hyperdrive' {
import Hyperblobs, { BlobId } from 'hyperblobs'
import { Readable, Writable } from 'streamx'
import { TypedEmitter } from 'tiny-typed-emitter'
import { JsonValue } from 'type-fest'

interface HyperdriveOptions {
onwait: () => void
Expand Down Expand Up @@ -39,7 +40,7 @@ declare module 'hyperdrive' {
executable: boolean // whether the blob at path is an executable
linkname: null | string // if entry not symlink, otherwise a string to the entry this links to
blob: BlobId // a Hyperblob id that can be used to fetch the blob associated with this entry
metadata: null | object
metadata: JsonValue
}
}
}
Expand Down
Loading