Repository navigation
Expand file tree
/
Copy pathbuild-plugins.ts
More file actions
128 lines (113 loc) · 4.58 KB
/
Copy pathbuild-plugins.ts
File metadata and controls
128 lines (113 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Bundler plugins for the production build. They rewrite a dependency's
* source while it is bundled into the executables, so the installed package
* — and the published one, which carries no build — stays untouched.
*/
const stringWidthDeclaration =
"export default function stringWidth(input, options = {}) {";
/**
* The `string-width` sources the rewrite has been verified against, by the
* SHA-256 of `index.js`. The rewrite repeats the module's preparation (ANSI
* stripping, the printable-ASCII fast path) and relies on its width being a
* sum of independent per-cluster widths; a release can change either while
* keeping every literal the rewrite edits. So any other source fails the
* build until test/build-plugins.test.ts has shown the rewrite exact for it
* and its hash is added here.
*/
export const verifiedStringWidthSources: ReadonlyMap<string, string> = new Map([
["60c197f20cc966a2ede2dfec1ad4b764aa4095a43d225792b2652bca6afeb21b", "8.2.2"],
["abc20775da6b552188b098688ebec671e29f5416d6339d3534bb7b016174003e", "8.3.0"],
]);
/**
* Cache keys start with this, one per `ambiguousIsNarrow` value. It is also
* how a test recognises the rewrite in a minified executable.
*/
export const clusterKeyPrefix = "tx-string-width-cluster";
/**
* `string-width`'s source, measuring each distinct grapheme cluster once.
*
* The module sums a width per grapheme cluster, and each cluster's width
* depends only on the cluster and `ambiguousIsNarrow`. Measuring one costs a
* `v`-flag `\p{RGI_Emoji}` test that Bun runs about a thousand times slower
* than other engines, and Ink measures every visible row on every frame — the
* same few clusters (box drawing, letters) over and over. The rewrite keeps
* the module's function as it is, renamed, and puts in front of it the same
* preparation (ANSI stripping, the printable-ASCII fast path) followed by a
* per-cluster sum that asks the module's own function about each cluster it
* has not measured yet. The results are the module's own; only repeated work
* is skipped. The cache is bounded and starts over when full.
*
* Applies to any source with the declaration it renames; the production build
* uses {@link cachedStringWidthSource}, which accepts only verified sources.
*/
export function rewriteStringWidthSource(source: string): string {
return `${source.replace(
stringWidthDeclaration,
"function stringWidthOfCluster(input, options = {}) {",
)}
const clusterWidths = new Map();
export default function stringWidth(input, options = {}) {
if (typeof input !== 'string' || input.length === 0) {
return 0;
}
const {
ambiguousIsNarrow = true,
countAnsiEscapeCodes = false,
} = options;
let string = input;
if (!countAnsiEscapeCodes && (string.includes('\\u001B') || string.includes('\\u009B'))) {
string = stripAnsi(string);
}
if (string.length === 0) {
return 0;
}
if (/^[\\u0020-\\u007E]*$/.test(string)) {
return string.length;
}
// The string is already stripped as asked, so each cluster is measured as it stands.
const clusterOptions = {ambiguousIsNarrow, countAnsiEscapeCodes: true};
const prefix = ambiguousIsNarrow ? '${clusterKeyPrefix}:narrow:' : '${clusterKeyPrefix}:wide:';
let width = 0;
for (const {segment} of segmenter.segment(string)) {
const key = prefix + segment;
let clusterWidth = clusterWidths.get(key);
if (clusterWidth === undefined) {
clusterWidth = stringWidthOfCluster(segment, clusterOptions);
if (clusterWidths.size >= 4096) {
clusterWidths.clear();
}
clusterWidths.set(key, clusterWidth);
}
width += clusterWidth;
}
return width;
}
`;
}
/**
* {@link rewriteStringWidthSource} for a source in
* {@link verifiedStringWidthSources} only, so a dependency update cannot
* silently change what the rewrite relies on.
*/
export function cachedStringWidthSource(source: string): string {
const hash = new Bun.CryptoHasher("sha256").update(source).digest("hex");
if (!verifiedStringWidthSources.has(hash)) {
throw new Error(
`string-width source ${hash} is not one the cached-width rewrite was verified against; verify it with test/build-plugins.test.ts and add its hash to verifiedStringWidthSources`,
);
}
return rewriteStringWidthSource(source);
}
/** Bundle every `string-width` copy with {@link cachedStringWidthSource}. */
export const cachedStringWidth: Bun.BunPlugin = {
name: "cached-string-width",
setup(builder) {
builder.onLoad(
{ filter: /[/\\]string-width[/\\]index\.js$/ },
async ({ path }) => ({
contents: cachedStringWidthSource(await Bun.file(path).text()),
loader: "js",
}),
);
},
};