When I started working with nodejs I had to decide if I would work with IBM
I connectivity or more general with nodejs. I feel that IBM I connectivity
is to narrow and also covered by IBM (db2a, XMLservice etc.) and Profound
and I am sure that Niels (System & Method) also has a bid in that area, so
I have decided to work on more general modules to support what becomes a
number of specific business oriented modules and eventual a new powerEXT
framework this time based on nodejs as backend and
Angular/Bootstrap/Material as frontend.



after 45 years with IBM midrange, IBM midrange is no longer “a must”,
but “an option” – even though it is an option I know rather well <<



The first business orientated modules are under development and includes
one of my main areas – EDI, in practice technical formats like X.12,
EDIfact, XML and JSON that are transferred as “business documents” like
“INVOIC” and enveloped in formats like SOAP, X.12 or EDIfact. In general,
this means a lot of mappings of hierarchical data to/from “inhouse” tables
and reading/storing data in textual notations as well as directory handling.



At the same time nodejs are in the birth of embracing promises/await/catch
instead of the OPM with callback’s so I find myself over and over again
tackling the OPM.



So, I have started all over again and embraces promise/await/catch in a
core module in regards to fs and http handling where I have decided to use
express and axios.



I have now my first core module running in beta and I am invite others to
take a look before it eventually becomes a NPM for nodejs version 8.x. …



nodejs fs – 82 original public methods we all know:

F_OK,FileReadStream,FileWriteStream,R_OK,ReadStream,Stats,W_OK,WriteStream,X_OK,_toUnixTimestamp,access,accessSync,appendFile,appendFileSync,chmod,chmodSync,chown,chownSync,close,closeSync,constants,copyFile,copyFileSync,createReadStream,createWriteStream,exists,existsSync,fchmod,fchmodSync,fchown,fchownSync,fdatasync,fdatasyncSync,fstat,fstatSync,fsync,fsyncSync,ftruncate,ftruncateSync,futimes,futimesSync,link,linkSync,lstat,lstatSync,mkdir,mkdirSync,mkdtemp,mkdtempSync,open,openSync,read,readFile,readFileSync,readSync,readdir,readdirSync,readlink,readlinkSync,realpath,realpathSync,rename,renameSync,rmdir,rmdirSync,stat,statSync,symlink,symlinkSync,truncate,truncateSync,unlink,unlinkSync,unwatchFile,utimes,utimesSync,watch,watchFile,write,writeFile,writeFileSync,writeSync



Examples:



// =====================================

ORIGINAL: SYNC - Not good, hangs up other nodejs processes:

res = fs.readFileSync(“myfile.txt”)



// =====================================

ORIGINAL: ASYNC - Welcome to callback hell:

fs.readFile(“myfile.txt”, (err, data) => { <<< try to get any data out of
this inner >>> });



// =====================================

NEW: fs.readFileAwait() - ASYNC with AWAIT without error handling, throws
errors about unhandled promise

res = await fs.readFileAwait(“myfile.txt”)

Typical Console Error and **WARNING** nodejs processing terminates if in
error:

(node:24716) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file
or directory, open 'C:\myfile.txt'

(node:24716) UnhandledPromiseRejectionWarning: Unhandled promise rejection.
This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with
.catch(). (rejection id: 1)

(node:24716) [DEP0018] DeprecationWarning: Unhandled promise rejections are
deprecated. In the future, promise rejections that are not handled will
terminate the Node.js process with a non-zero exit code.

Proper Use:

await fs.readFileAwait("myfile.txt", 'utf8')

.then(function(response) {

console.log(response)

})

.catch(function (error) {

console.error("the readFileAwait function Failed")

})



// =====================================

NEW: fs.readFileCatch() - ASYNC with AWAIT and CATCH error handling, result
is returned undefined if functions fails.

res = await fs.readFileCatch(“myfile.txt”)

Typical Console Error:

Error in (zCoreNv8.zEnhancedFs).readFileCatch()

- Error: ENOENT: no such file or directory, open ‘C:\myfile.txt'

Proper Use:

res = await fs.readFileAwait("myfile.txt", 'utf8')

if (res == undefined) function throwed an error, do your own error handling
– note that processing continues without handling.



nodejs fs - 66 new added public methods:

accessAwait,accessCatch,appendFileAwait,appendFileCatch,chmodAwait,chmodCatch,chownAwait,chownCatch,closeAwait,closeCatch,copyFileAwait,copyFileCatch,existsAwait,existsCatch,fchmodAwait,fchmodCatch,fchownAwait,fchownCatch,fdatasyncAwait,fdatasyncCatch,fstatAwait,fstatCatch,fsyncAwait,fsyncCatch,ftruncateAwait,ftruncateCatch,futimesAwait,futimesCatch,linkAwait,linkCatch,lstatAwait,lstatCatch,mkdirAwait,mkdirCatch,mkdtempAwait,mkdtempCatch,openAwait,openCatch,readFileAwait,readFileCatch,readAwait,readCatch,readdirAwait,readdirCatch,readlinkAwait,readlinkCatch,realpathAwait,realpathCatch,renameAwait,renameCatch,rmdirAwait,rmdirCatch,statAwait,statCatch,symlinkAwait,symlinkCatch,truncateAwait,truncateCatch,unlinkAwait,unlinkCatch,utimesAwait,utimesCatch,writeFileAwait,writeFileCatch,writeAwait,writeCatch



How is it done?

Instead of changing the existing FS module, the new module is a base module
without methods and when called it reads the public methods of FS and
creates/inserts passthrough to the FS modules within it self while it takes
all Sync modules and creates and Promisify a Await method based on the old
Async method of the old Sync method and based on a template then creates a
Catch method of the new Await method.



Not everyday vanilla javascript, but it leaves fs intact and is done in
less than 50 lines – fun to make and I like the idea of programs that
writes themselves just by calling them (don’t try to do that in your IBM i
RPGLE kitchen sink) 😊



Other includes …



Enhance Axios with await getCatch(url, config, result)

Requires: npm install axios


var res = {}

res = await http.getCatch(“https://google.com”;)

or

// run parallel gets ….

var url1 = ”https://google.com”;,

res1 {},

url2 = ”http://ibm.com”;,

res2 {}

await http.all([http.getCatch(url1,{},res1),http.getCatch(url2,{},res2)])

if (res1.success == true) console.log(res1.data)

if (res1.success == false) console.log(res1.errorText)

if (res2.success == true) console.log(res2.data)

if (res2.success == false) console.log(res2.errorText)





Advanced Object Manipulations such as

* - copyDeep(obj), if one really wants to f*ck up an object do it in a deep
copy ;-)

* - stringifyFunctions(obj), conversion of typeof function >
<![FUNCTION[some function]]>

* - evalFunctions(obj), conversion of <![FUNCTION[some function]]> > typeof
function

* - slice(obj, anchor, [log]), slice an object based on dynamic anchoring

* > anchor = "abc|5|def|5"

* > xpath =
"/maybeeSomethingInFront/abc[5]/maybeeSomethingInTheMiddle/def[5]"

* > return = shallow object/array or value (in this case the value of the
fifth element in def)

* > response time in 4,000 node objects (based on 258KB JSON) with dept
up to 12 = in average 0.0085ms that equals to 122,000 reads pr. Sec. on
win10/i7-7700K 4,4Ghz/12% util, in iterated object execution it may bring
down access down to 0.001ms the closer one can bring the fraction of the
object.

* > logging features:

* >>> false (runs optimized version)

* >>> true, log the run through the tree

* >>> xpath, draws a xpath map of the object

* >>> data, documents all elements with data

* >>> dataObj, returns an object with the data-log



If any of the areas has your interest you can preview and grip the code
here ( http://caernacon.com/zCoreNv8.txt ) or I will send you the core
script on a personal mail, if not - “Hakuna Matata” (Swahili for “no
problem” for those of you whom “Timon” and “Pumpa” are strangers ) 😉



As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.