@syncular/server-service-worker 0.0.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.
- package/package.json +56 -0
- package/src/index.test.ts +150 -0
- package/src/index.ts +697 -0
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@syncular/server-service-worker",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Service Worker server/runtime + wake transport helpers for Syncular",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Benjamin Kniffler",
|
|
7
|
+
"homepage": "https://syncular.dev",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/syncular/syncular.git",
|
|
11
|
+
"directory": "packages/server-service-worker"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/syncular/syncular/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"sync",
|
|
18
|
+
"offline-first",
|
|
19
|
+
"service-worker",
|
|
20
|
+
"realtime",
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"private": false,
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"bun": "./src/index.ts",
|
|
31
|
+
"browser": "./src/index.ts",
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"test": "bun test --pass-with-no-tests",
|
|
40
|
+
"tsgo": "tsgo --noEmit",
|
|
41
|
+
"build": "tsgo",
|
|
42
|
+
"release": "bunx syncular-publish"
|
|
43
|
+
},
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@syncular/core": "0.0.0",
|
|
47
|
+
"@syncular/transport-http": "0.0.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@syncular/config": "0.0.0"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"dist",
|
|
54
|
+
"src"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
createServiceWorkerServer,
|
|
4
|
+
createSyncWakeMessageResolver,
|
|
5
|
+
isServiceWorkerWakeMessage,
|
|
6
|
+
SERVICE_WORKER_WAKE_MESSAGE_TYPE,
|
|
7
|
+
} from './index';
|
|
8
|
+
|
|
9
|
+
describe('isServiceWorkerWakeMessage', () => {
|
|
10
|
+
test('accepts valid wake message', () => {
|
|
11
|
+
expect(
|
|
12
|
+
isServiceWorkerWakeMessage({
|
|
13
|
+
type: SERVICE_WORKER_WAKE_MESSAGE_TYPE,
|
|
14
|
+
timestamp: Date.now(),
|
|
15
|
+
cursor: 10,
|
|
16
|
+
sourceClientId: 'client-a',
|
|
17
|
+
})
|
|
18
|
+
).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('rejects invalid wake message', () => {
|
|
22
|
+
expect(isServiceWorkerWakeMessage(null)).toBe(false);
|
|
23
|
+
expect(
|
|
24
|
+
isServiceWorkerWakeMessage({
|
|
25
|
+
type: SERVICE_WORKER_WAKE_MESSAGE_TYPE,
|
|
26
|
+
timestamp: 'x',
|
|
27
|
+
})
|
|
28
|
+
).toBe(false);
|
|
29
|
+
expect(
|
|
30
|
+
isServiceWorkerWakeMessage({
|
|
31
|
+
type: 'other',
|
|
32
|
+
timestamp: Date.now(),
|
|
33
|
+
})
|
|
34
|
+
).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('createSyncWakeMessageResolver', () => {
|
|
39
|
+
test('captures push context and emits wake message for applied sync push', async () => {
|
|
40
|
+
const resolver = createSyncWakeMessageResolver();
|
|
41
|
+
const request = new Request('https://demo.local/api/sync', {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'content-type': 'application/json',
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
clientId: 'client-a',
|
|
48
|
+
push: {
|
|
49
|
+
operations: [{ table: 'tasks', op: 'upsert' }],
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const response = new Response(
|
|
55
|
+
JSON.stringify({
|
|
56
|
+
push: {
|
|
57
|
+
status: 'applied',
|
|
58
|
+
commitSeq: 42,
|
|
59
|
+
},
|
|
60
|
+
}),
|
|
61
|
+
{
|
|
62
|
+
status: 200,
|
|
63
|
+
headers: {
|
|
64
|
+
'content-type': 'application/json',
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const wakeContext = await resolver.captureContext(request);
|
|
70
|
+
const wakeMessage = await resolver.resolveMessage({
|
|
71
|
+
request,
|
|
72
|
+
response,
|
|
73
|
+
wakeContext,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(wakeMessage?.type).toBe(SERVICE_WORKER_WAKE_MESSAGE_TYPE);
|
|
77
|
+
expect(wakeMessage?.cursor).toBe(42);
|
|
78
|
+
expect(wakeMessage?.sourceClientId).toBe('client-a');
|
|
79
|
+
expect(typeof wakeMessage?.timestamp).toBe('number');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('createServiceWorkerServer', () => {
|
|
84
|
+
test('checks request scope using local origin + api prefix', async () => {
|
|
85
|
+
const server = createServiceWorkerServer({
|
|
86
|
+
apiPrefix: '/api',
|
|
87
|
+
handleRequest: async () => new Response('ok'),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const localRequest = new Request('https://demo.local/api/health');
|
|
91
|
+
const remoteRequest = new Request('https://other.local/api/health');
|
|
92
|
+
const wrongPrefixRequest = new Request('https://demo.local/not-api');
|
|
93
|
+
|
|
94
|
+
expect(server.shouldHandleRequest(localRequest, 'https://demo.local')).toBe(
|
|
95
|
+
true
|
|
96
|
+
);
|
|
97
|
+
expect(
|
|
98
|
+
server.shouldHandleRequest(remoteRequest, 'https://demo.local')
|
|
99
|
+
).toBe(false);
|
|
100
|
+
expect(
|
|
101
|
+
server.shouldHandleRequest(wrongPrefixRequest, 'https://demo.local')
|
|
102
|
+
).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('supports wake resolution when handleRequest consumes request body', async () => {
|
|
106
|
+
const server = createServiceWorkerServer({
|
|
107
|
+
handleRequest: async (request) => {
|
|
108
|
+
await request.json();
|
|
109
|
+
return new Response(
|
|
110
|
+
JSON.stringify({
|
|
111
|
+
push: {
|
|
112
|
+
status: 'applied',
|
|
113
|
+
commitSeq: 7,
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
{
|
|
117
|
+
status: 200,
|
|
118
|
+
headers: {
|
|
119
|
+
'content-type': 'application/json',
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const request = new Request('https://demo.local/api/sync', {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
headers: {
|
|
129
|
+
'content-type': 'application/json',
|
|
130
|
+
},
|
|
131
|
+
body: JSON.stringify({
|
|
132
|
+
clientId: 'client-x',
|
|
133
|
+
push: {
|
|
134
|
+
operations: [{ table: 'tasks', op: 'upsert' }],
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const wakeContext = await server.captureWakeContext(request);
|
|
140
|
+
const response = await server.handleRequest(request);
|
|
141
|
+
const wakeMessage = await server.resolveWakeMessage({
|
|
142
|
+
request,
|
|
143
|
+
response,
|
|
144
|
+
wakeContext,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
expect(wakeMessage?.cursor).toBe(7);
|
|
148
|
+
expect(wakeMessage?.sourceClientId).toBe('client-x');
|
|
149
|
+
});
|
|
150
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import type { SyncTransport } from '@syncular/core';
|
|
2
|
+
import {
|
|
3
|
+
type ClientOptions,
|
|
4
|
+
createHttpTransport,
|
|
5
|
+
} from '@syncular/transport-http';
|
|
6
|
+
|
|
7
|
+
export const SERVICE_WORKER_WAKE_CHANNEL = 'syncular-sw-realtime-v1';
|
|
8
|
+
export const SERVICE_WORKER_WAKE_MESSAGE_TYPE =
|
|
9
|
+
'syncular:service-worker:wakeup';
|
|
10
|
+
|
|
11
|
+
export type ServiceWorkerConnectionState =
|
|
12
|
+
| 'disconnected'
|
|
13
|
+
| 'connecting'
|
|
14
|
+
| 'connected';
|
|
15
|
+
|
|
16
|
+
export interface ServiceWorkerWakeMessage {
|
|
17
|
+
type: string;
|
|
18
|
+
timestamp: number;
|
|
19
|
+
cursor?: number;
|
|
20
|
+
sourceClientId?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function isServiceWorkerWakeMessage(
|
|
24
|
+
value: unknown
|
|
25
|
+
): value is ServiceWorkerWakeMessage {
|
|
26
|
+
if (!value || typeof value !== 'object') return false;
|
|
27
|
+
const record = value as Record<string, unknown>;
|
|
28
|
+
if (record.type !== SERVICE_WORKER_WAKE_MESSAGE_TYPE) return false;
|
|
29
|
+
if (
|
|
30
|
+
typeof record.timestamp !== 'number' ||
|
|
31
|
+
!Number.isFinite(record.timestamp)
|
|
32
|
+
) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (
|
|
36
|
+
record.cursor !== undefined &&
|
|
37
|
+
(typeof record.cursor !== 'number' || !Number.isFinite(record.cursor))
|
|
38
|
+
) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
if (
|
|
42
|
+
record.sourceClientId !== undefined &&
|
|
43
|
+
(typeof record.sourceClientId !== 'string' ||
|
|
44
|
+
record.sourceClientId.length === 0)
|
|
45
|
+
) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ServiceWorkerWakeTransport extends SyncTransport {
|
|
52
|
+
connect(
|
|
53
|
+
args: { clientId: string },
|
|
54
|
+
onEvent: (event: {
|
|
55
|
+
event: 'sync';
|
|
56
|
+
data: { cursor?: number; timestamp: number };
|
|
57
|
+
}) => void,
|
|
58
|
+
onStateChange?: (state: ServiceWorkerConnectionState) => void
|
|
59
|
+
): () => void;
|
|
60
|
+
getConnectionState(): ServiceWorkerConnectionState;
|
|
61
|
+
reconnect(): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ServiceWorkerWakeTransportOptions extends ClientOptions {
|
|
65
|
+
channelName?: string;
|
|
66
|
+
includeServiceWorkerMessages?: boolean;
|
|
67
|
+
isWakeMessage?: (payload: unknown) => payload is ServiceWorkerWakeMessage;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createServiceWorkerWakeTransport(
|
|
71
|
+
options: ServiceWorkerWakeTransportOptions
|
|
72
|
+
): ServiceWorkerWakeTransport {
|
|
73
|
+
const httpTransport = createHttpTransport({
|
|
74
|
+
baseUrl: options.baseUrl,
|
|
75
|
+
getHeaders: options.getHeaders,
|
|
76
|
+
authLifecycle: options.authLifecycle,
|
|
77
|
+
fetch: options.fetch,
|
|
78
|
+
transportPath: options.transportPath,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const channelName = options.channelName ?? SERVICE_WORKER_WAKE_CHANNEL;
|
|
82
|
+
const includeServiceWorkerMessages =
|
|
83
|
+
options.includeServiceWorkerMessages ?? true;
|
|
84
|
+
const wakeMessageGuard = options.isWakeMessage ?? isServiceWorkerWakeMessage;
|
|
85
|
+
|
|
86
|
+
let connectionState: ServiceWorkerConnectionState = 'disconnected';
|
|
87
|
+
let currentClientId: string | null = null;
|
|
88
|
+
let eventCallback:
|
|
89
|
+
| ((event: {
|
|
90
|
+
event: 'sync';
|
|
91
|
+
data: { cursor?: number; timestamp: number };
|
|
92
|
+
}) => void)
|
|
93
|
+
| null = null;
|
|
94
|
+
let stateCallback: ((state: ServiceWorkerConnectionState) => void) | null =
|
|
95
|
+
null;
|
|
96
|
+
let channel: BroadcastChannel | null = null;
|
|
97
|
+
let swMessageListener: ((event: MessageEvent<unknown>) => void) | null = null;
|
|
98
|
+
|
|
99
|
+
const setConnectionState = (state: ServiceWorkerConnectionState): void => {
|
|
100
|
+
if (connectionState === state) return;
|
|
101
|
+
connectionState = state;
|
|
102
|
+
stateCallback?.(state);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const handleWakeMessage = (payload: unknown): void => {
|
|
106
|
+
if (!eventCallback) return;
|
|
107
|
+
if (!wakeMessageGuard(payload)) return;
|
|
108
|
+
if (currentClientId && payload.sourceClientId === currentClientId) return;
|
|
109
|
+
|
|
110
|
+
eventCallback({
|
|
111
|
+
event: 'sync',
|
|
112
|
+
data: {
|
|
113
|
+
cursor: payload.cursor,
|
|
114
|
+
timestamp: payload.timestamp,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const detachListeners = (): void => {
|
|
120
|
+
if (channel) {
|
|
121
|
+
channel.onmessage = null;
|
|
122
|
+
channel.close();
|
|
123
|
+
channel = null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
swMessageListener &&
|
|
128
|
+
typeof navigator !== 'undefined' &&
|
|
129
|
+
'serviceWorker' in navigator
|
|
130
|
+
) {
|
|
131
|
+
navigator.serviceWorker.removeEventListener('message', swMessageListener);
|
|
132
|
+
swMessageListener = null;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const attachListeners = (): boolean => {
|
|
137
|
+
detachListeners();
|
|
138
|
+
let attached = false;
|
|
139
|
+
|
|
140
|
+
if (typeof BroadcastChannel !== 'undefined') {
|
|
141
|
+
channel = new BroadcastChannel(channelName);
|
|
142
|
+
channel.onmessage = (event) => {
|
|
143
|
+
handleWakeMessage(event.data);
|
|
144
|
+
};
|
|
145
|
+
attached = true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (
|
|
149
|
+
includeServiceWorkerMessages &&
|
|
150
|
+
typeof navigator !== 'undefined' &&
|
|
151
|
+
'serviceWorker' in navigator
|
|
152
|
+
) {
|
|
153
|
+
swMessageListener = (event: MessageEvent<unknown>) => {
|
|
154
|
+
handleWakeMessage(event.data);
|
|
155
|
+
};
|
|
156
|
+
navigator.serviceWorker.addEventListener('message', swMessageListener);
|
|
157
|
+
attached = true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return attached;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
...httpTransport,
|
|
165
|
+
|
|
166
|
+
connect(args, onEvent, onStateChange) {
|
|
167
|
+
currentClientId = args.clientId;
|
|
168
|
+
eventCallback = onEvent;
|
|
169
|
+
stateCallback = onStateChange ?? null;
|
|
170
|
+
|
|
171
|
+
setConnectionState('connecting');
|
|
172
|
+
const attached = attachListeners();
|
|
173
|
+
setConnectionState(attached ? 'connected' : 'disconnected');
|
|
174
|
+
|
|
175
|
+
return () => {
|
|
176
|
+
detachListeners();
|
|
177
|
+
currentClientId = null;
|
|
178
|
+
eventCallback = null;
|
|
179
|
+
stateCallback = null;
|
|
180
|
+
setConnectionState('disconnected');
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
getConnectionState() {
|
|
185
|
+
return connectionState;
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
reconnect() {
|
|
189
|
+
if (!eventCallback) return;
|
|
190
|
+
setConnectionState('connecting');
|
|
191
|
+
const attached = attachListeners();
|
|
192
|
+
setConnectionState(attached ? 'connected' : 'disconnected');
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface ServiceWorkerServer {
|
|
198
|
+
shouldHandleRequest(request: Request, localOrigin?: string): boolean;
|
|
199
|
+
handleRequest(request: Request): Promise<Response>;
|
|
200
|
+
captureWakeContext(request: Request): Promise<unknown>;
|
|
201
|
+
resolveWakeMessage(args: {
|
|
202
|
+
request: Request;
|
|
203
|
+
response: Response;
|
|
204
|
+
wakeContext: unknown;
|
|
205
|
+
}): Promise<ServiceWorkerWakeMessage | null>;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export interface ResolveWakeMessageArgs {
|
|
209
|
+
request: Request;
|
|
210
|
+
response: Response;
|
|
211
|
+
wakeContext: unknown;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export interface ServiceWorkerSyncWakeContext {
|
|
215
|
+
sourceClientId?: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface ServiceWorkerSyncWakeMessageResolverOptions {
|
|
219
|
+
syncPathnames?: string[];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface CreateServiceWorkerServerOptions {
|
|
223
|
+
handleRequest: (request: Request) => Promise<Response> | Response;
|
|
224
|
+
apiPrefix?: string;
|
|
225
|
+
serviceWorkerScriptPath?: string;
|
|
226
|
+
syncPathnames?: string[];
|
|
227
|
+
captureWakeContext?: (request: Request) => Promise<unknown>;
|
|
228
|
+
resolveWakeMessage?: (
|
|
229
|
+
args: ResolveWakeMessageArgs
|
|
230
|
+
) => Promise<ServiceWorkerWakeMessage | null>;
|
|
231
|
+
onError?: (error: unknown, request: Request) => Promise<Response> | Response;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function normalizePrefix(prefix: string | undefined): string {
|
|
235
|
+
const raw = (prefix ?? '/api').trim();
|
|
236
|
+
if (!raw) return '/api';
|
|
237
|
+
const withSlash = raw.startsWith('/') ? raw : `/${raw}`;
|
|
238
|
+
const trimmed = withSlash.replace(/\/+$/, '');
|
|
239
|
+
return trimmed || '/';
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function pathStartsWithPrefix(pathname: string, prefix: string): boolean {
|
|
243
|
+
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
247
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function getSyncPushContext(
|
|
251
|
+
request: Request,
|
|
252
|
+
syncPathnameSet: ReadonlySet<string>
|
|
253
|
+
): Promise<{ sourceClientId?: string } | null> {
|
|
254
|
+
if (request.method !== 'POST') return null;
|
|
255
|
+
|
|
256
|
+
const pathname = new URL(request.url).pathname;
|
|
257
|
+
if (!syncPathnameSet.has(pathname)) return null;
|
|
258
|
+
|
|
259
|
+
try {
|
|
260
|
+
const payload = await request.clone().json();
|
|
261
|
+
if (!isRecord(payload)) return null;
|
|
262
|
+
if (!isRecord(payload.push)) return null;
|
|
263
|
+
if (!Array.isArray(payload.push.operations)) return null;
|
|
264
|
+
if (payload.push.operations.length === 0) return null;
|
|
265
|
+
|
|
266
|
+
const sourceClientId =
|
|
267
|
+
typeof payload.clientId === 'string' && payload.clientId.length > 0
|
|
268
|
+
? payload.clientId
|
|
269
|
+
: undefined;
|
|
270
|
+
|
|
271
|
+
return { sourceClientId };
|
|
272
|
+
} catch {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function getAppliedCommitCursor(
|
|
278
|
+
response: Response
|
|
279
|
+
): Promise<number | undefined> {
|
|
280
|
+
if (!response.ok) return undefined;
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
const payload = await response.clone().json();
|
|
284
|
+
if (!isRecord(payload)) return undefined;
|
|
285
|
+
if (!isRecord(payload.push)) return undefined;
|
|
286
|
+
if (payload.push.status !== 'applied') return undefined;
|
|
287
|
+
|
|
288
|
+
if (typeof payload.push.commitSeq === 'number') {
|
|
289
|
+
return payload.push.commitSeq;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return undefined;
|
|
293
|
+
} catch {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function createSyncWakeMessageResolver(
|
|
299
|
+
options?: ServiceWorkerSyncWakeMessageResolverOptions
|
|
300
|
+
): {
|
|
301
|
+
captureContext: (
|
|
302
|
+
request: Request
|
|
303
|
+
) => Promise<ServiceWorkerSyncWakeContext | null>;
|
|
304
|
+
resolveMessage: (
|
|
305
|
+
args: ResolveWakeMessageArgs
|
|
306
|
+
) => Promise<ServiceWorkerWakeMessage | null>;
|
|
307
|
+
} {
|
|
308
|
+
const syncPathnameSet = new Set(
|
|
309
|
+
options?.syncPathnames ?? ['/api/sync', '/api/sync/']
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
captureContext: async (request: Request) => {
|
|
314
|
+
return await getSyncPushContext(request, syncPathnameSet);
|
|
315
|
+
},
|
|
316
|
+
resolveMessage: async (args: ResolveWakeMessageArgs) => {
|
|
317
|
+
const syncContext =
|
|
318
|
+
args.wakeContext && typeof args.wakeContext === 'object'
|
|
319
|
+
? (args.wakeContext as ServiceWorkerSyncWakeContext)
|
|
320
|
+
: null;
|
|
321
|
+
if (!syncContext) return null;
|
|
322
|
+
|
|
323
|
+
const cursor = await getAppliedCommitCursor(args.response);
|
|
324
|
+
if (cursor === undefined) return null;
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
type: SERVICE_WORKER_WAKE_MESSAGE_TYPE,
|
|
328
|
+
timestamp: Date.now(),
|
|
329
|
+
cursor,
|
|
330
|
+
...(syncContext.sourceClientId
|
|
331
|
+
? { sourceClientId: syncContext.sourceClientId }
|
|
332
|
+
: {}),
|
|
333
|
+
} satisfies ServiceWorkerWakeMessage;
|
|
334
|
+
},
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export function createServiceWorkerServer(
|
|
339
|
+
options: CreateServiceWorkerServerOptions
|
|
340
|
+
): ServiceWorkerServer {
|
|
341
|
+
const apiPrefix = normalizePrefix(options.apiPrefix);
|
|
342
|
+
const scriptPath = options.serviceWorkerScriptPath;
|
|
343
|
+
const syncWakeResolver = createSyncWakeMessageResolver({
|
|
344
|
+
syncPathnames: options.syncPathnames,
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const captureWakeContext =
|
|
348
|
+
options.captureWakeContext ?? syncWakeResolver.captureContext;
|
|
349
|
+
const resolveWakeMessage =
|
|
350
|
+
options.resolveWakeMessage ?? syncWakeResolver.resolveMessage;
|
|
351
|
+
|
|
352
|
+
const onError =
|
|
353
|
+
options.onError ??
|
|
354
|
+
(() =>
|
|
355
|
+
new Response('Service worker server failed', {
|
|
356
|
+
status: 500,
|
|
357
|
+
headers: { 'content-type': 'text/plain; charset=utf-8' },
|
|
358
|
+
}));
|
|
359
|
+
|
|
360
|
+
return {
|
|
361
|
+
shouldHandleRequest(request: Request, localOrigin?: string): boolean {
|
|
362
|
+
const requestUrl = new URL(request.url);
|
|
363
|
+
|
|
364
|
+
if (localOrigin && requestUrl.origin !== localOrigin) {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
if (!pathStartsWithPrefix(requestUrl.pathname, apiPrefix)) {
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
if (scriptPath && requestUrl.pathname === scriptPath) {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
return true;
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
async handleRequest(request: Request): Promise<Response> {
|
|
377
|
+
try {
|
|
378
|
+
return await options.handleRequest(request);
|
|
379
|
+
} catch (error) {
|
|
380
|
+
return await onError(error, request);
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
async captureWakeContext(request: Request) {
|
|
385
|
+
return await captureWakeContext(request);
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
async resolveWakeMessage(args: ResolveWakeMessageArgs) {
|
|
389
|
+
return await resolveWakeMessage(args);
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
export interface ServiceWorkerGlobalScopeLike {
|
|
395
|
+
addEventListener(type: string, listener: (event: unknown) => void): void;
|
|
396
|
+
location?: { origin?: string };
|
|
397
|
+
skipWaiting?: () => Promise<void>;
|
|
398
|
+
clients?: {
|
|
399
|
+
claim?: () => Promise<void>;
|
|
400
|
+
matchAll?: (options?: {
|
|
401
|
+
type?: 'window' | 'worker' | 'sharedworker' | 'all';
|
|
402
|
+
includeUncontrolled?: boolean;
|
|
403
|
+
}) => Promise<Array<{ postMessage: (message: unknown) => void }>>;
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
interface ServiceWorkerLifecycleEventLike {
|
|
408
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
interface ServiceWorkerFetchEventLike {
|
|
412
|
+
request: Request;
|
|
413
|
+
respondWith(response: Response | Promise<Response>): void;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export interface AttachServiceWorkerServerOptions {
|
|
417
|
+
manageLifecycle?: boolean;
|
|
418
|
+
channelName?: string;
|
|
419
|
+
logger?: {
|
|
420
|
+
error?: (...args: unknown[]) => void;
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function createWakeBroadcaster(
|
|
425
|
+
globalScope: ServiceWorkerGlobalScopeLike,
|
|
426
|
+
channelName: string
|
|
427
|
+
): (message: ServiceWorkerWakeMessage) => void {
|
|
428
|
+
const channel =
|
|
429
|
+
typeof BroadcastChannel !== 'undefined'
|
|
430
|
+
? new BroadcastChannel(channelName)
|
|
431
|
+
: null;
|
|
432
|
+
|
|
433
|
+
return (message: ServiceWorkerWakeMessage): void => {
|
|
434
|
+
try {
|
|
435
|
+
if (channel) {
|
|
436
|
+
channel.postMessage(message);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
} catch {
|
|
440
|
+
// fall through to postMessage fallback
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
void globalScope.clients
|
|
444
|
+
?.matchAll?.({ type: 'window', includeUncontrolled: true })
|
|
445
|
+
.then((clients) => {
|
|
446
|
+
for (const client of clients) {
|
|
447
|
+
try {
|
|
448
|
+
client.postMessage(message);
|
|
449
|
+
} catch {
|
|
450
|
+
// best-effort wake-up
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
.catch(() => {
|
|
455
|
+
// best-effort wake-up
|
|
456
|
+
});
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function attachServiceWorkerServer(
|
|
461
|
+
globalScope: ServiceWorkerGlobalScopeLike,
|
|
462
|
+
server: ServiceWorkerServer,
|
|
463
|
+
options?: AttachServiceWorkerServerOptions
|
|
464
|
+
): void {
|
|
465
|
+
const manageLifecycle = options?.manageLifecycle ?? true;
|
|
466
|
+
const localOrigin = globalScope.location?.origin;
|
|
467
|
+
const broadcastWakeMessage = createWakeBroadcaster(
|
|
468
|
+
globalScope,
|
|
469
|
+
options?.channelName ?? SERVICE_WORKER_WAKE_CHANNEL
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
if (manageLifecycle) {
|
|
473
|
+
globalScope.addEventListener('install', (event: unknown) => {
|
|
474
|
+
const installEvent = event as ServiceWorkerLifecycleEventLike;
|
|
475
|
+
installEvent.waitUntil(globalScope.skipWaiting?.() ?? Promise.resolve());
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
globalScope.addEventListener('activate', (event: unknown) => {
|
|
479
|
+
const activateEvent = event as ServiceWorkerLifecycleEventLike;
|
|
480
|
+
activateEvent.waitUntil(
|
|
481
|
+
globalScope.clients?.claim?.() ?? Promise.resolve()
|
|
482
|
+
);
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
globalScope.addEventListener('fetch', (event: unknown) => {
|
|
487
|
+
const fetchEvent = event as ServiceWorkerFetchEventLike;
|
|
488
|
+
const request = fetchEvent.request;
|
|
489
|
+
|
|
490
|
+
if (!server.shouldHandleRequest(request, localOrigin)) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
fetchEvent.respondWith(
|
|
495
|
+
(async () => {
|
|
496
|
+
const wakeContext = await server.captureWakeContext(request);
|
|
497
|
+
const response = await server.handleRequest(request);
|
|
498
|
+
|
|
499
|
+
try {
|
|
500
|
+
const wakeMessage = await server.resolveWakeMessage({
|
|
501
|
+
request,
|
|
502
|
+
response,
|
|
503
|
+
wakeContext,
|
|
504
|
+
});
|
|
505
|
+
if (wakeMessage) {
|
|
506
|
+
broadcastWakeMessage(wakeMessage);
|
|
507
|
+
}
|
|
508
|
+
} catch (error) {
|
|
509
|
+
options?.logger?.error?.(
|
|
510
|
+
'[server-service-worker] failed to resolve wake message',
|
|
511
|
+
error
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return response;
|
|
516
|
+
})()
|
|
517
|
+
);
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
export interface ConfigureServiceWorkerServerOptions {
|
|
522
|
+
scriptPath: string;
|
|
523
|
+
healthPath?: string;
|
|
524
|
+
healthCheck?: (response: Response) => boolean | Promise<boolean>;
|
|
525
|
+
enabled?: boolean;
|
|
526
|
+
scope?: string;
|
|
527
|
+
type?: 'classic' | 'module';
|
|
528
|
+
updateViaCache?: 'imports' | 'all' | 'none';
|
|
529
|
+
unregisterOnDisable?: boolean;
|
|
530
|
+
controllerTimeoutMs?: number;
|
|
531
|
+
healthTimeoutMs?: number;
|
|
532
|
+
healthRetryDelayMs?: number;
|
|
533
|
+
healthRequestTimeoutMs?: number;
|
|
534
|
+
fetchImpl?: typeof fetch;
|
|
535
|
+
logger?: {
|
|
536
|
+
info?: (...args: unknown[]) => void;
|
|
537
|
+
warn?: (...args: unknown[]) => void;
|
|
538
|
+
error?: (...args: unknown[]) => void;
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export async function unregisterServiceWorkerRegistrations(
|
|
543
|
+
scriptPath: string
|
|
544
|
+
): Promise<void> {
|
|
545
|
+
if (typeof navigator === 'undefined' || !('serviceWorker' in navigator)) {
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const registrations = await navigator.serviceWorker.getRegistrations();
|
|
550
|
+
const unregisterTasks = registrations
|
|
551
|
+
.filter((registration) => {
|
|
552
|
+
const scriptUrl =
|
|
553
|
+
registration.active?.scriptURL ??
|
|
554
|
+
registration.waiting?.scriptURL ??
|
|
555
|
+
registration.installing?.scriptURL;
|
|
556
|
+
return scriptUrl?.includes(scriptPath) === true;
|
|
557
|
+
})
|
|
558
|
+
.map((registration) => registration.unregister());
|
|
559
|
+
|
|
560
|
+
await Promise.all(unregisterTasks);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function waitForControllerChange(timeoutMs: number): Promise<void> {
|
|
564
|
+
if (
|
|
565
|
+
typeof navigator === 'undefined' ||
|
|
566
|
+
!('serviceWorker' in navigator) ||
|
|
567
|
+
navigator.serviceWorker.controller
|
|
568
|
+
) {
|
|
569
|
+
return Promise.resolve();
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
return new Promise((resolve) => {
|
|
573
|
+
const onControllerChange = () => {
|
|
574
|
+
clearTimeout(timeoutId);
|
|
575
|
+
navigator.serviceWorker.removeEventListener(
|
|
576
|
+
'controllerchange',
|
|
577
|
+
onControllerChange
|
|
578
|
+
);
|
|
579
|
+
resolve();
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
const timeoutId = setTimeout(() => {
|
|
583
|
+
navigator.serviceWorker.removeEventListener(
|
|
584
|
+
'controllerchange',
|
|
585
|
+
onControllerChange
|
|
586
|
+
);
|
|
587
|
+
resolve();
|
|
588
|
+
}, timeoutMs);
|
|
589
|
+
|
|
590
|
+
navigator.serviceWorker.addEventListener(
|
|
591
|
+
'controllerchange',
|
|
592
|
+
onControllerChange
|
|
593
|
+
);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
async function waitForHealth(args: {
|
|
598
|
+
path: string;
|
|
599
|
+
timeoutMs: number;
|
|
600
|
+
retryDelayMs: number;
|
|
601
|
+
requestTimeoutMs: number;
|
|
602
|
+
fetchImpl: typeof fetch;
|
|
603
|
+
healthCheck?: (response: Response) => boolean | Promise<boolean>;
|
|
604
|
+
}): Promise<boolean> {
|
|
605
|
+
const started = Date.now();
|
|
606
|
+
|
|
607
|
+
while (Date.now() - started < args.timeoutMs) {
|
|
608
|
+
try {
|
|
609
|
+
const controller = new AbortController();
|
|
610
|
+
const timeoutId = setTimeout(
|
|
611
|
+
() => controller.abort(),
|
|
612
|
+
args.requestTimeoutMs
|
|
613
|
+
);
|
|
614
|
+
let response: Response;
|
|
615
|
+
try {
|
|
616
|
+
response = await args.fetchImpl(args.path, {
|
|
617
|
+
method: 'GET',
|
|
618
|
+
cache: 'no-store',
|
|
619
|
+
signal: controller.signal,
|
|
620
|
+
});
|
|
621
|
+
} finally {
|
|
622
|
+
clearTimeout(timeoutId);
|
|
623
|
+
}
|
|
624
|
+
if (response.ok) {
|
|
625
|
+
if (args.healthCheck) {
|
|
626
|
+
const matches = await args.healthCheck(response);
|
|
627
|
+
if (!matches) {
|
|
628
|
+
throw new Error('Service Worker health probe mismatch');
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
} catch {
|
|
634
|
+
// keep retrying until timeout
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
await new Promise((resolve) => setTimeout(resolve, args.retryDelayMs));
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
export async function configureServiceWorkerServer(
|
|
644
|
+
options: ConfigureServiceWorkerServerOptions
|
|
645
|
+
): Promise<boolean> {
|
|
646
|
+
if (
|
|
647
|
+
typeof window === 'undefined' ||
|
|
648
|
+
typeof navigator === 'undefined' ||
|
|
649
|
+
!('serviceWorker' in navigator)
|
|
650
|
+
) {
|
|
651
|
+
return false;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const enabled = options.enabled ?? true;
|
|
655
|
+
const logger = options.logger;
|
|
656
|
+
|
|
657
|
+
if (!enabled) {
|
|
658
|
+
if (options.unregisterOnDisable !== false) {
|
|
659
|
+
await unregisterServiceWorkerRegistrations(options.scriptPath);
|
|
660
|
+
}
|
|
661
|
+
logger?.warn?.('[server-service-worker] service worker mode disabled');
|
|
662
|
+
return false;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
try {
|
|
666
|
+
await navigator.serviceWorker.register(options.scriptPath, {
|
|
667
|
+
scope: options.scope,
|
|
668
|
+
type: options.type ?? 'module',
|
|
669
|
+
updateViaCache: options.updateViaCache ?? 'none',
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
await navigator.serviceWorker.ready;
|
|
673
|
+
await waitForControllerChange(options.controllerTimeoutMs ?? 5_000);
|
|
674
|
+
|
|
675
|
+
const healthy = await waitForHealth({
|
|
676
|
+
path: options.healthPath ?? '/api/health',
|
|
677
|
+
timeoutMs: options.healthTimeoutMs ?? 10_000,
|
|
678
|
+
retryDelayMs: options.healthRetryDelayMs ?? 150,
|
|
679
|
+
requestTimeoutMs: options.healthRequestTimeoutMs ?? 2_000,
|
|
680
|
+
fetchImpl: options.fetchImpl ?? fetch,
|
|
681
|
+
healthCheck: options.healthCheck,
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
if (!healthy) {
|
|
685
|
+
throw new Error('Service Worker server health check timed out');
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
logger?.info?.('[server-service-worker] service worker server enabled');
|
|
689
|
+
return true;
|
|
690
|
+
} catch (error) {
|
|
691
|
+
logger?.error?.(
|
|
692
|
+
'[server-service-worker] failed to enable service worker server',
|
|
693
|
+
error
|
|
694
|
+
);
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
}
|