Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.3.7 #117

Merged
merged 10 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.6-green"/>
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.7-green"/>

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.

Expand Down
4 changes: 2 additions & 2 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
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
9 changes: 4 additions & 5 deletions src/Lighthouse/tests/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
}
Expand Down
1 change: 0 additions & 1 deletion src/Lighthouse/upload/buffer/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default async (blob: any, apiKey: string, mimeType = '') => {
method: 'POST',
body: formData,
headers: {
Encryption: 'false',
'Mime-Type': mimeType,
Authorization: token,
},
Expand Down
32 changes: 10 additions & 22 deletions src/Lighthouse/upload/files/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T extends boolean>(
files: any,
accessToken: string,
multi: boolean,
dealParameters: DealParameters | undefined,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
): Promise<{ data: UploadFileReturnType<T> }> => {
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++) {
Expand All @@ -36,7 +37,7 @@ export default async <T extends boolean>(
})

const response = uploadProgressCallback
? await retryFetch(endpoint, {
? await fetchWithTimeout(endpoint, {
method: 'POST',
body: formData,
headers: headers,
Expand All @@ -47,7 +48,7 @@ export default async <T extends boolean>(
})
},
})
: await retryFetch(endpoint, {
: await fetchWithTimeout(endpoint, {
method: 'POST',
body: formData,
headers: headers,
Expand All @@ -59,20 +60,7 @@ export default async <T extends boolean>(
}

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)
}
Expand Down
38 changes: 9 additions & 29 deletions src/Lighthouse/upload/files/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,34 @@ import {
async function uploadFiles(
sourcePath: string | any,
apiKey: string,
multi?: false,
dealParameters?: DealParameters,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
): Promise<{ data: IFileUploadedResponse }>

async function uploadFiles(
sourcePath: string | any,
apiKey: string,
multi?: true,
dealParameters?: DealParameters,
uploadProgressCallback?: (data: IUploadProgressCallback) => void
): Promise<{ data: IFileUploadedResponse[] }>

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
)
}
}

Expand Down
34 changes: 7 additions & 27 deletions src/Lighthouse/upload/files/node.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -25,7 +25,6 @@ export async function walk(dir: string) {
export default async <T extends boolean>(
sourcePath: string,
apiKey: string,
multi: boolean,
dealParameters: DealParameters | undefined
): Promise<{ data: UploadFileReturnType<T> }> => {
const { createReadStream, lstatSync } = eval(`require`)('fs-extra')
Expand All @@ -36,7 +35,7 @@ export default async <T extends boolean>(
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)
Expand All @@ -48,13 +47,11 @@ export default async <T extends boolean>(

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)
Expand All @@ -67,12 +64,7 @@ export default async <T extends boolean>(
}

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 {
Expand All @@ -90,17 +82,15 @@ export default async <T extends boolean>(
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)
Expand All @@ -113,17 +103,7 @@ export default async <T extends boolean>(
}

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 }
}
Expand Down
5 changes: 2 additions & 3 deletions src/Lighthouse/upload/text/browser.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
},
Expand Down
5 changes: 2 additions & 3 deletions src/Lighthouse/upload/text/node.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions src/Lighthouse/uploadEncrypted/decrypt/browser.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/Lighthouse/uploadEncrypted/decrypt/node.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
Loading
Loading