@context-use/open-sync 0.5.0 → 0.5.2

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": "@context-use/open-sync",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/open-sync.ts",
@@ -51,7 +51,12 @@ export function createProviderController(input: {
51
51
  await input.providers.configure({ ...(await scope(request)), ...params, ...body }),
52
52
  {
53
53
  params: t.Object({ service }),
54
- body: t.Object({ values }),
54
+ body: t.Object({
55
+ values,
56
+ requestedScopes: t.Optional(
57
+ t.Array(t.String({ minLength: 1, maxLength: 256 }), { minItems: 1, maxItems: 128 }),
58
+ ),
59
+ }),
55
60
  },
56
61
  )
57
62
  .post(
@@ -13,7 +13,7 @@ export interface CatalogRepository {
13
13
  sync(input: Resource): Sync;
14
14
  syncs(scope: Scope): Sync[];
15
15
  polls(input: Resource & { before?: number }): { polls: SyncPoll[]; nextCursor: number | null };
16
- connectSync(input: Resource & { connection: ConnectionRef }): Sync;
16
+ connectSync(input: Resource & { connection: ConnectionRef; checkpoint: JsonValue }): Sync;
17
17
  setEnabled(input: Resource & { enabled: boolean }): Sync;
18
18
  runNow(input: Resource): void;
19
19
  resync(input: Resource & { checkpoint: JsonValue }): void;
@@ -56,17 +56,28 @@ export class SqliteCatalog implements CatalogRepository {
56
56
  const polls = rows.slice(0, pageSize);
57
57
  return { polls, nextCursor: rows.length > pageSize ? polls.at(-1)!.id : null };
58
58
  }
59
- connectSync(input: Resource & { connection: ConnectionRef }) {
59
+ connectSync(input: Resource & { connection: ConnectionRef; checkpoint: JsonValue }) {
60
60
  return this.db
61
61
  .transaction(() => {
62
62
  const sync = this.sync(input);
63
- if (sync.connection || sync.enabled) {
63
+ if (sync.enabled) {
64
64
  fail('already_connected');
65
65
  }
66
66
  this.db
67
- .query(`UPDATE syncs SET connection=?,enabled=1,
68
- status='ready',error_code=NULL WHERE owner_id=? AND id=?`)
69
- .run(canonicalJson(input.connection).json, input.ownerId, input.id);
67
+ .query(`UPDATE sync_polls SET state='interrupted',error_code='connection_changed',completed_at=?
68
+ WHERE owner_id=? AND sync_id=? AND completed_at IS NULL`)
69
+ .run(Date.now(), input.ownerId, input.id);
70
+ this.db
71
+ .query(`UPDATE syncs SET connection=?,checkpoint=?,enabled=?,
72
+ status=?,error_code=NULL,generation=generation+1,expires_at=NULL WHERE owner_id=? AND id=?`)
73
+ .run(
74
+ canonicalJson(input.connection).json,
75
+ canonicalJson(input.checkpoint).json,
76
+ Number(!sync.connection),
77
+ sync.connection ? 'disabled' : 'ready',
78
+ input.ownerId,
79
+ input.id,
80
+ );
70
81
  return this.sync(input);
71
82
  })
72
83
  .immediate();
@@ -104,10 +104,13 @@ export class SyncManagement {
104
104
  validateCursor(input.before);
105
105
  return this.input.catalog.polls(input);
106
106
  }
107
+ /** Bind a waiting sync, or replace a paused sync's connection and restart acquisition.
108
+ * Replacement keeps the sync paused and preserves delivered records and queued deliveries.
109
+ */
107
110
  async connectSync(input: Resource & { connection: ConnectionRef }) {
108
111
  this.guard(input);
109
112
  const sync = this.input.catalog.sync(input);
110
- if (sync.connection || sync.enabled) {
113
+ if (sync.enabled) {
111
114
  fail('already_connected');
112
115
  }
113
116
  const { definition } = this.input.registry.definition(sync.definition);
@@ -121,7 +124,10 @@ export class SyncManagement {
121
124
  signal: AbortSignal.timeout(this.input.timeoutMs),
122
125
  });
123
126
  this.guard(input);
124
- const connected = this.input.catalog.connectSync(input);
127
+ const connected = this.input.catalog.connectSync({
128
+ ...input,
129
+ checkpoint: definition.initialCheckpoint,
130
+ });
125
131
  this.input.worker.wake();
126
132
  return summarizeSync(connected);
127
133
  }
@@ -124,7 +124,13 @@ export class ProviderService {
124
124
  ),
125
125
  };
126
126
  }
127
- async configure(input: ProviderScope & { service: string; values: Record<string, string> }) {
127
+ async configure(
128
+ input: ProviderScope & {
129
+ service: string;
130
+ values: Record<string, string>;
131
+ requestedScopes?: string[];
132
+ },
133
+ ) {
128
134
  this.guard(input);
129
135
  if (!(await this.input.canConfigure(input))) {
130
136
  fail('forbidden');
@@ -143,7 +149,10 @@ export class ProviderService {
143
149
  await this.input.connector.call({
144
150
  path: `/api/oauth/configs/${encodeURIComponent(input.service)}`,
145
151
  method: 'PUT',
146
- body: oauthClientValues({ auth, values: input.values }),
152
+ body: {
153
+ ...oauthClientValues({ auth, values: input.values }),
154
+ requestedScopes: input.requestedScopes,
155
+ },
147
156
  });
148
157
  },
149
158
  });