Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-11-06 14:42:23 +01:00
parent aec712fae9
commit ce632aaa38
2 changed files with 14 additions and 6 deletions
@@ -55,8 +55,12 @@ export class IdentityAuthInjectorFetchMiddleware implements FetchMiddleware {
apply(next: typeof fetch): typeof fetch {
return async (input, init) => {
// Skip this middleware if the header already exists, or if the URL
// doesn't match any of the allowlist items, or if there was no token
const request = new Request(input, init);
// doesn't match any of the allowlist items, or if there was no token.
// NOTE(freben): The "as any" casts here and below are because of subtle
// undici type differences that happened in a node types bump. Those are
// immaterial to the code at hand at runtime, as the global fetch and
// Request are always taken from the same place.
const request = new Request(input as any, init);
const { token } = await this.identityApi.getCredentials();
if (
request.headers.get(this.headerName) ||
@@ -64,7 +68,7 @@ export class IdentityAuthInjectorFetchMiddleware implements FetchMiddleware {
!token ||
!this.allowUrl(request.url)
) {
return next(input, init);
return next(input as any, init);
}
request.headers.set(this.headerName, this.headerValue(token));
@@ -34,11 +34,15 @@ export class PluginProtocolResolverFetchMiddleware implements FetchMiddleware {
apply(next: typeof fetch): typeof fetch {
return async (input, init) => {
const request = new Request(input, init);
// NOTE(freben): The "as any" casts here and below are because of subtle
// undici type differences that happened in a node types bump. Those are
// immaterial to the code at hand at runtime, as the global fetch and
// Request are always taken from the same place.
const request = new Request(input as any, init);
const prefix = 'plugin://';
if (!request.url.startsWith(prefix)) {
return next(input, init);
return next(input as any, init);
}
// Switch to a known protocol, since browser URL parsing misbehaves wildly
@@ -57,7 +61,7 @@ export class PluginProtocolResolverFetchMiddleware implements FetchMiddleware {
const target = `${join(base, pathname)}${search}${hash}`;
return next(
target,
typeof input === 'string' || isUrl(input) ? init : input,
typeof input === 'string' || isUrl(input) ? init : (input as any),
);
};
}