Skip to content

Commit 4571add

Browse files
authored
Merge pull request #117 from lighthouse-web3/v0.3.7
V0.3.7
2 parents 878fefc + 69d6e5e commit 4571add

File tree

18 files changed

+77
-157
lines changed

18 files changed

+77
-157
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.6-green"/>
1+
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.7-green"/>
22

33
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.
44

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lighthouse-web3/sdk",
3-
"version": "0.3.6",
3+
"version": "0.3.7",
44
"description": "NPM package and CLI tool to interact with lighthouse protocol",
55
"main": "./dist/Lighthouse/index.js",
66
"types": "./dist/Lighthouse/index.d.ts",

src/Commands/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Command.prototype.helpInformation = function (context: any) {
7272
}
7373

7474
widgets.addHelpText('before', 'Welcome to lighthouse-web3')
75-
widgets.version('0.3.6')
75+
widgets.version('0.3.7')
7676

7777
widgets
7878
.command('wallet')

src/Lighthouse/tests/upload.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('uploadFiles', () => {
1111
'src/Lighthouse/tests/testImages/testImage1.svg'
1212
)
1313
const fileName = path.split('/').slice(-1)[0]
14-
const deployResponse = (await lighthouse.upload(path, apiKey, false)).data
14+
const deployResponse = (await lighthouse.upload(path, apiKey)).data
1515

1616
expect(deployResponse).toHaveProperty('Name')
1717
expect(deployResponse).toHaveProperty('Hash')
@@ -24,11 +24,10 @@ describe('uploadFiles', () => {
2424

2525
it('should upload folder to ipfs when correct path is provided', async () => {
2626
const path = resolve(process.cwd(), 'src/Lighthouse/tests/testImages')
27-
const full_deployResponse = (await lighthouse.upload(path, apiKey, true))
27+
const full_deployResponse = (await lighthouse.upload(path, apiKey))
2828
.data
2929

30-
expect(full_deployResponse.length).toBeGreaterThan(1)
31-
const deployResponse = full_deployResponse[0]
30+
const deployResponse = full_deployResponse
3231
expect(deployResponse).toHaveProperty('Name')
3332
expect(deployResponse).toHaveProperty('Hash')
3433
expect(deployResponse).toHaveProperty('Size')
@@ -41,7 +40,7 @@ describe('uploadFiles', () => {
4140
it('should not upload to ipfs when incorrect path is provided', async () => {
4241
try {
4342
const path = 'invalid/path/img.svg'
44-
const deployResponse = await lighthouse.upload(path, apiKey, false)
43+
const deployResponse = await lighthouse.upload(path, apiKey)
4544
} catch (error) {
4645
expect(error.code).toBe('ENOENT')
4746
}

src/Lighthouse/upload/buffer/browser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export default async (blob: any, apiKey: string, mimeType = '') => {
1313
method: 'POST',
1414
body: formData,
1515
headers: {
16-
Encryption: 'false',
1716
'Mime-Type': mimeType,
1817
Authorization: token,
1918
},

src/Lighthouse/upload/files/browser.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import {
55
UploadFileReturnType,
66
DealParameters,
77
} from '../../../types'
8-
import { checkDuplicateFileNames, retryFetch } from '../../utils/util'
8+
import { fetchWithTimeout } from '../../utils/util'
99

1010
// eslint-disable-next-line @typescript-eslint/no-empty-function
1111
export default async <T extends boolean>(
1212
files: any,
1313
accessToken: string,
14-
multi: boolean,
1514
dealParameters: DealParameters | undefined,
1615
uploadProgressCallback?: (data: IUploadProgressCallback) => void
1716
): Promise<{ data: UploadFileReturnType<T> }> => {
1817
try {
19-
const endpoint =
20-
lighthouseConfig.lighthouseNode +
21-
`/api/v0/add?wrap-with-directory=${multi}`
22-
checkDuplicateFileNames(files)
18+
const isDirectory = [...files].some(file => file.webkitRelativePath)
19+
let endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=false`
20+
21+
if(!isDirectory && files.length > 1) {
22+
endpoint = lighthouseConfig.lighthouseNode + `/api/v0/add?wrap-with-directory=true`
23+
}
2324

2425
const formData = new FormData()
2526
for (let i = 0; i < files.length; i++) {
@@ -36,7 +37,7 @@ export default async <T extends boolean>(
3637
})
3738

3839
const response = uploadProgressCallback
39-
? await retryFetch(endpoint, {
40+
? await fetchWithTimeout(endpoint, {
4041
method: 'POST',
4142
body: formData,
4243
headers: headers,
@@ -47,7 +48,7 @@ export default async <T extends boolean>(
4748
})
4849
},
4950
})
50-
: await retryFetch(endpoint, {
51+
: await fetchWithTimeout(endpoint, {
5152
method: 'POST',
5253
body: formData,
5354
headers: headers,
@@ -59,20 +60,7 @@ export default async <T extends boolean>(
5960
}
6061

6162
const responseText = await response.text()
62-
63-
let data
64-
if (typeof responseText === 'string') {
65-
if (multi) {
66-
data = JSON.parse(
67-
`[${responseText.slice(0, -1)}]`.split('\n').join(',')
68-
)
69-
} else {
70-
const temp = responseText.split('\n')
71-
data = JSON.parse(temp[temp.length - 2])
72-
}
73-
}
74-
75-
return { data }
63+
return { data: JSON.parse(responseText) }
7664
} catch (error: any) {
7765
throw new Error(error?.message)
7866
}

src/Lighthouse/upload/files/index.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,34 @@ import {
99
async function uploadFiles(
1010
sourcePath: string | any,
1111
apiKey: string,
12-
multi?: false,
1312
dealParameters?: DealParameters,
1413
uploadProgressCallback?: (data: IUploadProgressCallback) => void
1514
): Promise<{ data: IFileUploadedResponse }>
1615

1716
async function uploadFiles(
1817
sourcePath: string | any,
1918
apiKey: string,
20-
multi?: true,
2119
dealParameters?: DealParameters,
2220
uploadProgressCallback?: (data: IUploadProgressCallback) => void
2321
): Promise<{ data: IFileUploadedResponse[] }>
2422

2523
async function uploadFiles(
2624
path: string | any,
2725
apiKey: string,
28-
multi?: boolean,
2926
dealParameters?: DealParameters,
3027
uploadProgressCallback?: (data: IUploadProgressCallback) => void
3128
) {
3229
// Upload File to IPFS
33-
34-
if (multi) {
35-
//@ts-ignore
36-
if (typeof window === 'undefined') {
37-
return await uploadFile(path, apiKey, true, dealParameters)
38-
} else {
39-
return await uploadFileBrowser(
40-
path,
41-
apiKey,
42-
true,
43-
dealParameters,
44-
uploadProgressCallback
45-
)
46-
}
30+
//@ts-ignore
31+
if (typeof window === 'undefined') {
32+
return await uploadFile(path, apiKey, dealParameters)
4733
} else {
48-
//@ts-ignore
49-
if (typeof window === 'undefined') {
50-
return await uploadFile(path, apiKey, false, dealParameters)
51-
} else {
52-
return await uploadFileBrowser(
53-
path,
54-
apiKey,
55-
false,
56-
dealParameters,
57-
uploadProgressCallback
58-
)
59-
}
34+
return await uploadFileBrowser(
35+
path,
36+
apiKey,
37+
dealParameters,
38+
uploadProgressCallback
39+
)
6040
}
6141
}
6242

src/Lighthouse/upload/files/node.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import basePathConvert from '../../utils/basePathConvert'
22
import { lighthouseConfig } from '../../../lighthouse.config'
33
import { UploadFileReturnType, DealParameters } from '../../../types'
4-
import { retryFetch } from '../../utils/util'
4+
import { fetchWithTimeout } from '../../utils/util'
55

66
export async function walk(dir: string) {
77
const { readdir, stat } = eval(`require`)('fs-extra')
@@ -25,7 +25,6 @@ export async function walk(dir: string) {
2525
export default async <T extends boolean>(
2626
sourcePath: string,
2727
apiKey: string,
28-
multi: boolean,
2928
dealParameters: DealParameters | undefined
3029
): Promise<{ data: UploadFileReturnType<T> }> => {
3130
const { createReadStream, lstatSync } = eval(`require`)('fs-extra')
@@ -36,7 +35,7 @@ export default async <T extends boolean>(
3635
try {
3736
const endpoint =
3837
lighthouseConfig.lighthouseNode +
39-
`/api/v0/add?wrap-with-directory=${multi}`
38+
`/api/v0/add?wrap-with-directory=false`
4039
if (stats.isFile()) {
4140
const data = new FormData()
4241
const stream = createReadStream(sourcePath)
@@ -48,13 +47,11 @@ export default async <T extends boolean>(
4847

4948
data.append('file', blob, path.basename(sourcePath))
5049

51-
const response = await retryFetch(endpoint, {
50+
const response = await fetchWithTimeout(endpoint, {
5251
method: 'POST',
5352
body: data,
54-
credentials: 'include',
5553
timeout: 7200000,
5654
headers: {
57-
Encryption: 'false',
5855
Authorization: token,
5956
'X-Deal-Parameter': dealParameters
6057
? JSON.stringify(dealParameters)
@@ -67,12 +64,7 @@ export default async <T extends boolean>(
6764
}
6865

6966
let responseData = (await response.text()) as any
70-
if (multi) {
71-
const temp = responseData.split('\n')
72-
responseData = JSON.parse(temp[temp.length - 2])
73-
} else {
74-
responseData = JSON.parse(responseData)
75-
}
67+
responseData = JSON.parse(responseData)
7668

7769
return { data: responseData }
7870
} else {
@@ -90,17 +82,15 @@ export default async <T extends boolean>(
9082
data.append(
9183
'file',
9284
blob,
93-
multi ? path.basename(file) : basePathConvert(sourcePath, file)
85+
basePathConvert(sourcePath, file)
9486
)
9587
}
9688

97-
const response = await retryFetch(endpoint, {
89+
const response = await fetchWithTimeout(endpoint, {
9890
method: 'POST',
9991
body: data,
100-
credentials: 'include',
10192
timeout: 7200000,
10293
headers: {
103-
Encryption: 'false',
10494
Authorization: token,
10595
'X-Deal-Parameter': dealParameters
10696
? JSON.stringify(dealParameters)
@@ -113,17 +103,7 @@ export default async <T extends boolean>(
113103
}
114104

115105
let responseData = (await response.text()) as any
116-
117-
if (typeof responseData === 'string') {
118-
if (multi) {
119-
responseData = JSON.parse(
120-
`[${responseData.slice(0, -1)}]`.split('\n').join(',')
121-
)
122-
} else {
123-
const temp = responseData.split('\n')
124-
responseData = JSON.parse(temp[temp.length - 2])
125-
}
126-
}
106+
responseData = JSON.parse(responseData)
127107

128108
return { data: responseData }
129109
}

src/Lighthouse/upload/text/browser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lighthouseConfig } from '../../../lighthouse.config'
2-
import { retryFetch } from '../../utils/util'
2+
import { fetchWithTimeout } from '../../utils/util'
33

44
export default async (text: string, apiKey: string, name: string) => {
55
try {
@@ -11,12 +11,11 @@ export default async (text: string, apiKey: string, name: string) => {
1111
const blob = new Blob([text], { type: 'text/plain' })
1212
formData.append('file', blob, name)
1313

14-
const response = await retryFetch(endpoint, {
14+
const response = await fetchWithTimeout(endpoint, {
1515
method: 'POST',
1616
body: formData,
1717
timeout: 7200000,
1818
headers: {
19-
Encryption: 'false',
2019
'Mime-Type': 'text/plain',
2120
Authorization: token,
2221
},

0 commit comments

Comments
 (0)