[upd] plankton

This commit is contained in:
Christian Fraß 2023-07-23 09:31:54 +02:00
parent 43d600ea3e
commit d75bec75a9
2 changed files with 136 additions and 82 deletions

View file

@ -1800,8 +1800,7 @@ declare namespace lib_plankton.http {
* @author fenris <frass@greenscale.de> * @author fenris <frass@greenscale.de>
*/ */
type type_request = { type type_request = {
host: string; target: string;
query: string;
method: enum_method; method: enum_method;
headers: Record<string, string>; headers: Record<string, string>;
body: string; body: string;
@ -1840,6 +1839,7 @@ declare namespace lib_plankton.http {
function call(request: type_request, options?: { function call(request: type_request, options?: {
timeout?: (null | float); timeout?: (null | float);
follow_redirects?: boolean; follow_redirects?: boolean;
implementation?: ("fetch" | "http_module");
}): Promise<type_response>; }): Promise<type_response>;
} }
declare namespace lib_plankton.http { declare namespace lib_plankton.http {

View file

@ -4237,7 +4237,7 @@ var lib_plankton;
return (new Promise(function (resolve, reject) { return (new Promise(function (resolve, reject) {
nm_fs.readFile(path, { nm_fs.readFile(path, {
"encoding": "utf8", "encoding": "utf8",
"flag": "r" "flag": "r",
}, function (error, content) { }, function (error, content) {
if (error == null) { if (error == null) {
resolve(content); resolve(content);
@ -4256,7 +4256,7 @@ var lib_plankton;
var nm_fs = require("fs"); var nm_fs = require("fs");
return (new Promise(function (resolve, reject) { return (new Promise(function (resolve, reject) {
nm_fs.readFile(path, { nm_fs.readFile(path, {
"flag": "r" "flag": "r",
}, function (error, content) { }, function (error, content) {
if (error == null) { if (error == null) {
resolve(content); resolve(content);
@ -4293,13 +4293,13 @@ var lib_plankton;
function write(path, content, options) { function write(path, content, options) {
if (options === void 0) { options = {}; } if (options === void 0) { options = {}; }
options = Object.assign({ options = Object.assign({
"encoding": "utf-8" "encoding": "utf-8",
}, options); }, options);
var nm_fs = require("fs"); var nm_fs = require("fs");
return (new Promise(function (resolve, reject) { return (new Promise(function (resolve, reject) {
nm_fs.writeFile(path, content, { nm_fs.writeFile(path, content, {
"encoding": options.encoding, "encoding": options.encoding,
"flag": "w" "flag": "w",
}, function (error) { }, function (error) {
if (error == null) { if (error == null) {
resolve(undefined); resolve(undefined);
@ -4320,7 +4320,7 @@ var lib_plankton;
var nm_fs = require("fs"); var nm_fs = require("fs");
return (new Promise(function (resolve, reject) { return (new Promise(function (resolve, reject) {
nm_fs.writeFile(path, content, { nm_fs.writeFile(path, content, {
"flag": "w" "flag": "w",
}, function (error) { }, function (error) {
if (error == null) { if (error == null) {
resolve(undefined); resolve(undefined);
@ -4814,8 +4814,8 @@ var lib_plankton;
"info": info, "info": info,
"hidden": hidden, "hidden": hidden,
"parameters": { "parameters": {
"index": index "index": index,
} },
})); }));
}; };
/** /**
@ -4833,8 +4833,8 @@ var lib_plankton;
"hidden": hidden, "hidden": hidden,
"parameters": { "parameters": {
"indicators_short": indicators_short, "indicators_short": indicators_short,
"indicators_long": indicators_long "indicators_long": indicators_long,
} },
})); }));
}; };
/** /**
@ -5080,17 +5080,17 @@ var lib_plankton;
"symbols": { "symbols": {
"delimiter": " ", "delimiter": " ",
"prefix": "--", "prefix": "--",
"assignment": "=" "assignment": "=",
} },
}, },
"url": { "url": {
"symbols": { "symbols": {
"delimiter": "&", "delimiter": "&",
"prefix": "", "prefix": "",
"assignment": "=" "assignment": "=",
}
} }
} }
},
}; };
/** /**
* @author fenris * @author fenris
@ -5167,14 +5167,14 @@ var lib_plankton;
"pattern_from": pattern_from, "pattern_from": pattern_from,
"pattern_to": pattern_to, "pattern_to": pattern_to,
"input": input, "input": input,
"result": result "result": result,
}); });
input = result; input = result;
} }
} }
} }
lib_plankton.log.debug("lib_args:read:current_input", { lib_plankton.log.debug("lib_args:read:current_input", {
"input": input "input": input,
}); });
} }
// parsing // parsing
@ -5185,18 +5185,18 @@ var lib_plankton;
var index_expected_1 = 0; var index_expected_1 = 0;
parts.forEach(function (part) { parts.forEach(function (part) {
lib_plankton.log.debug("lib_args:read:analyzing", { lib_plankton.log.debug("lib_args:read:analyzing", {
"part": part "part": part,
}); });
var found = [ var found = [
function () { function () {
lib_plankton.log.debug("lib_args:read:probing_as_volatile", { lib_plankton.log.debug("lib_args:read:probing_as_volatile", {
"part": part "part": part,
}); });
for (var _i = 0, _a = Object.entries(_this.filter(args.enum_kind.volatile)); _i < _a.length; _i++) { for (var _i = 0, _a = Object.entries(_this.filter(args.enum_kind.volatile)); _i < _a.length; _i++) {
var _b = _a[_i], name = _b[0], argument = _b[1]; var _b = _a[_i], name = _b[0], argument = _b[1];
lib_plankton.log.debug("lib_args:read:probing_as_volatile:trying", { lib_plankton.log.debug("lib_args:read:probing_as_volatile:trying", {
"part": part, "part": part,
"argument": argument.toString() "argument": argument.toString(),
}); });
var pattern = ""; var pattern = "";
{ {
@ -5215,12 +5215,12 @@ var lib_plankton;
pattern += pattern_back; pattern += pattern_back;
} }
lib_plankton.log.debug("lib_args:read:probing_as_volatile:pattern", { lib_plankton.log.debug("lib_args:read:probing_as_volatile:pattern", {
"pattern": pattern "pattern": pattern,
}); });
var regexp = new RegExp(pattern); var regexp = new RegExp(pattern);
var matching = regexp.exec(part); var matching = regexp.exec(part);
lib_plankton.log.debug("lib_args:read:probing_as_volatile:matching", { lib_plankton.log.debug("lib_args:read:probing_as_volatile:matching", {
"matching": matching "matching": matching,
}); });
if (matching == null) { if (matching == null) {
// do nothing // do nothing
@ -5234,7 +5234,7 @@ var lib_plankton;
}, },
function () { function () {
lib_plankton.log.debug("lib_args:read:probing_as_positional", { lib_plankton.log.debug("lib_args:read:probing_as_positional", {
"part": part "part": part,
}); });
var positional = _this.filter(args.enum_kind.positional); var positional = _this.filter(args.enum_kind.positional);
for (var _i = 0, _a = Object.entries(positional); _i < _a.length; _i++) { for (var _i = 0, _a = Object.entries(positional); _i < _a.length; _i++) {
@ -5245,7 +5245,7 @@ var lib_plankton;
else { else {
lib_plankton.log.debug("lib_args:read:probing_as_positional:trying", { lib_plankton.log.debug("lib_args:read:probing_as_positional:trying", {
"part": part, "part": part,
"argument": argument.toString() "argument": argument.toString(),
}); });
var pattern = ""; var pattern = "";
{ {
@ -5254,12 +5254,12 @@ var lib_plankton;
pattern += pattern_back; pattern += pattern_back;
} }
lib_plankton.log.debug("lib_args:read:probing_as_positional:pattern", { lib_plankton.log.debug("lib_args:read:probing_as_positional:pattern", {
"pattern": pattern "pattern": pattern,
}); });
var regexp = new RegExp(pattern); var regexp = new RegExp(pattern);
var matching = regexp.exec(part); var matching = regexp.exec(part);
lib_plankton.log.debug("lib_args:read:probing_as_positional:matching", { lib_plankton.log.debug("lib_args:read:probing_as_positional:matching", {
"matching": matching "matching": matching,
}); });
if (matching == null) { if (matching == null) {
return false; return false;
@ -5276,7 +5276,7 @@ var lib_plankton;
].some(function (x) { return x(); }); ].some(function (x) { return x(); });
if (!found) { if (!found) {
lib_plankton.log.warning("lib_args:read:could_not_parse", { lib_plankton.log.warning("lib_args:read:could_not_parse", {
"part": part "part": part,
}); });
} }
}); });
@ -5625,8 +5625,8 @@ var lib_plankton;
*/ */
function encode_request(request) { function encode_request(request) {
let request_raw = ""; let request_raw = "";
request_raw += (encode_method(request.method) + " " + request.query + " " + "HTTP/1.1" + linebreak); request_raw += (encode_method(request.method) + " " + request.target + " " + "HTTP/1.1" + linebreak);
request_raw += ("Host: " + request.host + linebreak); // request_raw += ("Host: " + request.host + linebreak);
for (const [key, value] of Object.entries(request.headers)) { for (const [key, value] of Object.entries(request.headers)) {
request_raw += (key + ": " + value + linebreak); request_raw += (key + ": " + value + linebreak);
} }
@ -5641,7 +5641,7 @@ var lib_plankton;
function decode_request(request_raw) { function decode_request(request_raw) {
const lines = request_raw.split(linebreak); const lines = request_raw.split(linebreak);
const first = lines.shift(); const first = lines.shift();
const [method_raw, query, version] = first.split(" "); const [method_raw, target, version] = first.split(" ");
let headers = {}; let headers = {};
while (true) { while (true) {
const line = lines.shift(); const line = lines.shift();
@ -5655,8 +5655,9 @@ var lib_plankton;
} }
const body = lines.join(linebreak); const body = lines.join(linebreak);
const request = { const request = {
"host": headers["Host"], // "host": headers["Host"],
"query": query, // "query": query,
"target": target,
"method": decode_method(method_raw), "method": decode_method(method_raw),
"headers": headers, "headers": headers,
"body": body, "body": body,
@ -5714,9 +5715,18 @@ var lib_plankton;
options = Object.assign({ options = Object.assign({
"timeout": 5.0, "timeout": 5.0,
"follow_redirects": false, "follow_redirects": false,
"implementation": "fetch",
}, options); }, options);
switch (options.implementation) {
default: {
return Promise.reject("invalid implementation: " + options.implementation);
break;
}
case "fetch": {
function core(signal) { function core(signal) {
return (fetch((request.host + request.query), { return (fetch(
// (request.host + request.query),
request.target, {
"method": ((method => { "method": ((method => {
switch (method) { switch (method) {
case http.enum_method.get: return "GET"; case http.enum_method.get: return "GET";
@ -5730,6 +5740,10 @@ var lib_plankton;
})(request.method)), })(request.method)),
"redirect": (options.follow_redirects ? "follow" : "manual"), "redirect": (options.follow_redirects ? "follow" : "manual"),
"signal": (signal ?? undefined), "signal": (signal ?? undefined),
})
.catch((reason) => {
console.info(reason);
return Promise.reject(reason);
}) })
.then((response_raw) => (response_raw.text() .then((response_raw) => (response_raw.text()
.then((body) => Promise.resolve({ .then((body) => Promise.resolve({
@ -5769,6 +5783,46 @@ var lib_plankton;
else { else {
return response; return response;
} }
break;
}
case "http_module": {
const nm_http = require("http");
const nm_https = require("https");
return (new Promise((resolve, reject) => {
const req = (request.target.includes("https")
? nm_https
: nm_http)
.request(request.target, {
"method": request.method,
"headers": request.headers,
}, (res) => {
try {
let response_body = "";
res.setEncoding("utf8");
res.on("data", (chunk) => {
response_body += chunk;
});
res.on("end", () => {
resolve({
"statuscode": res.statusCode,
"headers": res.headers,
"body": response_body,
});
});
}
catch (error) {
reject(error);
}
});
req.on("error", (error) => {
reject(error);
});
req.write(request.body);
req.end();
}));
break;
}
}
} }
http.call = call; http.call = call;
})(http = lib_plankton.http || (lib_plankton.http = {})); })(http = lib_plankton.http || (lib_plankton.http = {}));