diff --git a/README.md b/README.md
index 9f688fed..7b1beaea 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 a63ccf71..75a90aac 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 df3f7ea8..2c3f716c 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 80413af2..bd8ad816 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')
diff --git a/src/Lighthouse/tests/upload.test.ts b/src/Lighthouse/tests/upload.test.ts
index cc157bc0..88415bcf 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')
}
diff --git a/src/Lighthouse/upload/buffer/browser.ts b/src/Lighthouse/upload/buffer/browser.ts
index 8aa452a8..0230dd75 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 33646a8f..42860489 100644
--- a/src/Lighthouse/upload/files/browser.ts
+++ b/src/Lighthouse/upload/files/browser.ts
@@ -5,21 +5,22 @@ import {
UploadFileReturnType,
DealParameters,
} from '../../../types'
-import { checkDuplicateFileNames, retryFetch } from '../../utils/util'
+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].some(file => file.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 +37,7 @@ export default async (
})
const response = uploadProgressCallback
- ? await retryFetch(endpoint, {
+ ? await fetchWithTimeout(endpoint, {
method: 'POST',
body: formData,
headers: headers,
@@ -47,7 +48,7 @@ export default async (
})
},
})
- : await retryFetch(endpoint, {
+ : await fetchWithTimeout(endpoint, {
method: 'POST',
body: formData,
headers: headers,
@@ -59,20 +60,7 @@ export default async (
}
const responseText = await response.text()
-
- 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 4b72ec2c..a436f592 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 4a9be57f..2420571d 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,13 +47,11 @@ 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',
timeout: 7200000,
headers: {
- Encryption: 'false',
Authorization: token,
'X-Deal-Parameter': dealParameters
? JSON.stringify(dealParameters)
@@ -67,12 +64,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,17 +82,15 @@ 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',
timeout: 7200000,
headers: {
- Encryption: 'false',
Authorization: token,
'X-Deal-Parameter': dealParameters
? JSON.stringify(dealParameters)
@@ -113,17 +103,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 ec29ca0b..d7d8fddd 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,12 +11,11 @@ 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,
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 2da9f618..85bbbc06 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,13 +12,12 @@ 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',
timeout: 7200000,
headers: {
- Encryption: 'false',
'Mime-Type': 'text/plain',
Authorization: token,
},
diff --git a/src/Lighthouse/uploadEncrypted/decrypt/browser.ts b/src/Lighthouse/uploadEncrypted/decrypt/browser.ts
index f9ae64ed..9808ef69 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 a2e0d0c7..77f584a8 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 4172f0cd..a39822c4 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,
@@ -101,30 +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)
- }
+ // 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
+ // 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[]
- 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) => {
+ jsondata.map(async (data: IFileUploadedResponse) => {
return saveShards(publicKey, data.Hash, auth_token, keyMap[data.Name])
})
)
@@ -145,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 109bdd19..3ffa2e82 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,
@@ -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)
}
@@ -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 90061213..b2158f71 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 6d94d2b2..edf594db 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',
diff --git a/src/Lighthouse/utils/util.ts b/src/Lighthouse/utils/util.ts
index e18b7948..0b39a359 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,28 +127,10 @@ async function fetchWithTimeout(
}
}
-async function retryFetch(
- resource: string,
- 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')
-}
-
export {
isCID,
isPrivateKey,
addressValidator,
checkDuplicateFileNames,
- retryFetch,
+ fetchWithTimeout,
}