@vrowzer/service-worker-server 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/LICENSE +20 -0
- package/README.md +81 -0
- package/dist/index.d.ts +240 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +225 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kazuya kawaguchi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @vrowzer/service-worker-server
|
|
2
|
+
|
|
3
|
+
A Node.js HTTP Server-like interface for Service Worker environments. Wraps `@vrowzer/service-worker` to provide familiar `listen()` / `close()` / event patterns for handling fetch events and MessageChannel connections inside a Service Worker.
|
|
4
|
+
|
|
5
|
+
## đŋ Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
# npm
|
|
9
|
+
npm install --save @vrowzer/service-worker-server
|
|
10
|
+
|
|
11
|
+
# pnpm
|
|
12
|
+
pnpm add @vrowzer/service-worker-server
|
|
13
|
+
|
|
14
|
+
# yarn
|
|
15
|
+
yarn add @vrowzer/service-worker-server
|
|
16
|
+
|
|
17
|
+
# bun
|
|
18
|
+
bun add @vrowzer/service-worker-server
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## đ Usage
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createSvcWorkerServer } from '@vrowzer/service-worker-server'
|
|
25
|
+
|
|
26
|
+
const server = createSvcWorkerServer(self, {
|
|
27
|
+
version: 'v1',
|
|
28
|
+
claimOnActivate: true
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
// Set fetch handler (like Node.js HTTP request handler)
|
|
32
|
+
server.setFetchHandler(event => {
|
|
33
|
+
event.respondWith(new Response('Hello from Service Worker!'))
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
// Start listening for fetch events
|
|
37
|
+
server.listen()
|
|
38
|
+
|
|
39
|
+
server.on('listening', () => {
|
|
40
|
+
console.log('Service Worker server is listening')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
server.on('error', err => {
|
|
44
|
+
console.error('Server error:', err)
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### MessageChannel Connections
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const server = createSvcWorkerServer(self, { version: 'v1' })
|
|
52
|
+
|
|
53
|
+
server.setFetchHandler(event => {
|
|
54
|
+
event.respondWith(new Response('OK'))
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
// Enable listening for MessageChannel port connections
|
|
58
|
+
server.listen({ enableListenConnections: true })
|
|
59
|
+
|
|
60
|
+
server.on('connection', event => {
|
|
61
|
+
console.log('Client connected:', event.clientId)
|
|
62
|
+
console.log('Ports:', event.ports)
|
|
63
|
+
console.log('Data:', event.data)
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## đ API Documentation
|
|
68
|
+
|
|
69
|
+
See [packages/service-worker/docs](../service-worker/docs/) for full API documentation.
|
|
70
|
+
|
|
71
|
+
## đ¤ Sponsors
|
|
72
|
+
|
|
73
|
+
<p align="center">
|
|
74
|
+
<a href="https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg">
|
|
75
|
+
<img alt="sponsor" src='https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg'/>
|
|
76
|
+
</a>
|
|
77
|
+
</p>
|
|
78
|
+
|
|
79
|
+
## ÂŠī¸ License
|
|
80
|
+
|
|
81
|
+
[MIT](http://opensource.org/licenses/MIT)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Emittable } from "@kazupon/jts-utils/event/emitter";
|
|
2
|
+
|
|
3
|
+
//#region ../service-worker/src/worker.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Service Worker options for {@link createSvcWorker}
|
|
7
|
+
*/
|
|
8
|
+
interface SvcWorkerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The version of this service worker
|
|
11
|
+
* This is used to identify the service worker when communicating with {@link SvcWorkerController}
|
|
12
|
+
*/
|
|
13
|
+
version: string;
|
|
14
|
+
/**
|
|
15
|
+
* Heartbeat interval in milliseconds
|
|
16
|
+
* @default 30000
|
|
17
|
+
*/
|
|
18
|
+
heartbeatInterval?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Timeout after which a session is considered stale (no PONG received)
|
|
21
|
+
* @default 60000
|
|
22
|
+
*/
|
|
23
|
+
sessionTimeout?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Debug logger function
|
|
26
|
+
*/
|
|
27
|
+
debug?: Console["debug"];
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/index.d.ts
|
|
31
|
+
/**
|
|
32
|
+
* Service worker server error
|
|
33
|
+
*/
|
|
34
|
+
declare class SvcWorkerServerError extends Error {
|
|
35
|
+
name: string;
|
|
36
|
+
constructor(message: string, cause?: Error);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The {@link SvcWorkerServer} constructor options
|
|
40
|
+
*/
|
|
41
|
+
interface SvcWorkerServerOptions extends SvcWorkerOptions {
|
|
42
|
+
/**
|
|
43
|
+
* Automatically call `clients.claim()` on `activate` event.
|
|
44
|
+
*/
|
|
45
|
+
claimOnActivate?: boolean;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Extend {@link ServiceWorkerState} with additional states
|
|
49
|
+
*
|
|
50
|
+
* - 'suspended': State when {@link SvcWorker | service worker} is suspended.
|
|
51
|
+
*/
|
|
52
|
+
type SvcWorkerServerState = ServiceWorkerState | "suspended";
|
|
53
|
+
/**
|
|
54
|
+
* Options for the {@link SvcWorkerServer.listen} method
|
|
55
|
+
*/
|
|
56
|
+
interface ListenOptions {
|
|
57
|
+
/**
|
|
58
|
+
* Timeout in milliseconds for waiting for the activate event.
|
|
59
|
+
* If the timeout is exceeded, an 'error' event is emitted.
|
|
60
|
+
* @default 30000 (30 seconds)
|
|
61
|
+
*/
|
|
62
|
+
activateTimeout?: number;
|
|
63
|
+
/**
|
|
64
|
+
* Enable listening for MessageChannel port connections.
|
|
65
|
+
* If set to true, the server will accept connections via `message` events from clients.
|
|
66
|
+
* @default false
|
|
67
|
+
*/
|
|
68
|
+
enableListenConnections?: boolean;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Connection event payload for {@link MessageChannel} connections.
|
|
72
|
+
*
|
|
73
|
+
* This interface represents a connection event that is emitted when a client
|
|
74
|
+
* sends a message with {@link MessagePort | MessagePorts} (typically for establishing a MessageChannel connection).
|
|
75
|
+
*
|
|
76
|
+
* @typeParam T - The type of the message data. Defaults to `unknown`.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* interface MyMessage {
|
|
81
|
+
* type: 'greeting' | 'farewell'
|
|
82
|
+
* payload: string
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* const server = createSvcWorkerServer<MyMessage>(self, options)
|
|
86
|
+
* server.on('connection', (event) => {
|
|
87
|
+
* // event.data is typed as MyMessage
|
|
88
|
+
* console.log(event.data.type, event.data.payload)
|
|
89
|
+
* // Access the MessagePorts
|
|
90
|
+
* console.log(event.ports)
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
interface ConnectionEvent<T = unknown> {
|
|
95
|
+
/**
|
|
96
|
+
* The MessagePorts received from the client.
|
|
97
|
+
*/
|
|
98
|
+
readonly ports: readonly MessagePort[];
|
|
99
|
+
/**
|
|
100
|
+
* The source of the message (Client, ServiceWorker, or MessagePort).
|
|
101
|
+
*/
|
|
102
|
+
readonly source: Client | ServiceWorker | MessagePort | null;
|
|
103
|
+
/**
|
|
104
|
+
* The message data with type safety.
|
|
105
|
+
*/
|
|
106
|
+
readonly data: T;
|
|
107
|
+
/**
|
|
108
|
+
* The client ID if the source is a Client.
|
|
109
|
+
*/
|
|
110
|
+
readonly clientId?: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Event map for {@link SvcWorkerServer}.
|
|
114
|
+
*
|
|
115
|
+
* This type defines the payload types for each event.
|
|
116
|
+
*
|
|
117
|
+
* @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.
|
|
118
|
+
*/
|
|
119
|
+
type SvcWorkerServerEventMap<MessageData = unknown> = {
|
|
120
|
+
/**
|
|
121
|
+
* Emitted when the server starts listening for fetch events.
|
|
122
|
+
*/
|
|
123
|
+
listening: void;
|
|
124
|
+
/**
|
|
125
|
+
* Emitted when a client connects via MessageChannel.
|
|
126
|
+
* This event is fired only when the message contains MessagePorts.
|
|
127
|
+
*/
|
|
128
|
+
connection: ConnectionEvent<MessageData>;
|
|
129
|
+
/**
|
|
130
|
+
* Emitted when the server is closed.
|
|
131
|
+
*/
|
|
132
|
+
close: void;
|
|
133
|
+
/**
|
|
134
|
+
* Emitted when an error occurs.
|
|
135
|
+
*/
|
|
136
|
+
error: Error;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* The Server for service worker environment
|
|
140
|
+
*
|
|
141
|
+
* This interface has like Node.js HTTP Server interfaces.
|
|
142
|
+
* This will be used as server that runs within a Service Worker environment.
|
|
143
|
+
*
|
|
144
|
+
* @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.
|
|
145
|
+
*/
|
|
146
|
+
interface SvcWorkerServer<MessageData = unknown> extends Emittable<SvcWorkerServerEventMap<MessageData>>, Disposable, AsyncDisposable {
|
|
147
|
+
/**
|
|
148
|
+
* The current state of the server
|
|
149
|
+
*/
|
|
150
|
+
readonly state: SvcWorkerServerState;
|
|
151
|
+
/**
|
|
152
|
+
* Set a fetch event handler
|
|
153
|
+
* @param handler - A function to handle fetch events
|
|
154
|
+
*/
|
|
155
|
+
setFetchHandler(handler: (event: FetchEvent) => void): void;
|
|
156
|
+
/**
|
|
157
|
+
* Start a server listening for service worker fetch events
|
|
158
|
+
*
|
|
159
|
+
* When the service worker fetch event handler is bound, the 'listening' event will be emitted.
|
|
160
|
+
* If `enableListenConnections` option is set to `true`, server will be started to listen MessageChannel connection too via {@link SvcWorkerServer.listenConnections} internally.
|
|
161
|
+
*
|
|
162
|
+
* @param options - Options for listening
|
|
163
|
+
* @returns The server instance
|
|
164
|
+
* @throws {SvcWorkerServerError} When the server is already listening or fetch handler is not set
|
|
165
|
+
*/
|
|
166
|
+
listen(options?: ListenOptions): SvcWorkerServer<MessageData>;
|
|
167
|
+
/**
|
|
168
|
+
* Start a MessageChannel port connections listening with `message` events from clients.
|
|
169
|
+
*
|
|
170
|
+
* @returns The server instance
|
|
171
|
+
*/
|
|
172
|
+
listenConnections(): SvcWorkerServer<MessageData>;
|
|
173
|
+
/**
|
|
174
|
+
* Stops the server from accepting new fetch event and close {@link MessageChannel} port connections
|
|
175
|
+
*
|
|
176
|
+
* When it will be finished, the optional callback `fn` will be called, and trigger 'close' event.
|
|
177
|
+
*
|
|
178
|
+
* @param cb - An optional callback function which will be called when the server is closed
|
|
179
|
+
* @param stopConnectionListening - If `true`, also stops listening for MessageChannel port connections too via {@link SvcWorkerServer.closeConnections}. Defaults to `false`.
|
|
180
|
+
* @returns The server instance
|
|
181
|
+
*/
|
|
182
|
+
close(cb?: (err?: Error) => void, stopConnectionListening?: boolean): SvcWorkerServer<MessageData>;
|
|
183
|
+
/**
|
|
184
|
+
* Closes {@link MessageChannel} port connections connected to this server.
|
|
185
|
+
*
|
|
186
|
+
* @param cb - An optional callback function which will be called when MessageChannel port connections are closed
|
|
187
|
+
* @returns The server instance
|
|
188
|
+
*/
|
|
189
|
+
closeConnections(cb?: (err?: Error) => void): SvcWorkerServer<MessageData>;
|
|
190
|
+
/**
|
|
191
|
+
* Returns the bound service worker address
|
|
192
|
+
*
|
|
193
|
+
* the address service worker script URL, or `null` if the server is not listening.
|
|
194
|
+
*
|
|
195
|
+
* @returns The service worker script URL or `null`
|
|
196
|
+
*/
|
|
197
|
+
address(): URL | null;
|
|
198
|
+
/**
|
|
199
|
+
* Asynchronously get the number of concurrent {@link MessageChannel} port connections on the server.
|
|
200
|
+
*/
|
|
201
|
+
getConnections(cb: (error: Error | null, count: number) => void): SvcWorkerServer<MessageData>;
|
|
202
|
+
/**
|
|
203
|
+
* `Symbol.dispose` for `using` syntax support (TypeScript 5.2+)
|
|
204
|
+
*/
|
|
205
|
+
[Symbol.dispose](): void;
|
|
206
|
+
/**
|
|
207
|
+
* Calls `close()` and returns a promise that fulfills when the server has closed.
|
|
208
|
+
*/
|
|
209
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Create a {@link SvcWorkerServer | Service worker server} instance.
|
|
213
|
+
*
|
|
214
|
+
* @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.
|
|
215
|
+
* @param self - The {@link ServiceWorkerGlobalScope} instance (typically `self` in a service worker)
|
|
216
|
+
* @param options - {@link SvcWorkerServerOptions | Service worker server options}
|
|
217
|
+
* @returns {@link SvcWorkerServer | Service worker server instance}
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* interface MyMessage {
|
|
222
|
+
* type: 'greeting' | 'farewell'
|
|
223
|
+
* payload: string
|
|
224
|
+
* }
|
|
225
|
+
*
|
|
226
|
+
* const server = createSvcWorkerServer<MyMessage>(self, { version: '1.0.0' })
|
|
227
|
+
* server.on('connection', (event) => {
|
|
228
|
+
* // event.data is typed as MyMessage
|
|
229
|
+
* console.log(event.data.type, event.data.payload)
|
|
230
|
+
* // Access the MessagePorts
|
|
231
|
+
* console.log(event.ports)
|
|
232
|
+
* // Access client ID if available
|
|
233
|
+
* console.log(event.clientId)
|
|
234
|
+
* })
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
declare function createSvcWorkerServer<MessageData = unknown>(self: ServiceWorkerGlobalScope, options: SvcWorkerServerOptions): SvcWorkerServer<MessageData>;
|
|
238
|
+
//#endregion
|
|
239
|
+
export { ConnectionEvent, ListenOptions, SvcWorkerServer, SvcWorkerServerError, SvcWorkerServerEventMap, SvcWorkerServerOptions, SvcWorkerServerState, createSvcWorkerServer };
|
|
240
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../service-worker/src/worker.ts","../src/index.ts"],"sourcesContent":[],"mappings":";;;;ACoBA;AAUA;AAYA;AAKiB,UDiDA,gBAAA,CCjDA;EAuCjB;;;;EAQ4C,OAAA,EAAA,MAAA;EAI3B;;AAcjB;;EASc,iBAAA,CAAA,EAAA,MAAA;EAQL;;AAgBT;;EACoB,cAAA,CAAA,EAAA,MAAA;EAIF;;;EAkBiC,KAAA,CAAA,EDlDzC,OCkDyC,CAAA,OAAA,CAAA;;;;;ADxEnD;;cC5Ea,oBAAA,SAA6B,KAAA;;EAA1C,WAAa,CAAA,OAAA,EAAA,MAAA,EAAA,KAE0B,CAF1B,EAE0B,KAAA;AAQvC;AAYA;AAKA;AAuCA;AAI2B,UA5DV,sBAAA,SAA+B,gBA4DrB,CAAA;EAIR;;;EAIF,eAAA,CAAA,EAAA,OAAA;;AAcjB;;;;;AAiCiB,KAvGL,oBAAA,GAAuB,kBAuGlB,GAAA,WAAA;;;;AAWkB,UA7GlB,aAAA,CA6GkB;EAYhB;;;;;EAkBC,eAAA,CAAA,EAAA,MAAA;EAAoE;;;;;EAiB3E,uBAAA,CAAA,EAAA,OAAA;;;;;;;;;AA4Cb;;;;;;;;;;;;;;;;;UAjKiB;;;;2BAIU;;;;mBAIR,SAAS,gBAAgB;;;;iBAI3B;;;;;;;;;;;;;KAcL;;;;;;;;;cASE,gBAAgB;;;;;;;;SAQrB;;;;;;;;;;UAgBQ,+CACP,UAAU,wBAAwB,eAAe,YAAY;;;;kBAIrD;;;;;mCAMiB;;;;;;;;;;;mBAYhB,gBAAgB,gBAAgB;;;;;;uBAO5B,gBAAgB;;;;;;;;;;oBAWnB,oDAAoD,gBAAgB;;;;;;;+BAQzD,iBAAiB,gBAAgB;;;;;;;;aASnD;;;;6BAKgB,uCAAuC,gBAAgB;;;;;;;;2BAUzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6BX,mDACR,mCACG,yBACR,gBAAgB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { Emitter } from "@kazupon/jts-utils/event";
|
|
2
|
+
import { createSvcWorker } from "@vrowzer/service-worker/worker";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* This entry file is for service worker server
|
|
7
|
+
*
|
|
8
|
+
* @module service-worker-server
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Service worker server error
|
|
16
|
+
*/
|
|
17
|
+
var SvcWorkerServerError = class extends Error {
|
|
18
|
+
name = "SvcWorkerServerError";
|
|
19
|
+
constructor(message, cause) {
|
|
20
|
+
super(message, { cause });
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Default timeout for waiting for the activate event (30 seconds)
|
|
25
|
+
*/
|
|
26
|
+
const DEFAULT_ACTIVATE_TIMEOUT = 3e4;
|
|
27
|
+
/**
|
|
28
|
+
* Create a {@link SvcWorkerServer | Service worker server} instance.
|
|
29
|
+
*
|
|
30
|
+
* @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.
|
|
31
|
+
* @param self - The {@link ServiceWorkerGlobalScope} instance (typically `self` in a service worker)
|
|
32
|
+
* @param options - {@link SvcWorkerServerOptions | Service worker server options}
|
|
33
|
+
* @returns {@link SvcWorkerServer | Service worker server instance}
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* interface MyMessage {
|
|
38
|
+
* type: 'greeting' | 'farewell'
|
|
39
|
+
* payload: string
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* const server = createSvcWorkerServer<MyMessage>(self, { version: '1.0.0' })
|
|
43
|
+
* server.on('connection', (event) => {
|
|
44
|
+
* // event.data is typed as MyMessage
|
|
45
|
+
* console.log(event.data.type, event.data.payload)
|
|
46
|
+
* // Access the MessagePorts
|
|
47
|
+
* console.log(event.ports)
|
|
48
|
+
* // Access client ID if available
|
|
49
|
+
* console.log(event.clientId)
|
|
50
|
+
* })
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function createSvcWorkerServer(self, options) {
|
|
54
|
+
const _emitter = Emitter();
|
|
55
|
+
const _svcWorker = createSvcWorker(self, options);
|
|
56
|
+
const _options = options;
|
|
57
|
+
const _ports = /* @__PURE__ */ new Set();
|
|
58
|
+
let _listening = false;
|
|
59
|
+
let _listeningConnections = false;
|
|
60
|
+
let _fetchHandler = null;
|
|
61
|
+
let _boundFetchHandler = null;
|
|
62
|
+
let _activateHandler = null;
|
|
63
|
+
let _activateTimeoutId = null;
|
|
64
|
+
let _messageHandler = null;
|
|
65
|
+
/**
|
|
66
|
+
* Cleanup activate waiting state (timeout and handler)
|
|
67
|
+
*/
|
|
68
|
+
function cleanupActivateWaiting() {
|
|
69
|
+
if (_activateTimeoutId !== null) {
|
|
70
|
+
clearTimeout(_activateTimeoutId);
|
|
71
|
+
_activateTimeoutId = null;
|
|
72
|
+
}
|
|
73
|
+
if (_activateHandler) {
|
|
74
|
+
_svcWorker.removeEventListener("activate", _activateHandler);
|
|
75
|
+
_activateHandler = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function listenConnections() {
|
|
79
|
+
if (_listeningConnections) return instance;
|
|
80
|
+
_messageHandler = (event) => {
|
|
81
|
+
if (event.ports && event.ports.length > 0) {
|
|
82
|
+
for (const port of event.ports) _ports.add(port);
|
|
83
|
+
const clientId = event.source?.id;
|
|
84
|
+
const connectionEvent = {
|
|
85
|
+
ports: event.ports,
|
|
86
|
+
source: event.source,
|
|
87
|
+
data: event.data,
|
|
88
|
+
...clientId !== void 0 && { clientId }
|
|
89
|
+
};
|
|
90
|
+
_emitter.emit("connection", connectionEvent);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
self.addEventListener("message", _messageHandler);
|
|
94
|
+
_listeningConnections = true;
|
|
95
|
+
return instance;
|
|
96
|
+
}
|
|
97
|
+
function setFetchHandler(handler) {
|
|
98
|
+
if (typeof handler !== "function") throw new SvcWorkerServerError("fetch handler must be a function");
|
|
99
|
+
if (_boundFetchHandler) _svcWorker.removeEventListener("fetch", _boundFetchHandler);
|
|
100
|
+
_fetchHandler = handler;
|
|
101
|
+
_boundFetchHandler = (event) => {
|
|
102
|
+
if (!_listening || _svcWorker.suspended) return;
|
|
103
|
+
try {
|
|
104
|
+
_fetchHandler(event);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
_emitter.emit("error", err);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
_svcWorker.addEventListener("fetch", _boundFetchHandler);
|
|
110
|
+
}
|
|
111
|
+
function listen(listenOptions) {
|
|
112
|
+
if (_listening) {
|
|
113
|
+
queueMicrotask(() => _emitter.emit("error", new SvcWorkerServerError("Server is already listening")));
|
|
114
|
+
return instance;
|
|
115
|
+
}
|
|
116
|
+
if (!_fetchHandler) {
|
|
117
|
+
queueMicrotask(() => _emitter.emit("error", new SvcWorkerServerError("Fetch handler not set. Call setFetchHandler() first.")));
|
|
118
|
+
return instance;
|
|
119
|
+
}
|
|
120
|
+
const activateTimeout = listenOptions?.activateTimeout ?? DEFAULT_ACTIVATE_TIMEOUT;
|
|
121
|
+
const enableListenConnections = listenOptions?.enableListenConnections ?? false;
|
|
122
|
+
_listening = true;
|
|
123
|
+
if (enableListenConnections) listenConnections();
|
|
124
|
+
if (_svcWorker.registration.active !== null) queueMicrotask(() => {
|
|
125
|
+
if (_listening) _emitter.emit("listening");
|
|
126
|
+
});
|
|
127
|
+
else {
|
|
128
|
+
_activateTimeoutId = setTimeout(() => {
|
|
129
|
+
cleanupActivateWaiting();
|
|
130
|
+
if (_listening) {
|
|
131
|
+
_listening = false;
|
|
132
|
+
_fetchHandler = null;
|
|
133
|
+
_boundFetchHandler = null;
|
|
134
|
+
_emitter.emit("error", new SvcWorkerServerError(`Activate timeout: Service Worker did not activate within ${activateTimeout}ms`));
|
|
135
|
+
}
|
|
136
|
+
}, activateTimeout);
|
|
137
|
+
_activateHandler = (event) => {
|
|
138
|
+
cleanupActivateWaiting();
|
|
139
|
+
if (_options.claimOnActivate) {
|
|
140
|
+
const serviceWorkerScope = _svcWorker;
|
|
141
|
+
event.waitUntil(serviceWorkerScope.clients.claim());
|
|
142
|
+
}
|
|
143
|
+
if (_listening) _emitter.emit("listening");
|
|
144
|
+
};
|
|
145
|
+
_svcWorker.addEventListener("activate", _activateHandler);
|
|
146
|
+
}
|
|
147
|
+
return instance;
|
|
148
|
+
}
|
|
149
|
+
function closeConnections(cb) {
|
|
150
|
+
if (_messageHandler) {
|
|
151
|
+
self.removeEventListener("message", _messageHandler);
|
|
152
|
+
_messageHandler = null;
|
|
153
|
+
}
|
|
154
|
+
closeAllConnections();
|
|
155
|
+
_listeningConnections = false;
|
|
156
|
+
queueMicrotask(() => {
|
|
157
|
+
cb?.();
|
|
158
|
+
});
|
|
159
|
+
return instance;
|
|
160
|
+
}
|
|
161
|
+
function close(cb, stopConnectionListening) {
|
|
162
|
+
if (!_listening) {
|
|
163
|
+
queueMicrotask(() => {
|
|
164
|
+
cb?.();
|
|
165
|
+
_emitter.emit("close");
|
|
166
|
+
});
|
|
167
|
+
return instance;
|
|
168
|
+
}
|
|
169
|
+
cleanupActivateWaiting();
|
|
170
|
+
if (_boundFetchHandler) {
|
|
171
|
+
_svcWorker.removeEventListener("fetch", _boundFetchHandler);
|
|
172
|
+
_boundFetchHandler = null;
|
|
173
|
+
}
|
|
174
|
+
if (stopConnectionListening) closeConnections();
|
|
175
|
+
_fetchHandler = null;
|
|
176
|
+
_listening = false;
|
|
177
|
+
queueMicrotask(() => {
|
|
178
|
+
cb?.();
|
|
179
|
+
_emitter.emit("close");
|
|
180
|
+
});
|
|
181
|
+
return instance;
|
|
182
|
+
}
|
|
183
|
+
function address() {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
function getConnections(cb) {
|
|
187
|
+
queueMicrotask(() => cb(null, _ports.size));
|
|
188
|
+
return instance;
|
|
189
|
+
}
|
|
190
|
+
function closeAllConnections() {
|
|
191
|
+
for (const port of _ports) port.close();
|
|
192
|
+
_ports.clear();
|
|
193
|
+
}
|
|
194
|
+
function dispose() {
|
|
195
|
+
close();
|
|
196
|
+
}
|
|
197
|
+
async function asyncDispose() {
|
|
198
|
+
return new Promise((resolve, reject) => {
|
|
199
|
+
close((err) => {
|
|
200
|
+
if (err) reject(err);
|
|
201
|
+
else resolve();
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
const instance = {
|
|
206
|
+
..._emitter,
|
|
207
|
+
get state() {
|
|
208
|
+
return "installing";
|
|
209
|
+
},
|
|
210
|
+
setFetchHandler,
|
|
211
|
+
listen,
|
|
212
|
+
listenConnections,
|
|
213
|
+
close,
|
|
214
|
+
closeConnections,
|
|
215
|
+
address,
|
|
216
|
+
getConnections,
|
|
217
|
+
[Symbol.dispose]: dispose,
|
|
218
|
+
[Symbol.asyncDispose]: asyncDispose
|
|
219
|
+
};
|
|
220
|
+
return instance;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
//#endregion
|
|
224
|
+
export { SvcWorkerServerError, createSvcWorkerServer };
|
|
225
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["_svcWorker: SvcWorker","_ports: Set<MessagePort>","_fetchHandler: ((event: FetchEvent) => void) | null","_boundFetchHandler: ((event: FetchEvent) => void) | null","_activateHandler: ((event: ExtendableEvent) => void) | null","_activateTimeoutId: ReturnType<typeof setTimeout> | null","_messageHandler: ((event: ExtendableMessageEvent) => void) | null","connectionEvent: ConnectionEvent<MessageData>","instance: SvcWorkerServer<MessageData>"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * This entry file is for service worker server\n *\n * @module service-worker-server\n */\n\n/**\n * @author kazuya kawaguchi (a.k.a. kazupon)\n * @license MIT\n */\n\nimport { Emitter } from '@kazupon/jts-utils/event'\nimport { createSvcWorker } from '@vrowzer/service-worker/worker'\n\nimport type { Emittable } from '@kazupon/jts-utils/event/emitter'\nimport type { SvcWorker, SvcWorkerOptions } from '@vrowzer/service-worker/worker'\n\n/**\n * Service worker server error\n */\nexport class SvcWorkerServerError extends Error {\n name = 'SvcWorkerServerError'\n constructor(message: string, cause?: Error) {\n super(message, { cause })\n }\n}\n\n/**\n * The {@link SvcWorkerServer} constructor options\n */\nexport interface SvcWorkerServerOptions extends SvcWorkerOptions {\n /**\n * Automatically call `clients.claim()` on `activate` event.\n */\n claimOnActivate?: boolean\n}\n\n/**\n * Extend {@link ServiceWorkerState} with additional states\n *\n * - 'suspended': State when {@link SvcWorker | service worker} is suspended.\n */\nexport type SvcWorkerServerState = ServiceWorkerState | 'suspended'\n\n/**\n * Options for the {@link SvcWorkerServer.listen} method\n */\nexport interface ListenOptions {\n /**\n * Timeout in milliseconds for waiting for the activate event.\n * If the timeout is exceeded, an 'error' event is emitted.\n * @default 30000 (30 seconds)\n */\n activateTimeout?: number\n /**\n * Enable listening for MessageChannel port connections.\n * If set to true, the server will accept connections via `message` events from clients.\n * @default false\n */\n enableListenConnections?: boolean\n}\n\n/**\n * Connection event payload for {@link MessageChannel} connections.\n *\n * This interface represents a connection event that is emitted when a client\n * sends a message with {@link MessagePort | MessagePorts} (typically for establishing a MessageChannel connection).\n *\n * @typeParam T - The type of the message data. Defaults to `unknown`.\n *\n * @example\n * ```ts\n * interface MyMessage {\n * type: 'greeting' | 'farewell'\n * payload: string\n * }\n *\n * const server = createSvcWorkerServer<MyMessage>(self, options)\n * server.on('connection', (event) => {\n * // event.data is typed as MyMessage\n * console.log(event.data.type, event.data.payload)\n * // Access the MessagePorts\n * console.log(event.ports)\n * })\n * ```\n */\nexport interface ConnectionEvent<T = unknown> {\n /**\n * The MessagePorts received from the client.\n */\n readonly ports: readonly MessagePort[]\n /**\n * The source of the message (Client, ServiceWorker, or MessagePort).\n */\n readonly source: Client | ServiceWorker | MessagePort | null\n /**\n * The message data with type safety.\n */\n readonly data: T\n /**\n * The client ID if the source is a Client.\n */\n readonly clientId?: string\n}\n\n/**\n * Event map for {@link SvcWorkerServer}.\n *\n * This type defines the payload types for each event.\n *\n * @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.\n */\nexport type SvcWorkerServerEventMap<MessageData = unknown> = {\n /**\n * Emitted when the server starts listening for fetch events.\n */\n listening: void\n /**\n * Emitted when a client connects via MessageChannel.\n * This event is fired only when the message contains MessagePorts.\n */\n connection: ConnectionEvent<MessageData>\n /**\n * Emitted when the server is closed.\n */\n close: void\n /**\n * Emitted when an error occurs.\n */\n error: Error\n}\n\n/**\n * Default timeout for waiting for the activate event (30 seconds)\n */\nconst DEFAULT_ACTIVATE_TIMEOUT = 30000\n\n/**\n * The Server for service worker environment\n *\n * This interface has like Node.js HTTP Server interfaces.\n * This will be used as server that runs within a Service Worker environment.\n *\n * @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.\n */\nexport interface SvcWorkerServer<MessageData = unknown>\n extends Emittable<SvcWorkerServerEventMap<MessageData>>, Disposable, AsyncDisposable {\n /**\n * The current state of the server\n */\n readonly state: SvcWorkerServerState\n\n /**\n * Set a fetch event handler\n * @param handler - A function to handle fetch events\n */\n setFetchHandler(handler: (event: FetchEvent) => void): void\n\n /**\n * Start a server listening for service worker fetch events\n *\n * When the service worker fetch event handler is bound, the 'listening' event will be emitted.\n * If `enableListenConnections` option is set to `true`, server will be started to listen MessageChannel connection too via {@link SvcWorkerServer.listenConnections} internally.\n *\n * @param options - Options for listening\n * @returns The server instance\n * @throws {SvcWorkerServerError} When the server is already listening or fetch handler is not set\n */\n listen(options?: ListenOptions): SvcWorkerServer<MessageData>\n\n /**\n * Start a MessageChannel port connections listening with `message` events from clients.\n *\n * @returns The server instance\n */\n listenConnections(): SvcWorkerServer<MessageData>\n\n /**\n * Stops the server from accepting new fetch event and close {@link MessageChannel} port connections\n *\n * When it will be finished, the optional callback `fn` will be called, and trigger 'close' event.\n *\n * @param cb - An optional callback function which will be called when the server is closed\n * @param stopConnectionListening - If `true`, also stops listening for MessageChannel port connections too via {@link SvcWorkerServer.closeConnections}. Defaults to `false`.\n * @returns The server instance\n */\n close(cb?: (err?: Error) => void, stopConnectionListening?: boolean): SvcWorkerServer<MessageData>\n\n /**\n * Closes {@link MessageChannel} port connections connected to this server.\n *\n * @param cb - An optional callback function which will be called when MessageChannel port connections are closed\n * @returns The server instance\n */\n closeConnections(cb?: (err?: Error) => void): SvcWorkerServer<MessageData>\n\n /**\n * Returns the bound service worker address\n *\n * the address service worker script URL, or `null` if the server is not listening.\n *\n * @returns The service worker script URL or `null`\n */\n address(): URL | null\n\n /**\n * Asynchronously get the number of concurrent {@link MessageChannel} port connections on the server.\n */\n getConnections(cb: (error: Error | null, count: number) => void): SvcWorkerServer<MessageData>\n\n /**\n * `Symbol.dispose` for `using` syntax support (TypeScript 5.2+)\n */\n [Symbol.dispose](): void\n\n /**\n * Calls `close()` and returns a promise that fulfills when the server has closed.\n */\n [Symbol.asyncDispose](): Promise<void>\n}\n\n/**\n * Create a {@link SvcWorkerServer | Service worker server} instance.\n *\n * @typeParam MessageData - The type of the message data for the `connection` event. Defaults to `unknown`.\n * @param self - The {@link ServiceWorkerGlobalScope} instance (typically `self` in a service worker)\n * @param options - {@link SvcWorkerServerOptions | Service worker server options}\n * @returns {@link SvcWorkerServer | Service worker server instance}\n *\n * @example\n * ```ts\n * interface MyMessage {\n * type: 'greeting' | 'farewell'\n * payload: string\n * }\n *\n * const server = createSvcWorkerServer<MyMessage>(self, { version: '1.0.0' })\n * server.on('connection', (event) => {\n * // event.data is typed as MyMessage\n * console.log(event.data.type, event.data.payload)\n * // Access the MessagePorts\n * console.log(event.ports)\n * // Access client ID if available\n * console.log(event.clientId)\n * })\n * ```\n */\nexport function createSvcWorkerServer<MessageData = unknown>(\n self: ServiceWorkerGlobalScope,\n options: SvcWorkerServerOptions\n): SvcWorkerServer<MessageData> {\n const _emitter = Emitter<SvcWorkerServerEventMap<MessageData>>()\n const _svcWorker: SvcWorker = createSvcWorker(self, options)\n const _options = options\n\n const _ports: Set<MessagePort> = new Set()\n\n let _listening = false\n let _listeningConnections = false\n let _fetchHandler: ((event: FetchEvent) => void) | null = null\n let _boundFetchHandler: ((event: FetchEvent) => void) | null = null\n let _activateHandler: ((event: ExtendableEvent) => void) | null = null\n let _activateTimeoutId: ReturnType<typeof setTimeout> | null = null\n let _messageHandler: ((event: ExtendableMessageEvent) => void) | null = null\n\n /**\n * Cleanup activate waiting state (timeout and handler)\n */\n function cleanupActivateWaiting(): void {\n if (_activateTimeoutId !== null) {\n clearTimeout(_activateTimeoutId)\n _activateTimeoutId = null\n }\n if (_activateHandler) {\n _svcWorker.removeEventListener('activate', _activateHandler)\n _activateHandler = null\n }\n }\n\n function listenConnections(): SvcWorkerServer<MessageData> {\n // Already listening for connections -> do nothing\n if (_listeningConnections) {\n return instance\n }\n\n // Register message handler for connection events\n _messageHandler = (event: ExtendableMessageEvent) => {\n // Only emit connection event when ports are present\n if (event.ports && event.ports.length > 0) {\n // Register ports to the Set\n for (const port of event.ports) {\n _ports.add(port)\n }\n\n const clientId = (event.source as Client | null)?.id\n const connectionEvent: ConnectionEvent<MessageData> = {\n ports: event.ports,\n source: event.source,\n data: event.data as MessageData,\n ...(clientId !== undefined && { clientId })\n }\n _emitter.emit('connection', connectionEvent)\n }\n }\n self.addEventListener('message', _messageHandler)\n\n _listeningConnections = true\n\n return instance\n }\n\n function setFetchHandler(handler: (event: FetchEvent) => void): void {\n if (typeof handler !== 'function') {\n throw new SvcWorkerServerError('fetch handler must be a function')\n }\n\n // If already registered, remove previous handler first\n if (_boundFetchHandler) {\n _svcWorker.removeEventListener('fetch', _boundFetchHandler)\n }\n\n // Store user's handler\n _fetchHandler = handler\n\n // Create wrapper with suspended/listening check\n // This wrapper is registered immediately but only calls user's handler when listening\n _boundFetchHandler = (event: FetchEvent) => {\n // Don't call user's handler if not listening or suspended\n if (!_listening || _svcWorker.suspended) {\n return // Fall through to network\n }\n try {\n _fetchHandler!(event)\n } catch (err) {\n _emitter.emit('error', err as Error)\n }\n }\n\n // Register fetch handler immediately (required by Service Worker spec)\n // IMPORTANT: Service Workers require fetch event listeners to be added during\n // the initial script execution, not asynchronously during event callbacks.\n _svcWorker.addEventListener('fetch', _boundFetchHandler)\n }\n\n function listen(listenOptions?: ListenOptions): SvcWorkerServer<MessageData> {\n // Prevent double listen\n if (_listening) {\n queueMicrotask(() =>\n _emitter.emit('error', new SvcWorkerServerError('Server is already listening'))\n )\n return instance\n }\n\n // Validate fetch handler is set\n if (!_fetchHandler) {\n queueMicrotask(() =>\n _emitter.emit(\n 'error',\n new SvcWorkerServerError('Fetch handler not set. Call setFetchHandler() first.')\n )\n )\n return instance\n }\n\n // Resolve options\n const activateTimeout = listenOptions?.activateTimeout ?? DEFAULT_ACTIVATE_TIMEOUT\n const enableListenConnections = listenOptions?.enableListenConnections ?? false\n\n // Update state - this enables the fetch handler wrapper to call user's handler\n _listening = true\n\n // NOTE: addEventListener('fetch') is already registered in setFetchHandler()\n\n // Register message handler if enableListenConnections is true\n if (enableListenConnections) {\n listenConnections()\n }\n\n // Check activated state\n const sw = _svcWorker as unknown as ServiceWorkerGlobalScope\n const isActivated = sw.registration.active !== null\n\n if (isActivated) {\n // Already activated -> emit 'listening' immediately (async)\n queueMicrotask(() => {\n if (_listening) {\n _emitter.emit('listening')\n }\n })\n } else {\n // Not activated -> wait for activate event with timeout\n\n // Set timeout\n _activateTimeoutId = setTimeout(() => {\n cleanupActivateWaiting()\n if (_listening) {\n _listening = false\n _fetchHandler = null\n _boundFetchHandler = null\n _emitter.emit(\n 'error',\n new SvcWorkerServerError(\n `Activate timeout: Service Worker did not activate within ${activateTimeout}ms`\n )\n )\n }\n }, activateTimeout)\n\n // Set activate handler\n _activateHandler = (event: ExtendableEvent) => {\n // Cleanup timeout and handler\n cleanupActivateWaiting()\n\n // Call `clients.claim()` if `claimOnActivate` is true\n if (_options.claimOnActivate) {\n const serviceWorkerScope = _svcWorker as unknown as ServiceWorkerGlobalScope\n event.waitUntil(serviceWorkerScope.clients.claim())\n }\n\n // Emit listening event after activation\n if (_listening) {\n _emitter.emit('listening')\n }\n }\n _svcWorker.addEventListener('activate', _activateHandler)\n }\n\n return instance\n }\n\n function closeConnections(cb?: (err?: Error) => void): SvcWorkerServer<MessageData> {\n // Remove message event listener\n if (_messageHandler) {\n self.removeEventListener('message', _messageHandler)\n _messageHandler = null\n }\n\n // Close all MessagePort connections\n closeAllConnections()\n\n // Update state\n _listeningConnections = false\n\n // Emit callback (async)\n queueMicrotask(() => {\n cb?.()\n })\n\n return instance\n }\n\n function close(\n cb?: (err?: Error) => void,\n stopConnectionListening?: boolean\n ): SvcWorkerServer<MessageData> {\n // Emit callback and 'close' event even if not listening\n if (!_listening) {\n queueMicrotask(() => {\n cb?.()\n _emitter.emit('close')\n })\n return instance\n }\n\n // Cleanup activate waiting state (timeout and handler)\n cleanupActivateWaiting()\n\n // Remove fetch event listener\n if (_boundFetchHandler) {\n _svcWorker.removeEventListener('fetch', _boundFetchHandler)\n _boundFetchHandler = null\n }\n\n // Stop connection listening if requested (via closeConnections)\n if (stopConnectionListening) {\n closeConnections()\n }\n\n // Clear handler reference\n _fetchHandler = null\n\n // Update state\n _listening = false\n\n // Emit callback and 'close' event (async)\n queueMicrotask(() => {\n cb?.()\n _emitter.emit('close')\n })\n\n return instance\n }\n\n function address(): URL | null {\n // TODO: implement\n return null\n }\n\n function getConnections(\n cb: (error: Error | null, count: number) => void\n ): SvcWorkerServer<MessageData> {\n queueMicrotask(() => cb(null, _ports.size))\n return instance\n }\n\n function closeAllConnections(): void {\n for (const port of _ports) {\n port.close()\n }\n _ports.clear()\n }\n\n function dispose(): void {\n close()\n }\n\n async function asyncDispose(): Promise<void> {\n return new Promise((resolve, reject) => {\n close(err => {\n if (err) {\n reject(err)\n } else {\n resolve()\n }\n })\n })\n }\n\n const instance: SvcWorkerServer<MessageData> = {\n ..._emitter,\n get state(): SvcWorkerServerState {\n // TODO:\n return 'installing'\n },\n setFetchHandler,\n listen,\n listenConnections,\n close,\n closeConnections,\n address,\n getConnections,\n [Symbol.dispose]: dispose,\n [Symbol.asyncDispose]: asyncDispose\n } as SvcWorkerServer<MessageData>\n\n return instance\n}\n"],"mappings":"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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vrowzer/service-worker-server",
|
|
3
|
+
"description": "Serverized service worker with the Node Server interface",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "kazuya kawaguchi",
|
|
7
|
+
"email": "kawakazu80@gmail.com"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"funding": "https://github.com/sponsors/kazupon",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/kazupon/vrowzer/issues"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/kazupon/vrowzer.git",
|
|
17
|
+
"directory": "packages/service-worker-server"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"service-worker",
|
|
21
|
+
"vrowzer",
|
|
22
|
+
"server"
|
|
23
|
+
],
|
|
24
|
+
"homepage": "https://github.com/kazupon/vrowzer/packages/service-worker-server/README.md",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">= 20.19.0"
|
|
30
|
+
},
|
|
31
|
+
"type": "module",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"typesVersions": {
|
|
43
|
+
"*": {
|
|
44
|
+
"*": [
|
|
45
|
+
"./dist/*",
|
|
46
|
+
"./*"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@kazupon/jts-utils": "^0.17.0",
|
|
52
|
+
"@vrowzer/service-worker": "0.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@playwright/test": "1.57.0",
|
|
56
|
+
"get-port-please": "^3.2.0",
|
|
57
|
+
"publint": "^0.3.18",
|
|
58
|
+
"tsdown": "0.18.4",
|
|
59
|
+
"typedoc": "^0.28.17",
|
|
60
|
+
"vite": "^8.0.0"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"typecheck": "tsgo --noEmit -p tsconfig.json",
|
|
64
|
+
"build": "tsdown",
|
|
65
|
+
"build:docs": "typedoc --excludeInternal"
|
|
66
|
+
}
|
|
67
|
+
}
|