mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-05-22 14:56:38 +00:00
Fix ES5 build
This commit is contained in:
parent
2682d3ba42
commit
de057e7ec6
93
dist/es5/OrbitDB.js
vendored
93
dist/es5/OrbitDB.js
vendored
@ -37,6 +37,7 @@ var Cache = require('orbit-db-cache');
|
|||||||
var Keystore = require('orbit-db-keystore');
|
var Keystore = require('orbit-db-keystore');
|
||||||
var AccessController = require('./ipfs-access-controller');
|
var AccessController = require('./ipfs-access-controller');
|
||||||
var OrbitDBAddress = require('./orbit-db-address');
|
var OrbitDBAddress = require('./orbit-db-address');
|
||||||
|
var createDBManifest = require('./db-manifest');
|
||||||
|
|
||||||
var Logger = require('logplease');
|
var Logger = require('logplease');
|
||||||
var logger = Logger.create("orbit-db");
|
var logger = Logger.create("orbit-db");
|
||||||
@ -297,44 +298,23 @@ var OrbitDB = function () {
|
|||||||
// Save the Access Controller in IPFS
|
// Save the Access Controller in IPFS
|
||||||
var accessControllerAddress = await accessController.save
|
var accessControllerAddress = await accessController.save
|
||||||
|
|
||||||
// Creates a DB manifest file
|
|
||||||
();var createDBManifest = function createDBManifest() {
|
|
||||||
return {
|
|
||||||
name: name,
|
|
||||||
type: type,
|
|
||||||
accessController: path.join('/ipfs', accessControllerAddress)
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Save the manifest to IPFS
|
// Save the manifest to IPFS
|
||||||
var manifest = createDBManifest();
|
();var manifestHash = await createDBManifest(this._ipfs, name, type, accessControllerAddress
|
||||||
var dag = await this._ipfs.object.put(new Buffer((0, _stringify2.default)(manifest)));
|
|
||||||
var manifestHash = dag.toJSON().multihash.toString
|
|
||||||
|
|
||||||
// Create the database address
|
// Create the database address
|
||||||
();var address = path.join('/orbitdb', manifestHash, name);
|
);var dbAddress = OrbitDBAddress.parse(path.join('/orbitdb', manifestHash, name)
|
||||||
var dbAddress = OrbitDBAddress.parse(address
|
|
||||||
|
|
||||||
// Load local cache
|
// // Load local cache
|
||||||
);try {
|
);var haveManifest = await this._loadCache(directory, dbAddress).then(function (cache) {
|
||||||
var cacheFilePath = path.join(dbAddress.root, dbAddress.path);
|
return cache.get(dbAddress.toString());
|
||||||
this._cache = new Cache(path.join(directory), cacheFilePath);
|
}).then(function (localData) {
|
||||||
await this._cache.load();
|
return localData && localData.manifest;
|
||||||
} catch (e) {
|
});
|
||||||
logger.warn("Couldn't load Cache:", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we already have the database
|
if (haveManifest && !options.overwrite) throw new Error('Database \'' + dbAddress + '\' already exists!');
|
||||||
var localData = await this._cache.get(dbAddress.toString());
|
|
||||||
|
|
||||||
if (localData && localData.manifest && !options.overwrite) throw new Error('Database \'' + dbAddress + '\' already exists!');
|
|
||||||
|
|
||||||
// Save the database locally
|
// Save the database locally
|
||||||
localData = (0, _assign2.default)({}, this._cache.get(dbAddress.toString()), {
|
await this._saveDBManifest(directory, dbAddress);
|
||||||
manifest: dbAddress.root
|
|
||||||
});
|
|
||||||
await this._cache.set(dbAddress.toString(), localData);
|
|
||||||
logger.debug('Saved manifest to IPFS as \'' + dbAddress.root + '\'');
|
|
||||||
|
|
||||||
logger.debug('Created database \'' + dbAddress + '\''
|
logger.debug('Created database \'' + dbAddress + '\''
|
||||||
|
|
||||||
@ -379,19 +359,13 @@ var OrbitDB = function () {
|
|||||||
// Parse the database address
|
// Parse the database address
|
||||||
var dbAddress = OrbitDBAddress.parse(address
|
var dbAddress = OrbitDBAddress.parse(address
|
||||||
|
|
||||||
// Load local cache
|
|
||||||
);try {
|
|
||||||
var cacheFilePath = path.join(dbAddress.root, dbAddress.path);
|
|
||||||
this._cache = new Cache(path.join(directory), cacheFilePath);
|
|
||||||
await this._cache.load();
|
|
||||||
} catch (e) {
|
|
||||||
console.warn(e);
|
|
||||||
logger.warn("Couldn't load Cache:", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have the database
|
// Check if we have the database
|
||||||
var localData = await this._cache.get(dbAddress.toString());
|
);var haveDB = await this._loadCache(directory, dbAddress).then(function (cache) {
|
||||||
var haveDB = localData && localData.manifest;
|
return cache.get(dbAddress.toString());
|
||||||
|
}).then(function (localData) {
|
||||||
|
return localData && localData.manifest;
|
||||||
|
});
|
||||||
|
|
||||||
logger.debug((haveDB ? 'Found' : 'Didn\'t find') + (' database \'' + dbAddress + '\'')
|
logger.debug((haveDB ? 'Found' : 'Didn\'t find') + (' database \'' + dbAddress + '\'')
|
||||||
|
|
||||||
// If we want to try and open the database local-only, throw an error
|
// If we want to try and open the database local-only, throw an error
|
||||||
@ -412,16 +386,39 @@ var OrbitDB = function () {
|
|||||||
);if (options.type && manifest.type !== options.type) throw new Error('Database \'' + dbAddress + '\' is type \'' + manifest.type + '\' but was opened as \'' + options.type + '\'');
|
);if (options.type && manifest.type !== options.type) throw new Error('Database \'' + dbAddress + '\' is type \'' + manifest.type + '\' but was opened as \'' + options.type + '\'');
|
||||||
|
|
||||||
// Save the database locally
|
// Save the database locally
|
||||||
localData = (0, _assign2.default)({}, this._cache.get(dbAddress.toString()), {
|
await this._saveDBManifest(directory, dbAddress
|
||||||
manifest: dbAddress.root
|
|
||||||
});
|
|
||||||
await this._cache.set(dbAddress.toString(), localData);
|
|
||||||
logger.debug('Saved manifest to IPFS as \'' + dbAddress.root + '\''
|
|
||||||
|
|
||||||
// Open the the database
|
// Open the the database
|
||||||
);options = (0, _assign2.default)({}, options, { accessControllerAddress: manifest.accessController });
|
);options = (0, _assign2.default)({}, options, { accessControllerAddress: manifest.accessController });
|
||||||
return this._openDatabase(dbAddress, manifest.type, options);
|
return this._openDatabase(dbAddress, manifest.type, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the database locally
|
||||||
|
|
||||||
|
}, {
|
||||||
|
key: '_saveDBManifest',
|
||||||
|
value: async function _saveDBManifest(directory, dbAddress) {
|
||||||
|
var cache = await this._loadCache(directory, dbAddress);
|
||||||
|
var localData = (0, _assign2.default)({}, cache.get(dbAddress.toString()), {
|
||||||
|
manifest: dbAddress.root
|
||||||
|
});
|
||||||
|
await cache.set(dbAddress.toString(), localData);
|
||||||
|
logger.debug('Saved manifest to IPFS as \'' + dbAddress.root + '\'');
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
key: '_loadCache',
|
||||||
|
value: async function _loadCache(directory, dbAddress) {
|
||||||
|
var cache = void 0;
|
||||||
|
try {
|
||||||
|
var cacheFilePath = path.join(dbAddress.root, dbAddress.path);
|
||||||
|
cache = new Cache(path.join(directory), cacheFilePath);
|
||||||
|
await cache.load();
|
||||||
|
} catch (e) {
|
||||||
|
logger.warn("Couldn't load Cache:", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
key: '_openDatabase',
|
key: '_openDatabase',
|
||||||
value: async function _openDatabase(address, type, options) {
|
value: async function _openDatabase(address, type, options) {
|
||||||
|
22
dist/es5/db-manifest.js
vendored
Normal file
22
dist/es5/db-manifest.js
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var _stringify = require('babel-runtime/core-js/json/stringify');
|
||||||
|
|
||||||
|
var _stringify2 = _interopRequireDefault(_stringify);
|
||||||
|
|
||||||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||||
|
|
||||||
|
var path = require('path'
|
||||||
|
|
||||||
|
// Creates a DB manifest file and saves it in IPFS
|
||||||
|
);var createDBManifest = async function createDBManifest(ipfs, name, type, accessControllerAddress) {
|
||||||
|
var manifest = {
|
||||||
|
name: name,
|
||||||
|
type: type,
|
||||||
|
accessController: path.join('/ipfs', accessControllerAddress)
|
||||||
|
};
|
||||||
|
var dag = await ipfs.object.put(Buffer.from((0, _stringify2.default)(manifest)));
|
||||||
|
return dag.toJSON().multihash.toString();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = createDBManifest;
|
Loading…
x
Reference in New Issue
Block a user