From 4c622362936524228a717c39b7284ab5fc1fa380 Mon Sep 17 00:00:00 2001 From: saul Date: Mon, 1 Apr 2024 10:08:48 +1300 Subject: [PATCH] Remove isMemory util. --- packages/daemon/src/common/index.ts | 8 ++++---- packages/daemon/src/common/interface.ts | 4 +++- packages/daemon/src/interface.ts | 2 -- packages/daemon/src/utils.ts | 3 --- packages/daemon/test/utils.spec.ts | 23 ----------------------- 5 files changed, 7 insertions(+), 33 deletions(-) diff --git a/packages/daemon/src/common/index.ts b/packages/daemon/src/common/index.ts index 843bacec..abc795da 100644 --- a/packages/daemon/src/common/index.ts +++ b/packages/daemon/src/common/index.ts @@ -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 @@ -45,11 +45,11 @@ export default async (settings: Partial = {}): Promise => { 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')) diff --git a/packages/daemon/src/common/interface.ts b/packages/daemon/src/common/interface.ts index f14bc9fb..2e223c49 100644 --- a/packages/daemon/src/common/interface.ts +++ b/packages/daemon/src/common/interface.ts @@ -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), diff --git a/packages/daemon/src/interface.ts b/packages/daemon/src/interface.ts index bdcea6af..fb5723d4 100644 --- a/packages/daemon/src/interface.ts +++ b/packages/daemon/src/interface.ts @@ -5,8 +5,6 @@ export interface KeyvalueDB extends Database { store: Keyvalue } -export const MEMORY_MAGIC = ':memory:' - export interface ModuleMethod< Context extends Record = Record > { diff --git a/packages/daemon/src/utils.ts b/packages/daemon/src/utils.ts index 2a9a92dd..a9f236cf 100644 --- a/packages/daemon/src/utils.ts +++ b/packages/daemon/src/utils.ts @@ -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)) diff --git a/packages/daemon/test/utils.spec.ts b/packages/daemon/test/utils.spec.ts index 46292fdb..11b509d8 100644 --- a/packages/daemon/test/utils.spec.ts +++ b/packages/daemon/test/utils.spec.ts @@ -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 = [ {