Lines 1-29javascript
1import { createHash } from 'node:crypto';
2import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3import { dirname } from 'node:path';
4import { ChecksumError, DimensionMismatchError, SnapshotTargetExistsError, SnapshotVersionError } from './errors.js';
5export class SnapshotImporter {
8 this.options = options;
10 async import(snapshotPath, targetDbPath, opts = {}) {
11 const { header, dbBytes } = this.readSnapshot(snapshotPath);
MediumDynamic Require
Package source references dynamic require/import behavior.
dist/snapshot/SnapshotImporter.jsView on unpkg · L9 12 if (header.version !== 1)
13 throw new SnapshotVersionError(header.version);
14 if (header.embeddingDimension !== this.options.expectedEmbeddingDimension) {
15 throw new DimensionMismatchError(this.options.expectedEmbeddingDimension, header.embeddingDimension);
17 const checksum = createHash('sha256').update(dbBytes).digest('hex');
18 if (checksum !== header.checksum)
19 throw new ChecksumError();
21 return { header, targetPath: targetDbPath, skipped: true };
23 if (existsSync(targetDbPath) && opts.overwrite !== true) {
24 throw new SnapshotTargetExistsError(targetDbPath);
26 mkdirSync(dirname(targetDbPath), { recursive: true });
27 writeFileSync(targetDbPath, dbBytes);
28 return { header, targetPath: targetDbPath, skipped: false };