-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.js
417 lines (363 loc) · 10.5 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
'use strict'
const _get = require('just-safe-get')
const debug = require('debug')
const Big = require('bignumber.js')
const errcode = require('err-code')
const migrator = require('ipfs-repo-migrations')
const bytes = require('bytes')
const pathJoin = require('ipfs-utils/src/path-join')
const constants = require('./constants')
const backends = require('./backends')
const version = require('./version')
const config = require('./config')
const spec = require('./spec')
const apiAddr = require('./api-addr')
const blockstore = require('./blockstore')
const defaultOptions = require('./default-options')
const defaultDatastore = require('./default-datastore')
const ERRORS = require('./errors')
const log = debug('ipfs:repo')
const noLimit = Number.MAX_SAFE_INTEGER
const AUTO_MIGRATE_CONFIG_KEY = 'repoAutoMigrate'
const lockers = {
memory: require('./lock-memory'),
fs: require('./lock')
}
/**
* IpfsRepo implements all required functionality to read and write to an ipfs repo.
*/
class IpfsRepo {
/**
* @param {String} repoPath - path where the repo is stored
* @param {Object} options - Configuration
*/
constructor (repoPath, options) {
if (typeof repoPath !== 'string') {
throw new Error('missing repoPath')
}
this.options = buildOptions(options)
this.closed = true
this.path = repoPath
this._locker = this._getLocker()
this.root = backends.create('root', this.path, this.options)
this.version = version(this.root)
this.config = config(this.root)
this.spec = spec(this.root)
this.apiAddr = apiAddr(this.root)
}
/**
* Initialize a new repo.
*
* @param {Object} config - config to write into `config`.
* @returns {Promise<void>}
*/
async init (config) {
log('initializing at: %s', this.path)
await this._openRoot()
await this.config.set(buildConfig(config))
await this.spec.set(buildDatastoreSpec(config))
await this.version.set(constants.repoVersion)
}
/**
* Check if the repo is already initialized.
* @returns {Promise<Boolean>}
*/
async isInitialized () {
if (!this.closed) {
// repo is open, must be initialized
return true
}
try {
// have to open the root datastore in the browser before
// we can check whether it's been initialized
await this._openRoot()
await this._checkInitialized()
await this.root.close()
return true
} catch (err) {
// FIXME: do not use exceptions for flow control
return false
}
}
/**
* Open the repo. If the repo is already open an error will be thrown.
* If the repo is not initialized it will throw an error.
*
* @returns {Promise<void>}
*/
async open () {
if (!this.closed) {
throw errcode(new Error('repo is already open'), ERRORS.ERR_REPO_ALREADY_OPEN)
}
log('opening at: %s', this.path)
// check if the repo is already initialized
try {
await this._openRoot()
await this._checkInitialized()
this.lockfile = await this._openLock(this.path)
log('acquired repo.lock')
log('creating datastore')
this.datastore = backends.create('datastore', pathJoin(this.path, 'datastore'), this.options)
await this.datastore.open()
log('creating blocks')
const blocksBaseStore = backends.create('blocks', pathJoin(this.path, 'blocks'), this.options)
await blocksBaseStore.open()
this.blocks = await blockstore(blocksBaseStore, this.options.storageBackendOptions.blocks)
log('creating keystore')
this.keys = backends.create('keys', pathJoin(this.path, 'keys'), this.options)
await this.keys.open()
log('creating pins')
this.pins = backends.create('pins', pathJoin(this.path, 'pins'), this.options)
await this.pins.open()
const isCompatible = await this.version.check(constants.repoVersion)
if (!isCompatible) {
if (await this._isAutoMigrationEnabled()) {
await this._migrate(constants.repoVersion)
} else {
throw new ERRORS.InvalidRepoVersionError('Incompatible repo versions. Automatic migrations disabled. Please migrate the repo manually.')
}
}
this.closed = false
log('all opened')
} catch (err) {
if (this.lockfile) {
try {
await this._closeLock()
this.lockfile = null
} catch (err2) {
log('error removing lock', err2)
}
}
throw err
}
}
/**
* Returns the repo locker to be used. Null will be returned if no locker is requested
*
* @private
* @returns {Locker}
*/
_getLocker () {
if (typeof this.options.lock === 'string') {
if (!lockers[this.options.lock]) {
throw new Error('Unknown lock type: ' + this.options.lock)
}
return lockers[this.options.lock]
}
if (!this.options.lock) {
throw new Error('No lock provided')
}
return this.options.lock
}
/**
* Opens the root backend, catching and ignoring an 'Already open' error
* @returns {Promise}
*/
async _openRoot () {
try {
await this.root.open()
} catch (err) {
if (err.message !== 'Already open') {
throw err
}
}
}
/**
* Creates a lock on the repo if a locker is specified. The lockfile object will
* be returned in the callback if one has been created.
*
* @param {String} path
* @returns {Promise<lockfile>}
*/
async _openLock (path) {
const lockfile = await this._locker.lock(path)
if (typeof lockfile.close !== 'function') {
throw errcode(new Error('Locks must have a close method'), 'ERR_NO_CLOSE_FUNCTION')
}
return lockfile
}
/**
* Closes the lock on the repo
*
* @returns {Promise<void>}
*/
_closeLock () {
return this.lockfile.close()
}
/**
* Check if the repo is already initialized.
* @private
* @returns {Promise}
*/
async _checkInitialized () {
log('init check')
let config
try {
[config] = await Promise.all([
this.config.exists(),
this.spec.exists(),
this.version.exists()
])
} catch (err) {
if (err.code === 'ERR_NOT_FOUND') {
throw errcode(new Error('repo is not initialized yet'), ERRORS.ERR_REPO_NOT_INITIALIZED, {
path: this.path
})
}
throw err
}
if (!config) {
throw errcode(new Error('repo is not initialized yet'), ERRORS.ERR_REPO_NOT_INITIALIZED, {
path: this.path
})
}
}
/**
* Close the repo and cleanup.
*
* @returns {Promise<void>}
*/
async close () {
if (this.closed) {
throw errcode(new Error('repo is already closed'), ERRORS.ERR_REPO_ALREADY_CLOSED)
}
log('closing at: %s', this.path)
try {
// Delete api, ignoring irrelevant errors
await this.apiAddr.delete()
} catch (err) {
if (err.code !== ERRORS.ERR_REPO_NOT_INITIALIZED && !err.message.startsWith('ENOENT')) {
throw err
}
}
await Promise.all([
this.root,
this.blocks,
this.keys,
this.datastore,
this.pins
].map((store) => store.close()))
log('unlocking')
this.closed = true
await this._closeLock()
this.lockfile = null
}
/**
* Check if a repo exists.
*
* @returns {Promise<bool>}
*/
async exists () { // eslint-disable-line require-await
return this.version.exists()
}
/**
* Get repo status.
*
* @returns {Object}
*/
async stat () {
const [storageMax, blocks, version, datastore, keys] = await Promise.all([
this._storageMaxStat(),
this._blockStat(),
this.version.get(),
getSize(this.datastore),
getSize(this.keys)
])
const size = blocks.size
.plus(datastore)
.plus(keys)
return {
repoPath: this.path,
storageMax,
version: version,
numObjects: blocks.count,
repoSize: size
}
}
async _isAutoMigrationEnabled () {
if (this.options.autoMigrate !== undefined) {
return this.options.autoMigrate
}
let autoMigrateConfig
try {
autoMigrateConfig = await this.config.get(AUTO_MIGRATE_CONFIG_KEY)
} catch (e) {
if (e.code === ERRORS.NotFoundError.code) {
autoMigrateConfig = true // Config's default value is True
} else {
throw e
}
}
return autoMigrateConfig
}
async _migrate (toVersion) {
const currentRepoVersion = await this.version.get()
if (currentRepoVersion > toVersion) {
log('reverting to version ' + toVersion)
return migrator.revert(this.path, toVersion, { ignoreLock: true, repoOptions: this.options })
} else {
log('migrating to version ' + toVersion)
return migrator.migrate(this.path, toVersion, { ignoreLock: true, repoOptions: this.options })
}
}
async _storageMaxStat () {
try {
const max = await this.config.get('Datastore.StorageMax')
return new Big(bytes(max))
} catch (err) {
return new Big(noLimit)
}
}
async _blockStat () {
let count = new Big(0)
let size = new Big(0)
for await (const block of this.blocks.query({})) {
count = count.plus(1)
size = size
.plus(block.data.byteLength)
.plus(block.cid.buffer.byteLength)
}
return { count, size }
}
}
async function getSize (queryFn) {
const sum = new Big(0)
for await (const block of queryFn.query({})) {
sum.plus(block.value.byteLength)
.plus(block.key.toBuffer().byteLength)
}
return sum
}
module.exports = IpfsRepo
module.exports.utils = { blockstore: require('./blockstore-utils') }
module.exports.repoVersion = constants.repoVersion
module.exports.errors = ERRORS
function buildOptions (_options) {
const options = Object.assign({}, defaultOptions, _options)
options.storageBackends = Object.assign(
{},
defaultOptions.storageBackends,
options.storageBackends)
options.storageBackendOptions = Object.assign(
{},
defaultOptions.storageBackendOptions,
options.storageBackendOptions)
return options
}
// TODO this should come from js-ipfs instead
function buildConfig (_config) {
_config.datastore = Object.assign({}, defaultDatastore, _get(_config, 'datastore', {}))
return _config
}
function buildDatastoreSpec (_config) {
const spec = Object.assign({}, defaultDatastore.Spec, _get(_config, 'datastore.Spec', {}))
return {
type: spec.type,
mounts: spec.mounts.map((mounting) => ({
mountpoint: mounting.mountpoint,
type: mounting.child.type,
path: mounting.child.path,
shardFunc: mounting.child.shardFunc
}))
}
}