From 1cf5ad56178998059bc2ba5b34dd69ac360c6a57 Mon Sep 17 00:00:00 2001 From: Rogelio Morrell Date: Tue, 20 Sep 2022 07:36:20 -0500 Subject: [PATCH 1/4] feat: hack - monkey patch bee getKy --- package-lock.json | 20 -------------------- src/fdp-storage.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3032c3f4..9228d81e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,6 @@ "@types/jest-environment-puppeteer": "^4.4.1", "@types/node": "^15.12.4", "@types/puppeteer": "^5.4.3", - "@types/terser-webpack-plugin": "^5.0.3", "@types/webpack-bundle-analyzer": "^4.4.1", "@types/ws": "^7.4.5", "@typescript-eslint/eslint-plugin": "^4.28.0", @@ -4277,16 +4276,6 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, - "node_modules/@types/terser-webpack-plugin": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/@types/terser-webpack-plugin/-/terser-webpack-plugin-5.2.0.tgz", - "integrity": "sha512-iHDR2pRfFjGyDqCALX2tgUgFtGoQf2AJhKpC2XD1IMBQVJF2bny6WChGRDKj9eaZJl4F2RmvBhxJNtVPj7aTRw==", - "deprecated": "This is a stub types definition. terser-webpack-plugin provides its own type definitions, so you do not need this installed.", - "dev": true, - "dependencies": { - "terser-webpack-plugin": "*" - } - }, "node_modules/@types/webpack-bundle-analyzer": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/@types/webpack-bundle-analyzer/-/webpack-bundle-analyzer-4.4.1.tgz", @@ -17645,15 +17634,6 @@ "integrity": "sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==", "dev": true }, - "@types/terser-webpack-plugin": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/@types/terser-webpack-plugin/-/terser-webpack-plugin-5.2.0.tgz", - "integrity": "sha512-iHDR2pRfFjGyDqCALX2tgUgFtGoQf2AJhKpC2XD1IMBQVJF2bny6WChGRDKj9eaZJl4F2RmvBhxJNtVPj7aTRw==", - "dev": true, - "requires": { - "terser-webpack-plugin": "*" - } - }, "@types/webpack-bundle-analyzer": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/@types/webpack-bundle-analyzer/-/webpack-bundle-analyzer-4.4.1.tgz", diff --git a/src/fdp-storage.ts b/src/fdp-storage.ts index 1a66039c..51098843 100644 --- a/src/fdp-storage.ts +++ b/src/fdp-storage.ts @@ -1,4 +1,4 @@ -import { BatchId, Bee } from '@ethersphere/bee-js' +import { BatchId, Bee, KyRequestOptions, RequestOptions } from '@ethersphere/bee-js' import { AccountData } from './account/account-data' import { PersonalStorage } from './pod/personal-storage' import { Connection } from './connection/connection' @@ -6,6 +6,7 @@ import { Options } from './types' import { Directory } from './directory/directory' import { File } from './file/file' import { ENS } from '@fairdatasociety/fdp-contracts' +import { Url } from 'url' export class FdpStorage { public readonly connection: Connection @@ -16,11 +17,32 @@ export class FdpStorage { public readonly ens: ENS constructor(beeUrl: string, postageBatchId: BatchId, options?: Options) { - this.connection = new Connection(new Bee(beeUrl), postageBatchId, options) + // @ts-ignore + Bee.prototype.getKy = this.fetchPolyfill + const bee = new Bee(beeUrl) + this.connection = new Connection(bee, postageBatchId, options) this.ens = new ENS(options?.ensOptions, null, options?.ensDomain) this.account = new AccountData(this.connection, this.ens) this.personalStorage = new PersonalStorage(this.account) this.directory = new Directory(this.account) this.file = new File(this.account) } + + /** + * fetch polyfill for ky and bee-js + * @param options Options that affects the request behavior + */ + fetchPolyfill(options: RequestOptions = {}): any { + return async (url: URL, kyOpts: KyRequestOptions): Promise => { + const res = await fetch(url, { + ...kyOpts, + ...options, + headers: Object(kyOpts.headers), + }) + return { + data: undefined, + ...res, + } // KyResponse type + } + } } From 78fd0a538b381e763eade2e2d96f613685aacbba Mon Sep 17 00:00:00 2001 From: Rogelio Morrell Date: Tue, 20 Sep 2022 09:04:23 -0500 Subject: [PATCH 2/4] feat: only monkey patch upload --- src/fdp-storage.ts | 26 ++------------------------ src/file/file.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/fdp-storage.ts b/src/fdp-storage.ts index 51098843..1a66039c 100644 --- a/src/fdp-storage.ts +++ b/src/fdp-storage.ts @@ -1,4 +1,4 @@ -import { BatchId, Bee, KyRequestOptions, RequestOptions } from '@ethersphere/bee-js' +import { BatchId, Bee } from '@ethersphere/bee-js' import { AccountData } from './account/account-data' import { PersonalStorage } from './pod/personal-storage' import { Connection } from './connection/connection' @@ -6,7 +6,6 @@ import { Options } from './types' import { Directory } from './directory/directory' import { File } from './file/file' import { ENS } from '@fairdatasociety/fdp-contracts' -import { Url } from 'url' export class FdpStorage { public readonly connection: Connection @@ -17,32 +16,11 @@ export class FdpStorage { public readonly ens: ENS constructor(beeUrl: string, postageBatchId: BatchId, options?: Options) { - // @ts-ignore - Bee.prototype.getKy = this.fetchPolyfill - const bee = new Bee(beeUrl) - this.connection = new Connection(bee, postageBatchId, options) + this.connection = new Connection(new Bee(beeUrl), postageBatchId, options) this.ens = new ENS(options?.ensOptions, null, options?.ensDomain) this.account = new AccountData(this.connection, this.ens) this.personalStorage = new PersonalStorage(this.account) this.directory = new Directory(this.account) this.file = new File(this.account) } - - /** - * fetch polyfill for ky and bee-js - * @param options Options that affects the request behavior - */ - fetchPolyfill(options: RequestOptions = {}): any { - return async (url: URL, kyOpts: KyRequestOptions): Promise => { - const res = await fetch(url, { - ...kyOpts, - ...options, - headers: Object(kyOpts.headers), - }) - return { - data: undefined, - ...res, - } // KyResponse type - } - } } diff --git a/src/file/file.ts b/src/file/file.ts index d4b1d840..72567174 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -17,7 +17,7 @@ import { downloadData, generateBlockName } from './handler' import { blocksToManifest, getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter' import { Blocks, DataUploadOptions, FileReceiveOptions, FileShareInfo } from './types' import { addEntryToDirectory, removeEntryFromDirectory } from '../content-items/handler' -import { Data, Reference } from '@ethersphere/bee-js' +import { Bee, BeeError, Data, KyRequestOptions, Reference, RequestOptions } from '@ethersphere/bee-js' import { getRawMetadata } from '../content-items/utils' import { assertRawFileMetadata, combine } from '../directory/utils' import { assertEncryptedReference, EncryptedReference } from '../utils/hex' @@ -67,6 +67,12 @@ export class File { data: Uint8Array | string, options?: DataUploadOptions, ): Promise { + // @ts-ignore + const cloneGetKy = Bee.prototype.getKy + + // @ts-ignore + Bee.prototype.getKy = this.fetchPolyfill(this.accountData.connection.bee.url) + options = { ...this.defaultUploadOptions, ...options } assertAccount(this.accountData) assertPodName(podName) @@ -112,6 +118,8 @@ export class File { await addEntryToDirectory(connection, extendedInfo.podWallet, pathInfo.path, pathInfo.filename, true) await writeFeedData(connection, fullPath, getFileMetadataRawBytes(meta), extendedInfo.podWallet.privateKey) + // @ts-ignore + Bee.prototype.getKy = cloneGetKy return meta } @@ -201,4 +209,27 @@ export class File { return meta } + + /** + * fetch polyfill for ky and bee-js + * @param options Options that affects the request behavior + */ + fetchPolyfill(beeUrl: string) { + return (options: RequestOptions = {}): any => { + return async (url: any, kyOpts: KyRequestOptions): Promise => { + const _url = `${beeUrl}/${url}` + kyOpts.responseType = 'json' + console.log(_url, kyOpts, options) + const res = await fetch(_url, { + ...kyOpts, + ...options, + headers: Object(kyOpts.headers), + }) + return { + data: undefined, + ...res, + } // KyResponse type + } + } + } } From acbd5138fc9c5a0a225f50d579a156ef84358caf Mon Sep 17 00:00:00 2001 From: Rogelio Morrell Date: Tue, 20 Sep 2022 09:07:19 -0500 Subject: [PATCH 3/4] fix: remove console log --- src/file/file.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/file/file.ts b/src/file/file.ts index 72567174..c67acd3f 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -219,7 +219,6 @@ export class File { return async (url: any, kyOpts: KyRequestOptions): Promise => { const _url = `${beeUrl}/${url}` kyOpts.responseType = 'json' - console.log(_url, kyOpts, options) const res = await fetch(_url, { ...kyOpts, ...options, From ab784ed5e4760b746896809b1d1eae4cddb7a77c Mon Sep 17 00:00:00 2001 From: Rogelio Morrell Date: Tue, 20 Sep 2022 09:41:03 -0500 Subject: [PATCH 4/4] feat: remove responseType = json --- src/file/file.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/file/file.ts b/src/file/file.ts index c67acd3f..42241971 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -218,7 +218,6 @@ export class File { return (options: RequestOptions = {}): any => { return async (url: any, kyOpts: KyRequestOptions): Promise => { const _url = `${beeUrl}/${url}` - kyOpts.responseType = 'json' const res = await fetch(_url, { ...kyOpts, ...options,