Skip to content

Commit

Permalink
Use msgpack for permalinks instead of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
gingershaped committed Nov 11, 2024
1 parent 3da0b9c commit ad2f0df
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 3 deletions.
135 changes: 135 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"immer": "^10.0.3",
"lezer-loader": "^0.3.0",
"magic-snowflakes": "^6.3.0",
"msgpackr": "^1.11.2",
"react": "^18.2.0",
"react-bootstrap": "^2.10.1",
"react-dom": "^18.2.0",
Expand Down
17 changes: 14 additions & 3 deletions src/latest/scripts/interpreter/permalink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import compatRaw from "../../data/compat.json?raw";
import { gunzipString, gzipString } from "gzip-utils";
import { gunzipString, gzipUint8Array } from "gzip-utils";
import { pack, unpack } from "msgpackr";

const compat = JSON.parse(compatRaw);

Expand Down Expand Up @@ -29,7 +30,9 @@ function incompatible(permalinkVersion: string) {
}

export function encodeHash(permalink: Omit<Permalink, "format">): Promise<string> {
return gzipString(JSON.stringify({ format: latestPermalink, ...permalink } as Permalink), "base64url") as Promise<string>;
// probably not great for performance but whatever
const buf = new Uint8Array([0xff, ...pack({ format: latestPermalink, ...permalink } as Permalink)]);
return gzipUint8Array(buf, "base64url") as Promise<string>;
}

type DecodeResult = {
Expand All @@ -43,7 +46,15 @@ type DecodeResult = {
export async function decodeHash(hash: string): Promise<DecodeResult | null> {
let permalink: OldPermalinks | Permalink;
try {
permalink = JSON.parse(await gunzipString(hash, "base64url", "utf8") as string);
// First try compressed permalinks
const decompressed = await gunzipString(hash, "base64url", "raw") as Uint8Array;
if (decompressed[0] == 0xff) {
// msgpack
permalink = unpack(decompressed.slice(1));
} else {
// JSON
permalink = JSON.parse(new TextDecoder().decode(decompressed));
}
} catch (decompressError) {
// Try to decode it as a non-compressed permalink
try {
Expand Down

0 comments on commit ad2f0df

Please sign in to comment.