@crombolo/data 0.61.0 → 0.62.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.61.0",
3
+ "version": "0.62.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/core": "0.61.0",
32
+ "@crombolo/core": "0.62.0",
33
33
  "kysely": "0.29.6"
34
34
  }
35
35
  }
package/src/index.ts CHANGED
@@ -34,6 +34,7 @@ export * from './repositories/extension-settings.ts'
34
34
  export * from './repositories/external-logins.ts'
35
35
  export * from './repositories/folders.ts'
36
36
  export * from './repositories/form-entries.ts'
37
+ export * from './repositories/form-workflow-secrets.ts'
37
38
  export * from './repositories/form-workflows.ts'
38
39
  export * from './repositories/jobs.ts'
39
40
  export * from './repositories/languages.ts'
@@ -92,6 +93,7 @@ export * from './schema/extension-settings.ts'
92
93
  export * from './schema/form-library.ts'
93
94
  export * from './schema/form-library-types.ts'
94
95
  export * from './schema/form-permission-prefix.ts'
96
+ export * from './schema/form-workflow-secrets.ts'
95
97
  export * from './schema/group-permissions.ts'
96
98
  export * from './schema/identity.ts'
97
99
  export * from './schema/image-alt-text.ts'
@@ -0,0 +1,63 @@
1
+ /**
2
+ * A form workflow's secret headers (story 304), each already sealed, with the
3
+ * URL it was set for. docs/forms.md, "Where a form may send".
4
+ */
5
+ import type { Db } from '../database.ts'
6
+ import { DbDate } from '../dialect.ts'
7
+
8
+ export interface FormWorkflowSecret {
9
+ header: string
10
+ sealed: string
11
+ url: string
12
+ }
13
+
14
+ export class FormWorkflowSecretRepository {
15
+ constructor(private readonly db: Db) {}
16
+
17
+ /** One workflow's secrets, by header. */
18
+ async of(formKey: string, workflowKey: string): Promise<Map<string, FormWorkflowSecret>> {
19
+ const rows = await this.db.q
20
+ .selectFrom('form_workflow_secret')
21
+ .select(['header', 'sealed', 'url'])
22
+ .where('form_key', '=', formKey)
23
+ .where('workflow_key', '=', workflowKey)
24
+ .execute()
25
+ return new Map(rows.map((row) => [row.header, row]))
26
+ }
27
+
28
+ /** Sets one header's secret for `url`, or with `undefined` removes it. */
29
+ async set(
30
+ formKey: string,
31
+ workflowKey: string,
32
+ header: string,
33
+ secret: { sealed: string; url: string } | undefined,
34
+ by: string | null,
35
+ ): Promise<void> {
36
+ await this.db.q
37
+ .deleteFrom('form_workflow_secret')
38
+ .where('form_key', '=', formKey)
39
+ .where('workflow_key', '=', workflowKey)
40
+ .where('header', '=', header)
41
+ .execute()
42
+ if (!secret) return
43
+ await this.db.q
44
+ .insertInto('form_workflow_secret')
45
+ .values({
46
+ form_key: formKey,
47
+ workflow_key: workflowKey,
48
+ header,
49
+ sealed: secret.sealed,
50
+ url: secret.url,
51
+ set_by: by,
52
+ set_at: DbDate.toDb(new Date()),
53
+ })
54
+ .execute()
55
+ }
56
+
57
+ /** Forgets a form's secrets, or only those of the workflows not in `keep`. */
58
+ async forget(formKey: string, keep: readonly string[] = []): Promise<void> {
59
+ let query = this.db.q.deleteFrom('form_workflow_secret').where('form_key', '=', formKey)
60
+ if (keep.length > 0) query = query.where('workflow_key', 'not in', [...keep])
61
+ await query.execute()
62
+ }
63
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Migration 073 — a form workflow's secret headers (story 304): each sealed
3
+ * with `CROMBOLO_SECRET_KEY` and kept here rather than in the form's file,
4
+ * with the URL it was set for, so it is sent only there.
5
+ */
6
+ import type { Db } from '../database.ts'
7
+ import type { Migration } from '../migrations.ts'
8
+ import { sql } from '../query.ts'
9
+ import { STATE_EXTENSION_ACTIVITY } from './extension-activity.ts'
10
+
11
+ export const STATE_FORM_WORKFLOW_SECRETS = '{c4d1f7a2-0073-4e6b-8a15-3f9e2b7d0073}'
12
+
13
+ export const formWorkflowSecretsMigration: Migration = {
14
+ from: STATE_EXTENSION_ACTIVITY,
15
+ to: STATE_FORM_WORKFLOW_SECRETS,
16
+ name: 'AddFormWorkflowSecrets',
17
+ kind: 'expand',
18
+ release: '0.62.0',
19
+ async up(db: Db) {
20
+ const t = db.dialect.types
21
+ await db.q.schema
22
+ .createTable('form_workflow_secret')
23
+ .addColumn('form_key', sql.raw(t.varchar(64)), (c) => c.notNull())
24
+ .addColumn('workflow_key', sql.raw(t.varchar(64)), (c) => c.notNull())
25
+ .addColumn('header', sql.raw(t.varchar(255)), (c) => c.notNull())
26
+ .addColumn('sealed', sql.raw(t.text), (c) => c.notNull())
27
+ .addColumn('url', sql.raw(t.text), (c) => c.notNull())
28
+ .addColumn('set_by', sql.raw(t.varchar(255)))
29
+ .addColumn('set_at', sql.raw(t.timestamp), (c) => c.notNull())
30
+ .addPrimaryKeyConstraint('form_workflow_secret_pk', ['form_key', 'workflow_key', 'header'])
31
+ .execute()
32
+ },
33
+ }
@@ -46,6 +46,7 @@ import { extensionSettingsMigration } from './extension-settings.ts'
46
46
  import { formLibraryMigration } from './form-library.ts'
47
47
  import { formPermissionPrefixMigration } from './form-permission-prefix.ts'
48
48
  import { formPermissionsMigration } from './form-permissions.ts'
49
+ import { formWorkflowSecretsMigration } from './form-workflow-secrets.ts'
49
50
  import { formWorkflowsMigration } from './form-workflows.ts'
50
51
  import { formsMigration } from './forms.ts'
51
52
  import { groupPermissionsMigration } from './group-permissions.ts'
@@ -159,4 +160,5 @@ export const cromboloPlan = new MigrationPlan([
159
160
  contentTypeSeoMigration,
160
161
  bundleGrantDelegateMigration,
161
162
  extensionActivityMigration,
163
+ formWorkflowSecretsMigration,
162
164
  ])
package/src/tables.gen.ts CHANGED
@@ -447,6 +447,15 @@ export interface CoreTables {
447
447
  workflow_name: string
448
448
  workflow_type: string
449
449
  }
450
+ form_workflow_secret: {
451
+ form_key: string
452
+ header: string
453
+ sealed: string
454
+ set_at: Timestamp
455
+ set_by: string | null
456
+ url: string
457
+ workflow_key: string
458
+ }
450
459
  job: {
451
460
  attempts: number
452
461
  claimed_at: Timestamp | null