mirror of
https://github.com/p-stream/simple-proxy.git
synced 2026-01-11 20:10:35 +00:00
add disable cache env
This commit is contained in:
parent
d37f33313e
commit
8ef47be346
2 changed files with 46 additions and 19 deletions
|
|
@ -1,5 +1,8 @@
|
||||||
import { setResponseHeaders } from 'h3';
|
import { setResponseHeaders } from 'h3';
|
||||||
|
|
||||||
|
// Check if caching is disabled via environment variable
|
||||||
|
const isCacheDisabled = () => process.env.DISABLE_CACHE === 'true';
|
||||||
|
|
||||||
function parseURL(req_url: string, baseUrl?: string) {
|
function parseURL(req_url: string, baseUrl?: string) {
|
||||||
if (baseUrl) {
|
if (baseUrl) {
|
||||||
return new URL(req_url, baseUrl).href;
|
return new URL(req_url, baseUrl).href;
|
||||||
|
|
@ -87,6 +90,11 @@ function startCacheCleanupInterval() {
|
||||||
startCacheCleanupInterval();
|
startCacheCleanupInterval();
|
||||||
|
|
||||||
async function prefetchSegment(url: string, headers: HeadersInit) {
|
async function prefetchSegment(url: string, headers: HeadersInit) {
|
||||||
|
// Skip prefetching if cache is disabled
|
||||||
|
if (isCacheDisabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (segmentCache.size >= CACHE_MAX_SIZE) {
|
if (segmentCache.size >= CACHE_MAX_SIZE) {
|
||||||
cleanupCache();
|
cleanupCache();
|
||||||
}
|
}
|
||||||
|
|
@ -131,6 +139,11 @@ async function prefetchSegment(url: string, headers: HeadersInit) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCachedSegment(url: string) {
|
export function getCachedSegment(url: string) {
|
||||||
|
// Return undefined immediately if cache is disabled
|
||||||
|
if (isCacheDisabled()) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const entry = segmentCache.get(url);
|
const entry = segmentCache.get(url);
|
||||||
if (entry) {
|
if (entry) {
|
||||||
if (Date.now() - entry.timestamp > CACHE_EXPIRY_MS) {
|
if (Date.now() - entry.timestamp > CACHE_EXPIRY_MS) {
|
||||||
|
|
@ -275,7 +288,10 @@ async function proxyM3U8(event: any) {
|
||||||
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
|
const proxyKeyUrl = `${baseProxyUrl}/ts-proxy?url=${encodeURIComponent(keyUrl)}&headers=${encodeURIComponent(JSON.stringify(headers))}`;
|
||||||
newLines.push(line.replace(keyUrl, proxyKeyUrl));
|
newLines.push(line.replace(keyUrl, proxyKeyUrl));
|
||||||
|
|
||||||
prefetchSegment(keyUrl, headers as HeadersInit);
|
// Only prefetch if cache is enabled
|
||||||
|
if (!isCacheDisabled()) {
|
||||||
|
prefetchSegment(keyUrl, headers as HeadersInit);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
newLines.push(line);
|
newLines.push(line);
|
||||||
}
|
}
|
||||||
|
|
@ -301,13 +317,18 @@ async function proxyM3U8(event: any) {
|
||||||
if (segmentUrls.length > 0) {
|
if (segmentUrls.length > 0) {
|
||||||
console.log(`Starting to prefetch ${segmentUrls.length} segments for ${url}`);
|
console.log(`Starting to prefetch ${segmentUrls.length} segments for ${url}`);
|
||||||
|
|
||||||
cleanupCache();
|
// Only perform cache operations if cache is enabled
|
||||||
|
if (!isCacheDisabled()) {
|
||||||
Promise.all(segmentUrls.map(segmentUrl =>
|
cleanupCache();
|
||||||
prefetchSegment(segmentUrl, headers as HeadersInit)
|
|
||||||
)).catch(error => {
|
Promise.all(segmentUrls.map(segmentUrl =>
|
||||||
console.error('Error prefetching segments:', error);
|
prefetchSegment(segmentUrl, headers as HeadersInit)
|
||||||
});
|
)).catch(error => {
|
||||||
|
console.error('Error prefetching segments:', error);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.log('Cache disabled - skipping prefetch operations');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set appropriate headers
|
// Set appropriate headers
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
import { setResponseHeaders } from 'h3';
|
import { setResponseHeaders } from 'h3';
|
||||||
import { getCachedSegment } from './m3u8-proxy';
|
import { getCachedSegment } from './m3u8-proxy';
|
||||||
|
|
||||||
|
// Check if caching is disabled via environment variable
|
||||||
|
const isCacheDisabled = () => process.env.DISABLE_CACHE === 'true';
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
// Handle CORS preflight requests
|
// Handle CORS preflight requests
|
||||||
if (isPreflightRequest(event)) return handleCors(event, {});
|
if (isPreflightRequest(event)) return handleCors(event, {});
|
||||||
|
|
@ -26,18 +29,21 @@ export default defineEventHandler(async (event) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cachedSegment = getCachedSegment(url);
|
// Only check cache if caching is enabled
|
||||||
|
if (!isCacheDisabled()) {
|
||||||
if (cachedSegment) {
|
const cachedSegment = getCachedSegment(url);
|
||||||
setResponseHeaders(event, {
|
|
||||||
'Content-Type': cachedSegment.headers['content-type'] || 'video/mp2t',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Access-Control-Allow-Headers': '*',
|
|
||||||
'Access-Control-Allow-Methods': '*',
|
|
||||||
'Cache-Control': 'public, max-age=3600' // Allow caching of TS segments
|
|
||||||
});
|
|
||||||
|
|
||||||
return cachedSegment.data;
|
if (cachedSegment) {
|
||||||
|
setResponseHeaders(event, {
|
||||||
|
'Content-Type': cachedSegment.headers['content-type'] || 'video/mp2t',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Headers': '*',
|
||||||
|
'Access-Control-Allow-Methods': '*',
|
||||||
|
'Cache-Control': 'public, max-age=3600' // Allow caching of TS segments
|
||||||
|
});
|
||||||
|
|
||||||
|
return cachedSegment.data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await globalThis.fetch(url, {
|
const response = await globalThis.fetch(url, {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue