Require Immich 3.x; code cleanup
This commit is contained in:
@@ -3,12 +3,10 @@ import { resolve } from 'path'
|
||||
|
||||
export type Config = Record<string, unknown>
|
||||
|
||||
/** Where each app keeps its config file, and how it upgrades legacy config shapes. */
|
||||
/** Where each app keeps its config file. */
|
||||
export interface LoadConfigOptions {
|
||||
/** Absolute path of the bundled config file, used when `IPP_CONFIG` is unset. */
|
||||
defaultPath?: string
|
||||
/** Rewrites legacy keys in place before the config is cached. */
|
||||
migrate?: (config: Config) => void
|
||||
}
|
||||
|
||||
// Module-level cache populated by `loadConfig()`. Access through
|
||||
@@ -19,8 +17,8 @@ let currentConfig: Config = {}
|
||||
|
||||
/**
|
||||
* Read the runtime configuration from `process.env.CONFIG` (an inline JSON
|
||||
* string, typically set in docker-compose) or from the config file. Applies
|
||||
* the app's `migrate` hook, caches the result, and returns it.
|
||||
* string, typically set in docker-compose) or from the config file, cache
|
||||
* the result, and return it.
|
||||
*
|
||||
* The default path comes from the app, not from this module's location:
|
||||
* core is compiled into its own package, so a path relative to `__dirname`
|
||||
@@ -29,7 +27,7 @@ let currentConfig: Config = {}
|
||||
* Called once from each app's `index.ts` at startup. Safe to call again in
|
||||
* tests with a fresh env to reset state.
|
||||
*/
|
||||
export function loadConfig ({ defaultPath, migrate }: LoadConfigOptions = {}): Config {
|
||||
export function loadConfig ({ defaultPath }: LoadConfigOptions = {}): Config {
|
||||
let config: Config = {}
|
||||
try {
|
||||
if (process.env.CONFIG) {
|
||||
@@ -48,7 +46,6 @@ export function loadConfig ({ defaultPath, migrate }: LoadConfigOptions = {}): C
|
||||
console.log(e)
|
||||
}
|
||||
|
||||
migrate?.(config)
|
||||
currentConfig = config
|
||||
return config
|
||||
}
|
||||
|
||||
+1
-17
@@ -1,7 +1,5 @@
|
||||
import { NextFunction, Request, Response } from 'express-serve-static-core'
|
||||
import { getConfigOption } from './config/access'
|
||||
import { respondToInvalidRequest } from './invalidRequest'
|
||||
import { log } from './utils/log'
|
||||
|
||||
/**
|
||||
* Apply the response headers configured under `ipp.responseHeaders` to the
|
||||
@@ -15,7 +13,7 @@ export function addResponseHeaders (res: Response): void {
|
||||
})
|
||||
}
|
||||
|
||||
/** Stop browsers and shared caches storing a response from a password-protected share. */
|
||||
/** Stop browsers and shared caches storing a response: a password-protected share, or an error. */
|
||||
export function addNoStoreHeaders (res: Response): void {
|
||||
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
|
||||
res.set('Pragma', 'no-cache')
|
||||
@@ -31,17 +29,3 @@ export function asyncHandler (fn: (req: Request, res: Response, next: NextFuncti
|
||||
fn(req, res, next).catch(next)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminal Express error middleware. Logs the error server-side, then applies the
|
||||
* same privacy policy as any other invalid request.
|
||||
*/
|
||||
export function errorHandler (err: unknown, req: Request, res: Response, _next: NextFunction): void {
|
||||
log.error('Error handling ' + req.method + ' ' + req.path + ' - ' +
|
||||
(err instanceof Error ? (err.stack || err.message) : String(err)))
|
||||
if (res.headersSent) {
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
respondToInvalidRequest(res, 404, 'Unhandled error for ' + req.path)
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ export async function accessible () {
|
||||
}
|
||||
|
||||
// Minimum Immich server version IPP is compatible with
|
||||
export const MIN_IMMICH_VERSION: ImmichVersion = { major: 2, minor: 0, patch: 0 }
|
||||
export const MIN_IMMICH_VERSION: ImmichVersion = { major: 3, minor: 0, patch: 0 }
|
||||
|
||||
const formatVersion = (v: ImmichVersion) => `${v.major}.${v.minor}.${v.patch}`
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Response } from 'express-serve-static-core'
|
||||
import { NextFunction, Request, Response } from 'express-serve-static-core'
|
||||
import { getConfigOption } from './config/access'
|
||||
import { addNoStoreHeaders } from './http'
|
||||
import { log } from './utils/log'
|
||||
|
||||
/** The public contract of a 404 handler, kept stable for operator replacements. */
|
||||
@@ -60,7 +61,25 @@ export function setInvalidRequestHandler (fn: InvalidRequestHandler): void {
|
||||
/**
|
||||
* Answer an invalid request through the registered handler. Every caller, in
|
||||
* core and in the apps, goes through here so a replacement applies everywhere.
|
||||
*
|
||||
* Never cacheable: routes apply `ipp.responseHeaders` (a 30-day public TTL by
|
||||
* default) before they know the request is valid.
|
||||
*/
|
||||
export function respondToInvalidRequest (res: Response, defaultResponse: number | string | null, logMessage = ''): void {
|
||||
addNoStoreHeaders(res)
|
||||
handler(res, defaultResponse, logMessage)
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminal Express error middleware. Logs the error server-side, then applies the
|
||||
* same privacy policy as any other invalid request.
|
||||
*/
|
||||
export function errorHandler (err: unknown, req: Request, res: Response, _next: NextFunction): void {
|
||||
log.error('Error handling ' + req.method + ' ' + req.path + ' - ' +
|
||||
(err instanceof Error ? (err.stack || err.message) : String(err)))
|
||||
if (res.headersSent) {
|
||||
res.end()
|
||||
return
|
||||
}
|
||||
respondToInvalidRequest(res, 404, 'Unhandled error for ' + req.path)
|
||||
}
|
||||
|
||||
@@ -10,11 +10,6 @@ export enum KeyType {
|
||||
|
||||
export interface ExifInfo {
|
||||
description?: string;
|
||||
exifImageWidth?: number;
|
||||
exifImageHeight?: number;
|
||||
// EXIF orientation string ("1".."8") or null. Values 5-8 indicate the image
|
||||
// is rotated 90°/270°, so the displayed aspect ratio swaps width/height.
|
||||
orientation?: string | null;
|
||||
// Additional EXIF fields surfaced by Immich for the metadata sidebar
|
||||
// (gated server-side by ipp.showMetadata.exif.* and .location.* config).
|
||||
dateTimeOriginal?: string | null;
|
||||
|
||||
@@ -2,7 +2,8 @@ import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest'
|
||||
import express from 'express'
|
||||
import type { Server } from 'http'
|
||||
import type { AddressInfo } from 'net'
|
||||
import { asyncHandler, errorHandler } from '../src/http'
|
||||
import { asyncHandler } from '../src/http'
|
||||
import { errorHandler } from '../src/invalidRequest'
|
||||
|
||||
/*
|
||||
Regression tests for the per-request crash class: under Express 4 a rejected
|
||||
@@ -26,6 +27,10 @@ describe('asyncHandler + errorHandler', () => {
|
||||
app.get('/boom', asyncHandler(async () => {
|
||||
throw new TypeError("Cannot read properties of undefined (reading 'headers')")
|
||||
}))
|
||||
app.get('/public-boom', asyncHandler(async (_req, res) => {
|
||||
res.set('Cache-Control', 'public, max-age=2592000')
|
||||
throw new Error('failed after the operator headers were applied')
|
||||
}))
|
||||
app.get('/mid-stream', asyncHandler(async (_req, res) => {
|
||||
res.status(200)
|
||||
res.write('partial')
|
||||
@@ -50,6 +55,12 @@ describe('asyncHandler + errorHandler', () => {
|
||||
expect(await res.text()).toBe('')
|
||||
})
|
||||
|
||||
it('sends the 404 no-store, replacing the public TTL a route set first', async () => {
|
||||
const res = await fetch(base + '/public-boom')
|
||||
expect(res.status).toBe(404)
|
||||
expect(res.headers.get('cache-control')).toContain('no-store')
|
||||
})
|
||||
|
||||
it('keeps serving other requests after a route has thrown', async () => {
|
||||
await fetch(base + '/boom')
|
||||
const res = await fetch(base + '/ok')
|
||||
|
||||
@@ -2,8 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { getImmichVersion, isImmichVersionSupported, MIN_IMMICH_VERSION } from '../src/immich/client'
|
||||
|
||||
// The startup guard that refuses to run against an Immich server older than
|
||||
// IPP supports. The floor is 2.0.0 - the columnar timeline API IPP relies on
|
||||
// to enumerate album shares landed in Immich 2.0.
|
||||
// IPP supports (3.0.0).
|
||||
|
||||
function jsonResponse (body: unknown, status = 200) {
|
||||
return {
|
||||
@@ -20,13 +19,14 @@ describe('isImmichVersionSupported', () => {
|
||||
expect(isImmichVersionSupported(MIN_IMMICH_VERSION)).toBe(true)
|
||||
})
|
||||
|
||||
it('accepts newer major/minor/patch across the 2.x and 3.x lines', () => {
|
||||
expect(isImmichVersionSupported({ major: 2, minor: 7, patch: 5 })).toBe(true)
|
||||
it('accepts newer major/minor/patch', () => {
|
||||
expect(isImmichVersionSupported({ major: 3, minor: 0, patch: 2 })).toBe(true)
|
||||
expect(isImmichVersionSupported({ major: 3, minor: 4, patch: 0 })).toBe(true)
|
||||
expect(isImmichVersionSupported({ major: 4, minor: 0, patch: 0 })).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects 1.x, which predates the columnar timeline API', () => {
|
||||
it('rejects 2.x and 1.x', () => {
|
||||
expect(isImmichVersionSupported({ major: 2, minor: 7, patch: 5 })).toBe(false)
|
||||
expect(isImmichVersionSupported({ major: 1, minor: 137, patch: 3 })).toBe(false)
|
||||
expect(isImmichVersionSupported({ major: 1, minor: 125, patch: 7 })).toBe(false)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user