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

Commit 5c00d18

Browse files
committed
fix: support uint8arrays
Relaxes input from requiring node `Buffer`s to being `Uint8Arrays`. This also means that the `.buffer` and `.prefix` properties are now `Uint8Array`s. BREAKING CHANGES: - node `Buffer`s have been replaced with `Uint8Array`s - the `.buffer` property is now a `Uint8Array` - the `.prefix` property is now a `Uint8Array`
1 parent 1cf3ba9 commit 5c00d18

File tree

10 files changed

+142
-131
lines changed

10 files changed

+142
-131
lines changed

README.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# js-cid
1+
# js-cid <!-- omit in toc -->
22

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

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

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

17-
## Table of Contents
17+
## Table of Contents <!-- omit in toc -->
1818

1919
- [Install](#install)
20+
- [In Node.js through npm](#in-nodejs-through-npm)
21+
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
22+
- [In the Browser through `<script>` tag](#in-the-browser-through-script-tag)
2023
- [Usage](#usage)
2124
- [API](#api)
25+
- [CID.isCID(cid)](#cidiscidcid)
26+
- [CID.validateCID(cid)](#cidvalidatecidcid)
27+
- [new CID(version, codec, multihash, [multibaseName])](#new-cidversion-codec-multihash-multibasename)
28+
- [new CID(baseEncodedString)](#new-cidbaseencodedstring)
29+
- [new CID(Uint8Array)](#new-ciduint8array)
30+
- [cid.codec](#cidcodec)
31+
- [cid.version](#cidversion)
32+
- [cid.multihash](#cidmultihash)
33+
- [cid.multibaseName](#cidmultibasename)
34+
- [cid.bytes](#cidbytes)
35+
- [cid.prefix](#cidprefix)
36+
- [cid.toV0()](#cidtov0)
37+
- [cid.toV1()](#cidtov1)
38+
- [cid.toBaseEncodedString(base=this.multibaseName)](#cidtobaseencodedstringbasethismultibasename)
39+
- [cid.toString(base=this.multibaseName)](#cidtostringbasethismultibasename)
40+
- [cid.equals(cid)](#cidequalscid)
2241
- [Contribute](#contribute)
2342
- [License](#license)
2443

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

51-
#### Gotchas
52-
53-
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).
54-
5570
## Usage
5671

57-
You can create an instance from a CID string or CID Buffer
72+
You can create an instance from a CID string or CID Uint8Array
5873

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

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

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

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

124140
`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.
125141

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

131-
### new CID(Buffer)
147+
### new CID(Uint8Array)
132148

133-
Additionally, you can instantiate an instance from a buffer.
149+
Additionally, you can instantiate an instance from a `Uint8Array`.
134150

135151
#### cid.codec
136152

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

143159
#### cid.multihash
144160

145-
Property containing the multihash buffer.
161+
Property containing the multihash `Uint8Array`.
146162

147163
#### cid.multibaseName
148164

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

151-
#### cid.buffer
167+
#### cid.bytes
152168

153-
Property containing the full CID encoded as a `Buffer`.
169+
Property containing the full CID encoded as a `Uint8Array`.
154170

155171
#### cid.prefix
156172

157-
Proprety containing a buffer of the CID version, codec, and the prefix
173+
Proprety containing a `Uint8Array` of the CID version, codec, and the prefix
158174
section of the multihash.
159175

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

176192
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.
177-
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.
193+
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.
178194

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

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,15 @@
3535
"url": "https://github.com/multiformats/js-cid/issues"
3636
},
3737
"dependencies": {
38-
"buffer": "^5.6.0",
3938
"class-is": "^1.1.0",
40-
"multibase": "^1.0.0",
41-
"multicodec": "^1.0.1",
42-
"multihashes": "^1.0.1"
39+
"multibase": "^3.0.0",
40+
"multicodec": "^2.0.0",
41+
"multihashes": "^3.0.1",
42+
"uint8arrays": "^1.0.0"
4343
},
4444
"devDependencies": {
45-
"aegir": "^23.0.0",
46-
"chai": "^4.2.0",
47-
"dirty-chai": "^2.0.1",
48-
"multihashing-async": "~0.8.1"
45+
"aegir": "^25.0.0",
46+
"multihashing-async": "^2.0.0"
4947
},
5048
"engines": {
5149
"node": ">=4.0.0",

src/cid-util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
const mh = require('multihashes')
4-
const { Buffer } = require('buffer')
5-
var CIDUtil = {
4+
5+
const CIDUtil = {
66
/**
77
* Test if the given input is a valid CID object.
88
* Returns an error message if it is not.
@@ -33,8 +33,8 @@ var CIDUtil = {
3333
}
3434
}
3535

36-
if (!Buffer.isBuffer(other.multihash)) {
37-
return 'multihash must be a Buffer'
36+
if (!(other.multihash instanceof Uint8Array)) {
37+
return 'multihash must be a Uint8Array'
3838
}
3939

4040
try {

src/index.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare class CID {
1313
* else if (str)
1414
* if (1st char is on multibase table) -> CID String
1515
* else -> bs58 encoded multihash
16-
* else if (Buffer)
16+
* else if (Uint8Array)
1717
* if (1st byte is 0 or 1) -> CID
1818
* else -> multihash
1919
* else if (Number)
@@ -23,20 +23,20 @@ declare class CID {
2323
* @example
2424
* new CID(<version>, <codec>, <multihash>, <multibaseName>)
2525
* new CID(<cidStr>)
26-
* new CID(<cid.buffer>)
26+
* new CID(<cid.bytes>)
2727
* new CID(<multihash>)
2828
* new CID(<bs58 encoded multihash>)
2929
* new CID(<cid>)
3030
*/
3131
constructor(
3232
version: 0 | 1,
3333
codec: string,
34-
multhash: Buffer,
34+
multhash: Uint8Array,
3535
multibaseName?: string
3636
);
3737
constructor(cid: CID);
3838
constructor(str: string);
39-
constructor(buf: Buffer);
39+
constructor(buf: Uint8Array);
4040

4141
/**
4242
* The version of the CID.
@@ -51,22 +51,22 @@ declare class CID {
5151
/**
5252
* The multihash of the CID.
5353
*/
54-
multihash: Buffer;
54+
multihash: Uint8Array;
5555

5656
/**
5757
* Multibase name as string.
5858
*/
5959
multibaseName: string;
6060

6161
/**
62-
* The CID as a `Buffer`
62+
* The CID as a `Uint8Array`
6363
*/
64-
readonly buffer: Buffer;
64+
readonly bytes: Uint8Array;
6565

6666
/**
6767
* The prefix of the CID.
6868
*/
69-
readonly prefix: Buffer;
69+
readonly prefix: Uint8Array;
7070

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

9898
/**
9999
* Compare equality with another CID.

0 commit comments

Comments
 (0)