Skip to content

Commit

Permalink
Use RAM.reusable() as default storage to support reopening cores
Browse files Browse the repository at this point in the history
Some libraries (such as `autobase`) will close their cores assuming they
persist. To support this while the program is still running,
`random-access-memory` supports `RAM.reusable()` which returns a
reusable version of the RAM storage.
  • Loading branch information
lejeunerenard committed Mar 27, 2024
1 parent f30bd08 commit 5e33e21
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export async function create ({
if (isStringStorage && !isPathStorage) {
storageBackend = RAA(storage)
} else if (storage === false) {
storageBackend = RAM
storageBackend = RAM.reusable()
}

const corestore = opts.corestore || new CoreStore(storageBackend, { ...corestoreOpts })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"hyperdrive": "^11.8.1",
"hyperswarm": "^4.7.14",
"random-access-application": "^2.0.0",
"random-access-memory": "^6.1.0",
"random-access-memory": "^6.2.0",
"z32": "^1.0.1"
},
"devDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ test('Specify storage for sdk', async (t) => {
}, { unsafeCleanup: true })
})

test('Support storage reuse by default', async (t) => {
const sdk = await create({ storage: false })
const core = await sdk.get('persist in memory')
const key = core.key

const data = b4a.from('beep')
await core.append(data)
await core.close()
t.ok(core.closed, 'initial core was closed')

const coreAgain = await sdk.get(key)
t.deepEqual(await coreAgain.get(0, { wait: false }), data, 'found persisted data')

await sdk.close()
})

test('Load hypercores by names and urls', async (t) => {
const sdk = await create({ storage: false })
const name = 'example'
Expand Down

0 comments on commit 5e33e21

Please sign in to comment.