-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: flakey5 <[email protected]>
- Loading branch information
Showing
4 changed files
with
276 additions
and
79 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
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,98 @@ | ||
'use strict' | ||
|
||
const util = require('../core/util.js') | ||
const DecoratorHandler = require('../handler/decorator-handler') | ||
|
||
/** | ||
* TODO docs | ||
* @see https://www.rfc-editor.org/rfc/rfc9111.html#name-validation | ||
* | ||
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandlers} DispatchHandlers | ||
* @implements {DispatchHandlers} | ||
*/ | ||
class CacheRevalidationHandler extends DecoratorHandler { | ||
#successful = false | ||
/** | ||
* @type {() => void} | ||
*/ | ||
#successCallback = null | ||
/** | ||
* @type {import('../../types/dispatcher.d.ts').default.DispatchHandlers} | ||
*/ | ||
#handler = null | ||
|
||
/** | ||
* @param {() => void} successCallback | ||
* @param {import('../../types/dispatcher.d.ts').default.DispatchHandlers} handler | ||
*/ | ||
constructor (successCallback, handler) { | ||
super(handler) | ||
|
||
this.#successCallback = successCallback | ||
this.#handler = handler | ||
} | ||
|
||
/** | ||
* @see {DispatchHandlers.onHeaders} | ||
* | ||
* @param {number} statusCode | ||
* @param {Buffer[]} rawHeaders | ||
* @param {() => void} resume | ||
* @param {string} statusMessage | ||
* @param {string[] | undefined} headers | ||
* @returns {boolean} | ||
*/ | ||
onHeaders ( | ||
statusCode, | ||
rawHeaders, | ||
resume, | ||
statusMessage, | ||
headers = util.parseHeaders(rawHeaders) | ||
) { | ||
if (statusCode === 304) { | ||
// TODO update the cached value to be fresh again | ||
this.#successCallback() | ||
return true | ||
} | ||
|
||
return this.#handler.onHeaders( | ||
statusCode, | ||
rawHeaders, | ||
resume, | ||
statusMessage, | ||
headers | ||
) | ||
} | ||
|
||
/** | ||
* @see {DispatchHandlers.onData} | ||
* | ||
* @param {Buffer} chunk | ||
* @returns {boolean} | ||
*/ | ||
onData (chunk) { | ||
return this.#successful ? true : this.#handler.onData(chunk) | ||
} | ||
|
||
/** | ||
* @see {DispatchHandlers.onComplete} | ||
* | ||
* @param {string[] | null} rawTrailers | ||
*/ | ||
onComplete (rawTrailers) { | ||
if (!this.#successful) { | ||
this.#handler.onComplete(rawTrailers) | ||
} | ||
} | ||
|
||
/** | ||
* @see {DispatchHandlers.onError} | ||
* | ||
* @param {Error} err | ||
*/ | ||
onError (err) { | ||
this.#handler.onError(err) | ||
} | ||
} | ||
|
||
module.exports = CacheRevalidationHandler |
Oops, something went wrong.