@unvt/charites 2.1.2 → 2.1.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.
@@ -38,6 +38,14 @@ export async function serve(source, options) {
38
38
  }
39
39
  const server = http.createServer(async (req, res) => {
40
40
  const url = (req.url || '').replace(/\?.*/, '');
41
+ res.setHeader('Access-Control-Allow-Origin', '*');
42
+ res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS');
43
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
44
+ if (req.method === 'OPTIONS') {
45
+ res.statusCode = 204;
46
+ res.end();
47
+ return;
48
+ }
41
49
  if (typeof spriteOut !== 'undefined' &&
42
50
  url.match(/^\/sprite(@2x)?\.(json|png)/)) {
43
51
  res.statusCode = 200;
@@ -112,35 +120,38 @@ export async function serve(source, options) {
112
120
  }
113
121
  });
114
122
  const wss = new WebSocketServer({ server });
115
- wss.on('connection', (ws) => {
116
- const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
117
- console.log(`${(event || '').toUpperCase()}: ${file}`);
118
- try {
119
- if (file?.toLowerCase().endsWith('.yml')) {
120
- ws.send(JSON.stringify({
121
- event: 'styleUpdate',
122
- }));
123
- }
124
- else if (file?.toLowerCase().endsWith('.svg') &&
125
- typeof spriteRefresher !== 'undefined') {
126
- spriteRefresher().then(() => {
127
- ws.send(JSON.stringify({
128
- event: 'spriteUpdate',
129
- }));
130
- });
123
+ const sockets = new Set();
124
+ const watcher = watch(path.dirname(sourcePath), { recursive: true, filter: /\.yml$|\.svg$/i }, (event, file) => {
125
+ console.log(`${(event || '').toUpperCase()}: ${file}`);
126
+ try {
127
+ if (file?.toLowerCase().endsWith('.yml')) {
128
+ for (const client of sockets) {
129
+ client.send(JSON.stringify({ event: 'styleUpdate' }));
131
130
  }
132
131
  }
133
- catch (_) {
134
- // Nothing to do
132
+ else if (file?.toLowerCase().endsWith('.svg') &&
133
+ typeof spriteRefresher !== 'undefined') {
134
+ spriteRefresher().then(() => {
135
+ for (const client of sockets) {
136
+ client.send(JSON.stringify({ event: 'spriteUpdate' }));
137
+ }
138
+ });
135
139
  }
136
- });
137
- wss.on('close', () => {
138
- watcher.close();
140
+ }
141
+ catch (_) {
142
+ // Nothing to do
143
+ }
144
+ });
145
+ wss.on('connection', (ws) => {
146
+ sockets.add(ws);
147
+ ws.on('close', () => {
148
+ sockets.delete(ws);
139
149
  });
140
150
  });
141
151
  process.on('SIGINT', () => {
142
152
  console.log('Cleaning up...');
143
153
  server.close();
154
+ watcher.close();
144
155
  if (typeof spriteOut !== 'undefined') {
145
156
  fs.rmSync(spriteOut, { recursive: true });
146
157
  spriteOut = undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unvt/charites",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "",
5
5
  "bin": {
6
6
  "charites": "dist/cli.js"
@@ -5,6 +5,7 @@ import http from 'http'
5
5
  import open from 'open'
6
6
  // tweak to fix WebSocketServer is not a constructor
7
7
  const ws = await import('ws')
8
+ import type { WebSocket as WSWebSocket } from 'ws'
8
9
  const WebSocketServer = ws.default.WebSocketServer || ws.WebSocketServer
9
10
  import watch from 'node-watch'
10
11
 
@@ -54,6 +55,14 @@ export async function serve(source: string, options: serveOptions) {
54
55
 
55
56
  const server = http.createServer(async (req, res) => {
56
57
  const url = (req.url || '').replace(/\?.*/, '')
58
+ res.setHeader('Access-Control-Allow-Origin', '*')
59
+ res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS')
60
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
61
+ if (req.method === 'OPTIONS') {
62
+ res.statusCode = 204
63
+ res.end()
64
+ return
65
+ }
57
66
 
58
67
  if (
59
68
  typeof spriteOut !== 'undefined' &&
@@ -137,45 +146,45 @@ export async function serve(source: string, options: serveOptions) {
137
146
  })
138
147
 
139
148
  const wss = new WebSocketServer({ server })
149
+ const sockets = new Set<WSWebSocket>()
140
150
 
141
- wss.on('connection', (ws: WebSocket) => {
142
- const watcher = watch(
143
- path.dirname(sourcePath),
144
- { recursive: true, filter: /\.yml$|\.svg$/i },
145
- (event: string, file: string) => {
146
- console.log(`${(event || '').toUpperCase()}: ${file}`)
147
- try {
148
- if (file?.toLowerCase().endsWith('.yml')) {
149
- ws.send(
150
- JSON.stringify({
151
- event: 'styleUpdate',
152
- }),
153
- )
154
- } else if (
155
- file?.toLowerCase().endsWith('.svg') &&
156
- typeof spriteRefresher !== 'undefined'
157
- ) {
158
- spriteRefresher().then(() => {
159
- ws.send(
160
- JSON.stringify({
161
- event: 'spriteUpdate',
162
- }),
163
- )
164
- })
151
+ const watcher = watch(
152
+ path.dirname(sourcePath),
153
+ { recursive: true, filter: /\.yml$|\.svg$/i },
154
+ (event: string, file: string) => {
155
+ console.log(`${(event || '').toUpperCase()}: ${file}`)
156
+ try {
157
+ if (file?.toLowerCase().endsWith('.yml')) {
158
+ for (const client of sockets) {
159
+ client.send(JSON.stringify({ event: 'styleUpdate' }))
165
160
  }
166
- } catch (_) {
167
- // Nothing to do
161
+ } else if (
162
+ file?.toLowerCase().endsWith('.svg') &&
163
+ typeof spriteRefresher !== 'undefined'
164
+ ) {
165
+ spriteRefresher().then(() => {
166
+ for (const client of sockets) {
167
+ client.send(JSON.stringify({ event: 'spriteUpdate' }))
168
+ }
169
+ })
168
170
  }
169
- },
170
- )
171
- wss.on('close', () => {
172
- watcher.close()
171
+ } catch (_) {
172
+ // Nothing to do
173
+ }
174
+ },
175
+ )
176
+
177
+ wss.on('connection', (ws: WSWebSocket) => {
178
+ sockets.add(ws)
179
+ ws.on('close', () => {
180
+ sockets.delete(ws)
173
181
  })
174
182
  })
175
183
 
176
184
  process.on('SIGINT', () => {
177
185
  console.log('Cleaning up...')
178
186
  server.close()
187
+ watcher.close()
179
188
  if (typeof spriteOut !== 'undefined') {
180
189
  fs.rmSync(spriteOut, { recursive: true })
181
190
  spriteOut = undefined