Skip to content

Commit dfd6b19

Browse files
authored
docs: Add common JavaScript rule patterns (#66)
1 parent b0dac54 commit dfd6b19

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

docs/guide/javascript-rules.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/guide/rule-engines/javascript.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,50 @@ r.host === 'api.example.com' && r.path.startsWith('/v1/public/')
8888
['GET', 'HEAD', 'OPTIONS'].includes(r.method)
8989
```
9090

91+
### Host Whitelist
92+
93+
```javascript
94+
// Simple host whitelist
95+
const allowedHosts = [
96+
'github.com',
97+
'api.github.com',
98+
'raw.githubusercontent.com',
99+
'codeload.github.com'
100+
];
101+
102+
allowedHosts.includes(r.host)
103+
```
104+
105+
### Host + Method Whitelist
106+
107+
```javascript
108+
// Allow specific methods only for certain hosts
109+
const rules = [
110+
{host: 'api.github.com', methods: ['GET', 'POST']},
111+
{host: 'github.com', methods: ['GET']},
112+
{host: 'uploads.github.com', methods: ['POST', 'PUT']}
113+
];
114+
115+
rules.some(rule =>
116+
rule.host === r.host && rule.methods.includes(r.method)
117+
)
118+
```
119+
120+
### Regexp Matching on Method + URL
121+
122+
```javascript
123+
// Whitelist patterns for METHOD + URL combinations
124+
const patterns = [
125+
/^GET api\.github\.com\/repos\/.+/,
126+
/^POST api\.example\.com\/v[12]\/.*/,
127+
/^(GET|HEAD) .*\.cdn\.example\.com\/.*\.(jpg|png|gif)/
128+
];
129+
130+
// Build request string using host and path for simpler patterns
131+
const requestString = `${r.method} ${r.host}${r.path}`;
132+
patterns.some(pattern => pattern.test(requestString))
133+
```
134+
91135
## When to Use
92136

93137
Best for:
@@ -97,4 +141,4 @@ Best for:
97141

98142
Avoid for:
99143
- Stateful processing (use line processor)
100-
- External integrations (use shell or line processor)
144+
- External integrations (use shell or line processor)

0 commit comments

Comments
 (0)