Skip to content

Commit

Permalink
fix: extract util function to compress object
Browse files Browse the repository at this point in the history
  • Loading branch information
nadilas committed Jun 10, 2024
1 parent b9fd802 commit cd68d7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
6 changes: 2 additions & 4 deletions packages/ogre/src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from "fast-json-patch";
import { calculateCommitHash, Commit } from "./commit.js";
import { History, Reference } from "./interfaces.js";
import { compressSync, strToU8 } from "fflate";
import {
brancheNameToRef,
cleanRefValue,
Expand All @@ -26,6 +25,7 @@ import {
localHeadPathPrefix,
mapPath,
mutableMapCopy,
objectToTree,
REFS_HEAD_KEY,
REFS_MAIN_KEY,
refsAtCommit,
Expand Down Expand Up @@ -429,9 +429,7 @@ export class Repository<T extends { [k: PropertyKey]: any }>
timestamp,
});

const treeHash = Buffer.from(
compressSync(strToU8(JSON.stringify(this.data)), { level: 6, mem: 8 }),
).toString("base64");
const treeHash = objectToTree(this.data);
const commit = {
hash: sha,
message,
Expand Down
11 changes: 9 additions & 2 deletions packages/ogre/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// [RFC5322](https://www.ietf.org/rfc/rfc5322.txt)
import { Commit } from "./commit.js";
import { Reference } from "./interfaces.js";
import { decompressSync, strFromU8 } from "fflate";
import { compressSync, decompressSync, strFromU8, strToU8 } from "fflate";
import { validBranch, validRef } from "./ref.js";
import { deepClone, Operation } from "fast-json-patch";
import { RepositoryObject } from "./repository.js";
Expand Down Expand Up @@ -51,10 +51,16 @@ export const REFS_HEAD_KEY = "HEAD";
* Should only be used in local context
*/
export const REFS_MAIN_KEY = `${localHeadPathPrefix()}main`;

export function objectToTree(obj: any) {
return Buffer.from(
compressSync(strToU8(JSON.stringify(obj)), { level: 6, mem: 8 }),
).toString("base64");
}

export const treeToObject = <T = any>(tree: string): T => {
return JSON.parse(strFromU8(decompressSync(Buffer.from(tree, "base64"))));
};

/**
* Maps the path from a commit to another commit.
* It travels backwards through parent relationships until the root state.
Expand Down Expand Up @@ -235,6 +241,7 @@ export const printChangeLog = <T extends { [k: string]: any }>(
export const printChange = (chg: Operation) => {
console.log(` ${JSON.stringify(chg)}`);
};

/**
* Should be called with a `/` delimited ref path. E.g. refs/heads/main
* @param thePath
Expand Down

0 comments on commit cd68d7c

Please sign in to comment.