clientlinker-flow-localfile 4.1.8 → 5.0.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
@@ -25,7 +25,7 @@ npm i clientlinker-flow-localfile --save
25
25
  # Usage
26
26
 
27
27
  ```javascript
28
- var clientlinker = require('clientlinker');
28
+ var { clientlinker } = require('clientlinker');
29
29
  var linker = clientlinker({
30
30
  flows: ['localfile'],
31
31
  clients: {
@@ -35,7 +35,7 @@ var linker = clientlinker({
35
35
  }
36
36
  });
37
37
 
38
- linker.flow('localfile', require('clientlinker-flow-localfile'));
38
+ linker.flow('localfile', require('clientlinker-flow-localfile').localfileFlow);
39
39
 
40
40
  // use
41
41
  linker.run('client.js', null, {id: 13})
@@ -43,6 +43,13 @@ linker.run('client.js', null, {id: 13})
43
43
  ```
44
44
 
45
45
 
46
+ # Upgrade
47
+
48
+ ## 5.x
49
+
50
+ * The package entry no longer exports the installer function directly; it is now the named export `localfileFlow`.
51
+ * Deep imports moved from `flow/*` to `dist/flow/*`; `dist/flow/localfile` exports `localfile` and `parseContent` instead of a callable module.
52
+
46
53
  [npm-image]: https://img.shields.io/npm/v/clientlinker-flow-localfile.svg
47
54
  [downloads-image]: https://img.shields.io/npm/dm/clientlinker-flow-localfile.svg
48
55
  [npm-url]: https://www.npmjs.org/package/clientlinker-flow-localfile
@@ -0,0 +1,2 @@
1
+ import type { Flow } from 'clientlinker';
2
+ export declare function localfileFlow(flow: Flow): void;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.localfileFlow = localfileFlow;
4
+ const localfile_1 = require("./localfile");
5
+ const methods_1 = require("./methods");
6
+ function localfileFlow(flow) {
7
+ flow.run = localfile_1.localfile;
8
+ flow.methods = methods_1.methods;
9
+ }
@@ -0,0 +1,3 @@
1
+ import type { ClientRuntime, FlowRuntime, Linker } from 'clientlinker';
2
+ export declare function localfile(runtime: ClientRuntime, callback: FlowRuntime): Promise<unknown>;
3
+ export declare function parseContent(linker: Linker, content: string, extname: string): any;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.localfile = localfile;
7
+ exports.parseContent = parseContent;
8
+ const p_map_1 = __importDefault(require("p-map"));
9
+ const fs_1 = require("fs");
10
+ const vm_1 = require("vm");
11
+ const debug_1 = __importDefault(require("debug"));
12
+ const debug = (0, debug_1.default)('clientlinker-flow-localfile');
13
+ function localfile(runtime, callback) {
14
+ const client = runtime.client;
15
+ const options = client.options;
16
+ if (!options.localfile)
17
+ return callback.next();
18
+ const file = options.localfile + '/' + runtime.method;
19
+ return checkExists(file, ['js', 'json']).then(function (exists) {
20
+ const fileInfo = exists[0];
21
+ if (!fileInfo)
22
+ return callback.next();
23
+ return fs_1.promises.readFile(fileInfo.file, { encoding: 'utf8' })
24
+ .then(function (content) {
25
+ return parseContent(client.linker, content, fileInfo.extname);
26
+ })
27
+ .then(function (data) {
28
+ if (!data)
29
+ throw data;
30
+ else if (data.result)
31
+ throw data.result;
32
+ else
33
+ return data.data;
34
+ });
35
+ });
36
+ }
37
+ function checkExists(file, extnames) {
38
+ return (0, p_map_1.default)(extnames, function (extname) {
39
+ const thisFile = file + '.' + extname;
40
+ return fs_1.promises.stat(thisFile).then(function (stats) {
41
+ if (stats.isFile())
42
+ return { extname: extname, file: thisFile };
43
+ }, function (err) {
44
+ debug('file stat err, file: %s, err: %o', thisFile, err);
45
+ });
46
+ }, {
47
+ concurrency: 5
48
+ }).then(function (exists) {
49
+ return exists.filter(function (item) {
50
+ return !!item;
51
+ });
52
+ });
53
+ }
54
+ function parseContent(linker, content, extname) {
55
+ if (extname == 'json') {
56
+ return JSON.parse(content);
57
+ }
58
+ else {
59
+ const data = { module: { exports: {} } };
60
+ (0, vm_1.runInNewContext)(content, data);
61
+ return data.module.exports;
62
+ }
63
+ }
@@ -0,0 +1,2 @@
1
+ import type { Client } from 'clientlinker';
2
+ export declare function methods(client: Client): Promise<string[] | undefined> | undefined;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.methods = methods;
7
+ const p_map_1 = __importDefault(require("p-map"));
8
+ const fs_1 = require("fs");
9
+ const debug_1 = __importDefault(require("debug"));
10
+ const debug = (0, debug_1.default)('clientlinker-flow-localfile:methods');
11
+ function methods(client) {
12
+ const pathdir = client.options.localfile;
13
+ if (!pathdir)
14
+ return;
15
+ return fs_1.promises.readdir(pathdir).then(function (dirs) {
16
+ return (0, p_map_1.default)(dirs, function (filename) {
17
+ if (filename[0] == '.')
18
+ return;
19
+ const arr = filename.split('.');
20
+ const extname = arr.pop();
21
+ if (extname == 'json' || extname == 'js') {
22
+ return fs_1.promises.stat(pathdir + '/' + filename).then(function (stats) {
23
+ if (stats.isFile())
24
+ return arr.join('.');
25
+ }, function () {
26
+ return undefined;
27
+ });
28
+ }
29
+ }, {
30
+ concurrency: 5
31
+ }).then(function (methods) {
32
+ return methods.filter(function (method) {
33
+ return !!method;
34
+ });
35
+ });
36
+ }, function (err) {
37
+ debug('readdir err, dir: %s, err: %o', pathdir, err);
38
+ return undefined;
39
+ });
40
+ }
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "clientlinker-flow-localfile",
3
- "version": "4.1.8",
3
+ "version": "5.0.1",
4
4
  "description": "Localfile flow for clientlinker",
5
- "main": "flow/index.js",
5
+ "main": "dist/flow/index.js",
6
+ "types": "dist/flow/index.d.ts",
6
7
  "scripts": {
7
- "test": "cross-env DEBUG=clientlinker-flow-localfile* mocha test/test_*"
8
+ "build": "tsc -p tsconfig.json",
9
+ "prepare": "npm run build",
10
+ "pretest": "npm run build",
11
+ "test": "cross-env DEBUG=clientlinker-flow-localfile* jest"
8
12
  },
9
13
  "keywords": [
10
14
  "clientlinker",
@@ -19,10 +23,10 @@
19
23
  "p-map": "^4.0.0"
20
24
  },
21
25
  "devDependencies": {
22
- "clientlinker": "^13.1.4",
23
- "cross-env": "^7.0.3",
24
- "expect.js": "^0.3.1",
25
- "mocha": "^8.4.0"
26
+ "@types/debug": "^4.1.13",
27
+ "@types/node": "^20.19.43",
28
+ "clientlinker": "^14.0.1",
29
+ "cross-env": "^7.0.3"
26
30
  },
27
31
  "repository": {
28
32
  "type": "git",
@@ -32,5 +36,5 @@
32
36
  "url": "https://github.com/iGdea/node-clientlinker/issues"
33
37
  },
34
38
  "homepage": "https://github.com/iGdea/node-clientlinker/tree/master/packages/clientlinker-flow-localfile",
35
- "gitHead": "bad3b81f70101364868a5b2e7aaa84a1dcdcc85a"
39
+ "gitHead": "83da4b5802bfd2c2cfa6a4a14a1d8e74a6e3d029"
36
40
  }
package/flow/index.js DELETED
@@ -1,7 +0,0 @@
1
- const localfile = require('./localfile');
2
- const methods = require('./methods');
3
-
4
- module.exports = function(flow) {
5
- flow.run = localfile;
6
- flow.methods = methods;
7
- };
package/flow/localfile.js DELETED
@@ -1,67 +0,0 @@
1
- const PromiseMap = require('p-map');
2
- const fs = require('fs').promises;
3
- const vm = require('vm');
4
- const debug = require('debug')('clientlinker-flow-localfile');
5
-
6
- exports = module.exports = localfile;
7
-
8
- function localfile(runtime, callback) {
9
- const client = runtime.client;
10
- const options = client.options;
11
-
12
- if (!options.localfile) return callback.next();
13
-
14
- const file = options.localfile + '/' + runtime.method;
15
-
16
- return checkExists(file, ['js', 'json']).then(function(exists) {
17
- const fileInfo = exists[0];
18
- if (!fileInfo) return callback.next();
19
-
20
- return fs.readFile(fileInfo.file, { encoding: 'utf8' })
21
- .then(function(content) {
22
- return parseContent(client.linker, content, fileInfo.extname);
23
- })
24
- .then(function(data) {
25
- if (!data) throw data;
26
- else if (data.result) throw data.result;
27
- else return data.data;
28
- });
29
- });
30
- }
31
-
32
- function checkExists(file, extnames) {
33
- return PromiseMap(
34
- extnames,
35
- function(extname) {
36
- const thisFile = file + '.' + extname;
37
-
38
- return fs.stat(thisFile).then(
39
- function(stats) {
40
- if (stats.isFile())
41
- return { extname: extname, file: thisFile };
42
- },
43
- function(err) {
44
- debug('file stat err, file: %s, err: %o', thisFile, err);
45
- }
46
- );
47
- },
48
- {
49
- concurrency: 5
50
- }
51
- ).then(function(exists) {
52
- return exists.filter(function(item) {
53
- return !!item;
54
- });
55
- });
56
- }
57
-
58
- exports.parseContent = parseContent;
59
- function parseContent(linker, content, extname) {
60
- if (extname == 'json') {
61
- return JSON.parse(content);
62
- } else {
63
- const data = { module: { exports: {} } };
64
- vm.runInNewContext(content, data);
65
- return data.module.exports;
66
- }
67
- }
package/flow/methods.js DELETED
@@ -1,41 +0,0 @@
1
- const PromiseMap = require('p-map');
2
- const fs = require('fs').promises;
3
- const debug = require('debug')('clientlinker-flow-localfile:methods');
4
-
5
- module.exports = methods;
6
- function methods(client) {
7
- const pathdir = client.options.localfile;
8
- if (!pathdir) return;
9
-
10
- return fs.readdir(pathdir).then(
11
- function(dirs) {
12
- return PromiseMap(
13
- dirs,
14
- function(filename) {
15
- if (filename[0] == '.') return;
16
-
17
- const arr = filename.split('.');
18
- const extname = arr.pop();
19
- if (extname == 'json' || extname == 'js') {
20
- return fs.stat(pathdir + '/' + filename).then(
21
- function(stats) {
22
- if (stats.isFile()) return arr.join('.');
23
- },
24
- function() {}
25
- );
26
- }
27
- },
28
- {
29
- concurrency: 5
30
- }
31
- ).then(function(methods) {
32
- return methods.filter(function(method) {
33
- return method;
34
- });
35
- });
36
- },
37
- function(err) {
38
- debug('readdir err, dir: %s, err: %o', pathdir, err);
39
- }
40
- );
41
- }