@eik/core 1.3.54 → 1.3.56
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/lib/classes/alias.js +48 -48
- package/lib/classes/asset.js +99 -92
- package/lib/classes/author.js +20 -20
- package/lib/classes/http-incoming.js +52 -52
- package/lib/classes/http-outgoing.js +84 -83
- package/lib/classes/meta.js +20 -23
- package/lib/classes/package.js +73 -70
- package/lib/classes/versions.js +62 -60
- package/lib/handlers/alias.delete.js +125 -120
- package/lib/handlers/alias.get.js +92 -87
- package/lib/handlers/alias.post.js +196 -203
- package/lib/handlers/alias.put.js +196 -202
- package/lib/handlers/auth.post.js +95 -93
- package/lib/handlers/map.get.js +110 -111
- package/lib/handlers/map.put.js +256 -231
- package/lib/handlers/pkg.get.js +120 -122
- package/lib/handlers/pkg.log.js +112 -110
- package/lib/handlers/pkg.put.js +223 -213
- package/lib/handlers/versions.get.js +92 -101
- package/lib/main.js +47 -47
- package/lib/multipart/form-field.js +20 -23
- package/lib/multipart/form-file.js +22 -24
- package/lib/multipart/parser.js +231 -217
- package/lib/sinks/mem-entry.js +26 -31
- package/lib/sinks/test.js +289 -273
- package/lib/utils/defaults.js +11 -11
- package/lib/utils/globals.js +5 -5
- package/lib/utils/healthcheck.js +131 -108
- package/lib/utils/path-builders-fs.js +61 -29
- package/lib/utils/path-builders-uri.js +26 -18
- package/lib/utils/utils.js +76 -79
- package/package.json +20 -15
- package/types/classes/alias.d.ts +28 -0
- package/types/classes/asset.d.ts +48 -0
- package/types/classes/author.d.ts +17 -0
- package/types/classes/http-incoming.d.ts +37 -0
- package/types/classes/http-outgoing.d.ts +20 -0
- package/types/classes/meta.d.ts +17 -0
- package/types/classes/package.d.ts +40 -0
- package/types/classes/versions.d.ts +28 -0
- package/types/handlers/alias.delete.d.ts +33 -0
- package/types/handlers/alias.get.d.ts +48 -0
- package/types/handlers/alias.post.d.ts +83 -0
- package/types/handlers/alias.put.d.ts +83 -0
- package/types/handlers/auth.post.d.ts +82 -0
- package/types/handlers/map.get.d.ts +51 -0
- package/types/handlers/map.put.d.ts +78 -0
- package/types/handlers/pkg.get.d.ts +51 -0
- package/types/handlers/pkg.log.d.ts +51 -0
- package/types/handlers/pkg.put.d.ts +107 -0
- package/types/handlers/versions.get.d.ts +48 -0
- package/types/main.d.ts +44 -0
- package/types/multipart/form-field.d.ts +17 -0
- package/types/multipart/form-file.d.ts +17 -0
- package/types/multipart/parser.d.ts +52 -0
- package/types/sinks/mem-entry.d.ts +15 -0
- package/types/sinks/test.d.ts +32 -0
- package/types/utils/defaults.d.ts +9 -0
- package/types/utils/globals.d.ts +8 -0
- package/types/utils/healthcheck.d.ts +24 -0
- package/types/utils/path-builders-fs.d.ts +41 -0
- package/types/utils/path-builders-uri.d.ts +26 -0
- package/types/utils/utils.d.ts +6 -0
package/lib/handlers/map.get.js
CHANGED
|
@@ -1,116 +1,115 @@
|
|
|
1
|
-
import { validators } from
|
|
2
|
-
import originalUrl from
|
|
3
|
-
import HttpError from
|
|
4
|
-
import abslog from
|
|
5
|
-
import Metrics from
|
|
1
|
+
import { validators } from "@eik/common";
|
|
2
|
+
import originalUrl from "original-url";
|
|
3
|
+
import HttpError from "http-errors";
|
|
4
|
+
import abslog from "abslog";
|
|
5
|
+
import Metrics from "@metrics/client";
|
|
6
6
|
|
|
7
|
-
import { createFilePathToImportMap } from
|
|
8
|
-
import HttpOutgoing from
|
|
9
|
-
import config from
|
|
10
|
-
import { decodeUriComponent } from
|
|
7
|
+
import { createFilePathToImportMap } from "../utils/path-builders-fs.js";
|
|
8
|
+
import HttpOutgoing from "../classes/http-outgoing.js";
|
|
9
|
+
import config from "../utils/defaults.js";
|
|
10
|
+
import { decodeUriComponent } from "../utils/utils.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {object} MapGetOptions
|
|
14
|
+
* @property {boolean} [etag]
|
|
15
|
+
* @property {string} [cacheControl]
|
|
16
|
+
* @property {Array<[string, string]>} [organizations] List of key-value pairs [hostname, organization]
|
|
17
|
+
* @property {import("@eik/sink").default} [sink]
|
|
18
|
+
* @property {import("abslog").AbstractLoggerOptions} [logger]
|
|
19
|
+
*/
|
|
11
20
|
|
|
12
21
|
const MapGet = class MapGet {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return outgoing;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
this._log.debug(
|
|
108
|
-
`map:get - Import map not found - Pathname: ${path}`,
|
|
109
|
-
);
|
|
110
|
-
const e = new HttpError.NotFound();
|
|
111
|
-
end({ labels: { success: false, status: e.status, type: 'map' } });
|
|
112
|
-
throw e;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
22
|
+
/**
|
|
23
|
+
* @param {MapGetOptions} options
|
|
24
|
+
*/
|
|
25
|
+
constructor({ organizations, cacheControl, logger, sink, etag } = {}) {
|
|
26
|
+
this._organizations = organizations || config.organizations;
|
|
27
|
+
this._cacheControl = cacheControl || "public, max-age=31536000, immutable";
|
|
28
|
+
this._sink = sink;
|
|
29
|
+
this._etag = etag || config.etag;
|
|
30
|
+
this._log = abslog(logger);
|
|
31
|
+
this._metrics = new Metrics();
|
|
32
|
+
this._histogram = this._metrics.histogram({
|
|
33
|
+
name: "eik_core_map_get_handler",
|
|
34
|
+
description:
|
|
35
|
+
"Histogram measuring time taken in @eik/core MapGet handler method",
|
|
36
|
+
labels: {
|
|
37
|
+
success: true,
|
|
38
|
+
type: "unknown",
|
|
39
|
+
},
|
|
40
|
+
buckets: [0.005, 0.01, 0.06, 0.1, 0.6, 1.0, 2.0, 4.0],
|
|
41
|
+
});
|
|
42
|
+
this._orgRegistry = new Map(this._organizations);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get metrics() {
|
|
46
|
+
return this._metrics;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async handler(req, name, version) {
|
|
50
|
+
const end = this._histogram.timer();
|
|
51
|
+
|
|
52
|
+
const pVersion = decodeUriComponent(version);
|
|
53
|
+
const pName = decodeUriComponent(name);
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
validators.version(pVersion);
|
|
57
|
+
validators.name(pName);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
this._log.debug(`map:get - Validation failed - ${error.message}`);
|
|
60
|
+
const e = new HttpError.NotFound();
|
|
61
|
+
end({ labels: { success: false, status: e.status } });
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const url = originalUrl(req);
|
|
66
|
+
const org = this._orgRegistry.get(url.hostname);
|
|
67
|
+
|
|
68
|
+
if (!org) {
|
|
69
|
+
this._log.info(
|
|
70
|
+
`map:get - Hostname does not match a configured organization - ${url.hostname}`,
|
|
71
|
+
);
|
|
72
|
+
const e = new HttpError.BadRequest();
|
|
73
|
+
end({ labels: { success: false, status: e.status, type: "map" } });
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const path = createFilePathToImportMap({
|
|
78
|
+
org,
|
|
79
|
+
name: pName,
|
|
80
|
+
version: pVersion,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const file = await this._sink.read(path);
|
|
85
|
+
const outgoing = new HttpOutgoing();
|
|
86
|
+
outgoing.cacheControl = this._cacheControl;
|
|
87
|
+
outgoing.mimeType = "application/json";
|
|
88
|
+
|
|
89
|
+
if (this._etag) {
|
|
90
|
+
outgoing.etag = file.etag;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (this._etag && req.headers["if-none-match"] === file.etag) {
|
|
94
|
+
outgoing.statusCode = 304;
|
|
95
|
+
file.stream.destroy();
|
|
96
|
+
} else {
|
|
97
|
+
outgoing.statusCode = 200;
|
|
98
|
+
outgoing.stream = file.stream;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._log.debug(`map:get - Import map found - Pathname: ${path}`);
|
|
102
|
+
|
|
103
|
+
end({ labels: { status: outgoing.statusCode, type: "map" } });
|
|
104
|
+
|
|
105
|
+
return outgoing;
|
|
106
|
+
// eslint-disable-next-line no-unused-vars
|
|
107
|
+
} catch (error) {
|
|
108
|
+
this._log.debug(`map:get - Import map not found - Pathname: ${path}`);
|
|
109
|
+
const e = new HttpError.NotFound();
|
|
110
|
+
end({ labels: { success: false, status: e.status, type: "map" } });
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
115
114
|
};
|
|
116
115
|
export default MapGet;
|