alinea 1.5.0-preview.0 → 1.5.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.
Files changed (45) hide show
  1. package/dist/.init/content/pages/welcome.json +1 -1
  2. package/dist/LICENSES.md +78 -78
  3. package/dist/adapter/next/cms.js +1 -1
  4. package/dist/backend/Auth.d.ts +2 -1
  5. package/dist/backend/Auth.js +1 -0
  6. package/dist/backend/HandleAction.d.ts +1 -0
  7. package/dist/backend/HandleAction.js +1 -0
  8. package/dist/backend/Handler.js +122 -138
  9. package/dist/backend/api/OAuth2.js +55 -23
  10. package/dist/bundled.d.ts +22 -13
  11. package/dist/chunks/{chunk-T5PTMPOY.js → chunk-SH4E6FUG.js} +2 -3
  12. package/dist/chunks/{chunk-X6AXYEGR.js → chunk-Z3QMPIDC.js} +2 -4
  13. package/dist/cli/Serve.js +1 -1
  14. package/dist/cli/bin.js +1 -1
  15. package/dist/cloud/AuthResult.d.ts +4 -1
  16. package/dist/cloud/AuthResult.js +1 -0
  17. package/dist/cloud/CloudRemote.js +1 -1
  18. package/dist/cloud/view/CloudAuth.browser.js +16 -15
  19. package/dist/core/Client.d.ts +3 -0
  20. package/dist/core/Client.js +33 -18
  21. package/dist/core/Connection.d.ts +3 -1
  22. package/dist/core/Graph.js +1 -2
  23. package/dist/core/Session.d.ts +2 -3
  24. package/dist/core/db/CommitRequest.d.ts +0 -3
  25. package/dist/core/db/CommitRequest.js +0 -14
  26. package/dist/core/db/EntryIndex.d.ts +2 -0
  27. package/dist/core/db/EntryIndex.js +14 -6
  28. package/dist/core/db/EntryResolver.js +1 -1
  29. package/dist/core/db/EntryTransaction.js +28 -5
  30. package/dist/core/db/Mutation.d.ts +2 -2
  31. package/dist/core/db/Operation.d.ts +1 -1
  32. package/dist/core/db/Operation.js +6 -15
  33. package/dist/core/source/IndexedDBSource.js +40 -13
  34. package/dist/core/source/MemorySource.js +5 -5
  35. package/dist/core/source/ShaMismatchError.d.ts +1 -1
  36. package/dist/core/source/ShaMismatchError.js +2 -2
  37. package/dist/core/source/Tree.d.ts +1 -0
  38. package/dist/core/source/Tree.js +3 -0
  39. package/dist/dashboard/App.js +7 -7
  40. package/dist/dashboard/boot/BootDev.js +133 -1
  41. package/dist/dashboard/view/EntryTree.js +8 -2
  42. package/dist/dashboard/view/Toolbar.js +6 -6
  43. package/dist/dashboard/view/entry/EntryHeader.js +1 -1
  44. package/dist/dashboard/view/sidebar/SidebarSettings.js +3 -3
  45. package/package.json +2 -4
@@ -1,5 +1,5 @@
1
1
  {
2
- "_id": "30goFjOPTAuQcjsL8WnVg9QmGeR",
2
+ "_id": "32jmmrahEGbj2XLJgk85LnTNyEt",
3
3
  "_type": "Page",
4
4
  "_index": "a0",
5
5
  "_seeded": "welcome.json",
package/dist/LICENSES.md CHANGED
@@ -2,11 +2,11 @@ This file contains the licenses of the bundled modules in this distribution.
2
2
 
3
3
  ===
4
4
 
5
- # react-query@3.39.3 (MIT)
5
+ # rado@1.0.15 (MIT)
6
6
 
7
7
  MIT License
8
8
 
9
- Copyright (c) 2019 Tanner Linsley
9
+ Copyright (c) 2024 Ben Merckx
10
10
 
11
11
  Permission is hereby granted, free of charge, to any person obtaining a copy
12
12
  of this software and associated documentation files (the "Software"), to deal
@@ -26,14 +26,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
26
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
27
  SOFTWARE.
28
28
 
29
-
30
29
  ===
31
30
 
32
- # @headlessui/react@2.2.0 (MIT)
31
+ # react-query@3.39.3 (MIT)
33
32
 
34
33
  MIT License
35
34
 
36
- Copyright (c) 2020 Tailwind Labs
35
+ Copyright (c) 2019 Tanner Linsley
37
36
 
38
37
  Permission is hereby granted, free of charge, to any person obtaining a copy
39
38
  of this software and associated documentation files (the "Software"), to deal
@@ -53,31 +52,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
53
52
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54
53
  SOFTWARE.
55
54
 
56
- ===
57
-
58
- # rado@1.0.15 (MIT)
59
-
60
- MIT License
61
-
62
- Copyright (c) 2024 Ben Merckx
63
-
64
- Permission is hereby granted, free of charge, to any person obtaining a copy
65
- of this software and associated documentation files (the "Software"), to deal
66
- in the Software without restriction, including without limitation the rights
67
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
68
- copies of the Software, and to permit persons to whom the Software is
69
- furnished to do so, subject to the following conditions:
70
-
71
- The above copyright notice and this permission notice shall be included in all
72
- copies or substantial portions of the Software.
73
-
74
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
75
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
76
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
77
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
78
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
79
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
80
- SOFTWARE.
81
55
 
82
56
  ===
83
57
 
@@ -131,11 +105,28 @@ IN THE SOFTWARE.
131
105
 
132
106
  ===
133
107
 
134
- # @badgateway/oauth2-client@3.2.0 (MIT)
108
+ # domutils@3.1.0 (BSD-2-Clause)
109
+
110
+ Copyright (c) Felix Böhm
111
+ All rights reserved.
112
+
113
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
114
+
115
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
116
+
117
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
118
+
119
+ THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
120
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
121
+
122
+
123
+ ===
124
+
125
+ # @headlessui/react@2.2.0 (MIT)
135
126
 
136
127
  MIT License
137
128
 
138
- Copyright (c) 2019-2023 Evert Pot
129
+ Copyright (c) 2020 Tailwind Labs
139
130
 
140
131
  Permission is hereby granted, free of charge, to any person obtaining a copy
141
132
  of this software and associated documentation files (the "Software"), to deal
@@ -155,10 +146,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
155
146
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
156
147
  SOFTWARE.
157
148
 
158
-
159
149
  ===
160
150
 
161
- # domutils@3.1.0 (BSD-2-Clause)
151
+ # domhandler@5.0.3 (BSD-2-Clause)
162
152
 
163
153
  Copyright (c) Felix Böhm
164
154
  All rights reserved.
@@ -175,7 +165,7 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175
165
 
176
166
  ===
177
167
 
178
- # domhandler@5.0.3 (BSD-2-Clause)
168
+ # entities@4.5.0 (BSD-2-Clause)
179
169
 
180
170
  Copyright (c) Felix Böhm
181
171
  All rights reserved.
@@ -192,45 +182,29 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
192
182
 
193
183
  ===
194
184
 
195
- # @popperjs/core@2.11.8 (MIT)
185
+ # @badgateway/oauth2-client@3.2.0 (MIT)
196
186
 
197
- The MIT License (MIT)
187
+ MIT License
198
188
 
199
- Copyright (c) 2019 Federico Zivolo
189
+ Copyright (c) 2019-2023 Evert Pot
200
190
 
201
- Permission is hereby granted, free of charge, to any person obtaining a copy of
202
- this software and associated documentation files (the "Software"), to deal in
203
- the Software without restriction, including without limitation the rights to
204
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
205
- the Software, and to permit persons to whom the Software is furnished to do so,
206
- subject to the following conditions:
191
+ Permission is hereby granted, free of charge, to any person obtaining a copy
192
+ of this software and associated documentation files (the "Software"), to deal
193
+ in the Software without restriction, including without limitation the rights
194
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
195
+ copies of the Software, and to permit persons to whom the Software is
196
+ furnished to do so, subject to the following conditions:
207
197
 
208
198
  The above copyright notice and this permission notice shall be included in all
209
199
  copies or substantial portions of the Software.
210
200
 
211
201
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
212
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
213
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
214
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
215
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
216
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
217
-
218
-
219
- ===
220
-
221
- # entities@4.5.0 (BSD-2-Clause)
222
-
223
- Copyright (c) Felix Böhm
224
- All rights reserved.
225
-
226
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
227
-
228
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
229
-
230
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
231
-
232
- THIS IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS,
233
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
202
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
204
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
205
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
206
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
207
+ SOFTWARE.
234
208
 
235
209
 
236
210
  ===
@@ -467,6 +441,32 @@ SOFTWARE.
467
441
  limitations under the License.
468
442
 
469
443
 
444
+ ===
445
+
446
+ # @popperjs/core@2.11.8 (MIT)
447
+
448
+ The MIT License (MIT)
449
+
450
+ Copyright (c) 2019 Federico Zivolo
451
+
452
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
453
+ this software and associated documentation files (the "Software"), to deal in
454
+ the Software without restriction, including without limitation the rights to
455
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
456
+ the Software, and to permit persons to whom the Software is furnished to do so,
457
+ subject to the following conditions:
458
+
459
+ The above copyright notice and this permission notice shall be included in all
460
+ copies or substantial portions of the Software.
461
+
462
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
463
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
464
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
465
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
466
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
467
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
468
+
469
+
470
470
  ===
471
471
 
472
472
  # react-is@16.13.1 (MIT)
@@ -511,6 +511,18 @@ The above copyright notice and this permission notice shall be included in all c
511
511
  THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
512
512
 
513
513
 
514
+ ===
515
+
516
+ # @headless-tree/core@0.0.15 (MIT)
517
+
518
+ MIT
519
+
520
+ ===
521
+
522
+ # @headless-tree/react@0.0.15 (MIT)
523
+
524
+ MIT
525
+
514
526
  ===
515
527
 
516
528
  # @babel/runtime@7.25.4 (MIT)
@@ -539,18 +551,6 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
539
551
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
540
552
 
541
553
 
542
- ===
543
-
544
- # @headless-tree/core@0.0.15 (MIT)
545
-
546
- MIT
547
-
548
- ===
549
-
550
- # @headless-tree/react@0.0.15 (MIT)
551
-
552
- MIT
553
-
554
554
  ===
555
555
 
556
556
  # y-prosemirror@1.2.15 (MIT)
@@ -56,7 +56,7 @@ var NextCMS = class extends CMS {
56
56
  headers.set("Authorization", `Bearer ${apiKey}`);
57
57
  if (authCookies.length) {
58
58
  for (const [name, value] of authCookies) {
59
- headers.append("Cookie", `${name}=${value}`);
59
+ headers.set("Cookie", `${name}=${value}`);
60
60
  }
61
61
  }
62
62
  return { ...init, headers };
@@ -3,7 +3,8 @@ export declare enum AuthAction {
3
3
  Status = "status",
4
4
  Handshake = "handshake",
5
5
  Login = "login",
6
- Logout = "logout"
6
+ Logout = "logout",
7
+ Refresh = "refresh"
7
8
  }
8
9
  export declare class AuthError extends HttpError {
9
10
  name: string;
@@ -7,6 +7,7 @@ var AuthAction = /* @__PURE__ */ ((AuthAction2) => {
7
7
  AuthAction2["Handshake"] = "handshake";
8
8
  AuthAction2["Login"] = "login";
9
9
  AuthAction2["Logout"] = "logout";
10
+ AuthAction2["Refresh"] = "refresh";
10
11
  return AuthAction2;
11
12
  })(AuthAction || {});
12
13
  var AuthError = class extends HttpError {
@@ -1,4 +1,5 @@
1
1
  export declare enum HandleAction {
2
+ Auth = "auth",
2
3
  User = "user",
3
4
  Resolve = "resolve",
4
5
  Pending = "pending",
@@ -2,6 +2,7 @@ import "../chunks/chunk-NZLE2WMY.js";
2
2
 
3
3
  // src/backend/HandleAction.ts
4
4
  var HandleAction = /* @__PURE__ */ ((HandleAction2) => {
5
+ HandleAction2["Auth"] = "auth";
5
6
  HandleAction2["User"] = "user";
6
7
  HandleAction2["Resolve"] = "resolve";
7
8
  HandleAction2["Pending"] = "pending";
@@ -69,6 +69,11 @@ function createHandler({
69
69
  const acceptsJson = request.headers.get("accept")?.includes("application/json");
70
70
  if (!acceptsJson) throw new Response("Expected JSON", { status: 400 });
71
71
  };
72
+ if (action === HandleAction.Upload && request.method === "GET") {
73
+ const entryId = url.searchParams.get("entryId");
74
+ if (entryId && cnx.previewUpload)
75
+ return await cnx.previewUpload(entryId);
76
+ }
72
77
  try {
73
78
  userCtx = await cnx.verify(request);
74
79
  cnx = remote(userCtx);
@@ -82,148 +87,138 @@ function createHandler({
82
87
  throw new InvalidCredentialsError("Expected matching api key", {
83
88
  cause
84
89
  });
90
+ } else {
91
+ throw cause;
85
92
  }
86
- throw cause;
87
93
  }
88
- return transform(userCtx, async () => {
89
- if (action === HandleAction.User && request.method === "GET") {
90
- expectJson();
91
- return Response.json(userCtx ? userCtx.user : null);
92
- }
93
- const expectUser = () => {
94
- if (!userCtx) throw new Response("Unauthorized", { status: 401 });
95
- };
96
- const body = PLazy.from(() => {
97
- const isJson = request.headers.get("content-type")?.includes("application/json");
98
- if (!isJson) throw new Response("Expected JSON", { status: 400 });
99
- return request.json();
100
- });
101
- if (action === HandleAction.PreviewToken && request.method === "POST") {
102
- expectUser();
103
- expectJson();
104
- return Response.json(await previews.sign(PreviewBody(await body)));
105
- }
106
- if (action === HandleAction.Resolve && request.method === "POST") {
107
- expectJson();
108
- const raw = await request.text();
109
- const scope = getScope(cms.config);
110
- const query = scope.parse(raw);
111
- if (!query.preview) {
112
- await periodicSync(cnx, query.syncInterval);
113
- } else {
114
- const { parse } = await previewParser;
115
- const preview = await parse(
116
- query.preview,
117
- () => local.syncWith(cnx)
118
- );
119
- query.preview = preview;
120
- }
121
- return Response.json(await local.resolve(query) ?? null);
122
- }
123
- if (action === HandleAction.Mutate && request.method === "POST") {
124
- expectUser();
125
- expectJson();
126
- const mutations = await body;
127
- const attempt = async (retry = 0) => {
128
- await local.syncWith(cnx);
129
- const request2 = await local.request(mutations);
130
- try {
131
- let { sha } = await cnx.write(request2);
132
- if (sha === request2.intoSha) {
133
- await local.write(request2);
134
- } else {
135
- sha = await local.syncWith(cnx);
136
- }
137
- return sha;
138
- } catch (error) {
139
- if (error instanceof ShaMismatchError && retry < 3)
140
- return attempt(retry + 1);
141
- throw error;
142
- }
143
- };
144
- return Response.json({ sha: await attempt() });
145
- }
146
- if (action === HandleAction.Commit && request.method === "POST") {
147
- throw new Error("Mutations expected");
148
- }
149
- if (action === HandleAction.History && request.method === "GET") {
150
- expectUser();
151
- expectJson();
152
- const file = string(url.searchParams.get("file"));
153
- const revisionId = string.nullable(url.searchParams.get("revisionId"));
154
- const result = await (revisionId ? cnx.revisionData(file, revisionId) : cnx.revisions(file));
155
- return Response.json(result ?? null);
94
+ if (action === HandleAction.User && request.method === "GET") {
95
+ expectJson();
96
+ return Response.json(userCtx ? userCtx.user : null);
97
+ }
98
+ const expectUser = () => {
99
+ if (!userCtx) throw new Response("Unauthorized", { status: 401 });
100
+ };
101
+ const body = PLazy.from(() => {
102
+ const isJson = request.headers.get("content-type")?.includes("application/json");
103
+ if (!isJson) throw new Response("Expected JSON", { status: 400 });
104
+ return request.json();
105
+ });
106
+ if (action === HandleAction.PreviewToken && request.method === "POST") {
107
+ expectUser();
108
+ expectJson();
109
+ return Response.json(await previews.sign(PreviewBody(await body)));
110
+ }
111
+ if (action === HandleAction.Resolve && request.method === "POST") {
112
+ expectJson();
113
+ const raw = await request.text();
114
+ const scope = getScope(cms.config);
115
+ const query = scope.parse(raw);
116
+ if (!query.preview) {
117
+ await periodicSync(cnx, query.syncInterval);
118
+ } else {
119
+ const { parse } = await previewParser;
120
+ const preview = await parse(query.preview, () => local.syncWith(cnx));
121
+ query.preview = preview;
156
122
  }
157
- if (action === HandleAction.Tree && request.method === "GET") {
158
- expectJson();
159
- const sha = string(url.searchParams.get("sha"));
123
+ return Response.json(await local.resolve(query) ?? null);
124
+ }
125
+ if (action === HandleAction.Mutate && request.method === "POST") {
126
+ expectUser();
127
+ expectJson();
128
+ const mutations = await body;
129
+ const attempt = async (retry = 0) => {
160
130
  await local.syncWith(cnx);
161
- const tree = await local.getTreeIfDifferent(sha);
162
- return Response.json(tree ?? null);
163
- }
164
- if (action === HandleAction.Blob && request.method === "POST") {
165
- const { shas } = object({ shas: array(string) })(await body);
166
- await periodicSync(cnx);
167
- const tree = await local.source.getTree();
168
- const fromLocal = [];
169
- const fromRemote = [];
170
- for (const sha of shas) {
171
- if (tree.hasSha(sha)) fromLocal.push(sha);
172
- else fromRemote.push(sha);
173
- }
174
- const formData = new FormData();
175
- if (fromLocal.length > 0) {
176
- const blobs = local.source.getBlobs(fromLocal);
177
- for await (const [sha, blob] of blobs) {
178
- formData.append(sha, new Blob([blob]));
179
- }
180
- }
181
- if (fromRemote.length > 0) {
182
- const blobs = cnx.getBlobs(fromRemote);
183
- for await (const [sha, blob] of blobs) {
184
- formData.append(sha, new Blob([blob]));
131
+ const request2 = await local.request(mutations);
132
+ try {
133
+ let { sha } = await cnx.write(request2);
134
+ if (sha === request2.intoSha) {
135
+ await local.write(request2);
136
+ } else {
137
+ sha = await local.syncWith(cnx);
185
138
  }
139
+ return sha;
140
+ } catch (error) {
141
+ if (error instanceof ShaMismatchError && retry < 3)
142
+ return attempt(retry + 1);
143
+ throw error;
186
144
  }
187
- return new Response(formData);
145
+ };
146
+ return Response.json({ sha: await attempt() });
147
+ }
148
+ if (action === HandleAction.Commit && request.method === "POST") {
149
+ throw new Error("Mutations expected");
150
+ }
151
+ if (action === HandleAction.History && request.method === "GET") {
152
+ expectUser();
153
+ expectJson();
154
+ const file = string(url.searchParams.get("file"));
155
+ const revisionId = string.nullable(url.searchParams.get("revisionId"));
156
+ const result = await (revisionId ? cnx.revisionData(file, revisionId) : cnx.revisions(file));
157
+ return Response.json(result ?? null);
158
+ }
159
+ if (action === HandleAction.Tree && request.method === "GET") {
160
+ expectJson();
161
+ const sha = string(url.searchParams.get("sha"));
162
+ await local.syncWith(cnx);
163
+ const tree = await local.getTreeIfDifferent(sha);
164
+ return Response.json(tree ?? null);
165
+ }
166
+ if (action === HandleAction.Blob && request.method === "POST") {
167
+ const { shas } = object({ shas: array(string) })(await body);
168
+ await periodicSync(cnx);
169
+ const tree = await local.source.getTree();
170
+ const fromLocal = [];
171
+ const fromRemote = [];
172
+ for (const sha of shas) {
173
+ if (tree.hasSha(sha)) fromLocal.push(sha);
174
+ else fromRemote.push(sha);
188
175
  }
189
- if (action === HandleAction.Upload) {
190
- const entryId = url.searchParams.get("entryId");
191
- if (!entryId) {
192
- expectUser();
193
- expectJson();
194
- return Response.json(
195
- await cnx.prepareUpload(PrepareBody(await body).filename)
196
- );
176
+ const formData = new FormData();
177
+ if (fromLocal.length > 0) {
178
+ const blobs = local.source.getBlobs(fromLocal);
179
+ for await (const [sha, blob] of blobs) {
180
+ formData.append(sha, new Blob([blob]));
197
181
  }
198
- const isPost = request.method === "POST";
199
- if (isPost) {
200
- expectUser();
201
- if (!cnx.handleUpload)
202
- throw new Response("Bad Request", { status: 400 });
203
- await cnx.handleUpload(entryId, await request.blob());
204
- return new Response("OK", { status: 200 });
182
+ }
183
+ if (fromRemote.length > 0) {
184
+ const blobs = cnx.getBlobs(fromRemote);
185
+ for await (const [sha, blob] of blobs) {
186
+ formData.append(sha, new Blob([blob]));
205
187
  }
206
- if (!cnx.previewUpload)
207
- throw new Response("Bad Request", { status: 400 });
208
- return await cnx.previewUpload(entryId);
209
188
  }
210
- if (action === HandleAction.Draft && request.method === "GET") {
189
+ return new Response(formData);
190
+ }
191
+ if (action === HandleAction.Upload) {
192
+ expectUser();
193
+ const entryId = url.searchParams.get("entryId");
194
+ if (!entryId) {
211
195
  expectJson();
212
- const key = string(url.searchParams.get("key"));
213
- const draft = await cnx.getDraft(key);
214
196
  return Response.json(
215
- draft ? { ...draft, draft: base64.stringify(draft.draft) } : null
197
+ await cnx.prepareUpload(PrepareBody(await body).filename)
216
198
  );
217
199
  }
218
- if (action === HandleAction.Draft && request.method === "POST") {
219
- expectUser();
220
- expectJson();
221
- const data = await body;
222
- const draft = { ...data, draft: base64.parse(data.draft) };
223
- return Response.json(await cnx.storeDraft(draft));
200
+ const isPost = request.method === "POST";
201
+ if (isPost && cnx.handleUpload) {
202
+ await cnx.handleUpload(entryId, await request.blob());
203
+ return new Response("OK", { status: 200 });
224
204
  }
225
- return new Response("Bad Request", { status: 400 });
226
- });
205
+ }
206
+ if (action === HandleAction.Draft && request.method === "GET") {
207
+ expectJson();
208
+ const key = string(url.searchParams.get("key"));
209
+ const draft = await cnx.getDraft(key);
210
+ return Response.json(
211
+ draft ? { ...draft, draft: base64.stringify(draft.draft) } : null
212
+ );
213
+ }
214
+ if (action === HandleAction.Draft && request.method === "POST") {
215
+ expectUser();
216
+ expectJson();
217
+ const data = await body;
218
+ const draft = { ...data, draft: base64.parse(data.draft) };
219
+ return Response.json(await cnx.storeDraft(draft));
220
+ }
221
+ return new Response("Bad Request", { status: 400 });
227
222
  } catch (error) {
228
223
  if (error instanceof Response) return error;
229
224
  console.error(error);
@@ -237,17 +232,6 @@ function createHandler({
237
232
  }
238
233
  };
239
234
  }
240
- async function transform(ctx, run) {
241
- const transformer = ctx?.transformResponse;
242
- if (transformer) {
243
- const response = await run().catch((error) => {
244
- if (error instanceof Response) throw transformer(error);
245
- throw error;
246
- });
247
- return transformer(response);
248
- }
249
- return run();
250
- }
251
235
  export {
252
236
  createHandler
253
237
  };