@eik/core 1.3.54 → 1.3.55

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