• R/O
  • SSH

fcf-framework-core: Commit

Main functions and classes of the FCF framework


Commit MetaInfo

Révisionca2982a6e51461269aee4e506f1af8202727fed4 (tree)
l'heure2022-12-08 12:58:43
Auteurvmarkin
Commitervmarkin

Message de Log

development

Change Summary

Modification

diff -r 4cba50aa22af -r ca2982a6e514 NDetails/resolver.js
--- a/NDetails/resolver.js Wed Dec 07 09:28:24 2022 +0300
+++ b/NDetails/resolver.js Thu Dec 08 06:58:43 2022 +0300
@@ -2,7 +2,7 @@
22 const libPath = require("path");
33 const libModule = require("module");
44
5- let _gDirectories = { };
5+ let _directories = { };
66
77 module.exports = new (class {
88
@@ -21,19 +21,19 @@
2121 process.env.NODE_PATH = paths.join(":");
2222 }
2323 libModule.Module._initPaths();
24- _gDirectories = {};
24+ _directories = {};
2525 }
2626
2727 resolveModule(a_module){
2828 a_module = a_module.split("\\")[0].split("/")[0];
29- if (!(a_module in _gDirectories)) {
29+ if (!(a_module in _directories)) {
3030 try {
31- _gDirectories[a_module] = libPath.dirname(require.resolve(`${a_module}/package.json`));
31+ _directories[a_module] = libPath.dirname(require.resolve(`${a_module}/package.json`));
3232 } catch(e){
3333 return;
3434 }
3535 }
36- return _gDirectories[a_module];
36+ return _directories[a_module];
3737 }
3838
3939 })();
diff -r 4cba50aa22af -r ca2982a6e514 fcf.js
--- a/fcf.js Wed Dec 07 09:28:24 2022 +0300
+++ b/fcf.js Thu Dec 08 06:58:43 2022 +0300
@@ -1216,8 +1216,6 @@
12161216 }
12171217
12181218
1219- fcf.Exception = function () {};
1220-
12211219 fcf.styleToString = (a_name, a_value) => {
12221220 let result = "";
12231221 if (typeof a_name == "object" && a_name !== null){
@@ -1333,7 +1331,6 @@
13331331 /// - boolean noexcept = false - If the flag is true, then if the callback ends with an error,
13341332 /// the queue execution is not stopped and the handlers passed to catch are not called.
13351333 /// - mixed errorResult = undefined - The result returned by the result() method in case of an error
1336- /// - boolean quiet = false - If true, then raw error messages are not printed to the console.
13371334 /// @example
13381335 /// let actions = new fcf.Actions(async ()=>{
13391336 /// ...
@@ -1667,11 +1664,11 @@
16671664
16681665 /// @method fcf.Actions catch(function a_cb)
16691666 /// @brief Adds an error handler callback.
1670- /// @details When the a_cb function is called, this is set to fcf.Actions object
1671- /// Exceptions thrown in the catch method handler are not handled and are thrown
1667+ /// @details If the callback returns or throws an error object,
1668+ /// then it replaces the current fcf.Actions error
16721669 /// @param function a_cb - Error handler callback
16731670 /// - Has the following sigrature:
1674- /// a_cb(Error a_error)
1671+ /// undefined|Error a_cb(Error a_error)
16751672 /// @result fcf.Actions - Self object
16761673 /// @example
16771674 /// (new fcf.Actions())
@@ -1686,10 +1683,19 @@
16861683 catch(a_cb) {
16871684 if (!this._stack)
16881685 return;
1689- if (a_cb && this._error)
1690- a_cb.call(this, this._error);
1691- else if (a_cb)
1686+ this._flags |= ACTIONS_FLAGS_CATCH;
1687+ if (a_cb && this._error) {
1688+ try {
1689+ let e = a_cb.call(this, this._error);
1690+ if (e instanceof Error) {
1691+ this._error = e;
1692+ }
1693+ } catch(e) {
1694+ this._error = e;
1695+ }
1696+ } else if (a_cb) {
16921697 this._errorcbs.push(a_cb);
1698+ }
16931699 return this;
16941700 }
16951701
@@ -1724,7 +1730,11 @@
17241730 /// });
17251731 finally(a_cb) {
17261732 if (a_cb && this._error) {
1727- a_cb.call(this, undefined, {complete: ()=>{}, error: ()=>{}});
1733+ try {
1734+ a_cb.call(this, undefined, {complete: ()=>{}, error: ()=>{}});
1735+ } catch (e){
1736+ this._error = e;
1737+ }
17281738 } else {
17291739 this._stack.push({cb: a_cb, args: undefined, finally: true, autoComplete: fcf.getParamCount(a_cb) < 2 });
17301740 this._execute();
@@ -1758,10 +1768,14 @@
17581768 if (!arguments.length) {
17591769 return this._error;
17601770 }
1761- if (this._error)
1771+ if (this._error){
1772+ if (a_error) {
1773+ this._error = a_error;
1774+ }
17621775 return this;
1776+ }
17631777 this._error = a_error ? a_error : new Error("Unknown error");
1764- this._callErrors(this._error);
1778+ this._callErrors();
17651779 return this;
17661780 }
17671781
@@ -1864,13 +1878,15 @@
18641878 })
18651879 }
18661880
1867- _callErrors(a_error) {
1868- let catchError;
1881+ _callErrors() {
18691882 for (let i = 0; i < this._errorcbs.length; ++i){
18701883 try {
1871- this._errorcbs[i].call(this, a_error);
1884+ let e = this._errorcbs[i].call(this, this._error);
1885+ if (e instanceof Error) {
1886+ this._error = e;
1887+ }
18721888 } catch(e) {
1873- fcf.log.err("FCF", "An exception was caught in the catch block of the Actions object while handling an error. Error: ", e);
1889+ this._error = e;
18741890 }
18751891 }
18761892 for (let i = 0; i < this._stack.length; ++i){
@@ -1878,14 +1894,17 @@
18781894 try {
18791895 this._stack[i].cb.call(this, undefined, {complete: ()=>{}, error: ()=>{}});
18801896 } catch(e) {
1881- fcf.log.err("FCF", "An exception was caught in the finally block of the Actions object while handling an error. Error: ", e);
1897+ this._error = e;
18821898 }
18831899 }
18841900 }
1885-
18861901 if (!(this._flags & ACTIONS_FLAGS_QUIET)) {
1887- if (!this._errorcbs.length){
1888- fcf.log.err("FCF", "Unhandled error in fcf.Actions (to handle catch method or \"quiet\" flag). Error: ", a_error)
1902+ if (!(this._flags & ACTIONS_FLAGS_CATCH)){
1903+ setTimeout(()=>{
1904+ if (!(this._flags & ACTIONS_FLAGS_CATCH)){
1905+ fcf.log.err("FCF", "Unhandled error in fcf.Actions (to handle catch method or \"quiet\" flag).", this._error)
1906+ }
1907+ }, 0);
18891908 }
18901909 }
18911910 }
@@ -1939,7 +1958,7 @@
19391958 self._flags &= ~ACTIONS_FLAGS_RUN;
19401959 self._result = undefined;
19411960 self._error = a_error ? a_error : new Error("Unknown error");
1942- self._callErrors(self._error);
1961+ self._callErrors();
19431962 },
19441963 };
19451964 let args = [];
@@ -1999,6 +2018,7 @@
19992018 const ACTIONS_FLAGS_NOEXCEPT = 2;
20002019 const ACTIONS_FLAGS_RUN = 4;
20012020 const ACTIONS_FLAGS_QUIET = 8;
2021+ const ACTIONS_FLAGS_CATCH = 16;
20022022
20032023
20042024
@@ -2050,7 +2070,7 @@
20502070 /// <head>
20512071 /// <script src="/node_modules/fcf-framework-core/fcf.js"></script>
20522072 /// <script>
2053- /// // Adding data about the new package "package" to the configuration
2073+ /// // Adding data about the new "package" package to the configuration
20542074 /// fcf.configuration.append({
20552075 /// webModules: {
20562076 ///
@@ -3484,8 +3504,9 @@
34843504 fcf.NDetails.messages[a_messageName] = a_messageText;
34853505 }
34863506
3487- fcf.Exception = class {
3507+ fcf.Exception = class Exception extends Error{
34883508 constructor(a_nameOrMessageOrException, a_args, a_subException) {
3509+ super(a_nameOrMessageOrException);
34893510 let exceptionName;
34903511 let template;
34913512 let stackTxt;
@@ -4248,12 +4269,11 @@
42484269
42494270 for(var i = 2; i < arguments.length; ++i) {
42504271 if (_isServer && typeof arguments[i] == "object"){
4251- if (arguments[i] instanceof Error) {
4252- outputArr.push(arguments[i].toString());
4253- outputArr.push(arguments[i].stack);
4254- } else if (arguments[i] instanceof fcf.Exception){
4272+ if (arguments[i] instanceof fcf.Exception){
42554273 outputArr.push(arguments[i].toString(true, true));
42564274 outputArr.push("\nStack:\n" + fcf.Exception.stackToString(arguments[i].stackArr));
4275+ } else if (arguments[i] instanceof Error) {
4276+ outputArr.push(arguments[i].stack);
42574277 } else {
42584278 outputArr.push(JSON.stringify(arguments[i], 0, 2));
42594279 }
@@ -4438,6 +4458,131 @@
44384458 }
44394459
44404460
4461+
4462+ fcf.parseUrl = (a_url)=> {
4463+ var result = {};
4464+ var sep = a_url.indexOf('?');
4465+
4466+ anchorArr = a_url.split("#");
4467+ a_url = anchorArr[0];
4468+ result.anchor = anchorArr[1];
4469+
4470+ if (sep === -1) {
4471+ result.url = a_url;
4472+ result.referer = a_url;
4473+ result.urlArgs = {};
4474+ } else {
4475+ var rawQuery = a_url.substr(sep+1);
4476+ result.referer = a_url.substr(0, sep);
4477+ result.url = a_url;
4478+ result.urlArgs = {};
4479+ var arr = fcf.str(rawQuery).split('&');
4480+ for(var i = 0; i < arr.length; ++i) {
4481+ var val = arr[i].split('=');
4482+ result.urlArgs[decodeURIComponent(fcf.str(val[0]))] = decodeURIComponent(fcf.str(val[1]));
4483+ }
4484+ }
4485+
4486+ if (result.referer[0] == '/') {
4487+ result.uri = result.referer.substr(1);
4488+ result.server = '';
4489+ result.protocol = '';
4490+ } else {
4491+ var pos = result.referer.indexOf('://');
4492+ if (pos !== -1) {
4493+ var pos2 = result.referer.indexOf('/', pos+3);
4494+ if (pos2 === -1) {
4495+ result.uri = '/';
4496+ result.server = result.referer.substr(pos+3);
4497+ result.protocol = result.referer.substr(0, pos);
4498+ } else {
4499+ result.uri = result.referer.substr(pos2);
4500+ result.server = result.referer.substr(pos+3, pos2 - (pos+3));
4501+ result.protocol = result.referer.substr(0, pos);
4502+ }
4503+ } else {
4504+ result.server = '';
4505+ result.protocol = '';
4506+ result.uri = result.referer;
4507+ }
4508+ }
4509+
4510+ result.args = fcf.append({}, result.urlArgs);
4511+
4512+ let serverArr = result.server.split(":");
4513+ result.server = serverArr[0];
4514+ result.port = serverArr[1];
4515+
4516+ return result;
4517+ }
4518+
4519+ fcf.buildUrl = (a_url, a_args, a_anchor) => {
4520+ var urlInfo = typeof a_url == "object" ? a_url : fcf.parseUrl(a_url);
4521+ a_url = urlInfo.referer;
4522+ a_args = fcf.append({}, urlInfo.urlArgs, a_args);
4523+
4524+ var first = true;
4525+ for(var k in a_args) {
4526+ if (first)
4527+ a_url += a_url.indexOf('?') != -1 ? '&' : '?';
4528+ else
4529+ a_url += '&';
4530+
4531+ a_url += k;
4532+ if (a_args[k] !== null && a_args[k] !== undefined) {
4533+ a_url += '=';
4534+ if (typeof a_args[k] != 'object') {
4535+ a_url += encodeURIComponent(fcf.str(a_args[k]));
4536+ } else {
4537+ a_url += encodeURIComponent(JSON.stringify(a_args[k]));
4538+ }
4539+ }
4540+
4541+ first = false;
4542+ }
4543+
4544+ if (!fcf.empty(a_anchor))
4545+ a_url += "#" + a_anchor;
4546+ else if (!fcf.empty(urlInfo.anchor))
4547+ a_url += "#" + urlInfo.anchor;
4548+
4549+ return a_url;
4550+ }
4551+
4552+ fcf.addException("LOAD_MODULE", "Failed to load JS module @{{module}}@");
4553+ fcf.addException("LOAD_UNITEST_MODULE", "Failed to load fcf-framework-unitest module. Install the missing module to perform testing: $ npm install fcf-framework-unitest");
4554+
4555+ fcf.test = function(a_part, a_group, a_name, a_cbtest){
4556+ if (!_isServer) {
4557+ let route = fcf.parseUrl(window.location.href);
4558+ if (!route.args.fcf_unitest_port || !route.args.fcf_unitest_host){
4559+ return;
4560+ }
4561+ }
4562+ let currentArguments = arguments;
4563+ let loadModule = false;
4564+ return fcf.actions()
4565+ .then(()=>{
4566+ return fcf.require("fcf-framework-unitest", {showError: false});
4567+ })
4568+ .catch((a_error)=>{
4569+ if (!loadModule) {
4570+ return new fcf.Exception("LOAD_UNITEST_MODULE", a_error);
4571+ }
4572+ })
4573+ .then(([unitest]) => {
4574+ if (!unitest) {
4575+ throw new fcf.Exception("LOAD_MODULE", {module: "fcf-framework-unitest"});
4576+ }
4577+ loadModule = true;
4578+ unitest.add.apply(unitest, currentArguments);
4579+ })
4580+ .catch((a_error)=>{
4581+ fcf.log.err("unitest", a_error);
4582+ });
4583+ }
4584+
4585+
44414586 fcf.Configuration = class {
44424587 constructor(a_options) {
44434588 this.moduleDirectories = [];
Afficher sur ancien navigateur de dépôt.