1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const debug = require('debug')('cloudant:plugins:base');
|
17 | const lockFile = require('lockfile');
|
18 | const tmp = require('tmp');
|
19 |
|
20 | function noop() {}
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | class BasePlugin {
|
29 | constructor(client, cfg) {
|
30 | this._client = client;
|
31 | this._cfg = cfg;
|
32 | this._disabled = false;
|
33 | this._lockFile = tmp.tmpNameSync({ postfix: '.lock' });
|
34 | }
|
35 |
|
36 | get id() {
|
37 | return this.constructor.id;
|
38 | }
|
39 |
|
40 | get disabled() {
|
41 | return this._disabled;
|
42 | }
|
43 |
|
44 |
|
45 | set disabled(disabled) {
|
46 | this._disabled = disabled;
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 | onRequest(state, request, callback) {
|
52 | callback(state);
|
53 | }
|
54 |
|
55 | onResponse(state, response, callback) {
|
56 | callback(state);
|
57 | }
|
58 |
|
59 | onError(state, error, callback) {
|
60 | callback(state);
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | withLock(opts, callback) {
|
68 | var self = this;
|
69 | if (typeof opts === 'function') {
|
70 | callback = opts;
|
71 | opts = {};
|
72 | }
|
73 | debug('Acquiring lock...');
|
74 | lockFile.lock(self._lockFile, opts, function(error) {
|
75 | if (error) {
|
76 | callback(error, noop);
|
77 | } else {
|
78 | callback(null, function() {
|
79 |
|
80 | lockFile.unlock(self._lockFile, function(error) {
|
81 | if (error) {
|
82 | debug(`Failed to release lock: ${error}`);
|
83 | }
|
84 | });
|
85 | });
|
86 | }
|
87 | });
|
88 | }
|
89 | }
|
90 |
|
91 | BasePlugin.id = 'base';
|
92 | BasePlugin.pluginVersion = 2;
|
93 |
|
94 | module.exports = BasePlugin;
|