Repository navigation
Expand file tree
/
Copy pathIpcFixtureFile.ts
More file actions
50 lines (46 loc) · 1.7 KB
/
Copy pathIpcFixtureFile.ts
File metadata and controls
50 lines (46 loc) · 1.7 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { randomUUID } from 'node:crypto';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { setTimeout as delayAsync } from 'node:timers/promises';
export function writeIpcFixtureFile(filename: string, contents: string): void {
const temporary: string = path.join(
path.dirname(filename),
`.${path.basename(filename)}-${process.pid}-${randomUUID()}.tmp`
);
const descriptor: number = fs.openSync(temporary, 'wx', 0o600);
try {
try {
fs.writeFileSync(descriptor, contents, 'utf8');
} finally {
fs.closeSync(descriptor);
}
fs.renameSync(temporary, filename);
} finally {
fs.rmSync(temporary, { force: true });
}
}
export function readIpcFixtureEvents<T>(filename: string): T[] {
const contents: string = fs.readFileSync(filename, 'utf8');
// A concurrent append is not published until its terminating newline is visible.
return contents
.slice(0, contents.lastIndexOf('\n') + 1)
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line));
}
export async function readPressureGateAsync(
filename: string,
deadline: number
): Promise<{ additionalMemoryBytes?: number; cancelled?: boolean }> {
while (!fs.existsSync(filename)) {
if (performance.now() >= deadline) throw new Error('Pressure fixture gate was not released.');
await delayAsync(10);
}
const gate: { additionalMemoryBytes?: number; cancelled?: boolean } = JSON.parse(
fs.readFileSync(filename, 'utf8')
);
if (gate.cancelled) throw new Error('Pressure fixture setup was cancelled.');
return gate;
}