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