safebyok-convex 0.0.0-stage → 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 +201 -0
- package/README.md +188 -2
- package/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/component/_generated/api.d.ts +7 -0
- package/dist/component/_generated/api.js +9 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +40 -0
- package/dist/component/_generated/dataModel.js +0 -0
- package/dist/component/_generated/server.d.ts +127 -0
- package/dist/component/_generated/server.js +74 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +16 -0
- package/dist/component/convex.config.js +18 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/schema.d.ts +98 -0
- package/dist/component/schema.js +31 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +196 -0
- package/dist/component/validators.js +118 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/component/vaults.d.ts +225 -0
- package/dist/component/vaults.js +189 -0
- package/dist/component/vaults.js.map +1 -0
- package/dist/component-api.d.ts +20 -0
- package/dist/component-api.js +0 -0
- package/dist/convex-vault-db.d.ts +56 -0
- package/dist/convex-vault-db.js +170 -0
- package/dist/convex-vault-db.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/refusals.d.ts +18 -0
- package/dist/refusals.js +16 -0
- package/dist/refusals.js.map +1 -0
- package/dist/server.d.ts +231 -0
- package/dist/server.js +147 -0
- package/dist/server.js.map +1 -0
- package/package.json +96 -3
- package/src/component/_generated/api.ts +16 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +14 -0
- package/src/component/schema.ts +20 -0
- package/src/component/validators.ts +128 -0
- package/src/component/vaults.ts +218 -0
- package/src/component-api.ts +20 -0
- package/src/convex-vault-db.ts +214 -0
- package/src/index.ts +9 -0
- package/src/refusals.ts +26 -0
- package/src/server.ts +165 -0
- package/src/test.ts +38 -0
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
|
|
95
|
+
Derivative 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 [yyyy] [name of copyright owner]
|
|
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,189 @@
|
|
|
1
|
-
#
|
|
1
|
+
# safebyok-convex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
safebyok's storage on [Convex](https://convex.dev): a Convex **component** that holds each vault's
|
|
4
|
+
bundle and lockboxes, ciphertext and public fields only, in tables of its own, and
|
|
5
|
+
**`convexVaultDB`**, the client side a vault stores through. Your Convex backend never sees a
|
|
6
|
+
whisper, a passphrase, or any key that opens one.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add safebyok safebyok-convex convex
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`safebyok` and `convex` are peer dependencies: your app installs them itself, so the adapter and
|
|
15
|
+
your app share one copy of each, and a `VaultError` from one is a `VaultError` to the other.
|
|
16
|
+
|
|
17
|
+
## What your app adds to its `convex/` directory
|
|
18
|
+
|
|
19
|
+
Install the component, in `convex/convex.config.ts`:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { defineApp } from 'convex/server'
|
|
23
|
+
import safebyok from 'safebyok-convex/convex.config'
|
|
24
|
+
|
|
25
|
+
const app = defineApp()
|
|
26
|
+
app.use(safebyok)
|
|
27
|
+
export default app
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Serve its functions to your clients, from a module of your own, `convex/safebyok.ts`. They call
|
|
31
|
+
your `getUserId` first: say who is signed in, the same id your app passes to `openVault` as
|
|
32
|
+
`userId`.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { SafebyokComponent } from 'safebyok-convex/server'
|
|
36
|
+
import { components } from './_generated/api'
|
|
37
|
+
|
|
38
|
+
export const {
|
|
39
|
+
getBundle, createBundle, updateBundle, getBox, listBoxSummaries,
|
|
40
|
+
updateBox, eraseBox, eraseVault, watchVault,
|
|
41
|
+
} = new SafebyokComponent(components.safebyok).api({
|
|
42
|
+
getUserId: async (ctx) => (await ctx.auth.getUserIdentity())?.subject ?? null,
|
|
43
|
+
})
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Run your usual Convex codegen, and your clients reach these as `api.safebyok.*`.
|
|
47
|
+
|
|
48
|
+
* **Who may reach a vault.** Each function refuses a caller who is signed out (`getUserId`
|
|
49
|
+
answers null, undefined or empty), or whose id is not the vault's `user_id`, with a
|
|
50
|
+
`ConvexError` whose `data` is `{ kind: 'not_permitted', message }`. The kind is the safebyok
|
|
51
|
+
`VaultError` kind of the same name. The message names no user and quotes no record.
|
|
52
|
+
* **Writes that come too late.** An update (`updateBundle`, `updateBox`) or an erase
|
|
53
|
+
(`eraseBox`, `eraseVault`) carries what its writer expects to stand -- the vault's `vault_id`,
|
|
54
|
+
and the bundle's `passphrase_epoch` or the lockbox's `revision_id` -- and checks it in the same
|
|
55
|
+
transaction it writes in. When that no longer stands, it writes nothing and resolves `false`,
|
|
56
|
+
which the client turns into a `stale_write`. Not an error: no `ConvexError`, nothing in your logs.
|
|
57
|
+
|
|
58
|
+
## What your app does on the client
|
|
59
|
+
|
|
60
|
+
`convexVaultDB(client, api.safebyok)`, from `safebyok-convex`, is a vault's storage: a `VaultDB`
|
|
61
|
+
whose every call is one call to the functions above, through the Convex client your app already
|
|
62
|
+
has. Hand it to `openVault`:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { ConvexClient } from 'convex/browser'
|
|
66
|
+
import { openVault } from 'safebyok'
|
|
67
|
+
import { browserOpts } from 'safebyok/browser'
|
|
68
|
+
import { convexVaultDB } from 'safebyok-convex'
|
|
69
|
+
import { api } from '../convex/_generated/api'
|
|
70
|
+
|
|
71
|
+
const convex = new ConvexClient(import.meta.env.VITE_CONVEX_URL)
|
|
72
|
+
convex.setAuth(fetchToken) // your auth provider's token, as for any Convex call
|
|
73
|
+
|
|
74
|
+
const vault = await openVault({
|
|
75
|
+
applabel: 'acme', userId, vaultlabel: 'main',
|
|
76
|
+
vaultDB: convexVaultDB(convex, api.safebyok),
|
|
77
|
+
...browserOpts(), // the key deriver and device store a browser wants
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
In a React app, pass the `ConvexReactClient` you give `ConvexProvider`, so the vault shares its
|
|
82
|
+
socket and its signed-in user. `safebyok-convex` itself imports nothing of React's.
|
|
83
|
+
|
|
84
|
+
* **Sign in first.** `userId` is the id your `getUserId` answers for the signed-in user. Open
|
|
85
|
+
the vault once the Convex client is authenticated (in React, once `useConvexAuth()` says so):
|
|
86
|
+
a call made before then is refused with kind `not_permitted`.
|
|
87
|
+
* **Close the vault when the user signs out.** Signing out of Convex does not lock it: every
|
|
88
|
+
call it makes is refused from then on, but the unlocked vaultKey stays in memory until the
|
|
89
|
+
vault locks, expires or closes.
|
|
90
|
+
* **Other devices' writes arrive live.** The vault watches `watchVault` and refreshes whenever
|
|
91
|
+
it changes, so its snapshot (and a React render) follows writes from anywhere.
|
|
92
|
+
* **Refusals** are `VaultError`s: `not_permitted` (signed out, or not this user's vault);
|
|
93
|
+
`stale_write` (another device changed the vault first: the vault has already caught up, so
|
|
94
|
+
read again before retrying); and `storage_unavailable` for anything else Convex reported, with
|
|
95
|
+
Convex's own error as its `cause`.
|
|
96
|
+
* **Offline, a call waits** for the connection, as every Convex call does, rather than failing.
|
|
97
|
+
Convex resends a mutation when it reconnects, so a write may land after any timeout you set:
|
|
98
|
+
show progress, but don't report it failed.
|
|
99
|
+
* A record holds at most 1 MiB, Convex's document limit; `openVault` keeps `maxWhisperBytes`
|
|
100
|
+
under half of it. A vault holds at most 8 MiB in all (`maxVaultBytes`; see *How big a vault
|
|
101
|
+
can be*).
|
|
102
|
+
|
|
103
|
+
## What is stored
|
|
104
|
+
|
|
105
|
+
Two tables, inside the component, beside nothing of your app's:
|
|
106
|
+
|
|
107
|
+
| Table | One row per | Columns | Index |
|
|
108
|
+
| -------------------- | ----------- | --------------------------------------------------------------- | ---------------------------------------------------- |
|
|
109
|
+
| `safebyok_bundles` | vault | the `VaultBundle` record as-is | `by_scope`: `user_id`, `applabel`, `vaultlabel` |
|
|
110
|
+
| `safebyok_lockboxes` | lockbox | the `Lockbox` record as-is, and its vault's scope columns | `by_box`: `user_id`, `applabel`, `vaultlabel`, `boxlabel` |
|
|
111
|
+
|
|
112
|
+
* **Encrypted**, as `{ cipher_iv_b64, ciphertext_b64 }`: each whisper (`whisper_ciphered`), each
|
|
113
|
+
lockbox's lockboxKey (`lockbox_key_wrapped`), and the vaultKey (`vault_key_passphrase_wrapped`).
|
|
114
|
+
* **In the open**: the scope (`applabel`, `user_id`, `vaultlabel`), each `boxlabel` and `note`,
|
|
115
|
+
the `vault_id`, `passphrase_epoch`, `revision_id`s, the passphraseSalt and key deriver's label,
|
|
116
|
+
timestamps, and the length of every ciphertext.
|
|
117
|
+
* **Never**: a whisper, a passphrase, any key in usable form, or the device-local remembered
|
|
118
|
+
unlock. No history: an update replaces its row. (Convex's own backups and exports keep what
|
|
119
|
+
they keep.)
|
|
120
|
+
* `user_id` leads both indexes, so one user's rows are one range of each.
|
|
121
|
+
|
|
122
|
+
**How big a vault can be.** Listing a vault, watching it and erasing it each read the whole vault
|
|
123
|
+
in one Convex transaction, which may read 16 MiB; erasing reads it twice, so `convexVaultDB`
|
|
124
|
+
declares a `maxVaultBytes` of 8 MiB. A vault holds 100 lockboxes by default, raised with
|
|
125
|
+
`maxLockboxes` as far as `maxVaultBytes` allows: `openVault` refuses, with kind `bad_option`, a
|
|
126
|
+
`maxLockboxes` whose lockboxes at `maxWhisperBytes` each might not fit, and its message says how
|
|
127
|
+
many would. At the default `maxWhisperBytes` (40444) that is at most 101; at a `maxWhisperBytes`
|
|
128
|
+
of 2048, ample for API keys, it is 1365. A store past the limit is refused with kind
|
|
129
|
+
`vault_full`, until a lockbox is erased. Each record must also fit Convex's 1 MiB document limit,
|
|
130
|
+
which the core's `maxWhisperBytes` keeps well inside.
|
|
131
|
+
|
|
132
|
+
## The functions
|
|
133
|
+
|
|
134
|
+
Each is one `VaultDB` verb (the core's storage interface). Arguments and results are plain JSON:
|
|
135
|
+
the core's own records, scopes and expectations.
|
|
136
|
+
|
|
137
|
+
| Function | Kind | Arguments | Resolves |
|
|
138
|
+
| ------------------ | -------- | ------------------------------------------- | ----------------------------------------------------- |
|
|
139
|
+
| `getBundle` | query | `{ scope }` | the `VaultBundle`, or null |
|
|
140
|
+
| `createBundle` | mutation | `{ bundle }` | the bundle standing after: this one, or the first one |
|
|
141
|
+
| `updateBundle` | mutation | `{ bundle, expected: { vault_id, passphrase_epoch } }` | `true` if written, `false` if not |
|
|
142
|
+
| `getBox` | query | `{ scope, boxlabel }` | the `Lockbox`, or null |
|
|
143
|
+
| `listBoxSummaries` | query | `{ scope }` | each lockbox without its ciphered fields, `format` kept, in boxlabel order |
|
|
144
|
+
| `updateBox` | mutation | `{ scope, lockbox, expected: { vault_id, revision_id } }` | `true` if written, `false` if not |
|
|
145
|
+
| `eraseBox` | mutation | `{ scope, boxlabel, expected: { vault_id } }` | `true` if the vault still stands (box or none), `false` if not |
|
|
146
|
+
| `eraseVault` | mutation | `{ scope, expected: { vault_id } }` | `true` if erased, `false` if not |
|
|
147
|
+
| `watchVault` | query | `{ scope }` | `{ vault_id, passphrase_epoch, boxes: [[boxlabel, revision_id]] }`, changing with every write |
|
|
148
|
+
|
|
149
|
+
`scope` is `{ applabel, user_id, vaultlabel }`. A `revision_id` of null expects no lockbox there.
|
|
150
|
+
|
|
151
|
+
## Testing your own functions
|
|
152
|
+
|
|
153
|
+
`safebyok-convex/test` installs the component into a [`convex-test`](https://docs.convex.dev/testing/convex-test)
|
|
154
|
+
instance. It assumes vitest, or another runner on Vite: it loads the component's modules with
|
|
155
|
+
`import.meta.glob`.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { register } from 'safebyok-convex/test'
|
|
159
|
+
|
|
160
|
+
const t = convexTest(schema, modules)
|
|
161
|
+
register(t)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Developing this package
|
|
165
|
+
|
|
166
|
+
`pnpm justify` here runs its lint, typecheck, tests -- `convex-test` under the edge runtime,
|
|
167
|
+
in memory, with no deployment -- and the build with its publint and arethetypeswrong checks. The typecheck and lint read `safebyok` from its source, through
|
|
168
|
+
the workspace's `@safebyok/source` export condition, so they need no build. The tests resolve it
|
|
169
|
+
through its built `dist/`, as a consumer's would, and make real vaults with the core's own code;
|
|
170
|
+
they build the core first by themselves (`pnpm build:deps`), so a fresh clone needs no prebuild
|
|
171
|
+
step.
|
|
172
|
+
|
|
173
|
+
The client's tests run the real `ConvexClient` and `ConvexReactClient` against
|
|
174
|
+
`tests/fake-deployment.ts`: a fake WebSocket that answers the sync protocol's query and mutation
|
|
175
|
+
messages from `convex-test`. It runs one call at a time, reruns every watched query after each
|
|
176
|
+
mutation, and signs each socket in as one user from the start; its doc block says what that
|
|
177
|
+
cannot prove.
|
|
178
|
+
|
|
179
|
+
The build follows Convex's component template: ESM only, unbundled, so each of the component's
|
|
180
|
+
modules keeps its own file under `dist/component/`, beside its `convex.config.js` and
|
|
181
|
+
`_generated/`, for a host's Convex CLI to bundle at deploy. `safebyok-convex/test` alone ships as
|
|
182
|
+
source, `src/test.ts`, since its `import.meta.glob` must find the component's `.ts` modules; a
|
|
183
|
+
test runner that resolves as a bundler does (vitest) loads it.
|
|
184
|
+
|
|
185
|
+
`src/component/_generated/` holds Convex's codegen output for the component, written by the
|
|
186
|
+
templates of the installed `convex` and never edited by hand. Codegen for a component needs a
|
|
187
|
+
Convex deployment, so until one has run it holds only the files Convex writes without one
|
|
188
|
+
(`server.ts`, `dataModel.ts`, and the untyped stub `api.ts`); the component's typed API,
|
|
189
|
+
`ComponentApi`, is derived from the functions themselves in `src/component-api.ts`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __exportAll };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AnyApi, AnyComponents } from "convex/server";
|
|
2
|
+
//#region src/component/_generated/api.d.ts
|
|
3
|
+
export declare const api: AnyApi;
|
|
4
|
+
export declare const internal: AnyApi;
|
|
5
|
+
export declare const components: AnyComponents;
|
|
6
|
+
//#endregion
|
|
7
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
2
|
+
//#region src/component/_generated/api.ts
|
|
3
|
+
const api = anyApi;
|
|
4
|
+
const internal = anyApi;
|
|
5
|
+
const components = componentsGeneric();
|
|
6
|
+
//#endregion
|
|
7
|
+
export { api, components, internal };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","names":[],"sources":["../../../src/component/_generated/api.ts"],"sourcesContent":["/* eslint-disable */\n/**\n * Generated `api` utility.\n *\n * THIS CODE IS AUTOMATICALLY GENERATED.\n *\n * To regenerate, run `npx convex dev`.\n * @module\n */\n\nimport type { AnyApi, AnyComponents } from \"convex/server\";\nimport { anyApi, componentsGeneric } from \"convex/server\";\n\nexport const api: AnyApi = anyApi;\nexport const internal: AnyApi = anyApi;\nexport const components: AnyComponents = componentsGeneric();\n"],"mappings":";;AAaA,MAAa,MAAc;AAC3B,MAAa,WAAmB;AAChC,MAAa,aAA4B,kBAAkB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import _default from "../schema.js";
|
|
2
|
+
import { DataModelFromSchemaDefinition, DocumentByName, SystemTableNames, TableNamesInDataModel } from "convex/server";
|
|
3
|
+
import { GenericId } from "convex/values";
|
|
4
|
+
//#region src/component/_generated/dataModel.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* The names of all of your Convex tables.
|
|
7
|
+
*/
|
|
8
|
+
export type TableNames = TableNamesInDataModel<DataModel>;
|
|
9
|
+
/**
|
|
10
|
+
* The type of a document stored in Convex.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
13
|
+
*/
|
|
14
|
+
export type Doc<TableName extends TableNames> = DocumentByName<DataModel, TableName>;
|
|
15
|
+
/**
|
|
16
|
+
* An identifier for a document in Convex.
|
|
17
|
+
*
|
|
18
|
+
* Convex documents are uniquely identified by their `Id`, which is accessible
|
|
19
|
+
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
|
|
20
|
+
*
|
|
21
|
+
* Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
|
|
22
|
+
*
|
|
23
|
+
* IDs are just strings at runtime, but this type can be used to distinguish them from other
|
|
24
|
+
* strings when type checking.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
27
|
+
*/
|
|
28
|
+
export type Id<TableName extends TableNames | SystemTableNames> = GenericId<TableName>;
|
|
29
|
+
/**
|
|
30
|
+
* A type describing your Convex data model.
|
|
31
|
+
*
|
|
32
|
+
* This type includes information about what tables you have, the type of
|
|
33
|
+
* documents stored in those tables, and the indexes defined on them.
|
|
34
|
+
*
|
|
35
|
+
* This type is used to parameterize methods like `queryGeneric` and
|
|
36
|
+
* `mutationGeneric` to make them type-safe.
|
|
37
|
+
*/
|
|
38
|
+
export type DataModel = DataModelFromSchemaDefinition<typeof _default>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//# sourceMappingURL=dataModel.d.ts.map
|
|
File without changes
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { DataModel } from "./dataModel.js";
|
|
2
|
+
import { ActionBuilder, GenericActionCtx, GenericDatabaseReader, GenericDatabaseWriter, GenericMutationCtx, GenericQueryCtx, HttpActionBuilder, MutationBuilder, QueryBuilder } from "convex/server";
|
|
3
|
+
//#region src/component/_generated/server.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Typesafe environment variables.
|
|
6
|
+
*
|
|
7
|
+
* This includes platform-provided env vars and any variables declared in
|
|
8
|
+
* `convex.config.ts`.
|
|
9
|
+
*/
|
|
10
|
+
type Env = {
|
|
11
|
+
readonly CONVEX_CLOUD_URL: string;
|
|
12
|
+
readonly CONVEX_SITE_URL: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Define a query in this Convex app's public API.
|
|
16
|
+
*
|
|
17
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
18
|
+
*
|
|
19
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
20
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
21
|
+
*/
|
|
22
|
+
export declare const query: QueryBuilder<DataModel, "public">;
|
|
23
|
+
/**
|
|
24
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
25
|
+
*
|
|
26
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
27
|
+
*
|
|
28
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
29
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
30
|
+
*/
|
|
31
|
+
export declare const internalQuery: QueryBuilder<DataModel, "internal">;
|
|
32
|
+
/**
|
|
33
|
+
* Define a mutation in this Convex app's public API.
|
|
34
|
+
*
|
|
35
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
36
|
+
*
|
|
37
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
38
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
39
|
+
*/
|
|
40
|
+
export declare const mutation: MutationBuilder<DataModel, "public">;
|
|
41
|
+
/**
|
|
42
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
43
|
+
*
|
|
44
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
45
|
+
*
|
|
46
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
47
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
48
|
+
*/
|
|
49
|
+
export declare const internalMutation: MutationBuilder<DataModel, "internal">;
|
|
50
|
+
/**
|
|
51
|
+
* Define an action in this Convex app's public API.
|
|
52
|
+
*
|
|
53
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
54
|
+
* code and code with side-effects, like calling third-party services.
|
|
55
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
56
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
57
|
+
*
|
|
58
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
59
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
60
|
+
*/
|
|
61
|
+
export declare const action: ActionBuilder<DataModel, "public">;
|
|
62
|
+
/**
|
|
63
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
64
|
+
*
|
|
65
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
66
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
67
|
+
*/
|
|
68
|
+
export declare const internalAction: ActionBuilder<DataModel, "internal">;
|
|
69
|
+
/**
|
|
70
|
+
* Define an HTTP action.
|
|
71
|
+
*
|
|
72
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
73
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
74
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
75
|
+
*
|
|
76
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
77
|
+
* and a Fetch API `Request` object as its second.
|
|
78
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
79
|
+
*/
|
|
80
|
+
export declare const httpAction: HttpActionBuilder;
|
|
81
|
+
export declare const env: Env;
|
|
82
|
+
/**
|
|
83
|
+
* A set of services for use within Convex query functions.
|
|
84
|
+
*
|
|
85
|
+
* The query context is passed as the first argument to any Convex query
|
|
86
|
+
* function run on the server.
|
|
87
|
+
*
|
|
88
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
89
|
+
*/
|
|
90
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
91
|
+
/**
|
|
92
|
+
* A set of services for use within Convex mutation functions.
|
|
93
|
+
*
|
|
94
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
95
|
+
* function run on the server.
|
|
96
|
+
*
|
|
97
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
98
|
+
*/
|
|
99
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
100
|
+
/**
|
|
101
|
+
* A set of services for use within Convex action functions.
|
|
102
|
+
*
|
|
103
|
+
* The action context is passed as the first argument to any Convex action
|
|
104
|
+
* function run on the server.
|
|
105
|
+
*/
|
|
106
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
107
|
+
/**
|
|
108
|
+
* An interface to read from the database within Convex query functions.
|
|
109
|
+
*
|
|
110
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
111
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
112
|
+
* building a query.
|
|
113
|
+
*/
|
|
114
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
115
|
+
/**
|
|
116
|
+
* An interface to read from and write to the database within Convex mutation
|
|
117
|
+
* functions.
|
|
118
|
+
*
|
|
119
|
+
* Convex guarantees that all writes within a single mutation are
|
|
120
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
121
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
122
|
+
* for the guarantees Convex provides your functions.
|
|
123
|
+
*/
|
|
124
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
125
|
+
//#endregion
|
|
126
|
+
export {};
|
|
127
|
+
//# sourceMappingURL=server.d.ts.map
|