Skip to content

Commit

Permalink
fix asserts and async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanfallon committed Dec 19, 2024
1 parent fa25272 commit 810003b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
5 changes: 3 additions & 2 deletions api/src/etl/providers/FileManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { access, mapshaper, mkdir } from "@/deps.ts";
import { assert } from "@/dev_deps.ts";
import { createHash, sha256sum } from "@/lib/crypto/index.ts";
import fetcher from "@/lib/fetcher/index.ts";
import { logger } from "@/lib/logger/index.ts";
Expand Down Expand Up @@ -164,6 +163,8 @@ export class FileManager implements FileManagerInterface {

protected async checkSignature(data: ReadableStream<Uint8Array>, sha256: string | undefined): Promise<void> {
if (typeof sha256 === "undefined" || sha256 === "") return;
assert(await sha256sum(data), sha256);
if (await sha256sum(data) !== sha256) {
throw new Error("SHA256 checksum does not match");
}
}
}
2 changes: 1 addition & 1 deletion api/src/lib/crypto/crypto.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe("crypto:sha256sum", () => {
const path = getTmpDir() + "/hello.txt";
writeSync(path, str, { create: true });
assertEquals(await sha256sum(path), sha);
remove(path);
await remove(path);
});
});
6 changes: 4 additions & 2 deletions api/src/lib/crypto/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bcrypt, decodeBase64, encodeBase64, stdCrypto } from "@/deps.ts";
import { assert } from "@/dev_deps.ts";
import { exists, read } from "@/lib/file/index.ts";
import { encodeHex } from "https://deno.land/[email protected]/encoding/hex.ts";

Expand Down Expand Up @@ -77,7 +76,10 @@ export async function sha256sum(source: string | ReadableStream<Uint8Array>): Pr
if (source instanceof ReadableStream) {
stream = source;
} else {
assert(await exists(source), `File ${source} does not exist`);
if (!await exists(source)) {
throw new Error(`File not found: ${source}`);
}

const file = await read(source);
stream = file.readable;
}
Expand Down
16 changes: 9 additions & 7 deletions api/src/lib/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assert } from "@/dev_deps.ts";
import { env_or_default } from "@/lib/env/index.ts";

/**
Expand Down Expand Up @@ -36,15 +35,18 @@ export type OpenFileDescriptor = Deno.FsFile;
* @param options
* @returns
*/
export function open(filepath: string, options: OpenFileOptions = { read: true }): Promise<OpenFileDescriptor> {
export async function open(filepath: string, options: OpenFileOptions = { read: true }): Promise<OpenFileDescriptor> {
return Deno.open(filepath, {
...options,
create: true,
});
}

export function read(filepath: string): Promise<Deno.FsFile> {
assert(exists(filepath), `File ${filepath} does not exist`);
export async function read(filepath: string): Promise<Deno.FsFile> {
if (!await exists(filepath)) {
throw new Error(`File not found: ${filepath}`);
}

return Deno.open(filepath, { read: true });
}

Expand All @@ -64,17 +66,17 @@ export async function exists(filepath: string): Promise<boolean> {
}
}

export function readFile(filepath: string) {
export async function readFile(filepath: string) {
return Deno.readFile(filepath);
}

export function writeSync(filepath: string, data: string, options?: Deno.WriteFileOptions): void {
return Deno.writeTextFileSync(filepath, data, options);
}

export function remove(filepath: string): void {
export async function remove(filepath: string): Promise<void> {
try {
Deno.remove(filepath);
await Deno.remove(filepath);
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw new Error(`Failed to remove file ${filepath}: ${error.message}`);
Expand Down
2 changes: 1 addition & 1 deletion api/src/pdc/services/export/models/CSVWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class CSVWriter<T extends { [k: string]: unknown }> {
zip.writeZip(this.archivePath);

if (this.options.cleanup) {
remove(this.csvPath);
await remove(this.csvPath);
}

return this;
Expand Down
8 changes: 2 additions & 6 deletions api/src/pdc/services/export/services/StorageService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { provider } from "@/ilos/common/Decorators.ts";
import { remove } from "@/lib/file/index.ts";
import {
BucketName,
S3ObjectList,
S3StorageProvider,
} from "@/pdc/providers/storage/index.ts";
import { BucketName, S3ObjectList, S3StorageProvider } from "@/pdc/providers/storage/index.ts";

export abstract class StorageServiceInterfaceResolver {
public async init(): Promise<void> {
Expand Down Expand Up @@ -52,6 +48,6 @@ export class StorageService {
}

public async cleanup(filepath: string): Promise<void> {
remove(filepath);
await remove(filepath);
}
}

0 comments on commit 810003b

Please sign in to comment.