Comparing version 0.8.3 to 1.0.0
@@ -0,1 +1,23 @@ | ||
<a name="1.0.0"></a> | ||
# [1.0.0](https://github.com/multiformats/js-cid/compare/v0.8.3...v1.0.0) (2020-08-04) | ||
### Bug Fixes | ||
* replace node buffers with uint8arrays ([#117](https://github.com/multiformats/js-cid/issues/117)) ([a7ae250](https://github.com/multiformats/js-cid/commit/a7ae250)) | ||
### Features | ||
* expose codec code and allow construction by code ([59469b6](https://github.com/multiformats/js-cid/commit/59469b6)), closes [/github.com/multiformats/js-cid/pull/117#issuecomment-668131658](https://github.com//github.com/multiformats/js-cid/pull/117/issues/issuecomment-668131658) | ||
### 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` | ||
<a name="0.8.3"></a> | ||
@@ -2,0 +24,0 @@ ## [0.8.3](https://github.com/multiformats/js-cid/compare/v0.8.2...v0.8.3) (2020-06-19) |
@@ -7,1 +7,3 @@ /*! | ||
*/ | ||
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ |
{ | ||
"name": "cids", | ||
"version": "0.8.3", | ||
"version": "1.0.0", | ||
"description": "CID Implementation in JavaScript", | ||
@@ -38,13 +38,11 @@ "leadMaintainer": "Volker Mische <volker.mische@gmail.com>", | ||
"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" | ||
}, | ||
@@ -65,13 +63,13 @@ "engines": { | ||
"Irakli Gozalishvili <contact@gozala.io>", | ||
"Alex Potsides <alex@achingbrain.net>", | ||
"Hugo Dias <hugomrdias@gmail.com>", | ||
"Marcin Rataj <lidel@lidel.org>", | ||
"Real Harry <sidewaybot@gmail.com>", | ||
"Rod Vagg <rod@vagg.org>", | ||
"bluelovers <codelovers@users.sourceforge.net>", | ||
"Victor Bjelkholm <victorbjelkholm@gmail.com>", | ||
"Alex Potsides <alex@achingbrain.net>", | ||
"Henrique Dias <hacdias@gmail.com>", | ||
"Mitar <mitar.github@tnode.com>", | ||
"Richard Littauer <richard.littauer@gmail.com>", | ||
"Rod Vagg <rod@vagg.org>" | ||
"Richard Littauer <richard.littauer@gmail.com>" | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
# js-cid | ||
# js-cid <!-- omit in toc --> | ||
@@ -13,11 +13,31 @@ [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai/) | ||
## 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.code](#cidcode) | ||
- [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) | ||
@@ -52,9 +72,5 @@ - [License](#license) | ||
#### 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 | ||
@@ -68,2 +84,3 @@ ```js | ||
cid.codec // 'dag-pb' | ||
cid.code // 112 | ||
cid.multibaseName // 'base32' | ||
@@ -79,4 +96,5 @@ cid.toString() | ||
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) | ||
@@ -87,2 +105,10 @@ console.log(cid.toString()) | ||
The multicodec integer code can also be used to create a new CID: | ||
```js | ||
const cid = new CID(1, 112, hash) | ||
console.log(cid.toString()) | ||
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu | ||
``` | ||
The string form of v1 CIDs defaults to `base32` encoding (v0 CIDs are always `base58btc` encoded). When creating a new instance you can optionally specify the default multibase to use when calling `toBaseEncodedString()` or `toString()` | ||
@@ -127,3 +153,3 @@ | ||
`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). | ||
@@ -137,10 +163,14 @@ `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. | ||
### 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 | ||
Property containing the codec string. | ||
Property containing the string identifier of the codec. | ||
#### cid.code | ||
Property containing the integer identifier of the codec. | ||
#### cid.version | ||
@@ -152,3 +182,3 @@ | ||
Property containing the multihash buffer. | ||
Property containing the multihash `Uint8Array`. | ||
@@ -159,9 +189,9 @@ #### cid.multibaseName | ||
#### 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. | ||
@@ -186,3 +216,3 @@ | ||
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. | ||
@@ -189,0 +219,0 @@ #### cid.toString(base=this.multibaseName) |
'use strict' | ||
const mh = require('multihashes') | ||
const { Buffer } = require('buffer') | ||
var CIDUtil = { | ||
const CIDUtil = { | ||
/** | ||
@@ -36,4 +36,4 @@ * Test if the given input is a valid CID object. | ||
if (!Buffer.isBuffer(other.multihash)) { | ||
return 'multihash must be a Buffer' | ||
if (!(other.multihash instanceof Uint8Array)) { | ||
return 'multihash must be a Uint8Array' | ||
} | ||
@@ -40,0 +40,0 @@ |
@@ -16,3 +16,3 @@ /** | ||
* else -> bs58 encoded multihash | ||
* else if (Buffer) | ||
* else if (Uint8Array) | ||
* if (1st byte is 0 or 1) -> CID | ||
@@ -27,3 +27,3 @@ * else -> multihash | ||
* new CID(<cidStr>) | ||
* new CID(<cid.buffer>) | ||
* new CID(<cid.bytes>) | ||
* new CID(<multihash>) | ||
@@ -35,4 +35,4 @@ * new CID(<bs58 encoded multihash>) | ||
version: 0 | 1, | ||
codec: string, | ||
multhash: Buffer, | ||
codec: string | number, | ||
multhash: Uint8Array, | ||
multibaseName?: string | ||
@@ -42,3 +42,3 @@ ); | ||
constructor(str: string); | ||
constructor(buf: Buffer); | ||
constructor(buf: Uint8Array); | ||
@@ -56,5 +56,10 @@ /** | ||
/** | ||
* The codec of the CID in its number form. | ||
*/ | ||
code: number; | ||
/** | ||
* The multihash of the CID. | ||
*/ | ||
multihash: Buffer; | ||
multihash: Uint8Array; | ||
@@ -67,5 +72,5 @@ /** | ||
/** | ||
* The CID as a `Buffer` | ||
* The CID as a `Uint8Array` | ||
*/ | ||
readonly buffer: Buffer; | ||
readonly bytes: Uint8Array; | ||
@@ -75,3 +80,3 @@ /** | ||
*/ | ||
readonly prefix: Buffer; | ||
readonly prefix: Uint8Array; | ||
@@ -103,3 +108,3 @@ /** | ||
*/ | ||
toJSON(): { codec: string; version: 0 | 1; hash: Buffer }; | ||
toJSON(): { codec: string; version: 0 | 1; hash: Uint8Array }; | ||
@@ -106,0 +111,0 @@ /** |
'use strict' | ||
const { Buffer } = require('buffer') | ||
const mh = require('multihashes') | ||
@@ -10,3 +9,11 @@ const multibase = require('multibase') | ||
const withIs = require('class-is') | ||
const uint8ArrayConcat = require('uint8arrays/concat') | ||
const uint8ArrayToString = require('uint8arrays/to-string') | ||
const uint8ArrayEquals = require('uint8arrays/equals') | ||
const codecInts = Object.keys(codecs).reduce((p, name) => { | ||
p[codecs[name]] = name | ||
return p | ||
}, {}) | ||
/** | ||
@@ -16,3 +23,3 @@ * @typedef {Object} SerializedCID | ||
* @param {number} version | ||
* @param {Buffer} multihash | ||
* @param {Uint8Array} multihash | ||
*/ | ||
@@ -45,3 +52,3 @@ | ||
* else -> bs58 encoded multihash | ||
* else if (Buffer) | ||
* else if (Uint8Array) | ||
* if (1st byte is 0 or 1) -> CID | ||
@@ -53,5 +60,5 @@ * else -> multihash | ||
* | ||
* @param {string|Buffer|CID} version | ||
* @param {string} [codec] | ||
* @param {Buffer} [multihash] | ||
* @param {string|Uint8Array|CID} version | ||
* @param {string|number} [codec] | ||
* @param {Uint8Array} [multihash] | ||
* @param {string} [multibaseName] | ||
@@ -62,3 +69,3 @@ * | ||
* new CID(<cidStr>) | ||
* new CID(<cid.buffer>) | ||
* new CID(<cid.bytes>) | ||
* new CID(<multihash>) | ||
@@ -74,3 +81,3 @@ * new CID(<bs58 encoded multihash>) | ||
this.codec = cid.codec | ||
this.multihash = Buffer.from(cid.multihash) | ||
this.multihash = cid.multihash | ||
// Default guard for when a CID < 0.7 is passed with no multibaseName | ||
@@ -103,7 +110,7 @@ this.multibaseName = cid.multibaseName || (cid.version === 0 ? 'base58btc' : 'base32') | ||
if (Buffer.isBuffer(version)) { | ||
if (version instanceof Uint8Array) { | ||
const firstByte = version.slice(0, 1) | ||
const v = parseInt(firstByte.toString('hex'), 16) | ||
if (v === 1) { | ||
// version is a CID buffer | ||
// version is a CID Uint8Array | ||
const cid = version | ||
@@ -115,3 +122,3 @@ this.version = v | ||
} else { | ||
// version is a raw multihash buffer, so v0 | ||
// version is a raw multihash Uint8Array, so v0 | ||
this.version = 0 | ||
@@ -133,2 +140,6 @@ this.codec = 'dag-pb' | ||
if (typeof codec === 'number') { | ||
codec = codecInts[codec] | ||
} | ||
/** | ||
@@ -140,3 +151,3 @@ * @type {string} | ||
/** | ||
* @type {Buffer} | ||
* @type {Uint8Array} | ||
*/ | ||
@@ -154,5 +165,5 @@ this.multihash = multihash | ||
/** | ||
* The CID as a `Buffer` | ||
* The CID as a `Uint8Array` | ||
* | ||
* @return {Buffer} | ||
* @return {Uint8Array} | ||
* @readonly | ||
@@ -162,14 +173,13 @@ * | ||
*/ | ||
get buffer () { | ||
let buffer = this._buffer | ||
get bytes () { | ||
let bytes = this._bytes | ||
if (!buffer) { | ||
if (!bytes) { | ||
if (this.version === 0) { | ||
buffer = this.multihash | ||
bytes = this.multihash | ||
} else if (this.version === 1) { | ||
buffer = Buffer.concat([ | ||
Buffer.from('01', 'hex'), | ||
multicodec.getCodeVarint(this.codec), | ||
this.multihash | ||
]) | ||
const codec = multicodec.getCodeVarint(this.codec) | ||
bytes = uint8ArrayConcat([ | ||
[1], codec, this.multihash | ||
], 1 + codec.byteLength + this.multihash.byteLength) | ||
} else { | ||
@@ -179,7 +189,7 @@ throw new Error('unsupported version') | ||
// Cache this buffer so it doesn't have to be recreated | ||
Object.defineProperty(this, '_buffer', { value: buffer }) | ||
// Cache this Uint8Array so it doesn't have to be recreated | ||
Object.defineProperty(this, '_bytes', { value: bytes }) | ||
} | ||
return buffer | ||
return bytes | ||
} | ||
@@ -190,13 +200,19 @@ | ||
* | ||
* @returns {Buffer} | ||
* @returns {Uint8Array} | ||
* @readonly | ||
*/ | ||
get prefix () { | ||
return Buffer.concat([ | ||
Buffer.from(`0${this.version}`, 'hex'), | ||
multicodec.getCodeVarint(this.codec), | ||
mh.prefix(this.multihash) | ||
]) | ||
const codec = multicodec.getCodeVarint(this.codec) | ||
const multihash = mh.prefix(this.multihash) | ||
const prefix = uint8ArrayConcat([ | ||
[this.version], codec, multihash | ||
], 1 + codec.byteLength + multihash.byteLength) | ||
return prefix | ||
} | ||
get code () { | ||
return codecs[this.codec] | ||
} | ||
/** | ||
@@ -251,3 +267,3 @@ * Convert to a CID of version `0`. | ||
} else if (this.version === 1) { | ||
str = multibase.encode(base, this.buffer).toString() | ||
str = uint8ArrayToString(multibase.encode(base, this.bytes)) | ||
} else { | ||
@@ -298,3 +314,3 @@ throw new Error('unsupported version') | ||
this.version === other.version && | ||
this.multihash.equals(other.multihash) | ||
uint8ArrayEquals(this.multihash, other.multihash) | ||
} | ||
@@ -301,0 +317,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
2267
1
231
1674189
+ Addeduint8arrays@^1.0.0
+ Added@multiformats/base-x@4.0.1(transitive)
+ Added@zxing/text-encoding@0.9.0(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedmultibase@3.1.2(transitive)
+ Addedmulticodec@2.1.3(transitive)
+ Addedmultiformats@9.9.0(transitive)
+ Addedmultihashes@3.1.2(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addeduint8arrays@1.1.02.1.10(transitive)
+ Addedutil@0.12.5(transitive)
+ Addedvarint@6.0.0(transitive)
+ Addedweb-encoding@1.1.5(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removedbuffer@^5.6.0
- Removedbase-x@3.0.10(transitive)
- Removedbase64-js@1.5.1(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedieee754@1.2.1(transitive)
- Removedmultibase@1.0.1(transitive)
- Removedmulticodec@1.0.4(transitive)
- Removedmultihashes@1.0.1(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedvarint@5.0.2(transitive)
Updatedmultibase@^3.0.0
Updatedmulticodec@^2.0.0
Updatedmultihashes@^3.0.1