-
Notifications
You must be signed in to change notification settings - Fork 40k
feat(copilot): opt-in HTTP cache for the Node fetch fetcher #317721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5d2d5b
feat(copilot): opt-in HTTP cache for the Node fetch fetcher
deepak1556 6c68fab
fix: undici integration tests
deepak1556 cd23554
chore: fix lint
deepak1556 e34fb56
chore: cleanup cache marker in favor of function arg
deepak1556 f91551c
chore: fix typecheck
deepak1556 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
102 changes: 102 additions & 0 deletions
102
extensions/copilot/src/platform/networking/node/taggedCacheInterceptor.ts
This file contains hidden or 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,102 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import * as undici from 'undici'; | ||
|
|
||
| /** | ||
| * Internal response header stamped by {@link taggedCacheInterceptor} so the | ||
| * fetcher can attribute a response to the cache for telemetry purposes. | ||
| */ | ||
| export const VSCODE_CACHE_STATUS_HEADER = 'x-vscode-cache-status'; | ||
|
|
||
| export type CacheStatus = 'hit' | 'stale-hit' | 'revalidated' | 'miss'; | ||
|
|
||
| /** | ||
| * Wraps undici's built-in `interceptors.cache(...)` and stamps the served | ||
| * response with {@link VSCODE_CACHE_STATUS_HEADER} so downstream code can | ||
| * tell whether the response came from the cache without relying on header | ||
| * heuristics. | ||
| * | ||
| * Detection is based on observing whether the cache interceptor invoked the | ||
| * downstream dispatcher for a given request and whether it added conditional | ||
| * revalidation headers — both of which are deterministic for a given code | ||
| * path in `undici/lib/interceptor/cache.js`. | ||
| */ | ||
| export function taggedCacheInterceptor( | ||
| cacheOpts: Parameters<typeof undici.interceptors.cache>[0], | ||
| ): undici.Dispatcher.DispatcherComposeInterceptor { | ||
| const cacheInterceptor = undici.interceptors.cache(cacheOpts); | ||
|
|
||
| return (dispatch) => (opts, handler) => { | ||
| const state = { networkCalled: false, conditional: false }; | ||
|
|
||
| const countingDispatch: typeof dispatch = (dOpts, dHandler) => { | ||
| state.networkCalled = true; | ||
| const h = dOpts.headers as Record<string, string> | undefined; | ||
| state.conditional = !!(h && (h['if-modified-since'] || h['if-none-match'])); | ||
| return dispatch(dOpts, dHandler); | ||
|
deepak1556 marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| const taggingHandler = new Proxy(handler, { | ||
| get(target, prop, receiver) { | ||
| if (prop === 'onResponseStart') { | ||
| return ( | ||
| controller: Parameters<NonNullable<undici.Dispatcher.DispatchHandler['onResponseStart']>>[0], | ||
| statusCode: number, | ||
| headers: unknown, | ||
| statusMessage?: string, | ||
| ) => { | ||
| const status = classify(state, headers); | ||
| stampStatus(headers, status); | ||
| const orig = Reflect.get(target, prop, receiver) as undici.Dispatcher.DispatchHandler['onResponseStart']; | ||
| return orig?.call(target, controller, statusCode, headers as Parameters<NonNullable<undici.Dispatcher.DispatchHandler['onResponseStart']>>[2], statusMessage); | ||
| }; | ||
| } | ||
| const value = Reflect.get(target, prop, receiver); | ||
| return typeof value === 'function' ? value.bind(target) : value; | ||
| }, | ||
| }) as undici.Dispatcher.DispatchHandler; | ||
|
|
||
| return cacheInterceptor(countingDispatch)(opts, taggingHandler); | ||
| }; | ||
| } | ||
|
|
||
| function classify( | ||
| state: { networkCalled: boolean; conditional: boolean }, | ||
| headers: unknown, | ||
| ): CacheStatus { | ||
| if (!state.networkCalled) { | ||
| return isStaleWarning(headers) ? 'stale-hit' : 'hit'; | ||
| } | ||
| if (state.conditional) { | ||
| return 'revalidated'; | ||
| } | ||
| return 'miss'; | ||
| } | ||
|
|
||
| function isStaleWarning(headers: unknown): boolean { | ||
| const value = readHeader(headers, 'warning'); | ||
| return typeof value === 'string' && value.startsWith('110'); | ||
| } | ||
|
|
||
| function readHeader(headers: unknown, name: string): string | undefined { | ||
| if (!headers || typeof headers !== 'object') { | ||
| return undefined; | ||
| } | ||
| const value = (headers as Record<string, unknown>)[name]; | ||
| if (typeof value === 'string') { | ||
| return value; | ||
| } | ||
| if (Array.isArray(value) && typeof value[0] === 'string') { | ||
| return value[0]; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function stampStatus(headers: unknown, status: CacheStatus): void { | ||
| if (headers && typeof headers === 'object' && !Array.isArray(headers)) { | ||
| (headers as Record<string, string>)[VSCODE_CACHE_STATUS_HEADER] = status; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.