@opencode-ai/util 0.0.0-beta-17595 → 0.0.0-beta-17727
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.
- package/dist/binary.d.ts +7 -0
- package/dist/binary.js +34 -0
- package/dist/binary.test.d.ts +1 -0
- package/dist/binary.test.js +16 -0
- package/dist/encode.d.ts +4 -0
- package/dist/encode.js +38 -0
- package/dist/encode.test.d.ts +1 -0
- package/dist/encode.test.js +23 -0
- package/dist/npm.d.ts +5 -0
- package/dist/npm.js +29 -0
- package/dist/path.d.ts +4 -0
- package/dist/path.js +33 -0
- package/dist/path.test.d.ts +1 -0
- package/dist/path.test.js +22 -0
- package/dist/retry.d.ts +8 -0
- package/dist/retry.js +37 -0
- package/dist/retry.test.d.ts +1 -0
- package/dist/retry.test.js +31 -0
- package/package.json +4 -4
package/dist/binary.d.ts
ADDED
package/dist/binary.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export var Binary;
|
|
2
|
+
(function (Binary) {
|
|
3
|
+
function search(array, id, compare) {
|
|
4
|
+
let left = 0;
|
|
5
|
+
let right = array.length - 1;
|
|
6
|
+
while (left <= right) {
|
|
7
|
+
const middle = Math.floor((left + right) / 2);
|
|
8
|
+
const value = compare(array[middle]);
|
|
9
|
+
if (value === id)
|
|
10
|
+
return { found: true, index: middle };
|
|
11
|
+
if (value < id)
|
|
12
|
+
left = middle + 1;
|
|
13
|
+
else
|
|
14
|
+
right = middle - 1;
|
|
15
|
+
}
|
|
16
|
+
return { found: false, index: left };
|
|
17
|
+
}
|
|
18
|
+
Binary.search = search;
|
|
19
|
+
function insert(array, item, compare) {
|
|
20
|
+
const id = compare(item);
|
|
21
|
+
let left = 0;
|
|
22
|
+
let right = array.length;
|
|
23
|
+
while (left < right) {
|
|
24
|
+
const middle = Math.floor((left + right) / 2);
|
|
25
|
+
if (compare(array[middle]) < id)
|
|
26
|
+
left = middle + 1;
|
|
27
|
+
else
|
|
28
|
+
right = middle;
|
|
29
|
+
}
|
|
30
|
+
array.splice(left, 0, item);
|
|
31
|
+
return array;
|
|
32
|
+
}
|
|
33
|
+
Binary.insert = insert;
|
|
34
|
+
})(Binary || (Binary = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { Binary } from "./binary.js";
|
|
3
|
+
describe("binary search", () => {
|
|
4
|
+
test("finds sorted values and their insertion points", () => {
|
|
5
|
+
const values = [{ id: "a" }, { id: "c" }, { id: "e" }];
|
|
6
|
+
expect(Binary.search(values, "c", (item) => item.id)).toEqual({ found: true, index: 1 });
|
|
7
|
+
expect(Binary.search(values, "d", (item) => item.id)).toEqual({ found: false, index: 2 });
|
|
8
|
+
expect(Binary.search(values, "0", (item) => item.id)).toEqual({ found: false, index: 0 });
|
|
9
|
+
});
|
|
10
|
+
test("inserts before the first matching sorted value", () => {
|
|
11
|
+
const values = [{ id: "a" }, { id: "c" }, { id: "c" }, { id: "e" }];
|
|
12
|
+
const item = { id: "c" };
|
|
13
|
+
expect(Binary.insert(values, item, (value) => value.id)).toBe(values);
|
|
14
|
+
expect(values).toEqual([{ id: "a" }, item, { id: "c" }, { id: "c" }, { id: "e" }]);
|
|
15
|
+
});
|
|
16
|
+
});
|
package/dist/encode.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function base64Encode(value: string): string;
|
|
2
|
+
export declare function base64Decode(value: string): string;
|
|
3
|
+
export declare function checksum(content: string): string | undefined;
|
|
4
|
+
export declare function sampledChecksum(content: string, limit?: number): string | undefined;
|
package/dist/encode.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function base64Encode(value) {
|
|
2
|
+
const bytes = new TextEncoder().encode(value);
|
|
3
|
+
const binary = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("");
|
|
4
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
5
|
+
}
|
|
6
|
+
export function base64Decode(value) {
|
|
7
|
+
const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/"));
|
|
8
|
+
return new TextDecoder().decode(Uint8Array.from(binary, (character) => character.charCodeAt(0)));
|
|
9
|
+
}
|
|
10
|
+
export function checksum(content) {
|
|
11
|
+
if (!content)
|
|
12
|
+
return;
|
|
13
|
+
let hash = 0x811c9dc5;
|
|
14
|
+
for (let index = 0; index < content.length; index++) {
|
|
15
|
+
hash ^= content.charCodeAt(index);
|
|
16
|
+
hash = Math.imul(hash, 0x01000193);
|
|
17
|
+
}
|
|
18
|
+
return (hash >>> 0).toString(36);
|
|
19
|
+
}
|
|
20
|
+
export function sampledChecksum(content, limit = 500_000) {
|
|
21
|
+
if (!content)
|
|
22
|
+
return;
|
|
23
|
+
if (content.length <= limit)
|
|
24
|
+
return checksum(content);
|
|
25
|
+
const size = 4096;
|
|
26
|
+
return `${content.length}:${[
|
|
27
|
+
0,
|
|
28
|
+
Math.floor(content.length * 0.25),
|
|
29
|
+
Math.floor(content.length * 0.5),
|
|
30
|
+
Math.floor(content.length * 0.75),
|
|
31
|
+
content.length - size,
|
|
32
|
+
]
|
|
33
|
+
.map((point) => {
|
|
34
|
+
const start = Math.max(0, Math.min(content.length - size, point - Math.floor(size / 2)));
|
|
35
|
+
return checksum(content.slice(start, start + size)) ?? "";
|
|
36
|
+
})
|
|
37
|
+
.join(":")}`;
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { base64Decode, base64Encode, checksum, sampledChecksum } from "./encode.js";
|
|
3
|
+
describe("frontend encoding", () => {
|
|
4
|
+
test("uses unpadded URL-safe UTF-8 base64", () => {
|
|
5
|
+
expect(base64Encode("hello")).toBe("aGVsbG8");
|
|
6
|
+
expect(base64Encode("✓ à la mode")).toBe("4pyTIMOgIGxhIG1vZGU");
|
|
7
|
+
expect(base64Decode("4pyTIMOgIGxhIG1vZGU")).toBe("✓ à la mode");
|
|
8
|
+
expect(base64Decode("dXNlcjpwYXNz")).toBe("user:pass");
|
|
9
|
+
});
|
|
10
|
+
test("rejects invalid base64", () => {
|
|
11
|
+
expect(() => base64Decode("%%%")).toThrow();
|
|
12
|
+
});
|
|
13
|
+
test("keeps stable FNV checksums", () => {
|
|
14
|
+
expect(checksum("")).toBeUndefined();
|
|
15
|
+
expect(checksum("hello")).toBe("m3bicr");
|
|
16
|
+
expect(checksum("✓ à la mode")).toBe("jmczk0");
|
|
17
|
+
});
|
|
18
|
+
test("samples large values without changing the size boundary", () => {
|
|
19
|
+
const value = "abcdef".repeat(100);
|
|
20
|
+
expect(sampledChecksum(value, value.length)).toBe(checksum(value));
|
|
21
|
+
expect(sampledChecksum(value, value.length - 1)).toBe("600:1isj1k5:1isj1k5:1isj1k5:1isj1k5:1isj1k5");
|
|
22
|
+
});
|
|
23
|
+
});
|
package/dist/npm.d.ts
CHANGED
|
@@ -17,12 +17,17 @@ export interface Interface {
|
|
|
17
17
|
readonly add: (pkg: string, options?: {
|
|
18
18
|
readonly subpaths?: readonly string[];
|
|
19
19
|
}) => Effect.Effect<EntryPoint, InstallFailedError | EffectFlock.LockError>;
|
|
20
|
+
readonly resolve: (pkg: string, options?: {
|
|
21
|
+
readonly subpaths?: readonly string[];
|
|
22
|
+
}) => Effect.Effect<EntryPoint>;
|
|
20
23
|
readonly which: (pkg: string, bin?: string) => Effect.Effect<string | undefined>;
|
|
21
24
|
}
|
|
22
25
|
declare const Service_base: Context.ServiceClass<Service, "@opencode/Npm", Interface>;
|
|
23
26
|
export declare class Service extends Service_base {
|
|
24
27
|
}
|
|
25
28
|
export declare function sanitize(pkg: string): string;
|
|
29
|
+
export declare function isRegistryPackage(pkg: string): Promise<boolean>;
|
|
26
30
|
export declare const node: LayerNode.Node<Service, never, LayerNode.Tag<"global">>;
|
|
27
31
|
export declare function add(...args: Parameters<Interface["add"]>): Promise<EntryPoint>;
|
|
32
|
+
export declare function resolve(...args: Parameters<Interface["resolve"]>): Promise<EntryPoint>;
|
|
28
33
|
export declare function which(...args: Parameters<Interface["which"]>): Promise<string | undefined>;
|
package/dist/npm.js
CHANGED
|
@@ -25,6 +25,16 @@ export function sanitize(pkg) {
|
|
|
25
25
|
return pkg;
|
|
26
26
|
return Array.from(pkg, (char) => (illegal.has(char) || char.charCodeAt(0) < 32 ? "_" : char)).join("");
|
|
27
27
|
}
|
|
28
|
+
export async function isRegistryPackage(pkg) {
|
|
29
|
+
const { default: npa } = await import("npm-package-arg");
|
|
30
|
+
try {
|
|
31
|
+
const result = npa(pkg);
|
|
32
|
+
return result.name !== undefined && ["version", "range", "tag"].includes(result.type);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
28
38
|
const resolveEntryPoint = (name, dir, subpaths = [""]) => {
|
|
29
39
|
const entrypoint = subpaths
|
|
30
40
|
.map((subpath) => {
|
|
@@ -100,6 +110,21 @@ const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
|
100
110
|
}
|
|
101
111
|
return resolveEntryPoint(first.name, first.path, options?.subpaths);
|
|
102
112
|
}, Effect.scoped);
|
|
113
|
+
const resolve = Effect.fn("Npm.resolve")(function* (pkg, options) {
|
|
114
|
+
const { default: npa } = yield* Effect.promise(() => import("npm-package-arg"));
|
|
115
|
+
const name = (() => {
|
|
116
|
+
try {
|
|
117
|
+
return npa(pkg).name ?? pkg;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return pkg;
|
|
121
|
+
}
|
|
122
|
+
})();
|
|
123
|
+
const dir = path.join(directory(pkg), "node_modules", name);
|
|
124
|
+
if (!(yield* afs.existsSafe(dir)))
|
|
125
|
+
return { directory: dir };
|
|
126
|
+
return resolveEntryPoint(name, dir, options?.subpaths);
|
|
127
|
+
});
|
|
103
128
|
const which = Effect.fn("Npm.which")(function* (pkg, bin) {
|
|
104
129
|
const dir = directory(pkg);
|
|
105
130
|
const binDir = path.join(dir, "node_modules", ".bin");
|
|
@@ -144,6 +169,7 @@ const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
|
144
169
|
});
|
|
145
170
|
return Service.of({
|
|
146
171
|
add,
|
|
172
|
+
resolve,
|
|
147
173
|
which,
|
|
148
174
|
});
|
|
149
175
|
}));
|
|
@@ -156,6 +182,9 @@ const { runPromise } = makeRuntime(Service, LayerNode.compile(node));
|
|
|
156
182
|
export async function add(...args) {
|
|
157
183
|
return runPromise((svc) => svc.add(...args));
|
|
158
184
|
}
|
|
185
|
+
export async function resolve(...args) {
|
|
186
|
+
return runPromise((svc) => svc.resolve(...args));
|
|
187
|
+
}
|
|
159
188
|
export async function which(...args) {
|
|
160
189
|
return runPromise((svc) => svc.which(...args));
|
|
161
190
|
}
|
package/dist/path.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function getFilename(path: string | undefined): string;
|
|
2
|
+
export declare function getDirectory(path: string | undefined): string;
|
|
3
|
+
export declare function getFilenameTruncated(path: string | undefined, maxLength?: number): string;
|
|
4
|
+
export declare function truncateMiddle(text: string, maxLength?: number): string;
|
package/dist/path.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function getFilename(path) {
|
|
2
|
+
if (!path)
|
|
3
|
+
return "";
|
|
4
|
+
const trimmed = path.replace(/[/\\]+$/, "");
|
|
5
|
+
const parts = trimmed.split(/[/\\]/);
|
|
6
|
+
return parts[parts.length - 1] ?? "";
|
|
7
|
+
}
|
|
8
|
+
export function getDirectory(path) {
|
|
9
|
+
if (!path)
|
|
10
|
+
return "";
|
|
11
|
+
const trimmed = path.replace(/[/\\]+$/, "");
|
|
12
|
+
const parts = trimmed.split(/[/\\]/);
|
|
13
|
+
return parts.slice(0, parts.length - 1).join("/") + "/";
|
|
14
|
+
}
|
|
15
|
+
export function getFilenameTruncated(path, maxLength = 20) {
|
|
16
|
+
const filename = getFilename(path);
|
|
17
|
+
if (filename.length <= maxLength)
|
|
18
|
+
return filename;
|
|
19
|
+
const lastDot = filename.lastIndexOf(".");
|
|
20
|
+
const extension = lastDot <= 0 ? "" : filename.slice(lastDot);
|
|
21
|
+
const available = maxLength - extension.length - 1;
|
|
22
|
+
if (available <= 0)
|
|
23
|
+
return filename.slice(0, maxLength - 1) + "…";
|
|
24
|
+
return filename.slice(0, available) + "…" + extension;
|
|
25
|
+
}
|
|
26
|
+
export function truncateMiddle(text, maxLength = 20) {
|
|
27
|
+
if (text.length <= maxLength)
|
|
28
|
+
return text;
|
|
29
|
+
const available = maxLength - 1;
|
|
30
|
+
const start = Math.ceil(available / 2);
|
|
31
|
+
const end = Math.floor(available / 2);
|
|
32
|
+
return text.slice(0, start) + "…" + text.slice(-end);
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { getDirectory, getFilename, getFilenameTruncated, truncateMiddle } from "./path.js";
|
|
3
|
+
describe("client paths", () => {
|
|
4
|
+
test("reads POSIX and Windows paths with the same rules", () => {
|
|
5
|
+
expect(getFilename("/repo/src/index.ts")).toBe("index.ts");
|
|
6
|
+
expect(getFilename("C:\\repo\\src\\index.ts\\")).toBe("index.ts");
|
|
7
|
+
expect(getDirectory("/repo/src/index.ts")).toBe("/repo/src/");
|
|
8
|
+
expect(getDirectory("C:\\repo\\src\\index.ts")).toBe("C:/repo/src/");
|
|
9
|
+
});
|
|
10
|
+
test("preserves root, UNC, mixed, and single-segment behavior", () => {
|
|
11
|
+
expect(getFilename("\\\\server\\share\\file")).toBe("file");
|
|
12
|
+
expect(getDirectory("\\\\server\\share\\file")).toBe("//server/share/");
|
|
13
|
+
expect(getDirectory("C:\\repo/src\\file")).toBe("C:/repo/src/");
|
|
14
|
+
expect(getDirectory("file")).toBe("/");
|
|
15
|
+
expect(getFilename(undefined)).toBe("");
|
|
16
|
+
expect(getDirectory("")).toBe("");
|
|
17
|
+
});
|
|
18
|
+
test("keeps filename truncation stable", () => {
|
|
19
|
+
expect(getFilenameTruncated("/repo/long-component-name.tsx", 16)).toBe("long-compon….tsx");
|
|
20
|
+
expect(truncateMiddle("abcdefghijklmnop", 9)).toBe("abcd…mnop");
|
|
21
|
+
});
|
|
22
|
+
});
|
package/dist/retry.d.ts
ADDED
package/dist/retry.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const transientMessages = [
|
|
2
|
+
"load failed",
|
|
3
|
+
"network connection was lost",
|
|
4
|
+
"network request failed",
|
|
5
|
+
"failed to fetch",
|
|
6
|
+
"econnreset",
|
|
7
|
+
"econnrefused",
|
|
8
|
+
"etimedout",
|
|
9
|
+
"socket hang up",
|
|
10
|
+
];
|
|
11
|
+
function isTransientError(error) {
|
|
12
|
+
if (!error)
|
|
13
|
+
return false;
|
|
14
|
+
// oxlint-disable-next-line no-base-to-string -- Error input is intentionally normalized for message matching.
|
|
15
|
+
const message = String(error instanceof Error ? error.message : error).toLowerCase();
|
|
16
|
+
return transientMessages.some((item) => message.includes(item));
|
|
17
|
+
}
|
|
18
|
+
export async function retry(operation, options = {}) {
|
|
19
|
+
const attempts = options.attempts ?? 3;
|
|
20
|
+
const delay = options.delay ?? 500;
|
|
21
|
+
const factor = options.factor ?? 2;
|
|
22
|
+
const maxDelay = options.maxDelay ?? 10_000;
|
|
23
|
+
const retryIf = options.retryIf ?? isTransientError;
|
|
24
|
+
let lastError;
|
|
25
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
26
|
+
try {
|
|
27
|
+
return await operation();
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
lastError = error;
|
|
31
|
+
if (attempt === attempts - 1 || !retryIf(error))
|
|
32
|
+
throw error;
|
|
33
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(delay * Math.pow(factor, attempt), maxDelay)));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
throw lastError;
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { retry } from "./retry.js";
|
|
3
|
+
describe("client retry", () => {
|
|
4
|
+
test("retries transient failures up to the configured attempt count", async () => {
|
|
5
|
+
const failures = [];
|
|
6
|
+
const value = await retry(async () => {
|
|
7
|
+
failures.push("attempt");
|
|
8
|
+
if (failures.length < 3)
|
|
9
|
+
throw new Error("Failed to fetch");
|
|
10
|
+
return "ready";
|
|
11
|
+
}, { delay: 0 });
|
|
12
|
+
expect(value).toBe("ready");
|
|
13
|
+
expect(failures).toHaveLength(3);
|
|
14
|
+
});
|
|
15
|
+
test("does not retry other failures", async () => {
|
|
16
|
+
const failures = [];
|
|
17
|
+
await expect(retry(async () => {
|
|
18
|
+
failures.push("attempt");
|
|
19
|
+
throw new Error("invalid response");
|
|
20
|
+
})).rejects.toThrow("invalid response");
|
|
21
|
+
expect(failures).toHaveLength(1);
|
|
22
|
+
});
|
|
23
|
+
test("uses a caller-owned retry condition", async () => {
|
|
24
|
+
const failures = [];
|
|
25
|
+
await expect(retry(async () => {
|
|
26
|
+
failures.push("attempt");
|
|
27
|
+
throw new Error("retry me");
|
|
28
|
+
}, { attempts: 2, delay: 0, retryIf: () => true })).rejects.toThrow("retry me");
|
|
29
|
+
expect(failures).toHaveLength(2);
|
|
30
|
+
});
|
|
31
|
+
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "@opencode-ai/util",
|
|
4
|
-
"version": "0.0.0-beta-
|
|
4
|
+
"version": "0.0.0-beta-17727",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"typecheck": "tsgo --noEmit"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@effect/opentelemetry": "4.0.0-
|
|
47
|
-
"@effect/platform-node": "4.0.0-
|
|
46
|
+
"@effect/opentelemetry": "4.0.0-rc.110",
|
|
47
|
+
"@effect/platform-node": "4.0.0-rc.110",
|
|
48
48
|
"@npmcli/arborist": "9.4.0",
|
|
49
49
|
"@npmcli/config": "10.8.1",
|
|
50
50
|
"@opentelemetry/api": "1.9.0",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@opentelemetry/sdk-trace-base": "2.6.1",
|
|
54
54
|
"@opentelemetry/sdk-trace-node": "2.6.1",
|
|
55
55
|
"cross-spawn": "7.0.6",
|
|
56
|
-
"effect": "4.0.0-
|
|
56
|
+
"effect": "4.0.0-rc.110",
|
|
57
57
|
"glob": "13.0.5",
|
|
58
58
|
"mime-types": "3.0.2",
|
|
59
59
|
"minimatch": "10.2.5",
|