@dudousxd/nestjs-catalog-react 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -13
- package/dist/ConnectionPanel.d.ts +11 -7
- package/dist/ConnectionPanel.js +65 -28
- package/dist/ObjectExplorer.js +424 -48
- package/dist/PipelineConsole.d.ts +23 -126
- package/dist/PipelineConsole.js +39 -551
- package/dist/QueryConsole.js +25 -17
- package/dist/SavedQueryPanel.js +1 -1
- package/dist/TransformEditor.js +12 -3
- package/dist/WorkflowCanvas.js +401 -29
- package/dist/bundler/index.d.ts +20 -0
- package/dist/bundler/index.js +20 -0
- package/dist/bundler/shiki-subset.d.ts +83 -0
- package/dist/bundler/shiki-subset.js +196 -0
- package/dist/context.d.ts +181 -38
- package/dist/context.js +44 -11
- package/dist/diff/RevisionDiff.d.ts +67 -4
- package/dist/diff/RevisionDiff.js +126 -91
- package/dist/index.d.ts +8 -5
- package/dist/index.js +56 -20
- package/dist/routes.d.ts +54 -13
- package/dist/routes.js +5 -4
- package/dist/schema-discovery.d.ts +216 -0
- package/dist/schema-discovery.js +225 -0
- package/dist/source-fields.d.ts +1 -12
- package/dist/source-fields.js +13 -16
- package/dist/ui/code-editor.d.ts +196 -8
- package/dist/ui/code-editor.js +184 -72
- package/dist/ui/code-languages.d.ts +80 -0
- package/dist/ui/code-languages.js +76 -0
- package/dist/ui/code-theme.d.ts +25 -0
- package/dist/ui/code-theme.js +26 -0
- package/dist/ui/dialog.d.ts +13 -1
- package/dist/ui/dialog.js +13 -2
- package/dist/workflow/graph.js +31 -9
- package/dist/workflow/index.d.ts +2 -0
- package/dist/workflow/index.js +7 -0
- package/dist/workflow/lifecycle.d.ts +164 -0
- package/dist/workflow/lifecycle.js +249 -0
- package/dist/workflow/model.d.ts +1 -1
- package/dist/workflow/nodes.js +10 -1
- package/dist/workflow/runs.d.ts +38 -0
- package/dist/workflow/runs.js +101 -0
- package/dist/workflow/shape.d.ts +169 -0
- package/dist/workflow/shape.js +249 -0
- package/dist/workflow/templates.d.ts +623 -0
- package/dist/workflow/templates.js +1092 -0
- package/dist/workflow/validate.d.ts +20 -1
- package/dist/workflow/validate.js +41 -3
- package/package.json +21 -17
- package/dist/diff/line-diff.d.ts +0 -124
- package/dist/diff/line-diff.js +0 -318
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type WorkflowEdge, type WorkflowIssueCode, type WorkflowNode } from '@dudousxd/nestjs-catalog/client';
|
|
2
|
+
import { type ShapeKnowledge } from './shape';
|
|
2
3
|
/**
|
|
3
4
|
* What is wrong with a graph, checked in the browser while it is being drawn.
|
|
4
5
|
*
|
|
@@ -22,6 +23,13 @@ import { type WorkflowEdge, type WorkflowIssueCode, type WorkflowNode } from '@d
|
|
|
22
23
|
* - **A sink with no object type.** Core's type requires `targetType` on a sink
|
|
23
24
|
* but its validator never checks that the string is non-empty, and the server
|
|
24
25
|
* refuses one that is. The check is additive, not contradictory.
|
|
26
|
+
* - **Whether a source's columns fit the type its sink writes.** Core validates
|
|
27
|
+
* topology, and topology cannot see a column: a sink wired to a source that
|
|
28
|
+
* supplies none of its fields is a perfectly well-formed graph. Answering it
|
|
29
|
+
* needs two things core has no access to — what discovery said the source
|
|
30
|
+
* reads, and what the catalog says the type is made of — so it lives in
|
|
31
|
+
* `shape.ts` and runs only when a caller supplies both. See that file's
|
|
32
|
+
* header for why the answer has three values and not two.
|
|
25
33
|
*
|
|
26
34
|
* Two sinks writing the same type is NOT in that list: core reports it itself,
|
|
27
35
|
* as `duplicate-sink-type`, and is passed straight through.
|
|
@@ -38,7 +46,7 @@ export type WorkflowProblemLevel = 'error' | 'warning';
|
|
|
38
46
|
* Core's are re-used verbatim rather than remapped so that a caller — or a test
|
|
39
47
|
* — matching on a code matches the same string the server would have sent back.
|
|
40
48
|
*/
|
|
41
|
-
export type WorkflowProblemCode = WorkflowIssueCode | 'missing-transform' | 'sink-has-no-type' | 'duplicate-sink-type';
|
|
49
|
+
export type WorkflowProblemCode = WorkflowIssueCode | 'missing-transform' | 'sink-has-no-type' | 'duplicate-sink-type' | 'shape-source-spelling' | 'shape-missing-column' | 'shape-unproven' | 'shape-not-checked';
|
|
42
50
|
export interface WorkflowProblem {
|
|
43
51
|
level: WorkflowProblemLevel;
|
|
44
52
|
code: WorkflowProblemCode;
|
|
@@ -59,6 +67,17 @@ export interface ValidateOptions {
|
|
|
59
67
|
* the list is passed in rather than assumed complete.
|
|
60
68
|
*/
|
|
61
69
|
transformIds: ReadonlySet<string>;
|
|
70
|
+
/**
|
|
71
|
+
* What each source reads and what each type is made of, when the caller can
|
|
72
|
+
* say. Absent means the column checks in `shape.ts` do not run at all.
|
|
73
|
+
*
|
|
74
|
+
* Optional rather than required, and absent rather than empty: a caller with
|
|
75
|
+
* no discovery to offer has not learned that every graph is fine, it has not
|
|
76
|
+
* asked. An empty implementation would report the same silence while looking
|
|
77
|
+
* like an answer, which is the distinction this whole pair of modules is
|
|
78
|
+
* about.
|
|
79
|
+
*/
|
|
80
|
+
shapes?: ShapeKnowledge;
|
|
62
81
|
}
|
|
63
82
|
/**
|
|
64
83
|
* The identity React Flow needs, derived rather than stored.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { validateWorkflow as validateCoreWorkflow, } from '@dudousxd/nestjs-catalog/client';
|
|
2
2
|
import { nodeName } from './model';
|
|
3
3
|
import { WORKFLOW_NAME } from './name';
|
|
4
|
+
import { checkShapes } from './shape';
|
|
4
5
|
/**
|
|
5
6
|
* Core issues this canvas deliberately does not show, and the only place the two
|
|
6
7
|
* validators disagree.
|
|
@@ -27,11 +28,25 @@ import { WORKFLOW_NAME } from './name';
|
|
|
27
28
|
* that hides one of the server's answers is a screen that will eventually
|
|
28
29
|
* promise a save the server refuses.
|
|
29
30
|
*/
|
|
30
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* The wires an issue is really about, since core names nodes and not edges.
|
|
33
|
+
*
|
|
34
|
+
* The two shape errors are here as well as core's three, because "this source
|
|
35
|
+
* does not supply that sink's columns" is a statement about the wire between
|
|
36
|
+
* them and the canvas should draw it that way. The two shape *warnings* are
|
|
37
|
+
* deliberately absent: a red wire is error styling, and the whole point of the
|
|
38
|
+
* third outcome is that it does not wear it.
|
|
39
|
+
*/
|
|
40
|
+
const EDGE_CODES = new Set([
|
|
41
|
+
'cycle',
|
|
42
|
+
'self-edge',
|
|
43
|
+
'duplicate-edge',
|
|
44
|
+
'shape-source-spelling',
|
|
45
|
+
'shape-missing-column',
|
|
46
|
+
]);
|
|
31
47
|
function edgesFor(code, nodeIds, edges) {
|
|
32
|
-
if (code
|
|
48
|
+
if (!EDGE_CODES.has(code))
|
|
33
49
|
return [];
|
|
34
|
-
}
|
|
35
50
|
const named = new Set(nodeIds);
|
|
36
51
|
return edges
|
|
37
52
|
.filter((edge) => named.has(edge.from) && named.has(edge.to))
|
|
@@ -105,8 +120,31 @@ export function validateWorkflow(draft, options) {
|
|
|
105
120
|
});
|
|
106
121
|
}
|
|
107
122
|
}
|
|
123
|
+
// Last, and only when a caller supplied the two halves it needs. The columns
|
|
124
|
+
// are a different question from the wiring — a graph can be structurally
|
|
125
|
+
// perfect and still write nulls into every row — so it is asked whatever the
|
|
126
|
+
// checks above said, for the same reason they are asked whatever core said.
|
|
127
|
+
problems.push(...shapeProblems(draft, options));
|
|
108
128
|
return problems;
|
|
109
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* The column checks, with their wires filled in the same way everything else's
|
|
132
|
+
* are.
|
|
133
|
+
*
|
|
134
|
+
* `shape.ts` returns no edge ids of its own on purpose: `edgesFor` above is the
|
|
135
|
+
* one derivation of a wire from the nodes an issue names, and a second copy
|
|
136
|
+
* inside the shape module is exactly the drift this file's header is about. Which
|
|
137
|
+
* codes get a wire is therefore decided in one place, and the warnings are not
|
|
138
|
+
* among them.
|
|
139
|
+
*/
|
|
140
|
+
function shapeProblems(draft, options) {
|
|
141
|
+
if (!options.shapes)
|
|
142
|
+
return [];
|
|
143
|
+
return checkShapes(draft.nodes, draft.edges, options.shapes).map((problem) => ({
|
|
144
|
+
...problem,
|
|
145
|
+
edgeIds: edgesFor(problem.code, problem.nodeIds, draft.edges),
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
110
148
|
/**
|
|
111
149
|
* Would adding `from -> to` close a loop?
|
|
112
150
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dudousxd/nestjs-catalog-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Ready-made React screens for @dudousxd/nestjs-catalog — a model manager and a generic object explorer you can drop into your own app shell.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Davide Carvalho",
|
|
@@ -35,6 +35,10 @@
|
|
|
35
35
|
"./workflow": {
|
|
36
36
|
"types": "./dist/workflow/index.d.ts",
|
|
37
37
|
"default": "./dist/workflow/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./bundler": {
|
|
40
|
+
"types": "./dist/bundler/index.d.ts",
|
|
41
|
+
"default": "./dist/bundler/index.js"
|
|
38
42
|
}
|
|
39
43
|
},
|
|
40
44
|
"files": [
|
|
@@ -43,10 +47,17 @@
|
|
|
43
47
|
],
|
|
44
48
|
"peerDependencies": {
|
|
45
49
|
"@base-ui/react": ">=1.6.0",
|
|
50
|
+
"@pierre/diffs": ">=1.3.3",
|
|
46
51
|
"@dnd-kit/core": ">=6",
|
|
47
52
|
"@dnd-kit/sortable": ">=10",
|
|
48
53
|
"@dnd-kit/utilities": ">=3",
|
|
54
|
+
"@dudousxd/nestjs-catalog": ">=0.1.0",
|
|
49
55
|
"@tanstack/react-query": ">=5",
|
|
56
|
+
"@tanstack/react-table": ">=9",
|
|
57
|
+
"@tiptap/core": ">=3",
|
|
58
|
+
"@tiptap/pm": ">=3",
|
|
59
|
+
"@tiptap/react": ">=3",
|
|
60
|
+
"@tiptap/starter-kit": ">=3",
|
|
50
61
|
"@visx/curve": "4.0.1-alpha.0",
|
|
51
62
|
"@visx/event": "4.0.1-alpha.0",
|
|
52
63
|
"@visx/grid": "4.0.1-alpha.0",
|
|
@@ -59,24 +70,23 @@
|
|
|
59
70
|
"d3-shape": ">=3",
|
|
60
71
|
"lucide-react": ">=0.400.0",
|
|
61
72
|
"motion": ">=11",
|
|
62
|
-
"prism-react-renderer": ">=2.4.0",
|
|
63
73
|
"react": ">=18",
|
|
64
74
|
"react-dom": ">=18",
|
|
65
75
|
"recharts": ">=2.12.0",
|
|
66
|
-
"
|
|
67
|
-
"@tanstack/react-table": ">=9",
|
|
68
|
-
"@tiptap/react": ">=3",
|
|
69
|
-
"@tiptap/starter-kit": ">=3",
|
|
70
|
-
"@tiptap/pm": ">=3",
|
|
71
|
-
"tiptap-markdown": ">=0.9",
|
|
72
|
-
"@tiptap/core": ">=3"
|
|
76
|
+
"tiptap-markdown": ">=0.9"
|
|
73
77
|
},
|
|
74
78
|
"devDependencies": {
|
|
75
79
|
"@base-ui/react": "1.6.0",
|
|
76
80
|
"@dnd-kit/core": "6.3.1",
|
|
77
81
|
"@dnd-kit/sortable": "10.0.0",
|
|
78
82
|
"@dnd-kit/utilities": "3.2.2",
|
|
83
|
+
"@pierre/diffs": "1.3.3",
|
|
79
84
|
"@tanstack/react-query": "5.99.2",
|
|
85
|
+
"@tanstack/react-table": "9.0.0",
|
|
86
|
+
"@tiptap/core": "3.29.2",
|
|
87
|
+
"@tiptap/pm": "3.29.2",
|
|
88
|
+
"@tiptap/react": "3.29.2",
|
|
89
|
+
"@tiptap/starter-kit": "3.29.2",
|
|
80
90
|
"@types/d3-array": "^3.2.2",
|
|
81
91
|
"@types/d3-shape": "^3.1.8",
|
|
82
92
|
"@types/react": "19.2.14",
|
|
@@ -94,19 +104,13 @@
|
|
|
94
104
|
"d3-shape": "^3.2.0",
|
|
95
105
|
"lucide-react": "0.552.0",
|
|
96
106
|
"motion": "^12.43.0",
|
|
97
|
-
"prism-react-renderer": "2.4.1",
|
|
98
107
|
"react": "19.2.0",
|
|
99
108
|
"react-dom": "19.2.0",
|
|
100
109
|
"recharts": "2.15.4",
|
|
101
110
|
"tailwind-merge": "3.4.1",
|
|
102
|
-
"typescript": "5.9.3",
|
|
103
|
-
"@tanstack/react-table": "9.0.0",
|
|
104
|
-
"@tiptap/react": "3.29.2",
|
|
105
|
-
"@tiptap/starter-kit": "3.29.2",
|
|
106
|
-
"@tiptap/pm": "3.29.2",
|
|
107
111
|
"tiptap-markdown": "0.9.0",
|
|
108
|
-
"
|
|
109
|
-
"@dudousxd/nestjs-catalog": "0.
|
|
112
|
+
"typescript": "5.9.3",
|
|
113
|
+
"@dudousxd/nestjs-catalog": "0.15.0"
|
|
110
114
|
},
|
|
111
115
|
"dependencies": {
|
|
112
116
|
"clsx": "2.1.1",
|
package/dist/diff/line-diff.d.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Two texts, lined up, computed in this file and nowhere else.
|
|
3
|
-
*
|
|
4
|
-
* WHY THIS IS HAND-ROLLED
|
|
5
|
-
* -----------------------
|
|
6
|
-
* The two strings this console ever diffs are a transform's code and a saved
|
|
7
|
-
* query's SQL. Those are the most sensitive strings in the product: a transform
|
|
8
|
-
* is executable code with the catalog's own database reachable from it, and a
|
|
9
|
-
* saved query is somebody's SQL against military logistics data — the column
|
|
10
|
-
* names alone describe a fleet. A diff library is a dependency that gets to see
|
|
11
|
-
* both, on every render, and "it only renders locally" is a property of the
|
|
12
|
-
* version you audited rather than of the package. There is no version of this
|
|
13
|
-
* feature worth a supply-chain question mark on that content, so there is no
|
|
14
|
-
* dependency here at all: `diffLines` is forty lines of dynamic programming with
|
|
15
|
-
* no imports, and the whole of what it can do is visible on this screen.
|
|
16
|
-
*
|
|
17
|
-
* It is also the cheaper answer. This package already declines dependencies it
|
|
18
|
-
* could plausibly justify — `charts/css.tsx` draws bars with divs, `ui/button.tsx`
|
|
19
|
-
* hand-rolls what `class-variance-authority` would compile to, and `export/pdf.ts`
|
|
20
|
-
* is a seam rather than a renderer — for the same reason each time: every
|
|
21
|
-
* consumer pays for it whether or not they render this screen.
|
|
22
|
-
*
|
|
23
|
-
* WHAT IT DOES NOT DO
|
|
24
|
-
* -------------------
|
|
25
|
-
* No word- or character-level diff inside a changed line, and no move detection.
|
|
26
|
-
* A changed line is reported as a removal and an addition, which is what a
|
|
27
|
-
* unified diff has always done and what the renderer colours. Both are real
|
|
28
|
-
* improvements somebody could make; neither is the question this screen exists
|
|
29
|
-
* to answer, which is "which lines are different between the version that ran
|
|
30
|
-
* and the version that is current".
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* The largest DP table this will build, in cells.
|
|
34
|
-
*
|
|
35
|
-
* The comparison is O(n·m) in the CHANGED region only — the common prefix and
|
|
36
|
-
* suffix are trimmed first, so the ordinary case (somebody edited four lines of
|
|
37
|
-
* a three-hundred-line transform) is a handful of cells regardless of file size.
|
|
38
|
-
* This bound exists for the case that survives the trim: two bodies that share
|
|
39
|
-
* almost nothing, which is what a rewrite looks like.
|
|
40
|
-
*
|
|
41
|
-
* 1.5M cells is a `Uint32Array` of 6MB and roughly a 1,200×1,200 changed region.
|
|
42
|
-
* Past that the table costs more memory than the browser tab should spend on a
|
|
43
|
-
* comparison nobody could read anyway, so {@link diffLines} stops computing and
|
|
44
|
-
* SAYS it stopped — see `alignment`. A silent fallback would be the same failure
|
|
45
|
-
* `capLines` names one package over: a truncation nobody is told about.
|
|
46
|
-
*/
|
|
47
|
-
export declare const DIFF_MAX_CELLS = 1500000;
|
|
48
|
-
/** What happened to one line between the two bodies. */
|
|
49
|
-
export type DiffOp = 'same' | 'added' | 'removed';
|
|
50
|
-
export interface DiffLine {
|
|
51
|
-
op: DiffOp;
|
|
52
|
-
/** 1-based line number in the earlier body. Null on an added line. */
|
|
53
|
-
before: number | null;
|
|
54
|
-
/** 1-based line number in the later body. Null on a removed line. */
|
|
55
|
-
after: number | null;
|
|
56
|
-
text: string;
|
|
57
|
-
}
|
|
58
|
-
export interface LineDiff {
|
|
59
|
-
lines: DiffLine[];
|
|
60
|
-
added: number;
|
|
61
|
-
removed: number;
|
|
62
|
-
/**
|
|
63
|
-
* The two bodies are byte-for-byte the same.
|
|
64
|
-
*
|
|
65
|
-
* A separate field rather than `added === 0 && removed === 0`, because the
|
|
66
|
-
* caller has to distinguish "these two versions are identical" from "there is
|
|
67
|
-
* no second version to compare against", and only one of those is a statement
|
|
68
|
-
* about the texts. See `RevisionDiff`, where conflating them is the exact
|
|
69
|
-
* thing the empty states are written to avoid.
|
|
70
|
-
*/
|
|
71
|
-
identical: boolean;
|
|
72
|
-
/**
|
|
73
|
-
* How the lines were matched up.
|
|
74
|
-
*
|
|
75
|
-
* `exact` means every line was lined up against its counterpart. `coarse`
|
|
76
|
-
* means the changed region was past {@link DIFF_MAX_CELLS} and the whole of it
|
|
77
|
-
* is reported as removed-then-added — true, but blunter than it could be, and
|
|
78
|
-
* the screen says so rather than letting somebody read "everything changed" as
|
|
79
|
-
* a finding about their code.
|
|
80
|
-
*/
|
|
81
|
-
alignment: 'exact' | 'coarse';
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Line up two bodies.
|
|
85
|
-
*
|
|
86
|
-
* `before` is the earlier version, `after` the later one. The order matters to
|
|
87
|
-
* the reading and not to the algorithm: swapping them turns every addition into
|
|
88
|
-
* a removal, which is why the caller that pins "the version that ran" on the
|
|
89
|
-
* left is the one that gets a screen answering "what changed since".
|
|
90
|
-
*/
|
|
91
|
-
export declare function diffLines(before: string, after: string): LineDiff;
|
|
92
|
-
/** Lines to keep either side of a change before folding the rest away. */
|
|
93
|
-
export declare const DIFF_CONTEXT_LINES = 3;
|
|
94
|
-
/**
|
|
95
|
-
* The smallest run worth putting a button on.
|
|
96
|
-
*
|
|
97
|
-
* Folding two lines behind a control that says "2 unchanged lines" costs a row
|
|
98
|
-
* to save two, and it costs a click. Below this the run is simply shown.
|
|
99
|
-
*/
|
|
100
|
-
export declare const DIFF_MIN_FOLD = 4;
|
|
101
|
-
export type DiffSection = {
|
|
102
|
-
kind: 'shown';
|
|
103
|
-
lines: DiffLine[];
|
|
104
|
-
} | {
|
|
105
|
-
kind: 'folded';
|
|
106
|
-
lines: DiffLine[];
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Collapse the long unchanged stretches, keeping context around every change.
|
|
110
|
-
*
|
|
111
|
-
* A transform can be hundreds of lines and the answer to "why did Tuesday's load
|
|
112
|
-
* differ from Monday's" is almost never more than a handful of them. Rendering
|
|
113
|
-
* all of it puts the four lines that matter somewhere in the middle of a wall
|
|
114
|
-
* and makes the reader scroll for them.
|
|
115
|
-
*
|
|
116
|
-
* Folded rather than dropped, and the sections carry their lines rather than a
|
|
117
|
-
* count, so the renderer can expand one in place. A diff that hides code with no
|
|
118
|
-
* way to see it is a diff you cannot trust, and this is code somebody is about
|
|
119
|
-
* to make a decision about.
|
|
120
|
-
*
|
|
121
|
-
* A run at the very start or end of the file gets no context on that side —
|
|
122
|
-
* there is no change up there to be in the context of.
|
|
123
|
-
*/
|
|
124
|
-
export declare function foldUnchanged(lines: DiffLine[], context?: number, minFold?: number): DiffSection[];
|
package/dist/diff/line-diff.js
DELETED
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Two texts, lined up, computed in this file and nowhere else.
|
|
3
|
-
*
|
|
4
|
-
* WHY THIS IS HAND-ROLLED
|
|
5
|
-
* -----------------------
|
|
6
|
-
* The two strings this console ever diffs are a transform's code and a saved
|
|
7
|
-
* query's SQL. Those are the most sensitive strings in the product: a transform
|
|
8
|
-
* is executable code with the catalog's own database reachable from it, and a
|
|
9
|
-
* saved query is somebody's SQL against military logistics data — the column
|
|
10
|
-
* names alone describe a fleet. A diff library is a dependency that gets to see
|
|
11
|
-
* both, on every render, and "it only renders locally" is a property of the
|
|
12
|
-
* version you audited rather than of the package. There is no version of this
|
|
13
|
-
* feature worth a supply-chain question mark on that content, so there is no
|
|
14
|
-
* dependency here at all: `diffLines` is forty lines of dynamic programming with
|
|
15
|
-
* no imports, and the whole of what it can do is visible on this screen.
|
|
16
|
-
*
|
|
17
|
-
* It is also the cheaper answer. This package already declines dependencies it
|
|
18
|
-
* could plausibly justify — `charts/css.tsx` draws bars with divs, `ui/button.tsx`
|
|
19
|
-
* hand-rolls what `class-variance-authority` would compile to, and `export/pdf.ts`
|
|
20
|
-
* is a seam rather than a renderer — for the same reason each time: every
|
|
21
|
-
* consumer pays for it whether or not they render this screen.
|
|
22
|
-
*
|
|
23
|
-
* WHAT IT DOES NOT DO
|
|
24
|
-
* -------------------
|
|
25
|
-
* No word- or character-level diff inside a changed line, and no move detection.
|
|
26
|
-
* A changed line is reported as a removal and an addition, which is what a
|
|
27
|
-
* unified diff has always done and what the renderer colours. Both are real
|
|
28
|
-
* improvements somebody could make; neither is the question this screen exists
|
|
29
|
-
* to answer, which is "which lines are different between the version that ran
|
|
30
|
-
* and the version that is current".
|
|
31
|
-
*/
|
|
32
|
-
/**
|
|
33
|
-
* The largest DP table this will build, in cells.
|
|
34
|
-
*
|
|
35
|
-
* The comparison is O(n·m) in the CHANGED region only — the common prefix and
|
|
36
|
-
* suffix are trimmed first, so the ordinary case (somebody edited four lines of
|
|
37
|
-
* a three-hundred-line transform) is a handful of cells regardless of file size.
|
|
38
|
-
* This bound exists for the case that survives the trim: two bodies that share
|
|
39
|
-
* almost nothing, which is what a rewrite looks like.
|
|
40
|
-
*
|
|
41
|
-
* 1.5M cells is a `Uint32Array` of 6MB and roughly a 1,200×1,200 changed region.
|
|
42
|
-
* Past that the table costs more memory than the browser tab should spend on a
|
|
43
|
-
* comparison nobody could read anyway, so {@link diffLines} stops computing and
|
|
44
|
-
* SAYS it stopped — see `alignment`. A silent fallback would be the same failure
|
|
45
|
-
* `capLines` names one package over: a truncation nobody is told about.
|
|
46
|
-
*/
|
|
47
|
-
export const DIFF_MAX_CELLS = 1_500_000;
|
|
48
|
-
/**
|
|
49
|
-
* Split on `\n` alone, and keep whatever that leaves.
|
|
50
|
-
*
|
|
51
|
-
* A lone `\r` therefore stays inside the line text, so a body that changed from
|
|
52
|
-
* CRLF to LF reports every line as changed. That is noisy and it is also true:
|
|
53
|
-
* the stored bodies differ on every line, a connector pinned to the old version
|
|
54
|
-
* would run different bytes, and a diff that normalised the endings away would
|
|
55
|
-
* be answering a question nobody asked ("would these be the same if…") in place
|
|
56
|
-
* of the one they did.
|
|
57
|
-
*
|
|
58
|
-
* An empty body is zero lines rather than one empty line, so writing the first
|
|
59
|
-
* version of a transform reads as N additions rather than N additions and one
|
|
60
|
-
* mysterious deletion. A body ending in a newline genuinely does have a trailing
|
|
61
|
-
* empty line and keeps it.
|
|
62
|
-
*/
|
|
63
|
-
function splitLines(text) {
|
|
64
|
-
return text.length === 0 ? [] : text.split('\n');
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Line up two bodies.
|
|
68
|
-
*
|
|
69
|
-
* `before` is the earlier version, `after` the later one. The order matters to
|
|
70
|
-
* the reading and not to the algorithm: swapping them turns every addition into
|
|
71
|
-
* a removal, which is why the caller that pins "the version that ran" on the
|
|
72
|
-
* left is the one that gets a screen answering "what changed since".
|
|
73
|
-
*/
|
|
74
|
-
export function diffLines(before, after) {
|
|
75
|
-
const beforeLines = splitLines(before);
|
|
76
|
-
const afterLines = splitLines(after);
|
|
77
|
-
if (before === after) {
|
|
78
|
-
return {
|
|
79
|
-
lines: beforeLines.map((text, index) => ({
|
|
80
|
-
op: 'same',
|
|
81
|
-
before: index + 1,
|
|
82
|
-
after: index + 1,
|
|
83
|
-
text,
|
|
84
|
-
})),
|
|
85
|
-
added: 0,
|
|
86
|
-
removed: 0,
|
|
87
|
-
identical: true,
|
|
88
|
-
alignment: 'exact',
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
// The trim, which is what makes this cheap on the case that actually happens.
|
|
92
|
-
// A four-line edit inside a three-hundred-line transform leaves a DP table of
|
|
93
|
-
// sixteen cells; without it, ninety thousand.
|
|
94
|
-
let head = 0;
|
|
95
|
-
while (head < beforeLines.length &&
|
|
96
|
-
head < afterLines.length &&
|
|
97
|
-
beforeLines[head] === afterLines[head]) {
|
|
98
|
-
head += 1;
|
|
99
|
-
}
|
|
100
|
-
let tail = 0;
|
|
101
|
-
while (tail < beforeLines.length - head &&
|
|
102
|
-
tail < afterLines.length - head &&
|
|
103
|
-
beforeLines[beforeLines.length - 1 - tail] === afterLines[afterLines.length - 1 - tail]) {
|
|
104
|
-
tail += 1;
|
|
105
|
-
}
|
|
106
|
-
const beforeMiddle = beforeLines.slice(head, beforeLines.length - tail);
|
|
107
|
-
const afterMiddle = afterLines.slice(head, afterLines.length - tail);
|
|
108
|
-
const middle = beforeMiddle.length * afterMiddle.length > DIFF_MAX_CELLS
|
|
109
|
-
? coarse(beforeMiddle, afterMiddle, head)
|
|
110
|
-
: align(beforeMiddle, afterMiddle, head);
|
|
111
|
-
const lines = [
|
|
112
|
-
...beforeLines.slice(0, head).map((text, index) => sameLine(text, index + 1, index + 1)),
|
|
113
|
-
...middle.lines,
|
|
114
|
-
...beforeLines
|
|
115
|
-
.slice(beforeLines.length - tail)
|
|
116
|
-
.map((text, index) => sameLine(text, beforeLines.length - tail + index + 1, afterLines.length - tail + index + 1)),
|
|
117
|
-
];
|
|
118
|
-
return {
|
|
119
|
-
lines,
|
|
120
|
-
added: middle.added,
|
|
121
|
-
removed: middle.removed,
|
|
122
|
-
identical: false,
|
|
123
|
-
alignment: middle.alignment,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
function sameLine(text, before, after) {
|
|
127
|
-
return { op: 'same', before, after, text };
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* The longest common subsequence of two line arrays, walked into a diff.
|
|
131
|
-
*
|
|
132
|
-
* The table is filled from the end so the walk can go forwards, which is what
|
|
133
|
-
* keeps the output in file order without a reversal at the end. `Uint32Array`
|
|
134
|
-
* rather than nested arrays because the table is the only thing here that has a
|
|
135
|
-
* size worth thinking about, and a flat typed array is one allocation.
|
|
136
|
-
*
|
|
137
|
-
* When neither direction is better the walk prefers the REMOVAL, which is what
|
|
138
|
-
* puts every deleted line of a hunk above every added one — the unified-diff
|
|
139
|
-
* order, and the order the renderer's red-then-green block depends on.
|
|
140
|
-
*/
|
|
141
|
-
function align(beforeMiddle, afterMiddle, offset) {
|
|
142
|
-
const m = beforeMiddle.length;
|
|
143
|
-
const n = afterMiddle.length;
|
|
144
|
-
const width = n + 1;
|
|
145
|
-
const table = lcsTable(beforeMiddle, afterMiddle);
|
|
146
|
-
const lines = [];
|
|
147
|
-
let added = 0;
|
|
148
|
-
let removed = 0;
|
|
149
|
-
let i = 0;
|
|
150
|
-
let j = 0;
|
|
151
|
-
while (i < m && j < n) {
|
|
152
|
-
if (beforeMiddle[i] === afterMiddle[j]) {
|
|
153
|
-
lines.push(sameLine(beforeMiddle[i] ?? '', offset + i + 1, offset + j + 1));
|
|
154
|
-
i += 1;
|
|
155
|
-
j += 1;
|
|
156
|
-
}
|
|
157
|
-
else if ((table[(i + 1) * width + j] ?? 0) >= (table[i * width + j + 1] ?? 0)) {
|
|
158
|
-
lines.push(removedLine(beforeMiddle[i] ?? '', offset + i + 1));
|
|
159
|
-
removed += 1;
|
|
160
|
-
i += 1;
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
lines.push(addedLine(afterMiddle[j] ?? '', offset + j + 1));
|
|
164
|
-
added += 1;
|
|
165
|
-
j += 1;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
// Whatever is left when one side runs out. Removals first, again, so a hunk at
|
|
169
|
-
// the very end of the file reads the same way as one in the middle.
|
|
170
|
-
const tail = drain(beforeMiddle.slice(i), afterMiddle.slice(j), offset + i, offset + j);
|
|
171
|
-
lines.push(...tail.lines);
|
|
172
|
-
return {
|
|
173
|
-
lines,
|
|
174
|
-
added: added + tail.added,
|
|
175
|
-
removed: removed + tail.removed,
|
|
176
|
-
alignment: 'exact',
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* The remainder of one side, once the walk has exhausted the other.
|
|
181
|
-
*
|
|
182
|
-
* At most one of these two arrays is non-empty — the walk only stops when an
|
|
183
|
-
* index reaches its end — so this is not a second comparison. It is the same
|
|
184
|
-
* shape as {@link coarse}, and calling that would have been tempting; it is
|
|
185
|
-
* separate because `coarse` means "we declined to line these up" and this means
|
|
186
|
-
* "there was nothing left to line them up against", and a screen that reads
|
|
187
|
-
* `alignment` must not be told the first when the second happened.
|
|
188
|
-
*/
|
|
189
|
-
function drain(beforeRest, afterRest, beforeOffset, afterOffset) {
|
|
190
|
-
return {
|
|
191
|
-
lines: [
|
|
192
|
-
...beforeRest.map((text, index) => removedLine(text, beforeOffset + index + 1)),
|
|
193
|
-
...afterRest.map((text, index) => addedLine(text, afterOffset + index + 1)),
|
|
194
|
-
],
|
|
195
|
-
added: afterRest.length,
|
|
196
|
-
removed: beforeRest.length,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* `table[i * width + j]` is the length of the longest common subsequence of
|
|
201
|
-
* `a.slice(i)` and `b.slice(j)`.
|
|
202
|
-
*
|
|
203
|
-
* Filled from the END, which is what lets {@link align} walk forwards and emit
|
|
204
|
-
* lines already in file order rather than reversing at the finish.
|
|
205
|
-
*/
|
|
206
|
-
function lcsTable(a, b) {
|
|
207
|
-
const width = b.length + 1;
|
|
208
|
-
const table = new Uint32Array((a.length + 1) * width);
|
|
209
|
-
for (let i = a.length - 1; i >= 0; i -= 1) {
|
|
210
|
-
for (let j = b.length - 1; j >= 0; j -= 1) {
|
|
211
|
-
table[i * width + j] =
|
|
212
|
-
a[i] === b[j]
|
|
213
|
-
? (table[(i + 1) * width + j + 1] ?? 0) + 1
|
|
214
|
-
: Math.max(table[(i + 1) * width + j] ?? 0, table[i * width + j + 1] ?? 0);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return table;
|
|
218
|
-
}
|
|
219
|
-
function removedLine(text, before) {
|
|
220
|
-
return { op: 'removed', before, after: null, text };
|
|
221
|
-
}
|
|
222
|
-
function addedLine(text, after) {
|
|
223
|
-
return { op: 'added', before: null, after, text };
|
|
224
|
-
}
|
|
225
|
-
/** The whole changed region as one removal and one addition. See {@link DIFF_MAX_CELLS}. */
|
|
226
|
-
function coarse(beforeMiddle, afterMiddle, offset) {
|
|
227
|
-
return {
|
|
228
|
-
lines: [
|
|
229
|
-
...beforeMiddle.map((text, index) => ({
|
|
230
|
-
op: 'removed',
|
|
231
|
-
before: offset + index + 1,
|
|
232
|
-
after: null,
|
|
233
|
-
text,
|
|
234
|
-
})),
|
|
235
|
-
...afterMiddle.map((text, index) => ({
|
|
236
|
-
op: 'added',
|
|
237
|
-
before: null,
|
|
238
|
-
after: offset + index + 1,
|
|
239
|
-
text,
|
|
240
|
-
})),
|
|
241
|
-
],
|
|
242
|
-
added: afterMiddle.length,
|
|
243
|
-
removed: beforeMiddle.length,
|
|
244
|
-
alignment: 'coarse',
|
|
245
|
-
};
|
|
246
|
-
}
|
|
247
|
-
/** Lines to keep either side of a change before folding the rest away. */
|
|
248
|
-
export const DIFF_CONTEXT_LINES = 3;
|
|
249
|
-
/**
|
|
250
|
-
* The smallest run worth putting a button on.
|
|
251
|
-
*
|
|
252
|
-
* Folding two lines behind a control that says "2 unchanged lines" costs a row
|
|
253
|
-
* to save two, and it costs a click. Below this the run is simply shown.
|
|
254
|
-
*/
|
|
255
|
-
export const DIFF_MIN_FOLD = 4;
|
|
256
|
-
/**
|
|
257
|
-
* Collapse the long unchanged stretches, keeping context around every change.
|
|
258
|
-
*
|
|
259
|
-
* A transform can be hundreds of lines and the answer to "why did Tuesday's load
|
|
260
|
-
* differ from Monday's" is almost never more than a handful of them. Rendering
|
|
261
|
-
* all of it puts the four lines that matter somewhere in the middle of a wall
|
|
262
|
-
* and makes the reader scroll for them.
|
|
263
|
-
*
|
|
264
|
-
* Folded rather than dropped, and the sections carry their lines rather than a
|
|
265
|
-
* count, so the renderer can expand one in place. A diff that hides code with no
|
|
266
|
-
* way to see it is a diff you cannot trust, and this is code somebody is about
|
|
267
|
-
* to make a decision about.
|
|
268
|
-
*
|
|
269
|
-
* A run at the very start or end of the file gets no context on that side —
|
|
270
|
-
* there is no change up there to be in the context of.
|
|
271
|
-
*/
|
|
272
|
-
export function foldUnchanged(lines, context = DIFF_CONTEXT_LINES, minFold = DIFF_MIN_FOLD) {
|
|
273
|
-
const sections = [];
|
|
274
|
-
let shown = [];
|
|
275
|
-
const flush = () => {
|
|
276
|
-
if (shown.length > 0) {
|
|
277
|
-
sections.push({ kind: 'shown', lines: shown });
|
|
278
|
-
shown = [];
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
let index = 0;
|
|
282
|
-
while (index < lines.length) {
|
|
283
|
-
const line = lines[index];
|
|
284
|
-
if (!line)
|
|
285
|
-
break;
|
|
286
|
-
if (line.op !== 'same') {
|
|
287
|
-
shown.push(line);
|
|
288
|
-
index += 1;
|
|
289
|
-
continue;
|
|
290
|
-
}
|
|
291
|
-
const end = runEnd(lines, index);
|
|
292
|
-
const run = lines.slice(index, end);
|
|
293
|
-
// No context on a side there is no change on: a run touching the start of
|
|
294
|
-
// the file has nothing above it to be the context of, and neither does one
|
|
295
|
-
// touching the end.
|
|
296
|
-
const leading = index === 0 ? 0 : context;
|
|
297
|
-
const trailing = end === lines.length ? 0 : context;
|
|
298
|
-
if (run.length > leading + trailing + minFold) {
|
|
299
|
-
shown.push(...run.slice(0, leading));
|
|
300
|
-
flush();
|
|
301
|
-
sections.push({ kind: 'folded', lines: run.slice(leading, run.length - trailing) });
|
|
302
|
-
shown.push(...run.slice(run.length - trailing));
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
shown.push(...run);
|
|
306
|
-
}
|
|
307
|
-
index = end;
|
|
308
|
-
}
|
|
309
|
-
flush();
|
|
310
|
-
return sections;
|
|
311
|
-
}
|
|
312
|
-
/** Where the run of unchanged lines starting at `start` stops. */
|
|
313
|
-
function runEnd(lines, start) {
|
|
314
|
-
let end = start;
|
|
315
|
-
while (end < lines.length && lines[end]?.op === 'same')
|
|
316
|
-
end += 1;
|
|
317
|
-
return end;
|
|
318
|
-
}
|