Skip to content

Commit

Permalink
Remove isMemory util.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Mar 31, 2024
1 parent f9aa449 commit 4c62236
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 33 deletions.
8 changes: 4 additions & 4 deletions packages/daemon/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import { EntryTracker } from './entry-tracker.js'
import { createGroups } from './groups.js'
import handleCommands from './handle-commands.js'
import handleEvents from './handle-events.js'
import { Config, type Components } from './interface.js'
import { Config, MEMORY_MAGIC, type Components } from './interface.js'
import createLibp2p from './libp2p.js'
import { PinManager } from './pin-manager/index.js'
import { Sneakernet } from './sneakernet/index.js'
import { createTick } from './tick.js'
import type { KeyvalueDB } from '@/interface.js'
import { createLogger } from '@/logger.js'
import { isMemory, extendDatastore } from '@/utils.js'
import { extendDatastore } from '@/utils.js'

interface Setup {
socket: string
Expand All @@ -45,11 +45,11 @@ export default async (settings: Partial<Setup> = {}): Promise<Components> => {
const config = parseConfig(Config)
const events = new EventTarget()

const datastore = isMemory(config.storage)
const datastore = config.storage === MEMORY_MAGIC
? new MemoryDatastore()
: new FsDatastore(Path.join(config.storage, 'datastore'))

const blockstore = isMemory(config.storage)
const blockstore = config.storage === MEMORY_MAGIC
? new MemoryBlockstore()
: new FsBlockstore(Path.join(config.storage, 'blockstore'))

Expand Down
4 changes: 3 additions & 1 deletion packages/daemon/src/common/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { Welo } from 'welo'

export const MEMORY_MAGIC = ':memory:'

export const Config = z.object({
storage: z.string().default(':memory:'),
storage: z.string().default(MEMORY_MAGIC),
slots: z.number().int().min(1).max(100).default(20),
tickInterval: z.number().default(10 * 60),
serverMode: z.boolean().default(false),
Expand Down
2 changes: 0 additions & 2 deletions packages/daemon/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export interface KeyvalueDB extends Database {
store: Keyvalue
}

export const MEMORY_MAGIC = ':memory:'

export interface ModuleMethod<
Context extends Record<string, unknown> = Record<string, unknown>
> {
Expand Down
3 changes: 0 additions & 3 deletions packages/daemon/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { NamespaceDatastore } from 'datastore-core'
import { type Datastore, Key } from 'interface-datastore'
import { MEMORY_MAGIC } from './interface.js'

export const isMemory = (storage?: string): boolean => storage === MEMORY_MAGIC

export const extendDatastore = (datastore: Datastore, name: string): NamespaceDatastore => new NamespaceDatastore(datastore, new Key(name))
23 changes: 0 additions & 23 deletions packages/daemon/test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,9 @@ import assert from 'assert/strict'
import { MemoryBlockstore } from 'blockstore-core'
import * as cborg from 'cborg'
import { type CID } from 'multiformats/cid'
import { MEMORY_MAGIC } from '../src/interface.js'
import { isMemory } from '../src/utils.js'
import { createDag } from './utils/dag.js'
import { walkDag, getDagSize } from '@/modules/filesystem/utils.js'

describe('isMemory', () => {
it('returns true if the memory magic is passed', () => {
assert(isMemory(MEMORY_MAGIC))
})

it('returns false for any other value than the memory magic', () => {
const values = [
'/my/path',
'my-directory',
':memory',
'memory',
'memory:',
':memory:/my-dir'
]

for (const value of values) {
assert(!isMemory(value))
}
})
})

describe('cbor encoding and decoding', () => {
const data = [
{
Expand Down

0 comments on commit 4c62236

Please sign in to comment.