@axium/client 0.39.2 → 0.39.4
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/dist/cli/cache.js +20 -7
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +7 -3
- package/dist/cli/sync.d.ts +2 -1
- package/dist/cli/sync.js +4 -12
- package/package.json +1 -1
package/dist/cli/cache.js
CHANGED
|
@@ -6,7 +6,7 @@ import { dirname, join, resolve } from 'node:path/posix';
|
|
|
6
6
|
import * as z from 'zod';
|
|
7
7
|
import { CacheData } from '../cache.js';
|
|
8
8
|
import { fetchAPI } from '../requests.js';
|
|
9
|
-
import { applyDiff } from '../sync.js';
|
|
9
|
+
import { applyDiff, onSync } from '../sync.js';
|
|
10
10
|
import { apiUserCache, getCurrentSession } from '../user.js';
|
|
11
11
|
export const dir = join(process.env.XDG_CACHE_HOME || join(homedir(), '.cache'), 'axium');
|
|
12
12
|
mkdirSync(dir, { recursive: true });
|
|
@@ -84,21 +84,34 @@ export const meta = useAt({
|
|
|
84
84
|
},
|
|
85
85
|
isValid: meta => meta.fetched + _dayMs > Date.now(),
|
|
86
86
|
});
|
|
87
|
+
function applySyncDiff(state, diff) {
|
|
88
|
+
applyDiff(state.objects, diff);
|
|
89
|
+
if (diff.index > state.index)
|
|
90
|
+
state.index = diff.index;
|
|
91
|
+
return state;
|
|
92
|
+
}
|
|
87
93
|
export const sync = useAt({
|
|
88
94
|
path: 'sync.json',
|
|
89
95
|
schema: SyncState,
|
|
90
|
-
async update(
|
|
91
|
-
if (!
|
|
96
|
+
async update(state) {
|
|
97
|
+
if (!state)
|
|
98
|
+
return await fetchAPI('GET', 'sync/init');
|
|
99
|
+
const diff = await fetchAPI('GET', 'sync', { since: state.index });
|
|
100
|
+
if (diff.index < state.index)
|
|
92
101
|
return await fetchAPI('GET', 'sync/init');
|
|
93
|
-
|
|
94
|
-
applyDiff(sync.objects, diff);
|
|
95
|
-
return { objects: sync.objects, index: diff.index };
|
|
102
|
+
return applySyncDiff(state, diff);
|
|
96
103
|
},
|
|
97
104
|
async isValid({ index }) {
|
|
98
105
|
const md = await fetchAPI('GET', 'sync/metadata');
|
|
99
|
-
return md.index
|
|
106
|
+
return md.index == index;
|
|
100
107
|
},
|
|
101
108
|
});
|
|
109
|
+
onSync(diff => {
|
|
110
|
+
if (!sync.data)
|
|
111
|
+
return;
|
|
112
|
+
applySyncDiff(sync.data, diff);
|
|
113
|
+
sync.save();
|
|
114
|
+
});
|
|
102
115
|
const persistedAPICaches = [];
|
|
103
116
|
function persistAPI(cache, path) {
|
|
104
117
|
path = resolve(dir, path);
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env -S node --disable-warning=ExperimentalWarning
|
|
2
2
|
export {};
|
package/dist/cli/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env -S node --disable-warning=ExperimentalWarning
|
|
2
2
|
import { createPluginCommand } from '@axium/core/node';
|
|
3
3
|
import { waitForPlugins } from '@axium/core/node/plugins';
|
|
4
4
|
import { _findPlugin, plugins } from '@axium/core/plugins';
|
|
@@ -80,9 +80,13 @@ program
|
|
|
80
80
|
.action(async (opt) => {
|
|
81
81
|
for (const plugin of plugins.values())
|
|
82
82
|
await plugin._client?.run?.();
|
|
83
|
+
if (!opt.socket || !config.token)
|
|
84
|
+
return;
|
|
83
85
|
// Hold a socket connection to the server for the lifetime of the daemon.
|
|
84
|
-
|
|
85
|
-
|
|
86
|
+
const socket = await connectSocket({ rejectUnauthorized: !opt.insecure });
|
|
87
|
+
const catchUp = () => cache.sync.update().catch(e => io.error('Failed to update sync cache:', io.errorText(e)));
|
|
88
|
+
await catchUp();
|
|
89
|
+
socket.io.on('reconnect', catchUp);
|
|
86
90
|
});
|
|
87
91
|
const axcPlugin = createPluginCommand('client', program, {
|
|
88
92
|
safe,
|
package/dist/cli/sync.d.ts
CHANGED
|
@@ -11,4 +11,5 @@ type ObjectValues = keyof $Objects extends never ? Record<string, {
|
|
|
11
11
|
})[];
|
|
12
12
|
};
|
|
13
13
|
export declare function get<Type extends ObjectType>(type: Type): ObjectValues[Type];
|
|
14
|
-
|
|
14
|
+
/** Pull changes from the server into the local cache. */
|
|
15
|
+
export declare function pull(): Promise<void>;
|
package/dist/cli/sync.js
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
import { schemas } from '../sync.js';
|
|
2
2
|
import { sync as syncCache } from './cache.js';
|
|
3
3
|
export { schemas };
|
|
4
|
-
let _byType;
|
|
5
|
-
function byType() {
|
|
6
|
-
_byType ||= Object.groupBy(syncCache.data.objects, o => o.$type);
|
|
7
|
-
return _byType;
|
|
8
|
-
}
|
|
9
4
|
export function get(type) {
|
|
10
|
-
const value =
|
|
5
|
+
const value = syncCache.data.objects.filter(o => o.$type == type);
|
|
11
6
|
const schema = schemas.get(type);
|
|
12
7
|
if (!schema)
|
|
13
8
|
return value;
|
|
14
9
|
return value.map(obj => schema.parse(obj));
|
|
15
10
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
syncCache.data.objects = syncCache.data.objects.filter(o => o.$type !== type);
|
|
20
|
-
for (const object of objects)
|
|
21
|
-
syncCache.data.objects.push(Object.assign(object, { $type: type }));
|
|
11
|
+
/** Pull changes from the server into the local cache. */
|
|
12
|
+
export async function pull() {
|
|
13
|
+
await syncCache.update();
|
|
22
14
|
}
|