blockstore-core
Version:
Contains various implementations of the API contract described in interface-blockstore
79 lines • 3.17 kB
JavaScript
import { NotFoundError } from 'interface-store';
import { BaseBlockstore } from "./base.js";
// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2
const IDENTITY_CODEC = 0x00;
class IdentityHashDigestTooLongError extends Error {
static name = 'IdentityHashDigestTooLongError';
name = 'IdentityHashDigestTooLongError';
}
export class IdentityBlockstore extends BaseBlockstore {
child;
maxDigestLength;
constructor(child, init) {
super();
this.child = child;
this.maxDigestLength = init?.maxDigestLength;
}
put(key, block, options) {
if (key.multihash.code === IDENTITY_CODEC) {
if (this.maxDigestLength != null && key.multihash.digest.byteLength > this.maxDigestLength) {
throw new IdentityHashDigestTooLongError(`Identity digest too long - ${key.multihash.digest.byteLength} > this.maxDigestLength`);
}
options?.signal?.throwIfAborted();
return key;
}
if (this.child == null) {
options?.signal?.throwIfAborted();
return key;
}
return this.child.put(key, block, options);
}
async *get(key, options) {
if (key.multihash.code === IDENTITY_CODEC) {
if (this.maxDigestLength != null && key.multihash.digest.byteLength > this.maxDigestLength) {
throw new IdentityHashDigestTooLongError(`Identity digest too long - ${key.multihash.digest.byteLength} > this.maxDigestLength`);
}
options?.signal?.throwIfAborted();
yield key.multihash.digest;
return;
}
if (this.child == null) {
options?.signal?.throwIfAborted();
throw new NotFoundError();
}
yield* this.child.get(key, options);
}
has(key, options) {
if (key.multihash.code === IDENTITY_CODEC) {
if (this.maxDigestLength != null && key.multihash.digest.byteLength > this.maxDigestLength) {
throw new IdentityHashDigestTooLongError(`Identity digest too long - ${key.multihash.digest.byteLength} > this.maxDigestLength`);
}
options?.signal?.throwIfAborted();
return true;
}
if (this.child == null) {
options?.signal?.throwIfAborted();
return false;
}
return this.child.has(key, options);
}
delete(key, options) {
if (key.code === IDENTITY_CODEC) {
if (this.maxDigestLength != null && key.multihash.digest.byteLength > this.maxDigestLength) {
throw new IdentityHashDigestTooLongError(`Identity digest too long - ${key.multihash.digest.byteLength} > this.maxDigestLength`);
}
options?.signal?.throwIfAborted();
return;
}
if (this.child != null) {
return this.child.delete(key, options);
}
}
async *getAll(options) {
if (this.child != null) {
yield* this.child.getAll(options);
}
options?.signal?.throwIfAborted();
}
}
//# sourceMappingURL=identity.js.map