Skip to content

Commit

Permalink
Encoded definition paths remain unique (#29348)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: e0de53d21aac0ba2db641ab471b35235916dafee
  • Loading branch information
thomasballinger authored and Convex, Inc. committed Aug 29, 2024
1 parent 49111a9 commit 6290711
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/cli/lib/components/definition/bundle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test, expect, describe } from "vitest";
import { encodeDefinitionPath } from "./bundle.js";
import { ComponentDefinitionPath } from "./directoryStructure.js";

describe("encodeDefinitionPath", async () => {
test("Escaped definition paths are distinguishable from unescaped ones", () => {
const a = encodeDefinitionPath("foo/bar-baz" as ComponentDefinitionPath);
const b = encodeDefinitionPath("foo/bar_baz" as ComponentDefinitionPath);
expect(a).not.toEqual(b);
});
});
11 changes: 8 additions & 3 deletions src/cli/lib/components/definition/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,21 @@ async function findComponentDependencies(
return { components, dependencyGraph };
}

// Each path component is less than 64 bytes and escape all a-zA-Z0-9
// Each path component is less than 64 bytes and is limited to a-zA-Z0-9
// This is the only version of the path the server will receive.
export function encodeDefinitionPath(
s: ComponentDefinitionPath,
): EncodedComponentDefinitionPath {
const components = s.split(path.sep);
return components
.map((s) => {
const escaped = s.replaceAll("-", "_").replaceAll("+", "_");
if (escaped.length <= 64) {
const escaped = s
.replaceAll("-", "_")
.replaceAll("+", "_")
.replaceAll(" ", "_")
.replaceAll(".", "_");
if (escaped.length <= 64 && escaped === s) {
// If escaping lost any information then
return escaped;
}
const hash = crypto.createHash("md5").update(s).digest("hex");
Expand Down

0 comments on commit 6290711

Please sign in to comment.