Compare commits
1 Commits
bugfix/api
...
chore/reve
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a3877be51 |
@@ -51,3 +51,8 @@ MAX_FILE_BYTES=20971520
|
|||||||
# internal docker network. Override only if you're running the
|
# internal docker network. Override only if you're running the
|
||||||
# frontend container against a backend somewhere else.
|
# frontend container against a backend somewhere else.
|
||||||
BACKEND_URL=http://backend:8080
|
BACKEND_URL=http://backend:8080
|
||||||
|
# Per-request wall-clock cap for the /api/* reverse proxy (milliseconds).
|
||||||
|
# Default 300000 (5 min) covers a typical 200 MiB chapter upload over
|
||||||
|
# 25 Mbps; raise for users on slower upstream links or lower if a
|
||||||
|
# tighter front proxy already bounds the request lifetime.
|
||||||
|
BACKEND_PROXY_TIMEOUT_MS=300000
|
||||||
|
|||||||
2
backend/Cargo.lock
generated
2
backend/Cargo.lock
generated
@@ -1470,7 +1470,7 @@ checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.34.1"
|
version = "0.34.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "mangalord"
|
name = "mangalord"
|
||||||
version = "0.34.1"
|
version = "0.34.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
default-run = "mangalord"
|
default-run = "mangalord"
|
||||||
|
|
||||||
|
|||||||
@@ -230,24 +230,8 @@ async fn create_token(
|
|||||||
Json(input): Json<CreateTokenInput>,
|
Json(input): Json<CreateTokenInput>,
|
||||||
) -> AppResult<impl IntoResponse> {
|
) -> AppResult<impl IntoResponse> {
|
||||||
let name = input.name.trim();
|
let name = input.name.trim();
|
||||||
// Both arms use `ValidationFailed` (422 with field details) to
|
|
||||||
// match the structured-error shape `attach_tag` returns for the
|
|
||||||
// same kind of free-form-identifier validation. The other
|
|
||||||
// /auth/* handlers in this file use `InvalidInput` (400); the
|
|
||||||
// divergence is pre-existing and would warrant a project-wide
|
|
||||||
// pass to flip them all if the client side wants uniform per-
|
|
||||||
// field error rendering.
|
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
return Err(AppError::ValidationFailed {
|
return Err(AppError::InvalidInput("token name is required".into()));
|
||||||
message: "token name is required".into(),
|
|
||||||
details: serde_json::json!({ "name": "required" }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if name.chars().count() > 64 {
|
|
||||||
return Err(AppError::ValidationFailed {
|
|
||||||
message: "token name too long".into(),
|
|
||||||
details: serde_json::json!({ "name": "max 64 characters" }),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
let (raw, hash) = generate_token();
|
let (raw, hash) = generate_token();
|
||||||
let token = repo::api_token::create(&state.db, user.id, name, &hash).await?;
|
let token = repo::api_token::create(&state.db, user.id, name, &hash).await?;
|
||||||
|
|||||||
@@ -348,7 +348,6 @@ async fn attach_tag(
|
|||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(body): Json<AttachTagBody>,
|
Json(body): Json<AttachTagBody>,
|
||||||
) -> AppResult<(StatusCode, Json<TagRef>)> {
|
) -> AppResult<(StatusCode, Json<TagRef>)> {
|
||||||
validate_tag_name(&body.name)?;
|
|
||||||
if !repo::manga::exists(&state.db, id).await? {
|
if !repo::manga::exists(&state.db, id).await? {
|
||||||
return Err(AppError::NotFound);
|
return Err(AppError::NotFound);
|
||||||
}
|
}
|
||||||
@@ -395,27 +394,6 @@ async fn detach_tag(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request-side validation for `POST /mangas/:id/tags` body. Mirrors
|
|
||||||
/// the repo-level cap in `repo::tag::upsert_by_name` (max 64 chars
|
|
||||||
/// after trim) but surfaces the failure at the handler boundary with
|
|
||||||
/// the same envelope shape other validations use.
|
|
||||||
fn validate_tag_name(name: &str) -> AppResult<()> {
|
|
||||||
let trimmed = name.trim();
|
|
||||||
if trimmed.is_empty() {
|
|
||||||
return Err(AppError::ValidationFailed {
|
|
||||||
message: "tag name cannot be empty".into(),
|
|
||||||
details: json!({ "name": "required" }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if trimmed.chars().count() > 64 {
|
|
||||||
return Err(AppError::ValidationFailed {
|
|
||||||
message: "tag name too long".into(),
|
|
||||||
details: json!({ "name": "max 64 characters" }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn validate_new_manga(input: &NewManga) -> AppResult<()> {
|
fn validate_new_manga(input: &NewManga) -> AppResult<()> {
|
||||||
if input.title.trim().is_empty() {
|
if input.title.trim().is_empty() {
|
||||||
return Err(AppError::ValidationFailed {
|
return Err(AppError::ValidationFailed {
|
||||||
|
|||||||
@@ -16,13 +16,6 @@ impl LocalStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn resolve(&self, key: &str) -> Result<PathBuf, StorageError> {
|
fn resolve(&self, key: &str) -> Result<PathBuf, StorageError> {
|
||||||
// NUL bytes are rejected by the Linux syscall layer, but the
|
|
||||||
// error surfaces as an opaque IO failure rather than the
|
|
||||||
// explicit `BadKey` the rest of the contract uses. Catch it
|
|
||||||
// here so the error path is consistent.
|
|
||||||
if key.contains('\0') {
|
|
||||||
return Err(StorageError::BadKey);
|
|
||||||
}
|
|
||||||
let key = key.trim_start_matches('/');
|
let key = key.trim_start_matches('/');
|
||||||
if key.is_empty() {
|
if key.is_empty() {
|
||||||
return Err(StorageError::BadKey);
|
return Err(StorageError::BadKey);
|
||||||
@@ -121,9 +114,6 @@ mod tests {
|
|||||||
assert!(matches!(s.get(".").await, Err(StorageError::BadKey)));
|
assert!(matches!(s.get(".").await, Err(StorageError::BadKey)));
|
||||||
// Empty segment via doubled slash.
|
// Empty segment via doubled slash.
|
||||||
assert!(matches!(s.get("a//b").await, Err(StorageError::BadKey)));
|
assert!(matches!(s.get("a//b").await, Err(StorageError::BadKey)));
|
||||||
// NUL byte (rejected explicitly so callers see BadKey rather
|
|
||||||
// than an opaque IO error from the kernel).
|
|
||||||
assert!(matches!(s.put("a\0b", b"x").await, Err(StorageError::BadKey)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
|||||||
@@ -581,27 +581,3 @@ async fn delete_unknown_token_is_404(pool: PgPool) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Bot token names are user-supplied free-form strings; a 10 MB name
|
|
||||||
/// was accepted before. Cap at 64 chars to match the other free-form
|
|
||||||
/// identifier caps (tags, collection names). The response uses
|
|
||||||
/// `ValidationFailed` (422 with per-field details) so clients can
|
|
||||||
/// render the same shape they already handle for `attach_tag`.
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn create_token_rejects_name_over_64_chars(pool: PgPool) {
|
|
||||||
let h = common::harness(pool);
|
|
||||||
let (_, cookie) = common::register_user(&h.app).await;
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(common::post_json_with_cookie(
|
|
||||||
"/api/v1/auth/tokens",
|
|
||||||
json!({ "name": "x".repeat(65) }),
|
|
||||||
&cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
|
||||||
let body = common::body_json(resp).await;
|
|
||||||
assert_eq!(body["error"]["code"], "validation_failed");
|
|
||||||
assert!(body["error"]["details"]["name"].is_string());
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -59,31 +59,6 @@ async fn reattach_same_tag_is_idempotent_and_returns_200(pool: PgPool) {
|
|||||||
assert_eq!(second.status(), StatusCode::OK);
|
assert_eq!(second.status(), StatusCode::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tag names over 64 chars are rejected at the handler boundary. The
|
|
||||||
/// repo enforces the same cap, but doing it at the handler keeps the
|
|
||||||
/// envelope consistent with the other validation paths
|
|
||||||
/// (username, collection name, etc.).
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
|
||||||
async fn attach_rejects_tag_name_over_64_chars(pool: PgPool) {
|
|
||||||
let h = common::harness(pool);
|
|
||||||
let (_, cookie) = common::register_user(&h.app).await;
|
|
||||||
let manga_id = common::seed_manga_via_api(&h.app, &cookie, "Berserk").await;
|
|
||||||
|
|
||||||
let long_name: String = "x".repeat(65);
|
|
||||||
let resp = h
|
|
||||||
.app
|
|
||||||
.oneshot(common::post_json_with_cookie(
|
|
||||||
&format!("/api/v1/mangas/{manga_id}/tags"),
|
|
||||||
json!({ "name": long_name }),
|
|
||||||
&cookie,
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
|
||||||
let body = common::body_json(resp).await;
|
|
||||||
assert_eq!(body["error"]["code"], "validation_failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[sqlx::test(migrations = "./migrations")]
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
async fn tag_names_dedup_case_insensitively(pool: PgPool) {
|
async fn tag_names_dedup_case_insensitively(pool: PgPool) {
|
||||||
let h = common::harness(pool);
|
let h = common::harness(pool);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mangalord-frontend",
|
"name": "mangalord-frontend",
|
||||||
"version": "0.34.1",
|
"version": "0.34.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -118,4 +118,77 @@ describe('hooks.server proxy', () => {
|
|||||||
expect(body.error.code).toBe('upstream_unavailable');
|
expect(body.error.code).toBe('upstream_unavailable');
|
||||||
expect(errSpy).toHaveBeenCalled();
|
expect(errSpy).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('strips every hop-by-hop header listed in RFC 7230 §6.1', async () => {
|
||||||
|
// Defence in depth: axum doesn't emit these, but a future
|
||||||
|
// middleware that did would otherwise leak per-connection
|
||||||
|
// state across the proxy boundary.
|
||||||
|
fetchSpy.mockResolvedValueOnce(new Response('[]', { status: 200 }));
|
||||||
|
const resolve = vi.fn();
|
||||||
|
await handle({
|
||||||
|
event: makeEvent('/api/v1/health', {
|
||||||
|
headers: {
|
||||||
|
host: 'app.example.com',
|
||||||
|
'content-length': '0',
|
||||||
|
connection: 'keep-alive',
|
||||||
|
'keep-alive': 'timeout=5',
|
||||||
|
'proxy-authenticate': 'Basic realm=x',
|
||||||
|
'proxy-authorization': 'Basic xyz',
|
||||||
|
te: 'trailers',
|
||||||
|
trailer: 'Expires',
|
||||||
|
'transfer-encoding': 'chunked',
|
||||||
|
upgrade: 'websocket',
|
||||||
|
// A non-hop-by-hop header to ensure non-targets
|
||||||
|
// aren't accidentally stripped.
|
||||||
|
'x-custom': 'pass-through'
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
resolve
|
||||||
|
});
|
||||||
|
const init = fetchSpy.mock.calls[0][1] as RequestInit;
|
||||||
|
const headers = init.headers as Headers;
|
||||||
|
for (const h of [
|
||||||
|
'host',
|
||||||
|
'content-length',
|
||||||
|
'connection',
|
||||||
|
'keep-alive',
|
||||||
|
'proxy-authenticate',
|
||||||
|
'proxy-authorization',
|
||||||
|
'te',
|
||||||
|
'trailer',
|
||||||
|
'transfer-encoding',
|
||||||
|
'upgrade'
|
||||||
|
]) {
|
||||||
|
expect(headers.get(h), `${h} should be stripped`).toBeNull();
|
||||||
|
}
|
||||||
|
expect(headers.get('x-custom')).toBe('pass-through');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('aborts and returns 502 when the upstream stalls past the timeout', async () => {
|
||||||
|
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||||
|
// Simulate an aborted fetch (AbortController.abort() raises a
|
||||||
|
// DOMException with name 'AbortError' on Node's fetch). The
|
||||||
|
// handler should treat it as the same upstream_unavailable
|
||||||
|
// 502 it uses for any other network failure.
|
||||||
|
const abortErr = new DOMException('aborted', 'AbortError');
|
||||||
|
fetchSpy.mockRejectedValueOnce(abortErr);
|
||||||
|
|
||||||
|
const resolve = vi.fn();
|
||||||
|
const resp = await handle({ event: makeEvent('/api/v1/slow'), resolve });
|
||||||
|
expect(resp.status).toBe(502);
|
||||||
|
const body = await resp.json();
|
||||||
|
expect(body.error.code).toBe('upstream_unavailable');
|
||||||
|
expect(errSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('attaches an AbortSignal to the upstream fetch so it can time out', async () => {
|
||||||
|
fetchSpy.mockResolvedValueOnce(new Response('[]', { status: 200 }));
|
||||||
|
const resolve = vi.fn();
|
||||||
|
await handle({ event: makeEvent('/api/v1/health'), resolve });
|
||||||
|
const init = fetchSpy.mock.calls[0][1] as RequestInit;
|
||||||
|
expect(init.signal).toBeInstanceOf(AbortSignal);
|
||||||
|
// The signal hasn't fired (handler returned in time), but its
|
||||||
|
// presence is the contract this test is pinning.
|
||||||
|
expect(init.signal?.aborted).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -12,20 +12,66 @@ import type { Handle } from '@sveltejs/kit';
|
|||||||
|
|
||||||
const BACKEND_URL = process.env.BACKEND_URL ?? 'http://localhost:8080';
|
const BACKEND_URL = process.env.BACKEND_URL ?? 'http://localhost:8080';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hop-by-hop headers per RFC 7230 §6.1. These are scoped to a single
|
||||||
|
* transport-level connection and must not be forwarded by a proxy.
|
||||||
|
* Plus `host` and `content-length`: `host` would mislead the backend
|
||||||
|
* about its origin, and `content-length` is recomputed by the upstream
|
||||||
|
* fetch from the body stream.
|
||||||
|
*/
|
||||||
|
const HOP_BY_HOP_HEADERS = [
|
||||||
|
'host',
|
||||||
|
'content-length',
|
||||||
|
'connection',
|
||||||
|
'keep-alive',
|
||||||
|
'proxy-authenticate',
|
||||||
|
'proxy-authorization',
|
||||||
|
'te',
|
||||||
|
'trailer',
|
||||||
|
'transfer-encoding',
|
||||||
|
'upgrade'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cap each proxied request at 5 minutes. The bound exists to surface
|
||||||
|
* a wedged backend (stuck on a slow DB query, deadlocked, etc.) as a
|
||||||
|
* 502 rather than letting the browser request hang indefinitely.
|
||||||
|
*
|
||||||
|
* The default leans toward the slow-upload end of the spectrum: at a
|
||||||
|
* 1 Mbps upstream, a 200 MiB chapter upload (the default
|
||||||
|
* `MAX_REQUEST_BYTES` cap) needs ~27 minutes; 300 s covers the more
|
||||||
|
* realistic 25 Mbps urban-broadband case (~64 s for the same upload)
|
||||||
|
* with comfortable headroom. Operators serving very slow clients
|
||||||
|
* should raise `BACKEND_PROXY_TIMEOUT_MS`; operators behind a
|
||||||
|
* tighter upstream proxy may want to lower it. A future improvement
|
||||||
|
* is an idle-based timeout (reset per chunk) instead of this
|
||||||
|
* wall-clock budget — that's a fair bit more code, deferred.
|
||||||
|
*/
|
||||||
|
const PROXY_TIMEOUT_MS = (() => {
|
||||||
|
const raw = process.env.BACKEND_PROXY_TIMEOUT_MS;
|
||||||
|
const n = raw ? Number(raw) : 300_000;
|
||||||
|
return Number.isFinite(n) && n > 0 ? n : 300_000;
|
||||||
|
})();
|
||||||
|
|
||||||
export const handle: Handle = async ({ event, resolve }) => {
|
export const handle: Handle = async ({ event, resolve }) => {
|
||||||
if (event.url.pathname.startsWith('/api/')) {
|
if (event.url.pathname.startsWith('/api/')) {
|
||||||
const target = `${BACKEND_URL}${event.url.pathname}${event.url.search}`;
|
const target = `${BACKEND_URL}${event.url.pathname}${event.url.search}`;
|
||||||
|
|
||||||
// Strip hop-by-hop headers — `host` would mislead the backend
|
|
||||||
// about the origin, and `content-length` will be recomputed.
|
|
||||||
const headers = new Headers(event.request.headers);
|
const headers = new Headers(event.request.headers);
|
||||||
headers.delete('host');
|
for (const h of HOP_BY_HOP_HEADERS) headers.delete(h);
|
||||||
headers.delete('content-length');
|
|
||||||
|
// AbortController times the upstream fetch out so a backend
|
||||||
|
// wedged on a slow DB query doesn't keep the browser request
|
||||||
|
// hanging forever. The `signal` is also wired into the
|
||||||
|
// RequestInit so the body stream is cancelled cleanly.
|
||||||
|
const ctrl = new AbortController();
|
||||||
|
const timeoutHandle = setTimeout(() => ctrl.abort(), PROXY_TIMEOUT_MS);
|
||||||
|
|
||||||
const init: RequestInit & { duplex?: 'half' } = {
|
const init: RequestInit & { duplex?: 'half' } = {
|
||||||
method: event.request.method,
|
method: event.request.method,
|
||||||
headers,
|
headers,
|
||||||
redirect: 'manual'
|
redirect: 'manual',
|
||||||
|
signal: ctrl.signal
|
||||||
};
|
};
|
||||||
if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {
|
if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {
|
||||||
init.body = event.request.body;
|
init.body = event.request.body;
|
||||||
@@ -39,11 +85,13 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|||||||
upstream = await fetch(target, init);
|
upstream = await fetch(target, init);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Network-layer failure (DNS / connection refused / TLS
|
// Network-layer failure (DNS / connection refused / TLS
|
||||||
// handshake) — most commonly "backend container restarting".
|
// handshake / abort by timeout) — most commonly "backend
|
||||||
// SvelteKit's default 500 would be an HTML page that
|
// container restarting". SvelteKit's default 500 would be
|
||||||
// client.ts can't .json(), which masks the real cause. Emit
|
// an HTML page that client.ts can't .json(), which masks
|
||||||
// the standard envelope with a dedicated code instead.
|
// the real cause. Emit the standard envelope with a
|
||||||
|
// dedicated code instead.
|
||||||
console.error('Proxy to backend failed:', e);
|
console.error('Proxy to backend failed:', e);
|
||||||
|
clearTimeout(timeoutHandle);
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
error: {
|
error: {
|
||||||
@@ -58,6 +106,7 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearTimeout(timeoutHandle);
|
||||||
return new Response(upstream.body, {
|
return new Response(upstream.body, {
|
||||||
status: upstream.status,
|
status: upstream.status,
|
||||||
statusText: upstream.statusText,
|
statusText: upstream.statusText,
|
||||||
|
|||||||
@@ -94,11 +94,6 @@ describe('auth api client', () => {
|
|||||||
expect(url).toMatch(/\/v1\/auth\/logout$/);
|
expect(url).toMatch(/\/v1\/auth\/logout$/);
|
||||||
const init = fetchSpy.mock.calls[0][1] as RequestInit;
|
const init = fetchSpy.mock.calls[0][1] as RequestInit;
|
||||||
expect(init.method).toBe('POST');
|
expect(init.method).toBe('POST');
|
||||||
// Consistent content-type for all mutation requests, matching
|
|
||||||
// the rest of the module — axum doesn't require it but the
|
|
||||||
// header keeps the request style uniform.
|
|
||||||
const headers = new Headers(init.headers);
|
|
||||||
expect(headers.get('content-type')).toBe('application/json');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('me returns the user on 200', async () => {
|
it('me returns the user on 200', async () => {
|
||||||
|
|||||||
@@ -32,14 +32,7 @@ export async function login(creds: Credentials): Promise<User> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function logout(): Promise<void> {
|
export async function logout(): Promise<void> {
|
||||||
await request<void>('/v1/auth/logout', {
|
await request<void>('/v1/auth/logout', { method: 'POST' });
|
||||||
method: 'POST',
|
|
||||||
// Consistent with the other POST/PATCH helpers in this module.
|
|
||||||
// axum doesn't require it (no body), but keeping the header
|
|
||||||
// on every mutation request avoids the false-flag in logs and
|
|
||||||
// matches the project's style.
|
|
||||||
headers: { 'content-type': 'application/json' }
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ChangePassword = {
|
export type ChangePassword = {
|
||||||
|
|||||||
@@ -350,48 +350,54 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flush read-progress as the tab is closing. A plain `fetch()`
|
* `fetch()` initiated during `pagehide` / `beforeunload` is
|
||||||
* during `pagehide` / `beforeunload` is cancelled by every
|
* cancelled by every browser by default. `sendBeacon` is the
|
||||||
* browser; `fetch(..., { keepalive: true })` is the supported
|
* supported way to ship a small payload during unload — it's
|
||||||
* escape hatch and survives the close.
|
* guaranteed to survive even if the tab is closing. Failure here
|
||||||
*
|
* is silent because the API is fire-and-forget.
|
||||||
* `sendBeacon` would be the textbook alternative, but it's
|
|
||||||
* POST-only and `/me/read-progress` takes PUT — so a beacon
|
|
||||||
* always 405s, adds server-log noise, then falls through to this
|
|
||||||
* same keepalive path anyway. The beacon was dropped; the
|
|
||||||
* keepalive fetch is the only path.
|
|
||||||
*/
|
*/
|
||||||
function flushFinalProgress() {
|
function beaconFinalProgress() {
|
||||||
if (!session.user) return;
|
if (!session.user) return;
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
manga_id: manga.id,
|
manga_id: manga.id,
|
||||||
chapter_id: chapter.id,
|
chapter_id: chapter.id,
|
||||||
page: progressPage
|
page: progressPage
|
||||||
});
|
});
|
||||||
|
const blob = new Blob([body], { type: 'application/json' });
|
||||||
|
// sendBeacon only supports POST — the server's PUT route is
|
||||||
|
// strict on method. The dedicated POST alias is omitted; in
|
||||||
|
// practice the in-app navigation path (back-link, chapter
|
||||||
|
// links) already covers the common-case unmount via the
|
||||||
|
// onDestroy fetch. Fall through to fetch+keepalive for browser
|
||||||
|
// implementations that don't honor sendBeacon for this endpoint.
|
||||||
try {
|
try {
|
||||||
void fetch('/api/v1/me/read-progress', {
|
const ok = navigator.sendBeacon('/api/v1/me/read-progress', blob);
|
||||||
method: 'PUT',
|
if (!ok) throw new Error('sendBeacon rejected');
|
||||||
headers: { 'content-type': 'application/json' },
|
|
||||||
body,
|
|
||||||
keepalive: true,
|
|
||||||
credentials: 'include'
|
|
||||||
});
|
|
||||||
} catch {
|
} catch {
|
||||||
// keepalive fetch was rejected (very old Firefox etc.);
|
try {
|
||||||
// the in-app onDestroy flush below catches the SPA-
|
void fetch('/api/v1/me/read-progress', {
|
||||||
// navigation case, which is the common one anyway.
|
method: 'PUT',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body,
|
||||||
|
keepalive: true,
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
// Final fallback failed; the in-app onDestroy flush
|
||||||
|
// below catches the SPA-navigation case.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
window.addEventListener('pagehide', flushFinalProgress);
|
window.addEventListener('pagehide', beaconFinalProgress);
|
||||||
});
|
});
|
||||||
|
|
||||||
onDestroy(() => {
|
onDestroy(() => {
|
||||||
observer?.disconnect();
|
observer?.disconnect();
|
||||||
if (progressTimer) clearTimeout(progressTimer);
|
if (progressTimer) clearTimeout(progressTimer);
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
window.removeEventListener('pagehide', flushFinalProgress);
|
window.removeEventListener('pagehide', beaconFinalProgress);
|
||||||
}
|
}
|
||||||
// Don't let the fullscreen flag leak to non-reader pages —
|
// Don't let the fullscreen flag leak to non-reader pages —
|
||||||
// otherwise the layout header would stay slid-off on /upload
|
// otherwise the layout header would stay slid-off on /upload
|
||||||
|
|||||||
Reference in New Issue
Block a user