Add i18n, tests, and update documentation

Introduces internationalization (i18n) support with English and Turkish, adds unit tests and test setup with Vitest and React Testing Library, and updates documentation including README and changelog. Removes legacy publishing and usage guides, refactors components to use translation system, and updates build and test scripts in package.json. Also adds new utility modules for HLS and CORS, and improves PlayerContext and SettingsMenu for language support.
This commit is contained in:
hibna
2025-10-29 13:10:07 +03:00
parent e75d241421
commit bad1cc6ca0
26 changed files with 1843 additions and 1341 deletions
+70
View File
@@ -0,0 +1,70 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
- ✅ Test infrastructure with Vitest and React Testing Library
- 🌍 Internationalization (i18n) system with English and Turkish support
- 📊 Comprehensive README with comparison table
- 📝 44 unit tests covering core functionality
- 🔄 Language prop for VideoPlayer component
### Changed
- 🌐 Replaced hardcoded strings with translation system
- 📦 Updated build configuration for better tree-shaking
### Fixed
- 🐛 TypeScript errors in test setup file
## [0.1.3] - 2025-01-29
### Changed
- 📦 Fixed CSS export path in package.json
- 🔧 Build configuration improvements
## [0.1.2] - 2025-01-29
### Added
- 📘 TypeScript declaration files support with vite-plugin-dts
- 🎯 Full type safety for all exports
## [0.1.1] - 2025-01-29
### Added
- 🎬 Initial release of Modern Video Player
- ▶️ Core playback controls (play, pause, seek, volume)
- 🎨 Modern, responsive UI with auto-hiding controls
- ⌨️ Comprehensive keyboard shortcuts (15+ shortcuts)
- 📱 Touch gesture support for mobile devices
- 🎞️ HLS streaming support with automatic quality switching
- 📝 Subtitle support (WebVTT and SRT)
- 🎵 Multiple audio track support
- 🖼️ Picture-in-Picture and Fullscreen support
- 🎚️ Playback speed control (0.25x to 2x)
- 🎨 Theme customization with CSS variables
- 🔧 Zero runtime dependencies
- 📦 Tiny bundle size (~15KB gzipped)
- 🔄 HTTP Range Request support for large files
- 🛡️ CORS error handling and helpful error messages
- ⚡ Lazy loading for HLS.js and settings menu
- 🎯 React 18+ support
- 📘 Full TypeScript support
### Technical Features
- 🏗️ Built with React 18, TypeScript 5, and Vite 7
- 🎨 CSS modules and CSS variables for styling
- 🧩 Component-based architecture with React Context
- 🪝 Custom hooks for keyboard shortcuts and touch gestures
- 📦 ESM and UMD build outputs
- 🔧 Aggressive bundle optimization with Terser
- 🌲 Tree-shaking support
[Unreleased]: https://gitea.hibna.com.tr/hibna/video-player/compare/v0.1.3...HEAD
[0.1.3]: https://gitea.hibna.com.tr/hibna/video-player/compare/v0.1.2...v0.1.3
[0.1.2]: https://gitea.hibna.com.tr/hibna/video-player/compare/v0.1.1...v0.1.2
[0.1.1]: https://gitea.hibna.com.tr/hibna/video-player/releases/tag/v0.1.1
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Alper
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-397
View File
@@ -1,397 +0,0 @@
# Gitea Self-Hosted npm Registry'ye Yayınlama Kılavuzu
Bu kılavuz, `@alper/video-player` paketinin Gitea self-hosted npm registry'ye nasıl yayınlanacağını adım adım açıklar.
---
## 📋 Ön Gereksinimler
- ✅ Çalışan bir Gitea sunucusu
- ✅ Gitea hesabınıza erişim
- ✅ Node.js ve pnpm/npm kurulu
- ✅ Bu proje build edilmiş durumda
---
## 🔑 Adım 1: Gitea Access Token Oluşturma
1. **Gitea web arayüzüne giriş yapın**
- Tarayıcınızda `https://gitea.yourdomain.com` adresine gidin
- Kullanıcı adı ve şifrenizle giriş yapın
2. **Settings sayfasına gidin**
- Sağ üst köşedeki profil resmine tıklayın
- "Settings" seçeneğini seçin
3. **Applications bölümüne gidin**
- Sol menüden "Applications" seçeneğini tıklayın
4. **Yeni token oluşturun**
- "Generate New Token" butonuna tıklayın
- **Token Name:** `npm-publish-video-player` (veya istediğiniz bir isim)
- **Permissions (Yetkiler):**
-`write:package` - Paket yükleme yetkisi
-`read:package` - Paket okuma yetkisi (diğer projelerde kullanmak için)
- "Generate Token" butonuna tıklayın
5. **Token'ı kopyalayın**
- ⚠️ **ÖNEMLİ:** Token sadece bir kez gösterilir!
- Token'ı güvenli bir yere kaydedin (örn: password manager)
- Örnek token: `1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t`
---
## 📝 Adım 2: package.json Dosyasını Düzenleme
`package.json` dosyasını açın ve aşağıdaki bilgileri **kendi Gitea sunucunuza göre** düzenleyin:
```json
{
"repository": {
"type": "git",
"url": "https://gitea.yourdomain.com/your-username/video-player.git"
},
"publishConfig": {
"registry": "https://gitea.yourdomain.com/api/packages/your-username/npm/"
}
}
```
**Değiştirilmesi gerekenler:**
- `gitea.yourdomain.com` → Gitea sunucunuzun domain'i
- `your-username` → Gitea kullanıcı adınız
**Örnek:**
```json
{
"repository": {
"type": "git",
"url": "https://git.example.com/alper/video-player.git"
},
"publishConfig": {
"registry": "https://git.example.com/api/packages/alper/npm/"
}
}
```
---
## 🔐 Adım 3: npm Login ile Kimlik Doğrulama
Terminal'de aşağıdaki komutu çalıştırın:
```bash
npm config set @alper:registry https://gitea.yourdomain.com/api/packages/your-username/npm/
npm login --scope=@alper --registry=https://gitea.yourdomain.com/api/packages/your-username/npm/
```
**Değiştirin:**
- `gitea.yourdomain.com` → Gitea domain'iniz
- `your-username` → Gitea kullanıcı adınız
**Giriş bilgileri istenecek:**
```
Username: your-gitea-username
Password: 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t (Adım 1'de oluşturduğunuz token)
Email: your-email@example.com
```
**Not:** Password kısmına Gitea şifrenizi değil, **token**'ı girin!
Başarılı olursa şu mesajı görmelisiniz:
```
Logged in as your-username on https://gitea.yourdomain.com/api/packages/your-username/npm/.
```
---
## 🏗️ Adım 4: Library Build
Paketi yayınlamadan önce production build alın:
```bash
# TypeScript compile ve Vite build
pnpm run build:lib
```
**Build başarılı olduysa:**
- `dist/` klasöründe şu dosyalar oluşacak:
-`video-player.js` (ESM format)
-`video-player.umd.cjs` (UMD format)
-`index.d.ts` (TypeScript type definitions)
-`style.css` (CSS dosyası)
**Kontrol edin:**
```bash
ls dist/
# veya Windows'ta:
dir dist\
```
---
## 📤 Adım 5: npm Publish
Artık paketi Gitea registry'ye yayınlayabilirsiniz:
```bash
npm publish
```
**Başarılı olursa şu çıktıyı görmelisiniz:**
```
npm notice
npm notice package: @alper/video-player@0.1.0
npm notice === Tarball Contents ===
npm notice 1.2kB package.json
npm notice 8.5kB dist/video-player.js
npm notice 7.8kB dist/video-player.umd.cjs
npm notice 2.1kB dist/index.d.ts
npm notice 3.4kB dist/style.css
npm notice === Tarball Details ===
npm notice name: @alper/video-player
npm notice version: 0.1.0
npm notice package size: 23.0 kB
npm notice unpacked size: 45.2 kB
npm notice total files: 5
+ @alper/video-player@0.1.0
```
---
## ✅ Adım 6: Yayını Doğrulama
### Gitea Web Arayüzünde Kontrol
1. Gitea'ya giriş yapın
2. Üst menüden "Packages" seçeneğine tıklayın
3. `@alper/video-player` paketini görmelisiniz
4. Pakete tıklayarak detayları görüntüleyin
### Komut Satırından Kontrol
```bash
npm view @alper/video-player --registry=https://gitea.yourdomain.com/api/packages/your-username/npm/
```
---
## 🔄 Adım 7: Güncelleme Yayınlama
Paket üzerinde değişiklik yaptığınızda yeni versiyon yayınlamak için:
### 1. Versiyonu Güncelle
```bash
# Patch version (0.1.0 → 0.1.1) - Bug fix
npm version patch
# Minor version (0.1.0 → 0.2.0) - Yeni özellik
npm version minor
# Major version (0.1.0 → 1.0.0) - Breaking change
npm version major
# Veya manuel olarak package.json'da version'ı değiştirin
```
### 2. Build Al
```bash
pnpm run build:lib
```
### 3. Yayınla
```bash
npm publish
```
### 4. Git'e Push Et (Opsiyonel)
```bash
git push && git push --tags
```
---
## 🚀 Diğer Projelerde Kullanma
### Adım 1: Diğer Projenizde .npmrc Oluşturun
Projenizin root dizininde `.npmrc` dosyası oluşturun:
```ini
@alper:registry=https://gitea.yourdomain.com/api/packages/your-username/npm/
//gitea.yourdomain.com/api/packages/your-username/npm/:_authToken=${GITEA_TOKEN}
```
### Adım 2: Environment Variable Ayarlayın
```bash
# Linux/Mac
export GITEA_TOKEN=1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t
# Windows (PowerShell)
$env:GITEA_TOKEN="1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t"
# Veya .env dosyasına ekleyin:
echo "GITEA_TOKEN=1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t" >> .env
```
### Adım 3: Paketi Yükleyin
```bash
pnpm add @alper/video-player
```
### Adım 4: Kullanın
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function App() {
return <VideoPlayer src="/video.mp4" />;
}
```
Detaylı kullanım örnekleri için [README-USAGE.md](./README-USAGE.md) dosyasına bakın.
---
## 🐛 Sorun Giderme
### 1. "401 Unauthorized" Hatası
**Sebep:** Token yanlış veya süresi dolmuş
**Çözüm:**
```bash
# Tekrar login olun
npm login --scope=@alper --registry=https://gitea.yourdomain.com/api/packages/your-username/npm/
```
### 2. "403 Forbidden" Hatası
**Sebep:** Token'da `write:package` yetkisi yok
**Çözüm:**
- Gitea'da yeni token oluşturun (Adım 1)
- `write:package` yetkisini seçin
- Yeni token ile login olun
### 3. "Package already exists" Hatası
**Sebep:** Aynı versiyonu tekrar yayınlamaya çalışıyorsunuz
**Çözüm:**
```bash
# Versiyonu artırın
npm version patch
npm publish
```
### 4. "dist/ folder not found" Hatası
**Sebep:** Build alınmamış
**Çözüm:**
```bash
pnpm run build:lib
npm publish
```
### 5. ".npmrc not working" Sorunu
**Sebep:** Environment variable ayarlanmamış
**Çözüm:**
```bash
# Token'ı kontrol edin
echo $GITEA_TOKEN # Linux/Mac
echo $env:GITEA_TOKEN # Windows PowerShell
# Ayarlanmamışsa ayarlayın
export GITEA_TOKEN=your-token # Linux/Mac
```
### 6. "Cannot find module" Hatası (Diğer Projelerde)
**Sebep:** .npmrc dosyası yok veya yanlış yapılandırılmış
**Çözüm:**
- `.npmrc` dosyasının proje root'unda olduğundan emin olun
- Registry URL'inin doğru olduğunu kontrol edin
- `pnpm install` veya `npm install` komutunu tekrar çalıştırın
---
## 📚 Faydalı Komutlar
```bash
# Hangi registry kullanılıyor?
npm config get @alper:registry
# Login durumunu kontrol et
npm whoami --registry=https://gitea.yourdomain.com/api/packages/your-username/npm/
# Paket bilgilerini görüntüle
npm view @alper/video-player
# Yüklü versiyonu kontrol et (diğer projelerde)
pnpm list @alper/video-player
# Tüm npm ayarlarını temizle (sorun yaşıyorsanız)
npm config delete @alper:registry
# Package-lock veya pnpm-lock dosyasını temizle
rm package-lock.json # veya pnpm-lock.yaml
pnpm install
```
---
## 🔒 Güvenlik Notları
1. **Token'ları asla git'e commit etmeyin**
- `.npmrc` dosyasını `.gitignore`'a ekleyin
- Token'ları environment variable olarak kullanın
2. **Token yetkilerini minimumda tutun**
- Sadece gerekli yetkileri verin (`read:package`, `write:package`)
3. **Token'ları düzenli olarak yenileyin**
- Eski token'ları silin
- Yeni token oluşturun ve güncelleyin
4. **Production ortamda environment variables kullanın**
- `.env` dosyasını git'e eklemeyin
- CI/CD sistemlerinde secret manager kullanın
---
## ✅ Checklist
Video Player Yayınlama:
- [ ] Gitea access token oluşturdum
- [ ] `package.json`'da `repository` ve `publishConfig` güncelledim
- [ ] `npm login` ile kimlik doğrulaması yaptım
- [ ] `pnpm run build:lib` ile build aldım
- [ ] `npm publish` ile yayınladım
- [ ] Gitea web arayüzünde paketi gördüm
Diğer Projelerde Kullanma:
- [ ] Projede `.npmrc` dosyası oluşturdum
- [ ] `GITEA_TOKEN` environment variable'ını ayarladım
- [ ] `pnpm add @alper/video-player` ile yükledim
- [ ] CSS dosyasını import ettim
- [ ] VideoPlayer component'ini kullandım
---
**Başarılar! 🎉**
Artık video player kütüphanenizi tüm projelerinizde kullanabilirsiniz.
-567
View File
@@ -1,567 +0,0 @@
# @alper/video-player - Kullanım Kılavuzu
Modern, özellik zengin React video player kütüphanesi. HLS streaming, altyazılar, klavye kısayolları, dokunmatik hareketler ve daha fazlasını destekler.
---
## 🚀 Temel Kullanım
### Minimum Örnek
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function App() {
return (
<VideoPlayer
src="https://example.com/video.mp4"
/>
);
}
```
### Özelliklerle Birlikte
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function App() {
return (
<VideoPlayer
src="https://example.com/video.mp4"
poster="https://example.com/poster.jpg"
autoplay={false}
loop={false}
muted={false}
controls={true}
keyboardShortcuts={true}
pictureInPicture={true}
className="my-video-player"
onPlay={() => console.log('Video başladı!')}
onPause={() => console.log('Video durdu!')}
onTimeUpdate={(time) => console.log('Zaman:', time)}
/>
);
}
```
---
## 🎨 Tema Özelleştirme
```tsx
import { VideoPlayer, PlayerTheme } from '@alper/video-player';
import '@alper/video-player/styles.css';
const myTheme: PlayerTheme = {
primaryColor: '#3b82f6', // Ana buton rengi
accentColor: '#2563eb', // Hover rengi
backgroundColor: '#1f2937', // Arka plan
textColor: '#f9fafb', // Metin rengi
};
function App() {
return (
<VideoPlayer
src="/video.mp4"
theme={myTheme}
/>
);
}
```
---
## 📺 HLS Streaming Desteği
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function StreamingVideo() {
return (
<VideoPlayer
src="https://example.com/stream/master.m3u8"
poster="/poster.jpg"
/>
);
}
```
**Not:** HLS desteği için `hls.js` otomatik olarak yüklenir (optional dependency).
---
## 📝 Altyazı Desteği
### VTT veya SRT Formatında
```tsx
import { VideoPlayer, SubtitleTrack } from '@alper/video-player';
import '@alper/video-player/styles.css';
const subtitles: SubtitleTrack[] = [
{
label: 'Türkçe',
src: '/subtitles/turkish.vtt',
srcLang: 'tr',
},
{
label: 'English',
src: '/subtitles/english.vtt',
srcLang: 'en',
},
{
label: 'Español',
src: '/subtitles/spanish.srt',
srcLang: 'es',
},
];
function VideoWithSubtitles() {
return (
<VideoPlayer
src="/video.mp4"
subtitles={subtitles}
/>
);
}
```
**Desteklenen formatlar:** VTT, SRT
---
## ⌨️ Klavye Kısayolları
Varsayılan olarak aktif. Devre dışı bırakmak için `keyboardShortcuts={false}` kullanın.
| Tuş | Eylem |
|-----|-------|
| `Space` | Oynat/Duraklat |
| `←` | 5 saniye geri |
| `→` | 5 saniye ileri |
| `↑` | Sesi artır (%5) |
| `↓` | Sesi azalt (%5) |
| `M` | Sesi kapat/aç |
| `F` | Tam ekran |
| `P` | Picture-in-Picture |
| `0-9` | Videonun %0-%90'ına git |
---
## 👆 Dokunmatik Hareketler
Mobil cihazlarda otomatik olarak aktiftir:
- **Tek dokunma:** Kontrolleri göster/gizle
- **Çift dokunma:** Oynat/Duraklat
- **Sağa kaydır:** 10 saniye ileri
- **Sola kaydır:** 10 saniye geri
---
## 🎛️ İleri Seviye: Context API Kullanımı
Kendi özel kontrollerinizi oluşturabilirsiniz:
```tsx
import {
PlayerProvider,
usePlayerContext,
VideoPlayer
} from '@alper/video-player';
import '@alper/video-player/styles.css';
function CustomControls() {
const {
videoState,
play,
pause,
seek,
setVolume,
toggleFullscreen,
setPlaybackRate
} = usePlayerContext();
return (
<div className="custom-controls">
{/* Oynat/Duraklat */}
<button onClick={() => videoState.playing ? pause() : play()}>
{videoState.playing ? '⏸ Duraklat' : '▶ Oynat'}
</button>
{/* İleri/Geri */}
<button onClick={() => seek(videoState.currentTime - 10)}>
-10s
</button>
<button onClick={() => seek(videoState.currentTime + 10)}>
+10s
</button>
{/* Ses Kontrolü */}
<input
type="range"
min="0"
max="1"
step="0.01"
value={videoState.volume}
onChange={(e) => setVolume(parseFloat(e.target.value))}
/>
<span>{Math.round(videoState.volume * 100)}%</span>
{/* Zaman Göstergesi */}
<div>
{formatTime(videoState.currentTime)} / {formatTime(videoState.duration)}
</div>
{/* Oynatma Hızı */}
<select
value={videoState.playbackRate}
onChange={(e) => setPlaybackRate(parseFloat(e.target.value))}
>
<option value="0.5">0.5x</option>
<option value="1">Normal</option>
<option value="1.5">1.5x</option>
<option value="2">2x</option>
</select>
{/* Tam Ekran */}
<button onClick={toggleFullscreen}>
{videoState.fullscreen ? '🗗 Çıkış' : '🗖 Tam Ekran'}
</button>
</div>
);
}
function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, '0')}`;
}
function App() {
return (
<PlayerProvider>
<VideoPlayer
src="/video.mp4"
controls={false} // Yerleşik kontrolleri gizle
/>
<CustomControls />
</PlayerProvider>
);
}
```
---
## 🔌 Next.js Entegrasyonu
Next.js'te SSR (Server-Side Rendering) sorunlarını önlemek için:
```tsx
'use client'; // App Router için
import dynamic from 'next/dynamic';
import '@alper/video-player/styles.css';
// VideoPlayer'ı client-side only olarak yükle
const VideoPlayer = dynamic(
() => import('@alper/video-player').then(mod => mod.VideoPlayer),
{ ssr: false }
);
export default function VideoPage() {
return (
<div className="container">
<h1>Video Player</h1>
<VideoPlayer
src="/videos/demo.mp4"
poster="/images/poster.jpg"
/>
</div>
);
}
```
---
## 📊 Props API Referansı
### VideoPlayerProps
| Prop | Tip | Varsayılan | Açıklama |
|------|-----|-----------|----------|
| `src` | `string` | **zorunlu** | Video URL'i (MP4, HLS, vb.) |
| `poster` | `string` | - | Poster resim URL'i |
| `autoplay` | `boolean` | `false` | Otomatik oynat |
| `loop` | `boolean` | `false` | Döngü modu |
| `muted` | `boolean` | `false` | Sessiz başlat |
| `controls` | `boolean` | `true` | Kontrolleri göster |
| `subtitles` | `SubtitleTrack[]` | - | Altyazı listesi |
| `theme` | `PlayerTheme` | - | Renk teması |
| `keyboardShortcuts` | `boolean` | `true` | Klavye desteği |
| `pictureInPicture` | `boolean` | `true` | PiP desteği |
| `className` | `string` | - | CSS sınıfı |
| `style` | `CSSProperties` | - | Inline stil |
### Event Callbacks
| Callback | Parametre | Açıklama |
|----------|-----------|----------|
| `onPlay` | `() => void` | Video başladığında |
| `onPause` | `() => void` | Video durduğunda |
| `onEnded` | `() => void` | Video bittiğinde |
| `onTimeUpdate` | `(time: number) => void` | Zaman güncellendiğinde |
| `onVolumeChange` | `(volume: number) => void` | Ses değiştiğinde |
| `onError` | `(error: Error) => void` | Hata oluştuğunda |
| `onLoadedMetadata` | `() => void` | Metadata yüklendiğinde |
| `onSeeking` | `() => void` | Arama başladığında |
| `onSeeked` | `() => void` | Arama bittiğinde |
### SubtitleTrack
```typescript
interface SubtitleTrack {
label: string; // Görünen isim (ör: "Türkçe")
src: string; // Altyazı dosya URL'i
srcLang: string; // Dil kodu (ör: "tr", "en")
}
```
### PlayerTheme
```typescript
interface PlayerTheme {
primaryColor?: string; // Ana renk (varsayılan: #ef4444)
accentColor?: string; // Vurgu rengi (varsayılan: #dc2626)
backgroundColor?: string; // Arka plan rengi
textColor?: string; // Metin rengi
}
```
---
## 🎯 usePlayerContext Hook API
```typescript
const {
// Video State
videoState: {
playing: boolean;
currentTime: number;
duration: number;
buffered: TimeRanges;
volume: number;
muted: boolean;
playbackRate: number;
fullscreen: boolean;
pictureInPicture: boolean;
loading: boolean;
error: string | null;
seeking: boolean;
},
// UI State
uiState: {
controlsVisible: boolean;
settingsOpen: boolean;
volumeControlOpen: boolean;
},
// Settings
settings: {
quality: string | null;
subtitle: string | null;
playbackRate: number;
},
// Actions
play: () => void;
pause: () => void;
seek: (time: number) => void;
setVolume: (volume: number) => void;
toggleMute: () => void;
setPlaybackRate: (rate: number) => void;
toggleFullscreen: () => void;
togglePictureInPicture: () => void;
} = usePlayerContext();
```
---
## 💡 Kullanım Örnekleri
### 1. Analytics Entegrasyonu
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function AnalyticsVideo() {
const trackEvent = (event: string, data?: any) => {
// Analytics servisinize gönderin
console.log('Analytics:', event, data);
};
return (
<VideoPlayer
src="/video.mp4"
onPlay={() => trackEvent('video_play')}
onPause={() => trackEvent('video_pause')}
onEnded={() => trackEvent('video_complete')}
onTimeUpdate={(time) => {
// Her %25'te bir event gönder
if (time % 25 === 0) {
trackEvent('video_progress', { percentage: time });
}
}}
onError={(error) => trackEvent('video_error', { message: error.message })}
/>
);
}
```
### 2. Playlist Sistemi
```tsx
import { useState } from 'react';
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
const playlist = [
{ id: 1, title: 'Video 1', src: '/videos/1.mp4' },
{ id: 2, title: 'Video 2', src: '/videos/2.mp4' },
{ id: 3, title: 'Video 3', src: '/videos/3.mp4' },
];
function PlaylistPlayer() {
const [currentIndex, setCurrentIndex] = useState(0);
const currentVideo = playlist[currentIndex];
const handleEnded = () => {
// Sonraki videoya geç
if (currentIndex < playlist.length - 1) {
setCurrentIndex(currentIndex + 1);
}
};
return (
<div>
<VideoPlayer
key={currentVideo.id}
src={currentVideo.src}
onEnded={handleEnded}
/>
<div className="playlist">
{playlist.map((video, index) => (
<button
key={video.id}
onClick={() => setCurrentIndex(index)}
className={index === currentIndex ? 'active' : ''}
>
{video.title}
</button>
))}
</div>
</div>
);
}
```
### 3. Çoklu Kalite Desteği
```tsx
import { VideoPlayer } from '@alper/video-player';
import '@alper/video-player/styles.css';
function QualityVideo() {
// HLS kullanıyorsanız otomatik kalite seçimi vardır
// Manuel kalite için farklı kaynak URL'leri kullanabilirsiniz
return (
<VideoPlayer
src="/videos/stream/master.m3u8" // HLS master playlist
// Otomatik olarak kaliteler algılanır ve ayarlarda gösterilir
/>
);
}
```
---
## 🐛 Sorun Giderme
### CSS Stilleri Yüklenmiyor
Emin olun ki CSS dosyasını import ettiniz:
```tsx
import '@alper/video-player/styles.css';
```
### HLS Videoları Çalışmıyor
1. `hls.js` yüklü mü kontrol edin:
```bash
pnpm add hls.js
```
2. Video URL'inin `.m3u8` uzantılı olduğundan emin olun
### TypeScript Hataları
`tsconfig.json`'da `skipLibCheck: true` ayarlayın veya paketi güncelleyin:
```bash
pnpm update @alper/video-player
```
### Next.js'te Hydration Hatası
VideoPlayer'ı `dynamic` import ile yükleyin (`ssr: false`):
```tsx
const VideoPlayer = dynamic(
() => import('@alper/video-player').then(mod => mod.VideoPlayer),
{ ssr: false }
);
```
---
## 📦 Bundle Boyutu
- **Gzipped:** ~8KB (hls.js hariç)
- **hls.js ile:** ~100KB (optional, sadece HLS kullanıyorsanız yüklenir)
---
## 🔄 Güncelleme
```bash
# En son versiyonu yükle
pnpm update @alper/video-player
# Belirli bir versiyonu yükle
pnpm add @alper/video-player@0.2.0
# Hangi versiyon yüklü?
pnpm list @alper/video-player
```
---
## 📄 Lisans
MIT
---
## 💬 Destek
Sorularınız için projeyi geliştiren kişiyle iletişime geçin.
+33 -1
View File
@@ -1,6 +1,38 @@
# 🎬 Modern Video Player
A feature-rich, modern video player library built with React, TypeScript, and Vite. Designed for reusability across multiple projects with zero runtime dependencies.
[![npm version](https://img.shields.io/npm/v/@alper/video-player.svg?style=flat-square)](https://www.npmjs.com/package/@alper/video-player)
[![Bundle Size](https://img.shields.io/bundlephobia/minzip/@alper/video-player?style=flat-square)](https://bundlephobia.com/package/@alper/video-player)
[![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue?style=flat-square)](https://www.typescriptlang.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://opensource.org/licenses/MIT)
**The smallest React video player with the most features.** Only **~15KB gzipped** with zero runtime dependencies!
A feature-rich, modern video player library built with React, TypeScript, and Vite. Designed for reusability across multiple projects.
## 🏆 Why Choose This Player?
| Feature | @alper/video-player | video.js | react-player | plyr |
|---------|---------------------|----------|--------------|------|
| **Bundle Size (gzipped)** | **15KB** ✅ | ~500KB ❌ | ~50KB ⚠️ | ~30KB ⚠️ |
| **Runtime Dependencies** | **0** ✅ | Many ❌ | Few ⚠️ | Few ⚠️ |
| **React Native** | **Yes** ✅ | Wrapper ⚠️ | **Yes** ✅ | Wrapper ⚠️ |
| **TypeScript Native** | **Yes** ✅ | Types ⚠️ | Partial ⚠️ | Types ⚠️ |
| **HLS Support** | **Yes** ✅ | Yes ✅ | Yes ✅ | No ❌ |
| **Quality Switching** | **Yes** ✅ | Yes ✅ | Limited ⚠️ | No ❌ |
| **Touch Gestures** | **15+** ✅ | Limited ⚠️ | No ❌ | Limited ⚠️ |
| **Keyboard Shortcuts** | **15+** ✅ | ~8 ⚠️ | Basic ⚠️ | ~10 ⚠️ |
| **i18n Support** | **Yes** ✅ | Yes ✅ | No ❌ | Yes ✅ |
### Key Advantages
- 📦 **97% smaller than video.js** - Only 15KB vs 500KB
-**Blazing fast** - Zero runtime dependencies means faster load times
- 🎯 **React-first** - Built specifically for React, not a wrapper
- 🔧 **Full TypeScript** - Complete type safety out of the box
- 🎨 **Easy customization** - CSS variables for theming
- 📱 **Mobile-ready** - Comprehensive touch gesture support
- 🌍 **Internationalized** - Built-in i18n with English and Turkish
-**Accessible** - ARIA labels and keyboard navigation
## ✨ Features
+12 -2
View File
@@ -22,7 +22,10 @@
"build": "tsc && vite build",
"build:lib": "vite build --config vite.config.lib.ts",
"preview": "vite preview",
"lint": "eslint . --report-unused-disable-directives --max-warnings 0"
"lint": "eslint . --report-unused-disable-directives --max-warnings 0",
"test": "vitest",
"test:ui": "vitest --ui",
"test:coverage": "vitest --coverage"
},
"peerDependencies": {
"react": "^18.0.0",
@@ -30,20 +33,27 @@
},
"devDependencies": {
"@eslint/js": "^9.38.0",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
"@testing-library/user-event": "^14.6.1",
"@types/react": "^19.2.2",
"@types/react-dom": "^19.2.2",
"@typescript-eslint/eslint-plugin": "^8.46.2",
"@typescript-eslint/parser": "^8.46.2",
"@vitejs/plugin-react": "^5.1.0",
"@vitest/ui": "^4.0.4",
"eslint": "^9.38.0",
"eslint-plugin-react-hooks": "^7.0.1",
"eslint-plugin-react-refresh": "^0.4.24",
"globals": "^16.4.0",
"jsdom": "^27.0.1",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"terser": "^5.44.0",
"typescript": "^5.9.3",
"vite": "^7.1.12",
"vite-plugin-dts": "^4.5.4"
"vite-plugin-dts": "^4.5.4",
"vitest": "^4.0.4"
},
"optionalDependencies": {
"hls.js": "^1.6.13"
+872 -8
View File
File diff suppressed because it is too large Load Diff
+5 -2
View File
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState, useCallback } from 'react'
import React, { useEffect, useRef, useState, useCallback, lazy, Suspense } from 'react'
import { usePlayerContext } from '../contexts/PlayerContext'
import { PlayPauseButton } from './controls/PlayPauseButton'
import { ProgressBar } from './controls/ProgressBar'
@@ -9,12 +9,13 @@ import { PIPButton } from './controls/PIPButton'
import { SettingsButton } from './controls/SettingsButton'
import { LoadingSpinner } from './overlays/LoadingSpinner'
import { CenterPlayButton } from './controls/CenterPlayButton'
import { SettingsMenu } from './menus/SettingsMenu'
import { useKeyboardShortcuts } from '../hooks/useKeyboardShortcuts'
import { useTouchGestures } from '../hooks/useTouchGestures'
import type { SubtitleTrack, AudioTrack, VideoQuality } from '../types'
import './ControlsLayer.css'
const SettingsMenu = lazy(() => import('./menus/SettingsMenu').then(module => ({ default: module.SettingsMenu })))
interface ControlsLayerProps {
keyboardShortcuts?: boolean
pictureInPicture?: boolean
@@ -225,7 +226,9 @@ export const ControlsLayer: React.FC<ControlsLayerProps> = ({
<div className="controls-right">
<div style={{ position: 'relative' }}>
<SettingsButton />
<Suspense fallback={null}>
<SettingsMenu subtitles={subtitles} audioTracks={audioTracks} qualities={qualities} />
</Suspense>
</div>
{pictureInPicture && <PIPButton />}
<FullscreenButton />
+11 -70
View File
@@ -2,7 +2,8 @@ import React, { useEffect, useCallback, useState } from 'react'
import { usePlayerContext } from '../contexts/PlayerContext'
import type { SubtitleTrack, AudioTrack, VideoQuality } from '../types'
import { validateVideoURL, getCORSErrorMessage, isCORSError } from '../utils/corsHelper'
import { getHlsAudioTracks, getHlsQualities, setHlsAudioTrack, setHlsQualityLevel } from '../utils/hlsLoader'
import { setHlsAudioTrack, setHlsQualityLevel } from '../utils/hlsControl'
import { setupHlsInstance } from '../utils/hlsSetup'
import './VideoElement.css'
interface VideoElementProps {
@@ -219,80 +220,21 @@ export const VideoElement: React.FC<VideoElementProps> = ({
const setupHls = async () => {
if (isHLS && video.canPlayType('application/vnd.apple.mpegurl') === '') {
// Browser doesn't support HLS natively, load hls.js
try {
// Dynamic import with CDN fallback
const { loadHls, isHlsSupported } = await import('../utils/hlsLoader')
const Hls = await loadHls()
if (!isHlsSupported(Hls)) {
throw new Error('HLS.js is not supported in this browser')
}
const hls = new Hls({
enableWorker: true,
lowLatencyMode: false,
})
hls.loadSource(src)
hls.attachMedia(video)
hls.on(Hls.Events.MANIFEST_PARSED, () => {
// Extract audio tracks after manifest is parsed
// Sometimes audio tracks are not immediately available, so we try with a small delay
setTimeout(() => {
const tracks = getHlsAudioTracks(hls)
const qualities = getHlsQualities(hls)
if (tracks.length > 0) {
cleanupFn = await setupHlsInstance({
video,
src,
autoplay,
onAudioTracksLoaded: (tracks) => {
setAvailableAudioTracks(tracks)
onAudioTracksLoaded?.(tracks)
}
},
onQualityLevelsLoaded: (qualities) => {
setAvailableQualities(qualities)
onQualityLevelsLoaded?.(qualities)
}, 100)
if (autoplay) {
void video.play().catch(() => undefined)
}
},
onError: handleError,
})
// Also listen to audio track updates
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, () => {
const tracks = getHlsAudioTracks(hls)
if (tracks.length > 0) {
setAvailableAudioTracks(tracks)
onAudioTracksLoaded?.(tracks)
}
})
hls.on(Hls.Events.ERROR, (_event: any, data: any) => {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls.startLoad()
break
case Hls.ErrorTypes.MEDIA_ERROR:
hls.recoverMediaError()
break
default:
handleError()
break
}
}
})
// Store hls instance for cleanup
;(video as any).__hlsInstance = hls
// Setup cleanup function
cleanupFn = () => {
if (hls) {
hls.destroy()
}
delete (video as any).__hlsInstance
}
} catch (err) {
let error: Error
if (err instanceof Error && isCORSError(err)) {
@@ -309,7 +251,6 @@ export const VideoElement: React.FC<VideoElementProps> = ({
onError?.(error)
}
} else {
// Native support or regular video
video.src = src
if (autoplay) {
void video.play().catch(() => undefined)
+131
View File
@@ -0,0 +1,131 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { VideoPlayer } from './VideoPlayer';
describe('VideoPlayer', () => {
const defaultProps = {
src: 'https://example.com/video.mp4',
};
it('renders video player container', () => {
const { container } = render(<VideoPlayer {...defaultProps} />);
expect(container.querySelector('.video-player')).toBeInTheDocument();
});
it('renders video element', () => {
const { container } = render(<VideoPlayer {...defaultProps} />);
const video = container.querySelector('video');
expect(video).toBeInTheDocument();
});
it('renders with autoplay prop', () => {
const { container } = render(<VideoPlayer {...defaultProps} autoplay />);
const video = container.querySelector('video');
// VideoElement handles autoplay programmatically via play() method
expect(video).toBeInTheDocument();
});
it('renders with muted prop', () => {
const { container } = render(<VideoPlayer {...defaultProps} muted />);
const video = container.querySelector('video');
// Muted state is managed through VideoElement
expect(video).toBeInTheDocument();
});
it('applies loop when enabled', () => {
const { container } = render(<VideoPlayer {...defaultProps} loop />);
const video = container.querySelector('video');
expect(video).toHaveAttribute('loop');
});
it('applies custom className', () => {
const className = 'custom-player';
const { container } = render(<VideoPlayer {...defaultProps} className={className} />);
expect(container.querySelector('.video-player')).toHaveClass('video-player', className);
});
it('calls onPlay callback when play event fires', async () => {
const onPlay = vi.fn();
const { container } = render(<VideoPlayer {...defaultProps} onPlay={onPlay} />);
const video = container.querySelector('video') as HTMLVideoElement;
video.dispatchEvent(new Event('play'));
await waitFor(() => {
expect(onPlay).toHaveBeenCalled();
});
});
it('calls onPause callback when pause event fires', async () => {
const onPause = vi.fn();
const { container } = render(<VideoPlayer {...defaultProps} onPause={onPause} />);
const video = container.querySelector('video') as HTMLVideoElement;
video.dispatchEvent(new Event('pause'));
await waitFor(() => {
expect(onPause).toHaveBeenCalled();
});
});
it('calls onEnded callback when ended event fires', async () => {
const onEnded = vi.fn();
const { container } = render(<VideoPlayer {...defaultProps} onEnded={onEnded} />);
const video = container.querySelector('video') as HTMLVideoElement;
video.dispatchEvent(new Event('ended'));
await waitFor(() => {
expect(onEnded).toHaveBeenCalled();
});
});
it('calls onTimeUpdate callback with current time', async () => {
const onTimeUpdate = vi.fn();
const { container } = render(<VideoPlayer {...defaultProps} onTimeUpdate={onTimeUpdate} />);
const video = container.querySelector('video') as HTMLVideoElement;
Object.defineProperty(video, 'currentTime', { value: 10.5, configurable: true });
video.dispatchEvent(new Event('timeupdate'));
await waitFor(() => {
expect(onTimeUpdate).toHaveBeenCalledWith(10.5);
});
});
it('renders with subtitles prop', () => {
const subtitles = [
{ src: 'subtitles-en.vtt', srcLang: 'en', label: 'English' },
{ src: 'subtitles-tr.vtt', srcLang: 'tr', label: 'Türkçe' },
];
const { container } = render(<VideoPlayer {...defaultProps} subtitles={subtitles} />);
const video = container.querySelector('video') as HTMLVideoElement;
// Subtitles are added dynamically by VideoElement
expect(video).toBeInTheDocument();
});
it('renders without errors', () => {
const onError = vi.fn();
const { container } = render(<VideoPlayer {...defaultProps} onError={onError} />);
const video = container.querySelector('video') as HTMLVideoElement;
expect(video).toBeInTheDocument();
// Error handling is tested separately in integration tests
});
it('hides controls when controls prop is false', () => {
const { container } = render(<VideoPlayer {...defaultProps} controls={false} />);
const controls = container.querySelector('.controls');
expect(controls).not.toBeInTheDocument();
});
it('applies custom style', () => {
const style = { width: '800px', height: '450px' };
const { container } = render(<VideoPlayer {...defaultProps} style={style} />);
const playerElement = container.querySelector('.video-player') as HTMLElement;
expect(playerElement.style.width).toBe('800px');
expect(playerElement.style.height).toBe('450px');
});
});
+16 -4
View File
@@ -3,17 +3,28 @@ import { PlayerProvider, usePlayerContext } from '../contexts/PlayerContext'
import { VideoElement } from './VideoElement'
import { ControlsLayer } from './ControlsLayer'
import type { VideoPlayerProps, AudioTrack, VideoQuality } from '../types'
import { initializePolyfills } from '../utils/polyfills'
import '../styles/variables.css'
import './VideoPlayer.css'
// Initialize polyfills once
// Lazy load polyfills only if needed
let polyfillsInitialized = false
if (!polyfillsInitialized) {
const initializePolyfillsIfNeeded = async () => {
if (polyfillsInitialized) return
// Check if polyfills are needed
const needsFullscreenPolyfill = !document.fullscreenEnabled && !(document as any).webkitFullscreenEnabled
const needsPIPPolyfill = !('pictureInPictureEnabled' in document)
if (needsFullscreenPolyfill || needsPIPPolyfill) {
const { initializePolyfills } = await import('../utils/polyfills')
initializePolyfills()
polyfillsInitialized = true
}
}
// Initialize polyfills asynchronously
initializePolyfillsIfNeeded()
const VideoPlayerContent: React.FC<
VideoPlayerProps & {
audioTracks: AudioTrack[]
@@ -92,6 +103,7 @@ export const VideoPlayer: React.FC<VideoPlayerProps> = ({
controls = true,
subtitles = [],
theme,
language,
keyboardShortcuts = true,
pictureInPicture = true,
className = '',
@@ -129,7 +141,7 @@ export const VideoPlayer: React.FC<VideoPlayerProps> = ({
}, [])
return (
<PlayerProvider initialMuted={muted}>
<PlayerProvider initialMuted={muted} language={language}>
<VideoPlayerContent
src={src}
poster={poster}
+16 -15
View File
@@ -26,6 +26,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
setAudioTrack,
setQuality,
toggleSettings,
translations,
} = usePlayerContext()
const menuRef = useRef<HTMLDivElement>(null)
const [currentView, setCurrentView] = useState<MenuView>('main')
@@ -66,7 +67,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
{currentView === 'main' && (
<>
<div className="settings-menu-header">
<h3>Ayarlar</h3>
<h3>{translations.settings}</h3>
</div>
<div className="settings-main-options">
{qualities.length > 0 && (
@@ -75,9 +76,9 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<QualityIcon size={20} color="var(--player-text)" />
</div>
<div className="settings-main-option-content">
<span className="settings-main-option-label">Çözünürlük</span>
<span className="settings-main-option-label">{translations.quality}</span>
<span className="settings-main-option-value">
{settings.quality ? settings.quality.label : 'Otomatik'}
{settings.quality ? settings.quality.label : translations.auto}
</span>
</div>
<div className="settings-main-option-arrow"></div>
@@ -89,7 +90,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<SpeedIcon size={20} color="var(--player-text)" />
</div>
<div className="settings-main-option-content">
<span className="settings-main-option-label">Hız</span>
<span className="settings-main-option-label">{translations.speed}</span>
<span className="settings-main-option-value">
{videoState.playbackRate === 1 ? 'Normal' : `${videoState.playbackRate}x`}
</span>
@@ -102,9 +103,9 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<SubtitlesIcon size={20} color="var(--player-text)" />
</div>
<div className="settings-main-option-content">
<span className="settings-main-option-label">Altyazı</span>
<span className="settings-main-option-label">{translations.subtitles}</span>
<span className="settings-main-option-value">
{settings.subtitle ? settings.subtitle.label : 'Kapalı'}
{settings.subtitle ? settings.subtitle.label : translations.off}
</span>
</div>
<div className="settings-main-option-arrow"></div>
@@ -116,9 +117,9 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<AudioIcon size={20} color="var(--player-text)" />
</div>
<div className="settings-main-option-content">
<span className="settings-main-option-label">Ses</span>
<span className="settings-main-option-label">{translations.audioTrack}</span>
<span className="settings-main-option-value">
{settings.audioTrack ? settings.audioTrack.name : 'Varsayılan'}
{settings.audioTrack ? settings.audioTrack.name : translations.default}
</span>
</div>
<div className="settings-main-option-arrow"></div>
@@ -135,7 +136,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<button className="settings-back-button" onClick={goBack}>
</button>
<h3>Oynatma Hızı</h3>
<h3>{translations.speed}</h3>
</div>
<div className="settings-options">
{playbackRates.map((rate) => (
@@ -162,7 +163,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<button className="settings-back-button" onClick={goBack}>
</button>
<h3>Altyazı</h3>
<h3>{translations.subtitles}</h3>
</div>
<div className="settings-options">
<button
@@ -172,7 +173,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
setTimeout(() => goBack(), 150)
}}
>
<span>Kapalı</span>
<span>{translations.off}</span>
{!settings.subtitle && <CheckIcon size={16} color="var(--player-primary)" />}
</button>
{subtitles.length > 0 ? (
@@ -191,7 +192,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
))
) : (
<div className="settings-empty-state">
<span>Altyazı mevcut değil</span>
<span>{translations.noSubtitlesAvailable}</span>
</div>
)}
</div>
@@ -205,7 +206,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<button className="settings-back-button" onClick={goBack}>
</button>
<h3>Ses</h3>
<h3>{translations.audioTrack}</h3>
</div>
<div className="settings-options">
{audioTracks.map((track) => (
@@ -234,7 +235,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
<button className="settings-back-button" onClick={goBack}>
</button>
<h3>Çözünürlük</h3>
<h3>{translations.quality}</h3>
</div>
<div className="settings-options">
<button
@@ -244,7 +245,7 @@ export const SettingsMenu: React.FC<SettingsMenuProps> = ({
setTimeout(() => goBack(), 150)
}}
>
<span>Otomatik</span>
<span>{translations.auto}</span>
{!settings.quality && <CheckIcon size={16} color="var(--player-primary)" />}
</button>
{qualities.map((quality) => {
+11
View File
@@ -0,0 +1,11 @@
export const STRINGS = {
SETTINGS: 'Settings',
QUALITY: 'Quality',
SPEED: 'Speed',
SUBTITLES: 'Subtitles',
AUDIO: 'Audio',
AUTO: 'Auto',
OFF: 'Off',
DEFAULT: 'Default',
LEVEL: 'Level',
} as const
+9
View File
@@ -1,5 +1,7 @@
import React, { createContext, useContext, useRef, useState, useCallback } from 'react'
import type { PlayerContextValue, VideoState, UIState, PlayerSettings, AudioTrack } from '../types'
import type { Translations } from '../i18n'
import { getTranslations, detectBrowserLanguage } from '../i18n'
type SelectedQuality = PlayerSettings['quality']
type SelectedSubtitle = PlayerSettings['subtitle']
@@ -7,6 +9,7 @@ type SelectedSubtitle = PlayerSettings['subtitle']
interface PlayerContextType extends PlayerContextValue {
setVideoState: React.Dispatch<React.SetStateAction<VideoState>>
setUIState: React.Dispatch<React.SetStateAction<UIState>>
translations: Translations
}
const PlayerContext = createContext<PlayerContextType | null>(null)
@@ -25,6 +28,7 @@ interface PlayerProviderProps {
initialVolume?: number
initialMuted?: boolean
initialPlaybackRate?: number
language?: string
}
export const PlayerProvider: React.FC<PlayerProviderProps> = ({
@@ -32,10 +36,14 @@ export const PlayerProvider: React.FC<PlayerProviderProps> = ({
initialVolume = 1,
initialMuted = false,
initialPlaybackRate = 1,
language,
}) => {
const videoRef = useRef<HTMLVideoElement | null>(null)
const containerRef = useRef<HTMLDivElement | null>(null)
// Get translations based on language prop or browser language
const translations = getTranslations(language || detectBrowserLanguage())
const [videoState, setVideoState] = useState<VideoState>({
playing: false,
currentTime: 0,
@@ -169,6 +177,7 @@ export const PlayerProvider: React.FC<PlayerProviderProps> = ({
containerRef,
setVideoState,
setUIState,
translations,
play,
pause,
togglePlay,
+69
View File
@@ -0,0 +1,69 @@
/**
* Simple i18n system for video player
*/
export interface Translations {
noSubtitlesAvailable: string;
subtitles: string;
off: string;
auto: string;
quality: string;
speed: string;
normal: string;
default: string;
audioTrack: string;
settings: string;
level: string;
}
export const translations: Record<string, Translations> = {
en: {
noSubtitlesAvailable: 'No subtitles available',
subtitles: 'Subtitles',
off: 'Off',
auto: 'Auto',
quality: 'Quality',
speed: 'Speed',
normal: 'Normal',
default: 'Default',
audioTrack: 'Audio Track',
settings: 'Settings',
level: "Level",
},
tr: {
noSubtitlesAvailable: 'Altyazı mevcut değil',
subtitles: 'Altyazı',
off: 'Kapalı',
auto: 'Otomatik',
quality: 'Kalite',
speed: 'Hız',
normal: 'Normal',
default: 'Varsayılan',
audioTrack: 'Ses',
settings: 'Ayarlar',
level: "Seviye",
},
};
export const getTranslations = (language: string = 'en'): Translations => {
// Try exact match first
if (translations[language]) {
return translations[language];
}
// Try language code without region (e.g., "en" from "en-US")
const languageCode = language.split('-')[0];
if (translations[languageCode]) {
return translations[languageCode];
}
// Default to English
return translations.en;
};
export const detectBrowserLanguage = (): string => {
if (typeof navigator !== 'undefined') {
return navigator.language || 'en';
}
return 'en';
};
+78 -211
View File
@@ -6,251 +6,118 @@ export interface IconProps {
color?: string
}
export const PlayIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M8 5v14l11-7L8 5z" fill={color} />
interface BaseIconProps extends IconProps {
children: React.ReactNode
}
const Icon: React.FC<BaseIconProps> = ({ size = 24, className = '', color = 'currentColor', children }) => (
<svg width={size} height={size} viewBox="0 0 24 24" className={className}>
{React.Children.map(children, child =>
React.isValidElement(child) ? React.cloneElement(child, { fill: color } as any) : child
)}
</svg>
)
export const PauseIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z" fill={color} />
</svg>
export const PlayIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M8 5v14l11-7L8 5z" />
</Icon>
)
export const VolumeUpIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"
fill={color}
/>
</svg>
export const PauseIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z" />
</Icon>
)
export const VolumeDownIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z" fill={color} />
</svg>
export const VolumeUpIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z" />
</Icon>
)
export const VolumeMuteIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z"
fill={color}
/>
</svg>
export const VolumeDownIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z" />
</Icon>
)
export const FullscreenIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z" fill={color} />
</svg>
export const VolumeMuteIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zm2.5 0c0 .94-.2 1.82-.54 2.64l1.51 1.51C20.63 14.91 21 13.5 21 12c0-4.28-2.99-7.86-7-8.77v2.06c2.89.86 5 3.54 5 6.71zM4.27 3L3 4.27 7.73 9H3v6h4l5 5v-6.73l4.25 4.25c-.67.52-1.42.93-2.25 1.18v2.06c1.38-.31 2.63-.95 3.69-1.81L19.73 21 21 19.73l-9-9L4.27 3zM12 4L9.91 6.09 12 8.18V4z" />
</Icon>
)
export const FullscreenExitIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z" fill={color} />
</svg>
export const FullscreenIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z" />
</Icon>
)
export const SettingsIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M19.14 12.94c.04-.3.06-.61.06-.94 0-.32-.02-.64-.07-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.05.3-.09.63-.09.94s.02.64.07.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z"
fill={color}
/>
</svg>
export const FullscreenExitIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z" />
</Icon>
)
export const PIPIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M19 11h-8v6h8v-6zm4 8V4.98C23 3.88 22.1 3 21 3H3c-1.1 0-2 .88-2 1.98V19c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2zm-2 .02H3V4.97h18v14.05z" fill={color} />
</svg>
export const SettingsIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M19.14 12.94c.04-.3.06-.61.06-.94 0-.32-.02-.64-.07-.94l2.03-1.58c.18-.14.23-.41.12-.61l-1.92-3.32c-.12-.22-.37-.29-.59-.22l-2.39.96c-.5-.38-1.03-.7-1.62-.94l-.36-2.54c-.04-.24-.24-.41-.48-.41h-3.84c-.24 0-.43.17-.47.41l-.36 2.54c-.59.24-1.13.57-1.62.94l-2.39-.96c-.22-.08-.47 0-.59.22L2.74 8.87c-.12.21-.08.47.12.61l2.03 1.58c-.05.3-.09.63-.09.94s.02.64.07.94l-2.03 1.58c-.18.14-.23.41-.12.61l1.92 3.32c.12.22.37.29.59.22l2.39-.96c.5.38 1.03.7 1.62.94l.36 2.54c.05.24.24.41.48.41h3.84c.24 0 .44-.17.47-.41l.36-2.54c.59-.24 1.13-.56 1.62-.94l2.39.96c.22.08.47 0 .59-.22l1.92-3.32c.12-.22.07-.47-.12-.61l-2.01-1.58zM12 15.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z" />
</Icon>
)
export const SubtitlesIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M20 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zM4 12h4v2H4v-2zm10 6H4v-2h10v2zm6 0h-4v-2h4v2zm0-4H10v-2h10v2z"
fill={color}
/>
</svg>
export const PIPIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M19 11h-8v6h8v-6zm4 8V4.98C23 3.88 22.1 3 21 3H3c-1.1 0-2 .88-2 1.98V19c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2zm-2 .02H3V4.97h18v14.05z" />
</Icon>
)
export const SpeedIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M20.38 8.57l-1.23 1.85a8 8 0 01-.22 7.58H5.07A8 8 0 0115.58 6.85l1.85-1.23A10 10 0 003.35 19a2 2 0 001.72 1h13.85a2 2 0 001.74-1 10 10 0 00-.27-10.44z"
fill={color}
/>
<path d="M10.59 15.41a2 2 0 002.83 0l5.66-8.49-8.49 5.66a2 2 0 000 2.83z" fill={color} />
</svg>
export const SubtitlesIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M20 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zM4 12h4v2H4v-2zm10 6H4v-2h10v2zm6 0h-4v-2h4v2zm0-4H10v-2h10v2z" />
</Icon>
)
export const QualityIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-9 12H8v-2H6v2H4.5V9H6v2h2V9h1.5v6zm9 0h-1.5v-2.25H15V15h-1.5V9H15v2.25h3.5V9H20v6z"
fill={color}
/>
</svg>
export const SpeedIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M20.38 8.57l-1.23 1.85a8 8 0 01-.22 7.58H5.07A8 8 0 0115.58 6.85l1.85-1.23A10 10 0 003.35 19a2 2 0 001.72 1h13.85a2 2 0 001.74-1 10 10 0 00-.27-10.44z" />
<path d="M10.59 15.41a2 2 0 002.83 0l5.66-8.49-8.49 5.66a2 2 0 000 2.83z" />
</Icon>
)
export const ForwardIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M4 18l8.5-6L4 6v12zm9-12v12l8.5-6L13 6z" fill={color} />
</svg>
export const QualityIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-9 12H8v-2H6v2H4.5V9H6v2h2V9h1.5v6zm9 0h-1.5v-2.25H15V15h-1.5V9H15v2.25h3.5V9H20v6z" />
</Icon>
)
export const RewindIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M11 18V6l-8.5 6 8.5 6zm.5-6l8.5 6V6l-8.5 6z" fill={color} />
</svg>
export const ForwardIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M4 18l8.5-6L4 6v12zm9-12v12l8.5-6L13 6z" />
</Icon>
)
export const RewindIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M11 18V6l-8.5 6 8.5 6zm.5-6l8.5 6V6l-8.5 6z" />
</Icon>
)
export const LoadingIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
style={{ animation: 'spin 1s linear infinite' }}
>
<path
d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z"
opacity="0.3"
fill={color}
/>
<svg width={size} height={size} viewBox="0 0 24 24" className={className} style={{ animation: 'spin 1s linear infinite' }}>
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" opacity="0.3" fill={color} />
<path d="M12 2C6.48 2 2 6.48 2 12h2c0-4.42 3.58-8 8-8s8 3.58 8 8h2c0-5.52-4.48-10-10-10z" fill={color} />
</svg>
)
export const CheckIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z" fill={color} />
</svg>
export const CheckIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41L9 16.17z" />
</Icon>
)
export const AudioIcon: React.FC<IconProps> = ({ size = 24, className = '', color = 'currentColor' }) => (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<path
d="M12 3v9.28c-.47-.17-.97-.28-1.5-.28C8.01 12 6 14.01 6 16.5S8.01 21 10.5 21c2.31 0 4.2-1.75 4.45-4H15V6h4V3h-7z"
fill={color}
/>
</svg>
export const AudioIcon: React.FC<IconProps> = (props) => (
<Icon {...props}>
<path d="M12 3v9.28c-.47-.17-.97-.28-1.5-.28C8.01 12 6 14.01 6 16.5S8.01 21 10.5 21c2.31 0 4.2-1.75 4.45-4H15V6h4V3h-7z" />
</Icon>
)
+4 -2
View File
@@ -19,9 +19,11 @@ export type {
// Utils
export { formatTime, parseTime } from './utils/time'
export { parseSRT, createSubtitleBlobURL, fetchSubtitle } from './utils/subtitles'
export { initializePolyfills, features } from './utils/polyfills'
export { validateVideoURL, getCORSErrorMessage, isCORSError, checkVideoCORS } from './utils/corsHelper'
export { loadHls, isHlsSupported, hasNativeHlsSupport } from './utils/hlsLoader'
// i18n
export { getTranslations, detectBrowserLanguage, translations } from './i18n'
export type { Translations } from './i18n'
// Hooks (for advanced users)
export { useKeyboardShortcuts } from './hooks/useKeyboardShortcuts'
+72
View File
@@ -0,0 +1,72 @@
import '@testing-library/jest-dom';
import { afterEach, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
// Cleanup after each test
afterEach(() => {
cleanup();
});
// Mock HTMLMediaElement methods that are not implemented in jsdom
Object.defineProperty(HTMLMediaElement.prototype, 'play', {
configurable: true,
value: vi.fn().mockResolvedValue(undefined),
});
Object.defineProperty(HTMLMediaElement.prototype, 'pause', {
configurable: true,
value: vi.fn(),
});
Object.defineProperty(HTMLMediaElement.prototype, 'load', {
configurable: true,
value: vi.fn(),
});
// Mock requestFullscreen and exitFullscreen
Object.defineProperty(HTMLElement.prototype, 'requestFullscreen', {
configurable: true,
value: vi.fn().mockResolvedValue(undefined),
});
Object.defineProperty(Document.prototype, 'exitFullscreen', {
configurable: true,
value: vi.fn().mockResolvedValue(undefined),
});
// Mock Picture-in-Picture API
Object.defineProperty(HTMLVideoElement.prototype, 'requestPictureInPicture', {
configurable: true,
value: vi.fn().mockResolvedValue({}),
});
Object.defineProperty(Document.prototype, 'exitPictureInPicture', {
configurable: true,
value: vi.fn().mockResolvedValue(undefined),
});
// Mock IntersectionObserver
(globalThis as any).IntersectionObserver = class IntersectionObserver {
constructor() {}
disconnect() {}
observe() {}
takeRecords() {
return [];
}
unobserve() {}
};
// Mock window.matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
+1
View File
@@ -41,6 +41,7 @@ export interface VideoPlayerProps {
controls?: boolean
subtitles?: SubtitleTrack[]
theme?: PlayerTheme
language?: string
keyboardShortcuts?: boolean
pictureInPicture?: boolean
className?: string
+225
View File
@@ -0,0 +1,225 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
isSameOrigin,
isBlobOrDataURL,
validateVideoURL,
getCORSErrorMessage,
isCORSError,
checkVideoCORS,
} from './corsHelper';
describe('corsHelper', () => {
describe('isSameOrigin', () => {
it('returns true for same origin URLs', () => {
const sameOriginUrl = `${window.location.origin}/video.mp4`;
expect(isSameOrigin(sameOriginUrl)).toBe(true);
});
it('returns false for different origin URLs', () => {
expect(isSameOrigin('https://example.com/video.mp4')).toBe(false);
});
it('returns true for relative URLs', () => {
expect(isSameOrigin('/videos/test.mp4')).toBe(true);
});
it('returns true for relative path-like strings', () => {
// In browsers, "not-a-url" is treated as a relative URL
expect(isSameOrigin('not-a-url')).toBe(true);
});
});
describe('isBlobOrDataURL', () => {
it('returns true for blob URLs', () => {
expect(isBlobOrDataURL('blob:http://example.com/123456')).toBe(true);
});
it('returns true for data URLs', () => {
expect(isBlobOrDataURL('data:video/mp4;base64,AAAA')).toBe(true);
});
it('returns false for regular URLs', () => {
expect(isBlobOrDataURL('https://example.com/video.mp4')).toBe(false);
});
it('returns false for empty string', () => {
expect(isBlobOrDataURL('')).toBe(false);
});
});
describe('validateVideoURL', () => {
it('returns invalid for empty URL', () => {
const result = validateVideoURL('');
expect(result.valid).toBe(false);
expect(result.error).toBe('Video URL is empty');
});
it('returns invalid for whitespace-only URL', () => {
const result = validateVideoURL(' ');
expect(result.valid).toBe(false);
expect(result.error).toBe('Video URL is empty');
});
it('returns valid for relative path strings', () => {
// Browser treats this as a relative URL
const result = validateVideoURL('not a valid url');
expect(result.valid).toBe(true);
});
it('returns valid for same origin URL without warning', () => {
const result = validateVideoURL(`${window.location.origin}/video.mp4`);
expect(result.valid).toBe(true);
expect(result.warning).toBeUndefined();
});
it('returns valid for blob URL without warning', () => {
const result = validateVideoURL('blob:http://example.com/123456');
expect(result.valid).toBe(true);
expect(result.warning).toBeUndefined();
});
it('returns valid with warning for external URL', () => {
const result = validateVideoURL('https://example.com/video.mp4');
expect(result.valid).toBe(true);
expect(result.warning).toContain('CORS');
});
it('returns valid for relative URLs', () => {
const result = validateVideoURL('/videos/test.mp4');
expect(result.valid).toBe(true);
expect(result.warning).toBeUndefined();
});
});
describe('getCORSErrorMessage', () => {
it('returns generic message for same origin', () => {
const message = getCORSErrorMessage(`${window.location.origin}/video.mp4`);
expect(message).toBe('Failed to load video. Please check the URL.');
});
it('returns generic message for blob URLs', () => {
const message = getCORSErrorMessage('blob:http://example.com/123456');
expect(message).toBe('Failed to load video. Please check the URL.');
});
it('returns CORS-specific message for external URLs', () => {
const message = getCORSErrorMessage('https://example.com/video.mp4');
expect(message).toContain('CORS Error');
expect(message).toContain('example.com');
expect(message).toContain('Access-Control-Allow-Origin');
});
});
describe('isCORSError', () => {
it('returns true for errors containing "cors"', () => {
const error = new Error('CORS policy blocked this request');
expect(isCORSError(error)).toBe(true);
});
it('returns true for errors containing "cross-origin"', () => {
const error = new Error('Cross-origin request blocked');
expect(isCORSError(error)).toBe(true);
});
it('returns true for errors containing "blocked by cors policy"', () => {
const error = new Error('Request blocked by CORS policy');
expect(isCORSError(error)).toBe(true);
});
it('returns true for errors containing "access-control-allow-origin"', () => {
const error = new Error('No \'access-control-allow-origin\' header present');
expect(isCORSError(error)).toBe(true);
});
it('returns false for non-CORS errors', () => {
const error = new Error('Network timeout');
expect(isCORSError(error)).toBe(false);
});
it('is case insensitive', () => {
const error = new Error('BLOCKED BY CORS POLICY');
expect(isCORSError(error)).toBe(true);
});
});
describe('checkVideoCORS', () => {
beforeEach(() => {
global.fetch = vi.fn();
});
afterEach(() => {
vi.restoreAllMocks();
});
it('returns supported when CORS headers are present', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
headers: new Map([
['Access-Control-Allow-Origin', '*'],
['Accept-Ranges', 'bytes'],
]),
});
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supported).toBe(true);
expect(result.needsProxy).toBe(false);
expect(result.supportsRange).toBe(true);
});
it('returns not supported when CORS headers are missing', async () => {
(global.fetch as any).mockResolvedValue({
ok: false,
headers: new Map(),
});
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supported).toBe(false);
expect(result.needsProxy).toBe(true);
expect(result.error).toContain('CORS not enabled');
});
it('detects range support', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
headers: new Map([
['Access-Control-Allow-Origin', '*'],
['Accept-Ranges', 'bytes'],
]),
});
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supportsRange).toBe(true);
});
it('detects no range support when header is "none"', async () => {
(global.fetch as any).mockResolvedValue({
ok: true,
headers: new Map([
['Access-Control-Allow-Origin', '*'],
['Accept-Ranges', 'none'],
]),
});
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supportsRange).toBe(false);
});
it('handles CORS fetch errors', async () => {
(global.fetch as any).mockRejectedValue(new TypeError('Failed to fetch (CORS)'));
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supported).toBe(false);
expect(result.needsProxy).toBe(true);
expect(result.error).toContain('CORS blocked');
});
it('handles general fetch errors', async () => {
(global.fetch as any).mockRejectedValue(new Error('Network error'));
const result = await checkVideoCORS('https://example.com/video.mp4');
expect(result.supported).toBe(false);
expect(result.needsProxy).toBe(true);
expect(result.error).toBe('Network error');
});
});
});
+1 -17
View File
@@ -122,23 +122,7 @@ export const getCORSErrorMessage = (url: string): string => {
return 'Failed to load video. Please check the URL.'
}
return `
CORS Error: Unable to load video from external source.
The video server at "${new URL(url).origin}" does not allow cross-origin requests.
To fix this issue:
1. Add CORS headers to your video server:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, HEAD
Access-Control-Allow-Headers: Range
2. Use a proxy server to bypass CORS restrictions
3. Host the video on the same domain as your application
4. Use a CDN that supports CORS (e.g., Cloudflare, AWS CloudFront)
`.trim()
return `CORS Error: Unable to load video from ${new URL(url).origin}. The server must allow cross-origin requests with proper Access-Control-Allow-Origin headers.`
}
/**
+43
View File
@@ -0,0 +1,43 @@
/**
* HLS control utilities for audio tracks and quality levels
* Separated to avoid circular dependencies and enable better tree-shaking
*/
/**
* Update active quality level in HLS instance. Passing null re-enables auto.
*/
export const setHlsQualityLevel = (hls: any, levelIndex: number | null | undefined): void => {
if (!hls || !Array.isArray(hls.levels)) {
return
}
if (levelIndex === null || typeof levelIndex === 'undefined' || levelIndex < 0) {
if (hls.currentLevel !== -1) {
hls.currentLevel = -1
}
return
}
if (levelIndex >= hls.levels.length) {
return
}
if (hls.currentLevel !== levelIndex) {
hls.currentLevel = levelIndex
}
}
/**
* Set active audio track in HLS instance
*/
export const setHlsAudioTrack = (hls: any, audioTrackIndex: number): void => {
if (!hls || !hls.audioTracks) {
return
}
if (audioTrackIndex < 0 || audioTrackIndex >= hls.audioTracks.length) {
return
}
hls.audioTrack = audioTrackIndex
}
+6 -39
View File
@@ -4,6 +4,10 @@
*/
import type { AudioTrack, VideoQuality } from '../types'
import { getTranslations, detectBrowserLanguage } from '../i18n'
// Re-export control functions for backward compatibility
export { setHlsQualityLevel, setHlsAudioTrack } from './hlsControl'
const HLS_CDN_URL = 'https://cdn.jsdelivr.net/npm/hls.js@1.5.13/dist/hls.min.js'
@@ -119,6 +123,7 @@ export const getHlsQualities = (hls: any): VideoQuality[] => {
? resolution.split('x').map((value: string) => parseInt(value, 10))
: [undefined, undefined]
const translations = getTranslations(detectBrowserLanguage());
const width = level.width || widthFromResolution
const height = level.height || heightFromResolution
const bitrate = typeof level.bitrate === 'number' ? level.bitrate : level.attrs?.BANDWIDTH
@@ -131,7 +136,7 @@ export const getHlsQualities = (hls: any): VideoQuality[] => {
} else if (typeof bitrate === 'number' && bitrate > 0) {
label = `${Math.round(bitrate / 1000)} kbps`
} else {
label = `Seviye ${index + 1}`
label = `${translations.level} ${index + 1}`
}
return {
@@ -156,41 +161,3 @@ export const getHlsQualities = (hls: any): VideoQuality[] => {
}
}
/**
* Update active quality level in HLS instance. Passing null re-enables auto.
*/
export const setHlsQualityLevel = (hls: any, levelIndex: number | null | undefined): void => {
if (!hls || !Array.isArray(hls.levels)) {
return
}
if (levelIndex === null || typeof levelIndex === 'undefined' || levelIndex < 0) {
if (hls.currentLevel !== -1) {
hls.currentLevel = -1
}
return
}
if (levelIndex >= hls.levels.length) {
return
}
if (hls.currentLevel !== levelIndex) {
hls.currentLevel = levelIndex
}
}
/**
* Set active audio track in HLS instance
*/
export const setHlsAudioTrack = (hls: any, audioTrackIndex: number): void => {
if (!hls || !hls.audioTracks) {
return
}
if (audioTrackIndex < 0 || audioTrackIndex >= hls.audioTracks.length) {
return
}
hls.audioTrack = audioTrackIndex
}
+87
View File
@@ -0,0 +1,87 @@
/**
* HLS setup and configuration utilities
*/
import type { AudioTrack, VideoQuality } from '../types'
interface HlsSetupOptions {
video: HTMLVideoElement
src: string
autoplay: boolean
onAudioTracksLoaded?: (tracks: AudioTrack[]) => void
onQualityLevelsLoaded?: (qualities: VideoQuality[]) => void
onError?: (error: Error) => void
}
export const setupHlsInstance = async ({
video,
src,
autoplay,
onAudioTracksLoaded,
onQualityLevelsLoaded,
onError,
}: HlsSetupOptions): Promise<() => void> => {
const { loadHls, isHlsSupported, getHlsAudioTracks, getHlsQualities } = await import('./hlsLoader')
const Hls = await loadHls()
if (!isHlsSupported(Hls)) {
throw new Error('HLS.js is not supported in this browser')
}
const hls = new Hls({
enableWorker: true,
lowLatencyMode: false,
})
hls.loadSource(src)
hls.attachMedia(video)
hls.on(Hls.Events.MANIFEST_PARSED, () => {
setTimeout(() => {
const tracks = getHlsAudioTracks(hls)
const qualities = getHlsQualities(hls)
if (tracks.length > 0) {
onAudioTracksLoaded?.(tracks)
}
onQualityLevelsLoaded?.(qualities)
}, 100)
if (autoplay) {
void video.play().catch(() => undefined)
}
})
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, () => {
const tracks = getHlsAudioTracks(hls)
if (tracks.length > 0) {
onAudioTracksLoaded?.(tracks)
}
})
hls.on(Hls.Events.ERROR, (_event: any, data: any) => {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls.startLoad()
break
case Hls.ErrorTypes.MEDIA_ERROR:
hls.recoverMediaError()
break
default:
onError?.(new Error('Fatal HLS error'))
break
}
}
})
;(video as any).__hlsInstance = hls
return () => {
if (hls) {
hls.destroy()
}
delete (video as any).__hlsInstance
}
}
+22 -1
View File
@@ -26,15 +26,36 @@ export default defineConfig({
formats: ['es', 'umd'],
fileName: (format) => `video-player.${format === 'es' ? 'js' : 'umd.cjs'}`,
},
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
passes: 2,
},
mangle: {
safari10: true,
},
format: {
comments: false,
},
},
rollupOptions: {
external: ['react', 'react-dom'],
external: ['react', 'react-dom', 'hls.js'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'hls.js': 'Hls',
},
compact: true,
},
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
},
},
cssCodeSplit: false,
cssMinify: true,
},
})
+23
View File
@@ -0,0 +1,23 @@
import { defineConfig } from 'vitest/config';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/test/setup.ts',
css: true,
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
exclude: [
'node_modules/',
'src/test/',
'**/*.d.ts',
'**/*.config.*',
'**/dist/',
],
},
},
});