-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
package.js
104 lines (91 loc) 路 2.33 KB
/
package.js
1
2
3
4
5
6
7
8
9
10
11
12
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
'use strict';
const orm = require('ormnomnom');
const joi = require('@hapi/joi');
module.exports = class Package {
#namespace = null;
constructor({
id,
name,
namespace_id,
namespace,
require_tfa,
version_integrities,
yanked,
created,
modified,
active,
tags
}) {
this.id = id;
this.name = name;
this.namespace_id = namespace_id;
this.#namespace = namespace ? Promise.resolve(namespace) : null;
this.require_tfa = require_tfa;
this.yanked = yanked;
this.version_integrities = version_integrities;
this.created = created;
this.modified = modified;
this.active = active;
this.tags = tags;
}
async serialize() {
const namespace = await this.namespace;
const host = await namespace.host;
return {
name: `${namespace.name}@${host.name}/${encodeURIComponent(this.name)}`,
yanked: this.yanked,
created: this.created,
modified: this.modified,
require_tfa: Boolean(this.require_tfa),
versions: this.version_integrities,
tags: this.tags
};
}
// TODO: precompute this on version change events.
async versions() {
const versions = await PackageVersion.objects
.filter({
active: true,
parent: this
})
.then(x => x);
const acc = {};
for (const version of versions) {
if (version.yanked) {
continue;
}
const [integrity, _] = await version.toSSRI();
acc[version.version] = String(integrity);
}
return acc;
}
get namespace() {
if (this.#namespace === null) {
this.#namespace = Namespace.objects.get({ id: this.namespace_id });
this.#namespace.catch(() => {});
}
return this.#namespace;
}
set namespace(u) {
this.#namespace = Promise.resolve(u);
this.namespace_id = this.#namespace.id;
}
};
const PackageVersion = require('./package-version');
const Namespace = require('./namespace');
module.exports.objects = orm(module.exports, {
id: joi
.number()
.integer()
.greater(-1)
.required(),
name: joi.string().min(1),
namespace: orm.fk(Namespace),
require_tfa: joi.boolean(),
version_integrities: joi.object().unknown(),
yanked: joi.boolean().default(false),
created: joi.date(),
modified: joi.date(),
active: joi.boolean().default(true),
tags: joi.object().unknown()
});