-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathpaste.ts
More file actions
254 lines (229 loc) · 8.01 KB
/
Copy pathpaste.ts
File metadata and controls
254 lines (229 loc) · 8.01 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
export const PASTE_LIMITS = {
/** Crash-only fallback for controls without a more specific downstream contract. */
DEFAULT_BYTES: 32 * 1024 * 1024,
/** Matches the existing collaborative-document seed boundary. */
RICH_MARKDOWN_BYTES: 5 * 1024 * 1024,
/** Matches the inline workspace-file content boundary. */
TEXT_EDITOR_BYTES: 50 * 1024 * 1024,
/** Matches the deployed chat request contract. */
CHAT_CHARACTERS: 1_000_000,
/** A Unicode scalar can occupy at most four UTF-8 bytes. */
CHAT_BYTES: 4_000_000,
/** Crash-only bound; admitted input is streamed to the PTY in 64 KiB chunks. */
TERMINAL_BYTES: 8 * 1024 * 1024,
/** The server's row ceiling remains the primary table bound. */
STRUCTURED_BYTES: 32 * 1024 * 1024,
} as const
export const PASTE_RENDER_THRESHOLDS = {
/** Above this size, skip decorative parsing and render a native text surface. */
ENHANCED_TEXT_CHARACTERS: 256 * 1024,
} as const
export interface TextPasteAdmissionInput {
pastedText: string
/** Maximum UTF-8 bytes allowed in the clipboard payload itself. */
maxPastedBytes?: number
/** Maximum UTF-16 code units allowed in the clipboard payload itself. */
maxPastedCharacters?: number
/** Existing value when the projected post-paste value must also be bounded. */
currentText?: string
/** Selection offsets within {@link currentText}. */
selectionStart?: number
selectionEnd?: number
/** Maximum UTF-8 bytes allowed after replacing the selection. */
maxResultBytes?: number
/** Maximum UTF-16 code units allowed after replacing the selection. */
maxResultCharacters?: number
}
export type TextPasteRejectionReason =
| 'pasted-bytes'
| 'pasted-characters'
| 'result-bytes'
| 'result-characters'
export type TextPasteAdmission =
| {
accepted: true
pastedBytes?: number
resultBytes?: number
resultCharacters?: number
}
| {
accepted: false
reason: TextPasteRejectionReason
actual: number
limit: number
}
/**
* Measures UTF-8 without allocating the second full-size buffer that `TextEncoder.encode()` creates.
* When `stopAfter` is supplied, the scan exits as soon as the caller already knows the value is too
* large. Lone UTF-16 surrogates match `TextEncoder` and count as the three-byte replacement scalar.
*/
export function utf8ByteLengthRange(
value: string,
start = 0,
end = value.length,
stopAfter = Number.POSITIVE_INFINITY
): number {
let bytes = 0
const safeStart = Math.min(Math.max(start, 0), value.length)
const safeEnd = Math.min(Math.max(end, safeStart), value.length)
for (let index = safeStart; index < safeEnd; index++) {
const code = value.charCodeAt(index)
if (code <= 0x7f) {
bytes += 1
} else if (code <= 0x7ff) {
bytes += 2
} else if (code >= 0xd800 && code <= 0xdbff) {
const next = value.charCodeAt(index + 1)
if (index + 1 < safeEnd && next >= 0xdc00 && next <= 0xdfff) {
bytes += 4
index += 1
} else {
bytes += 3
}
} else {
bytes += 3
}
if (bytes > stopAfter) return bytes
}
return bytes
}
export function utf8ByteLength(value: string, stopAfter = Number.POSITIVE_INFINITY): number {
return utf8ByteLengthRange(value, 0, value.length, stopAfter)
}
function isGuaranteedWithinUtf8Limit(characters: number, limit: number): boolean {
return characters <= Math.floor(limit / 3)
}
function normalizedSelection(input: TextPasteAdmissionInput): {
currentText: string
start: number
end: number
} {
const currentText = input.currentText ?? ''
const rawStart = input.selectionStart ?? currentText.length
const rawEnd = input.selectionEnd ?? rawStart
const start = Math.min(Math.max(Math.min(rawStart, rawEnd), 0), currentText.length)
const end = Math.min(Math.max(Math.max(rawStart, rawEnd), start), currentText.length)
return { currentText, start, end }
}
/**
* Applies the common text-paste policy before an editor parses, tokenizes, renders, or persists the
* payload. Character checks are constant-time. Byte scans short-circuit at the configured ceiling and
* never allocate a full encoded copy of the clipboard string.
*/
export function assessTextPaste(input: TextPasteAdmissionInput): TextPasteAdmission {
const { pastedText } = input
if (input.maxPastedCharacters !== undefined && pastedText.length > input.maxPastedCharacters) {
return {
accepted: false,
reason: 'pasted-characters',
actual: pastedText.length,
limit: input.maxPastedCharacters,
}
}
if (input.maxPastedBytes !== undefined && pastedText.length > input.maxPastedBytes) {
return {
accepted: false,
reason: 'pasted-bytes',
actual: pastedText.length,
limit: input.maxPastedBytes,
}
}
const projectsResult =
input.maxResultBytes !== undefined || input.maxResultCharacters !== undefined
if (
!projectsResult &&
input.maxPastedBytes !== undefined &&
isGuaranteedWithinUtf8Limit(pastedText.length, input.maxPastedBytes)
) {
return { accepted: true }
}
const selection = projectsResult ? normalizedSelection(input) : null
const resultCharacters = selection
? selection.currentText.length - (selection.end - selection.start) + pastedText.length
: undefined
if (
input.maxResultCharacters !== undefined &&
resultCharacters !== undefined &&
resultCharacters > input.maxResultCharacters
) {
return {
accepted: false,
reason: 'result-characters',
actual: resultCharacters,
limit: input.maxResultCharacters,
}
}
if (
input.maxResultBytes !== undefined &&
resultCharacters !== undefined &&
isGuaranteedWithinUtf8Limit(resultCharacters, input.maxResultBytes)
) {
return { accepted: true, resultCharacters }
}
const pastedByteLimit = Math.max(input.maxPastedBytes ?? 0, input.maxResultBytes ?? 0)
const pastedBytes = pastedByteLimit > 0 ? utf8ByteLength(pastedText, pastedByteLimit) : undefined
if (
input.maxPastedBytes !== undefined &&
pastedBytes !== undefined &&
pastedBytes > input.maxPastedBytes
) {
return {
accepted: false,
reason: 'pasted-bytes',
actual: pastedBytes,
limit: input.maxPastedBytes,
}
}
if (!projectsResult) {
return { accepted: true, pastedBytes }
}
const { currentText, start, end } = selection as ReturnType<typeof normalizedSelection>
if (input.maxResultBytes === undefined) {
return { accepted: true, pastedBytes, resultCharacters }
}
const prefixBytes = utf8ByteLengthRange(currentText, 0, start, input.maxResultBytes)
if (prefixBytes > input.maxResultBytes) {
return {
accepted: false,
reason: 'result-bytes',
actual: prefixBytes,
limit: input.maxResultBytes,
}
}
const suffixBudget = input.maxResultBytes - prefixBytes
const suffixBytes = utf8ByteLengthRange(currentText, end, currentText.length, suffixBudget)
const resultBytes = prefixBytes + suffixBytes + (pastedBytes ?? utf8ByteLength(pastedText))
if (resultBytes > input.maxResultBytes) {
return {
accepted: false,
reason: 'result-bytes',
actual: resultBytes,
limit: input.maxResultBytes,
}
}
return { accepted: true, pastedBytes, resultBytes, resultCharacters }
}
/** Counts logical rows without allocating the array produced by `split()`. */
export function countPasteRows(text: string, stopAfter = Number.POSITIVE_INFINITY): number {
if (!text) return 0
let rows = 1
for (let index = 0; index < text.length; index++) {
const code = text.charCodeAt(index)
if (code === 10) {
rows += 1
} else if (code === 13) {
rows += 1
if (text.charCodeAt(index + 1) === 10) index += 1
}
if (rows > stopAfter) return rows
}
return rows
}
export function formatPasteLimit(bytes: number): string {
if (bytes >= 1_000_000 && bytes % 1_000_000 === 0) return `${bytes / 1_000_000} MB`
if (bytes >= 1024 * 1024) {
const mebibytes = bytes / (1024 * 1024)
return `${Number.isInteger(mebibytes) ? mebibytes : mebibytes.toFixed(1)} MiB`
}
return `${Math.round(bytes / 1024)} KiB`
}