astro-integration-pocketbase 2.1.2-next.2 → 2.1.2-next.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-integration-pocketbase",
|
|
3
|
-
"version": "2.1.2-next.
|
|
3
|
+
"version": "2.1.2-next.4",
|
|
4
4
|
"description": "An Astro integration to support developers working with astro-loader-pocketbase.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -42,12 +42,12 @@
|
|
|
42
42
|
"@commitlint/cli": "^19.8.1",
|
|
43
43
|
"@commitlint/config-conventional": "^19.8.1",
|
|
44
44
|
"@types/node": "^22.14.1",
|
|
45
|
-
"astro": "^5.13.
|
|
45
|
+
"astro": "^5.13.5",
|
|
46
46
|
"eventsource": "^4.0.0",
|
|
47
47
|
"globals": "^16.3.0",
|
|
48
48
|
"husky": "^9.1.7",
|
|
49
49
|
"lint-staged": "^16.1.5",
|
|
50
|
-
"oxlint": "^1.
|
|
50
|
+
"oxlint": "^1.14.0",
|
|
51
51
|
"prettier": "^3.6.2",
|
|
52
52
|
"prettier-plugin-organize-imports": "^4.2.0",
|
|
53
53
|
"prettier-plugin-packagejson": "^2.5.19",
|
|
@@ -1,18 +1,11 @@
|
|
|
1
|
-
import type { BaseIntegrationHooks } from "astro";
|
|
1
|
+
import type { AstroIntegrationLogger, BaseIntegrationHooks } from "astro";
|
|
2
2
|
import { EventSource } from "eventsource";
|
|
3
3
|
import type { PocketBaseIntegrationOptions } from "../types/pocketbase-integration-options.type";
|
|
4
4
|
import { getSuperuserToken } from "../utils/get-superuser-token";
|
|
5
5
|
import { mapCollectionsToWatch } from "../utils/map-collections-to-watch";
|
|
6
6
|
|
|
7
|
-
// This function is not as complex as it seems, but uses a lot of shared state
|
|
8
|
-
// that makes it hard to split into smaller functions.
|
|
9
|
-
// oxlint-disable-next-line max-lines-per-function
|
|
10
7
|
export function refreshCollectionsRealtime(
|
|
11
|
-
|
|
12
|
-
url,
|
|
13
|
-
superuserCredentials,
|
|
14
|
-
collectionsToWatch
|
|
15
|
-
}: PocketBaseIntegrationOptions,
|
|
8
|
+
options: PocketBaseIntegrationOptions,
|
|
16
9
|
{
|
|
17
10
|
logger,
|
|
18
11
|
refreshContent,
|
|
@@ -20,7 +13,7 @@ export function refreshCollectionsRealtime(
|
|
|
20
13
|
}: Parameters<BaseIntegrationHooks["astro:server:setup"]>[0]
|
|
21
14
|
): EventSource | undefined {
|
|
22
15
|
// Check if collections should be watched
|
|
23
|
-
const collectionsMap = mapCollectionsToWatch(collectionsToWatch);
|
|
16
|
+
const collectionsMap = mapCollectionsToWatch(options.collectionsToWatch);
|
|
24
17
|
if (!collectionsMap) {
|
|
25
18
|
return undefined;
|
|
26
19
|
}
|
|
@@ -49,7 +42,7 @@ export function refreshCollectionsRealtime(
|
|
|
49
42
|
refreshEnabled = enabled;
|
|
50
43
|
});
|
|
51
44
|
|
|
52
|
-
const eventSource = new EventSource(`${url}/api/realtime`);
|
|
45
|
+
const eventSource = new EventSource(`${options.url}/api/realtime`);
|
|
53
46
|
let wasConnectedOnce = false;
|
|
54
47
|
let isConnected = false;
|
|
55
48
|
|
|
@@ -94,55 +87,74 @@ export function refreshCollectionsRealtime(
|
|
|
94
87
|
|
|
95
88
|
// Add event listener for the connection event
|
|
96
89
|
eventSource.addEventListener("PB_CONNECT", async (event) => {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
superuserToken = await getSuperuserToken(
|
|
107
|
-
url,
|
|
108
|
-
superuserCredentials,
|
|
109
|
-
logger
|
|
110
|
-
);
|
|
111
|
-
}
|
|
90
|
+
isConnected = await handleConnectEvent(
|
|
91
|
+
event,
|
|
92
|
+
remoteCollections,
|
|
93
|
+
wasConnectedOnce,
|
|
94
|
+
options,
|
|
95
|
+
logger
|
|
96
|
+
);
|
|
97
|
+
if (isConnected) {
|
|
98
|
+
wasConnectedOnce = true;
|
|
112
99
|
}
|
|
100
|
+
});
|
|
113
101
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
method: "POST",
|
|
117
|
-
headers: {
|
|
118
|
-
"Content-Type": "application/json",
|
|
119
|
-
Authorization: superuserToken || ""
|
|
120
|
-
},
|
|
121
|
-
body: JSON.stringify({
|
|
122
|
-
clientId: clientId,
|
|
123
|
-
subscriptions: remoteCollections.map((collection) => `${collection}/*`)
|
|
124
|
-
})
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Log the connection status
|
|
128
|
-
if (!result.ok) {
|
|
129
|
-
logger.error(
|
|
130
|
-
`Error while subscribing to PocketBase realtime API: ${result.status}`
|
|
131
|
-
);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
102
|
+
return eventSource;
|
|
103
|
+
}
|
|
134
104
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
105
|
+
async function handleConnectEvent(
|
|
106
|
+
event: MessageEvent<void>,
|
|
107
|
+
remoteCollections: Array<string>,
|
|
108
|
+
wasConnectedOnce: boolean,
|
|
109
|
+
options: PocketBaseIntegrationOptions,
|
|
110
|
+
logger: AstroIntegrationLogger
|
|
111
|
+
): Promise<boolean> {
|
|
112
|
+
// Extract the clientId
|
|
113
|
+
const clientId = event.lastEventId;
|
|
114
|
+
|
|
115
|
+
// Get the superuser token if credentials are available
|
|
116
|
+
let superuserToken: string | undefined;
|
|
117
|
+
if (options.superuserCredentials) {
|
|
118
|
+
if ("impersonateToken" in options.superuserCredentials) {
|
|
119
|
+
superuserToken = options.superuserCredentials.impersonateToken;
|
|
120
|
+
} else {
|
|
121
|
+
superuserToken = await getSuperuserToken(
|
|
122
|
+
options.url,
|
|
123
|
+
options.superuserCredentials,
|
|
124
|
+
logger
|
|
141
125
|
);
|
|
142
126
|
}
|
|
127
|
+
}
|
|
143
128
|
|
|
144
|
-
|
|
129
|
+
// Subscribe to the PocketBase realtime API
|
|
130
|
+
const result = await fetch(`${options.url}/api/realtime`, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: {
|
|
133
|
+
"Content-Type": "application/json",
|
|
134
|
+
Authorization: superuserToken || ""
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify({
|
|
137
|
+
clientId: clientId,
|
|
138
|
+
subscriptions: remoteCollections.map((collection) => `${collection}/*`)
|
|
139
|
+
})
|
|
145
140
|
});
|
|
146
141
|
|
|
147
|
-
|
|
142
|
+
// Log the connection status
|
|
143
|
+
if (!result.ok) {
|
|
144
|
+
logger.error(
|
|
145
|
+
`Error while subscribing to PocketBase realtime API: ${result.status}`
|
|
146
|
+
);
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!wasConnectedOnce) {
|
|
151
|
+
wasConnectedOnce = true;
|
|
152
|
+
logger.info(
|
|
153
|
+
`Subscribed to PocketBase realtime API. Waiting for updates on ${remoteCollections.join(
|
|
154
|
+
", "
|
|
155
|
+
)}.`
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return true;
|
|
148
160
|
}
|