read-data-file 2.0.9 → 2.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/package.json +6 -6
- package/rdf.mjs +21 -6
- package/test/cats.json.gz +0 -0
- package/test/cats.ndjson +3 -0
- package/test/cats.yaml.gz +0 -0
- package/test/cats.yaml.gz.base64.txt +3 -0
- package/test/usage.mjs +2 -0
package/README.md
CHANGED
|
@@ -9,6 +9,13 @@ Read data/config files in various formats (parsers list is configurable).
|
|
|
9
9
|
<!--/#echo -->
|
|
10
10
|
|
|
11
11
|
|
|
12
|
+
Usage
|
|
13
|
+
-----
|
|
14
|
+
|
|
15
|
+
see [test/usage.mjs](test/usage.mjs)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
12
19
|
API
|
|
13
20
|
---
|
|
14
21
|
|
|
@@ -17,6 +24,10 @@ This module exports one function:
|
|
|
17
24
|
### readDataFile(path)
|
|
18
25
|
|
|
19
26
|
Return a promise for data read from the file at path `path`.
|
|
27
|
+
If no reader and parser can be determined from config, the promise will be
|
|
28
|
+
rejected with error name `ReadDataFileUnsupportedFext`.
|
|
29
|
+
|
|
30
|
+
|
|
20
31
|
|
|
21
32
|
### readDataFile.cfg(opts)
|
|
22
33
|
|
|
@@ -68,10 +79,7 @@ Default parsers:
|
|
|
68
79
|
|
|
69
80
|
|
|
70
81
|
|
|
71
|
-
Usage
|
|
72
|
-
-----
|
|
73
82
|
|
|
74
|
-
see [test/usage.js](test/usage.js)
|
|
75
83
|
|
|
76
84
|
|
|
77
85
|
<!--#toc stop="scan" -->
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{ "name": "read-data-file",
|
|
2
|
-
"version": "2.0.
|
|
2
|
+
"version": "2.0.11",
|
|
3
3
|
"description": "Read data/config files in various formats (parsers list is configurable).",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"read data file",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"map-assoc-core": "^0.1.3",
|
|
40
40
|
"merge-options": "^3.0.4",
|
|
41
41
|
"p-each-series": "^2.2.0",
|
|
42
|
-
"p-fatal": "^0.1.
|
|
42
|
+
"p-fatal": "^0.1.5",
|
|
43
43
|
"pify": "^5.0.0",
|
|
44
44
|
"safeload-yaml-pmb": "^4.210519.0",
|
|
45
45
|
"strip-bom": "^4.0.0",
|
|
@@ -48,12 +48,12 @@
|
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"absdir": "^1.0.6",
|
|
51
|
-
"equal-pmb": "^0.1.
|
|
52
|
-
"eslint-config-nodejs-pmb": "^0.3.
|
|
51
|
+
"equal-pmb": "^0.1.27",
|
|
52
|
+
"eslint-config-nodejs-pmb": "^0.3.5",
|
|
53
53
|
"eslint-plugin-json-light-pmb": "^1.0.7",
|
|
54
|
-
"eslint-plugin-
|
|
54
|
+
"eslint-plugin-n": "^15.2.4",
|
|
55
55
|
"eslint-pretty-pmb": "^1.0.5",
|
|
56
|
-
"nodemjs": "^0.1.
|
|
56
|
+
"nodemjs": "^0.1.14"
|
|
57
57
|
},
|
|
58
58
|
|
|
59
59
|
|
package/rdf.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// -*- coding: utf-8, tab-width: 2 -*-
|
|
2
2
|
|
|
3
|
-
import pathLib from 'path';
|
|
4
3
|
import nodeFs from 'fs';
|
|
4
|
+
import pathLib from 'path';
|
|
5
|
+
import zlib from 'zlib';
|
|
5
6
|
|
|
6
7
|
import getOwn from 'getown';
|
|
7
8
|
import ifFun from 'if-fun';
|
|
@@ -19,6 +20,10 @@ import tomlLib from 'toml';
|
|
|
19
20
|
import yamlSafeLoad from 'safeload-yaml-pmb';
|
|
20
21
|
|
|
21
22
|
|
|
23
|
+
const readRawFilePr = pify(nodeFs.readFile);
|
|
24
|
+
const decodeGzipBuffer = pify(zlib.gunzip);
|
|
25
|
+
|
|
26
|
+
|
|
22
27
|
function makeReaderFunc() {
|
|
23
28
|
const f = function readDataFile(path) { return f.readFile(path); };
|
|
24
29
|
return f;
|
|
@@ -35,6 +40,15 @@ function dlookup(dict, key) {
|
|
|
35
40
|
function identity(x) { return x; }
|
|
36
41
|
|
|
37
42
|
|
|
43
|
+
async function readGzippedUtf8(path) {
|
|
44
|
+
let d = await readRawFilePr(path);
|
|
45
|
+
d = await decodeGzipBuffer(d);
|
|
46
|
+
d = String(d);
|
|
47
|
+
d = stripBom(d);
|
|
48
|
+
return d;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
38
52
|
const defaultImpl = {
|
|
39
53
|
|
|
40
54
|
// #BEGIN# default opts
|
|
@@ -50,6 +64,7 @@ const defaultImpl = {
|
|
|
50
64
|
|
|
51
65
|
readersByFext: {
|
|
52
66
|
// qar(path) { return this.readFile(path + '.meta.json'); },
|
|
67
|
+
gz: readGzippedUtf8,
|
|
53
68
|
},
|
|
54
69
|
// #ENDOF# default opts
|
|
55
70
|
|
|
@@ -86,16 +101,15 @@ const defaultImpl = {
|
|
|
86
101
|
let parser;
|
|
87
102
|
pathDotParts.forEach(function lookup(dotPart) {
|
|
88
103
|
fext = dotPart + (fext ? '.' + fext : '');
|
|
89
|
-
if (reader === undefined) { reader = dlookup(readersByFext,
|
|
90
|
-
if (parser === undefined) { parser = dlookup(parsersByFext,
|
|
104
|
+
if (reader === undefined) { reader = dlookup(readersByFext, dotPart); }
|
|
105
|
+
if (parser === undefined) { parser = dlookup(parsersByFext, dotPart); }
|
|
91
106
|
});
|
|
92
107
|
if (reader || parser) { return { reader, parser }; }
|
|
93
108
|
return false;
|
|
94
109
|
},
|
|
95
110
|
|
|
96
111
|
async defaultReader(path) {
|
|
97
|
-
const text = stripBom(await
|
|
98
|
-
{ encoding: 'UTF-8' }));
|
|
112
|
+
const text = stripBom(await readRawFilePr(path, { encoding: 'UTF-8' }));
|
|
99
113
|
return text;
|
|
100
114
|
},
|
|
101
115
|
|
|
@@ -120,7 +134,8 @@ const defaultImpl = {
|
|
|
120
134
|
return data;
|
|
121
135
|
} catch (err) {
|
|
122
136
|
err.dataFilePath = path;
|
|
123
|
-
|
|
137
|
+
const trace = 'While reading file ' + JSON.stringify(path) + ': ';
|
|
138
|
+
err.message = trace + err.message;
|
|
124
139
|
throw err;
|
|
125
140
|
}
|
|
126
141
|
},
|
|
Binary file
|
package/test/cats.ndjson
ADDED
|
Binary file
|
package/test/usage.mjs
CHANGED
|
@@ -35,8 +35,10 @@ async function verifyAllFixtures() {
|
|
|
35
35
|
|
|
36
36
|
await verifyOneFixture('cats.ceson');
|
|
37
37
|
await verifyOneFixture('cats.ini');
|
|
38
|
+
await verifyOneFixture('cats.json.gz');
|
|
38
39
|
await verifyOneFixture('cats.json5');
|
|
39
40
|
await verifyOneFixture('cats.toml');
|
|
41
|
+
await verifyOneFixture('cats.yaml.gz');
|
|
40
42
|
await verifyOneFixture('cats.yaml');
|
|
41
43
|
await verifyOneFixture('cats.yml');
|
|
42
44
|
|