@opencode/util 0.0.0-dev-20670 → 0.0.0-dev-20672
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/artifact.d.ts +1 -1
- package/dist/artifact.js +9 -1
- package/dist/artifact.test.js +4 -0
- package/dist/base64.d.ts +4 -0
- package/dist/base64.js +24 -0
- package/dist/base64.test.d.ts +1 -0
- package/dist/base64.test.js +10 -0
- package/package.json +1 -1
package/dist/artifact.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/** How the app shows a file, from its extension. */
|
|
2
|
-
export type ArtifactKind = "image" | "svg" | "audio" | "video" | "pdf" | "html" | "markdown" | "mermaid" | "table" | "font" | "text";
|
|
2
|
+
export type ArtifactKind = "image" | "svg" | "audio" | "video" | "pdf" | "html" | "markdown" | "mermaid" | "table" | "font" | "document" | "spreadsheet" | "presentation" | "text";
|
|
3
3
|
export declare function artifactMime(path: string): string | undefined;
|
|
4
4
|
export declare function artifactKind(path: string): ArtifactKind;
|
package/dist/artifact.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
const officeKinds = new Map([
|
|
2
|
+
["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document"],
|
|
3
|
+
["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet"],
|
|
4
|
+
["application/vnd.openxmlformats-officedocument.presentationml.presentation", "presentation"],
|
|
5
|
+
]);
|
|
1
6
|
const mimes = new Map([
|
|
2
7
|
["png", "image/png"],
|
|
3
8
|
["jpg", "image/jpeg"],
|
|
@@ -40,6 +45,9 @@ const mimes = new Map([
|
|
|
40
45
|
["otf", "font/otf"],
|
|
41
46
|
["woff", "font/woff"],
|
|
42
47
|
["woff2", "font/woff2"],
|
|
48
|
+
["docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
|
49
|
+
["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
|
|
50
|
+
["pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
|
|
43
51
|
]);
|
|
44
52
|
function artifactExtension(path) {
|
|
45
53
|
const name = path.split(/[\\/]/).pop() ?? "";
|
|
@@ -73,5 +81,5 @@ export function artifactKind(path) {
|
|
|
73
81
|
return "audio";
|
|
74
82
|
if (mime.startsWith("font/"))
|
|
75
83
|
return "font";
|
|
76
|
-
return "video";
|
|
84
|
+
return officeKinds.get(mime) ?? "video";
|
|
77
85
|
}
|
package/dist/artifact.test.js
CHANGED
|
@@ -14,6 +14,10 @@ describe("artifactKind", () => {
|
|
|
14
14
|
["data.csv", "table"],
|
|
15
15
|
["data.tsv", "table"],
|
|
16
16
|
["Inter.woff2", "font"],
|
|
17
|
+
["report.docx", "document"],
|
|
18
|
+
["budget.XLSX", "spreadsheet"],
|
|
19
|
+
["deck.pptx", "presentation"],
|
|
20
|
+
["legacy.doc", "text"],
|
|
17
21
|
["src/app.ts", "text"],
|
|
18
22
|
["Makefile", "text"],
|
|
19
23
|
[".env", "text"],
|
package/dist/base64.d.ts
ADDED
package/dist/base64.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// The native codecs (Chromium 140, Firefox 133, Safari 18.2) run about 100 times faster than a string round trip. The
|
|
2
|
+
// type libraries declare them, but older web browsers lack them, so each is read as possibly missing first.
|
|
3
|
+
const nativeEncode = Uint8Array.prototype.toBase64;
|
|
4
|
+
const nativeDecode = Uint8Array.fromBase64;
|
|
5
|
+
/** Encodes bytes as standard base64. */
|
|
6
|
+
export function bytesToBase64(bytes) {
|
|
7
|
+
if (nativeEncode)
|
|
8
|
+
return bytes.toBase64();
|
|
9
|
+
const parts = [];
|
|
10
|
+
for (let index = 0; index < bytes.length; index += 0x8000) {
|
|
11
|
+
parts.push(String.fromCharCode(...bytes.subarray(index, index + 0x8000)));
|
|
12
|
+
}
|
|
13
|
+
return btoa(parts.join(""));
|
|
14
|
+
}
|
|
15
|
+
/** Decodes standard base64 into bytes. */
|
|
16
|
+
export function base64ToBytes(value) {
|
|
17
|
+
if (nativeDecode)
|
|
18
|
+
return Uint8Array.fromBase64(value);
|
|
19
|
+
const binary = atob(value);
|
|
20
|
+
const bytes = new Uint8Array(binary.length);
|
|
21
|
+
for (let index = 0; index < binary.length; index++)
|
|
22
|
+
bytes[index] = binary.charCodeAt(index);
|
|
23
|
+
return bytes;
|
|
24
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { base64ToBytes, bytesToBase64 } from "./base64.js";
|
|
3
|
+
describe("base64", () => {
|
|
4
|
+
test("encodes as standard base64 and decodes back to the same bytes", () => {
|
|
5
|
+
const bytes = Uint8Array.from({ length: 70_000 }, (_, index) => index % 256);
|
|
6
|
+
const encoded = bytesToBase64(bytes);
|
|
7
|
+
expect(encoded).toBe(Buffer.from(bytes).toString("base64"));
|
|
8
|
+
expect(base64ToBytes(encoded)).toEqual(bytes);
|
|
9
|
+
});
|
|
10
|
+
});
|