Skip to content

Commit

Permalink
chore: reduced usage of the as keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 12, 2024
1 parent 651ff9d commit f238c3e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 70 deletions.
47 changes: 23 additions & 24 deletions src/secrets/CommandCat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import type {
ContentSuccessMessage,
ErrorMessage,
} from 'polykey/dist/client/types';
import type { ContentOrErrorMessage } from 'polykey/dist/client/types';
import type { ReadableStream } from 'stream/web';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
Expand Down Expand Up @@ -98,29 +96,30 @@ class CommandGet extends CommandPolykey {
await writer.close();
// Print out incoming data to standard out
let hasErrored = false;
for await (const chunk of response.readable) {
if (chunk.type === 'error') {
// TypeScript cannot properly perform type narrowing on this type, so
// the `as` keyword is used to help it out.
for await (const result of response.readable as ReadableStream<ContentOrErrorMessage>) {
if (result.type === 'error') {
hasErrored = true;
// TypeScript cannot properly decode this type, so we need to use
// the `as` keyword so it doesn't throw errors.
const error = chunk as ErrorMessage;
if (error.code === 'ENOENT') {
// Attempt to cat a non-existent file
process.stderr.write(
`cat: ${error.reason}: No such file or directory\n`,
);
} else if (error.code === 'EISDIR') {
// Attempt to cat a directory
process.stderr.write(`cat: ${error.reason}: Is a directory\n`);
} else {
// No other code should be thrown
throw error;
switch (result.code) {
case 'ENOENT':
// Attempt to cat a non-existent file
process.stderr.write(
`cat: ${result.reason}: No such file or directory\n`,
);
break;
case 'EISDIR':
// Attempt to cat a directory
process.stderr.write(
`cat: ${result.reason}: Is a directory\n`,
);
break;
default:
// No other code should be thrown
throw result;
}
} else {
// TypeScript cannot properly decode this type, so we need to use
// the `as` keyword so it doesn't throw errors.
const data = chunk as ContentSuccessMessage;
process.stdout.write(data.secretContent);
process.stdout.write(result.secretContent);
}
}
return hasErrored;
Expand Down
37 changes: 17 additions & 20 deletions src/secrets/CommandMkdir.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import type { ErrorMessage } from 'polykey/dist/client/types';
import type { SuccessOrErrorMessage } from 'polykey/dist/client/types';
import type { ReadableStream } from 'stream/web';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binParsers from '../utils/parsers';
import * as binProcessors from '../utils/processors';
import {
ErrorPolykeyCLIMakeDirectory,
ErrorPolykeyCLIUncaughtException,
} from '../errors';
import { ErrorPolykeyCLIMakeDirectory } from '../errors';

class CommandMkdir extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
Expand Down Expand Up @@ -79,29 +77,28 @@ class CommandMkdir extends CommandPolykey {
// Print out incoming data to standard out, or incoming errors to
// standard error.
let hasErrored = false;
for await (const result of response.readable) {
// TypeScript cannot properly perform type narrowing on this type, so
// the `as` keyword is used to help it out.
for await (const result of response.readable as ReadableStream<SuccessOrErrorMessage>) {
if (result.type === 'error') {
// TS cannot properly evaluate a type this deeply nested, so we use
// the as keyword to help it. Inside this block, the type of data
// is ensured to be 'error'.
const error = result as ErrorMessage;
hasErrored = true;
let message: string = '';
switch (error.code) {
switch (result.code) {
case 'ENOENT':
message = 'No such secret or directory';
// Attempt to create a directory without existing parents
process.stderr.write(
`mkdir: cannot create directory ${result.reason}: No such file or directory\n`,
);
break;
case 'EEXIST':
message = 'Secret or directory exists';
// Attempt to create a directory where something already exists
process.stderr.write(
`mkdir: cannot create directory ${result.reason}: File or directory exists\n`,
);
break;
default:
throw new ErrorPolykeyCLIUncaughtException(
`Unexpected error code: ${error.code}`,
);
// No other code should be thrown
throw result;
}
process.stderr.write(
`${error.code}: cannot create directory ${error.reason}: ${message}\n`,
);
}
}
return hasErrored;
Expand Down
54 changes: 28 additions & 26 deletions src/secrets/CommandRemove.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import type { ErrorMessage } from 'polykey/dist/client/types';
import type { SuccessOrErrorMessage } from 'polykey/dist/client/types';
import type { ReadableStream } from 'stream/web';
import CommandPolykey from '../CommandPolykey';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
Expand Down Expand Up @@ -70,32 +71,33 @@ class CommandRemove extends CommandPolykey {
await writer.close();
// Check if any errors were raised
let hasErrored = false;
for await (const chunk of response.readable) {
if (chunk.type === 'error') {
// TypeScript cannot properly perform type narrowing on this type, so
// the `as` keyword is used to help it out.
for await (const result of response.readable as ReadableStream<SuccessOrErrorMessage>) {
if (result.type === 'error') {
hasErrored = true;
// TypeScript cannot properly decode this type, so we need to use
// the `as` keyword so it doesn't throw errors.
const error = chunk as ErrorMessage;
if (error.code === 'ENOTEMPTY') {
// Attempt to delete the directory without recursive set
process.stderr.write(
`rm: cannot remove '${error.reason}': Is a directory\n`,
);
continue;
} else if (error.code === 'ENOENT') {
// Attempt to delete a non-existent path
process.stderr.write(
`rm: cannot remove '${error.reason}': No such file or directory\n`,
);
continue;
} else if (error.code === 'EINVAL') {
// Attempt to delete vault root
process.stderr.write(
`rm: cannot remove '${error.reason}': Permission denied\n`,
);
} else {
// No other code should be thrown
throw error;
switch (result.code) {
case 'ENOTEMPTY':
// Attempt to delete the directory without recursive set
process.stderr.write(
`rm: cannot remove '${result.reason}': Is a directory\n`,
);
break;
case 'ENOENT':
// Attempt to delete a non-existent path
process.stderr.write(
`rm: cannot remove '${result.reason}': No such file or directory\n`,
);
break;
case 'EINVAL':
// Attempt to delete vault root
process.stderr.write(
`rm: cannot remove '${result.reason}': Permission denied\n`,
);
break;
default:
// No other code should be thrown
throw result;
}
}
}
Expand Down

0 comments on commit f238c3e

Please sign in to comment.