pubo-node 1.0.141 → 1.0.142

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.
@@ -1,144 +1,27 @@
1
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) {
3
- return value instanceof P ? value : new P(function (resolve) {
4
- resolve(value);
5
- });
6
- }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) {
9
- try {
10
- step(generator.next(value));
11
- } catch (e) {
12
- reject(e);
13
- }
14
- }
15
- function rejected(value) {
16
- try {
17
- step(generator["throw"](value));
18
- } catch (e) {
19
- reject(e);
20
- }
21
- }
22
- function step(result) {
23
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
24
- }
25
- step((generator = generator.apply(thisArg, _arguments || [])).next());
26
- });
27
- };
28
- var __generator = this && this.__generator || function (thisArg, body) {
29
- var _ = {
30
- label: 0,
31
- sent: function sent() {
32
- if (t[0] & 1) throw t[1];
33
- return t[1];
34
- },
35
- trys: [],
36
- ops: []
37
- },
38
- f,
39
- y,
40
- t,
41
- g;
42
- return g = {
43
- next: verb(0),
44
- "throw": verb(1),
45
- "return": verb(2)
46
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
47
- return this;
48
- }), g;
49
- function verb(n) {
50
- return function (v) {
51
- return step([n, v]);
52
- };
53
- }
54
- function step(op) {
55
- if (f) throw new TypeError("Generator is already executing.");
56
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
57
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
58
- if (y = 0, t) op = [op[0] & 2, t.value];
59
- switch (op[0]) {
60
- case 0:
61
- case 1:
62
- t = op;
63
- break;
64
- case 4:
65
- _.label++;
66
- return {
67
- value: op[1],
68
- done: false
69
- };
70
- case 5:
71
- _.label++;
72
- y = op[1];
73
- op = [0];
74
- continue;
75
- case 7:
76
- op = _.ops.pop();
77
- _.trys.pop();
78
- continue;
79
- default:
80
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
81
- _ = 0;
82
- continue;
83
- }
84
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
85
- _.label = op[1];
86
- break;
87
- }
88
- if (op[0] === 6 && _.label < t[1]) {
89
- _.label = t[1];
90
- t = op;
91
- break;
92
- }
93
- if (t && _.label < t[2]) {
94
- _.label = t[2];
95
- _.ops.push(op);
96
- break;
97
- }
98
- if (t[2]) _.ops.pop();
99
- _.trys.pop();
100
- continue;
101
- }
102
- op = body.call(thisArg, _);
103
- } catch (e) {
104
- op = [6, e];
105
- y = 0;
106
- } finally {
107
- f = t = 0;
108
- }
109
- if (op[0] & 5) throw op[1];
110
- return {
111
- value: op[0] ? op[1] : void 0,
112
- done: true
113
- };
114
- }
115
- };
116
- var __values = this && this.__values || function (o) {
117
- var s = typeof Symbol === "function" && Symbol.iterator,
118
- m = s && o[s],
119
- i = 0;
120
- if (m) return m.call(o);
121
- if (o && typeof o.length === "number") return {
122
- next: function next() {
123
- if (o && i >= o.length) o = void 0;
124
- return {
125
- value: o && o[i++],
126
- done: !o
127
- };
128
- }
129
- };
130
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
131
- };
132
1
  import { readFileSync, writeFile, mkdirSync } from 'fs';
133
2
  import { SyncQueue } from 'pubo-utils';
134
3
  import { v4 as uuid } from 'uuid';
4
+ function _await(value, then, direct) {
5
+ if (direct) {
6
+ return then ? then(value) : value;
7
+ }
8
+ if (!value || !value.then) {
9
+ value = Promise.resolve(value);
10
+ }
11
+ return then ? value.then(then) : value;
12
+ }
135
13
  var cluster = require('cluster');
136
14
  // 主线程的实现
137
- var Manager = /** @class */function () {
15
+
16
+ function _empty() {}
17
+ var Manager = /*#__PURE__*/function () {
138
18
  function Manager(path, defaultState) {
139
19
  var _this = this;
20
+ this.path = void 0;
140
21
  this._state = {};
141
22
  this.queue = new SyncQueue();
23
+ this.key = void 0;
24
+ this.defaultState = void 0;
142
25
  this.path = path;
143
26
  this.defaultState = defaultState;
144
27
  this.key = encodeURIComponent(path);
@@ -149,120 +32,113 @@ var Manager = /** @class */function () {
149
32
  });
150
33
  this.restore();
151
34
  }
152
- Manager.prototype.onMessage = function (message, worker) {
153
- return __awaiter(this, void 0, void 0, function () {
35
+ var _proto = Manager.prototype;
36
+ _proto.onMessage = function onMessage(message, worker) {
37
+ try {
38
+ var _this2 = this;
39
+ if (message.key !== _this2.key) {
40
+ return _await();
41
+ }
154
42
  var payload;
155
- return __generator(this, function (_a) {
156
- switch (_a.label) {
157
- case 0:
158
- if (message.key !== this.key) {
159
- return [2 /*return*/];
160
- }
161
-
162
- if (!(message.type === 'get')) return [3 /*break*/, 2];
163
- return [4 /*yield*/, this.getState()];
164
- case 1:
165
- payload = _a.sent();
166
- return [3 /*break*/, 4];
167
- case 2:
168
- if (!(message.type === 'set')) return [3 /*break*/, 4];
169
- return [4 /*yield*/, this.setState(message.payload)];
170
- case 3:
171
- payload = _a.sent();
172
- _a.label = 4;
173
- case 4:
174
- worker.send({
175
- uid: message.uid,
176
- key: this.key,
177
- payload: payload
43
+ return _await(_invoke(function () {
44
+ if (message.type === 'get') {
45
+ return _await(_this2.getState(), function (_this2$getState) {
46
+ payload = _this2$getState;
47
+ });
48
+ } else return _invokeIgnored(function () {
49
+ if (message.type === 'set') {
50
+ return _await(_this2.setState(message.payload), function (_this2$setState) {
51
+ payload = _this2$setState;
178
52
  });
179
- return [2 /*return*/];
180
- }
181
- });
182
- });
53
+ }
54
+ });
55
+ }, function () {
56
+ worker.send({
57
+ uid: message.uid,
58
+ key: _this2.key,
59
+ payload: payload
60
+ });
61
+ }));
62
+ } catch (e) {
63
+ return Promise.reject(e);
64
+ }
183
65
  };
184
-
185
- Manager.prototype.sync = function () {
186
- return __awaiter(this, void 0, void 0, function () {
187
- return __generator(this, function (_a) {
188
- switch (_a.label) {
189
- case 0:
190
- if (this.queue.length > 0) {
191
- return [2 /*return*/];
192
- }
193
-
194
- return [4 /*yield*/, this.queue.push(this._syncFile.bind(this))];
195
- case 1:
196
- _a.sent();
197
- return [2 /*return*/];
198
- }
199
- });
200
- });
66
+ _proto.sync = function sync() {
67
+ try {
68
+ var _this3 = this;
69
+ if (_this3.queue.length > 0) {
70
+ return _await();
71
+ }
72
+ return _await(_awaitIgnored(_this3.queue.push(_this3._syncFile.bind(_this3))));
73
+ } catch (e) {
74
+ return Promise.reject(e);
75
+ }
201
76
  };
202
-
203
- Manager.prototype._syncFile = function () {
204
- return __awaiter(this, void 0, void 0, function () {
205
- var _this = this;
206
- return __generator(this, function (_a) {
207
- return [2 /*return*/, new Promise(function (resolve, reject) {
208
- writeFile(_this.path, JSON.stringify(_this._state), function (err) {
209
- if (err) {
210
- reject(err);
211
- } else {
212
- resolve('');
213
- }
214
- });
215
- })];
216
- });
217
- });
77
+ _proto._syncFile = function _syncFile() {
78
+ try {
79
+ var _this4 = this;
80
+ return _await(new Promise(function (resolve, reject) {
81
+ writeFile(_this4.path, JSON.stringify(_this4._state), function (err) {
82
+ if (err) {
83
+ reject(err);
84
+ } else {
85
+ resolve('');
86
+ }
87
+ });
88
+ }));
89
+ } catch (e) {
90
+ return Promise.reject(e);
91
+ }
218
92
  };
219
- Manager.prototype.restore = function () {
220
- var _a;
93
+ _proto.restore = function restore() {
221
94
  try {
222
95
  var buf = readFileSync(this.path);
223
96
  this._state = JSON.parse(buf.toString());
224
97
  } catch (err) {
98
+ var _this$defaultState;
225
99
  var str = process.platform === 'win32' ? '\\' : '/';
226
100
  if (str) {
227
101
  mkdirSync(this.path.split(str).slice(0, -1).join(str), {
228
102
  recursive: true
229
103
  });
230
104
  }
231
- this.setState((_a = this.defaultState) !== null && _a !== void 0 ? _a : {});
105
+ this.setState((_this$defaultState = this.defaultState) != null ? _this$defaultState : {});
232
106
  }
233
107
  };
234
- Manager.prototype.getState = function () {
235
- return __awaiter(this, void 0, void 0, function () {
236
- return __generator(this, function (_a) {
237
- return [2 /*return*/, this._state];
238
- });
239
- });
108
+ _proto.getState = function getState() {
109
+ try {
110
+ var _this5 = this;
111
+ return _await(_this5._state);
112
+ } catch (e) {
113
+ return Promise.reject(e);
114
+ }
240
115
  };
241
- Manager.prototype.setState = function (values) {
242
- return __awaiter(this, void 0, void 0, function () {
243
- return __generator(this, function (_a) {
244
- switch (_a.label) {
245
- case 0:
246
- this._state = values;
247
- return [4 /*yield*/, this.sync()];
248
- case 1:
249
- _a.sent();
250
- return [2 /*return*/];
251
- }
252
- });
253
- });
116
+ _proto.setState = function setState(values) {
117
+ try {
118
+ var _this6 = this;
119
+ _this6._state = values;
120
+ return _await(_awaitIgnored(_this6.sync()));
121
+ } catch (e) {
122
+ return Promise.reject(e);
123
+ }
254
124
  };
255
-
256
125
  return Manager;
257
- }();
258
- // work 线程的实现
259
- var Worker = /** @class */function () {
126
+ }(); // work 线程的实现
127
+ function _invokeIgnored(body) {
128
+ var result = body();
129
+ if (result && result.then) {
130
+ return result.then(_empty);
131
+ }
132
+ }
133
+ var Worker = /*#__PURE__*/function () {
260
134
  function Worker(path) {
135
+ this.key = void 0;
261
136
  this.callback = {};
262
137
  this.key = encodeURIComponent(path);
263
138
  process.on('message', this.onMessage.bind(this));
264
139
  }
265
- Worker.prototype.onMessage = function (message) {
140
+ var _proto2 = Worker.prototype;
141
+ _proto2.onMessage = function onMessage(message) {
266
142
  if (message.key !== this.key) {
267
143
  return;
268
144
  }
@@ -271,168 +147,146 @@ var Worker = /** @class */function () {
271
147
  delete this.callback[message.uid];
272
148
  }
273
149
  };
274
- Worker.prototype.call = function (_a) {
275
- var type = _a.type,
276
- payload = _a.payload;
277
- return __awaiter(this, void 0, void 0, function () {
278
- var _this = this;
279
- return __generator(this, function (_b) {
280
- return [2 /*return*/, new Promise(function (resolve) {
281
- var uid = uuid();
282
- _this.callback[uid] = function (data) {
283
- return resolve(data);
284
- };
285
- // @ts-ignore
286
- process.send({
287
- uid: uid,
288
- type: type,
289
- payload: payload,
290
- key: _this.key
291
- });
292
- })];
293
- });
294
- });
150
+ _proto2.call = function call(_ref) {
151
+ var type = _ref.type,
152
+ payload = _ref.payload;
153
+ try {
154
+ var _this7 = this;
155
+ return _await(new Promise(function (resolve) {
156
+ var uid = uuid();
157
+ _this7.callback[uid] = function (data) {
158
+ return resolve(data);
159
+ };
160
+ // @ts-ignore
161
+ process.send({
162
+ uid: uid,
163
+ type: type,
164
+ payload: payload,
165
+ key: _this7.key
166
+ });
167
+ }));
168
+ } catch (e) {
169
+ return Promise.reject(e);
170
+ }
295
171
  };
296
- Worker.prototype.getState = function () {
297
- return __awaiter(this, void 0, void 0, function () {
298
- return __generator(this, function (_a) {
299
- return [2 /*return*/, this.call({
300
- type: 'get',
301
- payload: {}
302
- })];
303
- });
304
- });
172
+ _proto2.getState = function getState() {
173
+ try {
174
+ var _this8 = this;
175
+ return _await(_this8.call({
176
+ type: 'get',
177
+ payload: {}
178
+ }));
179
+ } catch (e) {
180
+ return Promise.reject(e);
181
+ }
305
182
  };
306
- Worker.prototype.setState = function (payload) {
307
- return __awaiter(this, void 0, void 0, function () {
308
- return __generator(this, function (_a) {
309
- return [2 /*return*/, this.call({
310
- type: 'set',
311
- payload: payload
312
- })];
313
- });
314
- });
183
+ _proto2.setState = function setState(payload) {
184
+ try {
185
+ var _this9 = this;
186
+ return _await(_this9.call({
187
+ type: 'set',
188
+ payload: payload
189
+ }));
190
+ } catch (e) {
191
+ return Promise.reject(e);
192
+ }
315
193
  };
316
194
  return Worker;
317
195
  }();
318
- var JsonStorage = /** @class */function () {
196
+ function _invoke(body, then) {
197
+ var result = body();
198
+ if (result && result.then) {
199
+ return result.then(then);
200
+ }
201
+ return then(result);
202
+ }
203
+ function _awaitIgnored(value, direct) {
204
+ if (!direct) {
205
+ return value && value.then ? value.then(_empty) : Promise.resolve();
206
+ }
207
+ }
208
+ export var JsonStorage = /*#__PURE__*/function () {
319
209
  function JsonStorage(path, options) {
210
+ var _options;
320
211
  if (options === void 0) {
321
212
  options = {};
322
213
  }
214
+ this.instance = void 0;
323
215
  if (cluster.isPrimary) {
324
216
  this.instance = new Manager(path, options.defaultState);
325
217
  } else {
326
218
  this.instance = new Worker(path);
327
219
  }
328
- if (options === null || options === void 0 ? void 0 : options.initialState) {
220
+ if ((_options = options) != null && _options.initialState) {
329
221
  this.merge(options.initialState);
330
222
  }
331
223
  }
332
- JsonStorage.prototype.getState = function () {
333
- return __awaiter(this, void 0, void 0, function () {
334
- return __generator(this, function (_a) {
335
- return [2 /*return*/, this.instance.getState()];
336
- });
337
- });
224
+ var _proto3 = JsonStorage.prototype;
225
+ _proto3.getState = function getState() {
226
+ try {
227
+ var _this10 = this;
228
+ return _await(_this10.instance.getState());
229
+ } catch (e) {
230
+ return Promise.reject(e);
231
+ }
338
232
  };
339
- JsonStorage.prototype.setState = function (state) {
340
- return __awaiter(this, void 0, void 0, function () {
341
- return __generator(this, function (_a) {
342
- return [2 /*return*/, this.instance.setState(state)];
343
- });
344
- });
233
+ _proto3.setState = function setState(state) {
234
+ try {
235
+ var _this11 = this;
236
+ return _await(_this11.instance.setState(state));
237
+ } catch (e) {
238
+ return Promise.reject(e);
239
+ }
345
240
  };
346
- JsonStorage.prototype.get = function (key) {
347
- return __awaiter(this, void 0, void 0, function () {
348
- var state;
349
- return __generator(this, function (_a) {
350
- switch (_a.label) {
351
- case 0:
352
- if (!!key) return [3 /*break*/, 1];
353
- return [2 /*return*/, this.getState()];
354
- case 1:
355
- return [4 /*yield*/, this.getState()];
356
- case 2:
357
- state = _a.sent();
358
- return [2 /*return*/, state[key]];
359
- }
360
- });
361
- });
241
+ _proto3.get = function get(key) {
242
+ try {
243
+ var _this12 = this;
244
+ if (!key) {
245
+ return _await(_this12.getState());
246
+ } else {
247
+ return _await(_this12.getState(), function (state) {
248
+ return state[key];
249
+ });
250
+ }
251
+ } catch (e) {
252
+ return Promise.reject(e);
253
+ }
362
254
  };
363
- JsonStorage.prototype.set = function (key, values) {
364
- return __awaiter(this, void 0, void 0, function () {
365
- var state;
366
- return __generator(this, function (_a) {
367
- switch (_a.label) {
368
- case 0:
369
- return [4 /*yield*/, this.getState()];
370
- case 1:
371
- state = _a.sent();
372
- state[key] = values;
373
- return [4 /*yield*/, this.setState(state)];
374
- case 2:
375
- _a.sent();
376
- return [2 /*return*/];
377
- }
255
+ _proto3.set = function set(key, values) {
256
+ try {
257
+ var _this13 = this;
258
+ return _await(_this13.getState(), function (state) {
259
+ state[key] = values;
260
+ return _awaitIgnored(_this13.setState(state));
378
261
  });
379
- });
262
+ } catch (e) {
263
+ return Promise.reject(e);
264
+ }
380
265
  };
381
-
382
- JsonStorage.prototype.merge = function (values) {
383
- return __awaiter(this, void 0, void 0, function () {
384
- var state, _a, _b, key;
385
- var e_1, _c;
386
- return __generator(this, function (_d) {
387
- switch (_d.label) {
388
- case 0:
389
- return [4 /*yield*/, this.getState()];
390
- case 1:
391
- state = _d.sent();
392
- try {
393
- for (_a = __values(Object.keys(values)), _b = _a.next(); !_b.done; _b = _a.next()) {
394
- key = _b.value;
395
- state[key] = values[key];
396
- }
397
- } catch (e_1_1) {
398
- e_1 = {
399
- error: e_1_1
400
- };
401
- } finally {
402
- try {
403
- if (_b && !_b.done && (_c = _a["return"])) _c.call(_a);
404
- } finally {
405
- if (e_1) throw e_1.error;
406
- }
407
- }
408
- return [4 /*yield*/, this.setState(state)];
409
- case 2:
410
- _d.sent();
411
- return [2 /*return*/];
266
+ _proto3.merge = function merge(values) {
267
+ try {
268
+ var _this14 = this;
269
+ return _await(_this14.getState(), function (state) {
270
+ for (var _i = 0, _Object$keys = Object.keys(values); _i < _Object$keys.length; _i++) {
271
+ var key = _Object$keys[_i];
272
+ state[key] = values[key];
412
273
  }
274
+ return _awaitIgnored(_this14.setState(state));
413
275
  });
414
- });
276
+ } catch (e) {
277
+ return Promise.reject(e);
278
+ }
415
279
  };
416
-
417
- JsonStorage.prototype.remove = function (key) {
418
- return __awaiter(this, void 0, void 0, function () {
419
- var state;
420
- return __generator(this, function (_a) {
421
- switch (_a.label) {
422
- case 0:
423
- return [4 /*yield*/, this.getState()];
424
- case 1:
425
- state = _a.sent();
426
- delete state[key];
427
- return [4 /*yield*/, this.setState(state)];
428
- case 2:
429
- _a.sent();
430
- return [2 /*return*/];
431
- }
280
+ _proto3.remove = function remove(key) {
281
+ try {
282
+ var _this15 = this;
283
+ return _await(_this15.getState(), function (state) {
284
+ delete state[key];
285
+ return _awaitIgnored(_this15.setState(state));
432
286
  });
433
- });
287
+ } catch (e) {
288
+ return Promise.reject(e);
289
+ }
434
290
  };
435
-
436
291
  return JsonStorage;
437
- }();
438
- export { JsonStorage };
292
+ }();