@@ -68,22 +68,30 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
68
68
) ;
69
69
} ;
70
70
71
- private readonly baseUrl ;
71
+ private readonly baseUrl : URL ;
72
72
73
73
private instrumentation : BlobArchiveClientInstrumentation ;
74
74
75
75
constructor ( baseUrl : string , telemetry : TelemetryClient = getTelemetryClient ( ) ) {
76
- this . baseUrl = baseUrl . replace ( / ^ h t t p s ? : \/ \/ / , '' ) ;
76
+ this . baseUrl = new URL ( baseUrl ) ;
77
+ if ( this . baseUrl . protocol !== 'https:' ) {
78
+ throw new TypeError ( 'BaseURL must be secure: ' + baseUrl ) ;
79
+ }
77
80
this . instrumentation = new BlobArchiveClientInstrumentation (
78
81
telemetry ,
79
- new URL ( baseUrl ) . host ,
82
+ this . baseUrl . origin ,
80
83
'BlobscanArchiveClient' ,
81
84
) ;
82
85
}
83
86
84
87
public async getLatestBlock ( ) : Promise < { hash : string ; number : number ; slot : number } > {
85
- const url = `https://${ this . baseUrl } /blocks?sort=desc&type=canonical&p=1&ps=1` ;
86
- this . logger . trace ( `Fetching latest block from ${ url } ` ) ;
88
+ const url = new URL ( 'blocks' , this . baseUrl ) ;
89
+ url . searchParams . set ( 'sort' , 'desc' ) ;
90
+ url . searchParams . set ( 'type' , 'canonical' ) ;
91
+ url . searchParams . set ( 'p' , '1' ) ;
92
+ url . searchParams . set ( 'ps' , '1' ) ;
93
+
94
+ this . logger . trace ( `Fetching latest block from ${ url . href } ` ) ;
87
95
const response = await this . fetch ( url , this . fetchOpts ) ;
88
96
89
97
if ( response . status !== 200 ) {
@@ -106,12 +114,15 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
106
114
}
107
115
108
116
public getBaseUrl ( ) : string {
109
- return this . baseUrl ;
117
+ return this . baseUrl . href ;
110
118
}
111
119
112
120
public async getBlobsFromBlock ( blockId : string ) : Promise < BlobJson [ ] | undefined > {
113
- const url = `https://${ this . baseUrl } /blocks/${ blockId } ?type=canonical&expand=blob%2Cblob_data` ;
114
- this . logger . trace ( `Fetching blobs for block ${ blockId } from ${ url } ` ) ;
121
+ const url = new URL ( `blocks/${ blockId } ` , this . baseUrl ) ;
122
+ url . searchParams . set ( 'type' , 'canonical' ) ;
123
+ url . searchParams . set ( 'expand' , 'blob,blob_data' ) ;
124
+
125
+ this . logger . trace ( `Fetching blobs for block ${ blockId } from ${ url . href } ` ) ;
115
126
const response = await this . fetch ( url , this . fetchOpts ) ;
116
127
117
128
this . instrumentation . incRequest ( 'blocks' , response . status ) ;
@@ -137,7 +148,9 @@ export class BlobscanArchiveClient implements BlobArchiveClient {
137
148
}
138
149
139
150
public async getBlobData ( id : string ) : Promise < Buffer | undefined > {
140
- const response = await this . fetch ( `https://${ this . baseUrl } /blobs/${ id } /data` , this . fetchOpts ) ;
151
+ const url = new URL ( `blobs/${ id } /data` , this . baseUrl ) ;
152
+
153
+ const response = await this . fetch ( url , this . fetchOpts ) ;
141
154
this . instrumentation . incRequest ( 'blobs' , response . status ) ;
142
155
if ( response . status === 404 ) {
143
156
return undefined ;
0 commit comments