@reatomized/convex 0.0.0-stage → 0.1.1
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/README.md +110 -2
- package/dist/browser/logging.d.ts +39 -0
- package/dist/browser/logging.js +117 -0
- package/dist/browser/reatom/auth.d.ts +75 -0
- package/dist/browser/reatom/auth.js +329 -0
- package/dist/browser/reatom/client.d.ts +348 -0
- package/dist/browser/reatom/client.js +1138 -0
- package/dist/browser/reatom/connection_hooks.d.ts +11 -0
- package/dist/browser/reatom/connection_hooks.js +15 -0
- package/dist/browser/reatom/errors.d.ts +3 -0
- package/dist/browser/reatom/errors.js +8 -0
- package/dist/browser/reatom/index.d.ts +6 -0
- package/dist/browser/reatom/index.js +2 -0
- package/dist/browser/reatom/optimistic.d.ts +44 -0
- package/dist/browser/reatom/optimistic.js +70 -0
- package/dist/browser/reatom/paginated.d.ts +56 -0
- package/dist/browser/reatom/paginated.js +162 -0
- package/dist/browser/reatom/socket.d.ts +52 -0
- package/dist/browser/reatom/socket.js +523 -0
- package/dist/browser/sync/function_result.d.ts +21 -0
- package/dist/browser/sync/function_result.js +1 -0
- package/dist/browser/sync/metrics.d.ts +9 -0
- package/dist/browser/sync/metrics.js +43 -0
- package/dist/browser/sync/pagination.d.ts +22 -0
- package/dist/browser/sync/pagination.js +20 -0
- package/dist/browser/sync/protocol.d.ts +206 -0
- package/dist/browser/sync/protocol.js +75 -0
- package/dist/browser/sync/session.d.ts +1 -0
- package/dist/browser/sync/session.js +10 -0
- package/dist/browser/sync/udf_path_utils.d.ts +30 -0
- package/dist/browser/sync/udf_path_utils.js +38 -0
- package/dist/common/index.d.ts +11 -0
- package/dist/common/index.js +52 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/vendor/jwt-decode/LICENSE +21 -0
- package/dist/vendor/jwt-decode/index.d.ts +23 -0
- package/dist/vendor/jwt-decode/index.js +57 -0
- package/dist/vendor/long.d.ts +16 -0
- package/dist/vendor/long.js +297 -0
- package/package.json +59 -4
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 2026 Convex, Inc.
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,111 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @reatomized/convex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A [Convex](https://convex.dev) sync client whose state lives in
|
|
4
|
+
[Reatom](https://github.com/reatom/reatom) v1001 atoms: queries, paginated
|
|
5
|
+
queries, the connection and auth. It replaces `convex/browser`'s WebSocket
|
|
6
|
+
client and nothing else, so keep the `convex` package for your functions, the
|
|
7
|
+
CLI and codegen.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @reatomized/convex convex @reatom/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`convex` and `@reatom/core` are peer dependencies. The client takes function
|
|
14
|
+
references and `ConvexError` from your `convex`, and its atoms live in your
|
|
15
|
+
app's Reatom context. It speaks the sync protocol of convex 1.48.0.
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { reatomConvexClient } from "@reatomized/convex";
|
|
21
|
+
import { atom, computed } from "@reatom/core";
|
|
22
|
+
import { api } from "../convex/_generated/api";
|
|
23
|
+
import type { Id } from "../convex/_generated/dataModel";
|
|
24
|
+
|
|
25
|
+
const convex = reatomConvexClient(import.meta.env.VITE_CONVEX_URL);
|
|
26
|
+
|
|
27
|
+
const noteId = atom<Id<"notes"> | null>(null);
|
|
28
|
+
const note = computed(() => {
|
|
29
|
+
const id = noteId();
|
|
30
|
+
return id ? convex.query(api.notes.get, { id }).data() : undefined;
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- `convex.query(ref, args)` returns the entry for that function and arguments,
|
|
35
|
+
the same object for equal arguments. Its `result`, `data`, `error` and
|
|
36
|
+
`status` are atoms. Reading one in a subscribed graph subscribes the query on
|
|
37
|
+
the server; once nothing reads it, the query is unsubscribed.
|
|
38
|
+
- Query set changes made in one microtask go out as one `ModifyQuerySet`. In the
|
|
39
|
+
example above, changing `noteId` sends one message that adds the new query and
|
|
40
|
+
removes the old one.
|
|
41
|
+
- `convex.paginatedQuery(ref, args, { initialNumItems })` has `results`,
|
|
42
|
+
`status` and `error` atoms and a `loadMore` action. Equal arguments share
|
|
43
|
+
their pages; pass `id` for a separate page list.
|
|
44
|
+
- `convex.mutation(ref, args, { optimisticUpdate })` and
|
|
45
|
+
`convex.action(ref, args)` return promises and run as Reatom actions. A failed
|
|
46
|
+
call rejects with a `ConvexError` from `convex/values` when the function threw
|
|
47
|
+
one.
|
|
48
|
+
- `connectionState`, `isAuthenticated` (`null` while a token waits for the
|
|
49
|
+
server), `isRefreshingAuth`, `authClaims` and `socketState` are atoms.
|
|
50
|
+
`setAuth`, `clearAuth` and `close` are actions.
|
|
51
|
+
|
|
52
|
+
State is per Reatom context: call the client inside a frame, and each context
|
|
53
|
+
gets its own connection. The socket opens on first use in a context.
|
|
54
|
+
|
|
55
|
+
There are no React hooks. Read the atoms through your Reatom view binding, such
|
|
56
|
+
as `@reatom/react` or `@reatom/jsx`.
|
|
57
|
+
|
|
58
|
+
## Node
|
|
59
|
+
|
|
60
|
+
With `@reatom/core@1001.3.0` loaded, Node never exits on its own, so a script
|
|
61
|
+
that uses this client hangs after its work is done. The fix is on Reatom's
|
|
62
|
+
`v1001` branch; install that build in projects with such scripts (npm 12
|
|
63
|
+
installs from a URL only with `--allow-remote`):
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
npm install --allow-remote=root @reatom/core@https://pkg.pr.new/@reatom/core@5f42184
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Node 22 and later have a global `WebSocket`. On Node 20, pass one, such as the
|
|
70
|
+
`ws` package's, as `webSocketConstructor`.
|
|
71
|
+
|
|
72
|
+
## Differences from convex/browser 1.48.0
|
|
73
|
+
|
|
74
|
+
- Query set changes are batched per microtask instead of sent one per subscribe
|
|
75
|
+
and unsubscribe.
|
|
76
|
+
- A transition updates the queries it names. Optimistic updates replay only
|
|
77
|
+
while some are pending. convex/browser rebuilds and diffs every result on each
|
|
78
|
+
transition, which makes mounting N queries cost O(N²).
|
|
79
|
+
- `clearAuth` stops the auth flow. In convex/browser a refetch scheduled before
|
|
80
|
+
`clearAuth` still runs and can authenticate the old session again.
|
|
81
|
+
- When a token fetch returns `null` for a signed-in client, the client sends the
|
|
82
|
+
server `Authenticate` without a token. convex/browser drops that message while
|
|
83
|
+
the socket is paused for the fetch, so the server keeps the old identity.
|
|
84
|
+
- With `expectAuth`, a socket that opened before the first token sends `Connect`
|
|
85
|
+
once the token arrives. convex/browser never sends it, and `hasEverConnected`
|
|
86
|
+
stays false.
|
|
87
|
+
- A failed page of a paginated query lands in its `error` atom. In
|
|
88
|
+
convex/browser it throws out of the WebSocket message handler.
|
|
89
|
+
- Server results stay visible across a reconnect until the new connection
|
|
90
|
+
answers.
|
|
91
|
+
- Minified, the client is 13.8 KiB gzip, or 19.0 KiB with the parts of
|
|
92
|
+
`convex/values` and `convex/server` it uses, against 15.8 KiB for
|
|
93
|
+
convex/browser's `BaseConvexClient`. The Reatom functions it uses add 4.6 KiB,
|
|
94
|
+
shared with the rest of the app.
|
|
95
|
+
|
|
96
|
+
With 1000 components each running its own query in Chromium, mounting takes 9.24
|
|
97
|
+
ms against 65.5 ms for convex/react, unmounting 1.99 ms against 9.87 ms.
|
|
98
|
+
[CONTRIBUTING.md](https://github.com/reatomized/convex/blob/reatom/CONTRIBUTING.md#performance)
|
|
99
|
+
has the full table and how it was measured.
|
|
100
|
+
|
|
101
|
+
## Development
|
|
102
|
+
|
|
103
|
+
The repository is a fork of
|
|
104
|
+
[convex/convex-js](https://github.com/convex/convex-js) cut down to this client
|
|
105
|
+
and the upstream sync helpers it reuses. Server and value utilities are imported
|
|
106
|
+
directly from the official `convex` peer in both source and builds.
|
|
107
|
+
[CONTRIBUTING.md](https://github.com/reatomized/convex/blob/reatom/CONTRIBUTING.md)
|
|
108
|
+
covers the layout, the parity tests against upstream's client, syncing with
|
|
109
|
+
upstream, and releases.
|
|
110
|
+
|
|
111
|
+
Apache-2.0, like convex-js.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ConvexError, Value } from "convex/values";
|
|
2
|
+
import { FunctionFailure } from "./sync/function_result.js";
|
|
3
|
+
export type UdfType = "query" | "mutation" | "action" | "any";
|
|
4
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
5
|
+
/**
|
|
6
|
+
* A logger that can be used to log messages. By default, this is a wrapper
|
|
7
|
+
* around `console`, but can be configured to not log at all or to log somewhere
|
|
8
|
+
* else.
|
|
9
|
+
*/
|
|
10
|
+
export type Logger = {
|
|
11
|
+
logVerbose(...args: any[]): void;
|
|
12
|
+
log(...args: any[]): void;
|
|
13
|
+
warn(...args: any[]): void;
|
|
14
|
+
error(...args: any[]): void;
|
|
15
|
+
};
|
|
16
|
+
export declare class DefaultLogger implements Logger {
|
|
17
|
+
private _onLogLineFuncs;
|
|
18
|
+
private _verbose;
|
|
19
|
+
constructor(options: {
|
|
20
|
+
verbose: boolean;
|
|
21
|
+
});
|
|
22
|
+
addLogLineListener(func: (level: LogLevel, ...args: any[]) => void): () => void;
|
|
23
|
+
logVerbose(...args: any[]): void;
|
|
24
|
+
log(...args: any[]): void;
|
|
25
|
+
warn(...args: any[]): void;
|
|
26
|
+
error(...args: any[]): void;
|
|
27
|
+
}
|
|
28
|
+
export declare function instantiateDefaultLogger(options: {
|
|
29
|
+
verbose: boolean;
|
|
30
|
+
}): Logger;
|
|
31
|
+
export declare function instantiateNoopLogger(options: {
|
|
32
|
+
verbose: boolean;
|
|
33
|
+
}): Logger;
|
|
34
|
+
export declare function logForFunction(logger: Logger, type: "info" | "error", source: UdfType, udfPath: string, message: string | {
|
|
35
|
+
errorData: Value;
|
|
36
|
+
}): void;
|
|
37
|
+
export declare function logFatalError(logger: Logger, message: string): Error;
|
|
38
|
+
export declare function createHybridErrorStacktrace(source: UdfType, udfPath: string, result: FunctionFailure): string;
|
|
39
|
+
export declare function forwardData(result: FunctionFailure, error: ConvexError<string>): ConvexError<string>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// This is blue #9 from https://www.radix-ui.com/docs/colors/palette-composition/the-scales
|
|
2
|
+
// It must look good in both light and dark mode.
|
|
3
|
+
const INFO_COLOR = "color:rgb(0, 145, 255)";
|
|
4
|
+
function prefix_for_source(source) {
|
|
5
|
+
switch (source) {
|
|
6
|
+
case "query":
|
|
7
|
+
return "Q";
|
|
8
|
+
case "mutation":
|
|
9
|
+
return "M";
|
|
10
|
+
case "action":
|
|
11
|
+
return "A";
|
|
12
|
+
case "any":
|
|
13
|
+
return "?";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class DefaultLogger {
|
|
17
|
+
_onLogLineFuncs;
|
|
18
|
+
_verbose;
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this._onLogLineFuncs = {};
|
|
21
|
+
this._verbose = options.verbose;
|
|
22
|
+
}
|
|
23
|
+
addLogLineListener(func) {
|
|
24
|
+
let id = Math.random().toString(36).substring(2, 15);
|
|
25
|
+
for (let i = 0; i < 10; i++) {
|
|
26
|
+
if (this._onLogLineFuncs[id] === undefined) {
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
id = Math.random().toString(36).substring(2, 15);
|
|
30
|
+
}
|
|
31
|
+
this._onLogLineFuncs[id] = func;
|
|
32
|
+
return () => {
|
|
33
|
+
delete this._onLogLineFuncs[id];
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
logVerbose(...args) {
|
|
37
|
+
if (this._verbose) {
|
|
38
|
+
for (const func of Object.values(this._onLogLineFuncs)) {
|
|
39
|
+
func("debug", `${new Date().toISOString()}`, ...args);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
log(...args) {
|
|
44
|
+
for (const func of Object.values(this._onLogLineFuncs)) {
|
|
45
|
+
func("info", ...args);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
warn(...args) {
|
|
49
|
+
for (const func of Object.values(this._onLogLineFuncs)) {
|
|
50
|
+
func("warn", ...args);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
error(...args) {
|
|
54
|
+
for (const func of Object.values(this._onLogLineFuncs)) {
|
|
55
|
+
func("error", ...args);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function instantiateDefaultLogger(options) {
|
|
60
|
+
const logger = new DefaultLogger(options);
|
|
61
|
+
logger.addLogLineListener((level, ...args) => {
|
|
62
|
+
switch (level) {
|
|
63
|
+
case "debug":
|
|
64
|
+
console.debug(...args);
|
|
65
|
+
break;
|
|
66
|
+
case "info":
|
|
67
|
+
console.log(...args);
|
|
68
|
+
break;
|
|
69
|
+
case "warn":
|
|
70
|
+
console.warn(...args);
|
|
71
|
+
break;
|
|
72
|
+
case "error":
|
|
73
|
+
console.error(...args);
|
|
74
|
+
break;
|
|
75
|
+
default: {
|
|
76
|
+
level;
|
|
77
|
+
console.log(...args);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return logger;
|
|
82
|
+
}
|
|
83
|
+
export function instantiateNoopLogger(options) {
|
|
84
|
+
return new DefaultLogger(options);
|
|
85
|
+
}
|
|
86
|
+
export function logForFunction(logger, type, source, udfPath, message) {
|
|
87
|
+
const prefix = prefix_for_source(source);
|
|
88
|
+
if (typeof message === "object") {
|
|
89
|
+
message = `ConvexError ${JSON.stringify(message.errorData, null, 2)}`;
|
|
90
|
+
}
|
|
91
|
+
if (type === "info") {
|
|
92
|
+
const match = message.match(/^\[.*?\] /);
|
|
93
|
+
if (match === null) {
|
|
94
|
+
logger.error(`[CONVEX ${prefix}(${udfPath})] Could not parse console.log`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const level = message.slice(1, match[0].length - 2);
|
|
98
|
+
const args = message.slice(match[0].length);
|
|
99
|
+
logger.log(`%c[CONVEX ${prefix}(${udfPath})] [${level}]`, INFO_COLOR, args);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
logger.error(`[CONVEX ${prefix}(${udfPath})] ${message}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export function logFatalError(logger, message) {
|
|
106
|
+
const errorMessage = `[CONVEX FATAL ERROR] ${message}`;
|
|
107
|
+
logger.error(errorMessage);
|
|
108
|
+
return new Error(errorMessage);
|
|
109
|
+
}
|
|
110
|
+
export function createHybridErrorStacktrace(source, udfPath, result) {
|
|
111
|
+
const prefix = prefix_for_source(source);
|
|
112
|
+
return `[CONVEX ${prefix}(${udfPath})] ${result.errorMessage}\n Called by client`;
|
|
113
|
+
}
|
|
114
|
+
export function forwardData(result, error) {
|
|
115
|
+
error.data = result.errorData;
|
|
116
|
+
return error;
|
|
117
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Logger } from "../logging.js";
|
|
2
|
+
import { AuthError, Transition } from "../sync/protocol.js";
|
|
3
|
+
/**
|
|
4
|
+
* An async function returning a JWT. Depending on the auth providers
|
|
5
|
+
* configured in convex/auth.config.ts, this may be a JWT-encoded OpenID
|
|
6
|
+
* Connect Identity Token or a traditional JWT.
|
|
7
|
+
*
|
|
8
|
+
* `forceRefreshToken` is `true` if the server rejected a previously
|
|
9
|
+
* returned token or the token is anticipated to expiring soon
|
|
10
|
+
* based on its `exp` time.
|
|
11
|
+
*
|
|
12
|
+
* See {@link ConvexReactClient.setAuth}.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export type AuthTokenFetcher = (args: {
|
|
17
|
+
forceRefreshToken: boolean;
|
|
18
|
+
}) => Promise<string | null | undefined>;
|
|
19
|
+
export type AuthConfig = {
|
|
20
|
+
fetchToken: AuthTokenFetcher;
|
|
21
|
+
onAuthChange: (isAuthenticated: boolean) => void;
|
|
22
|
+
onRefreshChange?: ((isRefreshing: boolean) => void) | undefined;
|
|
23
|
+
};
|
|
24
|
+
export type AuthState = {
|
|
25
|
+
state: "noAuth";
|
|
26
|
+
} | {
|
|
27
|
+
state: "waitingForServerConfirmationOfCachedToken";
|
|
28
|
+
config: AuthConfig;
|
|
29
|
+
hasRetried: boolean;
|
|
30
|
+
} | {
|
|
31
|
+
state: "initialRefetch";
|
|
32
|
+
config: AuthConfig;
|
|
33
|
+
} | {
|
|
34
|
+
state: "waitingForServerConfirmationOfFreshToken";
|
|
35
|
+
config: AuthConfig;
|
|
36
|
+
hadAuth: boolean;
|
|
37
|
+
token: string;
|
|
38
|
+
} | {
|
|
39
|
+
state: "waitingForScheduledRefetch";
|
|
40
|
+
config: AuthConfig;
|
|
41
|
+
refetchTokenTimeoutId: ReturnType<typeof setTimeout>;
|
|
42
|
+
} | {
|
|
43
|
+
state: "notRefetching";
|
|
44
|
+
config: AuthConfig;
|
|
45
|
+
};
|
|
46
|
+
/** The client's side of auth: its identity state and the socket. */
|
|
47
|
+
export type AuthSync = {
|
|
48
|
+
isCurrentOrNewerAuthVersion(version: number): boolean;
|
|
49
|
+
markAuthCompletion(): void;
|
|
50
|
+
token(): string | undefined;
|
|
51
|
+
hasAuth(): boolean;
|
|
52
|
+
/** Sends `Authenticate` with the token. */
|
|
53
|
+
authenticate(token: string): void;
|
|
54
|
+
/** Forgets the token without sending anything; the socket is stopped. */
|
|
55
|
+
dropAuth(): void;
|
|
56
|
+
/** Forgets the token and sends `Authenticate` with none. */
|
|
57
|
+
clearAuth(): void;
|
|
58
|
+
stopSocket(): Promise<void>;
|
|
59
|
+
tryRestartSocket(): void;
|
|
60
|
+
pauseSocket(): void;
|
|
61
|
+
resumeSocket(): void;
|
|
62
|
+
};
|
|
63
|
+
export declare function reatomAuth(sync: AuthSync, options: {
|
|
64
|
+
logger: Logger;
|
|
65
|
+
refreshTokenLeewaySeconds: number;
|
|
66
|
+
initialAuthTokenReuse: boolean;
|
|
67
|
+
}, name: string): {
|
|
68
|
+
/** The auth state machine. */
|
|
69
|
+
state: import("@reatom/core").Atom<AuthState, [newState: AuthState]>;
|
|
70
|
+
setConfig(fetchToken: AuthTokenFetcher, onChange: (isAuthenticated: boolean) => void, onRefreshChange?: ((isRefreshing: boolean) => void) | undefined): Promise<void>;
|
|
71
|
+
onTransition(serverMessage: Transition): void;
|
|
72
|
+
onAuthError(serverMessage: AuthError): void;
|
|
73
|
+
stop(): void;
|
|
74
|
+
};
|
|
75
|
+
export type ReatomAuth = ReturnType<typeof reatomAuth>;
|