Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
simplify CID handling and some type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Aug 28, 2020
1 parent 0c3b1ee commit 57cc917
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 70 deletions.
19 changes: 13 additions & 6 deletions datastore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import interfaceDatastore from 'interface-datastore'
import { toKey } from './lib/util.js'

const { filter, map } = interfaceDatastore.utils

Expand All @@ -12,7 +11,7 @@ const { filter, map } = interfaceDatastore.utils
*/
class CarDatastore {
constructor (multiformats, reader, writer) {
this._multiformats = multiformats
this.multiformats = multiformats
this._reader = reader
this._writer = writer
}
Expand All @@ -34,7 +33,7 @@ class CarDatastore {
* @return {Uint8Array} the IPLD block data referenced by the CID.
*/
async get (key) {
key = toKey(this._multiformats, key, 'get')
key = toKey(this.multiformats, key, 'get')
return this._reader.get(key)
}

Expand All @@ -55,7 +54,7 @@ class CarDatastore {
* @return {boolean} indicating whether the key exists in this Datastore.
*/
async has (key) {
key = toKey(this._multiformats, key, 'has')
key = toKey(this.multiformats, key, 'has')
return this._reader.has(key)
}

Expand All @@ -78,7 +77,7 @@ class CarDatastore {
* `CID`.
*/
async put (key, value) {
key = toKey(this._multiformats, key, 'put')
key = toKey(this.multiformats, key, 'put')
if (!(value instanceof Uint8Array)) {
throw new TypeError('put() can only receive Uint8Arrays or Buffers')
}
Expand All @@ -97,7 +96,7 @@ class CarDatastore {
* the block.
*/
async delete (key) {
key = toKey(this._multiformats, key, 'delete')
key = toKey(this.multiformats, key, 'delete')
return this._writer.delete(key)
}

Expand Down Expand Up @@ -227,4 +226,12 @@ class CarDatastore {
}
}

function toKey (multiformats, key, method) {
try {
return multiformats.CID.from(key.toString())
} catch (e) {
throw new TypeError(`${method}() only accepts CIDs or CID strings`)
}
}

export default CarDatastore
13 changes: 8 additions & 5 deletions lib/coding-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import varint from 'varint'
import { isBlock, isCID } from './util.js'

const CIDV0_BYTES = {
SHA2_256: 0x12,
Expand Down Expand Up @@ -152,9 +151,10 @@ function Encoder (multiformats, writer) {
if (!Array.isArray(roots)) {
roots = [roots]
}
for (const root of roots) {
if (!isCID(root)) {
throw new TypeError('Roots must be CIDs')
for (let i = 0; i < roots.length; i++) {
roots[i] = multiformats.CID.asCID(roots[i])
if (!roots[i]) {
throw new TypeError('Roots must be CIDs') // or gently coercable to CIDs
}
}

Expand All @@ -164,7 +164,10 @@ function Encoder (multiformats, writer) {
},

async writeBlock (block) {
if (!isBlock(block)) {
if (typeof block !== 'object' ||
!(block.binary instanceof Uint8Array) ||
!block.cid ||
!(block.cid.bytes instanceof Uint8Array)) {
throw new TypeError('Block list must be of type { cid, binary }')
}
await writer(new Uint8Array(varint.encode(block.cid.bytes.length + block.binary.length)))
Expand Down
2 changes: 1 addition & 1 deletion lib/reader-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createFromDecoded } from './reader-writer-iface.js'

async function createBufferReader (multiformats, data) {
const decoded = await coding.decodeBuffer(multiformats, data)
return createFromDecoded(decoded)
return createFromDecoded(multiformats, decoded)
}

export { createBufferReader }
29 changes: 21 additions & 8 deletions lib/reader-writer-iface.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import interfaceDatastore from 'interface-datastore'
import { verifyRoots } from './util.js'

const { Errors } = interfaceDatastore

class Reader {
constructor (multiformats) {
this.multiformats = multiformats
}

/* c8 ignore next 3 */
get () {
throw new Error('Unimplemented method')
Expand All @@ -27,6 +30,10 @@ class Reader {
}

class Writer {
constructor (multiformats) {
this.multiformats = multiformats
}

put () {
throw new Error('Unimplemented method')
}
Expand All @@ -36,7 +43,13 @@ class Writer {
}

setRoots (roots) {
this.roots = verifyRoots(roots)
for (let i = 0; i < roots.length; i++) {
roots[i] = this.multiformats.CID.asCID(roots[i])
if (!roots[i]) {
throw new TypeError('Roots must be CIDs') // or gently coercable to CIDs
}
}
this.roots = roots
}

close () {}
Expand All @@ -49,8 +62,8 @@ class NoWriter extends Writer {
}

class DecodedReader extends Reader {
constructor (carData) {
super()
constructor (multiformats, carData) {
super(multiformats)
this._carData = carData
}

Expand All @@ -76,8 +89,8 @@ class DecodedReader extends Reader {
}

class StreamingReader extends Reader {
constructor (decoder) {
super()
constructor (multiformats, decoder) {
super(multiformats)
this._decoder = decoder
}

Expand Down Expand Up @@ -106,10 +119,10 @@ class StreamingReader extends Reader {
}
}

async function createFromDecoded (decoded) {
async function createFromDecoded (multiformats, decoded) {
const cids = decoded.blocks.map((b) => b.cid)
decoded.keys = cids.map((c) => c.toString())
return new DecodedReader(decoded)
return new DecodedReader(multiformats, decoded)
}

export { createFromDecoded, Reader, Writer, StreamingReader, NoWriter }
12 changes: 6 additions & 6 deletions lib/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const { Errors } = interfaceDatastore

async function createStreamCompleteReader (multiformats, stream) {
const decoded = await coding.decodeStream(multiformats, stream)
return createFromDecoded(decoded)
return createFromDecoded(multiformats, decoded)
}

async function createStreamingReader (multiformats, stream) {
const decoder = coding.StreamDecoder(multiformats, stream)
return new StreamingReader(decoder)
return new StreamingReader(multiformats, decoder)
}

async function createFileReader (multiformats, data) {
const decoded = await coding.decodeFile(multiformats, data)
return createFromDecoded(decoded)
return createFromDecoded(multiformats, decoded)
}

async function createFileIndexedReader (multiformats, path) {
Expand All @@ -30,12 +30,12 @@ async function createFileIndexedReader (multiformats, path) {
index.set(cidStr, { cid, blockLength, blockOffset })
order.push(cidStr)
}
return new IndexedReader(path, roots, index, order)
return new IndexedReader(multiformats, path, roots, index, order)
}

class IndexedReader extends Reader {
constructor (path, roots, index, order) {
super()
constructor (multiformats, path, roots, index, order) {
super(multiformats)
this._path = path
this._roots = roots
this._index = index
Expand Down
43 changes: 0 additions & 43 deletions lib/util.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/writer-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Encoder, createStreamWriter } from './coding.js'

class StreamWriter extends Writer {
constructor (multiformats, outStream) {
super()
super(multiformats)
this._outStream = outStream
const writer = createStreamWriter(outStream)
this._encoder = Encoder(multiformats, writer)
Expand Down

0 comments on commit 57cc917

Please sign in to comment.