@livestore/adapter-node 0.3.0-dev.36 → 0.3.0-dev.38

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 (36) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/client-session/adapter.d.ts.map +1 -1
  3. package/dist/client-session/adapter.js +17 -33
  4. package/dist/client-session/adapter.js.map +1 -1
  5. package/dist/devtools/devtools-server.d.ts +5 -6
  6. package/dist/devtools/devtools-server.d.ts.map +1 -1
  7. package/dist/devtools/devtools-server.js +22 -19
  8. package/dist/devtools/devtools-server.js.map +1 -1
  9. package/dist/devtools/vite-dev-server.d.ts +0 -2
  10. package/dist/devtools/vite-dev-server.d.ts.map +1 -1
  11. package/dist/devtools/vite-dev-server.js +1 -1
  12. package/dist/devtools/vite-dev-server.js.map +1 -1
  13. package/dist/leader-thread-shared.d.ts.map +1 -1
  14. package/dist/leader-thread-shared.js +21 -20
  15. package/dist/leader-thread-shared.js.map +1 -1
  16. package/dist/worker-schema.d.ts +3 -3
  17. package/dist/worker-schema.js +1 -1
  18. package/dist/worker-schema.js.map +1 -1
  19. package/package.json +6 -7
  20. package/src/client-session/adapter.ts +18 -44
  21. package/src/devtools/devtools-server.ts +30 -25
  22. package/src/devtools/vite-dev-server.ts +3 -3
  23. package/src/leader-thread-shared.ts +24 -21
  24. package/src/worker-schema.ts +1 -1
  25. package/dist/client-session/in-memory-adapter.d.ts +0 -164
  26. package/dist/client-session/in-memory-adapter.d.ts.map +0 -1
  27. package/dist/client-session/in-memory-adapter.js +0 -74
  28. package/dist/client-session/in-memory-adapter.js.map +0 -1
  29. package/dist/client-session/persisted-adapter.d.ts +0 -44
  30. package/dist/client-session/persisted-adapter.d.ts.map +0 -1
  31. package/dist/client-session/persisted-adapter.js +0 -199
  32. package/dist/client-session/persisted-adapter.js.map +0 -1
  33. package/dist/leader-shared.d.ts +0 -29
  34. package/dist/leader-shared.d.ts.map +0 -1
  35. package/dist/leader-shared.js +0 -87
  36. package/dist/leader-shared.js.map +0 -1
@@ -1,7 +1,9 @@
1
1
  import http from 'node:http'
2
2
  import path from 'node:path'
3
3
 
4
+ import type { Devtools } from '@livestore/common'
4
5
  import { LS_DEV } from '@livestore/utils'
6
+ import type { HttpClient } from '@livestore/utils/effect'
5
7
  import {
6
8
  Deferred,
7
9
  Effect,
@@ -25,25 +27,18 @@ import { makeViteMiddleware } from './vite-dev-server.js'
25
27
  */
26
28
  export const startDevtoolsServer = ({
27
29
  schemaPath,
28
- schemaAlias,
29
- storeId,
30
- clientId,
31
- sessionId,
30
+ clientSessionInfo,
32
31
  port,
33
32
  host,
34
33
  }: {
35
34
  schemaPath: string
36
- schemaAlias: string
37
- storeId: string
38
- clientId: string
39
- sessionId: string
35
+ clientSessionInfo: Devtools.SessionInfo.SessionInfo | undefined
40
36
  host: string
41
37
  port: number
42
- }) =>
38
+ }): Effect.Effect<never, never, HttpClient.HttpClient> =>
43
39
  Effect.gen(function* () {
44
- const clientSessionInfo = { storeId, clientId, sessionId }
45
40
  const viteMiddleware = yield* makeViteMiddleware({
46
- mode: { _tag: 'node', clientSessionInfo, url: `ws://localhost:${port}` },
41
+ mode: { _tag: 'node', url: `http://${host}:${port}` },
47
42
  schemaPath: path.resolve(process.cwd(), schemaPath),
48
43
  viteConfig: (viteConfig) => {
49
44
  if (LS_DEV) {
@@ -67,23 +62,29 @@ export const startDevtoolsServer = ({
67
62
  const req = yield* HttpServerRequest.HttpServerRequest
68
63
 
69
64
  if (Headers.has(req.headers, 'upgrade')) {
70
- // yield* Effect.log(`WS Relay ${relayNodeName}: request ${req.url}`)
65
+ // yield* Effect.logDebug(`WS Relay ${relayNodeName}: WS upgrade request ${req.url}`)
71
66
 
72
67
  const socket = yield* req.upgrade
73
68
 
74
69
  const { webChannel, from } = yield* makeWebSocketEdge({ socket, socketType: { _tag: 'relay' } })
75
70
 
76
- yield* node
77
- .addEdge({ target: from, edgeChannel: webChannel, replaceIfExists: true })
78
- .pipe(Effect.acquireRelease(() => node.removeEdge(from).pipe(Effect.orDie)))
71
+ // To handle websocket closing, we need to race the `webChannel.closedDeferred` to properly interrupt the handler
72
+ yield* Effect.raceFirst(
73
+ Effect.gen(function* () {
74
+ yield* node
75
+ .addEdge({ target: from, edgeChannel: webChannel, replaceIfExists: true })
76
+ .pipe(Effect.acquireRelease(() => node.removeEdge(from).pipe(Effect.orDie)))
79
77
 
80
- if (LS_DEV) {
81
- yield* Effect.log(`WS Relay ${relayNodeName}: added edge from '${from}'`)
82
- yield* Effect.addFinalizerLog(`WS Relay ${relayNodeName}: removed edge from '${from}'`)
83
- }
78
+ if (LS_DEV) {
79
+ yield* Effect.log(`WS Relay ${relayNodeName}: added edge from '${from}'`)
80
+ yield* Effect.addFinalizerLog(`WS Relay ${relayNodeName}: removed edge from '${from}'`)
81
+ }
84
82
 
85
- // We want to keep the websocket open until the client disconnects or the server shuts down
86
- yield* Effect.never
83
+ // We want to keep the websocket open until the client disconnects or the server shuts down
84
+ yield* Effect.never
85
+ }),
86
+ webChannel.closedDeferred,
87
+ )
87
88
 
88
89
  return HttpServerResponse.empty({ status: 101 })
89
90
  } else {
@@ -105,19 +106,23 @@ export const startDevtoolsServer = ({
105
106
  }
106
107
 
107
108
  return HttpServerResponse.text('Not found')
108
- }).pipe(Effect.interruptible)
109
+ }).pipe(Effect.tapCauseLogPretty, Effect.interruptible)
110
+
111
+ const sessionSuffix = clientSessionInfo
112
+ ? `/${clientSessionInfo.storeId}/${clientSessionInfo.clientId}/${clientSessionInfo.sessionId}/${clientSessionInfo.schemaAlias}`
113
+ : '?autoconnect'
109
114
 
110
115
  yield* Effect.logDebug(
111
- `[@livestore/adapter-node:devtools] LiveStore devtools are available at http://${host}:${port}/_livestore/node/${storeId}/${clientId}/${sessionId}/${schemaAlias}`,
116
+ `[@livestore/devtools] LiveStore devtools are available at http://${host}:${port}/_livestore/node${sessionSuffix}`,
112
117
  )
113
118
 
114
119
  return HttpServer.serve(handler, HttpMiddleware.logger)
115
120
  }).pipe(
116
121
  Effect.withSpan('@livestore/adapter-node:startDevtoolsServer', {
117
- attributes: { storeId, clientId, sessionId, port, host, schemaPath },
122
+ attributes: { clientSessionInfo, port, host, schemaPath },
118
123
  }),
124
+ HttpMiddleware.withLoggerDisabled,
119
125
  Layer.unwrapScoped,
120
- // HttpServer.withLogAddress,
121
126
  Layer.provide(PlatformNode.NodeHttpServer.layer(() => http.createServer(), { port, host })),
122
127
  Layer.launch,
123
128
  Effect.orDie,
@@ -22,7 +22,7 @@ export type ViteDevtoolsOptions = {
22
22
  *
23
23
  * @default 'node'
24
24
  */
25
- mode: Extract<Devtools.DevtoolsMode, { _tag: 'node' } | { _tag: 'expo' }>
25
+ mode: Extract<Devtools.DevtoolsMode, { _tag: 'node' }>
26
26
  }
27
27
 
28
28
  const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -30,10 +30,10 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
30
30
  // NOTE this is currently also used in @livestore/devtools-expo
31
31
  export const makeViteMiddleware = (options: ViteDevtoolsOptions): Effect.Effect<Vite.ViteDevServer, UnexpectedError> =>
32
32
  Effect.gen(function* () {
33
- const hmrPort = yield* getFreePort
34
-
35
33
  const cwd = process.cwd()
36
34
 
35
+ const hmrPort = yield* getFreePort
36
+
37
37
  const defaultViteConfig = Vite.defineConfig({
38
38
  server: {
39
39
  middlewareMode: true,
@@ -11,10 +11,10 @@ import { Devtools, liveStoreStorageFormatVersion, UnexpectedError } from '@lives
11
11
  import type { DevtoolsOptions, LeaderSqliteDb, LeaderThreadCtx } from '@livestore/common/leader-thread'
12
12
  import { configureConnection, makeLeaderThreadLayer } from '@livestore/common/leader-thread'
13
13
  import type { LiveStoreSchema } from '@livestore/common/schema'
14
- import { makeNodeDevtoolsChannel } from '@livestore/devtools-node-common/web-channel'
15
14
  import type { MakeNodeSqliteDb } from '@livestore/sqlite-wasm/node'
16
15
  import type { FileSystem, HttpClient, Layer, Schema, Scope } from '@livestore/utils/effect'
17
- import { Effect, FetchHttpClient } from '@livestore/utils/effect'
16
+ import { Effect } from '@livestore/utils/effect'
17
+ import * as Webmesh from '@livestore/webmesh'
18
18
 
19
19
  import { makeShutdownChannel } from './shutdown-channel.js'
20
20
  import type * as WorkerSchema from './worker-schema.js'
@@ -134,35 +134,38 @@ const makeDevtoolsOptions = ({
134
134
 
135
135
  return {
136
136
  enabled: true,
137
- makeBootContext: Effect.gen(function* () {
137
+ boot: Effect.gen(function* () {
138
138
  // Lazy import to improve startup time
139
139
  const { startDevtoolsServer } = yield* Effect.promise(() => import('./devtools/devtools-server.js'))
140
140
 
141
141
  // TODO instead of failing when the port is already in use, we should try to use that WS server instead of starting a new one
142
142
  yield* startDevtoolsServer({
143
143
  schemaPath: devtools.schemaPath,
144
- schemaAlias: devtools.schemaAlias,
145
- storeId,
146
- clientId,
147
- sessionId: 'static', // TODO make this dynamic
144
+ clientSessionInfo: Devtools.SessionInfo.SessionInfo.make({
145
+ storeId,
146
+ clientId,
147
+ sessionId: 'static', // TODO make this dynamic
148
+ schemaAlias: devtools.schemaAlias,
149
+ isLeader: true,
150
+ }),
148
151
  port: devtools.port,
149
152
  host: devtools.host,
150
153
  }).pipe(Effect.tapCauseLogPretty, Effect.forkScoped)
151
154
 
152
- const devtoolsWebChannel = yield* makeNodeDevtoolsChannel({
153
- nodeName: `leader-${storeId}-${clientId}`,
154
- target: `devtools-${storeId}-${clientId}-static`,
155
- url: `ws://localhost:${devtools.port}`,
156
- schema: { listen: Devtools.Leader.MessageToApp, send: Devtools.Leader.MessageFromApp },
157
- })
158
-
159
- return {
160
- devtoolsWebChannel,
161
- persistenceInfo: {
162
- state: dbState.metadata.persistenceInfo,
163
- eventlog: dbEventlog.metadata.persistenceInfo,
164
- },
155
+ const node = yield* Webmesh.makeMeshNode(Devtools.makeNodeName.client.leader({ storeId, clientId }))
156
+
157
+ yield* Webmesh.connectViaWebSocket({
158
+ node,
159
+ url: `http://${devtools.host}:${devtools.port}`,
160
+ openTimeout: 50,
161
+ }).pipe(Effect.tapCauseLogPretty, Effect.forkScoped)
162
+
163
+ const persistenceInfo = {
164
+ state: dbState.metadata.persistenceInfo,
165
+ eventlog: dbEventlog.metadata.persistenceInfo,
165
166
  }
166
- }).pipe(Effect.provide(FetchHttpClient.layer)),
167
+
168
+ return { node, persistenceInfo, mode: 'proxy' }
169
+ }),
167
170
  }
168
171
  })
@@ -31,7 +31,7 @@ export const StorageTypeFs = Schema.Struct({
31
31
  *
32
32
  * @default Current working directory
33
33
  */
34
- baseDirectory: Schema.String,
34
+ baseDirectory: Schema.optional(Schema.String),
35
35
  })
36
36
 
37
37
  export type StorageTypeFs = typeof StorageTypeFs.Type
@@ -1,164 +0,0 @@
1
- import type { Adapter, MakeSqliteDb, SyncOptions } from '@livestore/common';
2
- import { UnexpectedError } from '@livestore/common';
3
- import type { LiveStoreSchema } from '@livestore/common/schema';
4
- import { LiveStoreEvent } from '@livestore/common/schema';
5
- import type { Schema } from '@livestore/utils/effect';
6
- import { Effect, Stream } from '@livestore/utils/effect';
7
- import type { TestingOverrides } from '../leader-shared.js';
8
- export interface InMemoryAdapterOptions {
9
- sync?: SyncOptions;
10
- /**
11
- * @default 'in-memory'
12
- */
13
- clientId?: string;
14
- /**
15
- * @default nanoid(6)
16
- */
17
- sessionId?: string;
18
- /** Only used internally for testing */
19
- testing?: {
20
- overrides?: TestingOverrides;
21
- };
22
- }
23
- export declare const makeInMemoryAdapter: ({ sync: syncOptions, clientId, sessionId, testing }: InMemoryAdapterOptions) => Adapter;
24
- export declare const makeInMemoryLeaderThread: ({ storeId, clientId, schema, makeSqliteDb, syncOptions, syncPayload, testing, }: {
25
- storeId: string;
26
- clientId: string;
27
- schema: LiveStoreSchema;
28
- makeSqliteDb: MakeSqliteDb;
29
- syncOptions: SyncOptions | undefined;
30
- syncPayload: Schema.JsonValue | undefined;
31
- testing?: {
32
- overrides?: TestingOverrides;
33
- };
34
- }) => Effect.Effect<{
35
- leaderThread: {
36
- events: {
37
- pull: (args: {
38
- cursor: import("@livestore/common").LeaderPullCursor;
39
- }) => Stream.Stream<{
40
- payload: {
41
- readonly _tag: "upstream-rebase";
42
- readonly rollbackEvents: readonly LiveStoreEvent.EncodedWithMeta[];
43
- readonly newEvents: readonly LiveStoreEvent.EncodedWithMeta[];
44
- } | {
45
- readonly _tag: "upstream-advance";
46
- readonly newEvents: readonly LiveStoreEvent.EncodedWithMeta[];
47
- };
48
- mergeCounter: number;
49
- }, UnexpectedError>;
50
- push: (batch: readonly LiveStoreEvent.AnyEncoded[]) => Effect.Effect<void, import("@livestore/common").LeaderAheadError, never>;
51
- };
52
- initialState: {
53
- leaderHead: import("@livestore/common/dist/schema/EventId.js").EventId;
54
- migrationsReport: {
55
- readonly migrations: readonly {
56
- readonly tableName: string;
57
- readonly hashes: {
58
- readonly expected: number;
59
- readonly actual?: number | undefined;
60
- };
61
- }[];
62
- };
63
- };
64
- export: Effect.Effect<Uint8Array<ArrayBufferLike>, never, never>;
65
- getEventlogData: Effect.Effect<Uint8Array<ArrayBufferLike>, never, never>;
66
- getSyncState: import("@livestore/utils/dist/effect/Subscribable.js").Subscribable<import("@livestore/common/dist/sync/syncstate.js").SyncState, never, never>;
67
- sendDevtoolsMessage: (message: {
68
- readonly clientId: string;
69
- readonly _tag: "LSD.Leader.SnapshotReq";
70
- readonly liveStoreVersion: "0.3.0-dev.33";
71
- readonly requestId: string;
72
- } | {
73
- readonly clientId: string;
74
- readonly _tag: "LSD.Leader.EventlogReq";
75
- readonly liveStoreVersion: "0.3.0-dev.33";
76
- readonly requestId: string;
77
- } | {
78
- readonly clientId: string;
79
- readonly _tag: "LSD.Leader.NetworkStatusSubscribe";
80
- readonly liveStoreVersion: "0.3.0-dev.33";
81
- readonly requestId: string;
82
- readonly subscriptionId: string;
83
- } | {
84
- readonly clientId: string;
85
- readonly _tag: "LSD.Leader.NetworkStatusUnsubscribe";
86
- readonly liveStoreVersion: "0.3.0-dev.33";
87
- readonly requestId: string;
88
- readonly subscriptionId: string;
89
- } | {
90
- readonly clientId: string;
91
- readonly _tag: "LSD.Leader.Disconnect";
92
- readonly liveStoreVersion: "0.3.0-dev.33";
93
- readonly requestId: string;
94
- } | {
95
- readonly clientId: string;
96
- readonly _tag: "LSD.Leader.CommitEventReq";
97
- readonly liveStoreVersion: "0.3.0-dev.33";
98
- readonly requestId: string;
99
- readonly eventEncoded: {
100
- readonly name: string;
101
- readonly args: any;
102
- };
103
- } | {
104
- readonly clientId: string;
105
- readonly _tag: "LSD.Leader.Ping";
106
- readonly liveStoreVersion: "0.3.0-dev.33";
107
- readonly requestId: string;
108
- } | {
109
- readonly clientId: string;
110
- readonly _tag: "LSD.Leader.DatabaseFileInfoReq";
111
- readonly liveStoreVersion: "0.3.0-dev.33";
112
- readonly requestId: string;
113
- } | {
114
- readonly clientId: string;
115
- readonly _tag: "LSD.Leader.SyncHistorySubscribe";
116
- readonly liveStoreVersion: "0.3.0-dev.33";
117
- readonly requestId: string;
118
- readonly subscriptionId: string;
119
- } | {
120
- readonly clientId: string;
121
- readonly _tag: "LSD.Leader.SyncHistoryUnsubscribe";
122
- readonly liveStoreVersion: "0.3.0-dev.33";
123
- readonly requestId: string;
124
- readonly subscriptionId: string;
125
- } | {
126
- readonly clientId: string;
127
- readonly _tag: "LSD.Leader.SyncingInfoReq";
128
- readonly liveStoreVersion: "0.3.0-dev.33";
129
- readonly requestId: string;
130
- } | {
131
- readonly clientId: string;
132
- readonly _tag: "LSD.Leader.SyncHeadSubscribe";
133
- readonly liveStoreVersion: "0.3.0-dev.33";
134
- readonly requestId: string;
135
- readonly subscriptionId: string;
136
- } | {
137
- readonly clientId: string;
138
- readonly _tag: "LSD.Leader.SyncHeadUnsubscribe";
139
- readonly liveStoreVersion: "0.3.0-dev.33";
140
- readonly requestId: string;
141
- readonly subscriptionId: string;
142
- } | {
143
- readonly clientId: string;
144
- readonly _tag: "LSD.Leader.LoadDatabaseFile.Request";
145
- readonly liveStoreVersion: "0.3.0-dev.33";
146
- readonly requestId: string;
147
- readonly data: Uint8Array<ArrayBufferLike>;
148
- } | {
149
- readonly clientId: string;
150
- readonly _tag: "LSD.Leader.ResetAllData.Request";
151
- readonly mode: "all-data" | "only-app-db";
152
- readonly liveStoreVersion: "0.3.0-dev.33";
153
- readonly requestId: string;
154
- } | {
155
- readonly clientId: string;
156
- readonly _tag: "LSD.Leader.SetSyncLatch.Request";
157
- readonly liveStoreVersion: "0.3.0-dev.33";
158
- readonly requestId: string;
159
- readonly closeLatch: boolean;
160
- }) => Effect.Effect<boolean, never, never>;
161
- };
162
- initialSnapshot: Uint8Array<ArrayBufferLike>;
163
- }, UnexpectedError | import("@livestore/common").SqliteError, import("effect/Scope").Scope>;
164
- //# sourceMappingURL=in-memory-adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"in-memory-adapter.d.ts","sourceRoot":"","sources":["../../src/client-session/in-memory-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAIP,YAAY,EAEZ,WAAW,EACZ,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEnD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAGzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAS,MAAM,EAA0B,MAAM,EAAmB,MAAM,yBAAyB,CAAA;AAGxG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAK3D,MAAM,WAAW,sBAAsB;IACrC,IAAI,CAAC,EAAE,WAAW,CAAA;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,uCAAuC;IACvC,OAAO,CAAC,EAAE;QACR,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAC7B,CAAA;CACF;AAED,eAAO,MAAM,mBAAmB,GAC7B,qDAA+E,sBAAsB,KAAG,OAkD1D,CAAA;AAEjD,eAAO,MAAM,wBAAwB,GAAI,iFAQtC;IACD,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,eAAe,CAAA;IACvB,YAAY,EAAE,YAAY,CAAA;IAC1B,WAAW,EAAE,WAAW,GAAG,SAAS,CAAA;IACpC,WAAW,EAAE,MAAM,CAAC,SAAS,GAAG,SAAS,CAAA;IACzC,OAAO,CAAC,EAAE;QACR,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAC7B,CAAA;CACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2FA+CG,CAAA"}
@@ -1,74 +0,0 @@
1
- import { UnexpectedError } from '@livestore/common';
2
- import { Eventlog, LeaderThreadCtx, makeLeaderThreadLayer } from '@livestore/common/leader-thread';
3
- import { LiveStoreEvent } from '@livestore/common/schema';
4
- import { sqliteDbFactory } from '@livestore/sqlite-wasm/browser';
5
- import { loadSqlite3Wasm } from '@livestore/sqlite-wasm/load-wasm';
6
- import { Cause, Effect, FetchHttpClient, Layer, Stream, SubscriptionRef } from '@livestore/utils/effect';
7
- import { nanoid } from '@livestore/utils/nanoid';
8
- import { makeShutdownChannel } from '../shutdown-channel.js';
9
- export const makeInMemoryAdapter = ({ sync: syncOptions, clientId = 'in-memory', sessionId = nanoid(6), testing }) => ({ schema, storeId, shutdown, syncPayload,
10
- // devtoolsEnabled, bootStatusQueue, shutdown, connectDevtoolsToStore
11
- }) => Effect.gen(function* () {
12
- const sqlite3 = yield* Effect.promise(() => loadSqlite3Wasm());
13
- const makeSqliteDb = sqliteDbFactory({ sqlite3 });
14
- const sqliteDb = yield* makeSqliteDb({ _tag: 'in-memory' });
15
- const lockStatus = yield* SubscriptionRef.make('has-lock');
16
- const shutdownChannel = yield* makeShutdownChannel(storeId);
17
- yield* shutdownChannel.listen.pipe(Stream.flatten(), Stream.tap((error) => Effect.sync(() => shutdown(Cause.fail(error)))), Stream.runDrain, Effect.interruptible, Effect.tapCauseLogPretty, Effect.forkScoped);
18
- const { leaderThread, initialSnapshot } = yield* makeInMemoryLeaderThread({
19
- storeId,
20
- clientId,
21
- schema,
22
- makeSqliteDb,
23
- syncOptions,
24
- syncPayload,
25
- testing,
26
- });
27
- sqliteDb.import(initialSnapshot);
28
- const clientSession = {
29
- sqliteDb,
30
- devtools: { enabled: false },
31
- clientId,
32
- sessionId,
33
- lockStatus,
34
- leaderThread,
35
- shutdown,
36
- };
37
- return clientSession;
38
- }).pipe(UnexpectedError.mapToUnexpectedError);
39
- export const makeInMemoryLeaderThread = ({ storeId, clientId, schema, makeSqliteDb, syncOptions, syncPayload, testing, }) => Effect.gen(function* () {
40
- const layer = yield* Layer.memoize(makeLeaderThreadLayer({
41
- clientId,
42
- dbState: yield* makeSqliteDb({ _tag: 'in-memory' }),
43
- dbEventlog: testing?.overrides?.makeLeaderThread?.dbEventlog
44
- ? yield* testing.overrides.makeLeaderThread.dbEventlog(makeSqliteDb)
45
- : yield* makeSqliteDb({ _tag: 'in-memory' }),
46
- devtoolsOptions: { enabled: false },
47
- makeSqliteDb,
48
- schema,
49
- // NOTE we're creating a separate channel here since you can't listen to your own channel messages
50
- shutdownChannel: yield* makeShutdownChannel(storeId),
51
- storeId,
52
- syncOptions,
53
- syncPayload,
54
- }).pipe(Layer.provideMerge(FetchHttpClient.layer)));
55
- return yield* Effect.gen(function* () {
56
- const { dbState, dbEventlog, syncProcessor, extraIncomingMessagesQueue, initialState } = yield* LeaderThreadCtx;
57
- const initialLeaderHead = Eventlog.getClientHeadFromDb(dbEventlog);
58
- const leaderThread = {
59
- events: {
60
- pull: testing?.overrides?.clientSession?.leaderThreadProxy?.events?.pull ??
61
- (({ cursor }) => syncProcessor.pull({ cursor })),
62
- push: (batch) => syncProcessor.push(batch.map((item) => new LiveStoreEvent.EncodedWithMeta(item)), { waitForProcessing: true }),
63
- },
64
- initialState: { leaderHead: initialLeaderHead, migrationsReport: initialState.migrationsReport },
65
- export: Effect.sync(() => dbState.export()),
66
- getEventlogData: Effect.sync(() => dbEventlog.export()),
67
- getSyncState: syncProcessor.syncState,
68
- sendDevtoolsMessage: (message) => extraIncomingMessagesQueue.offer(message),
69
- };
70
- const initialSnapshot = dbState.export();
71
- return { leaderThread, initialSnapshot };
72
- }).pipe(Effect.provide(layer));
73
- });
74
- //# sourceMappingURL=in-memory-adapter.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"in-memory-adapter.js","sourceRoot":"","sources":["../../src/client-session/in-memory-adapter.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,iCAAiC,CAAA;AAElG,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAA;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAA;AAElE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACxG,OAAO,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AAGhD,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAqB5D,MAAM,CAAC,MAAM,mBAAmB,GAC9B,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,GAAG,WAAW,EAAE,SAAS,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,EAA0B,EAAW,EAAE,CACnH,CAAC,EACC,MAAM,EACN,OAAO,EACP,QAAQ,E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,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAA;QAElE,MAAM,YAAY,GAAG;YACnB,MAAM,EAAE;gBACN,IAAI,EACF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,EAAE,IAAI;oBAClE,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;gBAClD,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CACd,aAAa,CAAC,IAAI,CAChB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,cAAc,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,EAC7D,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAC5B;aACJ;YACD,YAAY,EAAE,EAAE,UAAU,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,YAAY,CAAC,gBAAgB,EAAE;YAChG,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC3C,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;YACvD,YAAY,EAAE,aAAa,CAAC,SAAS;YACrC,mBAAmB,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,0BAA0B,CAAC,KAAK,CAAC,OAAO,CAAC;SACnC,CAAA;QAE1C,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,EAAE,CAAA;QAExC,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,CAAA;IAC1C,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;AAChC,CAAC,CAAC,CAAA"}
@@ -1,44 +0,0 @@
1
- import type { Adapter, SyncOptions } from '@livestore/common';
2
- import type { TestingOverrides } from '../leader-shared.js';
3
- import * as WorkerSchema from '../worker-schema.js';
4
- export interface NodeAdapterOptions {
5
- storage: WorkerSchema.StorageType;
6
- /** The default is the hostname of the current machine */
7
- clientId?: string;
8
- /**
9
- * Warning: This adapter doesn't currently support multiple client sessions for the same client (i.e. same storeId + clientId)
10
- * @default 'static'
11
- */
12
- sessionId?: string;
13
- devtools?: {
14
- schemaPath: string;
15
- /**
16
- * Where to run the devtools server (via Vite)
17
- *
18
- * @default 4242
19
- */
20
- port?: number;
21
- /**
22
- * @default 'localhost'
23
- */
24
- host?: string;
25
- };
26
- /** Only used internally for testing */
27
- testing?: {
28
- overrides?: TestingOverrides;
29
- };
30
- }
31
- /** Runs everything in the same thread. Use `makeWorkerAdapter` for multi-threaded implementation. */
32
- export declare const makeAdapter: ({ sync, ...options }: NodeAdapterOptions & {
33
- sync?: SyncOptions;
34
- }) => Adapter;
35
- /**
36
- * Runs persistence and syncing in a worker thread.
37
- */
38
- export declare const makeWorkerAdapter: ({ workerUrl, ...options }: NodeAdapterOptions & {
39
- /**
40
- * Example: `new URL('./livestore.worker.js', import.meta.url)`
41
- */
42
- workerUrl: URL;
43
- }) => Adapter;
44
- //# sourceMappingURL=persisted-adapter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"persisted-adapter.d.ts","sourceRoot":"","sources":["../../src/client-session/persisted-adapter.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,OAAO,EAOP,WAAW,EACZ,MAAM,mBAAmB,CAAA;AAwB1B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAG3D,OAAO,KAAK,YAAY,MAAM,qBAAqB,CAAA;AAEnD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,YAAY,CAAC,WAAW,CAAA;IACjC,yDAAyD;IACzD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,QAAQ,CAAC,EAAE;QACT,UAAU,EAAE,MAAM,CAAA;QAClB;;;;WAIG;QACH,IAAI,CAAC,EAAE,MAAM,CAAA;QACb;;WAEG;QACH,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IAED,uCAAuC;IACvC,OAAO,CAAC,EAAE;QACR,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAC7B,CAAA;CACF;AAED,qGAAqG;AACrG,eAAO,MAAM,WAAW,GAAI,sBAGzB,kBAAkB,GAAG;IACtB,IAAI,CAAC,EAAE,WAAW,CAAA;CACnB,KAAG,OAA2F,CAAA;AAE/F;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,2BAG/B,kBAAkB,GAAG;IACtB;;OAEG;IACH,SAAS,EAAE,GAAG,CAAA;CACf,KAAG,OAA+F,CAAA"}