Skip to content

Commit 38cd517

Browse files
authored
Merge pull request #1169 from DavidIsa/rm-tmp-promise
Remove tmp-promise
2 parents 9e8d36f + f9d127f commit 38cd517

File tree

4 files changed

+65
-63
lines changed

4 files changed

+65
-63
lines changed

package-lock.json

Lines changed: 35 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"rfc4648": "^1.3.0",
6767
"stream-buffers": "^3.0.2",
6868
"tar": "^6.1.11",
69-
"tmp-promise": "^3.0.2",
7069
"tslib": "^2.4.1",
7170
"ws": "^8.11.0"
7271
},

src/cp.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as fs from 'fs';
22
import { WritableStreamBuffer } from 'stream-buffers';
33
import * as tar from 'tar';
4-
import * as tmp from 'tmp-promise';
5-
64
import { KubeConfig } from './config';
75
import { Exec } from './exec';
6+
import { generateTmpFileName } from './util';
87

98
export class Cp {
109
public execInstance: Exec;
@@ -18,7 +17,7 @@ export class Cp {
1817
* @param {string} containerName - The name of the container in the pod to exec the command inside.
1918
* @param {string} srcPath - The source path in the pod
2019
* @param {string} tgtPath - The target path in local
21-
* @param {string} cwd - The directory that is used as the parent in the pod when downloading
20+
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
2221
*/
2322
public async cpFromPod(
2423
namespace: string,
@@ -28,8 +27,7 @@ export class Cp {
2827
tgtPath: string,
2928
cwd?: string,
3029
): Promise<void> {
31-
const tmpFile = tmp.fileSync();
32-
const tmpFileName = tmpFile.name;
30+
const tmpFileName = await generateTmpFileName();
3331
const command = ['tar', 'zcf', '-'];
3432
if (cwd) {
3533
command.push('-C', cwd);
@@ -65,7 +63,7 @@ export class Cp {
6563
* @param {string} containerName - The name of the container in the pod to exec the command inside.
6664
* @param {string} srcPath - The source path in local
6765
* @param {string} tgtPath - The target path in the pod
68-
* @param {string} cwd - The directory that is used as the parent in the host when uploading
66+
* @param {string} [cwd] - The directory that is used as the parent in the host when uploading
6967
*/
7068
public async cpToPod(
7169
namespace: string,
@@ -75,16 +73,9 @@ export class Cp {
7573
tgtPath: string,
7674
cwd?: string,
7775
): Promise<void> {
78-
const tmpFile = tmp.fileSync();
79-
const tmpFileName = tmpFile.name;
76+
const tmpFileName = await generateTmpFileName();
8077
const command = ['tar', 'xf', '-', '-C', tgtPath];
81-
await tar.c(
82-
{
83-
file: tmpFile.name,
84-
cwd,
85-
},
86-
[srcPath],
87-
);
78+
await tar.c({ file: tmpFileName, cwd }, [srcPath]);
8879
const readStream = fs.createReadStream(tmpFileName);
8980
const errStream = new WritableStreamBuffer();
9081
this.execInstance.exec(

src/util.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { tmpdir } from 'os';
2+
import { randomUUID } from 'crypto';
3+
import { promises as fs, constants as fsConstants } from 'fs';
14
import { CoreV1Api, V1Container, V1Pod } from './gen/api';
25

36
export async function podsForNode(api: CoreV1Api, nodeName: string): Promise<V1Pod[]> {
@@ -142,3 +145,24 @@ export function totalForResource(pod: V1Pod, resource: string): ResourceStatus {
142145
});
143146
return new ResourceStatus(reqTotal, limitTotal, resource);
144147
}
148+
149+
export async function generateTmpFileName(): Promise<string> {
150+
let tmpFileName: string;
151+
152+
let i = 0;
153+
do {
154+
tmpFileName = `${tmpdir()}/${randomUUID()}`;
155+
156+
try {
157+
await fs.access(tmpFileName, fsConstants.W_OK);
158+
159+
console.warn('Tmp file already exists');
160+
} catch (err) {
161+
return tmpFileName;
162+
}
163+
164+
i++;
165+
} while (i < 10);
166+
167+
throw new Error('Cannot generate tmp file name');
168+
}

0 commit comments

Comments
 (0)