refactor: extract payload pack/unpack pipeline into @cryptgeon/shared

This commit is contained in:
2026-09-12 10:01:49 +02:00
parent 77b7ae1de6
commit 82900adef8
7 changed files with 133 additions and 60 deletions
+1
View File
@@ -2,4 +2,5 @@ export * from "./crypto.js";
export * from "./types.js";
export * from "./api.js";
export * from "./compression.js";
export * from "./payload.js";
export { encode, decode } from "@msgpack/msgpack";
+31
View File
@@ -0,0 +1,31 @@
import { describe, expect, it } from "vitest";
import { packContent, unpackContent } from "./payload";
import { bytesToUtf8, utf8ToBytes } from "./crypto";
describe("payload", () => {
it("round-trips a text note through the full pipeline", () => {
const { data, extra, key } = packContent({ type: "text", text: "hello world" });
expect(extra.length).toBe(0);
const content = unpackContent(data, key);
expect(content).toEqual({ type: "text", data: "hello world" });
});
it("round-trips with a password and sets extra", () => {
const { data, extra, key } = packContent({ type: "text", text: "secret" }, "pw123");
expect(extra.length).toBeGreaterThan(0);
const content = unpackContent(data, key);
expect(content).toEqual({ type: "text", data: "secret" });
});
it("round-trips files (FileDTO)", () => {
const file = { name: "a.txt", mime: "text/plain", size: 5, data: utf8ToBytes("hello") };
const { data, key } = packContent({ type: "files", files: [file] });
const content = unpackContent(data, key);
expect(content.type).toBe("files");
if (content.type === "files") {
expect(content.data).toHaveLength(1);
expect(content.data[0]!.name).toBe("a.txt");
expect(bytesToUtf8(content.data[0]!.data)).toBe("hello");
}
});
});
+49
View File
@@ -0,0 +1,49 @@
import { encode, decode } from "@msgpack/msgpack";
import { compress, decompress } from "./compression.js";
import {
deriveKey,
encrypt,
decrypt,
generateKey,
randomBytes,
} from "./crypto.js";
import type { FileDTO, NoteContent } from "./types.js";
export type NoteInput =
| { type: "text"; text: string }
| { type: "files"; files: FileDTO[] };
export type PackResult = {
data: Uint8Array;
extra: Uint8Array;
key: Uint8Array;
};
export function packContent(input: NoteInput, password?: string): PackResult {
let key: Uint8Array;
let extra: Uint8Array;
if (password) {
const salt = randomBytes(16);
key = deriveKey(password, salt);
extra = encode({ salt, N: 32768, r: 8, p: 1 });
} else {
key = generateKey();
extra = new Uint8Array();
}
const content: NoteContent =
input.type === "text"
? { type: "text", data: input.text }
: { type: "files", data: input.files };
const encoded = encrypt(compress(encode(content)), key);
return { data: encoded, extra, key };
}
export function unpackContent(data: Uint8Array, key: Uint8Array): NoteContent {
const content = decode(decompress(decrypt(data, key))) as NoteContent;
if (content.type !== "text" && content.type !== "files") {
throw new Error("Unknown content type");
}
return content;
}