I have a NodeJs (14 & 18) service which list and read files from the local filesystem, on IBMi or AS/400...
I use:
await fsPromises.readdir(mydir)
for to list all files of my directoryawait fsPromises.readFile(filename)
for to read the content...
For filenames without accent, there is no problem... But, when there are some french accents, I have several problems:
readdir(mydir)
return name_��.png instead of name_éè.png ... But I resolve this first problem with the encoding parameter, andreaddir(mydir, {encoding: 'binary'})
return the good name !calling
await fsPromises.readFile('name_éè.png')
orawait fsPromises.readFile('name_éè.png', {encoding: 'binary'})
causes an access exception: name_��.png: ENOENT: no such file or directory
It seems that the "encoding" option only applies to the result, and not to the filename parameter. This problem does not seem to exist on a Windows machine, but seems specific to AS/400.
Context: Software version: tested with NodeJS v 14.21.3, and OS400 V7R3
Usually it is started via a CL which contains the command
CMD(QSH CMD(&CMD)) JOB(&JOB) JOBQ(QUSRNOMAX) CCSID(1147) SPLFACN(*DETACH)
where &CMD is the path of the SH script ( node app.js &
)
and &JOB is the service's name...
I also tested it whithout the CCSID(1147) parameter, but I had the same problem... (finally, it doesn't mean anything because it's the system default...)
Any idea to resolve this ? Thanks
I finally solved my problem: it's a problem of character encoding difference between Node and the AS/400 job environment:
in Node, all strings are utf8 encoded
but OS400 is linked to the CCSID (1147 in my case) of the execution environment (of the job, of the user profile, etc.) and therefore the names of utf8 files must be converted as soon as they are transmitted to the "fs" primitives or "fsPromises"...
Concretely:
For read the list of elements of a directory, the result must also be re encoded:
NOTE: it doesn't run for some API like
await Jimp.read(convertFilenameCharset(filepath))
because they don't support Buffer parameter like the "fs" API: in those cases, I use the normalize-diacritics module to generate a new filename without accents, and I do a work copy of the file source...