total5 0.0.16-7 → 0.0.16-9
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/aimodel.js +47 -9
- package/api.js +58 -28
- package/builders.js +20 -3
- package/changelog.txt +5 -0
- package/global.js +2 -2
- package/index.js +1 -0
- package/package.json +1 -1
- package/routing.js +7 -7
- package/utils.js +52 -33
- package/websocket.js +5 -4
- package/workers.js +13 -13
package/aimodel.js
CHANGED
|
@@ -7,11 +7,11 @@ function AI(model) {
|
|
|
7
7
|
|
|
8
8
|
t.options = {};
|
|
9
9
|
t.options.callback = NOOP;
|
|
10
|
+
t.config = {};
|
|
10
11
|
|
|
11
12
|
t.payload = {};
|
|
12
13
|
t.payload.model = model;
|
|
13
14
|
t.payload.messages = [];
|
|
14
|
-
t.response = [];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
// Enables think mode
|
|
@@ -30,13 +30,34 @@ AI.prototype.message = function(role, content, merge) {
|
|
|
30
30
|
if (merge) {
|
|
31
31
|
for (let m of t.payload.messages) {
|
|
32
32
|
if (m.role === role) {
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
if (typeof(content) === 'object') {
|
|
35
|
+
for (let key in content)
|
|
36
|
+
m[key] = content[key];
|
|
37
|
+
} else
|
|
38
|
+
m.content += merge + content;
|
|
39
|
+
|
|
34
40
|
return this;
|
|
35
41
|
}
|
|
36
42
|
}
|
|
37
43
|
}
|
|
38
44
|
|
|
39
|
-
|
|
45
|
+
const msg = { role: role };
|
|
46
|
+
|
|
47
|
+
if (typeof(content) === 'object') {
|
|
48
|
+
for (let key in content)
|
|
49
|
+
msg[key] = content[key];
|
|
50
|
+
} else
|
|
51
|
+
msg.content = content;
|
|
52
|
+
|
|
53
|
+
t.payload.messages.push(msg);
|
|
54
|
+
return t;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
AI.prototype.configure = function(opt) {
|
|
58
|
+
const t = this;
|
|
59
|
+
for (let key in opt)
|
|
60
|
+
t.config[key] = opt[key];
|
|
40
61
|
return t;
|
|
41
62
|
};
|
|
42
63
|
|
|
@@ -48,6 +69,11 @@ AI.prototype.user = function(content, merge) {
|
|
|
48
69
|
return this.message('user', content, merge);
|
|
49
70
|
};
|
|
50
71
|
|
|
72
|
+
AI.prototype.prompt = function(content) {
|
|
73
|
+
this.payload.prompt = content;
|
|
74
|
+
return this;
|
|
75
|
+
};
|
|
76
|
+
|
|
51
77
|
AI.prototype.assistant = function(content, merge) {
|
|
52
78
|
return this.message('assistant', content, merge);
|
|
53
79
|
};
|
|
@@ -86,10 +112,16 @@ AI.prototype.callback = function(fn) {
|
|
|
86
112
|
// Internal function
|
|
87
113
|
AI.prototype.run = function() {
|
|
88
114
|
const t = this;
|
|
89
|
-
let
|
|
90
|
-
if (
|
|
91
|
-
|
|
92
|
-
|
|
115
|
+
let ai = F.aimodels[t.payload.model];
|
|
116
|
+
if (ai) {
|
|
117
|
+
if (ai.config) {
|
|
118
|
+
for (let key in ai.config) {
|
|
119
|
+
if (t.config[key] === undefined)
|
|
120
|
+
t.config[key] = ai.config[key];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
ai.callback(t, t.options.callback);
|
|
124
|
+
} else
|
|
93
125
|
t.options.callback('AI model not found.');
|
|
94
126
|
return t;
|
|
95
127
|
};
|
|
@@ -98,8 +130,14 @@ exports.exec = function(model) {
|
|
|
98
130
|
return new AI(model);
|
|
99
131
|
};
|
|
100
132
|
|
|
101
|
-
exports.newai = function
|
|
133
|
+
exports.newai = function(model, config, callback) {
|
|
134
|
+
|
|
135
|
+
if (typeof(config) === 'function') {
|
|
136
|
+
callback = config;
|
|
137
|
+
config = null;
|
|
138
|
+
}
|
|
139
|
+
|
|
102
140
|
const models = model.split(/,/).trim();
|
|
103
141
|
for (const m of models)
|
|
104
|
-
F.aimodels[m] = callback;
|
|
142
|
+
F.aimodels[m] = { config, callback };
|
|
105
143
|
};
|
package/api.js
CHANGED
|
@@ -10,17 +10,29 @@ const REG_TEXT = /^text\/(html|plain|xml)/;
|
|
|
10
10
|
var cache = {};
|
|
11
11
|
|
|
12
12
|
// Registers a new API type
|
|
13
|
-
exports.newapi = function(type, callback) {
|
|
13
|
+
exports.newapi = function(type, config, callback) {
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
let t = typeof(type);
|
|
16
|
+
|
|
17
|
+
if (t === 'function') {
|
|
16
18
|
callback = type;
|
|
17
19
|
type = 'default';
|
|
20
|
+
config = null;
|
|
21
|
+
} else if (t === 'object') {
|
|
22
|
+
callback = config;
|
|
23
|
+
config = type;
|
|
24
|
+
type = 'default';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (typeof(config) === 'function') {
|
|
28
|
+
callback = config;
|
|
29
|
+
config = null;
|
|
18
30
|
}
|
|
19
31
|
|
|
20
32
|
if (type.indexOf(',') !== -1) {
|
|
21
33
|
var arr = type.split(',').trim();
|
|
22
34
|
for (var m of arr)
|
|
23
|
-
exports.newapi(m, callback);
|
|
35
|
+
exports.newapi(m, config, callback);
|
|
24
36
|
return;
|
|
25
37
|
}
|
|
26
38
|
|
|
@@ -30,15 +42,17 @@ exports.newapi = function(type, callback) {
|
|
|
30
42
|
cache[lower] = lower;
|
|
31
43
|
|
|
32
44
|
if (callback)
|
|
33
|
-
F.apiservices[lower] = callback;
|
|
45
|
+
F.apiservices[lower] = { config, callback };
|
|
34
46
|
else
|
|
35
47
|
delete F.apiservices[lower];
|
|
36
48
|
|
|
37
49
|
};
|
|
38
50
|
|
|
39
51
|
function APIOptions(api) {
|
|
40
|
-
|
|
41
|
-
|
|
52
|
+
const t = this;
|
|
53
|
+
t.api = api;
|
|
54
|
+
t.retries = 0;
|
|
55
|
+
t.config = {};
|
|
42
56
|
}
|
|
43
57
|
|
|
44
58
|
APIOptions.prototype.retry = function() {
|
|
@@ -58,8 +72,8 @@ APICallProto.output = function(type) {
|
|
|
58
72
|
};
|
|
59
73
|
|
|
60
74
|
APICallProto.promise = function($) {
|
|
61
|
-
|
|
62
|
-
|
|
75
|
+
const t = this;
|
|
76
|
+
const promise = new Promise(function(resolve, reject) {
|
|
63
77
|
t.$callback = function(err, response) {
|
|
64
78
|
if (err) {
|
|
65
79
|
if ($ && $.invalid) {
|
|
@@ -75,7 +89,7 @@ APICallProto.promise = function($) {
|
|
|
75
89
|
};
|
|
76
90
|
|
|
77
91
|
APICallProto.audit = function($, message, type) {
|
|
78
|
-
|
|
92
|
+
const t = this;
|
|
79
93
|
t.$audit = function() {
|
|
80
94
|
// Dynamic arguments
|
|
81
95
|
if (message)
|
|
@@ -85,8 +99,15 @@ APICallProto.audit = function($, message, type) {
|
|
|
85
99
|
return t;
|
|
86
100
|
};
|
|
87
101
|
|
|
102
|
+
APICallProto.configure = function(opt) {
|
|
103
|
+
const t = this;
|
|
104
|
+
for (let key in opt)
|
|
105
|
+
t.options.config[key] = opt[key];
|
|
106
|
+
return t;
|
|
107
|
+
};
|
|
108
|
+
|
|
88
109
|
APICallProto.done = function($, callback) {
|
|
89
|
-
|
|
110
|
+
const t = this;
|
|
90
111
|
t.$callback = function(err, response) {
|
|
91
112
|
if (err)
|
|
92
113
|
$.invalid(err);
|
|
@@ -119,12 +140,12 @@ APICallProto.controller = function($) {
|
|
|
119
140
|
|
|
120
141
|
APICallProto.file = function(filename, path, name) {
|
|
121
142
|
|
|
122
|
-
|
|
143
|
+
const t = this;
|
|
123
144
|
|
|
124
145
|
if (!t.options.files)
|
|
125
146
|
t.options.files = [];
|
|
126
147
|
|
|
127
|
-
|
|
148
|
+
const obj = { name: name || ('file' + t.options.files.length), filename: filename, path: path };
|
|
128
149
|
|
|
129
150
|
if (t.options.files)
|
|
130
151
|
t.options.files.push(obj);
|
|
@@ -146,14 +167,14 @@ APICallProto.logerror = function() {
|
|
|
146
167
|
};
|
|
147
168
|
|
|
148
169
|
APICallProto.callback = APICallProto.pipe = function($) {
|
|
149
|
-
|
|
170
|
+
const t = this;
|
|
150
171
|
t.$callback = typeof($) === 'function' ? $ : $.callback();
|
|
151
172
|
return t;
|
|
152
173
|
};
|
|
153
174
|
|
|
154
175
|
APICallProto.evaluate = function(err, response) {
|
|
155
176
|
|
|
156
|
-
|
|
177
|
+
const t = this;
|
|
157
178
|
if (!err && t.$error) {
|
|
158
179
|
if (t.$error_reverse) {
|
|
159
180
|
if (response)
|
|
@@ -184,16 +205,24 @@ APICallProto.evaluate = function(err, response) {
|
|
|
184
205
|
};
|
|
185
206
|
|
|
186
207
|
function execapi(api) {
|
|
187
|
-
|
|
188
|
-
if (conn)
|
|
189
|
-
|
|
190
|
-
|
|
208
|
+
const conn = F.apiservices[cache[api.options.name]] || F.apiservices['*'];
|
|
209
|
+
if (conn) {
|
|
210
|
+
|
|
211
|
+
if (conn.config) {
|
|
212
|
+
for (let key in conn.config) {
|
|
213
|
+
if (api.options.config[key] === undefined)
|
|
214
|
+
api.options.config[key] = conn.config[key];
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
conn.callback.call(api, api.options, (err, response) => api.evaluate(err, response));
|
|
219
|
+
} else
|
|
191
220
|
api.evaluate('API is not initialized');
|
|
192
221
|
}
|
|
193
222
|
|
|
194
223
|
// Executes API
|
|
195
224
|
exports.exec = function(name, schema, data, $) {
|
|
196
|
-
|
|
225
|
+
const api = new APICall();
|
|
197
226
|
api.options.name = cache[name] || name;
|
|
198
227
|
api.options.schema = schema;
|
|
199
228
|
api.options.data = data;
|
|
@@ -212,7 +241,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
|
|
|
212
241
|
if (opt.data && typeof(opt.data) !== 'object')
|
|
213
242
|
opt.data = { value: opt.data };
|
|
214
243
|
|
|
215
|
-
|
|
244
|
+
const req = {};
|
|
216
245
|
|
|
217
246
|
req.method = 'POST';
|
|
218
247
|
req.url = 'https://' + F.config.$tapiurl + '.api.totaljs.com/' + opt.schema + '/';
|
|
@@ -226,7 +255,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
|
|
|
226
255
|
req.type = 'json';
|
|
227
256
|
req.timeout = 60000;
|
|
228
257
|
req.keepalive = true;
|
|
229
|
-
req.headers = { 'x-token': opt.token || F.config.totalapi || F.config.secret_totalapi || F.config.$tapisecret || '-', 'x-app': encodeURIComponent(F.config.name) };
|
|
258
|
+
req.headers = { 'x-token': opt.token || opt.config.token || F.config.totalapi || F.config.secret_totalapi || F.config.$tapisecret || '-', 'x-app': encodeURIComponent(F.config.name) };
|
|
230
259
|
req.custom = true;
|
|
231
260
|
|
|
232
261
|
req.callback = function(err, response) {
|
|
@@ -236,7 +265,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
|
|
|
236
265
|
return;
|
|
237
266
|
}
|
|
238
267
|
|
|
239
|
-
|
|
268
|
+
const buffer = [];
|
|
240
269
|
|
|
241
270
|
// Error
|
|
242
271
|
if (response.status > 200) {
|
|
@@ -256,7 +285,8 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
|
|
|
256
285
|
if (opt.output === 'base64') {
|
|
257
286
|
output = output.toString('base64');
|
|
258
287
|
} else if (opt.output !== 'binary' && opt.output !== 'buffer') {
|
|
259
|
-
|
|
288
|
+
|
|
289
|
+
const type = response.headers['content-type'];
|
|
260
290
|
|
|
261
291
|
if (REG_BINARY.test(type)) {
|
|
262
292
|
next(null, output);
|
|
@@ -292,20 +322,20 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
|
|
|
292
322
|
}
|
|
293
323
|
|
|
294
324
|
var type = (response.headers['content-type'] || '').toLowerCase();
|
|
295
|
-
|
|
325
|
+
const index = type.lastIndexOf(';');
|
|
296
326
|
if (index !== -1)
|
|
297
327
|
type = type.substring(0, index);
|
|
298
328
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
329
|
+
const ext = type ? F.TUtils.getExtensionFromContentType(type) : 'bin';
|
|
330
|
+
const id = fsdata[1] || UID();
|
|
331
|
+
const filename = fsdata[2] || id + '.' + ext;
|
|
302
332
|
|
|
303
333
|
response.stream.pause();
|
|
304
334
|
fs.save(id, filename, response.stream, next);
|
|
305
335
|
return;
|
|
306
336
|
}
|
|
307
337
|
|
|
308
|
-
|
|
338
|
+
const writer = F.Fs.createWriteStream(opt.output);
|
|
309
339
|
response.stream.pipe(writer);
|
|
310
340
|
F.cleanup(writer, () => opt.next(null, opt.output));
|
|
311
341
|
};
|
package/builders.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Total.js Builders
|
|
2
2
|
// The MIT License
|
|
3
|
-
// Copyright 2023 (c) Peter Širka <petersirka@gmail.com>
|
|
3
|
+
// Copyright 2023-2026 (c) Peter Širka <petersirka@gmail.com>
|
|
4
4
|
|
|
5
5
|
'use strict';
|
|
6
6
|
|
|
@@ -247,7 +247,7 @@ Options.prototype.successful = function(callback) {
|
|
|
247
247
|
};
|
|
248
248
|
};
|
|
249
249
|
|
|
250
|
-
Options.prototype.callback = Options.prototype.pipe = function(value) {
|
|
250
|
+
Options.prototype.callback = Options.prototype.output = Options.prototype.pipe = function(value) {
|
|
251
251
|
|
|
252
252
|
var self = this;
|
|
253
253
|
|
|
@@ -1405,6 +1405,7 @@ ActionCaller.prototype.exec = function() {
|
|
|
1405
1405
|
let type = meta.payload || (action.input ? '+' : '-');
|
|
1406
1406
|
let $ = self.$;
|
|
1407
1407
|
|
|
1408
|
+
$.$end = false;
|
|
1408
1409
|
$.name = action.name;
|
|
1409
1410
|
$.id = action.id;
|
|
1410
1411
|
$.error = self.error;
|
|
@@ -1416,6 +1417,16 @@ ActionCaller.prototype.exec = function() {
|
|
|
1416
1417
|
|
|
1417
1418
|
$.$callback = function(err, response) {
|
|
1418
1419
|
|
|
1420
|
+
if ($.$end)
|
|
1421
|
+
return;
|
|
1422
|
+
|
|
1423
|
+
$.$end = true;
|
|
1424
|
+
|
|
1425
|
+
if ($.$timeout) {
|
|
1426
|
+
clearTimeout($.$timeout);
|
|
1427
|
+
$.$timeout = null;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1419
1430
|
if (!err) {
|
|
1420
1431
|
if (action.jsoutput)
|
|
1421
1432
|
response = action.jsoutput.transform(response).response;
|
|
@@ -1514,6 +1525,12 @@ ActionCaller.prototype.exec = function() {
|
|
|
1514
1525
|
if (self.options.progress)
|
|
1515
1526
|
$.progress2 = self.options.progress;
|
|
1516
1527
|
|
|
1528
|
+
if (action.timeout) {
|
|
1529
|
+
$.$timeout = setTimeout(function() {
|
|
1530
|
+
$.invalid('Timeout.');
|
|
1531
|
+
}, action.timeout);
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1517
1534
|
if (action.middleware) {
|
|
1518
1535
|
action.middleware.wait(function(name, next) {
|
|
1519
1536
|
let fn = F.routes.middleware[name];
|
|
@@ -1538,7 +1555,7 @@ ActionCaller.prototype.finish = function(value) {
|
|
|
1538
1555
|
if (self.error.length)
|
|
1539
1556
|
$.invalid(self.error);
|
|
1540
1557
|
else
|
|
1541
|
-
$.callback.call(
|
|
1558
|
+
$.callback.call($, value === undefined ? self.$.response : value);
|
|
1542
1559
|
} else
|
|
1543
1560
|
self.options.callback.call(self.$, self.error.length ? self.error : null, value === undefined ? self.$.response : value);
|
|
1544
1561
|
|
package/changelog.txt
CHANGED
|
@@ -24,6 +24,11 @@
|
|
|
24
24
|
- added a new global methods `AIMODEL()` and `NEWAIMODEL()`
|
|
25
25
|
- added `exec` command to the remote editing functionality
|
|
26
26
|
- always set `cwd` directory for the current executed script
|
|
27
|
+
- added `timeout {Number}` property into the `NEWACTION` method
|
|
28
|
+
- extended `NEWAPI(type, [config], config)` by adding the `config` argument
|
|
29
|
+
- replaced `Url.parse()` with `new URL()`
|
|
30
|
+
- fixed `NEWFORK()` method
|
|
31
|
+
- fixed callback with `$` in the `ACTION().callback($)`
|
|
27
32
|
|
|
28
33
|
========================
|
|
29
34
|
0.0.15
|
package/global.js
CHANGED
|
@@ -250,11 +250,11 @@ global.TMSCLIENT = F.TTMS.client;
|
|
|
250
250
|
|
|
251
251
|
// API
|
|
252
252
|
global.API = (name, schema, data, $) => F.TApi.exec(name, schema, data, $);
|
|
253
|
-
global.NEWAPI = (name, callback) => F.TApi.newapi(name, callback);
|
|
253
|
+
global.NEWAPI = (name, config, callback) => F.TApi.newapi(name, config, callback);
|
|
254
254
|
|
|
255
255
|
// AI
|
|
256
256
|
global.AIMODEL = (name, schema, data, $) => F.TAIModel.exec(name, schema, data, $);
|
|
257
|
-
global.NEWAIMODEL = (name, callback) => F.TAIModel.newai(name, callback);
|
|
257
|
+
global.NEWAIMODEL = (name, config, callback) => F.TAIModel.newai(name, config, callback);
|
|
258
258
|
|
|
259
259
|
// NoSQL
|
|
260
260
|
global.NOSQL = F.TNoSQL.nosql;
|
package/index.js
CHANGED
|
@@ -2942,6 +2942,7 @@ process.on('message', function(msg, h) {
|
|
|
2942
2942
|
F.Tls = F.require('node:tls');
|
|
2943
2943
|
F.Stream = F.require('node:stream');
|
|
2944
2944
|
F.Cluster = require('node:cluster');
|
|
2945
|
+
F.Util = F.require('node:util');
|
|
2945
2946
|
|
|
2946
2947
|
// Total.js modules
|
|
2947
2948
|
F.TUtils = require('./utils');
|
package/package.json
CHANGED
package/routing.js
CHANGED
|
@@ -804,7 +804,7 @@ function Proxy(url, target) {
|
|
|
804
804
|
if (typeof(target) === 'function')
|
|
805
805
|
t.stream = target;
|
|
806
806
|
else if ((/^(https|http):\/\//).test(target))
|
|
807
|
-
t.target = F.
|
|
807
|
+
t.target = F.TUtils.parseURI(target);
|
|
808
808
|
else
|
|
809
809
|
t.target = { socketPath: target };
|
|
810
810
|
|
|
@@ -887,20 +887,20 @@ exports.proxy = function(url, target) {
|
|
|
887
887
|
url = F.virtualpath(url);
|
|
888
888
|
|
|
889
889
|
if (!target) {
|
|
890
|
-
|
|
890
|
+
const index = F.routes.proxies.TfindIndex('url', url.toLowerCase());
|
|
891
891
|
if (index !== -1)
|
|
892
892
|
F.routes.proxies.splice(index, 1);
|
|
893
893
|
return;
|
|
894
894
|
}
|
|
895
895
|
|
|
896
|
-
|
|
896
|
+
const proxy = new Proxy(url, target);
|
|
897
897
|
F.routes.proxies.push(proxy);
|
|
898
898
|
return proxy;
|
|
899
899
|
};
|
|
900
900
|
|
|
901
901
|
exports.lookupproxy = function(ctrl) {
|
|
902
|
-
for (
|
|
903
|
-
|
|
902
|
+
for (let proxy of F.routes.proxies) {
|
|
903
|
+
let u = ctrl.uri.key.substring(0, proxy.url.length);
|
|
904
904
|
if (u[u.length - 1] !== '/')
|
|
905
905
|
u += '/';
|
|
906
906
|
if (u === proxy.url && (!proxy.$check || proxy.$check(ctrl))) {
|
|
@@ -913,10 +913,10 @@ exports.lookupproxy = function(ctrl) {
|
|
|
913
913
|
|
|
914
914
|
function proxyheadersws(header, headers) {
|
|
915
915
|
|
|
916
|
-
|
|
916
|
+
const output = [];
|
|
917
917
|
|
|
918
918
|
for (let key in headers) {
|
|
919
|
-
|
|
919
|
+
const value = headers[key];
|
|
920
920
|
if (value instanceof Array) {
|
|
921
921
|
for (let item of value)
|
|
922
922
|
output.push(key + ': ' + item);
|
package/utils.js
CHANGED
|
@@ -491,16 +491,16 @@ exports.toURLEncode = function(value) {
|
|
|
491
491
|
|
|
492
492
|
exports.resolve = function(url, callback, param) {
|
|
493
493
|
|
|
494
|
-
|
|
494
|
+
let uri;
|
|
495
495
|
|
|
496
496
|
try {
|
|
497
|
-
uri =
|
|
497
|
+
uri = exports.parseURI(url);
|
|
498
498
|
} catch (e) {
|
|
499
499
|
callback(e);
|
|
500
500
|
return;
|
|
501
501
|
}
|
|
502
502
|
|
|
503
|
-
|
|
503
|
+
const cache = F.temporary.dnscache[uri.host];
|
|
504
504
|
|
|
505
505
|
if (!callback)
|
|
506
506
|
return cache;
|
|
@@ -538,7 +538,7 @@ function keywordscleaner(c) {
|
|
|
538
538
|
|
|
539
539
|
function parseProxy(p) {
|
|
540
540
|
|
|
541
|
-
|
|
541
|
+
const key = 'proxy_' + p;
|
|
542
542
|
|
|
543
543
|
if (F.temporary.utils[key])
|
|
544
544
|
return F.temporary.utils[key];
|
|
@@ -546,7 +546,7 @@ function parseProxy(p) {
|
|
|
546
546
|
if (p.indexOf('://') === -1)
|
|
547
547
|
p = 'http://' + p;
|
|
548
548
|
|
|
549
|
-
|
|
549
|
+
const obj = exports.parseURI(p);
|
|
550
550
|
|
|
551
551
|
if (obj.auth)
|
|
552
552
|
obj._auth = 'Basic ' + Buffer.from(obj.auth).toString('base64');
|
|
@@ -563,7 +563,7 @@ function parseProxy(p) {
|
|
|
563
563
|
|
|
564
564
|
function _request(opt, callback) {
|
|
565
565
|
|
|
566
|
-
|
|
566
|
+
const options = { length: 0, timeout: opt.timeout == false || opt.timeout == 0 ? 0 : (opt.timeout || 8000), encoding: opt.encoding || 'utf8', callback: callback || opt.callback || NOOP, post: true, redirect: 0 };
|
|
567
567
|
var proxy;
|
|
568
568
|
|
|
569
569
|
F.stats.performance.external++;
|
|
@@ -691,7 +691,7 @@ function _request(opt, callback) {
|
|
|
691
691
|
}
|
|
692
692
|
}
|
|
693
693
|
|
|
694
|
-
|
|
694
|
+
const uri = opt.unixsocket ? { socketPath: opt.unixsocket.socket, path: opt.unixsocket.path } : exports.parseURI(opt.url);
|
|
695
695
|
|
|
696
696
|
if ((opt.unixsocket && !uri.socketPath) || (!opt.unixsocket && (!uri.hostname || !uri.host))) {
|
|
697
697
|
options.response.canceled = true;
|
|
@@ -794,9 +794,9 @@ PAP.createConnection = function(pending) {
|
|
|
794
794
|
|
|
795
795
|
PAP.createSocket = function(options, callback) {
|
|
796
796
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
797
|
+
const self = this;
|
|
798
|
+
const proxy = self.options.proxy;
|
|
799
|
+
const uri = self.options.uri;
|
|
800
800
|
|
|
801
801
|
PROXYOPTIONS.host = proxy.hostname;
|
|
802
802
|
PROXYOPTIONS.port = proxy.port;
|
|
@@ -805,7 +805,7 @@ PAP.createSocket = function(options, callback) {
|
|
|
805
805
|
if (proxy._auth)
|
|
806
806
|
PROXYOPTIONS.headers['Proxy-Authorization'] = proxy._auth;
|
|
807
807
|
|
|
808
|
-
|
|
808
|
+
const req = self.request(PROXYOPTIONS);
|
|
809
809
|
req.setTimeout(10000);
|
|
810
810
|
req.on('response', proxyagent_response);
|
|
811
811
|
req.on('connect', function(res, socket) {
|
|
@@ -825,7 +825,7 @@ PAP.createSocket = function(options, callback) {
|
|
|
825
825
|
});
|
|
826
826
|
|
|
827
827
|
req.on('error', function(err) {
|
|
828
|
-
|
|
828
|
+
const e = new Error('Request Proxy "proxy {0} --> target {1}": {2}'.format(PROXYOPTIONS.host + ':' + proxy.port, PROXYOPTIONS.path, err.toString()));
|
|
829
829
|
e.code = err.code;
|
|
830
830
|
req.destroy && req.destroy();
|
|
831
831
|
req = null;
|
|
@@ -866,14 +866,17 @@ function request_call(uri, options) {
|
|
|
866
866
|
opt.path = uri.href;
|
|
867
867
|
opt.headers = uri.headers;
|
|
868
868
|
opt.method = uri.method;
|
|
869
|
-
|
|
869
|
+
|
|
870
|
+
if (uri.host)
|
|
871
|
+
opt.headers.host = uri.host;
|
|
872
|
+
|
|
870
873
|
if (options.proxy._auth)
|
|
871
874
|
opt.headers['Proxy-Authorization'] = options.proxy._auth;
|
|
872
875
|
} else
|
|
873
876
|
opt = uri;
|
|
874
877
|
|
|
875
|
-
|
|
876
|
-
|
|
878
|
+
const connection = uri.protocol === 'https:' ? F.Https : F.Http;
|
|
879
|
+
const req = opt.method === 'GET' ? connection.get(opt, request_response) : connection.request(opt, request_response);
|
|
877
880
|
|
|
878
881
|
req.$options = options;
|
|
879
882
|
req.$uri = uri;
|
|
@@ -986,11 +989,10 @@ function request_writefile(req, options, file, next) {
|
|
|
986
989
|
return;
|
|
987
990
|
}
|
|
988
991
|
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
var filename = (isbuffer || isurl ? file.filename : exports.getName(file.filename));
|
|
992
|
+
const filedata = file.buffer || file.url;
|
|
993
|
+
const isbuffer = filedata instanceof Buffer;
|
|
994
|
+
const isurl = isbuffer ? false : typeof(filedata) === 'string' && filedata;
|
|
995
|
+
const filename = (isbuffer || isurl ? file.filename : exports.getName(file.filename));
|
|
994
996
|
|
|
995
997
|
req.write((options.first ? '' : NEWLINE) + '--' + options.boundary + NEWLINE + 'Content-Disposition: form-data; name="' + file.name + '"; filename="' + filename + '"' + NEWLINE + 'Content-Type: ' + exports.getContentType(exports.getExtension(filename)) + NEWLINE + NEWLINE);
|
|
996
998
|
|
|
@@ -1030,8 +1032,8 @@ function request_writefile(req, options, file, next) {
|
|
|
1030
1032
|
|
|
1031
1033
|
function request_response(res) {
|
|
1032
1034
|
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
+
const options = this.$options;
|
|
1036
|
+
const uri = this.$uri;
|
|
1035
1037
|
|
|
1036
1038
|
res._buffer = null;
|
|
1037
1039
|
res._bufferlength = 0;
|
|
@@ -1093,18 +1095,18 @@ function request_response(res) {
|
|
|
1093
1095
|
|
|
1094
1096
|
options.redirect++;
|
|
1095
1097
|
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
+
let loc = res.headers.location;
|
|
1099
|
+
const proto = loc.substring(0, 6);
|
|
1098
1100
|
|
|
1099
1101
|
if (proto !== 'http:/' && proto !== 'https:')
|
|
1100
1102
|
loc = uri.protocol + '//' + uri.hostname + (uri.port && !SKI_PPORTS[uri.port] ? (':' + uri.port) : '') + loc;
|
|
1101
1103
|
|
|
1102
|
-
var tmp =
|
|
1104
|
+
var tmp = exports.parseURI(loc);
|
|
1103
1105
|
tmp.headers = uri.headers;
|
|
1104
1106
|
|
|
1105
1107
|
// Transfers cookies
|
|
1106
1108
|
if (!options.nocookies) {
|
|
1107
|
-
|
|
1109
|
+
const cookies = res.headers['set-cookie'];
|
|
1108
1110
|
if (cookies) {
|
|
1109
1111
|
|
|
1110
1112
|
if (options.$totalinit.cook && !options.$totalinit.cookies)
|
|
@@ -1113,14 +1115,14 @@ function request_response(res) {
|
|
|
1113
1115
|
if (!options.cookies)
|
|
1114
1116
|
options.cookies = {};
|
|
1115
1117
|
|
|
1116
|
-
for (
|
|
1117
|
-
|
|
1118
|
-
|
|
1118
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
1119
|
+
let cookie = cookies[i];
|
|
1120
|
+
let index = cookie.indexOf(';');
|
|
1119
1121
|
if (index !== -1){
|
|
1120
1122
|
cookie = cookie.substring(0, index);
|
|
1121
1123
|
index = cookie.indexOf('=');
|
|
1122
1124
|
if (index !== -1) {
|
|
1123
|
-
|
|
1125
|
+
const key = decodeURIComponent(cookie.substring(0, index));
|
|
1124
1126
|
options.cookies[key] = decodeURIComponent(cookie.substring(index + 1));
|
|
1125
1127
|
if (options.$totalinit.cookies)
|
|
1126
1128
|
options.$totalinit.cookies[key] = options.cookies[key];
|
|
@@ -1128,7 +1130,7 @@ function request_response(res) {
|
|
|
1128
1130
|
}
|
|
1129
1131
|
}
|
|
1130
1132
|
|
|
1131
|
-
|
|
1133
|
+
let builder = '';
|
|
1132
1134
|
for (var m in options.cookies)
|
|
1133
1135
|
builder += (builder ? '; ' : '') + encodeURIComponent(m) + '=' + encodeURIComponent(options.cookies[m]);
|
|
1134
1136
|
|
|
@@ -1162,12 +1164,13 @@ function request_response(res) {
|
|
|
1162
1164
|
if (!options.resolve) {
|
|
1163
1165
|
res.removeAllListeners();
|
|
1164
1166
|
res = null;
|
|
1165
|
-
|
|
1167
|
+
request_call(tmp, options);
|
|
1168
|
+
return;
|
|
1166
1169
|
}
|
|
1167
1170
|
|
|
1168
1171
|
exports.resolve(tmp, function(err, u, param, origin) {
|
|
1169
1172
|
if (!err) {
|
|
1170
|
-
tmp.
|
|
1173
|
+
tmp.ip = u.host;
|
|
1171
1174
|
options.origin = origin;
|
|
1172
1175
|
}
|
|
1173
1176
|
res.removeAllListeners();
|
|
@@ -6459,6 +6462,22 @@ exports.uidr = function() {
|
|
|
6459
6462
|
return builder + RANDOM_STRING[sum] + 'r'; // "r" version
|
|
6460
6463
|
};
|
|
6461
6464
|
|
|
6465
|
+
exports.parseURI = function(url) {
|
|
6466
|
+
const uri = new URL(url);
|
|
6467
|
+
return {
|
|
6468
|
+
auth: (uri.username || uri.password) ? ((uri.username || '') + ':' + (user.password || '')) : '',
|
|
6469
|
+
port: uri.port,
|
|
6470
|
+
search: uri.search,
|
|
6471
|
+
hash: uri.hash,
|
|
6472
|
+
pathname: uri.pathname,
|
|
6473
|
+
href: uri.href,
|
|
6474
|
+
protocol: uri.protocol,
|
|
6475
|
+
hostname: uri.hostname,
|
|
6476
|
+
host: uri.host,
|
|
6477
|
+
path: uri.pathname + uri.search
|
|
6478
|
+
};
|
|
6479
|
+
};
|
|
6480
|
+
|
|
6462
6481
|
exports.paginate = function(page, pages, max) {
|
|
6463
6482
|
|
|
6464
6483
|
if (!page)
|
package/websocket.js
CHANGED
|
@@ -1225,7 +1225,7 @@ WebSocketClient.prototype.connect = function(url, protocol, origin) {
|
|
|
1225
1225
|
|
|
1226
1226
|
WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
|
|
1227
1227
|
|
|
1228
|
-
|
|
1228
|
+
const options = {};
|
|
1229
1229
|
|
|
1230
1230
|
self.url = url;
|
|
1231
1231
|
self.origin = origin;
|
|
@@ -1238,12 +1238,12 @@ WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
|
|
|
1238
1238
|
var secured = false;
|
|
1239
1239
|
|
|
1240
1240
|
if (typeof(url) === 'string') {
|
|
1241
|
-
url = F.
|
|
1241
|
+
url = F.TUtils.parseURI(url);
|
|
1242
1242
|
options.host = url.hostname;
|
|
1243
|
-
options.path = url.path;
|
|
1244
1243
|
options.query = url.query;
|
|
1245
1244
|
secured = url.protocol === 'wss:';
|
|
1246
1245
|
options.port = url.port || (secured ? 443 : 80);
|
|
1246
|
+
options.path = url.path;
|
|
1247
1247
|
} else {
|
|
1248
1248
|
options.socketPath = url.socket;
|
|
1249
1249
|
options.path = url.path;
|
|
@@ -1284,7 +1284,8 @@ WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
|
|
|
1284
1284
|
for (let key in self.headers)
|
|
1285
1285
|
options.headers[key] = self.headers[key];
|
|
1286
1286
|
|
|
1287
|
-
|
|
1287
|
+
const tmp = [];
|
|
1288
|
+
|
|
1288
1289
|
for (let key in self.cookies)
|
|
1289
1290
|
tmp.push(key + '=' + self.cookies[key]);
|
|
1290
1291
|
|
package/workers.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Total.js Workers
|
|
2
2
|
// The MIT License
|
|
3
|
-
// Copyright
|
|
3
|
+
// Copyright 2023-2026 (c) Peter Širka <petersirka@gmail.com>
|
|
4
4
|
|
|
5
5
|
const HEADER = { cwd: '' };
|
|
6
6
|
|
|
@@ -26,7 +26,7 @@ function process_thread() {
|
|
|
26
26
|
F.worker.postMessage = F.worker.send = function() {
|
|
27
27
|
if (Port)
|
|
28
28
|
Port.postMessage.apply(Port, arguments);
|
|
29
|
-
else
|
|
29
|
+
else if (process.send)
|
|
30
30
|
process.send.apply(process, arguments);
|
|
31
31
|
};
|
|
32
32
|
|
|
@@ -34,7 +34,7 @@ function process_thread() {
|
|
|
34
34
|
process.exit(code || 0);
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const onmessage = function() {
|
|
38
38
|
F.worker.message && F.worker.message.apply(this, arguments);
|
|
39
39
|
};
|
|
40
40
|
|
|
@@ -49,8 +49,8 @@ function process_thread() {
|
|
|
49
49
|
exports.createthread = function(name, data) {
|
|
50
50
|
if (!name)
|
|
51
51
|
return process_thread();
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
const filename = name[0] === '~' ? name.substring(1) : F.path.root('workers/' + name + '.js');
|
|
53
|
+
const worker = new F.Worker.Worker(filename, { workerData: data, cwd: HEADER, argv: ['--worker'] });
|
|
54
54
|
worker.kill = worker.exit = () => worker.terminate();
|
|
55
55
|
return worker;
|
|
56
56
|
};
|
|
@@ -60,8 +60,8 @@ exports.createfork = function(name) {
|
|
|
60
60
|
if (!name)
|
|
61
61
|
return process_thread();
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
const filename = name[0] === '~' ? name.substring(1) : F.path.root('workers/' + name + '.js');
|
|
64
|
+
const fork = new F.Child.fork(filename, { cwd: HEADER.cwd, argv: ['--worker'] });
|
|
65
65
|
fork.postMessage = fork.send;
|
|
66
66
|
fork.terminate = () => fork.kill('SIGTERM');
|
|
67
67
|
return fork;
|
|
@@ -69,14 +69,14 @@ exports.createfork = function(name) {
|
|
|
69
69
|
|
|
70
70
|
exports.createpool = function(name, count, isfork) {
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
const pool = {};
|
|
73
73
|
pool.workers = [];
|
|
74
74
|
pool.pending = [];
|
|
75
75
|
pool.count = pool;
|
|
76
76
|
pool.next = function() {
|
|
77
77
|
for (let worker of pool.workers) {
|
|
78
78
|
if (worker.$released) {
|
|
79
|
-
|
|
79
|
+
const fn = pool.pending.shift();
|
|
80
80
|
if (fn) {
|
|
81
81
|
worker.removeAllListeners('message');
|
|
82
82
|
worker.$released = false;
|
|
@@ -91,9 +91,9 @@ exports.createpool = function(name, count, isfork) {
|
|
|
91
91
|
|
|
92
92
|
var release = function(worker) {
|
|
93
93
|
worker.on('exit', function() {
|
|
94
|
-
|
|
94
|
+
const index = pool.workers.indexOf(worker);
|
|
95
95
|
pool.workers.splice(index, 1);
|
|
96
|
-
|
|
96
|
+
const worker = isfork ? exports.createfork(name) : exports.createthread(name);
|
|
97
97
|
worker.$pool = pool;
|
|
98
98
|
worker.release = release(worker);
|
|
99
99
|
});
|
|
@@ -105,8 +105,8 @@ exports.createpool = function(name, count, isfork) {
|
|
|
105
105
|
|
|
106
106
|
};
|
|
107
107
|
|
|
108
|
-
for (
|
|
109
|
-
|
|
108
|
+
for (let i = 0; i < count; i++) {
|
|
109
|
+
const worker = isfork ? exports.createfork(name) : exports.createthread(name);
|
|
110
110
|
worker.$pool = pool;
|
|
111
111
|
worker.$released = true;
|
|
112
112
|
worker.release = release(worker);
|