Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
fix: replace node buffers with uint8arrays (#117)
Browse files Browse the repository at this point in the history
Relaxes input from requiring node `Buffer`s to being `Uint8Arrays`.

This also means that the `.buffer` (now `.byte`) and `.prefix` properties have changed to be `Uint8Array`s.

BREAKING CHANGES:

- node `Buffer`s have been replaced with `Uint8Array`s
- the `.buffer` property has been renamed to `.bytes` and is now a `Uint8Array`
- the `.prefix` property is now a `Uint8Array`
  • Loading branch information
achingbrain committed Aug 4, 2020
1 parent 1cf3ba9 commit a7ae250
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 132 deletions.
50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# js-cid
# js-cid <!-- omit in toc -->

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
Expand All @@ -10,15 +10,34 @@

> [CID](https://github.com/multiformats/cid) implementation in JavaScript.
## Lead Maintainer
## Lead Maintainer <!-- omit in toc -->

[Volker Mische](https://github.com/vmx)

## Table of Contents
## Table of Contents <!-- omit in toc -->

- [Install](#install)
- [In Node.js through npm](#in-nodejs-through-npm)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [In the Browser through `<script>` tag](#in-the-browser-through-script-tag)
- [Usage](#usage)
- [API](#api)
- [CID.isCID(cid)](#cidiscidcid)
- [CID.validateCID(cid)](#cidvalidatecidcid)
- [new CID(version, codec, multihash, [multibaseName])](#new-cidversion-codec-multihash-multibasename)
- [new CID(baseEncodedString)](#new-cidbaseencodedstring)
- [new CID(Uint8Array)](#new-ciduint8array)
- [cid.codec](#cidcodec)
- [cid.version](#cidversion)
- [cid.multihash](#cidmultihash)
- [cid.multibaseName](#cidmultibasename)
- [cid.bytes](#cidbytes)
- [cid.prefix](#cidprefix)
- [cid.toV0()](#cidtov0)
- [cid.toV1()](#cidtov1)
- [cid.toBaseEncodedString(base=this.multibaseName)](#cidtobaseencodedstringbasethismultibasename)
- [cid.toString(base=this.multibaseName)](#cidtostringbasethismultibasename)
- [cid.equals(cid)](#cidequalscid)
- [Contribute](#contribute)
- [License](#license)

Expand Down Expand Up @@ -48,13 +67,9 @@ Loading this module through a script tag will make the ```Cids``` obj available
<script src="https://unpkg.com/cids/dist/index.js"></script>
```

#### Gotchas

You will need to use Node.js `Buffer` API compatible, if you are running inside the browser, you can access it by `multihash.Buffer` or you can install Feross's [Buffer](https://github.com/feross/buffer).

## Usage

You can create an instance from a CID string or CID Buffer
You can create an instance from a CID string or CID Uint8Array

```js
const CID = require('cids')
Expand All @@ -73,8 +88,9 @@ or by specifying the [cid version](https://github.com/multiformats/cid#versions)
```js
const CID = require('cids')
const multihashing = require('multihashing-async')
const bytes = new TextEncoder('utf8').encode('OMG!')

const hash = await multihashing(Buffer.from('OMG!'), 'sha2-256')
const hash = await multihashing(bytes, 'sha2-256')
const cid = new CID(1, 'dag-pb', hash)
console.log(cid.toString())
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
Expand Down Expand Up @@ -119,7 +135,7 @@ instance. Throws an `Error` if not valid.

`codec` must be a string of a valid [registered codec](https://github.com/multiformats/multicodec/blob/master/table.csv).

`multihash` must be a `Buffer` instance of a valid [multihash](https://github.com/multiformats/multihash).
`multihash` must be a `Uint8Array` instance of a valid [multihash](https://github.com/multiformats/multihash).

`multibaseName` optional string. Must be a valid [multibase](https://github.com/multiformats/multibase/blob/master/multibase.csv) name. Default is `base58btc` for v0 CIDs or `base32` for v1 CIDs.

Expand All @@ -128,9 +144,9 @@ instance. Throws an `Error` if not valid.
Additionally, you can instantiate an instance from a base encoded
string.

### new CID(Buffer)
### new CID(Uint8Array)

Additionally, you can instantiate an instance from a buffer.
Additionally, you can instantiate an instance from a `Uint8Array`.

#### cid.codec

Expand All @@ -142,19 +158,19 @@ Property containing the CID version integer.

#### cid.multihash

Property containing the multihash buffer.
Property containing the multihash `Uint8Array`.

#### cid.multibaseName

Property containing the default base to use when calling `.toString`

#### cid.buffer
#### cid.bytes

Property containing the full CID encoded as a `Buffer`.
Property containing the full CID encoded as a `Uint8Array`.

#### cid.prefix

Proprety containing a buffer of the CID version, codec, and the prefix
Proprety containing a `Uint8Array` of the CID version, codec, and the prefix
section of the multihash.

#### cid.toV0()
Expand All @@ -174,7 +190,7 @@ Returns a base encoded string of the CID. Defaults to the base encoding in `this
The value of `this.multibaseName` depends on how the instance was constructed:

1. If the CID was constructed from an object that already had a multibase (a string or an existing CID) then it retains that base.
2. If the CID was constructed from an object that _did not_ have a multibase (a buffer, or by passing only version + codec + multihash to the constructor), then `multibaseName` will be `base58btc` for a v0 CID or `base32` for a v1 CID.
2. If the CID was constructed from an object that _did not_ have a multibase (a `Uint8Array`, or by passing only version + codec + multihash to the constructor), then `multibaseName` will be `base58btc` for a v0 CID or `base32` for a v1 CID.

#### cid.toString(base=this.multibaseName)

Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@
"url": "https://github.com/multiformats/js-cid/issues"
},
"dependencies": {
"buffer": "^5.6.0",
"class-is": "^1.1.0",
"multibase": "^1.0.0",
"multicodec": "^1.0.1",
"multihashes": "^1.0.1"
"multibase": "^3.0.0",
"multicodec": "^2.0.0",
"multihashes": "^3.0.1",
"uint8arrays": "^1.0.0"
},
"devDependencies": {
"aegir": "^23.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"multihashing-async": "~0.8.1"
"aegir": "^25.0.0",
"multihashing-async": "^2.0.0"
},
"engines": {
"node": ">=4.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/cid-util.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const mh = require('multihashes')
const { Buffer } = require('buffer')
var CIDUtil = {

const CIDUtil = {
/**
* Test if the given input is a valid CID object.
* Returns an error message if it is not.
Expand Down Expand Up @@ -33,8 +33,8 @@ var CIDUtil = {
}
}

if (!Buffer.isBuffer(other.multihash)) {
return 'multihash must be a Buffer'
if (!(other.multihash instanceof Uint8Array)) {
return 'multihash must be a Uint8Array'
}

try {
Expand Down
18 changes: 9 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare class CID {
* else if (str)
* if (1st char is on multibase table) -> CID String
* else -> bs58 encoded multihash
* else if (Buffer)
* else if (Uint8Array)
* if (1st byte is 0 or 1) -> CID
* else -> multihash
* else if (Number)
Expand All @@ -23,20 +23,20 @@ declare class CID {
* @example
* new CID(<version>, <codec>, <multihash>, <multibaseName>)
* new CID(<cidStr>)
* new CID(<cid.buffer>)
* new CID(<cid.bytes>)
* new CID(<multihash>)
* new CID(<bs58 encoded multihash>)
* new CID(<cid>)
*/
constructor(
version: 0 | 1,
codec: string,
multhash: Buffer,
multhash: Uint8Array,
multibaseName?: string
);
constructor(cid: CID);
constructor(str: string);
constructor(buf: Buffer);
constructor(buf: Uint8Array);

/**
* The version of the CID.
Expand All @@ -51,22 +51,22 @@ declare class CID {
/**
* The multihash of the CID.
*/
multihash: Buffer;
multihash: Uint8Array;

/**
* Multibase name as string.
*/
multibaseName: string;

/**
* The CID as a `Buffer`
* The CID as a `Uint8Array`
*/
readonly buffer: Buffer;
readonly bytes: Uint8Array;

/**
* The prefix of the CID.
*/
readonly prefix: Buffer;
readonly prefix: Uint8Array;

/**
* Convert to a CID of version `0`.
Expand All @@ -93,7 +93,7 @@ declare class CID {
/**
* Serialize to a plain object.
*/
toJSON(): { codec: string; version: 0 | 1; hash: Buffer };
toJSON(): { codec: string; version: 0 | 1; hash: Uint8Array };

/**
* Compare equality with another CID.
Expand Down
Loading

0 comments on commit a7ae250

Please sign in to comment.