Skip to content

Commit b1640f9

Browse files
committed
chore: streaming secret to file
[ci skip]
1 parent 4415fbb commit b1640f9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/secrets/CommandEdit.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,21 @@ class CommandEdit extends CommandPolykey {
7474
metadata: auth,
7575
});
7676
await writer.close();
77-
let rawSecretContent: string = '';
77+
let fileWriter: fs.WriteStream | undefined = undefined;
7878
for await (const chunk of response.readable) {
7979
if (chunk.type === 'SuccessMessage') {
80-
rawSecretContent += chunk.secretContent;
80+
// Only create the file if we are getting data
81+
if (fileWriter == null) {
82+
fileWriter = fs.createWriteStream(tmpFile, { flags: 'a' });
83+
}
84+
// Wait for the internal buffer to drain if it is full
85+
if (
86+
!fileWriter.write(Buffer.from(chunk.secretContent, 'binary'))
87+
) {
88+
await new Promise((resolve) =>
89+
fileWriter!.once('drain', resolve),
90+
);
91+
}
8192
} else {
8293
if (chunk.code === 'ENOENT') {
8394
exists = false;
@@ -101,12 +112,8 @@ class CommandEdit extends CommandPolykey {
101112
}
102113
}
103114
}
104-
// Only make the temp file is the secret actually exists
105-
if (exists) {
106-
const secretContent = Buffer.from(rawSecretContent, 'binary');
107-
await this.fs.promises.writeFile(tmpFile, secretContent);
108-
return exists;
109-
}
115+
if (fileWriter != null) fileWriter.end();
116+
return exists;
110117
},
111118
meta,
112119
);

0 commit comments

Comments
 (0)