Skip to content

Commit

Permalink
feat: change the default storage provider from gftp to ws
Browse files Browse the repository at this point in the history
Added implementation for `uploadFile` and `receiveFile` to websocket storage provider.
Changed the default  provider to `ws`.

You can still use the old `GftpStorageProvider` by setting `storageProvider: "gftp"`
  • Loading branch information
SewerynKras committed Oct 9, 2024
1 parent 2c70207 commit 083a0eb
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 70 deletions.
30 changes: 23 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"@types/eventsource": "^1.1.15",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.20",
"@types/node": "^18.19.55",
"@types/semver": "^7.5.8",
"@types/supertest": "^6.0.2",
"@types/tmp": "^0.2.6",
Expand Down
4 changes: 2 additions & 2 deletions src/golem-network/golem-network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MarketApiAdapter, PaymentApiAdapter } from "../shared/yagna";
import { ActivityApiAdapter } from "../shared/yagna/adapters/activity-api-adapter";
import { GolemNetwork, MarketOrderSpec } from "./golem-network";
import { _, instance, mock, reset, spy, verify, when } from "@johanblumenberg/ts-mockito";
import { GftpStorageProvider } from "../shared/storage";
import { WebSocketStorageProvider } from "../shared/storage";

const order: MarketOrderSpec = Object.freeze({
demand: {
Expand All @@ -34,7 +34,7 @@ const mockYagna = mock(YagnaApi);
const mockPaymentApi = mock(PaymentApiAdapter);
const mockActivityApi = mock(ActivityApiAdapter);
const mockMarketApi = mock(MarketApiAdapter);
const mockStorageProvider = mock(GftpStorageProvider);
const mockStorageProvider = mock(WebSocketStorageProvider);

afterEach(() => {
reset(mockYagna);
Expand Down
27 changes: 13 additions & 14 deletions src/golem-network/golem-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { anyAbortSignal, createAbortSignalFromTimeout, defaultLogger, isNode, Logger, YagnaApi } from "../shared/utils";
import { anyAbortSignal, createAbortSignalFromTimeout, defaultLogger, Logger, YagnaApi } from "../shared/utils";
import {
Demand,
DraftOfferProposalPool,
Expand Down Expand Up @@ -29,17 +29,13 @@ import { ProposalRepository } from "../shared/yagna/repository/proposal-reposito
import { CacheService } from "../shared/cache/CacheService";
import { DemandRepository } from "../shared/yagna/repository/demand-repository";
import { IDemandRepository, OrderDemandOptions } from "../market/demand";
import { GftpServerAdapter } from "../shared/storage/GftpServerAdapter";
import {
GftpStorageProvider,
NullStorageProvider,
StorageProvider,
WebSocketBrowserStorageProvider,
} from "../shared/storage";
import { StorageServerAdapter } from "../shared/storage/StorageServerAdapter";
import { GftpStorageProvider, NullStorageProvider, StorageProvider, WebSocketStorageProvider } from "../shared/storage";
import { DataTransferProtocol } from "../shared/types";
import { NetworkApiAdapter } from "../shared/yagna/adapters/network-api-adapter";
import { IProposalRepository } from "../market/proposal";
import { Subscription } from "rxjs";
import { GolemConfigError } from "../shared/error/golem-error";

/**
* Instance of an object or a factory function that you can call `new` on.
Expand Down Expand Up @@ -105,7 +101,7 @@ export interface GolemNetworkOptions {

/**
* Set the data transfer protocol to use for file transfers.
* Default is `gftp`.
* Default is `ws`.
*/
dataTransferProtocol?: DataTransferProtocol;

Expand Down Expand Up @@ -228,7 +224,7 @@ export class GolemNetwork {

constructor(options: Partial<GolemNetworkOptions> = {}) {
const optDefaults: GolemNetworkOptions = {
dataTransferProtocol: isNode ? "gftp" : "ws",
dataTransferProtocol: "ws",
};

this.options = {
Expand Down Expand Up @@ -285,7 +281,7 @@ export class GolemNetwork {
this.options.override?.marketApi ||
new MarketApiAdapter(this.yagna, agreementRepository, proposalRepository, demandRepository, this.logger),
networkApi: this.options.override?.networkApi || new NetworkApiAdapter(this.yagna),
fileServer: this.options.override?.fileServer || new GftpServerAdapter(this.storageProvider),
fileServer: this.options.override?.fileServer || new StorageServerAdapter(this.storageProvider),
};
this.network = getFactory(NetworkModuleImpl, this.options.override?.network)(this.services);
this.market = getFactory(MarketModuleImpl, this.options.override?.market)(
Expand Down Expand Up @@ -655,13 +651,16 @@ export class GolemNetwork {
private createStorageProvider(): StorageProvider {
if (typeof this.options.dataTransferProtocol === "string") {
switch (this.options.dataTransferProtocol) {
case "gftp":
return new GftpStorageProvider(this.logger);
case "ws":
return new WebSocketBrowserStorageProvider(this.yagna, {
return new WebSocketStorageProvider(this.yagna, {
logger: this.logger,
});
case "gftp":
default:
return new GftpStorageProvider(this.logger);
throw new GolemConfigError(
`Unsupported data transfer protocol ${this.options.dataTransferProtocol}. Supported protocols are "gftp" and "ws"`,
);
}
} else if (this.options.dataTransferProtocol !== undefined) {
return this.options.dataTransferProtocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import fs from "fs";
import jsSha3 from "js-sha3";

/**
* This class provides GFTP based implementation of the IFileServer interface used in the SDK
* IFileServer implementation that uses any StorageProvider to serve files.
* Make sure that the storage provider implements the `.publishFile()` method.
*/
export class GftpServerAdapter implements IFileServer {
class StorageServerAdapter implements IFileServer {
private published = new Map<string, FileServerEntry>();

constructor(private readonly storage: StorageProvider) {}
Expand Down Expand Up @@ -57,3 +58,12 @@ export class GftpServerAdapter implements IFileServer {
});
}
}

/**
* @deprecated Use StorageServerAdapter instead. This will be removed in the next major version.
*
* This class provides GFTP based implementation of the IFileServer interface used in the SDK
*/
class GftpServerAdapter extends StorageServerAdapter {}

export { GftpServerAdapter, StorageServerAdapter };
18 changes: 5 additions & 13 deletions src/shared/storage/default.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { GftpStorageProvider } from "./gftp";
import { WebSocketBrowserStorageProvider } from "./ws-browser";
import { NullStorageProvider } from "./null";
import { Logger, YagnaApi, isNode, isBrowser } from "../utils";
import { WebSocketStorageProvider } from "./ws";
import { Logger, YagnaApi } from "../utils";

export function createDefaultStorageProvider(yagnaApi: YagnaApi, logger?: Logger) {
if (isNode) {
return new GftpStorageProvider(logger?.child("storage"));
}
if (isBrowser) {
return new WebSocketBrowserStorageProvider(yagnaApi, {
logger: logger?.child("storage"),
});
}
return new NullStorageProvider();
return new WebSocketStorageProvider(yagnaApi, {
logger: logger?.child("storage"),
});
}
5 changes: 5 additions & 0 deletions src/shared/storage/gftp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { GolemInternalError, GolemUserError } from "../error/golem-error";
import { v4 } from "uuid";
import AsyncLock from "async-lock";

/**
* @deprecated Use WebSocketStorageProvider instead. This will be removed in the next major version.
*
* Storage provider that spawns a GFTP process and uses it to serve files.
*/
export class GftpStorageProvider implements StorageProvider {
private gftpServerProcess?: ChildProcess;
private logger: Logger;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { StorageProvider } from "./provider";
export { GftpStorageProvider } from "./gftp";
export { NullStorageProvider } from "./null";
export { WebSocketBrowserStorageProvider, WebSocketStorageProviderOptions } from "./ws-browser";
export { WebSocketStorageProvider, WebSocketStorageProviderOptions } from "./ws";
export { createDefaultStorageProvider } from "./default";
Loading

0 comments on commit 083a0eb

Please sign in to comment.