webspresso 0.0.103 → 0.0.105
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/.agents/AGENTS.md +6 -0
- package/.agents/ARCHITECTURE.md +23 -0
- package/.agents/CACHE.md +147 -0
- package/.agents/CLI.md +1 -1
- package/.agents/EARLY_HINTS.md +120 -0
- package/.agents/HEALTH.md +118 -0
- package/.agents/LIMITS.md +109 -0
- package/.agents/ORM.md +155 -1
- package/.agents/PLUGINS.md +28 -0
- package/.agents/RESILIENCE.md +235 -0
- package/AGENTS.md +6 -0
- package/README.md +2 -2
- package/bin/commands/add-docker.js +6 -6
- package/bin/commands/dev.js +4 -3
- package/bin/commands/new.js +5 -5
- package/bin/commands/openapi.js +1 -1
- package/bin/commands/start.js +1 -1
- package/bin/utils/orm-map-html.js +2 -8
- package/bin/utils/project.js +2 -2
- package/core/auth/hash.js +5 -4
- package/core/auth/jwt.js +6 -57
- package/core/auth/magic-link.js +4 -5
- package/core/auth/manager.js +3 -5
- package/core/auth/oauth.js +45 -30
- package/core/auth/totp.js +5 -16
- package/core/auth/user-agent.js +1 -21
- package/core/cache/byte-size.js +123 -0
- package/core/cache/index.js +21 -0
- package/core/cache/lru.js +477 -0
- package/core/cache/tagged-lru.js +346 -0
- package/core/commands/registry.js +6 -20
- package/core/content/renderer.js +1 -13
- package/core/early-hints/index.js +267 -0
- package/core/errors/domain.js +48 -0
- package/core/errors/http.js +31 -0
- package/core/errors/index.js +10 -0
- package/core/errors/middleware.js +2 -8
- package/core/errors/normalize.js +88 -2
- package/core/events/discovery.js +6 -24
- package/core/events/dispatcher.js +2 -7
- package/core/forms/form-builder.js +1 -9
- package/core/health/index.js +41 -0
- package/core/health/middleware.js +213 -0
- package/core/health/normalize.js +140 -0
- package/core/health/probes.js +203 -0
- package/core/limits/index.js +34 -0
- package/core/limits/middleware.js +317 -0
- package/core/limits/normalize.js +222 -0
- package/core/orm/batch.js +70 -0
- package/core/orm/cache/fingerprint.js +2 -2
- package/core/orm/cache/memory-provider.js +27 -110
- package/core/orm/index.js +76 -8
- package/core/orm/json-fields.js +22 -8
- package/core/orm/model.js +10 -0
- package/core/orm/query-builder.js +63 -9
- package/core/orm/query-filters.js +393 -0
- package/core/orm/repository.js +318 -19
- package/core/orm/schema-helpers.js +8 -10
- package/core/orm/scopes.js +321 -1
- package/core/orm/transaction.js +10 -2
- package/core/orm/utils.js +4 -86
- package/core/queue/adapters/database.js +4 -8
- package/core/queue/manager.js +55 -41
- package/core/resilience/circuit-breaker.js +391 -0
- package/core/resilience/errors.js +63 -0
- package/core/resilience/index.js +53 -0
- package/core/resilience/registry.js +97 -0
- package/core/resilience/retry.js +203 -0
- package/core/schema-cache.js +9 -2
- package/core/shutdown/http-adapter.js +4 -1
- package/core/shutdown/shutdown-manager.js +132 -31
- package/core/utils/array.js +12 -0
- package/core/utils/base64url.js +8 -0
- package/core/utils/crypto.js +191 -0
- package/core/utils/ensure-leading-slash.js +6 -0
- package/core/utils/escape-html.js +7 -0
- package/core/utils/escape-regexp.js +7 -0
- package/core/utils/fetch.js +121 -0
- package/core/utils/fs.js +144 -0
- package/core/utils/get-client-ip.js +6 -0
- package/core/utils/hmac.js +6 -0
- package/core/utils/html.js +72 -0
- package/core/utils/ip.js +86 -0
- package/core/utils/json.js +98 -0
- package/core/utils/object.js +106 -0
- package/core/utils/random.js +9 -0
- package/core/utils/regex.js +77 -0
- package/core/utils/safe-compare.js +3 -0
- package/core/utils/sha256.js +6 -0
- package/core/utils/string.js +87 -0
- package/core/utils/strip-trailing-slash.js +6 -0
- package/core/utils/time.js +96 -0
- package/core/utils/timeout.js +348 -0
- package/core/utils/url.js +106 -0
- package/core/utils/walk.js +7 -0
- package/index.d.ts +627 -2
- package/index.js +158 -0
- package/package.json +1 -2
- package/plugins/admin-panel/auth.js +15 -7
- package/plugins/admin-panel/client/parts/01-state-api-breadcrumb.js +82 -10
- package/plugins/admin-panel/client/parts/05-rich-text-file-helpers.js +4 -2
- package/plugins/admin-panel/client/parts/08-record-list.js +3 -2
- package/plugins/admin-panel/client/parts/10-export-registry.js +6 -0
- package/plugins/admin-panel/core/api-extensions.js +3 -80
- package/plugins/admin-panel/index.js +1 -1
- package/plugins/admin-panel/lib/login-rate-limiter.js +15 -13
- package/plugins/admin-panel/modules/menu.js +1 -1
- package/plugins/api-key/crypto.js +3 -26
- package/plugins/api-key/scopes.js +5 -7
- package/plugins/audit-log/api-handlers.js +8 -7
- package/plugins/audit-log/middleware.js +11 -2
- package/plugins/audit-log/parse.js +3 -3
- package/plugins/basic-auth/index.js +2 -26
- package/plugins/cors.js +2 -2
- package/plugins/csrf/index.js +7 -23
- package/plugins/dashboard/app.js +2 -2
- package/plugins/dashboard/index.js +5 -5
- package/plugins/data-exchange/record-selection.js +2 -5
- package/plugins/email/api-handlers.js +5 -18
- package/plugins/email/auth-bridge.js +3 -2
- package/plugins/file-manager/admin-component.js +3 -1
- package/plugins/file-manager/core.js +34 -4
- package/plugins/health-check.js +2 -1
- package/plugins/index.js +2 -0
- package/plugins/lazy.js +5 -6
- package/plugins/mcp/auth.js +1 -13
- package/plugins/mcp/discovery.js +18 -17
- package/plugins/mcp/index.js +23 -1
- package/plugins/mcp/transports/sse.js +10 -1
- package/plugins/orm-cache-admin/admin-component.js +3 -2
- package/plugins/polar/src/api.js +11 -3
- package/plugins/polar/src/webhooks.js +7 -13
- package/plugins/rate-limit/index.js +3 -1
- package/plugins/recaptcha/helpers.js +1 -7
- package/plugins/recaptcha/verify.js +37 -23
- package/plugins/redirect/index.js +57 -7
- package/plugins/rest-resources/index.js +1 -207
- package/plugins/rls/index.js +170 -0
- package/plugins/schema-explorer/admin-component.js +4 -2
- package/plugins/schema-explorer.js +40 -29
- package/plugins/seo-checker/index.js +5 -2
- package/plugins/site-analytics/tracking.js +21 -18
- package/plugins/sitemap.js +21 -17
- package/plugins/swagger.js +13 -17
- package/plugins/upload/index.js +4 -3
- package/plugins/upload/orphan-pruner.js +4 -3
- package/plugins/webhooks/crypto.js +6 -28
- package/plugins/webhooks/dispatcher.js +14 -9
- package/plugins/webhooks/index.js +6 -0
- package/plugins/webhooks/matcher.js +28 -17
- package/plugins/webhooks/sender.js +40 -13
- package/src/app-context.js +41 -5
- package/src/dev/banner.js +6 -2
- package/src/dev-reload/index.js +47 -6
- package/src/file-router.js +1 -3
- package/src/helpers.js +180 -33
- package/src/modules/module-discovery.js +1 -9
- package/src/pages/page-loader.js +53 -1
- package/src/plugin-manager.js +90 -17
- package/src/server.js +137 -26
- package/src/services/discovery.js +19 -40
- package/src/services/executor.js +86 -25
- package/src/services/memoize.js +3 -25
- package/src/services/registry.js +6 -0
- package/src/template/precompile.js +8 -19
package/plugins/mcp/index.js
CHANGED
|
@@ -52,6 +52,7 @@ function mcpPlugin(options = {}) {
|
|
|
52
52
|
} = options;
|
|
53
53
|
|
|
54
54
|
let serverInstance = null;
|
|
55
|
+
let sseTransport = null;
|
|
55
56
|
|
|
56
57
|
return {
|
|
57
58
|
name: 'mcp',
|
|
@@ -63,6 +64,19 @@ function mcpPlugin(options = {}) {
|
|
|
63
64
|
registerTool: (tool) => serverInstance && serverInstance.registerTool(tool),
|
|
64
65
|
registerResource: (res) => serverInstance && serverInstance.registerResource(res),
|
|
65
66
|
registerPrompt: (prompt) => serverInstance && serverInstance.registerPrompt(prompt),
|
|
67
|
+
closeTransport: () => sseTransport && sseTransport.close && sseTransport.close(),
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
dispose() {
|
|
71
|
+
if (sseTransport && typeof sseTransport.close === 'function') {
|
|
72
|
+
sseTransport.close();
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
shutdown() {
|
|
77
|
+
if (sseTransport && typeof sseTransport.close === 'function') {
|
|
78
|
+
sseTransport.close();
|
|
79
|
+
}
|
|
66
80
|
},
|
|
67
81
|
|
|
68
82
|
register(ctx) {
|
|
@@ -180,12 +194,20 @@ function mcpPlugin(options = {}) {
|
|
|
180
194
|
const authMiddleware =
|
|
181
195
|
auth.token || auth.localhostOnly || auth.verify ? createMcpAuthMiddleware(auth) : null;
|
|
182
196
|
|
|
183
|
-
mountSseTransport({
|
|
197
|
+
sseTransport = mountSseTransport({
|
|
184
198
|
app,
|
|
185
199
|
server: serverInstance,
|
|
186
200
|
path,
|
|
187
201
|
authMiddleware,
|
|
188
202
|
});
|
|
203
|
+
|
|
204
|
+
if (typeof ctx.onDispose === 'function') {
|
|
205
|
+
ctx.onDispose(() => {
|
|
206
|
+
if (sseTransport && typeof sseTransport.close === 'function') {
|
|
207
|
+
sseTransport.close();
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
}
|
|
189
211
|
},
|
|
190
212
|
};
|
|
191
213
|
}
|
|
@@ -150,7 +150,16 @@ function mountSseTransport(options) {
|
|
|
150
150
|
}
|
|
151
151
|
});
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
const close = () => {
|
|
154
|
+
for (const [id, session] of sessions.entries()) {
|
|
155
|
+
try {
|
|
156
|
+
session.res.end();
|
|
157
|
+
} catch {}
|
|
158
|
+
}
|
|
159
|
+
sessions.clear();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return { sessions, close };
|
|
154
163
|
}
|
|
155
164
|
|
|
156
165
|
module.exports = {
|
|
@@ -8,12 +8,13 @@ function generateOrmCacheAdminComponent() {
|
|
|
8
8
|
(function() {
|
|
9
9
|
|
|
10
10
|
function ormCacheApi(method, path, body) {
|
|
11
|
-
var opts = { method: method, credentials: 'same-origin' };
|
|
11
|
+
var opts = { method: method, credentials: 'same-origin', timeout: 30000 };
|
|
12
12
|
if (body !== undefined) {
|
|
13
13
|
opts.headers = { 'Content-Type': 'application/json' };
|
|
14
14
|
opts.body = JSON.stringify(body);
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
var fetchFn = (typeof window !== 'undefined' && window.Admin && window.Admin.fetch) ? window.Admin.fetch : fetch;
|
|
17
|
+
return fetchFn('/_admin/api/orm-cache' + path, opts).then(function(r) {
|
|
17
18
|
return r.json().then(function(j) {
|
|
18
19
|
if (!r.ok) throw new Error(j.error || r.statusText);
|
|
19
20
|
return j;
|
package/plugins/polar/src/api.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { fetchWithTimeout } = require('../../../core/utils/fetch');
|
|
4
|
+
const { ensureLeadingSlash } = require('../../../core/utils/url');
|
|
5
|
+
|
|
3
6
|
function getPolarApiBase(sandbox) {
|
|
4
7
|
return sandbox ? 'https://sandbox-api.polar.sh/v1' : 'https://api.polar.sh/v1';
|
|
5
8
|
}
|
|
@@ -11,14 +14,17 @@ function getPolarApiBase(sandbox) {
|
|
|
11
14
|
* @param {Object} [opts.body]
|
|
12
15
|
* @param {string} opts.accessToken
|
|
13
16
|
* @param {boolean} [opts.sandbox=false]
|
|
17
|
+
* @param {number} [opts.timeoutMs=15000]
|
|
18
|
+
* @param {AbortSignal} [opts.signal]
|
|
14
19
|
*/
|
|
15
|
-
async function polarApiRequest(path, { method = 'GET', body, accessToken, sandbox = false } = {}) {
|
|
20
|
+
async function polarApiRequest(path, { method = 'GET', body, accessToken, sandbox = false, timeoutMs = 15000, signal } = {}) {
|
|
16
21
|
if (!accessToken) {
|
|
17
22
|
throw new Error('POLAR_ACCESS_TOKEN is not configured');
|
|
18
23
|
}
|
|
19
24
|
|
|
20
|
-
const normalizedPath = path
|
|
21
|
-
|
|
25
|
+
const normalizedPath = ensureLeadingSlash(path);
|
|
26
|
+
|
|
27
|
+
const res = await fetchWithTimeout(`${getPolarApiBase(sandbox)}${normalizedPath}`, {
|
|
22
28
|
method,
|
|
23
29
|
headers: {
|
|
24
30
|
Authorization: `Bearer ${accessToken}`,
|
|
@@ -26,6 +32,8 @@ async function polarApiRequest(path, { method = 'GET', body, accessToken, sandbo
|
|
|
26
32
|
Accept: 'application/json',
|
|
27
33
|
},
|
|
28
34
|
body: body ? JSON.stringify(body) : undefined,
|
|
35
|
+
timeout: timeoutMs,
|
|
36
|
+
signal,
|
|
29
37
|
});
|
|
30
38
|
|
|
31
39
|
const raw = await res.text();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const { safeCompare, hmacSha256 } = require('../../../core/utils/crypto');
|
|
4
4
|
const { syncPolarBillingForUser, applyPolarProSubscription } = require('./sync');
|
|
5
5
|
const {
|
|
6
6
|
collectPolarMetadata,
|
|
@@ -37,23 +37,17 @@ function verifyPolarWebhook(rawPayload, headers, secret) {
|
|
|
37
37
|
? Buffer.from(secret.slice(6), 'base64')
|
|
38
38
|
: secret;
|
|
39
39
|
|
|
40
|
-
const expectedBase64 =
|
|
41
|
-
const expectedHex =
|
|
42
|
-
const rawExpectedBase64 =
|
|
43
|
-
const rawExpectedHex =
|
|
40
|
+
const expectedBase64 = hmacSha256(standardToSign, cleanSecret, 'base64');
|
|
41
|
+
const expectedHex = hmacSha256(standardToSign, cleanSecret, 'hex');
|
|
42
|
+
const rawExpectedBase64 = hmacSha256(payloadStr, cleanSecret, 'base64');
|
|
43
|
+
const rawExpectedHex = hmacSha256(payloadStr, cleanSecret, 'hex');
|
|
44
44
|
|
|
45
45
|
const expectedList = [expectedBase64, expectedHex, rawExpectedBase64, rawExpectedHex];
|
|
46
46
|
|
|
47
47
|
for (const candidate of candidates) {
|
|
48
48
|
for (const exp of expectedList) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const b = Buffer.from(exp);
|
|
52
|
-
if (a.length === b.length && crypto.timingSafeEqual(a, b)) {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
} catch {
|
|
56
|
-
// continue
|
|
49
|
+
if (safeCompare(candidate, exp)) {
|
|
50
|
+
return true;
|
|
57
51
|
}
|
|
58
52
|
}
|
|
59
53
|
}
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* @module plugins/rate-limit
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
const { ensureArray } = require('../../core/utils/object');
|
|
7
|
+
|
|
6
8
|
const PLUGIN_ONLY_KEYS = new Set(['global', 'globalOverrides', 'globalSkipPaths']);
|
|
7
9
|
|
|
8
10
|
const DEFAULT_BASE = {
|
|
@@ -95,7 +97,7 @@ function combineSkip(userSkip, builtin) {
|
|
|
95
97
|
* @returns {import('express').RequestHandler}
|
|
96
98
|
*/
|
|
97
99
|
function createDefaultGlobalSkip(extraPathPrefixes = []) {
|
|
98
|
-
const extras = (
|
|
100
|
+
const extras = ensureArray(extraPathPrefixes).filter(Boolean);
|
|
99
101
|
/** @type {string[]} */
|
|
100
102
|
const prefixes = [
|
|
101
103
|
'/__webspresso/client-runtime',
|
|
@@ -72,13 +72,7 @@ function recaptchaV3ExecuteScript(opts = {}) {
|
|
|
72
72
|
return hidden + script;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
return String(s)
|
|
77
|
-
.replace(/&/g, '&')
|
|
78
|
-
.replace(/"/g, '"')
|
|
79
|
-
.replace(/</g, '<')
|
|
80
|
-
.replace(/>/g, '>');
|
|
81
|
-
}
|
|
75
|
+
const { escapeHtml } = require('../../core/utils/html');
|
|
82
76
|
|
|
83
77
|
function escapeJsInline(s) {
|
|
84
78
|
return String(s).replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
* @module plugins/recaptcha/verify
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
const { fetchWithTimeout } = require('../../core/utils/fetch');
|
|
7
|
+
const { getClientIp } = require('../../core/utils/ip');
|
|
8
|
+
|
|
6
9
|
const SITEVERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
|
|
7
10
|
|
|
8
11
|
/**
|
|
@@ -20,7 +23,8 @@ const SITEVERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
|
|
|
20
23
|
* @param {VerifyOptions} options
|
|
21
24
|
* @returns {Promise<{ success: boolean, score?: number, action?: string, hostname?: string, challenge_ts?: string, errorCodes?: string[], raw: object }>}
|
|
22
25
|
*/
|
|
23
|
-
async function verifyRecaptcha(options) {
|
|
26
|
+
async function verifyRecaptcha(options = {}) {
|
|
27
|
+
const opts = options || {};
|
|
24
28
|
const {
|
|
25
29
|
secret,
|
|
26
30
|
token,
|
|
@@ -28,7 +32,8 @@ async function verifyRecaptcha(options) {
|
|
|
28
32
|
version = 'v2',
|
|
29
33
|
minScore = 0.5,
|
|
30
34
|
expectedAction,
|
|
31
|
-
|
|
35
|
+
timeoutMs = 10000,
|
|
36
|
+
} = opts;
|
|
32
37
|
|
|
33
38
|
if (!secret || typeof secret !== 'string') {
|
|
34
39
|
throw new Error('recaptcha: secret is required');
|
|
@@ -49,13 +54,24 @@ async function verifyRecaptcha(options) {
|
|
|
49
54
|
body.set('remoteip', remoteIp);
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
let res;
|
|
58
|
+
try {
|
|
59
|
+
res = await fetchWithTimeout(SITEVERIFY_URL, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: {
|
|
62
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
63
|
+
},
|
|
64
|
+
body: body.toString(),
|
|
65
|
+
timeout: timeoutMs,
|
|
66
|
+
});
|
|
67
|
+
} catch (netErr) {
|
|
68
|
+
const isTimeout = netErr.name === 'AbortError' || netErr.name === 'TimeoutError' || netErr.code === 'ETIMEDOUT';
|
|
69
|
+
return {
|
|
70
|
+
success: false,
|
|
71
|
+
errorCodes: [isTimeout ? 'timeout' : 'network-error'],
|
|
72
|
+
raw: {},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
59
75
|
|
|
60
76
|
if (!res.ok) {
|
|
61
77
|
return {
|
|
@@ -66,8 +82,17 @@ async function verifyRecaptcha(options) {
|
|
|
66
82
|
}
|
|
67
83
|
|
|
68
84
|
/** @type {Record<string, unknown>} */
|
|
69
|
-
|
|
70
|
-
|
|
85
|
+
let raw;
|
|
86
|
+
try {
|
|
87
|
+
raw = await res.json();
|
|
88
|
+
} catch (jsonErr) {
|
|
89
|
+
return {
|
|
90
|
+
success: false,
|
|
91
|
+
errorCodes: ['invalid-json-response'],
|
|
92
|
+
raw: {},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const googleSuccess = raw && raw.success === true;
|
|
71
96
|
|
|
72
97
|
if (!googleSuccess) {
|
|
73
98
|
return {
|
|
@@ -129,18 +154,7 @@ async function verifyRecaptcha(options) {
|
|
|
129
154
|
* @returns {string|undefined}
|
|
130
155
|
*/
|
|
131
156
|
function getRemoteIp(req) {
|
|
132
|
-
|
|
133
|
-
const xf = req.headers && req.headers['x-forwarded-for'];
|
|
134
|
-
if (typeof xf === 'string' && xf.length) {
|
|
135
|
-
return xf.split(',')[0].trim();
|
|
136
|
-
}
|
|
137
|
-
if (req.ip) {
|
|
138
|
-
return req.ip;
|
|
139
|
-
}
|
|
140
|
-
if (req.socket && req.socket.remoteAddress) {
|
|
141
|
-
return req.socket.remoteAddress;
|
|
142
|
-
}
|
|
143
|
-
return undefined;
|
|
157
|
+
return getClientIp(req, undefined);
|
|
144
158
|
}
|
|
145
159
|
|
|
146
160
|
module.exports = {
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
7
|
+
const { stripTrailingSlash, ensureTrailingSlash } = require('../../core/utils/url');
|
|
8
|
+
const { ensureArray } = require('../../core/utils/object');
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* @param {string} to
|
|
@@ -32,10 +34,10 @@ function normalizePathForTrailingSlash(mode, path) {
|
|
|
32
34
|
if (!path || mode === false || mode == null) return path;
|
|
33
35
|
if (path === '/') return path;
|
|
34
36
|
if (mode === 'strip') {
|
|
35
|
-
return
|
|
37
|
+
return stripTrailingSlash(path, { preserveRoot: true });
|
|
36
38
|
}
|
|
37
39
|
if (mode === 'add') {
|
|
38
|
-
return path
|
|
40
|
+
return ensureTrailingSlash(path);
|
|
39
41
|
}
|
|
40
42
|
return path;
|
|
41
43
|
}
|
|
@@ -119,8 +121,8 @@ function pathMatches(compiled, pathForMatch, rawPath) {
|
|
|
119
121
|
if (compiled.kind === 'string') {
|
|
120
122
|
if (pathForMatch === compiled.fromNorm) return true;
|
|
121
123
|
if (!compiled.trailing) {
|
|
122
|
-
const a = rawPath
|
|
123
|
-
const b = compiled.fromRaw
|
|
124
|
+
const a = stripTrailingSlash(rawPath, { preserveRoot: true });
|
|
125
|
+
const b = stripTrailingSlash(compiled.fromRaw, { preserveRoot: true });
|
|
124
126
|
if (a === b) return true;
|
|
125
127
|
}
|
|
126
128
|
return false;
|
|
@@ -151,6 +153,7 @@ function methodMatches(compiled, method) {
|
|
|
151
153
|
* @returns {{ name: string, version: string, description: string, register: Function }}
|
|
152
154
|
*/
|
|
153
155
|
function redirectPlugin(options = {}) {
|
|
156
|
+
const opts = options || {};
|
|
154
157
|
const {
|
|
155
158
|
rules = [],
|
|
156
159
|
defaultStatus = 302,
|
|
@@ -158,7 +161,7 @@ function redirectPlugin(options = {}) {
|
|
|
158
161
|
allowExternal = false,
|
|
159
162
|
trailingSlash = false,
|
|
160
163
|
defaultMethods = ['GET', 'HEAD'],
|
|
161
|
-
} =
|
|
164
|
+
} = opts;
|
|
162
165
|
|
|
163
166
|
const pluginOpts = {
|
|
164
167
|
defaultStatus: REDIRECT_STATUSES.has(Number(defaultStatus)) ? Number(defaultStatus) : 302,
|
|
@@ -169,16 +172,63 @@ function redirectPlugin(options = {}) {
|
|
|
169
172
|
};
|
|
170
173
|
|
|
171
174
|
const compiled = [];
|
|
172
|
-
|
|
175
|
+
const stringRulesMap = new Map();
|
|
176
|
+
let hasRegexRules = false;
|
|
177
|
+
|
|
178
|
+
const ruleList = ensureArray(rules);
|
|
179
|
+
for (const rule of ruleList) {
|
|
173
180
|
const c = compileRule(rule, pluginOpts);
|
|
174
|
-
if (c)
|
|
181
|
+
if (!c) continue;
|
|
182
|
+
compiled.push(c);
|
|
183
|
+
if (c.kind === 'string') {
|
|
184
|
+
const keys = [c.fromNorm];
|
|
185
|
+
if (!c.trailing) {
|
|
186
|
+
const alt = stripTrailingSlash(c.fromRaw, { preserveRoot: true });
|
|
187
|
+
if (!keys.includes(alt)) keys.push(alt);
|
|
188
|
+
}
|
|
189
|
+
for (const k of keys) {
|
|
190
|
+
let list = stringRulesMap.get(k);
|
|
191
|
+
if (!list) {
|
|
192
|
+
list = [];
|
|
193
|
+
stringRulesMap.set(k, list);
|
|
194
|
+
}
|
|
195
|
+
list.push(c);
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
hasRegexRules = true;
|
|
199
|
+
}
|
|
175
200
|
}
|
|
176
201
|
|
|
177
202
|
function redirectMiddleware(req, res, next) {
|
|
203
|
+
if (!req) return next();
|
|
178
204
|
const rawPath = req.path || '/';
|
|
179
205
|
const pathForMatch = normalizePathForTrailingSlash(pluginOpts.trailingSlash, rawPath);
|
|
180
206
|
const method = req.method || 'GET';
|
|
181
207
|
|
|
208
|
+
// Fast O(1) path when all configured rules are string based
|
|
209
|
+
if (!hasRegexRules) {
|
|
210
|
+
const candidates = stringRulesMap.get(pathForMatch) || (!pluginOpts.trailingSlash ? stringRulesMap.get(stripTrailingSlash(rawPath, { preserveRoot: true })) : null);
|
|
211
|
+
if (candidates) {
|
|
212
|
+
for (const c of candidates) {
|
|
213
|
+
if (!methodMatches(c, method)) continue;
|
|
214
|
+
if (!pathMatches(c, pathForMatch, rawPath)) continue;
|
|
215
|
+
|
|
216
|
+
let location = c.to;
|
|
217
|
+
if (pluginOpts.preserveQuery && !location.includes('?')) {
|
|
218
|
+
const full = req.originalUrl || req.url || '';
|
|
219
|
+
const qi = full.indexOf('?');
|
|
220
|
+
if (qi !== -1) {
|
|
221
|
+
location += full.slice(qi);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
res.redirect(c.status, location);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return next();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Linear path when regex rules are present to preserve definition order
|
|
182
232
|
for (const c of compiled) {
|
|
183
233
|
if (!methodMatches(c, method)) continue;
|
|
184
234
|
if (!pathMatches(c, pathForMatch, rawPath)) continue;
|
|
@@ -8,8 +8,7 @@ const { getAllModels } = require('../../core/orm/model');
|
|
|
8
8
|
const { omit } = require('../../core/orm/utils');
|
|
9
9
|
const { trimUrlPathSlashes } = require('../../core/url-path-normalize');
|
|
10
10
|
const { validateQueryComplexity } = require('../../core/orm/complexity');
|
|
11
|
-
|
|
12
|
-
const RESERVED_QUERY_KEYS = new Set(['page', 'perPage', 'sort', 'order', 'include', 'trashed']);
|
|
11
|
+
const { applyColumnFilters, RESERVED_QUERY_KEYS } = require('../../core/orm/query-filters');
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* Pluralize a PascalCase model name to a URL segment (e.g. User -> users, Company -> companies)
|
|
@@ -234,212 +233,7 @@ function parseSortParams(model, rawSort, rawOrder) {
|
|
|
234
233
|
return result;
|
|
235
234
|
}
|
|
236
235
|
|
|
237
|
-
function applyColumnFilters(query, countQuery, model, req) {
|
|
238
|
-
let q = query;
|
|
239
|
-
let c = countQuery;
|
|
240
|
-
|
|
241
|
-
function applyCondition(col, op, rawVal) {
|
|
242
|
-
if (!model.columns.has(col)) return;
|
|
243
|
-
const meta = model.columns.get(col);
|
|
244
|
-
const colType = meta?.type;
|
|
245
|
-
|
|
246
|
-
let operator = String(op || 'eq').toLowerCase();
|
|
247
|
-
let val = rawVal;
|
|
248
|
-
|
|
249
|
-
// Normalize booleans
|
|
250
|
-
if (colType === 'boolean' && typeof val === 'string') {
|
|
251
|
-
if (val === 'true' || val === '1') val = true;
|
|
252
|
-
else if (val === 'false' || val === '0') val = false;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
switch (operator) {
|
|
256
|
-
case 'eq':
|
|
257
|
-
case '=':
|
|
258
|
-
if (val !== undefined && val !== '') {
|
|
259
|
-
q = q.where(col, '=', val);
|
|
260
|
-
c = c.where(col, '=', val);
|
|
261
|
-
}
|
|
262
|
-
break;
|
|
263
|
-
|
|
264
|
-
case 'ne':
|
|
265
|
-
case '!=':
|
|
266
|
-
case '<>':
|
|
267
|
-
if (val !== undefined && val !== '') {
|
|
268
|
-
q = q.where(col, '!=', val);
|
|
269
|
-
c = c.where(col, '!=', val);
|
|
270
|
-
}
|
|
271
|
-
break;
|
|
272
|
-
|
|
273
|
-
case 'gt':
|
|
274
|
-
case '>':
|
|
275
|
-
if (val !== undefined && val !== '') {
|
|
276
|
-
q = q.where(col, '>', val);
|
|
277
|
-
c = c.where(col, '>', val);
|
|
278
|
-
}
|
|
279
|
-
break;
|
|
280
|
-
|
|
281
|
-
case 'gte':
|
|
282
|
-
case '>=':
|
|
283
|
-
if (val !== undefined && val !== '') {
|
|
284
|
-
q = q.where(col, '>=', val);
|
|
285
|
-
c = c.where(col, '>=', val);
|
|
286
|
-
}
|
|
287
|
-
break;
|
|
288
|
-
|
|
289
|
-
case 'lt':
|
|
290
|
-
case '<':
|
|
291
|
-
if (val !== undefined && val !== '') {
|
|
292
|
-
q = q.where(col, '<', val);
|
|
293
|
-
c = c.where(col, '<', val);
|
|
294
|
-
}
|
|
295
|
-
break;
|
|
296
|
-
|
|
297
|
-
case 'lte':
|
|
298
|
-
case '<=':
|
|
299
|
-
if (val !== undefined && val !== '') {
|
|
300
|
-
q = q.where(col, '<=', val);
|
|
301
|
-
c = c.where(col, '<=', val);
|
|
302
|
-
}
|
|
303
|
-
break;
|
|
304
|
-
|
|
305
|
-
case 'in': {
|
|
306
|
-
const arr = Array.isArray(val)
|
|
307
|
-
? val
|
|
308
|
-
: (typeof val === 'string' ? val.split(',').map(s => s.trim()).filter(Boolean) : [val]);
|
|
309
|
-
if (arr.length > 0) {
|
|
310
|
-
q = q.whereIn(col, arr);
|
|
311
|
-
c = c.whereIn(col, arr);
|
|
312
|
-
}
|
|
313
|
-
break;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
case 'nin':
|
|
317
|
-
case 'notin': {
|
|
318
|
-
const arr = Array.isArray(val)
|
|
319
|
-
? val
|
|
320
|
-
: (typeof val === 'string' ? val.split(',').map(s => s.trim()).filter(Boolean) : [val]);
|
|
321
|
-
if (arr.length > 0) {
|
|
322
|
-
q = q.whereNotIn(col, arr);
|
|
323
|
-
c = c.whereNotIn(col, arr);
|
|
324
|
-
}
|
|
325
|
-
break;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
case 'like':
|
|
329
|
-
if (val !== undefined && val !== '') {
|
|
330
|
-
q = q.where(col, 'like', String(val));
|
|
331
|
-
c = c.where(col, 'like', String(val));
|
|
332
|
-
}
|
|
333
|
-
break;
|
|
334
|
-
|
|
335
|
-
case 'ilike':
|
|
336
|
-
if (val !== undefined && val !== '') {
|
|
337
|
-
q = q.where(col, 'ilike', String(val));
|
|
338
|
-
c = c.where(col, 'ilike', String(val));
|
|
339
|
-
}
|
|
340
|
-
break;
|
|
341
|
-
|
|
342
|
-
case 'contains':
|
|
343
|
-
if (val !== undefined && val !== '') {
|
|
344
|
-
q = q.where(col, 'like', `%${String(val)}%`);
|
|
345
|
-
c = c.where(col, 'like', `%${String(val)}%`);
|
|
346
|
-
}
|
|
347
|
-
break;
|
|
348
|
-
|
|
349
|
-
case 'startswith':
|
|
350
|
-
if (val !== undefined && val !== '') {
|
|
351
|
-
q = q.where(col, 'like', `${String(val)}%`);
|
|
352
|
-
c = c.where(col, 'like', `${String(val)}%`);
|
|
353
|
-
}
|
|
354
|
-
break;
|
|
355
|
-
|
|
356
|
-
case 'endswith':
|
|
357
|
-
if (val !== undefined && val !== '') {
|
|
358
|
-
q = q.where(col, 'like', `%${String(val)}`);
|
|
359
|
-
c = c.where(col, 'like', `%${String(val)}`);
|
|
360
|
-
}
|
|
361
|
-
break;
|
|
362
|
-
|
|
363
|
-
case 'isnull':
|
|
364
|
-
case 'is_null': {
|
|
365
|
-
const isTrue = val === true || val === 'true' || val === '1' || val === 1 || val === '';
|
|
366
|
-
if (isTrue) {
|
|
367
|
-
q = q.whereNull(col);
|
|
368
|
-
c = c.whereNull(col);
|
|
369
|
-
}
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
case 'isnotnull':
|
|
374
|
-
case 'is_not_null': {
|
|
375
|
-
const isTrue = val === true || val === 'true' || val === '1' || val === 1 || val === '';
|
|
376
|
-
if (isTrue) {
|
|
377
|
-
q = q.whereNotNull(col);
|
|
378
|
-
c = c.whereNotNull(col);
|
|
379
|
-
}
|
|
380
|
-
break;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
case 'between': {
|
|
384
|
-
let parts = [];
|
|
385
|
-
if (Array.isArray(val)) {
|
|
386
|
-
parts = val;
|
|
387
|
-
} else if (typeof val === 'string') {
|
|
388
|
-
parts = val.split(',').map(s => s.trim());
|
|
389
|
-
}
|
|
390
|
-
if (parts.length === 2 && parts[0] !== '' && parts[1] !== '') {
|
|
391
|
-
q = q.where(col, '>=', parts[0]).where(col, '<=', parts[1]);
|
|
392
|
-
c = c.where(col, '>=', parts[0]).where(col, '<=', parts[1]);
|
|
393
|
-
}
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
default:
|
|
398
|
-
if (val !== undefined && val !== '') {
|
|
399
|
-
q = q.where(col, '=', val);
|
|
400
|
-
c = c.where(col, '=', val);
|
|
401
|
-
}
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// 1. Process dedicated filter object ?filter[col]=... or ?filter[col][op]=...
|
|
407
|
-
if (req.query.filter && typeof req.query.filter === 'object') {
|
|
408
|
-
for (const [col, colFilter] of Object.entries(req.query.filter)) {
|
|
409
|
-
if (!model.columns.has(col)) continue;
|
|
410
|
-
if (colFilter && typeof colFilter === 'object' && !Array.isArray(colFilter)) {
|
|
411
|
-
if (colFilter.op !== undefined && colFilter.value !== undefined) {
|
|
412
|
-
applyCondition(col, colFilter.op, colFilter.value);
|
|
413
|
-
} else {
|
|
414
|
-
for (const [op, val] of Object.entries(colFilter)) {
|
|
415
|
-
applyCondition(col, op, val);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
} else if (colFilter !== undefined && colFilter !== '') {
|
|
419
|
-
applyCondition(col, 'eq', colFilter);
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
236
|
|
|
424
|
-
// 2. Process top-level query parameters ?price[gte]=100 or ?status=active
|
|
425
|
-
for (const [key, value] of Object.entries(req.query)) {
|
|
426
|
-
if (RESERVED_QUERY_KEYS.has(key) || key === 'filter') {
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
if (!model.columns.has(key)) {
|
|
430
|
-
continue;
|
|
431
|
-
}
|
|
432
|
-
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
433
|
-
for (const [op, val] of Object.entries(value)) {
|
|
434
|
-
applyCondition(key, op, val);
|
|
435
|
-
}
|
|
436
|
-
} else if (value !== undefined && value !== '') {
|
|
437
|
-
applyCondition(key, 'eq', value);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
return { query: q, countQuery: c };
|
|
442
|
-
}
|
|
443
237
|
|
|
444
238
|
/**
|
|
445
239
|
* @param {Object} options
|