@rayfold/explorer 0.1.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/LICENSE +202 -0
- package/NOTICE +10 -0
- package/README.md +55 -0
- package/index.d.ts +30 -0
- package/index.js +271 -0
- package/index.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Rayfold
|
|
2
|
+
Copyright 2026 The Rayfold Authors
|
|
3
|
+
|
|
4
|
+
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
The data in data/gutenberg.json.gz is derived from the Project Gutenberg catalogue
|
|
7
|
+
(https://www.gutenberg.org/ebooks/offline_catalogs.html) and is not covered by this
|
|
8
|
+
project's copyright. See data/README.md for its source. Project Gutenberg is a
|
|
9
|
+
registered trademark of the Project Gutenberg Literary Archive Foundation; Rayfold is
|
|
10
|
+
not affiliated with or endorsed by Project Gutenberg.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @rayfold/explorer
|
|
2
|
+
|
|
3
|
+
The page a [Rayfold](https://github.com/eddyboutros/rayfold) server serves next to its endpoint: the counterpart of
|
|
4
|
+
Swagger UI and GraphiQL. It reads the server's manifest, so it shows what the schema actually says rather than a copy
|
|
5
|
+
that can drift.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @rayfold/explorer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createServer } from "node:http";
|
|
13
|
+
import { createHttpHandler } from "@rayfold/server";
|
|
14
|
+
import { createExplorerHandler } from "@rayfold/explorer";
|
|
15
|
+
|
|
16
|
+
const endpoint = createHttpHandler(server, { viewer });
|
|
17
|
+
const explorer = createExplorerHandler({ endpoint: "/rayfold", title: "Acme API" });
|
|
18
|
+
|
|
19
|
+
createServer((req, res) => {
|
|
20
|
+
if (explorer(req, res)) return; // serves /rayfold/explorer
|
|
21
|
+
if ((req.url ?? "").startsWith("/rayfold")) return endpoint(req, res);
|
|
22
|
+
res.writeHead(404).end();
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The page lists every operation with its arguments, what it returns, what it costs and which policies guard it; fills
|
|
27
|
+
in a request with a shape that selects the scalar fields of the result, as a starting point; sends the batch to the
|
|
28
|
+
endpoint like any other client; and shows the frames as they arrive, each with its cost. A command that allows
|
|
29
|
+
`@simulate` can be run as a **dry run**, which reports what would happen and writes nothing. A query can be sent
|
|
30
|
+
`live`, and the frames keep arriving as the data changes.
|
|
31
|
+
|
|
32
|
+
It is one self-contained document: no fonts, scripts or styles from anywhere else, so it works behind a strict
|
|
33
|
+
`Content-Security-Policy` and with no network beyond your own server.
|
|
34
|
+
|
|
35
|
+
## Where it is served, and to whom
|
|
36
|
+
|
|
37
|
+
Nothing is served unless you mount it. The explorer talks to the endpoint as a browser client: what a visitor can read
|
|
38
|
+
through it is exactly what the endpoint's own policies allow for the token they paste into it, and no more. On a public
|
|
39
|
+
deployment, mount it behind the same authentication as the rest of your administration pages, or only in development:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
if (process.env.NODE_ENV !== "production") app.use(createExplorerHandler());
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`explorerHtml(options)` returns the same page as a string, for serving it from a framework of your own.
|
|
46
|
+
|
|
47
|
+
## Options
|
|
48
|
+
|
|
49
|
+
| Option | Default | What it does |
|
|
50
|
+
| --- | --- | --- |
|
|
51
|
+
| `endpoint` | `/rayfold` | Where the page sends its batches, and where it reads the manifest. |
|
|
52
|
+
| `path` | `<endpoint>/explorer` | Where the page itself is served. |
|
|
53
|
+
| `title` | `Rayfold` | Shown in the header, to tell one service from another. |
|
|
54
|
+
|
|
55
|
+
Apache-2.0.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Rayfold explorer: the page a Rayfold server serves next to its endpoint, the counterpart of Swagger UI and
|
|
3
|
+
* GraphiQL. It lists every operation the manifest declares with its arguments, result, cost and the policies that
|
|
4
|
+
* guard it; helps write a shape; sends a batch and shows the frames as they arrive, with their cost; and dry-runs a
|
|
5
|
+
* command that allows `@simulate`, so trying one changes nothing.
|
|
6
|
+
*
|
|
7
|
+
* It talks to the endpoint over HTTP like any other client, so it needs no access to the server object and adds no
|
|
8
|
+
* dependency to it. Mount it where you like:
|
|
9
|
+
*
|
|
10
|
+
* const explorer = createExplorerHandler({ endpoint: "/rayfold", title: "Acme API" });
|
|
11
|
+
* createServer((req, res) => { if (explorer(req, res)) return; ... });
|
|
12
|
+
*
|
|
13
|
+
* Nothing is served unless you mount it, so a production deployment exposes it only on purpose.
|
|
14
|
+
*/
|
|
15
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
16
|
+
export interface ExplorerOptions {
|
|
17
|
+
/** Where the Rayfold endpoint is mounted. Default `/rayfold`. */
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
/** Where this page is served. Default `<endpoint>/explorer`. */
|
|
20
|
+
path?: string;
|
|
21
|
+
/** Shown in the header, to tell one service from another. */
|
|
22
|
+
title?: string;
|
|
23
|
+
}
|
|
24
|
+
/** The page itself, for serving from a framework of your own. */
|
|
25
|
+
export declare function explorerHtml(opts?: ExplorerOptions): string;
|
|
26
|
+
/**
|
|
27
|
+
* A Node handler that serves the page. It answers only its own path and returns false for anything else, so it can
|
|
28
|
+
* sit in front of the rest of an application.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createExplorerHandler(opts?: ExplorerOptions): (req: IncomingMessage, res: ServerResponse) => boolean;
|
package/index.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
const settings = (opts = {}) => {
|
|
2
|
+
const endpoint = opts.endpoint ?? "/rayfold";
|
|
3
|
+
return {
|
|
4
|
+
endpoint,
|
|
5
|
+
path: opts.path ?? `${endpoint}/explorer`,
|
|
6
|
+
config: { endpoint, title: opts.title ?? "Rayfold" },
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
/** The page itself, for serving from a framework of your own. */
|
|
10
|
+
export function explorerHtml(opts = {}) {
|
|
11
|
+
return PAGE.replace("__RAYFOLD_EXPLORER_CONFIG__", JSON.stringify(settings(opts).config).replace(/</g, "\\u003c"));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A Node handler that serves the page. It answers only its own path and returns false for anything else, so it can
|
|
15
|
+
* sit in front of the rest of an application.
|
|
16
|
+
*/
|
|
17
|
+
export function createExplorerHandler(opts = {}) {
|
|
18
|
+
const { path } = settings(opts);
|
|
19
|
+
const html = explorerHtml(opts);
|
|
20
|
+
return (req, res) => {
|
|
21
|
+
const url = (req.url ?? "/").split("?")[0];
|
|
22
|
+
if (url !== path && url !== `${path}/`)
|
|
23
|
+
return false;
|
|
24
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
25
|
+
res.writeHead(405, { allow: "GET, HEAD" }).end();
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
|
|
29
|
+
res.end(req.method === "HEAD" ? undefined : html);
|
|
30
|
+
return true;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
// The page is one self-contained document: no fonts, scripts or styles from anywhere else, so it works behind a
|
|
34
|
+
// strict content-security-policy and offline. Client code avoids template literals, since this is one itself.
|
|
35
|
+
const PAGE = String.raw `<!doctype html>
|
|
36
|
+
<html lang="en">
|
|
37
|
+
<head>
|
|
38
|
+
<meta charset="utf-8">
|
|
39
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
40
|
+
<title>Rayfold explorer</title>
|
|
41
|
+
<style>
|
|
42
|
+
:root { color-scheme: light dark; --ink: #16202b; --muted: #5b6875; --rule: #d6dde5; --surface: #fff; --ground: #f3f6f9; --accent: #2a78d6; --ok: #0a7d3f; --bad: #b3261e; }
|
|
43
|
+
@media (prefers-color-scheme: dark) { :root { --ink: #e8eef5; --muted: #a2aebc; --rule: #2b333d; --surface: #171c22; --ground: #0f1318; --accent: #5aa2f0; --ok: #4cc073; --bad: #f2836f; } }
|
|
44
|
+
* { box-sizing: border-box; }
|
|
45
|
+
body { margin: 0; background: var(--ground); color: var(--ink); font: 14px/1.55 ui-sans-serif, system-ui, "Segoe UI", sans-serif; }
|
|
46
|
+
header { display: flex; gap: 12px; align-items: baseline; padding: 12px 16px; border-bottom: 1px solid var(--rule); background: var(--surface); }
|
|
47
|
+
header h1 { font-size: 15px; margin: 0; font-weight: 600; }
|
|
48
|
+
header .meta { color: var(--muted); font: 12px/1.4 ui-monospace, monospace; }
|
|
49
|
+
main { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 0; height: calc(100vh - 49px); }
|
|
50
|
+
@media (max-width: 800px) { main { grid-template-columns: 1fr; height: auto; } }
|
|
51
|
+
aside { border-right: 1px solid var(--rule); overflow: auto; background: var(--surface); }
|
|
52
|
+
aside h2 { font: 600 11px/1 ui-monospace, monospace; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 12px 6px; }
|
|
53
|
+
button.op { display: block; width: 100%; text-align: left; border: 0; background: none; color: inherit; font: inherit; padding: 6px 12px; cursor: pointer; }
|
|
54
|
+
button.op:hover, button.op.on { background: var(--ground); }
|
|
55
|
+
button.op .kind { font: 11px ui-monospace, monospace; color: var(--muted); margin-left: 6px; }
|
|
56
|
+
section.work { display: grid; grid-template-rows: auto auto 1fr; min-width: 0; }
|
|
57
|
+
.doc { padding: 12px 16px; border-bottom: 1px solid var(--rule); background: var(--surface); }
|
|
58
|
+
.doc h3 { margin: 0 0 4px; font-size: 15px; }
|
|
59
|
+
.doc dl { display: grid; grid-template-columns: 92px minmax(0, 1fr); gap: 2px 10px; margin: 6px 0 0; font-size: 13px; }
|
|
60
|
+
.doc dt { color: var(--muted); font: 12px ui-monospace, monospace; }
|
|
61
|
+
.doc dd { margin: 0; font-family: ui-monospace, monospace; overflow-wrap: anywhere; }
|
|
62
|
+
.editor { display: grid; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--rule); }
|
|
63
|
+
textarea, input[type=text] { width: 100%; font: 12.5px/1.5 ui-monospace, monospace; color: inherit; background: var(--surface); border: 1px solid var(--rule); border-radius: 6px; padding: 8px; }
|
|
64
|
+
textarea { min-height: 120px; resize: vertical; }
|
|
65
|
+
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
|
|
66
|
+
.row label { font-size: 12.5px; color: var(--muted); display: flex; gap: 5px; align-items: center; }
|
|
67
|
+
button.go { background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 7px 14px; font: 600 13px/1 inherit; cursor: pointer; }
|
|
68
|
+
button.go:disabled { opacity: .55; cursor: default; }
|
|
69
|
+
.out { overflow: auto; padding: 12px 16px; display: grid; gap: 8px; align-content: start; }
|
|
70
|
+
.frame { border: 1px solid var(--rule); border-left-width: 3px; border-radius: 6px; background: var(--surface); padding: 8px 10px; }
|
|
71
|
+
.frame pre { margin: 4px 0 0; font: 12px/1.5 ui-monospace, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
|
|
72
|
+
.frame .tag { font: 600 11px ui-monospace, monospace; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
|
|
73
|
+
.frame.ok, .frame.data { border-left-color: var(--ok); }
|
|
74
|
+
.frame.error { border-left-color: var(--bad); }
|
|
75
|
+
.frame.patch, .frame.item, .frame.at { border-left-color: var(--accent); }
|
|
76
|
+
.hint { color: var(--muted); font-size: 12.5px; }
|
|
77
|
+
code { font-family: ui-monospace, monospace; }
|
|
78
|
+
</style>
|
|
79
|
+
</head>
|
|
80
|
+
<body>
|
|
81
|
+
<script type="application/json" id="config">__RAYFOLD_EXPLORER_CONFIG__</script>
|
|
82
|
+
<header>
|
|
83
|
+
<h1 id="title">Rayfold</h1>
|
|
84
|
+
<span class="meta" id="about">reading the manifest...</span>
|
|
85
|
+
</header>
|
|
86
|
+
<main>
|
|
87
|
+
<aside id="ops"></aside>
|
|
88
|
+
<section class="work">
|
|
89
|
+
<div class="doc" id="doc"><p class="hint">Pick an operation on the left. The explorer reads everything it shows from the server's manifest.</p></div>
|
|
90
|
+
<div class="editor">
|
|
91
|
+
<textarea id="req" spellcheck="false"></textarea>
|
|
92
|
+
<div class="row">
|
|
93
|
+
<button class="go" id="send">Send</button>
|
|
94
|
+
<label><input type="checkbox" id="simulate"> dry run</label>
|
|
95
|
+
<label><input type="checkbox" id="live"> live</label>
|
|
96
|
+
<label>auth <input type="text" id="token" placeholder="bearer token" style="width:170px"></label>
|
|
97
|
+
<span class="hint" id="status"></span>
|
|
98
|
+
</div>
|
|
99
|
+
<p class="hint">A batch can hold several ops, and a later one can use an earlier result: <code>{"$ref": "1.id"}</code> in its args. Ctrl+Enter sends.</p>
|
|
100
|
+
</div>
|
|
101
|
+
<div class="out" id="out"></div>
|
|
102
|
+
</section>
|
|
103
|
+
</main>
|
|
104
|
+
<script>
|
|
105
|
+
var CONFIG = JSON.parse(document.getElementById("config").textContent);
|
|
106
|
+
var $ = function (sel) { return document.querySelector(sel); };
|
|
107
|
+
var manifest = null;
|
|
108
|
+
var current = null;
|
|
109
|
+
|
|
110
|
+
document.getElementById("title").textContent = CONFIG.title;
|
|
111
|
+
|
|
112
|
+
function text(node, s) { node.textContent = s; return node; }
|
|
113
|
+
function el(tag, cls, s) { var n = document.createElement(tag); if (cls) n.className = cls; if (s !== undefined) n.textContent = s; return n; }
|
|
114
|
+
function typeName(t) {
|
|
115
|
+
if (!t) return "?";
|
|
116
|
+
if (t.kind === "list") return "[" + typeName(t.of) + "]" + (t.nullable ? "?" : "");
|
|
117
|
+
var args = t.args && t.args.length ? "<" + t.args.map(typeName).join(", ") + ">" : "";
|
|
118
|
+
return t.name + args + (t.nullable ? "?" : "");
|
|
119
|
+
}
|
|
120
|
+
function annotationsOf(def, name) { return (def.annotations || []).filter(function (a) { return a.name === name; }); }
|
|
121
|
+
function costOf(def) {
|
|
122
|
+
var c = annotationsOf(def, "cost")[0];
|
|
123
|
+
if (!c) return "default";
|
|
124
|
+
return Object.keys(c.args).map(function (k) { return k + " " + JSON.stringify(c.args[k]); }).join(", ");
|
|
125
|
+
}
|
|
126
|
+
function policiesOf(def) {
|
|
127
|
+
var names = [];
|
|
128
|
+
annotationsOf(def, "allow").forEach(function () { names.push("allow"); });
|
|
129
|
+
annotationsOf(def, "deny").forEach(function () { names.push("deny"); });
|
|
130
|
+
return names.length ? names.join(", ") : "none on the operation";
|
|
131
|
+
}
|
|
132
|
+
/** A shape that selects the scalar fields of what an op returns: a starting point, not a rule. */
|
|
133
|
+
function defaultShape(typeRef) {
|
|
134
|
+
var base = typeRef;
|
|
135
|
+
while (base && base.kind === "list") base = base.of;
|
|
136
|
+
if (!base) return "";
|
|
137
|
+
var def = manifest.schema.types[base.name];
|
|
138
|
+
if (!def || !def.fields) return "";
|
|
139
|
+
if (base.name === "Page" && base.args && base.args.length) {
|
|
140
|
+
return "{ total hasMore cursor items " + (defaultShape(base.args[0]) || "{ id }") + " }";
|
|
141
|
+
}
|
|
142
|
+
var scalars = def.fields.filter(function (f) {
|
|
143
|
+
var t = f.type.kind === "list" ? f.type.of : f.type;
|
|
144
|
+
var sub = manifest.schema.types[t.name];
|
|
145
|
+
return !f.args.length && sub && (sub.kind === "scalar" || sub.kind === "enum");
|
|
146
|
+
});
|
|
147
|
+
if (!scalars.length) return "";
|
|
148
|
+
return "{ " + scalars.map(function (f) { return f.name; }).join(" ") + " }";
|
|
149
|
+
}
|
|
150
|
+
function argSkeleton(def) {
|
|
151
|
+
var args = {};
|
|
152
|
+
def.args.forEach(function (a) {
|
|
153
|
+
if (a.default !== undefined) { args[a.name] = a.default; return; }
|
|
154
|
+
var t = a.type.kind === "list" ? a.type.of : a.type;
|
|
155
|
+
var sub = manifest.schema.types[t.name];
|
|
156
|
+
args[a.name] = a.type.kind === "list" ? [] : sub && sub.kind === "enum" ? sub.values[0].name : t.name === "Int" || t.name === "Float" || t.name === "Long" ? 0 : t.name === "Boolean" ? false : "";
|
|
157
|
+
});
|
|
158
|
+
return args;
|
|
159
|
+
}
|
|
160
|
+
function show(op) {
|
|
161
|
+
current = op;
|
|
162
|
+
Array.prototype.forEach.call(document.querySelectorAll("button.op"), function (b) { b.classList.toggle("on", b.dataset.op === op.name); });
|
|
163
|
+
var doc = document.getElementById("doc");
|
|
164
|
+
doc.innerHTML = "";
|
|
165
|
+
doc.appendChild(el("h3", null, op.name + "(" + op.args.map(function (a) { return a.name + ": " + typeName(a.type); }).join(", ") + ")"));
|
|
166
|
+
if (op.description) doc.appendChild(el("p", "hint", op.description));
|
|
167
|
+
var dl = el("dl");
|
|
168
|
+
function add(k, v) { dl.appendChild(el("dt", null, k)); dl.appendChild(el("dd", null, v)); }
|
|
169
|
+
add("kind", op.kind);
|
|
170
|
+
add("returns", typeName(op.returns));
|
|
171
|
+
add("cost", costOf(op));
|
|
172
|
+
add("policies", policiesOf(op));
|
|
173
|
+
if (op.throws && op.throws.length) add("throws", op.throws.join(" | "));
|
|
174
|
+
if (op.emits && op.emits.length) add("emits", op.emits.join(" "));
|
|
175
|
+
if (annotationsOf(op, "simulate").length) add("dry run", "allowed: tick it and nothing is written");
|
|
176
|
+
doc.appendChild(dl);
|
|
177
|
+
|
|
178
|
+
var request = { ops: [{ id: 1, op: op.name, args: argSkeleton(op) }] };
|
|
179
|
+
var shape = defaultShape(op.returns);
|
|
180
|
+
if (shape) request.ops[0].shape = shape;
|
|
181
|
+
if (op.kind === "command") request.ops[0].key = "explorer-" + Math.random().toString(36).slice(2).padEnd(16, "0").slice(0, 16);
|
|
182
|
+
$("#req").value = JSON.stringify(request, null, 2);
|
|
183
|
+
$("#simulate").disabled = op.kind !== "command";
|
|
184
|
+
$("#live").disabled = op.kind !== "query";
|
|
185
|
+
}
|
|
186
|
+
function render(frames, ms) {
|
|
187
|
+
var out = document.getElementById("out");
|
|
188
|
+
out.innerHTML = "";
|
|
189
|
+
frames.forEach(function (f) {
|
|
190
|
+
var kind = "error" in f ? "error" : "ok" in f ? "ok" : "at" in f ? "at" : "patch" in f ? "patch" : "item" in f ? "item" : "data" in f ? "data" : "fin";
|
|
191
|
+
var box = el("div", "frame " + kind);
|
|
192
|
+
var cost = f.meta && f.meta.cost !== undefined ? " cost " + f.meta.cost : "";
|
|
193
|
+
box.appendChild(el("span", "tag", kind + (f.id !== undefined ? " · op " + f.id : "") + cost));
|
|
194
|
+
box.appendChild(el("pre", null, JSON.stringify(f, null, 2)));
|
|
195
|
+
out.appendChild(box);
|
|
196
|
+
});
|
|
197
|
+
$("#status").textContent = frames.length + (frames.length === 1 ? " frame" : " frames") + " in " + ms + " ms";
|
|
198
|
+
}
|
|
199
|
+
async function send() {
|
|
200
|
+
var button = $("#send");
|
|
201
|
+
button.disabled = true;
|
|
202
|
+
$("#status").textContent = "sending...";
|
|
203
|
+
var started = Date.now();
|
|
204
|
+
try {
|
|
205
|
+
var body = JSON.parse($("#req").value);
|
|
206
|
+
if (current && current.kind === "command" && $("#simulate").checked) body.ops.forEach(function (o) { o.simulate = true; });
|
|
207
|
+
if (current && current.kind === "query" && $("#live").checked) body.ops.forEach(function (o) { o.live = true; });
|
|
208
|
+
var headers = { "content-type": "application/rayfold+json" };
|
|
209
|
+
if ($("#token").value) headers.authorization = "Bearer " + $("#token").value;
|
|
210
|
+
var res = await fetch(CONFIG.endpoint, { method: "POST", headers: headers, body: JSON.stringify(body) });
|
|
211
|
+
var frames = [];
|
|
212
|
+
var reader = res.body.getReader();
|
|
213
|
+
var decoder = new TextDecoder();
|
|
214
|
+
var buffered = "";
|
|
215
|
+
for (;;) {
|
|
216
|
+
var chunk = await reader.read();
|
|
217
|
+
if (chunk.done) break;
|
|
218
|
+
buffered += decoder.decode(chunk.value, { stream: true });
|
|
219
|
+
var cut;
|
|
220
|
+
while ((cut = buffered.indexOf(String.fromCharCode(10))) >= 0) {
|
|
221
|
+
var line = buffered.slice(0, cut);
|
|
222
|
+
buffered = buffered.slice(cut + 1);
|
|
223
|
+
if (line.trim()) frames.push(JSON.parse(line));
|
|
224
|
+
render(frames, Date.now() - started);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (buffered.trim()) frames.push(JSON.parse(buffered));
|
|
228
|
+
render(frames, Date.now() - started);
|
|
229
|
+
} catch (e) {
|
|
230
|
+
$("#out").innerHTML = "";
|
|
231
|
+
var box = el("div", "frame error");
|
|
232
|
+
box.appendChild(el("span", "tag", "explorer"));
|
|
233
|
+
box.appendChild(el("pre", null, String(e && e.message ? e.message : e)));
|
|
234
|
+
$("#out").appendChild(box);
|
|
235
|
+
$("#status").textContent = "";
|
|
236
|
+
} finally {
|
|
237
|
+
button.disabled = false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async function start() {
|
|
241
|
+
try {
|
|
242
|
+
manifest = await (await fetch(CONFIG.endpoint + "/manifest")).json();
|
|
243
|
+
} catch (e) {
|
|
244
|
+
text(document.getElementById("about"), "no manifest at " + CONFIG.endpoint + "/manifest");
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
text(document.getElementById("about"), "rayfold " + manifest.rayfold + " · schema " + String(manifest.schemaHash).slice(0, 12) + " · extensions " + (manifest.extensions || []).join(", "));
|
|
248
|
+
var ops = Object.keys(manifest.schema.ops).map(function (n) { return manifest.schema.ops[n]; });
|
|
249
|
+
var aside = document.getElementById("ops");
|
|
250
|
+
["query", "command", "stream"].forEach(function (kind) {
|
|
251
|
+
var mine = ops.filter(function (o) { return o.kind === kind; }).sort(function (a, b) { return a.name < b.name ? -1 : 1; });
|
|
252
|
+
if (!mine.length) return;
|
|
253
|
+
aside.appendChild(el("h2", null, kind === "query" ? "queries" : kind === "command" ? "commands" : "streams"));
|
|
254
|
+
mine.forEach(function (op) {
|
|
255
|
+
var b = el("button", "op");
|
|
256
|
+
b.dataset.op = op.name;
|
|
257
|
+
b.appendChild(document.createTextNode(op.name));
|
|
258
|
+
b.appendChild(el("span", "kind", typeName(op.returns)));
|
|
259
|
+
b.onclick = function () { show(op); };
|
|
260
|
+
aside.appendChild(b);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
if (ops.length) show(ops.filter(function (o) { return o.kind === "query"; })[0] || ops[0]);
|
|
264
|
+
}
|
|
265
|
+
$("#send").onclick = send;
|
|
266
|
+
$("#req").addEventListener("keydown", function (e) { if (e.ctrlKey && e.key === "Enter") send(); });
|
|
267
|
+
start();
|
|
268
|
+
</script>
|
|
269
|
+
</body>
|
|
270
|
+
</html>`;
|
|
271
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA8BA,MAAM,QAAQ,GAAG,CAAC,IAAI,GAAoB,EAAE,EAA8D,EAAE;IAC1G,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,UAAU,CAAC;IAC7C,OAAO;QACL,QAAQ;QACR,IAAI,EAAE,IAAI,CAAC,IAAI,IAAI,GAAG,QAAQ,WAAW;QACzC,MAAM,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,SAAS,EAAE;KACrD,CAAC;AACJ,CAAC,CAAC;AAEF,iEAAiE;AACjE,MAAM,UAAU,YAAY,CAAC,IAAI,GAAoB,EAAE;IACrD,OAAO,IAAI,CAAC,OAAO,CAAC,6BAA6B,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;AACrH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAI,GAAoB,EAAE;IAC9D,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAClB,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3C,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,GAAG,IAAI,GAAG;YAAE,OAAO,KAAK,CAAC;QACrD,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE,CAAC;YAClD,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;YACjD,OAAO,IAAI,CAAC;QACd,CAAC;QACD,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,0BAA0B,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC,CAAC;QAChG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAClD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC;AAED,gHAAgH;AAChH,8GAA8G;AAC9G,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QA2Of,CAAC","sourcesContent":["/**\n * The Rayfold explorer: the page a Rayfold server serves next to its endpoint, the counterpart of Swagger UI and\n * GraphiQL. It lists every operation the manifest declares with its arguments, result, cost and the policies that\n * guard it; helps write a shape; sends a batch and shows the frames as they arrive, with their cost; and dry-runs a\n * command that allows `@simulate`, so trying one changes nothing.\n *\n * It talks to the endpoint over HTTP like any other client, so it needs no access to the server object and adds no\n * dependency to it. Mount it where you like:\n *\n * const explorer = createExplorerHandler({ endpoint: \"/rayfold\", title: \"Acme API\" });\n * createServer((req, res) => { if (explorer(req, res)) return; ... });\n *\n * Nothing is served unless you mount it, so a production deployment exposes it only on purpose.\n */\nimport type { IncomingMessage, ServerResponse } from \"node:http\";\n\nexport interface ExplorerOptions {\n /** Where the Rayfold endpoint is mounted. Default `/rayfold`. */\n endpoint?: string;\n /** Where this page is served. Default `<endpoint>/explorer`. */\n path?: string;\n /** Shown in the header, to tell one service from another. */\n title?: string;\n}\n\ninterface ExplorerConfig {\n endpoint: string;\n title: string;\n}\n\nconst settings = (opts: ExplorerOptions = {}): { endpoint: string; path: string; config: ExplorerConfig } => {\n const endpoint = opts.endpoint ?? \"/rayfold\";\n return {\n endpoint,\n path: opts.path ?? `${endpoint}/explorer`,\n config: { endpoint, title: opts.title ?? \"Rayfold\" },\n };\n};\n\n/** The page itself, for serving from a framework of your own. */\nexport function explorerHtml(opts: ExplorerOptions = {}): string {\n return PAGE.replace(\"__RAYFOLD_EXPLORER_CONFIG__\", JSON.stringify(settings(opts).config).replace(/</g, \"\\\\u003c\"));\n}\n\n/**\n * A Node handler that serves the page. It answers only its own path and returns false for anything else, so it can\n * sit in front of the rest of an application.\n */\nexport function createExplorerHandler(opts: ExplorerOptions = {}): (req: IncomingMessage, res: ServerResponse) => boolean {\n const { path } = settings(opts);\n const html = explorerHtml(opts);\n return (req, res) => {\n const url = (req.url ?? \"/\").split(\"?\")[0];\n if (url !== path && url !== `${path}/`) return false;\n if (req.method !== \"GET\" && req.method !== \"HEAD\") {\n res.writeHead(405, { allow: \"GET, HEAD\" }).end();\n return true;\n }\n res.writeHead(200, { \"content-type\": \"text/html; charset=utf-8\", \"cache-control\": \"no-store\" });\n res.end(req.method === \"HEAD\" ? undefined : html);\n return true;\n };\n}\n\n// The page is one self-contained document: no fonts, scripts or styles from anywhere else, so it works behind a\n// strict content-security-policy and offline. Client code avoids template literals, since this is one itself.\nconst PAGE = String.raw`<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n<title>Rayfold explorer</title>\n<style>\n:root { color-scheme: light dark; --ink: #16202b; --muted: #5b6875; --rule: #d6dde5; --surface: #fff; --ground: #f3f6f9; --accent: #2a78d6; --ok: #0a7d3f; --bad: #b3261e; }\n@media (prefers-color-scheme: dark) { :root { --ink: #e8eef5; --muted: #a2aebc; --rule: #2b333d; --surface: #171c22; --ground: #0f1318; --accent: #5aa2f0; --ok: #4cc073; --bad: #f2836f; } }\n* { box-sizing: border-box; }\nbody { margin: 0; background: var(--ground); color: var(--ink); font: 14px/1.55 ui-sans-serif, system-ui, \"Segoe UI\", sans-serif; }\nheader { display: flex; gap: 12px; align-items: baseline; padding: 12px 16px; border-bottom: 1px solid var(--rule); background: var(--surface); }\nheader h1 { font-size: 15px; margin: 0; font-weight: 600; }\nheader .meta { color: var(--muted); font: 12px/1.4 ui-monospace, monospace; }\nmain { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 0; height: calc(100vh - 49px); }\n@media (max-width: 800px) { main { grid-template-columns: 1fr; height: auto; } }\naside { border-right: 1px solid var(--rule); overflow: auto; background: var(--surface); }\naside h2 { font: 600 11px/1 ui-monospace, monospace; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 14px 12px 6px; }\nbutton.op { display: block; width: 100%; text-align: left; border: 0; background: none; color: inherit; font: inherit; padding: 6px 12px; cursor: pointer; }\nbutton.op:hover, button.op.on { background: var(--ground); }\nbutton.op .kind { font: 11px ui-monospace, monospace; color: var(--muted); margin-left: 6px; }\nsection.work { display: grid; grid-template-rows: auto auto 1fr; min-width: 0; }\n.doc { padding: 12px 16px; border-bottom: 1px solid var(--rule); background: var(--surface); }\n.doc h3 { margin: 0 0 4px; font-size: 15px; }\n.doc dl { display: grid; grid-template-columns: 92px minmax(0, 1fr); gap: 2px 10px; margin: 6px 0 0; font-size: 13px; }\n.doc dt { color: var(--muted); font: 12px ui-monospace, monospace; }\n.doc dd { margin: 0; font-family: ui-monospace, monospace; overflow-wrap: anywhere; }\n.editor { display: grid; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--rule); }\ntextarea, input[type=text] { width: 100%; font: 12.5px/1.5 ui-monospace, monospace; color: inherit; background: var(--surface); border: 1px solid var(--rule); border-radius: 6px; padding: 8px; }\ntextarea { min-height: 120px; resize: vertical; }\n.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }\n.row label { font-size: 12.5px; color: var(--muted); display: flex; gap: 5px; align-items: center; }\nbutton.go { background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 7px 14px; font: 600 13px/1 inherit; cursor: pointer; }\nbutton.go:disabled { opacity: .55; cursor: default; }\n.out { overflow: auto; padding: 12px 16px; display: grid; gap: 8px; align-content: start; }\n.frame { border: 1px solid var(--rule); border-left-width: 3px; border-radius: 6px; background: var(--surface); padding: 8px 10px; }\n.frame pre { margin: 4px 0 0; font: 12px/1.5 ui-monospace, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }\n.frame .tag { font: 600 11px ui-monospace, monospace; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }\n.frame.ok, .frame.data { border-left-color: var(--ok); }\n.frame.error { border-left-color: var(--bad); }\n.frame.patch, .frame.item, .frame.at { border-left-color: var(--accent); }\n.hint { color: var(--muted); font-size: 12.5px; }\ncode { font-family: ui-monospace, monospace; }\n</style>\n</head>\n<body>\n<script type=\"application/json\" id=\"config\">__RAYFOLD_EXPLORER_CONFIG__</script>\n<header>\n <h1 id=\"title\">Rayfold</h1>\n <span class=\"meta\" id=\"about\">reading the manifest...</span>\n</header>\n<main>\n <aside id=\"ops\"></aside>\n <section class=\"work\">\n <div class=\"doc\" id=\"doc\"><p class=\"hint\">Pick an operation on the left. The explorer reads everything it shows from the server's manifest.</p></div>\n <div class=\"editor\">\n <textarea id=\"req\" spellcheck=\"false\"></textarea>\n <div class=\"row\">\n <button class=\"go\" id=\"send\">Send</button>\n <label><input type=\"checkbox\" id=\"simulate\"> dry run</label>\n <label><input type=\"checkbox\" id=\"live\"> live</label>\n <label>auth <input type=\"text\" id=\"token\" placeholder=\"bearer token\" style=\"width:170px\"></label>\n <span class=\"hint\" id=\"status\"></span>\n </div>\n <p class=\"hint\">A batch can hold several ops, and a later one can use an earlier result: <code>{\"$ref\": \"1.id\"}</code> in its args. Ctrl+Enter sends.</p>\n </div>\n <div class=\"out\" id=\"out\"></div>\n </section>\n</main>\n<script>\nvar CONFIG = JSON.parse(document.getElementById(\"config\").textContent);\nvar $ = function (sel) { return document.querySelector(sel); };\nvar manifest = null;\nvar current = null;\n\ndocument.getElementById(\"title\").textContent = CONFIG.title;\n\nfunction text(node, s) { node.textContent = s; return node; }\nfunction el(tag, cls, s) { var n = document.createElement(tag); if (cls) n.className = cls; if (s !== undefined) n.textContent = s; return n; }\nfunction typeName(t) {\n if (!t) return \"?\";\n if (t.kind === \"list\") return \"[\" + typeName(t.of) + \"]\" + (t.nullable ? \"?\" : \"\");\n var args = t.args && t.args.length ? \"<\" + t.args.map(typeName).join(\", \") + \">\" : \"\";\n return t.name + args + (t.nullable ? \"?\" : \"\");\n}\nfunction annotationsOf(def, name) { return (def.annotations || []).filter(function (a) { return a.name === name; }); }\nfunction costOf(def) {\n var c = annotationsOf(def, \"cost\")[0];\n if (!c) return \"default\";\n return Object.keys(c.args).map(function (k) { return k + \" \" + JSON.stringify(c.args[k]); }).join(\", \");\n}\nfunction policiesOf(def) {\n var names = [];\n annotationsOf(def, \"allow\").forEach(function () { names.push(\"allow\"); });\n annotationsOf(def, \"deny\").forEach(function () { names.push(\"deny\"); });\n return names.length ? names.join(\", \") : \"none on the operation\";\n}\n/** A shape that selects the scalar fields of what an op returns: a starting point, not a rule. */\nfunction defaultShape(typeRef) {\n var base = typeRef;\n while (base && base.kind === \"list\") base = base.of;\n if (!base) return \"\";\n var def = manifest.schema.types[base.name];\n if (!def || !def.fields) return \"\";\n if (base.name === \"Page\" && base.args && base.args.length) {\n return \"{ total hasMore cursor items \" + (defaultShape(base.args[0]) || \"{ id }\") + \" }\";\n }\n var scalars = def.fields.filter(function (f) {\n var t = f.type.kind === \"list\" ? f.type.of : f.type;\n var sub = manifest.schema.types[t.name];\n return !f.args.length && sub && (sub.kind === \"scalar\" || sub.kind === \"enum\");\n });\n if (!scalars.length) return \"\";\n return \"{ \" + scalars.map(function (f) { return f.name; }).join(\" \") + \" }\";\n}\nfunction argSkeleton(def) {\n var args = {};\n def.args.forEach(function (a) {\n if (a.default !== undefined) { args[a.name] = a.default; return; }\n var t = a.type.kind === \"list\" ? a.type.of : a.type;\n var sub = manifest.schema.types[t.name];\n args[a.name] = a.type.kind === \"list\" ? [] : sub && sub.kind === \"enum\" ? sub.values[0].name : t.name === \"Int\" || t.name === \"Float\" || t.name === \"Long\" ? 0 : t.name === \"Boolean\" ? false : \"\";\n });\n return args;\n}\nfunction show(op) {\n current = op;\n Array.prototype.forEach.call(document.querySelectorAll(\"button.op\"), function (b) { b.classList.toggle(\"on\", b.dataset.op === op.name); });\n var doc = document.getElementById(\"doc\");\n doc.innerHTML = \"\";\n doc.appendChild(el(\"h3\", null, op.name + \"(\" + op.args.map(function (a) { return a.name + \": \" + typeName(a.type); }).join(\", \") + \")\"));\n if (op.description) doc.appendChild(el(\"p\", \"hint\", op.description));\n var dl = el(\"dl\");\n function add(k, v) { dl.appendChild(el(\"dt\", null, k)); dl.appendChild(el(\"dd\", null, v)); }\n add(\"kind\", op.kind);\n add(\"returns\", typeName(op.returns));\n add(\"cost\", costOf(op));\n add(\"policies\", policiesOf(op));\n if (op.throws && op.throws.length) add(\"throws\", op.throws.join(\" | \"));\n if (op.emits && op.emits.length) add(\"emits\", op.emits.join(\" \"));\n if (annotationsOf(op, \"simulate\").length) add(\"dry run\", \"allowed: tick it and nothing is written\");\n doc.appendChild(dl);\n\n var request = { ops: [{ id: 1, op: op.name, args: argSkeleton(op) }] };\n var shape = defaultShape(op.returns);\n if (shape) request.ops[0].shape = shape;\n if (op.kind === \"command\") request.ops[0].key = \"explorer-\" + Math.random().toString(36).slice(2).padEnd(16, \"0\").slice(0, 16);\n $(\"#req\").value = JSON.stringify(request, null, 2);\n $(\"#simulate\").disabled = op.kind !== \"command\";\n $(\"#live\").disabled = op.kind !== \"query\";\n}\nfunction render(frames, ms) {\n var out = document.getElementById(\"out\");\n out.innerHTML = \"\";\n frames.forEach(function (f) {\n var kind = \"error\" in f ? \"error\" : \"ok\" in f ? \"ok\" : \"at\" in f ? \"at\" : \"patch\" in f ? \"patch\" : \"item\" in f ? \"item\" : \"data\" in f ? \"data\" : \"fin\";\n var box = el(\"div\", \"frame \" + kind);\n var cost = f.meta && f.meta.cost !== undefined ? \" cost \" + f.meta.cost : \"\";\n box.appendChild(el(\"span\", \"tag\", kind + (f.id !== undefined ? \" · op \" + f.id : \"\") + cost));\n box.appendChild(el(\"pre\", null, JSON.stringify(f, null, 2)));\n out.appendChild(box);\n });\n $(\"#status\").textContent = frames.length + (frames.length === 1 ? \" frame\" : \" frames\") + \" in \" + ms + \" ms\";\n}\nasync function send() {\n var button = $(\"#send\");\n button.disabled = true;\n $(\"#status\").textContent = \"sending...\";\n var started = Date.now();\n try {\n var body = JSON.parse($(\"#req\").value);\n if (current && current.kind === \"command\" && $(\"#simulate\").checked) body.ops.forEach(function (o) { o.simulate = true; });\n if (current && current.kind === \"query\" && $(\"#live\").checked) body.ops.forEach(function (o) { o.live = true; });\n var headers = { \"content-type\": \"application/rayfold+json\" };\n if ($(\"#token\").value) headers.authorization = \"Bearer \" + $(\"#token\").value;\n var res = await fetch(CONFIG.endpoint, { method: \"POST\", headers: headers, body: JSON.stringify(body) });\n var frames = [];\n var reader = res.body.getReader();\n var decoder = new TextDecoder();\n var buffered = \"\";\n for (;;) {\n var chunk = await reader.read();\n if (chunk.done) break;\n buffered += decoder.decode(chunk.value, { stream: true });\n var cut;\n while ((cut = buffered.indexOf(String.fromCharCode(10))) >= 0) {\n var line = buffered.slice(0, cut);\n buffered = buffered.slice(cut + 1);\n if (line.trim()) frames.push(JSON.parse(line));\n render(frames, Date.now() - started);\n }\n }\n if (buffered.trim()) frames.push(JSON.parse(buffered));\n render(frames, Date.now() - started);\n } catch (e) {\n $(\"#out\").innerHTML = \"\";\n var box = el(\"div\", \"frame error\");\n box.appendChild(el(\"span\", \"tag\", \"explorer\"));\n box.appendChild(el(\"pre\", null, String(e && e.message ? e.message : e)));\n $(\"#out\").appendChild(box);\n $(\"#status\").textContent = \"\";\n } finally {\n button.disabled = false;\n }\n}\nasync function start() {\n try {\n manifest = await (await fetch(CONFIG.endpoint + \"/manifest\")).json();\n } catch (e) {\n text(document.getElementById(\"about\"), \"no manifest at \" + CONFIG.endpoint + \"/manifest\");\n return;\n }\n text(document.getElementById(\"about\"), \"rayfold \" + manifest.rayfold + \" · schema \" + String(manifest.schemaHash).slice(0, 12) + \" · extensions \" + (manifest.extensions || []).join(\", \"));\n var ops = Object.keys(manifest.schema.ops).map(function (n) { return manifest.schema.ops[n]; });\n var aside = document.getElementById(\"ops\");\n [\"query\", \"command\", \"stream\"].forEach(function (kind) {\n var mine = ops.filter(function (o) { return o.kind === kind; }).sort(function (a, b) { return a.name < b.name ? -1 : 1; });\n if (!mine.length) return;\n aside.appendChild(el(\"h2\", null, kind === \"query\" ? \"queries\" : kind === \"command\" ? \"commands\" : \"streams\"));\n mine.forEach(function (op) {\n var b = el(\"button\", \"op\");\n b.dataset.op = op.name;\n b.appendChild(document.createTextNode(op.name));\n b.appendChild(el(\"span\", \"kind\", typeName(op.returns)));\n b.onclick = function () { show(op); };\n aside.appendChild(b);\n });\n });\n if (ops.length) show(ops.filter(function (o) { return o.kind === \"query\"; })[0] || ops[0]);\n}\n$(\"#send\").onclick = send;\n$(\"#req\").addEventListener(\"keydown\", function (e) { if (e.ctrlKey && e.key === \"Enter\") send(); });\nstart();\n</script>\n</body>\n</html>`;\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rayfold/explorer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Rayfold explorer: a page served next to the endpoint that lists every operation from the manifest, writes shapes, sends batches and dry-runs commands.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rayfold",
|
|
7
|
+
"api",
|
|
8
|
+
"protocol",
|
|
9
|
+
"explorer",
|
|
10
|
+
"playground",
|
|
11
|
+
"documentation"
|
|
12
|
+
],
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"homepage": "https://eddyboutros.github.io/rayfold/",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/eddyboutros/rayfold/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/eddyboutros/rayfold.git",
|
|
21
|
+
"directory": "packages/explorer"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
},
|
|
28
|
+
"main": "./index.js",
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"default": "./index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|