Skip to content

Commit

Permalink
build: update fs imports to get around issue in rollup node polyfill …
Browse files Browse the repository at this point in the history
…plugin
  • Loading branch information
SewerynKras committed Oct 9, 2024
1 parent 2cb34de commit 258a653
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
27 changes: 14 additions & 13 deletions src/shared/storage/ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof fs>;

type UploadChunkChunk = { offset: number; content: Uint8Array };
Expand Down Expand Up @@ -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() });
Expand All @@ -189,21 +190,21 @@ 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<FileHandle>;
mockFs.open.mockResolvedValue(fileHandle);
} as unknown as jest.Mocked<fs.promises.FileHandle>;
mockFs.promises.open = jest.fn().mockResolvedValue(fileHandle);
});

it("should read the file and upload it", async () => {
expect.assertions(9);
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) => {
Expand Down Expand Up @@ -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() });
Expand All @@ -366,16 +367,16 @@ describe("WebSocketStorageProvider", () => {
fileHandle = {
write: jest.fn(),
close: jest.fn(),
} as unknown as jest.Mocked<FileHandle>;
mockFs.open.mockResolvedValue(fileHandle);
} as unknown as jest.Mocked<fs.promises.FileHandle>;
mockFs.promises.open = jest.fn().mockResolvedValue(fileHandle);
});

it("should receive the file and write it to the disc", async () => {
expect.assertions(10);
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);
Expand Down
7 changes: 5 additions & 2 deletions src/shared/storage/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -62,7 +65,7 @@ export class WebSocketStorageProvider implements StorageProvider {
private services = new Map<string, string>();
private logger: Logger;
private ready = false;
private openHandles = new Set<FileHandle>();
private openHandles = new Set<fs.promises.FileHandle>();

constructor(
private readonly yagnaApi: YagnaApi,
Expand Down

0 comments on commit 258a653

Please sign in to comment.