Skip to content

Commit 878fefc

Browse files
authored
Merge pull request #116 from lighthouse-web3/v0.3.6
V0.3.6
2 parents 3a4f192 + 4c69521 commit 878fefc

File tree

10 files changed

+137
-75
lines changed

10 files changed

+137
-75
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.5-green"/>
1+
# Lighthouse <img src="https://img.shields.io/badge/BETA-v0.3.6-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.5",
3+
"version": "0.3.6",
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.5')
75+
widgets.version('0.3.6')
7676

7777
widgets
7878
.command('wallet')

src/Lighthouse/upload/files/browser.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default async <T extends boolean>(
1313
accessToken: string,
1414
multi: boolean,
1515
dealParameters: DealParameters | undefined,
16-
uploadProgressCallback: (data: IUploadProgressCallback) => void
16+
uploadProgressCallback?: (data: IUploadProgressCallback) => void
1717
): Promise<{ data: UploadFileReturnType<T> }> => {
1818
try {
1919
const endpoint =
@@ -35,35 +35,30 @@ export default async <T extends boolean>(
3535
: 'null',
3636
})
3737

38-
const response = await retryFetch(endpoint, {
39-
method: 'POST',
40-
body: formData,
41-
headers: headers,
42-
timeout: 7200000,
43-
})
38+
const response = uploadProgressCallback
39+
? await retryFetch(endpoint, {
40+
method: 'POST',
41+
body: formData,
42+
headers: headers,
43+
timeout: 7200000,
44+
onProgress: (progress) => {
45+
uploadProgressCallback({
46+
progress: progress,
47+
})
48+
},
49+
})
50+
: await retryFetch(endpoint, {
51+
method: 'POST',
52+
body: formData,
53+
headers: headers,
54+
timeout: 7200000,
55+
})
4456

4557
if (!response.ok) {
4658
throw new Error(`Request failed with status code ${response.status}`)
4759
}
4860

49-
const reader = response.body?.getReader()
50-
const decoder = new TextDecoder()
51-
let responseText = ''
52-
53-
if (reader) {
54-
let totalBytes = 0
55-
while (true) {
56-
const { done, value } = await reader.read()
57-
if (done) break
58-
totalBytes += value?.length || 0
59-
responseText += decoder.decode(value, { stream: true })
60-
uploadProgressCallback({
61-
progress: 1, // We can't accurately calculate progress without knowing the total size
62-
total: totalBytes,
63-
uploaded: totalBytes,
64-
})
65-
}
66-
}
61+
const responseText = await response.text()
6762

6863
let data
6964
if (typeof responseText === 'string') {

src/Lighthouse/upload/files/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ async function uploadFiles(
4141
apiKey,
4242
true,
4343
dealParameters,
44-
uploadProgressCallback ||
45-
(() => {
46-
return
47-
})
44+
uploadProgressCallback
4845
)
4946
}
5047
} else {
@@ -57,10 +54,7 @@ async function uploadFiles(
5754
apiKey,
5855
false,
5956
dealParameters,
60-
uploadProgressCallback ||
61-
(() => {
62-
return
63-
})
57+
uploadProgressCallback
6458
)
6559
}
6660
}

src/Lighthouse/uploadEncrypted/encrypt/file/browser.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default async (
2929
apiKey: string,
3030
publicKey: string,
3131
auth_token: string,
32-
uploadProgressCallback: (data: IUploadProgressCallback) => void
32+
uploadProgressCallback?: (data: IUploadProgressCallback) => void
3333
): Promise<{ data: IFileUploadedResponse[] }> => {
3434
try {
3535
let keyMap = {} as any
@@ -73,40 +73,42 @@ export default async (
7373
)
7474
})
7575

76-
const controller = new AbortController()
77-
const signal = controller.signal
78-
const response = await retryFetch(endpoint, {
79-
method: 'POST',
80-
body: formData,
81-
timeout: 7200000,
82-
headers: {
83-
Encryption: `${true}`,
84-
Authorization: token,
85-
},
86-
signal,
87-
})
76+
const response = uploadProgressCallback
77+
? await retryFetch(endpoint, {
78+
method: 'POST',
79+
body: formData,
80+
timeout: 7200000,
81+
headers: {
82+
Encryption: `${true}`,
83+
Authorization: token,
84+
},
85+
onProgress: (progress) => {
86+
uploadProgressCallback({
87+
progress: progress,
88+
})
89+
},
90+
})
91+
: await retryFetch(endpoint, {
92+
method: 'POST',
93+
body: formData,
94+
timeout: 7200000,
95+
headers: {
96+
Encryption: `${true}`,
97+
Authorization: token,
98+
},
99+
})
88100
if (!response.ok) {
89101
throw new Error(`HTTP error! status: ${response.status}`)
90102
}
91103

92104
const reader = response.body?.getReader()
93-
const contentLength = +response.headers.get('Content-Length')!
94-
let receivedLength = 0
95105
let chunks = []
96106
while (true) {
97107
const { done, value } = await reader!.read()
98108
if (done) {
99109
break
100110
}
101111
chunks.push(value)
102-
receivedLength += value.length
103-
uploadProgressCallback({
104-
progress: contentLength
105-
? Math.round(receivedLength / contentLength)
106-
: 0,
107-
total: contentLength || 0,
108-
uploaded: receivedLength,
109-
})
110112
}
111113

112114
let responseData = new TextDecoder('utf-8').decode(

src/Lighthouse/utils/util.ts

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ethers } from 'ethers'
22

33
interface FetchOptions extends RequestInit {
44
timeout?: number
5+
onProgress?: (progress: number) => void
56
}
67

78
const isCID = (cid: string) => {
@@ -41,17 +42,85 @@ async function fetchWithTimeout(
4142
resource: string,
4243
options: FetchOptions
4344
): Promise<Response> {
44-
const { timeout = 8000, ...rest } = options
45+
const { timeout = 8000, onProgress, ...rest } = options
4546

4647
const controller = new AbortController()
4748
const id = setTimeout(() => controller.abort(), timeout)
49+
4850
try {
49-
const response = await fetch(resource, {
50-
...rest,
51-
signal: controller.signal,
52-
})
53-
clearTimeout(id)
54-
return response
51+
if (onProgress && rest.body instanceof FormData) {
52+
return new Promise<Response>((resolve, reject) => {
53+
const xhr = new XMLHttpRequest()
54+
xhr.open(rest.method || 'GET', resource)
55+
56+
if (rest.headers) {
57+
if (rest.headers instanceof Headers) {
58+
rest.headers.forEach((value, key) => {
59+
xhr.setRequestHeader(key, value)
60+
})
61+
} else if (typeof rest.headers === 'object') {
62+
for (const [key, value] of Object.entries(rest.headers)) {
63+
xhr.setRequestHeader(key, value as string)
64+
}
65+
}
66+
}
67+
68+
xhr.timeout = timeout
69+
xhr.onload = () => {
70+
const headers = new Headers()
71+
xhr
72+
.getAllResponseHeaders()
73+
.trim()
74+
.split(/[\r\n]+/)
75+
.forEach((line) => {
76+
const parts = line.split(': ')
77+
const header = parts.shift()
78+
const value = parts.join(': ')
79+
if (header) headers.set(header, value)
80+
})
81+
82+
resolve(
83+
new Response(xhr.response, {
84+
status: xhr.status,
85+
statusText: xhr.statusText,
86+
headers: headers,
87+
})
88+
)
89+
}
90+
xhr.onerror = () => reject(new Error('Network error'))
91+
xhr.ontimeout = () => reject(new Error('Request timed out'))
92+
xhr.upload.onprogress = (event: ProgressEvent) => {
93+
if (event.lengthComputable) {
94+
onProgress(event.loaded / event.total)
95+
}
96+
}
97+
98+
// Handle different body types
99+
if (rest.body instanceof FormData) {
100+
xhr.send(rest.body)
101+
} else if (rest.body instanceof ReadableStream) {
102+
// Convert ReadableStream to Blob and then send
103+
new Response(rest.body).blob().then((blob) => xhr.send(blob))
104+
} else if (
105+
typeof rest.body === 'string' ||
106+
rest.body instanceof Blob ||
107+
rest.body instanceof ArrayBuffer
108+
) {
109+
xhr.send(rest.body)
110+
} else if (rest.body == null) {
111+
xhr.send()
112+
} else {
113+
reject(new Error('Unsupported body type'))
114+
}
115+
})
116+
} else {
117+
const response = await fetch(resource, {
118+
...rest,
119+
signal: controller.signal,
120+
})
121+
clearTimeout(id)
122+
return response
123+
}
55124
} catch (error) {
56125
clearTimeout(id)
57126
throw error

src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export interface IUploadProgressCallback {
22
progress: number
3-
total: number
4-
uploaded: number
53
}
64

75
export interface IFileUploadedResponse {

tsconfig.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
"compilerOptions": {
33
"outDir": "dist",
44
"target": "es2022",
5-
"lib": [
6-
"es2019"
7-
],
5+
"lib": ["es2019", "es6", "dom"],
86
"module": "commonjs",
97
"sourceMap": false,
108
"strict": true,
11-
"esModuleInterop": true,
9+
"esModuleInterop": true,
1210
"skipLibCheck": true,
1311
"forceConsistentCasingInFileNames": true,
1412
"experimentalDecorators": true,
@@ -19,5 +17,11 @@
1917
"resolveJsonModule": true
2018
},
2119
"include": ["src"],
22-
"exclude": ["node_modules", "**/*.spec.ts","**/*.test.ts",".vscode",".d.ts"]
20+
"exclude": [
21+
"node_modules",
22+
"**/*.spec.ts",
23+
"**/*.test.ts",
24+
".vscode",
25+
".d.ts"
26+
]
2327
}

0 commit comments

Comments
 (0)