read-data-file 2.0.8 → 2.0.10

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 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.8",
2
+ "version": "2.0.10",
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.3",
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.24",
52
- "eslint-config-nodejs-pmb": "^0.3.4",
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-node": "^11.1.0",
54
+ "eslint-plugin-n": "^15.2.4",
55
55
  "eslint-pretty-pmb": "^1.0.5",
56
- "nodemjs": "^0.1.12"
56
+ "nodemjs": "^0.1.14"
57
57
  },
58
58
 
59
59
 
package/rdf.mjs CHANGED
@@ -1,14 +1,15 @@
1
1
  // -*- coding: utf-8, tab-width: 2 -*-
2
2
 
3
- import pathLib from 'path';
4
3
  import nodeFs from 'fs';
5
- import pify from 'pify';
4
+ import pathLib from 'path';
5
+ import zlib from 'zlib';
6
6
 
7
7
  import getOwn from 'getown';
8
8
  import ifFun from 'if-fun';
9
9
  import isStr from 'is-string';
10
10
  import mapObj from 'map-assoc-core';
11
11
  import mergeOpts from 'merge-options';
12
+ import pify from 'pify';
12
13
  import stripBom from 'strip-bom';
13
14
 
14
15
  import cesonParser from 'ceson/parse.js';
@@ -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, fext); }
90
- if (parser === undefined) { parser = dlookup(parsersByFext, fext); }
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 pify(nodeFs.readFile)(path,
98
- { encoding: 'UTF-8' }));
112
+ const text = stripBom(await readRawFilePr(path, { encoding: 'UTF-8' }));
99
113
  return text;
100
114
  },
101
115
 
@@ -114,9 +128,15 @@ const defaultImpl = {
114
128
  const rop = this.findBestReaderAndParser(dotParts);
115
129
  if (!rop) { return this.unsupportedFext(realPath); }
116
130
  const { reader, parser } = rop;
117
- const content = await ifFun(reader, this.defaultReader)(realPath);
118
- const data = await ifFun(parser, identity)(content);
119
- return data;
131
+ try {
132
+ const content = await ifFun(reader, this.defaultReader)(realPath);
133
+ const data = await ifFun(parser, identity)(content);
134
+ return data;
135
+ } catch (err) {
136
+ err.dataFilePath = path;
137
+ err.message += ' — Error occurred while trying to read file ' + path;
138
+ throw err;
139
+ }
120
140
  },
121
141
 
122
142
  },
Binary file
@@ -0,0 +1,3 @@
1
+ { "name": "Cassy", "color": "black" }
2
+ { "name": "Milly", "color": "white" }
3
+ { "name": "Nero", "color": "orange" }
Binary file
@@ -0,0 +1,3 @@
1
+ H4sICMvmI14AA2NhdHMueWFtbABTjXT09VEw1DPiUlbQ1dJVSM5PycxLt1IIDXHTtdBRKElM0i3P
2
+ TCnJsFIwAclz6erqcnE5JxYXV1pxKQBBcn5OfpGVQlJOYnI2F5dvZk4OqkR5RmZJKheXX2pRPop4
3
+ flFiXjpQQk9PjwsAjJGvX4QAAAA=
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