@bsv/sdk 1.4.1 → 1.4.3
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/registry/RegistryClient.js +112 -89
- package/dist/cjs/src/registry/RegistryClient.js.map +1 -1
- package/dist/cjs/src/storage/StorageDownloader.js +82 -0
- package/dist/cjs/src/storage/StorageDownloader.js.map +1 -0
- package/dist/cjs/src/storage/__test/StorageDownloader.test.js +144 -0
- package/dist/cjs/src/storage/__test/StorageDownloader.test.js.map +1 -0
- package/dist/cjs/src/storage/index.js +3 -1
- package/dist/cjs/src/storage/index.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/registry/RegistryClient.js +110 -88
- package/dist/esm/src/registry/RegistryClient.js.map +1 -1
- package/dist/esm/src/storage/StorageDownloader.js +75 -0
- package/dist/esm/src/storage/StorageDownloader.js.map +1 -0
- package/dist/esm/src/storage/__test/StorageDownloader.test.js +139 -0
- package/dist/esm/src/storage/__test/StorageDownloader.test.js.map +1 -0
- package/dist/esm/src/storage/index.js +1 -0
- package/dist/esm/src/storage/index.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/registry/RegistryClient.d.ts +24 -24
- package/dist/types/src/registry/RegistryClient.d.ts.map +1 -1
- package/dist/types/src/registry/types/index.d.ts +46 -19
- package/dist/types/src/registry/types/index.d.ts.map +1 -1
- package/dist/types/src/storage/StorageDownloader.d.ts +25 -0
- package/dist/types/src/storage/StorageDownloader.d.ts.map +1 -0
- package/dist/types/src/storage/__test/StorageDownloader.test.d.ts +2 -0
- package/dist/types/src/storage/__test/StorageDownloader.test.d.ts.map +1 -0
- package/dist/types/src/storage/index.d.ts +1 -0
- package/dist/types/src/storage/index.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/docs/storage.md +51 -0
- package/package.json +1 -1
- package/src/registry/RegistryClient.ts +142 -114
- package/src/registry/__tests/RegistryClient.test.ts +136 -100
- package/src/registry/types/index.ts +50 -20
- package/src/storage/StorageDownloader.ts +91 -0
- package/src/storage/__test/StorageDownloader.test.ts +170 -0
- package/src/storage/index.ts +3 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { StorageDownloader } from '../StorageDownloader.js'
|
|
2
|
+
import { StorageUtils } from '../index.js'
|
|
3
|
+
import { LookupResolver } from '../../overlay-tools/index.js'
|
|
4
|
+
import Transaction from '../../transaction/Transaction.js'
|
|
5
|
+
import PushDrop from '../../script/templates/PushDrop.js'
|
|
6
|
+
import { Hash, PublicKey } from '../../primitives/index.js'
|
|
7
|
+
import { Utils } from '../../primitives/index.js'
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
jest.restoreAllMocks()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
describe('StorageDownloader', () => {
|
|
14
|
+
let downloader: StorageDownloader
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
// Create a fresh instance
|
|
18
|
+
downloader = new StorageDownloader()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('resolve()', () => {
|
|
22
|
+
it('throws if the lookup response is not "output-list"', async () => {
|
|
23
|
+
// Mock the LookupResolver to return something invalid
|
|
24
|
+
jest.spyOn(LookupResolver.prototype, 'query').mockResolvedValue({
|
|
25
|
+
type: 'something-else',
|
|
26
|
+
outputs: []
|
|
27
|
+
} as any)
|
|
28
|
+
|
|
29
|
+
await expect(downloader.resolve('fakeUhrpUrl'))
|
|
30
|
+
.rejects
|
|
31
|
+
.toThrow('Lookup answer must be an output list')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('decodes each output with Transaction.fromBEEF and PushDrop.decode', async () => {
|
|
35
|
+
// 1) Mock lookup response
|
|
36
|
+
jest.spyOn(LookupResolver.prototype, 'query').mockResolvedValue({
|
|
37
|
+
type: 'output-list',
|
|
38
|
+
outputs: [
|
|
39
|
+
{ beef: 'fake-beef-a', outputIndex: 0 },
|
|
40
|
+
{ beef: 'fake-beef-b', outputIndex: 1 }
|
|
41
|
+
]
|
|
42
|
+
} as any)
|
|
43
|
+
|
|
44
|
+
// 2) Mock Transaction.fromBEEF -> returns a dummy transaction
|
|
45
|
+
jest.spyOn(Transaction, 'fromBEEF').mockImplementation(() => {
|
|
46
|
+
// Each transaction might have multiple outputs; we only care about `outputIndex`
|
|
47
|
+
return {
|
|
48
|
+
outputs: [
|
|
49
|
+
{ lockingScript: {} }, // index 0
|
|
50
|
+
{ lockingScript: {} } // index 1
|
|
51
|
+
]
|
|
52
|
+
} as any
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// 3) Mock PushDrop.decode -> returns { fields: number[][] }
|
|
56
|
+
jest.spyOn(PushDrop, 'decode').mockImplementation(() => {
|
|
57
|
+
// The decode function returns an object with `fields`,
|
|
58
|
+
return {
|
|
59
|
+
lockingPublicKey: {} as PublicKey,
|
|
60
|
+
fields: [
|
|
61
|
+
[11],
|
|
62
|
+
[22],
|
|
63
|
+
[104, 116, 116, 112, 58, 47, 47, 97, 46, 99, 111, 109]
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
// 4) Mock Utils.toUTF8 to convert that number[] to a string
|
|
69
|
+
jest.spyOn(Utils, 'toUTF8').mockReturnValue('http://a.com')
|
|
70
|
+
|
|
71
|
+
const resolved = await downloader.resolve('fakeUhrpUrl')
|
|
72
|
+
expect(resolved).toEqual(['http://a.com', 'http://a.com'])
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('download()', () => {
|
|
77
|
+
it('throws if UHRP URL is invalid', async () => {
|
|
78
|
+
jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(false)
|
|
79
|
+
|
|
80
|
+
await expect(downloader.download('invalidUrl'))
|
|
81
|
+
.rejects
|
|
82
|
+
.toThrow('Invalid parameter UHRP url')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('throws if no hosts are found', async () => {
|
|
86
|
+
// Valid UHRP URL
|
|
87
|
+
jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
|
|
88
|
+
// Return some random 32-byte hash so we can pass the check
|
|
89
|
+
jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
|
|
90
|
+
|
|
91
|
+
// Force resolve() to return an empty array
|
|
92
|
+
jest.spyOn(downloader, 'resolve').mockResolvedValue([])
|
|
93
|
+
|
|
94
|
+
await expect(downloader.download('validButUnhostedUrl'))
|
|
95
|
+
.rejects
|
|
96
|
+
.toThrow('No one currently hosts this file!')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('downloads successfully from the first working host', async () => {
|
|
100
|
+
jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
|
|
101
|
+
const knownHash = [
|
|
102
|
+
102, 104, 122, 173, 248, 98, 189, 119, 108, 143,
|
|
103
|
+
193, 139, 142, 159, 142, 32, 8, 151, 20, 133,
|
|
104
|
+
110, 226, 51, 179, 144, 42, 89, 29, 13, 95,
|
|
105
|
+
41, 37
|
|
106
|
+
]
|
|
107
|
+
jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(knownHash)
|
|
108
|
+
|
|
109
|
+
// Suppose two possible download URLs
|
|
110
|
+
jest.spyOn(downloader, 'resolve').mockResolvedValue([
|
|
111
|
+
'http://host1/404',
|
|
112
|
+
'http://host2/ok'
|
|
113
|
+
])
|
|
114
|
+
|
|
115
|
+
// The first fetch -> 404, second fetch -> success
|
|
116
|
+
const fetchSpy = jest.spyOn(global, 'fetch')
|
|
117
|
+
.mockResolvedValueOnce(new Response(null, { status: 404 }))
|
|
118
|
+
.mockResolvedValueOnce(new Response(new Uint8Array(32).fill(0), {
|
|
119
|
+
status: 200,
|
|
120
|
+
headers: { 'Content-Type': 'application/test' }
|
|
121
|
+
}))
|
|
122
|
+
|
|
123
|
+
const result = await downloader.download('validUrl')
|
|
124
|
+
expect(fetchSpy).toHaveBeenCalledTimes(2)
|
|
125
|
+
expect(result).toEqual({
|
|
126
|
+
data: new Array(32).fill(0),
|
|
127
|
+
mimeType: 'application/test'
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('throws if content hash mismatches the UHRP hash', async () => {
|
|
132
|
+
jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
|
|
133
|
+
// The expected hash is all zeros
|
|
134
|
+
jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
|
|
135
|
+
|
|
136
|
+
// One potential host
|
|
137
|
+
jest.spyOn(downloader, 'resolve').mockResolvedValue([
|
|
138
|
+
'http://bad-content.test'
|
|
139
|
+
])
|
|
140
|
+
|
|
141
|
+
// The fetch returns 32 bytes of all 1's => hash mismatch
|
|
142
|
+
jest.spyOn(global, 'fetch').mockResolvedValue(
|
|
143
|
+
new Response(new Uint8Array(32).fill(1), { status: 200 })
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
await expect(downloader.download('validButBadHashUrl'))
|
|
147
|
+
.rejects
|
|
148
|
+
.toThrow()
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('throws if all hosts fail or mismatch', async () => {
|
|
152
|
+
jest.spyOn(StorageUtils, 'isValidURL').mockReturnValue(true)
|
|
153
|
+
jest.spyOn(StorageUtils, 'getHashFromURL').mockReturnValue(new Array(32).fill(0))
|
|
154
|
+
|
|
155
|
+
jest.spyOn(downloader, 'resolve').mockResolvedValue([
|
|
156
|
+
'http://host1.test',
|
|
157
|
+
'http://host2.test'
|
|
158
|
+
])
|
|
159
|
+
|
|
160
|
+
// Both fetches fail with 500 or something >=400
|
|
161
|
+
jest.spyOn(global, 'fetch').mockResolvedValue(
|
|
162
|
+
new Response(null, { status: 500 })
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
await expect(downloader.download('validButNoGoodHostUrl'))
|
|
166
|
+
.rejects
|
|
167
|
+
.toThrow('Unable to download content from validButNoGoodHostUrl')
|
|
168
|
+
})
|
|
169
|
+
})
|
|
170
|
+
})
|