This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
util.js
100 lines (86 loc) · 2.77 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'
const multihashing = require('multihashing-async')
const CID = require('cids')
const multicodec = require('multicodec')
const { Buffer } = require('buffer')
const uint8ArrayToString = require('uint8arrays/to-string')
const gitUtil = require('./util/util')
const commit = require('./util/commit')
const tag = require('./util/tag')
const tree = require('./util/tree')
exports = module.exports
exports.codec = multicodec.GIT_RAW
exports.defaultHashAlg = multicodec.SHA1
/**
* Serialize internal representation into a binary Git block.
*
* @param {GitBlock} dagNode - Internal representation of a Git block
* @returns {Uint8Array}
*/
exports.serialize = (dagNode) => {
if (dagNode === null) {
throw new Error('dagNode passed to serialize was null')
}
if (dagNode instanceof Uint8Array) {
if (uint8ArrayToString(dagNode.slice(0, 4)) === 'blob') {
return dagNode
} else {
throw new Error('unexpected dagNode passed to serialize')
}
}
switch (dagNode.gitType) {
case 'commit':
return commit.serialize(dagNode)
case 'tag':
return tag.serialize(dagNode)
default:
// assume tree as a file named 'type' is legal
return tree.serialize(dagNode)
}
}
/**
* Deserialize Git block into the internal representation.
*
* @param {Uint8Array} data - Binary representation of a Git block.
* @returns {BitcoinBlock}
*/
exports.deserialize = (data) => {
if (!Buffer.isBuffer(data)) {
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
}
const headLen = gitUtil.find(data, 0)
const head = data.slice(0, headLen).toString()
const typeLen = head.match(/([^ ]+) (\d+)/)
if (!typeLen) {
throw new Error('invalid object header')
}
switch (typeLen[1]) {
case 'blob':
return data
case 'commit':
return commit.deserialize(data.slice(headLen + 1))
case 'tag':
return tag.deserialize(data.slice(headLen + 1))
case 'tree':
return tree.deserialize(data.slice(headLen + 1))
default:
throw new Error('unknown object type ' + typeLen[1])
}
}
/**
* Calculate the CID of the binary blob.
*
* @param {Object} binaryBlob - Encoded IPLD Node
* @param {Object} [userOptions] - Options to create the CID
* @param {number} [userOptions.cidVersion=1] - CID version number
* @param {string} [UserOptions.hashAlg] - Defaults to the defaultHashAlg of the format
* @returns {Promise.<CID>}
*/
exports.cid = async (binaryBlob, userOptions) => {
const defaultOptions = { cidVersion: 1, hashAlg: exports.defaultHashAlg }
const options = Object.assign(defaultOptions, userOptions)
const multihash = await multihashing(binaryBlob, options.hashAlg)
const codecName = multicodec.print[exports.codec]
const cid = new CID(options.cidVersion, codecName, multihash)
return cid
}