@jaypie/express 1.1.6 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.cjs +54 -43
- package/dist/module.esm.js +54 -43
- package/package.json +2 -2
- package/src/cors.helper.js +54 -42
package/dist/module.cjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var errors = require('@jaypie/errors');
|
|
4
|
-
var expressCors = require('cors');
|
|
5
4
|
var core = require('@jaypie/core');
|
|
5
|
+
var expressCors = require('cors');
|
|
6
6
|
var serverlessExpress = require('@codegenie/serverless-express');
|
|
7
7
|
|
|
8
8
|
//
|
|
@@ -23,8 +23,6 @@ const EXPRESS = {
|
|
|
23
23
|
// Constants
|
|
24
24
|
//
|
|
25
25
|
|
|
26
|
-
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
27
|
-
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
28
26
|
const HTTP_PROTOCOL = "http://";
|
|
29
27
|
const HTTPS_PROTOCOL = "https://";
|
|
30
28
|
const SANDBOX_ENV = "sandbox";
|
|
@@ -41,54 +39,67 @@ const ensureProtocol = (url) => {
|
|
|
41
39
|
return HTTPS_PROTOCOL + url;
|
|
42
40
|
};
|
|
43
41
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
//
|
|
47
|
-
|
|
42
|
+
const dynamicOriginCallbackHandler = (origins) => {
|
|
43
|
+
return (origin, callback) => {
|
|
44
|
+
// Handle wildcard origin
|
|
45
|
+
if (origins === "*") {
|
|
46
|
+
callback(null, true);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
51
|
+
if (!origin) {
|
|
52
|
+
callback(null, true);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const allowedOrigins = [];
|
|
57
|
+
if (process.env.BASE_URL) {
|
|
58
|
+
allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
|
|
59
|
+
}
|
|
60
|
+
if (process.env.PROJECT_BASE_URL) {
|
|
61
|
+
allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
|
|
62
|
+
}
|
|
63
|
+
if (origins) {
|
|
64
|
+
const additionalOrigins = core.force.array(origins);
|
|
65
|
+
allowedOrigins.push(...additionalOrigins);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Add localhost origins in sandbox
|
|
69
|
+
if (
|
|
70
|
+
process.env.PROJECT_ENV === SANDBOX_ENV ||
|
|
71
|
+
core.envBoolean("PROJECT_SANDBOX_MODE")
|
|
72
|
+
) {
|
|
73
|
+
allowedOrigins.push("http://localhost");
|
|
74
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
75
|
+
}
|
|
59
76
|
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
return;
|
|
77
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
78
|
+
if (allowed instanceof RegExp) {
|
|
79
|
+
return allowed.test(origin);
|
|
64
80
|
}
|
|
81
|
+
return origin.includes(allowed);
|
|
82
|
+
});
|
|
65
83
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
84
|
+
if (isAllowed) {
|
|
85
|
+
callback(null, true);
|
|
86
|
+
} else {
|
|
87
|
+
callback(new errors.CorsError());
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
70
91
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
92
|
+
//
|
|
93
|
+
//
|
|
94
|
+
// Main
|
|
95
|
+
//
|
|
76
96
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return allowed.test(origin);
|
|
80
|
-
}
|
|
81
|
-
return origin.includes(allowed);
|
|
82
|
-
});
|
|
97
|
+
const corsHelper = (config = {}) => {
|
|
98
|
+
const { origins, overrides = {} } = config;
|
|
83
99
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
callback(new errors.CorsError());
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
91
|
-
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
100
|
+
const options = {
|
|
101
|
+
origin: dynamicOriginCallbackHandler(origins),
|
|
102
|
+
// * The default behavior is to allow any headers and methods so they are not included here
|
|
92
103
|
...overrides,
|
|
93
104
|
};
|
|
94
105
|
|
package/dist/module.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CorsError } from '@jaypie/errors';
|
|
2
|
+
import { force, envBoolean, log, JAYPIE, HTTP, validate, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError, NotImplementedError } from '@jaypie/core';
|
|
2
3
|
import expressCors from 'cors';
|
|
3
|
-
import { log, JAYPIE, HTTP, validate, force, jaypieHandler, UnhandledError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, MethodNotAllowedError, GoneError, TeapotError, InternalError, BadGatewayError, UnavailableError, GatewayTimeoutError, NotImplementedError } from '@jaypie/core';
|
|
4
4
|
import { getCurrentInvoke } from '@codegenie/serverless-express';
|
|
5
5
|
|
|
6
6
|
//
|
|
@@ -21,8 +21,6 @@ const EXPRESS = {
|
|
|
21
21
|
// Constants
|
|
22
22
|
//
|
|
23
23
|
|
|
24
|
-
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
25
|
-
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
26
24
|
const HTTP_PROTOCOL = "http://";
|
|
27
25
|
const HTTPS_PROTOCOL = "https://";
|
|
28
26
|
const SANDBOX_ENV = "sandbox";
|
|
@@ -39,54 +37,67 @@ const ensureProtocol = (url) => {
|
|
|
39
37
|
return HTTPS_PROTOCOL + url;
|
|
40
38
|
};
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
|
|
40
|
+
const dynamicOriginCallbackHandler = (origins) => {
|
|
41
|
+
return (origin, callback) => {
|
|
42
|
+
// Handle wildcard origin
|
|
43
|
+
if (origins === "*") {
|
|
44
|
+
callback(null, true);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
49
|
+
if (!origin) {
|
|
50
|
+
callback(null, true);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
const allowedOrigins = [];
|
|
55
|
+
if (process.env.BASE_URL) {
|
|
56
|
+
allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
|
|
57
|
+
}
|
|
58
|
+
if (process.env.PROJECT_BASE_URL) {
|
|
59
|
+
allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
|
|
60
|
+
}
|
|
61
|
+
if (origins) {
|
|
62
|
+
const additionalOrigins = force.array(origins);
|
|
63
|
+
allowedOrigins.push(...additionalOrigins);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Add localhost origins in sandbox
|
|
67
|
+
if (
|
|
68
|
+
process.env.PROJECT_ENV === SANDBOX_ENV ||
|
|
69
|
+
envBoolean("PROJECT_SANDBOX_MODE")
|
|
70
|
+
) {
|
|
71
|
+
allowedOrigins.push("http://localhost");
|
|
72
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
73
|
+
}
|
|
57
74
|
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
return;
|
|
75
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
76
|
+
if (allowed instanceof RegExp) {
|
|
77
|
+
return allowed.test(origin);
|
|
62
78
|
}
|
|
79
|
+
return origin.includes(allowed);
|
|
80
|
+
});
|
|
63
81
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
if (isAllowed) {
|
|
83
|
+
callback(null, true);
|
|
84
|
+
} else {
|
|
85
|
+
callback(new CorsError());
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
68
89
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
90
|
+
//
|
|
91
|
+
//
|
|
92
|
+
// Main
|
|
93
|
+
//
|
|
74
94
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return allowed.test(origin);
|
|
78
|
-
}
|
|
79
|
-
return origin.includes(allowed);
|
|
80
|
-
});
|
|
95
|
+
const corsHelper = (config = {}) => {
|
|
96
|
+
const { origins, overrides = {} } = config;
|
|
81
97
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
callback(new CorsError());
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
89
|
-
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
98
|
+
const options = {
|
|
99
|
+
origin: dynamicOriginCallbackHandler(origins),
|
|
100
|
+
// * The default behavior is to allow any headers and methods so they are not included here
|
|
90
101
|
...overrides,
|
|
91
102
|
};
|
|
92
103
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jaypie/express",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Finlayson Studio",
|
|
6
6
|
"type": "module",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "11eb680bba07d4885cc04dc173165e0b7982b780"
|
|
47
47
|
}
|
package/src/cors.helper.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CorsError } from "@jaypie/errors";
|
|
2
|
+
import { envBoolean, force } from "@jaypie/core";
|
|
2
3
|
import expressCors from "cors";
|
|
3
4
|
|
|
4
5
|
//
|
|
@@ -6,8 +7,6 @@ import expressCors from "cors";
|
|
|
6
7
|
// Constants
|
|
7
8
|
//
|
|
8
9
|
|
|
9
|
-
const DEFAULT_HEADERS = ["Authorization", "X-Session-Id"];
|
|
10
|
-
const DEFAULT_METHODS = ["DELETE", "HEAD", "GET", "POST", "PUT"];
|
|
11
10
|
const HTTP_PROTOCOL = "http://";
|
|
12
11
|
const HTTPS_PROTOCOL = "https://";
|
|
13
12
|
const SANDBOX_ENV = "sandbox";
|
|
@@ -24,54 +23,67 @@ const ensureProtocol = (url) => {
|
|
|
24
23
|
return HTTPS_PROTOCOL + url;
|
|
25
24
|
};
|
|
26
25
|
|
|
26
|
+
export const dynamicOriginCallbackHandler = (origins) => {
|
|
27
|
+
return (origin, callback) => {
|
|
28
|
+
// Handle wildcard origin
|
|
29
|
+
if (origins === "*") {
|
|
30
|
+
callback(null, true);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
35
|
+
if (!origin) {
|
|
36
|
+
callback(null, true);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const allowedOrigins = [];
|
|
41
|
+
if (process.env.BASE_URL) {
|
|
42
|
+
allowedOrigins.push(ensureProtocol(process.env.BASE_URL));
|
|
43
|
+
}
|
|
44
|
+
if (process.env.PROJECT_BASE_URL) {
|
|
45
|
+
allowedOrigins.push(ensureProtocol(process.env.PROJECT_BASE_URL));
|
|
46
|
+
}
|
|
47
|
+
if (origins) {
|
|
48
|
+
const additionalOrigins = force.array(origins);
|
|
49
|
+
allowedOrigins.push(...additionalOrigins);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Add localhost origins in sandbox
|
|
53
|
+
if (
|
|
54
|
+
process.env.PROJECT_ENV === SANDBOX_ENV ||
|
|
55
|
+
envBoolean("PROJECT_SANDBOX_MODE")
|
|
56
|
+
) {
|
|
57
|
+
allowedOrigins.push("http://localhost");
|
|
58
|
+
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const isAllowed = allowedOrigins.some((allowed) => {
|
|
62
|
+
if (allowed instanceof RegExp) {
|
|
63
|
+
return allowed.test(origin);
|
|
64
|
+
}
|
|
65
|
+
return origin.includes(allowed);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (isAllowed) {
|
|
69
|
+
callback(null, true);
|
|
70
|
+
} else {
|
|
71
|
+
callback(new CorsError());
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
27
76
|
//
|
|
28
77
|
//
|
|
29
78
|
// Main
|
|
30
79
|
//
|
|
31
80
|
|
|
32
81
|
const corsHelper = (config = {}) => {
|
|
33
|
-
const { origins,
|
|
82
|
+
const { origins, overrides = {} } = config;
|
|
34
83
|
|
|
35
84
|
const options = {
|
|
36
|
-
origin(
|
|
37
|
-
|
|
38
|
-
if (origins === "*") {
|
|
39
|
-
callback(null, true);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Allow requests with no origin (like mobile apps, curl, etc)
|
|
44
|
-
if (!origin) {
|
|
45
|
-
callback(null, true);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const allowedOrigins = origins || [
|
|
50
|
-
ensureProtocol(process.env.BASE_URL),
|
|
51
|
-
ensureProtocol(process.env.PROJECT_BASE_URL),
|
|
52
|
-
];
|
|
53
|
-
|
|
54
|
-
// Add localhost origins in sandbox
|
|
55
|
-
if (!origins && process.env.PROJECT_ENV === SANDBOX_ENV) {
|
|
56
|
-
allowedOrigins.push("http://localhost");
|
|
57
|
-
allowedOrigins.push(/^http:\/\/localhost:\d+$/);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const isAllowed = allowedOrigins.some((allowed) => {
|
|
61
|
-
if (allowed instanceof RegExp) {
|
|
62
|
-
return allowed.test(origin);
|
|
63
|
-
}
|
|
64
|
-
return origin.includes(allowed);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
if (isAllowed) {
|
|
68
|
-
callback(null, true);
|
|
69
|
-
} else {
|
|
70
|
-
callback(new CorsError());
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
methods: [...DEFAULT_METHODS, ...(methods || [])],
|
|
74
|
-
allowedHeaders: [...DEFAULT_HEADERS, ...(headers || [])],
|
|
85
|
+
origin: dynamicOriginCallbackHandler(origins),
|
|
86
|
+
// * The default behavior is to allow any headers and methods so they are not included here
|
|
75
87
|
...overrides,
|
|
76
88
|
};
|
|
77
89
|
|