@sourceregistry/sveltekit-enhance 2.0.0 → 2.0.1
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/helpers/CSRF.js +2 -1
- package/dist/helpers/cache.js +2 -1
- package/dist/helpers/featureflag.js +4 -3
- package/dist/helpers/internal/matches_pattern.d.ts +1 -0
- package/dist/helpers/internal/matches_pattern.js +10 -0
- package/dist/helpers/request-monitor.js +10 -3
- package/dist/helpers/request-rate-limit.js +2 -1
- package/package.json +10 -1
package/dist/helpers/CSRF.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { json } from '@sveltejs/kit';
|
|
2
2
|
import { error } from "../index.js";
|
|
3
|
+
import { matchesPattern } from "./internal/matches_pattern.js";
|
|
3
4
|
function isContentType(request, ...types) {
|
|
4
5
|
const type = request.headers.get('content-type')?.split(';', 1)[0].trim() ?? '';
|
|
5
6
|
return types.includes(type.toLowerCase());
|
|
@@ -33,6 +34,6 @@ export const CSRF = {
|
|
|
33
34
|
}
|
|
34
35
|
};
|
|
35
36
|
export const CSRFChecker = {
|
|
36
|
-
regex: (...oneOf) => (input) => oneOf.some((exp) => exp
|
|
37
|
+
regex: (...oneOf) => (input) => oneOf.some((exp) => matchesPattern(exp, input.url.pathname)),
|
|
37
38
|
list: (...oneOf) => (input) => oneOf.includes(input.url.pathname)
|
|
38
39
|
};
|
package/dist/helpers/cache.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { matchesPattern } from './internal/matches_pattern.js';
|
|
2
|
+
const matches = (rule, pathname) => rule.match instanceof RegExp ? matchesPattern(rule.match, pathname) : rule.match(pathname);
|
|
2
3
|
export const CacheControl = {
|
|
3
4
|
// ── Directive helpers ──────────────────────────────────────────────────────
|
|
4
5
|
directive: {
|
|
@@ -3,6 +3,7 @@ import { dev } from '$app/env';
|
|
|
3
3
|
import { not_good } from "../index.js";
|
|
4
4
|
const _enabled = ['true', 'TRUE', 'on', 'ON', '1'];
|
|
5
5
|
const env = publicEnv;
|
|
6
|
+
const flagValue = (flag) => String(env[flag] ?? '').toUpperCase();
|
|
6
7
|
export const FeatureFlagChecker = {
|
|
7
8
|
enabled: (value) => _enabled.includes(value),
|
|
8
9
|
disabled: (value) => !_enabled.includes(value)
|
|
@@ -13,7 +14,7 @@ export const FeatureFlag = {
|
|
|
13
14
|
all: (...flags) => {
|
|
14
15
|
return (input) => {
|
|
15
16
|
// Which of the requested flags are *not* enabled
|
|
16
|
-
const disabledFlags = flags.filter((flag) => !_enabled.includes((
|
|
17
|
+
const disabledFlags = flags.filter((flag) => !_enabled.includes(flagValue(flag)));
|
|
17
18
|
if (!(disabledFlags.length === 0 || dev))
|
|
18
19
|
not_good(input, 503, {
|
|
19
20
|
message: 'Feature not enabled'
|
|
@@ -24,7 +25,7 @@ export const FeatureFlag = {
|
|
|
24
25
|
oneOf: (...flags) => {
|
|
25
26
|
return (input) => {
|
|
26
27
|
// True if at least one of the given flags is enabled
|
|
27
|
-
const anyEnabled = flags.some((flag) => _enabled.includes((
|
|
28
|
+
const anyEnabled = flags.some((flag) => _enabled.includes(flagValue(flag)));
|
|
28
29
|
if (!(anyEnabled || dev))
|
|
29
30
|
not_good(input, 503, {
|
|
30
31
|
message: 'Feature not enabled'
|
|
@@ -47,5 +48,5 @@ export const FeatureFlag = {
|
|
|
47
48
|
* @param flag
|
|
48
49
|
* @param predicate
|
|
49
50
|
*/
|
|
50
|
-
is: (flag, predicate = enabled) => dev || predicate((
|
|
51
|
+
is: (flag, predicate = enabled) => dev || predicate(flagValue(flag))
|
|
51
52
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const matchesPattern: (pattern: RegExp, value: string) => boolean;
|
|
@@ -6,6 +6,13 @@ export const RequestMonitor = {
|
|
|
6
6
|
const { logger = console, record, } = options;
|
|
7
7
|
return async (event) => {
|
|
8
8
|
const locals = event.locals;
|
|
9
|
+
let clientIp;
|
|
10
|
+
try {
|
|
11
|
+
clientIp = event.getClientAddress?.();
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
// Some adapters cannot provide a client address for every request.
|
|
15
|
+
}
|
|
9
16
|
const requestId = locals.correlation_id ??
|
|
10
17
|
locals.request_id ??
|
|
11
18
|
event.request.headers.get('x-correlation-id') ??
|
|
@@ -20,7 +27,7 @@ export const RequestMonitor = {
|
|
|
20
27
|
request_id: requestId,
|
|
21
28
|
method: event.request.method,
|
|
22
29
|
route,
|
|
23
|
-
client_ip:
|
|
30
|
+
client_ip: clientIp
|
|
24
31
|
});
|
|
25
32
|
try {
|
|
26
33
|
const response = await event.resolve(event.event);
|
|
@@ -41,7 +48,7 @@ export const RequestMonitor = {
|
|
|
41
48
|
url: event.url.toString(),
|
|
42
49
|
status: response.status,
|
|
43
50
|
duration_ms: elapsedMs,
|
|
44
|
-
client_ip:
|
|
51
|
+
client_ip: clientIp
|
|
45
52
|
};
|
|
46
53
|
if (response.status >= 500) {
|
|
47
54
|
logger.error("http.request.completed", context);
|
|
@@ -69,7 +76,7 @@ export const RequestMonitor = {
|
|
|
69
76
|
route,
|
|
70
77
|
url: event.url,
|
|
71
78
|
duration_ms: elapsedMs,
|
|
72
|
-
client_ip:
|
|
79
|
+
client_ip: clientIp,
|
|
73
80
|
error: error instanceof Error
|
|
74
81
|
? { name: error.name, message: error.message }
|
|
75
82
|
: { value: await Promise.resolve(JSON.stringify(error)).catch(() => String(error)) }
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ClientIp } from "./internal/client_ip.js";
|
|
2
|
+
import { matchesPattern } from "./internal/matches_pattern.js";
|
|
2
3
|
/**
|
|
3
4
|
* Single-process, in-memory store. Fine for one instance; buckets aren't
|
|
4
5
|
* shared across processes/machines, so a multi-instance deployment (eg.
|
|
@@ -31,7 +32,7 @@ const defaultStore = createMemoryRateLimitStore();
|
|
|
31
32
|
const includes = (input, element) => {
|
|
32
33
|
return input[Symbol.iterator]().find((v) => v === element);
|
|
33
34
|
};
|
|
34
|
-
const findRule = (rules, method, pathname) => rules.find((rule) => rule.pattern
|
|
35
|
+
const findRule = (rules, method, pathname) => rules.find((rule) => matchesPattern(rule.pattern, pathname) && (!rule.methods || includes(rule.methods, method.toUpperCase())));
|
|
35
36
|
const getClientIdentifier = (input) => {
|
|
36
37
|
try {
|
|
37
38
|
const address = input.getClientAddress?.();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/sveltekit-enhance",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Composable enhance and form utilities for SvelteKit actions, loads, methods, and hooks.",
|
|
5
5
|
"author": "A.P.A. Slaa (a.p.a.slaa@projectsource.nl)",
|
|
6
6
|
"engines": {
|
|
@@ -108,10 +108,19 @@
|
|
|
108
108
|
{
|
|
109
109
|
"assets": [
|
|
110
110
|
"package.json",
|
|
111
|
+
"package-lock.json",
|
|
111
112
|
"CHANGELOG.md"
|
|
112
113
|
],
|
|
113
114
|
"message": "sveltekit-enhance(release): 🚀 v${nextRelease.version}\n\n${nextRelease.notes}"
|
|
114
115
|
}
|
|
116
|
+
],
|
|
117
|
+
[
|
|
118
|
+
"@semantic-release/github",
|
|
119
|
+
{
|
|
120
|
+
"successCommentCondition": false,
|
|
121
|
+
"failCommentCondition": false,
|
|
122
|
+
"releasedLabels": false
|
|
123
|
+
}
|
|
115
124
|
]
|
|
116
125
|
]
|
|
117
126
|
}
|