Create a store that names each artifact by the hash of its framed bytes.
Content addressing gives three properties a plain blob key cannot. Writing the
same artifact twice stores one copy, because identical bytes produce one
address. Reading an address cannot return different content later without
failing, because the bytes are re-hashed before decoding. And an address is
portable across processes and machines that share the hash function, which is
why the hash is injected rather than chosen here.
A hash collision — two artifacts, one address — is detected rather than
served: add compares the stored bytes with the ones it was about to write,
and reports ADDRESS_COLLISION instead of returning an address whose content
is the other artifact. That check costs one read on the duplicate path, which
is the price of not depending on the hash being collision-free.
@param ― options - The backend, the hash, and the artifact family's magic.
@returns ― A store whose every failure carries a code from CAS_ERROR_CODES.
@throws ― {CasInvalidInputError} With INVALID_ARTIFACT when options.magic is
not four bytes, since no artifact of this family could ever be valid.
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Create a store that names each artifact by the hash of its framed bytes.
Content addressing gives three properties a plain blob key cannot. Writing the
same artifact twice stores one copy, because identical bytes produce one
address. Reading an address cannot return different content later without
failing, because the bytes are re-hashed before decoding. And an address is
portable across processes and machines that share the hash function, which is
why the hash is injected rather than chosen here.
A hash collision — two artifacts, one address — is detected rather than
served: add compares the stored bytes with the ones it was about to write,
and reports ADDRESS_COLLISION instead of returning an address whose content
is the other artifact. That check costs one read on the duplicate path, which
is the price of not depending on the hash being collision-free.
@param ― options - The backend, the hash, and the artifact family's magic.
@returns ― A store whose every failure carries a code from CAS_ERROR_CODES.
@throws ― {CasInvalidInputError} With INVALID_ARTIFACT when options.magic is
not four bytes, since no artifact of this family could ever be valid.
contentAddressedStore({
// Any object with has/get/set works: a Map, S3 client, or fetch-backed store.
Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
@returns ― Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.
Store bytes under address, replacing whatever was there.
Content addressing makes a rewrite idempotent when the hash is
deterministic, so a backend need not be idempotent itself: the store writes
an address only when it read nothing back, or read back different bytes
that it has already reported as a failure.
Content hash from framed bytes to address. Injected, so a caller chooses the
algorithm and its width, and so a test can use a hash that cannot collide.
It must be deterministic — two calls on equal bytes return one string — and
collision-resistant enough for the store's expected population. A 64-bit
non-cryptographic hash is adequate below roughly 2^32 artifacts; above that,
or against an adversary choosing the inputs, use SHA-256. A collision is not
silent: see
The four-byte family marker this store accepts and writes. One store
addresses one family of artifacts, so a knotkit/heap artifact read through
a knotkit/trie store fails as INVALID_MAGIC rather than decoding under
the wrong package's rules.
magic:
var Uint8Array:Uint8ArrayConstructor
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
@param ― items A set of elements to include in the new array object.
of(0x4b, 0x4b, 0x48, 0x50),
})
const
constartifact: {
payloadVersion:number;
packageHeader:Uint8Array<ArrayBuffer>;
sections: {
tag:number;
bytes:Uint8Array<ArrayBuffer>;
}[];
}
artifact= {
payloadVersion: number
payloadVersion: 1,
packageHeader: Uint8Array<ArrayBuffer>
packageHeader:
var Uint8Array:Uint8ArrayConstructor
A typed array of 8-bit unsigned integer values. The contents are initialized to 0. If the
requested number of bytes could not be allocated an exception is raised.
Frame the artifact, then store it under its address unless the same bytes
are already there. Returns the address. O(bytes).
@throws ― {CasInvalidInputError} As addressOf, plus BACKEND_FAILURE
when the backend throws.
@throws ― {CasParseError} With ADDRESS_COLLISION when the address is present
but holds different bytes, which means the injected hash mapped two
artifacts to one address.
add(
constartifact: {
payloadVersion:number;
packageHeader:Uint8Array<ArrayBuffer>;
sections: {
tag:number;
bytes:Uint8Array<ArrayBuffer>;
}[];
}
artifact)
constaddress:string
address// a 16-character hex address for these exact bytes
Frame the artifact, then store it under its address unless the same bytes
are already there. Returns the address. O(bytes).
@throws ― {CasInvalidInputError} As addressOf, plus BACKEND_FAILURE
when the backend throws.
@throws ― {CasParseError} With ADDRESS_COLLISION when the address is present
but holds different bytes, which means the injected hash mapped two
artifacts to one address.
add(
constartifact: {
payloadVersion:number;
packageHeader:Uint8Array<ArrayBuffer>;
sections: {
tag:number;
bytes:Uint8Array<ArrayBuffer>;
}[];
}
artifact) // one stored copy: the address is already present
Read and verify the artifact stored under address, or undefined when the
address is absent. O(bytes).
The bytes are re-hashed before decoding, so a backend that returned the
wrong content, or a hash that stopped being deterministic, fails as
ADDRESS_MISMATCH rather than as a payload that happens to decode.
@throws ― {CasInvalidInputError} With INVALID_ADDRESS when the address is
not a safe storage key, or BACKEND_FAILURE when the backend throws.
@throws ― {CasParseError} With ADDRESS_MISMATCH when the stored bytes do not
hash to their address, or with any knotkit/wire failure the envelope
raises under this store's magic.
Every error this module throws is CasInvalidInputError or CasParseError, with a code from a closed, exported list. Branch on the code; the message is for people.
Use knotkit/cas for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/cas'.
Exports:
- `contentAddressedStore` → `ContentAddressedStore`: Bind a backend, a deterministic content hash, and one family magic into a deduplicating, verifying store.
Inputs and their sources:
- content hash from framed bytes to address: `(bytes: Uint8Array) => string` from `knotkit/hash or crypto.subtle`
- durable storage: `ArtifactBackend` from `the caller's backend`
Use something else when:
- persist a structure to local disk in one process → the structure package's serialize function and the platform file API
- an artifact that must be readable without the hash that produced its key → a caller-chosen storage key and an envelope from knotkit/wire
- cryptographic collision resistance or signed artifacts → crypto.subtle with SHA-256, or signature verification over the envelope bytes
- a cache with eviction, expiry, or least-recently-used ordering → knotkit/cache
- mutable blobs whose key stays stable as content changes → a plain key-value backend; a content address changes with the content
Errors are CasInvalidInputError or CasParseError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/cas.md
Types: node_modules/knotkit/cas/ and https://knotkit.jkomyno.dev/reference/knotkit/cas/