Skip to content

Commit

Permalink
potentially naive attempt to fix pipe in BlobApi.writeFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Nov 9, 2023
1 parent 9486d8a commit ac43586
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/blob-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs'
import { Transform } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { createHash } from 'node:crypto'
import sodium from 'sodium-universal'
Expand Down Expand Up @@ -66,8 +67,8 @@ export class BlobApi {
variant: 'original',
type: blobType,
},
metadata
// contentHash
metadata,
contentHash
)

if (preview) {
Expand Down Expand Up @@ -111,11 +112,14 @@ export class BlobApi {
*/
async writeFile(filepath, { name, variant, type }, metadata, hash) {
if (hash) {
// @ts-ignore TODO: return value types don't match pipeline's expectations, though they should
await pipeline(
fs.createReadStream(filepath),
hash,

new Transform({
transform: (data, _, cb) => {
hash.update(data)
cb(null, data)
},
}),
// @ts-ignore TODO: remove driveId property from createWriteStream
this.#blobStore.createWriteStream({ type, variant, name }, { metadata })
)
Expand Down
5 changes: 2 additions & 3 deletions tests/blob-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test('create blobs', async (t) => {

const hash = createHash('sha256')
const originalContent = await fs.readFile(join(directory, 'original.png'))

hash.update(originalContent)

const attachment = await blobApi.create(
Expand All @@ -37,9 +38,7 @@ test('create blobs', async (t) => {

t.is(attachment.driveId, blobStore.writerDriveId)
t.is(attachment.type, 'photo')
// TODO: Need to fix BlobApi implementation
// https://github.com/digidem/mapeo-core-next/pull/365#pullrequestreview-1716846341
// t.alike(attachment.hash, hash.digest('hex'))
t.alike(attachment.hash, hash.digest('hex'))
})

test('get url from blobId', async (t) => {
Expand Down

0 comments on commit ac43586

Please sign in to comment.