π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[space][web] Diagnose file reads after image load failures
Anand Baburajan6d3afccadced
diff --git a/web/apps/space/src/state/AppStateProvider.tsx b/web/apps/space/src/state/AppStateProvider.tsxindex 8154b04622..53532af79f 100644--- a/web/apps/space/src/state/AppStateProvider.tsx+++ b/web/apps/space/src/state/AppStateProvider.tsx@@ -46,6 +46,7 @@ import {failLocalFeedPost,} from "utils/local-feed-post";import { prepareSpacePostImageFromEdit } from "utils/post-image";+import { logSpaceImageReadCheck } from "utils/post-image-diagnostics";import { spacePostFrameAspectRatio } from "utils/post-photos";import { prepareSpaceVideo } from "utils/post-video";@@ -248,6 +249,16 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({image.rotationDegrees,);} catch (error) {+ if (!image.video)+ void logSpaceImageReadCheck(+ image.file,+ error,+ {+ requestId: session.requestId,+ attempt,+ item: index + 1,+ },+ ).catch(() => undefined);const details =error instanceof Error? {diff --git a/web/apps/space/src/utils/post-image-diagnostics.ts b/web/apps/space/src/utils/post-image-diagnostics.tsnew file mode 100644index 0000000000..2de5f38161--- /dev/null+++ b/web/apps/space/src/utils/post-image-diagnostics.ts@@ -0,0 +1,45 @@+import { logToDisk } from "ente-base/log-web";++const checkedFiles = new WeakSet<File>();++export const logSpaceImageReadCheck = async (+ file: File,+ preparationError: unknown,+ context: { requestId?: string; attempt: number; item: number },+) => {+ if (+ !(preparationError instanceof Error) ||+ preparationError.message != "Could not decode image" ||+ checkedFiles.has(file)+ )+ return;+ checkedFiles.add(file);++ const { requestId, attempt, item } = context;+ const startedAt = performance.now();+ logToDisk(+ `[info] Space image read check ${JSON.stringify({ requestId, attempt, item, outcome: "started" })}`,+ );+ let outcome = "readable";+ let error: string | undefined;+ try {+ await file.arrayBuffer();+ } catch (cause) {+ outcome = "failed";+ const name = cause instanceof Error ? cause.name : "Unknown";+ error = [+ "NotReadableError",+ "NotFoundError",+ "SecurityError",+ "AbortError",+ "InvalidStateError",+ "TypeError",+ "RangeError",+ ].includes(name)+ ? name+ : "Unknown";+ }+ logToDisk(+ `[info] Space image read check ${JSON.stringify({ requestId, attempt, item, outcome, error, elapsedMs: Math.round(performance.now() - startedAt) })}`,+ );+};diff --git a/web/apps/space/tests/post-image-diagnostics.test.ts b/web/apps/space/tests/post-image-diagnostics.test.tsnew file mode 100644index 0000000000..82be89c191--- /dev/null+++ b/web/apps/space/tests/post-image-diagnostics.test.ts@@ -0,0 +1,85 @@+import { afterEach, beforeEach, expect, test, vi } from "vitest";+import { logSpaceImageReadCheck } from "../src/utils/post-image-diagnostics";++const mocks = vi.hoisted(() => ({+ logToDisk: vi.fn<(line: string) => void>(),+}));+vi.mock("ente-base/log-web", () => ({ logToDisk: mocks.logToDisk }));++const context = {+ requestId: "12345678-1234-1234-1234-123456789abc",+ attempt: 1,+ item: 2,+};+const decodeError = new Error("Could not decode image");+const entries = () =>+ mocks.logToDisk.mock.calls.map(+ ([line]: [string]) =>+ JSON.parse(line.slice(line.indexOf("{"))) as unknown,+ );++beforeEach(() => mocks.logToDisk.mockReset());+afterEach(() => vi.restoreAllMocks());++test("checks a failed image once and logs progress while the read is pending", async () => {+ const file = new File(["PRIVATE_CONTENT"], "PRIVATE_FILENAME.jpg");+ const pendingRead = Promise.withResolvers<ArrayBuffer>();+ const read = vi+ .spyOn(file, "arrayBuffer")+ .mockReturnValue(pendingRead.promise);+ vi.spyOn(performance, "now").mockReturnValue(100);++ const check = logSpaceImageReadCheck(file, decodeError, context);+ await logSpaceImageReadCheck(file, decodeError, { ...context, attempt: 2 });++ expect(read).toHaveBeenCalledOnce();+ expect(entries()).toEqual([{ ...context, outcome: "started" }]);+ vi.spyOn(performance, "now").mockReturnValue(150);+ pendingRead.resolve(new TextEncoder().encode("PRIVATE_CONTENT").buffer);+ await check;++ expect(entries()).toEqual([+ { ...context, outcome: "started" },+ { ...context, outcome: "readable", elapsedMs: 50 },+ ]);+});++test.each(["NotReadableError", "PRIVATE_NAME"])(+ "logs a safe read error category for %s without private data",+ async (name) => {+ const file = new File(["PRIVATE_CONTENT"], "PRIVATE_FILENAME.jpg", {+ type: "private/type",+ lastModified: 1700123456789,+ });+ const error = new DOMException("PRIVATE_PATH", name);+ error.stack = "PRIVATE_STACK";+ vi.spyOn(file, "arrayBuffer").mockRejectedValue(error);+ vi.spyOn(performance, "now").mockReturnValue(100);++ await logSpaceImageReadCheck(file, decodeError, context);++ expect(entries()).toEqual([+ { ...context, outcome: "started" },+ {+ ...context,+ outcome: "failed",+ error: name == "NotReadableError" ? name : "Unknown",+ elapsedMs: 0,+ },+ ]);+ expect(JSON.stringify(entries())).not.toMatch(/PRIVATE|1700123456789/i);+ },+);++test.each([new Error("Could not encode image"), "PRIVATE_ERROR"])(+ "does not read or log for other preparation failures: %s",+ async (error) => {+ const file = new File(["photo"], "photo.jpg");+ const read = vi.spyOn(file, "arrayBuffer");++ await logSpaceImageReadCheck(file, error, context);++ expect(read).not.toHaveBeenCalled();+ expect(mocks.logToDisk).not.toHaveBeenCalled();+ },+);