Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,11 @@ | ||
<a name="0.3.0"></a> | ||
# [0.3.0](https://github.com/ipfs/js-cid/compare/v0.2.0...v0.3.0) (2016-12-05) | ||
### Features | ||
* **deps:** update to multihashes[@0](https://github.com/0).3.0 ([a0e331d](https://github.com/ipfs/js-cid/commit/a0e331d)) | ||
<a name="0.2.0"></a> | ||
@@ -2,0 +12,0 @@ # [0.2.0](https://github.com/ipfs/js-cid/compare/v0.1.1...v0.2.0) (2016-10-24) |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
'use strict' | ||
@@ -13,2 +13,2 @@ /* | ||
'eth-tx': new Buffer('91', 'hex') | ||
}; | ||
} |
250
lib/index.js
@@ -1,17 +0,32 @@ | ||
'use strict'; | ||
/* @flow */ | ||
'use strict' | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
const mh = require('multihashes') | ||
const multibase = require('multibase') | ||
const multicodec = require('multicodec') | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
const codecs = require('./codecs') | ||
var mh = require('multihashes'); | ||
var multibase = require('multibase'); | ||
var multicodec = require('multicodec'); | ||
var codecs = require('./codecs'); | ||
// CID: <mbase><version><mcodec><mhash> | ||
var CID = function () { | ||
/* | ||
/** | ||
* Class representing a CID `<mbase><version><mcodec><mhash>` | ||
* | ||
* @class CID | ||
*/ | ||
class CID { | ||
version: number; | ||
multihash: Buffer; | ||
codec: string; | ||
/** | ||
* Construct a CID. | ||
* @param {string} version | ||
* @param {number} codec | ||
* @param {Buffer} multihash | ||
* | ||
* @prop {number} version | ||
* @prop {Buffer} multihash | ||
* @prop {string} codec | ||
* | ||
* @static {any} codecs | ||
* @static {function} isCID | ||
* | ||
* if (str) | ||
@@ -28,110 +43,143 @@ * if (1st char is on multibase table) -> CID String | ||
*/ | ||
function CID(version, codec, multihash) { | ||
_classCallCheck(this, CID); | ||
constructor (version: string, codec: number, multihash: Buffer) { | ||
if (typeof version === 'string') { | ||
if (multibase.isEncoded(version)) { | ||
// CID String (encoded with multibase) | ||
var cid = multibase.decode(version); | ||
this.version = parseInt(cid.slice(0, 1).toString('hex'), 16); | ||
this.codec = multicodec.getCodec(cid.slice(1)); | ||
this.multihash = multicodec.rmPrefix(cid.slice(1)); | ||
} else { | ||
// bs58 string encoded multihash | ||
this.codec = 'dag-pb'; | ||
this.multihash = mh.fromB58String(version); | ||
this.version = 0; | ||
if (multibase.isEncoded(version)) { // CID String (encoded with multibase) | ||
const cid = multibase.decode(version) | ||
this.version = parseInt(cid.slice(0, 1).toString('hex'), 16) | ||
this.codec = multicodec.getCodec(cid.slice(1)) | ||
this.multihash = multicodec.rmPrefix(cid.slice(1)) | ||
} else { // bs58 string encoded multihash | ||
this.codec = 'dag-pb' | ||
this.multihash = mh.fromB58String(version) | ||
this.version = 0 | ||
} | ||
} else if (Buffer.isBuffer(version)) { | ||
var firstByte = version.slice(0, 1); | ||
var v = parseInt(firstByte.toString('hex'), 16); | ||
if (v === 0 || v === 1) { | ||
// CID | ||
var _cid = version; | ||
this.version = v; | ||
this.codec = multicodec.getCodec(_cid.slice(1)); | ||
this.multihash = multicodec.rmPrefix(_cid.slice(1)); | ||
} else { | ||
// multihash | ||
this.codec = 'dag-pb'; | ||
this.multihash = version; | ||
this.version = 0; | ||
const firstByte = version.slice(0, 1) | ||
const v = parseInt(firstByte.toString('hex'), 16) | ||
if (v === 0 || v === 1) { // CID | ||
const cid = version | ||
this.version = v | ||
this.codec = multicodec.getCodec(cid.slice(1)) | ||
this.multihash = multicodec.rmPrefix(cid.slice(1)) | ||
} else { // multihash | ||
this.codec = 'dag-pb' | ||
this.multihash = version | ||
this.version = 0 | ||
} | ||
} else if (typeof version === 'number') { | ||
if (typeof codec !== 'string') { | ||
throw new Error('codec must be string'); | ||
throw new Error('codec must be string') | ||
} | ||
if (!(version === 0 || version === 1)) { | ||
throw new Error('version must be a number equal to 0 or 1'); | ||
throw new Error('version must be a number equal to 0 or 1') | ||
} | ||
mh.validate(multihash); | ||
this.codec = codec; | ||
this.version = version; | ||
this.multihash = multihash; | ||
mh.validate(multihash) | ||
this.codec = codec | ||
this.version = version | ||
this.multihash = multihash | ||
} | ||
} | ||
_createClass(CID, [{ | ||
key: 'toV0', | ||
value: function toV0() { | ||
return this.multihash; | ||
/** | ||
* The CID as a `Buffer` | ||
* | ||
* @return {Buffer} | ||
* @readonly | ||
* | ||
* @memberOf CID | ||
*/ | ||
get buffer () : Buffer { | ||
switch (this.version) { | ||
case 0: | ||
return this.multihash | ||
case 1: | ||
return Buffer.concat([ | ||
new Buffer('01', 'hex'), | ||
new Buffer(codecs[this.codec]), | ||
this.multihash | ||
]) | ||
default: | ||
throw new Error('unsupported version') | ||
} | ||
}, { | ||
key: 'toV1', | ||
value: function toV1() { | ||
return this.buffer; | ||
} | ||
} | ||
/* defaults to base58btc */ | ||
toV0 () { | ||
return this.multihash | ||
} | ||
}, { | ||
key: 'toBaseEncodedString', | ||
value: function toBaseEncodedString(base) { | ||
base = base || 'base58btc'; | ||
toV1 () { | ||
return this.buffer | ||
} | ||
switch (this.version) { | ||
case 0: | ||
return mh.toB58String(this.multihash); | ||
case 1: | ||
return multibase.encode(base, this.buffer).toString(); | ||
default: | ||
throw new Error('Unsupported version'); | ||
} | ||
/** | ||
* Encode the CID into a string. | ||
* | ||
* @param {string} base - Mutlibase to use for encoding | ||
* @return {string} | ||
* | ||
* @memberOf CID | ||
*/ | ||
toBaseEncodedString (base: string) : string { | ||
base = base || 'base58btc' | ||
switch (this.version) { | ||
case 0: | ||
return mh.toB58String(this.multihash) | ||
case 1: | ||
return multibase.encode(base, this.buffer).toString() | ||
default: | ||
throw new Error('Unsupported version') | ||
} | ||
}, { | ||
key: 'toJSON', | ||
value: function toJSON() { | ||
return { | ||
codec: this.codec, | ||
version: this.version, | ||
hash: this.multihash | ||
}; | ||
} | ||
/** | ||
* @typedef {Object} SerializedCID | ||
* @property {string} codec | ||
* @property {number} version | ||
* @property {Buffer} multihash | ||
* | ||
*/ | ||
/** | ||
* Serialize to a plain object. | ||
* | ||
* @return {SerializedCID} | ||
* | ||
* @memberOf CID | ||
*/ | ||
toJSON () { | ||
return { | ||
codec: this.codec, | ||
version: this.version, | ||
hash: this.multihash | ||
} | ||
}, { | ||
key: 'equals', | ||
value: function equals(other) { | ||
return this.codec === other.codec && this.version === other.version && this.multihash.equals(other.multihash); | ||
} | ||
}, { | ||
key: 'buffer', | ||
get: function get() { | ||
switch (this.version) { | ||
case 0: | ||
return this.multihash; | ||
case 1: | ||
return Buffer.concat([Buffer('01', 'hex'), Buffer(codecs[this.codec]), this.multihash]); | ||
default: | ||
throw new Error('unsupported version'); | ||
} | ||
} | ||
}]); | ||
} | ||
return CID; | ||
}(); | ||
/** | ||
* Compare equality with another CID. | ||
* | ||
* @param {CID} other | ||
* @return {boolean} | ||
* | ||
* @memberOf CID | ||
*/ | ||
equals (other: CID) : boolean { | ||
return this.codec === other.codec && | ||
this.version === other.version && | ||
this.multihash.equals(other.multihash) | ||
} | ||
} | ||
CID.codecs = codecs; | ||
CID.isCID = function (other) { | ||
return other.constructor.name === 'CID'; | ||
}; | ||
CID.codecs = codecs | ||
module.exports = CID; | ||
/** | ||
* Test if the given input is a valid CID object. | ||
* | ||
* @param {any} other | ||
* @return {boolean} | ||
*/ | ||
CID.isCID = (other) => { | ||
return other.constructor.name === 'CID' | ||
} | ||
module.exports = CID |
{ | ||
"name": "cids", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "cid implementation", | ||
"main": "lib/index.js", | ||
"jsnext:main": "src/index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
@@ -40,12 +39,12 @@ "test:node": "aegir-test node", | ||
"multicodec": "0.1.0", | ||
"multihashes": "^0.2.2" | ||
"multihashes": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^8.0.1", | ||
"aegir": "^9.0.1", | ||
"chai": "^3.5.0", | ||
"multihashing": "^0.2.1", | ||
"pre-commit": "^1.1.2" | ||
"multihashing-async": "^0.2.0", | ||
"pre-commit": "^1.1.3" | ||
}, | ||
"engines": { | ||
"node": ">=4.2.2" | ||
"node": ">=4.0.0" | ||
}, | ||
@@ -55,4 +54,5 @@ "contributors": [ | ||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>", | ||
"Richard Littauer <richard.littauer@gmail.com>" | ||
"Richard Littauer <richard.littauer@gmail.com>", | ||
"kumavis <kumavis@users.noreply.github.com>" | ||
] | ||
} |
@@ -10,3 +10,7 @@ # js-cid | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) | ||
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square) | ||
[![Sauce Test Status](https://saucelabs.com/browser-matrix/ipfs-js-cid.svg)](https://saucelabs.com/u/ipfs-js-cid) | ||
> CID implementation in JavaScript. | ||
@@ -13,0 +17,0 @@ |
Sorry, the diff of this file is not supported yet
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 too big to display
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
125
232823
2901
+ Addedbase-x@3.0.10(transitive)
+ Addedbs58@4.0.1(transitive)
+ Addedmultihashes@0.3.3(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
- Removedmultihashes@0.2.2(transitive)
Updatedmultihashes@^0.3.0