From a2c5b1defb4e6b2d14f25f88427e8d53af36154f Mon Sep 17 00:00:00 2001 From: BluJ Date: Thu, 4 May 2023 13:10:35 -0600 Subject: [PATCH] feat: remove the file things since we now are in node --- lib/backup/Backups.ts | 93 +++++++++++++------------- lib/backup/setupBackups.ts | 3 +- lib/config/setupConfig.ts | 2 +- lib/health/checkFns/checkWebUrl.ts | 6 +- lib/health/checkFns/runHealthScript.ts | 6 +- lib/types.ts | 71 +------------------- lib/util/fileHelper.ts | 53 +++++---------- lib/util/index.ts | 18 ----- 8 files changed, 74 insertions(+), 178 deletions(-) diff --git a/lib/backup/Backups.ts b/lib/backup/Backups.ts index 137335e..e088b80 100644 --- a/lib/backup/Backups.ts +++ b/lib/backup/Backups.ts @@ -1,5 +1,6 @@ import { SDKManifest } from "../manifest/ManifestTypes" import * as T from "../types" +import fs from "fs" export type BACKUP = "BACKUP" export const DEFAULT_OPTIONS: T.BackupOptions = { @@ -93,47 +94,45 @@ export class Backups { const createBackup: T.ExpectedExports.createBackup = async ({ effects, }) => { - const previousItems = ( - await effects - .readDir({ - volumeId: Backups.BACKUP, - path: ".", - }) - .catch(() => []) - ).map((x) => `${x}`) - const backupPaths = this.backupSet - .filter((x) => x.dstVolume === Backups.BACKUP) - .map((x) => x.dstPath) - .map((x) => x.replace(/\.\/([^]*)\//, "$1")) - const filteredItems = previousItems.filter( - (x) => backupPaths.indexOf(x) === -1, - ) - for (const itemToRemove of filteredItems) { - effects.console.error(`Trying to remove ${itemToRemove}`) - await effects - .removeDir({ - volumeId: Backups.BACKUP, - path: itemToRemove, - }) - .catch(() => - effects.removeFile({ - volumeId: Backups.BACKUP, - path: itemToRemove, - }), - ) - .catch(() => { - effects.console.warn( - `Failed to remove ${itemToRemove} from backup volume`, - ) - }) - } + // const previousItems = ( + // await effects + // .readDir({ + // volumeId: Backups.BACKUP, + // path: ".", + // }) + // .catch(() => []) + // ).map((x) => `${x}`) + // const backupPaths = this.backupSet + // .filter((x) => x.dstVolume === Backups.BACKUP) + // .map((x) => x.dstPath) + // .map((x) => x.replace(/\.\/([^]*)\//, "$1")) + // const filteredItems = previousItems.filter( + // (x) => backupPaths.indexOf(x) === -1, + // ) + // for (const itemToRemove of filteredItems) { + // effects.console.error(`Trying to remove ${itemToRemove}`) + // await effects + // .removeDir({ + // volumeId: Backups.BACKUP, + // path: itemToRemove, + // }) + // .catch(() => + // effects.removeFile({ + // volumeId: Backups.BACKUP, + // path: itemToRemove, + // }), + // ) + // .catch(() => { + // console.warn(`Failed to remove ${itemToRemove} from backup volume`) + // }) + // } for (const item of this.backupSet) { - if (notEmptyPath(item.dstPath)) { - await effects.createDir({ - volumeId: item.dstVolume, - path: item.dstPath, - }) - } + // if (notEmptyPath(item.dstPath)) { + // await effects.createDir({ + // volumeId: item.dstVolume, + // path: item.dstPath, + // }) + // } await effects .runRsync({ ...item, @@ -150,12 +149,14 @@ export class Backups { effects, }) => { for (const item of this.backupSet) { - if (notEmptyPath(item.srcPath)) { - await effects.createDir({ - volumeId: item.srcVolume, - path: item.srcPath, - }) - } + // if (notEmptyPath(item.srcPath)) { + // await new Promise((resolve, reject) => fs.mkdir(items.src)).createDir( + // { + // volumeId: item.srcVolume, + // path: item.srcPath, + // }, + // ) + // } await effects .runRsync({ options: { diff --git a/lib/backup/setupBackups.ts b/lib/backup/setupBackups.ts index e802cef..de3106a 100644 --- a/lib/backup/setupBackups.ts +++ b/lib/backup/setupBackups.ts @@ -1,7 +1,6 @@ -import { string } from "ts-matches" import { Backups } from "./Backups" import { SDKManifest } from "../manifest/ManifestTypes" -import { BackupOptions, ExpectedExports } from "../types" +import { ExpectedExports } from "../types" import { _ } from "../util" export type SetupBackupsParams = Array< diff --git a/lib/config/setupConfig.ts b/lib/config/setupConfig.ts index 93d0a91..07d0057 100644 --- a/lib/config/setupConfig.ts +++ b/lib/config/setupConfig.ts @@ -60,7 +60,7 @@ export function setupConfig< return { setConfig: (async ({ effects, input }) => { if (!validator.test(input)) { - await effects.console.error(String(validator.errorMessage(input))) + await console.error(String(validator.errorMessage(input))) return { error: "Set config type error for config" } } const { restart } = await write({ diff --git a/lib/health/checkFns/checkWebUrl.ts b/lib/health/checkFns/checkWebUrl.ts index 4b0acd5..c0968be 100644 --- a/lib/health/checkFns/checkWebUrl.ts +++ b/lib/health/checkFns/checkWebUrl.ts @@ -23,9 +23,9 @@ export const checkWebUrl = async ( message: successMessage, })) .catch((e) => { - effects.console.warn(`Error while fetching URL: ${url}`) - effects.console.error(JSON.stringify(e)) - effects.console.error(e.toString()) + console.warn(`Error while fetching URL: ${url}`) + console.error(JSON.stringify(e)) + console.error(e.toString()) return { status: "failing" as const, message: errorMessage } }) } diff --git a/lib/health/checkFns/runHealthScript.ts b/lib/health/checkFns/runHealthScript.ts index ad1ebfe..b2071a3 100644 --- a/lib/health/checkFns/runHealthScript.ts +++ b/lib/health/checkFns/runHealthScript.ts @@ -23,9 +23,9 @@ export const runHealthScript = async ( effects.runCommand(runCommand, { timeoutMillis: timeout }), timeoutPromise(timeout), ]).catch((e) => { - effects.console.warn(errorMessage) - effects.console.warn(JSON.stringify(e)) - effects.console.warn(e.toString()) + console.warn(errorMessage) + console.warn(JSON.stringify(e)) + console.warn(e.toString()) throw { status: "failing", message: errorMessage } as CheckResult }) return { diff --git a/lib/types.ts b/lib/types.ts index 96b3340..82ee5cb 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -181,63 +181,11 @@ export type NetworkInterface = { /** Used to reach out from the pure js runtime */ export type Effects = { - /** Usable when not sandboxed */ - writeFile(input: { - path: string - volumeId: string - toWrite: string - }): Promise - readFile(input: { volumeId: string; path: string }): Promise - /** Usable when not sandboxed */ - appendFile(input: { - path: string - volumeId: string - toWrite: string - }): Promise - /** - * Move file from src to dst - * Usable when not sandboxed */ - moveFile(input: { - srcVolume: string - dstVolume: string - srcPath: string - dstPath: string - }): Promise - /** - * copy from src to dst - * Usable when not sandboxed */ - copyFile(input: { - srcVolume: string - dstVolume: string - srcPath: string - dstPath: string - }): Promise - metadata(input: { volumeId: string; path: string }): Promise - /** Create a directory. Usable when not sandboxed */ - createDir(input: { volumeId: string; path: string }): Promise - - readDir(input: { volumeId: string; path: string }): Promise - /** Remove a directory. Usable when not sandboxed */ - removeDir(input: { volumeId: string; path: string }): Promise - removeFile(input: { volumeId: string; path: string }): Promise - - /** Write a json file into an object. Usable when not sandboxed */ - writeJsonFile(input: { - volumeId: string - path: string - toWrite: Record - }): Promise - - /** Read a json file into an object */ - readJsonFile(input: { - volumeId: string - path: string - }): Promise> - runCommand( command: ValidIfNoStupidEscape | [string, ...string[]], options?: { timeoutMillis?: number + env?: Record }, ): Promise runDaemon( @@ -252,23 +200,6 @@ export type Effects = { /** Uses the chmod on the system */ chmod(input: { volumeId: string; path: string; mode: string }): Promise - sleep(timeMs: TimeMs): Promise - - console: { - /** Log at the trace level */ - log(whatToPrint: string): Promise - /** Log at the trace level */ - trace(whatToPrint: string): Promise - /** Log at the warn level */ - warn(whatToPrint: string): Promise - /** Log at the error level */ - error(whatToPrint: string): Promise - /** Log at the debug level */ - debug(whatToPrint: string): Promise - /** Log at the info level */ - info(whatToPrint: string): Promise - } - /** Sandbox mode lets us read but not write */ is_sandboxed(): boolean diff --git a/lib/util/fileHelper.ts b/lib/util/fileHelper.ts index a0ef34d..5ea8110 100644 --- a/lib/util/fileHelper.ts +++ b/lib/util/fileHelper.ts @@ -2,7 +2,7 @@ import * as matches from "ts-matches" import * as YAML from "yaml" import * as TOML from "@iarna/toml" import * as T from "../types" -import { exists } from "." +import fs from "fs" const previousPath = /(.+?)\/([^/]*)$/ @@ -54,39 +54,32 @@ const previousPath = /(.+?)\/([^/]*)$/ export class FileHelper { protected constructor( readonly path: string, - readonly volume: string, readonly writeData: (dataIn: A) => string, readonly readData: (stringValue: string) => A, ) {} async write(data: A, effects: T.Effects) { - let matched - if ((matched = previousPath.exec(this.path))) { - await effects.createDir({ - volumeId: this.volume, - path: matched[1], - }) + if (previousPath.exec(this.path)) { + await new Promise((resolve, reject) => + fs.mkdir(this.path, (err) => (!err ? resolve(null) : reject(err))), + ) } - await effects.writeFile({ - path: this.path, - volumeId: this.volume, - toWrite: this.writeData(data), - }) + await new Promise((resolve, reject) => + fs.writeFile(this.path, this.writeData(data), (err) => + !err ? resolve(null) : reject(err), + ), + ) } async read(effects: T.Effects) { - if ( - !(await exists(effects, { - path: this.path, - volumeId: this.volume, - })) - ) { + if (!fs.existsSync(this.path)) { return null } return this.readData( - await effects.readFile({ - path: this.path, - volumeId: this.volume, - }), + await new Promise((resolve, reject) => + fs.readFile(this.path, (err, data) => + !err ? resolve(data.toString("utf-8")) : reject(err), + ), + ), ) } /** @@ -96,23 +89,17 @@ export class FileHelper { */ static raw( path: string, - volume: string, toFile: (dataIn: A) => string, fromFile: (rawData: string) => A, ) { - return new FileHelper(path, volume, toFile, fromFile) + return new FileHelper(path, toFile, fromFile) } /** * Create a File Helper for a .json file */ - static json( - path: string, - volume: string, - shape: matches.Validator, - ) { + static json(path: string, shape: matches.Validator) { return new FileHelper( path, - volume, (inData) => { return JSON.stringify(inData, null, 2) }, @@ -126,12 +113,10 @@ export class FileHelper { */ static toml>( path: string, - volume: string, shape: matches.Validator, ) { return new FileHelper( path, - volume, (inData) => { return JSON.stringify(inData, null, 2) }, @@ -145,12 +130,10 @@ export class FileHelper { */ static yaml>( path: string, - volume: string, shape: matches.Validator, ) { return new FileHelper( path, - volume, (inData) => { return JSON.stringify(inData, null, 2) }, diff --git a/lib/util/index.ts b/lib/util/index.ts index fa5eb72..4f9525f 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -30,27 +30,11 @@ T extends object ? {} & {[P in keyof T]: T[P]} : export type _ = FlattenIntersection -/** Used to check if the file exists before hand */ -export const exists = ( - effects: T.Effects, - props: { path: string; volumeId: string }, -) => - effects.metadata(props).then( - (_) => true, - (_) => false, - ) - export const isKnownError = (e: unknown): e is T.KnownError => e instanceof Object && ("error" in e || "error-code" in e) -type Cdr = A extends [unknown, ...infer Cdr] ? Cdr : [] - declare const affine: unique symbol -function withAffine() { - return {} as { [affine]: B } -} - export type WrapperDataOptionals = { validator?: Parser> /** Defaults to what ever the package currently in */ @@ -95,7 +79,6 @@ export type Utils = { bindLan: (port: number) => Promise networkBuilder: () => NetworkBuilder torHostName: (id: string) => TorHostname - exists: (props: { path: string; volumeId: string }) => Promise nullIfEmpty: typeof nullIfEmpty } export const utils = ( @@ -106,7 +89,6 @@ export const utils = ( readFile: (fileHelper: FileHelper) => fileHelper.read(effects), writeFile: (fileHelper: FileHelper, data: A) => fileHelper.write(data, effects), - exists: (props: { path: string; volumeId: string }) => exists(effects, props), nullIfEmpty, getWrapperData: ( packageId: string,