@relate/contrib-observable 0.0.0-stage → 0.1.2
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/LICENSE +201 -0
- package/README.md +117 -2
- package/dist/attachments.d.ts +9 -0
- package/dist/attachments.js +21 -0
- package/dist/attachments.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/loader/fail.d.ts +2 -0
- package/dist/loader/fail.js +7 -0
- package/dist/loader/fail.js.map +1 -0
- package/dist/loader/graph.d.ts +25 -0
- package/dist/loader/graph.js +49 -0
- package/dist/loader/graph.js.map +1 -0
- package/dist/loader/index.d.ts +10 -0
- package/dist/loader/index.js +41 -0
- package/dist/loader/index.js.map +1 -0
- package/dist/loader/resolve.d.ts +2 -0
- package/dist/loader/resolve.js +8 -0
- package/dist/loader/resolve.js.map +1 -0
- package/dist/open.d.ts +40 -0
- package/dist/open.js +81 -0
- package/dist/open.js.map +1 -0
- package/dist/results/cypher.d.ts +13 -0
- package/dist/results/cypher.js +26 -0
- package/dist/results/cypher.js.map +1 -0
- package/dist/results/graph.d.ts +8 -0
- package/dist/results/graph.js +29 -0
- package/dist/results/graph.js.map +1 -0
- package/dist/results/index.d.ts +5 -0
- package/dist/results/index.js +4 -0
- package/dist/results/index.js.map +1 -0
- package/dist/results/normalize.d.ts +8 -0
- package/dist/results/normalize.js +42 -0
- package/dist/results/normalize.js.map +1 -0
- package/dist/results/types.d.ts +57 -0
- package/dist/results/types.js +3 -0
- package/dist/results/types.js.map +1 -0
- package/package.json +69 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 The Relate Authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @relate/contrib-observable
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Implements [ADR-006](../../design/adr/ADR-006-observable-adapter.md). Needs Observable Framework 1.x, `@relate/contrib-web` and `@relate/relate-wasm` (all peer dependencies). Static builds and the preview server are supported.
|
|
4
|
+
|
|
5
|
+
Framework has no plugin interface, so the package is three entries you call from your own files:
|
|
6
|
+
|
|
7
|
+
- `@relate/contrib-observable/loader`: helpers for **data loaders** (scripts in `docs/data/` that Framework runs and publishes under hashed names). They place the engine's binary and the core's worker and publish a graph's document and descriptor.
|
|
8
|
+
- `@relate/contrib-observable`: `open`, which creates the core's client for a graph and ties it to a cell.
|
|
9
|
+
- `@relate/contrib-observable/results`: the normalization, the graph shape for a force layout, and the `cypher` tagged template (re-exported by the main entry).
|
|
10
|
+
|
|
11
|
+
## Use, in five steps
|
|
12
|
+
|
|
13
|
+
**1. Install** `@relate/contrib-observable`, `@relate/contrib-web` and `@relate/relate-wasm` beside Framework. The package depends only on `esbuild`.
|
|
14
|
+
|
|
15
|
+
**2. Add two loaders that place the engine and the worker** (once per site):
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
// docs/data/relate.wasm.js
|
|
19
|
+
import { engine } from "@relate/contrib-observable/loader";
|
|
20
|
+
await engine();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
// docs/data/relate-worker.js.js
|
|
25
|
+
import { worker } from "@relate/contrib-observable/loader";
|
|
26
|
+
await worker();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`worker()` bundles the core's worker and the engine's script glue when the site builds, from the engine the site has installed, so the glue and the binary always match. Framework does not place either file by itself: a worker started with `new Worker(new URL(...))` is not published, and the engine's glue finds no binary.
|
|
30
|
+
|
|
31
|
+
**3. Declare each graph** that is shared or large as a source module and two loaders (a graph a cell holds as an object needs no loaders):
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
// docs/lib/pantry.js: any module whose default export returns an ADR-002 document, or a promise of one
|
|
35
|
+
export default () => ({ version: 1, nodes: [/* ... */], links: [/* ... */] });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
// docs/data/pantry.graph.json.js
|
|
40
|
+
import { document } from "@relate/contrib-observable/loader";
|
|
41
|
+
import source from "../lib/pantry.js";
|
|
42
|
+
await document("pantry", source);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
// docs/data/pantry.descriptor.json.js
|
|
47
|
+
import { descriptor } from "@relate/contrib-observable/loader";
|
|
48
|
+
import source from "../lib/pantry.js";
|
|
49
|
+
await descriptor("pantry", source, { indexes: [{ label: "Page", property: "id" }] });
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
An invalid document, a malformed id or a source that throws stops the build and names the graph and the first problem. The descriptor carries a content revision, so a page restores a stored copy of the graph in the visitor's browser while the revision is unchanged.
|
|
53
|
+
|
|
54
|
+
**4. Clear Framework's loader cache before every build.** Framework runs a loader again only when its output is older than the loader script itself, and `observable build` reuses a cached output even then. An edit to a graph source, a content change a source reads, or an engine upgrade reaches the published files only after the cache folder is removed. Framework 1.13 has no `clean` command; its templates clear the folder with a script:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{ "scripts": { "clean": "rm -rf docs/.observablehq/cache dist", "build": "npm run clean && observable build", "dev": "npm run clean && observable preview" } }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
In the preview server, after it has started, a source edit takes effect only when a loader script is saved or the server is restarted after a clean.
|
|
61
|
+
|
|
62
|
+
**5. Open the graph in a cell** and query it:
|
|
63
|
+
|
|
64
|
+
````md
|
|
65
|
+
```js
|
|
66
|
+
import { open } from "@relate/contrib-observable";
|
|
67
|
+
const db = await open({
|
|
68
|
+
engine: FileAttachment("data/relate.wasm"),
|
|
69
|
+
worker: FileAttachment("data/relate-worker.js"),
|
|
70
|
+
graph: { descriptor: FileAttachment("data/pantry.descriptor.json"), data: FileAttachment("data/pantry.graph.json") },
|
|
71
|
+
// or: graph: { document: FileAttachment("data/pantry.graph.json"), indexes }
|
|
72
|
+
// or: graph: { document: anObjectTheCellHolds, indexes }
|
|
73
|
+
invalidation,
|
|
74
|
+
});
|
|
75
|
+
await db.ready;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
const rows = await db.cypher`MATCH (p:Page) WHERE p.words > ${minWords} RETURN p.title AS title ORDER BY title`;
|
|
80
|
+
display(Inputs.table(rows));
|
|
81
|
+
```
|
|
82
|
+
````
|
|
83
|
+
|
|
84
|
+
`open` takes file attachments, not URLs, because Framework finds a page's files by reading literal `FileAttachment("...")` calls. Its promise resolves as soon as the client exists, not when the graph is ready; `db.ready` resolves when it is, and rejects with the core's wording if the client reaches `failed`. `${}` values are bound parameters, never spliced into the statement. The rows are an array with `columns`, `columnTypes`, `stats` and `elapsedMs`; entities in a row are flattened (`d => d.p.age`) and read as text in a table; `db.graph(rows.result)` gives `{ nodes, links }` for a force layout.
|
|
85
|
+
|
|
86
|
+
`db.client` is the core's client: `status`, `subscribe`, `run` (with a stop control) and the rest of its interface. A statement a visitor types should go through `db.client.run`, which has a timeout and can be stopped.
|
|
87
|
+
|
|
88
|
+
## Lifetime
|
|
89
|
+
|
|
90
|
+
The client is destroyed when the cell's `invalidation` resolves and when the page hides, and a page restored from the browser's back-forward cache reloads once. Open a graph in a cell that reads no input: an input a cell reads re-runs it, which destroys the client and opens a new worker each time. A page may open any number of graphs; each is independent, with its own worker. Framework pages are separate documents, so only the core's per-page profile applies; there is no `navigation` option.
|
|
91
|
+
|
|
92
|
+
## Cell rules (Framework's, not this package's)
|
|
93
|
+
|
|
94
|
+
- A cell cannot define a variable named `document` (or another global): `SyntaxError: Global 'document' cannot be redefined`.
|
|
95
|
+
- A top-level `await` inside the body of a loop is not detected; the cell compiles as non-async and its file attachments are not registered. Write `await db.ready` instead of a polling loop.
|
|
96
|
+
- A mistake in a cell shows in the page where its output would be, not in the build. Only a loader stops a build.
|
|
97
|
+
- Framework publishes only the files its cells name in literal `FileAttachment(...)` calls, here or in a module they import (paths in a module are relative to it).
|
|
98
|
+
|
|
99
|
+
## Workspace sites
|
|
100
|
+
|
|
101
|
+
A Framework site that installs these packages from a workspace must not link them as plain directories outside the site: the bundler fails with `Could not resolve entry module`. A pnpm injected dependency (`"dependenciesMeta": { "@relate/contrib-web": { "injected": true } }`), a copy, or a published package builds. See `conformance-site/` for the injected form and `scripts/refresh-deps.mjs`, which refreshes the copies, since pnpm does not add new built files to one after install.
|
|
102
|
+
|
|
103
|
+
## Not measured
|
|
104
|
+
|
|
105
|
+
The cold-network cost of the engine (about 6 MB) and the worker bundle (about 45 kB); the cost of clearing the cache on every build; the cost of each extra instance on a page.
|
|
106
|
+
|
|
107
|
+
## Develop
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
pnpm run build:wasm
|
|
111
|
+
pnpm install
|
|
112
|
+
pnpm --filter @relate/contrib-observable build
|
|
113
|
+
pnpm test:observable # results against the real engine, loaders, build, freshness, preview server
|
|
114
|
+
pnpm test:observable-browsers # the adapter's behavior in Chromium, Firefox and WebKit
|
|
115
|
+
pnpm test:observable-conformance # the core's conformance scenarios over conformance-site/
|
|
116
|
+
pnpm test:observable-package # packs adapter, core and engine; installs in a clean Framework project
|
|
117
|
+
pnpm test:observable-trace # every requirement has a passing, tagged test
|
|
118
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface Attachment {
|
|
2
|
+
url(): Promise<string>;
|
|
3
|
+
json(): Promise<unknown>;
|
|
4
|
+
}
|
|
5
|
+
export declare const isAttachment: (v: unknown) => v is Attachment;
|
|
6
|
+
/** The published location of an attachment, asked for once. */
|
|
7
|
+
export declare function locationOf(attachment: Attachment): Promise<string>;
|
|
8
|
+
/** The parsed JSON content of an attachment, read once. */
|
|
9
|
+
export declare function contentOf(attachment: Attachment): Promise<unknown>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// A file attachment is what a cell gets from `FileAttachment("data/x")`. The package takes attachments, not locations, because
|
|
2
|
+
// Framework finds a page's files by reading literal `FileAttachment(...)` calls in cells (ADR-006 §2.3).
|
|
3
|
+
/** Framework returns the same object for one file on one page, so identity keys what is resolved: once per page (research D-3). */
|
|
4
|
+
const locations = new WeakMap();
|
|
5
|
+
const contents = new WeakMap();
|
|
6
|
+
export const isAttachment = (v) => !!v && typeof v === "object" && typeof v.url === "function" && typeof v.json === "function";
|
|
7
|
+
/** The published location of an attachment, asked for once. */
|
|
8
|
+
export function locationOf(attachment) {
|
|
9
|
+
let pending = locations.get(attachment);
|
|
10
|
+
if (!pending)
|
|
11
|
+
locations.set(attachment, (pending = attachment.url()));
|
|
12
|
+
return pending;
|
|
13
|
+
}
|
|
14
|
+
/** The parsed JSON content of an attachment, read once. */
|
|
15
|
+
export function contentOf(attachment) {
|
|
16
|
+
let pending = contents.get(attachment);
|
|
17
|
+
if (!pending)
|
|
18
|
+
contents.set(attachment, (pending = attachment.json()));
|
|
19
|
+
return pending;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=attachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.js","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA,+HAA+H;AAC/H,yGAAyG;AAOzG,mIAAmI;AACnI,MAAM,SAAS,GAAG,IAAI,OAAO,EAA2B,CAAC;AACzD,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA4B,CAAC;AAEzD,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAU,EAAmB,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAQ,CAAgB,CAAC,GAAG,KAAK,UAAU,IAAI,OAAQ,CAAgB,CAAC,IAAI,KAAK,UAAU,CAAC;AAEzL,+DAA+D;AAC/D,MAAM,UAAU,UAAU,CAAC,UAAsB;IAC/C,IAAI,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,IAAI,CAAC,OAAO;QAAE,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IACtE,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,SAAS,CAAC,UAAsB;IAC9C,IAAI,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACvC,IAAI,CAAC,OAAO;QAAE,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,OAAO,GAAG,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACtE,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { open } from "./open.js";
|
|
2
|
+
export type { GraphInput, Opened, OpenOptions } from "./open.js";
|
|
3
|
+
export type { Attachment } from "./attachments.js";
|
|
4
|
+
export { makeCypher, normalize, toGraph } from "./results/index.js";
|
|
5
|
+
export type { Cypher, Envelope, ForceGraph, NormalizedResult, Rows } from "./results/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// A loader that cannot produce its file writes the message to standard error and exits non-zero; Framework then stops the build.
|
|
2
|
+
/** Stop the build with `message`, which names the graph or file and the first problem. */
|
|
3
|
+
export function fail(message) {
|
|
4
|
+
process.stderr.write(message.endsWith("\n") ? message : `${message}\n`);
|
|
5
|
+
process.exit(1);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=fail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fail.js","sourceRoot":"","sources":["../../src/loader/fail.ts"],"names":[],"mappings":"AAAA,iIAAiI;AAEjI,0FAA0F;AAC1F,MAAM,UAAU,IAAI,CAAC,OAAe;IAClC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,CAAC;IACxE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Evaluated } from "@relate/contrib-web/build";
|
|
2
|
+
import type { IndexSpec } from "@relate/contrib-web/validate";
|
|
3
|
+
export declare const OWNER = "@relate/contrib-observable";
|
|
4
|
+
/** A module's default export: a function returning an ADR-002 document, or a promise of one. */
|
|
5
|
+
export type Source = () => unknown;
|
|
6
|
+
export interface DescriptorOptions {
|
|
7
|
+
/** Indexes the core creates before the nodes, as `{ label, property }`. */
|
|
8
|
+
indexes?: IndexSpec[];
|
|
9
|
+
}
|
|
10
|
+
/** Where the data file is, as the descriptor first names it; the page replaces it with the published location. */
|
|
11
|
+
export declare const dataName: (id: string) => string;
|
|
12
|
+
/** Check the graph's id and indexes by the descriptor rules (ADR-003), naming the graph and the problem. */
|
|
13
|
+
export declare function check(id: string, source: unknown, indexes?: IndexSpec[]): void;
|
|
14
|
+
/** Call the source and prepare its output: validated, canonical, with its revision and counts. */
|
|
15
|
+
export declare function prepared(id: string, source: Source, indexes?: IndexSpec[]): Promise<Evaluated>;
|
|
16
|
+
/**
|
|
17
|
+
* The canonical text with the revision it hashes to, carried in the document's own `revision` key (ADR-002). The core's
|
|
18
|
+
* agreement check compares that key with the descriptor's revision at load, which is how a source that is not deterministic is
|
|
19
|
+
* caught: the document and the descriptor come from separate loader runs.
|
|
20
|
+
*/
|
|
21
|
+
export declare function withRevision(evaluated: Evaluated): string;
|
|
22
|
+
/** The loader for a graph's document: writes it, or stops the build naming the graph and the first problem. */
|
|
23
|
+
export declare function document(id: string, source: Source): Promise<void>;
|
|
24
|
+
/** The loader for a graph's descriptor: the revision and counts of what `document` writes from the same source. */
|
|
25
|
+
export declare function descriptor(id: string, source: Source, options?: DescriptorOptions): Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The graph loaders: a source's document, validated and in canonical text, and the ADR-003 descriptor that names its revision.
|
|
2
|
+
import { descriptorOf, evaluate } from "@relate/contrib-web/build";
|
|
3
|
+
import { validateDescriptor } from "@relate/contrib-web/validate";
|
|
4
|
+
import { fail } from "./fail.js";
|
|
5
|
+
export const OWNER = "@relate/contrib-observable";
|
|
6
|
+
/** Where the data file is, as the descriptor first names it; the page replaces it with the published location. */
|
|
7
|
+
export const dataName = (id) => `data/${id}.graph.json`;
|
|
8
|
+
/** Check the graph's id and indexes by the descriptor rules (ADR-003), naming the graph and the problem. */
|
|
9
|
+
export function check(id, source, indexes = []) {
|
|
10
|
+
const probe = validateDescriptor({ version: 1, id, revision: "0", data: { url: "/x", nodes: 0, links: 0 }, ...(indexes.length ? { indexes } : {}) });
|
|
11
|
+
if (!probe.ok)
|
|
12
|
+
throw new Error(`${OWNER}: graph "${id}": ${probe.path}: ${probe.message}`);
|
|
13
|
+
if (typeof source !== "function")
|
|
14
|
+
throw new Error(`${OWNER}: graph "${id}": the source must be a function that returns a document`);
|
|
15
|
+
}
|
|
16
|
+
/** Call the source and prepare its output: validated, canonical, with its revision and counts. */
|
|
17
|
+
export async function prepared(id, source, indexes = []) {
|
|
18
|
+
check(id, source, indexes);
|
|
19
|
+
return evaluate(OWNER, id, source);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The canonical text with the revision it hashes to, carried in the document's own `revision` key (ADR-002). The core's
|
|
23
|
+
* agreement check compares that key with the descriptor's revision at load, which is how a source that is not deterministic is
|
|
24
|
+
* caught: the document and the descriptor come from separate loader runs.
|
|
25
|
+
*/
|
|
26
|
+
export function withRevision(evaluated) {
|
|
27
|
+
return evaluated.text.replace(/^\{/, `{"revision":${JSON.stringify(evaluated.revision)},`);
|
|
28
|
+
}
|
|
29
|
+
/** The loader for a graph's document: writes it, or stops the build naming the graph and the first problem. */
|
|
30
|
+
export async function document(id, source) {
|
|
31
|
+
try {
|
|
32
|
+
process.stdout.write(withRevision(await prepared(id, source)));
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
fail(e instanceof Error ? e.message : String(e));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** The loader for a graph's descriptor: the revision and counts of what `document` writes from the same source. */
|
|
39
|
+
export async function descriptor(id, source, options = {}) {
|
|
40
|
+
try {
|
|
41
|
+
const indexes = options.indexes ?? [];
|
|
42
|
+
const evaluated = await prepared(id, source, indexes);
|
|
43
|
+
process.stdout.write(JSON.stringify(descriptorOf(id, evaluated, indexes, "", dataName(id))) + "\n");
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
fail(e instanceof Error ? e.message : String(e));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/loader/graph.ts"],"names":[],"mappings":"AAAA,+HAA+H;AAC/H,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAEnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAElE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,MAAM,CAAC,MAAM,KAAK,GAAG,4BAA4B,CAAC;AAUlD,k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+GAA+G;AAC/G,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,EAAU,EAAE,MAAc;IACvD,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACnD,CAAC;AACH,CAAC;AAED,mHAAmH;AACnH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,EAAU,EAAE,MAAc,EAAE,OAAO,GAAsB,EAAE;IAC1F,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;QACtC,MAAM,SAAS,GAAG,MAAM,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACtD,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IACtG,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACnD,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { descriptor, document } from "./graph.js";
|
|
2
|
+
export type { DescriptorOptions, Source } from "./graph.js";
|
|
3
|
+
/** The bytes of the installed engine's binary, so a loader that calls this publishes it under a content-hashed name. */
|
|
4
|
+
export declare function engine(): Promise<void>;
|
|
5
|
+
/**
|
|
6
|
+
* The core's worker and the engine's script glue in one self-contained ES module, made now from the packages the site has
|
|
7
|
+
* installed, so the glue and the binary come from the same engine. The engine is resolved from the site and not from wherever
|
|
8
|
+
* the core's files happen to be, because the glue and the binary only work together.
|
|
9
|
+
*/
|
|
10
|
+
export declare function worker(): Promise<void>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The loader entry (contracts/loader-entry.md): imported from data loaders, which Framework runs as Node scripts with the
|
|
2
|
+
// project root as working directory. Each helper writes its file to standard output and, on failure, stops the build.
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
import { build } from "esbuild";
|
|
6
|
+
import { fail } from "./fail.js";
|
|
7
|
+
import { resolveFromSite } from "./resolve.js";
|
|
8
|
+
export { descriptor, document } from "./graph.js";
|
|
9
|
+
/** The bytes of the installed engine's binary, so a loader that calls this publishes it under a content-hashed name. */
|
|
10
|
+
export async function engine() {
|
|
11
|
+
try {
|
|
12
|
+
process.stdout.write(readFileSync(resolveFromSite("@relate/relate-wasm/relate_bg.wasm")));
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
fail(`@relate/contrib-observable: engine(): the engine's binary was not found (is @relate/relate-wasm installed?): ${e instanceof Error ? e.message : String(e)}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The core's worker and the engine's script glue in one self-contained ES module, made now from the packages the site has
|
|
20
|
+
* installed, so the glue and the binary come from the same engine. The engine is resolved from the site and not from wherever
|
|
21
|
+
* the core's files happen to be, because the glue and the binary only work together.
|
|
22
|
+
*/
|
|
23
|
+
export async function worker() {
|
|
24
|
+
try {
|
|
25
|
+
const enginePackage = dirname(resolveFromSite("@relate/relate-wasm/relate_bg.wasm"));
|
|
26
|
+
const result = await build({
|
|
27
|
+
entryPoints: [resolveFromSite("@relate/contrib-web/worker")],
|
|
28
|
+
bundle: true,
|
|
29
|
+
format: "esm",
|
|
30
|
+
platform: "browser",
|
|
31
|
+
write: false,
|
|
32
|
+
logLevel: "silent",
|
|
33
|
+
alias: { "@relate/relate-wasm": enginePackage },
|
|
34
|
+
});
|
|
35
|
+
process.stdout.write(result.outputFiles[0].text);
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
fail(`@relate/contrib-observable: worker(): the worker could not be bundled: ${e instanceof Error ? e.message : String(e)}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/loader/index.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,sHAAsH;AACtH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGlD,wHAAwH;AACxH,MAAM,CAAC,KAAK,UAAU,MAAM;IAC1B,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,eAAe,CAAC,oCAAoC,CAAC,CAAC,CAAC,CAAC;IAC5F,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,gHAAgH,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACrK,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM;IAC1B,IAAI,CAAC;QACH,MAAM,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC,oCAAoC,CAAC,CAAC,CAAC;QACrF,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC;YACzB,WAAW,EAAE,CAAC,eAAe,CAAC,4BAA4B,CAAC,CAAC;YAC5D,MAAM,EAAE,IAAI;YACZ,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,SAAS;YACnB,KAAK,EAAE,KAAK;YACZ,QAAQ,EAAE,QAAQ;YAClB,KAAK,EAAE,EAAE,qBAAqB,EAAE,aAAa,EAAE;SAChD,CAAC,CAAC;QACH,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAE,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,IAAI,CAAC,0EAA0E,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/H,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Package resolution for loaders (research D-1): from the site's root, which Framework gives a loader as its working directory.
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
/** The path a site's own installed package `specifier` resolves to, starting from `root` (default: the working directory). */
|
|
5
|
+
export function resolveFromSite(specifier, root = process.cwd()) {
|
|
6
|
+
return createRequire(join(root, "package.json")).resolve(specifier);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve.js","sourceRoot":"","sources":["../../src/loader/resolve.ts"],"names":[],"mappings":"AAAA,gIAAgI;AAChI,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,8HAA8H;AAC9H,MAAM,UAAU,eAAe,CAAC,SAAiB,EAAE,IAAI,GAAW,OAAO,CAAC,GAAG,EAAE;IAC7E,OAAO,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;AACtE,CAAC"}
|
package/dist/open.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Client, GraphDocument, IndexSpec } from "@relate/contrib-web";
|
|
2
|
+
import type { Attachment } from "./attachments.js";
|
|
3
|
+
import type { Cypher } from "./results/cypher.js";
|
|
4
|
+
import type { ForceGraph, NormalizedResult } from "./results/types.js";
|
|
5
|
+
export type GraphInput =
|
|
6
|
+
/** A descriptor attachment and the attachment of the data it names. */
|
|
7
|
+
{
|
|
8
|
+
descriptor: Attachment;
|
|
9
|
+
data: Attachment;
|
|
10
|
+
}
|
|
11
|
+
/** A document: an attachment of one, or an object the cell holds. No revision and no stored copy. */
|
|
12
|
+
| {
|
|
13
|
+
document: Attachment | GraphDocument;
|
|
14
|
+
indexes?: IndexSpec[];
|
|
15
|
+
};
|
|
16
|
+
export interface OpenOptions {
|
|
17
|
+
/** The engine's binary, from a loader that calls `engine()`. */
|
|
18
|
+
engine: Attachment;
|
|
19
|
+
/** The worker module, from a loader that calls `worker()`. */
|
|
20
|
+
worker: Attachment;
|
|
21
|
+
graph: GraphInput;
|
|
22
|
+
/** The cell's `invalidation`: the client is destroyed when it resolves. */
|
|
23
|
+
invalidation: Promise<unknown>;
|
|
24
|
+
}
|
|
25
|
+
export interface Opened {
|
|
26
|
+
/** The core's client: status, `subscribe`, `run` and the rest of its interface. */
|
|
27
|
+
client: Client;
|
|
28
|
+
/** Resolves when the instance is ready; rejects, with the core's wording, when the client reaches `failed`. */
|
|
29
|
+
ready: Promise<void>;
|
|
30
|
+
cypher: Cypher;
|
|
31
|
+
query(statement: string, params?: Record<string, unknown> | null): Promise<NormalizedResult>;
|
|
32
|
+
graph(result: {
|
|
33
|
+
rows: Array<Record<string, unknown>>;
|
|
34
|
+
}): ForceGraph;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Open a graph. The returned promise resolves as soon as the attachments' locations are known and the client exists, not when
|
|
38
|
+
* the graph is ready; `ready` says that. Nothing starts before `open` is called.
|
|
39
|
+
*/
|
|
40
|
+
export declare function open(options: OpenOptions): Promise<Opened>;
|
package/dist/open.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// `open`: a core client for a graph, tied to the cell that opened it (ADR-006 §2.3; contracts/browser-entry.md).
|
|
2
|
+
import { createClient } from "@relate/contrib-web";
|
|
3
|
+
import { contentOf, isAttachment, locationOf } from "./attachments.js";
|
|
4
|
+
import { makeCypher } from "./results/cypher.js";
|
|
5
|
+
import { toGraph } from "./results/graph.js";
|
|
6
|
+
import { normalize } from "./results/normalize.js";
|
|
7
|
+
const isThenable = (v) => !!v && typeof v.then === "function";
|
|
8
|
+
async function graphSource(graph) {
|
|
9
|
+
if ("descriptor" in graph) {
|
|
10
|
+
// The descriptor names its data by a logical name; the published name carries a content hash the loader could not know.
|
|
11
|
+
const descriptor = structuredClone(await contentOf(graph.descriptor));
|
|
12
|
+
descriptor.data = { ...descriptor.data, url: await locationOf(graph.data) };
|
|
13
|
+
return { descriptor };
|
|
14
|
+
}
|
|
15
|
+
const indexes = graph.indexes;
|
|
16
|
+
return { document: isAttachment(graph.document) ? await locationOf(graph.document) : graph.document, ...(indexes ? { indexes } : {}) };
|
|
17
|
+
}
|
|
18
|
+
function readiness(client) {
|
|
19
|
+
const ready = new Promise((resolve, reject) => {
|
|
20
|
+
const settle = () => {
|
|
21
|
+
if (client.status === "ready")
|
|
22
|
+
resolve();
|
|
23
|
+
else if (client.status === "failed")
|
|
24
|
+
reject(new Error(client.failureMessage()));
|
|
25
|
+
};
|
|
26
|
+
client.on("status", settle);
|
|
27
|
+
settle();
|
|
28
|
+
});
|
|
29
|
+
ready.catch(() => undefined); // a cell that never awaits `ready` is not an unhandled rejection
|
|
30
|
+
return ready;
|
|
31
|
+
}
|
|
32
|
+
let reloaded = false;
|
|
33
|
+
/** Tie `client` to its cell and its page. */
|
|
34
|
+
function ownedBy(client, invalidation) {
|
|
35
|
+
const target = globalThis;
|
|
36
|
+
let destroyed = false;
|
|
37
|
+
const destroy = () => {
|
|
38
|
+
if (destroyed)
|
|
39
|
+
return;
|
|
40
|
+
destroyed = true;
|
|
41
|
+
client.destroy();
|
|
42
|
+
};
|
|
43
|
+
const pagehide = () => destroy();
|
|
44
|
+
// A page restored from the back-forward cache has a destroyed client and a cell cannot rebuild one, so the document loads again.
|
|
45
|
+
const pageshow = (event) => {
|
|
46
|
+
if (event.persisted && !reloaded) {
|
|
47
|
+
reloaded = true;
|
|
48
|
+
globalThis.location.reload();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
target.addEventListener("pagehide", pagehide);
|
|
52
|
+
target.addEventListener("pageshow", pageshow);
|
|
53
|
+
void invalidation.then(() => {
|
|
54
|
+
destroy();
|
|
55
|
+
target.removeEventListener("pagehide", pagehide);
|
|
56
|
+
target.removeEventListener("pageshow", pageshow);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Open a graph. The returned promise resolves as soon as the attachments' locations are known and the client exists, not when
|
|
61
|
+
* the graph is ready; `ready` says that. Nothing starts before `open` is called.
|
|
62
|
+
*/
|
|
63
|
+
export async function open(options) {
|
|
64
|
+
const { engine, worker, graph, invalidation } = options;
|
|
65
|
+
if (!isAttachment(engine) || !isAttachment(worker))
|
|
66
|
+
throw new TypeError("@relate/contrib-observable: open(): `engine` and `worker` are file attachments, from FileAttachment(...)");
|
|
67
|
+
if (!isThenable(invalidation))
|
|
68
|
+
throw new TypeError("@relate/contrib-observable: open(): `invalidation` is the cell's invalidation promise; without it the client would outlive the cell");
|
|
69
|
+
const [engineUrl, workerUrl] = await Promise.all([locationOf(engine), locationOf(worker)]);
|
|
70
|
+
const source = await graphSource(graph);
|
|
71
|
+
const client = createClient({
|
|
72
|
+
engineUrl,
|
|
73
|
+
graph: source,
|
|
74
|
+
navigation: "multi-page",
|
|
75
|
+
workerFactory: () => new Worker(workerUrl, { type: "module" }),
|
|
76
|
+
});
|
|
77
|
+
ownedBy(client, invalidation);
|
|
78
|
+
const query = async (statement, params = null) => normalize((await client.query(statement, params)));
|
|
79
|
+
return { client, ready: readiness(client), query, cypher: makeCypher(query), graph: toGraph };
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=open.js.map
|
package/dist/open.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open.js","sourceRoot":"","sources":["../src/open.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEvE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AA6BnD,MAAM,UAAU,GAAG,CAAC,CAAU,EAAyB,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,OAAQ,CAAsB,CAAC,IAAI,KAAK,UAAU,CAAC;AAEpH,KAAK,UAAU,WAAW,CAAC,KAAiB;IAC1C,IAAI,YAAY,IAAI,KAAK,EAAE,CAAC;QAC1B,wHAAwH;QACxH,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,CAAe,CAAC;QACpF,UAAU,CAAC,IAAI,GAAG,EAAE,GAAG,UAAU,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5E,OAAO,EAAE,UAAU,EAAE,CAAC;IACxB,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,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M,GAAG,YAAY,CAAC;QAC1B,SAAS;QACT,KAAK,EAAE,MAAM;QACb,UAAU,EAAE,YAAY;QACxB,aAAa,EAAE,GAAG,EAAE,CAAC,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;KAC/D,CAAC,CAAC;IACH,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,KAAK,EAAE,SAAiB,EAAE,MAAM,GAAmC,IAAI,EAA6B,EAAE,CAAC,SAAS,CAAC,CAAC,MAAM,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAa,CAAC,CAAC;IACpL,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAChG,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { NormalizedResult, Rows } from "./types.js";
|
|
2
|
+
export type Query = (text: string, params: Record<string, unknown> | null) => Promise<NormalizedResult>;
|
|
3
|
+
export type Cypher = (strings: TemplateStringsArray, ...values: unknown[]) => Promise<Rows>;
|
|
4
|
+
/**
|
|
5
|
+
* A tagged template over `query`, in the style of Observable's database clients:
|
|
6
|
+
*
|
|
7
|
+
* const rows = await cypher`MATCH (p:Person) WHERE p.age >= ${minAge} RETURN p.name AS name`;
|
|
8
|
+
*
|
|
9
|
+
* Each `${}` becomes a parameter (`$p0`, `$p1`, ...), so a value is never spliced into the statement text. The call resolves to an
|
|
10
|
+
* array of flattened rows with `columns`, `columnTypes`, `stats` and `elapsedMs` attached and the normalized result, with the
|
|
11
|
+
* engine's original entities, on the non-enumerable `result`. It rejects when the engine returns an error.
|
|
12
|
+
*/
|
|
13
|
+
export declare function makeCypher(query: Query): Cypher;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tagged template over `query`, in the style of Observable's database clients:
|
|
3
|
+
*
|
|
4
|
+
* const rows = await cypher`MATCH (p:Person) WHERE p.age >= ${minAge} RETURN p.name AS name`;
|
|
5
|
+
*
|
|
6
|
+
* Each `${}` becomes a parameter (`$p0`, `$p1`, ...), so a value is never spliced into the statement text. The call resolves to an
|
|
7
|
+
* array of flattened rows with `columns`, `columnTypes`, `stats` and `elapsedMs` attached and the normalized result, with the
|
|
8
|
+
* engine's original entities, on the non-enumerable `result`. It rejects when the engine returns an error.
|
|
9
|
+
*/
|
|
10
|
+
export function makeCypher(query) {
|
|
11
|
+
return async function cypher(strings, ...values) {
|
|
12
|
+
const text = strings.reduce((acc, s, i) => acc + s + (i < values.length ? `$p${i}` : ""), "");
|
|
13
|
+
const params = values.length ? Object.fromEntries(values.map((v, i) => [`p${i}`, v])) : null;
|
|
14
|
+
const result = await query(text, params);
|
|
15
|
+
if (result.error)
|
|
16
|
+
throw new Error(result.error);
|
|
17
|
+
const rows = result.tableRows;
|
|
18
|
+
rows.columns = result.columns;
|
|
19
|
+
rows.columnTypes = result.columnTypes;
|
|
20
|
+
rows.stats = result.stats;
|
|
21
|
+
rows.elapsedMs = result.elapsedMs;
|
|
22
|
+
Object.defineProperty(rows, "result", { value: result });
|
|
23
|
+
return rows;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=cypher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cypher.js","sourceRoot":"","sources":["../../src/results/cypher.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,KAAY;IACrC,OAAO,KAAK,UAAU,MAAM,CAAC,OAAO,EAAE,GAAG,MAAM;QAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;QAC9F,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC7F,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACzC,IAAI,MAAM,CAAC,KAAK;YAAE,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAChD,MAAM,IAAI,GAAG,MAAM,CAAC,SAAiB,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACtC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;QACzD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ForceGraph } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Every node and relationship a result carries, in the shape a force layout takes. A relationship whose endpoints are not both in
|
|
4
|
+
* the result is dropped (a layout's link force fails on a missing node). A result with no entities gives an empty graph.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toGraph(result: {
|
|
7
|
+
rows: Array<Record<string, unknown>>;
|
|
8
|
+
}): ForceGraph;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isNode, isRelationship, nodeName } from "./normalize.js";
|
|
2
|
+
const isPathObject = (v) => !!v && typeof v === "object" && Array.isArray(v.nodes) && Array.isArray(v.relationships);
|
|
3
|
+
/**
|
|
4
|
+
* Every node and relationship a result carries, in the shape a force layout takes. A relationship whose endpoints are not both in
|
|
5
|
+
* the result is dropped (a layout's link force fails on a missing node). A result with no entities gives an empty graph.
|
|
6
|
+
*/
|
|
7
|
+
export function toGraph(result) {
|
|
8
|
+
const nodes = new Map();
|
|
9
|
+
const relationships = new Map();
|
|
10
|
+
const visit = (v) => {
|
|
11
|
+
if (isNode(v))
|
|
12
|
+
nodes.set(v.id, v);
|
|
13
|
+
else if (isRelationship(v))
|
|
14
|
+
relationships.set(v.id, v);
|
|
15
|
+
else if (isPathObject(v))
|
|
16
|
+
[...v.nodes, ...v.relationships].forEach(visit);
|
|
17
|
+
else if (Array.isArray(v))
|
|
18
|
+
v.forEach(visit);
|
|
19
|
+
};
|
|
20
|
+
for (const row of result.rows)
|
|
21
|
+
Object.values(row).forEach(visit);
|
|
22
|
+
return {
|
|
23
|
+
nodes: [...nodes.values()].map((n) => ({ id: n.id, label: n.labels?.[0] ?? "Node", name: nodeName(n), properties: n.properties })),
|
|
24
|
+
links: [...relationships.values()]
|
|
25
|
+
.filter((r) => nodes.has(r.start_node) && nodes.has(r.end_node))
|
|
26
|
+
.map((r) => ({ source: r.start_node, target: r.end_node, type: r.type })),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/results/graph.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAGlE,MAAM,YAAY,GAAG,CAAC,CAAU,EAAuD,EAAE,CACvF,CAAC,CAAC,CAAC,IAAI,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,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC;QAClI,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC;aAC/B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;aAC/D,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;KAC5E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { normalize, nodeName } from "./normalize.js";
|
|
2
|
+
export { toGraph } from "./graph.js";
|
|
3
|
+
export { makeCypher } from "./cypher.js";
|
|
4
|
+
export type { Cypher, Query } from "./cypher.js";
|
|
5
|
+
export type { Envelope, ForceGraph, GraphLink, GraphNode, NodeValue, NormalizedResult, RelationshipValue, Rows } from "./types.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/results/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Envelope, NodeValue, NormalizedResult, RelationshipValue } from "./types.js";
|
|
2
|
+
export declare const isNode: (v: unknown) => v is NodeValue;
|
|
3
|
+
export declare const isRelationship: (v: unknown) => v is RelationshipValue;
|
|
4
|
+
export declare const isPathArray: (v: unknown) => v is unknown[];
|
|
5
|
+
/** A readable name for a node: its `name` or `title` property, else its first label and id. */
|
|
6
|
+
export declare function nodeName(node: NodeValue): string;
|
|
7
|
+
/** The parsed envelope as `{ columns, columnTypes, rows, tableRows, stats, error, elapsedMs }`; `rows` keep the original entities. */
|
|
8
|
+
export declare function normalize(envelope: Envelope): NormalizedResult;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export const isNode = (v) => !!v && typeof v === "object" && v.kind === "node";
|
|
2
|
+
export const isRelationship = (v) => !!v && typeof v === "object" && v.kind === "relationship";
|
|
3
|
+
export const isPathArray = (v) => Array.isArray(v) && v.length > 0 && isNode(v[0]);
|
|
4
|
+
/** A readable name for a node: its `name` or `title` property, else its first label and id. */
|
|
5
|
+
export function nodeName(node) {
|
|
6
|
+
const p = node.properties ?? {};
|
|
7
|
+
return String(p.name ?? p.title ?? `${node.labels?.[0] ?? "Node"} ${node.id}`);
|
|
8
|
+
}
|
|
9
|
+
// Entities flatten for table cells. A table formats cells with String(), so a flattened entity carries a non-enumerable toString.
|
|
10
|
+
function flatten(v) {
|
|
11
|
+
if (isNode(v)) {
|
|
12
|
+
const flat = { id: v.id, labels: v.labels, ...v.properties };
|
|
13
|
+
Object.defineProperty(flat, "toString", { value: () => `(${v.labels.join(":")} ${nodeName(v)})` });
|
|
14
|
+
return flat;
|
|
15
|
+
}
|
|
16
|
+
if (isRelationship(v)) {
|
|
17
|
+
const flat = { id: v.id, type: v.type, start: v.start_node, end: v.end_node, ...v.properties };
|
|
18
|
+
Object.defineProperty(flat, "toString", { value: () => `[:${v.type}]` });
|
|
19
|
+
return flat;
|
|
20
|
+
}
|
|
21
|
+
if (isPathArray(v)) {
|
|
22
|
+
const out = v.map(flatten);
|
|
23
|
+
Object.defineProperty(out, "toString", { value: () => v.map((e) => (isNode(e) ? nodeName(e) : `-[:${e.type}]-`)).join("") });
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
return v;
|
|
27
|
+
}
|
|
28
|
+
/** The parsed envelope as `{ columns, columnTypes, rows, tableRows, stats, error, elapsedMs }`; `rows` keep the original entities. */
|
|
29
|
+
export function normalize(envelope) {
|
|
30
|
+
const columns = envelope.columns ?? [];
|
|
31
|
+
const rows = (envelope.rows ?? []).map((row) => Object.fromEntries(columns.map((c) => [c, row[c]])));
|
|
32
|
+
return {
|
|
33
|
+
columns,
|
|
34
|
+
columnTypes: envelope.columnTypes ?? [],
|
|
35
|
+
rows,
|
|
36
|
+
tableRows: rows.map((row) => Object.fromEntries(columns.map((c) => [c, flatten(row[c])]))),
|
|
37
|
+
stats: envelope.stats ?? {},
|
|
38
|
+
error: envelope.error ?? null,
|
|
39
|
+
elapsedMs: envelope.elapsed_ms ?? null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/results/normalize.ts"],"names":[],"mappings":"AAQA,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,CAAU,EAAkB,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAK,CAAwB,CAAC,IAAI,KAAK,MAAM,CAAC;AAChI,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAU,EAA0B,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAK,CAAwB,CAAC,IAAI,KAAK,cAAc,CAAC;AACxJ,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAU,EAAkB,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAE5G,+FAA+F;AAC/F,MAAM,UAAU,QAAQ,CAAC,IAAe;IACtC,MAAM,CAAC,GAAG,IAAI,CAAC,UAAU,IAAI,EAAE,CAAC;IAChC,OAAO,MAAM,CAAC,CAAC,C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
|
|
1
|
+
export interface Envelope {
|
|
2
|
+
columns?: string[];
|
|
3
|
+
columnTypes?: string[];
|
|
4
|
+
rows?: Array<Record<string, unknown>>;
|
|
5
|
+
stats?: Record<string, unknown>;
|
|
6
|
+
error?: string | null;
|
|
7
|
+
elapsed_ms?: number | null;
|
|
8
|
+
}
|
|
9
|
+
export interface NodeValue {
|
|
10
|
+
kind: "node";
|
|
11
|
+
id: number | string;
|
|
12
|
+
labels: string[];
|
|
13
|
+
properties?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export interface RelationshipValue {
|
|
16
|
+
kind: "relationship";
|
|
17
|
+
id: number | string;
|
|
18
|
+
type: string;
|
|
19
|
+
start_node: number | string;
|
|
20
|
+
end_node: number | string;
|
|
21
|
+
properties?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
export interface NormalizedResult {
|
|
24
|
+
columns: string[];
|
|
25
|
+
columnTypes: string[];
|
|
26
|
+
/** Rows keyed in column order, with the engine's tagged entities. */
|
|
27
|
+
rows: Array<Record<string, unknown>>;
|
|
28
|
+
/** The same rows with entities flattened for tables and accessors. */
|
|
29
|
+
tableRows: Array<Record<string, unknown>>;
|
|
30
|
+
stats: Record<string, unknown>;
|
|
31
|
+
error: string | null;
|
|
32
|
+
elapsedMs: number | null;
|
|
33
|
+
}
|
|
34
|
+
/** The rows of a query written as a tagged template: flattened rows with the result's metadata attached. */
|
|
35
|
+
export type Rows = Array<Record<string, unknown>> & {
|
|
36
|
+
columns: string[];
|
|
37
|
+
columnTypes: string[];
|
|
38
|
+
stats: Record<string, unknown>;
|
|
39
|
+
elapsedMs: number | null;
|
|
40
|
+
/** The normalized result with the engine's original entities; not enumerable. */
|
|
41
|
+
readonly result: NormalizedResult;
|
|
42
|
+
};
|
|
43
|
+
export interface GraphNode {
|
|
44
|
+
id: number | string;
|
|
45
|
+
label: string;
|
|
46
|
+
name: string;
|
|
47
|
+
properties: Record<string, unknown> | undefined;
|
|
48
|
+
}
|
|
49
|
+
export interface GraphLink {
|
|
50
|
+
source: number | string;
|
|
51
|
+
target: number | string;
|
|
52
|
+
type: string;
|
|
53
|
+
}
|
|
54
|
+
export interface ForceGraph {
|
|
55
|
+
nodes: GraphNode[];
|
|
56
|
+
links: GraphLink[];
|
|
57
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/results/types.ts"],"names":[],"mappings":"AAAA,kHAAkH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relate/contrib-observable",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Observable Framework helpers for @relate/contrib-web: loaders that place the engine and the worker and publish graph data, a call that ties a graph client to a cell, and result shaping for Framework's components.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/relatedb/relate-contrib.git",
|
|
9
|
+
"directory": "packages/observable"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/relatedb/relate-contrib/tree/main/packages/observable#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/relatedb/relate-contrib/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"relate",
|
|
17
|
+
"relatedb",
|
|
18
|
+
"observable",
|
|
19
|
+
"observable-framework",
|
|
20
|
+
"graph",
|
|
21
|
+
"wasm"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./loader": {
|
|
37
|
+
"types": "./dist/loader/index.d.ts",
|
|
38
|
+
"default": "./dist/loader/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./results": {
|
|
41
|
+
"types": "./dist/results/index.d.ts",
|
|
42
|
+
"default": "./dist/results/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@observablehq/framework": "^1.13.0",
|
|
47
|
+
"@relate/contrib-web": ">=0.0.0 <1.0.0",
|
|
48
|
+
"@relate/relate-wasm": ">=1.2.0 <1.3.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"esbuild": "^0.28.2"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@observablehq/framework": "^1.13.4",
|
|
55
|
+
"@playwright/test": "^1.64.1",
|
|
56
|
+
"@relate/relate-wasm": "1.2.0",
|
|
57
|
+
"@types/node": "^26.6.6",
|
|
58
|
+
"tsx": "^4.23.15",
|
|
59
|
+
"typescript": "^7.0.2",
|
|
60
|
+
"vitest": "^5.0.5",
|
|
61
|
+
"@relate/contrib-web": "0.1.2"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsc -p tsconfig.json",
|
|
65
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
66
|
+
"test:observable": "vitest run --reporter=default --reporter=json --outputFile.json=reports/vitest.json",
|
|
67
|
+
"test:observable-browsers": "playwright test --grep-invert \"@conformance|@figures\"",
|
|
68
|
+
"test:observable-conformance": "REL_REPORT=reports/conformance.json playwright test --grep @conformance",
|
|
69
|
+
"test:observable-package": "vitest run --config vitest.package.config.ts --reporter=default --reporter=json --outputFile.json=reports/package.json",
|
|
70
|
+
"test:observable-trace": "tsx tests/trace/check-trace.ts"
|
|
71
|
+
}
|
|
6
72
|
}
|