-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from lennybakkalian/dev
feat: refactor fetch middleware and header forwarding
- Loading branch information
Showing
9 changed files
with
142 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export class Mutex { | ||
private queue: (() => void)[] = []; | ||
private locked = false; | ||
|
||
async acquire(): Promise<() => void> { | ||
return new Promise<() => void>((resolve) => { | ||
const release = () => { | ||
this.locked = false; | ||
const next = this.queue.shift(); | ||
if (next) { | ||
next(); | ||
} | ||
}; | ||
|
||
if (!this.locked) { | ||
this.locked = true; | ||
resolve(release); | ||
} else { | ||
this.queue.push(() => { | ||
this.locked = true; | ||
resolve(release); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export async function wrapInMutex<T>( | ||
fn: () => Promise<T>, | ||
mutex: Mutex, | ||
disable?: boolean | ||
): Promise<T> { | ||
if (disable) { | ||
return await fn(); | ||
} | ||
const release = await mutex.acquire(); | ||
try { | ||
return await fn(); | ||
} finally { | ||
release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {ITrpcConfig} from '../trpc.config'; | ||
import * as cookie from 'cookie'; | ||
import {Mutex, wrapInMutex} from '../libs/mutex.util'; | ||
|
||
export class FetchMiddleware { | ||
private _setCookiesCache?: string; | ||
|
||
private _mutex = new Mutex(); | ||
|
||
constructor( | ||
private _config: ITrpcConfig, | ||
private _request: Request | null, | ||
private _response: ResponseInit | null | ||
) {} | ||
|
||
async fetch(input: RequestInfo | URL | string, init?: RequestInit) { | ||
// Wrap this in mutex. Because of setCookieCache, we need to make sequential requests in SSR. | ||
// This should not be a problem, since we use batch calls. | ||
return wrapInMutex( | ||
async () => { | ||
if (typeof input != 'string') { | ||
throw new Error('[ngx-trpc] Only string urls are supported right now.'); | ||
} | ||
|
||
if (this._request) { | ||
const headers: HeadersInit = {}; | ||
this._request.headers.forEach((value: string, key: string) => { | ||
headers[key] = value; | ||
}); | ||
|
||
if (this._setCookiesCache) { | ||
const cookies = cookie.parse(headers['cookie'] || ''); | ||
const newCookies = cookie.parse(this._setCookiesCache); | ||
|
||
for (let key in newCookies) { | ||
cookies[key] = newCookies[key]; | ||
} | ||
|
||
headers['cookie'] = Object.entries(cookies) | ||
.filter(([_, value]) => value !== undefined) | ||
.map(([key, value]) => cookie.serialize(key, value!)) | ||
.join('; '); | ||
} | ||
|
||
init = {...init, headers}; | ||
} | ||
|
||
const r = await fetch(input, init); | ||
if (this._response && this._response.headers && this._response.headers instanceof Headers) { | ||
for (let [key, value] of r.headers) { | ||
if (this._config.ssr?.forwardHeaders?.includes(key) || key === 'set-cookie') { | ||
this._response.headers.set(key, value); | ||
} | ||
|
||
// set the cookie in the angular request object so we can use it in sequential requests when not using batchLink | ||
if (key == 'set-cookie') { | ||
this._setCookiesCache = value; | ||
} | ||
} | ||
} | ||
return r; | ||
}, | ||
this._mutex, | ||
this._config.ssr?.disableSequentialRequests | ||
); | ||
} | ||
} |