@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.6
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/README.md +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +783 -47
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1651 -382
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +701 -45
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +2138 -34
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +4343 -306
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1338 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +45 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +958 -340
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +93 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +18032 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +12615 -5249
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +17367 -8567
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +13587 -7333
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +11601 -5401
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +84 -62
- package/dist/server2.mjs.map +1 -1
- package/package.json +31 -5
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +11 -3
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +195 -10
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +15 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +52 -473
- package/src/client/auth/client.ts +22 -3
- package/src/client/comments.ts +205 -0
- package/src/client/community.ts +40 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +39 -11
- package/src/client/documents.ts +160 -0
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +4 -0
- package/src/client/portal/account.ts +73 -3
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +6 -2
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +110 -0
- package/src/contract/account.ts +111 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +235 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/comments.ts +223 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +94 -5
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +64 -5
- package/src/contract/links.ts +62 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +3 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +62 -20
- package/src/contract/workspaces.ts +5 -0
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +169 -0
- package/src/email/locales/de.json +169 -0
- package/src/email/locales/es.json +169 -0
- package/src/email/locales/fr.json +169 -0
- package/src/email/locales/it.json +169 -0
- package/src/email/locales/ja.json +169 -0
- package/src/email/locales/pl.json +169 -0
- package/src/email/locales/pt-br.json +169 -0
- package/src/email/locales/ru.json +169 -0
- package/src/email/locales/zh-cn.json +169 -0
- package/src/email/messages.ts +235 -0
- package/src/email/render.ts +160 -276
- package/src/email/samples.ts +142 -0
- package/src/email/schema.ts +98 -8
- package/src/email/templates/BrandEmail.vue +56 -0
- package/src/email/templates/CommentNotification.vue +39 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentInvitation.vue +53 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +47 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +15 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/{server/errors → errors}/posthog.ts +28 -4
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +28 -7
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +14 -1
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +14 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +20 -4
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +5 -2
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +40 -4
- package/src/server/account/routes.ts +305 -253
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +44 -6
- package/src/server/app/checks.ts +89 -0
- package/src/server/app/cors.ts +44 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +123 -0
- package/src/server/app/services.ts +219 -0
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +36 -3
- package/src/server/auth/email.ts +33 -18
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +165 -189
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +190 -2
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/session.ts +2 -0
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +8 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +80 -166
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +35 -10
- package/src/server/config/index.ts +2 -2
- package/src/server/config/public.ts +25 -6
- package/src/server/config/schema.ts +53 -293
- package/src/server/config/toml/schema.ts +330 -0
- package/src/server/db/index.ts +3 -1
- package/src/server/db/migrate.ts +33 -1
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +122 -3
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +31 -407
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +86 -26
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +39 -6
- package/src/server/email/types.ts +13 -0
- package/src/server/errors/index.ts +49 -12
- package/src/server/index.ts +27 -6
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +3 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +5 -3
- package/src/server/reports/service.ts +27 -3
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +3 -4
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +17 -8
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +35 -24
- package/src/server/workspaces/members.ts +57 -31
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +31 -22
- package/src/services/config.ts +48 -8
- package/src/services/metering.ts +15 -27
- package/src/services/usage.ts +6 -14
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/app.ts +0 -453
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
cloudAdminSessionResponseSchema,
|
|
9
9
|
cloudAdminUserDetailSchema,
|
|
10
10
|
cloudAdminUsersResponseSchema,
|
|
11
|
+
documentDownloadSchema,
|
|
12
|
+
enrollmentInviteResponseSchema,
|
|
11
13
|
enrollmentReviewResponseSchema,
|
|
12
14
|
enrollmentsResponseSchema,
|
|
13
15
|
type CloudAdminAuditFilter,
|
|
@@ -15,6 +17,7 @@ import {
|
|
|
15
17
|
type CloudAdminReportFilter,
|
|
16
18
|
type CloudAdminEmailFilter,
|
|
17
19
|
type CloudAdminUserFilter,
|
|
20
|
+
type CloudAdminUserMethod,
|
|
18
21
|
type EnrollmentStatus
|
|
19
22
|
} from '#cloud/contract'
|
|
20
23
|
|
|
@@ -28,14 +31,19 @@ export type AdminListRequest<Filter extends string> = {
|
|
|
28
31
|
offset?: number
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
/** A count as a query string carries it. */
|
|
35
|
+
function countParameter(count: number | undefined): string | undefined {
|
|
36
|
+
return count === undefined ? undefined : String(count)
|
|
37
|
+
}
|
|
38
|
+
|
|
31
39
|
/** The query string for `request`; an empty search asks for everything. */
|
|
32
40
|
function listQuery<Filter extends string>(request: AdminListRequest<Filter>) {
|
|
33
41
|
return {
|
|
34
42
|
query: {
|
|
35
43
|
search: request.search || undefined,
|
|
36
44
|
filter: request.filter,
|
|
37
|
-
limit: request.limit,
|
|
38
|
-
offset: request.offset
|
|
45
|
+
limit: countParameter(request.limit),
|
|
46
|
+
offset: countParameter(request.offset)
|
|
39
47
|
}
|
|
40
48
|
}
|
|
41
49
|
}
|
|
@@ -69,9 +77,22 @@ export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalR
|
|
|
69
77
|
signal
|
|
70
78
|
)
|
|
71
79
|
},
|
|
72
|
-
|
|
80
|
+
/** Approves every waiting request and sends each the invitation letter (official only). */
|
|
81
|
+
inviteEnrollments(signal?: AbortSignal) {
|
|
82
|
+
return call(
|
|
83
|
+
(options) => admin.enrollments.invite.$post(undefined, options),
|
|
84
|
+
enrollmentInviteResponseSchema,
|
|
85
|
+
signal
|
|
86
|
+
)
|
|
87
|
+
},
|
|
88
|
+
/** A page of people; `method` narrows any filter to those who sign in that way. */
|
|
89
|
+
users(
|
|
90
|
+
request: AdminListRequest<CloudAdminUserFilter> & { method?: CloudAdminUserMethod },
|
|
91
|
+
signal?: AbortSignal
|
|
92
|
+
) {
|
|
93
|
+
const { query } = listQuery(request)
|
|
73
94
|
return call(
|
|
74
|
-
(options) => admin.users.$get(
|
|
95
|
+
(options) => admin.users.$get({ query: { ...query, method: request.method } }, options),
|
|
75
96
|
cloudAdminUsersResponseSchema,
|
|
76
97
|
signal
|
|
77
98
|
)
|
|
@@ -84,7 +105,7 @@ export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalR
|
|
|
84
105
|
)
|
|
85
106
|
},
|
|
86
107
|
userAction(
|
|
87
|
-
action: 'ban' | 'unban' | 'revoke-sessions' | 'reset-two-step' | 'remove-picture',
|
|
108
|
+
action: 'ban' | 'unban' | 'revoke-sessions' | 'reset-two-step' | 'remove-picture' | 'delete',
|
|
88
109
|
userId: string,
|
|
89
110
|
reason?: string,
|
|
90
111
|
signal?: AbortSignal
|
|
@@ -95,6 +116,15 @@ export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalR
|
|
|
95
116
|
signal
|
|
96
117
|
)
|
|
97
118
|
},
|
|
119
|
+
/** Removes one of an account's sign-in methods; the server keeps its last. */
|
|
120
|
+
removeSignInMethod(userId: string, methodId: string, signal?: AbortSignal) {
|
|
121
|
+
return call(
|
|
122
|
+
(options) =>
|
|
123
|
+
admin.users['remove-sign-in-method'].$post({ json: { userId, methodId } }, options),
|
|
124
|
+
adminMutationResponseSchema,
|
|
125
|
+
signal
|
|
126
|
+
)
|
|
127
|
+
},
|
|
98
128
|
setAdmin(userId: string, enabled: boolean, signal?: AbortSignal) {
|
|
99
129
|
return call(
|
|
100
130
|
(options) => admin.users['set-admin'].$post({ json: { userId, enabled } }, options),
|
|
@@ -145,6 +175,23 @@ export function createAdministrationAPI(call: CloudCall, { admin, api }: PortalR
|
|
|
145
175
|
signal
|
|
146
176
|
)
|
|
147
177
|
},
|
|
178
|
+
/**
|
|
179
|
+
* Where a document's thumbnail at `revisionId` is: an image the console shows on the server's
|
|
180
|
+
* own address, so the administrator's session goes with it.
|
|
181
|
+
*/
|
|
182
|
+
documentThumbnailURL(documentId: string, revisionId: string): string {
|
|
183
|
+
return admin.documents[':documentId'].thumbnails[':revisionId']
|
|
184
|
+
.$url({ param: { documentId, revisionId } })
|
|
185
|
+
.toString()
|
|
186
|
+
},
|
|
187
|
+
/** Links to a document's file and images as it is now; the server records the download. */
|
|
188
|
+
downloadDocument(id: string, signal?: AbortSignal) {
|
|
189
|
+
return call(
|
|
190
|
+
(options) => admin.documents[':id'].download.$post({ param: { id } }, options),
|
|
191
|
+
documentDownloadSchema,
|
|
192
|
+
signal
|
|
193
|
+
)
|
|
194
|
+
},
|
|
148
195
|
/** Takes a document down, or puts it back. */
|
|
149
196
|
documentAction(
|
|
150
197
|
id: string,
|
|
@@ -7,10 +7,13 @@ import {
|
|
|
7
7
|
type AdminErrorCode
|
|
8
8
|
} from '#cloud/contract'
|
|
9
9
|
import type { CloudAccountRoutes } from '#cloud/server/account/routes'
|
|
10
|
+
import type { CloudAccessTokenRoutes } from '#cloud/server/account/tokens'
|
|
10
11
|
import type { CloudAPI } from '#cloud/server/api'
|
|
11
12
|
import { hc } from 'hono/client'
|
|
12
13
|
import * as v from 'valibot'
|
|
13
14
|
|
|
15
|
+
import { anySignal, DEADLINE_MS } from '@open-pencil/std/timing'
|
|
16
|
+
|
|
14
17
|
export type CloudPortalAPIErrorKind =
|
|
15
18
|
| 'authentication-required'
|
|
16
19
|
| 'authorization-required'
|
|
@@ -56,10 +59,10 @@ export type CloudCall = <Output>(
|
|
|
56
59
|
* body is checked against the contract.
|
|
57
60
|
*/
|
|
58
61
|
export function createCloudCall(options: CloudPortalClientOptions = {}): CloudCall {
|
|
59
|
-
const timeoutMs = options.timeoutMs ??
|
|
62
|
+
const timeoutMs = options.timeoutMs ?? DEADLINE_MS.request
|
|
60
63
|
return async (send, schema, callerSignal) => {
|
|
61
64
|
const timeout = AbortSignal.timeout(timeoutMs)
|
|
62
|
-
const signal =
|
|
65
|
+
const signal = anySignal([callerSignal, timeout])
|
|
63
66
|
let response: PortalResponse
|
|
64
67
|
try {
|
|
65
68
|
response = await send({ init: { signal } })
|
|
@@ -107,6 +110,7 @@ export function createPortalRouteClients(options: CloudPortalClientOptions = {})
|
|
|
107
110
|
}
|
|
108
111
|
return {
|
|
109
112
|
account: hc<CloudAccountRoutes>(origin, clientOptions).api.account,
|
|
113
|
+
tokens: hc<CloudAccessTokenRoutes>(origin, clientOptions).api.account.tokens,
|
|
110
114
|
admin: hc<CloudAdminRoutes>(new URL('/api/admin', origin).href, clientOptions),
|
|
111
115
|
api: hc<CloudAPI>(new URL('/api', origin).href, clientOptions)
|
|
112
116
|
}
|
package/src/client/response.ts
CHANGED
|
@@ -49,3 +49,24 @@ export async function parseResponse<
|
|
|
49
49
|
): Promise<v.InferOutput<TSchema>> {
|
|
50
50
|
return v.parse(schema, await responseBody(await request, onDiagnostic))
|
|
51
51
|
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The JSON a service deployed apart from Cloud answered, parsed with `schema` rather than trusted;
|
|
55
|
+
* a refusal throws what `refused` makes of its status and body.
|
|
56
|
+
*/
|
|
57
|
+
export async function serviceJSON<TSchema extends v.GenericSchema>(
|
|
58
|
+
response: Pick<Response, 'ok' | 'status' | 'text'>,
|
|
59
|
+
schema: TSchema,
|
|
60
|
+
refused: (status: number, body: string) => Error
|
|
61
|
+
): Promise<v.InferOutput<TSchema>> {
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
// An unreadable body still leaves the status, which callers tell refusals apart by.
|
|
64
|
+
const body = await response.text().catch((failure: unknown) => {
|
|
65
|
+
const error = refused(response.status, '')
|
|
66
|
+
error.cause = failure
|
|
67
|
+
throw error
|
|
68
|
+
})
|
|
69
|
+
throw refused(response.status, body)
|
|
70
|
+
}
|
|
71
|
+
return v.parse(v.pipe(v.string(), v.parseJson(), schema), await response.text())
|
|
72
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { collaborationTicketSchema, documentSummarySchema } from '#cloud/contract'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
/** What the API answers with one document, as creating, restoring, or duplicating one does. */
|
|
5
|
+
export const documentResponseSchema = v.object({ document: documentSummarySchema })
|
|
6
|
+
|
|
7
|
+
/** What the API answers with a ticket into a document's live room. */
|
|
8
|
+
export const collaborationTicketResponseSchema = v.object({ ticket: collaborationTicketSchema })
|
package/src/client/services.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { CloudFetch } from '#cloud/client/discovery'
|
|
2
|
+
import { serviceJSON } from '#cloud/client/response'
|
|
2
3
|
import {
|
|
3
4
|
serviceCatalogSchema,
|
|
4
5
|
serviceErrorSchema,
|
|
5
6
|
creditWalletSchema,
|
|
6
7
|
searchResponseSchema,
|
|
7
8
|
CLOUD_SERVICES_API_PATHS,
|
|
9
|
+
sendsTokenSafely,
|
|
8
10
|
type ServiceCatalog,
|
|
9
11
|
type ServiceErrorCode,
|
|
10
12
|
type CreditWallet,
|
|
@@ -13,6 +15,7 @@ import {
|
|
|
13
15
|
type CloudServicesAPI
|
|
14
16
|
} from '#cloud/contract'
|
|
15
17
|
import { hc } from 'hono/client'
|
|
18
|
+
import { joinURL, withoutTrailingSlash } from 'ufo'
|
|
16
19
|
import * as v from 'valibot'
|
|
17
20
|
|
|
18
21
|
/** Where a request gets the token it sends, and how it drops one the service refused. */
|
|
@@ -33,6 +36,15 @@ export class CloudServiceError extends Error {
|
|
|
33
36
|
}
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
/** A service endpoint a token would reach in the clear, which no request is sent to. */
|
|
40
|
+
export class CloudServiceInsecureEndpointError extends Error {
|
|
41
|
+
override readonly name = 'CloudServiceInsecureEndpointError'
|
|
42
|
+
|
|
43
|
+
constructor(readonly url: string) {
|
|
44
|
+
super('OpenPencil services and billing are reached only over HTTPS')
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
36
48
|
/** Whether a request can be sent twice: a stream, in `init` or a `Request`, is read by the first. */
|
|
37
49
|
function replayable(input: RequestInfo | URL, body: RequestInit['body']): boolean {
|
|
38
50
|
if (input instanceof Request && input.body) return false
|
|
@@ -54,6 +66,9 @@ export function cloudServicesFetch(
|
|
|
54
66
|
return fetch(input, { ...init, headers })
|
|
55
67
|
}
|
|
56
68
|
return async (input, init) => {
|
|
69
|
+
const url = input instanceof Request ? input.url : input
|
|
70
|
+
// Refused before a token is even signed, so none is sent where the network could read it.
|
|
71
|
+
if (!sendsTokenSafely(url)) throw new CloudServiceInsecureEndpointError(String(url))
|
|
57
72
|
const token = await tokens.token()
|
|
58
73
|
const response = await send(input, init, token)
|
|
59
74
|
if (response.status !== 401) return response
|
|
@@ -65,19 +80,9 @@ export function cloudServicesFetch(
|
|
|
65
80
|
|
|
66
81
|
const errorBodySchema = v.pipe(v.string(), v.parseJson(), serviceErrorSchema)
|
|
67
82
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
): Promise<v.InferOutput<TSchema>> {
|
|
72
|
-
const text = await response.text()
|
|
73
|
-
if (!response.ok) {
|
|
74
|
-
const error = v.safeParse(errorBodySchema, text)
|
|
75
|
-
throw new CloudServiceError(
|
|
76
|
-
response.status,
|
|
77
|
-
error.success ? error.output.error.code : undefined
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
return v.parse(v.pipe(v.string(), v.parseJson(), schema), text)
|
|
83
|
+
function refused(status: number, body: string): CloudServiceError {
|
|
84
|
+
const error = v.safeParse(errorBodySchema, body)
|
|
85
|
+
return new CloudServiceError(status, error.success ? error.output.error.code : undefined)
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
export type CloudServicesClientOptions = {
|
|
@@ -89,23 +94,23 @@ export type CloudServicesClientOptions = {
|
|
|
89
94
|
|
|
90
95
|
/** The services a Cloud advertises: the model API base URL, catalog, wallet, and search. */
|
|
91
96
|
export function createCloudServicesClient(options: CloudServicesClientOptions) {
|
|
92
|
-
const endpoint = options.endpoint
|
|
97
|
+
const endpoint = withoutTrailingSlash(options.endpoint)
|
|
93
98
|
const fetch = cloudServicesFetch(options.tokens, options.fetch)
|
|
94
99
|
// Answers are parsed rather than trusted: the service is deployed apart from this client.
|
|
95
100
|
const { v1 } = hc<CloudServicesAPI>(endpoint, { fetch })
|
|
96
101
|
return {
|
|
97
102
|
/** The base URL an OpenAI-compatible client sends chat completions below. */
|
|
98
|
-
modelBaseURL:
|
|
103
|
+
modelBaseURL: joinURL(endpoint, CLOUD_SERVICES_API_PATHS.base),
|
|
99
104
|
fetch,
|
|
100
105
|
async catalog(): Promise<ServiceCatalog> {
|
|
101
|
-
return
|
|
106
|
+
return serviceJSON(await v1.catalog.$get(), serviceCatalogSchema, refused)
|
|
102
107
|
},
|
|
103
108
|
async wallet(): Promise<CreditWallet> {
|
|
104
|
-
return
|
|
109
|
+
return serviceJSON(await v1.wallet.$get(), creditWalletSchema, refused)
|
|
105
110
|
},
|
|
106
111
|
/** A web search, charged to the workspace; only when the catalog lists `search`. */
|
|
107
112
|
async search(input: SearchRequestInput): Promise<SearchResponse> {
|
|
108
|
-
return
|
|
113
|
+
return serviceJSON(await v1.search.$post({ json: input }), searchResponseSchema, refused)
|
|
109
114
|
}
|
|
110
115
|
}
|
|
111
116
|
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import {
|
|
2
|
+
acceptedInvitationSchema,
|
|
3
|
+
createInvitationContinuationSchema,
|
|
4
|
+
documentDownloadSchema,
|
|
5
|
+
documentGrantSchema,
|
|
6
|
+
documentInvitationSchema,
|
|
7
|
+
documentShareSchema,
|
|
8
|
+
invitationContinuationSchema,
|
|
9
|
+
invitationPreviewSchema,
|
|
10
|
+
resolvedDocumentShareSchema,
|
|
11
|
+
type AcceptDocumentInvitationInput,
|
|
12
|
+
type CollaborationTicket,
|
|
13
|
+
type CreateInvitationContinuationInput,
|
|
14
|
+
type InvitationContinuation,
|
|
15
|
+
type CreateDocumentInvitationInput,
|
|
16
|
+
type CreateDocumentShareInput,
|
|
17
|
+
type DocumentGrant,
|
|
18
|
+
type DocumentInvitation,
|
|
19
|
+
type DocumentShare,
|
|
20
|
+
type InvitationPreview,
|
|
21
|
+
type PutDocumentGrantInput,
|
|
22
|
+
type ResolveDocumentShareInput,
|
|
23
|
+
fileDocumentReportResponseSchema,
|
|
24
|
+
type FileDocumentReportInput,
|
|
25
|
+
type UpdateDocumentShareInput,
|
|
26
|
+
type AcceptedInvitation
|
|
27
|
+
} from '#cloud/contract'
|
|
28
|
+
import type { CloudAPI, PublicCloudAPI } from '#cloud/server/api'
|
|
29
|
+
import type { hc } from 'hono/client'
|
|
30
|
+
import * as v from 'valibot'
|
|
31
|
+
|
|
32
|
+
import { parseResponse, responseBody, type CloudClientDiagnostic } from './response'
|
|
33
|
+
import { collaborationTicketResponseSchema } from './schema'
|
|
34
|
+
|
|
35
|
+
const sharesResponseSchema = v.object({ shares: v.array(documentShareSchema) })
|
|
36
|
+
const shareResponseSchema = v.object({ share: documentShareSchema })
|
|
37
|
+
const shareCapabilityResponseSchema = v.object({
|
|
38
|
+
share: documentShareSchema,
|
|
39
|
+
secret: v.string(),
|
|
40
|
+
path: v.string()
|
|
41
|
+
})
|
|
42
|
+
const grantsResponseSchema = v.object({ grants: v.array(documentGrantSchema) })
|
|
43
|
+
const grantResponseSchema = v.object({ grant: documentGrantSchema })
|
|
44
|
+
const acceptedResponseSchema = v.object({ accepted: acceptedInvitationSchema })
|
|
45
|
+
const invitationPreviewResponseSchema = v.object({ invitation: invitationPreviewSchema })
|
|
46
|
+
const invitationsResponseSchema = v.object({ invitations: v.array(documentInvitationSchema) })
|
|
47
|
+
const invitationCapabilityResponseSchema = v.object({
|
|
48
|
+
invitation: documentInvitationSchema,
|
|
49
|
+
token: v.string()
|
|
50
|
+
})
|
|
51
|
+
const resolvedShareResponseSchema = v.object({ resolution: resolvedDocumentShareSchema })
|
|
52
|
+
const sharedDocumentResponseSchema = v.object({
|
|
53
|
+
resolution: resolvedDocumentShareSchema,
|
|
54
|
+
document: documentDownloadSchema
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
/** Sharing a document: its links, grants, and invitations, and opening what someone shared. */
|
|
58
|
+
export function createSharingRequests(
|
|
59
|
+
client: ReturnType<typeof hc<CloudAPI>>,
|
|
60
|
+
publicClient: ReturnType<typeof hc<PublicCloudAPI>>,
|
|
61
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
62
|
+
) {
|
|
63
|
+
return {
|
|
64
|
+
async listDocumentShares(documentId: string): Promise<DocumentShare[]> {
|
|
65
|
+
const response = await parseResponse(
|
|
66
|
+
client.documents[':documentId'].shares.$get({ param: { documentId } }),
|
|
67
|
+
sharesResponseSchema,
|
|
68
|
+
onDiagnostic
|
|
69
|
+
)
|
|
70
|
+
return response.shares
|
|
71
|
+
},
|
|
72
|
+
async createDocumentShare(documentId: string, input: CreateDocumentShareInput) {
|
|
73
|
+
return parseResponse(
|
|
74
|
+
client.documents[':documentId'].shares.$post({
|
|
75
|
+
param: { documentId },
|
|
76
|
+
json: input
|
|
77
|
+
}),
|
|
78
|
+
shareCapabilityResponseSchema,
|
|
79
|
+
onDiagnostic
|
|
80
|
+
)
|
|
81
|
+
},
|
|
82
|
+
async updateDocumentShare(
|
|
83
|
+
documentId: string,
|
|
84
|
+
shareId: string,
|
|
85
|
+
input: UpdateDocumentShareInput
|
|
86
|
+
): Promise<DocumentShare> {
|
|
87
|
+
const response = await parseResponse(
|
|
88
|
+
client.documents[':documentId'].shares[':shareId'].$patch({
|
|
89
|
+
param: { documentId, shareId },
|
|
90
|
+
json: input
|
|
91
|
+
}),
|
|
92
|
+
shareResponseSchema,
|
|
93
|
+
onDiagnostic
|
|
94
|
+
)
|
|
95
|
+
return response.share
|
|
96
|
+
},
|
|
97
|
+
async rotateDocumentShare(documentId: string, shareId: string) {
|
|
98
|
+
return parseResponse(
|
|
99
|
+
client.documents[':documentId'].shares[':shareId'].rotate.$post({
|
|
100
|
+
param: { documentId, shareId }
|
|
101
|
+
}),
|
|
102
|
+
shareCapabilityResponseSchema,
|
|
103
|
+
onDiagnostic
|
|
104
|
+
)
|
|
105
|
+
},
|
|
106
|
+
async revokeDocumentShare(documentId: string, shareId: string): Promise<void> {
|
|
107
|
+
await responseBody(
|
|
108
|
+
await client.documents[':documentId'].shares[':shareId'].$delete({
|
|
109
|
+
param: { documentId, shareId }
|
|
110
|
+
}),
|
|
111
|
+
onDiagnostic
|
|
112
|
+
)
|
|
113
|
+
},
|
|
114
|
+
async listDocumentGrants(documentId: string): Promise<DocumentGrant[]> {
|
|
115
|
+
const response = await parseResponse(
|
|
116
|
+
client.documents[':documentId'].grants.$get({ param: { documentId } }),
|
|
117
|
+
grantsResponseSchema,
|
|
118
|
+
onDiagnostic
|
|
119
|
+
)
|
|
120
|
+
return response.grants
|
|
121
|
+
},
|
|
122
|
+
async putDocumentGrant(
|
|
123
|
+
documentId: string,
|
|
124
|
+
userId: string,
|
|
125
|
+
input: PutDocumentGrantInput
|
|
126
|
+
): Promise<DocumentGrant> {
|
|
127
|
+
const response = await parseResponse(
|
|
128
|
+
client.documents[':documentId'].grants[':userId'].$put({
|
|
129
|
+
param: { documentId, userId },
|
|
130
|
+
json: input
|
|
131
|
+
}),
|
|
132
|
+
grantResponseSchema,
|
|
133
|
+
onDiagnostic
|
|
134
|
+
)
|
|
135
|
+
return response.grant
|
|
136
|
+
},
|
|
137
|
+
async revokeDocumentGrant(documentId: string, userId: string): Promise<void> {
|
|
138
|
+
await responseBody(
|
|
139
|
+
await client.documents[':documentId'].grants[':userId'].$delete({
|
|
140
|
+
param: { documentId, userId }
|
|
141
|
+
}),
|
|
142
|
+
onDiagnostic
|
|
143
|
+
)
|
|
144
|
+
},
|
|
145
|
+
async listDocumentInvitations(documentId: string): Promise<DocumentInvitation[]> {
|
|
146
|
+
const response = await parseResponse(
|
|
147
|
+
client.documents[':documentId'].invitations.$get({ param: { documentId } }),
|
|
148
|
+
invitationsResponseSchema,
|
|
149
|
+
onDiagnostic
|
|
150
|
+
)
|
|
151
|
+
return response.invitations
|
|
152
|
+
},
|
|
153
|
+
async createDocumentInvitation(documentId: string, input: CreateDocumentInvitationInput) {
|
|
154
|
+
return parseResponse(
|
|
155
|
+
client.documents[':documentId'].invitations.$post({
|
|
156
|
+
param: { documentId },
|
|
157
|
+
json: input
|
|
158
|
+
}),
|
|
159
|
+
invitationCapabilityResponseSchema,
|
|
160
|
+
onDiagnostic
|
|
161
|
+
)
|
|
162
|
+
},
|
|
163
|
+
async createInvitationContinuation(
|
|
164
|
+
input: CreateInvitationContinuationInput
|
|
165
|
+
): Promise<InvitationContinuation> {
|
|
166
|
+
return parseResponse(
|
|
167
|
+
publicClient.invitations.continuations.$post({ json: input }),
|
|
168
|
+
invitationContinuationSchema,
|
|
169
|
+
onDiagnostic
|
|
170
|
+
)
|
|
171
|
+
},
|
|
172
|
+
async consumeInvitationContinuation(id: string): Promise<CreateInvitationContinuationInput> {
|
|
173
|
+
return parseResponse(
|
|
174
|
+
publicClient.invitations.continuations[':continuationId'].consume.$post({
|
|
175
|
+
param: { continuationId: id }
|
|
176
|
+
}),
|
|
177
|
+
createInvitationContinuationSchema,
|
|
178
|
+
onDiagnostic
|
|
179
|
+
)
|
|
180
|
+
},
|
|
181
|
+
async previewInvitation(
|
|
182
|
+
invitationId: string,
|
|
183
|
+
input: AcceptDocumentInvitationInput
|
|
184
|
+
): Promise<InvitationPreview> {
|
|
185
|
+
const response = await parseResponse(
|
|
186
|
+
publicClient.invitations[':invitationId'].preview.$post({
|
|
187
|
+
param: { invitationId },
|
|
188
|
+
json: input
|
|
189
|
+
}),
|
|
190
|
+
invitationPreviewResponseSchema,
|
|
191
|
+
onDiagnostic
|
|
192
|
+
)
|
|
193
|
+
return response.invitation
|
|
194
|
+
},
|
|
195
|
+
/** Accepts a document or workspace invitation, and says which it was. */
|
|
196
|
+
async acceptInvitation(
|
|
197
|
+
invitationId: string,
|
|
198
|
+
input: AcceptDocumentInvitationInput
|
|
199
|
+
): Promise<AcceptedInvitation> {
|
|
200
|
+
const response = await parseResponse(
|
|
201
|
+
client.invitations[':invitationId'].accept.$post({
|
|
202
|
+
param: { invitationId },
|
|
203
|
+
json: input
|
|
204
|
+
}),
|
|
205
|
+
acceptedResponseSchema,
|
|
206
|
+
onDiagnostic
|
|
207
|
+
)
|
|
208
|
+
return response.accepted
|
|
209
|
+
},
|
|
210
|
+
async revokeDocumentInvitation(documentId: string, invitationId: string): Promise<void> {
|
|
211
|
+
await responseBody(
|
|
212
|
+
await client.documents[':documentId'].invitations[':invitationId'].$delete({
|
|
213
|
+
param: { documentId, invitationId }
|
|
214
|
+
}),
|
|
215
|
+
onDiagnostic
|
|
216
|
+
)
|
|
217
|
+
},
|
|
218
|
+
/** Reports the document a share link opens to the server's administrators. */
|
|
219
|
+
async fileDocumentReport(shareId: string, input: FileDocumentReportInput): Promise<void> {
|
|
220
|
+
await parseResponse(
|
|
221
|
+
publicClient.shares[':shareId'].reports.$post({ param: { shareId }, json: input }),
|
|
222
|
+
fileDocumentReportResponseSchema,
|
|
223
|
+
onDiagnostic
|
|
224
|
+
)
|
|
225
|
+
},
|
|
226
|
+
async resolveDocumentShare(shareId: string, input: ResolveDocumentShareInput) {
|
|
227
|
+
const response = await parseResponse(
|
|
228
|
+
publicClient.shares[':shareId'].resolve.$post({
|
|
229
|
+
param: { shareId },
|
|
230
|
+
json: input
|
|
231
|
+
}),
|
|
232
|
+
resolvedShareResponseSchema,
|
|
233
|
+
onDiagnostic
|
|
234
|
+
)
|
|
235
|
+
return response.resolution
|
|
236
|
+
},
|
|
237
|
+
async getSharedDocument(shareId: string, input: ResolveDocumentShareInput) {
|
|
238
|
+
return parseResponse(
|
|
239
|
+
publicClient.shares[':shareId'].document.$post({
|
|
240
|
+
param: { shareId },
|
|
241
|
+
json: input
|
|
242
|
+
}),
|
|
243
|
+
sharedDocumentResponseSchema,
|
|
244
|
+
onDiagnostic
|
|
245
|
+
)
|
|
246
|
+
},
|
|
247
|
+
async getSharedCollaborationTicket(
|
|
248
|
+
shareId: string,
|
|
249
|
+
input: ResolveDocumentShareInput
|
|
250
|
+
): Promise<CollaborationTicket> {
|
|
251
|
+
const response = await parseResponse(
|
|
252
|
+
publicClient.shares[':shareId']['collaboration-ticket'].$post({
|
|
253
|
+
param: { shareId },
|
|
254
|
+
json: input
|
|
255
|
+
}),
|
|
256
|
+
collaborationTicketResponseSchema,
|
|
257
|
+
onDiagnostic
|
|
258
|
+
)
|
|
259
|
+
return response.ticket
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { anySignal, DEADLINE_MS } from '@open-pencil/std/timing'
|
|
2
|
+
|
|
3
|
+
import { createCloudAPIClient } from './api'
|
|
4
|
+
import { discoverCloud, type CloudFetch } from './discovery'
|
|
5
|
+
import { downloadCloudDocument } from './files'
|
|
6
|
+
|
|
7
|
+
/** The tour a server's gallery names, as one file, and the release it was named for. */
|
|
8
|
+
export type CommunityTour = { name: string; version: string | null; bytes: Uint8Array }
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The tour a Cloud server's community gallery names (`[community] tour`), with its images, as
|
|
12
|
+
* the named version for `version` when the gallery has one, such as the release an older editor
|
|
13
|
+
* is, else as it is now. Null when the server names no tour or its gallery no longer holds it.
|
|
14
|
+
* Requests carry no account, so any page may make them, as the documentation site's landing does.
|
|
15
|
+
*/
|
|
16
|
+
export async function fetchCommunityTour(
|
|
17
|
+
serverURL: string,
|
|
18
|
+
options: { version?: string | null; fetch?: CloudFetch; signal?: AbortSignal } = {}
|
|
19
|
+
): Promise<CommunityTour | null> {
|
|
20
|
+
const base = options.fetch ?? globalThis.fetch
|
|
21
|
+
const fetch: CloudFetch = (input, init) => base(input, { ...init, credentials: 'omit' })
|
|
22
|
+
const discovery = await discoverCloud(serverURL, { fetch, signal: options.signal })
|
|
23
|
+
const tourId = discovery.community?.tourDocumentId
|
|
24
|
+
if (!tourId) return null
|
|
25
|
+
const cloud = createCloudAPIClient(discovery.apiURL, { fetch, signal: options.signal })
|
|
26
|
+
const tour = (await cloud.listCommunityDocuments()).documents.find(
|
|
27
|
+
(document) => document.id === tourId
|
|
28
|
+
)
|
|
29
|
+
if (!tour) return null
|
|
30
|
+
const release = tour.versions.find((version) => version.name === options.version) ?? null
|
|
31
|
+
const download = await cloud.getCommunityDocument(tour.id, release?.id ?? null)
|
|
32
|
+
// Each object from the store is bounded too, its body included, as the API's requests are: a
|
|
33
|
+
// caller such as the docs landing passes no signal and keeps the answer it waits for.
|
|
34
|
+
const bytes = await downloadCloudDocument(
|
|
35
|
+
download.document,
|
|
36
|
+
(input, init) =>
|
|
37
|
+
fetch(input, {
|
|
38
|
+
...init,
|
|
39
|
+
signal: anySignal([init.signal, AbortSignal.timeout(DEADLINE_MS.transfer)])
|
|
40
|
+
}),
|
|
41
|
+
{ signal: options.signal }
|
|
42
|
+
)
|
|
43
|
+
return { name: tour.name, version: download.version?.name ?? null, bytes }
|
|
44
|
+
}
|