@vite-hub/source 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ import { ViteHubError, getViteHubErrorShape } from "@vite-hub/runtime";
2
+ //#region src/core/errors.ts
3
+ function sourceError(message, options) {
4
+ return new ViteHubError("SOURCE_FAILED", message, options);
5
+ }
6
+ function sourceNotFoundError(name) {
7
+ return new ViteHubError("SOURCE_NOT_FOUND", `[vitehub] Source "${name}" is not registered.`, { details: { name } });
8
+ }
9
+ function sourcePathError(path) {
10
+ const publicPath = path.slice(0, 4096);
11
+ return new ViteHubError("SOURCE_PATH_INVALID", `[vitehub] Source path escapes the source root: ${JSON.stringify(publicPath)}.`, { details: { path: publicPath } });
12
+ }
13
+ function isSourceError(value) {
14
+ const code = getViteHubErrorShape(value)?.code;
15
+ return code === "SOURCE_FAILED" || code === "SOURCE_NOT_FOUND" || code === "SOURCE_PATH_INVALID";
16
+ }
17
+ //#endregion
18
+ //#region src/core/path.ts
19
+ function normalizeSourcePath(path = "") {
20
+ return path.replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "");
21
+ }
22
+ function normalizeSafeSourcePath(path = "", options = {}) {
23
+ const raw = path.replace(/\\/g, "/");
24
+ const normalized = normalizeSourcePath(path);
25
+ const parts = normalized.split("/").filter(Boolean);
26
+ if (!options.allowEmpty && !normalized) throw sourcePathError(path);
27
+ if (raw.startsWith("/") || /^[a-z]:\//i.test(raw) || parts.some((part) => part === "." || part === "..")) throw sourcePathError(path);
28
+ if (!options.allowReserved && (parts[0] === ".git" || parts[0] === ".vitehub")) throw sourcePathError(path);
29
+ return normalized;
30
+ }
31
+ function decodeSourceContent(content, options) {
32
+ if (options?.encoding === "binary") return typeof content === "string" ? new TextEncoder().encode(content) : content;
33
+ return typeof content === "string" ? content : new TextDecoder().decode(content);
34
+ }
35
+ //#endregion
36
+ export { sourceError as a, isSourceError as i, normalizeSafeSourcePath as n, sourceNotFoundError as o, normalizeSourcePath as r, sourcePathError as s, decodeSourceContent as t };