Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 94x 94x 2x 93x 93x 94x 94x 94x 94x 94x 94x 94x 94x 93x 93x 94x 1x 92x 92x 92x 146x 1x 1x 1x 1x 1x | import { DefaultAzureCredential } from './default-azure-credential'
import {
getAuthorizationHeader,
isReplayableBody,
sendWithRetry,
storageOAuthScope,
} from './internal/request-core'
import type {
AzureRequestCredential,
RequestSigningCredential,
RetryOptions,
} from './internal/request-core'
import { defaultFetch } from './internal/runtime'
import { requireHttpsUrl } from './internal/url'
export type AzureClientCredential = AzureRequestCredential | RequestSigningCredential
export interface AzureRequestOverrides {
scope?: string | string[]
credential?: AzureClientCredential
authorityHost?: string
/** Retries of the request; token requests use the options of their credential. */
retryOptions?: RetryOptions
}
export interface AzureClientOptions extends AzureRequestOverrides {
fetch?: typeof globalThis.fetch
}
export interface AzureRequestInit extends RequestInit {
azure?: AzureRequestOverrides
}
export class AzureClient {
private readonly options: AzureClientOptions
private readonly fetcher: typeof globalThis.fetch
private readonly defaultCredentials = new Map<string | undefined, DefaultAzureCredential>()
public constructor(options: AzureClientOptions = {}) {
if (options.authorityHost !== undefined) {
requireHttpsUrl(options.authorityHost, 'authorityHost')
}
this.options = options
this.fetcher = options.fetch ?? defaultFetch
}
public async fetch(input: RequestInfo | URL, init: AzureRequestInit = {}): Promise<Response> {
const { azure = {}, ...requestInit } = init
// `Request` turns every body into a stream, so replayability is decided on the original body.
const body = requestInit.body ?? (input instanceof Request ? input.body : null)
const replayable = isReplayableBody(body)
// A nonempty RequestInit resets navigation state even when its values match the input.
const inheritReferrer =
input instanceof Request && (input.mode !== 'navigate' || Object.keys(requestInit).length > 0)
const request = new Request(
input,
inheritReferrer
? {
...requestInit,
referrer: requestInit.referrer ?? input.referrer,
referrerPolicy: requestInit.referrerPolicy ?? input.referrerPolicy,
}
: requestInit,
)
const credential =
azure.credential ??
this.options.credential ??
this.getDefaultCredential(azure.authorityHost ?? this.options.authorityHost)
const signsRequest = 'signRequest' in credential
if (!signsRequest) {
requireHttpsUrl(request.url, 'Azure request URL')
}
const scope = azure.scope ?? this.options.scope ?? storageOAuthScope
if (signsRequest) {
await credential.signRequest(request)
} else {
const signal =
requestInit.signal !== undefined || input instanceof Request ? request.signal : undefined
request.headers.set('Authorization', await getAuthorizationHeader(credential, scope, signal))
}
return await sendWithRetry(
async () => await this.fetcher(replayable ? request.clone() : request),
{ ...this.options.retryOptions, ...azure.retryOptions, signal: request.signal, body },
)
}
private getDefaultCredential(authorityHost: string | undefined): DefaultAzureCredential {
let credential = this.defaultCredentials.get(authorityHost)
Eif (credential === undefined) {
credential = new DefaultAzureCredential({
authorityHost,
retryOptions: this.options.retryOptions,
fetch: this.fetcher,
})
this.defaultCredentials.set(authorityHost, credential)
}
return credential
}
}
|