@ember-lattice/protocol 0.0.0-stage → 1.0.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.
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Hard limits on a request. They stop hostile input before it costs memory, stack or SQL.
3
+ * The JSON Schema reads the same values. Servers must also cap the request body size.
4
+ */
5
+ export declare const LIMITS: {
6
+ /** Nesting of `and`, `or` and `not`. A leaf at the root has depth 1. */
7
+ readonly maxFilterDepth: 16;
8
+ /** All nodes in one filter tree. */
9
+ readonly maxFilterNodes: 256;
10
+ /** All `in`, `notIn` and range values in one filter tree. Each one is a SQL bind parameter. */
11
+ readonly maxFilterValues: 1000;
12
+ /** Items in one list: `in` values, `and`/`or` nodes, `columns`, aggregates. */
13
+ readonly maxList: 1000;
14
+ readonly maxSort: 16;
15
+ /** `group.by` and `pivot.by`. */
16
+ readonly maxGroupBy: 16;
17
+ /** Keys in `tree.keys`: the depth of the parent row. */
18
+ readonly maxTreeDepth: 16;
19
+ /** Rows in one page: `end - start`, or `limit`. */
20
+ readonly maxPage: 10000;
21
+ readonly maxString: 1024;
22
+ readonly maxSearch: 256;
23
+ readonly maxCursor: 4096;
24
+ /**
25
+ * Distinct `pivot.by` value tuples in the scope of a request. The values come from the data, so
26
+ * `evaluate` checks this at run time. A server counts them before it builds the pivot SQL.
27
+ */
28
+ readonly maxPivotValues: 200;
29
+ /** `parseRowsRequest` stops after this many errors. */
30
+ readonly maxErrors: 50;
31
+ };
@@ -0,0 +1,24 @@
1
+ import { type Aggregate, type AggregateFn, type AllowList, type ColumnType, type FilterOp, type ParseResult, type RowsRequest } from './types';
2
+ /** Names that must never become an object key that the client controls. */
3
+ export declare const RESERVED_NAMES: ReadonlySet<string>;
4
+ /**
5
+ * Date-only or date-time, ISO 8601. A date-time without a zone is UTC. The written year is 0001
6
+ * to 9999. The groups are year, month, day, hour, minute, second, fraction, offset hour and
7
+ * offset minute.
8
+ */
9
+ export declare const ISO_DATE: RegExp;
10
+ /** The default output key of an aggregate: `as`, else `fn(column)`. */
11
+ export declare function aggregateKey(a: Aggregate): string;
12
+ /** True when the op makes sense for the column type. */
13
+ export declare function opFitsType(op: FilterOp, type: ColumnType): boolean;
14
+ export declare function aggregateFitsType(fn: AggregateFn, type: ColumnType): boolean;
15
+ /**
16
+ * True when the string has the `ISO_DATE` form and each field exists on the calendar. Hour 24 is
17
+ * permitted only as `24:00`, `24:00:00` or with a zero fraction (the ISO 8601 end of the day).
18
+ */
19
+ export declare function isDateString(value: unknown): value is string;
20
+ /**
21
+ * Validate a request against the server allow-list. This is the trust boundary of every server.
22
+ * It never throws. On success it returns a fresh copy that holds only known keys.
23
+ */
24
+ export declare function parseRowsRequest(input: unknown, allowList: AllowList): ParseResult<RowsRequest>;
@@ -0,0 +1,91 @@
1
+ export declare const rowsRequestSchema: {
2
+ $defs: {
3
+ columnName: {
4
+ type: string;
5
+ minLength: number;
6
+ maxLength: 1024;
7
+ pattern: string;
8
+ };
9
+ scalar: {
10
+ anyOf: ({
11
+ type: string;
12
+ maxLength: number;
13
+ pattern: string;
14
+ } | {
15
+ type: string;
16
+ })[];
17
+ };
18
+ groupKey: {
19
+ anyOf: {
20
+ type: string;
21
+ }[];
22
+ };
23
+ aggregate: {
24
+ type: string;
25
+ additionalProperties: boolean;
26
+ required: string[];
27
+ properties: Record<string, unknown>;
28
+ };
29
+ filterNode: {
30
+ oneOf: {
31
+ type: string;
32
+ additionalProperties: boolean;
33
+ required: string[];
34
+ properties: Record<string, unknown>;
35
+ }[];
36
+ };
37
+ };
38
+ type: string;
39
+ additionalProperties: boolean;
40
+ required: string[];
41
+ properties: Record<string, unknown>;
42
+ $schema: string;
43
+ $id: string;
44
+ title: string;
45
+ };
46
+ export declare const rowsResponseSchema: {
47
+ type: string;
48
+ additionalProperties: boolean;
49
+ required: string[];
50
+ properties: Record<string, unknown>;
51
+ $schema: string;
52
+ $id: string;
53
+ title: string;
54
+ };
55
+ export declare const transactionSchema: {
56
+ type: string;
57
+ additionalProperties: boolean;
58
+ required: string[];
59
+ properties: Record<string, unknown>;
60
+ $schema: string;
61
+ $id: string;
62
+ title: string;
63
+ };
64
+ export declare const rowOpSchema: {
65
+ $schema: string;
66
+ $id: string;
67
+ title: string;
68
+ oneOf: {
69
+ type: string;
70
+ additionalProperties: boolean;
71
+ required: string[];
72
+ properties: Record<string, unknown>;
73
+ }[];
74
+ };
75
+ export declare const mutateResultSchema: {
76
+ $defs: {
77
+ transaction: {
78
+ type: string;
79
+ additionalProperties: boolean;
80
+ required: string[];
81
+ properties: Record<string, unknown>;
82
+ };
83
+ };
84
+ type: string;
85
+ additionalProperties: boolean;
86
+ required: string[];
87
+ properties: Record<string, unknown>;
88
+ $schema: string;
89
+ $id: string;
90
+ title: string;
91
+ };
@@ -0,0 +1,207 @@
1
+ /** The Lattice protocol, version 1 (plan 5.3, ADR 0005). Plain data: every message is JSON. */
2
+ export declare const PROTOCOL_VERSION = 1;
3
+ /** A cell value that a filter, a group key or a pivot key can hold. */
4
+ export type Scalar = string | number | boolean;
5
+ /** A group key or a pivot key. `null` is the group of empty values. */
6
+ export type GroupKey = Scalar | null;
7
+ export type ColumnType = 'string' | 'number' | 'boolean' | 'date';
8
+ /** The closed list of leaf ops. */
9
+ export declare const FILTER_OPS: readonly ["eq", "ne", "lt", "lte", "gt", "gte", "in", "notIn", "contains", "startsWith", "endsWith", "blank", "notBlank", "between", "inRange"];
10
+ export type FilterOp = (typeof FILTER_OPS)[number];
11
+ export declare const AGGREGATE_FNS: readonly ["sum", "avg", "min", "max", "count", "first", "last"];
12
+ export type AggregateFn = (typeof AGGREGATE_FNS)[number];
13
+ /** Ops with one scalar value. */
14
+ export type ScalarOp = 'eq' | 'ne' | 'lt' | 'lte' | 'gt' | 'gte' | 'contains' | 'startsWith' | 'endsWith';
15
+ export type ListOp = 'in' | 'notIn';
16
+ export type RangeOp = 'between' | 'inRange';
17
+ export type BlankOp = 'blank' | 'notBlank';
18
+ export type FilterLeaf = {
19
+ column: string;
20
+ op: ScalarOp;
21
+ value: Scalar;
22
+ } | {
23
+ column: string;
24
+ op: ListOp;
25
+ value: Scalar[];
26
+ } | {
27
+ column: string;
28
+ op: RangeOp;
29
+ value: [Scalar, Scalar];
30
+ } | {
31
+ column: string;
32
+ op: BlankOp;
33
+ };
34
+ /** One typed tree: `and`, `or`, `not` and leaf nodes. */
35
+ export type FilterNode = {
36
+ op: 'and';
37
+ nodes: FilterNode[];
38
+ } | {
39
+ op: 'or';
40
+ nodes: FilterNode[];
41
+ } | {
42
+ op: 'not';
43
+ node: FilterNode;
44
+ } | FilterLeaf;
45
+ export interface OffsetRange {
46
+ /** First row, inclusive. */
47
+ start: number;
48
+ /** Last row, exclusive. */
49
+ end: number;
50
+ }
51
+ /** Keyset paging. Cursors are opaque: only the server that made a cursor reads it. */
52
+ export interface CursorRange {
53
+ after?: string;
54
+ before?: string;
55
+ limit: number;
56
+ }
57
+ export interface SortItem {
58
+ column: string;
59
+ dir: 'asc' | 'desc';
60
+ /** Default `last`, for both directions. */
61
+ nulls?: 'first' | 'last';
62
+ }
63
+ export interface Aggregate {
64
+ column: string;
65
+ fn: AggregateFn;
66
+ /** The key of the value in the row. Default `fn(column)`, for example `sum(salary)`. */
67
+ as?: string;
68
+ }
69
+ export interface RowsRequest {
70
+ v: 1;
71
+ range: OffsetRange | CursorRange;
72
+ sort: SortItem[];
73
+ filter: FilterNode | null;
74
+ /** Quick search: a case-insensitive substring match on the columns with `search: true`. */
75
+ search?: string;
76
+ /** `keys.length` is the level. Group rows when `keys.length < by.length`, else leaf rows. */
77
+ group: {
78
+ by: string[];
79
+ keys: GroupKey[];
80
+ };
81
+ aggregate: Aggregate[];
82
+ pivot?: {
83
+ by: string[];
84
+ values: Aggregate[];
85
+ };
86
+ /** Projection. The key column is always in the row. */
87
+ columns?: string[];
88
+ /** Ask for `count`. The server also sends `count` when the page is the last page. */
89
+ count?: boolean;
90
+ /**
91
+ * Tree data (ADR 0022): the rows of one tree level. `keys` is the path of row keys from a root
92
+ * to the parent row. Empty `keys` gives the roots. Each row then has `$count`: the number of its
93
+ * child rows that match the filter and the search.
94
+ */
95
+ tree?: {
96
+ keys: Scalar[];
97
+ };
98
+ }
99
+ /** One pivot result column. `field` is its key in each row. */
100
+ export interface PivotColumn {
101
+ field: string;
102
+ /** The pivot key values, one for each `pivot.by` column. */
103
+ keys: GroupKey[];
104
+ /** The output key of the value aggregate (`Aggregate.as` or `fn(column)`). */
105
+ value: string;
106
+ }
107
+ export type Row = Record<string, unknown>;
108
+ /** The key of the child row count in a group row. Column names cannot start with `$`. */
109
+ export declare const GROUP_COUNT_KEY = "$count";
110
+ export interface RowsResponse<R = Row> {
111
+ rows: R[];
112
+ /** The number of rows (or groups) in the scope of the request, before paging. */
113
+ count?: number;
114
+ cursor?: {
115
+ next?: string;
116
+ prev?: string;
117
+ };
118
+ pivotColumns?: PivotColumn[];
119
+ /** The aggregates over all the rows in the scope of the request (the grand total at the root). */
120
+ total?: Record<string, unknown>;
121
+ }
122
+ /** One write from the client (`DataSource.mutate`). */
123
+ export type RowOp<R = Row> = {
124
+ type: 'add';
125
+ row: R;
126
+ route?: GroupKey[];
127
+ index?: number;
128
+ } | {
129
+ type: 'update';
130
+ id: string;
131
+ changes: Partial<R>;
132
+ } | {
133
+ type: 'remove';
134
+ id: string;
135
+ };
136
+ /** Live push from the server (`DataSource.subscribe`). The shape follows AG Grid's server-side transaction. */
137
+ export interface Transaction<R = Row> {
138
+ v: 1;
139
+ /** The group path the change applies to. Empty or absent is the root. */
140
+ route?: GroupKey[];
141
+ add?: R[];
142
+ addIndex?: number;
143
+ update?: R[];
144
+ /** Row ids. */
145
+ remove?: string[];
146
+ }
147
+ export type RowOpResult<R = Row> = {
148
+ status: 'ok';
149
+ id: string;
150
+ row?: R;
151
+ } | {
152
+ status: 'error';
153
+ id?: string;
154
+ message: string;
155
+ };
156
+ export interface MutateResult<R = Row> {
157
+ /** One result for each op, in the same order. */
158
+ results: RowOpResult<R>[];
159
+ /** The change as the server applied it, so other clients see the same transaction. */
160
+ transaction?: Transaction<R>;
161
+ }
162
+ /**
163
+ * One entry of the server allow-list. The allow-list is the trust boundary: a request can touch
164
+ * only the columns, ops and aggregate functions that it names.
165
+ */
166
+ export interface ColumnSchema {
167
+ /** `[A-Za-z_][A-Za-z0-9_]*`. Never `__proto__`, `constructor` or `prototype`. */
168
+ name: string;
169
+ type: ColumnType;
170
+ /** The unique row id. Exactly one column must have it for cursor paging. It is the last sort key. */
171
+ key?: boolean;
172
+ filter?: readonly FilterOp[];
173
+ sort?: boolean;
174
+ /** Group by this column, and pivot by it. */
175
+ group?: boolean;
176
+ aggregate?: readonly AggregateFn[];
177
+ /** Quick search looks in this column. String columns only. */
178
+ search?: boolean;
179
+ /** Tree data: this column holds the key of the parent row, or null for a root. Same type as the key. */
180
+ parent?: boolean;
181
+ /**
182
+ * Tree data: this string column holds the keys of the ancestors from the root, joined with
183
+ * `TREE_PATH_SEPARATOR`. A root has the empty string.
184
+ */
185
+ path?: boolean;
186
+ }
187
+ /** The separator of the keys in a `path` column. A string key cannot contain it. */
188
+ export declare const TREE_PATH_SEPARATOR = "/";
189
+ export type AllowList = readonly ColumnSchema[];
190
+ export interface ProtocolIssue {
191
+ /** A JSON path such as `filter.nodes[2].column`. */
192
+ path: string;
193
+ code: IssueCode;
194
+ message: string;
195
+ }
196
+ export type IssueCode = 'invalid' | 'unknown_key' | 'unknown_column' | 'op_not_allowed' | 'aggregate_not_allowed' | 'not_sortable' | 'not_groupable' | 'bad_range' | 'bad_cursor' | 'too_many_keys' | 'too_deep' | 'too_long'
197
+ /** A string holds U+0000. Postgres cannot store it. */
198
+ | 'bad_string'
199
+ /** The scope has more distinct pivot values than `LIMITS.maxPivotValues`. */
200
+ | 'pivot_too_wide';
201
+ export type ParseResult<T> = {
202
+ ok: true;
203
+ value: T;
204
+ } | {
205
+ ok: false;
206
+ errors: ProtocolIssue[];
207
+ };
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@ember-lattice/protocol",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "The Lattice wire protocol: row requests and responses, the filter tree, JSON Schema and the AG Grid translator. No framework.",
5
+ "keywords": [
6
+ "data-grid",
7
+ "server-side-row-model",
8
+ "protocol"
9
+ ],
10
+ "license": "MIT",
11
+ "author": "Joe Gaudet <joe@joegaudet.com>",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/joegaudet/ember-lattice.git",
15
+ "directory": "packages/protocol"
16
+ },
17
+ "homepage": "https://joegaudet.com/ember-lattice/",
18
+ "bugs": "https://github.com/joegaudet/ember-lattice/issues",
19
+ "type": "module",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./conformance/cases.v1.json": "./conformance/cases.v1.json",
27
+ "./package.json": "./package.json"
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "conformance",
32
+ "CHANGELOG.md",
33
+ "README.md",
34
+ "LICENSE.md",
35
+ "NOTICE"
36
+ ],
37
+ "sideEffects": false,
38
+ "devDependencies": {
39
+ "tsup": "^8.5.1"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "engines": {
45
+ "node": ">=22.12.0"
46
+ },
47
+ "scripts": {
48
+ "build": "tsc --build --force && tsup",
49
+ "conformance:export": "cd ../.. && UPDATE_CONFORMANCE=1 vitest run packages/protocol/tests/conformance.test.ts && prettier --write packages/protocol/conformance"
50
+ }
6
51
  }