package identifier

This commit is contained in:
Nate 2025-04-08 17:55:43 -07:00
parent 7ea89dc3fa
commit cbc4df275c
2 changed files with 134 additions and 0 deletions

View file

@ -1,9 +1,11 @@
import {
decodeFQSN,
decodeFullSlug,
decodePackageIdentifier,
decodePackageSlug,
encodeFQSN,
encodeFullSlug,
encodePackageIdentifier,
encodePackageSlug,
VirtualTags,
} from "./slugs.js";
@ -80,3 +82,80 @@ describe("PackageSlug", () => {
);
});
});
describe("PackageIdentifier", () => {
it("should encode/decode slug-based package identifier", () => {
const testIdentifier = {
uriType: "slug" as const,
fullSlug: {
ownerSlug: "test-owner",
packageSlug: "test-package",
versionSlug: "2.1.0",
},
};
const encoded = encodePackageIdentifier(testIdentifier);
expect(encoded).toBe("test-owner/test-package@2.1.0");
const decoded = decodePackageIdentifier(encoded);
expect(decoded).toEqual(testIdentifier);
});
it("should encode/decode slug-based package identifier without version", () => {
const encoded = "test-owner/test-package";
const decoded = decodePackageIdentifier(encoded);
expect(decoded).toEqual({
uriType: "slug",
fullSlug: {
ownerSlug: "test-owner",
packageSlug: "test-package",
versionSlug: VirtualTags.Latest,
},
});
});
it("should encode/decode file-based package identifier with relative path", () => {
const testIdentifier = {
uriType: "file" as const,
filePath: "./path/to/package.yaml",
};
const encoded = encodePackageIdentifier(testIdentifier);
expect(encoded).toBe("./path/to/package.yaml");
const decoded = decodePackageIdentifier(encoded);
expect(decoded).toEqual(testIdentifier);
});
it("should encode/decode file-based package identifier with absolute path", () => {
const testIdentifier = {
uriType: "file" as const,
filePath: "/absolute/path/to/package.yaml",
};
const encoded = encodePackageIdentifier(testIdentifier);
expect(encoded).toBe("/absolute/path/to/package.yaml");
const decoded = decodePackageIdentifier(encoded);
expect(decoded).toEqual(testIdentifier);
});
it("should decode file-based package identifier with file:// protocol", () => {
const encoded = "file:///path/to/package.yaml";
const decoded = decodePackageIdentifier(encoded);
expect(decoded).toEqual({
uriType: "file" as const,
filePath: "/path/to/package.yaml",
});
});
it("should throw error for unknown URI type", () => {
const invalidIdentifier = {
uriType: "invalid",
data: "something",
};
expect(() => encodePackageIdentifier(invalidIdentifier as any)).toThrow(
"Unknown URI type: invalid",
);
});
});

View file

@ -2,10 +2,65 @@ export interface PackageSlug {
ownerSlug: string;
packageSlug: string;
}
export interface FullSlug extends PackageSlug {
versionSlug: string;
}
// Identifier roughly equals URI, except that we want to provide shorthand for slugs and filepaths
// Later it is possible to allow URIs as well since they can be uniquely parsed
interface BasePackageIdentifier {
uriType: "file" | "slug";
}
interface FullSlugIdentifier extends BasePackageIdentifier {
uriType: "slug";
fullSlug: FullSlug;
}
interface FileIdentifier extends BasePackageIdentifier {
uriType: "file";
filePath: string;
}
export type PackageIdentifier = FullSlugIdentifier | FileIdentifier;
export function encodePackageIdentifier(identifier: PackageIdentifier): string {
switch (identifier.uriType) {
case "slug":
return encodeFullSlug(identifier.fullSlug);
case "file":
// For file paths, just return the path directly without a prefix
return identifier.filePath;
default:
throw new Error(`Unknown URI type: ${(identifier as any).uriType}`);
}
}
export function decodePackageIdentifier(identifier: string): PackageIdentifier {
// Shorthand: if it starts with . or /, then it's a path
if (identifier.startsWith(".") || identifier.startsWith("/")) {
return {
uriType: "file",
filePath: identifier,
};
}
// Keep support for explicit file:// protocol
else if (identifier.startsWith("file://")) {
return {
uriType: "file",
filePath: identifier.substring(7),
};
}
// Otherwise, it's a slug
else {
return {
uriType: "slug",
fullSlug: decodeFullSlug(identifier),
};
}
}
export enum VirtualTags {
Latest = "latest",
}