@zyro-sec/util 1.0.2 → 1.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.
- package/docs/layer-node.md +155 -155
- package/package.json +1 -1
- package/script/build.ts +9 -9
- package/script/pack.ts +5 -5
- package/src/abort.ts +9 -9
- package/src/activity-calendar.ts +50 -50
- package/src/artifact.ts +95 -95
- package/src/base64.ts +29 -29
- package/src/binary.ts +30 -30
- package/src/bom.ts +46 -46
- package/src/cross-spawn-spawner.ts +694 -694
- package/src/duration.ts +22 -22
- package/src/effect/app-node-platform.ts +40 -40
- package/src/effect/app-node.ts +14 -14
- package/src/effect/layer-node.ts +360 -360
- package/src/effect-flock.ts +313 -313
- package/src/encode.ts +39 -39
- package/src/execute-output.ts +23 -23
- package/src/flock.ts +346 -346
- package/src/fs-util.ts +271 -271
- package/src/glob.ts +38 -38
- package/src/global-roots.ts +19 -19
- package/src/global-roots.workerd.ts +16 -16
- package/src/global.ts +88 -88
- package/src/hash.ts +11 -11
- package/src/heap-snapshot.ts +44 -44
- package/src/no-timeout-fetch.ts +5 -5
- package/src/npm-config.ts +41 -41
- package/src/npm.ts +557 -557
- package/src/observability/logging.ts +179 -179
- package/src/observability/otlp.ts +109 -109
- package/src/observability/shared.ts +8 -8
- package/src/observability.ts +60 -60
- package/src/open.ts +12 -12
- package/src/patch.ts +382 -382
- package/src/path.ts +149 -149
- package/src/process-signal.ts +23 -23
- package/src/process.ts +270 -270
- package/src/qr.ts +323 -323
- package/src/record.ts +3 -3
- package/src/retry.ts +51 -51
- package/src/runtime/import.bun.ts +10 -10
- package/src/runtime/import.node.ts +61 -61
- package/src/runtime/import.workerd.ts +9 -9
- package/src/runtime-import.ts +1 -1
- package/src/session-mention.ts +24 -24
- package/src/session-title-fallback.ts +36 -36
- package/src/session-turn.ts +82 -82
- package/src/teardown.ts +19 -19
- package/src/text-boundary.ts +36 -36
- package/src/uuid.ts +15 -15
- package/test/fixtures/qr.json +625 -625
- package/tsconfig.build.json +12 -12
- package/tsconfig.dist.json +10 -10
- package/tsconfig.json +14 -14
- package/src/abort.test.ts +0 -16
- package/src/activity-calendar.test.ts +0 -136
- package/src/artifact.test.ts +0 -29
- package/src/base64.test.ts +0 -12
- package/src/binary.test.ts +0 -19
- package/src/bom.test.ts +0 -21
- package/src/encode.test.ts +0 -32
- package/src/execute-output.test.ts +0 -36
- package/src/no-timeout-fetch.test.ts +0 -33
- package/src/open.test.ts +0 -16
- package/src/path.test.ts +0 -89
- package/src/process-signal.test.ts +0 -38
- package/src/retry.test.ts +0 -87
- package/src/session-mention.test.ts +0 -19
- package/src/session-turn.test.ts +0 -131
- package/src/text-boundary.test.ts +0 -39
- package/src/uuid.test.ts +0 -80
- package/test/glob.test.ts +0 -11
- package/test/global-roots.test.ts +0 -62
- package/test/global.test.ts +0 -99
- package/test/observability-otlp.test.ts +0 -45
- package/test/qr.test.ts +0 -113
- package/test/session-title-fallback.test.ts +0 -49
package/src/artifact.ts
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
/** How the app shows a file, from its extension. */
|
|
2
|
-
export type ArtifactKind =
|
|
3
|
-
| "image"
|
|
4
|
-
| "svg"
|
|
5
|
-
| "audio"
|
|
6
|
-
| "video"
|
|
7
|
-
| "pdf"
|
|
8
|
-
| "html"
|
|
9
|
-
| "markdown"
|
|
10
|
-
| "mermaid"
|
|
11
|
-
| "table"
|
|
12
|
-
| "font"
|
|
13
|
-
| "document"
|
|
14
|
-
| "spreadsheet"
|
|
15
|
-
| "presentation"
|
|
16
|
-
| "text"
|
|
17
|
-
|
|
18
|
-
const officeKinds = new Map<string, ArtifactKind>([
|
|
19
|
-
["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document"],
|
|
20
|
-
["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet"],
|
|
21
|
-
["application/vnd.openxmlformats-officedocument.presentationml.presentation", "presentation"],
|
|
22
|
-
])
|
|
23
|
-
|
|
24
|
-
const mimes = new Map([
|
|
25
|
-
["png", "image/png"],
|
|
26
|
-
["jpg", "image/jpeg"],
|
|
27
|
-
["jpeg", "image/jpeg"],
|
|
28
|
-
["gif", "image/gif"],
|
|
29
|
-
["webp", "image/webp"],
|
|
30
|
-
["avif", "image/avif"],
|
|
31
|
-
["bmp", "image/bmp"],
|
|
32
|
-
["ico", "image/x-icon"],
|
|
33
|
-
["tif", "image/tiff"],
|
|
34
|
-
["tiff", "image/tiff"],
|
|
35
|
-
["heic", "image/heic"],
|
|
36
|
-
["svg", "image/svg+xml"],
|
|
37
|
-
["mp3", "audio/mpeg"],
|
|
38
|
-
["wav", "audio/wav"],
|
|
39
|
-
["ogg", "audio/ogg"],
|
|
40
|
-
["oga", "audio/ogg"],
|
|
41
|
-
["m4a", "audio/mp4"],
|
|
42
|
-
["aac", "audio/aac"],
|
|
43
|
-
["flac", "audio/flac"],
|
|
44
|
-
["opus", "audio/ogg"],
|
|
45
|
-
["weba", "audio/webm"],
|
|
46
|
-
["mp4", "video/mp4"],
|
|
47
|
-
["m4v", "video/mp4"],
|
|
48
|
-
["webm", "video/webm"],
|
|
49
|
-
["mov", "video/quicktime"],
|
|
50
|
-
["ogv", "video/ogg"],
|
|
51
|
-
["mkv", "video/x-matroska"],
|
|
52
|
-
["pdf", "application/pdf"],
|
|
53
|
-
["html", "text/html"],
|
|
54
|
-
["htm", "text/html"],
|
|
55
|
-
["md", "text/markdown"],
|
|
56
|
-
["markdown", "text/markdown"],
|
|
57
|
-
["mdx", "text/markdown"],
|
|
58
|
-
["mmd", "text/vnd.mermaid"],
|
|
59
|
-
["mermaid", "text/vnd.mermaid"],
|
|
60
|
-
["csv", "text/csv"],
|
|
61
|
-
["tsv", "text/tab-separated-values"],
|
|
62
|
-
["ttf", "font/ttf"],
|
|
63
|
-
["otf", "font/otf"],
|
|
64
|
-
["woff", "font/woff"],
|
|
65
|
-
["woff2", "font/woff2"],
|
|
66
|
-
["docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
|
67
|
-
["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
|
|
68
|
-
["pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
|
|
69
|
-
])
|
|
70
|
-
|
|
71
|
-
function artifactExtension(path: string) {
|
|
72
|
-
const name = path.split(/[\\/]/).pop() ?? ""
|
|
73
|
-
const index = name.lastIndexOf(".")
|
|
74
|
-
if (index <= 0) return ""
|
|
75
|
-
return name.slice(index + 1).toLowerCase()
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function artifactMime(path: string) {
|
|
79
|
-
return mimes.get(artifactExtension(path))
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function artifactKind(path: string): ArtifactKind {
|
|
83
|
-
const mime = artifactMime(path)
|
|
84
|
-
if (!mime) return "text"
|
|
85
|
-
if (mime === "image/svg+xml") return "svg"
|
|
86
|
-
if (mime === "application/pdf") return "pdf"
|
|
87
|
-
if (mime === "text/html") return "html"
|
|
88
|
-
if (mime === "text/markdown") return "markdown"
|
|
89
|
-
if (mime === "text/vnd.mermaid") return "mermaid"
|
|
90
|
-
if (mime === "text/csv" || mime === "text/tab-separated-values") return "table"
|
|
91
|
-
if (mime.startsWith("image/")) return "image"
|
|
92
|
-
if (mime.startsWith("audio/")) return "audio"
|
|
93
|
-
if (mime.startsWith("font/")) return "font"
|
|
94
|
-
return officeKinds.get(mime) ?? "video"
|
|
95
|
-
}
|
|
1
|
+
/** How the app shows a file, from its extension. */
|
|
2
|
+
export type ArtifactKind =
|
|
3
|
+
| "image"
|
|
4
|
+
| "svg"
|
|
5
|
+
| "audio"
|
|
6
|
+
| "video"
|
|
7
|
+
| "pdf"
|
|
8
|
+
| "html"
|
|
9
|
+
| "markdown"
|
|
10
|
+
| "mermaid"
|
|
11
|
+
| "table"
|
|
12
|
+
| "font"
|
|
13
|
+
| "document"
|
|
14
|
+
| "spreadsheet"
|
|
15
|
+
| "presentation"
|
|
16
|
+
| "text"
|
|
17
|
+
|
|
18
|
+
const officeKinds = new Map<string, ArtifactKind>([
|
|
19
|
+
["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document"],
|
|
20
|
+
["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "spreadsheet"],
|
|
21
|
+
["application/vnd.openxmlformats-officedocument.presentationml.presentation", "presentation"],
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
const mimes = new Map([
|
|
25
|
+
["png", "image/png"],
|
|
26
|
+
["jpg", "image/jpeg"],
|
|
27
|
+
["jpeg", "image/jpeg"],
|
|
28
|
+
["gif", "image/gif"],
|
|
29
|
+
["webp", "image/webp"],
|
|
30
|
+
["avif", "image/avif"],
|
|
31
|
+
["bmp", "image/bmp"],
|
|
32
|
+
["ico", "image/x-icon"],
|
|
33
|
+
["tif", "image/tiff"],
|
|
34
|
+
["tiff", "image/tiff"],
|
|
35
|
+
["heic", "image/heic"],
|
|
36
|
+
["svg", "image/svg+xml"],
|
|
37
|
+
["mp3", "audio/mpeg"],
|
|
38
|
+
["wav", "audio/wav"],
|
|
39
|
+
["ogg", "audio/ogg"],
|
|
40
|
+
["oga", "audio/ogg"],
|
|
41
|
+
["m4a", "audio/mp4"],
|
|
42
|
+
["aac", "audio/aac"],
|
|
43
|
+
["flac", "audio/flac"],
|
|
44
|
+
["opus", "audio/ogg"],
|
|
45
|
+
["weba", "audio/webm"],
|
|
46
|
+
["mp4", "video/mp4"],
|
|
47
|
+
["m4v", "video/mp4"],
|
|
48
|
+
["webm", "video/webm"],
|
|
49
|
+
["mov", "video/quicktime"],
|
|
50
|
+
["ogv", "video/ogg"],
|
|
51
|
+
["mkv", "video/x-matroska"],
|
|
52
|
+
["pdf", "application/pdf"],
|
|
53
|
+
["html", "text/html"],
|
|
54
|
+
["htm", "text/html"],
|
|
55
|
+
["md", "text/markdown"],
|
|
56
|
+
["markdown", "text/markdown"],
|
|
57
|
+
["mdx", "text/markdown"],
|
|
58
|
+
["mmd", "text/vnd.mermaid"],
|
|
59
|
+
["mermaid", "text/vnd.mermaid"],
|
|
60
|
+
["csv", "text/csv"],
|
|
61
|
+
["tsv", "text/tab-separated-values"],
|
|
62
|
+
["ttf", "font/ttf"],
|
|
63
|
+
["otf", "font/otf"],
|
|
64
|
+
["woff", "font/woff"],
|
|
65
|
+
["woff2", "font/woff2"],
|
|
66
|
+
["docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"],
|
|
67
|
+
["xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],
|
|
68
|
+
["pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"],
|
|
69
|
+
])
|
|
70
|
+
|
|
71
|
+
function artifactExtension(path: string) {
|
|
72
|
+
const name = path.split(/[\\/]/).pop() ?? ""
|
|
73
|
+
const index = name.lastIndexOf(".")
|
|
74
|
+
if (index <= 0) return ""
|
|
75
|
+
return name.slice(index + 1).toLowerCase()
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function artifactMime(path: string) {
|
|
79
|
+
return mimes.get(artifactExtension(path))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function artifactKind(path: string): ArtifactKind {
|
|
83
|
+
const mime = artifactMime(path)
|
|
84
|
+
if (!mime) return "text"
|
|
85
|
+
if (mime === "image/svg+xml") return "svg"
|
|
86
|
+
if (mime === "application/pdf") return "pdf"
|
|
87
|
+
if (mime === "text/html") return "html"
|
|
88
|
+
if (mime === "text/markdown") return "markdown"
|
|
89
|
+
if (mime === "text/vnd.mermaid") return "mermaid"
|
|
90
|
+
if (mime === "text/csv" || mime === "text/tab-separated-values") return "table"
|
|
91
|
+
if (mime.startsWith("image/")) return "image"
|
|
92
|
+
if (mime.startsWith("audio/")) return "audio"
|
|
93
|
+
if (mime.startsWith("font/")) return "font"
|
|
94
|
+
return officeKinds.get(mime) ?? "video"
|
|
95
|
+
}
|
package/src/base64.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
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: (() => string) | undefined = Uint8Array.prototype.toBase64
|
|
4
|
-
const nativeDecode: ((value: string) => Uint8Array) | undefined = Uint8Array.fromBase64
|
|
5
|
-
|
|
6
|
-
/** Encodes bytes as standard base64. */
|
|
7
|
-
export function bytesToBase64(bytes: Uint8Array) {
|
|
8
|
-
if (nativeEncode) return bytes.toBase64()
|
|
9
|
-
|
|
10
|
-
const parts: string[] = []
|
|
11
|
-
|
|
12
|
-
for (let index = 0; index < bytes.length; index += 0x8000) {
|
|
13
|
-
parts.push(String.fromCharCode(...bytes.subarray(index, index + 0x8000)))
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return btoa(parts.join(""))
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Decodes standard base64 into bytes. */
|
|
20
|
-
export function base64ToBytes(value: string) {
|
|
21
|
-
if (nativeDecode) return Uint8Array.fromBase64(value)
|
|
22
|
-
|
|
23
|
-
const binary = atob(value)
|
|
24
|
-
const bytes = new Uint8Array(binary.length)
|
|
25
|
-
|
|
26
|
-
for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index)
|
|
27
|
-
|
|
28
|
-
return bytes
|
|
29
|
-
}
|
|
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: (() => string) | undefined = Uint8Array.prototype.toBase64
|
|
4
|
+
const nativeDecode: ((value: string) => Uint8Array) | undefined = Uint8Array.fromBase64
|
|
5
|
+
|
|
6
|
+
/** Encodes bytes as standard base64. */
|
|
7
|
+
export function bytesToBase64(bytes: Uint8Array) {
|
|
8
|
+
if (nativeEncode) return bytes.toBase64()
|
|
9
|
+
|
|
10
|
+
const parts: string[] = []
|
|
11
|
+
|
|
12
|
+
for (let index = 0; index < bytes.length; index += 0x8000) {
|
|
13
|
+
parts.push(String.fromCharCode(...bytes.subarray(index, index + 0x8000)))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return btoa(parts.join(""))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Decodes standard base64 into bytes. */
|
|
20
|
+
export function base64ToBytes(value: string) {
|
|
21
|
+
if (nativeDecode) return Uint8Array.fromBase64(value)
|
|
22
|
+
|
|
23
|
+
const binary = atob(value)
|
|
24
|
+
const bytes = new Uint8Array(binary.length)
|
|
25
|
+
|
|
26
|
+
for (let index = 0; index < binary.length; index++) bytes[index] = binary.charCodeAt(index)
|
|
27
|
+
|
|
28
|
+
return bytes
|
|
29
|
+
}
|
package/src/binary.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
export namespace Binary {
|
|
2
|
-
export function search<T>(array: T[], id: string, compare: (item: T) => string) {
|
|
3
|
-
let left = 0
|
|
4
|
-
let right = array.length - 1
|
|
5
|
-
|
|
6
|
-
while (left <= right) {
|
|
7
|
-
const middle = Math.floor((left + right) / 2)
|
|
8
|
-
const value = compare(array[middle])
|
|
9
|
-
if (value === id) return { found: true, index: middle }
|
|
10
|
-
if (value < id) left = middle + 1
|
|
11
|
-
else right = middle - 1
|
|
12
|
-
}
|
|
13
|
-
return { found: false, index: left }
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function insert<T>(array: T[], item: T, compare: (item: T) => string) {
|
|
17
|
-
const id = compare(item)
|
|
18
|
-
let left = 0
|
|
19
|
-
let right = array.length
|
|
20
|
-
|
|
21
|
-
while (left < right) {
|
|
22
|
-
const middle = Math.floor((left + right) / 2)
|
|
23
|
-
if (compare(array[middle]) < id) left = middle + 1
|
|
24
|
-
else right = middle
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
array.splice(left, 0, item)
|
|
28
|
-
return array
|
|
29
|
-
}
|
|
30
|
-
}
|
|
1
|
+
export namespace Binary {
|
|
2
|
+
export function search<T>(array: T[], id: string, compare: (item: T) => string) {
|
|
3
|
+
let left = 0
|
|
4
|
+
let right = array.length - 1
|
|
5
|
+
|
|
6
|
+
while (left <= right) {
|
|
7
|
+
const middle = Math.floor((left + right) / 2)
|
|
8
|
+
const value = compare(array[middle])
|
|
9
|
+
if (value === id) return { found: true, index: middle }
|
|
10
|
+
if (value < id) left = middle + 1
|
|
11
|
+
else right = middle - 1
|
|
12
|
+
}
|
|
13
|
+
return { found: false, index: left }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function insert<T>(array: T[], item: T, compare: (item: T) => string) {
|
|
17
|
+
const id = compare(item)
|
|
18
|
+
let left = 0
|
|
19
|
+
let right = array.length
|
|
20
|
+
|
|
21
|
+
while (left < right) {
|
|
22
|
+
const middle = Math.floor((left + right) / 2)
|
|
23
|
+
if (compare(array[middle]) < id) left = middle + 1
|
|
24
|
+
else right = middle
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
array.splice(left, 0, item)
|
|
28
|
+
return array
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/bom.ts
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
export * as Bom from "./bom.js"
|
|
2
|
-
|
|
3
|
-
import { Effect } from "effect"
|
|
4
|
-
import type { FSUtil } from "./fs-util.js"
|
|
5
|
-
|
|
6
|
-
const code = 0xfeff
|
|
7
|
-
const value = String.fromCharCode(code)
|
|
8
|
-
|
|
9
|
-
export function split(text: string) {
|
|
10
|
-
const stripped = text.replace(/^\uFEFF+/, "")
|
|
11
|
-
return { bom: stripped.length !== text.length, text: stripped }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function join(text: string, bom: boolean) {
|
|
15
|
-
const stripped = split(text).text
|
|
16
|
-
return bom ? value + stripped : stripped
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function has(content: Uint8Array) {
|
|
20
|
-
return content[0] === 0xef && content[1] === 0xbb && content[2] === 0xbf
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function decodeBytes(content: Uint8Array) {
|
|
24
|
-
return split(decode(content))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function syncBytes(content: Uint8Array, bom: boolean) {
|
|
28
|
-
const decoded = decode(content)
|
|
29
|
-
const current = split(decoded)
|
|
30
|
-
const canonical = bom ? value + current.text : current.text
|
|
31
|
-
return { text: current.text, bytes: decoded === canonical ? undefined : new TextEncoder().encode(canonical) }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export const readFile = Effect.fn("Bom.readFile")(function* (fs: FSUtil.Interface, filepath: string) {
|
|
35
|
-
return decodeBytes(yield* fs.readFile(filepath))
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
export const syncFile = Effect.fn("Bom.syncFile")(function* (fs: FSUtil.Interface, filepath: string, bom: boolean) {
|
|
39
|
-
const synced = syncBytes(yield* fs.readFile(filepath), bom)
|
|
40
|
-
if (synced.bytes) yield* fs.writeWithDirs(filepath, synced.bytes)
|
|
41
|
-
return synced.text
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
function decode(content: Uint8Array) {
|
|
45
|
-
return new TextDecoder("utf-8", { ignoreBOM: true }).decode(content)
|
|
46
|
-
}
|
|
1
|
+
export * as Bom from "./bom.js"
|
|
2
|
+
|
|
3
|
+
import { Effect } from "effect"
|
|
4
|
+
import type { FSUtil } from "./fs-util.js"
|
|
5
|
+
|
|
6
|
+
const code = 0xfeff
|
|
7
|
+
const value = String.fromCharCode(code)
|
|
8
|
+
|
|
9
|
+
export function split(text: string) {
|
|
10
|
+
const stripped = text.replace(/^\uFEFF+/, "")
|
|
11
|
+
return { bom: stripped.length !== text.length, text: stripped }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function join(text: string, bom: boolean) {
|
|
15
|
+
const stripped = split(text).text
|
|
16
|
+
return bom ? value + stripped : stripped
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function has(content: Uint8Array) {
|
|
20
|
+
return content[0] === 0xef && content[1] === 0xbb && content[2] === 0xbf
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function decodeBytes(content: Uint8Array) {
|
|
24
|
+
return split(decode(content))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function syncBytes(content: Uint8Array, bom: boolean) {
|
|
28
|
+
const decoded = decode(content)
|
|
29
|
+
const current = split(decoded)
|
|
30
|
+
const canonical = bom ? value + current.text : current.text
|
|
31
|
+
return { text: current.text, bytes: decoded === canonical ? undefined : new TextEncoder().encode(canonical) }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const readFile = Effect.fn("Bom.readFile")(function* (fs: FSUtil.Interface, filepath: string) {
|
|
35
|
+
return decodeBytes(yield* fs.readFile(filepath))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export const syncFile = Effect.fn("Bom.syncFile")(function* (fs: FSUtil.Interface, filepath: string, bom: boolean) {
|
|
39
|
+
const synced = syncBytes(yield* fs.readFile(filepath), bom)
|
|
40
|
+
if (synced.bytes) yield* fs.writeWithDirs(filepath, synced.bytes)
|
|
41
|
+
return synced.text
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
function decode(content: Uint8Array) {
|
|
45
|
+
return new TextDecoder("utf-8", { ignoreBOM: true }).decode(content)
|
|
46
|
+
}
|