Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extract util function to compress object #185

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
nadilas marked this conversation as resolved.
Show resolved Hide resolved

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
Loading