Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Remove promise-fs

Manav Rathif2c288bdab9a
diff --git a/desktop/package.json b/desktop/package.json
index 407e505b9b..39e5d8b38a 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -34,15 +34,12 @@
"jpeg-js": "^0.4.4",
"next-electron-server": "^1",
"node-stream-zip": "^1.15.0",
- "onnxruntime-node": "^1.16.3",
- "promise-fs": "^2.1.1"
+ "onnxruntime-node": "^1.16.3"
},
"devDependencies": {
"@types/auto-launch": "^5.0.2",
"@types/ffmpeg-static": "^3.0.1",
"@types/get-folder-size": "^2.0.0",
- "@types/node-fetch": "^2.6.2",
- "@types/promise-fs": "^2.1.1",
"@typescript-eslint/eslint-plugin": "^7",
"@typescript-eslint/parser": "^7",
"concurrently": "^8",
diff --git a/desktop/src/services/clipService.ts b/desktop/src/services/clipService.ts
index 65b4a52bb0..58f0d376bd 100644
--- a/desktop/src/services/clipService.ts
+++ b/desktop/src/services/clipService.ts
@@ -1,9 +1,8 @@
import * as log from "electron-log";
import { app, net } from "electron/main";
import { existsSync } from "fs";
-import fs from "fs/promises";
-import path from "path";
-import { readFile } from "promise-fs";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import util from "util";
import { CustomErrors } from "../constants/errors";
import { Model } from "../types";
diff --git a/desktop/src/services/diskCache.ts b/desktop/src/services/diskCache.ts
index 1f4cd52934..947e45aa89 100644
--- a/desktop/src/services/diskCache.ts
+++ b/desktop/src/services/diskCache.ts
@@ -1,5 +1,6 @@
-import path from "path";
-import { existsSync, stat, unlink } from "promise-fs";
+import { existsSync } from "node:fs";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import DiskLRUService from "../services/diskLRU";
import { LimitedCache } from "../types/cache";
import { getFileStream, writeStream } from "./fs";
@@ -28,19 +29,19 @@ export class DiskCache implements LimitedCache {
): Promise<Response> {
const cachePath = path.join(this.cacheBucketDir, cacheKey);
if (existsSync(cachePath)) {
- const fileStats = await stat(cachePath);
+ const fileStats = await fs.stat(cachePath);
if (sizeInBytes && fileStats.size !== sizeInBytes) {
logError(
Error(),
"Cache key exists but size does not match. Deleting cache key.",
);
- unlink(cachePath).catch((e) => {
+ fs.unlink(cachePath).catch((e) => {
if (e.code === "ENOENT") return;
logError(e, "Failed to delete cache key");
});
return undefined;
}
- DiskLRUService.touch(cachePath);
+ DiskLRUService.markUse(cachePath);
return new Response(await getFileStream(cachePath));
} else {
return undefined;
@@ -49,7 +50,7 @@ export class DiskCache implements LimitedCache {
async delete(cacheKey: string): Promise<boolean> {
const cachePath = path.join(this.cacheBucketDir, cacheKey);
if (existsSync(cachePath)) {
- await unlink(cachePath);
+ await fs.unlink(cachePath);
return true;
} else {
return false;
diff --git a/desktop/src/services/diskLRU.ts b/desktop/src/services/diskLRU.ts
index 44b05c0994..d42e1cabc9 100644
--- a/desktop/src/services/diskLRU.ts
+++ b/desktop/src/services/diskLRU.ts
@@ -1,6 +1,6 @@
import getFolderSize from "get-folder-size";
-import path from "path";
-import { close, open, readdir, stat, unlink, utimes } from "promise-fs";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import { logError } from "../services/logging";
export interface LeastRecentlyUsedResult {
@@ -12,19 +12,10 @@ class DiskLRUService {
private isRunning: Promise<any> = null;
private reRun: boolean = false;
- async touch(path: string) {
- try {
- const time = new Date();
- await utimes(path, time, time);
- } catch (err) {
- logError(err, "utimes method touch failed");
- try {
- await close(await open(path, "w"));
- } catch (e) {
- logError(e, "open-close method touch failed");
- }
- // log and ignore
- }
+ /** Mark "use" of a given file by updating its modified time */
+ async markUse(path: string) {
+ const now = new Date();
+ await fs.utimes(path, now, now);
}
enforceCacheSizeLimit(cacheDir: string, maxSize: number) {
@@ -53,7 +44,7 @@ class DiskLRUService {
const leastRecentlyUsed =
await this.findLeastRecentlyUsed(cacheDir);
try {
- await unlink(leastRecentlyUsed.path);
+ await fs.unlink(leastRecentlyUsed.path);
} catch (e) {
// ENOENT: File not found
// which can be ignored as we are trying to delete the file anyway
@@ -81,16 +72,15 @@ class DiskLRUService {
): Promise<LeastRecentlyUsedResult> {
result = result || { atime: new Date(), path: "" };
- const files = await readdir(dir);
+ const files = await fs.readdir(dir);
for (const file of files) {
const newBase = path.join(dir, file);
- const stats = await stat(newBase);
- if (stats.isDirectory()) {
+ const st = await fs.stat(newBase);
+ if (st.isDirectory()) {
result = await this.findLeastRecentlyUsed(newBase, result);
} else {
- const { atime } = await stat(newBase);
-
- if (atime.getTime() < result.atime.getTime()) {
+ const { atime } = st;
+ if (st.atime.getTime() < result.atime.getTime()) {
result = {
atime,
path: newBase,
diff --git a/desktop/src/services/ffmpeg.ts b/desktop/src/services/ffmpeg.ts
index a79b5c4e11..227bd310e5 100644
--- a/desktop/src/services/ffmpeg.ts
+++ b/desktop/src/services/ffmpeg.ts
@@ -1,7 +1,7 @@
import log from "electron-log";
import pathToFfmpeg from "ffmpeg-static";
-import { existsSync } from "fs";
-import { readFile, rmSync, writeFile } from "promise-fs";
+import { existsSync } from "node:fs";
+import * as fs from "node:fs/promises";
import util from "util";
import { CustomErrors } from "../constants/errors";
import { generateTempFilePath, getTempDirPath } from "../utils/temp";
@@ -80,14 +80,14 @@ export async function runFFmpegCmd(
"ms",
);
- const outputFile = await readFile(tempOutputFilePath);
+ const outputFile = await fs.readFile(tempOutputFilePath);
return new Uint8Array(outputFile);
} catch (e) {
logErrorSentry(e, "ffmpeg run command error");
throw e;
} finally {
try {
- rmSync(tempOutputFilePath, { force: true });
+ await fs.rm(tempOutputFilePath, { force: true });
} catch (e) {
logErrorSentry(e, "failed to remove tempOutputFile");
}
@@ -109,7 +109,7 @@ const ffmpegBinaryPath = () => {
export async function writeTempFile(fileStream: Uint8Array, fileName: string) {
const tempFilePath = await generateTempFilePath(fileName);
- await writeFile(tempFilePath, fileStream);
+ await fs.writeFile(tempFilePath, fileStream);
return tempFilePath;
}
@@ -121,7 +121,7 @@ export async function deleteTempFile(tempFilePath: string) {
"tried to delete a non temp file",
);
}
- rmSync(tempFilePath, { force: true });
+ await fs.rm(tempFilePath, { force: true });
}
export const promiseWithTimeout = async <T>(
diff --git a/desktop/src/services/fs.ts b/desktop/src/services/fs.ts
index bcc49ae5ca..c74790d01f 100644
--- a/desktop/src/services/fs.ts
+++ b/desktop/src/services/fs.ts
@@ -1,7 +1,7 @@
-import { existsSync } from "fs";
import StreamZip from "node-stream-zip";
-import path from "path";
-import * as fs from "promise-fs";
+import { createWriteStream, existsSync } from "node:fs";
+import * as fs from "node:fs/promises";
+import * as path from "node:path";
import { Readable } from "stream";
import { ElectronFile } from "../types";
import { logError } from "./logging";
@@ -212,7 +212,7 @@ export async function writeNodeStream(
filePath: string,
fileStream: NodeJS.ReadableStream,
) {
- const writeable = fs.createWriteStream(filePath);
+ const writeable = createWriteStream(filePath);
fileStream.on("error", (error) => {
writeable.destroy(error); // Close the writable stream with an error
@@ -222,7 +222,7 @@ export async function writeNodeStream(
await new Promise((resolve, reject) => {
writeable.on("finish", resolve);
- writeable.on("error", async (e) => {
+ writeable.on("error", async (e: unknown) => {
if (existsSync(filePath)) {
await fs.unlink(filePath);
}
diff --git a/desktop/yarn.lock b/desktop/yarn.lock
index 8d4c9649e3..b341cad169 100644
--- a/desktop/yarn.lock
+++ b/desktop/yarn.lock
@@ -205,11 +205,6 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"
-"@octetstream/[email protected]":
- version "2.0.2"
- resolved "https://registry.yarnpkg.com/@octetstream/promisify/-/promisify-2.0.2.tgz#29ac3bd7aefba646db670227f895d812c1a19615"
- integrity sha512-7XHoRB61hxsz8lBQrjC1tq/3OEIgpvGWg6DKAdwi7WRzruwkmsdwmOoUXbU4Dtd4RSOMDwed0SkP3y8UlMt1Bg==
-
"@pkgr/core@^0.1.0":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@pkgr/core/-/core-0.1.1.tgz#1ec17e2edbec25c8306d424ecfbf13c7de1aaa31"
@@ -293,14 +288,6 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
-"@types/node-fetch@^2.6.2":
- version "2.6.2"
- resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.2.tgz#d1a9c5fd049d9415dce61571557104dec3ec81da"
- integrity sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A==
- dependencies:
- "@types/node" "*"
- form-data "^3.0.0"
-
"@types/node@*":
version "18.0.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.3.tgz#463fc47f13ec0688a33aec75d078a0541a447199"
@@ -329,13 +316,6 @@
"@types/node" "*"
xmlbuilder ">=11.0.1"
-"@types/promise-fs@^2.1.1":
- version "2.1.2"
- resolved "https://registry.yarnpkg.com/@types/promise-fs/-/promise-fs-2.1.2.tgz#7ef6ab00c7fbc68081e34e560d2f008d3dd27fd2"
- integrity sha512-s3YON1LmplAUVrvTT2d1I0m2Rk0hSgc/1l5/krnU96YpP4NG9VEN/qopaFv8yk5a2Z+AgYzafS1LCP+kQH0MYw==
- dependencies:
- "@types/node" "*"
-
"@types/responselike@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/responselike/-/responselike-1.0.0.tgz#251f4fe7d154d2bad125abe1b429b23afd262e29"
@@ -1503,15 +1483,6 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.6.tgz#022e9218c637f9f3fc9c35ab9c9193f05add60b2"
integrity sha512-0sQoMh9s0BYsm+12Huy/rkKxVu4R1+r96YX5cG44rHV0pQ6iC3Q+mkoMFaGWObMFYQxCVT+ssG1ksneA2MI9KQ==
-form-data@^3.0.0:
- version "3.0.1"
- resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
- integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
- dependencies:
- asynckit "^0.4.0"
- combined-stream "^1.0.8"
- mime-types "^2.1.12"
-
form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
@@ -2478,13 +2449,6 @@ progress@^2.0.3:
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA==
-promise-fs@^2.1.1:
- version "2.1.1"
- resolved "https://registry.yarnpkg.com/promise-fs/-/promise-fs-2.1.1.tgz#0b725a592c165ff16157d1f13640ba390637e557"
- integrity sha512-43p7e4QzAQ3w6eyN0+gbBL7jXiZFWLWYITg9wIObqkBySu/a5K1EDcQ/S6UyB/bmiZWDA4NjTbcopKLTaKcGSw==
- dependencies:
- "@octetstream/promisify" "2.0.2"
-
promise-retry@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/promise-retry/-/promise-retry-2.0.1.tgz#ff747a13620ab57ba688f5fc67855410c370da22"