@powersync/service-core 0.5.0 → 0.6.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 (41) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/entry/commands/compact-action.js +3 -4
  3. package/dist/entry/commands/compact-action.js.map +1 -1
  4. package/dist/routes/configure-fastify.d.ts +883 -0
  5. package/dist/routes/configure-fastify.js +58 -0
  6. package/dist/routes/configure-fastify.js.map +1 -0
  7. package/dist/routes/configure-rsocket.d.ts +13 -0
  8. package/dist/routes/configure-rsocket.js +46 -0
  9. package/dist/routes/configure-rsocket.js.map +1 -0
  10. package/dist/routes/endpoints/socket-route.js +3 -10
  11. package/dist/routes/endpoints/socket-route.js.map +1 -1
  12. package/dist/routes/route-register.d.ts +1 -1
  13. package/dist/routes/route-register.js +1 -1
  14. package/dist/routes/route-register.js.map +1 -1
  15. package/dist/routes/router-socket.d.ts +4 -4
  16. package/dist/routes/router-socket.js.map +1 -1
  17. package/dist/routes/router.d.ts +1 -0
  18. package/dist/routes/router.js.map +1 -1
  19. package/dist/routes/routes-index.d.ts +2 -0
  20. package/dist/routes/routes-index.js +2 -0
  21. package/dist/routes/routes-index.js.map +1 -1
  22. package/dist/storage/BucketStorage.d.ts +6 -0
  23. package/dist/storage/mongo/MongoCompactor.d.ts +2 -0
  24. package/dist/storage/mongo/MongoCompactor.js +16 -2
  25. package/dist/storage/mongo/MongoCompactor.js.map +1 -1
  26. package/dist/sync/sync-index.d.ts +1 -0
  27. package/dist/sync/sync-index.js +1 -0
  28. package/dist/sync/sync-index.js.map +1 -1
  29. package/package.json +3 -3
  30. package/src/entry/commands/compact-action.ts +3 -4
  31. package/src/routes/configure-fastify.ts +102 -0
  32. package/src/routes/configure-rsocket.ts +59 -0
  33. package/src/routes/endpoints/socket-route.ts +3 -10
  34. package/src/routes/route-register.ts +2 -2
  35. package/src/routes/router-socket.ts +5 -5
  36. package/src/routes/router.ts +2 -0
  37. package/src/routes/routes-index.ts +2 -0
  38. package/src/storage/BucketStorage.ts +7 -0
  39. package/src/storage/mongo/MongoCompactor.ts +17 -2
  40. package/src/sync/sync-index.ts +1 -0
  41. package/tsconfig.tsbuildinfo +1 -1
@@ -3,20 +3,13 @@ import { RequestParameters } from '@powersync/service-sync-rules';
3
3
  import { serialize } from 'bson';
4
4
 
5
5
  import { Metrics } from '../../metrics/Metrics.js';
6
- import { streamResponse } from '../../sync/sync.js';
6
+ import * as sync from '../../sync/sync-index.js';
7
7
  import * as util from '../../util/util-index.js';
8
8
  import { SocketRouteGenerator } from '../router-socket.js';
9
9
  import { SyncRoutes } from './sync-stream.js';
10
- import { RequestTracker } from '../../sync/RequestTracker.js';
11
10
 
12
11
  export const syncStreamReactive: SocketRouteGenerator = (router) =>
13
12
  router.reactiveStream<util.StreamingSyncRequest, any>(SyncRoutes.STREAM, {
14
- authorize: ({ context }) => {
15
- return {
16
- authorized: !!context.token_payload,
17
- errors: ['Authentication required'].concat(context.token_errors ?? [])
18
- };
19
- },
20
13
  validator: schema.createTsCodecValidator(util.StreamingSyncRequest, { allowAdditional: true }),
21
14
  handler: async ({ context, params, responder, observer, initialN }) => {
22
15
  const { system } = context;
@@ -67,9 +60,9 @@ export const syncStreamReactive: SocketRouteGenerator = (router) =>
67
60
  });
68
61
 
69
62
  Metrics.getInstance().concurrent_connections.add(1);
70
- const tracker = new RequestTracker();
63
+ const tracker = new sync.RequestTracker();
71
64
  try {
72
- for await (const data of streamResponse({
65
+ for await (const data of sync.streamResponse({
73
66
  storage,
74
67
  params: {
75
68
  ...params,
@@ -1,6 +1,6 @@
1
- import fastify from 'fastify';
1
+ import type fastify from 'fastify';
2
2
 
3
- import { errors, router, HTTPMethod, logger } from '@powersync/lib-services-framework';
3
+ import { errors, HTTPMethod, logger, router } from '@powersync/lib-services-framework';
4
4
  import { Context, ContextProvider, RequestEndpoint, RequestEndpointHandlerPayload } from './router.js';
5
5
 
6
6
  export type FastifyEndpoint<I, O, C> = RequestEndpoint<I, O, C> & {
@@ -1,13 +1,13 @@
1
+ import { IReactiveStream, ReactiveSocketRouter } from '@powersync/service-rsocket-router';
1
2
  import * as t from 'ts-codec';
2
- import { ReactiveSocketRouter, IReactiveStream } from '@powersync/service-rsocket-router';
3
3
 
4
4
  import { Context } from './router.js';
5
5
 
6
- export const RSocketContextMeta = t.object({
7
- token: t.string
8
- });
9
-
10
6
  /**
11
7
  * Creates a socket route handler given a router instance
12
8
  */
13
9
  export type SocketRouteGenerator = (router: ReactiveSocketRouter<Context>) => IReactiveStream;
10
+
11
+ export const RSocketContextMeta = t.object({
12
+ token: t.string
13
+ });
@@ -36,6 +36,8 @@ export type RequestEndpointHandlerPayload<
36
36
  request: Request;
37
37
  };
38
38
 
39
+ export type RouteDefinition<I = any, O = any> = RequestEndpoint<I, O>;
40
+
39
41
  /**
40
42
  * Helper function for making generics work well when defining routes
41
43
  */
@@ -1,4 +1,6 @@
1
1
  export * as auth from './auth.js';
2
+ export * from './configure-fastify.js';
3
+ export * from './configure-rsocket.js';
2
4
  export * as endpoints from './endpoints/route-endpoints-index.js';
3
5
  export * as hooks from './hooks.js';
4
6
  export * from './route-register.js';
@@ -426,4 +426,11 @@ export interface CompactOptions {
426
426
  * not be compacted, to avoid invalidating checkpoints in use.
427
427
  */
428
428
  maxOpId?: bigint;
429
+
430
+ /**
431
+ * If specified, compact only the specific buckets.
432
+ *
433
+ * If not specified, compacts all buckets.
434
+ */
435
+ compactBuckets?: string[];
429
436
  }
@@ -55,6 +55,7 @@ export class MongoCompactor {
55
55
  private moveBatchQueryLimit: number;
56
56
  private clearBatchLimit: number;
57
57
  private maxOpId: bigint | undefined;
58
+ private buckets: string[] | undefined;
58
59
 
59
60
  constructor(private db: PowerSyncMongo, private group_id: number, options?: MongoCompactOptions) {
60
61
  this.idLimitBytes = (options?.memoryLimitMB ?? DEFAULT_MEMORY_LIMIT_MB) * 1024 * 1024;
@@ -62,6 +63,7 @@ export class MongoCompactor {
62
63
  this.moveBatchQueryLimit = options?.moveBatchQueryLimit ?? DEFAULT_MOVE_BATCH_QUERY_LIMIT;
63
64
  this.clearBatchLimit = options?.clearBatchLimit ?? DEFAULT_CLEAR_BATCH_LIMIT;
64
65
  this.maxOpId = options?.maxOpId;
66
+ this.buckets = options?.compactBuckets;
65
67
  }
66
68
 
67
69
  /**
@@ -70,6 +72,19 @@ export class MongoCompactor {
70
72
  * See /docs/compacting-operations.md for details.
71
73
  */
72
74
  async compact() {
75
+ if (this.buckets) {
76
+ for (let bucket of this.buckets) {
77
+ // We can make this more efficient later on by iterating
78
+ // through the buckets in a single query.
79
+ // That makes batching more tricky, so we leave for later.
80
+ await this.compactInternal(bucket);
81
+ }
82
+ } else {
83
+ await this.compactInternal(undefined);
84
+ }
85
+ }
86
+
87
+ async compactInternal(bucket: string | undefined) {
73
88
  const idLimitBytes = this.idLimitBytes;
74
89
 
75
90
  let currentState: CurrentBucketState | null = null;
@@ -77,14 +92,14 @@ export class MongoCompactor {
77
92
  // Constant lower bound
78
93
  const lowerBound: BucketDataKey = {
79
94
  g: this.group_id,
80
- b: new MinKey() as any,
95
+ b: bucket ?? (new MinKey() as any),
81
96
  o: new MinKey() as any
82
97
  };
83
98
 
84
99
  // Upper bound is adjusted for each batch
85
100
  let upperBound: BucketDataKey = {
86
101
  g: this.group_id,
87
- b: new MaxKey() as any,
102
+ b: bucket ?? (new MaxKey() as any),
88
103
  o: new MaxKey() as any
89
104
  };
90
105
 
@@ -1,6 +1,7 @@
1
1
  export * from './BroadcastIterable.js';
2
2
  export * from './LastValueSink.js';
3
3
  export * from './merge.js';
4
+ export * from './RequestTracker.js';
4
5
  export * from './safeRace.js';
5
6
  export * from './sync.js';
6
7
  export * from './util.js';