diff --git a/apps/yaak-proxy/components/AddColumnDialog.tsx b/apps/yaak-proxy/components/AddColumnDialog.tsx index 2cba433ad..c2f3cea22 100644 --- a/apps/yaak-proxy/components/AddColumnDialog.tsx +++ b/apps/yaak-proxy/components/AddColumnDialog.tsx @@ -1,4 +1,6 @@ -import { Compartment, type Extension } from "@codemirror/state"; +import { startCompletion } from "@codemirror/autocomplete"; +import { Compartment, EditorState, type Extension } from "@codemirror/state"; +import { EditorView, ViewPlugin } from "@codemirror/view"; import type { Schema } from "@yaakapp-internal/query"; import { Dialog, @@ -11,7 +13,12 @@ import { queryFilter } from "@yaakapp-internal/ui/editor"; import { atom, useAtom, useSetAtom } from "jotai"; import { useEffect, useMemo, useRef, useState } from "react"; import { useRpcQuery } from "../hooks/useRpcQuery"; -import { addCustomColumn, customPath } from "../lib/columns"; +import { + addCustomColumn, + customPath, + customPathProblem, + fieldReferenceChanges, +} from "../lib/columns"; import { loadSeenValues } from "../lib/searchBox"; import { columnPrefsAtom } from "../lib/store"; @@ -20,9 +27,31 @@ export const addColumnDialogAtom = atom(false); const FORM_ID = "add-column"; +/** Keeps the field a reference to a field, `@header.x-cache`, whatever was typed or pasted */ +const asFieldReference = EditorState.transactionFilter.of((tr) => { + if (!tr.docChanged) return tr; + const changes = fieldReferenceChanges(tr.newDoc.toString()); + return changes.length === 0 ? tr : [tr, { changes, sequential: true }]; +}); + +/** Opens completion on the fields while the field is focused and empty */ +const completeWhenEmpty = [ + ViewPlugin.define((view) => { + if (view.hasFocus && view.state.doc.length === 0) { + queueMicrotask(() => startCompletion(view)); + } + return {}; + }), + EditorView.updateListener.of((update) => { + if (update.focusChanged && update.view.hasFocus && update.state.doc.length === 0) { + startCompletion(update.view); + } + }), +]; + /** - * Adds a column for a header or query parameter, named by its field as the search names it, - * `header.x-request-id` or `query.page`, completed as the search box completes it + * Adds a column for a header or query parameter, named by a reference to its field as the search + * names it, `@header.x-request-id` or `@query.page`, completed from the fields and the names seen */ export function AddColumnDialog() { const [open, setOpen] = useAtom(addColumnDialogAtom); @@ -41,13 +70,17 @@ function AddColumnForm({ onClose }: { onClose: () => void }) { const filter = useMemo(() => { if (schema == null) return []; const maps: Schema = { ...schema, fields: schema.fields.filter((f) => f.map) }; - return queryFilter({ schema: maps, loadValues: loadSeenValues }); + return [ + queryFilter({ schema: maps, loadValues: loadSeenValues, fieldsOnly: true }), + completeWhenEmpty, + ]; }, [schema]); - const extensions = useMemo(() => [compartment.current.of(filter)], [filter]); + const extensions = useMemo(() => [asFieldReference, compartment.current.of(filter)], [filter]); useEffect(() => { handleRef.current?.dispatch({ effects: compartment.current.reconfigure(filter) }); }, [filter]); const path = customPath(text); + const problem = customPathProblem(text); const submit = () => { if (path == null) return; @@ -77,6 +110,7 @@ function AddColumnForm({ onClose }: { onClose: () => void }) { placeholder="@header.x-request-id" defaultValue="" onChange={setText} + validate={problem == null} extensions={extensions} onKeyDown={(e) => { if (e.key === "Enter" && !e.defaultPrevented) { @@ -85,6 +119,7 @@ function AddColumnForm({ onClose }: { onClose: () => void }) { } }} /> +
{problem}
{ + (column: ColumnId, add: boolean) => { const current = latest.current.sort; const next = add ? toggleSortKey(current, column) : nextSort(current, column); if (next.length === 0) { diff --git a/apps/yaak-proxy/components/FlowListHeader.tsx b/apps/yaak-proxy/components/FlowListHeader.tsx index 65033f37c..19a483752 100644 --- a/apps/yaak-proxy/components/FlowListHeader.tsx +++ b/apps/yaak-proxy/components/FlowListHeader.tsx @@ -1,4 +1,4 @@ -import type { FlowSortKey, SortColumn } from "@yaakapp-internal/proxy-lib"; +import type { FlowSortKey } from "@yaakapp-internal/proxy-lib"; import { ContextMenu, Dropdown, @@ -17,6 +17,7 @@ import { type ColumnDef, type ColumnId, type ColumnPrefs, + type CustomColumnId, canHide, hideOtherColumns, isCustomColumn, @@ -38,7 +39,7 @@ interface Props { prefs: ColumnPrefs; sort: FlowSortKey[]; /** A click on a label, or with `add`, an Option-click */ - onSort: (column: SortColumn, add: boolean) => void; + onSort: (column: ColumnId, add: boolean) => void; /** Sets the sort from the menus; empty goes back to arrival order */ onSetSort: (sort: FlowSortKey[]) => void; onResetSort: () => void; @@ -67,6 +68,12 @@ export const FlowListHeader = memo(function FlowListHeader({ // Widths when a drag began, so each move sets the width from where it started. const startWidth = useRef(0); + // Taking out the sort's only key goes back to arrival order, as Reset Sort does + const removeColumn = (id: CustomColumnId) => { + if (sort.length > 0 && sort.every((k) => k.column === id)) onResetSort(); + setPrefs((p) => removeCustomColumn(p, id)); + }; + const pickerItems: DropdownItem[] = [ { type: "content", @@ -92,7 +99,7 @@ export const FlowListHeader = memo(function FlowListHeader({ label: "Remove Column", icon: "trash" as const, color: "danger" as const, - onSelect: () => setPrefs((p) => removeCustomColumn(p, c.id as `field:${string}`)), + onSelect: () => removeColumn(c.id as CustomColumnId), }, ], } @@ -106,22 +113,13 @@ export const FlowListHeader = memo(function FlowListHeader({ ]; const columnItems = (c: ColumnDef): DropdownItem[] => { - const column = c.sortable ? (c.id as SortColumn) : null; - const sortBy = (descending: boolean) => { - if (column != null) setSort([{ column, descending }]); - }; - const addTo = (descending: boolean) => { - if (column != null) setSort(addSortKey(sort, column, descending)); - }; + const sortBy = (descending: boolean) => setSort([{ column: c.id, descending }]); + const addTo = (descending: boolean) => setSort(addSortKey(sort, c.id, descending)); return [ - ...(column == null - ? [] - : ([ - { label: "Sort Ascending", onSelect: () => sortBy(false) }, - { label: "Sort Descending", onSelect: () => sortBy(true) }, - { label: "Add to Sort Ascending", onSelect: () => addTo(false) }, - { label: "Add to Sort Descending", onSelect: () => addTo(true) }, - ] satisfies DropdownItem[])), + { label: "Sort Ascending", onSelect: () => sortBy(false) }, + { label: "Sort Descending", onSelect: () => sortBy(true) }, + { label: "Add to Sort Ascending", onSelect: () => addTo(false) }, + { label: "Add to Sort Descending", onSelect: () => addTo(true) }, { label: "Clear Sorting", disabled: sort.length === 0, onSelect: onResetSort }, { type: "separator" }, { @@ -139,7 +137,7 @@ export const FlowListHeader = memo(function FlowListHeader({ { label: "Remove Column", color: "danger" as const, - onSelect: () => setPrefs((p) => removeCustomColumn(p, c.id as `field:${string}`)), + onSelect: () => removeColumn(c.id as CustomColumnId), }, ] : []), @@ -213,19 +211,15 @@ export const FlowListHeader = memo(function FlowListHeader({ data-column={c.id} className="relative h-full flex items-center min-w-0 px-1.5" > - {c.sortable ? ( - - ) : ( - {label} - )} + {/* On the column's right edge, the boundary with the next, resizing this one */} 0 ? manyFlows(many) : fixtures.map((f) => f.flow)) - .map((f, i) => ({ ...f, number: 1401 + i })) - .reverse(); - return { flows, total: flows.length, matching: null }; + const all = (many > 0 ? manyFlows(many) : fixtures.map((f) => f.flow)).map((f, i) => ({ + ...f, + number: 1401 + i, + })); + // In a sort's order, as the database lists it, all in one page + const sort = (req.sort ?? []) as FlowSortKey[]; + const flows = sort.length > 0 ? sortFlows(all, sort) : all.reverse(); + return { flows, total: flows.length, matching: null, cursor: null }; } case "list_seen": return { hosts: [], apps: [] }; @@ -179,6 +184,14 @@ function manyFlows(count: number): Flow[] { responseSize: pending ? 0 : base.responseSize, elapsedMs: pending ? null : base.elapsedMs, clientCwd: base.clientBundleId == null ? (folders[i % folders.length] ?? null) : null, + // A cache header with a few values and some without, for a column of it to sort + responseHeaders: + i % 5 === 4 + ? base.responseHeaders + : [ + ...base.responseHeaders, + { name: "X-Cache", value: ["HIT", "miss", "Hit", "EXPIRED"][i % 4] ?? "" }, + ], }; }); } diff --git a/apps/yaak-proxy/lib/columns.test.ts b/apps/yaak-proxy/lib/columns.test.ts index 5f4120a10..95b3dcce9 100644 --- a/apps/yaak-proxy/lib/columns.test.ts +++ b/apps/yaak-proxy/lib/columns.test.ts @@ -1,13 +1,17 @@ import type { Flow, FlowSortKey } from "@yaakapp-internal/proxy-lib"; import { describe, expect, test } from "vite-plus/test"; +import customSortFixture from "../../../crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json"; import { addCustomColumn, addSortKey, cellFilterTerm, type ColumnPrefs, customPath, + customPathProblem, customValue, defaultColumnPrefs, + fieldReference, + fieldReferenceChanges, gridTemplate, gridWidth, hideOtherColumns, @@ -154,6 +158,19 @@ describe("column preferences", () => { expect(prefs).toEqual(defaultColumnPrefs); }); + test("a custom column's sort persists with it and goes when it does", () => { + let prefs = addCustomColumn(defaultColumnPrefs, "header.x-cache"); + const sort: FlowSortKey[] = [ + { column: "field:header.x-cache", descending: true }, + { column: "status", descending: false }, + ]; + prefs = { ...prefs, sort }; + expect(parseColumnPrefs(JSON.parse(JSON.stringify(prefs))).sort).toEqual(sort); + // A key for a column that is no longer there is dropped + expect(parseColumnPrefs({ ...prefs, custom: [] }).sort).toEqual([sort[1]]); + expect(removeCustomColumn(prefs, "field:header.x-cache").sort).toEqual([sort[1]]); + }); + test("Operation shows itself once, unless the person set it", () => { const grpc = flow(1, { grpcService: "pkg.Greeter", grpcMethod: "SayHello" }); const anonymous = flow(2, { graphqlOperation: "" }); @@ -193,6 +210,33 @@ describe("sorting", () => { expect(addSortKey([], "time", true)).toEqual(one("time", true)); }); + test("a custom column sorts on click and Option-click as the others do", () => { + const cache = "field:header.x-cache"; + expect(nextSort([], cache)).toEqual(one(cache, false)); + expect(nextSort(one(cache, false), cache)).toEqual(one(cache, true)); + expect(toggleSortKey(one("status", true), cache)).toEqual([ + ...one("status", true), + ...one(cache, false), + ]); + }); + + // Flows and the order each sort lists them in, shared with + // `a_custom_column_sorts_by_its_value_and_pages` in the lib's `flows.rs`, which checks the + // database lists them in the same order. + test("a custom column sorts as the database does", () => { + const fixture = customSortFixture as { + flows: Partial[]; + sorts: { sort: FlowSortKey[]; order: number[] }[]; + }; + const flows = fixture.flows.map((f, i) => flow(i, f)); + for (const { sort, order } of fixture.sorts) { + expect( + sortFlows(flows, sort).map((f) => f.number), + JSON.stringify(sort), + ).toEqual(order); + } + }); + // The same flows and orders as `a_sort_on_several_columns_pages_in_its_order` in the lib's // `flows.rs`, so the app places a flow where the database would. const flows = [ @@ -261,6 +305,33 @@ describe("values", () => { expect(customPath("query.filter[status]")).toBeNull(); }); + test("says tersely why what was typed names no column", () => { + expect(customPathProblem("")).toBeNull(); + expect(customPathProblem("@")).toBeNull(); + expect(customPathProblem("@req.header.user-agent")).toBeNull(); + expect(customPathProblem("@hea")).toBe("Incomplete"); + expect(customPathProblem("@res.header")).toBe("Incomplete"); + expect(customPathProblem("@header.")).toBe("Missing name"); + expect(customPathProblem("@query.a b")).toBe("Invalid name"); + expect(customPathProblem("@host")).toBe("Unknown field"); + }); + + test("keeps the Add Column field a reference to a field", () => { + expect(fieldReference("")).toBe(""); + expect(fieldReference("@")).toBe("@"); + expect(fieldReference("h")).toBe("@h"); + expect(fieldReference("header.x-cache")).toBe("@header.x-cache"); + expect(fieldReference("@req.header.user-agent:")).toBe("@req.header.user-agent"); + expect(fieldReference(" @@Header.X-Cache: ")).toBe("@Header.X-Cache"); + expect(fieldReference(":")).toBe(""); + expect(fieldReference("@:")).toBe("@"); + expect(fieldReferenceChanges("@header.x")).toEqual([]); + expect(fieldReferenceChanges("query.page:")).toEqual([ + { from: 0, to: 0, insert: "@" }, + { from: 10, to: 11, insert: "" }, + ]); + }); + test("a custom column shows the first value, the response's header before the request's", () => { const f = flow(1, { url: "https://a.com/?Page=2&page=3", diff --git a/apps/yaak-proxy/lib/columns.ts b/apps/yaak-proxy/lib/columns.ts index b7f0e4492..f94655389 100644 --- a/apps/yaak-proxy/lib/columns.ts +++ b/apps/yaak-proxy/lib/columns.ts @@ -21,8 +21,6 @@ export interface ColumnDef { align: "left" | "right"; /** Shown until the person hides it */ defaultVisible: boolean; - /** Sorted by in the database; custom columns are not */ - sortable: boolean; /** The header or query parameter a custom column shows: `header.x-request-id` */ path?: string; } @@ -34,7 +32,7 @@ const builtin = ( minWidth: number, align: "left" | "right", defaultVisible: boolean, -): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible, sortable: true }); +): ColumnDef => ({ id, label, width, minWidth, align, defaultVisible }); export const COLUMNS: ColumnDef[] = [ builtin("number", "#", 46, 30, "left", true), @@ -72,6 +70,45 @@ export function customPath(typed: string): string | null { return map + key.toLowerCase(); } +/** Why what was typed names no custom column, in a few words; null when it does or is empty */ +export function customPathProblem(typed: string): string | null { + if (customPath(typed) != null) return null; + const path = typed.trim().replace(/^@/, "").replace(/:$/, "").trim().toLowerCase(); + if (path === "") return null; + const map = CUSTOM_MAPS.find((m) => path.startsWith(m)); + if (map != null) return path === map ? "Missing name" : "Invalid name"; + // On the way to a map: `@hea`, `@req.header` + if (CUSTOM_MAPS.some((m) => m.startsWith(path))) return "Incomplete"; + return "Unknown field"; +} + +/** + * The edits that turn what the Add Column field holds into a reference to a field, `@` and its + * path: an `@` in front when it lacks one, and no colon or space after, as a pasted + * `@header.x-cache:` or a typed `header.x-cache` has. A lone `@` stays, to complete from. + */ +export function fieldReferenceChanges( + text: string, +): { from: number; to: number; insert: string }[] { + const lead = /^[\s@]*/.exec(text)?.[0].length ?? 0; + const trail = /[\s:]*$/.exec(text.slice(lead))?.[0].length ?? 0; + const end = text.length - trail; + const at = end > lead || text.includes("@") ? "@" : ""; + const changes = []; + if (text.slice(0, lead) !== at) changes.push({ from: 0, to: lead, insert: at }); + if (trail > 0) changes.push({ from: end, to: text.length, insert: "" }); + return changes; +} + +/** What the Add Column field shows for `text`, `fieldReferenceChanges` made */ +export function fieldReference(text: string): string { + let result = text; + for (const { from, to, insert } of [...fieldReferenceChanges(text)].reverse()) { + result = result.slice(0, from) + insert + result.slice(to); + } + return result; +} + export function customColumnId(path: string): CustomColumnId { return `field:${path}`; } @@ -89,7 +126,6 @@ function customDef(id: CustomColumnId): ColumnDef { minWidth: 40, align: "left", defaultVisible: true, - sortable: false, path, }; } @@ -198,10 +234,13 @@ export function addCustomColumn(prefs: ColumnPrefs, path: string): ColumnPrefs { return { ...prefs, custom, visible: { ...prefs.visible, [id]: true } }; } +/** Takes the column out, and out of the sort */ export function removeCustomColumn(prefs: ColumnPrefs, id: CustomColumnId): ColumnPrefs { const { [id]: _v, ...visible } = prefs.visible; const { [id]: _w, ...widths } = prefs.widths; - return { ...prefs, custom: prefs.custom.filter((c) => c !== id), visible, widths }; + const custom = prefs.custom.filter((c) => c !== id); + const sort = prefs.sort.filter((k) => k.column !== id); + return { ...prefs, custom, visible, widths, sort }; } export function resizeColumn(prefs: ColumnPrefs, id: ColumnId, width: number): ColumnPrefs { @@ -254,9 +293,11 @@ export function parseColumnPrefs(value: unknown): ColumnPrefs { if (key == null || typeof key !== "object") continue; const { column, descending } = key as Record; if (typeof column !== "string" || typeof descending !== "boolean") continue; - const def = DEFS.get(column as ColumnId); - if (def == null || prefs.sort.some((k) => k.column === column)) continue; - prefs.sort.push({ column: def.id as SortColumn, descending }); + // A built-in column, or one the person added that is still there + const id = column as ColumnId; + const known = isCustomColumn(id) ? prefs.custom.includes(id) : DEFS.has(id); + if (!known || prefs.sort.some((k) => k.column === id)) continue; + prefs.sort.push({ column: id, descending }); } } return prefs; @@ -265,7 +306,7 @@ export function parseColumnPrefs(value: unknown): ColumnPrefs { // -- Sorting -- /** Numbers sort largest first on the first click, names from A */ -export function firstDirection(column: SortColumn): boolean { +export function firstDirection(column: ColumnId): boolean { switch (column) { case "number": case "status": @@ -283,7 +324,7 @@ export function firstDirection(column: SortColumn): boolean { * A click on a column's label: sort by it alone, reverse the sort it has alone, then, from the * reversed sort, go back to the order flows arrive in */ -export function nextSort(sort: FlowSortKey[], column: SortColumn): FlowSortKey[] { +export function nextSort(sort: FlowSortKey[], column: ColumnId): FlowSortKey[] { const first = firstDirection(column); const only = sort.length === 1 ? sort[0] : null; if (only?.column !== column) return [{ column, descending: first }]; @@ -295,7 +336,7 @@ export function nextSort(sort: FlowSortKey[], column: SortColumn): FlowSortKey[] * An Option-click on a column's label: adds it as the last key, ascending; again reverses it, and * a third time takes it out */ -export function toggleSortKey(sort: FlowSortKey[], column: SortColumn): FlowSortKey[] { +export function toggleSortKey(sort: FlowSortKey[], column: ColumnId): FlowSortKey[] { const key = sort.find((k) => k.column === column); if (key == null) return [...sort, { column, descending: false }]; if (!key.descending) @@ -306,7 +347,7 @@ export function toggleSortKey(sort: FlowSortKey[], column: SortColumn): FlowSort /** Adds `column` as a key in `descending` order: the last key, or in its place if it is one */ export function addSortKey( sort: FlowSortKey[], - column: SortColumn, + column: ColumnId, descending: boolean, ): FlowSortKey[] { if (sort.some((k) => k.column === column)) { @@ -328,10 +369,12 @@ function emptyNumber(n: number | null | undefined): number { } /** - * The value `column` sorts `flow` by, as `SortColumn::sql` in the lib's `flows.rs` makes it in - * the database, so a flow placed here lands where a page read from there would have it + * The value `column` sorts `flow` by, as `SortBy::sql` in the lib's `flows.rs` makes it in the + * database, so a flow placed here lands where a page read from there would have it. A custom + * column's is the value it shows, as text. */ -export function columnSortKey(flow: Flow, column: SortColumn): string | number { +export function columnSortKey(flow: Flow, column: ColumnId): string | number { + if (isCustomColumn(column)) return noCase(customValue(flow, column.slice("field:".length))); switch (column) { case "number": return flow.number; diff --git a/crates-proxy/yaak-proxy-lib/bindings/gen_rpc.ts b/crates-proxy/yaak-proxy-lib/bindings/gen_rpc.ts index de0b1e6b3..266575b86 100644 --- a/crates-proxy/yaak-proxy-lib/bindings/gen_rpc.ts +++ b/crates-proxy/yaak-proxy-lib/bindings/gen_rpc.ts @@ -195,7 +195,7 @@ cursor?: Array, }; /** * One key of a sort. */ -export type FlowSortKey = { column: SortColumn, descending: boolean, }; +export type FlowSortKey = { column: SortColumn | `field:${string}`, descending: boolean, }; export type FlowsClearedPayload = Record; diff --git a/crates-proxy/yaak-proxy-lib/src/flows.rs b/crates-proxy/yaak-proxy-lib/src/flows.rs index 2885b77e0..d19962eed 100644 --- a/crates-proxy/yaak-proxy-lib/src/flows.rs +++ b/crates-proxy/yaak-proxy-lib/src/flows.rs @@ -98,14 +98,135 @@ pub enum SortColumn { } /// One key of a sort. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TS)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] #[ts(export, export_to = "gen_rpc.ts")] pub struct FlowSortKey { - pub column: SortColumn, + #[ts(type = "SortColumn | `field:${string}`")] + pub column: SortBy, pub descending: bool, } +/// What a sort key sorts by: a column the list has built in, or a column the person added for +/// a header or query parameter, written `field:header.x-cache` as the app names the column. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum SortBy { + Column(SortColumn), + Field(FieldPath), +} + +impl From for SortBy { + fn from(column: SortColumn) -> Self { + Self::Column(column) + } +} + +const FIELD_PREFIX: &str = "field:"; + +impl Serialize for SortBy { + fn serialize(&self, serializer: S) -> Result { + match self { + Self::Column(column) => column.serialize(serializer), + Self::Field(field) => serializer.serialize_str(&format!("{FIELD_PREFIX}{field}")), + } + } +} + +impl<'de> Deserialize<'de> for SortBy { + fn deserialize>(deserializer: D) -> Result { + use serde::de::{Error, IntoDeserializer}; + let text = String::deserialize(deserializer)?; + if let Some(path) = text.strip_prefix(FIELD_PREFIX) { + return FieldPath::parse(path) + .map(Self::Field) + .ok_or_else(|| D::Error::custom(format!("Invalid sort field {path:?}"))); + } + let column: serde::de::value::StrDeserializer<'_, D::Error> = + text.as_str().into_deserializer(); + SortColumn::deserialize(column).map(Self::Column) + } +} + +/// The map a custom column's field is under, as the query language names it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum FieldMap { + /// `header.`: the response's header, or the request's when the response has none. + Header, + RequestHeader, + ResponseHeader, + Query, +} + +impl FieldMap { + // `req.header.` and `res.header.` before `header.`, which neither starts with, all the same + const ALL: [Self; 4] = [ + Self::RequestHeader, + Self::ResponseHeader, + Self::Header, + Self::Query, + ]; + + fn prefix(self) -> &'static str { + match self { + Self::Header => "header.", + Self::RequestHeader => "req.header.", + Self::ResponseHeader => "res.header.", + Self::Query => "query.", + } + } +} + +/// A custom column's field: a header or query parameter by name, `header.x-cache`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct FieldPath { + map: FieldMap, + /// Lowercased: names compare without regard to case + name: String, +} + +impl FieldPath { + /// The field `path` names, by the rule the app's `customPath` in `lib/columns.ts` keeps for + /// a custom column: a map, then a name of the characters the query language takes in a + /// field path, so the column's value makes a term a search can run. + pub fn parse(path: &str) -> Option { + let lower = path.to_ascii_lowercase(); + let map = FieldMap::ALL.into_iter().find(|m| lower.starts_with(m.prefix()))?; + let name = &lower[map.prefix().len()..]; + if name.contains('.') || !yaak_query::is_valid_path(name) { + return None; + } + Some(Self { map, name: name.to_string() }) + } + + /// The value the column shows, as text in SQL, NULL when the flow has none: the first value + /// under the name, as `customValue` in the app's `lib/columns.ts` reads it. Each `?` is + /// bound to the name, one parameter for each. + fn sql(&self) -> (String, Vec) { + let first = |json: &str| { + format!( + "(SELECT json_extract(kv.value, '$.value') FROM json_each({json}) AS kv \ + WHERE q_lower(json_extract(kv.value, '$.name')) = ? ORDER BY kv.key LIMIT 1)" + ) + }; + let (sql, sides) = match self.map { + FieldMap::Header => ( + format!("coalesce({}, {})", first("response_headers"), first("request_headers")), + 2, + ), + FieldMap::RequestHeader => (first("request_headers"), 1), + FieldMap::ResponseHeader => (first("response_headers"), 1), + FieldMap::Query => (first("url_query_json(url)"), 1), + }; + (sql, vec![Value::Text(self.name.clone()); sides]) + } +} + +impl std::fmt::Display for FieldPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}{}", self.map.prefix(), self.name) + } +} + /// What an empty number sorts as: below every value, as an empty text's `''` is. const EMPTY_NUMBER: &str = "-1000000000000000000"; @@ -156,26 +277,55 @@ impl SortColumn { } } -/// A sort's keys in SQL, each with whether it descends, then start and id, ascending. -fn sort_terms(sort: &[FlowSortKey], columns: &FlowColumns) -> Vec<(String, bool)> { +impl SortBy { + /// The value the key sorts by, in SQL, as [`SortColumn::sql`] makes it, and the parameters + /// its `?`s are bound to, in order. + fn sql(&self, columns: &FlowColumns) -> (String, Vec) { + match self { + Self::Column(column) => (column.sql(columns), Vec::new()), + // Text, as the other text columns sort + Self::Field(field) => { + let (value, params) = field.sql(); + (format!("(coalesce({value}, '') COLLATE NOCASE)"), params) + } + } + } +} + +/// One term of a sort in SQL: its value, the parameters the value's `?`s are bound to, and +/// whether it descends. +struct SortTerm { + sql: String, + params: Vec, + descending: bool, +} + +/// A sort's keys in SQL, then start and id, ascending. +fn sort_terms(sort: &[FlowSortKey], columns: &FlowColumns) -> Vec { + let tiebreak = |sql: &str| SortTerm { sql: sql.into(), params: Vec::new(), descending: false }; sort.iter() - .map(|k| (k.column.sql(columns), k.descending)) - .chain([ - ("started_at".to_string(), false), - ("flows.id".to_string(), false), - ]) + .map(|k| { + let (sql, params) = k.column.sql(columns); + SortTerm { sql, params, descending: k.descending } + }) + .chain([tiebreak("started_at"), tiebreak("flows.id")]) .collect() } -/// The `ORDER BY` for a sort, and the condition for the flows after `cursor`'s values in it: -/// each key equal up to one that is past the cursor's. -fn sort_after(terms: &[(String, bool)], cursor: &[Value]) -> (String, Vec) { +/// The condition for the flows after `cursor`'s values in a sort, and its parameters: each key +/// equal up to one that is past the cursor's. +fn sort_after(terms: &[SortTerm], cursor: &[Value]) -> (String, Vec) { let mut params = Vec::new(); let mut alternatives = Vec::new(); - for (i, (term, descending)) in terms.iter().enumerate() { - let mut parts: Vec = terms[..i].iter().map(|(t, _)| format!("{t} = ?")).collect(); - params.extend(cursor[..i].iter().cloned()); - parts.push(format!("{term} {} ?", if *descending { "<" } else { ">" })); + for (i, term) in terms.iter().enumerate() { + let mut parts = Vec::new(); + for (before, value) in terms[..i].iter().zip(cursor) { + parts.push(format!("{} = ?", before.sql)); + params.extend(before.params.iter().cloned()); + params.push(value.clone()); + } + parts.push(format!("{} {} ?", term.sql, if term.descending { "<" } else { ">" })); + params.extend(term.params.iter().cloned()); params.push(cursor[i].clone()); alternatives.push(format!("({})", parts.join(" AND "))); } @@ -377,15 +527,17 @@ fn list_and_cursor( conn: &rusqlite::Connection, filter: &FlowFilter, ) -> Result<(Vec, Option>), String> { - let (condition, mut params) = filter.condition_in(conn); let terms = if filter.sort.is_empty() { Vec::new() } else { sort_terms(&filter.sort, &FlowColumns::of(conn)) }; - // The rowid, for the flow's number, and a sort's values, for its cursor. + // The rowid, for the flow's number, and a sort's values, for its cursor and its order. let keys: String = - terms.iter().enumerate().map(|(i, (t, _))| format!(", {t} AS sort_{i}")).collect(); + terms.iter().enumerate().map(|(i, t)| format!(", {} AS sort_{i}", t.sql)).collect(); + let mut params: Vec = terms.iter().flat_map(|t| t.params.iter().cloned()).collect(); + let (condition, condition_params) = filter.condition_in(conn); + params.extend(condition_params); let mut sql = format!("SELECT rowid, *{keys} FROM flows WHERE {condition}"); push_clear_mark(conn, &mut sql, &mut params); if let Some(id) = &filter.after { @@ -407,9 +559,11 @@ fn list_and_cursor( sql.push_str(&after); params.extend(after_params); } + // By the values selected above, so a key with parameters is computed once a row let order: Vec = terms .iter() - .map(|(t, descending)| format!("{t} {}", if *descending { "DESC" } else { "ASC" })) + .enumerate() + .map(|(i, t)| format!("sort_{i} {}", if t.descending { "DESC" } else { "ASC" })) .collect(); sql.push_str(" ORDER BY "); sql.push_str(&order.join(", ")); @@ -1254,7 +1408,8 @@ mod tests { flow("https://A.com/", "GET", Some(404), None), flow("https://c.com/", "GET", Some(200), None), ]); - let key = |column, descending| FlowSortKey { column, descending }; + let key = + |column: SortColumn, descending| FlowSortKey { column: column.into(), descending }; let read = |sort: &[FlowSortKey], cursor: Option>, limit| { let filter = FlowFilter { sort: sort.to_vec(), @@ -1300,6 +1455,84 @@ mod tests { assert_eq!(all, ["000", "001", "002", "003", "004", "005", "006", "007"]); } + /// Flows and the order each sort lists them in, shared with the app's `lib/columns.test.ts`, + /// whose in-memory sort must agree with the database's. + #[derive(Deserialize)] + struct SortFixture { + flows: Vec, + sorts: Vec, + } + + #[derive(Deserialize)] + struct SortCase { + sort: Vec, + order: Vec, + } + + /// A custom column sorts by the value it shows: a header's first value, the response's over + /// the request's for `header.`, or a query parameter's, as text without regard to case and + /// empty lowest; alone, after a built-in key or before another, and paged from a cursor. + #[test] + fn a_custom_column_sorts_by_its_value_and_pages() { + let fixture: SortFixture = + serde_json::from_str(include_str!("../tests/fixtures/custom_column_sort.json")) + .unwrap(); + let (_dir, db) = db_of(fixture.flows); + let read = |sort: &[FlowSortKey], cursor: Option>, limit| { + let filter = FlowFilter { + sort: sort.to_vec(), + cursor, + limit: Some(limit), + ..Default::default() + }; + let page = sorted_page(&db, &filter).unwrap(); + let order: Vec = page.flows.iter().map(|f| f.id[3..].parse().unwrap()).collect(); + (order, page.cursor) + }; + for case in &fixture.sorts { + let (all, _) = read(&case.sort, None, 100); + assert_eq!(all, case.order, "{:?}", case.sort); + let mut paged = Vec::new(); + let mut cursor = None; + loop { + let (page, next) = read(&case.sort, cursor.clone(), 3); + if page.is_empty() { + break; + } + cursor = next; + paged.extend(page); + } + assert_eq!(paged, case.order, "paged {:?}", case.sort); + } + } + + /// A custom column's key names it as the app does, `field:` and its path, and takes only + /// the paths a custom column can have. + #[test] + fn a_sort_key_names_a_custom_column_by_its_path() { + let parse = |column: &str| { + serde_json::from_value::( + serde_json::json!({ "column": column, "descending": false }), + ) + .map(|k| k.column) + }; + assert_eq!(parse("status").unwrap(), SortBy::Column(SortColumn::Status)); + let field = parse("field:Res.Header.X-Cache").unwrap(); + assert_eq!(serde_json::to_value(&field).unwrap(), "field:res.header.x-cache"); + assert_eq!(serde_json::to_value(parse("tls").unwrap()).unwrap(), "tls"); + for path in [ + "field:host", + "field:header.", + "field:header.a b", + "field:header.a.b", + "field:query.x'); DROP TABLE flows; --", + "field:query.filter[status]", + "nope", + ] { + assert!(parse(path).is_err(), "{path}"); + } + } + #[test] fn a_search_reads_the_newest_page_through_the_index() { let (_dir, db) = db_of(vec![]); diff --git a/crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json b/crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json new file mode 100644 index 000000000..ec15c4c83 --- /dev/null +++ b/crates-proxy/yaak-proxy-lib/tests/fixtures/custom_column_sort.json @@ -0,0 +1,101 @@ +{ + "flows": [ + { + "method": "GET", + "url": "https://x.com/a?page=2&Page=9", + "requestHeaders": [{ "name": "x-cache", "value": "req0" }], + "responseHeaders": [{ "name": "X-Cache", "value": "HIT" }] + }, + { + "method": "POST", + "url": "https://x.com/b", + "responseHeaders": [{ "name": "x-cache", "value": "miss" }] + }, + { + "method": "GET", + "url": "https://x.com/c?page=10" + }, + { + "method": "POST", + "url": "https://x.com/d?q=a+b", + "requestHeaders": [{ "name": "X-CACHE", "value": "zeta" }] + }, + { + "method": "GET", + "url": "https://x.com/e?page=", + "responseHeaders": [ + { "name": "x-cache", "value": "hit" }, + { "name": "x-cache", "value": "AAA" } + ] + }, + { + "method": "GET", + "url": "https://x.com/f?PAGE=1", + "requestHeaders": [{ "name": "x-cache", "value": "zzz" }], + "responseHeaders": [{ "name": "x-cache", "value": "Hit" }] + }, + { + "method": "GET", + "url": "https://x.com/g?page=%41b", + "requestHeaders": [{ "name": "x-cache", "value": "early" }], + "responseHeaders": [{ "name": "x-cache", "value": "" }] + }, + { + "method": "GET", + "url": "https://x.com/h", + "requestHeaders": [{ "name": "x-cache", "value": "Beta" }] + } + ], + "sorts": [ + { + "sort": [{ "column": "field:res.header.x-cache", "descending": false }], + "order": [2, 3, 6, 7, 0, 4, 5, 1] + }, + { + "sort": [{ "column": "field:res.header.x-cache", "descending": true }], + "order": [1, 0, 4, 5, 2, 3, 6, 7] + }, + { + "sort": [{ "column": "field:req.header.x-cache", "descending": false }], + "order": [1, 2, 4, 7, 6, 0, 3, 5] + }, + { + "sort": [{ "column": "field:req.header.x-cache", "descending": true }], + "order": [5, 3, 0, 6, 7, 1, 2, 4] + }, + { + "sort": [{ "column": "field:header.x-cache", "descending": false }], + "order": [2, 6, 7, 0, 4, 5, 1, 3] + }, + { + "sort": [{ "column": "field:header.x-cache", "descending": true }], + "order": [3, 1, 0, 4, 5, 7, 2, 6] + }, + { + "sort": [{ "column": "field:query.page", "descending": false }], + "order": [1, 3, 4, 7, 5, 2, 0, 6] + }, + { + "sort": [{ "column": "field:query.page", "descending": true }], + "order": [6, 0, 2, 5, 1, 3, 4, 7] + }, + { + "sort": [{ "column": "field:query.q", "descending": true }], + "order": [3, 0, 1, 2, 4, 5, 6, 7] + }, + { + "sort": [ + { "column": "method", "descending": false }, + { "column": "field:header.x-cache", "descending": true } + ], + "order": [0, 4, 5, 7, 2, 6, 3, 1] + }, + { + "sort": [ + { "column": "field:header.x-cache", "descending": false }, + { "column": "field:query.page", "descending": true } + ], + "order": [6, 2, 7, 0, 5, 4, 1, 3] + } + ] +} diff --git a/packages/ui/src/components/editor/query/extension.test.ts b/packages/ui/src/components/editor/query/extension.test.ts index 16a4be1d1..86947fa7f 100644 --- a/packages/ui/src/components/editor/query/extension.test.ts +++ b/packages/ui/src/components/editor/query/extension.test.ts @@ -1,5 +1,33 @@ import { describe, expect, test } from "vite-plus/test"; -import { pathsAt } from "./extension"; +import type { Completion } from "@yaakapp-internal/query"; +import { fieldReferences, pathsAt } from "./extension"; + +describe("fieldReferences", () => { + const item = (insert: string, kind: Completion["kind"] = "field"): Completion => ({ + label: insert, + insert, + kind, + info: null, + example: null, + reopen: true, + }); + const refs = (items: Completion[]) => fieldReferences(items).map((i) => [i.insert, i.reopen]); + + test("ends a field or a map's key with no colon, and does not open again", () => { + expect(refs([item("@req.header.user-agent:"), item("@host:")])).toEqual([ + ["@req.header.user-agent", false], + ["@host", false], + ]); + }); + + test("opens again after a map, for its key", () => { + expect(refs([item("@header.")])).toEqual([["@header.", true]]); + }); + + test("never offers values", () => { + expect(refs([item("Mozilla/5.0", "value"), item("@query.")])).toEqual([["@query.", true]]); + }); +}); describe("pathsAt", () => { const at = (text: string) => pathsAt(text, text.length); diff --git a/packages/ui/src/components/editor/query/extension.ts b/packages/ui/src/components/editor/query/extension.ts index aed6e0f52..e273af6a9 100644 --- a/packages/ui/src/components/editor/query/extension.ts +++ b/packages/ui/src/components/editor/query/extension.ts @@ -7,7 +7,7 @@ import type { DecorationSet, ViewUpdate } from "@codemirror/view"; import { Decoration, EditorView, ViewPlugin } from "@codemirror/view"; import type { Tag } from "@lezer/highlight"; import { tags as t } from "@lezer/highlight"; -import type { Schema, TokenKind } from "@yaakapp-internal/query"; +import type { Completion as QueryCompletion, Schema, TokenKind } from "@yaakapp-internal/query"; import { queryCompletions, queryTokens } from "@yaakapp-internal/query"; /** A term the search cannot run, to underline: `from` and `to` count UTF-16 units */ @@ -36,6 +36,11 @@ export interface QueryFilterOptions { moreCompletions?: (typed: string) => Completion[]; /** The terms of a query the search cannot run, underlined as typing settles */ problems?: (text: string) => Promise; + /** + * Completes a reference to a field, `@header.x-request-id`, rather than a term: no colon + * after the field and no values, so choosing a field ends it, as a map's key does + */ + fieldsOnly?: boolean; } const tokenTags: Record = { @@ -116,7 +121,26 @@ function infoOf(info: string | null, example: string | null): Completion["info"] }; } -function completionSource({ schema, values, loadValues, moreCompletions }: QueryFilterOptions) { +/** + * The engine's completions as references to fields: fields alone, inserted without the colon a + * term's value follows, opening again only after a map, for its key + */ +export function fieldReferences(items: QueryCompletion[]): QueryCompletion[] { + return items + .filter((item) => item.kind === "field") + .map((item) => { + const insert = item.insert.replace(/:$/, ""); + return { ...item, insert, reopen: insert.endsWith(".") }; + }); +} + +function completionSource({ + schema, + values, + loadValues, + moreCompletions, + fieldsOnly, +}: QueryFilterOptions) { return async (ctx: CompletionContext): Promise => { const text = ctx.state.doc.toString(); let known = values ?? {}; @@ -126,8 +150,9 @@ function completionSource({ schema, values, loadValues, moreCompletions }: Query if (ctx.aborted) return null; } const result = queryCompletions(text, ctx.pos, schema, known); + const items = fieldsOnly ? fieldReferences(result?.items ?? []) : (result?.items ?? []); - const options: Completion[] = (result?.items ?? []).map((item) => ({ + const options: Completion[] = items.map((item) => ({ label: item.label, // Not a CodeMirror type: it names the kind for `onlyFields` below, and draws no icon type: item.kind === "field" ? "field" : "value",