From 9e80bd3a38541d3895ef4db3c88f4caf9aef76e8 Mon Sep 17 00:00:00 2001 From: aakash-taneja Date: Fri, 26 Jul 2024 20:51:10 +0530 Subject: [PATCH 1/9] removed retry --- src/Lighthouse/utils/util.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/Lighthouse/utils/util.ts b/src/Lighthouse/utils/util.ts index e18b794..5ca8ff3 100644 --- a/src/Lighthouse/utils/util.ts +++ b/src/Lighthouse/utils/util.ts @@ -132,17 +132,7 @@ async function retryFetch( options: FetchOptions, retries = 3 ): Promise { - for (let i = 0; i < retries; i++) { - try { - return await fetchWithTimeout(resource, options) - } catch (error) { - if (i < retries - 1) { - } else { - throw error - } - } - } - throw new Error('fetch failed') + return await fetchWithTimeout(resource, options) } export { From 3a80d3685047db49a28fd03b50865d7446488077 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 16:41:27 +0530 Subject: [PATCH 2/9] removed retry --- src/Lighthouse/utils/util.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Lighthouse/utils/util.ts b/src/Lighthouse/utils/util.ts index 5ca8ff3..0b39a35 100644 --- a/src/Lighthouse/utils/util.ts +++ b/src/Lighthouse/utils/util.ts @@ -39,7 +39,7 @@ function checkDuplicateFileNames(files: any[]) { } async function fetchWithTimeout( - resource: string, + endpointURL: string, options: FetchOptions ): Promise { const { timeout = 8000, onProgress, ...rest } = options @@ -51,7 +51,7 @@ async function fetchWithTimeout( if (onProgress && rest.body instanceof FormData) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest() - xhr.open(rest.method || 'GET', resource) + xhr.open(rest.method || 'GET', endpointURL) if (rest.headers) { if (rest.headers instanceof Headers) { @@ -114,7 +114,7 @@ async function fetchWithTimeout( } }) } else { - const response = await fetch(resource, { + const response = await fetch(endpointURL, { ...rest, signal: controller.signal, }) @@ -127,18 +127,10 @@ async function fetchWithTimeout( } } -async function retryFetch( - resource: string, - options: FetchOptions, - retries = 3 -): Promise { - return await fetchWithTimeout(resource, options) -} - export { isCID, isPrivateKey, addressValidator, checkDuplicateFileNames, - retryFetch, + fetchWithTimeout, } From 7e46c3ed73a4bb5b157b0dd3b87ed8cfdf6dac86 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 16:41:52 +0530 Subject: [PATCH 3/9] removed multi --- src/Lighthouse/upload/files/browser.ts | 34 +++++++++-------------- src/Lighthouse/upload/files/index.ts | 38 ++++++-------------------- src/Lighthouse/upload/files/node.ts | 30 +++++--------------- src/Lighthouse/upload/text/browser.ts | 4 +-- src/Lighthouse/upload/text/node.ts | 4 +-- 5 files changed, 33 insertions(+), 77 deletions(-) diff --git a/src/Lighthouse/upload/files/browser.ts b/src/Lighthouse/upload/files/browser.ts index 33646a8..50d77d6 100644 --- a/src/Lighthouse/upload/files/browser.ts +++ b/src/Lighthouse/upload/files/browser.ts @@ -5,21 +5,23 @@ import { UploadFileReturnType, DealParameters, } from '../../../types' -import { checkDuplicateFileNames, retryFetch } from '../../utils/util' +import file from '../../uploadEncrypted/encrypt/file' +import { fetchWithTimeout } from '../../utils/util' // eslint-disable-next-line @typescript-eslint/no-empty-function export default async ( files: any, accessToken: string, - multi: boolean, dealParameters: DealParameters | undefined, uploadProgressCallback?: (data: IUploadProgressCallback) => void ): Promise<{ data: UploadFileReturnType }> => { try { - const endpoint = - lighthouseConfig.lighthouseNode + - `/api/v0/add?wrap-with-directory=${multi}` - checkDuplicateFileNames(files) + const isDirectory = files.length === 1 && files[0].webkitRelativePath + let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false` + + if(!isDirectory && files.length > 1) { + endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=true` + } const formData = new FormData() for (let i = 0; i < files.length; i++) { @@ -36,7 +38,7 @@ export default async ( }) const response = uploadProgressCallback - ? await retryFetch(endpoint, { + ? await fetchWithTimeout(endpoint, { method: 'POST', body: formData, headers: headers, @@ -47,7 +49,7 @@ export default async ( }) }, }) - : await retryFetch(endpoint, { + : await fetchWithTimeout(endpoint, { method: 'POST', body: formData, headers: headers, @@ -59,20 +61,10 @@ export default async ( } const responseText = await response.text() + console.log(responseText) + console.log(typeof(responseText)) - let data - if (typeof responseText === 'string') { - if (multi) { - data = JSON.parse( - `[${responseText.slice(0, -1)}]`.split('\n').join(',') - ) - } else { - const temp = responseText.split('\n') - data = JSON.parse(temp[temp.length - 2]) - } - } - - return { data } + return { data: JSON.parse(responseText) } } catch (error: any) { throw new Error(error?.message) } diff --git a/src/Lighthouse/upload/files/index.ts b/src/Lighthouse/upload/files/index.ts index 4b72ec2..a436f59 100644 --- a/src/Lighthouse/upload/files/index.ts +++ b/src/Lighthouse/upload/files/index.ts @@ -9,7 +9,6 @@ import { async function uploadFiles( sourcePath: string | any, apiKey: string, - multi?: false, dealParameters?: DealParameters, uploadProgressCallback?: (data: IUploadProgressCallback) => void ): Promise<{ data: IFileUploadedResponse }> @@ -17,7 +16,6 @@ async function uploadFiles( async function uploadFiles( sourcePath: string | any, apiKey: string, - multi?: true, dealParameters?: DealParameters, uploadProgressCallback?: (data: IUploadProgressCallback) => void ): Promise<{ data: IFileUploadedResponse[] }> @@ -25,38 +23,20 @@ async function uploadFiles( async function uploadFiles( path: string | any, apiKey: string, - multi?: boolean, dealParameters?: DealParameters, uploadProgressCallback?: (data: IUploadProgressCallback) => void ) { // Upload File to IPFS - - if (multi) { - //@ts-ignore - if (typeof window === 'undefined') { - return await uploadFile(path, apiKey, true, dealParameters) - } else { - return await uploadFileBrowser( - path, - apiKey, - true, - dealParameters, - uploadProgressCallback - ) - } + //@ts-ignore + if (typeof window === 'undefined') { + return await uploadFile(path, apiKey, dealParameters) } else { - //@ts-ignore - if (typeof window === 'undefined') { - return await uploadFile(path, apiKey, false, dealParameters) - } else { - return await uploadFileBrowser( - path, - apiKey, - false, - dealParameters, - uploadProgressCallback - ) - } + return await uploadFileBrowser( + path, + apiKey, + dealParameters, + uploadProgressCallback + ) } } diff --git a/src/Lighthouse/upload/files/node.ts b/src/Lighthouse/upload/files/node.ts index 4a9be57..a3ae117 100644 --- a/src/Lighthouse/upload/files/node.ts +++ b/src/Lighthouse/upload/files/node.ts @@ -1,7 +1,7 @@ import basePathConvert from '../../utils/basePathConvert' import { lighthouseConfig } from '../../../lighthouse.config' import { UploadFileReturnType, DealParameters } from '../../../types' -import { retryFetch } from '../../utils/util' +import { fetchWithTimeout } from '../../utils/util' export async function walk(dir: string) { const { readdir, stat } = eval(`require`)('fs-extra') @@ -25,7 +25,6 @@ export async function walk(dir: string) { export default async ( sourcePath: string, apiKey: string, - multi: boolean, dealParameters: DealParameters | undefined ): Promise<{ data: UploadFileReturnType }> => { const { createReadStream, lstatSync } = eval(`require`)('fs-extra') @@ -36,7 +35,7 @@ export default async ( try { const endpoint = lighthouseConfig.lighthouseNode + - `/api/v0/add?wrap-with-directory=${multi}` + `/api/v0/add?wrap-with-directory=false` if (stats.isFile()) { const data = new FormData() const stream = createReadStream(sourcePath) @@ -48,7 +47,7 @@ export default async ( data.append('file', blob, path.basename(sourcePath)) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: data, credentials: 'include', @@ -67,12 +66,7 @@ export default async ( } let responseData = (await response.text()) as any - if (multi) { - const temp = responseData.split('\n') - responseData = JSON.parse(temp[temp.length - 2]) - } else { - responseData = JSON.parse(responseData) - } + responseData = JSON.parse(responseData) return { data: responseData } } else { @@ -90,11 +84,11 @@ export default async ( data.append( 'file', blob, - multi ? path.basename(file) : basePathConvert(sourcePath, file) + basePathConvert(sourcePath, file) ) } - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: data, credentials: 'include', @@ -113,17 +107,7 @@ export default async ( } let responseData = (await response.text()) as any - - if (typeof responseData === 'string') { - if (multi) { - responseData = JSON.parse( - `[${responseData.slice(0, -1)}]`.split('\n').join(',') - ) - } else { - const temp = responseData.split('\n') - responseData = JSON.parse(temp[temp.length - 2]) - } - } + responseData = JSON.parse(responseData) return { data: responseData } } diff --git a/src/Lighthouse/upload/text/browser.ts b/src/Lighthouse/upload/text/browser.ts index ec29ca0..5fabae8 100644 --- a/src/Lighthouse/upload/text/browser.ts +++ b/src/Lighthouse/upload/text/browser.ts @@ -1,5 +1,5 @@ import { lighthouseConfig } from '../../../lighthouse.config' -import { retryFetch } from '../../utils/util' +import { fetchWithTimeout } from '../../utils/util' export default async (text: string, apiKey: string, name: string) => { try { @@ -11,7 +11,7 @@ export default async (text: string, apiKey: string, name: string) => { const blob = new Blob([text], { type: 'text/plain' }) formData.append('file', blob, name) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, diff --git a/src/Lighthouse/upload/text/node.ts b/src/Lighthouse/upload/text/node.ts index 2da9f61..a612f3e 100644 --- a/src/Lighthouse/upload/text/node.ts +++ b/src/Lighthouse/upload/text/node.ts @@ -1,5 +1,5 @@ import { lighthouseConfig } from '../../../lighthouse.config' -import { retryFetch } from '../../utils/util' +import { fetchWithTimeout } from '../../utils/util' export default async (text: string, apiKey: string, name: string) => { try { @@ -12,7 +12,7 @@ export default async (text: string, apiKey: string, name: string) => { formData.append('file', blob, name) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, credentials: 'include', From a6b69e660ae821b3525496b4cef0c0e8b60146c0 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 16:43:33 +0530 Subject: [PATCH 4/9] version --- README.md | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/Commands/index.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9f688fe..7b1beae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Lighthouse +# Lighthouse Lighthouse is a permanent decentralized file storage protocol that allows the ability to pay once and store forever. While traditionally, users need to repeatedly keep track and pay for their storage after every fixed amount of time, Lighthouse manages this for them and makes sure that user files are stored forever. The aim is to move users from a rent-based cost model where they are renting their own files on cloud storage to a permanent ownership model. It is built on top of IPFS, Filecoin, and Polygon. It uses the existing miner network and storage capacity of the filecoin network. diff --git a/package-lock.json b/package-lock.json index a63ccf7..75a90aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.3.6", + "version": "0.3.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lighthouse-web3/sdk", - "version": "0.3.6", + "version": "0.3.7", "license": "MIT", "dependencies": { "@lighthouse-web3/kavach": "^0.1.9", diff --git a/package.json b/package.json index df3f7ea..2c3f716 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lighthouse-web3/sdk", - "version": "0.3.6", + "version": "0.3.7", "description": "NPM package and CLI tool to interact with lighthouse protocol", "main": "./dist/Lighthouse/index.js", "types": "./dist/Lighthouse/index.d.ts", diff --git a/src/Commands/index.ts b/src/Commands/index.ts index 80413af..bd8ad81 100644 --- a/src/Commands/index.ts +++ b/src/Commands/index.ts @@ -72,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) { } widgets.addHelpText('before', 'Welcome to lighthouse-web3') -widgets.version('0.3.6') +widgets.version('0.3.7') widgets .command('wallet') From 4eec1f86ace067d8b80cfa0fef947351b44b6c50 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 18:44:28 +0530 Subject: [PATCH 5/9] response fixes --- src/Lighthouse/uploadEncrypted/decrypt/browser.ts | 4 ++-- src/Lighthouse/uploadEncrypted/decrypt/node.ts | 4 ++-- .../uploadEncrypted/encrypt/file/browser.ts | 14 ++++---------- .../uploadEncrypted/encrypt/file/node.ts | 10 ++++------ .../uploadEncrypted/encrypt/text/browser.ts | 4 ++-- .../uploadEncrypted/encrypt/text/node.ts | 4 ++-- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Lighthouse/uploadEncrypted/decrypt/browser.ts b/src/Lighthouse/uploadEncrypted/decrypt/browser.ts index f9ae64e..9808ef6 100644 --- a/src/Lighthouse/uploadEncrypted/decrypt/browser.ts +++ b/src/Lighthouse/uploadEncrypted/decrypt/browser.ts @@ -1,14 +1,14 @@ /* istanbul ignore file */ import { decryptFile } from '../encryptionBrowser' import { lighthouseConfig } from '../../../lighthouse.config' -import { retryFetch } from '../../utils/util' +import { fetchWithTimeout } from '../../utils/util' export default async ( cid: string, fileEncryptionKey: string, mimeType: string ) => { - const response = await retryFetch( + const response = await fetchWithTimeout( lighthouseConfig.lighthouseGateway + '/api/v0/cat/' + cid, { method: 'POST', diff --git a/src/Lighthouse/uploadEncrypted/decrypt/node.ts b/src/Lighthouse/uploadEncrypted/decrypt/node.ts index a2e0d0c..77f584a 100644 --- a/src/Lighthouse/uploadEncrypted/decrypt/node.ts +++ b/src/Lighthouse/uploadEncrypted/decrypt/node.ts @@ -1,11 +1,11 @@ /* istanbul ignore file */ import { decryptFile } from '../encryptionNode' import { lighthouseConfig } from '../../../lighthouse.config' -import { retryFetch } from '../../utils/util' +import { fetchWithTimeout } from '../../utils/util' export default async (cid: string, fileEncryptionKey: any) => { try { - const response = await retryFetch( + const response = await fetchWithTimeout( lighthouseConfig.lighthouseGateway + '/api/v0/cat/' + cid, { method: 'POST', diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts index 4172f0c..d35c516 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts @@ -6,7 +6,7 @@ import { } from '../../../../types' import { encryptFile } from '../../encryptionBrowser' import { lighthouseConfig } from '../../../../lighthouse.config' -import { checkDuplicateFileNames, retryFetch } from '../../../utils/util' +import { checkDuplicateFileNames, fetchWithTimeout } from '../../../utils/util' declare const FileReader: any @@ -74,7 +74,7 @@ export default async ( }) const response = uploadProgressCallback - ? await retryFetch(endpoint, { + ? await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, @@ -88,7 +88,7 @@ export default async ( }) }, }) - : await retryFetch(endpoint, { + : await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, @@ -115,13 +115,7 @@ export default async ( new Uint8Array(chunks.flatMap((chunk) => [...chunk])) ) as any - if (typeof responseData === 'string') { - responseData = JSON.parse( - `[${responseData.slice(0, -1)}]`.split('\n').join(',') - ) - } else { - responseData = [responseData] - } + responseData = JSON.parse(responseData) const savedKey = await Promise.all( responseData.map(async (data: IFileUploadedResponse) => { diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts index 109bdd1..3587cfb 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts @@ -3,7 +3,7 @@ import { generate, saveShards } from '@lighthouse-web3/kavach' import { encryptFile } from '../../encryptionNode' import { walk } from '../../../upload/files/node' import { IFileUploadedResponse } from '../../../../types' -import { retryFetch } from '../../../utils/util' +import { fetchWithTimeout } from '../../../utils/util' export default async ( sourcePath: any, @@ -28,7 +28,7 @@ export default async ( const blob = new Blob([Buffer.from(encryptedData)]) formData.append('file', blob, sourcePath.replace(/^.*[\\/]/, '')) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, @@ -81,7 +81,7 @@ export default async ( }) ) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, @@ -96,9 +96,7 @@ export default async ( } const responseText = await response.text() - const jsondata = JSON.parse( - `[${responseText.slice(0, -1)}]`.split('\n').join(',') - ) as IFileUploadedResponse[] + const jsondata = JSON.parse(responseText) as IFileUploadedResponse[] const savedKey = await Promise.all( jsondata.map(async (data) => { diff --git a/src/Lighthouse/uploadEncrypted/encrypt/text/browser.ts b/src/Lighthouse/uploadEncrypted/encrypt/text/browser.ts index 9006121..b2158f7 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/text/browser.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/text/browser.ts @@ -2,7 +2,7 @@ import { encryptFile } from '../../encryptionBrowser' import { generate, saveShards } from '@lighthouse-web3/kavach' import { lighthouseConfig } from '../../../../lighthouse.config' -import { retryFetch } from '../../../utils/util' +import { fetchWithTimeout } from '../../../utils/util' export default async ( text: string, @@ -32,7 +32,7 @@ export default async ( name ) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, timeout: 7200000, diff --git a/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts b/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts index 6d94d2b..edf594d 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/text/node.ts @@ -2,7 +2,7 @@ import { encryptFile } from '../../encryptionNode' import { generate, saveShards } from '@lighthouse-web3/kavach' import { lighthouseConfig } from '../../../../lighthouse.config' -import { retryFetch } from '../../../utils/util' +import { fetchWithTimeout } from '../../../utils/util' export default async ( text: string, @@ -27,7 +27,7 @@ export default async ( const blob = new Blob([Buffer.from(encryptedData)]) formData.append('file', blob, name) - const response = await retryFetch(endpoint, { + const response = await fetchWithTimeout(endpoint, { method: 'POST', body: formData, credentials: 'include', From 2316cd55b8cb6d86d9d1e12f7c5a7afbf1b3eb3c Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 18:45:13 +0530 Subject: [PATCH 6/9] bug fix --- src/Lighthouse/upload/files/node.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Lighthouse/upload/files/node.ts b/src/Lighthouse/upload/files/node.ts index a3ae117..e2111f2 100644 --- a/src/Lighthouse/upload/files/node.ts +++ b/src/Lighthouse/upload/files/node.ts @@ -53,7 +53,6 @@ export default async ( credentials: 'include', timeout: 7200000, headers: { - Encryption: 'false', Authorization: token, 'X-Deal-Parameter': dealParameters ? JSON.stringify(dealParameters) @@ -94,7 +93,6 @@ export default async ( credentials: 'include', timeout: 7200000, headers: { - Encryption: 'false', Authorization: token, 'X-Deal-Parameter': dealParameters ? JSON.stringify(dealParameters) From d5aaa429d9cb8e6521ca3c47802775a97b41085b Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Sun, 28 Jul 2024 19:34:06 +0530 Subject: [PATCH 7/9] bug fix --- src/Lighthouse/upload/buffer/browser.ts | 1 - src/Lighthouse/upload/files/browser.ts | 6 +----- src/Lighthouse/upload/text/browser.ts | 1 - src/Lighthouse/upload/text/node.ts | 1 - 4 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Lighthouse/upload/buffer/browser.ts b/src/Lighthouse/upload/buffer/browser.ts index 8aa452a..0230dd7 100644 --- a/src/Lighthouse/upload/buffer/browser.ts +++ b/src/Lighthouse/upload/buffer/browser.ts @@ -13,7 +13,6 @@ export default async (blob: any, apiKey: string, mimeType = '') => { method: 'POST', body: formData, headers: { - Encryption: 'false', 'Mime-Type': mimeType, Authorization: token, }, diff --git a/src/Lighthouse/upload/files/browser.ts b/src/Lighthouse/upload/files/browser.ts index 50d77d6..4286048 100644 --- a/src/Lighthouse/upload/files/browser.ts +++ b/src/Lighthouse/upload/files/browser.ts @@ -5,7 +5,6 @@ import { UploadFileReturnType, DealParameters, } from '../../../types' -import file from '../../uploadEncrypted/encrypt/file' import { fetchWithTimeout } from '../../utils/util' // eslint-disable-next-line @typescript-eslint/no-empty-function @@ -16,7 +15,7 @@ export default async ( uploadProgressCallback?: (data: IUploadProgressCallback) => void ): Promise<{ data: UploadFileReturnType }> => { try { - const isDirectory = files.length === 1 && files[0].webkitRelativePath + const isDirectory = [...files].some(file => file.webkitRelativePath) let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false` if(!isDirectory && files.length > 1) { @@ -61,9 +60,6 @@ export default async ( } const responseText = await response.text() - console.log(responseText) - console.log(typeof(responseText)) - return { data: JSON.parse(responseText) } } catch (error: any) { throw new Error(error?.message) diff --git a/src/Lighthouse/upload/text/browser.ts b/src/Lighthouse/upload/text/browser.ts index 5fabae8..d7d8fdd 100644 --- a/src/Lighthouse/upload/text/browser.ts +++ b/src/Lighthouse/upload/text/browser.ts @@ -16,7 +16,6 @@ export default async (text: string, apiKey: string, name: string) => { body: formData, timeout: 7200000, headers: { - Encryption: 'false', 'Mime-Type': 'text/plain', Authorization: token, }, diff --git a/src/Lighthouse/upload/text/node.ts b/src/Lighthouse/upload/text/node.ts index a612f3e..85bbbc0 100644 --- a/src/Lighthouse/upload/text/node.ts +++ b/src/Lighthouse/upload/text/node.ts @@ -18,7 +18,6 @@ export default async (text: string, apiKey: string, name: string) => { credentials: 'include', timeout: 7200000, headers: { - Encryption: 'false', 'Mime-Type': 'text/plain', Authorization: token, }, From 6bc3019c8533cd5b47c74b4f656950c661dc10f0 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Mon, 29 Jul 2024 12:19:44 +0530 Subject: [PATCH 8/9] fixed --- src/Lighthouse/upload/files/node.ts | 2 -- .../uploadEncrypted/encrypt/file/browser.ts | 36 ++++++++++--------- .../uploadEncrypted/encrypt/file/node.ts | 4 +-- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Lighthouse/upload/files/node.ts b/src/Lighthouse/upload/files/node.ts index e2111f2..2420571 100644 --- a/src/Lighthouse/upload/files/node.ts +++ b/src/Lighthouse/upload/files/node.ts @@ -50,7 +50,6 @@ export default async ( const response = await fetchWithTimeout(endpoint, { method: 'POST', body: data, - credentials: 'include', timeout: 7200000, headers: { Authorization: token, @@ -90,7 +89,6 @@ export default async ( const response = await fetchWithTimeout(endpoint, { method: 'POST', body: data, - credentials: 'include', timeout: 7200000, headers: { Authorization: token, diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts index d35c516..a39822c 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts @@ -101,24 +101,26 @@ export default async ( throw new Error(`HTTP error! status: ${response.status}`) } - const reader = response.body?.getReader() - let chunks = [] - while (true) { - const { done, value } = await reader!.read() - if (done) { - break - } - chunks.push(value) - } - - let responseData = new TextDecoder('utf-8').decode( - new Uint8Array(chunks.flatMap((chunk) => [...chunk])) - ) as any - - responseData = JSON.parse(responseData) + // const reader = response.body?.getReader() + // let chunks = [] + // while (true) { + // const { done, value } = await reader!.read() + // if (done) { + // break + // } + // chunks.push(value) + // } + + // let responseData = new TextDecoder('utf-8').decode( + // new Uint8Array(chunks.flatMap((chunk) => [...chunk])) + // ) as any + const responseText = await response.text() + const jsondata = JSON.parse(responseText) as IFileUploadedResponse[] + + // responseData = JSON.parse(responseData) const savedKey = await Promise.all( - responseData.map(async (data: IFileUploadedResponse) => { + jsondata.map(async (data: IFileUploadedResponse) => { return saveShards(publicKey, data.Hash, auth_token, keyMap[data.Name]) }) ) @@ -139,7 +141,7 @@ export default async ( } */ - return { data: responseData } + return { data: jsondata } } catch (error: any) { return error.message } diff --git a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts index 3587cfb..3ffa2e8 100644 --- a/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts +++ b/src/Lighthouse/uploadEncrypted/encrypt/file/node.ts @@ -46,7 +46,7 @@ export default async ( const { error } = await saveShards( publicKey, - responseData.Hash, + responseData[0].Hash, auth_token, keyShards ) @@ -54,7 +54,7 @@ export default async ( throw new Error('Error encrypting file') } - return { data: [responseData] } + return { data: responseData } } catch (error: any) { throw new Error(error.message) } From 69d6e5e1851d7214d77eeb613e71d424f6638a79 Mon Sep 17 00:00:00 2001 From: ravish1729 Date: Mon, 29 Jul 2024 12:33:39 +0530 Subject: [PATCH 9/9] multi removed --- src/Lighthouse/tests/upload.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Lighthouse/tests/upload.test.ts b/src/Lighthouse/tests/upload.test.ts index cc157bc..88415bc 100644 --- a/src/Lighthouse/tests/upload.test.ts +++ b/src/Lighthouse/tests/upload.test.ts @@ -11,7 +11,7 @@ describe('uploadFiles', () => { 'src/Lighthouse/tests/testImages/testImage1.svg' ) const fileName = path.split('/').slice(-1)[0] - const deployResponse = (await lighthouse.upload(path, apiKey, false)).data + const deployResponse = (await lighthouse.upload(path, apiKey)).data expect(deployResponse).toHaveProperty('Name') expect(deployResponse).toHaveProperty('Hash') @@ -24,11 +24,10 @@ describe('uploadFiles', () => { it('should upload folder to ipfs when correct path is provided', async () => { const path = resolve(process.cwd(), 'src/Lighthouse/tests/testImages') - const full_deployResponse = (await lighthouse.upload(path, apiKey, true)) + const full_deployResponse = (await lighthouse.upload(path, apiKey)) .data - expect(full_deployResponse.length).toBeGreaterThan(1) - const deployResponse = full_deployResponse[0] + const deployResponse = full_deployResponse expect(deployResponse).toHaveProperty('Name') expect(deployResponse).toHaveProperty('Hash') expect(deployResponse).toHaveProperty('Size') @@ -41,7 +40,7 @@ describe('uploadFiles', () => { it('should not upload to ipfs when incorrect path is provided', async () => { try { const path = 'invalid/path/img.svg' - const deployResponse = await lighthouse.upload(path, apiKey, false) + const deployResponse = await lighthouse.upload(path, apiKey) } catch (error) { expect(error.code).toBe('ENOENT') }