Skip to content

Commit

Permalink
fix!: encode/decode blockstore-s3 keys in base32upper (#201)
Browse files Browse the repository at this point in the history
For consistency with blockstore-fs and to avoid problems with casing
encode/decode all CID keys as base32upper

BREAKING CHANGE: s3 filenames are now all base32upper
  • Loading branch information
achingbrain committed Mar 24, 2023
1 parent f85d719 commit 513fd9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 15 additions & 3 deletions packages/blockstore-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
ListObjectsV2Command
} from '@aws-sdk/client-s3'
import { CID } from 'multiformats/cid'
import { base32upper } from 'multiformats/bases/base32'
import type { MultibaseCodec } from 'multiformats/bases/interface'

export interface S3DatastoreInit {
/**
Expand All @@ -31,6 +33,12 @@ export interface S3DatastoreInit {
* Whether to try to create the bucket if it is missing when `.open` is called
*/
createIfMissing?: boolean

/**
* The multibase codec to use - nb. should be case insensitive.
* default: base32upper
*/
base?: MultibaseCodec<string>
}

/**
Expand All @@ -41,6 +49,7 @@ export class S3Blockstore extends BaseBlockstore {
public createIfMissing: boolean
private readonly s3: S3
private readonly bucket: string
private readonly base: MultibaseCodec<string>

constructor (s3: S3, bucket: string, init?: S3DatastoreInit) {
super()
Expand All @@ -57,14 +66,17 @@ export class S3Blockstore extends BaseBlockstore {
this.s3 = s3
this.bucket = bucket
this.createIfMissing = init?.createIfMissing ?? false
this.base = init?.base ?? base32upper
}

/**
* Returns the full key which includes the path to the ipfs store
*/
_getFullKey (key: CID): string {
_getFullKey (cid: CID): string {
// Avoid absolute paths with s3
return [this.path, key.toString()].filter(Boolean).join('/').replace(/\/\/+/g, '/')
const str = this.base.encoder.encode(cid.multihash.bytes)

return [this.path, str].filter(Boolean).join('/').replace(/\/\/+/g, '/')
}

/**
Expand Down Expand Up @@ -212,7 +224,7 @@ export class S3Blockstore extends BaseBlockstore {
}

// Remove the path from the key
const cid = CID.parse(d.Key.slice((this.path ?? '').length))
const cid = CID.decode(this.base.decoder.decode(d.Key.slice((this.path ?? '').length)))

yield {
cid,
Expand Down
4 changes: 2 additions & 2 deletions packages/blockstore-s3/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('S3Blockstore', () => {
await store.put(cid, new TextEncoder().encode('test data'))

const command = await deferred.promise
expect(command).to.have.nested.property('input.Key', '.ipfs/datastore/QmeimKZyjcBnuXmAD9zMnSjM9JodTbgGT3gutofkTqz9rE')
expect(command).to.have.nested.property('input.Key', '.ipfs/datastore/BCIQPGZJ6QLZOFG3OP45NLMSJUWGJCO72QQKHLDTB6FXIB6BDSLRQYLY')
})

it('should return a standard error when the put fails', async () => {
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('S3Blockstore', () => {
expect(value).to.equalBytes(buf)

const getObjectCommand = await deferred.promise
expect(getObjectCommand).to.have.nested.property('input.Key', '.ipfs/datastore/QmeimKZyjcBnuXmAD9zMnSjM9JodTbgGT3gutofkTqz9rE')
expect(getObjectCommand).to.have.nested.property('input.Key', '.ipfs/datastore/BCIQPGZJ6QLZOFG3OP45NLMSJUWGJCO72QQKHLDTB6FXIB6BDSLRQYLY')
})

it('should return a standard not found error code if the key isn\'t found', async () => {
Expand Down

0 comments on commit 513fd9c

Please sign in to comment.