Skip to content

Commit

Permalink
8.11.2 Allow all methods Node.js supports on CORS as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Feb 23, 2025
1 parent 37d5fe2 commit d245225
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ want a `Content-Type` header in the response.
<details>
<summary>Supported Methods</summary>
<p>From Node.js <code>http.METHODS</code></p>
<p>From <code>require('node:http').METHODS</code></p>
<p>
ACL, BIND, CHECKOUT,
CONNECT, COPY, DELETE,
Expand Down Expand Up @@ -473,7 +473,7 @@ function capitalizePlugin(filePath) {
Defaults to `true`. When `true`, these are the default options:
```js
config.corsOrigins = ['*']
config.corsMethods = ['GET', 'PUT', 'DELETE', 'POST', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT']
config.corsMethods = require('node:http').METHODS
config.corsHeaders = ['content-type']
config.corsCredentials = true
config.corsMaxAge = 0 // seconds to cache the preflight req
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mockaton",
"description": "A deterministic server-side for developing and testing frontend clients",
"type": "module",
"version": "8.11.1",
"version": "8.11.2",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/Filename.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const httpMethods = [ // node:http.METHODS
const httpMethods = [ // @KeepSync with node:http.METHODS
'ACL', 'BIND', 'CHECKOUT',
'CONNECT', 'COPY', 'DELETE',
'GET', 'HEAD', 'LINK',
Expand Down
4 changes: 2 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { realpathSync } from 'node:fs'
import { isDirectory } from './utils/fs.js'
import { openInBrowser } from './utils/openInBrowser.js'
import { jsToJsonPlugin } from './MockDispatcherPlugins.js'
import { StandardMethods } from './utils/http-request.js'
import { SUPPORTED_METHODS } from './utils/http-request.js'
import { validateCorsAllowedMethods, validateCorsAllowedOrigins } from './utils/http-cors.js'


Expand All @@ -28,7 +28,7 @@ export const config = Object.seal({

corsAllowed: true,
corsOrigins: ['*'],
corsMethods: StandardMethods,
corsMethods: SUPPORTED_METHODS,
corsHeaders: ['content-type'],
corsExposedHeaders: [],
corsCredentials: true,
Expand Down
9 changes: 3 additions & 6 deletions src/utils/http-cors.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { StandardMethods } from './http-request.js'

import { methodIsSupported } from './http-request.js'

/* https://www.w3.org/TR/2020/SPSD-cors-20200602/#resource-processing-model */


export function validateCorsAllowedOrigins(arr) {
if (!Array.isArray(arr))
return false
Expand All @@ -13,8 +11,7 @@ export function validateCorsAllowedOrigins(arr) {
}

export function validateCorsAllowedMethods(arr) {
return Array.isArray(arr)
&& arr.every(m => StandardMethods.includes(m))
return Array.isArray(arr) && arr.every(methodIsSupported)
}


Expand All @@ -38,7 +35,7 @@ const CH = CorsHeader
export function isPreflight(req) {
return req.method === 'OPTIONS'
&& URL.canParse(req.headers[CH.Origin])
&& StandardMethods.includes(req.headers[CH.AccessControlRequestMethod])
&& methodIsSupported(req.headers[CH.AccessControlRequestMethod])
}


Expand Down
9 changes: 5 additions & 4 deletions src/utils/http-request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export const StandardMethods = [
'GET', 'PUT', 'DELETE', 'POST', 'PATCH',
'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'
]
import { METHODS } from 'node:http'


export const SUPPORTED_METHODS = METHODS
export const methodIsSupported = method => SUPPORTED_METHODS.includes(method)

export class BodyReaderError extends Error {name = 'BodyReaderError'}

Expand Down

0 comments on commit d245225

Please sign in to comment.