Search by

hassan-razhei / entity-streamer

Hassan-Razhei

High-performance RFC 7233 partial content HTTP 206 video and audio streaming engine for Laravel.

Package info

github.com/Hassan-Razhei/entity-streamer

pkg:composer/hassan-razhei/entity-streamer

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-11-24 23:02 UTC

This package is auto-updated.

Last update: 2026-11-24 23:38:58 UTC


README

High-performance RFC 7233 byte-range HTTP 206 video & audio streaming engine for Laravel.

Tests License: MIT PHP Laravel

๐ŸŒŸ Overview

entity-streamer is a memory-safe binary streaming package designed to serve multi-gigabyte video and audio files in Laravel applications. It strictly complies with RFC 7233 (Range Requests), enabling seamless seeking, fast scrubbing, and bandwidth optimization in HTML5 players (<video>, <audio>), Mobile Apps, and desktop browsers.

๐Ÿš€ Key Features

  • Strict RFC 7233 Engine: Full support for closed (0-1024), open-ended (1048576-), and suffix ranges (-500), returning proper HTTP 206 Partial Content or 416 Range Not Satisfiable.
  • Memory-Safe Kernel: Streams data in configurable chunks (default: 64 KB) without loading large media into server RAM.
  • Storage Agnostic: Native integration with Laravel Storage (Local, S3, MinIO) with automatic fallback path lookup.
  • Format Intelligence: Automatic MIME detection via binary magic bytes for all major video (MP4, WebM, MKV, QuickTime, AVI) and audio formats (MP3, WAV, AAC, FLAC, OGG, M4A).
  • HTTP Cache Optimization: Automatic ETag and Last-Modified generation with 304 Not Modified validation to conserve bandwidth.
  • Security Shield: Multi-layer sanitization against path traversal (.., %252e%252e, null bytes, system devices, and hidden files).
  • Signed URLs & Anti-Leech: HMAC-SHA256 time-limited streaming URLs with optional client IP binding to prevent hotlinking.
  • Eloquent Integration: HasStreamableMedia trait for one-line media streaming directly from models.
  • Developer Experience: Fluent builder, Streamer facade, and response()->streamMedia() macro.

๐Ÿ“ฅ Installation

Require the package via Composer:

composer require hassan-razhei/entity-streamer

Publish the configuration file (optional):

php artisan vendor:publish --tag=streamer-config

๐Ÿ›  Usage Examples

1. Using the Facade

use EntityStreamer\Facades\Streamer;

Route::get('/watch/{video}', function (string $video, Request $request) {
    return Streamer::disk('s3')
        ->withChunkSize(64 * 1024)
        ->asInline("lesson.mp4")
        ->toResponse($request);
});

2. Using the Response Macro

Route::get('/listen/{surah}', function (string $surah) {
    return response()->streamMedia("audio/{$surah}.mp3", disk: 'public');
});

3. Eloquent Model Integration

Implement CanStreamMedia and use HasStreamableMedia trait on your model:

namespace App\Models;

use EntityStreamer\Contracts\CanStreamMedia;
use EntityStreamer\Traits\HasStreamableMedia;
use Illuminate\Database\Eloquent\Model;

class Lesson extends Model implements CanStreamMedia
{
    use HasStreamableMedia;

    protected $fillable = ['title', 'video_path'];
}

Stream directly inside your controller:

public function stream(Lesson $lesson, Request $request)
{
    return $lesson->streamResponse($request);
}

Generate signed temporary streaming URLs with Anti-Leech protection:

// Signed for 1 hour, bound to the current user's IP
$url = $lesson->getStreamUrl(expiration: 3600, bindToIp: true);

4. Forcing Download (Attachment Mode)

return Streamer::stream('exports/recorded_session.mp4')
    ->asAttachment('lecture_archive.mp4')
    ->toResponse($request);

๐Ÿงช Testing

Run the test suite:

php packages/entity-streamer/tests/run_tests.php

All 77 test cases cover:

  • RFC 7233 partial, open, and suffix ranges.
  • Memory bounding and zero-ballooning during 2GB+ streaming.
  • Forward and backward bidirectional seeking accuracy.
  • Directory traversal fuzzing and null-byte defenses.
  • HMAC-SHA256 signature verification and Anti-Leech protection.
  • HTML5 media player lifecycle simulations.

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.