@bsv/sdk 2.0.3 → 2.0.4
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/auth/Peer.js +35 -33
- package/dist/cjs/src/auth/Peer.js.map +1 -1
- package/dist/cjs/src/auth/clients/AuthFetch.js +31 -5
- package/dist/cjs/src/auth/clients/AuthFetch.js.map +1 -1
- package/dist/cjs/src/auth/clients/__tests__/AuthFetch.test.js +77 -0
- package/dist/cjs/src/auth/clients/__tests__/AuthFetch.test.js.map +1 -1
- package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js +4 -1
- package/dist/cjs/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/auth/Peer.js +35 -33
- package/dist/esm/src/auth/Peer.js.map +1 -1
- package/dist/esm/src/auth/clients/AuthFetch.js +31 -5
- package/dist/esm/src/auth/clients/AuthFetch.js.map +1 -1
- package/dist/esm/src/auth/clients/__tests__/AuthFetch.test.js +77 -0
- package/dist/esm/src/auth/clients/__tests__/AuthFetch.test.js.map +1 -1
- package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js +4 -1
- package/dist/esm/src/auth/transports/SimplifiedFetchTransport.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/auth/Peer.d.ts.map +1 -1
- package/dist/types/src/auth/clients/AuthFetch.d.ts.map +1 -1
- package/dist/types/src/auth/transports/SimplifiedFetchTransport.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/dist/umd/bundle.js.map +1 -1
- package/package.json +1 -1
- package/src/auth/Peer.ts +49 -47
- package/src/auth/__tests/Peer.test.ts +35 -30
- package/src/auth/clients/AuthFetch.ts +46 -18
- package/src/auth/clients/__tests__/AuthFetch.test.ts +97 -0
- package/src/auth/transports/SimplifiedFetchTransport.ts +24 -21
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { AuthMessage, RequestedCertificateSet, Transport } from '../types.js'
|
|
4
4
|
import * as Utils from '../../primitives/utils.js'
|
|
5
5
|
|
|
6
|
-
const defaultFetch: typeof fetch =
|
|
6
|
+
const defaultFetch: typeof fetch =
|
|
7
7
|
typeof globalThis !== 'undefined' && typeof globalThis.fetch === 'function'
|
|
8
8
|
? globalThis.fetch.bind(globalThis)
|
|
9
9
|
: fetch
|
|
@@ -22,7 +22,7 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
22
22
|
* @param baseUrl - The base URL for all HTTP requests made by this transport.
|
|
23
23
|
* @param fetchClient - A fetch implementation to use for HTTP requests (default: global fetch).
|
|
24
24
|
*/
|
|
25
|
-
constructor
|
|
25
|
+
constructor(baseUrl: string, fetchClient = defaultFetch) {
|
|
26
26
|
if (typeof fetchClient !== 'function') {
|
|
27
27
|
throw new Error(
|
|
28
28
|
'SimplifiedFetchTransport requires a fetch implementation. ' +
|
|
@@ -44,7 +44,7 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
44
44
|
*
|
|
45
45
|
* @throws Will throw an error if no listener has been registered via `onData`.
|
|
46
46
|
*/
|
|
47
|
-
async send
|
|
47
|
+
async send(message: AuthMessage): Promise<void> {
|
|
48
48
|
if (this.onDataCallback == null) {
|
|
49
49
|
throw new Error('Listen before you start speaking. God gave you two ears and one mouth for a reason.')
|
|
50
50
|
}
|
|
@@ -240,24 +240,27 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
240
240
|
* @param callback - A function to invoke when an incoming AuthMessage is received.
|
|
241
241
|
* @returns A promise that resolves once the callback is set.
|
|
242
242
|
*/
|
|
243
|
-
async onData
|
|
243
|
+
async onData(callback: (message: AuthMessage) => Promise<void>): Promise<void> {
|
|
244
244
|
this.onDataCallback = (m) => {
|
|
245
|
-
void callback(m)
|
|
245
|
+
void callback(m).catch(() => {
|
|
246
|
+
// Errors from handleIncomingMessage on the client side are not
|
|
247
|
+
// actionable here — prevent unhandled promise rejections.
|
|
248
|
+
})
|
|
246
249
|
}
|
|
247
250
|
}
|
|
248
251
|
|
|
249
|
-
private createNetworkError
|
|
252
|
+
private createNetworkError(url: string, originalError: unknown): Error {
|
|
250
253
|
const baseMessage = `Network error while sending authenticated request to ${url}`
|
|
251
254
|
if (originalError instanceof Error) {
|
|
252
255
|
const error = new Error(`${baseMessage}: ${originalError.message}`)
|
|
253
256
|
error.stack = originalError.stack
|
|
254
|
-
|
|
257
|
+
; (error as any).cause = originalError
|
|
255
258
|
return error
|
|
256
259
|
}
|
|
257
260
|
return new Error(`${baseMessage}: ${String(originalError)}`)
|
|
258
261
|
}
|
|
259
262
|
|
|
260
|
-
private createUnauthenticatedResponseError
|
|
263
|
+
private createUnauthenticatedResponseError(
|
|
261
264
|
url: string,
|
|
262
265
|
response: Response,
|
|
263
266
|
bodyBytes: number[],
|
|
@@ -277,17 +280,17 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
277
280
|
}
|
|
278
281
|
|
|
279
282
|
const error = new Error(parts.join(' - '))
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
283
|
+
; (error as any).details = {
|
|
284
|
+
url,
|
|
285
|
+
status: response.status,
|
|
286
|
+
statusText: response.statusText,
|
|
287
|
+
missingHeaders,
|
|
288
|
+
bodyPreview
|
|
289
|
+
}
|
|
287
290
|
return error
|
|
288
291
|
}
|
|
289
292
|
|
|
290
|
-
private createMalformedHeaderError
|
|
293
|
+
private createMalformedHeaderError(
|
|
291
294
|
url: string,
|
|
292
295
|
headerName: string,
|
|
293
296
|
headerValue: string,
|
|
@@ -297,13 +300,13 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
297
300
|
if (cause instanceof Error) {
|
|
298
301
|
const error = new Error(`${errorMessage}. ${cause.message}`)
|
|
299
302
|
error.stack = cause.stack
|
|
300
|
-
|
|
303
|
+
; (error as any).cause = cause
|
|
301
304
|
return error
|
|
302
305
|
}
|
|
303
306
|
return new Error(`${errorMessage}. ${String(cause)}`)
|
|
304
307
|
}
|
|
305
308
|
|
|
306
|
-
private getBodyPreview
|
|
309
|
+
private getBodyPreview(bodyBytes: number[], contentType: string | null): string | undefined {
|
|
307
310
|
if (bodyBytes.length === 0) {
|
|
308
311
|
return undefined
|
|
309
312
|
}
|
|
@@ -333,7 +336,7 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
333
336
|
return preview
|
|
334
337
|
}
|
|
335
338
|
|
|
336
|
-
private isTextualContent
|
|
339
|
+
private isTextualContent(contentType: string | null, sample: number[]): boolean {
|
|
337
340
|
if (sample.length === 0) {
|
|
338
341
|
return false
|
|
339
342
|
}
|
|
@@ -367,7 +370,7 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
367
370
|
return (printableCount / sample.length) > 0.8
|
|
368
371
|
}
|
|
369
372
|
|
|
370
|
-
private formatBinaryPreview
|
|
373
|
+
private formatBinaryPreview(bytes: number[], truncated: boolean): string {
|
|
371
374
|
const hex = bytes.map(byte => byte.toString(16).padStart(2, '0')).join('')
|
|
372
375
|
return `0x${hex}${truncated ? '…' : ''}`
|
|
373
376
|
}
|
|
@@ -379,7 +382,7 @@ export class SimplifiedFetchTransport implements Transport {
|
|
|
379
382
|
* @returns An object representing the deserialized request, including the method,
|
|
380
383
|
* URL postfix (path and query string), headers, body, and request ID.
|
|
381
384
|
*/
|
|
382
|
-
deserializeRequestPayload
|
|
385
|
+
deserializeRequestPayload(payload: number[]): {
|
|
383
386
|
method: string
|
|
384
387
|
urlPostfix: string
|
|
385
388
|
headers: Record<string, string>
|