Skip to content

Commit

Permalink
make the serialization fully rfc 2396 compliant
Browse files Browse the repository at this point in the history
this is the old, obsolete standard for uris, and we now only use
unreserved characters that can appear anywhere in a uri
  • Loading branch information
pixelcmtd committed Nov 8, 2023
1 parent 6545d49 commit d0a155f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import b64 from "base64-js";
import base64 from "base64-js";

export const ratings: [string, string][] = [
["i dont know", "#808080"],
Expand Down Expand Up @@ -149,6 +149,11 @@ export const defaultRatings: ratings = Object.fromEntries(
([cat, kinks]) => [cat, kinks.map((k) => k[1].map(() => 0))])
);

const toBase64 = (x: Uint8Array) =>
base64.fromByteArray(x).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
const fromBase64 = (x: string) =>
base64.toByteArray(x.length % 4 ? x.padEnd(x.length + 4 - (x.length % 4), "=") : x);

export function encodeKinkCheck({ ratings }: { ratings: ratings }): string {
const r = Object.entries(ratings)
.flatMap(([cat, rats]) => kinks[cat]
Expand All @@ -158,15 +163,15 @@ export function encodeKinkCheck({ ratings }: { ratings: ratings }): string {
r[id] = (enc(rat[rat.length == 1 ? 0 : 1]) << 4) | enc(rat[0]);
return r;
}, []);
return "0;" + b64.fromByteArray(new Uint8Array(r));
return "0~" + toBase64(new Uint8Array(r));
}

export function decodeKinkCheck(s: string): { ratings: ratings } {
const x = s.split(";");
const x = s.split("~");
if (Number.parseInt(/[0-9]+/.exec(x[0])!.reduce((x, y) => x + y)) !== 0)
throw "unsupported kinkcheck serialization version";
const ratings = defaultRatings;
b64.toByteArray(x[1]).forEach((rat, id) => {
fromBase64(x[1]).forEach((rat, id) => {
Object.keys(ratings).forEach((cat) => {
ratings[cat].forEach((_, i) => {
if (kinks[cat][i][2] === id) {
Expand Down
8 changes: 4 additions & 4 deletions test/serialization.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from "vitest";
import { decodeKinkCheck, encodeKinkCheck } from "../src/base";

const exampleCheck = "0;ABkrNURTkhGqvMoAAAAAAAARIjMAAERVmaq7zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAiADNEVZkAqrvMAAAAAAAAAAAAAAA=";
const exampleCheck = "0~ABkrNURTkhGqvMoAAAAAAAARIjMAAERVmaq7zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAiADNEVZkAqrvMAAAAAAAAAAAAAAA";

test("re-encoding", () => {
expect(encodeKinkCheck(decodeKinkCheck(exampleCheck))).toBe(exampleCheck);
Expand All @@ -10,17 +10,17 @@ test("re-encoding", () => {
test("decodeKinkCheck throws when given a too new version", () => {
let x;
try {
x = decodeKinkCheck("01;");
x = decodeKinkCheck("01~");
} catch {
return;
}
throw x;
});

test("extra data is ignored", () => {
expect(decodeKinkCheck(exampleCheck + ";otherdata")).toStrictEqual(decodeKinkCheck(exampleCheck));
expect(decodeKinkCheck(exampleCheck + "~otherdata")).toStrictEqual(decodeKinkCheck(exampleCheck));
});

test("extra kinks are ignored", () => {
expect(decodeKinkCheck(exampleCheck.replace("=", "ACAB="))).toStrictEqual(decodeKinkCheck(exampleCheck));
expect(decodeKinkCheck(exampleCheck + "ACAB")).toStrictEqual(decodeKinkCheck(exampleCheck));
});

1 comment on commit d0a155f

@pixelcmtd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention: We now also strip = padding

Please sign in to comment.