From dae8a33d67124bd33c9048a600db53cd4477d54d Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 30 Oct 2026 05:40:44 +0100 Subject: [PATCH 1/5] [chuchi] don't throw on same host links with a path starting with // The history got the uri alone, and a path like //evil.com/x is read as another host, so pushState threw a SecurityError after the click was already prevented and the request was left half open. The history now gets the full url on the current origin. Refs #6 Co-Authored-By: Claude Opus 5.5 --- chuchi/src/routing/Router.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/chuchi/src/routing/Router.ts b/chuchi/src/routing/Router.ts index ad2ae61..7e6e88b 100644 --- a/chuchi/src/routing/Router.ts +++ b/chuchi/src/routing/Router.ts @@ -165,7 +165,7 @@ export default class Router { */ pushReq(req: Request): void { this.currentRequest.setSilent(req); - window.history.pushState(req.toHistoryState(), '', req.uri); + window.history.pushState(req.toHistoryState(), '', historyUrl(req)); this.currentRequest.notify(); } @@ -174,7 +174,7 @@ export default class Router { */ replaceReq(req: Request): void { this.currentRequest.setSilent(req); - window.history.replaceState(req.toHistoryState(), '', req.uri); + window.history.replaceState(req.toHistoryState(), '', historyUrl(req)); this.currentRequest.notify(); } @@ -273,9 +273,13 @@ export default class Router { } if (history === 'replace') { - window.history.replaceState(req.toHistoryState(), '', req.uri); + window.history.replaceState( + req.toHistoryState(), + '', + historyUrl(req), + ); } else if (history === 'push') { - window.history.pushState(req.toHistoryState(), '', nUri); + window.history.pushState(req.toHistoryState(), '', historyUrl(req)); } this.currentRequest.set(req); @@ -403,6 +407,16 @@ export default class Router { } } +/** + * Returns the url to store in the history + * + * The uri alone is not enough, a path starting with // would be read as + * another host and pushState would throw + */ +function historyUrl(req: Request): string { + return window.location.origin + req.uri; +} + /** * Returns true if the click should be intercepted */ From e71043d9b25693c41671c1f43add120c3b228a25 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 30 Oct 2026 05:41:55 +0100 Subject: [PATCH 2/5] [utils] fix range length, searchScore right distance and SlotArray double remove range computed its length as end - start / step, so range(0, 10, 2) had holes and range(-5, 5, 2) or range(5, 2) threw. It now collects the values in a plain array. searchScore measured the right distance with searchSpaceLeft, back over the match itself, so an exact match scored like a match inside a longer word. It now uses the unused searchSpaceRight from the last matched character: foo/foo is 1, foo/foobarbaz is 7. Removing a SlotArray slot twice added it to free twice, so two later pushes got the same index and the first value was overwritten. Refs #6 Co-Authored-By: Claude Opus 5.5 --- chuchi-utils/src/collections/SlotArray.ts | 3 ++- chuchi-utils/src/index.ts | 8 ++------ chuchi-utils/src/search.ts | 5 +---- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/chuchi-utils/src/collections/SlotArray.ts b/chuchi-utils/src/collections/SlotArray.ts index 673766e..a6039d1 100644 --- a/chuchi-utils/src/collections/SlotArray.ts +++ b/chuchi-utils/src/collections/SlotArray.ts @@ -71,7 +71,8 @@ export default class SlotArray { * @param id - The index to remove the item from. */ remove(id: number) { - if (this.inner.length <= id) return; + // removing twice would add the slot twice to free + if (this.inner.length <= id || this.inner[id] === null) return; this.inner[id] = null; this.free.push(id); diff --git a/chuchi-utils/src/index.ts b/chuchi-utils/src/index.ts index a8f3b4f..9caa89b 100644 --- a/chuchi-utils/src/index.ts +++ b/chuchi-utils/src/index.ts @@ -82,14 +82,10 @@ export function randomToken(length: number = 8): string { * @param step - The step size between values, defaults to 1. * @returns The generated array. */ -// todo improve this function export function range(start: number, end: number, step: number = 1): number[] { - const len = end - start / step; - const ar = new Array(len); - let c = 0; + const ar = []; for (let i = start; i < end; i += step) { - ar[c] = i; - c += 1; + ar.push(i); } return ar; } diff --git a/chuchi-utils/src/search.ts b/chuchi-utils/src/search.ts index 2723d62..40a1bf7 100644 --- a/chuchi-utils/src/search.ts +++ b/chuchi-utils/src/search.ts @@ -17,7 +17,7 @@ export function searchScore(search: string, val: string): number { if (i === -1) return 0; const distLeft = searchSpaceLeft(i, val); - const distRight = searchSpaceLeft(i + search.length, val); + const distRight = searchSpaceRight(i + search.length - 1, val); return 1 + distLeft + distRight; } @@ -35,9 +35,6 @@ function searchSpaceLeft(idx: number, val: string): number { } // calculate distance to space right of -// todo is this used??? -// @ts-ignore -/* eslint-disable @typescript-eslint/no-unused-vars */ function searchSpaceRight(idx: number, val: string): number { let dist = 0; while (idx < val.length - 1) { From ce9274bae7c205823c3509f28e62c7f04f6f8db1 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 30 Oct 2026 05:56:30 +0100 Subject: [PATCH 3/5] [core] don't crash in ApiError.fromJson on null, numbers or {} The XHR helpers pass null when the error response was not json, which threw "Cannot use 'in' operator" instead of returning an ApiError. Such values now become OTHER. Refs #6 Co-Authored-By: Claude Opus 5.5 --- chuchi-core/src/api/ApiError.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/chuchi-core/src/api/ApiError.ts b/chuchi-core/src/api/ApiError.ts index 6863f71..46af81e 100644 --- a/chuchi-core/src/api/ApiError.ts +++ b/chuchi-core/src/api/ApiError.ts @@ -47,11 +47,16 @@ export default class ApiError { static fromJson(obj: any): ApiError { if (typeof obj === 'string') return new ApiError(obj, null); + if (!obj || typeof obj !== 'object') + return ApiError.newOther('unexpected error response: ' + obj); + // {kind, data} if ('kind' in obj) return new ApiError(obj.kind, obj?.data ?? null); // {'kind': data} - const [kind, data] = Object.entries(obj)[0]; + const entry = Object.entries(obj)[0]; + if (!entry) return ApiError.newOther('empty error response'); + const [kind, data] = entry; return new ApiError(kind, data); } From 1ac0ef46024ab348cbb93a7ec3dfb3a47d70a80e Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 30 Oct 2026 05:56:30 +0100 Subject: [PATCH 4/5] [legacy] inViewY is true for elements taller than the viewport An element that covers the whole viewport has its top above and its bottom below the viewport, so neither check matched. Refs #6 Co-Authored-By: Claude Opus 5.5 --- chuchi-legacy/src/dom/utils.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/chuchi-legacy/src/dom/utils.ts b/chuchi-legacy/src/dom/utils.ts index a5b94c1..61f9f8b 100644 --- a/chuchi-legacy/src/dom/utils.ts +++ b/chuchi-legacy/src/dom/utils.ts @@ -74,10 +74,8 @@ export function inViewY(el: Element) { const wHeight = window.innerHeight; const { top, bottom } = el.getBoundingClientRect(); - const topInView = top >= 0 && top <= wHeight; - const bottomInView = bottom >= 0 && bottom <= wHeight; - - return topInView || bottomInView; + // also true if the element is taller than the viewport and covers it + return top <= wHeight && bottom >= 0; } // name --my-var From 9821d7f79767a1f1317a127468955441e7a6f8a7 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 30 Oct 2026 06:02:10 +0100 Subject: [PATCH 5/5] [utils] preallocate the range array again Collecting the values with push was about 3-4x slower than the old preallocated array for 1000+ values. range now preallocates with the correct length, which is as fast as before. A step of 0 or below returns an empty array instead of looping forever. Co-Authored-By: Claude Opus 5.5 --- chuchi-utils/src/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/chuchi-utils/src/index.ts b/chuchi-utils/src/index.ts index 9caa89b..59290d1 100644 --- a/chuchi-utils/src/index.ts +++ b/chuchi-utils/src/index.ts @@ -83,9 +83,11 @@ export function randomToken(length: number = 8): string { * @returns The generated array. */ export function range(start: number, end: number, step: number = 1): number[] { - const ar = []; - for (let i = start; i < end; i += step) { - ar.push(i); + // preallocating is a lot faster than push for bigger ranges + const len = step > 0 ? Math.max(0, Math.ceil((end - start) / step)) : 0; + const ar = new Array(len); + for (let c = 0; c < len; c++) { + ar[c] = start + c * step; } return ar; }