diff --git a/src/index.ts b/src/index.ts index 692e9ad..c3cdcc1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -import { promises as fs } from 'fs'; import { randomUUID } from 'node:crypto'; import axios from 'axios'; import { setupCache } from 'axios-cache-interceptor'; @@ -6,7 +5,6 @@ import axiosRetry from 'axios-retry'; import express, { NextFunction, Request, Response } from 'express'; // eslint-disable-next-line import/no-named-as-default import rateLimit from 'express-rate-limit'; -import { glob } from 'glob'; import winston from 'winston'; import { ConfigureController, ExtractController, ManifestController, StreamController } from './controller'; import { BlockedError, logErrorAndReturnNiceString } from './error'; @@ -15,7 +13,7 @@ import { createSources, Source } from './source'; import { HomeCine } from './source/HomeCine'; import { MeineCloud } from './source/MeineCloud'; import { MostraGuarda } from './source/MostraGuarda'; -import { contextFromRequestAndResponse, envGet, envIsProd, Fetcher, getCacheDir, StreamResolver } from './utils'; +import { clearCache, contextFromRequestAndResponse, envGet, envIsProd, Fetcher, StreamResolver } from './utils'; if (envIsProd()) { console.log = console.warn = console.error = console.info = console.debug = () => { /* disable in favor of logger */ }; @@ -58,10 +56,7 @@ if (envIsProd()) { if (envGet('CACHE_FILES_DELETE_ON_START')) { (async function () { - for (const file of await glob(`${getCacheDir()}/webstreamr*`)) { - logger.info(`Delete cache file ${file}`); - await fs.rm(file); - } + await clearCache(logger); })(); } diff --git a/src/utils/cache.ts b/src/utils/cache.ts index 940fccb..63c1c8d 100644 --- a/src/utils/cache.ts +++ b/src/utils/cache.ts @@ -1,11 +1,16 @@ /* istanbul ignore file */ import fs from 'node:fs'; +import * as os from 'node:os'; // eslint-disable-next-line import/no-named-as-default import KeyvSqlite from '@keyv/sqlite'; import { KeyvCacheableMemory } from 'cacheable'; +import { glob } from 'glob'; import { KeyvStoreAdapter } from 'keyv'; import * as sqlite3 from 'sqlite3'; -import { envIsTest, getCacheDir } from './env'; +import winston from 'winston'; +import { envGet, envIsTest } from './env'; + +const getCacheDir = (): string => envGet('CACHE_DIR') ?? os.tmpdir(); const scheduleKeyvSqliteCleanup = (keyvSqlite: KeyvSqlite): void => { const filename = keyvSqlite.opts.db; @@ -24,13 +29,22 @@ const scheduleKeyvSqliteCleanup = (keyvSqlite: KeyvSqlite): void => { }; export const createKeyvSqlite = (name: string): KeyvStoreAdapter => { - if (envIsTest()) { + const cacheDir = getCacheDir(); + + if (envIsTest() || !cacheDir) { return new KeyvCacheableMemory(); } - const keyvSqlite = new KeyvSqlite(`sqlite://${getCacheDir()}/webstreamr-${name}.sqlite`); + const keyvSqlite = new KeyvSqlite(`sqlite://${cacheDir}/webstreamr-${name}.sqlite`); scheduleKeyvSqliteCleanup(keyvSqlite); return keyvSqlite; }; + +export const clearCache = async (logger: winston.Logger): Promise => { + for (const file of await glob(`${getCacheDir()}/webstreamr*`)) { + logger.info(`Delete cache file ${file}`); + fs.rmSync(file); + } +}; diff --git a/src/utils/env.test.ts b/src/utils/env.test.ts index 024e7b0..71d21ed 100644 --- a/src/utils/env.test.ts +++ b/src/utils/env.test.ts @@ -1,6 +1,5 @@ -import * as os from 'node:os'; import { Request } from 'express'; -import { envGet, envGetAppId, envGetAppName, envGetRequired, envIsProd, getCacheDir, isElfHostedInstance } from './env'; +import { envGet, envGetAppId, envGetAppName, envGetRequired, envIsProd, isElfHostedInstance } from './env'; describe('env', () => { test('envGet', () => { @@ -44,13 +43,4 @@ describe('env', () => { expect(isElfHostedInstance({ host: 'someuser.elfhosted.com' } as Request)).toBeTruthy(); expect(isElfHostedInstance({ host: 'webstreamr.hayd.uk' } as Request)).toBeFalsy(); }); - - test('getCacheDir', () => { - const previousCacheDir = process.env['CACHE_DIR']; - delete process.env['CACHE_DIR']; - expect(getCacheDir()).toBe(os.tmpdir()); - process.env['CACHE_DIR'] = previousCacheDir; - - expect(getCacheDir()).toBe('/dev/null'); - }); }); diff --git a/src/utils/env.ts b/src/utils/env.ts index 9259d7f..cb43360 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -1,4 +1,3 @@ -import * as os from 'node:os'; import { Request } from 'express'; export const envGet = (name: string): string | undefined => process.env[name]; @@ -21,5 +20,3 @@ export const envIsProd = (): boolean => process.env['NODE_ENV'] === 'production' export const envIsTest = (): boolean => process.env['NODE_ENV'] === 'test'; export const isElfHostedInstance = (req: Request): boolean => req.host.endsWith('elfhosted.com'); - -export const getCacheDir = (): string => envGet('CACHE_DIR') ?? os.tmpdir();