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

Commit dad30b6

Browse files
achingbrainrvagg
andauthored
chore: upgrade to new multiformats module (#98)
BREAKING CHANGE: Uses new CID class Co-authored-by: Rod Vagg <[email protected]>
1 parent 3894700 commit dad30b6

29 files changed

+727
-755
lines changed

.aegir.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@ const path = require('path')
44

55
const esbuild = {
66
// this will inject all the named exports from 'node-globals.js' as globals
7-
inject: [path.join(__dirname, 'scripts/node-globals.js')],
8-
plugins: [
9-
{
10-
name: 'node built ins', // this will make the bundler resolve node builtins to the respective browser polyfill
11-
setup (build) {
12-
build.onResolve({ filter: /^stream$/ }, () => {
13-
return { path: require.resolve('readable-stream') }
14-
})
15-
}
16-
}
17-
]
7+
inject: [path.join(__dirname, 'scripts/node-globals.js')]
188
}
199

2010
/** @type {import('aegir').PartialOptions} */
@@ -27,6 +17,6 @@ module.exports = {
2717
}
2818
},
2919
build: {
30-
config: esbuild
20+
bundlesizeMax: '37kB'
3121
}
3222
}

migrations/migration-10/index.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const {
4-
createStore,
54
findLevelJs
65
} = require('../../src/utils')
76
const fromString = require('uint8arrays/from-string')
@@ -10,6 +9,7 @@ const toString = require('uint8arrays/to-string')
109
/**
1110
* @typedef {import('../../src/types').Migration} Migration
1211
* @typedef {import('interface-datastore').Datastore} Datastore
12+
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1313
* @typedef {import('../../src/types').MigrationProgressCallback} MigrationProgressCallback
1414
*
1515
* @typedef {{ type: 'del', key: string | Uint8Array } | { type: 'put', key: string | Uint8Array, value: Uint8Array }} Operation
@@ -78,18 +78,32 @@ async function keysToStrings (name, store, onProgress = () => {}) {
7878
}
7979

8080
/**
81-
*
82-
* @param {string} repoPath
83-
* @param {any} repoOptions
81+
* @param {any} store
82+
* @returns {Datastore}
83+
*/
84+
function unwrap (store) {
85+
if (store.child) {
86+
return unwrap(store.child)
87+
}
88+
89+
return store
90+
}
91+
92+
/**
93+
* @param {import('../../src/types').Backends} backends
8494
* @param {MigrationProgressCallback} onProgress
8595
* @param {*} fn
8696
*/
87-
async function process (repoPath, repoOptions, onProgress, fn) {
88-
const datastores = Object.keys(repoOptions.storageBackends)
89-
.filter(key => repoOptions.storageBackends[key].name === 'LevelDatastore')
90-
.map(name => ({
91-
name,
92-
store: createStore(repoPath, name, repoOptions)
97+
async function process (backends, onProgress, fn) {
98+
/**
99+
* @type {{ name: string, store: Datastore }[]}
100+
*/
101+
const datastores = Object.entries(backends)
102+
.map(([key, backend]) => ({ key, backend: unwrap(backend) }))
103+
.filter(({ key, backend }) => backend.constructor.name === 'LevelDatastore')
104+
.map(({ key, backend }) => ({
105+
name: key,
106+
store: backend
93107
}))
94108

95109
onProgress(0, `Migrating ${datastores.length} dbs`)
@@ -120,11 +134,11 @@ async function process (repoPath, repoOptions, onProgress, fn) {
120134
module.exports = {
121135
version: 10,
122136
description: 'Migrates datastore-level keys to binary',
123-
migrate: (repoPath, repoOptions, onProgress = () => {}) => {
124-
return process(repoPath, repoOptions, onProgress, keysToBinary)
137+
migrate: (backends, onProgress = () => {}) => {
138+
return process(backends, onProgress, keysToBinary)
125139
},
126-
revert: (repoPath, repoOptions, onProgress = () => {}) => {
127-
return process(repoPath, repoOptions, onProgress, keysToStrings)
140+
revert: (backends, onProgress = () => {}) => {
141+
return process(backends, onProgress, keysToStrings)
128142
}
129143
}
130144

migrations/migration-8/index.js

+53-30
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,82 @@
11
'use strict'
22

3-
const CID = require('cids')
3+
const { CID } = require('multiformats/cid')
44
const Key = require('interface-datastore').Key
5-
const mb = require('multibase')
65
const log = require('debug')('ipfs:repo:migrator:migration-8')
7-
const uint8ArrayToString = require('uint8arrays/to-string')
8-
const { createStore } = require('../../src/utils')
6+
97
const length = require('it-length')
8+
const { base32 } = require('multiformats/bases/base32')
9+
const raw = require('multiformats/codecs/raw')
10+
const mhd = require('multiformats/hashes/digest')
1011

1112
/**
1213
* @typedef {import('../../src/types').Migration} Migration
14+
* @typedef {import('interface-datastore').Datastore} Datastore
15+
*/
16+
17+
/**
18+
* @param {*} blockstore
19+
* @returns {Datastore}
1320
*/
21+
function unwrap (blockstore) {
22+
if (blockstore.child) {
23+
return unwrap(blockstore.child)
24+
}
25+
26+
return blockstore
27+
}
1428

1529
/**
1630
* @param {Key} key
1731
*/
1832
function keyToMultihash (key) {
19-
const buf = mb.decode(`b${key.toString().slice(1)}`)
20-
21-
// Extract multihash from CID
22-
let multihash = new CID(buf).multihash
33+
try {
34+
const buf = base32.decode(`b${key.toString().toLowerCase().slice(1)}`)
2335

24-
// Encode and slice off multibase codec
25-
multihash = mb.encode('base32', multihash).slice(1)
36+
// Extract multihash from CID
37+
let multihash = CID.decode(buf).multihash.bytes
2638

27-
// Should be uppercase for interop with go
28-
const multihashStr = uint8ArrayToString(multihash).toUpperCase()
39+
// Encode and slice off multibase codec
40+
// Should be uppercase for interop with go
41+
const multihashStr = base32.encode(multihash).slice(1).toUpperCase()
2942

30-
return new Key(`/${multihashStr}`, false)
43+
return new Key(`/${multihashStr}`, false)
44+
} catch (err) {
45+
return key
46+
}
3147
}
3248

3349
/**
3450
* @param {Key} key
3551
*/
3652
function keyToCid (key) {
37-
const buf = mb.decode(`b${key.toString().slice(1)}`)
53+
try {
54+
const buf = base32.decode(`b${key.toString().toLowerCase().slice(1)}`)
55+
const digest = mhd.decode(buf)
3856

39-
// CID to Key
40-
const multihash = mb.encode('base32', new CID(1, 'raw', buf).bytes).slice(1)
57+
// CID to Key
58+
const multihash = base32.encode(CID.createV1(raw.code, digest).bytes).slice(1)
4159

42-
return new Key(`/${uint8ArrayToString(multihash)}`.toUpperCase(), false)
60+
return new Key(`/${multihash.toUpperCase()}`, false)
61+
} catch {
62+
return key
63+
}
4364
}
4465

4566
/**
46-
* @param {string} repoPath
47-
* @param {*} repoOptions
67+
* @param {import('../../src/types').Backends} backends
4868
* @param {(percent: number, message: string) => void} onProgress
4969
* @param {(key: Key) => Key} keyFunction
5070
*/
51-
async function process (repoPath, repoOptions, onProgress, keyFunction) {
52-
const blockstore = createStore(repoPath, 'blocks', repoOptions)
71+
async function process (backends, onProgress, keyFunction) {
72+
const blockstore = backends.blocks
5373
await blockstore.open()
5474

75+
const unwrapped = unwrap(blockstore)
76+
5577
let blockCount
5678

57-
blockCount = await length(blockstore.queryKeys({
79+
blockCount = await length(unwrapped.queryKeys({
5880
filters: [(key) => {
5981
const newKey = keyFunction(key)
6082

@@ -65,15 +87,16 @@ async function process (repoPath, repoOptions, onProgress, keyFunction) {
6587
try {
6688
let counter = 0
6789

68-
for await (const block of blockstore.query({})) {
90+
for await (const block of unwrapped.query({})) {
6991
const newKey = keyFunction(block.key)
7092

7193
// If the Key is base32 CIDv0 then there's nothing to do
7294
if(newKey.toString() !== block.key.toString()) {
7395
counter += 1
74-
log(`Migrating Block from ${block.key} to ${newKey}`)
75-
await blockstore.delete(block.key)
76-
await blockstore.put(newKey, block.value)
96+
log(`Migrating Block from ${block.key} to ${newKey}`, await unwrapped.has(block.key))
97+
98+
await unwrapped.delete(block.key)
99+
await unwrapped.put(newKey, block.value)
77100

78101
onProgress((counter / blockCount) * 100, `Migrated Block from ${block.key} to ${newKey}`)
79102
}
@@ -87,10 +110,10 @@ async function process (repoPath, repoOptions, onProgress, keyFunction) {
87110
module.exports = {
88111
version: 8,
89112
description: 'Transforms key names into base32 encoding and converts Block store to use bare multihashes encoded as base32',
90-
migrate: (repoPath, repoOptions, onProgress = () => {}) => {
91-
return process(repoPath, repoOptions, onProgress, keyToMultihash)
113+
migrate: (backends, onProgress = () => {}) => {
114+
return process(backends, onProgress, keyToMultihash)
92115
},
93-
revert: (repoPath, repoOptions, onProgress = () => {}) => {
94-
return process(repoPath, repoOptions, onProgress, keyToCid)
116+
revert: (backends, onProgress = () => {}) => {
117+
return process(backends, onProgress, keyToCid)
95118
}
96119
}

0 commit comments

Comments
 (0)