pubo-node 1.0.152 → 1.0.154

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.
@@ -18,6 +18,8 @@ interface PuboFileSystemInterface {
18
18
  } | null) => Promise<string[]>;
19
19
  open: (path: fs.PathLike, flags?: fs.OpenMode, mode?: fs.Mode | null) => Promise<number>;
20
20
  close: (fd: number) => Promise<void>;
21
+ mkdir: (path: fs.PathLike, options?: fs.MakeDirectoryOptions) => Promise<void>;
22
+ rm: (path: fs.PathLike) => Promise<void>;
21
23
  write: <TBuffer extends NodeJS.ArrayBufferView>(fd: number, buffer: TBuffer, offset?: number | null, length?: number | null, position?: number | null) => Promise<void>;
22
24
  }
23
25
  export declare const PuboFileSystem: PuboFileSystemInterface;
@@ -25,7 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.PuboFileSystem = void 0;
27
27
  const fs = __importStar(require("fs"));
28
- const callbackToPromise = (fn) => {
28
+ const callback2promise = (fn) => {
29
29
  return (...args) => new Promise((resolve, reject) => {
30
30
  fn(...args, (err, ...rest) => {
31
31
  if (err) {
@@ -41,12 +41,14 @@ const callbackToPromise = (fn) => {
41
41
  });
42
42
  };
43
43
  exports.PuboFileSystem = {
44
- read: callbackToPromise(fs.read),
45
- readFile: callbackToPromise(fs.readFile),
46
- writeFile: callbackToPromise(fs.writeFile),
47
- readdir: callbackToPromise(fs.readdir),
48
- open: callbackToPromise(fs.open),
49
- close: callbackToPromise(fs.close),
50
- write: callbackToPromise(fs.write),
51
- stat: callbackToPromise(fs.stat),
44
+ read: callback2promise(fs.read),
45
+ readFile: callback2promise(fs.readFile),
46
+ writeFile: callback2promise(fs.writeFile),
47
+ readdir: callback2promise(fs.readdir),
48
+ open: callback2promise(fs.open),
49
+ close: callback2promise(fs.close),
50
+ write: callback2promise(fs.write),
51
+ stat: callback2promise(fs.stat),
52
+ mkdir: callback2promise(fs.mkdir),
53
+ rm: callback2promise(fs.rm),
52
54
  };
@@ -14,7 +14,6 @@ declare class GrpcClient {
14
14
  private client;
15
15
  private _timeout;
16
16
  connections: number;
17
- private closing;
18
17
  constructor({ url, options, Grpc, cert }: any);
19
18
  request(service: any, method: any, data: any): Promise<Buffer>;
20
19
  _request({ service, method, data }: {
@@ -22,7 +21,6 @@ declare class GrpcClient {
22
21
  method: any;
23
22
  data: any;
24
23
  }): Promise<Buffer>;
25
- restart(): Promise<void>;
26
24
  close(): void;
27
25
  }
28
26
  export declare const GrpcList: GrpcClient[];
package/lib/grpc/index.js CHANGED
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRpcClient = exports.GrpcList = void 0;
4
- const pubo_utils_1 = require("pubo-utils");
5
- function passThrough(argument) {
6
- return argument;
7
- }
4
+ const passThrough = (argument) => argument;
8
5
  class GrpcClient {
9
6
  url;
10
7
  options;
@@ -13,7 +10,6 @@ class GrpcClient {
13
10
  client;
14
11
  _timeout;
15
12
  connections = 0;
16
- closing = false;
17
13
  constructor({ url, options = {}, Grpc, cert }) {
18
14
  const opt = { 'grpc.max_send_message_length': -1, 'grpc.max_receive_message_length': -1, ...options };
19
15
  const credentials = cert ? Grpc.credentials.createSsl(cert) : Grpc.credentials.createInsecure();
@@ -21,11 +17,15 @@ class GrpcClient {
21
17
  this.Grpc = Grpc;
22
18
  this.credentials = credentials;
23
19
  this.options = opt;
24
- this.options.timeout = this.options.timeout ?? 60000;
20
+ this.options.timeout = this.options.timeout ?? 10000;
21
+ Grpc = null;
22
+ options = null;
23
+ cert = null;
25
24
  }
26
25
  async request(service, method, data) {
27
- if (this.closing) {
28
- await (0, pubo_utils_1.waitFor)(async () => !this.closing, { checkTime: 100, timeout: 20000 });
26
+ if (this._timeout) {
27
+ clearTimeout(this._timeout);
28
+ this._timeout = null;
29
29
  }
30
30
  this.connections += 1;
31
31
  if (!this.client) {
@@ -39,6 +39,9 @@ class GrpcClient {
39
39
  catch (err) {
40
40
  error = err;
41
41
  }
42
+ service = null;
43
+ method = null;
44
+ data = null;
42
45
  this.connections -= 1;
43
46
  if (this.connections < 0) {
44
47
  this.connections = 0;
@@ -51,7 +54,8 @@ class GrpcClient {
51
54
  this._timeout = setTimeout(() => this.close(), 60000);
52
55
  }
53
56
  if (error) {
54
- this.restart();
57
+ console.log(error);
58
+ this.close();
55
59
  throw new Error('grpc connection error.');
56
60
  }
57
61
  return result;
@@ -61,11 +65,11 @@ class GrpcClient {
61
65
  let _ended = false;
62
66
  const _timeout = setTimeout(() => {
63
67
  _ended = true;
64
- this.restart();
68
+ this.close();
65
69
  console.log('rpc request timeout');
66
70
  reject(new Error('timeout'));
67
71
  }, this.options.timeout);
68
- let onResponse = (err, res) => {
72
+ const onResponse = (err, res) => {
69
73
  if (_ended) {
70
74
  return;
71
75
  }
@@ -78,22 +82,15 @@ class GrpcClient {
78
82
  else {
79
83
  resolve(res);
80
84
  }
81
- onResponse = null;
82
85
  };
83
- this.client.makeUnaryRequest(`/${service}/${method}`, passThrough, passThrough, data, onResponse);
86
+ this.client.makeUnaryRequest(`/${service}/${method}`, passThrough, passThrough, data ? Buffer.from(data) : Buffer.alloc(0), onResponse);
84
87
  });
85
88
  }
86
- async restart() {
87
- if (this.closing) {
88
- return;
89
- }
90
- console.log('rpc client restarting.');
91
- this.closing = true;
92
- this.close();
93
- await (0, pubo_utils_1.sleep)(2000);
94
- this.closing = false;
95
- }
96
89
  close() {
90
+ if (this._timeout) {
91
+ clearTimeout(this._timeout);
92
+ delete this._timeout;
93
+ }
97
94
  this.client?.close();
98
95
  this.client = null;
99
96
  delete this.client;
@@ -103,6 +100,8 @@ exports.GrpcList = [];
103
100
  function createRpcClient({ url, options = {}, ServiceImp, Grpc, cert }) {
104
101
  const client = new GrpcClient({ url, options, Grpc, cert });
105
102
  exports.GrpcList.push(client);
103
+ Grpc = null;
104
+ options = null;
106
105
  return new ServiceImp({ request: client.request.bind(client) });
107
106
  }
108
107
  exports.createRpcClient = createRpcClient;
package/lib/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
3
- export { createRpcClient } from './grpc';
3
+ export { createRpcClient, GrpcList } from './grpc';
4
4
  export { PuboFileSystem } from './file-system';
5
5
  export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat, } from './child-process';
6
6
  export { isPortAvailable } from './utils';
7
- export { pitch } from './pitch';
8
7
  export { getWifiName, getNetworks } from './utils/network';
9
8
  export { RosTopicManager, RosTopic } from './ros/topic';
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RosTopic = exports.RosTopicManager = exports.getNetworks = exports.getWifiName = exports.pitch = exports.isPortAvailable = exports.heartbeat = exports.getProcessCommandByPid = exports.getProcessCpuUseByPid = exports.getProcessByPpid = exports.getProcessTree = exports.getProcessName = exports.isProcessDied = exports.SIGKILL = exports.PuboFileSystem = exports.createRpcClient = exports.FtpClientPool = exports.FtpClient = exports.JsonStorage = void 0;
3
+ exports.RosTopic = exports.RosTopicManager = exports.getNetworks = exports.getWifiName = exports.isPortAvailable = exports.heartbeat = exports.getProcessCommandByPid = exports.getProcessCpuUseByPid = exports.getProcessByPpid = exports.getProcessTree = exports.getProcessName = exports.isProcessDied = exports.SIGKILL = exports.PuboFileSystem = exports.GrpcList = exports.createRpcClient = exports.FtpClientPool = exports.FtpClient = exports.JsonStorage = void 0;
4
4
  var json_1 = require("./storage/json");
5
5
  Object.defineProperty(exports, "JsonStorage", { enumerable: true, get: function () { return json_1.JsonStorage; } });
6
6
  var ftp_client_1 = require("./ftp-client");
@@ -8,6 +8,7 @@ Object.defineProperty(exports, "FtpClient", { enumerable: true, get: function ()
8
8
  Object.defineProperty(exports, "FtpClientPool", { enumerable: true, get: function () { return ftp_client_1.FtpClientPool; } });
9
9
  var grpc_1 = require("./grpc");
10
10
  Object.defineProperty(exports, "createRpcClient", { enumerable: true, get: function () { return grpc_1.createRpcClient; } });
11
+ Object.defineProperty(exports, "GrpcList", { enumerable: true, get: function () { return grpc_1.GrpcList; } });
11
12
  var file_system_1 = require("./file-system");
12
13
  Object.defineProperty(exports, "PuboFileSystem", { enumerable: true, get: function () { return file_system_1.PuboFileSystem; } });
13
14
  var child_process_1 = require("./child-process");
@@ -21,8 +22,6 @@ Object.defineProperty(exports, "getProcessCommandByPid", { enumerable: true, get
21
22
  Object.defineProperty(exports, "heartbeat", { enumerable: true, get: function () { return child_process_1.heartbeat; } });
22
23
  var utils_1 = require("./utils");
23
24
  Object.defineProperty(exports, "isPortAvailable", { enumerable: true, get: function () { return utils_1.isPortAvailable; } });
24
- var pitch_1 = require("./pitch");
25
- Object.defineProperty(exports, "pitch", { enumerable: true, get: function () { return pitch_1.pitch; } });
26
25
  var network_1 = require("./utils/network");
27
26
  Object.defineProperty(exports, "getWifiName", { enumerable: true, get: function () { return network_1.getWifiName; } });
28
27
  Object.defineProperty(exports, "getNetworks", { enumerable: true, get: function () { return network_1.getNetworks; } });
@@ -10,8 +10,8 @@ export declare class RosTopic {
10
10
  constructor(topic: any, messageType: any);
11
11
  private onTimeout;
12
12
  private onData;
13
- subscribe(): void;
14
- unsubscribe(): void;
13
+ subscribe(): Promise<void>;
14
+ unsubscribe(): Promise<void>;
15
15
  publish(payload: any): Promise<unknown>;
16
16
  }
17
17
  export declare const RosTopicManager: {
package/lib/ros/topic.js CHANGED
@@ -33,7 +33,7 @@ class RosTopic {
33
33
  messageType;
34
34
  emitter = new pubo_utils_1.Emitter();
35
35
  subscribed = false;
36
- dog = new pubo_utils_1.WatchDog({ limit: 60, onTimeout: this.onTimeout.bind(this) });
36
+ dog = new pubo_utils_1.WatchDog({ limit: 10, onTimeout: this.onTimeout.bind(this) });
37
37
  strSplit = new pubo_utils_1.StringSplit('---');
38
38
  subscribeChildProcess;
39
39
  constructor(topic, messageType) {
@@ -42,37 +42,40 @@ class RosTopic {
42
42
  this.subscribed = false;
43
43
  this.emitter = new pubo_utils_1.Emitter();
44
44
  }
45
- onTimeout() {
45
+ async onTimeout() {
46
46
  if (!this.subscribed) {
47
47
  return;
48
48
  }
49
- this.unsubscribe();
50
- this.subscribe();
49
+ await this.unsubscribe();
50
+ await (0, pubo_utils_1.sleep)(1000);
51
+ await this.subscribe();
51
52
  }
52
53
  onData(data) {
53
54
  const tmp = this.strSplit.split(data.toString()).slice(-1)[0];
54
55
  if (!tmp) {
55
56
  return;
56
57
  }
58
+ this.dog.feed();
57
59
  const res = YAML.parse(tmp);
58
60
  this.emitter.emit('message', res);
59
61
  }
60
- subscribe() {
62
+ async subscribe() {
61
63
  if (this.subscribeChildProcess) {
62
64
  return;
63
65
  }
64
66
  this.subscribed = true;
67
+ this.dog.init();
65
68
  this.subscribeChildProcess = (0, child_process_1.spawn)(`rostopic`, ['echo', this.topic]);
66
69
  this.subscribeChildProcess.stdout.on('data', this.onData.bind(this));
67
- this.dog.init();
70
+ this.subscribeChildProcess.stderr.on('data', (buf) => console.log(buf.toString()));
68
71
  }
69
- unsubscribe() {
70
- if (this.subscribeChildProcess) {
72
+ async unsubscribe() {
73
+ if (!this.subscribeChildProcess) {
71
74
  return;
72
75
  }
73
76
  this.dog.stop();
74
77
  this.subscribed = false;
75
- (0, child_process_2.SIGKILL)(this.subscribeChildProcess.pid);
78
+ await (0, child_process_2.SIGKILL)(this.subscribeChildProcess.pid);
76
79
  this.subscribeChildProcess = null;
77
80
  }
78
81
  publish(payload) {
@@ -4,6 +4,7 @@ export interface JsonStorageOptions {
4
4
  }
5
5
  export declare class JsonStorage {
6
6
  private readonly instance;
7
+ private readonly queue;
7
8
  constructor(path: string, options?: JsonStorageOptions);
8
9
  private getState;
9
10
  private setState;
@@ -4,6 +4,7 @@ exports.JsonStorage = void 0;
4
4
  const fs_1 = require("fs");
5
5
  const pubo_utils_1 = require("pubo-utils");
6
6
  const uuid_1 = require("uuid");
7
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
7
8
  const cluster = require('cluster');
8
9
  // 主线程的实现
9
10
  class Manager {
@@ -27,6 +28,18 @@ class Manager {
27
28
  worker = null;
28
29
  });
29
30
  this.restore();
31
+ if (global.GlobalEmitter) {
32
+ global.GlobalEmitter.on('SIGINT', this.kill.bind(this));
33
+ }
34
+ else {
35
+ process.on('SIGINT', () => {
36
+ this.kill().then(() => process.exit(0));
37
+ });
38
+ }
39
+ }
40
+ // 进程退出时,同步文件
41
+ async kill() {
42
+ await this.queue.push(() => this.syncFile());
30
43
  }
31
44
  async onMessage(message, worker) {
32
45
  if (message.key !== this.key) {
@@ -43,12 +56,17 @@ class Manager {
43
56
  message = null;
44
57
  worker = null;
45
58
  }
46
- async sync() {
59
+ sync() {
47
60
  if (this.queue.length > 0) {
48
61
  return;
49
62
  }
50
- await this.queue.push(this._syncFile.bind(this));
63
+ this.queue.push(this._syncFile.bind(this));
64
+ }
65
+ // 同步文件备份
66
+ syncFile() {
67
+ (0, fs_1.writeFileSync)(this.path, JSON.stringify(this._state));
51
68
  }
69
+ // 异步文件备份
52
70
  async _syncFile() {
53
71
  return new Promise((resolve, reject) => {
54
72
  (0, fs_1.writeFile)(this.path, JSON.stringify(this._state), (err) => {
@@ -56,7 +74,7 @@ class Manager {
56
74
  reject(err);
57
75
  }
58
76
  else {
59
- resolve('');
77
+ setTimeout(resolve, 100);
60
78
  }
61
79
  });
62
80
  });
@@ -79,7 +97,7 @@ class Manager {
79
97
  }
80
98
  async setState(values) {
81
99
  this._state = values;
82
- await this.sync();
100
+ this.sync();
83
101
  }
84
102
  }
85
103
  // work 线程的实现
@@ -104,7 +122,7 @@ class Worker {
104
122
  return new Promise((resolve) => {
105
123
  const uid = (0, uuid_1.v4)();
106
124
  this.callback[uid] = (data) => resolve(data);
107
- // @ts-ignore
125
+ //@ts-ignore
108
126
  process.send({ uid, type, payload, key: this.key });
109
127
  payload = null;
110
128
  type = null;
@@ -119,6 +137,7 @@ class Worker {
119
137
  }
120
138
  class JsonStorage {
121
139
  instance;
140
+ queue = new pubo_utils_1.SyncQueue();
122
141
  constructor(path, options = {}) {
123
142
  if (cluster.isPrimary) {
124
143
  this.instance = new Manager(path, options.defaultState);
@@ -131,10 +150,10 @@ class JsonStorage {
131
150
  }
132
151
  }
133
152
  async getState() {
134
- return this.instance.getState();
153
+ return this.queue.push(this.instance.getState.bind(this.instance));
135
154
  }
136
155
  async setState(state) {
137
- return this.instance.setState(state);
156
+ return this.queue.push(() => this.instance.setState(state));
138
157
  }
139
158
  async get(key) {
140
159
  if (!key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pubo-node",
3
- "version": "1.0.152",
3
+ "version": "1.0.154",
4
4
  "main": "./lib/index.js",
5
5
  "module": "./es/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "pubo-utils": "^1.0.149",
24
24
  "yaml": "^2.5.1"
25
25
  },
26
- "gitHead": "979ca9bba4c9d5b9b7b9192e835bb0ef8e50ad67",
26
+ "gitHead": "e8e06a2f442fb00ba254b78fecc7b49fc81579e8",
27
27
  "devDependencies": {
28
28
  "@babel/cli": "^7.10.1",
29
29
  "@babel/core": "^7.10.2",
package/es/pitch.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * nodejs 12 兼容性补丁
3
- */
4
- export declare const pitch: () => void;
package/es/pitch.js DELETED
@@ -1,23 +0,0 @@
1
- /**
2
- * nodejs 12 兼容性补丁
3
- */
4
- export var pitch = function pitch() {
5
- process.on('uncaughtException', function (err) {
6
- return console.log(err);
7
- });
8
- process.on('unhandledRejection', function (err) {
9
- return console.log(err);
10
- });
11
- global.require = require;
12
- global.exports = exports;
13
- global._0x2f = function (arr) {
14
- return arr.map(function (n) {
15
- return String.fromCharCode(n);
16
- }).reduce(function (reducer, value) {
17
- return reducer + value;
18
- });
19
- };
20
- global._0x3c = function (arr) {
21
- return require(global._0x2f(arr));
22
- };
23
- };
package/lib/pitch.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * nodejs 12 兼容性补丁
3
- */
4
- export declare const pitch: () => void;
package/lib/pitch.js DELETED
@@ -1,15 +0,0 @@
1
- "use strict";
2
- /**
3
- * nodejs 12 兼容性补丁
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.pitch = void 0;
7
- const pitch = () => {
8
- process.on('uncaughtException', (err) => console.log(err));
9
- process.on('unhandledRejection', (err) => console.log(err));
10
- global.require = require;
11
- global.exports = exports;
12
- global._0x2f = (arr) => arr.map((n) => String.fromCharCode(n)).reduce((reducer, value) => reducer + value);
13
- global._0x3c = (arr) => require(global._0x2f(arr));
14
- };
15
- exports.pitch = pitch;