Skip to content

Commit

Permalink
allow passing userdata for atomic setup (#34)
Browse files Browse the repository at this point in the history
mafintosh authored Nov 8, 2024
1 parent 0b6d983 commit edc36f2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -358,7 +358,7 @@ module.exports = class CoreStorage {
return new HypercoreStorage(this, discoveryKey, core, data, null)
}

async create ({ key, manifest, keyPair, encryptionKey, discoveryKey }) {
async create ({ key, manifest, keyPair, encryptionKey, discoveryKey, userData }) {
await this.mutex.write.lock()

try {
@@ -390,7 +390,7 @@ module.exports = class CoreStorage {
const batch = new WriteBatch(storage, write)

initialiseCoreInfo(batch, { key, manifest, keyPair, encryptionKey })
initialiseCoreData(batch)
initialiseCoreData(batch, { userData })

await batch.flush()
return storage
@@ -745,6 +745,11 @@ function initialiseCoreInfo (db, { key, manifest, keyPair, encryptionKey }) {
if (encryptionKey) db.setEncryptionKey(encryptionKey)
}

function initialiseCoreData (db) {
function initialiseCoreData (db, { userData } = {}) {
db.setDataInfo({ version: 0 })
if (userData) {
for (const { key, value } of userData) {
db.setUserData(key, value)
}
}
}

0 comments on commit edc36f2

Please sign in to comment.