nv-file-reader
v1.0.7
Published
nv-file-reader ============== - simple async wrap of node fs.createReadStream AND readdir - walk , generator, read-file, read-symbol-link
Downloads
3
Readme
nv-file-reader
- simple async wrap of node fs.createReadStream AND readdir
- walk , generator, read-file, read-symbol-link
install
- npm install nv-file-reader
usage
const {rfile,rwalk,rfltr} = require("nv-file-reader")
example
rfile
> await rfile.rstr("./package.json")
'{\n' +
' "dependencies": {\n' +
' "nv-facutil-promise": "^1.0.29",\n' +
' "nv-file-basic": "^1.0.5",\n' +
' "nv-path-basic": "^1.0.1"\n' +
' }\n' +
'}\n'
>
>
var ag = rfile.agen_str("./package.json",20)
var sarr =[]
for await(let each of ag) {sarr.push(each)}
/*
> sarr
[
'{\n "dependencies": ',
'{\n "nv-facutil-pr',
'omise": "^1.0.29",\n ',
' "nv-file-basic": ',
'"^1.0.5",\n "nv-pa',
'th-basic": "^1.0.1"\n',
' }\n}\n'
]
>
*/
nv-file-reader# ln -s package.json s0
nv-file-reader# ln -s s0 s1
> await rfile.rstr("./s1")
'{\n' +
' "dependencies": {\n' +
' "nv-facutil-promise": "^1.0.29",\n' +
' "nv-file-basic": "^1.0.5",\n' +
' "nv-path-basic": "^1.0.1"\n' +
' }\n' +
'}\n'
>
//fs.writeFileSync("123.set",v8.serialize(new Set([1,2,new Date()])))
> await robj("./123.set")
Set(3) { 1, 2, 2022-03-06T03:04:29.438Z }
>
rwalk
treat symbolic_link as leaf
nv-file-reader# mkdir TEST
nv-file-reader# cd TEST/
TEST# touch txt
TEST# ln -s txt s0
TEST# ln -s s0 s1
TEST# mkdir subdir
TEST# cd subdir/
subdir# touch json
subdir# ln -s ../subdir s10
subdir# cd ../
TEST# cd ../
nv-file-reader# tree TEST/
TEST/
├── s0 -> txt
├── s1 -> s0
├── subdir
│ ├── json
│ └── s10 -> ../subdir
└── txt
ag = rwalk.agen("./TEST")
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/s0'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/s1'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/txt'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir/json'
> (await ag.next()).value.path
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir/s10'
var with_full_info = true
ag = rwalk.agen("./TEST",with_full_info)
/*
> ((await ag.next()).value.stat)
Stat {
dev: 2050,
mode: 16877,
nlink: 3,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 696986,
size: 4096,
blocks: 8,
atimeMs: 1646017500405.9639,
mtimeMs: 1646017491345.81,
ctimeMs: 1646017491345.81,
birthtimeMs: 1646017381243.771,
atime: 2022-02-28T03:05:00.406Z,
mtime: 2022-02-28T03:04:51.346Z,
ctime: 2022-02-28T03:04:51.346Z,
birthtime: 2022-02-28T03:03:01.244Z,
permission: Permission {
owner_can_read: true,
owner_can_write: true,
owner_can_exec: true,
group_can_read: true,
group_can_write: false,
group_can_exec: true,
other_can_read: true,
other_can_write: false,
other_can_exec: true
},
type: 'dir'
}
> ((await ag.next()).value.stat)
Stat {
dev: 2050,
mode: 41471,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: 4096,
ino: 692004,
size: 3,
blocks: 0,
atimeMs: 1646017484881.6992,
mtimeMs: 1646017470829.4548,
ctimeMs: 1646017470829.4548,
birthtimeMs: 1646017470829.4548,
atime: 2022-02-28T03:04:44.882Z,
mtime: 2022-02-28T03:04:30.829Z,
ctime: 2022-02-28T03:04:30.829Z,
birthtime: 2022-02-28T03:04:30.829Z,
permission: Permission {
owner_can_read: true,
owner_can_write: true,
owner_can_exec: true,
group_can_read: true,
group_can_write: true,
group_can_exec: true,
other_can_read: true,
other_can_write: true,
other_can_exec: true
},
type: 'slink'
}
>
.....
*/
resolve symbolic-link recursively
nv-file-reader# mkdir TEST-ANOTHER
nv-file-reader# cd TEST-ANOTHER
TEST-ANOTHER# mkfifo fifo
TEST-ANOTHER# touch some.js
TEST-ANOTHER# ln -s ../TEST/subdir ptr
TEST-ANOTHER# touch noexist
TEST-ANOTHER# ln -s noexist s0
TEST-ANOTHER# ln -s s0 s1
TEST-ANOTHER# rm noexist
TEST-ANOTHER# ls -l
total 0
prw-r--r-- 1 root root 0 Feb 28 03:13 fifo
lrwxrwxrwx 1 root root 14 Feb 28 03:14 ptr -> ../TEST/subdir
lrwxrwxrwx 1 root root 7 Feb 28 03:21 s0 -> noexist
lrwxrwxrwx 1 root root 2 Feb 28 03:21 s1 -> s0
-rw-r--r-- 1 root root 0 Feb 28 03:13 some.js
TEST-ANOTHER# cd ../
nv-file-reader# cd TEST
TEST# ln -s noexist noexist
TEST# cd subdir/
subdir# ln -s ../../TEST-ANOTHER/ ptr-to-grandfather
subdir# cd ../
subdir# cd ../
TEST# ls -l
total 4
lrwxrwxrwx 1 root root 7 Feb 28 03:24 noexist -> noexist
lrwxrwxrwx 1 root root 3 Feb 28 03:04 s0 -> txt
lrwxrwxrwx 1 root root 2 Feb 28 03:04 s1 -> s0
drwxr-xr-x 2 root root 4096 Feb 28 03:24 subdir
-rw-r--r-- 1 root root 0 Feb 28 03:04 txt
TEST#
nv-file-reader# tree TEST
TEST
├── noexist -> noexist
├── s0 -> txt
├── s1 -> s0
├── subdir
│ ├── json
│ ├── ptr-to-grandfather -> ../../TEST-ANOTHER/
│ └── s10 -> ../subdir
└── txt
3 directories, 5 files
nv-file-reader# tree TEST-ANOTHER/
TEST-ANOTHER/
├── fifo
├── ptr -> ../TEST/subdir
├── s0 -> noexist
├── s1 -> s0
└── some.js
var ag = rwalk.agen_dig_softlink("./TEST")
async function tst() {
let arr = []
for await(let each of ag) {
arr.push([each.path,each.type])
}
return(arr)
}
> Object.fromEntries(rslts)
{
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST': 'dir',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/noexist': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/s0': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/s1': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir': 'dir',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/txt': 'file',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir/json': 'file',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir/ptr-to-grandfather': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST/subdir/s10': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER': 'dir',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/fifo': 'fifo',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/ptr': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/s0': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/s1': 'slink',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/some.js': 'file',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST-ANOTHER/noexist': 'noexist'
}
>
> rslts.length
16
>
handle circular
nv-file-reader# tree TEST2/
TEST2/
└── circular -> ../TEST3/circular
nv-file-reader# tree TEST3
TEST3
└── circular -> ../TEST2/circular
> ag = rwalk.agen_dig_softlink("./TEST2/circular")
Object [AsyncGenerator] {}
> await ag.next()
{
value: _Ele {
ino: 692002,
path: '/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST2/circular',
type: 'slink',
'#children': []
},
done: false
}
> await ag.next()
{
value: _Ele {
ino: 692064,
path: '/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/TEST3/circular',
type: 'slink',
'#children': []
},
done: false
}
> await ag.next()
{ value: undefined, done: true }
hard link
- this is slow, coz it cmp ino between each other
const {
find_hard_link,
find_hard_link_dig_softlink
} = require("nv-file-reader").hlink;
nv-file-reader# mkdir HARD
nv-file-reader#
nv-file-reader# cd HARD/
HARD#
HARD# touch a
HARD# mkdir sub0
HARD# mkdir sub1
HARD# ln a sub0/hard
HARD# ln a sub1/hard
HARD#
nv-file-reader# tree HARD
HARD
├── a
├── sub0
│ └── hard
└── sub1
└── hard
2 directories, 3 files
/*
> await find_hard_link("./HARD")
{
'692002': [
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/HARD/a',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/HARD/sub0/hard',
'/opt/JS/NV5_/nv-file-/pkgs/nv-file-reader/HARD/sub1/hard'
]
}
>
*/
APIS
rfile
> async
{
_rbytes: [AsyncFunction: _rbytes],
_agen_bytes: [AsyncGeneratorFunction: _agen_bytes],
_rstr: [AsyncFunction: _rstr],
_agen_str: [AsyncGeneratorFunction: _agen_str],
_rjson: [AsyncFunction: _rjson],
rbytes: [AsyncFunction: rbytes],
agen_bytes: [AsyncGeneratorFunction: agen_bytes],
rstr: [AsyncFunction: rstr],
agen_str: [AsyncGeneratorFunction: agen_str],
rjson: [AsyncFunction: rjson]
robj: [AsyncFunction: robj]
}
>
rwalk
{
creat_ele: [AsyncFunction: creat_ele],
agen: [AsyncGeneratorFunction: agen],
agen_dig_softlink: [AsyncGeneratorFunction: agen_dig_softlink],
walk,
walk_dig_softlink
}
rfltr
{
filter: [AsyncFunction: filter],
filter_dig_softlink: [AsyncFunction: filter_dig_softlink],
by_type: [AsyncFunction: by_type],
dir: [AsyncFunction: dir],
file: [AsyncFunction: file],
char_dev: [AsyncFunction: char_dev],
blk_dev: [AsyncFunction: blk_dev],
fifo: [AsyncFunction: fifo],
sock: [AsyncFunction: sock],
slink: [AsyncFunction: slink],
noexist: [AsyncFunction: noexist],
valid_slink: [AsyncFunction: valid_slink],
invalid_slink: [AsyncFunction: invalid_slink]
}
hlink
{
find_hard_link: [AsyncFunction: find_hard_link],
find_hard_link_dig_softlink: [AsyncFunction: find_hard_link_dig_softlink]
}
PERFORMANCE
- bad
async function tst(fn) {
let now = (new Date).getTime()
let rslts = await walk_dig_softlink(fn,false,true)
console.log((new Date).getTime() - now)
return(rslts)
}
> var rslts = await tst("/dev")
308
> var rslts = await tst("/dev")
121
> var rslts = await tst("/dev")
157
> var rslts = await tst("/dev")
148
> var rslts = await tst("/dev")
131
> var rslts = await tst("/root")
37109
> var linux = await tst("/")
// very long time.....
> linux.length
1608673
fs.writeFileSync("linux-path.json",JSON.stringify(linux))
LICENSE
- ISC