This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
429 additions
and
46 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
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 |
---|---|---|
@@ -1,7 +1,2 @@ | ||
export function greeting(str?: string): string { | ||
if (str) { | ||
return `Hello ${str}`; | ||
} else { | ||
throw new Error("Please provide an input"); | ||
} | ||
} | ||
export { append } from "./src/append.ts"; | ||
export { vary } from "./src/vary.ts"; |
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,93 @@ | ||
/** | ||
* RegExp to match field-name in RFC 7230 sec 3.2 | ||
* | ||
* field-name = token | ||
* token = 1*tchar | ||
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" | ||
* / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" | ||
* / DIGIT / ALPHA | ||
* ; any VCHAR, except delimiters | ||
*/ | ||
const FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; | ||
|
||
/** | ||
* Append a field to a vary header. | ||
* | ||
* @param {String} header | ||
* @param {String|Array} field | ||
* | ||
* @return {String} | ||
*/ | ||
export const append = (header: string, field: string | string[]): string => { | ||
// get fields array | ||
const fields = !Array.isArray(field) ? parse(String(field)) : field; | ||
|
||
// assert on invalid field names | ||
for (let j = 0; j < fields.length; j++) { | ||
if (!FIELD_NAME_REGEXP.test(fields[j])) { | ||
throw new TypeError("field argument contains an invalid header name"); | ||
} | ||
} | ||
|
||
// existing, unspecified vary | ||
if (header === "*") { | ||
return header; | ||
} | ||
|
||
// enumerate current values | ||
let val = header; | ||
const vals = parse(header.toLowerCase()); | ||
|
||
// unspecified vary | ||
if (fields.indexOf("*") !== -1 || vals.indexOf("*") !== -1) { | ||
return "*"; | ||
} | ||
|
||
for (let i = 0; i < fields.length; i++) { | ||
const fld = fields[i].toLowerCase(); | ||
|
||
// append value (case-preserving) | ||
if (vals.indexOf(fld) === -1) { | ||
vals.push(fld); | ||
val = val ? val + ", " + fields[i] : fields[i]; | ||
} | ||
} | ||
|
||
return val; | ||
}; | ||
|
||
/** | ||
* Parse a vary header into an array. | ||
* | ||
* @param {String} header | ||
* | ||
* @return {Array} | ||
*/ | ||
const parse = (header: string): string[] => { | ||
const list = []; | ||
let end = 0; | ||
let start = 0; | ||
|
||
// gather tokens | ||
for (let i = 0, len = header.length; i < len; i++) { | ||
switch (header.charCodeAt(i)) { | ||
case 0x20:/* */ | ||
if (start === end) { | ||
start = end = i + 1; | ||
} | ||
break; | ||
case 0x2c:/* , */ | ||
list.push(header.substring(start, end)); | ||
start = end = i + 1; | ||
break; | ||
default: | ||
end = i + 1; | ||
break; | ||
} | ||
} | ||
|
||
// final token | ||
list.push(header.substring(start, end)); | ||
|
||
return list; | ||
}; |
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,25 @@ | ||
import { append } from "./append.ts"; | ||
|
||
/** | ||
* Mark that a request is varied on a header field. | ||
* | ||
* @param {function} responseHeadersGet | ||
* @param {function} responseHeadersSet | ||
* @param {String|Array} field | ||
* @public | ||
*/ | ||
export const vary = ( | ||
responseHeadersGet: (header: string) => string, | ||
responseHeadersSet: (header: string, value: string) => void, | ||
field: string | string[], | ||
) => { | ||
// get existing header | ||
let val = responseHeadersGet("Vary"); | ||
|
||
const header = Array.isArray(val) ? val.join(", ") : String(val); | ||
|
||
// set new header | ||
if ((val = append(header, field))) { | ||
responseHeadersSet("Vary", val); | ||
} | ||
}; |
Oops, something went wrong.