@ezs/basics 2.9.2 → 2.10.1

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
@@ -19,6 +19,7 @@ npm install @ezs/basics
19
19
  * [CSVObject](#csvobject)
20
20
  * [CSVParse](#csvparse)
21
21
  * [CSVString](#csvstring)
22
+ * [DataURLParse](#dataurlparse)
22
23
  * [FILELoad](#fileload)
23
24
  * [FILEMerge](#filemerge)
24
25
  * [FILESave](#filesave)
@@ -215,6 +216,16 @@ a;b;c
215
216
 
216
217
  Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
217
218
 
219
+ ### DataURLParse
220
+
221
+ Take an URL `String`, parse it as a data URLs, return `Object`.
222
+
223
+ See:
224
+
225
+ * <https://developer.mozilla.org/fr/docs/Web/URI/Reference/Schemes/data>
226
+
227
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**&#x20;
228
+
218
229
  ### FILELoad
219
230
 
220
231
  Take `Object` containing filename et throw content by chunk
@@ -316,7 +327,7 @@ Output:
316
327
  #### Parameters
317
328
 
318
329
  * `location` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Directory location (optional, default `TMPDIR`)
319
- * `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** File name
330
+ * `identifier` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** File name (if not set, it will be generated automatically)
320
331
  * `content` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Content to save instead of using input object
321
332
  * `jsonl` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Save as json line (optional, default `false`)
322
333
  * `compress` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Enable gzip compression (optional, default `false`)
@@ -742,6 +753,7 @@ Or if no target is specified, the output will be the returned content of URL.
742
753
  * `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** if present select value to send (by POST)
743
754
  * `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** choose the key to set
744
755
  * `json` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** parse as JSON the content of URL (optional, default `false`)
756
+ * `dataurl` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** encode content into DATA Url (optional, default `false`)
745
757
  * `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** timeout in milliseconds (optional, default `1000`)
746
758
  * `mimetype` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** mimetype for value of path (if presents) (optional, default `"application/json"`)
747
759
  * `noerror` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** ignore all errors, the target field will remain undefined (optional, default `false`)
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = DataURLParse;
7
+ var _url = require("url");
8
+ /**
9
+ * Take an URL `String`, parse it as a data URLs, return `Object`.
10
+ *
11
+ * See:
12
+ *
13
+ * - {@link https://developer.mozilla.org/fr/docs/Web/URI/Reference/Schemes/data}
14
+ *
15
+ * @name DataURLParse
16
+ * @returns {Object}
17
+ */
18
+ function DataURLParse(data, feed) {
19
+ if (this.isLast()) {
20
+ return feed.close();
21
+ }
22
+ const parts = String(data).split(',');
23
+ if (parts.length < 2) {
24
+ return feed.send(new Error('Invalid Data URL'));
25
+ }
26
+ return feed.send(Buffer.from(parts[1], 'base64'));
27
+ }
package/lib/file-save.js CHANGED
@@ -12,6 +12,7 @@ var _pathExists = _interopRequireDefault(require("path-exists"));
12
12
  var _makeDir = _interopRequireDefault(require("make-dir"));
13
13
  var _streamWrite = _interopRequireDefault(require("stream-write"));
14
14
  var _debug = _interopRequireDefault(require("debug"));
15
+ var _uuidRandom = _interopRequireDefault(require("uuid-random"));
15
16
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
17
  const eol = '\n';
17
18
  const toJSONL = line => JSON.stringify(line).concat(eol);
@@ -46,7 +47,7 @@ const toJSONL = line => JSON.stringify(line).concat(eol);
46
47
  *
47
48
  * @name FILESave
48
49
  * @param {String} [location=TMPDIR] Directory location
49
- * @param {String} [identifier] File name
50
+ * @param {String} [identifier] File name (if not set, it will be generated automatically)
50
51
  * @param {String} [content] Content to save instead of using input object
51
52
  * @param {Boolean} [jsonl=false] Save as json line
52
53
  * @param {Boolean} [compress=false] Enable gzip compression
@@ -55,7 +56,7 @@ const toJSONL = line => JSON.stringify(line).concat(eol);
55
56
  */
56
57
  function FILESave(data, feed) {
57
58
  if (this.isFirst()) {
58
- const identifier = String(this.getParam('identifier'));
59
+ const identifier = String(this.getParam('identifier', (0, _uuidRandom.default)()));
59
60
  const location = _path.default.normalize(this.getParam('location', (0, _os.tmpdir)()));
60
61
  const compress = this.getParam('compress', false);
61
62
  const flags = Boolean(this.getParam('append', false)) ? 'a' : 'w';
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ var _urlPagination = _interopRequireDefault(require("./url-pagination"));
28
28
  var _urlString = _interopRequireDefault(require("./url-string"));
29
29
  var _urlStream = _interopRequireDefault(require("./url-stream"));
30
30
  var _urlConnect = _interopRequireDefault(require("./url-connect"));
31
+ var _dataurlParse = _interopRequireDefault(require("./dataurl-parse"));
31
32
  var _txtZip = _interopRequireDefault(require("./txt-zip"));
32
33
  var _zipExtract = _interopRequireDefault(require("./zip-extract"));
33
34
  var _tarExtract = _interopRequireDefault(require("./tar-extract"));
@@ -62,6 +63,7 @@ const funcs = {
62
63
  URLString: _urlString.default,
63
64
  URLStream: _urlStream.default,
64
65
  URLConnect: _urlConnect.default,
66
+ DataURLParse: _dataurlParse.default,
65
67
  TXTZip: _txtZip.default,
66
68
  ZIPExtract: _zipExtract.default,
67
69
  TARExtract: _tarExtract.default,
package/lib/url-fetch.js CHANGED
@@ -11,6 +11,8 @@ var _parseHeaders = _interopRequireDefault(require("parse-headers"));
11
11
  var _asyncRetry = _interopRequireDefault(require("async-retry"));
12
12
  var _request = _interopRequireDefault(require("./request"));
13
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
+ const createObjectURL = (arrayBuffer, mimeType = 'application/octet-stream') => `data:${mimeType};base64,${Buffer.from(arrayBuffer).toString('base64')}`;
15
+
14
16
  /**
15
17
  * Add a new field to an `Object`, with the returned content of URL.
16
18
  *
@@ -21,6 +23,7 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
21
23
  * @param {String} [path] if present select value to send (by POST)
22
24
  * @param {String} [target] choose the key to set
23
25
  * @param {String} [json=false] parse as JSON the content of URL
26
+ * @param {String} [dataurl=false] encode content into DATA Url
24
27
  * @param {Number} [timeout=1000] timeout in milliseconds
25
28
  * @param {String} [mimetype="application/json"] mimetype for value of path (if presents)
26
29
  * @param {Boolean} [noerror=false] ignore all errors, the target field will remain undefined
@@ -31,10 +34,11 @@ async function URLFetch(data, feed) {
31
34
  if (this.isLast()) {
32
35
  return feed.close();
33
36
  }
34
- const url = this.getParam('url');
37
+ const location = this.getParam('url');
35
38
  const path = this.getParam('path');
36
39
  const target = [].concat(this.getParam('target')).filter(Boolean).shift();
37
40
  const json = Boolean(this.getParam('json', false));
41
+ const dataurl = Boolean(this.getParam('dataurl', false));
38
42
  const retries = Number(this.getParam('retries', 5));
39
43
  const noerror = Boolean(this.getParam('noerror', false));
40
44
  const timeout = Number(this.getParam('timeout')) || 1000;
@@ -57,9 +61,16 @@ async function URLFetch(data, feed) {
57
61
  (0, _lodash.set)(parameters, 'headers.content-type', mimetype);
58
62
  }
59
63
  try {
60
- const response = await (0, _asyncRetry.default)((0, _request.default)(url, parameters), options);
61
- const func = json ? 'json' : 'text';
62
- const value = await response[func]();
64
+ const response = await (0, _asyncRetry.default)((0, _request.default)(location, parameters), options);
65
+ let value;
66
+ if (json) {
67
+ value = await response.json();
68
+ } else if (dataurl) {
69
+ const content = await response.arrayBuffer();
70
+ value = createObjectURL(content, response.headers.get('content-type'));
71
+ } else {
72
+ value = await response.text();
73
+ }
63
74
  if (target) {
64
75
  const result = typeof data === 'object' ? {
65
76
  ...data
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ezs/basics",
3
3
  "description": "Basics statements for EZS",
4
- "version": "2.9.2",
4
+ "version": "2.10.1",
5
5
  "author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
6
6
  "bugs": "https://github.com/Inist-CNRS/ezs/issues",
7
7
  "dependencies": {
@@ -26,6 +26,7 @@
26
26
  "tar-stream": "3.1.6",
27
27
  "tmp-filepath": "2.0.0",
28
28
  "unzipper": "0.10.11",
29
+ "uuid-random": "1.3.2",
29
30
  "xml-mapping": "1.7.2",
30
31
  "xml-splitter": "1.2.1"
31
32
  },
@@ -35,7 +36,7 @@
35
36
  "directories": {
36
37
  "test": "test"
37
38
  },
38
- "gitHead": "6f6620a54a9c64ddb2ff0fe37afabd883ff7b6d3",
39
+ "gitHead": "f44fd79c0bce656a8ae1453ba36b7470a448c1b2",
39
40
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
40
41
  "keywords": [
41
42
  "ezs"