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

Commit 7a93583

Browse files
committed
Migration 8: Encoding key's names to base32
License: MIT Signed-off-by: Adam Uhlir <[email protected]>
1 parent 5a537ce commit 7a93583

15 files changed

+1057
-0
lines changed

migrations/migration-8/index.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict'
2+
3+
const Datastore = require('datastore-fs')
4+
const path = require('path')
5+
const base32 = require('base32.js')
6+
const Key = require('interface-datastore').Key
7+
const log = require('debug')('jsipfs-repo-migrations:migration-8')
8+
9+
const KEY_PREFIX = 'key_'
10+
11+
function encode(name) {
12+
name = Buffer.from(name)
13+
const encoder = new base32.Encoder({type: "rfc4648"});
14+
return (KEY_PREFIX + encoder.finalize(name)).toLowerCase()
15+
}
16+
17+
function decode(name) {
18+
log(name)
19+
if (!name.startsWith(KEY_PREFIX)) {
20+
throw Error("Unknown format of key's name!")
21+
}
22+
23+
const decoder = new base32.Decoder({type: "rfc4648"});
24+
return decoder.finalize(name.replace(KEY_PREFIX, '').toUpperCase())
25+
}
26+
27+
async function processFolder(store, prefix, fileNameProcessor) {
28+
const query = {
29+
prefix: `/${prefix}`,
30+
}
31+
32+
const files = await store.query(query)
33+
for await (let file of files) {
34+
const name = String(file.key._buf).replace(`/${prefix}/`, '')
35+
const encodedFileName = fileNameProcessor(name)
36+
const newKey = new Key(`${prefix}/${encodedFileName}`)
37+
38+
await store.delete(file.key)
39+
log(`Translating key's name '${file.key}' into '${newKey}'`)
40+
await store.put(newKey, file.value)
41+
}
42+
}
43+
44+
async function migrate(repoPath, isBrowser) {
45+
const store = new Datastore(path.join(repoPath, 'keys'), {extension: '.data', createIfMissing: false})
46+
store.open()
47+
try {
48+
const info = processFolder(store, 'info', encode)
49+
const data = processFolder(store, 'pkcs8', encode)
50+
51+
await Promise.all([info, data])
52+
} finally {
53+
await store.close()
54+
}
55+
}
56+
57+
async function revert(repoPath, isBrowser) {
58+
const store = new Datastore(path.join(repoPath, 'keys'), {extension: '.data', createIfMissing: false})
59+
store.open()
60+
61+
try {
62+
const info = processFolder(store, 'info', decode)
63+
const data = processFolder(store, 'pkcs8', decode)
64+
65+
await Promise.all([info, data])
66+
} finally {
67+
await store.close()
68+
}
69+
}
70+
71+
module.exports = {
72+
version: 8,
73+
description: 'Transforms key\'s names into base32 encoding.',
74+
reversible: true,
75+
migrate,
76+
revert,
77+
}

migrations/migration-8/node_modules/base32.js/.npmignore

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrations/migration-8/node_modules/base32.js/.travis.yml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrations/migration-8/node_modules/base32.js/HISTORY.md

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrations/migration-8/node_modules/base32.js/README.md

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)