@context-use/open-sync 0.2.0 → 0.3.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.
Files changed (59) hide show
  1. package/LICENSE +3 -0
  2. package/package.json +7 -4
  3. package/src/api.ts +18 -14
  4. package/src/build.ts +3 -21
  5. package/src/connector/client.ts +61 -28
  6. package/src/connector/failure.ts +49 -0
  7. package/src/connector/file.ts +104 -0
  8. package/src/connector/response.ts +22 -1
  9. package/src/db/client.ts +1 -11
  10. package/src/db/providers.sql +2 -3
  11. package/src/db/providers.ts +1 -11
  12. package/src/db/schema.sql +41 -36
  13. package/src/execution/abortable.ts +12 -0
  14. package/src/execution/diagnostics.ts +1 -2
  15. package/src/execution/provider.ts +1 -0
  16. package/src/execution/worker.ts +162 -39
  17. package/src/http/controller.ts +63 -42
  18. package/src/http/providers.ts +13 -0
  19. package/src/models/asset.ts +61 -0
  20. package/src/models/definition.ts +24 -15
  21. package/src/models/delivery.ts +25 -31
  22. package/src/models/error.ts +12 -1
  23. package/src/models/limits.ts +8 -3
  24. package/src/models/metadata.ts +42 -0
  25. package/src/models/page.ts +68 -13
  26. package/src/models/provider-catalog.ts +57 -0
  27. package/src/models/provider-path.ts +25 -0
  28. package/src/models/providers.ts +13 -2
  29. package/src/models/record.ts +22 -0
  30. package/src/models/registry.ts +15 -15
  31. package/src/models/source-http-error.ts +37 -0
  32. package/src/models/sync.ts +71 -0
  33. package/src/models/validation.ts +1 -1
  34. package/src/open-sync.ts +31 -7
  35. package/src/repositories/acquisition/contract.ts +18 -10
  36. package/src/repositories/acquisition/lease.ts +80 -94
  37. package/src/repositories/acquisition/outbox.ts +22 -22
  38. package/src/repositories/acquisition/records.ts +44 -13
  39. package/src/repositories/acquisition/sqlite.ts +78 -31
  40. package/src/repositories/assets/capacity.ts +16 -0
  41. package/src/repositories/assets/contract.ts +25 -0
  42. package/src/repositories/assets/filesystem.ts +103 -0
  43. package/src/repositories/assets/sqlite.ts +174 -0
  44. package/src/repositories/catalog/contract.ts +16 -18
  45. package/src/repositories/catalog/sqlite.ts +107 -115
  46. package/src/repositories/delivery/contract.ts +7 -11
  47. package/src/repositories/delivery/lease.ts +16 -0
  48. package/src/repositories/delivery/sqlite.ts +57 -34
  49. package/src/repositories/providers/contract.ts +2 -1
  50. package/src/repositories/providers/sqlite.ts +22 -6
  51. package/src/repositories/queue-usage.ts +7 -8
  52. package/src/repositories/rows.ts +10 -29
  53. package/src/runtime.ts +27 -6
  54. package/src/services/acquisition.ts +178 -52
  55. package/src/services/delivery.ts +39 -19
  56. package/src/services/management.ts +108 -44
  57. package/src/services/providers/service.ts +187 -31
  58. package/src/build/connector.ts +0 -143
  59. 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.2.0",
3
+ "version": "0.3.0",
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,11 +24,12 @@
22
24
  },
23
25
  "dependencies": {
24
26
  "@cfworker/json-schema": "4.1.1",
27
+ "@oomol-lab/open-connector": "1.6.5",
25
28
  "elysia": "^1.4.30",
26
- "@oomol-lab/open-connector": "1.6.0"
29
+ "minisearch": "^7.2.0"
27
30
  },
28
31
  "devDependencies": {
29
- "typescript": "^7.0.2"
32
+ "typescript": "6.0.3"
30
33
  },
31
34
  "engines": {
32
35
  "bun": ">=1.4.0"
package/src/api.ts CHANGED
@@ -6,16 +6,18 @@ export type SyncApi = Pick<
6
6
  SyncManagement,
7
7
  | 'definitions'
8
8
  | 'destinationTypes'
9
- | 'runs'
10
- | 'destinations'
11
- | 'createDestination'
12
- | 'createInstallation'
13
- | 'installations'
14
- | 'installation'
9
+ | 'createSync'
10
+ | 'connectSync'
11
+ | 'syncs'
12
+ | 'sync'
13
+ | 'polls'
15
14
  | 'setEnabled'
16
- | 'queueRun'
15
+ | 'runNow'
16
+ | 'resync'
17
+ | 'removeSync'
17
18
  | 'status'
18
19
  | 'deliveries'
20
+ | 'deliverable'
19
21
  | 'retryDelivery'
20
22
  >;
21
23
  export type ProviderApi = Pick<
@@ -26,17 +28,19 @@ export type ProviderApi = Pick<
26
28
  export function syncApi(service: SyncManagement): SyncApi {
27
29
  return {
28
30
  definitions: service.definitions.bind(service),
29
- destinations: service.destinations.bind(service),
30
31
  destinationTypes: service.destinationTypes.bind(service),
31
- runs: service.runs.bind(service),
32
- createDestination: service.createDestination.bind(service),
33
- createInstallation: service.createInstallation.bind(service),
34
- installations: service.installations.bind(service),
35
- 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),
36
37
  setEnabled: service.setEnabled.bind(service),
37
- queueRun: service.queueRun.bind(service),
38
+ runNow: service.runNow.bind(service),
39
+ resync: service.resync.bind(service),
40
+ removeSync: service.removeSync.bind(service),
38
41
  status: service.status.bind(service),
39
42
  deliveries: service.deliveries.bind(service),
43
+ deliverable: service.deliverable.bind(service),
40
44
  retryDelivery: service.retryDelivery.bind(service),
41
45
  };
42
46
  }
package/src/build.ts CHANGED
@@ -1,8 +1,4 @@
1
- import { mkdtemp, rm } from 'node:fs/promises';
2
- import { tmpdir } from 'node:os';
3
- import { dirname, join } from 'node:path';
4
- import { fileURLToPath } from 'node:url';
5
- import { prepareConnector } from './build/connector';
1
+ import { getConnectorBuildOptions } from '@oomol-lab/open-connector/build';
6
2
  import type { SyncRegistration } from './models/definition';
7
3
 
8
4
  /** Provider dependencies declared by registrations, without loading their sync executables. */
@@ -21,20 +17,6 @@ export function providersFromDefinitions(definitions: readonly SyncRegistration[
21
17
  * to compile.assets. Await dispose() after the build (also on failure); it removes staged files.
22
18
  * An empty provider list packages no providers. The installed dependency is never modified.
23
19
  */
24
- export async function getOpenSyncBuildOptions(options: { providers: readonly string[] }) {
25
- const directory = await mkdtemp(join(tmpdir(), 'open-sync-build-'));
26
- const dispose = () => rm(directory, { recursive: true, force: true });
27
- try {
28
- const entrypoint = fileURLToPath(import.meta.resolve('@oomol-lab/open-connector'));
29
- const assets = join(directory, 'open-connector');
30
- const plugin = await prepareConnector({
31
- root: dirname(dirname(dirname(entrypoint))),
32
- providers: [...new Set(options.providers)].sort(),
33
- assets,
34
- });
35
- return { assets: [assets], external: ['proxy-agent'], plugins: [plugin], dispose };
36
- } catch (error) {
37
- await dispose();
38
- throw error;
39
- }
20
+ export function getOpenSyncBuildOptions(options: { providers: readonly string[] }) {
21
+ return getConnectorBuildOptions(options);
40
22
  }
@@ -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 { canonicalJson, type JsonObject, type JsonValue } from '../models/json';
6
- import { providerResponse } from './response';
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.fetch(
43
- new Request(`${base}${input.path}`, {
44
- method: input.body ? 'POST' : 'GET',
45
- headers,
46
- signal: input.signal,
47
- body: input.body ? JSON.stringify(input.body) : undefined,
48
- }),
49
- );
50
- const result = (await response.json()) as { success?: boolean; data?: JsonValue };
51
- if (!response.ok || result.success !== true || result.data === undefined) {
52
- fail('connector_request_failed');
53
- }
54
- input.signal.throwIfAborted();
55
- return canonicalJson(result.data).value;
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
- async action(operation) {
111
- if (!requirements.actions.includes(operation.id)) {
112
- fail('operation_denied');
113
- }
114
- const path = `/v1/actions/${encodeURIComponent(operation.id)}`;
115
- const metadata = (await request({ path, signal })) as JsonObject;
116
- if (metadata.service !== requirements.service) {
117
- fail('operation_denied');
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?.includes(path)
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?.includes(path)
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 { JsonValue } from '../models/json';
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();
@@ -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;
@@ -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 definitions (
2
- id TEXT NOT NULL, version TEXT NOT NULL, artifact_id TEXT NOT NULL, manifest TEXT NOT NULL,
3
- PRIMARY KEY(id, version)
4
- );
5
- CREATE TABLE destinations (
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
- PRIMARY KEY(owner_id, id), UNIQUE(owner_id, source_id),
17
- FOREIGN KEY(owner_id, destination_id) REFERENCES destinations(owner_id, id)
18
- );
19
- CREATE TABLE runs (
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 UNIQUE INDEX one_acquisition ON runs((1)) WHERE state='running';
28
- CREATE TABLE records (
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, installation_id, kind, id),
32
- FOREIGN KEY(owner_id, installation_id) REFERENCES installations(owner_id, 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 TABLE deliveries (
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
- installation_id TEXT NOT NULL, destination_id TEXT NOT NULL, body TEXT NOT NULL,
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, worker_id TEXT, generation INTEGER NOT NULL DEFAULT 0,
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, installation_id) REFERENCES installations(owner_id, id),
42
- FOREIGN KEY(owner_id, destination_id) REFERENCES destinations(owner_id, id)
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 deliveries_due ON deliveries(state, due_at);
45
- PRAGMA user_version = 1;
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
+ }
@@ -2,8 +2,7 @@ import type { JsonObject } from '../models/json';
2
2
  export interface SyncEvent {
3
3
  code: string;
4
4
  ownerId?: string;
5
- installationId?: string;
6
- message?: string;
5
+ syncId?: string;
7
6
  fields?: JsonObject;
8
7
  }
9
8
  export type Logger = (event: SyncEvent) => void;
@@ -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
  };