Repository navigation
Expand file tree
/
Copy pathcountDownload.js
More file actions
98 lines (93 loc) · 3.15 KB
/
Copy pathcountDownload.js
File metadata and controls
98 lines (93 loc) · 3.15 KB
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
// Refer https://json-schema.org/understanding-json-schema/index.html
import {EXTENSIONS_DETAILS_TABLE, FIELD_EXTENSION_ID} from "../constants.js";
import db from "../db.js";
import {HTTP_STATUS_CODES} from "@aicore/libcommonutils";
const schema = {
schema: {
querystring: {
type: 'object',
required: ['extensionName', 'extensionVersion'],
properties: {
extensionName: {
type: 'string',
minLength: 1,
maxLength: 256
},
extensionVersion: {
type: 'string',
minLength: 1,
maxLength: 32
}
}
},
response: {
200: { //HTTP_STATUS_CODES.OK
type: 'object',
required: ['message'],
properties: {
message: {type: 'string'}
}
}
}
}
};
export function getCountDownloadSchema() {
return schema;
}
async function _getRegistryPkgJSON(extensionName) {
const queryObj = {};
queryObj[FIELD_EXTENSION_ID] = extensionName;
let registryPKGJSON = await db.getFromIndex(EXTENSIONS_DETAILS_TABLE, queryObj);
if(!registryPKGJSON.isSuccess){
// unexpected error
throw new Error("Error getting extensionPKG details from db: " + extensionName);
}
if(registryPKGJSON.documents.length === 1){
let existingRegistryDocumentId = registryPKGJSON.documents[0].documentId;
delete registryPKGJSON.documents[0].documentId;
return {
existingRegistryDocumentId,
registryPKGJSON: registryPKGJSON.documents[0]
};
}
return null;
}
/**
* Checks if the given version is in the list of published versions in the registry entry.
* @param registryPKGJSON
* @param version
* @private
*/
function _isPublishedVersion(registryPKGJSON, version) {
let versions = [];
versions.push(registryPKGJSON.metadata.version);
for(let versionInfo of registryPKGJSON.versions){
versions.push(versionInfo.version);
}
return versions.includes(version);
}
export async function countDownload(request, reply) {
const extensionName = request.query.extensionName; // extension.name
const extensionVersion = request.query.extensionVersion; // 1.0.2
reply.header("Access-Control-Allow-Origin", "*");
reply.header("Access-Control-Allow-Methods", "GET");
let registryEntry = await _getRegistryPkgJSON(extensionName);
if(!registryEntry) {
reply.status(HTTP_STATUS_CODES.BAD_REQUEST);
throw new Error("No such extension");
}
if(!_isPublishedVersion(registryEntry.registryPKGJSON, extensionVersion)) {
reply.status(HTTP_STATUS_CODES.BAD_REQUEST);
throw new Error("No such extension version");
}
let status = await db.mathAdd(EXTENSIONS_DETAILS_TABLE, registryEntry.existingRegistryDocumentId, {
totalDownloads: 1
});
if(!status.isSuccess) {
throw new Error("Could not increment download count.");
}
const response = {
message: `Done`
};
return response;
}