@squaredr/fieldcraft-supabase 1.0.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 +21 -0
- package/README.md +89 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +175 -0
- package/dist/index.mjs +135 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present SquaredR
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @squaredr/fieldcraft-supabase
|
|
2
|
+
|
|
3
|
+
Supabase storage adapter for [FieldCraft](https://squaredr.tech/products/fieldcraft). Stores form responses in Supabase with optional field-level AES-256-GCM encryption and row-level security support.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @squaredr/fieldcraft-supabase @supabase/supabase-js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependencies:** Requires `@squaredr/fieldcraft-core` v1.0.0+ and `@supabase/supabase-js` v2.0+.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { createClient } from '@supabase/supabase-js'
|
|
17
|
+
import { createSupabaseAdapter } from '@squaredr/fieldcraft-supabase'
|
|
18
|
+
|
|
19
|
+
const supabase = createClient(
|
|
20
|
+
process.env.SUPABASE_URL,
|
|
21
|
+
process.env.SUPABASE_ANON_KEY,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
const adapter = createSupabaseAdapter({
|
|
25
|
+
client: supabase,
|
|
26
|
+
table: 'formengine_responses', // default
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Pass the adapter to your form engine's `onSubmit`:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
<FormEngineRenderer
|
|
34
|
+
schema={schema}
|
|
35
|
+
onSubmit={(response) => adapter.submit(response)}
|
|
36
|
+
/>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Field-Level Encryption
|
|
40
|
+
|
|
41
|
+
Encrypt specific fields while leaving others in plaintext (useful for searching non-sensitive columns):
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
const adapter = createSupabaseAdapter({
|
|
45
|
+
client: supabase,
|
|
46
|
+
encryptionKey: process.env.ENCRYPTION_KEY, // 32-byte hex string
|
|
47
|
+
encryptFields: ['ssn', 'date_of_birth', 'medical_record'],
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
You can also use the encryption utilities directly:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { encryptFields, decryptFields } from '@squaredr/fieldcraft-supabase'
|
|
55
|
+
|
|
56
|
+
const encrypted = encryptFields(values, ['ssn', 'dob'], key)
|
|
57
|
+
const decrypted = decryptFields(encrypted, ['ssn', 'dob'], key)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Draft Persistence
|
|
61
|
+
|
|
62
|
+
Save and resume in-progress forms:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { createSupabaseDraftAdapter } from '@squaredr/fieldcraft-supabase'
|
|
66
|
+
|
|
67
|
+
const draftAdapter = createSupabaseDraftAdapter({
|
|
68
|
+
client: supabase,
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Configuration
|
|
73
|
+
|
|
74
|
+
| Option | Type | Required | Description |
|
|
75
|
+
|---|---|---|---|
|
|
76
|
+
| `client` | `SupabaseClient` | Yes | Supabase client instance |
|
|
77
|
+
| `table` | `string` | No | Table name (default: `formengine_responses`) |
|
|
78
|
+
| `encryptionKey` | `string` | No | 32-byte hex key for AES-256-GCM |
|
|
79
|
+
| `encryptFields` | `string[]` | No | Field names to encrypt |
|
|
80
|
+
| `onSuccess` | `(response, values) => void` | No | Callback after successful insert |
|
|
81
|
+
| `onError` | `(error) => void` | No | Callback on insert failure |
|
|
82
|
+
|
|
83
|
+
## Row-Level Security
|
|
84
|
+
|
|
85
|
+
The adapter is compatible with Supabase RLS policies. Configure your table's RLS rules to control which users can read/write responses.
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { FormResponse, SubmitAdapter, DraftAdapter } from '@squaredr/fieldcraft-core';
|
|
2
|
+
|
|
3
|
+
type SupabaseAdapterConfig = {
|
|
4
|
+
/** Supabase client instance from @supabase/supabase-js */
|
|
5
|
+
client: SupabaseClient;
|
|
6
|
+
/** Table name for responses (default: "formengine_responses") */
|
|
7
|
+
table?: string;
|
|
8
|
+
/** Field IDs to encrypt at rest */
|
|
9
|
+
encryptFields?: string[];
|
|
10
|
+
/** Base64-encoded 32-byte encryption key */
|
|
11
|
+
encryptionKey?: string;
|
|
12
|
+
/** Called after successful insert */
|
|
13
|
+
onSuccess?: (response: FormResponse, record: unknown) => void;
|
|
14
|
+
/** Called on error */
|
|
15
|
+
onError?: (error: Error) => void;
|
|
16
|
+
};
|
|
17
|
+
type SupabaseDraftAdapterConfig = {
|
|
18
|
+
/** Supabase client instance */
|
|
19
|
+
client: SupabaseClient;
|
|
20
|
+
/** Table name for drafts (default: "formengine_drafts") */
|
|
21
|
+
table?: string;
|
|
22
|
+
/** Draft time-to-live in hours (default: 72) */
|
|
23
|
+
ttlHours?: number;
|
|
24
|
+
};
|
|
25
|
+
/** Minimal Supabase client shape so we don't require the full dependency */
|
|
26
|
+
type SupabaseClient = {
|
|
27
|
+
from(table: string): SupabaseQueryBuilder;
|
|
28
|
+
};
|
|
29
|
+
type SupabaseQueryBuilder = {
|
|
30
|
+
insert(values: Record<string, unknown>): SupabaseFilterBuilder;
|
|
31
|
+
select(columns?: string): SupabaseFilterBuilder;
|
|
32
|
+
upsert(values: Record<string, unknown>, options?: {
|
|
33
|
+
onConflict?: string;
|
|
34
|
+
}): SupabaseFilterBuilder;
|
|
35
|
+
delete(): SupabaseFilterBuilder;
|
|
36
|
+
};
|
|
37
|
+
type SupabaseFilterBuilder = {
|
|
38
|
+
eq(column: string, value: unknown): SupabaseFilterBuilder;
|
|
39
|
+
gt(column: string, value: unknown): SupabaseFilterBuilder;
|
|
40
|
+
single(): Promise<{
|
|
41
|
+
data: Record<string, unknown> | null;
|
|
42
|
+
error: Error | null;
|
|
43
|
+
}>;
|
|
44
|
+
then(resolve: (value: {
|
|
45
|
+
data: unknown;
|
|
46
|
+
error: Error | null;
|
|
47
|
+
}) => void): Promise<void>;
|
|
48
|
+
[Symbol.toStringTag]?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Create a Supabase submit adapter. */
|
|
52
|
+
declare function createSupabaseAdapter(config: SupabaseAdapterConfig): SubmitAdapter;
|
|
53
|
+
|
|
54
|
+
/** Create a Supabase draft adapter. */
|
|
55
|
+
declare function createSupabaseDraftAdapter(config: SupabaseDraftAdapterConfig): DraftAdapter;
|
|
56
|
+
|
|
57
|
+
/** Encrypt specified fields in a values object. */
|
|
58
|
+
declare function encryptFields(values: Record<string, unknown>, fieldIds: string[], keyBase64: string): Record<string, unknown>;
|
|
59
|
+
/** Decrypt specified fields in a values object. */
|
|
60
|
+
declare function decryptFields(values: Record<string, unknown>, fieldIds: string[], keyBase64: string): Record<string, unknown>;
|
|
61
|
+
|
|
62
|
+
export { type SupabaseAdapterConfig, type SupabaseClient, type SupabaseDraftAdapterConfig, createSupabaseAdapter, createSupabaseDraftAdapter, decryptFields, encryptFields };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { FormResponse, SubmitAdapter, DraftAdapter } from '@squaredr/fieldcraft-core';
|
|
2
|
+
|
|
3
|
+
type SupabaseAdapterConfig = {
|
|
4
|
+
/** Supabase client instance from @supabase/supabase-js */
|
|
5
|
+
client: SupabaseClient;
|
|
6
|
+
/** Table name for responses (default: "formengine_responses") */
|
|
7
|
+
table?: string;
|
|
8
|
+
/** Field IDs to encrypt at rest */
|
|
9
|
+
encryptFields?: string[];
|
|
10
|
+
/** Base64-encoded 32-byte encryption key */
|
|
11
|
+
encryptionKey?: string;
|
|
12
|
+
/** Called after successful insert */
|
|
13
|
+
onSuccess?: (response: FormResponse, record: unknown) => void;
|
|
14
|
+
/** Called on error */
|
|
15
|
+
onError?: (error: Error) => void;
|
|
16
|
+
};
|
|
17
|
+
type SupabaseDraftAdapterConfig = {
|
|
18
|
+
/** Supabase client instance */
|
|
19
|
+
client: SupabaseClient;
|
|
20
|
+
/** Table name for drafts (default: "formengine_drafts") */
|
|
21
|
+
table?: string;
|
|
22
|
+
/** Draft time-to-live in hours (default: 72) */
|
|
23
|
+
ttlHours?: number;
|
|
24
|
+
};
|
|
25
|
+
/** Minimal Supabase client shape so we don't require the full dependency */
|
|
26
|
+
type SupabaseClient = {
|
|
27
|
+
from(table: string): SupabaseQueryBuilder;
|
|
28
|
+
};
|
|
29
|
+
type SupabaseQueryBuilder = {
|
|
30
|
+
insert(values: Record<string, unknown>): SupabaseFilterBuilder;
|
|
31
|
+
select(columns?: string): SupabaseFilterBuilder;
|
|
32
|
+
upsert(values: Record<string, unknown>, options?: {
|
|
33
|
+
onConflict?: string;
|
|
34
|
+
}): SupabaseFilterBuilder;
|
|
35
|
+
delete(): SupabaseFilterBuilder;
|
|
36
|
+
};
|
|
37
|
+
type SupabaseFilterBuilder = {
|
|
38
|
+
eq(column: string, value: unknown): SupabaseFilterBuilder;
|
|
39
|
+
gt(column: string, value: unknown): SupabaseFilterBuilder;
|
|
40
|
+
single(): Promise<{
|
|
41
|
+
data: Record<string, unknown> | null;
|
|
42
|
+
error: Error | null;
|
|
43
|
+
}>;
|
|
44
|
+
then(resolve: (value: {
|
|
45
|
+
data: unknown;
|
|
46
|
+
error: Error | null;
|
|
47
|
+
}) => void): Promise<void>;
|
|
48
|
+
[Symbol.toStringTag]?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Create a Supabase submit adapter. */
|
|
52
|
+
declare function createSupabaseAdapter(config: SupabaseAdapterConfig): SubmitAdapter;
|
|
53
|
+
|
|
54
|
+
/** Create a Supabase draft adapter. */
|
|
55
|
+
declare function createSupabaseDraftAdapter(config: SupabaseDraftAdapterConfig): DraftAdapter;
|
|
56
|
+
|
|
57
|
+
/** Encrypt specified fields in a values object. */
|
|
58
|
+
declare function encryptFields(values: Record<string, unknown>, fieldIds: string[], keyBase64: string): Record<string, unknown>;
|
|
59
|
+
/** Decrypt specified fields in a values object. */
|
|
60
|
+
declare function decryptFields(values: Record<string, unknown>, fieldIds: string[], keyBase64: string): Record<string, unknown>;
|
|
61
|
+
|
|
62
|
+
export { type SupabaseAdapterConfig, type SupabaseClient, type SupabaseDraftAdapterConfig, createSupabaseAdapter, createSupabaseDraftAdapter, decryptFields, encryptFields };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createSupabaseAdapter: () => createSupabaseAdapter,
|
|
34
|
+
createSupabaseDraftAdapter: () => createSupabaseDraftAdapter,
|
|
35
|
+
decryptFields: () => decryptFields,
|
|
36
|
+
encryptFields: () => encryptFields
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/encryption.ts
|
|
41
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
42
|
+
var IV_LENGTH = 12;
|
|
43
|
+
var AUTH_TAG_LENGTH = 16;
|
|
44
|
+
function parseKey(keyBase64) {
|
|
45
|
+
const key = Buffer.from(keyBase64, "base64");
|
|
46
|
+
if (key.length !== 32) {
|
|
47
|
+
throw new Error("Encryption key must be exactly 32 bytes (base64-encoded)");
|
|
48
|
+
}
|
|
49
|
+
return key;
|
|
50
|
+
}
|
|
51
|
+
function encryptValue(plaintext, key) {
|
|
52
|
+
const iv = import_node_crypto.default.randomBytes(IV_LENGTH);
|
|
53
|
+
const cipher = import_node_crypto.default.createCipheriv("aes-256-gcm", key, iv);
|
|
54
|
+
const encrypted = Buffer.concat([
|
|
55
|
+
cipher.update(plaintext, "utf8"),
|
|
56
|
+
cipher.final()
|
|
57
|
+
]);
|
|
58
|
+
const authTag = cipher.getAuthTag();
|
|
59
|
+
return Buffer.concat([iv, encrypted, authTag]).toString("base64");
|
|
60
|
+
}
|
|
61
|
+
function decryptValue(encoded, key) {
|
|
62
|
+
const buf = Buffer.from(encoded, "base64");
|
|
63
|
+
const iv = buf.subarray(0, IV_LENGTH);
|
|
64
|
+
const authTag = buf.subarray(buf.length - AUTH_TAG_LENGTH);
|
|
65
|
+
const ciphertext = buf.subarray(IV_LENGTH, buf.length - AUTH_TAG_LENGTH);
|
|
66
|
+
const decipher = import_node_crypto.default.createDecipheriv("aes-256-gcm", key, iv);
|
|
67
|
+
decipher.setAuthTag(authTag);
|
|
68
|
+
return decipher.update(ciphertext) + decipher.final("utf8");
|
|
69
|
+
}
|
|
70
|
+
function encryptFields(values, fieldIds, keyBase64) {
|
|
71
|
+
const key = parseKey(keyBase64);
|
|
72
|
+
const result = { ...values };
|
|
73
|
+
for (const fieldId of fieldIds) {
|
|
74
|
+
if (result[fieldId] !== void 0 && result[fieldId] !== null) {
|
|
75
|
+
result[fieldId] = encryptValue(
|
|
76
|
+
JSON.stringify(result[fieldId]),
|
|
77
|
+
key
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
function decryptFields(values, fieldIds, keyBase64) {
|
|
84
|
+
const key = parseKey(keyBase64);
|
|
85
|
+
const result = { ...values };
|
|
86
|
+
for (const fieldId of fieldIds) {
|
|
87
|
+
if (typeof result[fieldId] === "string") {
|
|
88
|
+
result[fieldId] = JSON.parse(
|
|
89
|
+
decryptValue(result[fieldId], key)
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/supabase-adapter.ts
|
|
97
|
+
function createSupabaseAdapter(config) {
|
|
98
|
+
return {
|
|
99
|
+
name: "supabase",
|
|
100
|
+
async submit(response) {
|
|
101
|
+
let values = { ...response.values };
|
|
102
|
+
if (config.encryptFields?.length && config.encryptionKey) {
|
|
103
|
+
values = encryptFields(
|
|
104
|
+
values,
|
|
105
|
+
config.encryptFields,
|
|
106
|
+
config.encryptionKey
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
const { error } = await config.client.from(config.table ?? "formengine_responses").insert({
|
|
110
|
+
schema_id: response.schemaId,
|
|
111
|
+
schema_version: response.schemaVersion,
|
|
112
|
+
session_token: response.sessionToken,
|
|
113
|
+
data: values,
|
|
114
|
+
metadata: response.metadata,
|
|
115
|
+
completion_time_ms: response.completionTimeMs,
|
|
116
|
+
submitted_at: response.submittedAt
|
|
117
|
+
});
|
|
118
|
+
if (error) {
|
|
119
|
+
const err = new Error(`Supabase insert failed: ${error.message}`);
|
|
120
|
+
config.onError?.(err);
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
config.onSuccess?.(response, values);
|
|
124
|
+
},
|
|
125
|
+
onError: config.onError
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/supabase-draft-adapter.ts
|
|
130
|
+
function createSupabaseDraftAdapter(config) {
|
|
131
|
+
const tableName = config.table ?? "formengine_drafts";
|
|
132
|
+
const ttlHours = config.ttlHours ?? 72;
|
|
133
|
+
return {
|
|
134
|
+
async save(draft) {
|
|
135
|
+
const expiresAt = new Date(
|
|
136
|
+
Date.now() + ttlHours * 60 * 60 * 1e3
|
|
137
|
+
).toISOString();
|
|
138
|
+
await config.client.from(tableName).upsert(
|
|
139
|
+
{
|
|
140
|
+
schema_id: draft.schemaId,
|
|
141
|
+
session_token: draft.sessionToken,
|
|
142
|
+
partial_data: draft.partialData,
|
|
143
|
+
current_section_id: draft.currentSectionId,
|
|
144
|
+
visited_section_ids: draft.visitedSectionIds,
|
|
145
|
+
expires_at: expiresAt,
|
|
146
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
147
|
+
},
|
|
148
|
+
{ onConflict: "schema_id,session_token" }
|
|
149
|
+
);
|
|
150
|
+
},
|
|
151
|
+
async load(schemaId, sessionToken) {
|
|
152
|
+
const { data, error } = await config.client.from(tableName).select("*").eq("schema_id", schemaId).eq("session_token", sessionToken).gt("expires_at", (/* @__PURE__ */ new Date()).toISOString()).single();
|
|
153
|
+
if (error || !data) return null;
|
|
154
|
+
return {
|
|
155
|
+
schemaId: data.schema_id,
|
|
156
|
+
sessionToken: data.session_token,
|
|
157
|
+
partialData: data.partial_data,
|
|
158
|
+
currentSectionId: data.current_section_id,
|
|
159
|
+
visitedSectionIds: data.visited_section_ids,
|
|
160
|
+
savedAt: data.updated_at,
|
|
161
|
+
expiresAt: data.expires_at
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
async delete(schemaId, sessionToken) {
|
|
165
|
+
await config.client.from(tableName).delete().eq("schema_id", schemaId).eq("session_token", sessionToken);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
170
|
+
0 && (module.exports = {
|
|
171
|
+
createSupabaseAdapter,
|
|
172
|
+
createSupabaseDraftAdapter,
|
|
173
|
+
decryptFields,
|
|
174
|
+
encryptFields
|
|
175
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// src/encryption.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
var IV_LENGTH = 12;
|
|
4
|
+
var AUTH_TAG_LENGTH = 16;
|
|
5
|
+
function parseKey(keyBase64) {
|
|
6
|
+
const key = Buffer.from(keyBase64, "base64");
|
|
7
|
+
if (key.length !== 32) {
|
|
8
|
+
throw new Error("Encryption key must be exactly 32 bytes (base64-encoded)");
|
|
9
|
+
}
|
|
10
|
+
return key;
|
|
11
|
+
}
|
|
12
|
+
function encryptValue(plaintext, key) {
|
|
13
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
14
|
+
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
15
|
+
const encrypted = Buffer.concat([
|
|
16
|
+
cipher.update(plaintext, "utf8"),
|
|
17
|
+
cipher.final()
|
|
18
|
+
]);
|
|
19
|
+
const authTag = cipher.getAuthTag();
|
|
20
|
+
return Buffer.concat([iv, encrypted, authTag]).toString("base64");
|
|
21
|
+
}
|
|
22
|
+
function decryptValue(encoded, key) {
|
|
23
|
+
const buf = Buffer.from(encoded, "base64");
|
|
24
|
+
const iv = buf.subarray(0, IV_LENGTH);
|
|
25
|
+
const authTag = buf.subarray(buf.length - AUTH_TAG_LENGTH);
|
|
26
|
+
const ciphertext = buf.subarray(IV_LENGTH, buf.length - AUTH_TAG_LENGTH);
|
|
27
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
28
|
+
decipher.setAuthTag(authTag);
|
|
29
|
+
return decipher.update(ciphertext) + decipher.final("utf8");
|
|
30
|
+
}
|
|
31
|
+
function encryptFields(values, fieldIds, keyBase64) {
|
|
32
|
+
const key = parseKey(keyBase64);
|
|
33
|
+
const result = { ...values };
|
|
34
|
+
for (const fieldId of fieldIds) {
|
|
35
|
+
if (result[fieldId] !== void 0 && result[fieldId] !== null) {
|
|
36
|
+
result[fieldId] = encryptValue(
|
|
37
|
+
JSON.stringify(result[fieldId]),
|
|
38
|
+
key
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
function decryptFields(values, fieldIds, keyBase64) {
|
|
45
|
+
const key = parseKey(keyBase64);
|
|
46
|
+
const result = { ...values };
|
|
47
|
+
for (const fieldId of fieldIds) {
|
|
48
|
+
if (typeof result[fieldId] === "string") {
|
|
49
|
+
result[fieldId] = JSON.parse(
|
|
50
|
+
decryptValue(result[fieldId], key)
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/supabase-adapter.ts
|
|
58
|
+
function createSupabaseAdapter(config) {
|
|
59
|
+
return {
|
|
60
|
+
name: "supabase",
|
|
61
|
+
async submit(response) {
|
|
62
|
+
let values = { ...response.values };
|
|
63
|
+
if (config.encryptFields?.length && config.encryptionKey) {
|
|
64
|
+
values = encryptFields(
|
|
65
|
+
values,
|
|
66
|
+
config.encryptFields,
|
|
67
|
+
config.encryptionKey
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const { error } = await config.client.from(config.table ?? "formengine_responses").insert({
|
|
71
|
+
schema_id: response.schemaId,
|
|
72
|
+
schema_version: response.schemaVersion,
|
|
73
|
+
session_token: response.sessionToken,
|
|
74
|
+
data: values,
|
|
75
|
+
metadata: response.metadata,
|
|
76
|
+
completion_time_ms: response.completionTimeMs,
|
|
77
|
+
submitted_at: response.submittedAt
|
|
78
|
+
});
|
|
79
|
+
if (error) {
|
|
80
|
+
const err = new Error(`Supabase insert failed: ${error.message}`);
|
|
81
|
+
config.onError?.(err);
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
config.onSuccess?.(response, values);
|
|
85
|
+
},
|
|
86
|
+
onError: config.onError
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/supabase-draft-adapter.ts
|
|
91
|
+
function createSupabaseDraftAdapter(config) {
|
|
92
|
+
const tableName = config.table ?? "formengine_drafts";
|
|
93
|
+
const ttlHours = config.ttlHours ?? 72;
|
|
94
|
+
return {
|
|
95
|
+
async save(draft) {
|
|
96
|
+
const expiresAt = new Date(
|
|
97
|
+
Date.now() + ttlHours * 60 * 60 * 1e3
|
|
98
|
+
).toISOString();
|
|
99
|
+
await config.client.from(tableName).upsert(
|
|
100
|
+
{
|
|
101
|
+
schema_id: draft.schemaId,
|
|
102
|
+
session_token: draft.sessionToken,
|
|
103
|
+
partial_data: draft.partialData,
|
|
104
|
+
current_section_id: draft.currentSectionId,
|
|
105
|
+
visited_section_ids: draft.visitedSectionIds,
|
|
106
|
+
expires_at: expiresAt,
|
|
107
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
108
|
+
},
|
|
109
|
+
{ onConflict: "schema_id,session_token" }
|
|
110
|
+
);
|
|
111
|
+
},
|
|
112
|
+
async load(schemaId, sessionToken) {
|
|
113
|
+
const { data, error } = await config.client.from(tableName).select("*").eq("schema_id", schemaId).eq("session_token", sessionToken).gt("expires_at", (/* @__PURE__ */ new Date()).toISOString()).single();
|
|
114
|
+
if (error || !data) return null;
|
|
115
|
+
return {
|
|
116
|
+
schemaId: data.schema_id,
|
|
117
|
+
sessionToken: data.session_token,
|
|
118
|
+
partialData: data.partial_data,
|
|
119
|
+
currentSectionId: data.current_section_id,
|
|
120
|
+
visitedSectionIds: data.visited_section_ids,
|
|
121
|
+
savedAt: data.updated_at,
|
|
122
|
+
expiresAt: data.expires_at
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
async delete(schemaId, sessionToken) {
|
|
126
|
+
await config.client.from(tableName).delete().eq("schema_id", schemaId).eq("session_token", sessionToken);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export {
|
|
131
|
+
createSupabaseAdapter,
|
|
132
|
+
createSupabaseDraftAdapter,
|
|
133
|
+
decryptFields,
|
|
134
|
+
encryptFields
|
|
135
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@squaredr/fieldcraft-supabase",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "FieldCraft Supabase storage adapter with field-level AES-256-GCM encryption",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist", "LICENSE"],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"typecheck": "tsc --noEmit",
|
|
21
|
+
"clean": "rm -rf dist"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@squaredr/fieldcraft-core": "^1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@supabase/supabase-js": "^2.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^22",
|
|
31
|
+
"typescript": "^5.5",
|
|
32
|
+
"tsup": "^8.0",
|
|
33
|
+
"vitest": "^2.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"author": "SquaredR",
|
|
37
|
+
"homepage": "https://squaredr.tech/products/fieldcraft",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/SquaredR98/fieldcraft/issues"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/SquaredR98/fieldcraft.git",
|
|
44
|
+
"directory": "packages/adapters/supabase"
|
|
45
|
+
},
|
|
46
|
+
"keywords": ["fieldcraft", "form-engine", "supabase", "adapter", "storage", "encryption"],
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
}
|
|
50
|
+
}
|