Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ build
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
docs
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ build
node_modules

test
docs
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ const multicodec = require('multicodec')

### API

#### `multicodec.addPrefix(<multicodecStrOrCode>, <data>)`

> Prefixes a buffer with a multicodec-packed

#### `multicodec.rmPrefix(<prefixedData>)`

> Decapsulate the multicodec-packed prefix from the data

#### `multicodec.getCodec(<prefixedData>)`

> Get the codec of the prefixedData

## [multicodec default table](https://github.com/multiformats/multicodec/blob/master/multicodec.md)

## Contribute
Expand Down
8 changes: 8 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const multicodec = require('multicodec')

const prefixedProtobuf = multicodec.addPrefix('protobuf', new Buffer('some protobuf code'))

console.log(prefixedProtobuf)
// => prefixedProtobuf 0x50...
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"build": "aegir-build",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"docs": "aegir-docs",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
Expand Down Expand Up @@ -39,17 +40,17 @@
},
"homepage": "https://github.com/multiformats/js-multicodec-packed#readme",
"dependencies": {
"varint": "^4.0.1"
"varint": "^5.0.0"
},
"devDependencies": {
"aegir": "^9.1.2",
"aegir": "^9.3.0",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
"pre-commit": "^1.2.2"
},
"contributors": [
"David Dias <[email protected]>",
"Richard Littauer <[email protected]>",
"kumavis <[email protected]>",
"wanderer <[email protected]>"
]
}
}
31 changes: 30 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
/**
* Implementation of the multicodec specification.
*
* @module multicodec
* @example
* const multicodec = require('multicodec')
*
* const prefixedProtobuf = multicodec.addPrefix('protobuf', protobufBuffer)
* // prefixedProtobuf 0x50...
*
*/
'use strict'

const varint = require('varint')
const codecNameToCodeVarint = require('./varint-table')
const codeToCodecName = require('./name-table')
const util = require('./util')

exports = module.exports

/**
* Prefix a buffer with a multicodec-packed.
*
* @param {string|number} multicodecStrOrCode
* @param {Buffer} data
* @returns {Buffer}
*/
exports.addPrefix = (multicodecStrOrCode, data) => {
let prefix

Expand All @@ -21,14 +40,24 @@ exports.addPrefix = (multicodecStrOrCode, data) => {
return Buffer.concat([prefix, data])
}

/**
* Decapsulate the multicodec-packed prefix from the data.
*
* @param {Buffer} data
* @returns {Buffer}
*/
exports.rmPrefix = (data) => {
varint.decode(data)
return data.slice(varint.decode.bytes)
}

/**
* Get the codec of the prefixed data.
* @param {Buffer} prefixedData
* @returns {string}
*/
exports.getCodec = (prefixedData) => {
const code = util.varintBufferDecode(prefixedData)
const codecName = codeToCodecName[code.toString('hex')]
return codecName
}