@comapeo/map-server 1.0.0-pre.4 → 1.0.0-pre.6
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/dist/routes/root.d.ts.map +1 -1
- package/dist/routes/root.js +30 -2
- package/package.json +9 -1
- package/src/routes/root.ts +32 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"root.d.ts","sourceRoot":"","sources":["../../src/routes/root.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAG5C,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"root.d.ts","sourceRoot":"","sources":["../../src/routes/root.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AAG5C,OAAO,KAAK,EAAgB,cAAc,EAAE,MAAM,aAAa,CAAA;AAoC/D,wBAAgB,UAAU,CAAC,EAAE,IAAU,EAAE;;CAAA,EAAE,GAAG,EAAE,OAAO,GAAG,cAAc,CAyBvE"}
|
package/dist/routes/root.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { json, Router } from 'itty-router';
|
|
1
|
+
import { cors, json, Router } from 'itty-router';
|
|
2
2
|
import { error } from '../lib/errors.js';
|
|
3
3
|
import { localhostOnly } from '../middlewares/localhost-only.js';
|
|
4
4
|
import { DownloadsRouter } from './downloads.js';
|
|
@@ -7,15 +7,43 @@ import { MapsRouter } from './maps.js';
|
|
|
7
7
|
const MAPS_BASE = '/maps/';
|
|
8
8
|
const MAP_SHARES_BASE = '/mapShares/';
|
|
9
9
|
const DOWNLOADS_BASE = '/downloads/';
|
|
10
|
+
const { preflight } = cors({
|
|
11
|
+
origin: '*',
|
|
12
|
+
allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
|
|
13
|
+
allowHeaders: ['Content-Type'],
|
|
14
|
+
});
|
|
15
|
+
/**
|
|
16
|
+
* Custom corsify that doesn't clone the response body.
|
|
17
|
+
* The built-in itty-router corsify uses response.clone() which breaks
|
|
18
|
+
* streaming response error handling (abort signals don't propagate correctly).
|
|
19
|
+
*/
|
|
20
|
+
function corsify(response) {
|
|
21
|
+
// Skip if CORS headers already present or if it's a WebSocket upgrade
|
|
22
|
+
if (response.headers.get('access-control-allow-origin') || response.status === 101) {
|
|
23
|
+
return response;
|
|
24
|
+
}
|
|
25
|
+
// Create new headers with CORS header added
|
|
26
|
+
const headers = new Headers(response.headers);
|
|
27
|
+
headers.set('access-control-allow-origin', '*');
|
|
28
|
+
// Return new Response with same body (not cloned) and updated headers
|
|
29
|
+
return new Response(response.body, {
|
|
30
|
+
status: response.status,
|
|
31
|
+
statusText: response.statusText,
|
|
32
|
+
headers,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
10
35
|
export function RootRouter({ base = '/' }, ctx) {
|
|
11
36
|
const router = Router({
|
|
12
37
|
base,
|
|
38
|
+
// Handle CORS preflight OPTIONS requests
|
|
39
|
+
before: [preflight],
|
|
13
40
|
// The `error` handler will send a response with the status code from any
|
|
14
41
|
// thrown StatusError, or a 500 for any other errors.
|
|
15
42
|
catch: (err) => error(err),
|
|
16
43
|
// Sends a 404 response for any requests that don't match a route, and for
|
|
17
44
|
// any request handlers that return JSON will send a JSON response.
|
|
18
|
-
|
|
45
|
+
// corsify adds CORS headers to all responses.
|
|
46
|
+
finally: [(response) => response ?? error(404), json, corsify],
|
|
19
47
|
});
|
|
20
48
|
const mapsRouter = MapsRouter({ base: MAPS_BASE }, ctx);
|
|
21
49
|
const downloadsRouter = DownloadsRouter({ base: DOWNLOADS_BASE }, ctx);
|
package/package.json
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@comapeo/map-server",
|
|
3
|
-
"version": "1.0.0-pre.
|
|
3
|
+
"version": "1.0.0-pre.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"default": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./errors.js": {
|
|
11
|
+
"types": "./dist/lib/errors.d.ts",
|
|
12
|
+
"default": "./dist/lib/errors.js"
|
|
13
|
+
},
|
|
14
|
+
"./constants.js": {
|
|
15
|
+
"types": "./dist/lib/constants.d.ts",
|
|
16
|
+
"default": "./dist/lib/constants.js"
|
|
9
17
|
}
|
|
10
18
|
},
|
|
11
19
|
"scripts": {
|
package/src/routes/root.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { json, Router, type IRequestStrict } from 'itty-router'
|
|
1
|
+
import { cors, json, Router, type IRequestStrict } from 'itty-router'
|
|
2
2
|
|
|
3
3
|
import type { Context } from '../context.js'
|
|
4
4
|
import { error } from '../lib/errors.js'
|
|
@@ -12,15 +12,45 @@ const MAPS_BASE = '/maps/'
|
|
|
12
12
|
const MAP_SHARES_BASE = '/mapShares/'
|
|
13
13
|
const DOWNLOADS_BASE = '/downloads/'
|
|
14
14
|
|
|
15
|
+
const { preflight } = cors({
|
|
16
|
+
origin: '*',
|
|
17
|
+
allowMethods: ['GET', 'POST', 'DELETE', 'OPTIONS'],
|
|
18
|
+
allowHeaders: ['Content-Type'],
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Custom corsify that doesn't clone the response body.
|
|
23
|
+
* The built-in itty-router corsify uses response.clone() which breaks
|
|
24
|
+
* streaming response error handling (abort signals don't propagate correctly).
|
|
25
|
+
*/
|
|
26
|
+
function corsify(response: Response): Response {
|
|
27
|
+
// Skip if CORS headers already present or if it's a WebSocket upgrade
|
|
28
|
+
if (response.headers.get('access-control-allow-origin') || response.status === 101) {
|
|
29
|
+
return response
|
|
30
|
+
}
|
|
31
|
+
// Create new headers with CORS header added
|
|
32
|
+
const headers = new Headers(response.headers)
|
|
33
|
+
headers.set('access-control-allow-origin', '*')
|
|
34
|
+
// Return new Response with same body (not cloned) and updated headers
|
|
35
|
+
return new Response(response.body, {
|
|
36
|
+
status: response.status,
|
|
37
|
+
statusText: response.statusText,
|
|
38
|
+
headers,
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
15
42
|
export function RootRouter({ base = '/' }, ctx: Context): RouterExternal {
|
|
16
43
|
const router = Router<IRequestStrict, [FetchContext]>({
|
|
17
44
|
base,
|
|
45
|
+
// Handle CORS preflight OPTIONS requests
|
|
46
|
+
before: [preflight],
|
|
18
47
|
// The `error` handler will send a response with the status code from any
|
|
19
48
|
// thrown StatusError, or a 500 for any other errors.
|
|
20
49
|
catch: (err) => error(err),
|
|
21
50
|
// Sends a 404 response for any requests that don't match a route, and for
|
|
22
51
|
// any request handlers that return JSON will send a JSON response.
|
|
23
|
-
|
|
52
|
+
// corsify adds CORS headers to all responses.
|
|
53
|
+
finally: [(response) => response ?? error(404), json, corsify],
|
|
24
54
|
})
|
|
25
55
|
|
|
26
56
|
const mapsRouter = MapsRouter({ base: MAPS_BASE }, ctx)
|