https://github.com/zackiles/response-rewriter
Strip or replace any and all instances of a string in a Response object (Fetch-based): URLs, headers, bodies, unicode, streaming, multipart, and regex support
https://github.com/zackiles/response-rewriter
deno fetch proxy regex rewriter
Last synced: about 2 months ago
JSON representation
Strip or replace any and all instances of a string in a Response object (Fetch-based): URLs, headers, bodies, unicode, streaming, multipart, and regex support
- Host: GitHub
- URL: https://github.com/zackiles/response-rewriter
- Owner: zackiles
- License: mit
- Created: 2025-02-22T22:35:27.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-23T00:49:26.000Z (over 1 year ago)
- Last Synced: 2026-03-29T00:26:17.690Z (2 months ago)
- Topics: deno, fetch, proxy, regex, rewriter
- Language: TypeScript
- Homepage: https://jsr.io/@zackiles/response-rewriter
- Size: 53.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# response-rewriter
Deno library for modifying Response objects by replacing text patterns in headers, bodies, and URLs. Supports streaming responses, multiple encodings, and complex Unicode scenarios. Handles even the most nightmarish of edge-cases. Perfect for rewriting Responses in proxies.
[](https://jsr.io/@zackiles/response-rewriter)
[](https://jsr.io/@zackiles/response-rewriter)
[](https://jsr.io/@zackiles)
## Features
- 🔄 Replace text in Response objects (headers, body, URLs)
- 🌊 Streaming support with proper chunking
- 🌐 Full Unicode support with proper surrogate pair handling
- 📝 JSON-aware with circular reference detection
- 🎭 Multipart response support
- 📚 Comprehensive test coverage
- 🔒 Safe header handling
- 🚀 High performance with minimal memory usage
## Installation
```bash
deno add jsr:@zackiles/response-rewriter
```
## Usage
```typescript
import { replaceInResponse } from "jsr:@zackiles/response-rewriter";
// Simple string replacement
const response = new Response("Hello world!", {
headers: { "x-custom": "old-value" }
});
const modified = await replaceInResponse("world", "Deno", response);
console.log(await modified.text()); // "Hello Deno!"
console.log(modified.headers.get("x-custom")); // "old-value"
// Regex with capture groups
const jsonResponse = new Response(JSON.stringify({
email: "user@old-domain.com",
nested: { value: "prefix_old_suffix" }
}), {
headers: { "content-type": "application/json" }
});
const regex = /(\w+)@old-domain\.com/;
const modified = await replaceInResponse(
regex,
"$1@new-domain.com",
jsonResponse
);
const result = await modified.json();
console.log(result.email); // "user@new-domain.com"
```
## API
### replaceInResponse(search, replacement, response)
Replaces all occurrences of a pattern in both headers and body of an HTTP response.
#### Parameters
- `search` (string | RegExp): The text or regex pattern to search for
- `replacement` (string): The replacement text
- `response` (Response): The original HTTP response to modify
#### Returns
- Promise: A new Response object with the replacements applied
## Advanced Features
### Streaming Support
The library properly handles chunked transfer encoding and streaming responses:
```typescript
const stream = new ReadableStream({
start(controller) {
controller.enqueue('Hello ');
controller.enqueue('world!');
controller.close();
}
});
const response = new Response(stream, {
headers: { "transfer-encoding": "chunked" }
});
const modified = await replaceInResponse("world", "Deno", response);
// Streams the response with replacements
```
### Unicode & Surrogate Pairs
Properly handles complex Unicode scenarios including surrogate pairs:
```typescript
const response = new Response(
"Hello 🌎!", // Uses surrogate pairs
{ headers: { "content-type": "text/plain; charset=utf-8" }}
);
const modified = await replaceInResponse("🌎", "🦕", response);
console.log(await modified.text()); // "Hello 🦕!"
```
### JSON Processing
Intelligently processes JSON content with circular reference protection:
```typescript
const obj = { name: "old" };
obj.self = obj; // Circular reference
const response = new Response(JSON.stringify(obj), {
headers: { "content-type": "application/json" }
});
const modified = await replaceInResponse("old", "new", response);
// Safely handles circular references
```
## License
MIT - See [LICENSE](LICENSE) for details.