@context-use/open-sync 0.2.1 → 0.3.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.
- package/LICENSE +3 -0
- package/package.json +7 -5
- package/src/api.ts +18 -16
- package/src/connector/client.ts +61 -28
- package/src/connector/failure.ts +49 -0
- package/src/connector/file.ts +104 -0
- package/src/connector/response.ts +22 -1
- package/src/db/client.ts +1 -11
- package/src/db/providers.sql +2 -3
- package/src/db/providers.ts +1 -11
- package/src/db/schema.sql +41 -36
- package/src/execution/abortable.ts +12 -0
- package/src/execution/diagnostics.ts +1 -2
- package/src/execution/provider.ts +1 -0
- package/src/execution/worker.ts +162 -39
- package/src/http/controller.ts +63 -42
- package/src/models/asset.ts +61 -0
- package/src/models/definition.ts +30 -15
- package/src/models/delivery.ts +25 -31
- package/src/models/error.ts +12 -1
- package/src/models/limits.ts +8 -3
- package/src/models/metadata.ts +42 -0
- package/src/models/page.ts +68 -13
- package/src/models/provider-path.ts +25 -0
- package/src/models/providers.ts +13 -2
- package/src/models/record.ts +22 -0
- package/src/models/registry.ts +15 -15
- package/src/models/source-http-error.ts +37 -0
- package/src/models/sync.ts +71 -0
- package/src/models/validation.ts +1 -1
- package/src/open-sync.ts +26 -7
- package/src/repositories/acquisition/contract.ts +18 -10
- package/src/repositories/acquisition/lease.ts +80 -94
- package/src/repositories/acquisition/outbox.ts +22 -22
- package/src/repositories/acquisition/records.ts +44 -13
- package/src/repositories/acquisition/sqlite.ts +78 -31
- package/src/repositories/assets/capacity.ts +16 -0
- package/src/repositories/assets/contract.ts +25 -0
- package/src/repositories/assets/filesystem.ts +103 -0
- package/src/repositories/assets/sqlite.ts +174 -0
- package/src/repositories/catalog/contract.ts +16 -19
- package/src/repositories/catalog/sqlite.ts +98 -121
- package/src/repositories/delivery/contract.ts +7 -11
- package/src/repositories/delivery/lease.ts +16 -0
- package/src/repositories/delivery/sqlite.ts +57 -34
- package/src/repositories/queue-usage.ts +7 -8
- package/src/repositories/rows.ts +10 -29
- package/src/runtime.ts +27 -6
- package/src/services/acquisition.ts +178 -52
- package/src/services/delivery.ts +39 -19
- package/src/services/management.ts +88 -51
- package/src/services/providers/service.ts +100 -10
- package/src/models/installation.ts +0 -37
package/LICENSE
CHANGED
|
@@ -2,6 +2,9 @@ MIT License
|
|
|
2
2
|
|
|
3
3
|
Copyright (c) [year] [fullname]
|
|
4
4
|
|
|
5
|
+
The Next.js Flare adaptation in apps/web/frontend/src/components/brand/flare:
|
|
6
|
+
Copyright (c) 2025 Vercel, Inc.
|
|
7
|
+
|
|
5
8
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
9
|
of this software and associated documentation files (the "Software"), to deal
|
|
7
10
|
in the Software without restriction, including without limitation the rights
|
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-use/open-sync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/open-sync.ts",
|
|
7
7
|
"./definition": "./src/models/definition.ts",
|
|
8
|
+
"./record": "./src/models/record.ts",
|
|
8
9
|
"./delivery": "./src/models/delivery.ts",
|
|
9
10
|
"./json": "./src/models/json.ts",
|
|
10
11
|
"./http": "./src/http/index.ts",
|
|
11
12
|
"./engine": "./src/runtime.ts",
|
|
12
|
-
"./build": "./src/build.ts"
|
|
13
|
+
"./build": "./src/build.ts",
|
|
14
|
+
"./assets": "./src/models/asset.ts"
|
|
13
15
|
},
|
|
14
16
|
"files": [
|
|
15
17
|
"src"
|
|
@@ -22,12 +24,12 @@
|
|
|
22
24
|
},
|
|
23
25
|
"dependencies": {
|
|
24
26
|
"@cfworker/json-schema": "4.1.1",
|
|
27
|
+
"@oomol-lab/open-connector": "1.7.0",
|
|
25
28
|
"elysia": "^1.4.30",
|
|
26
|
-
"minisearch": "^7.2.0"
|
|
27
|
-
"@oomol-lab/open-connector": "1.6.3"
|
|
29
|
+
"minisearch": "^7.2.0"
|
|
28
30
|
},
|
|
29
31
|
"devDependencies": {
|
|
30
|
-
"typescript": "
|
|
32
|
+
"typescript": "6.0.3"
|
|
31
33
|
},
|
|
32
34
|
"engines": {
|
|
33
35
|
"bun": ">=1.4.0"
|
package/src/api.ts
CHANGED
|
@@ -6,17 +6,18 @@ export type SyncApi = Pick<
|
|
|
6
6
|
SyncManagement,
|
|
7
7
|
| 'definitions'
|
|
8
8
|
| 'destinationTypes'
|
|
9
|
-
| '
|
|
10
|
-
| '
|
|
11
|
-
| '
|
|
12
|
-
| '
|
|
13
|
-
| '
|
|
14
|
-
| 'installations'
|
|
15
|
-
| 'installation'
|
|
9
|
+
| 'createSync'
|
|
10
|
+
| 'connectSync'
|
|
11
|
+
| 'syncs'
|
|
12
|
+
| 'sync'
|
|
13
|
+
| 'polls'
|
|
16
14
|
| 'setEnabled'
|
|
17
|
-
| '
|
|
15
|
+
| 'runNow'
|
|
16
|
+
| 'resync'
|
|
17
|
+
| 'removeSync'
|
|
18
18
|
| 'status'
|
|
19
19
|
| 'deliveries'
|
|
20
|
+
| 'deliverable'
|
|
20
21
|
| 'retryDelivery'
|
|
21
22
|
>;
|
|
22
23
|
export type ProviderApi = Pick<
|
|
@@ -27,18 +28,19 @@ export type ProviderApi = Pick<
|
|
|
27
28
|
export function syncApi(service: SyncManagement): SyncApi {
|
|
28
29
|
return {
|
|
29
30
|
definitions: service.definitions.bind(service),
|
|
30
|
-
destinations: service.destinations.bind(service),
|
|
31
31
|
destinationTypes: service.destinationTypes.bind(service),
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
installation: service.installation.bind(service),
|
|
32
|
+
createSync: service.createSync.bind(service),
|
|
33
|
+
connectSync: service.connectSync.bind(service),
|
|
34
|
+
syncs: service.syncs.bind(service),
|
|
35
|
+
sync: service.sync.bind(service),
|
|
36
|
+
polls: service.polls.bind(service),
|
|
38
37
|
setEnabled: service.setEnabled.bind(service),
|
|
39
|
-
|
|
38
|
+
runNow: service.runNow.bind(service),
|
|
39
|
+
resync: service.resync.bind(service),
|
|
40
|
+
removeSync: service.removeSync.bind(service),
|
|
40
41
|
status: service.status.bind(service),
|
|
41
42
|
deliveries: service.deliveries.bind(service),
|
|
43
|
+
deliverable: service.deliverable.bind(service),
|
|
42
44
|
retryDelivery: service.retryDelivery.bind(service),
|
|
43
45
|
};
|
|
44
46
|
}
|
package/src/connector/client.ts
CHANGED
|
@@ -2,10 +2,15 @@ import type { ProviderGateway } from '../execution/provider';
|
|
|
2
2
|
import type { ConnectionRef, ProviderOperations, ProviderRequirements } from '../models/definition';
|
|
3
3
|
import { fail } from '../models/error';
|
|
4
4
|
import type { Scope } from '../models/identity';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import type { JsonObject, JsonValue } from '../models/json';
|
|
6
|
+
import { matchesProviderPath } from '../models/provider-path';
|
|
7
|
+
import { connectorFailure } from './failure';
|
|
8
|
+
import { downloadFile } from './file';
|
|
9
|
+
import { connectorData, providerResponse } from './response';
|
|
7
10
|
|
|
8
11
|
interface RequestInput {
|
|
12
|
+
service: string;
|
|
13
|
+
operation: string;
|
|
9
14
|
path: string;
|
|
10
15
|
admin?: boolean;
|
|
11
16
|
body?: JsonObject;
|
|
@@ -39,20 +44,20 @@ export function createConnectorClient(options: ConnectorClientOptions): Provider
|
|
|
39
44
|
if (input.body) {
|
|
40
45
|
headers.set('content-type', 'application/json');
|
|
41
46
|
}
|
|
42
|
-
const response = await options
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return
|
|
47
|
+
const response = await options
|
|
48
|
+
.fetch(
|
|
49
|
+
new Request(`${base}${input.path}`, {
|
|
50
|
+
method: input.body ? 'POST' : 'GET',
|
|
51
|
+
headers,
|
|
52
|
+
signal: input.signal,
|
|
53
|
+
body: input.body ? JSON.stringify(input.body) : undefined,
|
|
54
|
+
}),
|
|
55
|
+
)
|
|
56
|
+
.catch(() => {
|
|
57
|
+
input.signal.throwIfAborted();
|
|
58
|
+
throw connectorFailure({ ...input, kind: 'transport' });
|
|
59
|
+
});
|
|
60
|
+
return connectorData({ ...input, response });
|
|
56
61
|
}
|
|
57
62
|
return {
|
|
58
63
|
async bind(input) {
|
|
@@ -69,6 +74,8 @@ export function createConnectorClient(options: ConnectorClientOptions): Provider
|
|
|
69
74
|
fail('connection_mismatch');
|
|
70
75
|
}
|
|
71
76
|
const metadata = (await request({
|
|
77
|
+
service: input.connection.service,
|
|
78
|
+
operation: 'connection.read',
|
|
72
79
|
path: `/v1/connections/by-id/${encodeURIComponent(input.connection.id)}`,
|
|
73
80
|
admin: true,
|
|
74
81
|
signal: input.signal,
|
|
@@ -76,6 +83,7 @@ export function createConnectorClient(options: ConnectorClientOptions): Provider
|
|
|
76
83
|
checkConnection({ metadata, connection: input.connection, requirements: input.requirements });
|
|
77
84
|
return operations({
|
|
78
85
|
request,
|
|
86
|
+
download: (file) => downloadFile({ ...options, ...file, signal: input.signal }),
|
|
79
87
|
requirements: input.requirements,
|
|
80
88
|
alias: metadata.alias as string,
|
|
81
89
|
signal: input.signal,
|
|
@@ -101,22 +109,43 @@ function checkConnection(input: {
|
|
|
101
109
|
}
|
|
102
110
|
function operations(input: {
|
|
103
111
|
request(input: RequestInput): Promise<JsonValue>;
|
|
112
|
+
download(input: {
|
|
113
|
+
file: JsonValue;
|
|
114
|
+
service: string;
|
|
115
|
+
operation: string;
|
|
116
|
+
}): Promise<ReadableStream<Uint8Array>>;
|
|
104
117
|
requirements: ProviderRequirements;
|
|
105
118
|
alias: string;
|
|
106
119
|
signal: AbortSignal;
|
|
107
120
|
}): ProviderOperations {
|
|
108
121
|
const { request, requirements, alias, signal } = input;
|
|
122
|
+
const action: ProviderOperations['action'] = async (operation) => {
|
|
123
|
+
if (!requirements.actions.includes(operation.id)) {
|
|
124
|
+
fail('operation_denied');
|
|
125
|
+
}
|
|
126
|
+
const path = `/v1/actions/${encodeURIComponent(operation.id)}`;
|
|
127
|
+
const diagnostics = { service: requirements.service, operation: operation.id };
|
|
128
|
+
const metadata = (await request({ ...diagnostics, path, signal })) as JsonObject;
|
|
129
|
+
if (metadata.service !== requirements.service) {
|
|
130
|
+
fail('operation_denied');
|
|
131
|
+
}
|
|
132
|
+
return await request({
|
|
133
|
+
...diagnostics,
|
|
134
|
+
path,
|
|
135
|
+
body: { input: operation.input },
|
|
136
|
+
alias,
|
|
137
|
+
signal,
|
|
138
|
+
});
|
|
139
|
+
};
|
|
109
140
|
return {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
return await request({ path, body: { input: operation.input }, alias, signal });
|
|
141
|
+
action,
|
|
142
|
+
async download(operation) {
|
|
143
|
+
const result = await action(operation);
|
|
144
|
+
const file =
|
|
145
|
+
operation.fileField && result && typeof result === 'object' && !Array.isArray(result)
|
|
146
|
+
? (result[operation.fileField] ?? null)
|
|
147
|
+
: result;
|
|
148
|
+
return input.download({ file, service: requirements.service, operation: operation.id });
|
|
120
149
|
},
|
|
121
150
|
get(operation) {
|
|
122
151
|
const { path } = operation;
|
|
@@ -124,11 +153,13 @@ function operations(input: {
|
|
|
124
153
|
!path.startsWith('/') ||
|
|
125
154
|
path.startsWith('//') ||
|
|
126
155
|
/[\\?#]/.test(path) ||
|
|
127
|
-
!requirements.proxyPaths
|
|
156
|
+
!matchesProviderPath({ path, allowed: requirements.proxyPaths })
|
|
128
157
|
) {
|
|
129
158
|
fail('operation_denied');
|
|
130
159
|
}
|
|
131
160
|
return request({
|
|
161
|
+
service: requirements.service,
|
|
162
|
+
operation: path,
|
|
132
163
|
path: `/v1/proxy/${encodeURIComponent(requirements.service)}`,
|
|
133
164
|
body: { endpoint: path, method: 'GET', query: operation.query ?? {} },
|
|
134
165
|
alias,
|
|
@@ -141,11 +172,13 @@ function operations(input: {
|
|
|
141
172
|
!path.startsWith('/') ||
|
|
142
173
|
path.startsWith('//') ||
|
|
143
174
|
/[\\?#]/.test(path) ||
|
|
144
|
-
!requirements.proxyPostPaths
|
|
175
|
+
!matchesProviderPath({ path, allowed: requirements.proxyPostPaths })
|
|
145
176
|
) {
|
|
146
177
|
fail('operation_denied');
|
|
147
178
|
}
|
|
148
179
|
return request({
|
|
180
|
+
service: requirements.service,
|
|
181
|
+
operation: path,
|
|
149
182
|
path: `/v1/proxy/${encodeURIComponent(requirements.service)}`,
|
|
150
183
|
body: { endpoint: path, method: 'POST', body: operation.body },
|
|
151
184
|
alias,
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { SyncError } from '../models/error';
|
|
2
|
+
import type { JsonObject } from '../models/json';
|
|
3
|
+
import { isErrorStatus } from '../models/source-http-error';
|
|
4
|
+
|
|
5
|
+
/** Only protocol metadata crosses this boundary; messages and payloads can contain credentials. */
|
|
6
|
+
export function connectorFailure(input: {
|
|
7
|
+
service: string;
|
|
8
|
+
operation: string;
|
|
9
|
+
kind: 'transport' | 'invalid_response' | 'rejected';
|
|
10
|
+
response?: Response;
|
|
11
|
+
body?: unknown;
|
|
12
|
+
}): SyncError {
|
|
13
|
+
const body = object(input.body);
|
|
14
|
+
const code = body?.errorCode;
|
|
15
|
+
const providerStatus = object(body?.data)?.status;
|
|
16
|
+
const diagnostics: JsonObject = {
|
|
17
|
+
service: input.service,
|
|
18
|
+
operation: input.operation,
|
|
19
|
+
failureKind: input.kind,
|
|
20
|
+
};
|
|
21
|
+
if (input.response) {
|
|
22
|
+
diagnostics.connectorStatus = input.response.status;
|
|
23
|
+
}
|
|
24
|
+
if (typeof code === 'string' && /^[a-z][a-z0-9_]{0,63}$/.test(code)) {
|
|
25
|
+
diagnostics.connectorErrorCode = code;
|
|
26
|
+
}
|
|
27
|
+
const minStatus = 100;
|
|
28
|
+
const maxStatus = 599;
|
|
29
|
+
if (
|
|
30
|
+
Number.isInteger(providerStatus) &&
|
|
31
|
+
Number(providerStatus) >= minStatus &&
|
|
32
|
+
Number(providerStatus) <= maxStatus
|
|
33
|
+
) {
|
|
34
|
+
diagnostics.providerStatus = Number(providerStatus);
|
|
35
|
+
}
|
|
36
|
+
const status = input.response?.status;
|
|
37
|
+
return new SyncError({
|
|
38
|
+
code: 'connector_request_failed',
|
|
39
|
+
message: 'connector request failed',
|
|
40
|
+
diagnostics,
|
|
41
|
+
status: isErrorStatus(status) ? status : undefined,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function object(value: unknown): Record<string, unknown> | undefined {
|
|
46
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
47
|
+
? (value as Record<string, unknown>)
|
|
48
|
+
: undefined;
|
|
49
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { SyncError } from '../models/error';
|
|
2
|
+
import type { JsonValue } from '../models/json';
|
|
3
|
+
import { connectorFailure } from './failure';
|
|
4
|
+
|
|
5
|
+
/** File IDs come only from a just-authorized action, never from source-supplied URLs. */
|
|
6
|
+
export async function downloadFile(input: {
|
|
7
|
+
file: JsonValue;
|
|
8
|
+
service: string;
|
|
9
|
+
operation: string;
|
|
10
|
+
fetch(request: Request): Promise<Response>;
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
adminToken: string;
|
|
13
|
+
runtimeToken: string;
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
}): Promise<ReadableStream<Uint8Array>> {
|
|
16
|
+
const fileId =
|
|
17
|
+
input.file && typeof input.file === 'object' && !Array.isArray(input.file)
|
|
18
|
+
? input.file.fileId
|
|
19
|
+
: undefined;
|
|
20
|
+
if (typeof fileId !== 'string' || !/^[a-f0-9]{32}(?:\.[a-z0-9]{1,16})?$/.test(fileId)) {
|
|
21
|
+
throw connectorFailure({ ...input, kind: 'invalid_response' });
|
|
22
|
+
}
|
|
23
|
+
const url = `${input.baseUrl.replace(/\/$/, '')}/api/files/${encodeURIComponent(fileId)}`;
|
|
24
|
+
const remove = async () => {
|
|
25
|
+
// Cleanup must outlive caller cancellation. Connector expiry remains the fallback on failure.
|
|
26
|
+
const timeoutMs = 5000;
|
|
27
|
+
await input
|
|
28
|
+
.fetch(
|
|
29
|
+
new Request(url, {
|
|
30
|
+
method: 'DELETE',
|
|
31
|
+
headers: { authorization: `Bearer ${input.adminToken}` },
|
|
32
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
33
|
+
}),
|
|
34
|
+
)
|
|
35
|
+
.then((response) => response.body?.cancel())
|
|
36
|
+
.catch(() => undefined);
|
|
37
|
+
};
|
|
38
|
+
let response: Response;
|
|
39
|
+
try {
|
|
40
|
+
input.signal.throwIfAborted();
|
|
41
|
+
response = await input.fetch(
|
|
42
|
+
new Request(url, {
|
|
43
|
+
headers: { authorization: `Bearer ${input.runtimeToken}` },
|
|
44
|
+
signal: input.signal,
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
if (!response.ok || !response.body) {
|
|
48
|
+
await response.body?.cancel();
|
|
49
|
+
throw connectorFailure({ ...input, response, kind: 'rejected' });
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
await remove();
|
|
53
|
+
input.signal.throwIfAborted();
|
|
54
|
+
if (error instanceof SyncError) {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
throw connectorFailure({ ...input, kind: 'transport' });
|
|
58
|
+
}
|
|
59
|
+
const reader = response.body!.getReader();
|
|
60
|
+
let cleanup: Promise<void> | undefined;
|
|
61
|
+
let abort: () => void;
|
|
62
|
+
const finish = () => {
|
|
63
|
+
cleanup ??= (async () => {
|
|
64
|
+
input.signal.removeEventListener('abort', abort);
|
|
65
|
+
await reader.cancel().catch(() => undefined);
|
|
66
|
+
reader.releaseLock();
|
|
67
|
+
await remove();
|
|
68
|
+
})();
|
|
69
|
+
return cleanup;
|
|
70
|
+
};
|
|
71
|
+
return new ReadableStream<Uint8Array>({
|
|
72
|
+
start(controller) {
|
|
73
|
+
abort = () => {
|
|
74
|
+
controller.error(input.signal.reason);
|
|
75
|
+
void finish();
|
|
76
|
+
};
|
|
77
|
+
input.signal.addEventListener('abort', abort, { once: true });
|
|
78
|
+
if (input.signal.aborted) {
|
|
79
|
+
abort();
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
async pull(controller) {
|
|
83
|
+
try {
|
|
84
|
+
input.signal.throwIfAborted();
|
|
85
|
+
const chunk = await reader.read();
|
|
86
|
+
input.signal.throwIfAborted();
|
|
87
|
+
if (chunk.done) {
|
|
88
|
+
await finish();
|
|
89
|
+
controller.close();
|
|
90
|
+
} else {
|
|
91
|
+
controller.enqueue(chunk.value);
|
|
92
|
+
}
|
|
93
|
+
} catch {
|
|
94
|
+
await finish();
|
|
95
|
+
controller.error(
|
|
96
|
+
input.signal.aborted
|
|
97
|
+
? input.signal.reason
|
|
98
|
+
: connectorFailure({ ...input, kind: 'transport' }),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
cancel: finish,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -1,7 +1,28 @@
|
|
|
1
1
|
import type { ProviderResponse } from '../models/definition';
|
|
2
2
|
import { fail } from '../models/error';
|
|
3
|
-
import type
|
|
3
|
+
import { canonicalJson, type JsonValue } from '../models/json';
|
|
4
4
|
import { validate } from '../models/validation';
|
|
5
|
+
import { connectorFailure } from './failure';
|
|
6
|
+
|
|
7
|
+
export async function connectorData(input: {
|
|
8
|
+
response: Response;
|
|
9
|
+
service: string;
|
|
10
|
+
operation: string;
|
|
11
|
+
signal: AbortSignal;
|
|
12
|
+
}): Promise<JsonValue> {
|
|
13
|
+
const result = (await input.response.json().catch(() => undefined)) as
|
|
14
|
+
| { success?: boolean; data?: JsonValue }
|
|
15
|
+
| undefined;
|
|
16
|
+
input.signal.throwIfAborted();
|
|
17
|
+
if (!input.response.ok || result?.success !== true || result.data === undefined) {
|
|
18
|
+
throw connectorFailure({
|
|
19
|
+
...input,
|
|
20
|
+
kind: result?.success === false ? 'rejected' : 'invalid_response',
|
|
21
|
+
body: result,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return canonicalJson(result.data).value;
|
|
25
|
+
}
|
|
5
26
|
|
|
6
27
|
/** Connector's wire envelope ends here; definitions only consume the Open Sync contract. */
|
|
7
28
|
export function providerResponse(value: unknown): ProviderResponse {
|
package/src/db/client.ts
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import { Database } from 'bun:sqlite';
|
|
2
|
-
import { fail } from '../models/error';
|
|
3
2
|
import schema from './schema.sql' with { type: 'text' };
|
|
4
3
|
|
|
5
4
|
export function openDatabase(path: string): Database {
|
|
6
5
|
const db = new Database(path, { create: true, strict: true });
|
|
7
6
|
try {
|
|
8
7
|
db.exec('PRAGMA foreign_keys=ON; PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000;');
|
|
9
|
-
db.transaction(() =>
|
|
10
|
-
const { user_version: version } = db
|
|
11
|
-
.query<{ user_version: number }, []>('PRAGMA user_version')
|
|
12
|
-
.get()!;
|
|
13
|
-
if (version === 0) {
|
|
14
|
-
db.exec(schema);
|
|
15
|
-
} else if (version !== 1) {
|
|
16
|
-
fail('schema_version');
|
|
17
|
-
}
|
|
18
|
-
}).immediate();
|
|
8
|
+
db.transaction(() => db.exec(schema)).immediate();
|
|
19
9
|
return db;
|
|
20
10
|
} catch (error) {
|
|
21
11
|
db.close();
|
package/src/db/providers.sql
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
CREATE TABLE provider_authorizations (
|
|
1
|
+
CREATE TABLE IF NOT EXISTS provider_authorizations (
|
|
2
2
|
owner_id TEXT NOT NULL PRIMARY KEY,
|
|
3
3
|
id TEXT NOT NULL UNIQUE,
|
|
4
4
|
request_id TEXT NOT NULL UNIQUE,
|
|
5
5
|
service TEXT NOT NULL
|
|
6
6
|
);
|
|
7
|
-
CREATE TABLE provider_connections (
|
|
7
|
+
CREATE TABLE IF NOT EXISTS provider_connections (
|
|
8
8
|
owner_id TEXT NOT NULL,
|
|
9
9
|
id TEXT NOT NULL,
|
|
10
10
|
connector_id TEXT NOT NULL UNIQUE,
|
|
@@ -12,4 +12,3 @@ CREATE TABLE provider_connections (
|
|
|
12
12
|
service TEXT NOT NULL,
|
|
13
13
|
PRIMARY KEY (owner_id, id)
|
|
14
14
|
);
|
|
15
|
-
PRAGMA user_version=1;
|
package/src/db/providers.ts
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
1
|
import { Database } from 'bun:sqlite';
|
|
2
|
-
import { fail } from '../models/error';
|
|
3
2
|
import schema from './providers.sql' with { type: 'text' };
|
|
4
3
|
|
|
5
4
|
export function openProviderDatabase(path: string) {
|
|
6
5
|
const db = new Database(path, { create: true, strict: true });
|
|
7
6
|
try {
|
|
8
7
|
db.exec('PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000');
|
|
9
|
-
db.transaction(() =>
|
|
10
|
-
const version = db
|
|
11
|
-
.query<{ user_version: number }, []>('PRAGMA user_version')
|
|
12
|
-
.get()!.user_version;
|
|
13
|
-
if (version === 0) {
|
|
14
|
-
db.exec(schema);
|
|
15
|
-
} else if (version !== 1) {
|
|
16
|
-
fail('provider_schema_version');
|
|
17
|
-
}
|
|
18
|
-
}).immediate();
|
|
8
|
+
db.transaction(() => db.exec(schema)).immediate();
|
|
19
9
|
return db;
|
|
20
10
|
} catch (error) {
|
|
21
11
|
db.close();
|
package/src/db/schema.sql
CHANGED
|
@@ -1,45 +1,50 @@
|
|
|
1
|
-
CREATE TABLE
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
owner_id TEXT NOT NULL, id TEXT NOT NULL, type TEXT NOT NULL, version TEXT NOT NULL, config TEXT NOT NULL,
|
|
7
|
-
PRIMARY KEY(owner_id, id)
|
|
8
|
-
);
|
|
9
|
-
CREATE TABLE installations (
|
|
10
|
-
owner_id TEXT NOT NULL, id TEXT NOT NULL, source_id TEXT NOT NULL,
|
|
11
|
-
definition_id TEXT NOT NULL, definition_version TEXT NOT NULL, artifact_id TEXT NOT NULL,
|
|
12
|
-
connection TEXT, config TEXT NOT NULL, destination_id TEXT NOT NULL,
|
|
13
|
-
enabled INTEGER NOT NULL, binding_epoch INTEGER NOT NULL DEFAULT 1,
|
|
14
|
-
checkpoint TEXT NOT NULL, checkpoint_revision INTEGER NOT NULL DEFAULT 0,
|
|
1
|
+
CREATE TABLE IF NOT EXISTS syncs (
|
|
2
|
+
owner_id TEXT NOT NULL, id TEXT NOT NULL, definition_id TEXT NOT NULL,
|
|
3
|
+
connection TEXT, config TEXT NOT NULL, destination_type TEXT NOT NULL, destination_config TEXT NOT NULL,
|
|
4
|
+
enabled INTEGER NOT NULL,
|
|
5
|
+
checkpoint TEXT NOT NULL,
|
|
15
6
|
interval_ms INTEGER NOT NULL, next_due_at INTEGER NOT NULL, status TEXT NOT NULL,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
owner_id TEXT NOT NULL, id TEXT NOT NULL, installation_id TEXT NOT NULL,
|
|
21
|
-
definition_ref TEXT NOT NULL, binding_epoch INTEGER NOT NULL,
|
|
22
|
-
worker_id TEXT NOT NULL, generation INTEGER NOT NULL, expires_at INTEGER NOT NULL,
|
|
23
|
-
checkpoint_revision INTEGER NOT NULL, state TEXT NOT NULL, started_at INTEGER NOT NULL,
|
|
24
|
-
completed_at INTEGER, pages INTEGER NOT NULL DEFAULT 0,
|
|
25
|
-
PRIMARY KEY(owner_id, id), FOREIGN KEY(owner_id, installation_id) REFERENCES installations(owner_id, id)
|
|
7
|
+
error_code TEXT,
|
|
8
|
+
generation INTEGER NOT NULL DEFAULT 0, expires_at INTEGER,
|
|
9
|
+
failure_count INTEGER NOT NULL DEFAULT 0, resync INTEGER NOT NULL DEFAULT 0,
|
|
10
|
+
PRIMARY KEY(owner_id, id)
|
|
26
11
|
);
|
|
27
|
-
CREATE
|
|
28
|
-
|
|
29
|
-
owner_id TEXT NOT NULL, installation_id TEXT NOT NULL, kind TEXT NOT NULL, id TEXT NOT NULL,
|
|
12
|
+
CREATE TABLE IF NOT EXISTS record_state (
|
|
13
|
+
owner_id TEXT NOT NULL, sync_id TEXT NOT NULL, kind TEXT NOT NULL, id TEXT NOT NULL,
|
|
30
14
|
hash TEXT NOT NULL, revision INTEGER NOT NULL, deleted INTEGER NOT NULL,
|
|
31
|
-
PRIMARY KEY(owner_id,
|
|
32
|
-
FOREIGN KEY(owner_id,
|
|
15
|
+
PRIMARY KEY(owner_id, sync_id, kind, id),
|
|
16
|
+
FOREIGN KEY(owner_id, sync_id) REFERENCES syncs(owner_id, id)
|
|
17
|
+
);
|
|
18
|
+
-- Polling diagnostics only; execution leases and scheduling belong to syncs.
|
|
19
|
+
CREATE TABLE IF NOT EXISTS sync_polls (
|
|
20
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT, owner_id TEXT NOT NULL, sync_id TEXT NOT NULL,
|
|
21
|
+
started_at INTEGER NOT NULL, completed_at INTEGER,
|
|
22
|
+
records_processed INTEGER NOT NULL DEFAULT 0, records_queued INTEGER NOT NULL DEFAULT 0,
|
|
23
|
+
state TEXT NOT NULL, error_code TEXT,
|
|
24
|
+
FOREIGN KEY(owner_id, sync_id) REFERENCES syncs(owner_id, id) ON DELETE CASCADE
|
|
33
25
|
);
|
|
34
|
-
CREATE
|
|
26
|
+
CREATE INDEX IF NOT EXISTS sync_poll_history ON sync_polls(owner_id,sync_id,id);
|
|
27
|
+
CREATE UNIQUE INDEX IF NOT EXISTS sync_poll_active ON sync_polls(owner_id,sync_id) WHERE completed_at IS NULL;
|
|
28
|
+
CREATE TABLE IF NOT EXISTS deliveries (
|
|
35
29
|
sequence INTEGER PRIMARY KEY AUTOINCREMENT, owner_id TEXT NOT NULL, id TEXT NOT NULL,
|
|
36
|
-
|
|
30
|
+
sync_id TEXT NOT NULL, body TEXT NOT NULL,
|
|
37
31
|
bytes INTEGER NOT NULL, record_count INTEGER NOT NULL, state TEXT NOT NULL DEFAULT 'pending',
|
|
38
|
-
due_at INTEGER NOT NULL,
|
|
32
|
+
due_at INTEGER NOT NULL, generation INTEGER NOT NULL DEFAULT 0,
|
|
39
33
|
expires_at INTEGER, attempt INTEGER NOT NULL DEFAULT 0, error_code TEXT,
|
|
40
34
|
UNIQUE(owner_id, id),
|
|
41
|
-
FOREIGN KEY(owner_id,
|
|
42
|
-
|
|
35
|
+
FOREIGN KEY(owner_id, sync_id) REFERENCES syncs(owner_id, id)
|
|
36
|
+
);
|
|
37
|
+
CREATE INDEX IF NOT EXISTS delivery_order ON deliveries(owner_id,sync_id,sequence);
|
|
38
|
+
CREATE INDEX IF NOT EXISTS deliveries_due ON deliveries(state, due_at);
|
|
39
|
+
-- Staging, queued bytes, and pending cleanup share one delivery-owned ledger.
|
|
40
|
+
-- No foreign keys: cleanup must survive deletion of the delivery or sync.
|
|
41
|
+
CREATE TABLE IF NOT EXISTS delivery_assets (
|
|
42
|
+
id TEXT PRIMARY KEY, owner_id TEXT NOT NULL, sync_id TEXT NOT NULL,
|
|
43
|
+
generation INTEGER NOT NULL,
|
|
44
|
+
asset_id TEXT NOT NULL, asset_version TEXT NOT NULL,
|
|
45
|
+
descriptor TEXT NOT NULL, ready INTEGER NOT NULL DEFAULT 0,
|
|
46
|
+
bytes INTEGER NOT NULL DEFAULT 0 CHECK(bytes>=0), delivery_id TEXT,
|
|
47
|
+
UNIQUE(owner_id,sync_id,generation,asset_id,asset_version)
|
|
43
48
|
);
|
|
44
|
-
CREATE INDEX
|
|
45
|
-
|
|
49
|
+
CREATE INDEX IF NOT EXISTS delivery_asset_queue ON delivery_assets(owner_id,delivery_id);
|
|
50
|
+
CREATE INDEX IF NOT EXISTS delivery_asset_sync ON delivery_assets(owner_id,sync_id);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Stop waiting for trusted async code that ignores cancellation; late results cannot commit. */
|
|
2
|
+
export async function abortable<T>(input: { signal: AbortSignal; run(): Promise<T> }): Promise<T> {
|
|
3
|
+
input.signal.throwIfAborted();
|
|
4
|
+
const stopped = Promise.withResolvers<never>();
|
|
5
|
+
const abort = () => stopped.reject(input.signal.reason);
|
|
6
|
+
input.signal.addEventListener('abort', abort, { once: true });
|
|
7
|
+
try {
|
|
8
|
+
return await Promise.race([input.run(), stopped.promise]);
|
|
9
|
+
} finally {
|
|
10
|
+
input.signal.removeEventListener('abort', abort);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -12,6 +12,7 @@ export interface ProviderGateway {
|
|
|
12
12
|
}
|
|
13
13
|
export const noProvider: ProviderOperations = {
|
|
14
14
|
action: () => Promise.reject(new Error('operation_denied')),
|
|
15
|
+
download: () => Promise.reject(new Error('operation_denied')),
|
|
15
16
|
get: () => Promise.reject(new Error('operation_denied')),
|
|
16
17
|
post: () => Promise.reject(new Error('operation_denied')),
|
|
17
18
|
};
|