@symbo.ls/sdk 3.1.1 → 3.2.3
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/README.md +174 -13
- package/dist/cjs/config/environment.js +32 -42
- package/dist/cjs/index.js +31 -24
- package/dist/cjs/services/AIService.js +3 -3
- package/dist/cjs/services/AuthService.js +44 -3
- package/dist/cjs/services/BasedService.js +530 -24
- package/dist/cjs/services/CollabService.js +420 -0
- package/dist/cjs/services/CoreService.js +2295 -0
- package/dist/cjs/services/SocketService.js +207 -59
- package/dist/cjs/services/SymstoryService.js +135 -49
- package/dist/cjs/services/index.js +8 -16
- package/dist/cjs/state/RootStateManager.js +86 -0
- package/dist/cjs/state/rootEventBus.js +65 -0
- package/dist/cjs/utils/CollabClient.js +157 -0
- package/dist/cjs/utils/TokenManager.js +409 -0
- package/dist/cjs/utils/basedQuerys.js +120 -0
- package/dist/cjs/utils/jsonDiff.js +103 -0
- package/dist/cjs/utils/permission.js +4 -4
- package/dist/cjs/utils/services.js +133 -69
- package/dist/cjs/utils/symstoryClient.js +33 -2
- package/dist/esm/config/environment.js +32 -42
- package/dist/esm/index.js +20586 -11525
- package/dist/esm/services/AIService.js +3 -3
- package/dist/esm/services/AuthService.js +48 -7
- package/dist/esm/services/BasedService.js +676 -65
- package/dist/esm/services/CollabService.js +18028 -0
- package/dist/esm/services/CoreService.js +2827 -0
- package/dist/esm/services/SocketService.js +323 -58
- package/dist/esm/services/SymstoryService.js +287 -111
- package/dist/esm/services/index.js +20456 -11470
- package/dist/esm/state/RootStateManager.js +102 -0
- package/dist/esm/state/rootEventBus.js +47 -0
- package/dist/esm/utils/CollabClient.js +17483 -0
- package/dist/esm/utils/TokenManager.js +395 -0
- package/dist/esm/utils/basedQuerys.js +120 -0
- package/dist/esm/utils/jsonDiff.js +6096 -0
- package/dist/esm/utils/permission.js +4 -4
- package/dist/esm/utils/services.js +133 -69
- package/dist/esm/utils/symstoryClient.js +63 -43
- package/dist/esm/utils/validation.js +89 -19
- package/dist/node/config/environment.js +32 -42
- package/dist/node/index.js +37 -28
- package/dist/node/services/AIService.js +3 -3
- package/dist/node/services/AuthService.js +44 -3
- package/dist/node/services/BasedService.js +531 -25
- package/dist/node/services/CollabService.js +401 -0
- package/dist/node/services/CoreService.js +2266 -0
- package/dist/node/services/SocketService.js +197 -59
- package/dist/node/services/SymstoryService.js +135 -49
- package/dist/node/services/index.js +8 -16
- package/dist/node/state/RootStateManager.js +57 -0
- package/dist/node/state/rootEventBus.js +46 -0
- package/dist/node/utils/CollabClient.js +128 -0
- package/dist/node/utils/TokenManager.js +390 -0
- package/dist/node/utils/basedQuerys.js +120 -0
- package/dist/node/utils/jsonDiff.js +74 -0
- package/dist/node/utils/permission.js +4 -4
- package/dist/node/utils/services.js +133 -69
- package/dist/node/utils/symstoryClient.js +33 -2
- package/package.json +23 -14
- package/src/config/environment.js +33 -42
- package/src/index.js +45 -28
- package/src/services/AIService.js +3 -3
- package/src/services/AuthService.js +52 -3
- package/src/services/BasedService.js +603 -23
- package/src/services/CollabService.js +491 -0
- package/src/services/CoreService.js +2548 -0
- package/src/services/SocketService.js +227 -59
- package/src/services/SymstoryService.js +150 -64
- package/src/services/index.js +7 -14
- package/src/state/RootStateManager.js +71 -0
- package/src/state/rootEventBus.js +48 -0
- package/src/utils/CollabClient.js +161 -0
- package/src/utils/TokenManager.js +462 -0
- package/src/utils/basedQuerys.js +123 -0
- package/src/utils/jsonDiff.js +109 -0
- package/src/utils/permission.js +4 -4
- package/src/utils/services.js +144 -69
- package/src/utils/symstoryClient.js +36 -2
- package/dist/cjs/services/SocketIOService.js +0 -309
- package/dist/esm/services/SocketIOService.js +0 -467
- package/dist/node/services/SocketIOService.js +0 -280
- package/src/services/SocketIOService.js +0 -356
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import * as utils from "@domql/utils";
|
|
2
|
+
import { rootBus } from "./rootEventBus.js";
|
|
3
|
+
const { isFunction } = utils.default || utils;
|
|
4
|
+
class RootStateManager {
|
|
5
|
+
constructor(rootState) {
|
|
6
|
+
this._rootState = rootState;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Apply change tuples to the root state using the built-in setPathCollection
|
|
10
|
+
* of Symbo.ls APP state tree.
|
|
11
|
+
*
|
|
12
|
+
* @param {Array} changes – eg. ['update', ['foo'], 'bar']
|
|
13
|
+
* @param {Object} opts – forwarded to setPathCollection
|
|
14
|
+
*/
|
|
15
|
+
applyChanges(changes = [], opts = {}) {
|
|
16
|
+
if (!this._rootState || !isFunction(this._rootState.setPathCollection)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const result = this._rootState.setPathCollection(changes, {
|
|
20
|
+
preventUpdate: true,
|
|
21
|
+
...opts
|
|
22
|
+
});
|
|
23
|
+
try {
|
|
24
|
+
const changedKeys = /* @__PURE__ */ new Set();
|
|
25
|
+
changes.forEach((tuple) => {
|
|
26
|
+
const [, path = []] = tuple;
|
|
27
|
+
if (!Array.isArray(path) || !path.length) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (path[0] === "components" && typeof path[1] === "string") {
|
|
31
|
+
changedKeys.add(path[1]);
|
|
32
|
+
}
|
|
33
|
+
if (path[0] === "schema" && path[1] === "components" && typeof path[2] === "string") {
|
|
34
|
+
changedKeys.add(path[2]);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (changedKeys.size) {
|
|
38
|
+
console.log("emit components:changed", [...changedKeys]);
|
|
39
|
+
rootBus.emit("components:changed", [...changedKeys]);
|
|
40
|
+
}
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error("[RootStateManager] emit components:changed failed", err);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
setVersion(v) {
|
|
47
|
+
if (this._rootState) {
|
|
48
|
+
this._rootState.version = v;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
get root() {
|
|
52
|
+
return this._rootState;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
RootStateManager
|
|
57
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const getGlobalBus = () => {
|
|
2
|
+
if (globalThis.__SMBLS_ROOT_BUS__) {
|
|
3
|
+
return globalThis.__SMBLS_ROOT_BUS__;
|
|
4
|
+
}
|
|
5
|
+
const events = {};
|
|
6
|
+
const bus = {
|
|
7
|
+
on(event, handler) {
|
|
8
|
+
(events[event] ||= []).push(handler);
|
|
9
|
+
},
|
|
10
|
+
off(event, handler) {
|
|
11
|
+
const list = events[event];
|
|
12
|
+
if (!list) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const idx = list.indexOf(handler);
|
|
16
|
+
if (idx !== -1) {
|
|
17
|
+
list.splice(idx, 1);
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
emit(event, payload) {
|
|
21
|
+
const list = events[event];
|
|
22
|
+
if (!list || !list.length) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
list.slice().forEach((fn) => {
|
|
26
|
+
try {
|
|
27
|
+
fn(payload);
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.error("[rootBus] handler error for", event, err);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(bus, "_listeners", {
|
|
35
|
+
value: events,
|
|
36
|
+
enumerable: false
|
|
37
|
+
});
|
|
38
|
+
globalThis.__SMBLS_ROOT_BUS__ = bus;
|
|
39
|
+
return bus;
|
|
40
|
+
};
|
|
41
|
+
const rootBus = getGlobalBus();
|
|
42
|
+
var rootEventBus_default = rootBus;
|
|
43
|
+
export {
|
|
44
|
+
rootEventBus_default as default,
|
|
45
|
+
rootBus
|
|
46
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { io } from "socket.io-client";
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
import { IndexeddbPersistence } from "y-indexeddb";
|
|
4
|
+
import Dexie from "dexie";
|
|
5
|
+
import { nanoid } from "nanoid";
|
|
6
|
+
import environment from "../config/environment.js";
|
|
7
|
+
import { diffJson, applyOpsToJson } from "./jsonDiff.js";
|
|
8
|
+
class CollabClient {
|
|
9
|
+
/* public fields */
|
|
10
|
+
socket = null;
|
|
11
|
+
ydoc = null;
|
|
12
|
+
branch = "main";
|
|
13
|
+
live = false;
|
|
14
|
+
projectId = null;
|
|
15
|
+
jwt = null;
|
|
16
|
+
/* private state */
|
|
17
|
+
_buffer = [];
|
|
18
|
+
_flushTimer = null;
|
|
19
|
+
_clientId = nanoid();
|
|
20
|
+
_outboxStore = null;
|
|
21
|
+
// Dexie table
|
|
22
|
+
_readyResolve;
|
|
23
|
+
ready = new Promise((res) => this._readyResolve = res);
|
|
24
|
+
constructor({ jwt, projectId, branch = "main", live = false }) {
|
|
25
|
+
Object.assign(this, { jwt, projectId, branch, live });
|
|
26
|
+
this.ydoc = new Y.Doc();
|
|
27
|
+
new IndexeddbPersistence(`${projectId}:${branch}`, this.ydoc);
|
|
28
|
+
this._outboxStore = createDexieOutbox(`${projectId}:${branch}`);
|
|
29
|
+
this.socket = io(environment.socketUrl, {
|
|
30
|
+
path: "/collab-socket",
|
|
31
|
+
transports: ["websocket"],
|
|
32
|
+
auth: { token: jwt, projectId, branch, live },
|
|
33
|
+
reconnectionAttempts: Infinity,
|
|
34
|
+
reconnectionDelayMax: 4e3
|
|
35
|
+
});
|
|
36
|
+
this.socket.on("snapshot", this._onSnapshot).on("ops", this._onOps).on("commit", this._onCommit).on("liveMode", (flag) => {
|
|
37
|
+
this.live = flag;
|
|
38
|
+
}).on("connect", this._onConnect).on("error", (e) => console.warn("[collab] socket error", e));
|
|
39
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
40
|
+
this.ydoc.on("afterTransaction", (tr) => {
|
|
41
|
+
if (tr.origin === "remote") {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const currentJson = this.ydoc.getMap("root").toJSON();
|
|
45
|
+
const ops = diffJson(this._prevJson, currentJson);
|
|
46
|
+
this._prevJson = currentJson;
|
|
47
|
+
if (!ops.length) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this._queueOps(ops);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/* ---------- public helpers ---------- */
|
|
54
|
+
toggleLive(flag) {
|
|
55
|
+
this.socket.emit("toggleLive", Boolean(flag));
|
|
56
|
+
}
|
|
57
|
+
sendCursor(data) {
|
|
58
|
+
this.socket.emit("cursor", data);
|
|
59
|
+
}
|
|
60
|
+
sendPresence(d) {
|
|
61
|
+
this.socket.emit("presence", d);
|
|
62
|
+
}
|
|
63
|
+
/* ---------- private handlers ---------- */
|
|
64
|
+
_onSnapshot = ({
|
|
65
|
+
data
|
|
66
|
+
/* Uint8Array */
|
|
67
|
+
}) => {
|
|
68
|
+
Y.applyUpdate(this.ydoc, Uint8Array.from(data));
|
|
69
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
70
|
+
if (typeof this._readyResolve === "function") {
|
|
71
|
+
this._readyResolve();
|
|
72
|
+
this._readyResolve = null;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
_onOps = ({ changes }) => {
|
|
76
|
+
applyOpsToJson(changes, this.ydoc);
|
|
77
|
+
this._prevJson = this.ydoc.getMap("root").toJSON();
|
|
78
|
+
};
|
|
79
|
+
_onCommit = async ({ version }) => {
|
|
80
|
+
await this._outboxStore.clear();
|
|
81
|
+
console.info("[collab] committed", version);
|
|
82
|
+
};
|
|
83
|
+
_onConnect = async () => {
|
|
84
|
+
if (typeof this._readyResolve === "function") {
|
|
85
|
+
this._readyResolve();
|
|
86
|
+
this._readyResolve = null;
|
|
87
|
+
}
|
|
88
|
+
const queued = await this._outboxStore.toArray();
|
|
89
|
+
if (queued.length) {
|
|
90
|
+
this.socket.emit("ops", {
|
|
91
|
+
changes: queued.flatMap((e) => e.ops),
|
|
92
|
+
ts: Date.now(),
|
|
93
|
+
clientId: this._clientId
|
|
94
|
+
});
|
|
95
|
+
await this._outboxStore.clear();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
/* ---------- buffering & debounce ---------- */
|
|
99
|
+
_queueOps(ops) {
|
|
100
|
+
this._buffer.push(...ops);
|
|
101
|
+
this._outboxStore.put({ id: nanoid(), ops });
|
|
102
|
+
if (this.live && this.socket.connected) {
|
|
103
|
+
this._flushNow();
|
|
104
|
+
} else {
|
|
105
|
+
clearTimeout(this._flushTimer);
|
|
106
|
+
this._flushTimer = setTimeout(() => this._flushNow(), 40);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_flushNow() {
|
|
110
|
+
if (!this._buffer.length || !this.socket.connected) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.socket.emit("ops", {
|
|
114
|
+
changes: this._buffer,
|
|
115
|
+
ts: Date.now(),
|
|
116
|
+
clientId: this._clientId
|
|
117
|
+
});
|
|
118
|
+
this._buffer.length = 0;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function createDexieOutbox(name) {
|
|
122
|
+
const db = new Dexie(`collab-${name}`);
|
|
123
|
+
db.version(1).stores({ outbox: "id, ops" });
|
|
124
|
+
return db.table("outbox");
|
|
125
|
+
}
|
|
126
|
+
export {
|
|
127
|
+
CollabClient
|
|
128
|
+
};
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
class TokenManager {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
this.config = {
|
|
4
|
+
storagePrefix: "symbols_",
|
|
5
|
+
storageType: "localStorage",
|
|
6
|
+
// 'localStorage' | 'sessionStorage' | 'memory'
|
|
7
|
+
refreshBuffer: 60 * 1e3,
|
|
8
|
+
// Refresh 1 minute before expiry
|
|
9
|
+
maxRetries: 3,
|
|
10
|
+
apiUrl: options.apiUrl || "/api",
|
|
11
|
+
onTokenRefresh: options.onTokenRefresh || null,
|
|
12
|
+
onTokenExpired: options.onTokenExpired || null,
|
|
13
|
+
onTokenError: options.onTokenError || null,
|
|
14
|
+
...options
|
|
15
|
+
};
|
|
16
|
+
this.tokens = {
|
|
17
|
+
accessToken: null,
|
|
18
|
+
refreshToken: null,
|
|
19
|
+
expiresAt: null,
|
|
20
|
+
expiresIn: null
|
|
21
|
+
};
|
|
22
|
+
this.refreshPromise = null;
|
|
23
|
+
this.refreshTimeout = null;
|
|
24
|
+
this.retryCount = 0;
|
|
25
|
+
this.loadTokens();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Storage keys
|
|
29
|
+
*/
|
|
30
|
+
get storageKeys() {
|
|
31
|
+
return {
|
|
32
|
+
accessToken: `${this.config.storagePrefix}access_token`,
|
|
33
|
+
refreshToken: `${this.config.storagePrefix}refresh_token`,
|
|
34
|
+
expiresAt: `${this.config.storagePrefix}expires_at`,
|
|
35
|
+
expiresIn: `${this.config.storagePrefix}expires_in`
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get storage instance based on configuration
|
|
40
|
+
*/
|
|
41
|
+
get storage() {
|
|
42
|
+
if (typeof window === "undefined") {
|
|
43
|
+
return this._memoryStorage;
|
|
44
|
+
}
|
|
45
|
+
switch (this.config.storageType) {
|
|
46
|
+
case "sessionStorage":
|
|
47
|
+
return window.sessionStorage;
|
|
48
|
+
case "memory":
|
|
49
|
+
return this._memoryStorage;
|
|
50
|
+
default:
|
|
51
|
+
return window.localStorage;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Memory storage fallback for server-side rendering
|
|
56
|
+
*/
|
|
57
|
+
_memoryStorage = {
|
|
58
|
+
_data: {},
|
|
59
|
+
getItem: (key) => this._memoryStorage._data[key] || null,
|
|
60
|
+
setItem: (key, value) => {
|
|
61
|
+
this._memoryStorage._data[key] = value;
|
|
62
|
+
},
|
|
63
|
+
removeItem: (key) => {
|
|
64
|
+
delete this._memoryStorage._data[key];
|
|
65
|
+
},
|
|
66
|
+
clear: () => {
|
|
67
|
+
this._memoryStorage._data = {};
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Set tokens and persist to storage
|
|
72
|
+
*/
|
|
73
|
+
setTokens(tokenData) {
|
|
74
|
+
const {
|
|
75
|
+
access_token: accessToken,
|
|
76
|
+
refresh_token: refreshToken,
|
|
77
|
+
expires_in: expiresIn,
|
|
78
|
+
token_type: tokenType = "Bearer"
|
|
79
|
+
} = tokenData;
|
|
80
|
+
if (!accessToken) {
|
|
81
|
+
throw new Error("Access token is required");
|
|
82
|
+
}
|
|
83
|
+
const now = Date.now();
|
|
84
|
+
const expiresAt = expiresIn ? now + expiresIn * 1e3 : null;
|
|
85
|
+
this.tokens = {
|
|
86
|
+
accessToken,
|
|
87
|
+
refreshToken: refreshToken || this.tokens.refreshToken,
|
|
88
|
+
expiresAt,
|
|
89
|
+
expiresIn,
|
|
90
|
+
tokenType
|
|
91
|
+
};
|
|
92
|
+
this.saveTokens();
|
|
93
|
+
this.scheduleRefresh();
|
|
94
|
+
if (this.config.onTokenRefresh) {
|
|
95
|
+
this.config.onTokenRefresh(this.tokens);
|
|
96
|
+
}
|
|
97
|
+
return this.tokens;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get current access token
|
|
101
|
+
*/
|
|
102
|
+
getAccessToken() {
|
|
103
|
+
return this.tokens.accessToken;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get current refresh token
|
|
107
|
+
*/
|
|
108
|
+
getRefreshToken() {
|
|
109
|
+
return this.tokens.refreshToken;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get authorization header value
|
|
113
|
+
*/
|
|
114
|
+
getAuthHeader() {
|
|
115
|
+
const token = this.getAccessToken();
|
|
116
|
+
if (!token) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return `${this.tokens.tokenType || "Bearer"} ${token}`;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if access token is valid and not expired
|
|
123
|
+
*/
|
|
124
|
+
isAccessTokenValid() {
|
|
125
|
+
if (!this.tokens.accessToken) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
if (!this.tokens.expiresAt) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
const now = Date.now();
|
|
132
|
+
const isValid = now < this.tokens.expiresAt - this.config.refreshBuffer;
|
|
133
|
+
if (!isValid) {
|
|
134
|
+
console.log("[TokenManager] Access token is expired or near expiry:", {
|
|
135
|
+
now: new Date(now).toISOString(),
|
|
136
|
+
expiresAt: new Date(this.tokens.expiresAt).toISOString(),
|
|
137
|
+
refreshBuffer: this.config.refreshBuffer
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return isValid;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Check if access token exists and is not expired (without refresh buffer)
|
|
144
|
+
*/
|
|
145
|
+
isAccessTokenActuallyValid() {
|
|
146
|
+
if (!this.tokens.accessToken) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
if (!this.tokens.expiresAt) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
const now = Date.now();
|
|
153
|
+
return now < this.tokens.expiresAt;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Check if tokens exist (regardless of expiry)
|
|
157
|
+
*/
|
|
158
|
+
hasTokens() {
|
|
159
|
+
return Boolean(this.tokens.accessToken);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Check if refresh token exists
|
|
163
|
+
*/
|
|
164
|
+
hasRefreshToken() {
|
|
165
|
+
return Boolean(this.tokens.refreshToken);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Automatically refresh tokens if needed
|
|
169
|
+
*/
|
|
170
|
+
async ensureValidToken() {
|
|
171
|
+
if (!this.hasTokens()) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
if (this.isAccessTokenValid()) {
|
|
175
|
+
return this.getAccessToken();
|
|
176
|
+
}
|
|
177
|
+
if (!this.hasRefreshToken()) {
|
|
178
|
+
this.clearTokens();
|
|
179
|
+
if (this.config.onTokenExpired) {
|
|
180
|
+
this.config.onTokenExpired();
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
try {
|
|
185
|
+
await this.refreshTokens();
|
|
186
|
+
return this.getAccessToken();
|
|
187
|
+
} catch (error) {
|
|
188
|
+
this.clearTokens();
|
|
189
|
+
if (this.config.onTokenError) {
|
|
190
|
+
this.config.onTokenError(error);
|
|
191
|
+
}
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Refresh access token using refresh token
|
|
197
|
+
*/
|
|
198
|
+
async refreshTokens() {
|
|
199
|
+
if (this.refreshPromise) {
|
|
200
|
+
return this.refreshPromise;
|
|
201
|
+
}
|
|
202
|
+
if (!this.hasRefreshToken()) {
|
|
203
|
+
throw new Error("No refresh token available");
|
|
204
|
+
}
|
|
205
|
+
if (this.retryCount >= this.config.maxRetries) {
|
|
206
|
+
throw new Error("Max refresh retries exceeded");
|
|
207
|
+
}
|
|
208
|
+
this.refreshPromise = this._performRefresh();
|
|
209
|
+
try {
|
|
210
|
+
const result = await this.refreshPromise;
|
|
211
|
+
this.retryCount = 0;
|
|
212
|
+
return result;
|
|
213
|
+
} catch (error) {
|
|
214
|
+
this.retryCount++;
|
|
215
|
+
throw error;
|
|
216
|
+
} finally {
|
|
217
|
+
this.refreshPromise = null;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Perform the actual token refresh request
|
|
222
|
+
*/
|
|
223
|
+
async _performRefresh() {
|
|
224
|
+
var _a;
|
|
225
|
+
const refreshToken = this.getRefreshToken();
|
|
226
|
+
const response = await fetch(`${this.config.apiUrl}/core/auth/refresh`, {
|
|
227
|
+
method: "POST",
|
|
228
|
+
headers: {
|
|
229
|
+
"Content-Type": "application/json"
|
|
230
|
+
},
|
|
231
|
+
body: JSON.stringify({ refreshToken })
|
|
232
|
+
});
|
|
233
|
+
if (!response.ok) {
|
|
234
|
+
const errorData = await response.json().catch(() => ({}));
|
|
235
|
+
throw new Error(errorData.message || `Token refresh failed: ${response.status}`);
|
|
236
|
+
}
|
|
237
|
+
const responseData = await response.json();
|
|
238
|
+
if (responseData.success && responseData.data && responseData.data.tokens) {
|
|
239
|
+
const { tokens } = responseData.data;
|
|
240
|
+
const tokenData = {
|
|
241
|
+
access_token: tokens.accessToken,
|
|
242
|
+
refresh_token: tokens.refreshToken,
|
|
243
|
+
expires_in: (_a = tokens.accessTokenExp) == null ? void 0 : _a.expiresIn,
|
|
244
|
+
token_type: "Bearer"
|
|
245
|
+
};
|
|
246
|
+
return this.setTokens(tokenData);
|
|
247
|
+
}
|
|
248
|
+
return this.setTokens(responseData);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Schedule automatic token refresh
|
|
252
|
+
*/
|
|
253
|
+
scheduleRefresh() {
|
|
254
|
+
if (this.refreshTimeout) {
|
|
255
|
+
clearTimeout(this.refreshTimeout);
|
|
256
|
+
this.refreshTimeout = null;
|
|
257
|
+
}
|
|
258
|
+
if (!this.tokens.expiresAt || !this.hasRefreshToken()) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const now = Date.now();
|
|
262
|
+
const refreshTime = this.tokens.expiresAt - this.config.refreshBuffer;
|
|
263
|
+
const delay = Math.max(0, refreshTime - now);
|
|
264
|
+
this.refreshTimeout = setTimeout(async () => {
|
|
265
|
+
try {
|
|
266
|
+
await this.refreshTokens();
|
|
267
|
+
} catch (error) {
|
|
268
|
+
console.error("Automatic token refresh failed:", error);
|
|
269
|
+
if (this.config.onTokenError) {
|
|
270
|
+
this.config.onTokenError(error);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}, delay);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Save tokens to storage
|
|
277
|
+
*/
|
|
278
|
+
saveTokens() {
|
|
279
|
+
try {
|
|
280
|
+
const { storage } = this;
|
|
281
|
+
const keys = this.storageKeys;
|
|
282
|
+
if (this.tokens.accessToken) {
|
|
283
|
+
storage.setItem(keys.accessToken, this.tokens.accessToken);
|
|
284
|
+
}
|
|
285
|
+
if (this.tokens.refreshToken) {
|
|
286
|
+
storage.setItem(keys.refreshToken, this.tokens.refreshToken);
|
|
287
|
+
}
|
|
288
|
+
if (this.tokens.expiresAt) {
|
|
289
|
+
storage.setItem(keys.expiresAt, this.tokens.expiresAt.toString());
|
|
290
|
+
}
|
|
291
|
+
if (this.tokens.expiresIn) {
|
|
292
|
+
storage.setItem(keys.expiresIn, this.tokens.expiresIn.toString());
|
|
293
|
+
}
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error("[TokenManager] Error saving tokens to storage:", error);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Load tokens from storage
|
|
300
|
+
*/
|
|
301
|
+
loadTokens() {
|
|
302
|
+
try {
|
|
303
|
+
const { storage } = this;
|
|
304
|
+
const keys = this.storageKeys;
|
|
305
|
+
const accessToken = storage.getItem(keys.accessToken);
|
|
306
|
+
const refreshToken = storage.getItem(keys.refreshToken);
|
|
307
|
+
const expiresAt = storage.getItem(keys.expiresAt);
|
|
308
|
+
const expiresIn = storage.getItem(keys.expiresIn);
|
|
309
|
+
if (accessToken) {
|
|
310
|
+
this.tokens = {
|
|
311
|
+
accessToken,
|
|
312
|
+
refreshToken,
|
|
313
|
+
expiresAt: expiresAt ? parseInt(expiresAt, 10) : null,
|
|
314
|
+
expiresIn: expiresIn ? parseInt(expiresIn, 10) : null,
|
|
315
|
+
tokenType: "Bearer"
|
|
316
|
+
};
|
|
317
|
+
this.scheduleRefresh();
|
|
318
|
+
}
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.error("[TokenManager] Error loading tokens from storage:", error);
|
|
321
|
+
this.tokens = {
|
|
322
|
+
accessToken: null,
|
|
323
|
+
refreshToken: null,
|
|
324
|
+
expiresAt: null,
|
|
325
|
+
expiresIn: null
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Clear all tokens
|
|
331
|
+
*/
|
|
332
|
+
clearTokens() {
|
|
333
|
+
this.tokens = {
|
|
334
|
+
accessToken: null,
|
|
335
|
+
refreshToken: null,
|
|
336
|
+
expiresAt: null,
|
|
337
|
+
expiresIn: null
|
|
338
|
+
};
|
|
339
|
+
const { storage } = this;
|
|
340
|
+
const keys = this.storageKeys;
|
|
341
|
+
Object.values(keys).forEach((key) => {
|
|
342
|
+
storage.removeItem(key);
|
|
343
|
+
});
|
|
344
|
+
if (this.refreshTimeout) {
|
|
345
|
+
clearTimeout(this.refreshTimeout);
|
|
346
|
+
this.refreshTimeout = null;
|
|
347
|
+
}
|
|
348
|
+
this.retryCount = 0;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Get token status information
|
|
352
|
+
*/
|
|
353
|
+
getTokenStatus() {
|
|
354
|
+
const hasTokens = this.hasTokens();
|
|
355
|
+
const isValid = this.isAccessTokenValid();
|
|
356
|
+
const { expiresAt } = this.tokens;
|
|
357
|
+
const timeToExpiry = expiresAt ? expiresAt - Date.now() : null;
|
|
358
|
+
return {
|
|
359
|
+
hasTokens,
|
|
360
|
+
isValid,
|
|
361
|
+
hasRefreshToken: this.hasRefreshToken(),
|
|
362
|
+
expiresAt,
|
|
363
|
+
timeToExpiry,
|
|
364
|
+
willExpireSoon: timeToExpiry ? timeToExpiry < this.config.refreshBuffer : false
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Cleanup resources
|
|
369
|
+
*/
|
|
370
|
+
destroy() {
|
|
371
|
+
if (this.refreshTimeout) {
|
|
372
|
+
clearTimeout(this.refreshTimeout);
|
|
373
|
+
this.refreshTimeout = null;
|
|
374
|
+
}
|
|
375
|
+
this.refreshPromise = null;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
let defaultTokenManager = null;
|
|
379
|
+
const getTokenManager = (options) => {
|
|
380
|
+
if (!defaultTokenManager) {
|
|
381
|
+
defaultTokenManager = new TokenManager(options);
|
|
382
|
+
}
|
|
383
|
+
return defaultTokenManager;
|
|
384
|
+
};
|
|
385
|
+
const createTokenManager = (options) => new TokenManager(options);
|
|
386
|
+
export {
|
|
387
|
+
TokenManager,
|
|
388
|
+
createTokenManager,
|
|
389
|
+
getTokenManager
|
|
390
|
+
};
|