From 258a653d0e09ead55b24ba8a784c1db570041123 Mon Sep 17 00:00:00 2001 From: Seweryn Kras Date: Wed, 9 Oct 2024 18:33:11 +0200 Subject: [PATCH] build: update fs imports to get around issue in rollup node polyfill plugin --- src/shared/storage/ws.test.ts | 27 ++++++++++++++------------- src/shared/storage/ws.ts | 7 +++++-- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/shared/storage/ws.test.ts b/src/shared/storage/ws.test.ts index d7e7a0c98..ab7e47c9a 100644 --- a/src/shared/storage/ws.test.ts +++ b/src/shared/storage/ws.test.ts @@ -6,12 +6,13 @@ import { encode, toObject } from "flatbuffers/js/flexbuffers.js"; import * as jsSha3 from "js-sha3"; import { GsbApi, IdentityApi } from "ya-ts-client"; import { anything, imock, instance, mock, reset, verify, when } from "@johanblumenberg/ts-mockito"; -import fs, { FileHandle } from "fs/promises"; -import { Stats } from "fs"; import WebSocket from "ws"; +import * as fs from "fs"; jest.mock("uuid", () => ({ v4: () => "uuid" })); -jest.mock("fs/promises"); +jest.mock("fs", () => ({ + promises: {}, +})); const mockFs = fs as jest.Mocked; type UploadChunkChunk = { offset: number; content: Uint8Array }; @@ -178,7 +179,7 @@ describe("WebSocketStorageProvider", () => { describe("publishFile()", () => { let socket: EventTarget & { send: jest.Mock }; let fileInfo: { id: string; url: string }; - let fileHandle: FileHandle; + let fileHandle: fs.promises.FileHandle; beforeEach(() => { socket = Object.assign(new EventTarget(), { send: jest.fn() }); @@ -189,12 +190,12 @@ describe("WebSocketStorageProvider", () => { jest.spyOn(provider as any, "createFileInfo").mockImplementation(() => Promise.resolve(fileInfo)); jest.spyOn(provider as any, "createSocket").mockImplementation(() => Promise.resolve(socket)); - mockFs.stat.mockResolvedValue({ size: 10 } as unknown as Stats); + mockFs.promises.stat = jest.fn().mockResolvedValue({ size: 10 } as unknown as fs.Stats); fileHandle = { read: jest.fn(), close: jest.fn(), - } as unknown as jest.Mocked; - mockFs.open.mockResolvedValue(fileHandle); + } as unknown as jest.Mocked; + mockFs.promises.open = jest.fn().mockResolvedValue(fileHandle); }); it("should read the file and upload it", async () => { @@ -202,8 +203,8 @@ describe("WebSocketStorageProvider", () => { const result = await provider["publishFile"]("./file.txt"); expect(result).toBe(fileInfo.url); expect(provider["createSocket"]).toHaveBeenCalledWith(fileInfo, ["GetMetadata", "GetChunk"]); - expect(mockFs.stat).toHaveBeenCalledWith("./file.txt"); - expect(mockFs.open).toHaveBeenCalledWith("./file.txt", "r"); + expect(mockFs.promises.stat).toHaveBeenCalledWith("./file.txt"); + expect(mockFs.promises.open).toHaveBeenCalledWith("./file.txt", "r"); async function sendGetChunk(chunk: number[], offset: number, id: string) { fileHandle.read = jest.fn().mockImplementationOnce((buffer: Buffer) => { @@ -352,7 +353,7 @@ describe("WebSocketStorageProvider", () => { describe("receiveFile()", () => { let socket: EventTarget & { send: jest.Mock }; let fileInfo: { id: string; url: string }; - let fileHandle: FileHandle; + let fileHandle: fs.promises.FileHandle; beforeEach(async () => { socket = Object.assign(new EventTarget(), { send: jest.fn() }); @@ -366,8 +367,8 @@ describe("WebSocketStorageProvider", () => { fileHandle = { write: jest.fn(), close: jest.fn(), - } as unknown as jest.Mocked; - mockFs.open.mockResolvedValue(fileHandle); + } as unknown as jest.Mocked; + mockFs.promises.open = jest.fn().mockResolvedValue(fileHandle); }); it("should receive the file and write it to the disc", async () => { @@ -375,7 +376,7 @@ describe("WebSocketStorageProvider", () => { const result = await provider["receiveFile"]("./file.txt"); expect(result).toBe(fileInfo.url); expect(provider["createSocket"]).toHaveBeenCalledWith(fileInfo, ["UploadChunk", "UploadFinished"]); - expect(mockFs.open).toHaveBeenCalledWith("./file.txt", "w"); + expect(mockFs.promises.open).toHaveBeenCalledWith("./file.txt", "w"); async function sendUploadChunk(chunk: number[], id: string) { const expectedBuffer = Buffer.alloc(chunk.length); diff --git a/src/shared/storage/ws.ts b/src/shared/storage/ws.ts index 23ec807a4..065839db8 100644 --- a/src/shared/storage/ws.ts +++ b/src/shared/storage/ws.ts @@ -5,9 +5,12 @@ import { encode, toObject } from "flatbuffers/js/flexbuffers.js"; import * as jsSha3 from "js-sha3"; import { defaultLogger, isBrowser, Logger, YagnaApi } from "../utils"; import { GolemInternalError } from "../error/golem-error"; -import fsPromises, { FileHandle } from "fs/promises"; import WebSocket from "ws"; +// FIXME: cannot import fs/promises because the rollup polyfill doesn't work with it +import * as fs from "fs"; +const fsPromises = fs.promises; + export interface WebSocketStorageProviderOptions { logger?: Logger; } @@ -62,7 +65,7 @@ export class WebSocketStorageProvider implements StorageProvider { private services = new Map(); private logger: Logger; private ready = false; - private openHandles = new Set(); + private openHandles = new Set(); constructor( private readonly yagnaApi: YagnaApi,