@comapeo/map-server 1.0.0-pre.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.
Files changed (179) hide show
  1. package/README.md +610 -0
  2. package/dist/context.d.ts +46 -0
  3. package/dist/context.d.ts.map +1 -0
  4. package/dist/context.js +181 -0
  5. package/dist/index.d.ts +25 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +112 -0
  8. package/dist/lib/constants.d.ts +7 -0
  9. package/dist/lib/constants.d.ts.map +1 -0
  10. package/dist/lib/constants.js +6 -0
  11. package/dist/lib/download-request.d.ts +17 -0
  12. package/dist/lib/download-request.d.ts.map +1 -0
  13. package/dist/lib/download-request.js +113 -0
  14. package/dist/lib/errors.d.ts +88 -0
  15. package/dist/lib/errors.d.ts.map +1 -0
  16. package/dist/lib/errors.js +158 -0
  17. package/dist/lib/event-stream-response.d.ts +17 -0
  18. package/dist/lib/event-stream-response.d.ts.map +1 -0
  19. package/dist/lib/event-stream-response.js +39 -0
  20. package/dist/lib/event-target.d.ts +9 -0
  21. package/dist/lib/event-target.d.ts.map +1 -0
  22. package/dist/lib/event-target.js +4 -0
  23. package/dist/lib/fetch-api.d.ts +3 -0
  24. package/dist/lib/fetch-api.d.ts.map +1 -0
  25. package/dist/lib/fetch-api.js +16 -0
  26. package/dist/lib/map-share.d.ts +52 -0
  27. package/dist/lib/map-share.d.ts.map +1 -0
  28. package/dist/lib/map-share.js +142 -0
  29. package/dist/lib/secret-stream-fetch.d.ts +7 -0
  30. package/dist/lib/secret-stream-fetch.d.ts.map +1 -0
  31. package/dist/lib/secret-stream-fetch.js +34 -0
  32. package/dist/lib/self-evicting-map.d.ts +16 -0
  33. package/dist/lib/self-evicting-map.d.ts.map +1 -0
  34. package/dist/lib/self-evicting-map.js +29 -0
  35. package/dist/lib/state-update-event.d.ts +8 -0
  36. package/dist/lib/state-update-event.d.ts.map +1 -0
  37. package/dist/lib/state-update-event.js +10 -0
  38. package/dist/lib/utils.d.ts +32 -0
  39. package/dist/lib/utils.d.ts.map +1 -0
  40. package/dist/lib/utils.js +96 -0
  41. package/dist/middlewares/localhost-only.d.ts +11 -0
  42. package/dist/middlewares/localhost-only.d.ts.map +1 -0
  43. package/dist/middlewares/localhost-only.js +10 -0
  44. package/dist/middlewares/parse-request.d.ts +11 -0
  45. package/dist/middlewares/parse-request.d.ts.map +1 -0
  46. package/dist/middlewares/parse-request.js +25 -0
  47. package/dist/routes/downloads.d.ts +15 -0
  48. package/dist/routes/downloads.d.ts.map +1 -0
  49. package/dist/routes/downloads.js +60 -0
  50. package/dist/routes/map-shares.d.ts +19 -0
  51. package/dist/routes/map-shares.d.ts.map +1 -0
  52. package/dist/routes/map-shares.js +192 -0
  53. package/dist/routes/maps.d.ts +6 -0
  54. package/dist/routes/maps.d.ts.map +1 -0
  55. package/dist/routes/maps.js +118 -0
  56. package/dist/routes/root.d.ts +6 -0
  57. package/dist/routes/root.d.ts.map +1 -0
  58. package/dist/routes/root.js +29 -0
  59. package/dist/types.d.ts +110 -0
  60. package/dist/types.d.ts.map +1 -0
  61. package/dist/types.js +96 -0
  62. package/node_modules/@envelop/instrumentation/LICENSE +21 -0
  63. package/node_modules/@envelop/instrumentation/README.md +30 -0
  64. package/node_modules/@envelop/instrumentation/cjs/index.js +5 -0
  65. package/node_modules/@envelop/instrumentation/cjs/instrumentation.js +89 -0
  66. package/node_modules/@envelop/instrumentation/cjs/package.json +1 -0
  67. package/node_modules/@envelop/instrumentation/esm/index.js +2 -0
  68. package/node_modules/@envelop/instrumentation/esm/instrumentation.js +82 -0
  69. package/node_modules/@envelop/instrumentation/package.json +57 -0
  70. package/node_modules/@envelop/instrumentation/typings/index.d.cts +1 -0
  71. package/node_modules/@envelop/instrumentation/typings/index.d.ts +1 -0
  72. package/node_modules/@envelop/instrumentation/typings/instrumentation.d.cts +44 -0
  73. package/node_modules/@envelop/instrumentation/typings/instrumentation.d.ts +44 -0
  74. package/node_modules/@whatwg-node/disposablestack/cjs/AsyncDisposableStack.js +73 -0
  75. package/node_modules/@whatwg-node/disposablestack/cjs/DisposableStack.js +62 -0
  76. package/node_modules/@whatwg-node/disposablestack/cjs/SupressedError.js +16 -0
  77. package/node_modules/@whatwg-node/disposablestack/cjs/index.js +11 -0
  78. package/node_modules/@whatwg-node/disposablestack/cjs/package.json +1 -0
  79. package/node_modules/@whatwg-node/disposablestack/cjs/symbols.js +20 -0
  80. package/node_modules/@whatwg-node/disposablestack/cjs/utils.js +11 -0
  81. package/node_modules/@whatwg-node/disposablestack/esm/AsyncDisposableStack.js +69 -0
  82. package/node_modules/@whatwg-node/disposablestack/esm/DisposableStack.js +58 -0
  83. package/node_modules/@whatwg-node/disposablestack/esm/SupressedError.js +12 -0
  84. package/node_modules/@whatwg-node/disposablestack/esm/index.js +7 -0
  85. package/node_modules/@whatwg-node/disposablestack/esm/symbols.js +16 -0
  86. package/node_modules/@whatwg-node/disposablestack/esm/utils.js +7 -0
  87. package/node_modules/@whatwg-node/disposablestack/package.json +44 -0
  88. package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.cts +15 -0
  89. package/node_modules/@whatwg-node/disposablestack/typings/AsyncDisposableStack.d.ts +15 -0
  90. package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.cts +14 -0
  91. package/node_modules/@whatwg-node/disposablestack/typings/DisposableStack.d.ts +14 -0
  92. package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.cts +5 -0
  93. package/node_modules/@whatwg-node/disposablestack/typings/SupressedError.d.ts +5 -0
  94. package/node_modules/@whatwg-node/disposablestack/typings/index.d.cts +4 -0
  95. package/node_modules/@whatwg-node/disposablestack/typings/index.d.ts +4 -0
  96. package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.cts +5 -0
  97. package/node_modules/@whatwg-node/disposablestack/typings/symbols.d.ts +5 -0
  98. package/node_modules/@whatwg-node/disposablestack/typings/utils.d.cts +2 -0
  99. package/node_modules/@whatwg-node/disposablestack/typings/utils.d.ts +2 -0
  100. package/node_modules/@whatwg-node/promise-helpers/cjs/index.js +270 -0
  101. package/node_modules/@whatwg-node/promise-helpers/cjs/package.json +1 -0
  102. package/node_modules/@whatwg-node/promise-helpers/esm/index.js +257 -0
  103. package/node_modules/@whatwg-node/promise-helpers/package.json +43 -0
  104. package/node_modules/@whatwg-node/promise-helpers/typings/index.d.cts +31 -0
  105. package/node_modules/@whatwg-node/promise-helpers/typings/index.d.ts +31 -0
  106. package/node_modules/@whatwg-node/server/README.md +590 -0
  107. package/node_modules/@whatwg-node/server/cjs/createServerAdapter.js +368 -0
  108. package/node_modules/@whatwg-node/server/cjs/index.js +17 -0
  109. package/node_modules/@whatwg-node/server/cjs/package.json +1 -0
  110. package/node_modules/@whatwg-node/server/cjs/plugins/types.js +0 -0
  111. package/node_modules/@whatwg-node/server/cjs/plugins/useContentEncoding.js +73 -0
  112. package/node_modules/@whatwg-node/server/cjs/plugins/useCors.js +124 -0
  113. package/node_modules/@whatwg-node/server/cjs/plugins/useErrorHandling.js +52 -0
  114. package/node_modules/@whatwg-node/server/cjs/types.js +0 -0
  115. package/node_modules/@whatwg-node/server/cjs/utils.js +599 -0
  116. package/node_modules/@whatwg-node/server/cjs/uwebsockets.js +241 -0
  117. package/node_modules/@whatwg-node/server/esm/createServerAdapter.js +365 -0
  118. package/node_modules/@whatwg-node/server/esm/index.js +11 -0
  119. package/node_modules/@whatwg-node/server/esm/plugins/types.js +0 -0
  120. package/node_modules/@whatwg-node/server/esm/plugins/useContentEncoding.js +70 -0
  121. package/node_modules/@whatwg-node/server/esm/plugins/useCors.js +120 -0
  122. package/node_modules/@whatwg-node/server/esm/plugins/useErrorHandling.js +46 -0
  123. package/node_modules/@whatwg-node/server/esm/types.js +0 -0
  124. package/node_modules/@whatwg-node/server/esm/utils.js +588 -0
  125. package/node_modules/@whatwg-node/server/esm/uwebsockets.js +234 -0
  126. package/node_modules/@whatwg-node/server/package.json +46 -0
  127. package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.cts +19 -0
  128. package/node_modules/@whatwg-node/server/typings/createServerAdapter.d.ts +19 -0
  129. package/node_modules/@whatwg-node/server/typings/index.d.cts +11 -0
  130. package/node_modules/@whatwg-node/server/typings/index.d.ts +11 -0
  131. package/node_modules/@whatwg-node/server/typings/plugins/types.d.cts +76 -0
  132. package/node_modules/@whatwg-node/server/typings/plugins/types.d.ts +76 -0
  133. package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.cts +2 -0
  134. package/node_modules/@whatwg-node/server/typings/plugins/useContentEncoding.d.ts +2 -0
  135. package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.cts +14 -0
  136. package/node_modules/@whatwg-node/server/typings/plugins/useCors.d.ts +14 -0
  137. package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.cts +13 -0
  138. package/node_modules/@whatwg-node/server/typings/plugins/useErrorHandling.d.ts +13 -0
  139. package/node_modules/@whatwg-node/server/typings/types.d.cts +100 -0
  140. package/node_modules/@whatwg-node/server/typings/types.d.ts +100 -0
  141. package/node_modules/@whatwg-node/server/typings/utils.d.cts +42 -0
  142. package/node_modules/@whatwg-node/server/typings/utils.d.ts +42 -0
  143. package/node_modules/@whatwg-node/server/typings/uwebsockets.d.cts +32 -0
  144. package/node_modules/@whatwg-node/server/typings/uwebsockets.d.ts +32 -0
  145. package/node_modules/tslib/CopyrightNotice.txt +15 -0
  146. package/node_modules/tslib/LICENSE.txt +12 -0
  147. package/node_modules/tslib/README.md +164 -0
  148. package/node_modules/tslib/SECURITY.md +41 -0
  149. package/node_modules/tslib/modules/index.d.ts +38 -0
  150. package/node_modules/tslib/modules/index.js +70 -0
  151. package/node_modules/tslib/modules/package.json +3 -0
  152. package/node_modules/tslib/package.json +47 -0
  153. package/node_modules/tslib/tslib.d.ts +460 -0
  154. package/node_modules/tslib/tslib.es6.html +1 -0
  155. package/node_modules/tslib/tslib.es6.js +402 -0
  156. package/node_modules/tslib/tslib.es6.mjs +401 -0
  157. package/node_modules/tslib/tslib.html +1 -0
  158. package/node_modules/tslib/tslib.js +484 -0
  159. package/package.json +87 -0
  160. package/src/context.ts +203 -0
  161. package/src/index.ts +193 -0
  162. package/src/lib/constants.ts +6 -0
  163. package/src/lib/download-request.ts +142 -0
  164. package/src/lib/errors.ts +187 -0
  165. package/src/lib/event-stream-response.ts +57 -0
  166. package/src/lib/event-target.ts +11 -0
  167. package/src/lib/fetch-api.ts +18 -0
  168. package/src/lib/map-share.ts +185 -0
  169. package/src/lib/secret-stream-fetch.ts +42 -0
  170. package/src/lib/self-evicting-map.ts +35 -0
  171. package/src/lib/state-update-event.ts +14 -0
  172. package/src/lib/utils.ts +110 -0
  173. package/src/middlewares/localhost-only.ts +16 -0
  174. package/src/middlewares/parse-request.ts +34 -0
  175. package/src/routes/downloads.ts +92 -0
  176. package/src/routes/map-shares.ts +246 -0
  177. package/src/routes/maps.ts +146 -0
  178. package/src/routes/root.ts +37 -0
  179. package/src/types.ts +152 -0
@@ -0,0 +1,120 @@
1
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
2
+ export function getCORSHeadersByRequestAndOptions(request, corsOptions) {
3
+ const currentOrigin = request.headers.get('origin');
4
+ if (corsOptions === false || currentOrigin == null) {
5
+ return null;
6
+ }
7
+ const headers = {};
8
+ // If defined origins have '*' or undefined by any means, we should allow all origins
9
+ if (corsOptions.origin == null ||
10
+ corsOptions.origin.length === 0 ||
11
+ corsOptions.origin.includes('*')) {
12
+ headers['Access-Control-Allow-Origin'] = currentOrigin;
13
+ // Vary by origin because there are multiple origins
14
+ headers['Vary'] = 'Origin';
15
+ }
16
+ else if (typeof corsOptions.origin === 'string') {
17
+ // If there is one specific origin is specified, use it directly
18
+ headers['Access-Control-Allow-Origin'] = corsOptions.origin;
19
+ }
20
+ else if (Array.isArray(corsOptions.origin)) {
21
+ // If there is only one origin defined in the array, consider it as a single one
22
+ if (corsOptions.origin.length === 1) {
23
+ headers['Access-Control-Allow-Origin'] = corsOptions.origin[0];
24
+ }
25
+ else if (corsOptions.origin.includes(currentOrigin)) {
26
+ // If origin is available in the headers, use it
27
+ headers['Access-Control-Allow-Origin'] = currentOrigin;
28
+ // Vary by origin because there are multiple origins
29
+ headers['Vary'] = 'Origin';
30
+ }
31
+ else {
32
+ // There is no origin found in the headers, so we should return null
33
+ headers['Access-Control-Allow-Origin'] = 'null';
34
+ }
35
+ }
36
+ if (corsOptions.methods?.length) {
37
+ headers['Access-Control-Allow-Methods'] = corsOptions.methods.join(', ');
38
+ }
39
+ else {
40
+ const requestMethod = request.headers.get('access-control-request-method');
41
+ if (requestMethod) {
42
+ headers['Access-Control-Allow-Methods'] = requestMethod;
43
+ }
44
+ }
45
+ if (corsOptions.allowedHeaders?.length) {
46
+ headers['Access-Control-Allow-Headers'] = corsOptions.allowedHeaders.join(', ');
47
+ }
48
+ else {
49
+ const requestHeaders = request.headers.get('access-control-request-headers');
50
+ if (requestHeaders) {
51
+ headers['Access-Control-Allow-Headers'] = requestHeaders;
52
+ if (headers['Vary']) {
53
+ headers['Vary'] += ', Access-Control-Request-Headers';
54
+ }
55
+ else {
56
+ headers['Vary'] = 'Access-Control-Request-Headers';
57
+ }
58
+ }
59
+ }
60
+ if (corsOptions.credentials != null) {
61
+ if (corsOptions.credentials === true) {
62
+ headers['Access-Control-Allow-Credentials'] = 'true';
63
+ }
64
+ }
65
+ else if (headers['Access-Control-Allow-Origin'] !== '*') {
66
+ headers['Access-Control-Allow-Credentials'] = 'true';
67
+ }
68
+ if (corsOptions.exposedHeaders) {
69
+ headers['Access-Control-Expose-Headers'] = corsOptions.exposedHeaders.join(', ');
70
+ }
71
+ if (corsOptions.maxAge) {
72
+ headers['Access-Control-Max-Age'] = corsOptions.maxAge.toString();
73
+ }
74
+ return headers;
75
+ }
76
+ function getCORSResponseHeaders(request, corsOptionsFactory, serverContext) {
77
+ return handleMaybePromise(() => corsOptionsFactory(request, serverContext), corsOptions => getCORSHeadersByRequestAndOptions(request, corsOptions));
78
+ }
79
+ export function useCORS(options) {
80
+ let corsOptionsFactory = () => ({});
81
+ if (options != null) {
82
+ if (typeof options === 'function') {
83
+ corsOptionsFactory = options;
84
+ }
85
+ else if (typeof options === 'object') {
86
+ const corsOptions = {
87
+ ...options,
88
+ };
89
+ corsOptionsFactory = () => corsOptions;
90
+ }
91
+ else if (options === false) {
92
+ corsOptionsFactory = () => false;
93
+ }
94
+ }
95
+ return {
96
+ onRequest({ request, fetchAPI, endResponse }) {
97
+ if (request.method.toUpperCase() === 'OPTIONS') {
98
+ const response = new fetchAPI.Response(null, {
99
+ status: 204,
100
+ // Safari (and potentially other browsers) need content-length 0,
101
+ // for 204 or they just hang waiting for a body
102
+ // see: https://github.com/expressjs/cors/blob/master/lib/index.js#L176
103
+ headers: {
104
+ 'Content-Length': '0',
105
+ },
106
+ });
107
+ endResponse(response);
108
+ }
109
+ },
110
+ onResponse({ request, serverContext, response }) {
111
+ return handleMaybePromise(() => getCORSResponseHeaders(request, corsOptionsFactory, serverContext), headers => {
112
+ if (headers != null) {
113
+ for (const headerName in headers) {
114
+ response.headers.set(headerName, headers[headerName]);
115
+ }
116
+ }
117
+ });
118
+ },
119
+ };
120
+ }
@@ -0,0 +1,46 @@
1
+ import { Response as DefaultResponseCtor } from '@whatwg-node/fetch';
2
+ import { handleMaybePromise } from '@whatwg-node/promise-helpers';
3
+ export function createDefaultErrorHandler(ResponseCtor = DefaultResponseCtor) {
4
+ return function defaultErrorHandler(e) {
5
+ if (e.details || e.status || e.headers || e.name === 'HTTPError') {
6
+ return new ResponseCtor(typeof e.details === 'object' ? JSON.stringify(e.details) : e.message, {
7
+ status: e.status,
8
+ headers: e.headers || {},
9
+ });
10
+ }
11
+ console.error(e);
12
+ return createDefaultErrorResponse(ResponseCtor);
13
+ };
14
+ }
15
+ function createDefaultErrorResponse(ResponseCtor) {
16
+ if (ResponseCtor.error) {
17
+ return ResponseCtor.error();
18
+ }
19
+ return new ResponseCtor(null, { status: 500 });
20
+ }
21
+ export class HTTPError extends Error {
22
+ status;
23
+ message;
24
+ headers;
25
+ details;
26
+ name = 'HTTPError';
27
+ constructor(status = 500, message, headers = {}, details) {
28
+ super(message);
29
+ this.status = status;
30
+ this.message = message;
31
+ this.headers = headers;
32
+ this.details = details;
33
+ Error.captureStackTrace(this, HTTPError);
34
+ }
35
+ }
36
+ export function useErrorHandling(onError) {
37
+ return {
38
+ onRequest({ requestHandler, setRequestHandler, fetchAPI }) {
39
+ const errorHandler = onError || createDefaultErrorHandler(fetchAPI.Response);
40
+ setRequestHandler(function handlerWithErrorHandling(request, serverContext) {
41
+ return handleMaybePromise(() => requestHandler(request, serverContext), response => response, e => errorHandler(e, request, serverContext) ||
42
+ createDefaultErrorResponse(fetchAPI.Response));
43
+ });
44
+ },
45
+ };
46
+ }
File without changes