zbateson/stream-decorators
Lightweight PHP stream decorators for composing and filtering streams. Wrap any resource to add behaviors like buffering, limiting, logging, or on-the-fly transforms while keeping a standard stream interface. Useful for email parsing, I/O pipelines, and testing.
Architecture fit:
Moderate for Laravel applications requiring low-level stream manipulation (e.g., custom file processing, MIME encoding/decoding, or legacy system integrations). Poor fit for high-level HTTP handling (e.g., API responses/requests), where Laravel’s built-in abstractions (e.g., Symfony\Component\HttpFoundation\StreamedResponse) or Guzzle’s StreamInterface implementations suffice. Ideal for:
Integration feasibility: Low due to:
Illuminate\Http\StreamedResponse) already uses Guzzle’s StreamInterface, which overlaps with this package’s functionality. Direct integration risks redundancy.zbateson/stream-decorators) is unverified (last release date is invalid/future-dated). Assess via:
zbateson/stream-decorators (if available).CharsetStream with multi-byte encodings).Technical risk: High due to:
spatie/stream-decorators).preg_replace in PregReplaceFilterStream could expose ReDoS risks if misused).GuzzleHttp\Psr7 (v2.6) should align, but test DecoratedCachingStream (depends on zbateson/mb-wrapper).Base64Stream buffers data). Benchmark for high-throughput use cases.Key questions:
Symfony\Component\Mime\Part\DataPart (for MIME).LimitStream, FilterStream (for HTTP).Symfony\Component\StreamedResponse (for streaming responses).StreamInterface or use php://memory?CharsetStream may fail silently on invalid encodings. Define fallback strategies.Stack fit: Limited to niche scenarios where Laravel’s abstractions are insufficient:
CharsetStream.LimitStream or RateLimitStream (if extended) for throttling.fopen resources) for compatibility.php://stdin/stdout for real-time processing (e.g., log filtering).Poor fit for:
Migration path:
fopen + manual fread/fwrite).Base64Stream for file attachments).use ZBateson\StreamDecorators\Base64Stream;
use Illuminate\Support\Facades\Storage;
$file = Storage::disk('s3')->readStream('large-file.bin');
$encoded = new Base64Stream($file);
return response()->stream(fn() => $encoded);
mb_convert_encoding + base64_encode for CharsetStream + Base64Stream).Compatibility:
CharsetStream edge cases.guzzlehttp/psr7 v2+. Laravel’s default (^2.6) should suffice.zbateson/mb-wrapper (for CharsetStream) must be compatible with Laravel’s ext-mbstring.resource or StreamInterface. Avoid StreamWrapper with CharsetStream (per README).Sequencing:
Base64Stream, CharsetStream, and LimitStream in isolation.SwiftMailer/MimeMessage attachments with decorated streams.Storage streams (e.g., readStream())."require": {
"zbateson/stream-decorators": "^3.0"
}
LimitStream for uploads >10MB").Maintenance:
CharsetStream + Base64Stream).StreamInterface).tell() position).DecoratedCachingStream buffers data).Support:
CharsetStream failures").Scaling:
DecoratedCachingStream) increase memory usage. Avoid for large streams (>100MB).LimitStream or extend SeekingLimitStream for throughput control.Failure modes:
| Decorator | Failure Scenario | Mitigation Strategy |
|---|---|---|
CharsetStream |
Invalid source/target encoding | Validate encodings; fall back to UTF-8. |
Base64Stream |
Corrupted Base64 data | Wrap in try-catch; log errors. |
LimitStream |
Seek operations on non-seekable streams | Document limitations; avoid seeking. |
PregReplaceFilterStream |
ReDoS via malicious input | Sanitize input; limit regex complexity. |
DecoratedCachingStream |
Memory exhaustion | Set size limits; avoid for large streams. |
Ramp-up:
How can I help you explore Laravel packages today?