-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathfiles.ts
More file actions
34 lines (30 loc) · 905 Bytes
/
files.ts
File metadata and controls
34 lines (30 loc) · 905 Bytes
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
export async function saveTemporaryFile(
data: Uint8Array<ArrayBufferLike>,
filename: string,
): Promise<{filepath: string}> {
try {
const dir = await fs.mkdtemp(
path.join(os.tmpdir(), 'chrome-devtools-mcp-'),
);
const filepath = path.join(dir, filename);
await fs.writeFile(filepath, data);
return {filepath};
} catch (err) {
throw new Error('Could not save a file', {cause: err});
}
}
export function ensureExtension(filepath: string, extension: string): string {
const normalizedExtension = extension.startsWith('.')
? extension
: `.${extension}`;
const ext = path.extname(filepath);
return filepath.slice(0, filepath.length - ext.length) + normalizedExtension;
}