Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/README.md +3 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/bin/esbuild +3 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/package.json +20 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/LICENSE +22 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/README.md +52 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/index.js +170 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/package.json +77 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/types/index.d.ts +17 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.gitattributes +2 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/.stale.yml +21 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/dependabot.yml +13 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/tests_checker.yml +8 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/workflows/ci.yml +26 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.taprc +2 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/LICENSE +24 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/README.md +237 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/benchmark/small-object.mjs +37 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/eslint.config.js +6 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/index.js +53 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/default-ajv-options.js +14 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/serializer-compiler.js +27 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/validator-compiler.js +58 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/package.json +84 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/standalone.js +44 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/.gitkeep +0 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/duplicated-id-compile.test.js +59 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/index.test.js +307 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/plugins.test.js +264 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/serialization.test.js +279 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/standalone.test.js +203 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/types/index.d.ts +72 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/types/index.test-d.ts +226 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.editorconfig +8 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.gitattributes +5 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/dependabot.yml +13 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/stale.yml +21 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/workflows/ci.yml +28 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/LICENSE +23 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/README.md +198 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/bench.js +17 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/benchmark/package.json +15 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/benchmark/vary.js +34 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/eslint.config.js +6 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/index.js +316 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/package.json +86 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/cors.test.js +1052 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/hooks.test.js +787 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/preflight.test.js +590 -0
- novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/vary.test.js +237 -0
.gitattributes
CHANGED
|
@@ -3526,3 +3526,4 @@ novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/esm/vs/la
|
|
| 3526 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/dev/vs/editor/editor.main.js.map filter=lfs diff=lfs merge=lfs -text
|
| 3527 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/dev/vs/language/typescript/tsWorker.js filter=lfs diff=lfs merge=lfs -text
|
| 3528 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/min-maps/vs/editor/editor.main.js.map filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 3526 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/dev/vs/editor/editor.main.js.map filter=lfs diff=lfs merge=lfs -text
|
| 3527 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/dev/vs/language/typescript/tsWorker.js filter=lfs diff=lfs merge=lfs -text
|
| 3528 |
novas/novacore-zephyr/claude-code-router/ui/node_modules/monaco-editor/min-maps/vs/editor/editor.main.js.map filter=lfs diff=lfs merge=lfs -text
|
| 3529 |
+
novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/bin/esbuild filter=lfs diff=lfs merge=lfs -text
|
novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# esbuild
|
| 2 |
+
|
| 3 |
+
This is the Linux 64-bit binary for esbuild, a JavaScript bundler and minifier. See https://github.com/evanw/esbuild for details.
|
novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/bin/esbuild
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:92d1ca653cf188da8d7650ddfe1c32d5a139bf3a9a2808f3e622e6d667ce0389
|
| 3 |
+
size 10305688
|
novas/novacore-zephyr/claude-code-router/node_modules/@esbuild/linux-x64/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@esbuild/linux-x64",
|
| 3 |
+
"version": "0.25.9",
|
| 4 |
+
"description": "The Linux 64-bit binary for esbuild, a JavaScript bundler.",
|
| 5 |
+
"repository": {
|
| 6 |
+
"type": "git",
|
| 7 |
+
"url": "git+https://github.com/evanw/esbuild.git"
|
| 8 |
+
},
|
| 9 |
+
"license": "MIT",
|
| 10 |
+
"preferUnplugged": true,
|
| 11 |
+
"engines": {
|
| 12 |
+
"node": ">=18"
|
| 13 |
+
},
|
| 14 |
+
"os": [
|
| 15 |
+
"linux"
|
| 16 |
+
],
|
| 17 |
+
"cpu": [
|
| 18 |
+
"x64"
|
| 19 |
+
]
|
| 20 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2022 The Fastify Team
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining
|
| 6 |
+
a copy of this software and associated documentation files (the
|
| 7 |
+
'Software'), to deal in the Software without restriction, including
|
| 8 |
+
without limitation the rights to use, copy, modify, merge, publish,
|
| 9 |
+
distribute, sublicense, and/or sell copies of the Software, and to
|
| 10 |
+
permit persons to whom the Software is furnished to do so, subject to
|
| 11 |
+
the following conditions:
|
| 12 |
+
|
| 13 |
+
The above copyright notice and this permission notice shall be
|
| 14 |
+
included in all copies or substantial portions of the Software.
|
| 15 |
+
|
| 16 |
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
| 17 |
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
| 18 |
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
| 19 |
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
| 20 |
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
| 21 |
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
| 22 |
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @fastify/accept-negotiator
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
[](https://github.com/fastify/accept-negotiator/actions/workflows/ci.yml)
|
| 5 |
+
[](https://www.npmjs.com/package/@fastify/accept-negotiator)
|
| 6 |
+
[](https://github.com/neostandard/neostandard)
|
| 7 |
+
|
| 8 |
+
A negotiator for accept-* headers.
|
| 9 |
+
|
| 10 |
+
### Install
|
| 11 |
+
```
|
| 12 |
+
npm i @fastify/accept-negotiator
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
### Usage
|
| 16 |
+
|
| 17 |
+
The module exports a function that you can use for negotiating an accept-* header such as [`accept-encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding). It takes 2 parameters:
|
| 18 |
+
|
| 19 |
+
```
|
| 20 |
+
negotiate(header, supportedValues)
|
| 21 |
+
```
|
| 22 |
+
|
| 23 |
+
- `header` (`string`, required) - The accept-header, e.g. accept-encoding
|
| 24 |
+
- `supportedValues` (`string[]`, required) - The values, which are supported
|
| 25 |
+
|
| 26 |
+
```js
|
| 27 |
+
const negotiate = require('@fastify/accept-negotiator').negotiate
|
| 28 |
+
const encoding = negotiate('gzip, deflate, br', ['br'])
|
| 29 |
+
console.log(encoding) // 'br*
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
The module also exports a class that you can use for negotiating an accept-* header, and use caching for better performance.
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
```
|
| 36 |
+
Negotiate(supportedValues)
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
- `supportedValues` (`string[]`, required) - The values, which are supported
|
| 40 |
+
- `cache` (`{ set: Function; get: Function; has: Function }`, optional) - A Cache-Store, e.g. ES6-Map or mnemonist LRUCache
|
| 41 |
+
|
| 42 |
+
```js
|
| 43 |
+
const Negotiator = require('@fastify/accept-negotiator').Negotiator
|
| 44 |
+
const encodingNegotiator = new Negotiator({ supportedValues: ['br'], cache: new Map() })
|
| 45 |
+
|
| 46 |
+
const encoding = encodingNegotiator.negotiate('gzip, deflate, br')
|
| 47 |
+
console.log(encoding) // 'br*
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## License
|
| 51 |
+
|
| 52 |
+
Licensed under [MIT](./LICENSE).
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
function Negotiator (options) {
|
| 4 |
+
if (!new.target) {
|
| 5 |
+
return new Negotiator(options)
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
const {
|
| 9 |
+
supportedValues = [],
|
| 10 |
+
cache
|
| 11 |
+
} = (options && typeof options === 'object' && options) || {}
|
| 12 |
+
|
| 13 |
+
this.supportedValues = supportedValues
|
| 14 |
+
|
| 15 |
+
this.cache = cache
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
Negotiator.prototype.negotiate = function (header) {
|
| 19 |
+
if (typeof header !== 'string') {
|
| 20 |
+
return null
|
| 21 |
+
}
|
| 22 |
+
if (!this.cache) {
|
| 23 |
+
return negotiate(header, this.supportedValues)
|
| 24 |
+
}
|
| 25 |
+
if (!this.cache.has(header)) {
|
| 26 |
+
this.cache.set(header, negotiate(header, this.supportedValues))
|
| 27 |
+
}
|
| 28 |
+
return this.cache.get(header)
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
function negotiate (header, supportedValues) {
|
| 32 |
+
if (
|
| 33 |
+
!header ||
|
| 34 |
+
!Array.isArray(supportedValues) ||
|
| 35 |
+
supportedValues.length === 0
|
| 36 |
+
) {
|
| 37 |
+
return null
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if (header === '*') {
|
| 41 |
+
return supportedValues[0]
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
let preferredEncoding = null
|
| 45 |
+
let preferredEncodingPriority = Infinity
|
| 46 |
+
let preferredEncodingQuality = 0
|
| 47 |
+
|
| 48 |
+
function processMatch (enc, quality) {
|
| 49 |
+
if (quality === 0 || preferredEncodingQuality > quality) {
|
| 50 |
+
return false
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
const encoding = (enc === '*' && supportedValues[0]) || enc
|
| 54 |
+
const priority = supportedValues.indexOf(encoding)
|
| 55 |
+
if (priority === -1) {
|
| 56 |
+
return false
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
if (priority === 0 && quality === 1) {
|
| 60 |
+
preferredEncoding = encoding
|
| 61 |
+
return true
|
| 62 |
+
} else if (preferredEncodingQuality < quality) {
|
| 63 |
+
preferredEncoding = encoding
|
| 64 |
+
preferredEncodingPriority = priority
|
| 65 |
+
preferredEncodingQuality = quality
|
| 66 |
+
} else if (preferredEncodingPriority > priority) {
|
| 67 |
+
preferredEncoding = encoding
|
| 68 |
+
preferredEncodingPriority = priority
|
| 69 |
+
preferredEncodingQuality = quality
|
| 70 |
+
}
|
| 71 |
+
return false
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
parse(header, processMatch)
|
| 75 |
+
|
| 76 |
+
return preferredEncoding
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
const BEGIN = 0
|
| 80 |
+
const TOKEN = 1
|
| 81 |
+
const QUALITY = 2
|
| 82 |
+
const END = 3
|
| 83 |
+
|
| 84 |
+
function parse (header, processMatch) {
|
| 85 |
+
let str = ''
|
| 86 |
+
let quality
|
| 87 |
+
let state = BEGIN
|
| 88 |
+
for (let i = 0, il = header.length; i < il; ++i) {
|
| 89 |
+
const char = header[i]
|
| 90 |
+
|
| 91 |
+
if (char === ' ' || char === '\t') {
|
| 92 |
+
continue
|
| 93 |
+
} else if (char === ';') {
|
| 94 |
+
if (state === TOKEN) {
|
| 95 |
+
state = QUALITY
|
| 96 |
+
quality = ''
|
| 97 |
+
}
|
| 98 |
+
continue
|
| 99 |
+
} else if (char === ',') {
|
| 100 |
+
if (state === TOKEN) {
|
| 101 |
+
if (processMatch(str, 1)) {
|
| 102 |
+
state = END
|
| 103 |
+
break
|
| 104 |
+
}
|
| 105 |
+
state = BEGIN
|
| 106 |
+
str = ''
|
| 107 |
+
} else if (state === QUALITY) {
|
| 108 |
+
if (processMatch(str, parseFloat(quality) || 0)) {
|
| 109 |
+
state = END
|
| 110 |
+
break
|
| 111 |
+
}
|
| 112 |
+
state = BEGIN
|
| 113 |
+
str = ''
|
| 114 |
+
quality = ''
|
| 115 |
+
}
|
| 116 |
+
continue
|
| 117 |
+
} else if (
|
| 118 |
+
state === QUALITY
|
| 119 |
+
) {
|
| 120 |
+
if (char === 'q' || char === '=') {
|
| 121 |
+
continue
|
| 122 |
+
} else if (
|
| 123 |
+
char === '.' ||
|
| 124 |
+
char === '1' ||
|
| 125 |
+
char === '0' ||
|
| 126 |
+
char === '2' ||
|
| 127 |
+
char === '3' ||
|
| 128 |
+
char === '4' ||
|
| 129 |
+
char === '5' ||
|
| 130 |
+
char === '6' ||
|
| 131 |
+
char === '7' ||
|
| 132 |
+
char === '8' ||
|
| 133 |
+
char === '9'
|
| 134 |
+
) {
|
| 135 |
+
quality += char
|
| 136 |
+
continue
|
| 137 |
+
}
|
| 138 |
+
} else if (state === BEGIN) {
|
| 139 |
+
state = TOKEN
|
| 140 |
+
str += char
|
| 141 |
+
continue
|
| 142 |
+
}
|
| 143 |
+
if (state === TOKEN) {
|
| 144 |
+
const prevChar = header[i - 1]
|
| 145 |
+
if (prevChar === ' ' || prevChar === '\t') {
|
| 146 |
+
str = ''
|
| 147 |
+
}
|
| 148 |
+
str += char
|
| 149 |
+
continue
|
| 150 |
+
}
|
| 151 |
+
if (processMatch(str, parseFloat(quality) || 0)) {
|
| 152 |
+
state = END
|
| 153 |
+
break
|
| 154 |
+
}
|
| 155 |
+
state = BEGIN
|
| 156 |
+
str = char
|
| 157 |
+
quality = ''
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
if (state === TOKEN) {
|
| 161 |
+
processMatch(str, 1)
|
| 162 |
+
} else if (state === QUALITY) {
|
| 163 |
+
processMatch(str, parseFloat(quality) || 0)
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
module.exports = negotiate
|
| 168 |
+
module.exports.default = negotiate
|
| 169 |
+
module.exports.negotiate = negotiate
|
| 170 |
+
module.exports.Negotiator = Negotiator
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@fastify/accept-negotiator",
|
| 3 |
+
"version": "2.0.1",
|
| 4 |
+
"description": "a negotiator for the accept-headers",
|
| 5 |
+
"type": "commonjs",
|
| 6 |
+
"main": "index.js",
|
| 7 |
+
"types": "types/index.d.ts",
|
| 8 |
+
"scripts": {
|
| 9 |
+
"lint": "eslint",
|
| 10 |
+
"lint:fix": "eslint --fix",
|
| 11 |
+
"test": "npm run test:unit && npm run test:typescript",
|
| 12 |
+
"test:unit": "c8 --100 node --test",
|
| 13 |
+
"test:typescript": "tsd"
|
| 14 |
+
},
|
| 15 |
+
"keywords": [
|
| 16 |
+
"encoding",
|
| 17 |
+
"negotiator",
|
| 18 |
+
"accept-encoding",
|
| 19 |
+
"accept",
|
| 20 |
+
"http",
|
| 21 |
+
"header"
|
| 22 |
+
],
|
| 23 |
+
"files": [
|
| 24 |
+
"README.md",
|
| 25 |
+
"LICENSE",
|
| 26 |
+
"index.js",
|
| 27 |
+
"types/index.d.ts"
|
| 28 |
+
],
|
| 29 |
+
"author": "Aras Abbasi <aras.abbasi@gmail.com>",
|
| 30 |
+
"contributors": [
|
| 31 |
+
{
|
| 32 |
+
"name": "Matteo Collina",
|
| 33 |
+
"email": "hello@matteocollina.com"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"name": "Manuel Spigolon",
|
| 37 |
+
"email": "behemoth89@gmail.com"
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"name": "James Sumners",
|
| 41 |
+
"url": "https://james.sumners.info"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"name": "Frazer Smith",
|
| 45 |
+
"email": "frazer.dev@icloud.com",
|
| 46 |
+
"url": "https://github.com/fdawgs"
|
| 47 |
+
}
|
| 48 |
+
],
|
| 49 |
+
"license": "MIT",
|
| 50 |
+
"devDependencies": {
|
| 51 |
+
"@fastify/pre-commit": "^2.1.0",
|
| 52 |
+
"@matteo.collina/tspl": "^0.1.1",
|
| 53 |
+
"benchmark": "2.1.4",
|
| 54 |
+
"c8": "^10.1.2",
|
| 55 |
+
"eslint": "^9.17.0",
|
| 56 |
+
"neostandard": "^0.12.0",
|
| 57 |
+
"tsd": "^0.31.0"
|
| 58 |
+
},
|
| 59 |
+
"repository": {
|
| 60 |
+
"type": "git",
|
| 61 |
+
"url": "git+https://github.com/fastify/accept-negotiator.git"
|
| 62 |
+
},
|
| 63 |
+
"bugs": {
|
| 64 |
+
"url": "https://github.com/fastify/accept-negotiator/issues"
|
| 65 |
+
},
|
| 66 |
+
"homepage": "https://github.com/fastify/accept-negotiator#readme",
|
| 67 |
+
"funding": [
|
| 68 |
+
{
|
| 69 |
+
"type": "github",
|
| 70 |
+
"url": "https://github.com/sponsors/fastify"
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"type": "opencollective",
|
| 74 |
+
"url": "https://opencollective.com/fastify"
|
| 75 |
+
}
|
| 76 |
+
]
|
| 77 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/accept-negotiator/types/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
type CacheStore = { set: (key: string, value: string) => CacheStore, get: (key: string) => string | undefined, has: (key: string) => boolean }
|
| 2 |
+
|
| 3 |
+
type NegotiateFn = typeof negotiate
|
| 4 |
+
|
| 5 |
+
declare namespace negotiate {
|
| 6 |
+
export class Negotiator<K extends string = string> {
|
| 7 |
+
constructor (options: { supportedValues: K[]; cache?: CacheStore })
|
| 8 |
+
|
| 9 |
+
negotiate (header: string): K | null
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export const negotiate: NegotiateFn
|
| 13 |
+
export { negotiate as default }
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
declare function negotiate<K extends string = string> (header: string, supportedValues: K[]): K | null
|
| 17 |
+
export = negotiate
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.gitattributes
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Set default behavior to automatically convert line endings
|
| 2 |
+
* text=auto eol=lf
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/.stale.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Number of days of inactivity before an issue becomes stale
|
| 2 |
+
daysUntilStale: 15
|
| 3 |
+
# Number of days of inactivity before a stale issue is closed
|
| 4 |
+
daysUntilClose: 7
|
| 5 |
+
# Issues with these labels will never be considered stale
|
| 6 |
+
exemptLabels:
|
| 7 |
+
- "discussion"
|
| 8 |
+
- "feature request"
|
| 9 |
+
- "bug"
|
| 10 |
+
- "help wanted"
|
| 11 |
+
- "plugin suggestion"
|
| 12 |
+
- "good first issue"
|
| 13 |
+
# Label to use when marking an issue as stale
|
| 14 |
+
staleLabel: stale
|
| 15 |
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
| 16 |
+
markComment: >
|
| 17 |
+
This issue has been automatically marked as stale because it has not had
|
| 18 |
+
recent activity. It will be closed if no further activity occurs. Thank you
|
| 19 |
+
for your contributions.
|
| 20 |
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
| 21 |
+
closeComment: false
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/dependabot.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: 2
|
| 2 |
+
updates:
|
| 3 |
+
- package-ecosystem: "github-actions"
|
| 4 |
+
directory: "/"
|
| 5 |
+
schedule:
|
| 6 |
+
interval: "monthly"
|
| 7 |
+
open-pull-requests-limit: 10
|
| 8 |
+
|
| 9 |
+
- package-ecosystem: "npm"
|
| 10 |
+
directory: "/"
|
| 11 |
+
schedule:
|
| 12 |
+
interval: "weekly"
|
| 13 |
+
open-pull-requests-limit: 10
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/tests_checker.yml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
comment: |
|
| 2 |
+
Hello! Thank you for contributing!
|
| 3 |
+
It appears that you have changed the code, but the tests that verify your change are missing. Could you please add them?
|
| 4 |
+
fileExtensions:
|
| 5 |
+
- '.ts'
|
| 6 |
+
- '.js'
|
| 7 |
+
|
| 8 |
+
testDir: 'test'
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.github/workflows/ci.yml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: CI
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
- master
|
| 8 |
+
- next
|
| 9 |
+
- 'v*'
|
| 10 |
+
paths-ignore:
|
| 11 |
+
- 'docs/**'
|
| 12 |
+
- '*.md'
|
| 13 |
+
pull_request:
|
| 14 |
+
paths-ignore:
|
| 15 |
+
- 'docs/**'
|
| 16 |
+
- '*.md'
|
| 17 |
+
|
| 18 |
+
env:
|
| 19 |
+
TZ: 'UTC'
|
| 20 |
+
|
| 21 |
+
jobs:
|
| 22 |
+
test:
|
| 23 |
+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
|
| 24 |
+
with:
|
| 25 |
+
lint: true
|
| 26 |
+
license-check: true
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/.taprc
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
files:
|
| 2 |
+
- test/**/*.test.js
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) The Fastify Team
|
| 4 |
+
|
| 5 |
+
The Fastify team members are listed at https://github.com/fastify/fastify#team
|
| 6 |
+
and in the README file.
|
| 7 |
+
|
| 8 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 9 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 10 |
+
in the Software without restriction, including without limitation the rights
|
| 11 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 12 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 13 |
+
furnished to do so, subject to the following conditions:
|
| 14 |
+
|
| 15 |
+
The above copyright notice and this permission notice shall be included in all
|
| 16 |
+
copies or substantial portions of the Software.
|
| 17 |
+
|
| 18 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 19 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 20 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 21 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 22 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 23 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 24 |
+
SOFTWARE.
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/README.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @fastify/ajv-compiler
|
| 2 |
+
|
| 3 |
+
[](https://github.com/fastify/ajv-compiler/actions/workflows/ci.yml)
|
| 4 |
+
[](https://www.npmjs.com/package/@fastify/ajv-compiler)
|
| 5 |
+
[](https://github.com/neostandard/neostandard)
|
| 6 |
+
|
| 7 |
+
This module manages the [`ajv`](https://www.npmjs.com/package/ajv) instances for the Fastify framework.
|
| 8 |
+
It isolates the `ajv` dependency so that the AJV version is not tightly coupled to the Fastify version.
|
| 9 |
+
This allows the user to decide which version of AJV to use in their Fastify-based application.
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
## Versions
|
| 13 |
+
|
| 14 |
+
| `@fastify/ajv-compiler` | `ajv` | Default in `fastify` |
|
| 15 |
+
|------------------------:|------:|---------------------:|
|
| 16 |
+
| v4.x | v8.x | ^5.x |
|
| 17 |
+
| v3.x | v8.x | ^4.x |
|
| 18 |
+
| v2.x | v8.x | - |
|
| 19 |
+
| v1.x | v6.x | ^3.14 |
|
| 20 |
+
|
| 21 |
+
### AJV Configuration
|
| 22 |
+
|
| 23 |
+
The Fastify's default [`ajv` options](https://github.com/ajv-validator/ajv/tree/v6#options) are:
|
| 24 |
+
|
| 25 |
+
```js
|
| 26 |
+
{
|
| 27 |
+
coerceTypes: 'array',
|
| 28 |
+
useDefaults: true,
|
| 29 |
+
removeAdditional: true,
|
| 30 |
+
uriResolver: require('fast-uri'),
|
| 31 |
+
addUsedSchema: false,
|
| 32 |
+
// Explicitly set allErrors to `false`.
|
| 33 |
+
// When set to `true`, a DoS attack is possible.
|
| 34 |
+
allErrors: false
|
| 35 |
+
}
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Moreover, the [`ajv-formats`](https://www.npmjs.com/package/ajv-formats) module is included by default.
|
| 39 |
+
If you need to customize it, check the _usage_ section below.
|
| 40 |
+
|
| 41 |
+
To customize the `ajv` options, see how in the [Fastify documentation](https://fastify.dev/docs/latest/Reference/Server/#ajv).
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
## Usage
|
| 45 |
+
|
| 46 |
+
This module is already used as default by Fastify.
|
| 47 |
+
If you need to provide your server instance with a different version, refer to [the Fastify docs](https://fastify.dev/docs/latest/Reference/Server/#schemacontroller).
|
| 48 |
+
|
| 49 |
+
### Customize the `ajv-formats` plugin
|
| 50 |
+
|
| 51 |
+
The `format` keyword is not part of the official `ajv` module since v7. To use it, you need to install the `ajv-formats` module and this module
|
| 52 |
+
does it for you with the default configuration.
|
| 53 |
+
|
| 54 |
+
If you need to configure the `ajv-formats` plugin you can do it using the standard Fastify configuration:
|
| 55 |
+
|
| 56 |
+
```js
|
| 57 |
+
const app = fastify({
|
| 58 |
+
ajv: {
|
| 59 |
+
plugins: [[require('ajv-formats'), { mode: 'fast' }]]
|
| 60 |
+
}
|
| 61 |
+
})
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
In this way, your setup will have precedence over the `@fastify/ajv-compiler` default configuration.
|
| 65 |
+
|
| 66 |
+
### Customize the `ajv` instance
|
| 67 |
+
|
| 68 |
+
If you need to customize the `ajv` instance and take full control of its configuration, you can do it by
|
| 69 |
+
using the `onCreate` option in the Fastify configuration that accepts a synchronous function that receives the `ajv` instance:
|
| 70 |
+
|
| 71 |
+
```js
|
| 72 |
+
const app = fastify({
|
| 73 |
+
ajv: {
|
| 74 |
+
onCreate: (ajv) => {
|
| 75 |
+
// Modify the ajv instance as you need.
|
| 76 |
+
ajv.addFormat('myFormat', (data) => typeof data === 'string')
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
})
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
### Fastify with JTD
|
| 83 |
+
|
| 84 |
+
The [JSON Type Definition](https://jsontypedef.com/) feature is supported by AJV v8.x and you can benefit from it in your Fastify application.
|
| 85 |
+
|
| 86 |
+
With Fastify v3.20.x and higher, you can use the `@fastify/ajv-compiler` module to load JSON Type Definitions like so:
|
| 87 |
+
|
| 88 |
+
```js
|
| 89 |
+
const factory = require('@fastify/ajv-compiler')()
|
| 90 |
+
|
| 91 |
+
const app = fastify({
|
| 92 |
+
jsonShorthand: false,
|
| 93 |
+
ajv: {
|
| 94 |
+
customOptions: { }, // additional JTD options
|
| 95 |
+
mode: 'JTD'
|
| 96 |
+
},
|
| 97 |
+
schemaController: {
|
| 98 |
+
compilersFactory: {
|
| 99 |
+
buildValidator: factory
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
})
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
The default AJV JTD options are the same as [Fastify's default options](#AJV-Configuration).
|
| 106 |
+
|
| 107 |
+
#### Fastify with JTD and serialization
|
| 108 |
+
|
| 109 |
+
You can use JTD Schemas to serialize your response object too:
|
| 110 |
+
|
| 111 |
+
```js
|
| 112 |
+
const factoryValidator = require('@fastify/ajv-compiler')()
|
| 113 |
+
const factorySerializer = require('@fastify/ajv-compiler')({ jtdSerializer: true })
|
| 114 |
+
|
| 115 |
+
const app = fastify({
|
| 116 |
+
jsonShorthand: false,
|
| 117 |
+
ajv: {
|
| 118 |
+
customOptions: { }, // additional JTD options
|
| 119 |
+
mode: 'JTD'
|
| 120 |
+
},
|
| 121 |
+
schemaController: {
|
| 122 |
+
compilersFactory: {
|
| 123 |
+
buildValidator: factoryValidator,
|
| 124 |
+
buildSerializer: factorySerializer
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
})
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
### AJV Standalone
|
| 132 |
+
|
| 133 |
+
AJV v8 introduced a [standalone feature](https://ajv.js.org/standalone.html) that lets you pre-compile your schemas and use them in your application for a faster startup.
|
| 134 |
+
|
| 135 |
+
To use this feature, you must be aware of the following:
|
| 136 |
+
|
| 137 |
+
1. You must generate and save the application's compiled schemas.
|
| 138 |
+
2. Read the compiled schemas from the file and provide them back to your Fastify application.
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
#### Generate and save the compiled schemas
|
| 142 |
+
|
| 143 |
+
Fastify helps you to generate the validation schemas functions and it is your choice to save them where you want.
|
| 144 |
+
To accomplish this, you must use a new compiler: `StandaloneValidator`.
|
| 145 |
+
|
| 146 |
+
You must provide 2 parameters to this compiler:
|
| 147 |
+
|
| 148 |
+
- `readMode: false`: a boolean to indicate that you want to generate the schemas functions string.
|
| 149 |
+
- `storeFunction`" a sync function that must store the source code of the schemas functions. You may provide an async function too, but you must manage errors.
|
| 150 |
+
|
| 151 |
+
When `readMode: false`, **the compiler is meant to be used in development ONLY**.
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
```js
|
| 155 |
+
const { StandaloneValidator } = require('@fastify/ajv-compiler')
|
| 156 |
+
const factory = StandaloneValidator({
|
| 157 |
+
readMode: false,
|
| 158 |
+
storeFunction (routeOpts, schemaValidationCode) {
|
| 159 |
+
// routeOpts is like: { schema, method, url, httpPart }
|
| 160 |
+
// schemaValidationCode is a string source code that is the compiled schema function
|
| 161 |
+
const fileName = generateFileName(routeOpts)
|
| 162 |
+
fs.writeFileSync(path.join(__dirname, fileName), schemaValidationCode)
|
| 163 |
+
}
|
| 164 |
+
})
|
| 165 |
+
|
| 166 |
+
const app = fastify({
|
| 167 |
+
jsonShorthand: false,
|
| 168 |
+
schemaController: {
|
| 169 |
+
compilersFactory: {
|
| 170 |
+
buildValidator: factory
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
})
|
| 174 |
+
|
| 175 |
+
// ... add all your routes with schemas ...
|
| 176 |
+
|
| 177 |
+
app.ready().then(() => {
|
| 178 |
+
// at this stage all your schemas are compiled and stored in the file system
|
| 179 |
+
// now it is important to turn off the readMode
|
| 180 |
+
})
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
#### Read the compiled schemas functions
|
| 184 |
+
|
| 185 |
+
At this stage, you should have a file for every route's schema.
|
| 186 |
+
To use them, you must use the `StandaloneValidator` with the parameters:
|
| 187 |
+
|
| 188 |
+
- `readMode: true`: a boolean to indicate that you want to read and use the schemas functions string.
|
| 189 |
+
- `restoreFunction`" a sync function that must return a function to validate the route.
|
| 190 |
+
|
| 191 |
+
Important keep away before you continue reading the documentation:
|
| 192 |
+
|
| 193 |
+
- when you use the `readMode: true`, the application schemas are not compiled (they are ignored). So, if you change your schemas, you must recompile them!
|
| 194 |
+
- as you can see, you must relate the route's schema to the file name using the `routeOpts` object. You may use the `routeOpts.schema.$id` field to do so, it is up to you to define a unique schema identifier.
|
| 195 |
+
|
| 196 |
+
```js
|
| 197 |
+
const { StandaloneValidator } = require('@fastify/ajv-compiler')
|
| 198 |
+
const factory = StandaloneValidator({
|
| 199 |
+
readMode: true,
|
| 200 |
+
restoreFunction (routeOpts) {
|
| 201 |
+
// routeOpts is like: { schema, method, url, httpPart }
|
| 202 |
+
const fileName = generateFileName(routeOpts)
|
| 203 |
+
return require(path.join(__dirname, fileName))
|
| 204 |
+
}
|
| 205 |
+
})
|
| 206 |
+
|
| 207 |
+
const app = fastify({
|
| 208 |
+
jsonShorthand: false,
|
| 209 |
+
schemaController: {
|
| 210 |
+
compilersFactory: {
|
| 211 |
+
buildValidator: factory
|
| 212 |
+
}
|
| 213 |
+
}
|
| 214 |
+
})
|
| 215 |
+
|
| 216 |
+
// ... add all your routes with schemas as before...
|
| 217 |
+
|
| 218 |
+
app.listen({ port: 3000 })
|
| 219 |
+
```
|
| 220 |
+
|
| 221 |
+
### How it works
|
| 222 |
+
|
| 223 |
+
This module provides a factory function to produce [Validator Compilers](https://fastify.dev/docs/latest/Reference/Server/#validatorcompiler) functions.
|
| 224 |
+
|
| 225 |
+
The Fastify factory function is just one per server instance and it is called for every encapsulated context created by the application through the `fastify.register()` call.
|
| 226 |
+
|
| 227 |
+
Every Validator Compiler produced has a dedicated AJV instance, so this factory will try to produce as less as possible AJV instances to reduce the memory footprint and the startup time.
|
| 228 |
+
|
| 229 |
+
The variables involved to choose if a Validator Compiler can be reused are:
|
| 230 |
+
|
| 231 |
+
- the AJV configuration: it is [one per server](https://fastify.dev/docs/latest/Reference/Server/#ajv)
|
| 232 |
+
- the external JSON schemas: once a new schema is added to a fastify's context, calling `fastify.addSchema()`, it will cause a new AJV initialization
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
## License
|
| 236 |
+
|
| 237 |
+
Licensed under [MIT](./LICENSE).
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/benchmark/small-object.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cronometro from 'cronometro'
|
| 2 |
+
|
| 3 |
+
import fjs from 'fast-json-stringify'
|
| 4 |
+
import AjvCompiler from '../index.js'
|
| 5 |
+
|
| 6 |
+
const fjsSerialize = buildFJSSerializerFunction({
|
| 7 |
+
type: 'object',
|
| 8 |
+
properties: {
|
| 9 |
+
hello: { type: 'string' },
|
| 10 |
+
name: { type: 'string' }
|
| 11 |
+
}
|
| 12 |
+
})
|
| 13 |
+
const ajvSerialize = buildAJVSerializerFunction({
|
| 14 |
+
properties: {
|
| 15 |
+
hello: { type: 'string' },
|
| 16 |
+
name: { type: 'string' }
|
| 17 |
+
}
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
await cronometro({
|
| 21 |
+
'fast-json-stringify': function () {
|
| 22 |
+
fjsSerialize({ hello: 'Ciao', name: 'Manuel' })
|
| 23 |
+
},
|
| 24 |
+
'ajv serializer': function () {
|
| 25 |
+
ajvSerialize({ hello: 'Ciao', name: 'Manuel' })
|
| 26 |
+
}
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
function buildFJSSerializerFunction (schema) {
|
| 30 |
+
return fjs(schema)
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
function buildAJVSerializerFunction (schema) {
|
| 34 |
+
const factory = AjvCompiler({ jtdSerializer: true })
|
| 35 |
+
const compiler = factory({}, { customOptions: {} })
|
| 36 |
+
return compiler({ schema })
|
| 37 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/eslint.config.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
module.exports = require('neostandard')({
|
| 4 |
+
ignores: require('neostandard').resolveIgnoresFromGitignore(),
|
| 5 |
+
ts: true
|
| 6 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const AjvReference = Symbol.for('fastify.ajv-compiler.reference')
|
| 4 |
+
const ValidatorCompiler = require('./lib/validator-compiler')
|
| 5 |
+
const SerializerCompiler = require('./lib/serializer-compiler')
|
| 6 |
+
|
| 7 |
+
function AjvCompiler (opts) {
|
| 8 |
+
const validatorPool = new Map()
|
| 9 |
+
const serializerPool = new Map()
|
| 10 |
+
|
| 11 |
+
if (opts && opts.jtdSerializer === true) {
|
| 12 |
+
return function buildSerializerFromPool (externalSchemas, serializerOpts) {
|
| 13 |
+
const uniqueAjvKey = getPoolKey({}, serializerOpts)
|
| 14 |
+
if (serializerPool.has(uniqueAjvKey)) {
|
| 15 |
+
return serializerPool.get(uniqueAjvKey)
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const compiler = new SerializerCompiler(externalSchemas, serializerOpts)
|
| 19 |
+
const ret = compiler.buildSerializerFunction.bind(compiler)
|
| 20 |
+
serializerPool.set(uniqueAjvKey, ret)
|
| 21 |
+
|
| 22 |
+
return ret
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
return function buildCompilerFromPool (externalSchemas, options) {
|
| 27 |
+
const uniqueAjvKey = getPoolKey(externalSchemas, options.customOptions)
|
| 28 |
+
if (validatorPool.has(uniqueAjvKey)) {
|
| 29 |
+
return validatorPool.get(uniqueAjvKey)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
const compiler = new ValidatorCompiler(externalSchemas, options)
|
| 33 |
+
const ret = compiler.buildValidatorFunction.bind(compiler)
|
| 34 |
+
validatorPool.set(uniqueAjvKey, ret)
|
| 35 |
+
|
| 36 |
+
if (options.customOptions.code !== undefined) {
|
| 37 |
+
ret[AjvReference] = compiler
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return ret
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
function getPoolKey (externalSchemas, options) {
|
| 45 |
+
const externals = JSON.stringify(externalSchemas)
|
| 46 |
+
const ajvConfig = JSON.stringify(options)
|
| 47 |
+
return `${externals}${ajvConfig}`
|
| 48 |
+
}
|
| 49 |
+
module.exports = AjvCompiler
|
| 50 |
+
module.exports.default = AjvCompiler
|
| 51 |
+
module.exports.AjvCompiler = AjvCompiler
|
| 52 |
+
module.exports.AjvReference = AjvReference
|
| 53 |
+
module.exports.StandaloneValidator = require('./standalone')
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/default-ajv-options.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const fastUri = require('fast-uri')
|
| 4 |
+
|
| 5 |
+
module.exports = Object.freeze({
|
| 6 |
+
coerceTypes: 'array',
|
| 7 |
+
useDefaults: true,
|
| 8 |
+
removeAdditional: true,
|
| 9 |
+
uriResolver: fastUri,
|
| 10 |
+
addUsedSchema: false,
|
| 11 |
+
// Explicitly set allErrors to `false`.
|
| 12 |
+
// When set to `true`, a DoS attack is possible.
|
| 13 |
+
allErrors: false
|
| 14 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/serializer-compiler.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const AjvJTD = require('ajv/dist/jtd')
|
| 4 |
+
|
| 5 |
+
const defaultAjvOptions = require('./default-ajv-options')
|
| 6 |
+
|
| 7 |
+
class SerializerCompiler {
|
| 8 |
+
constructor (_externalSchemas, options) {
|
| 9 |
+
this.ajv = new AjvJTD(Object.assign({}, defaultAjvOptions, options))
|
| 10 |
+
|
| 11 |
+
/**
|
| 12 |
+
* https://ajv.js.org/json-type-definition.html#ref-form
|
| 13 |
+
* Unlike JSON Schema, JTD does not allow to reference:
|
| 14 |
+
* - any schema fragment other than root level definitions member
|
| 15 |
+
* - root of the schema - there is another way to define a self-recursive schema (see Example 2)
|
| 16 |
+
* - another schema file (but you can still combine schemas from multiple files using JavaScript).
|
| 17 |
+
*
|
| 18 |
+
* So we ignore the externalSchemas parameter.
|
| 19 |
+
*/
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
buildSerializerFunction ({ schema/*, method, url, httpStatus */ }) {
|
| 23 |
+
return this.ajv.compileSerializer(schema)
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
module.exports = SerializerCompiler
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/lib/validator-compiler.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const Ajv = require('ajv').default
|
| 4 |
+
const AjvJTD = require('ajv/dist/jtd')
|
| 5 |
+
|
| 6 |
+
const defaultAjvOptions = require('./default-ajv-options')
|
| 7 |
+
|
| 8 |
+
class ValidatorCompiler {
|
| 9 |
+
constructor (externalSchemas, options) {
|
| 10 |
+
// This instance of Ajv is private
|
| 11 |
+
// it should not be customized or used
|
| 12 |
+
if (options.mode === 'JTD') {
|
| 13 |
+
this.ajv = new AjvJTD(Object.assign({}, defaultAjvOptions, options.customOptions))
|
| 14 |
+
} else {
|
| 15 |
+
this.ajv = new Ajv(Object.assign({}, defaultAjvOptions, options.customOptions))
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
let addFormatPlugin = true
|
| 19 |
+
if (options.plugins && options.plugins.length > 0) {
|
| 20 |
+
for (const plugin of options.plugins) {
|
| 21 |
+
if (Array.isArray(plugin)) {
|
| 22 |
+
addFormatPlugin = addFormatPlugin && plugin[0].name !== 'formatsPlugin'
|
| 23 |
+
plugin[0](this.ajv, plugin[1])
|
| 24 |
+
} else {
|
| 25 |
+
addFormatPlugin = addFormatPlugin && plugin.name !== 'formatsPlugin'
|
| 26 |
+
plugin(this.ajv)
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
if (addFormatPlugin) {
|
| 32 |
+
require('ajv-formats')(this.ajv)
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
options.onCreate?.(this.ajv)
|
| 36 |
+
|
| 37 |
+
const sourceSchemas = Object.values(externalSchemas)
|
| 38 |
+
for (const extSchema of sourceSchemas) {
|
| 39 |
+
this.ajv.addSchema(extSchema)
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
buildValidatorFunction ({ schema/*, method, url, httpPart */ }) {
|
| 44 |
+
// Ajv does not support compiling two schemas with the same
|
| 45 |
+
// id inside the same instance. Therefore if we have already
|
| 46 |
+
// compiled the schema with the given id, we just return it.
|
| 47 |
+
if (schema.$id) {
|
| 48 |
+
const stored = this.ajv.getSchema(schema.$id)
|
| 49 |
+
if (stored) {
|
| 50 |
+
return stored
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
return this.ajv.compile(schema)
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
module.exports = ValidatorCompiler
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/package.json
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@fastify/ajv-compiler",
|
| 3 |
+
"version": "4.0.2",
|
| 4 |
+
"description": "Build and manage the AJV instances for the fastify framework",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"type": "commonjs",
|
| 7 |
+
"types": "types/index.d.ts",
|
| 8 |
+
"directories": {
|
| 9 |
+
"test": "test"
|
| 10 |
+
},
|
| 11 |
+
"scripts": {
|
| 12 |
+
"lint": "eslint",
|
| 13 |
+
"lint:fix": "eslint --fix",
|
| 14 |
+
"unit": "tap",
|
| 15 |
+
"test": "npm run unit && npm run test:typescript",
|
| 16 |
+
"test:typescript": "tsd",
|
| 17 |
+
"ajv:compile": "ajv compile -s test/source.json -o test/validate_schema.js"
|
| 18 |
+
},
|
| 19 |
+
"repository": {
|
| 20 |
+
"type": "git",
|
| 21 |
+
"url": "git+https://github.com/fastify/ajv-compiler.git"
|
| 22 |
+
},
|
| 23 |
+
"keywords": [
|
| 24 |
+
"ajv",
|
| 25 |
+
"validator",
|
| 26 |
+
"schema",
|
| 27 |
+
"compiler",
|
| 28 |
+
"fastify"
|
| 29 |
+
],
|
| 30 |
+
"author": "Manuel Spigolon <behemoth89@gmail.com> (https://github.com/Eomm)",
|
| 31 |
+
"contributors": [
|
| 32 |
+
{
|
| 33 |
+
"name": "Matteo Collina",
|
| 34 |
+
"email": "hello@matteocollina.com"
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"name": "Aras Abbasi",
|
| 38 |
+
"email": "aras.abbasi@gmail.com"
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"name": "James Sumners",
|
| 42 |
+
"url": "https://james.sumners.info"
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"name": "Frazer Smith",
|
| 46 |
+
"email": "frazer.dev@icloud.com",
|
| 47 |
+
"url": "https://github.com/fdawgs"
|
| 48 |
+
}
|
| 49 |
+
],
|
| 50 |
+
"license": "MIT",
|
| 51 |
+
"bugs": {
|
| 52 |
+
"url": "https://github.com/fastify/ajv-compiler/issues"
|
| 53 |
+
},
|
| 54 |
+
"homepage": "https://github.com/fastify/ajv-compiler#readme",
|
| 55 |
+
"funding": [
|
| 56 |
+
{
|
| 57 |
+
"type": "github",
|
| 58 |
+
"url": "https://github.com/sponsors/fastify"
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"type": "opencollective",
|
| 62 |
+
"url": "https://opencollective.com/fastify"
|
| 63 |
+
}
|
| 64 |
+
],
|
| 65 |
+
"devDependencies": {
|
| 66 |
+
"ajv-cli": "^5.0.0",
|
| 67 |
+
"ajv-errors": "^3.0.0",
|
| 68 |
+
"ajv-i18n": "^4.2.0",
|
| 69 |
+
"ajv-merge-patch": "^5.0.1",
|
| 70 |
+
"cronometro": "^4.0.0",
|
| 71 |
+
"eslint": "^9.17.0",
|
| 72 |
+
"fastify": "^5.0.0",
|
| 73 |
+
"neostandard": "^0.12.0",
|
| 74 |
+
"require-from-string": "^2.0.2",
|
| 75 |
+
"sanitize-filename": "^1.6.3",
|
| 76 |
+
"tap": "^19.0.0",
|
| 77 |
+
"tsd": "^0.31.0"
|
| 78 |
+
},
|
| 79 |
+
"dependencies": {
|
| 80 |
+
"ajv": "^8.12.0",
|
| 81 |
+
"ajv-formats": "^3.0.1",
|
| 82 |
+
"fast-uri": "^3.0.0"
|
| 83 |
+
}
|
| 84 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/standalone.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const ValidatorSelector = require('./index')
|
| 4 |
+
const standaloneCode = require('ajv/dist/standalone').default
|
| 5 |
+
|
| 6 |
+
function StandaloneValidator (options = { readMode: true }) {
|
| 7 |
+
if (options.readMode === true && !options.restoreFunction) {
|
| 8 |
+
throw new Error('You must provide a restoreFunction options when readMode ON')
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
if (options.readMode !== true && !options.storeFunction) {
|
| 12 |
+
throw new Error('You must provide a storeFunction options when readMode OFF')
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
if (options.readMode === true) {
|
| 16 |
+
// READ MODE: it behalf only in the restore function provided by the user
|
| 17 |
+
return function wrapper () {
|
| 18 |
+
return function (opts) {
|
| 19 |
+
return options.restoreFunction(opts)
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// WRITE MODE: it behalf on the default ValidatorSelector, wrapping the API to run the Ajv Standalone code generation
|
| 25 |
+
const factory = ValidatorSelector()
|
| 26 |
+
return function wrapper (externalSchemas, ajvOptions = {}) {
|
| 27 |
+
if (!ajvOptions.customOptions || !ajvOptions.customOptions.code) {
|
| 28 |
+
// to generate the validation source code, these options are mandatory
|
| 29 |
+
ajvOptions.customOptions = Object.assign({}, ajvOptions.customOptions, { code: { source: true } })
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
const compiler = factory(externalSchemas, ajvOptions)
|
| 33 |
+
return function (opts) { // { schema/*, method, url, httpPart */ }
|
| 34 |
+
const validationFunc = compiler(opts)
|
| 35 |
+
|
| 36 |
+
const schemaValidationCode = standaloneCode(compiler[ValidatorSelector.AjvReference].ajv, validationFunc)
|
| 37 |
+
options.storeFunction(opts, schemaValidationCode)
|
| 38 |
+
|
| 39 |
+
return validationFunc
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
module.exports = StandaloneValidator
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/.gitkeep
ADDED
|
File without changes
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/duplicated-id-compile.test.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const t = require('tap')
|
| 4 |
+
const AjvCompiler = require('../index')
|
| 5 |
+
|
| 6 |
+
const postSchema = Object.freeze({
|
| 7 |
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
| 8 |
+
type: 'object',
|
| 9 |
+
$id: 'http://mydomain.com/user',
|
| 10 |
+
title: 'User schema',
|
| 11 |
+
description: 'Contains all user fields',
|
| 12 |
+
properties: {
|
| 13 |
+
username: { type: 'string', minLength: 4 },
|
| 14 |
+
firstName: { type: 'string', minLength: 1 },
|
| 15 |
+
lastName: { type: 'string', minLength: 1 },
|
| 16 |
+
email: { type: 'string' },
|
| 17 |
+
password: { type: 'string', minLength: 6 },
|
| 18 |
+
bio: { type: 'string' }
|
| 19 |
+
},
|
| 20 |
+
required: ['username', 'firstName', 'lastName', 'email', 'bio', 'password']
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
const patchSchema = Object.freeze({
|
| 24 |
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
| 25 |
+
type: 'object',
|
| 26 |
+
$id: 'http://mydomain.com/user',
|
| 27 |
+
title: 'User schema',
|
| 28 |
+
description: 'Contains all user fields',
|
| 29 |
+
properties: {
|
| 30 |
+
firstName: { type: 'string', minLength: 1 },
|
| 31 |
+
lastName: { type: 'string', minLength: 1 },
|
| 32 |
+
bio: { type: 'string' }
|
| 33 |
+
}
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
const fastifyAjvOptionsDefault = Object.freeze({
|
| 37 |
+
customOptions: {}
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
t.test('must not store schema on compile', t => {
|
| 41 |
+
t.plan(4)
|
| 42 |
+
const factory = AjvCompiler()
|
| 43 |
+
const compiler = factory({}, fastifyAjvOptionsDefault)
|
| 44 |
+
const postFn = compiler({ schema: postSchema })
|
| 45 |
+
const patchFn = compiler({ schema: patchSchema })
|
| 46 |
+
|
| 47 |
+
const resultForPost = postFn({})
|
| 48 |
+
t.equal(resultForPost, false)
|
| 49 |
+
t.has(postFn.errors, [
|
| 50 |
+
{
|
| 51 |
+
keyword: 'required',
|
| 52 |
+
message: "must have required property 'username'"
|
| 53 |
+
}
|
| 54 |
+
])
|
| 55 |
+
|
| 56 |
+
const resultForPatch = patchFn({})
|
| 57 |
+
t.ok(resultForPatch)
|
| 58 |
+
t.notOk(patchFn.errors)
|
| 59 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/index.test.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const t = require('tap')
|
| 4 |
+
const fastify = require('fastify')
|
| 5 |
+
const AjvCompiler = require('../index')
|
| 6 |
+
|
| 7 |
+
const sym = Symbol.for('fastify.ajv-compiler.reference')
|
| 8 |
+
|
| 9 |
+
const sampleSchema = Object.freeze({
|
| 10 |
+
$id: 'example1',
|
| 11 |
+
type: 'object',
|
| 12 |
+
properties: {
|
| 13 |
+
name: { type: 'string' }
|
| 14 |
+
}
|
| 15 |
+
})
|
| 16 |
+
|
| 17 |
+
const externalSchemas1 = Object.freeze({})
|
| 18 |
+
const externalSchemas2 = Object.freeze({
|
| 19 |
+
foo: {
|
| 20 |
+
$id: 'foo',
|
| 21 |
+
type: 'object',
|
| 22 |
+
properties: {
|
| 23 |
+
name: { type: 'string' }
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
const fastifyAjvOptionsDefault = Object.freeze({
|
| 29 |
+
customOptions: {}
|
| 30 |
+
})
|
| 31 |
+
|
| 32 |
+
const fastifyJtdDefault = Object.freeze({
|
| 33 |
+
customOptions: { },
|
| 34 |
+
mode: 'JTD'
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
const fastifyAjvOptionsCustom = Object.freeze({
|
| 38 |
+
customOptions: {
|
| 39 |
+
allErrors: true,
|
| 40 |
+
removeAdditional: false
|
| 41 |
+
},
|
| 42 |
+
plugins: [
|
| 43 |
+
require('ajv-formats'),
|
| 44 |
+
[require('ajv-errors'), { singleError: false }]
|
| 45 |
+
]
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
t.test('basic usage', t => {
|
| 49 |
+
t.plan(1)
|
| 50 |
+
const factory = AjvCompiler()
|
| 51 |
+
const compiler = factory(externalSchemas1, fastifyAjvOptionsDefault)
|
| 52 |
+
const validatorFunc = compiler({ schema: sampleSchema })
|
| 53 |
+
const result = validatorFunc({ name: 'hello' })
|
| 54 |
+
t.equal(result, true)
|
| 55 |
+
})
|
| 56 |
+
|
| 57 |
+
t.test('array coercion', t => {
|
| 58 |
+
t.plan(2)
|
| 59 |
+
const factory = AjvCompiler()
|
| 60 |
+
const compiler = factory(externalSchemas1, fastifyAjvOptionsDefault)
|
| 61 |
+
|
| 62 |
+
const arraySchema = {
|
| 63 |
+
$id: 'example1',
|
| 64 |
+
type: 'object',
|
| 65 |
+
properties: {
|
| 66 |
+
name: { type: 'array', items: { type: 'string' } }
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
const validatorFunc = compiler({ schema: arraySchema })
|
| 71 |
+
|
| 72 |
+
const inputObj = { name: 'hello' }
|
| 73 |
+
t.equal(validatorFunc(inputObj), true)
|
| 74 |
+
t.same(inputObj, { name: ['hello'] }, 'the name property should be coerced to an array')
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
t.test('nullable default', t => {
|
| 78 |
+
t.plan(2)
|
| 79 |
+
const factory = AjvCompiler()
|
| 80 |
+
const compiler = factory({}, fastifyAjvOptionsDefault)
|
| 81 |
+
const validatorFunc = compiler({
|
| 82 |
+
schema: {
|
| 83 |
+
type: 'object',
|
| 84 |
+
properties: {
|
| 85 |
+
nullable: { type: 'string', nullable: true },
|
| 86 |
+
notNullable: { type: 'string' }
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
})
|
| 90 |
+
const input = { nullable: null, notNullable: null }
|
| 91 |
+
const result = validatorFunc(input)
|
| 92 |
+
t.equal(result, true)
|
| 93 |
+
t.same(input, { nullable: null, notNullable: '' }, 'the notNullable field has been coerced')
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
t.test('plugin loading', t => {
|
| 97 |
+
t.plan(3)
|
| 98 |
+
const factory = AjvCompiler()
|
| 99 |
+
const compiler = factory(externalSchemas1, fastifyAjvOptionsCustom)
|
| 100 |
+
const validatorFunc = compiler({
|
| 101 |
+
schema: {
|
| 102 |
+
type: 'object',
|
| 103 |
+
properties: {
|
| 104 |
+
q: {
|
| 105 |
+
type: 'string',
|
| 106 |
+
format: 'date',
|
| 107 |
+
formatMinimum: '2016-02-06',
|
| 108 |
+
formatExclusiveMaximum: '2016-12-27'
|
| 109 |
+
}
|
| 110 |
+
},
|
| 111 |
+
required: ['q'],
|
| 112 |
+
errorMessage: 'hello world'
|
| 113 |
+
}
|
| 114 |
+
})
|
| 115 |
+
const result = validatorFunc({ q: '2016-10-02' })
|
| 116 |
+
t.equal(result, true)
|
| 117 |
+
|
| 118 |
+
const resultFail = validatorFunc({})
|
| 119 |
+
t.equal(resultFail, false)
|
| 120 |
+
t.equal(validatorFunc.errors[0].message, 'hello world')
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
t.test('optimization - cache ajv instance', t => {
|
| 124 |
+
t.plan(5)
|
| 125 |
+
const factory = AjvCompiler()
|
| 126 |
+
const compiler1 = factory(externalSchemas1, fastifyAjvOptionsDefault)
|
| 127 |
+
const compiler2 = factory(externalSchemas1, fastifyAjvOptionsDefault)
|
| 128 |
+
t.equal(compiler1, compiler2, 'same instance')
|
| 129 |
+
t.same(compiler1, compiler2, 'same instance')
|
| 130 |
+
|
| 131 |
+
const compiler3 = factory(externalSchemas2, fastifyAjvOptionsDefault)
|
| 132 |
+
t.not(compiler3, compiler1, 'new ajv instance when externa schema change')
|
| 133 |
+
|
| 134 |
+
const compiler4 = factory(externalSchemas1, fastifyAjvOptionsCustom)
|
| 135 |
+
t.not(compiler4, compiler1, 'new ajv instance when externa schema change')
|
| 136 |
+
t.not(compiler4, compiler3, 'new ajv instance when externa schema change')
|
| 137 |
+
})
|
| 138 |
+
|
| 139 |
+
t.test('the onCreate callback can enhance the ajv instance', t => {
|
| 140 |
+
t.plan(2)
|
| 141 |
+
const factory = AjvCompiler()
|
| 142 |
+
|
| 143 |
+
const fastifyAjvCustomOptionsFormats = Object.freeze({
|
| 144 |
+
onCreate (ajv) {
|
| 145 |
+
for (const [formatName, format] of Object.entries(this.customOptions.formats)) {
|
| 146 |
+
ajv.addFormat(formatName, format)
|
| 147 |
+
}
|
| 148 |
+
},
|
| 149 |
+
customOptions: {
|
| 150 |
+
formats: {
|
| 151 |
+
date: /foo/
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
})
|
| 155 |
+
|
| 156 |
+
const compiler1 = factory(externalSchemas1, fastifyAjvCustomOptionsFormats)
|
| 157 |
+
const validatorFunc = compiler1({
|
| 158 |
+
schema: {
|
| 159 |
+
type: 'string',
|
| 160 |
+
format: 'date'
|
| 161 |
+
}
|
| 162 |
+
})
|
| 163 |
+
const result = validatorFunc('foo')
|
| 164 |
+
t.equal(result, true)
|
| 165 |
+
|
| 166 |
+
const resultFail = validatorFunc('2016-10-02')
|
| 167 |
+
t.equal(resultFail, false)
|
| 168 |
+
})
|
| 169 |
+
|
| 170 |
+
// https://github.com/fastify/fastify/pull/2969
|
| 171 |
+
t.test('compile same $id when in external schema', t => {
|
| 172 |
+
t.plan(3)
|
| 173 |
+
const factory = AjvCompiler()
|
| 174 |
+
|
| 175 |
+
const base = {
|
| 176 |
+
$id: 'urn:schema:base',
|
| 177 |
+
definitions: {
|
| 178 |
+
hello: { type: 'string' }
|
| 179 |
+
},
|
| 180 |
+
type: 'object',
|
| 181 |
+
properties: {
|
| 182 |
+
hello: { $ref: '#/definitions/hello' }
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
const refSchema = {
|
| 187 |
+
$id: 'urn:schema:ref',
|
| 188 |
+
type: 'object',
|
| 189 |
+
properties: {
|
| 190 |
+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
|
| 191 |
+
}
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
const compiler = factory({
|
| 195 |
+
[base.$id]: base,
|
| 196 |
+
[refSchema.$id]: refSchema
|
| 197 |
+
|
| 198 |
+
}, fastifyAjvOptionsDefault)
|
| 199 |
+
|
| 200 |
+
t.notOk(compiler[sym], 'the ajv reference do not exists if code is not activated')
|
| 201 |
+
|
| 202 |
+
const validatorFunc1 = compiler({
|
| 203 |
+
schema: {
|
| 204 |
+
$id: 'urn:schema:ref'
|
| 205 |
+
}
|
| 206 |
+
})
|
| 207 |
+
|
| 208 |
+
const validatorFunc2 = compiler({
|
| 209 |
+
schema: {
|
| 210 |
+
$id: 'urn:schema:ref'
|
| 211 |
+
}
|
| 212 |
+
})
|
| 213 |
+
|
| 214 |
+
t.pass('the compile does not fail if the schema compiled is already in the external schemas')
|
| 215 |
+
t.equal(validatorFunc1, validatorFunc2, 'the returned function is the same')
|
| 216 |
+
})
|
| 217 |
+
|
| 218 |
+
t.test('JTD MODE', t => {
|
| 219 |
+
t.plan(2)
|
| 220 |
+
|
| 221 |
+
t.test('compile jtd schema', t => {
|
| 222 |
+
t.plan(4)
|
| 223 |
+
const factory = AjvCompiler()
|
| 224 |
+
|
| 225 |
+
const jtdSchema = {
|
| 226 |
+
discriminator: 'version',
|
| 227 |
+
mapping: {
|
| 228 |
+
1: {
|
| 229 |
+
properties: {
|
| 230 |
+
foo: { type: 'uint8' }
|
| 231 |
+
}
|
| 232 |
+
},
|
| 233 |
+
2: {
|
| 234 |
+
properties: {
|
| 235 |
+
foo: { type: 'string' }
|
| 236 |
+
}
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
const compiler = factory({}, fastifyJtdDefault)
|
| 242 |
+
const validatorFunc = compiler({ schema: jtdSchema })
|
| 243 |
+
t.pass('generated validation function for JTD SCHEMA')
|
| 244 |
+
|
| 245 |
+
const result = validatorFunc({
|
| 246 |
+
version: '2',
|
| 247 |
+
foo: []
|
| 248 |
+
})
|
| 249 |
+
t.notOk(result, 'failed validation')
|
| 250 |
+
t.type(validatorFunc.errors, 'Array')
|
| 251 |
+
|
| 252 |
+
const success = validatorFunc({
|
| 253 |
+
version: '1',
|
| 254 |
+
foo: 42
|
| 255 |
+
})
|
| 256 |
+
t.ok(success)
|
| 257 |
+
})
|
| 258 |
+
|
| 259 |
+
t.test('fastify integration', async t => {
|
| 260 |
+
const factory = AjvCompiler()
|
| 261 |
+
|
| 262 |
+
const app = fastify({
|
| 263 |
+
jsonShorthand: false,
|
| 264 |
+
ajv: {
|
| 265 |
+
customOptions: { },
|
| 266 |
+
mode: 'JTD'
|
| 267 |
+
},
|
| 268 |
+
schemaController: {
|
| 269 |
+
compilersFactory: {
|
| 270 |
+
buildValidator: factory
|
| 271 |
+
}
|
| 272 |
+
}
|
| 273 |
+
})
|
| 274 |
+
|
| 275 |
+
app.post('/', {
|
| 276 |
+
schema: {
|
| 277 |
+
body: {
|
| 278 |
+
discriminator: 'version',
|
| 279 |
+
mapping: {
|
| 280 |
+
1: {
|
| 281 |
+
properties: {
|
| 282 |
+
foo: { type: 'uint8' }
|
| 283 |
+
}
|
| 284 |
+
},
|
| 285 |
+
2: {
|
| 286 |
+
properties: {
|
| 287 |
+
foo: { type: 'string' }
|
| 288 |
+
}
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
}
|
| 292 |
+
}
|
| 293 |
+
}, () => {})
|
| 294 |
+
|
| 295 |
+
const res = await app.inject({
|
| 296 |
+
url: '/',
|
| 297 |
+
method: 'POST',
|
| 298 |
+
payload: {
|
| 299 |
+
version: '1',
|
| 300 |
+
foo: 'this is not a number'
|
| 301 |
+
}
|
| 302 |
+
})
|
| 303 |
+
|
| 304 |
+
t.equal(res.statusCode, 400)
|
| 305 |
+
t.equal(res.json().message, 'body/foo must be uint8')
|
| 306 |
+
})
|
| 307 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/plugins.test.js
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const t = require('tap')
|
| 4 |
+
const fastify = require('fastify')
|
| 5 |
+
const AjvCompiler = require('../index')
|
| 6 |
+
|
| 7 |
+
const ajvFormats = require('ajv-formats')
|
| 8 |
+
const ajvErrors = require('ajv-errors')
|
| 9 |
+
const localize = require('ajv-i18n')
|
| 10 |
+
|
| 11 |
+
t.test('Format Baseline test', async (t) => {
|
| 12 |
+
const app = buildApplication({
|
| 13 |
+
customOptions: {
|
| 14 |
+
validateFormats: false
|
| 15 |
+
}
|
| 16 |
+
})
|
| 17 |
+
|
| 18 |
+
const res = await app.inject({
|
| 19 |
+
url: '/hello',
|
| 20 |
+
headers: {
|
| 21 |
+
'x-foo': 'hello',
|
| 22 |
+
'x-date': 'not a date',
|
| 23 |
+
'x-email': 'not an email'
|
| 24 |
+
},
|
| 25 |
+
query: {
|
| 26 |
+
foo: 'hello',
|
| 27 |
+
date: 'not a date',
|
| 28 |
+
email: 'not an email'
|
| 29 |
+
}
|
| 30 |
+
})
|
| 31 |
+
t.equal(res.statusCode, 200, 'format validation does not apply as configured')
|
| 32 |
+
t.equal(res.payload, 'hello')
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
t.test('Custom Format plugin loading test', (t) => {
|
| 36 |
+
t.plan(6)
|
| 37 |
+
const app = buildApplication({
|
| 38 |
+
customOptions: {
|
| 39 |
+
validateFormats: true
|
| 40 |
+
},
|
| 41 |
+
plugins: [[ajvFormats, { mode: 'fast' }]]
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
app.inject('/hello', (err, res) => {
|
| 45 |
+
t.error(err)
|
| 46 |
+
t.equal(res.statusCode, 400, 'format validation applies')
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
app.inject('/2ad0612c-7578-4b18-9a6f-579863f40e0b', (err, res) => {
|
| 50 |
+
t.error(err)
|
| 51 |
+
t.equal(res.statusCode, 400, 'format validation applies')
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
app.inject({
|
| 55 |
+
url: '/2ad0612c-7578-4b18-9a6f-579863f40e0b',
|
| 56 |
+
headers: {
|
| 57 |
+
'x-foo': 'hello',
|
| 58 |
+
'x-date': new Date().toISOString(),
|
| 59 |
+
'x-email': 'foo@bar.baz'
|
| 60 |
+
},
|
| 61 |
+
query: {
|
| 62 |
+
foo: 'hello',
|
| 63 |
+
date: new Date().toISOString(),
|
| 64 |
+
email: 'foo@bar.baz'
|
| 65 |
+
}
|
| 66 |
+
}, (err, res) => {
|
| 67 |
+
t.error(err)
|
| 68 |
+
t.equal(res.statusCode, 200)
|
| 69 |
+
})
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
+
t.test('Format plugin set by default test', (t) => {
|
| 73 |
+
t.plan(6)
|
| 74 |
+
const app = buildApplication({})
|
| 75 |
+
|
| 76 |
+
app.inject('/hello', (err, res) => {
|
| 77 |
+
t.error(err)
|
| 78 |
+
t.equal(res.statusCode, 400, 'format validation applies')
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
app.inject('/2ad0612c-7578-4b18-9a6f-579863f40e0b', (err, res) => {
|
| 82 |
+
t.error(err)
|
| 83 |
+
t.equal(res.statusCode, 400, 'format validation applies')
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
app.inject({
|
| 87 |
+
url: '/2ad0612c-7578-4b18-9a6f-579863f40e0b',
|
| 88 |
+
headers: {
|
| 89 |
+
'x-foo': 'hello',
|
| 90 |
+
'x-date': new Date().toISOString(),
|
| 91 |
+
'x-email': 'foo@bar.baz'
|
| 92 |
+
},
|
| 93 |
+
query: {
|
| 94 |
+
foo: 'hello',
|
| 95 |
+
date: new Date().toISOString(),
|
| 96 |
+
email: 'foo@bar.baz'
|
| 97 |
+
}
|
| 98 |
+
}, (err, res) => {
|
| 99 |
+
t.error(err)
|
| 100 |
+
t.equal(res.statusCode, 200)
|
| 101 |
+
})
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
t.test('Custom error messages', (t) => {
|
| 105 |
+
t.plan(9)
|
| 106 |
+
|
| 107 |
+
const app = buildApplication({
|
| 108 |
+
customOptions: {
|
| 109 |
+
removeAdditional: false,
|
| 110 |
+
allErrors: true
|
| 111 |
+
},
|
| 112 |
+
plugins: [ajvFormats, ajvErrors]
|
| 113 |
+
})
|
| 114 |
+
|
| 115 |
+
const errorMessage = {
|
| 116 |
+
required: 'custom miss',
|
| 117 |
+
type: 'custom type', // will not replace internal "type" error for the property "foo"
|
| 118 |
+
_: 'custom type', // this prop will do it
|
| 119 |
+
additionalProperties: 'custom too many params'
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
app.post('/', {
|
| 123 |
+
handler: () => { t.fail('dont call me') },
|
| 124 |
+
schema: {
|
| 125 |
+
body: {
|
| 126 |
+
type: 'object',
|
| 127 |
+
required: ['foo'],
|
| 128 |
+
properties: {
|
| 129 |
+
foo: { type: 'integer' }
|
| 130 |
+
},
|
| 131 |
+
additionalProperties: false,
|
| 132 |
+
errorMessage
|
| 133 |
+
}
|
| 134 |
+
}
|
| 135 |
+
})
|
| 136 |
+
|
| 137 |
+
app.inject({
|
| 138 |
+
url: '/',
|
| 139 |
+
method: 'post',
|
| 140 |
+
payload: {}
|
| 141 |
+
}, (err, res) => {
|
| 142 |
+
t.error(err)
|
| 143 |
+
t.equal(res.statusCode, 400)
|
| 144 |
+
t.match(res.json().message, errorMessage.required)
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
app.inject({
|
| 148 |
+
url: '/',
|
| 149 |
+
method: 'post',
|
| 150 |
+
payload: { foo: 'not a number' }
|
| 151 |
+
}, (err, res) => {
|
| 152 |
+
t.error(err)
|
| 153 |
+
t.equal(res.statusCode, 400)
|
| 154 |
+
t.match(res.json().message, errorMessage.type)
|
| 155 |
+
})
|
| 156 |
+
|
| 157 |
+
app.inject({
|
| 158 |
+
url: '/',
|
| 159 |
+
method: 'post',
|
| 160 |
+
payload: { foo: 3, bar: 'ops' }
|
| 161 |
+
}, (err, res) => {
|
| 162 |
+
t.error(err)
|
| 163 |
+
t.equal(res.statusCode, 400)
|
| 164 |
+
t.match(res.json().message, errorMessage.additionalProperties)
|
| 165 |
+
})
|
| 166 |
+
})
|
| 167 |
+
|
| 168 |
+
t.test('Custom i18n error messages', (t) => {
|
| 169 |
+
t.plan(3)
|
| 170 |
+
|
| 171 |
+
const app = buildApplication({
|
| 172 |
+
customOptions: {
|
| 173 |
+
allErrors: true,
|
| 174 |
+
messages: false
|
| 175 |
+
},
|
| 176 |
+
plugins: [ajvFormats]
|
| 177 |
+
})
|
| 178 |
+
|
| 179 |
+
app.post('/', {
|
| 180 |
+
handler: () => { t.fail('dont call me') },
|
| 181 |
+
schema: {
|
| 182 |
+
body: {
|
| 183 |
+
type: 'object',
|
| 184 |
+
required: ['foo'],
|
| 185 |
+
properties: {
|
| 186 |
+
foo: { type: 'integer' }
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
}
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
app.setErrorHandler((error, request, reply) => {
|
| 193 |
+
t.pass('Error handler executed')
|
| 194 |
+
if (error.validation) {
|
| 195 |
+
localize.ru(error.validation)
|
| 196 |
+
reply.status(400).send(error.validation)
|
| 197 |
+
return
|
| 198 |
+
}
|
| 199 |
+
t.fail('not other errors')
|
| 200 |
+
})
|
| 201 |
+
|
| 202 |
+
app.inject({
|
| 203 |
+
method: 'POST',
|
| 204 |
+
url: '/',
|
| 205 |
+
payload: {
|
| 206 |
+
foo: 'string'
|
| 207 |
+
}
|
| 208 |
+
}, (err, res) => {
|
| 209 |
+
t.error(err)
|
| 210 |
+
t.equal(res.json()[0].message, 'должно быть integer')
|
| 211 |
+
})
|
| 212 |
+
})
|
| 213 |
+
|
| 214 |
+
function buildApplication (ajvOptions) {
|
| 215 |
+
const factory = AjvCompiler()
|
| 216 |
+
|
| 217 |
+
const app = fastify({
|
| 218 |
+
ajv: ajvOptions,
|
| 219 |
+
schemaController: {
|
| 220 |
+
compilersFactory: {
|
| 221 |
+
buildValidator: factory
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
})
|
| 225 |
+
|
| 226 |
+
app.get('/:id', {
|
| 227 |
+
schema: {
|
| 228 |
+
headers: {
|
| 229 |
+
type: 'object',
|
| 230 |
+
required: [
|
| 231 |
+
'x-foo',
|
| 232 |
+
'x-date',
|
| 233 |
+
'x-email'
|
| 234 |
+
],
|
| 235 |
+
properties: {
|
| 236 |
+
'x-foo': { type: 'string' },
|
| 237 |
+
'x-date': { type: 'string', format: 'date-time' },
|
| 238 |
+
'x-email': { type: 'string', format: 'email' }
|
| 239 |
+
}
|
| 240 |
+
},
|
| 241 |
+
query: {
|
| 242 |
+
type: 'object',
|
| 243 |
+
required: [
|
| 244 |
+
'foo',
|
| 245 |
+
'date',
|
| 246 |
+
'email'
|
| 247 |
+
],
|
| 248 |
+
properties: {
|
| 249 |
+
foo: { type: 'string' },
|
| 250 |
+
date: { type: 'string', format: 'date-time' },
|
| 251 |
+
email: { type: 'string', format: 'email' }
|
| 252 |
+
}
|
| 253 |
+
},
|
| 254 |
+
params: {
|
| 255 |
+
type: 'object',
|
| 256 |
+
properties: {
|
| 257 |
+
id: { type: 'string', format: 'uuid' }
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
}, async () => 'hello')
|
| 262 |
+
|
| 263 |
+
return app
|
| 264 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/serialization.test.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const t = require('tap')
|
| 4 |
+
const fastify = require('fastify')
|
| 5 |
+
const AjvCompiler = require('../index')
|
| 6 |
+
|
| 7 |
+
const jtdSchema = {
|
| 8 |
+
discriminator: 'version',
|
| 9 |
+
mapping: {
|
| 10 |
+
1: {
|
| 11 |
+
properties: {
|
| 12 |
+
foo: { type: 'uint8' }
|
| 13 |
+
}
|
| 14 |
+
},
|
| 15 |
+
2: {
|
| 16 |
+
properties: {
|
| 17 |
+
foo: { type: 'string' }
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
const externalSchemas1 = Object.freeze({})
|
| 24 |
+
const externalSchemas2 = Object.freeze({
|
| 25 |
+
foo: {
|
| 26 |
+
definitions: {
|
| 27 |
+
coordinates: {
|
| 28 |
+
properties: {
|
| 29 |
+
lat: { type: 'float32' },
|
| 30 |
+
lng: { type: 'float32' }
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
const fastifyAjvOptionsDefault = Object.freeze({
|
| 38 |
+
customOptions: {}
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
t.test('basic serializer usage', t => {
|
| 42 |
+
t.plan(4)
|
| 43 |
+
const factory = AjvCompiler({ jtdSerializer: true })
|
| 44 |
+
const compiler = factory(externalSchemas1, fastifyAjvOptionsDefault)
|
| 45 |
+
const serializeFunc = compiler({ schema: jtdSchema })
|
| 46 |
+
t.equal(serializeFunc({ version: '1', foo: 42 }), '{"version":"1","foo":42}')
|
| 47 |
+
t.equal(serializeFunc({ version: '2', foo: 'hello' }), '{"version":"2","foo":"hello"}')
|
| 48 |
+
t.equal(serializeFunc({ version: '3', foo: 'hello' }), '{"version":"3"}')
|
| 49 |
+
t.equal(serializeFunc({ version: '2', foo: ['not', 1, { string: 'string' }] }), '{"version":"2","foo":"not,1,[object Object]"}')
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
t.test('external schemas are ignored', t => {
|
| 53 |
+
t.plan(1)
|
| 54 |
+
const factory = AjvCompiler({ jtdSerializer: true })
|
| 55 |
+
const compiler = factory(externalSchemas2, fastifyAjvOptionsDefault)
|
| 56 |
+
const serializeFunc = compiler({
|
| 57 |
+
schema: {
|
| 58 |
+
definitions: {
|
| 59 |
+
coordinates: {
|
| 60 |
+
properties: {
|
| 61 |
+
lat: { type: 'float32' },
|
| 62 |
+
lng: { type: 'float32' }
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
},
|
| 66 |
+
properties: {
|
| 67 |
+
userLoc: { ref: 'coordinates' },
|
| 68 |
+
serverLoc: { ref: 'coordinates' }
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
})
|
| 72 |
+
t.equal(serializeFunc(
|
| 73 |
+
{ userLoc: { lat: 50, lng: -90 }, serverLoc: { lat: -15, lng: 50 } }),
|
| 74 |
+
'{"userLoc":{"lat":50,"lng":-90},"serverLoc":{"lat":-15,"lng":50}}'
|
| 75 |
+
)
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
t.test('fastify integration within JTD serializer', async t => {
|
| 79 |
+
const factoryValidator = AjvCompiler()
|
| 80 |
+
const factorySerializer = AjvCompiler({ jtdSerializer: true })
|
| 81 |
+
|
| 82 |
+
const app = fastify({
|
| 83 |
+
jsonShorthand: false,
|
| 84 |
+
ajv: {
|
| 85 |
+
customOptions: { },
|
| 86 |
+
mode: 'JTD'
|
| 87 |
+
},
|
| 88 |
+
schemaController: {
|
| 89 |
+
compilersFactory: {
|
| 90 |
+
buildValidator: factoryValidator,
|
| 91 |
+
buildSerializer: factorySerializer
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
app.post('/', {
|
| 97 |
+
schema: {
|
| 98 |
+
body: jtdSchema,
|
| 99 |
+
response: {
|
| 100 |
+
200: {
|
| 101 |
+
properties: {
|
| 102 |
+
id: { type: 'string' },
|
| 103 |
+
createdAt: { type: 'timestamp' },
|
| 104 |
+
karma: { type: 'int32' },
|
| 105 |
+
isAdmin: { type: 'boolean' }
|
| 106 |
+
}
|
| 107 |
+
},
|
| 108 |
+
400: jtdSchema
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
}, async () => {
|
| 112 |
+
return {
|
| 113 |
+
id: '123',
|
| 114 |
+
createdAt: new Date('1999-01-31T23:00:00.000Z'),
|
| 115 |
+
karma: 42,
|
| 116 |
+
isAdmin: true,
|
| 117 |
+
remove: 'me'
|
| 118 |
+
}
|
| 119 |
+
})
|
| 120 |
+
|
| 121 |
+
{
|
| 122 |
+
const res = await app.inject({
|
| 123 |
+
url: '/',
|
| 124 |
+
method: 'POST',
|
| 125 |
+
payload: {
|
| 126 |
+
version: '1',
|
| 127 |
+
foo: 'not a number'
|
| 128 |
+
}
|
| 129 |
+
})
|
| 130 |
+
|
| 131 |
+
t.equal(res.statusCode, 400)
|
| 132 |
+
t.same(res.json(), { version: 'undefined' })
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
{
|
| 136 |
+
const res = await app.inject({
|
| 137 |
+
url: '/',
|
| 138 |
+
method: 'POST',
|
| 139 |
+
payload: {
|
| 140 |
+
version: '1',
|
| 141 |
+
foo: 32
|
| 142 |
+
}
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
t.equal(res.statusCode, 200)
|
| 146 |
+
t.same(res.json(), {
|
| 147 |
+
id: '123',
|
| 148 |
+
createdAt: '1999-01-31T23:00:00.000Z',
|
| 149 |
+
karma: 42,
|
| 150 |
+
isAdmin: true
|
| 151 |
+
})
|
| 152 |
+
}
|
| 153 |
+
})
|
| 154 |
+
|
| 155 |
+
t.test('fastify integration and cached serializer', async t => {
|
| 156 |
+
const factoryValidator = AjvCompiler()
|
| 157 |
+
const factorySerializer = AjvCompiler({ jtdSerializer: true })
|
| 158 |
+
|
| 159 |
+
const app = fastify({
|
| 160 |
+
jsonShorthand: false,
|
| 161 |
+
ajv: {
|
| 162 |
+
customOptions: { },
|
| 163 |
+
mode: 'JTD'
|
| 164 |
+
},
|
| 165 |
+
schemaController: {
|
| 166 |
+
compilersFactory: {
|
| 167 |
+
buildValidator: factoryValidator,
|
| 168 |
+
buildSerializer: factorySerializer
|
| 169 |
+
}
|
| 170 |
+
}
|
| 171 |
+
})
|
| 172 |
+
|
| 173 |
+
app.register(async function plugin (app, opts) {
|
| 174 |
+
app.post('/', {
|
| 175 |
+
schema: {
|
| 176 |
+
body: jtdSchema,
|
| 177 |
+
response: {
|
| 178 |
+
200: {
|
| 179 |
+
properties: {
|
| 180 |
+
id: { type: 'string' },
|
| 181 |
+
createdAt: { type: 'timestamp' },
|
| 182 |
+
karma: { type: 'int32' },
|
| 183 |
+
isAdmin: { type: 'boolean' }
|
| 184 |
+
}
|
| 185 |
+
},
|
| 186 |
+
400: jtdSchema
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
}, async () => {
|
| 190 |
+
return {
|
| 191 |
+
id: '123',
|
| 192 |
+
createdAt: new Date('1999-01-31T23:00:00.000Z'),
|
| 193 |
+
karma: 42,
|
| 194 |
+
isAdmin: true,
|
| 195 |
+
remove: 'me'
|
| 196 |
+
}
|
| 197 |
+
})
|
| 198 |
+
})
|
| 199 |
+
|
| 200 |
+
app.register(async function plugin (app, opts) {
|
| 201 |
+
app.post('/two', {
|
| 202 |
+
schema: {
|
| 203 |
+
body: jtdSchema,
|
| 204 |
+
response: {
|
| 205 |
+
400: jtdSchema
|
| 206 |
+
}
|
| 207 |
+
}
|
| 208 |
+
}, () => {})
|
| 209 |
+
})
|
| 210 |
+
|
| 211 |
+
{
|
| 212 |
+
const res = await app.inject({
|
| 213 |
+
url: '/',
|
| 214 |
+
method: 'POST',
|
| 215 |
+
payload: {
|
| 216 |
+
version: '1',
|
| 217 |
+
foo: 'not a number'
|
| 218 |
+
}
|
| 219 |
+
})
|
| 220 |
+
|
| 221 |
+
t.equal(res.statusCode, 400)
|
| 222 |
+
t.same(res.json(), { version: 'undefined' })
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
{
|
| 226 |
+
const res = await app.inject({
|
| 227 |
+
url: '/',
|
| 228 |
+
method: 'POST',
|
| 229 |
+
payload: {
|
| 230 |
+
version: '1',
|
| 231 |
+
foo: 32
|
| 232 |
+
}
|
| 233 |
+
})
|
| 234 |
+
|
| 235 |
+
t.equal(res.statusCode, 200)
|
| 236 |
+
t.same(res.json(), {
|
| 237 |
+
id: '123',
|
| 238 |
+
createdAt: '1999-01-31T23:00:00.000Z',
|
| 239 |
+
karma: 42,
|
| 240 |
+
isAdmin: true
|
| 241 |
+
})
|
| 242 |
+
}
|
| 243 |
+
})
|
| 244 |
+
|
| 245 |
+
t.test('fastify integration within JTD serializer and custom options', async t => {
|
| 246 |
+
const factorySerializer = AjvCompiler({ jtdSerializer: true })
|
| 247 |
+
|
| 248 |
+
const app = fastify({
|
| 249 |
+
jsonShorthand: false,
|
| 250 |
+
serializerOpts: {
|
| 251 |
+
allErrors: true,
|
| 252 |
+
logger: 'wrong-value'
|
| 253 |
+
},
|
| 254 |
+
schemaController: {
|
| 255 |
+
compilersFactory: {
|
| 256 |
+
buildSerializer: factorySerializer
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
})
|
| 260 |
+
|
| 261 |
+
app.post('/', {
|
| 262 |
+
schema: {
|
| 263 |
+
response: {
|
| 264 |
+
200: {
|
| 265 |
+
properties: {
|
| 266 |
+
test: { type: 'boolean' }
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
}
|
| 270 |
+
}
|
| 271 |
+
}, async () => { })
|
| 272 |
+
|
| 273 |
+
try {
|
| 274 |
+
await app.ready()
|
| 275 |
+
t.fail('should throw')
|
| 276 |
+
} catch (error) {
|
| 277 |
+
t.equal(error.message, 'logger must implement log, warn and error methods', 'the wrong setting is forwarded to ajv/jtd')
|
| 278 |
+
}
|
| 279 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/test/standalone.test.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const fs = require('node:fs')
|
| 4 |
+
const path = require('node:path')
|
| 5 |
+
const t = require('tap')
|
| 6 |
+
const fastify = require('fastify')
|
| 7 |
+
const sanitize = require('sanitize-filename')
|
| 8 |
+
|
| 9 |
+
const { StandaloneValidator: AjvStandaloneValidator } = require('../')
|
| 10 |
+
|
| 11 |
+
function generateFileName (routeOpts) {
|
| 12 |
+
return `/ajv-generated-${sanitize(routeOpts.schema.$id)}-${routeOpts.method}-${routeOpts.httpPart}-${sanitize(routeOpts.url)}.js`
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
const generatedFileNames = []
|
| 16 |
+
|
| 17 |
+
t.test('standalone', t => {
|
| 18 |
+
t.plan(4)
|
| 19 |
+
|
| 20 |
+
t.teardown(async () => {
|
| 21 |
+
for (const fileName of generatedFileNames) {
|
| 22 |
+
await fs.promises.unlink(path.join(__dirname, fileName))
|
| 23 |
+
}
|
| 24 |
+
})
|
| 25 |
+
|
| 26 |
+
t.test('errors', t => {
|
| 27 |
+
t.plan(2)
|
| 28 |
+
t.throws(() => {
|
| 29 |
+
AjvStandaloneValidator()
|
| 30 |
+
}, 'missing restoreFunction')
|
| 31 |
+
t.throws(() => {
|
| 32 |
+
AjvStandaloneValidator({ readMode: false })
|
| 33 |
+
}, 'missing storeFunction')
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
t.test('generate standalone code', t => {
|
| 37 |
+
t.plan(5)
|
| 38 |
+
|
| 39 |
+
const base = {
|
| 40 |
+
$id: 'urn:schema:base',
|
| 41 |
+
definitions: {
|
| 42 |
+
hello: { type: 'string' }
|
| 43 |
+
},
|
| 44 |
+
type: 'object',
|
| 45 |
+
properties: {
|
| 46 |
+
hello: { $ref: '#/definitions/hello' }
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
const refSchema = {
|
| 51 |
+
$id: 'urn:schema:ref',
|
| 52 |
+
type: 'object',
|
| 53 |
+
properties: {
|
| 54 |
+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
const endpointSchema = {
|
| 59 |
+
schema: {
|
| 60 |
+
$id: 'urn:schema:endpoint',
|
| 61 |
+
$ref: 'urn:schema:ref'
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const schemaMap = {
|
| 66 |
+
[base.$id]: base,
|
| 67 |
+
[refSchema.$id]: refSchema
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
const factory = AjvStandaloneValidator({
|
| 71 |
+
readMode: false,
|
| 72 |
+
storeFunction (routeOpts, schemaValidationCode) {
|
| 73 |
+
t.same(routeOpts, endpointSchema)
|
| 74 |
+
t.type(schemaValidationCode, 'string')
|
| 75 |
+
fs.writeFileSync(path.join(__dirname, '/ajv-generated.js'), schemaValidationCode)
|
| 76 |
+
generatedFileNames.push('/ajv-generated.js')
|
| 77 |
+
t.pass('stored the validation function')
|
| 78 |
+
}
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
const compiler = factory(schemaMap)
|
| 82 |
+
compiler(endpointSchema)
|
| 83 |
+
t.pass('compiled the endpoint schema')
|
| 84 |
+
|
| 85 |
+
t.test('usage standalone code', t => {
|
| 86 |
+
t.plan(3)
|
| 87 |
+
const standaloneValidate = require('./ajv-generated')
|
| 88 |
+
|
| 89 |
+
const valid = standaloneValidate({ hello: 'world' })
|
| 90 |
+
t.ok(valid)
|
| 91 |
+
|
| 92 |
+
const invalid = standaloneValidate({ hello: [] })
|
| 93 |
+
t.notOk(invalid)
|
| 94 |
+
|
| 95 |
+
t.ok(standaloneValidate)
|
| 96 |
+
})
|
| 97 |
+
})
|
| 98 |
+
|
| 99 |
+
t.test('fastify integration - writeMode', async t => {
|
| 100 |
+
t.plan(6)
|
| 101 |
+
|
| 102 |
+
const factory = AjvStandaloneValidator({
|
| 103 |
+
readMode: false,
|
| 104 |
+
storeFunction (routeOpts, schemaValidationCode) {
|
| 105 |
+
const fileName = generateFileName(routeOpts)
|
| 106 |
+
t.ok(routeOpts)
|
| 107 |
+
fs.writeFileSync(path.join(__dirname, fileName), schemaValidationCode)
|
| 108 |
+
t.pass('stored the validation function')
|
| 109 |
+
generatedFileNames.push(fileName)
|
| 110 |
+
},
|
| 111 |
+
restoreFunction () {
|
| 112 |
+
t.fail('write mode ON')
|
| 113 |
+
}
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
const app = buildApp(factory)
|
| 117 |
+
await app.ready()
|
| 118 |
+
})
|
| 119 |
+
|
| 120 |
+
t.test('fastify integration - readMode', async t => {
|
| 121 |
+
t.plan(6)
|
| 122 |
+
|
| 123 |
+
const factory = AjvStandaloneValidator({
|
| 124 |
+
readMode: true,
|
| 125 |
+
storeFunction () {
|
| 126 |
+
t.fail('read mode ON')
|
| 127 |
+
},
|
| 128 |
+
restoreFunction (routeOpts) {
|
| 129 |
+
t.pass('restore the validation function')
|
| 130 |
+
const fileName = generateFileName(routeOpts)
|
| 131 |
+
return require(path.join(__dirname, fileName))
|
| 132 |
+
}
|
| 133 |
+
})
|
| 134 |
+
|
| 135 |
+
const app = buildApp(factory)
|
| 136 |
+
await app.ready()
|
| 137 |
+
|
| 138 |
+
let res = await app.inject({
|
| 139 |
+
url: '/foo',
|
| 140 |
+
method: 'POST',
|
| 141 |
+
payload: { hello: [] }
|
| 142 |
+
})
|
| 143 |
+
t.equal(res.statusCode, 400)
|
| 144 |
+
|
| 145 |
+
res = await app.inject({
|
| 146 |
+
url: '/bar?lang=invalid',
|
| 147 |
+
method: 'GET'
|
| 148 |
+
})
|
| 149 |
+
t.equal(res.statusCode, 400)
|
| 150 |
+
|
| 151 |
+
res = await app.inject({
|
| 152 |
+
url: '/bar?lang=it',
|
| 153 |
+
method: 'GET'
|
| 154 |
+
})
|
| 155 |
+
t.equal(res.statusCode, 200)
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
+
function buildApp (factory) {
|
| 159 |
+
const app = fastify({
|
| 160 |
+
jsonShorthand: false,
|
| 161 |
+
schemaController: {
|
| 162 |
+
compilersFactory: {
|
| 163 |
+
buildValidator: factory
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
})
|
| 167 |
+
|
| 168 |
+
app.addSchema({
|
| 169 |
+
$id: 'urn:schema:foo',
|
| 170 |
+
type: 'object',
|
| 171 |
+
properties: {
|
| 172 |
+
name: { type: 'string' },
|
| 173 |
+
id: { type: 'integer' }
|
| 174 |
+
}
|
| 175 |
+
})
|
| 176 |
+
|
| 177 |
+
app.post('/foo', {
|
| 178 |
+
schema: {
|
| 179 |
+
body: {
|
| 180 |
+
$id: 'urn:schema:body',
|
| 181 |
+
type: 'object',
|
| 182 |
+
properties: {
|
| 183 |
+
hello: { $ref: 'urn:schema:foo#/properties/name' }
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
}, () => { return 'ok' })
|
| 188 |
+
|
| 189 |
+
app.get('/bar', {
|
| 190 |
+
schema: {
|
| 191 |
+
query: {
|
| 192 |
+
$id: 'urn:schema:query',
|
| 193 |
+
type: 'object',
|
| 194 |
+
properties: {
|
| 195 |
+
lang: { type: 'string', enum: ['it', 'en'] }
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
}, () => { return 'ok' })
|
| 200 |
+
|
| 201 |
+
return app
|
| 202 |
+
}
|
| 203 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/types/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import _ajv, { AnySchema, Options as AjvOptions, ValidateFunction } from 'ajv'
|
| 2 |
+
import AjvJTD, { JTDOptions } from 'ajv/dist/jtd'
|
| 3 |
+
import type { Options, ErrorObject } from 'ajv'
|
| 4 |
+
import { AnyValidateFunction } from 'ajv/dist/core'
|
| 5 |
+
|
| 6 |
+
type Ajv = _ajv
|
| 7 |
+
type AjvSerializerGenerator = typeof AjvCompiler
|
| 8 |
+
|
| 9 |
+
type AjvJTDCompile = AjvJTD['compileSerializer']
|
| 10 |
+
type AjvCompile = (schema: AnySchema, _meta?: boolean) => AnyValidateFunction
|
| 11 |
+
|
| 12 |
+
declare function buildCompilerFromPool (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode: 'JTD'; customOptions?: JTDOptions; onCreate?: (ajvInstance: Ajv) => void }): AjvCompile
|
| 13 |
+
declare function buildCompilerFromPool (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode?: never; customOptions?: AjvOptions; onCreate?: (ajvInstance: Ajv) => void }): AjvCompile
|
| 14 |
+
|
| 15 |
+
declare function buildSerializerFromPool (externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions): AjvJTDCompile
|
| 16 |
+
|
| 17 |
+
declare function AjvCompiler (opts: { jtdSerializer: true }): AjvCompiler.BuildSerializerFromPool
|
| 18 |
+
declare function AjvCompiler (opts?: { jtdSerializer?: false }): AjvCompiler.BuildCompilerFromPool
|
| 19 |
+
|
| 20 |
+
declare function StandaloneValidator (options: AjvCompiler.StandaloneOptions): AjvCompiler.BuildCompilerFromPool
|
| 21 |
+
|
| 22 |
+
declare namespace AjvCompiler {
|
| 23 |
+
export type { Options, ErrorObject }
|
| 24 |
+
export { Ajv }
|
| 25 |
+
|
| 26 |
+
export type BuildSerializerFromPool = typeof buildSerializerFromPool
|
| 27 |
+
|
| 28 |
+
export type BuildCompilerFromPool = typeof buildCompilerFromPool
|
| 29 |
+
|
| 30 |
+
export const AjvReference: Symbol
|
| 31 |
+
|
| 32 |
+
export enum HttpParts {
|
| 33 |
+
Body = 'body',
|
| 34 |
+
Headers = 'headers',
|
| 35 |
+
Params = 'params',
|
| 36 |
+
Query = 'querystring',
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
export type RouteDefinition = {
|
| 40 |
+
method: string,
|
| 41 |
+
url: string,
|
| 42 |
+
httpPart: HttpParts,
|
| 43 |
+
schema?: unknown,
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
export type StandaloneRestoreFunction = (opts: RouteDefinition) => ValidateFunction
|
| 47 |
+
|
| 48 |
+
export type StandaloneStoreFunction = (opts: RouteDefinition, schemaValidationCode: string) => void
|
| 49 |
+
|
| 50 |
+
export type StandaloneOptionsReadModeOn = {
|
| 51 |
+
readMode: true;
|
| 52 |
+
restoreFunction?: StandaloneRestoreFunction
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
export type StandaloneOptionsReadModeOff = {
|
| 56 |
+
readMode?: false | undefined;
|
| 57 |
+
storeFunction?: StandaloneStoreFunction;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff
|
| 61 |
+
|
| 62 |
+
export type ValidatorFactory = BuildCompilerFromPool | BuildSerializerFromPool
|
| 63 |
+
|
| 64 |
+
export type ValidatorCompiler = ReturnType<ValidatorFactory>
|
| 65 |
+
|
| 66 |
+
export { StandaloneValidator }
|
| 67 |
+
|
| 68 |
+
export const AjvCompiler: AjvSerializerGenerator
|
| 69 |
+
export { AjvCompiler as default }
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
export = AjvCompiler
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/ajv-compiler/types/index.test-d.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { AnySchemaObject, ValidateFunction } from 'ajv'
|
| 2 |
+
import { AnyValidateFunction } from 'ajv/dist/core'
|
| 3 |
+
import { expectAssignable, expectType } from 'tsd'
|
| 4 |
+
import AjvCompiler, { AjvReference, ValidatorFactory, StandaloneValidator, RouteDefinition, ErrorObject, BuildCompilerFromPool, BuildSerializerFromPool, ValidatorCompiler } from '..'
|
| 5 |
+
|
| 6 |
+
{
|
| 7 |
+
const compiler = AjvCompiler({})
|
| 8 |
+
expectType<BuildCompilerFromPool>(compiler)
|
| 9 |
+
}
|
| 10 |
+
{
|
| 11 |
+
const compiler = AjvCompiler()
|
| 12 |
+
expectType<BuildCompilerFromPool>(compiler)
|
| 13 |
+
}
|
| 14 |
+
{
|
| 15 |
+
const compiler = AjvCompiler({ jtdSerializer: false })
|
| 16 |
+
expectType<BuildCompilerFromPool>(compiler)
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
{
|
| 20 |
+
const factory = AjvCompiler({ jtdSerializer: false })
|
| 21 |
+
expectType<BuildCompilerFromPool>(factory)
|
| 22 |
+
factory({}, {
|
| 23 |
+
onCreate (ajv) {
|
| 24 |
+
expectType<import('ajv').default>(ajv)
|
| 25 |
+
}
|
| 26 |
+
})
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
{
|
| 30 |
+
const compiler = AjvCompiler({ jtdSerializer: true })
|
| 31 |
+
expectType<BuildSerializerFromPool>(compiler)
|
| 32 |
+
}
|
| 33 |
+
const reader = StandaloneValidator({
|
| 34 |
+
readMode: true,
|
| 35 |
+
restoreFunction: (route: RouteDefinition) => {
|
| 36 |
+
expectAssignable<RouteDefinition>(route)
|
| 37 |
+
return {} as ValidateFunction
|
| 38 |
+
},
|
| 39 |
+
})
|
| 40 |
+
expectAssignable<ValidatorFactory>(reader)
|
| 41 |
+
|
| 42 |
+
const writer = StandaloneValidator({
|
| 43 |
+
readMode: false,
|
| 44 |
+
storeFunction: (route: RouteDefinition, code: string) => {
|
| 45 |
+
expectAssignable<RouteDefinition>(route)
|
| 46 |
+
expectAssignable<string>(code)
|
| 47 |
+
},
|
| 48 |
+
})
|
| 49 |
+
expectAssignable<ValidatorFactory>(writer)
|
| 50 |
+
|
| 51 |
+
expectType<unknown>(({} as ErrorObject).data)
|
| 52 |
+
expectType<string>(({} as ErrorObject).instancePath)
|
| 53 |
+
expectType<string>(({} as ErrorObject).keyword)
|
| 54 |
+
expectType<string | undefined>(({} as ErrorObject).message)
|
| 55 |
+
expectType<Record<string, any>>(({} as ErrorObject).params)
|
| 56 |
+
expectType<AnySchemaObject | undefined>(({} as ErrorObject).parentSchema)
|
| 57 |
+
expectType<string | undefined>(({} as ErrorObject).propertyName)
|
| 58 |
+
expectType<unknown>(({} as ErrorObject).schema)
|
| 59 |
+
expectType<string>(({} as ErrorObject).schemaPath)
|
| 60 |
+
|
| 61 |
+
expectType<Symbol>(AjvReference)
|
| 62 |
+
|
| 63 |
+
{
|
| 64 |
+
const jtdSchema = {
|
| 65 |
+
discriminator: 'version',
|
| 66 |
+
mapping: {
|
| 67 |
+
1: {
|
| 68 |
+
properties: {
|
| 69 |
+
foo: { type: 'uint8' }
|
| 70 |
+
}
|
| 71 |
+
},
|
| 72 |
+
2: {
|
| 73 |
+
properties: {
|
| 74 |
+
foo: { type: 'string' }
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const externalSchemas1 = {
|
| 81 |
+
foo: {
|
| 82 |
+
definitions: {
|
| 83 |
+
coordinates: {
|
| 84 |
+
properties: {
|
| 85 |
+
lat: { type: 'float32' },
|
| 86 |
+
lng: { type: 'float32' }
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
const factory = AjvCompiler({ jtdSerializer: true })
|
| 94 |
+
expectType<BuildSerializerFromPool>(factory)
|
| 95 |
+
const compiler = factory(externalSchemas1, {})
|
| 96 |
+
expectAssignable<Function>(compiler)
|
| 97 |
+
const serializeFunc = compiler({ schema: jtdSchema })
|
| 98 |
+
expectType<(data: unknown) => string>(serializeFunc)
|
| 99 |
+
expectType<string>(serializeFunc({ version: '1', foo: 42 }))
|
| 100 |
+
}
|
| 101 |
+
// JTD
|
| 102 |
+
{
|
| 103 |
+
const factory = AjvCompiler()
|
| 104 |
+
expectType<BuildCompilerFromPool>(factory)
|
| 105 |
+
|
| 106 |
+
const jtdSchema = {
|
| 107 |
+
discriminator: 'version',
|
| 108 |
+
mapping: {
|
| 109 |
+
1: {
|
| 110 |
+
properties: {
|
| 111 |
+
foo: { type: 'uint8' }
|
| 112 |
+
}
|
| 113 |
+
},
|
| 114 |
+
2: {
|
| 115 |
+
properties: {
|
| 116 |
+
foo: { type: 'string' }
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
const compiler = factory({}, {
|
| 123 |
+
customOptions: {},
|
| 124 |
+
mode: 'JTD'
|
| 125 |
+
})
|
| 126 |
+
expectAssignable<ValidatorCompiler>(compiler)
|
| 127 |
+
const validatorFunc = compiler({ schema: jtdSchema })
|
| 128 |
+
expectAssignable<ValidateFunction>(validatorFunc)
|
| 129 |
+
|
| 130 |
+
expectType<boolean | Promise<any>>(validatorFunc({
|
| 131 |
+
version: '2',
|
| 132 |
+
foo: []
|
| 133 |
+
}))
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// generate standalone code
|
| 137 |
+
{
|
| 138 |
+
const base = {
|
| 139 |
+
$id: 'urn:schema:base',
|
| 140 |
+
definitions: {
|
| 141 |
+
hello: { type: 'string' }
|
| 142 |
+
},
|
| 143 |
+
type: 'object',
|
| 144 |
+
properties: {
|
| 145 |
+
hello: { $ref: '#/definitions/hello' }
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
const refSchema = {
|
| 150 |
+
$id: 'urn:schema:ref',
|
| 151 |
+
type: 'object',
|
| 152 |
+
properties: {
|
| 153 |
+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
|
| 154 |
+
}
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
const endpointSchema = {
|
| 158 |
+
schema: {
|
| 159 |
+
$id: 'urn:schema:endpoint',
|
| 160 |
+
$ref: 'urn:schema:ref'
|
| 161 |
+
}
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
const schemaMap = {
|
| 165 |
+
[base.$id]: base,
|
| 166 |
+
[refSchema.$id]: refSchema
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
const factory = StandaloneValidator({
|
| 170 |
+
readMode: false,
|
| 171 |
+
storeFunction (routeOpts, schemaValidationCode) {
|
| 172 |
+
expectType<RouteDefinition>(routeOpts)
|
| 173 |
+
expectType<string>(schemaValidationCode)
|
| 174 |
+
}
|
| 175 |
+
})
|
| 176 |
+
expectAssignable<ValidatorFactory>(factory)
|
| 177 |
+
|
| 178 |
+
const compiler = factory(schemaMap)
|
| 179 |
+
expectAssignable<ValidatorCompiler>(compiler)
|
| 180 |
+
expectAssignable<Function>(compiler(endpointSchema))
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
{
|
| 184 |
+
const base = {
|
| 185 |
+
$id: 'urn:schema:base',
|
| 186 |
+
definitions: {
|
| 187 |
+
hello: { type: 'string' }
|
| 188 |
+
},
|
| 189 |
+
type: 'object',
|
| 190 |
+
properties: {
|
| 191 |
+
hello: { $ref: '#/definitions/hello' }
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
const refSchema = {
|
| 196 |
+
$id: 'urn:schema:ref',
|
| 197 |
+
type: 'object',
|
| 198 |
+
properties: {
|
| 199 |
+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
const endpointSchema = {
|
| 204 |
+
schema: {
|
| 205 |
+
$id: 'urn:schema:endpoint',
|
| 206 |
+
$ref: 'urn:schema:ref'
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
const schemaMap = {
|
| 211 |
+
[base.$id]: base,
|
| 212 |
+
[refSchema.$id]: refSchema
|
| 213 |
+
}
|
| 214 |
+
const factory = StandaloneValidator({
|
| 215 |
+
readMode: true,
|
| 216 |
+
restoreFunction (routeOpts) {
|
| 217 |
+
expectType<RouteDefinition>(routeOpts)
|
| 218 |
+
return {} as ValidateFunction
|
| 219 |
+
}
|
| 220 |
+
})
|
| 221 |
+
expectAssignable<ValidatorFactory>(factory)
|
| 222 |
+
|
| 223 |
+
const compiler = factory(schemaMap)
|
| 224 |
+
expectAssignable<ValidatorCompiler>(compiler)
|
| 225 |
+
expectType<AnyValidateFunction<any>>(compiler(endpointSchema))
|
| 226 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.editorconfig
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
root = true
|
| 2 |
+
|
| 3 |
+
[*]
|
| 4 |
+
indent_style = space
|
| 5 |
+
indent_size = 2
|
| 6 |
+
charset = utf-8
|
| 7 |
+
trim_trailing_whitespace = false
|
| 8 |
+
insert_final_newline = false
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.gitattributes
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Set the default behavior, in case people don't have core.autocrlf set
|
| 2 |
+
* text=auto
|
| 3 |
+
|
| 4 |
+
# Require Unix line endings
|
| 5 |
+
* text eol=lf
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/dependabot.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: 2
|
| 2 |
+
updates:
|
| 3 |
+
- package-ecosystem: "github-actions"
|
| 4 |
+
directory: "/"
|
| 5 |
+
schedule:
|
| 6 |
+
interval: "monthly"
|
| 7 |
+
open-pull-requests-limit: 10
|
| 8 |
+
|
| 9 |
+
- package-ecosystem: "npm"
|
| 10 |
+
directory: "/"
|
| 11 |
+
schedule:
|
| 12 |
+
interval: "monthly"
|
| 13 |
+
open-pull-requests-limit: 10
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/stale.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Number of days of inactivity before an issue becomes stale
|
| 2 |
+
daysUntilStale: 15
|
| 3 |
+
# Number of days of inactivity before a stale issue is closed
|
| 4 |
+
daysUntilClose: 7
|
| 5 |
+
# Issues with these labels will never be considered stale
|
| 6 |
+
exemptLabels:
|
| 7 |
+
- "discussion"
|
| 8 |
+
- "feature request"
|
| 9 |
+
- "bug"
|
| 10 |
+
- "help wanted"
|
| 11 |
+
- "plugin suggestion"
|
| 12 |
+
- "good first issue"
|
| 13 |
+
# Label to use when marking an issue as stale
|
| 14 |
+
staleLabel: stale
|
| 15 |
+
# Comment to post when marking an issue as stale. Set to `false` to disable
|
| 16 |
+
markComment: >
|
| 17 |
+
This issue has been automatically marked as stale because it has not had
|
| 18 |
+
recent activity. It will be closed if no further activity occurs. Thank you
|
| 19 |
+
for your contributions.
|
| 20 |
+
# Comment to post when closing a stale issue. Set to `false` to disable
|
| 21 |
+
closeComment: false
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/.github/workflows/ci.yml
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: CI
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
- next
|
| 8 |
+
- 'v*'
|
| 9 |
+
paths-ignore:
|
| 10 |
+
- 'docs/**'
|
| 11 |
+
- '*.md'
|
| 12 |
+
pull_request:
|
| 13 |
+
paths-ignore:
|
| 14 |
+
- 'docs/**'
|
| 15 |
+
- '*.md'
|
| 16 |
+
|
| 17 |
+
permissions:
|
| 18 |
+
contents: read
|
| 19 |
+
|
| 20 |
+
jobs:
|
| 21 |
+
test:
|
| 22 |
+
permissions:
|
| 23 |
+
contents: write
|
| 24 |
+
pull-requests: write
|
| 25 |
+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
|
| 26 |
+
with:
|
| 27 |
+
license-check: true
|
| 28 |
+
lint: true
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2018-present The Fastify team
|
| 4 |
+
|
| 5 |
+
The Fastify team members are listed at https://github.com/fastify/fastify#team.
|
| 6 |
+
|
| 7 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 8 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 9 |
+
in the Software without restriction, including without limitation the rights
|
| 10 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 11 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 12 |
+
furnished to do so, subject to the following conditions:
|
| 13 |
+
|
| 14 |
+
The above copyright notice and this permission notice shall be included in all
|
| 15 |
+
copies or substantial portions of the Software.
|
| 16 |
+
|
| 17 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 18 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 19 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 20 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 21 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 22 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 23 |
+
SOFTWARE.
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/README.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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@fastify/cors)
|
| 5 |
+
[](https://github.com/neostandard/neostandard)
|
| 6 |
+
|
| 7 |
+
`@fastify/cors` enables the use of [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) in a Fastify application.
|
| 8 |
+
|
| 9 |
+
## Install
|
| 10 |
+
```
|
| 11 |
+
npm i @fastify/cors
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
### Compatibility
|
| 15 |
+
|
| 16 |
+
| Plugin version | Fastify version |
|
| 17 |
+
| ---------------|-----------------|
|
| 18 |
+
| `^11.x` | `^5.x` |
|
| 19 |
+
| `^10.x` | `^5.x` |
|
| 20 |
+
| `^8.x` | `^4.x` |
|
| 21 |
+
| `^7.x` | `^3.x` |
|
| 22 |
+
| `>=3.x <7.x` | `^2.x` |
|
| 23 |
+
| `>=1.x <3.x` | `^1.x` |
|
| 24 |
+
|
| 25 |
+
Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
|
| 26 |
+
in the table above.
|
| 27 |
+
See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
|
| 28 |
+
|
| 29 |
+
## Usage
|
| 30 |
+
Require `@fastify/cors` and register it as any other plugin. It adds an `onRequest` hook and a [wildcard options route](https://github.com/fastify/fastify/issues/326#issuecomment-411360862).
|
| 31 |
+
```js
|
| 32 |
+
import Fastify from 'fastify'
|
| 33 |
+
import cors from '@fastify/cors'
|
| 34 |
+
|
| 35 |
+
const fastify = Fastify()
|
| 36 |
+
await fastify.register(cors, {
|
| 37 |
+
// put your options here
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
fastify.get('/', (req, reply) => {
|
| 41 |
+
reply.send({ hello: 'world' })
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
await fastify.listen({ port: 3000 })
|
| 45 |
+
```
|
| 46 |
+
You can use it as is without passing any option or you can configure it as explained below.
|
| 47 |
+
### Options
|
| 48 |
+
* `origin`: Configures the **Access-Control-Allow-Origin** CORS header. The value of origin can be:
|
| 49 |
+
- `Boolean`: Set to `true` to reflect the [request origin](http://tools.ietf.org/html/draft-abarth-origin-09), or `false` to disable CORS.
|
| 50 |
+
- `String`: Set to a specific origin (e.g., `"http://example.com"`). The special `*` value (default) allows any origin.
|
| 51 |
+
- `RegExp`: Set to a regular expression pattern to test the request origin. If it matches, the request origin is reflected (e.g., `/example\.com$/` returns the origin only if it ends with `example.com`).
|
| 52 |
+
- `Array`: Set to an array of valid origins, each being a `String` or `RegExp` (e.g., `["http://example1.com", /\.example2\.com$/]`).
|
| 53 |
+
- `Function`: Set to a function with custom logic. The function takes the request origin as the first parameter and a callback as the second (signature `err [Error | null], origin`). *Async-await* and promises are supported. The Fastify instance is bound to the function call and can be accessed via `this`. For example:
|
| 54 |
+
```js
|
| 55 |
+
origin: (origin, cb) => {
|
| 56 |
+
const hostname = new URL(origin).hostname
|
| 57 |
+
if(hostname === "localhost"){
|
| 58 |
+
// Request from localhost will pass
|
| 59 |
+
cb(null, true)
|
| 60 |
+
return
|
| 61 |
+
}
|
| 62 |
+
// Generate an error on other origins, disabling access
|
| 63 |
+
cb(new Error("Not allowed"), false)
|
| 64 |
+
}
|
| 65 |
+
```
|
| 66 |
+
* `methods`: Configures the **Access-Control-Allow-Methods** CORS header. Expects a comma-delimited string (e.g., 'GET,HEAD,POST') or an array (e.g., `['GET', 'HEAD', 'POST']`). Default: [CORS-safelisted methods](https://fetch.spec.whatwg.org/#methods) `GET,HEAD,POST`.
|
| 67 |
+
* `hook`: See [Custom Fastify hook name](#custom-fastify-hook-name). Default: `onRequest`.
|
| 68 |
+
* `allowedHeaders`: Configures the **Access-Control-Allow-Headers** CORS header. Expects a comma-delimited string (e.g., `'Content-Type,Authorization'`) or an array (e.g., `['Content-Type', 'Authorization']`). Defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header if not specified.
|
| 69 |
+
* `exposedHeaders`: Configures the **Access-Control-Expose-Headers** CORS header. Expects a comma-delimited string (e.g., `'Content-Range,X-Content-Range'`) or an array (e.g., `['Content-Range', 'X-Content-Range']`). No custom headers are exposed if not specified.
|
| 70 |
+
* `credentials`: Configures the **Access-Control-Allow-Credentials** CORS header. Set to `true` to pass the header; otherwise, it is omitted.
|
| 71 |
+
* `maxAge`: Configures the **Access-Control-Max-Age** CORS header in seconds. Set to an integer to pass the header; otherwise, it is omitted.
|
| 72 |
+
* `cacheControl`: Configures the **Cache-Control** header for CORS preflight responses. Set to an integer to pass the header as `Cache-Control: max-age=${cacheControl}`, or set to a string to pass the header as `Cache-Control: ${cacheControl}`. Otherwise, the header is omitted.
|
| 73 |
+
* `preflightContinue`: Passes the CORS preflight response to the route handler. Default: `false`.
|
| 74 |
+
* `optionsSuccessStatus`: Provides a status code for successful `OPTIONS` requests, as some legacy browsers (IE11, various SmartTVs) choke on `204`.
|
| 75 |
+
* `preflight`: Disables preflight by passing `false`. Default: `true`.
|
| 76 |
+
* `strictPreflight`: Enforces strict requirements for the CORS preflight request headers (**Access-Control-Request-Method** and **Origin**) as defined by the [W3C CORS specification](https://www.w3.org/TR/2020/SPSD-cors-20200602/#resource-preflight-requests). Preflight requests without the required headers result in 400 errors when set to `true`. Default: `true`.
|
| 77 |
+
* `hideOptionsRoute`: Hides the options route from documentation built using [@fastify/swagger](https://github.com/fastify/fastify-swagger). Default: `true`.
|
| 78 |
+
* `logLevel`: Sets the Fastify log level **only** for the internal CORS pre-flight `OPTIONS *` route.
|
| 79 |
+
Pass `'silent'` to suppress these requests in your logs, or any valid Fastify
|
| 80 |
+
log level (`'trace'`, `'debug'`, `'info'`, `'warn'`, `'error'`, `'fatal'`).
|
| 81 |
+
Default: inherits Fastify’s global log level.
|
| 82 |
+
|
| 83 |
+
#### :warning: DoS attacks
|
| 84 |
+
|
| 85 |
+
Using `RegExp` or a `function` for the `origin` parameter may enable Denial of Service attacks.
|
| 86 |
+
Craft with extreme care.
|
| 87 |
+
|
| 88 |
+
### Configuring CORS Asynchronously
|
| 89 |
+
|
| 90 |
+
```js
|
| 91 |
+
const fastify = require('fastify')()
|
| 92 |
+
|
| 93 |
+
fastify.register(require('@fastify/cors'), (instance) => {
|
| 94 |
+
return (req, callback) => {
|
| 95 |
+
const corsOptions = {
|
| 96 |
+
// This is NOT recommended for production as it enables reflection exploits
|
| 97 |
+
origin: true
|
| 98 |
+
};
|
| 99 |
+
|
| 100 |
+
// do not include CORS headers for requests from localhost
|
| 101 |
+
if (/^localhost$/m.test(req.headers.origin)) {
|
| 102 |
+
corsOptions.origin = false
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
// callback expects two parameters: error and options
|
| 106 |
+
callback(null, corsOptions)
|
| 107 |
+
}
|
| 108 |
+
})
|
| 109 |
+
|
| 110 |
+
fastify.register(async function (fastify) {
|
| 111 |
+
fastify.get('/', (req, reply) => {
|
| 112 |
+
reply.send({ hello: 'world' })
|
| 113 |
+
})
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
fastify.listen({ port: 3000 })
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
### Disabling CORS for a specific route
|
| 120 |
+
|
| 121 |
+
CORS can be disabled at the route level by setting the `cors` option to `false`.
|
| 122 |
+
|
| 123 |
+
```js
|
| 124 |
+
const fastify = require('fastify')()
|
| 125 |
+
|
| 126 |
+
fastify.register(require('@fastify/cors'), { origin: '*' })
|
| 127 |
+
|
| 128 |
+
fastify.get('/cors-enabled', (_req, reply) => {
|
| 129 |
+
reply.send('CORS headers')
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
+
fastify.get('/cors-disabled', { cors: false }, (_req, reply) => {
|
| 133 |
+
reply.send('No CORS headers')
|
| 134 |
+
})
|
| 135 |
+
|
| 136 |
+
fastify.listen({ port: 3000 })
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
### Custom Fastify hook name
|
| 140 |
+
|
| 141 |
+
By default, `@fastify/cors` adds an `onRequest` hook for validation and header injection. This can be customized by passing `hook` in the options. Valid values are `onRequest`, `preParsing`, `preValidation`, `preHandler`, `preSerialization`, and `onSend`.
|
| 142 |
+
|
| 143 |
+
```js
|
| 144 |
+
import Fastify from 'fastify'
|
| 145 |
+
import cors from '@fastify/cors'
|
| 146 |
+
|
| 147 |
+
const fastify = Fastify()
|
| 148 |
+
await fastify.register(cors, {
|
| 149 |
+
hook: 'preHandler',
|
| 150 |
+
})
|
| 151 |
+
|
| 152 |
+
fastify.get('/', (req, reply) => {
|
| 153 |
+
reply.send({ hello: 'world' })
|
| 154 |
+
})
|
| 155 |
+
|
| 156 |
+
await fastify.listen({ port: 3000 })
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
To configure CORS asynchronously, provide an object with the `delegator` key:
|
| 160 |
+
|
| 161 |
+
```js
|
| 162 |
+
const fastify = require('fastify')()
|
| 163 |
+
|
| 164 |
+
fastify.register(require('@fastify/cors'), {
|
| 165 |
+
hook: 'preHandler',
|
| 166 |
+
delegator: (req, callback) => {
|
| 167 |
+
const corsOptions = {
|
| 168 |
+
// This is NOT recommended for production as it enables reflection exploits
|
| 169 |
+
origin: true
|
| 170 |
+
};
|
| 171 |
+
|
| 172 |
+
// do not include CORS headers for requests from localhost
|
| 173 |
+
if (/^localhost$/m.test(req.headers.origin)) {
|
| 174 |
+
corsOptions.origin = false
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// callback expects two parameters: error and options
|
| 178 |
+
callback(null, corsOptions)
|
| 179 |
+
},
|
| 180 |
+
})
|
| 181 |
+
|
| 182 |
+
fastify.register(async function (fastify) {
|
| 183 |
+
fastify.get('/', (req, reply) => {
|
| 184 |
+
reply.send({ hello: 'world' })
|
| 185 |
+
})
|
| 186 |
+
})
|
| 187 |
+
|
| 188 |
+
fastify.listen({ port: 3000 })
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
## Acknowledgments
|
| 192 |
+
|
| 193 |
+
The code is a port for Fastify of [`expressjs/cors`](https://github.com/expressjs/cors).
|
| 194 |
+
|
| 195 |
+
## License
|
| 196 |
+
|
| 197 |
+
Licensed under [MIT](./LICENSE).<br/>
|
| 198 |
+
[`expressjs/cors` license](https://github.com/expressjs/cors/blob/master/LICENSE)
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/bench.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const fastify = require('fastify')()
|
| 4 |
+
|
| 5 |
+
fastify.register((instance, _opts, next) => {
|
| 6 |
+
instance.register(require('./index'))
|
| 7 |
+
instance.get('/fastify', (_req, reply) => reply.send('ok'))
|
| 8 |
+
next()
|
| 9 |
+
})
|
| 10 |
+
|
| 11 |
+
fastify.register((instance, _opts, next) => {
|
| 12 |
+
instance.use(require('cors')())
|
| 13 |
+
instance.get('/express', (_req, reply) => reply.send('ok'))
|
| 14 |
+
next()
|
| 15 |
+
})
|
| 16 |
+
|
| 17 |
+
fastify.listen({ port: 3000 })
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/benchmark/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "benchmark",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "",
|
| 5 |
+
"main": "vary.js",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"test": "echo \"Error: no test specified\" && exit 1"
|
| 8 |
+
},
|
| 9 |
+
"author": "",
|
| 10 |
+
"license": "ISC",
|
| 11 |
+
"dependencies": {
|
| 12 |
+
"benchmark": "^2.1.4",
|
| 13 |
+
"vary": "^1.1.2"
|
| 14 |
+
}
|
| 15 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/benchmark/vary.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const benchmark = require('benchmark')
|
| 4 |
+
const vary = require('vary')
|
| 5 |
+
const createAddFieldnameToVary = require('../vary').createAddFieldnameToVary
|
| 6 |
+
|
| 7 |
+
const replyMock = (header) => ({
|
| 8 |
+
getHeader () { return header },
|
| 9 |
+
setHeader () { },
|
| 10 |
+
header () { }
|
| 11 |
+
})
|
| 12 |
+
|
| 13 |
+
const addAcceptToVary = createAddFieldnameToVary('Accept')
|
| 14 |
+
const addWildcardToVary = createAddFieldnameToVary('*')
|
| 15 |
+
const addAcceptEncodingToVary = createAddFieldnameToVary('Accept-Encoding')
|
| 16 |
+
const addXFooToVary = createAddFieldnameToVary('X-Foo')
|
| 17 |
+
|
| 18 |
+
new benchmark.Suite()
|
| 19 |
+
.add('vary - field to undefined', function () { vary(replyMock(undefined), 'Accept-Encoding') }, { minSamples: 100 })
|
| 20 |
+
.add('vary - field to *', function () { vary(replyMock('*'), 'Accept-Encoding') }, { minSamples: 100 })
|
| 21 |
+
.add('vary - * to field', function () { vary(replyMock('Accept-Encoding'), '*') }, { minSamples: 100 })
|
| 22 |
+
.add('vary - field to empty', function () { vary(replyMock(''), 'Accept-Encoding') }, { minSamples: 100 })
|
| 23 |
+
.add('vary - fields string to empty', function () { vary(replyMock(''), 'Accept') }, { minSamples: 100 })
|
| 24 |
+
.add('vary - field to fields', function () { vary(replyMock('Accept, Accept-Encoding, Accept-Language'), 'X-Foo') }, { minSamples: 100 })
|
| 25 |
+
|
| 26 |
+
.add('cors - field to undefined', function () { addAcceptEncodingToVary(replyMock(undefined)) }, { minSamples: 100 })
|
| 27 |
+
.add('cors - field to *', function () { addAcceptEncodingToVary(replyMock('*')) }, { minSamples: 100 })
|
| 28 |
+
.add('cors - * to field', function () { addWildcardToVary(replyMock('Accept-Encoding')) }, { minSamples: 100 })
|
| 29 |
+
.add('cors - field to empty', function () { addAcceptEncodingToVary(replyMock('')) }, { minSamples: 100 })
|
| 30 |
+
.add('cors - fields string to empty', function () { addAcceptToVary(replyMock('')) }, { minSamples: 100 })
|
| 31 |
+
.add('cors - field to fields', function () { addXFooToVary(replyMock('Accept, Accept-Encoding, Accept-Language')) }, { minSamples: 100 })
|
| 32 |
+
|
| 33 |
+
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
|
| 34 |
+
.run({ async: false })
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/eslint.config.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
module.exports = require('neostandard')({
|
| 4 |
+
ignores: require('neostandard').resolveIgnoresFromGitignore(),
|
| 5 |
+
ts: true
|
| 6 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/index.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const fp = require('fastify-plugin')
|
| 4 |
+
const {
|
| 5 |
+
addAccessControlRequestHeadersToVaryHeader,
|
| 6 |
+
addOriginToVaryHeader
|
| 7 |
+
} = require('./vary')
|
| 8 |
+
|
| 9 |
+
const defaultOptions = {
|
| 10 |
+
origin: '*',
|
| 11 |
+
methods: 'GET,HEAD,POST',
|
| 12 |
+
hook: 'onRequest',
|
| 13 |
+
preflightContinue: false,
|
| 14 |
+
optionsSuccessStatus: 204,
|
| 15 |
+
credentials: false,
|
| 16 |
+
exposedHeaders: null,
|
| 17 |
+
allowedHeaders: null,
|
| 18 |
+
maxAge: null,
|
| 19 |
+
preflight: true,
|
| 20 |
+
strictPreflight: true
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
const validHooks = [
|
| 24 |
+
'onRequest',
|
| 25 |
+
'preParsing',
|
| 26 |
+
'preValidation',
|
| 27 |
+
'preHandler',
|
| 28 |
+
'preSerialization',
|
| 29 |
+
'onSend'
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
const hookWithPayload = [
|
| 33 |
+
'preSerialization',
|
| 34 |
+
'preParsing',
|
| 35 |
+
'onSend'
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
function validateHook (value, next) {
|
| 39 |
+
if (validHooks.indexOf(value) !== -1) {
|
| 40 |
+
return
|
| 41 |
+
}
|
| 42 |
+
next(new TypeError('@fastify/cors: Invalid hook option provided.'))
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
function fastifyCors (fastify, opts, next) {
|
| 46 |
+
fastify.decorateRequest('corsPreflightEnabled', false)
|
| 47 |
+
|
| 48 |
+
let hideOptionsRoute = true
|
| 49 |
+
let logLevel
|
| 50 |
+
|
| 51 |
+
if (typeof opts === 'function') {
|
| 52 |
+
handleCorsOptionsDelegator(opts, fastify, { hook: defaultOptions.hook }, next)
|
| 53 |
+
} else if (opts.delegator) {
|
| 54 |
+
const { delegator, ...options } = opts
|
| 55 |
+
handleCorsOptionsDelegator(delegator, fastify, options, next)
|
| 56 |
+
} else {
|
| 57 |
+
const corsOptions = normalizeCorsOptions(opts)
|
| 58 |
+
validateHook(corsOptions.hook, next)
|
| 59 |
+
if (hookWithPayload.indexOf(corsOptions.hook) !== -1) {
|
| 60 |
+
fastify.addHook(corsOptions.hook, function handleCors (req, reply, _payload, next) {
|
| 61 |
+
addCorsHeadersHandler(fastify, corsOptions, req, reply, next)
|
| 62 |
+
})
|
| 63 |
+
} else {
|
| 64 |
+
fastify.addHook(corsOptions.hook, function handleCors (req, reply, next) {
|
| 65 |
+
addCorsHeadersHandler(fastify, corsOptions, req, reply, next)
|
| 66 |
+
})
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
if (opts.logLevel !== undefined) logLevel = opts.logLevel
|
| 70 |
+
if (opts.hideOptionsRoute !== undefined) hideOptionsRoute = opts.hideOptionsRoute
|
| 71 |
+
|
| 72 |
+
// The preflight reply must occur in the hook. This allows fastify-cors to reply to
|
| 73 |
+
// preflight requests BEFORE possible authentication plugins. If the preflight reply
|
| 74 |
+
// occurred in this handler, other plugins may deny the request since the browser will
|
| 75 |
+
// remove most headers (such as the Authentication header).
|
| 76 |
+
//
|
| 77 |
+
// This route simply enables fastify to accept preflight requests.
|
| 78 |
+
|
| 79 |
+
fastify.options('*', { schema: { hide: hideOptionsRoute }, logLevel }, (req, reply) => {
|
| 80 |
+
if (!req.corsPreflightEnabled) {
|
| 81 |
+
// Do not handle preflight requests if the origin option disabled CORS
|
| 82 |
+
reply.callNotFound()
|
| 83 |
+
return
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
reply.send()
|
| 87 |
+
})
|
| 88 |
+
|
| 89 |
+
next()
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
function handleCorsOptionsDelegator (optionsResolver, fastify, opts, next) {
|
| 93 |
+
const hook = opts?.hook || defaultOptions.hook
|
| 94 |
+
validateHook(hook, next)
|
| 95 |
+
if (optionsResolver.length === 2) {
|
| 96 |
+
if (hookWithPayload.indexOf(hook) !== -1) {
|
| 97 |
+
fastify.addHook(hook, function handleCors (req, reply, _payload, next) {
|
| 98 |
+
handleCorsOptionsCallbackDelegator(optionsResolver, fastify, req, reply, next)
|
| 99 |
+
})
|
| 100 |
+
} else {
|
| 101 |
+
fastify.addHook(hook, function handleCors (req, reply, next) {
|
| 102 |
+
handleCorsOptionsCallbackDelegator(optionsResolver, fastify, req, reply, next)
|
| 103 |
+
})
|
| 104 |
+
}
|
| 105 |
+
} else {
|
| 106 |
+
if (hookWithPayload.indexOf(hook) !== -1) {
|
| 107 |
+
// handle delegator based on Promise
|
| 108 |
+
fastify.addHook(hook, function handleCors (req, reply, _payload, next) {
|
| 109 |
+
const ret = optionsResolver(req)
|
| 110 |
+
if (ret && typeof ret.then === 'function') {
|
| 111 |
+
ret.then(options => addCorsHeadersHandler(fastify, normalizeCorsOptions(options, true), req, reply, next)).catch(next)
|
| 112 |
+
return
|
| 113 |
+
}
|
| 114 |
+
next(new Error('Invalid CORS origin option'))
|
| 115 |
+
})
|
| 116 |
+
} else {
|
| 117 |
+
// handle delegator based on Promise
|
| 118 |
+
fastify.addHook(hook, function handleCors (req, reply, next) {
|
| 119 |
+
const ret = optionsResolver(req)
|
| 120 |
+
if (ret && typeof ret.then === 'function') {
|
| 121 |
+
ret.then(options => addCorsHeadersHandler(fastify, normalizeCorsOptions(options, true), req, reply, next)).catch(next)
|
| 122 |
+
return
|
| 123 |
+
}
|
| 124 |
+
next(new Error('Invalid CORS origin option'))
|
| 125 |
+
})
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
function handleCorsOptionsCallbackDelegator (optionsResolver, fastify, req, reply, next) {
|
| 131 |
+
optionsResolver(req, (err, options) => {
|
| 132 |
+
if (err) {
|
| 133 |
+
next(err)
|
| 134 |
+
} else {
|
| 135 |
+
addCorsHeadersHandler(fastify, normalizeCorsOptions(options, true), req, reply, next)
|
| 136 |
+
}
|
| 137 |
+
})
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
/**
|
| 141 |
+
* @param {import('./types').FastifyCorsOptions} opts
|
| 142 |
+
*/
|
| 143 |
+
function normalizeCorsOptions (opts, dynamic) {
|
| 144 |
+
const corsOptions = { ...defaultOptions, ...opts }
|
| 145 |
+
if (Array.isArray(opts.origin) && opts.origin.indexOf('*') !== -1) {
|
| 146 |
+
corsOptions.origin = '*'
|
| 147 |
+
}
|
| 148 |
+
if (Number.isInteger(corsOptions.cacheControl)) {
|
| 149 |
+
// integer numbers are formatted this way
|
| 150 |
+
corsOptions.cacheControl = `max-age=${corsOptions.cacheControl}`
|
| 151 |
+
} else if (typeof corsOptions.cacheControl !== 'string') {
|
| 152 |
+
// strings are applied directly and any other value is ignored
|
| 153 |
+
corsOptions.cacheControl = null
|
| 154 |
+
}
|
| 155 |
+
corsOptions.dynamic = dynamic || false
|
| 156 |
+
return corsOptions
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
function addCorsHeadersHandler (fastify, options, req, reply, next) {
|
| 160 |
+
if ((typeof options.origin !== 'string' && options.origin !== false) || options.dynamic) {
|
| 161 |
+
// Always set Vary header for non-static origin option
|
| 162 |
+
// https://fetch.spec.whatwg.org/#cors-protocol-and-http-caches
|
| 163 |
+
addOriginToVaryHeader(reply)
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
const resolveOriginOption = typeof options.origin === 'function' ? resolveOriginWrapper(fastify, options.origin) : (_, cb) => cb(null, options.origin)
|
| 167 |
+
|
| 168 |
+
resolveOriginOption(req, (error, resolvedOriginOption) => {
|
| 169 |
+
if (error !== null) {
|
| 170 |
+
return next(error)
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Disable CORS and preflight if false
|
| 174 |
+
if (resolvedOriginOption === false) {
|
| 175 |
+
return next()
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// Allow routes to disable CORS individually
|
| 179 |
+
if (req.routeOptions.config?.cors === false) {
|
| 180 |
+
return next()
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// Falsy values are invalid
|
| 184 |
+
if (!resolvedOriginOption) {
|
| 185 |
+
return next(new Error('Invalid CORS origin option'))
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
addCorsHeaders(req, reply, resolvedOriginOption, options)
|
| 189 |
+
|
| 190 |
+
if (req.raw.method === 'OPTIONS' && options.preflight === true) {
|
| 191 |
+
// Strict mode enforces the required headers for preflight
|
| 192 |
+
if (options.strictPreflight === true && (!req.headers.origin || !req.headers['access-control-request-method'])) {
|
| 193 |
+
reply.status(400).type('text/plain').send('Invalid Preflight Request')
|
| 194 |
+
return
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
req.corsPreflightEnabled = true
|
| 198 |
+
|
| 199 |
+
addPreflightHeaders(req, reply, options)
|
| 200 |
+
|
| 201 |
+
if (!options.preflightContinue) {
|
| 202 |
+
// Do not call the hook callback and terminate the request
|
| 203 |
+
// Safari (and potentially other browsers) need content-length 0,
|
| 204 |
+
// for 204 or they just hang waiting for a body
|
| 205 |
+
reply
|
| 206 |
+
.code(options.optionsSuccessStatus)
|
| 207 |
+
.header('Content-Length', '0')
|
| 208 |
+
.send()
|
| 209 |
+
return
|
| 210 |
+
}
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
return next()
|
| 214 |
+
})
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
function addCorsHeaders (req, reply, originOption, corsOptions) {
|
| 218 |
+
const origin = getAccessControlAllowOriginHeader(req.headers.origin, originOption)
|
| 219 |
+
// In the case of origin not allowed the header is not
|
| 220 |
+
// written in the response.
|
| 221 |
+
// https://github.com/fastify/fastify-cors/issues/127
|
| 222 |
+
if (origin) {
|
| 223 |
+
reply.header('Access-Control-Allow-Origin', origin)
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
if (corsOptions.credentials) {
|
| 227 |
+
reply.header('Access-Control-Allow-Credentials', 'true')
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
if (corsOptions.exposedHeaders !== null) {
|
| 231 |
+
reply.header(
|
| 232 |
+
'Access-Control-Expose-Headers',
|
| 233 |
+
Array.isArray(corsOptions.exposedHeaders) ? corsOptions.exposedHeaders.join(', ') : corsOptions.exposedHeaders
|
| 234 |
+
)
|
| 235 |
+
}
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
function addPreflightHeaders (req, reply, corsOptions) {
|
| 239 |
+
reply.header(
|
| 240 |
+
'Access-Control-Allow-Methods',
|
| 241 |
+
Array.isArray(corsOptions.methods) ? corsOptions.methods.join(', ') : corsOptions.methods
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
if (corsOptions.allowedHeaders === null) {
|
| 245 |
+
addAccessControlRequestHeadersToVaryHeader(reply)
|
| 246 |
+
const reqAllowedHeaders = req.headers['access-control-request-headers']
|
| 247 |
+
if (reqAllowedHeaders !== undefined) {
|
| 248 |
+
reply.header('Access-Control-Allow-Headers', reqAllowedHeaders)
|
| 249 |
+
}
|
| 250 |
+
} else {
|
| 251 |
+
reply.header(
|
| 252 |
+
'Access-Control-Allow-Headers',
|
| 253 |
+
Array.isArray(corsOptions.allowedHeaders) ? corsOptions.allowedHeaders.join(', ') : corsOptions.allowedHeaders
|
| 254 |
+
)
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
if (corsOptions.maxAge !== null) {
|
| 258 |
+
reply.header('Access-Control-Max-Age', String(corsOptions.maxAge))
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
if (corsOptions.cacheControl) {
|
| 262 |
+
reply.header('Cache-Control', corsOptions.cacheControl)
|
| 263 |
+
}
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
function resolveOriginWrapper (fastify, origin) {
|
| 267 |
+
return function (req, cb) {
|
| 268 |
+
const result = origin.call(fastify, req.headers.origin, cb)
|
| 269 |
+
|
| 270 |
+
// Allow for promises
|
| 271 |
+
if (result && typeof result.then === 'function') {
|
| 272 |
+
result.then(res => cb(null, res), cb)
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
function getAccessControlAllowOriginHeader (reqOrigin, originOption) {
|
| 278 |
+
if (typeof originOption === 'string') {
|
| 279 |
+
// fixed or any origin ('*')
|
| 280 |
+
return originOption
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
// reflect origin
|
| 284 |
+
return isRequestOriginAllowed(reqOrigin, originOption) ? reqOrigin : false
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
function isRequestOriginAllowed (reqOrigin, allowedOrigin) {
|
| 288 |
+
if (Array.isArray(allowedOrigin)) {
|
| 289 |
+
for (let i = 0; i < allowedOrigin.length; ++i) {
|
| 290 |
+
if (isRequestOriginAllowed(reqOrigin, allowedOrigin[i])) {
|
| 291 |
+
return true
|
| 292 |
+
}
|
| 293 |
+
}
|
| 294 |
+
return false
|
| 295 |
+
} else if (typeof allowedOrigin === 'string') {
|
| 296 |
+
return reqOrigin === allowedOrigin
|
| 297 |
+
} else if (allowedOrigin instanceof RegExp) {
|
| 298 |
+
allowedOrigin.lastIndex = 0
|
| 299 |
+
return allowedOrigin.test(reqOrigin)
|
| 300 |
+
} else {
|
| 301 |
+
return !!allowedOrigin
|
| 302 |
+
}
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
const _fastifyCors = fp(fastifyCors, {
|
| 306 |
+
fastify: '5.x',
|
| 307 |
+
name: '@fastify/cors'
|
| 308 |
+
})
|
| 309 |
+
|
| 310 |
+
/**
|
| 311 |
+
* These export configurations enable JS and TS developers
|
| 312 |
+
* to consumer fastify in whatever way best suits their needs.
|
| 313 |
+
*/
|
| 314 |
+
module.exports = _fastifyCors
|
| 315 |
+
module.exports.fastifyCors = _fastifyCors
|
| 316 |
+
module.exports.default = _fastifyCors
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@fastify/cors",
|
| 3 |
+
"version": "11.1.0",
|
| 4 |
+
"description": "Fastify CORS",
|
| 5 |
+
"main": "index.js",
|
| 6 |
+
"type": "commonjs",
|
| 7 |
+
"types": "types/index.d.ts",
|
| 8 |
+
"scripts": {
|
| 9 |
+
"lint": "eslint",
|
| 10 |
+
"lint:fix": "eslint --fix",
|
| 11 |
+
"test": "npm run test:unit && npm run test:typescript",
|
| 12 |
+
"test:typescript": "tsd",
|
| 13 |
+
"test:unit": "c8 --100 node --test"
|
| 14 |
+
},
|
| 15 |
+
"keywords": [
|
| 16 |
+
"fastify",
|
| 17 |
+
"cors",
|
| 18 |
+
"headers",
|
| 19 |
+
"access",
|
| 20 |
+
"control"
|
| 21 |
+
],
|
| 22 |
+
"author": "Tomas Della Vedova - @delvedor (http://delved.org)",
|
| 23 |
+
"contributors": [
|
| 24 |
+
{
|
| 25 |
+
"name": "Matteo Collina",
|
| 26 |
+
"email": "hello@matteocollina.com"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"name": "Manuel Spigolon",
|
| 30 |
+
"email": "behemoth89@gmail.com"
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"name": "Cemre Mengu",
|
| 34 |
+
"email": "cemremengu@gmail.com"
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"name": "Frazer Smith",
|
| 38 |
+
"email": "frazer.dev@icloud.com",
|
| 39 |
+
"url": "https://github.com/fdawgs"
|
| 40 |
+
}
|
| 41 |
+
],
|
| 42 |
+
"license": "MIT",
|
| 43 |
+
"repository": {
|
| 44 |
+
"type": "git",
|
| 45 |
+
"url": "git+https://github.com/fastify/fastify-cors.git"
|
| 46 |
+
},
|
| 47 |
+
"bugs": {
|
| 48 |
+
"url": "https://github.com/fastify/fastify-cors/issues"
|
| 49 |
+
},
|
| 50 |
+
"homepage": "https://github.com/fastify/fastify-cors#readme",
|
| 51 |
+
"funding": [
|
| 52 |
+
{
|
| 53 |
+
"type": "github",
|
| 54 |
+
"url": "https://github.com/sponsors/fastify"
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"type": "opencollective",
|
| 58 |
+
"url": "https://opencollective.com/fastify"
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
"devDependencies": {
|
| 62 |
+
"@fastify/pre-commit": "^2.1.0",
|
| 63 |
+
"@types/node": "^24.0.8",
|
| 64 |
+
"c8": "^10.1.2",
|
| 65 |
+
"cors": "^2.8.5",
|
| 66 |
+
"eslint": "^9.17.0",
|
| 67 |
+
"fastify": "^5.0.0",
|
| 68 |
+
"neostandard": "^0.12.0",
|
| 69 |
+
"tsd": "^0.32.0",
|
| 70 |
+
"typescript": "~5.8.2"
|
| 71 |
+
},
|
| 72 |
+
"dependencies": {
|
| 73 |
+
"fastify-plugin": "^5.0.0",
|
| 74 |
+
"toad-cache": "^3.7.0"
|
| 75 |
+
},
|
| 76 |
+
"tsd": {
|
| 77 |
+
"directory": "test"
|
| 78 |
+
},
|
| 79 |
+
"publishConfig": {
|
| 80 |
+
"access": "public"
|
| 81 |
+
},
|
| 82 |
+
"pre-commit": [
|
| 83 |
+
"lint",
|
| 84 |
+
"test"
|
| 85 |
+
]
|
| 86 |
+
}
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/cors.test.js
ADDED
|
@@ -0,0 +1,1052 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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readFileSync } = require('node:fs')
|
| 5 |
+
const Fastify = require('fastify')
|
| 6 |
+
const cors = require('../')
|
| 7 |
+
const { resolve } = require('node:path')
|
| 8 |
+
const { setTimeout: sleep } = require('node:timers/promises')
|
| 9 |
+
|
| 10 |
+
test('Should add cors headers', async t => {
|
| 11 |
+
t.plan(4)
|
| 12 |
+
|
| 13 |
+
const fastify = Fastify()
|
| 14 |
+
fastify.register(cors)
|
| 15 |
+
|
| 16 |
+
fastify.get('/', (_req, reply) => {
|
| 17 |
+
reply.send('ok')
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
const res = await fastify.inject({
|
| 21 |
+
method: 'GET',
|
| 22 |
+
url: '/'
|
| 23 |
+
})
|
| 24 |
+
|
| 25 |
+
t.assert.ok(res)
|
| 26 |
+
delete res.headers.date
|
| 27 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 28 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 29 |
+
t.assert.deepStrictEqual(res.headers['access-control-allow-origin'],
|
| 30 |
+
'*'
|
| 31 |
+
)
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
test('Should add cors headers when payload is a stream', async t => {
|
| 35 |
+
t.plan(4)
|
| 36 |
+
|
| 37 |
+
const fastify = Fastify()
|
| 38 |
+
fastify.register(cors)
|
| 39 |
+
const filePath = resolve(__dirname, __filename)
|
| 40 |
+
|
| 41 |
+
fastify.get('/', (_req, reply) => {
|
| 42 |
+
const stream = createReadStream(filePath)
|
| 43 |
+
reply
|
| 44 |
+
.type('application/json')
|
| 45 |
+
.header('Content-Length', statSync(filePath).size)
|
| 46 |
+
.send(stream)
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
const fileContent = readFileSync(filePath, 'utf-8')
|
| 50 |
+
|
| 51 |
+
const res = await fastify.inject({
|
| 52 |
+
method: 'GET',
|
| 53 |
+
url: '/'
|
| 54 |
+
})
|
| 55 |
+
t.assert.ok(res)
|
| 56 |
+
delete res.headers.date
|
| 57 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 58 |
+
t.assert.strictEqual(res.payload, fileContent)
|
| 59 |
+
const actualHeaders = {
|
| 60 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 61 |
+
'content-length': res.headers['content-length']
|
| 62 |
+
|
| 63 |
+
}
|
| 64 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 65 |
+
'access-control-allow-origin': '*',
|
| 66 |
+
'content-length': statSync(filePath).size.toString()
|
| 67 |
+
})
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
test('Should add cors headers (custom values)', async t => {
|
| 71 |
+
t.plan(10)
|
| 72 |
+
|
| 73 |
+
const fastify = Fastify()
|
| 74 |
+
fastify.register(cors, {
|
| 75 |
+
origin: 'example.com',
|
| 76 |
+
methods: 'GET',
|
| 77 |
+
credentials: true,
|
| 78 |
+
exposedHeaders: ['foo', 'bar'],
|
| 79 |
+
allowedHeaders: ['baz', 'woo'],
|
| 80 |
+
maxAge: 123,
|
| 81 |
+
cacheControl: 321
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
fastify.get('/', (_req, reply) => {
|
| 85 |
+
reply.send('ok')
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
let res = await fastify.inject({
|
| 89 |
+
method: 'OPTIONS',
|
| 90 |
+
url: '/',
|
| 91 |
+
headers: {
|
| 92 |
+
'access-control-request-method': 'GET',
|
| 93 |
+
origin: 'example.com'
|
| 94 |
+
}
|
| 95 |
+
})
|
| 96 |
+
t.assert.ok(res)
|
| 97 |
+
delete res.headers.date
|
| 98 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 99 |
+
t.assert.strictEqual(res.payload, '')
|
| 100 |
+
const actualHeaders = {
|
| 101 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 102 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 103 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 104 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 105 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 106 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 107 |
+
'cache-control': res.headers['cache-control'],
|
| 108 |
+
'content-length': res.headers['content-length']
|
| 109 |
+
}
|
| 110 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 111 |
+
'access-control-allow-origin': 'example.com',
|
| 112 |
+
'access-control-allow-credentials': 'true',
|
| 113 |
+
'access-control-expose-headers': 'foo, bar',
|
| 114 |
+
'access-control-allow-methods': 'GET',
|
| 115 |
+
'access-control-allow-headers': 'baz, woo',
|
| 116 |
+
'access-control-max-age': '123',
|
| 117 |
+
'cache-control': 'max-age=321',
|
| 118 |
+
'content-length': '0'
|
| 119 |
+
})
|
| 120 |
+
t.assert.notDeepEqual(res.headers, { vary: 'Origin' })
|
| 121 |
+
|
| 122 |
+
res = await fastify.inject({
|
| 123 |
+
method: 'GET',
|
| 124 |
+
url: '/'
|
| 125 |
+
})
|
| 126 |
+
t.assert.ok(res)
|
| 127 |
+
delete res.headers.date
|
| 128 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 129 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 130 |
+
const actualHeaders2 = {
|
| 131 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 132 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 133 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 134 |
+
'content-length': res.headers['content-length']
|
| 135 |
+
}
|
| 136 |
+
t.assert.deepStrictEqual(actualHeaders2, {
|
| 137 |
+
'access-control-allow-origin': 'example.com',
|
| 138 |
+
'access-control-allow-credentials': 'true',
|
| 139 |
+
'access-control-expose-headers': 'foo, bar',
|
| 140 |
+
'content-length': '2'
|
| 141 |
+
})
|
| 142 |
+
t.assert.notDeepEqual(res.headers, { vary: 'Origin' })
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
test('Should support dynamic config (callback)', async t => {
|
| 146 |
+
t.plan(16)
|
| 147 |
+
|
| 148 |
+
const configs = [{
|
| 149 |
+
origin: 'example.com',
|
| 150 |
+
methods: 'GET',
|
| 151 |
+
credentials: true,
|
| 152 |
+
exposedHeaders: ['foo', 'bar'],
|
| 153 |
+
allowedHeaders: ['baz', 'woo'],
|
| 154 |
+
maxAge: 123,
|
| 155 |
+
cacheControl: 456
|
| 156 |
+
}, {
|
| 157 |
+
origin: 'sample.com',
|
| 158 |
+
methods: 'GET',
|
| 159 |
+
credentials: true,
|
| 160 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 161 |
+
allowedHeaders: ['baz', 'foo'],
|
| 162 |
+
maxAge: 321,
|
| 163 |
+
cacheControl: '456'
|
| 164 |
+
}]
|
| 165 |
+
|
| 166 |
+
const fastify = Fastify()
|
| 167 |
+
let requestId = 0
|
| 168 |
+
const configDelegation = async function (req, cb) {
|
| 169 |
+
// request should have id
|
| 170 |
+
t.assert.ok(req.id)
|
| 171 |
+
// request should not have send
|
| 172 |
+
t.assert.ifError(req.send)
|
| 173 |
+
|
| 174 |
+
const config = configs[requestId]
|
| 175 |
+
requestId++
|
| 176 |
+
if (config) {
|
| 177 |
+
cb(null, config)
|
| 178 |
+
} else {
|
| 179 |
+
cb(new Error('ouch'))
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
await fastify.register(cors, () => configDelegation)
|
| 183 |
+
|
| 184 |
+
fastify.get('/', (_req, reply) => {
|
| 185 |
+
reply.send('ok')
|
| 186 |
+
})
|
| 187 |
+
|
| 188 |
+
let res = await fastify.inject({
|
| 189 |
+
method: 'GET',
|
| 190 |
+
url: '/'
|
| 191 |
+
})
|
| 192 |
+
t.assert.ok(res)
|
| 193 |
+
delete res.headers.date
|
| 194 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 195 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 196 |
+
const actualHeaders = {
|
| 197 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 198 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 199 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 200 |
+
'content-length': res.headers['content-length'],
|
| 201 |
+
vary: res.headers.vary
|
| 202 |
+
}
|
| 203 |
+
// Sleep to wait for callback
|
| 204 |
+
sleep()
|
| 205 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 206 |
+
'access-control-allow-origin': 'example.com',
|
| 207 |
+
'access-control-allow-credentials': 'true',
|
| 208 |
+
'access-control-expose-headers': 'foo, bar',
|
| 209 |
+
'content-length': '2',
|
| 210 |
+
vary: 'Origin'
|
| 211 |
+
})
|
| 212 |
+
|
| 213 |
+
res = await fastify.inject({
|
| 214 |
+
method: 'OPTIONS',
|
| 215 |
+
url: '/',
|
| 216 |
+
headers: {
|
| 217 |
+
'access-control-request-method': 'GET',
|
| 218 |
+
origin: 'example.com'
|
| 219 |
+
}
|
| 220 |
+
})
|
| 221 |
+
t.assert.ok(res)
|
| 222 |
+
delete res.headers.date
|
| 223 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 224 |
+
t.assert.strictEqual(res.payload, '')
|
| 225 |
+
const actualHeaders2 = {
|
| 226 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 227 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 228 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 229 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 230 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 231 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 232 |
+
'cache-control': res.headers['cache-control'],
|
| 233 |
+
'content-length': res.headers['content-length'],
|
| 234 |
+
vary: res.headers.vary
|
| 235 |
+
}
|
| 236 |
+
// Sleep to wait for callback
|
| 237 |
+
sleep()
|
| 238 |
+
t.assert.deepStrictEqual(actualHeaders2, {
|
| 239 |
+
'access-control-allow-origin': 'sample.com',
|
| 240 |
+
'access-control-allow-credentials': 'true',
|
| 241 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 242 |
+
'access-control-allow-methods': 'GET',
|
| 243 |
+
'access-control-allow-headers': 'baz, foo',
|
| 244 |
+
'access-control-max-age': '321',
|
| 245 |
+
'cache-control': '456',
|
| 246 |
+
'content-length': '0',
|
| 247 |
+
vary: 'Origin'
|
| 248 |
+
})
|
| 249 |
+
|
| 250 |
+
res = await fastify.inject({
|
| 251 |
+
method: 'GET',
|
| 252 |
+
url: '/',
|
| 253 |
+
headers: {
|
| 254 |
+
'access-control-request-method': 'GET',
|
| 255 |
+
origin: 'example.com'
|
| 256 |
+
}
|
| 257 |
+
})
|
| 258 |
+
t.assert.ok(res)
|
| 259 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 260 |
+
})
|
| 261 |
+
|
| 262 |
+
test('Should support dynamic config (Promise)', async t => {
|
| 263 |
+
t.plan(23)
|
| 264 |
+
|
| 265 |
+
const configs = [{
|
| 266 |
+
origin: 'example.com',
|
| 267 |
+
methods: 'GET',
|
| 268 |
+
credentials: true,
|
| 269 |
+
exposedHeaders: ['foo', 'bar'],
|
| 270 |
+
allowedHeaders: ['baz', 'woo'],
|
| 271 |
+
maxAge: 123,
|
| 272 |
+
cacheControl: 456
|
| 273 |
+
}, {
|
| 274 |
+
origin: 'sample.com',
|
| 275 |
+
methods: 'GET',
|
| 276 |
+
credentials: true,
|
| 277 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 278 |
+
allowedHeaders: ['baz', 'foo'],
|
| 279 |
+
maxAge: 321,
|
| 280 |
+
cacheControl: true // Invalid value should be ignored
|
| 281 |
+
}, {
|
| 282 |
+
origin: 'sample.com',
|
| 283 |
+
methods: 'GET',
|
| 284 |
+
credentials: true,
|
| 285 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 286 |
+
allowedHeaders: ['baz', 'foo'],
|
| 287 |
+
maxAge: 321,
|
| 288 |
+
cacheControl: 'public, max-age=456'
|
| 289 |
+
}]
|
| 290 |
+
|
| 291 |
+
const fastify = Fastify()
|
| 292 |
+
let requestId = 0
|
| 293 |
+
const configDelegation = async function (req) {
|
| 294 |
+
// request should have id
|
| 295 |
+
t.assert.ok(req.id)
|
| 296 |
+
// request should not have send
|
| 297 |
+
t.assert.ifError(req.send)
|
| 298 |
+
const config = configs[requestId]
|
| 299 |
+
requestId++
|
| 300 |
+
if (config) {
|
| 301 |
+
return Promise.resolve(config)
|
| 302 |
+
} else {
|
| 303 |
+
return Promise.reject(new Error('ouch'))
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
await fastify.register(cors, () => configDelegation)
|
| 307 |
+
|
| 308 |
+
fastify.get('/', (_req, reply) => {
|
| 309 |
+
reply.send('ok')
|
| 310 |
+
})
|
| 311 |
+
|
| 312 |
+
let res = await fastify.inject({
|
| 313 |
+
method: 'GET',
|
| 314 |
+
url: '/'
|
| 315 |
+
})
|
| 316 |
+
t.assert.ok(res)
|
| 317 |
+
delete res.headers.date
|
| 318 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 319 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 320 |
+
const actualHeaders = {
|
| 321 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 322 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 323 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 324 |
+
'content-length': res.headers['content-length'],
|
| 325 |
+
vary: res.headers.vary
|
| 326 |
+
}
|
| 327 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 328 |
+
'access-control-allow-origin': 'example.com',
|
| 329 |
+
'access-control-allow-credentials': 'true',
|
| 330 |
+
'access-control-expose-headers': 'foo, bar',
|
| 331 |
+
'content-length': '2',
|
| 332 |
+
vary: 'Origin'
|
| 333 |
+
})
|
| 334 |
+
|
| 335 |
+
res = await fastify.inject({
|
| 336 |
+
method: 'OPTIONS',
|
| 337 |
+
url: '/',
|
| 338 |
+
headers: {
|
| 339 |
+
'access-control-request-method': 'GET',
|
| 340 |
+
origin: 'sample.com'
|
| 341 |
+
}
|
| 342 |
+
})
|
| 343 |
+
t.assert.ok(res)
|
| 344 |
+
delete res.headers.date
|
| 345 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 346 |
+
t.assert.strictEqual(res.payload, '')
|
| 347 |
+
const acutalHeaders2 = {
|
| 348 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 349 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 350 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 351 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 352 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 353 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 354 |
+
'content-length': res.headers['content-length'],
|
| 355 |
+
vary: res.headers.vary
|
| 356 |
+
}
|
| 357 |
+
t.assert.deepStrictEqual(acutalHeaders2, {
|
| 358 |
+
'access-control-allow-origin': 'sample.com',
|
| 359 |
+
'access-control-allow-credentials': 'true',
|
| 360 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 361 |
+
'access-control-allow-methods': 'GET',
|
| 362 |
+
'access-control-allow-headers': 'baz, foo',
|
| 363 |
+
'access-control-max-age': '321',
|
| 364 |
+
'content-length': '0',
|
| 365 |
+
vary: 'Origin'
|
| 366 |
+
})
|
| 367 |
+
t.assert.strictEqual(res.headers['cache-control'], undefined, 'cache-control omitted (invalid value)')
|
| 368 |
+
|
| 369 |
+
res = await fastify.inject({
|
| 370 |
+
method: 'OPTIONS',
|
| 371 |
+
url: '/',
|
| 372 |
+
headers: {
|
| 373 |
+
'access-control-request-method': 'GET',
|
| 374 |
+
origin: 'example.com'
|
| 375 |
+
}
|
| 376 |
+
})
|
| 377 |
+
t.assert.ok(res)
|
| 378 |
+
delete res.headers.date
|
| 379 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 380 |
+
t.assert.strictEqual(res.payload, '')
|
| 381 |
+
const actualHeaders3 = {
|
| 382 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 383 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 384 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 385 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 386 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 387 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 388 |
+
'cache-control': res.headers['cache-control'],
|
| 389 |
+
'content-length': res.headers['content-length'],
|
| 390 |
+
vary: res.headers.vary
|
| 391 |
+
}
|
| 392 |
+
t.assert.deepStrictEqual(actualHeaders3, {
|
| 393 |
+
'access-control-allow-origin': 'sample.com',
|
| 394 |
+
'access-control-allow-credentials': 'true',
|
| 395 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 396 |
+
'access-control-allow-methods': 'GET',
|
| 397 |
+
'access-control-allow-headers': 'baz, foo',
|
| 398 |
+
'access-control-max-age': '321',
|
| 399 |
+
'cache-control': 'public, max-age=456', // cache-control included (custom string)
|
| 400 |
+
'content-length': '0',
|
| 401 |
+
vary: 'Origin'
|
| 402 |
+
})
|
| 403 |
+
|
| 404 |
+
res = await fastify.inject({
|
| 405 |
+
method: 'GET',
|
| 406 |
+
url: '/',
|
| 407 |
+
headers: {
|
| 408 |
+
'access-control-request-method': 'GET',
|
| 409 |
+
origin: 'example.com'
|
| 410 |
+
}
|
| 411 |
+
})
|
| 412 |
+
t.assert.ok(res)
|
| 413 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 414 |
+
})
|
| 415 |
+
|
| 416 |
+
test('Should support dynamic config. (Invalid function)', async t => {
|
| 417 |
+
t.plan(2)
|
| 418 |
+
|
| 419 |
+
const fastify = Fastify()
|
| 420 |
+
fastify.register(cors, () => () => {})
|
| 421 |
+
|
| 422 |
+
fastify.get('/', (_req, reply) => {
|
| 423 |
+
reply.send('ok')
|
| 424 |
+
})
|
| 425 |
+
|
| 426 |
+
const res = await fastify.inject({
|
| 427 |
+
method: 'GET',
|
| 428 |
+
url: '/',
|
| 429 |
+
headers: {
|
| 430 |
+
'access-control-request-method': 'GET',
|
| 431 |
+
origin: 'example.com'
|
| 432 |
+
}
|
| 433 |
+
})
|
| 434 |
+
t.assert.ok(res)
|
| 435 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 436 |
+
})
|
| 437 |
+
|
| 438 |
+
test('Dynamic origin resolution (valid origin)', async t => {
|
| 439 |
+
t.plan(6)
|
| 440 |
+
|
| 441 |
+
const fastify = Fastify()
|
| 442 |
+
const origin = function (header, cb) {
|
| 443 |
+
t.assert.strictEqual(header, 'example.com')
|
| 444 |
+
t.assert.equal(this, fastify)
|
| 445 |
+
cb(null, true)
|
| 446 |
+
}
|
| 447 |
+
fastify.register(cors, { origin })
|
| 448 |
+
|
| 449 |
+
fastify.get('/', (_req, reply) => {
|
| 450 |
+
reply.send('ok')
|
| 451 |
+
})
|
| 452 |
+
|
| 453 |
+
const res = await fastify.inject({
|
| 454 |
+
method: 'GET',
|
| 455 |
+
url: '/',
|
| 456 |
+
headers: { origin: 'example.com' }
|
| 457 |
+
})
|
| 458 |
+
t.assert.ok(res)
|
| 459 |
+
delete res.headers.date
|
| 460 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 461 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 462 |
+
const actualHeaders = {
|
| 463 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 464 |
+
vary: res.headers.vary
|
| 465 |
+
}
|
| 466 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 467 |
+
'access-control-allow-origin': 'example.com',
|
| 468 |
+
vary: 'Origin'
|
| 469 |
+
})
|
| 470 |
+
})
|
| 471 |
+
|
| 472 |
+
test('Dynamic origin resolution (not valid origin)', async t => {
|
| 473 |
+
t.plan(5)
|
| 474 |
+
|
| 475 |
+
const fastify = Fastify()
|
| 476 |
+
const origin = (header, cb) => {
|
| 477 |
+
t.assert.strictEqual(header, 'example.com')
|
| 478 |
+
cb(null, false)
|
| 479 |
+
}
|
| 480 |
+
fastify.register(cors, { origin })
|
| 481 |
+
|
| 482 |
+
fastify.get('/', (_req, reply) => {
|
| 483 |
+
reply.send('ok')
|
| 484 |
+
})
|
| 485 |
+
|
| 486 |
+
const res = await fastify.inject({
|
| 487 |
+
method: 'GET',
|
| 488 |
+
url: '/',
|
| 489 |
+
headers: { origin: 'example.com' }
|
| 490 |
+
})
|
| 491 |
+
t.assert.ok(res)
|
| 492 |
+
delete res.headers.date
|
| 493 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 494 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 495 |
+
const actualHeaders = {
|
| 496 |
+
'content-length': res.headers['content-length'],
|
| 497 |
+
'content-type': res.headers['content-type'],
|
| 498 |
+
connection: res.headers.connection,
|
| 499 |
+
vary: res.headers.vary
|
| 500 |
+
}
|
| 501 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 502 |
+
'content-length': '2',
|
| 503 |
+
'content-type': 'text/plain; charset=utf-8',
|
| 504 |
+
connection: 'keep-alive',
|
| 505 |
+
vary: 'Origin'
|
| 506 |
+
})
|
| 507 |
+
})
|
| 508 |
+
|
| 509 |
+
test('Dynamic origin resolution (errored)', async t => {
|
| 510 |
+
t.plan(3)
|
| 511 |
+
|
| 512 |
+
const fastify = Fastify()
|
| 513 |
+
const origin = (header, cb) => {
|
| 514 |
+
t.assert.strictEqual(header, 'example.com')
|
| 515 |
+
cb(new Error('ouch'))
|
| 516 |
+
}
|
| 517 |
+
fastify.register(cors, { origin })
|
| 518 |
+
|
| 519 |
+
const res = await fastify.inject({
|
| 520 |
+
method: 'GET',
|
| 521 |
+
url: '/',
|
| 522 |
+
headers: { origin: 'example.com' }
|
| 523 |
+
})
|
| 524 |
+
t.assert.ok(res)
|
| 525 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 526 |
+
})
|
| 527 |
+
|
| 528 |
+
test('Dynamic origin resolution (invalid result)', async t => {
|
| 529 |
+
t.plan(3)
|
| 530 |
+
|
| 531 |
+
const fastify = Fastify()
|
| 532 |
+
const origin = (header, cb) => {
|
| 533 |
+
t.assert.strictEqual(header, 'example.com')
|
| 534 |
+
cb(null, undefined)
|
| 535 |
+
}
|
| 536 |
+
fastify.register(cors, { origin })
|
| 537 |
+
|
| 538 |
+
const res = await fastify.inject({
|
| 539 |
+
method: 'GET',
|
| 540 |
+
url: '/',
|
| 541 |
+
headers: { origin: 'example.com' }
|
| 542 |
+
})
|
| 543 |
+
t.assert.ok(res)
|
| 544 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 545 |
+
})
|
| 546 |
+
|
| 547 |
+
test('Dynamic origin resolution (valid origin - promises)', async t => {
|
| 548 |
+
t.plan(5)
|
| 549 |
+
|
| 550 |
+
const fastify = Fastify()
|
| 551 |
+
const origin = (header) => {
|
| 552 |
+
return new Promise((resolve) => {
|
| 553 |
+
t.assert.strictEqual(header, 'example.com')
|
| 554 |
+
resolve(true)
|
| 555 |
+
})
|
| 556 |
+
}
|
| 557 |
+
fastify.register(cors, { origin })
|
| 558 |
+
|
| 559 |
+
fastify.get('/', (_req, reply) => {
|
| 560 |
+
reply.send('ok')
|
| 561 |
+
})
|
| 562 |
+
|
| 563 |
+
const res = await fastify.inject({
|
| 564 |
+
method: 'GET',
|
| 565 |
+
url: '/',
|
| 566 |
+
headers: { origin: 'example.com' }
|
| 567 |
+
})
|
| 568 |
+
t.assert.ok(res)
|
| 569 |
+
delete res.headers.date
|
| 570 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 571 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 572 |
+
const actualHeaders = {
|
| 573 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 574 |
+
vary: res.headers.vary
|
| 575 |
+
}
|
| 576 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 577 |
+
'access-control-allow-origin': 'example.com',
|
| 578 |
+
vary: 'Origin'
|
| 579 |
+
})
|
| 580 |
+
})
|
| 581 |
+
|
| 582 |
+
test('Dynamic origin resolution (not valid origin - promises)', async t => {
|
| 583 |
+
t.plan(5)
|
| 584 |
+
|
| 585 |
+
const fastify = Fastify()
|
| 586 |
+
const origin = (header) => {
|
| 587 |
+
return new Promise((resolve) => {
|
| 588 |
+
t.assert.strictEqual(header, 'example.com')
|
| 589 |
+
resolve(false)
|
| 590 |
+
})
|
| 591 |
+
}
|
| 592 |
+
fastify.register(cors, { origin })
|
| 593 |
+
|
| 594 |
+
fastify.get('/', (_req, reply) => {
|
| 595 |
+
reply.send('ok')
|
| 596 |
+
})
|
| 597 |
+
|
| 598 |
+
const res = await fastify.inject({
|
| 599 |
+
method: 'GET',
|
| 600 |
+
url: '/',
|
| 601 |
+
headers: { origin: 'example.com' }
|
| 602 |
+
})
|
| 603 |
+
t.assert.ok(res)
|
| 604 |
+
delete res.headers.date
|
| 605 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 606 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 607 |
+
const actualHeaders = {
|
| 608 |
+
'content-length': res.headers['content-length'],
|
| 609 |
+
'content-type': res.headers['content-type'],
|
| 610 |
+
connection: res.headers.connection,
|
| 611 |
+
vary: res.headers.vary
|
| 612 |
+
}
|
| 613 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 614 |
+
'content-length': '2',
|
| 615 |
+
'content-type': 'text/plain; charset=utf-8',
|
| 616 |
+
connection: 'keep-alive',
|
| 617 |
+
vary: 'Origin'
|
| 618 |
+
})
|
| 619 |
+
})
|
| 620 |
+
|
| 621 |
+
test('Dynamic origin resolution (errored - promises)', async t => {
|
| 622 |
+
t.plan(3)
|
| 623 |
+
|
| 624 |
+
const fastify = Fastify()
|
| 625 |
+
const origin = (header) => {
|
| 626 |
+
return new Promise((_resolve, reject) => {
|
| 627 |
+
t.assert.strictEqual(header, 'example.com')
|
| 628 |
+
reject(new Error('ouch'))
|
| 629 |
+
})
|
| 630 |
+
}
|
| 631 |
+
fastify.register(cors, { origin })
|
| 632 |
+
|
| 633 |
+
const res = await fastify.inject({
|
| 634 |
+
method: 'GET',
|
| 635 |
+
url: '/',
|
| 636 |
+
headers: { origin: 'example.com' }
|
| 637 |
+
})
|
| 638 |
+
t.assert.ok(res)
|
| 639 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 640 |
+
})
|
| 641 |
+
|
| 642 |
+
test('Should reply 404 without cors headers when origin is false', async t => {
|
| 643 |
+
t.plan(8)
|
| 644 |
+
|
| 645 |
+
const fastify = Fastify()
|
| 646 |
+
fastify.register(cors, {
|
| 647 |
+
origin: false,
|
| 648 |
+
methods: 'GET',
|
| 649 |
+
credentials: true,
|
| 650 |
+
exposedHeaders: ['foo', 'bar'],
|
| 651 |
+
allowedHeaders: ['baz', 'woo'],
|
| 652 |
+
maxAge: 123
|
| 653 |
+
})
|
| 654 |
+
|
| 655 |
+
fastify.get('/', (_req, reply) => {
|
| 656 |
+
reply.send('ok')
|
| 657 |
+
})
|
| 658 |
+
|
| 659 |
+
let res = await fastify.inject({
|
| 660 |
+
method: 'OPTIONS',
|
| 661 |
+
url: '/'
|
| 662 |
+
})
|
| 663 |
+
t.assert.ok(res)
|
| 664 |
+
delete res.headers.date
|
| 665 |
+
t.assert.strictEqual(res.statusCode, 404)
|
| 666 |
+
t.assert.strictEqual(res.payload, '{"message":"Route OPTIONS:/ not found","error":"Not Found","statusCode":404}')
|
| 667 |
+
const actualHeaders = {
|
| 668 |
+
'content-length': res.headers['content-length'],
|
| 669 |
+
'content-type': res.headers['content-type'],
|
| 670 |
+
connection: res.headers.connection
|
| 671 |
+
}
|
| 672 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 673 |
+
'content-length': '76',
|
| 674 |
+
'content-type': 'application/json; charset=utf-8',
|
| 675 |
+
connection: 'keep-alive'
|
| 676 |
+
})
|
| 677 |
+
|
| 678 |
+
res = await fastify.inject({
|
| 679 |
+
method: 'GET',
|
| 680 |
+
url: '/'
|
| 681 |
+
})
|
| 682 |
+
t.assert.ok(res)
|
| 683 |
+
delete res.headers.date
|
| 684 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 685 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 686 |
+
t.assert.deepStrictEqual(res.headers, {
|
| 687 |
+
'content-length': '2',
|
| 688 |
+
'content-type': 'text/plain; charset=utf-8',
|
| 689 |
+
connection: 'keep-alive'
|
| 690 |
+
})
|
| 691 |
+
})
|
| 692 |
+
|
| 693 |
+
test('Server error if origin option is falsy but not false', async t => {
|
| 694 |
+
t.plan(4)
|
| 695 |
+
|
| 696 |
+
const fastify = Fastify()
|
| 697 |
+
fastify.register(cors, { origin: '' })
|
| 698 |
+
|
| 699 |
+
const res = await fastify.inject({
|
| 700 |
+
method: 'GET',
|
| 701 |
+
url: '/',
|
| 702 |
+
headers: { origin: 'example.com' }
|
| 703 |
+
})
|
| 704 |
+
t.assert.ok(res)
|
| 705 |
+
delete res.headers.date
|
| 706 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 707 |
+
t.assert.deepStrictEqual(res.json(), { statusCode: 500, error: 'Internal Server Error', message: 'Invalid CORS origin option' })
|
| 708 |
+
const actualHeaders = {
|
| 709 |
+
'content-length': res.headers['content-length'],
|
| 710 |
+
'content-type': res.headers['content-type'],
|
| 711 |
+
connection: res.headers.connection
|
| 712 |
+
}
|
| 713 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 714 |
+
'content-length': '89',
|
| 715 |
+
'content-type': 'application/json; charset=utf-8',
|
| 716 |
+
connection: 'keep-alive'
|
| 717 |
+
})
|
| 718 |
+
})
|
| 719 |
+
|
| 720 |
+
test('Allow only request from a specific origin', async t => {
|
| 721 |
+
t.plan(5)
|
| 722 |
+
|
| 723 |
+
const fastify = Fastify()
|
| 724 |
+
fastify.register(cors, { origin: 'other.io' })
|
| 725 |
+
|
| 726 |
+
fastify.get('/', (_req, reply) => {
|
| 727 |
+
reply.send('ok')
|
| 728 |
+
})
|
| 729 |
+
|
| 730 |
+
const res = await fastify.inject({
|
| 731 |
+
method: 'GET',
|
| 732 |
+
url: '/',
|
| 733 |
+
headers: { origin: 'example.com' }
|
| 734 |
+
})
|
| 735 |
+
t.assert.ok(res)
|
| 736 |
+
delete res.headers.date
|
| 737 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 738 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 739 |
+
t.assert.deepStrictEqual(res.headers['access-control-allow-origin'],
|
| 740 |
+
'other.io'
|
| 741 |
+
)
|
| 742 |
+
t.assert.notDeepEqual(res.headers, { vary: 'Origin' })
|
| 743 |
+
})
|
| 744 |
+
|
| 745 |
+
test('Allow only request from multiple specific origin', async t => {
|
| 746 |
+
t.plan(9)
|
| 747 |
+
|
| 748 |
+
const fastify = Fastify()
|
| 749 |
+
fastify.register(cors, { origin: ['other.io', 'example.com'] })
|
| 750 |
+
|
| 751 |
+
fastify.get('/', (_req, reply) => {
|
| 752 |
+
reply.send('ok')
|
| 753 |
+
})
|
| 754 |
+
|
| 755 |
+
let res = await fastify.inject({
|
| 756 |
+
method: 'GET',
|
| 757 |
+
url: '/',
|
| 758 |
+
headers: { origin: 'other.io' }
|
| 759 |
+
})
|
| 760 |
+
t.assert.ok(res)
|
| 761 |
+
delete res.headers.date
|
| 762 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 763 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 764 |
+
const actualHeaders = {
|
| 765 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 766 |
+
vary: res.headers.vary
|
| 767 |
+
}
|
| 768 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 769 |
+
'access-control-allow-origin': 'other.io',
|
| 770 |
+
vary: 'Origin'
|
| 771 |
+
})
|
| 772 |
+
|
| 773 |
+
res = await fastify.inject({
|
| 774 |
+
method: 'GET',
|
| 775 |
+
url: '/',
|
| 776 |
+
headers: { origin: 'foo.com' }
|
| 777 |
+
})
|
| 778 |
+
t.assert.ok(res)
|
| 779 |
+
delete res.headers.date
|
| 780 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 781 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 782 |
+
t.assert.deepStrictEqual(res.headers.vary,
|
| 783 |
+
'Origin'
|
| 784 |
+
)
|
| 785 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'], undefined)
|
| 786 |
+
})
|
| 787 |
+
|
| 788 |
+
test('Allow only request from a specific origin using regex', async t => {
|
| 789 |
+
t.plan(8)
|
| 790 |
+
|
| 791 |
+
const fastify = Fastify()
|
| 792 |
+
fastify.register(cors, { origin: /(?:example|other)\.com\/?$/giu })
|
| 793 |
+
|
| 794 |
+
fastify.get('/', (_req, reply) => {
|
| 795 |
+
reply.send('ok')
|
| 796 |
+
})
|
| 797 |
+
|
| 798 |
+
// .test was previously used, which caused 2 consecutive requests to return
|
| 799 |
+
// different results with global (e.g. /g) regexes. Therefore, check this
|
| 800 |
+
// twice to check consistency
|
| 801 |
+
for (let i = 0; i < 2; i++) {
|
| 802 |
+
const res = await fastify.inject({
|
| 803 |
+
method: 'GET',
|
| 804 |
+
url: '/',
|
| 805 |
+
headers: { origin: 'https://www.example.com/' }
|
| 806 |
+
})
|
| 807 |
+
t.assert.ok(res)
|
| 808 |
+
delete res.headers.date
|
| 809 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 810 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 811 |
+
const actualHeaders = {
|
| 812 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 813 |
+
vary: res.headers.vary
|
| 814 |
+
}
|
| 815 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 816 |
+
'access-control-allow-origin': 'https://www.example.com/',
|
| 817 |
+
vary: 'Origin'
|
| 818 |
+
})
|
| 819 |
+
}
|
| 820 |
+
})
|
| 821 |
+
|
| 822 |
+
test('Disable preflight', async t => {
|
| 823 |
+
t.plan(7)
|
| 824 |
+
|
| 825 |
+
const fastify = Fastify()
|
| 826 |
+
fastify.register(cors, { preflight: false })
|
| 827 |
+
|
| 828 |
+
fastify.get('/', (_req, reply) => {
|
| 829 |
+
reply.send('ok')
|
| 830 |
+
})
|
| 831 |
+
|
| 832 |
+
let res = await fastify.inject({
|
| 833 |
+
method: 'OPTIONS',
|
| 834 |
+
url: '/hello'
|
| 835 |
+
})
|
| 836 |
+
t.assert.ok(res)
|
| 837 |
+
delete res.headers.date
|
| 838 |
+
t.assert.strictEqual(res.statusCode, 404)
|
| 839 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'],
|
| 840 |
+
'*'
|
| 841 |
+
)
|
| 842 |
+
|
| 843 |
+
res = await fastify.inject({
|
| 844 |
+
method: 'GET',
|
| 845 |
+
url: '/'
|
| 846 |
+
})
|
| 847 |
+
t.assert.ok(res)
|
| 848 |
+
delete res.headers.date
|
| 849 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 850 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 851 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'],
|
| 852 |
+
'*'
|
| 853 |
+
)
|
| 854 |
+
})
|
| 855 |
+
|
| 856 |
+
test('Should always add vary header to `Origin` for reflected origin', async t => {
|
| 857 |
+
t.plan(12)
|
| 858 |
+
|
| 859 |
+
const fastify = Fastify()
|
| 860 |
+
fastify.register(cors, { origin: true })
|
| 861 |
+
|
| 862 |
+
fastify.get('/', (_req, reply) => {
|
| 863 |
+
reply.send('ok')
|
| 864 |
+
})
|
| 865 |
+
|
| 866 |
+
// Invalid Preflight
|
| 867 |
+
let res = await fastify.inject({
|
| 868 |
+
method: 'OPTIONS',
|
| 869 |
+
url: '/'
|
| 870 |
+
})
|
| 871 |
+
t.assert.ok(res)
|
| 872 |
+
delete res.headers.date
|
| 873 |
+
t.assert.strictEqual(res.statusCode, 400)
|
| 874 |
+
t.assert.strictEqual(res.payload, 'Invalid Preflight Request')
|
| 875 |
+
t.assert.strictEqual(res.headers.vary,
|
| 876 |
+
'Origin'
|
| 877 |
+
)
|
| 878 |
+
|
| 879 |
+
// Valid Preflight
|
| 880 |
+
res = await fastify.inject({
|
| 881 |
+
method: 'OPTIONS',
|
| 882 |
+
url: '/',
|
| 883 |
+
headers: {
|
| 884 |
+
'access-control-request-method': 'GET',
|
| 885 |
+
origin: 'example.com'
|
| 886 |
+
}
|
| 887 |
+
})
|
| 888 |
+
t.assert.ok(res)
|
| 889 |
+
delete res.headers.date
|
| 890 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 891 |
+
t.assert.strictEqual(res.payload, '')
|
| 892 |
+
t.assert.strictEqual(res.headers.vary,
|
| 893 |
+
'Origin, Access-Control-Request-Headers'
|
| 894 |
+
)
|
| 895 |
+
|
| 896 |
+
// Other Route
|
| 897 |
+
res = await fastify.inject({
|
| 898 |
+
method: 'GET',
|
| 899 |
+
url: '/'
|
| 900 |
+
})
|
| 901 |
+
t.assert.ok(res)
|
| 902 |
+
delete res.headers.date
|
| 903 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 904 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 905 |
+
t.assert.strictEqual(res.headers.vary,
|
| 906 |
+
'Origin'
|
| 907 |
+
)
|
| 908 |
+
})
|
| 909 |
+
|
| 910 |
+
test('Should always add vary header to `Origin` for reflected origin (vary is array)', async t => {
|
| 911 |
+
t.plan(4)
|
| 912 |
+
|
| 913 |
+
const fastify = Fastify()
|
| 914 |
+
|
| 915 |
+
// Mock getHeader function
|
| 916 |
+
fastify.decorateReply('getHeader', () => ['foo', 'bar'])
|
| 917 |
+
|
| 918 |
+
fastify.register(cors, { origin: true })
|
| 919 |
+
|
| 920 |
+
fastify.get('/', (_req, reply) => {
|
| 921 |
+
reply.send('ok')
|
| 922 |
+
})
|
| 923 |
+
|
| 924 |
+
const res = await fastify.inject({
|
| 925 |
+
method: 'GET',
|
| 926 |
+
url: '/'
|
| 927 |
+
})
|
| 928 |
+
t.assert.ok(res)
|
| 929 |
+
delete res.headers.date
|
| 930 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 931 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 932 |
+
t.assert.strictEqual(res.headers.vary,
|
| 933 |
+
'foo, bar, Origin'
|
| 934 |
+
)
|
| 935 |
+
})
|
| 936 |
+
|
| 937 |
+
test('Allow only request from with specific headers', async t => {
|
| 938 |
+
t.plan(8)
|
| 939 |
+
|
| 940 |
+
const fastify = Fastify()
|
| 941 |
+
fastify.register(cors, {
|
| 942 |
+
allowedHeaders: 'foo',
|
| 943 |
+
exposedHeaders: 'bar'
|
| 944 |
+
})
|
| 945 |
+
|
| 946 |
+
fastify.get('/', (_req, reply) => {
|
| 947 |
+
reply.send('ok')
|
| 948 |
+
})
|
| 949 |
+
|
| 950 |
+
let res = await fastify.inject({
|
| 951 |
+
method: 'OPTIONS',
|
| 952 |
+
url: '/',
|
| 953 |
+
headers: {
|
| 954 |
+
'access-control-request-method': 'GET',
|
| 955 |
+
origin: 'example.com'
|
| 956 |
+
}
|
| 957 |
+
})
|
| 958 |
+
t.assert.ok(res)
|
| 959 |
+
delete res.headers.date
|
| 960 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 961 |
+
t.assert.deepStrictEqual(res.headers['access-control-allow-headers'],
|
| 962 |
+
'foo'
|
| 963 |
+
)
|
| 964 |
+
t.assert.notDeepEqual(res.headers.vary, 'Origin')
|
| 965 |
+
|
| 966 |
+
res = await fastify.inject({
|
| 967 |
+
method: 'GET',
|
| 968 |
+
url: '/'
|
| 969 |
+
})
|
| 970 |
+
t.assert.ok(res)
|
| 971 |
+
delete res.headers.date
|
| 972 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 973 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 974 |
+
t.assert.strictEqual(res.headers['access-control-expose-headers'],
|
| 975 |
+
'bar'
|
| 976 |
+
)
|
| 977 |
+
})
|
| 978 |
+
|
| 979 |
+
test('Should support wildcard config /1', async t => {
|
| 980 |
+
t.plan(4)
|
| 981 |
+
|
| 982 |
+
const fastify = Fastify()
|
| 983 |
+
fastify.register(cors, { origin: '*' })
|
| 984 |
+
|
| 985 |
+
fastify.get('/', (_req, reply) => {
|
| 986 |
+
reply.send('ok')
|
| 987 |
+
})
|
| 988 |
+
|
| 989 |
+
const res = await fastify.inject({
|
| 990 |
+
method: 'GET',
|
| 991 |
+
url: '/'
|
| 992 |
+
})
|
| 993 |
+
t.assert.ok(res)
|
| 994 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 995 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 996 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
|
| 997 |
+
})
|
| 998 |
+
|
| 999 |
+
test('Should support wildcard config /2', async t => {
|
| 1000 |
+
t.plan(4)
|
| 1001 |
+
|
| 1002 |
+
const fastify = Fastify()
|
| 1003 |
+
fastify.register(cors, { origin: ['*'] })
|
| 1004 |
+
|
| 1005 |
+
fastify.get('/', (_req, reply) => {
|
| 1006 |
+
reply.send('ok')
|
| 1007 |
+
})
|
| 1008 |
+
|
| 1009 |
+
const res = await fastify.inject({
|
| 1010 |
+
method: 'GET',
|
| 1011 |
+
url: '/'
|
| 1012 |
+
})
|
| 1013 |
+
t.assert.ok(res)
|
| 1014 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 1015 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 1016 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
|
| 1017 |
+
})
|
| 1018 |
+
|
| 1019 |
+
test('Should allow routes to disable CORS individually', async t => {
|
| 1020 |
+
t.plan(6)
|
| 1021 |
+
|
| 1022 |
+
const fastify = Fastify()
|
| 1023 |
+
fastify.register(cors, { origin: '*' })
|
| 1024 |
+
|
| 1025 |
+
fastify.get('/cors-enabled', (_req, reply) => {
|
| 1026 |
+
reply.send('ok')
|
| 1027 |
+
})
|
| 1028 |
+
|
| 1029 |
+
fastify.get('/cors-disabled', { config: { cors: false } }, (_req, reply) => {
|
| 1030 |
+
reply.send('ok')
|
| 1031 |
+
})
|
| 1032 |
+
|
| 1033 |
+
// Test CORS enabled route
|
| 1034 |
+
let res = await fastify.inject({
|
| 1035 |
+
method: 'GET',
|
| 1036 |
+
url: '/cors-enabled',
|
| 1037 |
+
headers: { origin: 'example.com' }
|
| 1038 |
+
})
|
| 1039 |
+
t.assert.ok(res)
|
| 1040 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 1041 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'], '*')
|
| 1042 |
+
|
| 1043 |
+
// Test CORS disabled route
|
| 1044 |
+
res = await fastify.inject({
|
| 1045 |
+
method: 'GET',
|
| 1046 |
+
url: '/cors-disabled',
|
| 1047 |
+
headers: { origin: 'example.com' }
|
| 1048 |
+
})
|
| 1049 |
+
t.assert.ok(res)
|
| 1050 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 1051 |
+
t.assert.strictEqual(res.headers['access-control-allow-origin'], undefined)
|
| 1052 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/hooks.test.js
ADDED
|
@@ -0,0 +1,787 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const { test } = require('node:test')
|
| 4 |
+
const Fastify = require('fastify')
|
| 5 |
+
const kFastifyContext = require('fastify/lib/symbols').kRouteContext
|
| 6 |
+
const cors = require('..')
|
| 7 |
+
const { setTimeout: sleep } = require('node:timers/promises')
|
| 8 |
+
|
| 9 |
+
test('Should error on invalid hook option', async (t) => {
|
| 10 |
+
t.plan(3)
|
| 11 |
+
|
| 12 |
+
const fastify = Fastify()
|
| 13 |
+
await t.assert.rejects(
|
| 14 |
+
async () => fastify.register(cors, { hook: 'invalid' }),
|
| 15 |
+
(err) => {
|
| 16 |
+
t.assert.strictEqual(err.name, 'TypeError')
|
| 17 |
+
t.assert.strictEqual(err.message, '@fastify/cors: Invalid hook option provided.')
|
| 18 |
+
return true
|
| 19 |
+
}
|
| 20 |
+
)
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
test('Should set hook onRequest if hook option is not set', async (t) => {
|
| 24 |
+
t.plan(10)
|
| 25 |
+
|
| 26 |
+
const fastify = Fastify()
|
| 27 |
+
|
| 28 |
+
fastify.register(cors)
|
| 29 |
+
|
| 30 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 31 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 32 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest.length, 1)
|
| 33 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 34 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 35 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 36 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 37 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 38 |
+
done()
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
fastify.get('/', (_req, reply) => {
|
| 42 |
+
reply.send('ok')
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
await fastify.ready()
|
| 46 |
+
|
| 47 |
+
const res = await fastify.inject({
|
| 48 |
+
method: 'GET',
|
| 49 |
+
url: '/'
|
| 50 |
+
})
|
| 51 |
+
delete res.headers.date
|
| 52 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 53 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 54 |
+
const actualHeader = {
|
| 55 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 56 |
+
}
|
| 57 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 58 |
+
'access-control-allow-origin': '*'
|
| 59 |
+
})
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
test('Should set hook onRequest if hook option is set to onRequest', async (t) => {
|
| 63 |
+
t.plan(10)
|
| 64 |
+
|
| 65 |
+
const fastify = Fastify()
|
| 66 |
+
|
| 67 |
+
fastify.register(cors, {
|
| 68 |
+
hook: 'onRequest'
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 72 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 73 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest.length, 1)
|
| 74 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 75 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 76 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 77 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 78 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 79 |
+
done()
|
| 80 |
+
})
|
| 81 |
+
|
| 82 |
+
fastify.get('/', (_req, reply) => {
|
| 83 |
+
reply.send('ok')
|
| 84 |
+
})
|
| 85 |
+
|
| 86 |
+
await fastify.ready()
|
| 87 |
+
|
| 88 |
+
const res = await fastify.inject({
|
| 89 |
+
method: 'GET',
|
| 90 |
+
url: '/'
|
| 91 |
+
})
|
| 92 |
+
delete res.headers.date
|
| 93 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 94 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 95 |
+
const actualHeader = {
|
| 96 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 97 |
+
}
|
| 98 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 99 |
+
'access-control-allow-origin': '*'
|
| 100 |
+
})
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
test('Should set hook preParsing if hook option is set to preParsing', async (t) => {
|
| 104 |
+
t.plan(11)
|
| 105 |
+
|
| 106 |
+
const fastify = Fastify()
|
| 107 |
+
|
| 108 |
+
fastify.register(cors, {
|
| 109 |
+
hook: 'preParsing'
|
| 110 |
+
})
|
| 111 |
+
|
| 112 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 113 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 114 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 115 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 116 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 117 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing.length, 1)
|
| 118 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 119 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 120 |
+
done()
|
| 121 |
+
})
|
| 122 |
+
|
| 123 |
+
fastify.get('/', (_req, reply) => {
|
| 124 |
+
reply.send('ok')
|
| 125 |
+
})
|
| 126 |
+
|
| 127 |
+
await fastify.ready()
|
| 128 |
+
|
| 129 |
+
const res = await fastify.inject({
|
| 130 |
+
method: 'GET',
|
| 131 |
+
url: '/'
|
| 132 |
+
})
|
| 133 |
+
delete res.headers.date
|
| 134 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 135 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 136 |
+
const actualHeader = {
|
| 137 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 138 |
+
}
|
| 139 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 140 |
+
'access-control-allow-origin': '*'
|
| 141 |
+
})
|
| 142 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 143 |
+
})
|
| 144 |
+
|
| 145 |
+
test('Should set hook preValidation if hook option is set to preValidation', async (t) => {
|
| 146 |
+
t.plan(11)
|
| 147 |
+
|
| 148 |
+
const fastify = Fastify()
|
| 149 |
+
|
| 150 |
+
fastify.register(cors, {
|
| 151 |
+
hook: 'preValidation'
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 155 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 156 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 157 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 158 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 159 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 160 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 161 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation.length, 1)
|
| 162 |
+
done()
|
| 163 |
+
})
|
| 164 |
+
|
| 165 |
+
fastify.get('/', (_req, reply) => {
|
| 166 |
+
reply.send('ok')
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
await fastify.ready()
|
| 170 |
+
|
| 171 |
+
const res = await fastify.inject({
|
| 172 |
+
method: 'GET',
|
| 173 |
+
url: '/'
|
| 174 |
+
})
|
| 175 |
+
delete res.headers.date
|
| 176 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 177 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 178 |
+
const actualHeader = {
|
| 179 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 180 |
+
}
|
| 181 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 182 |
+
'access-control-allow-origin': '*'
|
| 183 |
+
})
|
| 184 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 185 |
+
})
|
| 186 |
+
|
| 187 |
+
test('Should set hook preParsing if hook option is set to preParsing', async (t) => {
|
| 188 |
+
t.plan(11)
|
| 189 |
+
|
| 190 |
+
const fastify = Fastify()
|
| 191 |
+
|
| 192 |
+
fastify.register(cors, {
|
| 193 |
+
hook: 'preParsing'
|
| 194 |
+
})
|
| 195 |
+
|
| 196 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 197 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 198 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 199 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 200 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 201 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing.length, 1)
|
| 202 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 203 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 204 |
+
done()
|
| 205 |
+
})
|
| 206 |
+
|
| 207 |
+
fastify.get('/', (_req, reply) => {
|
| 208 |
+
reply.send('ok')
|
| 209 |
+
})
|
| 210 |
+
|
| 211 |
+
await fastify.ready()
|
| 212 |
+
|
| 213 |
+
const res = await fastify.inject({
|
| 214 |
+
method: 'GET',
|
| 215 |
+
url: '/'
|
| 216 |
+
})
|
| 217 |
+
delete res.headers.date
|
| 218 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 219 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 220 |
+
const actualHeader = {
|
| 221 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 222 |
+
}
|
| 223 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 224 |
+
'access-control-allow-origin': '*'
|
| 225 |
+
})
|
| 226 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 227 |
+
})
|
| 228 |
+
|
| 229 |
+
test('Should set hook preHandler if hook option is set to preHandler', async (t) => {
|
| 230 |
+
t.plan(11)
|
| 231 |
+
|
| 232 |
+
const fastify = Fastify()
|
| 233 |
+
|
| 234 |
+
fastify.register(cors, {
|
| 235 |
+
hook: 'preHandler'
|
| 236 |
+
})
|
| 237 |
+
|
| 238 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 239 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 240 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 241 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 242 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler.length, 1)
|
| 243 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 244 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 245 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 246 |
+
done()
|
| 247 |
+
})
|
| 248 |
+
|
| 249 |
+
fastify.get('/', (_req, reply) => {
|
| 250 |
+
reply.send('ok')
|
| 251 |
+
})
|
| 252 |
+
|
| 253 |
+
await fastify.ready()
|
| 254 |
+
|
| 255 |
+
const res = await fastify.inject({
|
| 256 |
+
method: 'GET',
|
| 257 |
+
url: '/'
|
| 258 |
+
})
|
| 259 |
+
delete res.headers.date
|
| 260 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 261 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 262 |
+
const actualHeader = {
|
| 263 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 264 |
+
}
|
| 265 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 266 |
+
'access-control-allow-origin': '*'
|
| 267 |
+
})
|
| 268 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 269 |
+
})
|
| 270 |
+
|
| 271 |
+
test('Should set hook onSend if hook option is set to onSend', async (t) => {
|
| 272 |
+
t.plan(11)
|
| 273 |
+
|
| 274 |
+
const fastify = Fastify()
|
| 275 |
+
|
| 276 |
+
fastify.register(cors, {
|
| 277 |
+
hook: 'onSend'
|
| 278 |
+
})
|
| 279 |
+
|
| 280 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 281 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 282 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 283 |
+
t.assert.strictEqual(request[kFastifyContext].onSend.length, 1)
|
| 284 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 285 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 286 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization, null)
|
| 287 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 288 |
+
done()
|
| 289 |
+
})
|
| 290 |
+
|
| 291 |
+
fastify.get('/', (_req, reply) => {
|
| 292 |
+
reply.send('ok')
|
| 293 |
+
})
|
| 294 |
+
|
| 295 |
+
await fastify.ready()
|
| 296 |
+
|
| 297 |
+
const res = await fastify.inject({
|
| 298 |
+
method: 'GET',
|
| 299 |
+
url: '/'
|
| 300 |
+
})
|
| 301 |
+
delete res.headers.date
|
| 302 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 303 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 304 |
+
const actualHeader = {
|
| 305 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 306 |
+
}
|
| 307 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 308 |
+
'access-control-allow-origin': '*'
|
| 309 |
+
})
|
| 310 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 311 |
+
})
|
| 312 |
+
|
| 313 |
+
test('Should set hook preSerialization if hook option is set to preSerialization', async (t) => {
|
| 314 |
+
t.plan(11)
|
| 315 |
+
|
| 316 |
+
const fastify = Fastify()
|
| 317 |
+
|
| 318 |
+
fastify.register(cors, {
|
| 319 |
+
hook: 'preSerialization'
|
| 320 |
+
})
|
| 321 |
+
|
| 322 |
+
fastify.addHook('onResponse', (request, _reply, done) => {
|
| 323 |
+
t.assert.strictEqual(request[kFastifyContext].onError, null)
|
| 324 |
+
t.assert.strictEqual(request[kFastifyContext].onRequest, null)
|
| 325 |
+
t.assert.strictEqual(request[kFastifyContext].onSend, null)
|
| 326 |
+
t.assert.strictEqual(request[kFastifyContext].preHandler, null)
|
| 327 |
+
t.assert.strictEqual(request[kFastifyContext].preParsing, null)
|
| 328 |
+
t.assert.strictEqual(request[kFastifyContext].preSerialization.length, 1)
|
| 329 |
+
t.assert.strictEqual(request[kFastifyContext].preValidation, null)
|
| 330 |
+
done()
|
| 331 |
+
})
|
| 332 |
+
|
| 333 |
+
fastify.get('/', (_req, reply) => {
|
| 334 |
+
reply.send({ nonString: true })
|
| 335 |
+
})
|
| 336 |
+
|
| 337 |
+
await fastify.ready()
|
| 338 |
+
|
| 339 |
+
const res = await fastify.inject({
|
| 340 |
+
method: 'GET',
|
| 341 |
+
url: '/'
|
| 342 |
+
})
|
| 343 |
+
delete res.headers.date
|
| 344 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 345 |
+
t.assert.strictEqual(res.payload, '{"nonString":true}')
|
| 346 |
+
const actualHeader = {
|
| 347 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 348 |
+
}
|
| 349 |
+
t.assert.deepStrictEqual(actualHeader, {
|
| 350 |
+
'access-control-allow-origin': '*'
|
| 351 |
+
})
|
| 352 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 353 |
+
})
|
| 354 |
+
|
| 355 |
+
test('Should support custom hook with dynamic config', async t => {
|
| 356 |
+
t.plan(16)
|
| 357 |
+
|
| 358 |
+
const configs = [{
|
| 359 |
+
origin: 'example.com',
|
| 360 |
+
methods: 'GET',
|
| 361 |
+
credentials: true,
|
| 362 |
+
exposedHeaders: ['foo', 'bar'],
|
| 363 |
+
allowedHeaders: ['baz', 'woo'],
|
| 364 |
+
maxAge: 123
|
| 365 |
+
}, {
|
| 366 |
+
origin: 'sample.com',
|
| 367 |
+
methods: 'GET',
|
| 368 |
+
credentials: true,
|
| 369 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 370 |
+
allowedHeaders: ['baz', 'foo'],
|
| 371 |
+
maxAge: 321
|
| 372 |
+
}]
|
| 373 |
+
|
| 374 |
+
const fastify = Fastify()
|
| 375 |
+
let requestId = 0
|
| 376 |
+
const configDelegation = async function (req) {
|
| 377 |
+
// request should have id
|
| 378 |
+
t.assert.ok(req.id)
|
| 379 |
+
// request should not have send
|
| 380 |
+
t.assert.ifError(req.send)
|
| 381 |
+
const config = configs[requestId]
|
| 382 |
+
requestId++
|
| 383 |
+
if (config) {
|
| 384 |
+
return Promise.resolve(config)
|
| 385 |
+
} else {
|
| 386 |
+
return Promise.reject(new Error('ouch'))
|
| 387 |
+
}
|
| 388 |
+
}
|
| 389 |
+
await fastify.register(cors, {
|
| 390 |
+
hook: 'preHandler',
|
| 391 |
+
delegator: configDelegation
|
| 392 |
+
})
|
| 393 |
+
|
| 394 |
+
fastify.get('/', (_req, reply) => {
|
| 395 |
+
reply.send('ok')
|
| 396 |
+
})
|
| 397 |
+
|
| 398 |
+
let res = await fastify.inject({
|
| 399 |
+
method: 'GET',
|
| 400 |
+
url: '/'
|
| 401 |
+
})
|
| 402 |
+
t.assert.ok(res)
|
| 403 |
+
delete res.headers.date
|
| 404 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 405 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 406 |
+
let actualHeaders = {
|
| 407 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 408 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 409 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 410 |
+
'content-length': res.headers['content-length'],
|
| 411 |
+
vary: res.headers.vary
|
| 412 |
+
}
|
| 413 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 414 |
+
'access-control-allow-origin': 'example.com',
|
| 415 |
+
'access-control-allow-credentials': 'true',
|
| 416 |
+
'access-control-expose-headers': 'foo, bar',
|
| 417 |
+
'content-length': '2',
|
| 418 |
+
vary: 'Origin'
|
| 419 |
+
})
|
| 420 |
+
|
| 421 |
+
res = await fastify.inject({
|
| 422 |
+
method: 'OPTIONS',
|
| 423 |
+
url: '/',
|
| 424 |
+
headers: {
|
| 425 |
+
'access-control-request-method': 'GET',
|
| 426 |
+
origin: 'example.com'
|
| 427 |
+
}
|
| 428 |
+
})
|
| 429 |
+
t.assert.ok(res)
|
| 430 |
+
delete res.headers.date
|
| 431 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 432 |
+
t.assert.strictEqual(res.payload, '')
|
| 433 |
+
actualHeaders = {
|
| 434 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 435 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 436 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 437 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 438 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 439 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 440 |
+
'content-length': res.headers['content-length'],
|
| 441 |
+
vary: res.headers.vary
|
| 442 |
+
}
|
| 443 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 444 |
+
'access-control-allow-origin': 'sample.com',
|
| 445 |
+
'access-control-allow-credentials': 'true',
|
| 446 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 447 |
+
'access-control-allow-methods': 'GET',
|
| 448 |
+
'access-control-allow-headers': 'baz, foo',
|
| 449 |
+
'access-control-max-age': '321',
|
| 450 |
+
'content-length': '0',
|
| 451 |
+
vary: 'Origin'
|
| 452 |
+
})
|
| 453 |
+
|
| 454 |
+
res = await fastify.inject({
|
| 455 |
+
method: 'GET',
|
| 456 |
+
url: '/',
|
| 457 |
+
headers: {
|
| 458 |
+
'access-control-request-method': 'GET',
|
| 459 |
+
origin: 'example.com'
|
| 460 |
+
}
|
| 461 |
+
})
|
| 462 |
+
t.assert.ok(res)
|
| 463 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 464 |
+
})
|
| 465 |
+
|
| 466 |
+
test('Should support custom hook with dynamic config (callback)', async t => {
|
| 467 |
+
t.plan(16)
|
| 468 |
+
|
| 469 |
+
const configs = [{
|
| 470 |
+
origin: 'example.com',
|
| 471 |
+
methods: 'GET',
|
| 472 |
+
credentials: true,
|
| 473 |
+
exposedHeaders: ['foo', 'bar'],
|
| 474 |
+
allowedHeaders: ['baz', 'woo'],
|
| 475 |
+
maxAge: 123
|
| 476 |
+
}, {
|
| 477 |
+
origin: 'sample.com',
|
| 478 |
+
methods: 'GET',
|
| 479 |
+
credentials: true,
|
| 480 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 481 |
+
allowedHeaders: ['baz', 'foo'],
|
| 482 |
+
maxAge: 321
|
| 483 |
+
}]
|
| 484 |
+
|
| 485 |
+
const fastify = Fastify()
|
| 486 |
+
let requestId = 0
|
| 487 |
+
const configDelegation = function (req, cb) {
|
| 488 |
+
// request should have id
|
| 489 |
+
t.assert.ok(req.id)
|
| 490 |
+
// request should not have send
|
| 491 |
+
t.assert.ifError(req.send)
|
| 492 |
+
const config = configs[requestId]
|
| 493 |
+
requestId++
|
| 494 |
+
if (config) {
|
| 495 |
+
cb(null, config)
|
| 496 |
+
} else {
|
| 497 |
+
cb(new Error('ouch'))
|
| 498 |
+
}
|
| 499 |
+
}
|
| 500 |
+
fastify.register(cors, {
|
| 501 |
+
hook: 'preParsing',
|
| 502 |
+
delegator: configDelegation
|
| 503 |
+
})
|
| 504 |
+
|
| 505 |
+
fastify.get('/', (_req, reply) => {
|
| 506 |
+
reply.send('ok')
|
| 507 |
+
})
|
| 508 |
+
|
| 509 |
+
fastify.inject({
|
| 510 |
+
method: 'GET',
|
| 511 |
+
url: '/'
|
| 512 |
+
}, (err, res) => {
|
| 513 |
+
t.assert.ifError(err)
|
| 514 |
+
delete res.headers.date
|
| 515 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 516 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 517 |
+
const actualHeaders = {
|
| 518 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 519 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 520 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 521 |
+
'content-length': res.headers['content-length'],
|
| 522 |
+
vary: res.headers.vary
|
| 523 |
+
}
|
| 524 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 525 |
+
'access-control-allow-origin': 'example.com',
|
| 526 |
+
'access-control-allow-credentials': 'true',
|
| 527 |
+
'access-control-expose-headers': 'foo, bar',
|
| 528 |
+
'content-length': '2',
|
| 529 |
+
vary: 'Origin'
|
| 530 |
+
})
|
| 531 |
+
})
|
| 532 |
+
|
| 533 |
+
fastify.inject({
|
| 534 |
+
method: 'OPTIONS',
|
| 535 |
+
url: '/',
|
| 536 |
+
headers: {
|
| 537 |
+
'access-control-request-method': 'GET',
|
| 538 |
+
origin: 'example.com'
|
| 539 |
+
}
|
| 540 |
+
}, (err, res) => {
|
| 541 |
+
t.assert.ifError(err)
|
| 542 |
+
delete res.headers.date
|
| 543 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 544 |
+
t.assert.strictEqual(res.payload, '')
|
| 545 |
+
const actualHeaders = {
|
| 546 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 547 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 548 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 549 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 550 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 551 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 552 |
+
'content-length': res.headers['content-length'],
|
| 553 |
+
vary: res.headers.vary
|
| 554 |
+
}
|
| 555 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 556 |
+
'access-control-allow-origin': 'sample.com',
|
| 557 |
+
'access-control-allow-credentials': 'true',
|
| 558 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 559 |
+
'access-control-allow-methods': 'GET',
|
| 560 |
+
'access-control-allow-headers': 'baz, foo',
|
| 561 |
+
'access-control-max-age': '321',
|
| 562 |
+
'content-length': '0',
|
| 563 |
+
vary: 'Origin'
|
| 564 |
+
})
|
| 565 |
+
})
|
| 566 |
+
|
| 567 |
+
fastify.inject({
|
| 568 |
+
method: 'GET',
|
| 569 |
+
url: '/',
|
| 570 |
+
headers: {
|
| 571 |
+
'access-control-request-method': 'GET',
|
| 572 |
+
origin: 'example.com'
|
| 573 |
+
}
|
| 574 |
+
}, (err, res) => {
|
| 575 |
+
t.assert.ifError(err)
|
| 576 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 577 |
+
})
|
| 578 |
+
await sleep()
|
| 579 |
+
})
|
| 580 |
+
|
| 581 |
+
test('Should support custom hook with dynamic config (Promise)', async t => {
|
| 582 |
+
t.plan(16)
|
| 583 |
+
|
| 584 |
+
const configs = [{
|
| 585 |
+
origin: 'example.com',
|
| 586 |
+
methods: 'GET',
|
| 587 |
+
credentials: true,
|
| 588 |
+
exposedHeaders: ['foo', 'bar'],
|
| 589 |
+
allowedHeaders: ['baz', 'woo'],
|
| 590 |
+
maxAge: 123
|
| 591 |
+
}, {
|
| 592 |
+
origin: 'sample.com',
|
| 593 |
+
methods: 'GET',
|
| 594 |
+
credentials: true,
|
| 595 |
+
exposedHeaders: ['zoo', 'bar'],
|
| 596 |
+
allowedHeaders: ['baz', 'foo'],
|
| 597 |
+
maxAge: 321
|
| 598 |
+
}]
|
| 599 |
+
|
| 600 |
+
const fastify = Fastify()
|
| 601 |
+
let requestId = 0
|
| 602 |
+
const configDelegation = async function (req) {
|
| 603 |
+
// request should have id
|
| 604 |
+
t.assert.ok(req.id)
|
| 605 |
+
// request should not have send
|
| 606 |
+
t.assert.ifError(req.send)
|
| 607 |
+
const config = configs[requestId]
|
| 608 |
+
requestId++
|
| 609 |
+
if (config) {
|
| 610 |
+
return Promise.resolve(config)
|
| 611 |
+
} else {
|
| 612 |
+
return Promise.reject(new Error('ouch'))
|
| 613 |
+
}
|
| 614 |
+
}
|
| 615 |
+
|
| 616 |
+
await fastify.register(cors, {
|
| 617 |
+
hook: 'preParsing',
|
| 618 |
+
delegator: configDelegation
|
| 619 |
+
})
|
| 620 |
+
|
| 621 |
+
fastify.get('/', (_req, reply) => {
|
| 622 |
+
reply.send('ok')
|
| 623 |
+
})
|
| 624 |
+
|
| 625 |
+
let res = await fastify.inject({
|
| 626 |
+
method: 'GET',
|
| 627 |
+
url: '/'
|
| 628 |
+
})
|
| 629 |
+
t.assert.ok(res)
|
| 630 |
+
delete res.headers.date
|
| 631 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 632 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 633 |
+
let actualHeaders = {
|
| 634 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 635 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 636 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 637 |
+
'content-length': res.headers['content-length'],
|
| 638 |
+
vary: res.headers.vary
|
| 639 |
+
}
|
| 640 |
+
|
| 641 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 642 |
+
'access-control-allow-origin': 'example.com',
|
| 643 |
+
'access-control-allow-credentials': 'true',
|
| 644 |
+
'access-control-expose-headers': 'foo, bar',
|
| 645 |
+
'content-length': '2',
|
| 646 |
+
vary: 'Origin'
|
| 647 |
+
})
|
| 648 |
+
|
| 649 |
+
res = await fastify.inject({
|
| 650 |
+
method: 'OPTIONS',
|
| 651 |
+
url: '/',
|
| 652 |
+
headers: {
|
| 653 |
+
'access-control-request-method': 'GET',
|
| 654 |
+
origin: 'example.com'
|
| 655 |
+
}
|
| 656 |
+
})
|
| 657 |
+
t.assert.ok(res)
|
| 658 |
+
delete res.headers.date
|
| 659 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 660 |
+
t.assert.strictEqual(res.payload, '')
|
| 661 |
+
actualHeaders = {
|
| 662 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 663 |
+
'access-control-allow-credentials': res.headers['access-control-allow-credentials'],
|
| 664 |
+
'access-control-expose-headers': res.headers['access-control-expose-headers'],
|
| 665 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 666 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 667 |
+
'access-control-max-age': res.headers['access-control-max-age'],
|
| 668 |
+
'content-length': res.headers['content-length'],
|
| 669 |
+
vary: res.headers.vary
|
| 670 |
+
}
|
| 671 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 672 |
+
'access-control-allow-origin': 'sample.com',
|
| 673 |
+
'access-control-allow-credentials': 'true',
|
| 674 |
+
'access-control-expose-headers': 'zoo, bar',
|
| 675 |
+
'access-control-allow-methods': 'GET',
|
| 676 |
+
'access-control-allow-headers': 'baz, foo',
|
| 677 |
+
'access-control-max-age': '321',
|
| 678 |
+
'content-length': '0',
|
| 679 |
+
vary: 'Origin'
|
| 680 |
+
})
|
| 681 |
+
|
| 682 |
+
res = await fastify.inject({
|
| 683 |
+
method: 'GET',
|
| 684 |
+
url: '/',
|
| 685 |
+
headers: {
|
| 686 |
+
'access-control-request-method': 'GET',
|
| 687 |
+
origin: 'example.com'
|
| 688 |
+
}
|
| 689 |
+
})
|
| 690 |
+
t.assert.ok(res)
|
| 691 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 692 |
+
})
|
| 693 |
+
|
| 694 |
+
test('Should support custom hook with dynamic config (Promise), but should error /1', async t => {
|
| 695 |
+
t.plan(6)
|
| 696 |
+
|
| 697 |
+
const fastify = Fastify()
|
| 698 |
+
const configDelegation = function () {
|
| 699 |
+
return false
|
| 700 |
+
}
|
| 701 |
+
|
| 702 |
+
await fastify.register(cors, {
|
| 703 |
+
hook: 'preParsing',
|
| 704 |
+
delegator: configDelegation
|
| 705 |
+
})
|
| 706 |
+
|
| 707 |
+
fastify.get('/', (_req, reply) => {
|
| 708 |
+
reply.send('ok')
|
| 709 |
+
})
|
| 710 |
+
|
| 711 |
+
let res = await fastify.inject({
|
| 712 |
+
method: 'OPTIONS',
|
| 713 |
+
url: '/',
|
| 714 |
+
headers: {
|
| 715 |
+
'access-control-request-method': 'GET',
|
| 716 |
+
origin: 'example.com'
|
| 717 |
+
}
|
| 718 |
+
})
|
| 719 |
+
t.assert.ok(res)
|
| 720 |
+
delete res.headers.date
|
| 721 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 722 |
+
t.assert.strictEqual(res.payload, '{"statusCode":500,"error":"Internal Server Error","message":"Invalid CORS origin option"}')
|
| 723 |
+
const actualHeaders = {
|
| 724 |
+
'content-length': res.headers['content-length']
|
| 725 |
+
}
|
| 726 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 727 |
+
'content-length': '89'
|
| 728 |
+
})
|
| 729 |
+
|
| 730 |
+
res = await fastify.inject({
|
| 731 |
+
method: 'GET',
|
| 732 |
+
url: '/',
|
| 733 |
+
headers: {
|
| 734 |
+
'access-control-request-method': 'GET',
|
| 735 |
+
origin: 'example.com'
|
| 736 |
+
}
|
| 737 |
+
})
|
| 738 |
+
t.assert.ok(res)
|
| 739 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 740 |
+
})
|
| 741 |
+
|
| 742 |
+
test('Should support custom hook with dynamic config (Promise), but should error /2', async t => {
|
| 743 |
+
t.plan(6)
|
| 744 |
+
|
| 745 |
+
const fastify = Fastify()
|
| 746 |
+
const configDelegation = function () {
|
| 747 |
+
return false
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
await fastify.register(cors, {
|
| 751 |
+
delegator: configDelegation
|
| 752 |
+
})
|
| 753 |
+
|
| 754 |
+
fastify.get('/', (_req, reply) => {
|
| 755 |
+
reply.send('ok')
|
| 756 |
+
})
|
| 757 |
+
|
| 758 |
+
let res = await fastify.inject({
|
| 759 |
+
method: 'OPTIONS',
|
| 760 |
+
url: '/',
|
| 761 |
+
headers: {
|
| 762 |
+
'access-control-request-method': 'GET',
|
| 763 |
+
origin: 'example.com'
|
| 764 |
+
}
|
| 765 |
+
})
|
| 766 |
+
t.assert.ok(res)
|
| 767 |
+
delete res.headers.date
|
| 768 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 769 |
+
t.assert.strictEqual(res.payload, '{"statusCode":500,"error":"Internal Server Error","message":"Invalid CORS origin option"}')
|
| 770 |
+
const actualHeaders = {
|
| 771 |
+
'content-length': res.headers['content-length']
|
| 772 |
+
}
|
| 773 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 774 |
+
'content-length': '89'
|
| 775 |
+
})
|
| 776 |
+
|
| 777 |
+
res = await fastify.inject({
|
| 778 |
+
method: 'GET',
|
| 779 |
+
url: '/',
|
| 780 |
+
headers: {
|
| 781 |
+
'access-control-request-method': 'GET',
|
| 782 |
+
origin: 'example.com'
|
| 783 |
+
}
|
| 784 |
+
})
|
| 785 |
+
t.assert.ok(res)
|
| 786 |
+
t.assert.strictEqual(res.statusCode, 500)
|
| 787 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/preflight.test.js
ADDED
|
@@ -0,0 +1,590 @@
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const { test } = require('node:test')
|
| 4 |
+
const Fastify = require('fastify')
|
| 5 |
+
const cors = require('../')
|
| 6 |
+
|
| 7 |
+
test('Should reply to preflight requests', async t => {
|
| 8 |
+
t.plan(4)
|
| 9 |
+
|
| 10 |
+
const fastify = Fastify()
|
| 11 |
+
await fastify.register(cors)
|
| 12 |
+
|
| 13 |
+
const res = await fastify.inject({
|
| 14 |
+
method: 'OPTIONS',
|
| 15 |
+
url: '/',
|
| 16 |
+
headers: {
|
| 17 |
+
'access-control-request-method': 'GET',
|
| 18 |
+
origin: 'example.com'
|
| 19 |
+
}
|
| 20 |
+
})
|
| 21 |
+
t.assert.ok(res)
|
| 22 |
+
delete res.headers.date
|
| 23 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 24 |
+
t.assert.strictEqual(res.payload, '')
|
| 25 |
+
const actualHeaders = {
|
| 26 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 27 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 28 |
+
vary: res.headers.vary,
|
| 29 |
+
'content-length': res.headers['content-length']
|
| 30 |
+
}
|
| 31 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 32 |
+
'access-control-allow-origin': '*',
|
| 33 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 34 |
+
vary: 'Access-Control-Request-Headers',
|
| 35 |
+
'content-length': '0'
|
| 36 |
+
})
|
| 37 |
+
})
|
| 38 |
+
|
| 39 |
+
test('Should add access-control-allow-headers to response if preflight req has access-control-request-headers', async t => {
|
| 40 |
+
t.plan(4)
|
| 41 |
+
|
| 42 |
+
const fastify = Fastify()
|
| 43 |
+
await fastify.register(cors)
|
| 44 |
+
|
| 45 |
+
const res = await fastify.inject({
|
| 46 |
+
method: 'OPTIONS',
|
| 47 |
+
url: '/',
|
| 48 |
+
headers: {
|
| 49 |
+
'access-control-request-headers': 'x-requested-with',
|
| 50 |
+
'access-control-request-method': 'GET',
|
| 51 |
+
origin: 'example.com'
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
})
|
| 55 |
+
t.assert.ok(res)
|
| 56 |
+
delete res.headers.date
|
| 57 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 58 |
+
t.assert.strictEqual(res.payload, '')
|
| 59 |
+
const actualHeaders = {
|
| 60 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 61 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 62 |
+
'access-control-allow-headers': res.headers['access-control-allow-headers'],
|
| 63 |
+
vary: res.headers.vary,
|
| 64 |
+
'content-length': res.headers['content-length']
|
| 65 |
+
}
|
| 66 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 67 |
+
'access-control-allow-origin': '*',
|
| 68 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 69 |
+
'access-control-allow-headers': 'x-requested-with',
|
| 70 |
+
vary: 'Access-Control-Request-Headers',
|
| 71 |
+
'content-length': '0'
|
| 72 |
+
})
|
| 73 |
+
})
|
| 74 |
+
|
| 75 |
+
test('Should reply to preflight requests with custom status code', async t => {
|
| 76 |
+
t.plan(4)
|
| 77 |
+
|
| 78 |
+
const fastify = Fastify()
|
| 79 |
+
await fastify.register(cors, { optionsSuccessStatus: 200 })
|
| 80 |
+
|
| 81 |
+
const res = await fastify.inject({
|
| 82 |
+
method: 'OPTIONS',
|
| 83 |
+
url: '/',
|
| 84 |
+
headers: {
|
| 85 |
+
'access-control-request-method': 'GET',
|
| 86 |
+
origin: 'example.com'
|
| 87 |
+
}
|
| 88 |
+
})
|
| 89 |
+
t.assert.ok(res)
|
| 90 |
+
delete res.headers.date
|
| 91 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 92 |
+
t.assert.strictEqual(res.payload, '')
|
| 93 |
+
const actualHeaders = {
|
| 94 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 95 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 96 |
+
vary: res.headers.vary,
|
| 97 |
+
'content-length': res.headers['content-length']
|
| 98 |
+
}
|
| 99 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 100 |
+
'access-control-allow-origin': '*',
|
| 101 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 102 |
+
vary: 'Access-Control-Request-Headers',
|
| 103 |
+
'content-length': '0'
|
| 104 |
+
})
|
| 105 |
+
})
|
| 106 |
+
|
| 107 |
+
test('Should be able to override preflight response with a route', async t => {
|
| 108 |
+
t.plan(5)
|
| 109 |
+
|
| 110 |
+
const fastify = Fastify()
|
| 111 |
+
await fastify.register(cors, { preflightContinue: true })
|
| 112 |
+
|
| 113 |
+
fastify.options('/', (_req, reply) => {
|
| 114 |
+
reply.send('ok')
|
| 115 |
+
})
|
| 116 |
+
|
| 117 |
+
const res = await fastify.inject({
|
| 118 |
+
method: 'OPTIONS',
|
| 119 |
+
url: '/',
|
| 120 |
+
headers: {
|
| 121 |
+
'access-control-request-method': 'GET',
|
| 122 |
+
origin: 'example.com'
|
| 123 |
+
}
|
| 124 |
+
})
|
| 125 |
+
t.assert.ok(res)
|
| 126 |
+
delete res.headers.date
|
| 127 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 128 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 129 |
+
const actualHeaders = {
|
| 130 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin']
|
| 131 |
+
}
|
| 132 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 133 |
+
// Only the base cors headers and no preflight headers
|
| 134 |
+
'access-control-allow-origin': '*'
|
| 135 |
+
})
|
| 136 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 137 |
+
})
|
| 138 |
+
|
| 139 |
+
test('Should reply to all options requests', async t => {
|
| 140 |
+
t.plan(4)
|
| 141 |
+
|
| 142 |
+
const fastify = Fastify()
|
| 143 |
+
await fastify.register(cors)
|
| 144 |
+
|
| 145 |
+
const res = await fastify.inject({
|
| 146 |
+
method: 'OPTIONS',
|
| 147 |
+
url: '/hello',
|
| 148 |
+
headers: {
|
| 149 |
+
'access-control-request-method': 'GET',
|
| 150 |
+
origin: 'example.com'
|
| 151 |
+
}
|
| 152 |
+
})
|
| 153 |
+
t.assert.ok(res)
|
| 154 |
+
delete res.headers.date
|
| 155 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 156 |
+
t.assert.strictEqual(res.payload, '')
|
| 157 |
+
const actualHeaders = {
|
| 158 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 159 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 160 |
+
vary: res.headers.vary,
|
| 161 |
+
'content-length': res.headers['content-length']
|
| 162 |
+
}
|
| 163 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 164 |
+
'access-control-allow-origin': '*',
|
| 165 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 166 |
+
vary: 'Access-Control-Request-Headers',
|
| 167 |
+
'content-length': '0'
|
| 168 |
+
})
|
| 169 |
+
})
|
| 170 |
+
|
| 171 |
+
test('Should support a prefix for preflight requests', async t => {
|
| 172 |
+
t.plan(6)
|
| 173 |
+
|
| 174 |
+
const fastify = Fastify()
|
| 175 |
+
await fastify.register((instance, _opts, next) => {
|
| 176 |
+
instance.register(cors)
|
| 177 |
+
next()
|
| 178 |
+
}, { prefix: '/subsystem' })
|
| 179 |
+
|
| 180 |
+
let res = await fastify.inject({
|
| 181 |
+
method: 'OPTIONS',
|
| 182 |
+
url: '/hello'
|
| 183 |
+
})
|
| 184 |
+
t.assert.ok(res)
|
| 185 |
+
t.assert.strictEqual(res.statusCode, 404)
|
| 186 |
+
|
| 187 |
+
res = await fastify.inject({
|
| 188 |
+
method: 'OPTIONS',
|
| 189 |
+
url: '/subsystem/hello',
|
| 190 |
+
headers: {
|
| 191 |
+
'access-control-request-method': 'GET',
|
| 192 |
+
origin: 'example.com'
|
| 193 |
+
}
|
| 194 |
+
})
|
| 195 |
+
t.assert.ok(res)
|
| 196 |
+
delete res.headers.date
|
| 197 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 198 |
+
t.assert.strictEqual(res.payload, '')
|
| 199 |
+
const actualHeaders = {
|
| 200 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 201 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 202 |
+
vary: res.headers.vary,
|
| 203 |
+
'content-length': res.headers['content-length']
|
| 204 |
+
}
|
| 205 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 206 |
+
'access-control-allow-origin': '*',
|
| 207 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 208 |
+
vary: 'Access-Control-Request-Headers',
|
| 209 |
+
'content-length': '0'
|
| 210 |
+
})
|
| 211 |
+
})
|
| 212 |
+
|
| 213 |
+
test('hide options route by default', async t => {
|
| 214 |
+
t.plan(2)
|
| 215 |
+
|
| 216 |
+
const fastify = Fastify()
|
| 217 |
+
|
| 218 |
+
fastify.addHook('onRoute', (route) => {
|
| 219 |
+
if (route.method === 'OPTIONS' && route.url === '*') {
|
| 220 |
+
t.assert.strictEqual(route.schema.hide, true)
|
| 221 |
+
}
|
| 222 |
+
})
|
| 223 |
+
await fastify.register(cors)
|
| 224 |
+
|
| 225 |
+
const ready = await fastify.ready()
|
| 226 |
+
t.assert.ok(ready)
|
| 227 |
+
})
|
| 228 |
+
|
| 229 |
+
test('show options route', async t => {
|
| 230 |
+
t.plan(2)
|
| 231 |
+
|
| 232 |
+
const fastify = Fastify()
|
| 233 |
+
|
| 234 |
+
fastify.addHook('onRoute', (route) => {
|
| 235 |
+
if (route.method === 'OPTIONS' && route.url === '*') {
|
| 236 |
+
t.assert.strictEqual(route.schema.hide, false)
|
| 237 |
+
}
|
| 238 |
+
})
|
| 239 |
+
await fastify.register(cors, { hideOptionsRoute: false })
|
| 240 |
+
|
| 241 |
+
const ready = await fastify.ready()
|
| 242 |
+
t.assert.ok(ready)
|
| 243 |
+
})
|
| 244 |
+
|
| 245 |
+
test('Allow only request from with specific methods', async t => {
|
| 246 |
+
t.plan(4)
|
| 247 |
+
|
| 248 |
+
const fastify = Fastify()
|
| 249 |
+
await fastify.register(cors, { methods: ['GET', 'POST'] })
|
| 250 |
+
|
| 251 |
+
const res = await fastify.inject({
|
| 252 |
+
method: 'OPTIONS',
|
| 253 |
+
url: '/',
|
| 254 |
+
headers: {
|
| 255 |
+
'access-control-request-method': 'GET',
|
| 256 |
+
origin: 'example.com'
|
| 257 |
+
}
|
| 258 |
+
})
|
| 259 |
+
t.assert.ok(res)
|
| 260 |
+
delete res.headers.date
|
| 261 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 262 |
+
const actualHeaders = {
|
| 263 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods']
|
| 264 |
+
}
|
| 265 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 266 |
+
'access-control-allow-methods': 'GET, POST'
|
| 267 |
+
})
|
| 268 |
+
t.assert.notStrictEqual(res.headers.vary, 'Origin')
|
| 269 |
+
})
|
| 270 |
+
|
| 271 |
+
test('Should reply with 400 error to OPTIONS requests missing origin header when default strictPreflight', async t => {
|
| 272 |
+
t.plan(3)
|
| 273 |
+
|
| 274 |
+
const fastify = Fastify()
|
| 275 |
+
await fastify.register(cors)
|
| 276 |
+
|
| 277 |
+
const res = await fastify.inject({
|
| 278 |
+
method: 'OPTIONS',
|
| 279 |
+
url: '/',
|
| 280 |
+
headers: {
|
| 281 |
+
'access-control-request-method': 'GET'
|
| 282 |
+
}
|
| 283 |
+
})
|
| 284 |
+
t.assert.ok(res)
|
| 285 |
+
t.assert.strictEqual(res.statusCode, 400)
|
| 286 |
+
t.assert.strictEqual(res.payload, 'Invalid Preflight Request')
|
| 287 |
+
})
|
| 288 |
+
|
| 289 |
+
test('Should reply with 400 to OPTIONS requests when missing Access-Control-Request-Method header when default strictPreflight', async t => {
|
| 290 |
+
t.plan(3)
|
| 291 |
+
|
| 292 |
+
const fastify = Fastify()
|
| 293 |
+
await fastify.register(cors, {
|
| 294 |
+
strictPreflight: true
|
| 295 |
+
})
|
| 296 |
+
|
| 297 |
+
const res = await fastify.inject({
|
| 298 |
+
method: 'OPTIONS',
|
| 299 |
+
url: '/',
|
| 300 |
+
headers: {
|
| 301 |
+
origin: 'example.com'
|
| 302 |
+
}
|
| 303 |
+
})
|
| 304 |
+
t.assert.ok(res)
|
| 305 |
+
t.assert.strictEqual(res.statusCode, 400)
|
| 306 |
+
t.assert.strictEqual(res.payload, 'Invalid Preflight Request')
|
| 307 |
+
})
|
| 308 |
+
|
| 309 |
+
test('Should reply to all preflight requests when strictPreflight is disabled', async t => {
|
| 310 |
+
t.plan(4)
|
| 311 |
+
|
| 312 |
+
const fastify = Fastify()
|
| 313 |
+
await fastify.register(cors, { strictPreflight: false })
|
| 314 |
+
|
| 315 |
+
const res = await fastify.inject({
|
| 316 |
+
method: 'OPTIONS',
|
| 317 |
+
url: '/'
|
| 318 |
+
// No access-control-request-method or origin headers
|
| 319 |
+
})
|
| 320 |
+
t.assert.ok(res)
|
| 321 |
+
delete res.headers.date
|
| 322 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 323 |
+
t.assert.strictEqual(res.payload, '')
|
| 324 |
+
const actualHeaders = {
|
| 325 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 326 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 327 |
+
vary: res.headers.vary,
|
| 328 |
+
'content-length': res.headers['content-length']
|
| 329 |
+
}
|
| 330 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 331 |
+
'access-control-allow-origin': '*',
|
| 332 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 333 |
+
vary: 'Access-Control-Request-Headers',
|
| 334 |
+
'content-length': '0'
|
| 335 |
+
})
|
| 336 |
+
})
|
| 337 |
+
|
| 338 |
+
test('Default empty 200 response with preflightContinue on OPTIONS routes', async t => {
|
| 339 |
+
t.plan(4)
|
| 340 |
+
|
| 341 |
+
const fastify = Fastify()
|
| 342 |
+
await fastify.register(cors, { preflightContinue: true })
|
| 343 |
+
|
| 344 |
+
const res = await fastify.inject({
|
| 345 |
+
method: 'OPTIONS',
|
| 346 |
+
url: '/doesnotexist',
|
| 347 |
+
headers: {
|
| 348 |
+
'access-control-request-method': 'GET',
|
| 349 |
+
origin: 'example.com'
|
| 350 |
+
}
|
| 351 |
+
})
|
| 352 |
+
t.assert.ok(res)
|
| 353 |
+
delete res.headers.date
|
| 354 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 355 |
+
t.assert.strictEqual(res.payload, '')
|
| 356 |
+
const actualHeaders = {
|
| 357 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 358 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 359 |
+
vary: res.headers.vary
|
| 360 |
+
}
|
| 361 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 362 |
+
'access-control-allow-origin': '*',
|
| 363 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 364 |
+
vary: 'Access-Control-Request-Headers'
|
| 365 |
+
})
|
| 366 |
+
})
|
| 367 |
+
|
| 368 |
+
test('Can override preflight response with preflightContinue', async t => {
|
| 369 |
+
t.plan(4)
|
| 370 |
+
|
| 371 |
+
const fastify = Fastify()
|
| 372 |
+
await fastify.register(cors, { preflightContinue: true })
|
| 373 |
+
|
| 374 |
+
fastify.options('/', (_req, reply) => {
|
| 375 |
+
reply.send('ok')
|
| 376 |
+
})
|
| 377 |
+
|
| 378 |
+
const res = await fastify.inject({
|
| 379 |
+
method: 'OPTIONS',
|
| 380 |
+
url: '/',
|
| 381 |
+
headers: {
|
| 382 |
+
'access-control-request-method': 'GET',
|
| 383 |
+
origin: 'example.com'
|
| 384 |
+
}
|
| 385 |
+
})
|
| 386 |
+
t.assert.ok(res)
|
| 387 |
+
delete res.headers.date
|
| 388 |
+
t.assert.strictEqual(res.statusCode, 200)
|
| 389 |
+
t.assert.strictEqual(res.payload, 'ok')
|
| 390 |
+
const actualHeaders = {
|
| 391 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 392 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 393 |
+
vary: res.headers.vary
|
| 394 |
+
}
|
| 395 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 396 |
+
'access-control-allow-origin': '*',
|
| 397 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 398 |
+
vary: 'Access-Control-Request-Headers'
|
| 399 |
+
})
|
| 400 |
+
})
|
| 401 |
+
|
| 402 |
+
test('Should support ongoing prefix ', async t => {
|
| 403 |
+
t.plan(12)
|
| 404 |
+
|
| 405 |
+
const fastify = Fastify()
|
| 406 |
+
|
| 407 |
+
await fastify.register(async (instance) => {
|
| 408 |
+
instance.register(cors)
|
| 409 |
+
}, { prefix: '/prefix' })
|
| 410 |
+
|
| 411 |
+
// support prefixed route
|
| 412 |
+
let res = await fastify.inject({
|
| 413 |
+
method: 'OPTIONS',
|
| 414 |
+
url: '/prefix',
|
| 415 |
+
headers: {
|
| 416 |
+
'access-control-request-method': 'GET',
|
| 417 |
+
origin: 'example.com'
|
| 418 |
+
}
|
| 419 |
+
})
|
| 420 |
+
t.assert.ok(res)
|
| 421 |
+
delete res.headers.date
|
| 422 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 423 |
+
t.assert.strictEqual(res.payload, '')
|
| 424 |
+
let actualHeaders = {
|
| 425 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 426 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 427 |
+
vary: res.headers.vary,
|
| 428 |
+
'content-length': res.headers['content-length']
|
| 429 |
+
}
|
| 430 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 431 |
+
'access-control-allow-origin': '*',
|
| 432 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 433 |
+
vary: 'Access-Control-Request-Headers',
|
| 434 |
+
'content-length': '0'
|
| 435 |
+
})
|
| 436 |
+
|
| 437 |
+
// support prefixed route without / continue
|
| 438 |
+
res = await fastify.inject({
|
| 439 |
+
method: 'OPTIONS',
|
| 440 |
+
url: '/prefixfoo',
|
| 441 |
+
headers: {
|
| 442 |
+
'access-control-request-method': 'GET',
|
| 443 |
+
origin: 'example.com'
|
| 444 |
+
}
|
| 445 |
+
})
|
| 446 |
+
t.assert.ok(res)
|
| 447 |
+
delete res.headers.date
|
| 448 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 449 |
+
t.assert.strictEqual(res.payload, '')
|
| 450 |
+
actualHeaders = {
|
| 451 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 452 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 453 |
+
vary: res.headers.vary,
|
| 454 |
+
'content-length': res.headers['content-length']
|
| 455 |
+
}
|
| 456 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 457 |
+
'access-control-allow-origin': '*',
|
| 458 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 459 |
+
vary: 'Access-Control-Request-Headers',
|
| 460 |
+
'content-length': '0'
|
| 461 |
+
})
|
| 462 |
+
|
| 463 |
+
// support prefixed route with / continue
|
| 464 |
+
res = await fastify.inject({
|
| 465 |
+
method: 'OPTIONS',
|
| 466 |
+
url: '/prefix/foo',
|
| 467 |
+
headers: {
|
| 468 |
+
'access-control-request-method': 'GET',
|
| 469 |
+
origin: 'example.com'
|
| 470 |
+
}
|
| 471 |
+
})
|
| 472 |
+
t.assert.ok(res)
|
| 473 |
+
delete res.headers.date
|
| 474 |
+
t.assert.strictEqual(res.statusCode, 204)
|
| 475 |
+
t.assert.strictEqual(res.payload, '')
|
| 476 |
+
actualHeaders = {
|
| 477 |
+
'access-control-allow-origin': res.headers['access-control-allow-origin'],
|
| 478 |
+
'access-control-allow-methods': res.headers['access-control-allow-methods'],
|
| 479 |
+
vary: res.headers.vary,
|
| 480 |
+
'content-length': res.headers['content-length']
|
| 481 |
+
}
|
| 482 |
+
t.assert.deepStrictEqual(actualHeaders, {
|
| 483 |
+
'access-control-allow-origin': '*',
|
| 484 |
+
'access-control-allow-methods': 'GET,HEAD,POST',
|
| 485 |
+
vary: 'Access-Control-Request-Headers',
|
| 486 |
+
'content-length': '0'
|
| 487 |
+
})
|
| 488 |
+
})
|
| 489 |
+
|
| 490 |
+
test('Silences preflight logs when logLevel is "silent"', async t => {
|
| 491 |
+
const logs = []
|
| 492 |
+
const fastify = Fastify({
|
| 493 |
+
logger: {
|
| 494 |
+
level: 'info',
|
| 495 |
+
stream: {
|
| 496 |
+
write (line) {
|
| 497 |
+
try {
|
| 498 |
+
logs.push(JSON.parse(line))
|
| 499 |
+
} catch {
|
| 500 |
+
}
|
| 501 |
+
}
|
| 502 |
+
}
|
| 503 |
+
}
|
| 504 |
+
})
|
| 505 |
+
|
| 506 |
+
await fastify.register(cors, { logLevel: 'silent' })
|
| 507 |
+
|
| 508 |
+
fastify.get('/', async () => ({ ok: true }))
|
| 509 |
+
|
| 510 |
+
await fastify.ready()
|
| 511 |
+
t.assert.ok(fastify)
|
| 512 |
+
|
| 513 |
+
await fastify.inject({
|
| 514 |
+
method: 'OPTIONS',
|
| 515 |
+
url: '/',
|
| 516 |
+
headers: {
|
| 517 |
+
'access-control-request-method': 'GET',
|
| 518 |
+
origin: 'https://example.com'
|
| 519 |
+
}
|
| 520 |
+
})
|
| 521 |
+
|
| 522 |
+
await fastify.inject({ method: 'GET', url: '/' })
|
| 523 |
+
|
| 524 |
+
const hasOptionsLog = logs.some(l => l.req && l.req.method === 'OPTIONS')
|
| 525 |
+
const hasGetLog = logs.some(l => l.req && l.req.method === 'GET')
|
| 526 |
+
|
| 527 |
+
t.assert.strictEqual(hasOptionsLog, false)
|
| 528 |
+
t.assert.strictEqual(hasGetLog, true)
|
| 529 |
+
|
| 530 |
+
await fastify.close()
|
| 531 |
+
})
|
| 532 |
+
test('delegator + logLevel:"silent" → OPTIONS logs are suppressed', async t => {
|
| 533 |
+
t.plan(3)
|
| 534 |
+
|
| 535 |
+
const logs = []
|
| 536 |
+
const app = Fastify({
|
| 537 |
+
logger: {
|
| 538 |
+
level: 'info',
|
| 539 |
+
stream: { write: l => { try { logs.push(JSON.parse(l)) } catch {} } }
|
| 540 |
+
}
|
| 541 |
+
})
|
| 542 |
+
|
| 543 |
+
await app.register(cors, {
|
| 544 |
+
delegator: () => ({ origin: '*' }),
|
| 545 |
+
logLevel: 'silent'
|
| 546 |
+
})
|
| 547 |
+
|
| 548 |
+
app.get('/', () => ({ ok: true }))
|
| 549 |
+
await app.ready()
|
| 550 |
+
t.assert.ok(app)
|
| 551 |
+
|
| 552 |
+
await app.inject({
|
| 553 |
+
method: 'OPTIONS',
|
| 554 |
+
url: '/',
|
| 555 |
+
headers: {
|
| 556 |
+
'access-control-request-method': 'GET',
|
| 557 |
+
origin: 'https://example.com'
|
| 558 |
+
}
|
| 559 |
+
})
|
| 560 |
+
|
| 561 |
+
await app.inject({ method: 'GET', url: '/' })
|
| 562 |
+
|
| 563 |
+
const hasOptionsLog = logs.some(l => l.req?.method === 'OPTIONS')
|
| 564 |
+
const hasGetLog = logs.some(l => l.req?.method === 'GET')
|
| 565 |
+
|
| 566 |
+
t.assert.strictEqual(hasOptionsLog, false)
|
| 567 |
+
t.assert.strictEqual(hasGetLog, true)
|
| 568 |
+
|
| 569 |
+
await app.close()
|
| 570 |
+
})
|
| 571 |
+
test('delegator + hideOptionsRoute:false → OPTIONS route is visible', async t => {
|
| 572 |
+
t.plan(2)
|
| 573 |
+
|
| 574 |
+
const app = Fastify()
|
| 575 |
+
|
| 576 |
+
app.addHook('onRoute', route => {
|
| 577 |
+
if (route.method === 'OPTIONS' && route.url === '*') {
|
| 578 |
+
t.assert.strictEqual(route.schema.hide, false)
|
| 579 |
+
}
|
| 580 |
+
})
|
| 581 |
+
|
| 582 |
+
await app.register(cors, {
|
| 583 |
+
delegator: () => ({ origin: '*' }),
|
| 584 |
+
hideOptionsRoute: false
|
| 585 |
+
})
|
| 586 |
+
|
| 587 |
+
await app.ready()
|
| 588 |
+
t.assert.ok(app)
|
| 589 |
+
await app.close()
|
| 590 |
+
})
|
novas/novacore-zephyr/claude-code-router/node_modules/@fastify/cors/test/vary.test.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use strict'
|
| 2 |
+
|
| 3 |
+
const { test } = require('node:test')
|
| 4 |
+
const { createAddFieldnameToVary } = require('../vary')
|
| 5 |
+
const { parse } = require('../vary')
|
| 6 |
+
|
| 7 |
+
test('Should set * even if we set a specific field', async t => {
|
| 8 |
+
t.plan(1)
|
| 9 |
+
|
| 10 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 11 |
+
const replyMock = {
|
| 12 |
+
getHeader () {
|
| 13 |
+
return '*'
|
| 14 |
+
},
|
| 15 |
+
header () {
|
| 16 |
+
t.fail('Should not be here')
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
addOriginToVary(replyMock)
|
| 21 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
test('Should set * even if we set a specific field', t => {
|
| 25 |
+
t.plan(2)
|
| 26 |
+
|
| 27 |
+
const addWildcardToVary = createAddFieldnameToVary('*')
|
| 28 |
+
const replyMock = {
|
| 29 |
+
getHeader () {
|
| 30 |
+
return 'Origin'
|
| 31 |
+
},
|
| 32 |
+
header (name, value) {
|
| 33 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 34 |
+
t.assert.deepStrictEqual(value, '*')
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
addWildcardToVary(replyMock)
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
test('Should set * when field contains a *', t => {
|
| 42 |
+
t.plan(3)
|
| 43 |
+
|
| 44 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 45 |
+
const replyMock = {
|
| 46 |
+
getHeader () {
|
| 47 |
+
return ['Origin', '*', 'Access-Control-Request-Headers']
|
| 48 |
+
},
|
| 49 |
+
header (name, value) {
|
| 50 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 51 |
+
t.assert.deepStrictEqual(value, '*')
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
addOriginToVary(replyMock)
|
| 56 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
test('Should concat vary values', t => {
|
| 60 |
+
t.plan(3)
|
| 61 |
+
|
| 62 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 63 |
+
const replyMock = {
|
| 64 |
+
getHeader () {
|
| 65 |
+
return 'Access-Control-Request-Headers'
|
| 66 |
+
},
|
| 67 |
+
header (name, value) {
|
| 68 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 69 |
+
t.assert.deepStrictEqual(value, 'Access-Control-Request-Headers, Origin')
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
addOriginToVary(replyMock)
|
| 74 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
test('Should concat vary values ignoring consecutive commas', t => {
|
| 78 |
+
t.plan(3)
|
| 79 |
+
|
| 80 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 81 |
+
const replyMock = {
|
| 82 |
+
getHeader () {
|
| 83 |
+
return ' Access-Control-Request-Headers,Access-Control-Request-Method'
|
| 84 |
+
},
|
| 85 |
+
header (name, value) {
|
| 86 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 87 |
+
t.assert.deepStrictEqual(value, ' Access-Control-Request-Headers,Access-Control-Request-Method, Origin')
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
addOriginToVary(replyMock)
|
| 92 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
test('Should concat vary values ignoring whitespace', t => {
|
| 96 |
+
t.plan(3)
|
| 97 |
+
|
| 98 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 99 |
+
const replyMock = {
|
| 100 |
+
getHeader () {
|
| 101 |
+
return ' Access-Control-Request-Headers ,Access-Control-Request-Method'
|
| 102 |
+
},
|
| 103 |
+
header (name, value) {
|
| 104 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 105 |
+
t.assert.deepStrictEqual(value, ' Access-Control-Request-Headers ,Access-Control-Request-Method, Origin')
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
addOriginToVary(replyMock)
|
| 110 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
test('Should set the field as value for vary if no vary is defined', t => {
|
| 114 |
+
t.plan(2)
|
| 115 |
+
|
| 116 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 117 |
+
const replyMock = {
|
| 118 |
+
getHeader () {
|
| 119 |
+
return undefined
|
| 120 |
+
},
|
| 121 |
+
header (name, value) {
|
| 122 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 123 |
+
t.assert.deepStrictEqual(value, 'Origin')
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
addOriginToVary(replyMock)
|
| 128 |
+
})
|
| 129 |
+
|
| 130 |
+
test('Should set * as value for vary if vary contains *', t => {
|
| 131 |
+
t.plan(2)
|
| 132 |
+
|
| 133 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 134 |
+
const replyMock = {
|
| 135 |
+
getHeader () {
|
| 136 |
+
return 'Accept,*'
|
| 137 |
+
},
|
| 138 |
+
header (name, value) {
|
| 139 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 140 |
+
t.assert.deepStrictEqual(value, '*')
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
addOriginToVary(replyMock)
|
| 145 |
+
})
|
| 146 |
+
|
| 147 |
+
test('Should set Accept-Encoding as value for vary if vary is empty string', t => {
|
| 148 |
+
t.plan(2)
|
| 149 |
+
|
| 150 |
+
const addAcceptEncodingToVary = createAddFieldnameToVary('Accept-Encoding')
|
| 151 |
+
const replyMock = {
|
| 152 |
+
getHeader () {
|
| 153 |
+
return ''
|
| 154 |
+
},
|
| 155 |
+
header (name, value) {
|
| 156 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 157 |
+
t.assert.deepStrictEqual(value, 'Accept-Encoding')
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
addAcceptEncodingToVary(replyMock)
|
| 162 |
+
})
|
| 163 |
+
|
| 164 |
+
test('Should have no issues with values containing dashes', t => {
|
| 165 |
+
t.plan(2)
|
| 166 |
+
|
| 167 |
+
const addXFooToVary = createAddFieldnameToVary('X-Foo')
|
| 168 |
+
const replyMock = {
|
| 169 |
+
value: 'Accept-Encoding',
|
| 170 |
+
getHeader () {
|
| 171 |
+
return this.value
|
| 172 |
+
},
|
| 173 |
+
header (name, value) {
|
| 174 |
+
t.assert.deepStrictEqual(name, 'Vary')
|
| 175 |
+
t.assert.deepStrictEqual(value, 'Accept-Encoding, X-Foo')
|
| 176 |
+
this.value = value
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
addXFooToVary(replyMock)
|
| 181 |
+
addXFooToVary(replyMock)
|
| 182 |
+
})
|
| 183 |
+
|
| 184 |
+
test('Should ignore the header as value for vary if it is already in vary', t => {
|
| 185 |
+
t.plan(1)
|
| 186 |
+
|
| 187 |
+
const addOriginToVary = createAddFieldnameToVary('Origin')
|
| 188 |
+
const replyMock = {
|
| 189 |
+
getHeader () {
|
| 190 |
+
return 'Origin'
|
| 191 |
+
},
|
| 192 |
+
header () {
|
| 193 |
+
t.fail('Should not be here')
|
| 194 |
+
}
|
| 195 |
+
}
|
| 196 |
+
addOriginToVary(replyMock)
|
| 197 |
+
addOriginToVary(replyMock)
|
| 198 |
+
|
| 199 |
+
t.assert.ok(true) // equalivant to tap t.pass()
|
| 200 |
+
})
|
| 201 |
+
|
| 202 |
+
test('parse', t => {
|
| 203 |
+
t.plan(18)
|
| 204 |
+
t.assert.deepStrictEqual(parse(''), [])
|
| 205 |
+
t.assert.deepStrictEqual(parse('a'), ['a'])
|
| 206 |
+
t.assert.deepStrictEqual(parse('a,b'), ['a', 'b'])
|
| 207 |
+
t.assert.deepStrictEqual(parse(' a,b'), ['a', 'b'])
|
| 208 |
+
t.assert.deepStrictEqual(parse('a,b '), ['a', 'b'])
|
| 209 |
+
t.assert.deepStrictEqual(parse('a,b,c'), ['a', 'b', 'c'])
|
| 210 |
+
t.assert.deepStrictEqual(parse('A,b,c'), ['a', 'b', 'c'])
|
| 211 |
+
t.assert.deepStrictEqual(parse('a,b,c,'), ['a', 'b', 'c'])
|
| 212 |
+
t.assert.deepStrictEqual(parse('a,b,c, '), ['a', 'b', 'c'])
|
| 213 |
+
t.assert.deepStrictEqual(parse(',a,b,c'), ['a', 'b', 'c'])
|
| 214 |
+
t.assert.deepStrictEqual(parse(' ,a,b,c'), ['a', 'b', 'c'])
|
| 215 |
+
t.assert.deepStrictEqual(parse('a,,b,c'), ['a', 'b', 'c'])
|
| 216 |
+
t.assert.deepStrictEqual(parse('a,,,b,,c'), ['a', 'b', 'c'])
|
| 217 |
+
t.assert.deepStrictEqual(parse('a, b,c'), ['a', 'b', 'c'])
|
| 218 |
+
t.assert.deepStrictEqual(parse('a, b,c'), ['a', 'b', 'c'])
|
| 219 |
+
t.assert.deepStrictEqual(parse('a, , b,c'), ['a', 'b', 'c'])
|
| 220 |
+
t.assert.deepStrictEqual(parse('a, , b,c'), ['a', 'b', 'c'])
|
| 221 |
+
|
| 222 |
+
// one for the cache
|
| 223 |
+
t.assert.deepStrictEqual(parse('A,b,c'), ['a', 'b', 'c'])
|
| 224 |
+
})
|
| 225 |
+
|
| 226 |
+
test('createAddFieldnameToVary', async t => {
|
| 227 |
+
t.plan(4)
|
| 228 |
+
t.assert.strictEqual(typeof createAddFieldnameToVary('valid-header'), 'function')
|
| 229 |
+
await t.assert.rejects(
|
| 230 |
+
async () => createAddFieldnameToVary('invalid:header'),
|
| 231 |
+
(err) => {
|
| 232 |
+
t.assert.strictEqual(err.name, 'TypeError')
|
| 233 |
+
t.assert.strictEqual(err.message, 'Fieldname contains invalid characters.')
|
| 234 |
+
return true
|
| 235 |
+
}
|
| 236 |
+
)
|
| 237 |
+
})
|