@ezs/basics 2.1.3 → 2.3.0
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/CHANGELOG.md +22 -0
- package/README.md +33 -3
- package/lib/index.js +3 -0
- package/lib/tar-extract.js +90 -0
- package/lib/txt-inflection.js +3 -1
- package/lib/txt-sentences.js +2 -0
- package/package.json +3 -2
- package/lib/fetch.js +0 -74
- package/lib/obj-columns.js +0 -53
- package/lib/skos-object.js +0 -73
- package/lib/url-pager.js +0 -179
- package/lib/utils.js +0 -18
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.3.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@2.2.0...@ezs/basics@2.3.0) (2023-06-23)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **strings:** Add inflection and sentences statements ([67ba36c](https://github.com/Inist-CNRS/ezs/commit/67ba36c8ba68c1798f5430a087172fed45d2b3c0))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [2.2.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@2.1.3...@ezs/basics@2.2.0) (2023-06-22)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* 🎸 add tar-extract ([a471aca](https://github.com/Inist-CNRS/ezs/commit/a471aca64b7ab08d92237917264a23080a2c3072))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
6
28
|
## [2.1.3](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@2.1.2...@ezs/basics@2.1.3) (2023-05-30)
|
|
7
29
|
|
|
8
30
|
**Note:** Version bump only for package @ezs/basics
|
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ npm install @ezs/basics
|
|
|
27
27
|
- [OBJFlatten](#objflatten)
|
|
28
28
|
- [OBJNamespaces](#objnamespaces)
|
|
29
29
|
- [OBJStandardize](#objstandardize)
|
|
30
|
+
- [TARExtract](#tarextract)
|
|
30
31
|
- [TXTConcat](#txtconcat)
|
|
31
32
|
- [TXTInflection](#txtinflection)
|
|
32
33
|
- [TXTObject](#txtobject)
|
|
@@ -536,6 +537,27 @@ Output:
|
|
|
536
537
|
|
|
537
538
|
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
|
538
539
|
|
|
540
|
+
### TARExtract
|
|
541
|
+
|
|
542
|
+
Take the content of a tar file, extract some files.
|
|
543
|
+
The JSON object is sent to the output stream for each file.
|
|
544
|
+
It returns to the output stream
|
|
545
|
+
|
|
546
|
+
```json
|
|
547
|
+
{
|
|
548
|
+
"id": "file name",
|
|
549
|
+
"value": "file contents"
|
|
550
|
+
}
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
#### Parameters
|
|
554
|
+
|
|
555
|
+
- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Regex to select the files to extract (optional, default `"**\/*.json"`)
|
|
556
|
+
- `json` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Parse as JSON the content of each file (optional, default `true`)
|
|
557
|
+
- `compress` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Enable gzip compression (optional, default `false`)
|
|
558
|
+
|
|
559
|
+
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<{id: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String), value: [String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)}>**
|
|
560
|
+
|
|
539
561
|
### TXTConcat
|
|
540
562
|
|
|
541
563
|
Concatenate all `String` items into one string
|
|
@@ -560,9 +582,6 @@ Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
|
|
|
560
582
|
|
|
561
583
|
### TXTInflection
|
|
562
584
|
|
|
563
|
-
- **See: <https://www.npmjs.com/package/inflection>
|
|
564
|
-
**
|
|
565
|
-
|
|
566
585
|
Take a `String` and inflect it with or more transformers from this list
|
|
567
586
|
pluralize, singularize, camelize, underscore, humanize, capitalize,
|
|
568
587
|
dasherize, titleize, demodulize, tableize, classify, foreign_key, ordinalize
|
|
@@ -595,6 +614,12 @@ Output:
|
|
|
595
614
|
|
|
596
615
|
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
|
|
597
616
|
|
|
617
|
+
**Meta**
|
|
618
|
+
|
|
619
|
+
- **deprecated**: see <https://inist-cnrs.github.io/ezs/#/plugin-strings?id=inflection>
|
|
620
|
+
see <https://www.npmjs.com/package/inflection>
|
|
621
|
+
|
|
622
|
+
|
|
598
623
|
### TXTObject
|
|
599
624
|
|
|
600
625
|
Take an array of values and generate an array containing objects with the
|
|
@@ -662,6 +687,11 @@ Output:
|
|
|
662
687
|
|
|
663
688
|
Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>**
|
|
664
689
|
|
|
690
|
+
**Meta**
|
|
691
|
+
|
|
692
|
+
- **deprecated**: see <https://inist-cnrs.github.io/ezs/#/plugin-strings?id=sentences>
|
|
693
|
+
|
|
694
|
+
|
|
665
695
|
### TXTZip
|
|
666
696
|
|
|
667
697
|
Take a `String` and zip it.
|
package/lib/index.js
CHANGED
|
@@ -59,6 +59,8 @@ var _txtZip = _interopRequireDefault(require("./txt-zip"));
|
|
|
59
59
|
|
|
60
60
|
var _zipExtract = _interopRequireDefault(require("./zip-extract"));
|
|
61
61
|
|
|
62
|
+
var _tarExtract = _interopRequireDefault(require("./tar-extract"));
|
|
63
|
+
|
|
62
64
|
var _iniString = _interopRequireDefault(require("./ini-string"));
|
|
63
65
|
|
|
64
66
|
var _fileSave = _interopRequireDefault(require("./file-save"));
|
|
@@ -95,6 +97,7 @@ const funcs = {
|
|
|
95
97
|
URLConnect: _urlConnect.default,
|
|
96
98
|
TXTZip: _txtZip.default,
|
|
97
99
|
ZIPExtract: _zipExtract.default,
|
|
100
|
+
TARExtract: _tarExtract.default,
|
|
98
101
|
INIString: _iniString.default,
|
|
99
102
|
FILESave: _fileSave.default,
|
|
100
103
|
FILELoad: _fileLoad.default,
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = TARExtract;
|
|
7
|
+
|
|
8
|
+
var _tarStream = _interopRequireDefault(require("tar-stream"));
|
|
9
|
+
|
|
10
|
+
var _micromatch = _interopRequireDefault(require("micromatch"));
|
|
11
|
+
|
|
12
|
+
var _zlib = require("zlib");
|
|
13
|
+
|
|
14
|
+
var _getStream = _interopRequireDefault(require("get-stream"));
|
|
15
|
+
|
|
16
|
+
var _streamWrite = _interopRequireDefault(require("stream-write"));
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Take the content of a tar file, extract some files.
|
|
22
|
+
* The JSON object is sent to the output stream for each file.
|
|
23
|
+
* It returns to the output stream
|
|
24
|
+
*
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "id": "file name",
|
|
28
|
+
* "value": "file contents"
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @name TARExtract
|
|
33
|
+
* @param {String} [path="**\/*.json"] Regex to select the files to extract
|
|
34
|
+
* @param {String} [json=true] Parse as JSON the content of each file
|
|
35
|
+
* @param {Boolean} [compress=false] Enable gzip compression
|
|
36
|
+
* @returns {{id: String, value: String}[]}
|
|
37
|
+
*/
|
|
38
|
+
function TARExtract(data, feed) {
|
|
39
|
+
const filesPatern = this.getParam('path', '**/*.json');
|
|
40
|
+
|
|
41
|
+
if (this.isFirst()) {
|
|
42
|
+
const {
|
|
43
|
+
ezs
|
|
44
|
+
} = this;
|
|
45
|
+
const json = this.getParam('json', true);
|
|
46
|
+
const compress = this.getParam('compress', false);
|
|
47
|
+
this.input = ezs.createStream(ezs.objectMode());
|
|
48
|
+
this.output = ezs.createStream(ezs.objectMode());
|
|
49
|
+
|
|
50
|
+
const extract = _tarStream.default.extract();
|
|
51
|
+
|
|
52
|
+
this.whenEnd = new Promise((resolve, reject) => {
|
|
53
|
+
extract.on('entry', async (header, stream, next) => {
|
|
54
|
+
if (_micromatch.default.isMatch(header.name, filesPatern)) {
|
|
55
|
+
const contentRaw = await (0, _getStream.default)(stream);
|
|
56
|
+
|
|
57
|
+
if (json) {
|
|
58
|
+
const contentJson = JSON.parse(contentRaw);
|
|
59
|
+
return (0, _streamWrite.default)(this.output, contentJson, () => next());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (0, _streamWrite.default)(this.output, {
|
|
63
|
+
id: header.name,
|
|
64
|
+
value: contentRaw
|
|
65
|
+
}, () => next());
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return next();
|
|
69
|
+
});
|
|
70
|
+
extract.on('error', reject);
|
|
71
|
+
extract.on('finish', resolve);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
if (compress) {
|
|
75
|
+
this.input.pipe((0, _zlib.createGunzip)()).pipe(extract);
|
|
76
|
+
} else {
|
|
77
|
+
this.input.pipe(extract);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.whenFinish = feed.flow(this.output);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (this.isLast()) {
|
|
84
|
+
this.whenEnd.finally(() => this.output.end());
|
|
85
|
+
this.whenFinish.finally(() => feed.close());
|
|
86
|
+
this.input.end();
|
|
87
|
+
} else {
|
|
88
|
+
(0, _streamWrite.default)(this.input, data, () => feed.end());
|
|
89
|
+
}
|
|
90
|
+
}
|
package/lib/txt-inflection.js
CHANGED
|
@@ -56,7 +56,9 @@ const TXTInflection = (data, feed, ctx) => {
|
|
|
56
56
|
* @param {String} [path="value"] path of the field to segment
|
|
57
57
|
* @param {String} [transform] name of a transformer
|
|
58
58
|
* @returns {String[]}
|
|
59
|
-
* @
|
|
59
|
+
* @deprecated
|
|
60
|
+
* see https://inist-cnrs.github.io/ezs/#/plugin-strings?id=inflection
|
|
61
|
+
* see https://www.npmjs.com/package/inflection
|
|
60
62
|
*/
|
|
61
63
|
|
|
62
64
|
|
package/lib/txt-sentences.js
CHANGED
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.
|
|
4
|
+
"version": "2.3.0",
|
|
5
5
|
"author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Inist-CNRS/ezs/issues",
|
|
7
7
|
"dependencies": {
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"parse-headers": "2.0.4",
|
|
29
29
|
"path-exists": "4.0.0",
|
|
30
30
|
"stream-write": "1.0.1",
|
|
31
|
+
"tar-stream": "3.1.4",
|
|
31
32
|
"tmp-filepath": "2.0.0",
|
|
32
33
|
"unzipper": "0.10.11",
|
|
33
34
|
"xml-mapping": "1.7.2",
|
|
@@ -40,7 +41,7 @@
|
|
|
40
41
|
"directories": {
|
|
41
42
|
"test": "test"
|
|
42
43
|
},
|
|
43
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "d363643a24747f3fd2230deb8d3eb68aa313369d",
|
|
44
45
|
"homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
|
|
45
46
|
"keywords": [
|
|
46
47
|
"ezs"
|
package/lib/fetch.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = fetch;
|
|
7
|
-
|
|
8
|
-
var _crossFetch = _interopRequireDefault(require("cross-fetch"));
|
|
9
|
-
|
|
10
|
-
var _proxyFromEnv = require("proxy-from-env");
|
|
11
|
-
|
|
12
|
-
var _http = _interopRequireDefault(require("http"));
|
|
13
|
-
|
|
14
|
-
var _https = _interopRequireDefault(require("https"));
|
|
15
|
-
|
|
16
|
-
var _betterHttpsProxyAgent = require("better-https-proxy-agent");
|
|
17
|
-
|
|
18
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
|
-
const DefaultOptions = {
|
|
21
|
-
keepAlive: true,
|
|
22
|
-
timeout: 1000,
|
|
23
|
-
keepAliveMsecs: 500,
|
|
24
|
-
maxSockets: 200,
|
|
25
|
-
maxFreeSockets: 5,
|
|
26
|
-
maxCachedSessions: 500
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const chooseAgent = (parsedURL, options) => {
|
|
30
|
-
const proxyurl = (0, _proxyFromEnv.getProxyForUrl)(parsedURL.href);
|
|
31
|
-
|
|
32
|
-
if (proxyurl) {
|
|
33
|
-
const proxyRequestOptions = new URL(proxyurl);
|
|
34
|
-
return new _betterHttpsProxyAgent.Agent(options, proxyRequestOptions);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (parsedURL.protocol === 'https:') {
|
|
38
|
-
return new _https.default.Agent(options);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return new _http.default.Agent(options);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
function fetch(url, options) {
|
|
45
|
-
const opts = options || {};
|
|
46
|
-
const {
|
|
47
|
-
keepAlive,
|
|
48
|
-
timeout,
|
|
49
|
-
keepAliveMsecs,
|
|
50
|
-
maxSockets,
|
|
51
|
-
maxFreeSockets,
|
|
52
|
-
maxCachedSessions
|
|
53
|
-
} = { ...options,
|
|
54
|
-
...DefaultOptions
|
|
55
|
-
};
|
|
56
|
-
let agent = chooseAgent(new URL(url), {
|
|
57
|
-
keepAlive,
|
|
58
|
-
timeout,
|
|
59
|
-
keepAliveMsecs,
|
|
60
|
-
maxSockets,
|
|
61
|
-
maxFreeSockets,
|
|
62
|
-
maxCachedSessions
|
|
63
|
-
});
|
|
64
|
-
opts.agent = agent;
|
|
65
|
-
|
|
66
|
-
if (opts.signal) {
|
|
67
|
-
opts.signal.addEventListener('abort', () => {
|
|
68
|
-
agent.destroy();
|
|
69
|
-
agent = null;
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return (0, _crossFetch.default)(url, options);
|
|
74
|
-
}
|
package/lib/obj-columns.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = OBJColumns;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Take an `Object` and flatten it to get only one level of keys.
|
|
10
|
-
*
|
|
11
|
-
* <caption>Input:</caption>
|
|
12
|
-
*
|
|
13
|
-
* ```json
|
|
14
|
-
* [{
|
|
15
|
-
* "foo": {
|
|
16
|
-
* "hello": "world"
|
|
17
|
-
* },
|
|
18
|
-
* "bar": "anything else",
|
|
19
|
-
* "baz": 1
|
|
20
|
-
* }]
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* <caption>Output:</caption>
|
|
24
|
-
*
|
|
25
|
-
* ```json
|
|
26
|
-
* [{
|
|
27
|
-
* "foo": "{\"hello\":\"world\"}",
|
|
28
|
-
* "bar": "anything else",
|
|
29
|
-
* "baz": 1
|
|
30
|
-
* }]
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @name OBJColumns
|
|
34
|
-
* @alias flatten
|
|
35
|
-
* @param {undefined} none
|
|
36
|
-
* @returns {Object}
|
|
37
|
-
*/
|
|
38
|
-
function OBJColumns(data, feed) {
|
|
39
|
-
if (this.isLast()) {
|
|
40
|
-
feed.close();
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const obj = {};
|
|
45
|
-
Object.keys(data).sort((x, y) => x.localeCompare(y)).forEach(key => {
|
|
46
|
-
if (typeof data[key] === 'object') {
|
|
47
|
-
obj[key] = JSON.stringify(data[key]);
|
|
48
|
-
} else {
|
|
49
|
-
obj[key] = data[key];
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
feed.send(obj);
|
|
53
|
-
}
|
package/lib/skos-object.js
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
function Concept(data, feed) {
|
|
9
|
-
const obj = {};
|
|
10
|
-
Object.keys(data).forEach(key => {
|
|
11
|
-
const newkey = key.replace('skos$', '');
|
|
12
|
-
|
|
13
|
-
if (Array.isArray(data[key])) {
|
|
14
|
-
data[key].filter(x => x.xml$lang).forEach(item => {
|
|
15
|
-
const localkey = newkey.concat('@').concat(item.xml$lang);
|
|
16
|
-
obj[localkey] = item.$t;
|
|
17
|
-
});
|
|
18
|
-
} else if (data[key].rdf$resource && !obj[newkey]) {
|
|
19
|
-
obj[newkey] = data[key].rdf$resource;
|
|
20
|
-
} else if (data[key].rdf$resource && obj[newkey]) {
|
|
21
|
-
obj[newkey] = [obj[newkey], data[key].rdf$resource];
|
|
22
|
-
} else if (data[key].$t && data[key].xml$lang) {
|
|
23
|
-
const localkey = newkey.concat('@').concat(data[key].xml$lang);
|
|
24
|
-
obj[localkey] = data[key].$t;
|
|
25
|
-
} else if (data[key].$t && Array.isArray(obj[newkey])) {
|
|
26
|
-
obj[newkey].push(data[key].$t);
|
|
27
|
-
} else if (data[key].$t && obj[newkey]) {
|
|
28
|
-
obj[newkey] = [obj[newkey], data[key].$t];
|
|
29
|
-
} else if (data[key].$t && !obj[newkey]) {
|
|
30
|
-
obj[newkey] = data[key].$t;
|
|
31
|
-
} else if (typeof data[key] === 'object') {
|
|
32
|
-
obj[newkey] = (this.getIndex().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase();
|
|
33
|
-
let counter = 0;
|
|
34
|
-
Object.keys(data[key]).forEach(key2 => {
|
|
35
|
-
if (typeof data[key][key2] === 'object') {
|
|
36
|
-
data[key][key2].rdf$about = obj[newkey];
|
|
37
|
-
Concept.call(this, data[key][key2], feed);
|
|
38
|
-
counter += 1;
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
if (counter === 0) {
|
|
43
|
-
delete obj[newkey];
|
|
44
|
-
}
|
|
45
|
-
} else {
|
|
46
|
-
obj[newkey] = data[key];
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
feed.write(obj);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function SKOSObject(data, feed) {
|
|
53
|
-
if (this.isLast()) {
|
|
54
|
-
feed.close();
|
|
55
|
-
} else {
|
|
56
|
-
Concept.call(this, data, feed);
|
|
57
|
-
feed.end();
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Take `Object` generated by XMLMapping & SKOS data and
|
|
62
|
-
* create a new basic object with only keys & values
|
|
63
|
-
*
|
|
64
|
-
* @name SKOSObject
|
|
65
|
-
* @param {undefined} none
|
|
66
|
-
* @returns {Object}
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
var _default = {
|
|
71
|
-
SKOSObject
|
|
72
|
-
};
|
|
73
|
-
exports.default = _default;
|
package/lib/url-pager.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = URLPager;
|
|
7
|
-
|
|
8
|
-
var _debug = _interopRequireDefault(require("debug"));
|
|
9
|
-
|
|
10
|
-
var _url = require("url");
|
|
11
|
-
|
|
12
|
-
var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
|
|
13
|
-
|
|
14
|
-
var _lodash = _interopRequireDefault(require("lodash.get"));
|
|
15
|
-
|
|
16
|
-
var _parseHeaders = _interopRequireDefault(require("parse-headers"));
|
|
17
|
-
|
|
18
|
-
var _asyncRetry = _interopRequireDefault(require("async-retry"));
|
|
19
|
-
|
|
20
|
-
var _request = _interopRequireDefault(require("./request"));
|
|
21
|
-
|
|
22
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Take `Object` as parameters of URL, throw each chunk from the result
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* Input:
|
|
29
|
-
*
|
|
30
|
-
* ```json
|
|
31
|
-
* [{"q": "a"}]
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* Script:
|
|
35
|
-
*
|
|
36
|
-
* ```ini
|
|
37
|
-
* [URLPager]
|
|
38
|
-
* url = https://api.search.net
|
|
39
|
-
* path = total
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* Output:
|
|
43
|
-
*
|
|
44
|
-
* ```json
|
|
45
|
-
* [
|
|
46
|
-
* {
|
|
47
|
-
* "q": "a",
|
|
48
|
-
* "total": 22
|
|
49
|
-
* "offset": 0,
|
|
50
|
-
* "pageNumber": 1,
|
|
51
|
-
* "totalPages", 3,
|
|
52
|
-
* "maxPages": 1000,
|
|
53
|
-
* "limit": 10
|
|
54
|
-
* },
|
|
55
|
-
* {
|
|
56
|
-
* "q": "a",
|
|
57
|
-
* "total": 22
|
|
58
|
-
* "offset": 10,
|
|
59
|
-
* "pageNumber": 2,
|
|
60
|
-
* "totalPages", 3,
|
|
61
|
-
* "maxPages": 1000,
|
|
62
|
-
* "limit": 10
|
|
63
|
-
* },
|
|
64
|
-
* {
|
|
65
|
-
* "q": "a",
|
|
66
|
-
* "total": 22
|
|
67
|
-
* "offset": 20,
|
|
68
|
-
* "pageNumber": 3,
|
|
69
|
-
* "totalPages", 3,
|
|
70
|
-
* "maxPages": 1000,
|
|
71
|
-
* "limit": 10
|
|
72
|
-
* }
|
|
73
|
-
* ]
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* #### Example with URLs
|
|
77
|
-
*
|
|
78
|
-
* Input:
|
|
79
|
-
*
|
|
80
|
-
* ```json
|
|
81
|
-
* [
|
|
82
|
-
* "https://httpbin.org/get?a=a",
|
|
83
|
-
* "https://httpbin.org/get?a=b",
|
|
84
|
-
* "https://httpbin.org/get?a=c"
|
|
85
|
-
* ]
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* Script:
|
|
89
|
-
*
|
|
90
|
-
* ```ini
|
|
91
|
-
* [URLPager]
|
|
92
|
-
* path = .args
|
|
93
|
-
* ```
|
|
94
|
-
*
|
|
95
|
-
* Output:
|
|
96
|
-
*
|
|
97
|
-
* ```json
|
|
98
|
-
* [{"a": "a"}, {"a": "b"}, {"a": "c" }]
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* @name URLPager
|
|
102
|
-
* @param {String} [url] URL to fetch (by default input string is taken)
|
|
103
|
-
* @param {String} [path=total] choose the path to find the number of result
|
|
104
|
-
* @param {Number} [timeout=1000] Timeout in milliseconds
|
|
105
|
-
* @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
|
|
106
|
-
* @param {Number} [retries=5] The maximum amount of times to retry the connection
|
|
107
|
-
* @returns {Object}
|
|
108
|
-
*/
|
|
109
|
-
async function URLPager(data, feed) {
|
|
110
|
-
if (this.isLast()) {
|
|
111
|
-
return feed.close();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const url = this.getParam('url');
|
|
115
|
-
const path = this.getParam('path', 'total');
|
|
116
|
-
const limit = Number(this.getParam('limit', 10));
|
|
117
|
-
const maxPages = Number(this.getParam('maxPages', 1000));
|
|
118
|
-
const retries = Number(this.getParam('retries', 5));
|
|
119
|
-
const noerror = Boolean(this.getParam('noerror', false));
|
|
120
|
-
const timeout = Number(this.getParam('timeout')) || 1000;
|
|
121
|
-
const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
|
|
122
|
-
const cURL = new _url.URL(url || data);
|
|
123
|
-
const controller = new _nodeAbortController.default();
|
|
124
|
-
const parameters = {
|
|
125
|
-
timeout,
|
|
126
|
-
headers,
|
|
127
|
-
signal: controller.signal
|
|
128
|
-
};
|
|
129
|
-
const options = {
|
|
130
|
-
retries
|
|
131
|
-
};
|
|
132
|
-
cURL.search = new _url.URLSearchParams(data);
|
|
133
|
-
|
|
134
|
-
const onError = e => {
|
|
135
|
-
controller.abort();
|
|
136
|
-
|
|
137
|
-
if (noerror) {
|
|
138
|
-
(0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLPager] <${e}>`);
|
|
139
|
-
return feed.send(data);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
(0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLPager] <${e}>`);
|
|
143
|
-
return feed.send(e);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
|
|
148
|
-
const json = await response.json();
|
|
149
|
-
const total = (0, _lodash.default)(json, path);
|
|
150
|
-
|
|
151
|
-
if (total === 0) {
|
|
152
|
-
return onError(new Error('No result.'));
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (total === undefined) {
|
|
156
|
-
return onError(new Error('Unexpected response.'));
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
let totalPages = Math.ceil(json.total / limit);
|
|
160
|
-
|
|
161
|
-
if (totalPages > maxPages) {
|
|
162
|
-
totalPages = maxPages;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
for (let pageNumber = 1; pageNumber <= totalPages; pageNumber += 1) {
|
|
166
|
-
feed.write({ ...data,
|
|
167
|
-
offset: (pageNumber - 1) * limit,
|
|
168
|
-
pageNumber,
|
|
169
|
-
totalPages,
|
|
170
|
-
maxPages,
|
|
171
|
-
limit
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
feed.end();
|
|
176
|
-
} catch (e) {
|
|
177
|
-
onError(e);
|
|
178
|
-
}
|
|
179
|
-
}
|
package/lib/utils.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = exports.writeTo = void 0;
|
|
7
|
-
|
|
8
|
-
const writeTo = function writeTo(stream, data, cb) {
|
|
9
|
-
if (!stream.write(data)) {
|
|
10
|
-
stream.once('drain', cb);
|
|
11
|
-
} else {
|
|
12
|
-
process.nextTick(cb);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
exports.writeTo = writeTo;
|
|
17
|
-
var _default = writeTo;
|
|
18
|
-
exports.default = _default;
|