Skip to content

Commit d1dd976

Browse files
committed
Improve docs and tests, and add instance conditions and href rewriting
1 parent ce25f0a commit d1dd976

32 files changed

+1595
-363
lines changed

README.md

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ console.log(response.body.toString())
6262
* `docroot` {String} Document root for PHP. **Default:** process.cwd()
6363
* `throwRequestErrors` {Boolean} Throw request errors rather than returning
6464
responses with error codes. **Default:** false
65+
* `rewriter` {Rewriter} Optional rewrite rules. **Default:** `undefined`
6566
* Returns: {Php}
6667

6768
Construct a new PHP instance to which to dispatch requests.
@@ -560,9 +561,111 @@ headers.forEach((value, name, headers) => {
560561
})
561562
```
562563

564+
### `new Rewriter(input)`
565+
566+
* `rules` {Array} The set of rewriting rules to apply to each request
567+
* `operation` {String} Operation type (`and` or `or`) **Default:** `and`
568+
* `conditions` {Array} Conditions to match against the request
569+
* `type` {String} Condition type
570+
* `args` {String} Arguments to pass to condition constructor
571+
* `rewriters` {Array} Rewrites to apply if the conditions match
572+
* `type` {String} Rewriter type
573+
* `args` {String} Arguments to pass to rewriter constructor
574+
* Returns: {Rewriter}
575+
576+
Construct a Rewriter to rewrite requests before they are dispatched to PHP.
577+
578+
```js
579+
import { Rewriter } from '@platformatic/php-node'
580+
581+
const rewriter = new Rewriter([{
582+
conditions: [{
583+
type: 'header',
584+
args: ['User-Agent', '^(Mozilla|Chrome)']
585+
}],
586+
rewriters: [{
587+
type: 'path',
588+
args: ['^/old-path/(.*)$', '/new-path/$1']
589+
}]
590+
}])
591+
```
592+
593+
#### Conditions
594+
595+
There are several types of conditions which may be used to match against the
596+
request. Each condition type has a set of arguments which are passed to the
597+
constructor of the condition. The condition will be evaluated against the
598+
request and if it matches, the rewriters will be applied.
599+
600+
The available condition types are:
601+
602+
- `exists` Matches if request path exists in docroot.
603+
- `not_exists` Matches if request path does not exist in docroot.
604+
- `header(name, pattern)` Matches named header against a pattern.
605+
- `name` {String} The name of the header to match.
606+
- `pattern` {String} The regex pattern to match against the header value.
607+
- `method(pattern)` Matches request method against a pattern.
608+
- `pattern` {String} The regex pattern to match against the HTTP method.
609+
- `path(pattern)`: Matches request path against a pattern.
610+
- `pattern` {String} The regex pattern to match against the request path.
611+
612+
#### Rewriters
613+
614+
There are several types of rewriters which may be used to rewrite the request
615+
before it is dispatched to PHP. Each rewriter type has a set of arguments which
616+
are passed to the constructor of the rewriter. The rewriter will be applied to
617+
the request if the conditions match.
618+
619+
The available rewriter types are:
620+
621+
- `header(name, replacement)` Sets a named header to a given replacement.
622+
- `name` {String} The name of the header to set.
623+
- `replacement` {String} The replacement string to use for named header.
624+
- `href(pattern, replacement)` Rewrites request path, query, and fragment to
625+
given replacement.
626+
- `pattern` {String} The regex pattern to match against the request path.
627+
- `replacement` {String} The replacement string to use for request path.
628+
- `method(replacement)` Sets the request method to a given replacement.
629+
- `replacement` {String} The replacement string to use for request method.
630+
- `path(pattern, replacement)` Rewrites request path to given replacement.
631+
- `pattern` {String} The regex pattern to match against the request path.
632+
- `replacement` {String} The replacement string to use for request path.
633+
634+
### `rewriter.rewrite(request, docroot)`
635+
636+
- `request` {Object} The request object.
637+
- `docroot` {String} The document root.
638+
639+
Rewrites the given request using the rules provided to the rewriter.
640+
641+
This is mainly exposed for testing purposes. It is not recommended to use
642+
directly. Rather, the `rewriter` should be provided to the `Php` constructor
643+
to allow rewriting to occur within the PHP environment where it will be aware
644+
of the original `REQUEST_URI` state.
645+
646+
```js
647+
import { Rewriter } from '@platformatic/php-node'
648+
649+
const rewriter = new Rewriter([{
650+
rewriters: [{
651+
type: 'path',
652+
args: ['^(.*)$', '/base/$1'
653+
}]
654+
}])
655+
656+
const request = new Request({
657+
url: 'http://example.com/foo/bar'
658+
})
659+
660+
const modified = rewriter.rewrite(request, import.meta.dirname)
661+
662+
console.log(modified.url) // http://example.com/base/foo/bar
663+
```
664+
563665
## Contributing
564666
565-
This project is part of the [Platformatic](https://github.com/platformatic) ecosystem. Please refer to the main repository for contribution guidelines.
667+
This project is part of the [Platformatic](https://github.com/platformatic)
668+
ecosystem. Please refer to the main repository for contribution guidelines.
566669
567670
## License
568671

__test__/handler.spec.mjs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,13 @@ test('Accept rewriter', async (t) => {
175175
})
176176
t.teardown(() => mockroot.clean())
177177

178-
const rewrite = new Rewriter([
178+
const rewriter = new Rewriter([
179179
{
180180
conditions: [
181-
{
182-
type: 'path',
183-
args: ['^/rewrite_me$']
184-
}
181+
{ type: 'path', args: ['^/rewrite_me$'] }
185182
],
186183
rewriters: [
187-
{
188-
type: 'path',
189-
args: ['^/rewrite_me$', '/index.php']
190-
}
184+
{ type: 'path', args: ['^/rewrite_me$', '/index.php'] }
191185
]
192186
}
193187
])
@@ -196,7 +190,7 @@ test('Accept rewriter', async (t) => {
196190
argv: process.argv,
197191
docroot: mockroot.path,
198192
throwRequestErrors: true,
199-
rewrite
193+
rewriter
200194
})
201195

202196
const req = new Request({

0 commit comments

Comments
 (0)