@comapeo/map-server 1.0.0-pre.3 → 1.0.0-pre.5
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/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/lib/download-request.js +1 -1
- package/dist/lib/map-share.js +2 -2
- package/dist/routes/root.d.ts.map +1 -1
- package/dist/routes/root.js +30 -2
- package/package.json +1 -1
- package/src/index.ts +7 -4
- package/src/lib/download-request.ts +1 -1
- package/src/lib/map-share.ts +2 -2
- package/src/routes/root.ts +32 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { errors } from './lib/errors.js';
|
|
2
|
+
export { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from './lib/constants.js';
|
|
2
3
|
export type { MapInfo, MapShareState, MapShareStateUpdate, DownloadStateUpdate, } from './types.js';
|
|
3
4
|
export type { DownloadState } from './lib/download-request.js';
|
|
4
5
|
export type { MapShareCreateParams, MapShareDeclineParams, } from './routes/map-shares.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAElE,YAAY,EACX,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,mBAAmB,GACnB,MAAM,YAAY,CAAA;AACnB,YAAY,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAC9D,YAAY,EACX,oBAAoB,EACpB,qBAAqB,GACrB,MAAM,wBAAwB,CAAA;AAC/B,YAAY,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAA;AAEjE,MAAM,MAAM,aAAa,GAAG;IAC3B,qBAAqB,EAAE,MAAM,GAAG,GAAG,CAAA;IACnC,aAAa,EAAE,MAAM,GAAG,GAAG,CAAA;IAC3B,eAAe,EAAE,MAAM,GAAG,GAAG,CAAA;IAC7B,OAAO,CAAC,EAAE;QACT,SAAS,EAAE,UAAU,CAAA;QACrB,SAAS,EAAE,UAAU,CAAA;KACrB,CAAA;CACD,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA;AAOD,wBAAgB,YAAY,CAAC,OAAO,EAAE,aAAa;kBA+C9B,aAAa;;;;;EA+BjC"}
|
package/dist/index.js
CHANGED
|
@@ -8,12 +8,13 @@ import { Context } from './context.js';
|
|
|
8
8
|
import { fetchAPI } from './lib/fetch-api.js';
|
|
9
9
|
import { RootRouter } from './routes/root.js';
|
|
10
10
|
export { errors } from './lib/errors.js';
|
|
11
|
+
export { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from './lib/constants.js';
|
|
11
12
|
export function createServer(options) {
|
|
12
13
|
validateOptions(options);
|
|
13
14
|
if (!options.keyPair) {
|
|
14
15
|
options.keyPair = Agent.keyPair();
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
+
let deferredListen = pDefer();
|
|
17
18
|
const context = new Context({
|
|
18
19
|
...options,
|
|
19
20
|
keyPair: options.keyPair,
|
|
@@ -78,6 +79,8 @@ export function createServer(options) {
|
|
|
78
79
|
once(localHttpServer, 'close'),
|
|
79
80
|
once(secretStreamServer, 'close'),
|
|
80
81
|
]);
|
|
82
|
+
// Reset deferred listen for potential restart with different ports
|
|
83
|
+
deferredListen = pDefer();
|
|
81
84
|
},
|
|
82
85
|
};
|
|
83
86
|
}
|
|
@@ -51,7 +51,7 @@ export class DownloadRequest extends TypedEventTarget {
|
|
|
51
51
|
}
|
|
52
52
|
else if (getErrorCode(error)) {
|
|
53
53
|
// Specific known error from the server
|
|
54
|
-
this.#updateState({ status: 'error', error });
|
|
54
|
+
this.#updateState({ status: 'error', error: jsonError(error) });
|
|
55
55
|
}
|
|
56
56
|
else {
|
|
57
57
|
// Once the download has started, the sender can only close the
|
package/dist/lib/map-share.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TypedEventTarget } from '../lib/event-target.js';
|
|
2
|
-
import { errors } from './errors.js';
|
|
2
|
+
import { errors, jsonError } from './errors.js';
|
|
3
3
|
import { StateUpdateEvent } from './state-update-event.js';
|
|
4
4
|
import { addTrailingSlash, generateId, getErrorCode } from './utils.js';
|
|
5
5
|
/**
|
|
@@ -117,7 +117,7 @@ export class DownloadResponse extends TypedEventTarget {
|
|
|
117
117
|
this.#updateState({ status: 'aborted' });
|
|
118
118
|
}
|
|
119
119
|
else {
|
|
120
|
-
this.#updateState({ status: 'error', error });
|
|
120
|
+
this.#updateState({ status: 'error', error: jsonError(error) });
|
|
121
121
|
}
|
|
122
122
|
});
|
|
123
123
|
this.#response = new Response(this.#stream.readable, {
|
|
@@ -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
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
2
|
import { once } from 'node:events'
|
|
3
3
|
import http from 'node:http'
|
|
4
|
-
import { type AddressInfo } from 'node:net'
|
|
4
|
+
import { type AddressInfo, type Socket } from 'node:net'
|
|
5
5
|
|
|
6
6
|
import { createServerAdapter } from '@whatwg-node/server'
|
|
7
7
|
import pDefer from 'p-defer'
|
|
@@ -16,6 +16,7 @@ import { RootRouter } from './routes/root.js'
|
|
|
16
16
|
import type { FetchContext } from './types.js'
|
|
17
17
|
|
|
18
18
|
export { errors } from './lib/errors.js'
|
|
19
|
+
export { CUSTOM_MAP_ID, DEFAULT_MAP_ID } from './lib/constants.js'
|
|
19
20
|
|
|
20
21
|
export type {
|
|
21
22
|
MapInfo,
|
|
@@ -56,7 +57,7 @@ export function createServer(options: ServerOptions) {
|
|
|
56
57
|
options.keyPair = Agent.keyPair()
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
|
|
60
|
+
let deferredListen = pDefer<ListenResult>()
|
|
60
61
|
const context = new Context({
|
|
61
62
|
...options,
|
|
62
63
|
keyPair: options.keyPair,
|
|
@@ -86,8 +87,8 @@ export function createServer(options: ServerOptions) {
|
|
|
86
87
|
})
|
|
87
88
|
|
|
88
89
|
// Track connections for proper cleanup
|
|
89
|
-
const connections = new Set<
|
|
90
|
-
const onConnection = (socket:
|
|
90
|
+
const connections = new Set<Socket>()
|
|
91
|
+
const onConnection = (socket: Socket) => {
|
|
91
92
|
connections.add(socket)
|
|
92
93
|
socket.once('close', () => {
|
|
93
94
|
connections.delete(socket)
|
|
@@ -124,6 +125,8 @@ export function createServer(options: ServerOptions) {
|
|
|
124
125
|
once(localHttpServer, 'close'),
|
|
125
126
|
once(secretStreamServer, 'close'),
|
|
126
127
|
])
|
|
128
|
+
// Reset deferred listen for potential restart with different ports
|
|
129
|
+
deferredListen = pDefer<ListenResult>()
|
|
127
130
|
},
|
|
128
131
|
}
|
|
129
132
|
}
|
|
@@ -67,7 +67,7 @@ export class DownloadRequest extends TypedEventTarget<
|
|
|
67
67
|
this.#updateState({ status: 'canceled' })
|
|
68
68
|
} else if (getErrorCode(error)) {
|
|
69
69
|
// Specific known error from the server
|
|
70
|
-
this.#updateState({ status: 'error', error })
|
|
70
|
+
this.#updateState({ status: 'error', error: jsonError(error) })
|
|
71
71
|
} else {
|
|
72
72
|
// Once the download has started, the sender can only close the
|
|
73
73
|
// connection to cancel the download, which we only see as an
|
package/src/lib/map-share.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
type DownloadStateUpdate,
|
|
6
6
|
type MapInfo,
|
|
7
7
|
} from '../types.js'
|
|
8
|
-
import { errors } from './errors.js'
|
|
8
|
+
import { errors, jsonError } from './errors.js'
|
|
9
9
|
import { StateUpdateEvent } from './state-update-event.js'
|
|
10
10
|
import { addTrailingSlash, generateId, getErrorCode } from './utils.js'
|
|
11
11
|
|
|
@@ -155,7 +155,7 @@ export class DownloadResponse extends TypedEventTarget<
|
|
|
155
155
|
} else if (getErrorCode(error) === 'ECONNRESET') {
|
|
156
156
|
this.#updateState({ status: 'aborted' })
|
|
157
157
|
} else {
|
|
158
|
-
this.#updateState({ status: 'error', error })
|
|
158
|
+
this.#updateState({ status: 'error', error: jsonError(error) })
|
|
159
159
|
}
|
|
160
160
|
})
|
|
161
161
|
|
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)
|