@datatruck/cli 0.36.4 → 0.36.5
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.
- package/lib/utils/datatruck/repository-server.js +40 -41
- package/lib/utils/math.d.ts +6 -0
- package/lib/utils/math.js +14 -1
- package/package.json +1 -1
|
@@ -4,10 +4,12 @@ exports.createDatatruckRepositoryServer = exports.headerKey = void 0;
|
|
|
4
4
|
const ConfigAction_1 = require("../../actions/ConfigAction");
|
|
5
5
|
const cli_1 = require("../cli");
|
|
6
6
|
const http_1 = require("../http");
|
|
7
|
+
const math_1 = require("../math");
|
|
7
8
|
const virtual_fs_1 = require("../virtual-fs");
|
|
8
9
|
const fs_1 = require("fs");
|
|
9
10
|
const promises_1 = require("fs/promises");
|
|
10
11
|
const http_2 = require("http");
|
|
12
|
+
const promises_2 = require("stream/promises");
|
|
11
13
|
exports.headerKey = {
|
|
12
14
|
user: "x-dtt-user",
|
|
13
15
|
password: "x-dtt-password",
|
|
@@ -58,33 +60,31 @@ const getRemoteAddress = (req, options) => {
|
|
|
58
60
|
: undefined) ?? req.socket.remoteAddress);
|
|
59
61
|
};
|
|
60
62
|
function createDatatruckRepositoryServer(inOptions, config = {}) {
|
|
63
|
+
const counter = new math_1.Counter();
|
|
61
64
|
return (0, http_2.createServer)(async (req, res) => {
|
|
65
|
+
const url = req.url || "";
|
|
66
|
+
if (url === "/" || url === "/favicon.ico")
|
|
67
|
+
return res.end();
|
|
68
|
+
const id = counter.next();
|
|
69
|
+
let requestError;
|
|
70
|
+
let responseError;
|
|
71
|
+
req.on("error", (error) => (requestError = error));
|
|
72
|
+
res.on("error", (error) => (responseError = error));
|
|
62
73
|
try {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (!repository || !action) {
|
|
67
|
-
res.statusCode = 404;
|
|
68
|
-
return res.end();
|
|
69
|
-
}
|
|
74
|
+
const { repository, action, params } = parseUrl(url);
|
|
75
|
+
if (!repository || !action)
|
|
76
|
+
return res.writeHead(404);
|
|
70
77
|
const fileOptions = config.configPath
|
|
71
78
|
? (await ConfigAction_1.ConfigAction.findAndParseFile(config.configPath)).server
|
|
72
79
|
?.repository
|
|
73
80
|
: undefined;
|
|
74
81
|
const options = fileOptions ?? inOptions;
|
|
75
82
|
const backend = findRepositoryBackend(req, repository, options);
|
|
76
|
-
if (!backend)
|
|
77
|
-
res.
|
|
78
|
-
return res.end();
|
|
79
|
-
}
|
|
83
|
+
if (!backend)
|
|
84
|
+
return res.writeHead(401);
|
|
80
85
|
if (config.log)
|
|
81
|
-
(0, cli_1.logJson)("repository-server", "request", {
|
|
82
|
-
|
|
83
|
-
url: req.url,
|
|
84
|
-
});
|
|
85
|
-
const fs = new virtual_fs_1.LocalFs({
|
|
86
|
-
backend: backend.path,
|
|
87
|
-
});
|
|
86
|
+
(0, cli_1.logJson)("repository-server", "request", { id, repository, url });
|
|
87
|
+
const fs = new virtual_fs_1.LocalFs({ backend: backend.path });
|
|
88
88
|
if (action === "comcheck") {
|
|
89
89
|
res.write(JSON.stringify({ success: true }));
|
|
90
90
|
}
|
|
@@ -92,12 +92,7 @@ function createDatatruckRepositoryServer(inOptions, config = {}) {
|
|
|
92
92
|
const [target] = params;
|
|
93
93
|
const path = fs.resolvePath(target);
|
|
94
94
|
const file = (0, fs_1.createWriteStream)(path);
|
|
95
|
-
|
|
96
|
-
await new Promise((resolve, reject) => {
|
|
97
|
-
req.on("error", reject);
|
|
98
|
-
file.on("error", reject);
|
|
99
|
-
file.on("close", resolve);
|
|
100
|
-
});
|
|
95
|
+
await (0, promises_2.pipeline)(req, file);
|
|
101
96
|
}
|
|
102
97
|
else if (action === "download") {
|
|
103
98
|
const [target] = params;
|
|
@@ -105,13 +100,7 @@ function createDatatruckRepositoryServer(inOptions, config = {}) {
|
|
|
105
100
|
const file = (0, fs_1.createReadStream)(path);
|
|
106
101
|
const fileStat = await (0, promises_1.stat)(path);
|
|
107
102
|
res.setHeader("Content-Length", fileStat.size);
|
|
108
|
-
|
|
109
|
-
await new Promise((resolve, reject) => {
|
|
110
|
-
req.on("error", reject);
|
|
111
|
-
file.on("error", reject);
|
|
112
|
-
res.on("error", reject);
|
|
113
|
-
res.on("close", resolve);
|
|
114
|
-
});
|
|
103
|
+
await (0, promises_2.pipeline)(file, res, { end: false });
|
|
115
104
|
}
|
|
116
105
|
else if (action === "writeFile") {
|
|
117
106
|
const data = await (0, http_1.readRequestData)(req);
|
|
@@ -127,21 +116,31 @@ function createDatatruckRepositoryServer(inOptions, config = {}) {
|
|
|
127
116
|
res.write(JSON.stringify(json));
|
|
128
117
|
}
|
|
129
118
|
if (config.log)
|
|
130
|
-
(0, cli_1.logJson)("repository-server", "request finished", {
|
|
131
|
-
url: req.url,
|
|
132
|
-
});
|
|
133
|
-
res.end();
|
|
119
|
+
(0, cli_1.logJson)("repository-server", "request finished", { id });
|
|
134
120
|
}
|
|
135
121
|
catch (error) {
|
|
136
122
|
if (config.log) {
|
|
137
|
-
(0, cli_1.logJson)("repository-server", "request failed", {
|
|
138
|
-
url: req.url,
|
|
139
|
-
});
|
|
123
|
+
(0, cli_1.logJson)("repository-server", "request failed", { id });
|
|
140
124
|
console.error(error);
|
|
141
125
|
}
|
|
142
|
-
res.
|
|
143
|
-
|
|
144
|
-
|
|
126
|
+
if (!res.headersSent)
|
|
127
|
+
res.writeHead(500, error.message);
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
if (requestError) {
|
|
131
|
+
(0, cli_1.logJson)("repository-server", "request error", { id });
|
|
132
|
+
console.error(requestError);
|
|
133
|
+
}
|
|
134
|
+
if (responseError) {
|
|
135
|
+
(0, cli_1.logJson)("repository-server", "response error", { id });
|
|
136
|
+
console.error(responseError);
|
|
137
|
+
}
|
|
138
|
+
if (requestError || responseError) {
|
|
139
|
+
res.destroy();
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
res.end();
|
|
143
|
+
}
|
|
145
144
|
}
|
|
146
145
|
});
|
|
147
146
|
}
|
package/lib/utils/math.d.ts
CHANGED
package/lib/utils/math.js
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.progressPercent = void 0;
|
|
3
|
+
exports.Counter = exports.progressPercent = void 0;
|
|
4
4
|
function progressPercent(total, current) {
|
|
5
5
|
if (total === 0 && current === 0)
|
|
6
6
|
return 0;
|
|
7
7
|
return Number(((current / total) * 100).toFixed(2));
|
|
8
8
|
}
|
|
9
9
|
exports.progressPercent = progressPercent;
|
|
10
|
+
class Counter {
|
|
11
|
+
maxValue;
|
|
12
|
+
value = 0;
|
|
13
|
+
constructor(maxValue = 4294967295) {
|
|
14
|
+
this.maxValue = maxValue;
|
|
15
|
+
}
|
|
16
|
+
next() {
|
|
17
|
+
if (this.maxValue && this.value >= this.maxValue)
|
|
18
|
+
this.value = 0;
|
|
19
|
+
return ++this.value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.Counter = Counter;
|