(message: JSONRPCMessage)
| 241 | } |
| 242 | |
| 243 | async send(message: JSONRPCMessage): Promise<void> { |
| 244 | if (!this._endpoint) { |
| 245 | throw new Error('Not connected'); |
| 246 | } |
| 247 | |
| 248 | try { |
| 249 | const headers = await this._commonHeaders(); |
| 250 | headers.set('content-type', 'application/json'); |
| 251 | const init = { |
| 252 | ...this._requestInit, |
| 253 | method: 'POST', |
| 254 | headers, |
| 255 | body: JSON.stringify(message), |
| 256 | signal: this._abortController?.signal |
| 257 | }; |
| 258 | |
| 259 | const response = await (this._fetch ?? fetch)(this._endpoint, init); |
| 260 | if (!response.ok) { |
| 261 | const text = await response.text().catch(() => null); |
| 262 | |
| 263 | if (response.status === 401 && this._authProvider) { |
| 264 | const { resourceMetadataUrl, scope } = extractWWWAuthenticateParams(response); |
| 265 | this._resourceMetadataUrl = resourceMetadataUrl; |
| 266 | this._scope = scope; |
| 267 | |
| 268 | const result = await auth(this._authProvider, { |
| 269 | serverUrl: this._url, |
| 270 | resourceMetadataUrl: this._resourceMetadataUrl, |
| 271 | scope: this._scope, |
| 272 | fetchFn: this._fetchWithInit |
| 273 | }); |
| 274 | if (result !== 'AUTHORIZED') { |
| 275 | throw new UnauthorizedError(); |
| 276 | } |
| 277 | |
| 278 | // Purposely _not_ awaited, so we don't call onerror twice |
| 279 | return this.send(message); |
| 280 | } |
| 281 | |
| 282 | throw new Error(`Error POSTing to endpoint (HTTP ${response.status}): ${text}`); |
| 283 | } |
| 284 | |
| 285 | // Release connection - POST responses don't have content we need |
| 286 | await response.body?.cancel(); |
| 287 | } catch (error) { |
| 288 | this.onerror?.(error as Error); |
| 289 | throw error; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | setProtocolVersion(version: string): void { |
| 294 | this._protocolVersion = version; |
nothing calls this directly
no test coverage detected