Skip to content

Commit 427b0cd

Browse files
committed
chore: enforced runtime type safety for some secrets commands
1 parent 04ad90a commit 427b0cd

File tree

4 files changed

+63
-46
lines changed

4 files changed

+63
-46
lines changed

src/secrets/CommandCat.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class CommandGet extends CommandPolykey {
2727
const { default: PolykeyClient } = await import(
2828
'polykey/dist/PolykeyClient'
2929
);
30+
const { never } = await import('polykey/dist/utils');
3031
const clientOptions = await binProcessors.processClientOptions(
3132
options.nodePath,
3233
options.nodeId,
@@ -95,27 +96,35 @@ class CommandGet extends CommandPolykey {
9596
// Print out incoming data to standard out
9697
let hasErrored = false;
9798
for await (const result of response.readable) {
98-
if (result.type === 'ErrorMessage') {
99-
hasErrored = true;
100-
switch (result.code) {
101-
case 'ENOENT':
102-
// Attempt to cat a non-existent file
103-
process.stderr.write(
104-
`cat: ${result.reason}: No such file or directory\n`,
105-
);
106-
break;
107-
case 'EISDIR':
108-
// Attempt to cat a directory
109-
process.stderr.write(
110-
`cat: ${result.reason}: Is a directory\n`,
111-
);
112-
break;
113-
default:
114-
// No other code should be thrown
115-
throw result;
116-
}
117-
} else {
118-
process.stdout.write(result.secretContent);
99+
const type = result.type;
100+
switch (type) {
101+
case 'ErrorMessage':
102+
hasErrored = true;
103+
switch (result.code) {
104+
case 'ENOENT':
105+
// Attempt to cat a non-existent file
106+
process.stderr.write(
107+
`cat: ${result.reason}: No such file or directory\n`,
108+
);
109+
break;
110+
case 'EISDIR':
111+
// Attempt to cat a directory
112+
process.stderr.write(
113+
`cat: ${result.reason}: Is a directory\n`,
114+
);
115+
break;
116+
default:
117+
// No other code should be thrown
118+
throw result;
119+
}
120+
break;
121+
case 'SuccessMessage':
122+
process.stdout.write(result.secretContent);
123+
break;
124+
default:
125+
never(
126+
`Expected "SuccessMessage" or "ContentMessage", got ${type}`,
127+
);
119128
}
120129
}
121130
return hasErrored;

src/secrets/CommandEdit.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CommandEdit extends CommandPolykey {
3030
const { default: PolykeyClient } = await import(
3131
'polykey/dist/PolykeyClient'
3232
);
33+
const { never } = await import('polykey/dist/utils');
3334
const clientOptions = await binProcessors.processClientOptions(
3435
options.nodePath,
3536
options.nodeId,
@@ -77,29 +78,35 @@ class CommandEdit extends CommandPolykey {
7778
const fd = await fs.promises.open(tmpFile, 'a');
7879
try {
7980
for await (const chunk of response.readable) {
80-
if (chunk.type === 'SuccessMessage') {
81-
exists = true;
82-
await fd.write(Buffer.from(chunk.secretContent, 'binary'));
83-
} else {
84-
if (chunk.code === 'ENOENT') {
81+
const type = chunk.type;
82+
switch (type) {
83+
case 'SuccessMessage':
84+
exists = true;
85+
await fd.write(Buffer.from(chunk.secretContent, 'binary'));
8586
break;
86-
} else if (chunk.code === 'EISDIR') {
87-
// First, write the inline error to standard error like other
88-
// secrets commands do.
89-
process.stderr.write(
90-
`edit: ${secretPath}: No such file or directory\n`,
87+
case 'ErrorMessage':
88+
// We expect the ENOENT error, so do nothing if we get that
89+
if (chunk.code === 'EISDIR') {
90+
// First, write the inline error to standard error like other
91+
// secrets commands do.
92+
process.stderr.write(
93+
`edit: ${secretPath}: No such file or directory\n`,
94+
);
95+
// Then, throw an error to get the non-zero exit code. As this
96+
// command is Polykey-specific, the code doesn't really matter
97+
// that much.
98+
throw new errors.ErrorPolykeyCLIEditSecret(
99+
'The specified secret cannot be edited',
100+
);
101+
} else {
102+
throw new errors.ErrorPolykeyCLIEditSecret(
103+
`Unexpected error value returned: ${chunk.code} (${chunk.data})`,
104+
);
105+
}
106+
default:
107+
never(
108+
`Expected "SuccessMessage" or "ContentMessage", got ${type}`,
91109
);
92-
// Then, throw an error to get the non-zero exit code. As this
93-
// command is Polykey-specific, the code doesn't really matter
94-
// that much.
95-
throw new errors.ErrorPolykeyCLIEditSecret(
96-
'The specified secret cannot be edited',
97-
);
98-
} else {
99-
throw new errors.ErrorPolykeyCLIEditSecret(
100-
`Unexpected error value returned: ${chunk.code} (${chunk.data})`,
101-
);
102-
}
103110
}
104111
}
105112
} finally {

src/secrets/CommandMkdir.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ class CommandMkdir extends CommandPolykey {
7272
first = false;
7373
}
7474
await writer.close();
75-
// Print out incoming data to standard out, or incoming errors to
76-
// standard error.
75+
// Print out incoming errors to standard error.
7776
let hasErrored = false;
78-
// TypeScript cannot properly perform type narrowing on this type, so
79-
// the `as` keyword is used to help it out.
8077
for await (const result of response.readable) {
8178
if (result.type === 'ErrorMessage') {
8279
hasErrored = true;
@@ -98,6 +95,8 @@ class CommandMkdir extends CommandPolykey {
9895
throw result;
9996
}
10097
}
98+
// No additional processing needs to be done if directory creation
99+
// was successful.
101100
}
102101
return hasErrored;
103102
}, meta);

src/secrets/CommandRemove.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ class CommandRemove extends CommandPolykey {
105105
throw result;
106106
}
107107
}
108+
// No additional processing needs to be done if file removal was
109+
// successful.
108110
}
109111
return hasErrored;
110112
}, meta);

0 commit comments

Comments
 (0)