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.
- 🔄 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
deno add jsr:@zackiles/response-rewriter
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"
Replaces all occurrences of a pattern in both headers and body of an HTTP response.
search
(string | RegExp): The text or regex pattern to search forreplacement
(string): The replacement textresponse
(Response): The original HTTP response to modify
- Promise: A new Response object with the replacements applied
The library properly handles chunked transfer encoding and streaming responses:
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
Properly handles complex Unicode scenarios including surrogate pairs:
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 🦕!"
Intelligently processes JSON content with circular reference protection:
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
MIT - See LICENSE for details.