@bsv/sdk 1.6.25 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/identity/ContactsManager.js +50 -14
- package/dist/cjs/src/identity/ContactsManager.js.map +1 -1
- package/dist/cjs/src/storage/StorageDownloader.js +24 -10
- package/dist/cjs/src/storage/StorageDownloader.js.map +1 -1
- package/dist/cjs/src/storage/StorageUploader.js +5 -4
- package/dist/cjs/src/storage/StorageUploader.js.map +1 -1
- package/dist/cjs/src/storage/StorageUtils.js +11 -3
- package/dist/cjs/src/storage/StorageUtils.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/identity/ContactsManager.js +48 -14
- package/dist/esm/src/identity/ContactsManager.js.map +1 -1
- package/dist/esm/src/storage/StorageDownloader.js +24 -10
- package/dist/esm/src/storage/StorageDownloader.js.map +1 -1
- package/dist/esm/src/storage/StorageUploader.js +5 -4
- package/dist/esm/src/storage/StorageUploader.js.map +1 -1
- package/dist/esm/src/storage/StorageUtils.js +11 -3
- package/dist/esm/src/storage/StorageUtils.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/identity/ContactsManager.d.ts +2 -0
- package/dist/types/src/identity/ContactsManager.d.ts.map +1 -1
- package/dist/types/src/storage/StorageDownloader.d.ts +1 -1
- package/dist/types/src/storage/StorageDownloader.d.ts.map +1 -1
- package/dist/types/src/storage/StorageUploader.d.ts +1 -1
- package/dist/types/src/storage/StorageUploader.d.ts.map +1 -1
- package/dist/types/src/storage/StorageUtils.d.ts +3 -2
- package/dist/types/src/storage/StorageUtils.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +3 -3
- package/dist/umd/bundle.js.map +1 -1
- package/docs/reference/storage.md +12 -5
- package/docs/tutorials/uhrp-storage.md +6 -6
- package/package.json +1 -1
- package/src/identity/ContactsManager.ts +54 -14
- package/src/identity/__tests/IdentityClient.test.ts +119 -85
- package/src/storage/StorageDownloader.ts +28 -12
- package/src/storage/StorageUploader.ts +6 -5
- package/src/storage/StorageUtils.ts +12 -4
- package/src/storage/__tests/StorageDownloader.test.ts +34 -2
- package/src/storage/__tests/StorageUploader.test.ts +20 -0
|
@@ -8,7 +8,7 @@ export interface UploaderConfig {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export interface UploadableFile {
|
|
11
|
-
data: number[]
|
|
11
|
+
data: Uint8Array | number[]
|
|
12
12
|
type: string
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -108,7 +108,7 @@ export class StorageUploader {
|
|
|
108
108
|
file: UploadableFile,
|
|
109
109
|
requiredHeaders: Record<string, string>
|
|
110
110
|
): Promise<UploadFileResult> {
|
|
111
|
-
const body = Uint8Array.from(file.data)
|
|
111
|
+
const body = file.data instanceof Uint8Array ? file.data : Uint8Array.from(file.data)
|
|
112
112
|
|
|
113
113
|
const response = await fetch(uploadURL, {
|
|
114
114
|
method: 'PUT',
|
|
@@ -122,7 +122,7 @@ export class StorageUploader {
|
|
|
122
122
|
throw new Error(`File upload failed: HTTP ${response.status}`)
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
const uhrpURL =
|
|
125
|
+
const uhrpURL = StorageUtils.getURLForFile(body)
|
|
126
126
|
return {
|
|
127
127
|
published: true,
|
|
128
128
|
uhrpURL
|
|
@@ -148,10 +148,11 @@ export class StorageUploader {
|
|
|
148
148
|
retentionPeriod: number
|
|
149
149
|
}): Promise<UploadFileResult> {
|
|
150
150
|
const { file, retentionPeriod } = params
|
|
151
|
-
const
|
|
151
|
+
const data = file.data instanceof Uint8Array ? file.data : Uint8Array.from(file.data)
|
|
152
|
+
const fileSize = data.byteLength
|
|
152
153
|
|
|
153
154
|
const { uploadURL, requiredHeaders } = await this.getUploadInfo(fileSize, retentionPeriod)
|
|
154
|
-
return await this.uploadFile(uploadURL, file, requiredHeaders)
|
|
155
|
+
return await this.uploadFile(uploadURL, { data, type: file.type }, requiredHeaders)
|
|
155
156
|
}
|
|
156
157
|
|
|
157
158
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { sha256 } from '../primitives/Hash.js'
|
|
2
1
|
import { toHex, fromBase58Check, toBase58Check, toArray } from '../primitives/utils.js'
|
|
2
|
+
import { Hash } from '../primitives/index.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Takes a UHRP URL and removes any prefixes.
|
|
@@ -26,11 +26,19 @@ export const getURLForHash = (hash: number[]): string => {
|
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Generates a UHRP URL for a given file.
|
|
29
|
-
*
|
|
29
|
+
* Uses a streaming hash to avoid excessive memory usage with large files.
|
|
30
|
+
* @param {Uint8Array | number[]} file - File content as a typed array or number array.
|
|
30
31
|
* @returns {string} - Base58Check encoded URL.
|
|
31
32
|
*/
|
|
32
|
-
export const getURLForFile = (file: number[]): string => {
|
|
33
|
-
const
|
|
33
|
+
export const getURLForFile = (file: Uint8Array | number[]): string => {
|
|
34
|
+
const data = file instanceof Uint8Array ? file : Uint8Array.from(file)
|
|
35
|
+
const hasher = new Hash.SHA256()
|
|
36
|
+
const chunkSize = 1024 * 1024
|
|
37
|
+
for (let i = 0; i < data.length; i += chunkSize) {
|
|
38
|
+
const chunk = data.subarray(i, i + chunkSize)
|
|
39
|
+
hasher.update(Array.from(chunk))
|
|
40
|
+
}
|
|
41
|
+
const hash = hasher.digest()
|
|
34
42
|
return getURLForHash(hash)
|
|
35
43
|
}
|
|
36
44
|
|
|
@@ -3,8 +3,9 @@ import { StorageUtils } from '../index.js'
|
|
|
3
3
|
import { LookupResolver } from '../../overlay-tools/index.js'
|
|
4
4
|
import Transaction from '../../transaction/Transaction.js'
|
|
5
5
|
import PushDrop from '../../script/templates/PushDrop.js'
|
|
6
|
-
import {
|
|
6
|
+
import { PublicKey } from '../../primitives/index.js'
|
|
7
7
|
import { Utils } from '../../primitives/index.js'
|
|
8
|
+
import { ReadableStream } from 'stream/web'
|
|
8
9
|
|
|
9
10
|
beforeEach(() => {
|
|
10
11
|
jest.restoreAllMocks()
|
|
@@ -125,7 +126,7 @@ describe('StorageDownloader', () => {
|
|
|
125
126
|
const result = await downloader.download('validUrl')
|
|
126
127
|
expect(fetchSpy).toHaveBeenCalledTimes(2)
|
|
127
128
|
expect(result).toEqual({
|
|
128
|
-
data: new
|
|
129
|
+
data: new Uint8Array(32).fill(0),
|
|
129
130
|
mimeType: 'application/test'
|
|
130
131
|
})
|
|
131
132
|
})
|
|
@@ -200,5 +201,36 @@ describe('StorageDownloader', () => {
|
|
|
200
201
|
.resolves
|
|
201
202
|
.toEqual(["", ""])
|
|
202
203
|
})
|
|
204
|
+
|
|
205
|
+
it.skip('downloads and verifies large streamed content', async () => {
|
|
206
|
+
const size = 5 * 1024 * 1024
|
|
207
|
+
const data = new Uint8Array(size)
|
|
208
|
+
for (let i = 0; i < size; i++) data[i] = i % 256
|
|
209
|
+
const uhrpUrl = StorageUtils.getURLForFile(data)
|
|
210
|
+
|
|
211
|
+
jest.spyOn(downloader, 'resolve').mockResolvedValue(['http://large-file'])
|
|
212
|
+
|
|
213
|
+
const chunkSize = 64 * 1024
|
|
214
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
215
|
+
start (controller) {
|
|
216
|
+
for (let offset = 0; offset < data.length; offset += chunkSize) {
|
|
217
|
+
controller.enqueue(data.subarray(offset, offset + chunkSize))
|
|
218
|
+
}
|
|
219
|
+
controller.close()
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
jest.spyOn(global, 'fetch').mockResolvedValue(
|
|
224
|
+
new Response(stream as any, {
|
|
225
|
+
status: 200,
|
|
226
|
+
headers: { 'Content-Type': 'application/octet-stream' }
|
|
227
|
+
})
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
const result = await downloader.download(uhrpUrl)
|
|
231
|
+
expect(result.mimeType).toBe('application/octet-stream')
|
|
232
|
+
expect(result.data.length).toBe(size)
|
|
233
|
+
expect(result.data).toEqual(data)
|
|
234
|
+
})
|
|
203
235
|
})
|
|
204
236
|
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { StorageUploader } from '../StorageUploader.js'
|
|
2
2
|
import * as StorageUtils from '../StorageUtils.js'
|
|
3
3
|
import WalletClient from '../../wallet/WalletClient.js'
|
|
4
|
+
import { createHash } from 'crypto'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* A helper for converting a string to a number[] of UTF-8 bytes
|
|
@@ -66,6 +67,25 @@ describe('StorageUploader Tests', () => {
|
|
|
66
67
|
expect(firstFour).toHaveLength(8)
|
|
67
68
|
})
|
|
68
69
|
|
|
70
|
+
it('should handle large file uploads efficiently', async () => {
|
|
71
|
+
const size = 5 * 1024 * 1024
|
|
72
|
+
const data = new Uint8Array(size)
|
|
73
|
+
for (let i = 0; i < size; i++) data[i] = i % 256
|
|
74
|
+
|
|
75
|
+
jest.spyOn(uploader as any, 'getUploadInfo').mockResolvedValue({
|
|
76
|
+
uploadURL: 'https://example-upload.com/put'
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const result = await uploader.publishFile({
|
|
80
|
+
file: { data, type: 'application/octet-stream' },
|
|
81
|
+
retentionPeriod: 7
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const expectedHash = createHash('sha256').update(data).digest()
|
|
85
|
+
const urlHash = StorageUtils.getHashFromURL(result.uhrpURL)
|
|
86
|
+
expect(Buffer.from(urlHash)).toEqual(expectedHash)
|
|
87
|
+
})
|
|
88
|
+
|
|
69
89
|
it('should throw if the upload fails with HTTP 500', async () => {
|
|
70
90
|
// Force the direct upload (global fetch) to fail
|
|
71
91
|
globalFetchSpy.mockResolvedValueOnce(new Response(null, { status: 500 }))
|