// The store package owns the Holepunch substrate — so the corestore dependency resolves
// here (store/node_modules), and higher layers (pds) compose store abstractions without
// reaching for corestore directly.
import Corestore from 'corestore'
// A dir path (or an existing Corestore) -> a Corestore. Idempotent on an instance.
export function openCorestore (storage) {
return isCorestore(storage) ? storage : new Corestore(storage)
}
// ⛔ NOT `instanceof`. That compares against ONE module instance's class object, and a nested
// node_modules tree routinely yields two copies of corestore — which is what bare-pack ships to the
// phone (…/hyper-proto-peer/node_modules/atproto-room/node_modules/atproto-store/node_modules/corestore
// alongside the daemon's own). A store built by copy A then fails `instanceof` against copy B, and every
// discriminator here reads that as "not a store, therefore a PATH", so Corestore tried to path.join an
// OBJECT and the node died with `Cannot read properties of undefined (reading 'length')` — a message
// that names a JS idiom and no cause (hyper-social #40).
//
// It cannot reproduce on a workstation, where the tree is deduped to one copy. The environments differ
// in their module graph, not their code, so identity is the wrong question: a store is what it DOES.
// `get` + `namespace` + `replicate` are the surface every caller here uses and nothing else implements.
// The instanceof check stays as a fast path for the common single-copy case.
export function isCorestore (x) {
if (!x || typeof x !== 'object') return false
if (x instanceof Corestore) return true
return typeof x.get === 'function' && typeof x.namespace === 'function' && typeof x.replicate === 'function'
}
export { Corestore }