feat(elasticsearch): render REST JSON responses
This commit is contained in:
1 parent
512f46a835
commit
24c63b6012
15 files changed
+1045
-303
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { appendJsonPointer, createJsonTreeRoot, getJsonTreeChildren, getVisibleJsonTreeNodes, isJsonTreeContainer, isJsonTreeInitiallyExpanded } from "../jsonTree";
|
||||
import { parseJsonPreservingLargeNumbers } from "../safeJsonFormat";
|
||||
|
||||
describe("jsonTree", () => {
|
||||
it("uses RFC 6901 paths for object keys and array positions", () => {
|
||||
const root = createJsonTreeRoot({ "a/b~c": ["value"] });
|
||||
const objectChild = getJsonTreeChildren(root)[0];
|
||||
const arrayChild = getJsonTreeChildren(objectChild)[0];
|
||||
|
||||
expect(root.path).toBe("");
|
||||
expect(objectChild.path).toBe("/a~1b~0c");
|
||||
expect(arrayChild.path).toBe("/a~1b~0c/0");
|
||||
expect(appendJsonPointer("/parent", "a/b~c")).toBe("/parent/a~1b~0c");
|
||||
});
|
||||
|
||||
it("keeps lossless JSON numbers as scalar nodes", () => {
|
||||
const value = parseJsonPreservingLargeNumbers('{"id":518400931654815740}') as Record<string, unknown>;
|
||||
const child = getJsonTreeChildren(createJsonTreeRoot(value))[0];
|
||||
|
||||
expect(isJsonTreeContainer(child.value)).toBe(false);
|
||||
});
|
||||
|
||||
it("treats initial depth as expanded container levels from the root", () => {
|
||||
expect(isJsonTreeInitiallyExpanded(0, 2)).toBe(true);
|
||||
expect(isJsonTreeInitiallyExpanded(1, 2)).toBe(true);
|
||||
expect(isJsonTreeInitiallyExpanded(2, 2)).toBe(false);
|
||||
expect(isJsonTreeInitiallyExpanded(99, Number.POSITIVE_INFINITY)).toBe(true);
|
||||
});
|
||||
|
||||
it("flattens expanded branches iteratively for virtual rendering", () => {
|
||||
const root = createJsonTreeRoot({ first: { nested: true }, second: ["kept"] });
|
||||
const nodes = getVisibleJsonTreeNodes(root, (node) => node.path !== "/first");
|
||||
|
||||
expect(nodes.map((node) => node.path)).toEqual(["", "/first", "/second", "/second/0"]);
|
||||
});
|
||||
|
||||
it("handles deeply nested expanded JSON without recursive traversal", () => {
|
||||
let value: unknown = true;
|
||||
for (let depth = 0; depth < 2_000; depth += 1) value = { child: value };
|
||||
|
||||
const nodes = getVisibleJsonTreeNodes(createJsonTreeRoot(value), () => true);
|
||||
|
||||
expect(nodes).toHaveLength(2_001);
|
||||
});
|
||||
});
|
||||
@@ -73,4 +73,16 @@ describe("safeJsonFormat", () => {
|
||||
expect(isLosslessJsonNumber(parsed.companyId) ? parsed.companyId.raw : null).toBe("518400931654815740");
|
||||
expect(parsed.safe).toBe(42);
|
||||
});
|
||||
|
||||
it("preserves fractional and exponent literals that Number would round or overflow", () => {
|
||||
const input = '{"fraction":0.123456789012345678901234,"scientific":1.234567890123456789e20,"overflow":1e999}';
|
||||
const parsed = parseJsonPreservingLargeNumbers(input) as Record<string, unknown>;
|
||||
|
||||
expect(isLosslessJsonNumber(parsed.fraction) ? parsed.fraction.raw : null).toBe("0.123456789012345678901234");
|
||||
expect(isLosslessJsonNumber(parsed.scientific) ? parsed.scientific.raw : null).toBe("1.234567890123456789e20");
|
||||
expect(isLosslessJsonNumber(parsed.overflow) ? parsed.overflow.raw : null).toBe("1e999");
|
||||
expect(safeJsonFormat(input, 2)).toContain("0.123456789012345678901234");
|
||||
expect(safeJsonFormat(input, 2)).toContain("1.234567890123456789e20");
|
||||
expect(safeJsonFormat(input, 2)).toContain("1e999");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,111 @@
|
||||
import { isLosslessJsonNumber } from "./safeJsonFormat";
|
||||
|
||||
export type JsonTreeContainerKind = "array" | "object";
|
||||
export type JsonTreeParentKind = JsonTreeContainerKind | "root";
|
||||
|
||||
export interface JsonTreeNode {
|
||||
key: string;
|
||||
label: string;
|
||||
value: unknown;
|
||||
/** RFC 6901 JSON Pointer. The root value is represented by an empty string. */
|
||||
path: string;
|
||||
/** Zero-based structural depth; the root value is at depth zero. */
|
||||
depth: number;
|
||||
parentKind: JsonTreeParentKind;
|
||||
}
|
||||
|
||||
export type JsonTreeContainer = Record<string, unknown> | unknown[];
|
||||
|
||||
export function createJsonTreeRoot(value: unknown): JsonTreeNode {
|
||||
return {
|
||||
key: "$",
|
||||
label: "$",
|
||||
value,
|
||||
path: "",
|
||||
depth: 0,
|
||||
parentKind: "root",
|
||||
};
|
||||
}
|
||||
|
||||
export function isJsonTreeContainer(value: unknown): value is JsonTreeContainer {
|
||||
// LosslessJsonNumber is an object wrapper, but represents a scalar JSON number.
|
||||
return value !== null && typeof value === "object" && !isLosslessJsonNumber(value);
|
||||
}
|
||||
|
||||
export function jsonTreeContainerKind(value: JsonTreeContainer): JsonTreeContainerKind {
|
||||
return Array.isArray(value) ? "array" : "object";
|
||||
}
|
||||
|
||||
export function jsonTreeContainerSummary(value: JsonTreeContainer, includeObjectLength = true): string {
|
||||
if (Array.isArray(value)) return `Array(${value.length})`;
|
||||
return includeObjectLength ? `Object(${Object.keys(value).length})` : "Object";
|
||||
}
|
||||
|
||||
/** Escape a reference token according to RFC 6901. */
|
||||
export function escapeJsonPointerSegment(segment: string): string {
|
||||
return segment.replaceAll("~", "~0").replaceAll("/", "~1");
|
||||
}
|
||||
|
||||
export function appendJsonPointer(path: string, segment: string | number): string {
|
||||
return `${path}/${escapeJsonPointerSegment(String(segment))}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create child nodes only for an already-expanded parent. Callers should not
|
||||
* invoke this while a container is collapsed so large JSON payloads stay lazy.
|
||||
*/
|
||||
export function getJsonTreeChildren(node: JsonTreeNode): JsonTreeNode[] {
|
||||
if (!isJsonTreeContainer(node.value)) return [];
|
||||
|
||||
if (Array.isArray(node.value)) {
|
||||
return node.value.map((value, index) => ({
|
||||
key: String(index),
|
||||
label: String(index),
|
||||
value,
|
||||
path: appendJsonPointer(node.path, index),
|
||||
depth: node.depth + 1,
|
||||
parentKind: "array",
|
||||
}));
|
||||
}
|
||||
|
||||
return Object.entries(node.value).map(([key, value]) => ({
|
||||
key,
|
||||
label: key,
|
||||
value,
|
||||
path: appendJsonPointer(node.path, key),
|
||||
depth: node.depth + 1,
|
||||
parentKind: "object",
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten the currently expanded tree with an iterative traversal. This lets
|
||||
* virtual renderers keep every node logically expanded without recursive DOM
|
||||
* creation or deep-call-stack failures.
|
||||
*/
|
||||
export function getVisibleJsonTreeNodes(root: JsonTreeNode, isExpanded: (node: JsonTreeNode) => boolean): JsonTreeNode[] {
|
||||
const nodes: JsonTreeNode[] = [];
|
||||
const pending = [root];
|
||||
|
||||
while (pending.length > 0) {
|
||||
const node = pending.pop();
|
||||
if (!node) continue;
|
||||
nodes.push(node);
|
||||
|
||||
if (!isJsonTreeContainer(node.value) || !isExpanded(node)) continue;
|
||||
const children = getJsonTreeChildren(node);
|
||||
for (let index = children.length - 1; index >= 0; index -= 1) pending.push(children[index]);
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* `initialExpandedDepth` counts container levels from the root. For example,
|
||||
* a value of 2 expands the root and its direct container children.
|
||||
*/
|
||||
export function isJsonTreeInitiallyExpanded(depth: number, initialExpandedDepth: number): boolean {
|
||||
if (initialExpandedDepth === Number.POSITIVE_INFINITY) return true;
|
||||
if (!Number.isFinite(initialExpandedDepth)) return false;
|
||||
return depth < Math.max(0, Math.floor(initialExpandedDepth));
|
||||
}
|
||||
@@ -18,7 +18,7 @@ export function isLosslessJsonNumber(value: unknown): value is LosslessJsonNumbe
|
||||
|
||||
/**
|
||||
* Parses JSON while retaining numeric literals that JavaScript cannot safely
|
||||
* represent as numbers. Callers can render these values without adding quotes.
|
||||
* represent exactly. Callers can render these values without adding quotes.
|
||||
*/
|
||||
export function parseJsonPreservingLargeNumbers(text: string): unknown {
|
||||
const protectedJson = protectLargeJsonNumbers(text);
|
||||
@@ -28,7 +28,8 @@ export function parseJsonPreservingLargeNumbers(text: string): unknown {
|
||||
|
||||
/**
|
||||
* Parse and re-stringify JSON while preserving numeric literals whose integer
|
||||
* parts exceed Number.MAX_SAFE_INTEGER (2^53 - 1).
|
||||
* parts exceed Number.MAX_SAFE_INTEGER (2^53 - 1), plus decimal and exponent
|
||||
* forms that JavaScript may round or turn into Infinity.
|
||||
*/
|
||||
export function safeJsonFormat(text: string, indent?: number): string {
|
||||
const protectedJson = protectLargeJsonNumbers(text);
|
||||
@@ -62,7 +63,7 @@ function protectLargeJsonNumbers(text: string): ProtectedJsonNumbers {
|
||||
const numberMatch = text.slice(index).match(/^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/);
|
||||
if (numberMatch) {
|
||||
const raw = numberMatch[0];
|
||||
if (hasUnsafeIntegerPart(raw)) {
|
||||
if (shouldPreserveJsonNumber(raw)) {
|
||||
// A quoted placeholder lets the native parser validate the rest of the JSON.
|
||||
const placeholder = `${placeholderPrefix}${numbers.size}__`;
|
||||
numbers.set(placeholder, raw);
|
||||
@@ -96,7 +97,12 @@ function findJsonStringEnd(text: string, start: number): number {
|
||||
return text.length;
|
||||
}
|
||||
|
||||
function hasUnsafeIntegerPart(raw: string): boolean {
|
||||
function shouldPreserveJsonNumber(raw: string): boolean {
|
||||
// Keep fractional/exponent forms verbatim. Even when a particular value is
|
||||
// representable today, parsing it through Number can change its precision or
|
||||
// spelling before the JSON viewer renders it.
|
||||
if (raw.includes(".") || raw.includes("e") || raw.includes("E") || raw === "-0") return true;
|
||||
|
||||
const unsigned = raw.startsWith("-") ? raw.slice(1) : raw;
|
||||
const integerPart = unsigned.split(/[.eE]/, 1)[0];
|
||||
const normalized = integerPart.replace(/^0+(?=\d)/, "");
|
||||
|
||||
+7
-7
@@ -1,8 +1,8 @@
|
||||
import type { AppThemeAppearance } from "@/lib/app/appTheme";
|
||||
|
||||
export type RedisJsonHighlighter = (content: string, appearance?: AppThemeAppearance) => string;
|
||||
export type JsonHighlighter = (content: string, appearance?: AppThemeAppearance) => string;
|
||||
|
||||
interface RedisShikiJsonHighlighterOptions {
|
||||
interface ShikiJsonHighlighterOptions {
|
||||
appearance: () => AppThemeAppearance;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ type ShikiHighlighter = Awaited<ReturnType<typeof import("shiki/core").createHig
|
||||
|
||||
let highlighterPromise: Promise<ShikiHighlighter> | undefined;
|
||||
|
||||
export async function createRedisShikiJsonHighlighter(options: RedisShikiJsonHighlighterOptions): Promise<RedisJsonHighlighter> {
|
||||
const highlighter = await getRedisShikiHighlighter();
|
||||
export async function createShikiJsonHighlighter(options: ShikiJsonHighlighterOptions): Promise<JsonHighlighter> {
|
||||
const highlighter = await getShikiJsonHighlighter();
|
||||
return (content, appearance = options.appearance()) =>
|
||||
highlighter.codeToHtml(content, {
|
||||
lang: "json",
|
||||
@@ -25,12 +25,12 @@ export async function createRedisShikiJsonHighlighter(options: RedisShikiJsonHig
|
||||
});
|
||||
}
|
||||
|
||||
function getRedisShikiHighlighter(): Promise<ShikiHighlighter> {
|
||||
highlighterPromise ??= loadRedisShikiHighlighter();
|
||||
function getShikiJsonHighlighter(): Promise<ShikiHighlighter> {
|
||||
highlighterPromise ??= loadShikiJsonHighlighter();
|
||||
return highlighterPromise;
|
||||
}
|
||||
|
||||
async function loadRedisShikiHighlighter(): Promise<ShikiHighlighter> {
|
||||
async function loadShikiJsonHighlighter(): Promise<ShikiHighlighter> {
|
||||
const [{ createHighlighterCore }, { createJavaScriptRegexEngine }, githubDark, githubLight, json] = await Promise.all([import("shiki/core"), import("shiki/engine/javascript"), import("shiki/themes/github-dark.mjs"), import("shiki/themes/github-light.mjs"), import("shiki/langs/json.mjs")]);
|
||||
|
||||
return createHighlighterCore({
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { DatabaseType, QueryResult } from "@/types/database";
|
||||
|
||||
export interface ElasticsearchJsonResponse {
|
||||
status: number;
|
||||
body: string;
|
||||
}
|
||||
|
||||
const ELASTICSEARCH_REST_STATEMENT = /^(?:GET|POST|PUT|DELETE)\s+\S+/i;
|
||||
|
||||
/**
|
||||
* Detect the result shape emitted for a JSON response to an explicit
|
||||
* Elasticsearch REST request. SQL and text (such as CAT) results keep using
|
||||
* the normal data-grid path.
|
||||
*/
|
||||
export function elasticsearchJsonResponseForResult(databaseType: DatabaseType | undefined, sourceStatement: string | undefined, result: QueryResult | undefined): ElasticsearchJsonResponse | undefined {
|
||||
if (databaseType !== "elasticsearch" || !result || typeof sourceStatement !== "string") return undefined;
|
||||
if (!ELASTICSEARCH_REST_STATEMENT.test(sourceStatement.trim())) return undefined;
|
||||
if (result.columns.length !== 2 || result.columns[0] !== "status" || result.columns[1] !== "response" || result.rows.length !== 1) return undefined;
|
||||
|
||||
const row = result.rows[0];
|
||||
if (!row || row.length !== 2) return undefined;
|
||||
|
||||
const [status, body] = row;
|
||||
if (typeof status !== "number" || !Number.isInteger(status) || status < 100 || status > 599 || typeof body !== "string") return undefined;
|
||||
return { status, body };
|
||||
}
|
||||
Reference in new issue
Block a user