@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,30 +1,27 @@
1
1
  const Meta = class Meta {
2
- constructor({
3
- value = '',
4
- name = '',
5
- } = {}) {
6
- this._value = value;
7
- this._name = name;
8
- }
2
+ constructor({ value = "", name = "" } = {}) {
3
+ this._value = value;
4
+ this._name = name;
5
+ }
9
6
 
10
- get value() {
11
- return this._value;
12
- }
7
+ get value() {
8
+ return this._value;
9
+ }
13
10
 
14
- get name() {
15
- return this._name;
16
- }
11
+ get name() {
12
+ return this._name;
13
+ }
17
14
 
18
- toJSON() {
19
- return {
20
- value: this.value,
21
- name: this.name,
22
- };
23
- }
15
+ toJSON() {
16
+ return {
17
+ value: this.value,
18
+ name: this.name,
19
+ };
20
+ }
24
21
 
25
- get [Symbol.toStringTag]() {
26
- return 'Meta';
27
- }
28
- }
22
+ get [Symbol.toStringTag]() {
23
+ return "Meta";
24
+ }
25
+ };
29
26
 
30
27
  export default Meta;
@@ -1,86 +1,89 @@
1
- import crypto from 'node:crypto';
2
- import Asset from './asset.js';
3
- import Meta from './meta.js';
1
+ import crypto from "node:crypto";
2
+ import Asset from "./asset.js";
3
+ import Meta from "./meta.js";
4
4
 
5
5
  const Package = class Package {
6
- constructor({ version = '', type = '', name = '', org = '', author = {} } = {}) {
7
- this._version = version;
8
- this._created = Math.floor(new Date() / 1000);
9
- this._author = author;
10
- this._type = type;
11
- this._name = name;
12
- this._org = org;
13
- this._files = [];
14
- this._meta = [];
15
- }
6
+ constructor({
7
+ version = "",
8
+ type = "",
9
+ name = "",
10
+ org = "",
11
+ author = {},
12
+ } = {}) {
13
+ this._version = version;
14
+ // @ts-expect-error
15
+ this._created = Math.floor(new Date() / 1000);
16
+ this._author = author;
17
+ this._type = type;
18
+ this._name = name;
19
+ this._org = org;
20
+ this._files = [];
21
+ this._meta = [];
22
+ }
16
23
 
17
- get integrity() {
18
- const hasher = crypto.createHash('sha512');
19
- const hashes = this._files.map(file => file.integrity);
24
+ get integrity() {
25
+ const hasher = crypto.createHash("sha512");
26
+ const hashes = this._files.map((file) => file.integrity);
20
27
 
21
- for (const hash of hashes.sort()) {
22
- hasher.update(hash);
23
- }
28
+ for (const hash of hashes.sort()) {
29
+ hasher.update(hash);
30
+ }
24
31
 
25
- return `sha512-${hasher.digest('base64')}`;
26
- }
32
+ return `sha512-${hasher.digest("base64")}`;
33
+ }
27
34
 
28
- get version() {
29
- return this._version;
30
- }
35
+ get version() {
36
+ return this._version;
37
+ }
31
38
 
32
- get created() {
33
- return this._created;
34
- }
39
+ get created() {
40
+ return this._created;
41
+ }
35
42
 
36
- get author() {
37
- return this._author;
38
- }
43
+ get author() {
44
+ return this._author;
45
+ }
39
46
 
40
- get type() {
41
- return this._type;
42
- }
47
+ get type() {
48
+ return this._type;
49
+ }
43
50
 
44
- get name() {
45
- return this._name;
46
- }
51
+ get name() {
52
+ return this._name;
53
+ }
47
54
 
48
- get org() {
49
- return this._org;
50
- }
55
+ get org() {
56
+ return this._org;
57
+ }
51
58
 
52
- setAsset(asset) {
53
- if (!(asset instanceof Asset))
54
- throw new TypeError(
55
- 'Argument "asset" must be an instance of Asset',
56
- );
57
- this._files.push(asset);
58
- }
59
+ setAsset(asset) {
60
+ if (!(asset instanceof Asset))
61
+ throw new TypeError('Argument "asset" must be an instance of Asset');
62
+ this._files.push(asset);
63
+ }
59
64
 
60
- setMeta(meta) {
61
- if (!(meta instanceof Meta))
62
- throw new TypeError(
63
- 'Argument "meta" must be an instance of Meta',
64
- );
65
- this._meta.push(meta);
66
- }
65
+ setMeta(meta) {
66
+ if (!(meta instanceof Meta))
67
+ throw new TypeError('Argument "meta" must be an instance of Meta');
68
+ this._meta.push(meta);
69
+ }
67
70
 
68
- toJSON() {
69
- return {
70
- integrity: this.integrity,
71
- version: this.version,
72
- created: this.created,
73
- author: this.author,
74
- type: this.type,
75
- name: this.name,
76
- org: this.org,
77
- files: this._files,
78
- meta: this._meta,
79
- }
80
- }
71
+ toJSON() {
72
+ return {
73
+ integrity: this.integrity,
74
+ version: this.version,
75
+ created: this.created,
76
+ author: this.author,
77
+ type: this.type,
78
+ name: this.name,
79
+ org: this.org,
80
+ files: this._files,
81
+ meta: this._meta,
82
+ };
83
+ }
81
84
 
82
- get [Symbol.toStringTag]() {
83
- return 'Package';
84
- }
85
- }
85
+ get [Symbol.toStringTag]() {
86
+ return "Package";
87
+ }
88
+ };
86
89
  export default Package;
@@ -1,63 +1,65 @@
1
- import semver from 'semver';
1
+ import semver from "semver";
2
2
 
3
3
  const Versions = class Versions {
4
- constructor({ versions = [], type = '', name = '', org = '' } = {}) {
5
- this._versions = new Map(versions);
6
- this._type = type;
7
- this._name = name;
8
- this._org = org;
9
- }
10
-
11
- get versions() {
12
- return Array.from(this._versions.entries()).sort((a, b) => a[0] > b[0] ? -1 : 1);
13
- }
14
-
15
- get type() {
16
- return this._type;
17
- }
18
-
19
- get name() {
20
- return this._name;
21
- }
22
-
23
- get org() {
24
- return this._org;
25
- }
26
-
27
- setVersion(version, integrity) {
28
- const major = semver.major(version);
29
- this._versions.set(major, {
30
- version,
31
- integrity,
32
- });
33
- }
34
-
35
- getVersion(major) {
36
- return this._versions.get(major);
37
- }
38
-
39
- check(version) {
40
- const major = semver.major(version);
41
- const previous = this.getVersion(major);
42
- if (previous) {
43
- if (semver.gte(previous.version, version)) {
44
- return false;
45
- }
46
- }
47
- return true;
48
- }
49
-
50
- toJSON() {
51
- return {
52
- versions: this.versions,
53
- type: this.type,
54
- name: this.name,
55
- org: this.org,
56
- };
57
- }
58
-
59
- get [Symbol.toStringTag]() {
60
- return 'Versions';
61
- }
62
- }
4
+ constructor({ versions = [], type = "", name = "", org = "" } = {}) {
5
+ this._versions = new Map(versions);
6
+ this._type = type;
7
+ this._name = name;
8
+ this._org = org;
9
+ }
10
+
11
+ get versions() {
12
+ return Array.from(this._versions.entries()).sort((a, b) =>
13
+ a[0] > b[0] ? -1 : 1,
14
+ );
15
+ }
16
+
17
+ get type() {
18
+ return this._type;
19
+ }
20
+
21
+ get name() {
22
+ return this._name;
23
+ }
24
+
25
+ get org() {
26
+ return this._org;
27
+ }
28
+
29
+ setVersion(version, integrity) {
30
+ const major = semver.major(version);
31
+ this._versions.set(major, {
32
+ version,
33
+ integrity,
34
+ });
35
+ }
36
+
37
+ getVersion(major) {
38
+ return this._versions.get(major);
39
+ }
40
+
41
+ check(version) {
42
+ const major = semver.major(version);
43
+ const previous = this.getVersion(major);
44
+ if (previous) {
45
+ if (semver.gte(previous.version, version)) {
46
+ return false;
47
+ }
48
+ }
49
+ return true;
50
+ }
51
+
52
+ toJSON() {
53
+ return {
54
+ versions: this.versions,
55
+ type: this.type,
56
+ name: this.name,
57
+ org: this.org,
58
+ };
59
+ }
60
+
61
+ get [Symbol.toStringTag]() {
62
+ return "Versions";
63
+ }
64
+ };
63
65
  export default Versions;
@@ -1,125 +1,130 @@
1
- import { validators } from '@eik/common';
2
- import originalUrl from 'original-url';
3
- import HttpError from 'http-errors';
4
- import Metrics from '@metrics/client';
5
- import abslog from 'abslog';
1
+ import { validators } from "@eik/common";
2
+ import originalUrl from "original-url";
3
+ import HttpError from "http-errors";
4
+ import Metrics from "@metrics/client";
5
+ import abslog from "abslog";
6
6
 
7
- import { createFilePathToAlias } 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 { createFilePathToAlias } 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} AliasDeleteOptions
14
+ * @property {string} [cacheControl]
15
+ * @property {Array<[string, string]>} [organizations] List of key-value pairs [hostname, organization]
16
+ * @property {import("@eik/sink").default} [sink]
17
+ * @property {import("abslog").AbstractLoggerOptions} [logger]
18
+ */
11
19
 
12
20
  const AliasDel = class AliasDel {
13
- constructor({
14
- organizations,
15
- cacheControl,
16
- logger,
17
- sink,
18
- } = {}) {
19
- this._organizations = organizations || config.organizations;
20
- this._cacheControl = cacheControl;
21
- this._sink = sink;
22
- this._log = abslog(logger);
23
- this._metrics = new Metrics();
24
- this._histogram = this._metrics.histogram({
25
- name: 'eik_core_alias_del_handler',
26
- description:
27
- 'Histogram measuring time taken in @eik/core AliasDel handler method',
28
- labels: {
29
- success: true,
30
- type: 'unknown',
31
- },
32
- buckets: [
33
- 0.005,
34
- 0.01,
35
- 0.06,
36
- 0.1,
37
- 0.6,
38
- 1.0,
39
- 2.0,
40
- 4.0,
41
- ],
42
- });
43
- this._orgRegistry = new Map(this._organizations);
44
- }
45
-
46
- get metrics() {
47
- return this._metrics;
48
- }
49
-
50
- async _exist(path = '') {
51
- try {
52
- await this._sink.exist(path);
53
- return true;
54
- } catch (error) {
55
- return false;
56
- }
57
- }
58
-
59
- async handler(req, user, type, name, alias) {
60
- const end = this._histogram.timer();
61
-
62
- const pAlias = decodeUriComponent(alias);
63
- const pName = decodeUriComponent(name);
64
-
65
- try {
66
- validators.alias(pAlias);
67
- validators.name(pName);
68
- validators.type(type);
69
- } catch (error) {
70
- this._log.info(`alias:del - Validation failed - ${error.message}`);
71
- const e = new HttpError.NotFound();
72
- end({ labels: { success: false, status: e.status } });
73
- throw e;
74
- }
75
-
76
- const url = originalUrl(req);
77
- const org = this._orgRegistry.get(url.hostname);
78
-
79
- if (!org) {
80
- this._log.info(`alias:del - Hostname does not match a configured organization - ${url.hostname}`);
81
- const e = new HttpError.BadRequest();
82
- end({ labels: { success: false, status: e.status, type } });
83
- throw e;
84
- }
85
-
86
- const path = createFilePathToAlias({ org, type, name: pName, alias: pAlias });
87
- const exist = await this._exist(path);
88
- if (!exist) {
89
- this._log.info(
90
- `alias:del - Alias does not exists - Org: ${org} - Type: ${type} - Name: ${pName} - Alias: ${pAlias}`,
91
- );
92
- const e = new HttpError.NotFound();
93
- end({ labels: { success: false, status: e.status, type } });
94
- throw e;
95
- }
96
-
97
- try {
98
- this._log.info(
99
- `alias:del - Start deleting alias from sink - Pathname: ${path}`,
100
- );
101
- await this._sink.delete(path);
102
- } catch (error) {
103
- this._log.error(
104
- `alias:del - Failed deleting alias from sink - Pathname: ${path}`,
105
- );
106
- this._log.trace(error);
107
- const e = new HttpError.BadGateway();
108
- end({ labels: { success: false, status: e.status, type } });
109
- return e;
110
- }
111
-
112
- this._log.info(
113
- `alias:del - Successfully deleted alias from sink - Pathname: ${path}`,
114
- );
115
-
116
- const outgoing = new HttpOutgoing();
117
- outgoing.cacheControl = this._cacheControl;
118
- outgoing.statusCode = 204;
119
-
120
- end({ labels: { status: outgoing.statusCode, type } });
121
-
122
- return outgoing;
123
- }
21
+ /**
22
+ * @param {AliasDeleteOptions} options
23
+ */
24
+ constructor({ organizations, cacheControl, logger, sink } = {}) {
25
+ this._organizations = organizations || config.organizations;
26
+ this._cacheControl = cacheControl;
27
+ this._sink = sink;
28
+ this._log = abslog(logger);
29
+ this._metrics = new Metrics();
30
+ this._histogram = this._metrics.histogram({
31
+ name: "eik_core_alias_del_handler",
32
+ description:
33
+ "Histogram measuring time taken in @eik/core AliasDel handler method",
34
+ labels: {
35
+ success: true,
36
+ type: "unknown",
37
+ },
38
+ buckets: [0.005, 0.01, 0.06, 0.1, 0.6, 1.0, 2.0, 4.0],
39
+ });
40
+ this._orgRegistry = new Map(this._organizations);
41
+ }
42
+
43
+ get metrics() {
44
+ return this._metrics;
45
+ }
46
+
47
+ async _exist(path = "") {
48
+ try {
49
+ await this._sink.exist(path);
50
+ return true;
51
+ // eslint-disable-next-line no-unused-vars
52
+ } catch (error) {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ async handler(req, user, type, name, alias) {
58
+ const end = this._histogram.timer();
59
+
60
+ const pAlias = decodeUriComponent(alias);
61
+ const pName = decodeUriComponent(name);
62
+
63
+ try {
64
+ validators.alias(pAlias);
65
+ validators.name(pName);
66
+ validators.type(type);
67
+ } catch (error) {
68
+ this._log.info(`alias:del - Validation failed - ${error.message}`);
69
+ const e = new HttpError.NotFound();
70
+ end({ labels: { success: false, status: e.status } });
71
+ throw e;
72
+ }
73
+
74
+ const url = originalUrl(req);
75
+ const org = this._orgRegistry.get(url.hostname);
76
+
77
+ if (!org) {
78
+ this._log.info(
79
+ `alias:del - Hostname does not match a configured organization - ${url.hostname}`,
80
+ );
81
+ const e = new HttpError.BadRequest();
82
+ end({ labels: { success: false, status: e.status, type } });
83
+ throw e;
84
+ }
85
+
86
+ const path = createFilePathToAlias({
87
+ org,
88
+ type,
89
+ name: pName,
90
+ alias: pAlias,
91
+ });
92
+ const exist = await this._exist(path);
93
+ if (!exist) {
94
+ this._log.info(
95
+ `alias:del - Alias does not exists - Org: ${org} - Type: ${type} - Name: ${pName} - Alias: ${pAlias}`,
96
+ );
97
+ const e = new HttpError.NotFound();
98
+ end({ labels: { success: false, status: e.status, type } });
99
+ throw e;
100
+ }
101
+
102
+ try {
103
+ this._log.info(
104
+ `alias:del - Start deleting alias from sink - Pathname: ${path}`,
105
+ );
106
+ await this._sink.delete(path);
107
+ } catch (error) {
108
+ this._log.error(
109
+ `alias:del - Failed deleting alias from sink - Pathname: ${path}`,
110
+ );
111
+ this._log.trace(error);
112
+ const e = new HttpError.BadGateway();
113
+ end({ labels: { success: false, status: e.status, type } });
114
+ return e;
115
+ }
116
+
117
+ this._log.info(
118
+ `alias:del - Successfully deleted alias from sink - Pathname: ${path}`,
119
+ );
120
+
121
+ const outgoing = new HttpOutgoing();
122
+ outgoing.cacheControl = this._cacheControl;
123
+ outgoing.statusCode = 204;
124
+
125
+ end({ labels: { status: outgoing.statusCode, type } });
126
+
127
+ return outgoing;
128
+ }
124
129
  };
125
130
  export default AliasDel;