-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.ts
More file actions
373 lines (331 loc) · 11.1 KB
/
edit.ts
File metadata and controls
373 lines (331 loc) · 11.1 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* @fileoverview Editable JSON file manipulation with formatting preservation.
*/
import process from 'node:process'
import { setTimeout as sleep } from 'node:timers/promises'
import {
INDENT_SYMBOL,
NEWLINE_SYMBOL,
detectIndent,
detectNewline,
getFormattingFromContent,
shouldSave as shouldSaveUtil,
sortKeys,
stringifyWithFormatting,
stripFormattingSymbols,
} from './format'
import type {
EditableJsonConstructor,
EditableJsonInstance,
EditableJsonOptions,
EditableJsonSaveOptions,
} from './types'
const identSymbol = INDENT_SYMBOL
const newlineSymbol = NEWLINE_SYMBOL
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
// `exports.SomeName = void 0;` which causes runtime errors.
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
const JSONParse = JSON.parse
let _EditableJsonClass: EditableJsonConstructor | undefined
let _fs: typeof import('node:fs') | undefined
/*@__NO_SIDE_EFFECTS__*/
function getFs() {
if (_fs === undefined) {
// Use non-'node:' prefixed require to avoid Webpack errors.
_fs = /*@__PURE__*/ require('node:fs')
}
return _fs as typeof import('node:fs')
}
/**
* Parse JSON content and extract formatting metadata.
* @private
*/
function parseJson(content: string): unknown {
return JSONParse(content)
}
/**
* Read file content from disk with retry logic for ENOENT errors.
* @private
*/
async function readFile(filepath: string): Promise<string> {
const { promises: fsPromises } = getFs()
// Retry on ENOENT since files may not be immediately accessible after writes
// Windows needs more retries due to slower filesystem operations
const maxRetries = process.platform === 'win32' ? 5 : 1
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
// eslint-disable-next-line no-await-in-loop
return await fsPromises.readFile(filepath, 'utf8')
} catch (err) {
const isLastAttempt = attempt === maxRetries
const isEnoent =
err instanceof Error && 'code' in err && err.code === 'ENOENT'
// Only retry ENOENT and not on last attempt
if (!isEnoent || isLastAttempt) {
throw err
}
// Wait before retry with exponential backoff
// Windows: 50ms, 100ms, 150ms, 200ms, 250ms (total 750ms + attempts)
// Others: 20ms
const delay = process.platform === 'win32' ? 50 * (attempt + 1) : 20
// eslint-disable-next-line no-await-in-loop
await sleep(delay)
}
}
// This line should never be reached but TypeScript requires it
throw new Error('Unreachable code')
}
/**
* Retry a file write operation with exponential backoff on Windows EPERM errors.
* Windows can have transient file locking issues with temp directories.
* @private
*/
async function retryWrite(
filepath: string,
content: string,
retries = 3,
baseDelay = 10,
): Promise<void> {
const fs = getFs()
const { promises: fsPromises } = fs
for (let attempt = 0; attempt <= retries; attempt++) {
try {
// eslint-disable-next-line no-await-in-loop
await fsPromises.writeFile(filepath, content)
// On Windows, add a delay and verify file exists to ensure it's fully flushed
// This prevents ENOENT errors when immediately reading after write
// Windows CI runners are significantly slower than local development
if (process.platform === 'win32') {
// Initial delay to allow OS to flush the write
// eslint-disable-next-line no-await-in-loop
await sleep(50)
// Verify the file is actually present with retries
let accessRetries = 0
const maxAccessRetries = 5
while (accessRetries < maxAccessRetries) {
if (fs.existsSync(filepath)) {
// Small final delay to ensure stability
// eslint-disable-next-line no-await-in-loop
await sleep(10)
break
}
// If file isn't present yet, wait with increasing delays
const delay = 20 * (accessRetries + 1)
// eslint-disable-next-line no-await-in-loop
await sleep(delay)
accessRetries++
}
}
return
} catch (err) {
const isLastAttempt = attempt === retries
const isRetriableError =
err instanceof Error &&
'code' in err &&
(err.code === 'EPERM' || err.code === 'EBUSY' || err.code === 'ENOENT')
// Only retry on Windows file system errors (EPERM/EBUSY/ENOENT), and not on the last attempt
if (!isRetriableError || isLastAttempt) {
throw err
}
// Exponential backoff: 10ms, 20ms, 40ms
const delay = baseDelay * 2 ** attempt
// eslint-disable-next-line no-await-in-loop
await sleep(delay)
}
}
}
/**
* Get the EditableJson class for JSON file manipulation.
*
* @example
* ```ts
* import { getEditableJsonClass } from '@socketsecurity/lib/json/edit'
*
* const EditableJson = getEditableJsonClass<MyConfigType>()
* const config = await EditableJson.load('./config.json')
* config.update({ someField: 'newValue' })
* await config.save({ sort: true })
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function getEditableJsonClass<
T = Record<string, unknown>,
>(): EditableJsonConstructor<T> {
if (_EditableJsonClass === undefined) {
_EditableJsonClass = class EditableJson<
T = Record<string, unknown>,
> implements EditableJsonInstance<T> {
_canSave = true
_content: T = {} as T
_path: string | undefined = undefined
_readFileContent = ''
_readFileJson: unknown = undefined
get content(): Readonly<T> {
return this._content
}
get filename(): string {
const path = this._path
if (!path) {
return ''
}
return path
}
get path(): string | undefined {
return this._path
}
static async create<T = Record<string, unknown>>(
path: string,
opts: EditableJsonOptions<T> = {},
): Promise<EditableJsonInstance<T>> {
const instance = new EditableJson<T>()
instance.create(path)
return opts.data ? instance.update(opts.data) : instance
}
static async load<T = Record<string, unknown>>(
path: string,
opts: EditableJsonOptions<T> = {},
): Promise<EditableJsonInstance<T>> {
const instance = new EditableJson<T>()
// Avoid try/catch if we aren't going to create
if (!opts.create) {
return await instance.load(path)
}
try {
return await instance.load(path)
} catch (err: unknown) {
if (
!(err as Error).message.includes('ENOENT') &&
!(err as Error).message.includes('no such file')
) {
throw err
}
return instance.create(path)
}
}
create(path: string): this {
this._path = path
this._content = {} as T
this._canSave = true
return this
}
fromContent(data: unknown): this {
this._content = data as T
this._canSave = false
return this
}
fromJSON(data: string): this {
const parsed = parseJson(data) as T & Record<symbol, unknown>
// Extract and preserve formatting metadata
const indent = detectIndent(data)
const newline = detectNewline(data)
// Store formatting metadata using symbols
;(parsed as Record<symbol, unknown>)[identSymbol] = indent
;(parsed as Record<symbol, unknown>)[newlineSymbol] = newline
this._content = parsed as T
return this
}
async load(path: string, create?: boolean): Promise<this> {
this._path = path
let parseErr: unknown
try {
this._readFileContent = await readFile(this.filename)
} catch (err) {
if (!create) {
throw err
}
parseErr = err
}
if (parseErr) {
throw parseErr
}
this.fromJSON(this._readFileContent)
// Add AFTER fromJSON is called in case it errors.
this._readFileJson = parseJson(this._readFileContent)
return this
}
update(content: Partial<T>): this {
this._content = {
...this._content,
...content,
} as T
return this
}
async save(options?: EditableJsonSaveOptions): Promise<boolean> {
if (!this._canSave || this.content === undefined) {
throw new Error('No file path to save to')
}
// Check if save is needed
if (
!shouldSaveUtil(
this.content as Record<string | symbol, unknown>,
this._readFileJson as Record<string | symbol, unknown>,
this._readFileContent,
options,
)
) {
return false
}
// Get content and formatting
const content = stripFormattingSymbols(
this.content as Record<string | symbol, unknown>,
)
const sortedContent = options?.sort ? sortKeys(content) : content
const formatting = getFormattingFromContent(
this.content as Record<string | symbol, unknown>,
)
// Generate file content
const fileContent = stringifyWithFormatting(sortedContent, formatting)
// Save to disk with retry logic for Windows file locking issues
await retryWrite(this.filename, fileContent)
this._readFileContent = fileContent
this._readFileJson = parseJson(fileContent)
return true
}
saveSync(options?: EditableJsonSaveOptions): boolean {
if (!this._canSave || this.content === undefined) {
throw new Error('No file path to save to')
}
// Check if save is needed
if (
!shouldSaveUtil(
this.content as Record<string | symbol, unknown>,
this._readFileJson as Record<string | symbol, unknown>,
this._readFileContent,
options,
)
) {
return false
}
// Get content and formatting
const content = stripFormattingSymbols(
this.content as Record<string | symbol, unknown>,
)
const sortedContent = options?.sort ? sortKeys(content) : content
const formatting = getFormattingFromContent(
this.content as Record<string | symbol, unknown>,
)
// Generate file content
const fileContent = stringifyWithFormatting(sortedContent, formatting)
// Save to disk
const fs = getFs()
fs.writeFileSync(this.filename, fileContent)
this._readFileContent = fileContent
this._readFileJson = parseJson(fileContent)
return true
}
willSave(options?: EditableJsonSaveOptions): boolean {
if (!this._canSave || this.content === undefined) {
return false
}
return shouldSaveUtil(
this.content as Record<string | symbol, unknown>,
this._readFileJson as Record<string | symbol, unknown>,
this._readFileContent,
options,
)
}
} as EditableJsonConstructor
}
return _EditableJsonClass as EditableJsonConstructor<T>
}