|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -var assert = require('assert'), |
4 | | - crypto = require('crypto'), |
5 | | - EventEmitter = require('events').EventEmitter, |
6 | | - fs = require('fs'), |
7 | | - util = require('util'), |
8 | | - escodb = require('../escodb'); |
9 | | - |
10 | | -var READERS = [ |
11 | | - require('./v03_reader'), |
12 | | - require('./v02_reader') |
13 | | -]; |
14 | | - |
15 | | -var random = function() { |
16 | | - return crypto.randomBytes(6).toString('hex'); |
17 | | -}; |
18 | | - |
19 | | -var Migrator = function(filename, password) { |
20 | | - this._filename = filename; |
21 | | - this._password = password; |
22 | | -}; |
23 | | -util.inherits(Migrator, EventEmitter); |
24 | | - |
25 | | -Migrator.prototype.run = function() { |
26 | | - var self = this; |
27 | | - |
28 | | - return new Promise(function(resolve, reject) { |
29 | | - try { |
30 | | - resolve(self._migrate()); |
31 | | - } catch (error) { |
32 | | - reject(error); |
33 | | - } |
34 | | - }); |
35 | | -}; |
36 | | - |
37 | | -Migrator.prototype.log = function(message) { |
38 | | - this.emit('message', message); |
39 | | -}; |
40 | | - |
41 | | -Migrator.prototype._migrate = function() { |
42 | | - var self = this; |
43 | | - |
44 | | - this._readInputFile(); |
45 | | - this._decryptFile(); |
46 | | - |
47 | | - this._createStore().then(function() { |
48 | | - return this._copySettings(); |
49 | | - }).then(function() { |
50 | | - return self._swapFiles(); |
51 | | - }).then(function() { |
52 | | - self.log('done'); |
53 | | - return self._backuppath; |
54 | | - }); |
55 | | -}; |
56 | | - |
57 | | -Migrator.prototype._readInputFile = function() { |
58 | | - this.log('reading input file: ' + this._filename); |
59 | | - |
60 | | - try { |
61 | | - this._content = fs.readFileSync(this._filename, 'utf8'); |
62 | | - } catch (error) { |
63 | | - throw new Error('File is unreadable: ' + this._filename); |
| 3 | +const assert = require('assert') |
| 4 | +const fs = require('fs') |
| 5 | + |
| 6 | +const crypto = require('../crypto') |
| 7 | +const escodb = require('../escodb') |
| 8 | +const Reader = require('./reader') |
| 9 | + |
| 10 | +class Migrator { |
| 11 | + constructor ({ logger, pathname, password }) { |
| 12 | + this._logger = logger |
| 13 | + this._filename = pathname |
| 14 | + this._password = password |
| 15 | + }; |
| 16 | + |
| 17 | + log (message) { |
| 18 | + this._logger.info(message) |
64 | 19 | } |
65 | 20 |
|
66 | | - this._buffer = Buffer.from(this._content, 'base64'); |
67 | | -}; |
| 21 | + async run () { |
| 22 | + await this._readInputFile() |
| 23 | + await this._decryptFile() |
| 24 | + await this._createStore() |
| 25 | + await this._copySettings() |
| 26 | + await this._swapFiles() |
| 27 | + |
| 28 | + this.log('done') |
| 29 | + return this._backuppath |
| 30 | + } |
68 | 31 |
|
69 | | -Migrator.prototype._decryptFile = function() { |
70 | | - READERS.forEach(function(Reader) { |
71 | | - if (this._data) return; |
| 32 | + _readInputFile () { |
| 33 | + this.log('reading input file: ' + this._filename) |
72 | 34 |
|
73 | | - var reader = new Reader(this, this._buffer, this._password); |
74 | 35 | try { |
75 | | - this._data = reader.run(); |
| 36 | + this._content = fs.readFileSync(this._filename, 'utf8') |
76 | 37 | } catch (error) { |
77 | | - this.log(error.message); |
| 38 | + throw new Error('file is unreadable: ' + this._filename) |
78 | 39 | } |
79 | | - }, this); |
80 | | - |
81 | | - if (!this._data) |
82 | | - throw new Error('Failed to decrypt the file'); |
83 | | -}; |
84 | | - |
85 | | -Migrator.prototype._createStore = function() { |
86 | | - this._storepath = '/tmp/vault-convert-' + random(); |
87 | | - this.log('creating new storage target: ' + this._storepath); |
88 | | - |
89 | | - var adapter = escodb.createFileAdapter(this._storepath); |
| 40 | + } |
90 | 41 |
|
91 | | - return escodb.createStore({adapter: adapter, password: this._password}).then(function(store) { |
92 | | - this._store = store; |
93 | | - }); |
94 | | -}; |
| 42 | + async _decryptFile () { |
| 43 | + let config = { logger: this._logger, password: this._password } |
| 44 | + this._data = await Reader.read(config, this._content) |
95 | 45 |
|
96 | | -Migrator.prototype._copySettings = function() { |
97 | | - var copies = [['/global', this._data.global]]; |
| 46 | + if (!this._data) { |
| 47 | + throw new Error('failed to decrypt the file') |
| 48 | + } |
| 49 | + } |
98 | 50 |
|
99 | | - for (var service in this._data.services) |
100 | | - copies.push(['/services/' + service, this._data.services[service]]); |
| 51 | + async _createStore () { |
| 52 | + this._storepath = '/tmp/vault-convert-' + random() |
| 53 | + this.log('creating new storage target: ' + this._storepath) |
101 | 54 |
|
102 | | - var self = this; |
| 55 | + let adapter = escodb.createFileAdapter(this._storepath) |
| 56 | + this._store = await escodb.createStore({ adapter, password: this._password }) |
| 57 | + } |
103 | 58 |
|
104 | | - return copies.reduce(function(state, copy) { |
105 | | - var key = copy[0], |
106 | | - value = copy[1]; |
| 59 | + async _copySettings () { |
| 60 | + let copies = [['/global', this._data.global]]; |
107 | 61 |
|
108 | | - if (value === undefined) return state; |
| 62 | + for (let service in this._data.services) { |
| 63 | + copies.push(['/services/' + service, this._data.services[service]]) |
| 64 | + } |
109 | 65 |
|
110 | | - return state.then(function() { |
111 | | - return self._store.update(key, function() { return value }); |
| 66 | + for (let [key, value] of copies) { |
| 67 | + await this._store.update(key, () => value) |
| 68 | + let stored = await this._store.get(key) |
112 | 69 |
|
113 | | - }).then(function() { |
114 | | - return self._store.get(key); |
| 70 | + assert.deepEqual(value, stored, |
| 71 | + 'failed to write: [' + key + '] ' + JSON.stringify(value)) |
115 | 72 |
|
116 | | - }).then(function(result) { |
117 | | - var message = 'Failed to write: [' + key + '] ' + JSON.stringify(value); |
118 | | - assert.deepEqual(result, value, message); |
| 73 | + this.log('wrote setting: ' + key) |
| 74 | + } |
| 75 | + } |
119 | 76 |
|
120 | | - self.log('wrote setting: ' + key); |
121 | | - }); |
122 | | - }, Promise.resolve()); |
123 | | -}; |
| 77 | + _swapFiles () { |
| 78 | + this._backuppath = '/tmp/vault-backup-' + random() |
124 | 79 |
|
125 | | -Migrator.prototype._swapFiles = function() { |
126 | | - this._backuppath = '/tmp/vault-backup-' + random(); |
| 80 | + this._rename(this._filename, this._backuppath) |
| 81 | + this._rename(this._storepath, this._filename) |
| 82 | + } |
127 | 83 |
|
128 | | - this.log('moving old file: ' + this._filename + ' -> ' + this._backuppath); |
129 | | - fs.renameSync(this._filename, this._backuppath); |
| 84 | + _rename (a, b) { |
| 85 | + this.log('moving file: ' + a + ' -> ' + b) |
| 86 | + fs.renameSync(a, b) |
| 87 | + } |
| 88 | +} |
130 | 89 |
|
131 | | - this.log('moving new file: ' + this._storepath + ' -> ' + this._filename); |
132 | | - fs.renameSync(this._storepath, this._filename); |
133 | | -}; |
| 90 | +function random () { |
| 91 | + return crypto.randomBytes(6).toString('hex') |
| 92 | +} |
134 | 93 |
|
135 | | -module.exports = Migrator; |
| 94 | +module.exports = Migrator |
0 commit comments