appium-mac2-driver 4.3.6 → 4.5.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.
Files changed (106) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/WebDriverAgentMac/IntegrationTests/AMSessionTests.m +14 -0
  3. package/WebDriverAgentMac/IntegrationTests/AMWindowTests.m +427 -0
  4. package/WebDriverAgentMac/WebDriverAgentLib/Commands/AMWindowCommands.m +63 -0
  5. package/WebDriverAgentMac/WebDriverAgentLib/Commands/FBDebugCommands.m +2 -0
  6. package/WebDriverAgentMac/WebDriverAgentLib/Commands/FBSessionCommands.m +17 -5
  7. package/WebDriverAgentMac/WebDriverAgentLib/Routing/FBCommandStatus.h +3 -0
  8. package/WebDriverAgentMac/WebDriverAgentLib/Routing/FBCommandStatus.m +13 -0
  9. package/WebDriverAgentMac/WebDriverAgentLib/Routing/FBSession.m +5 -1
  10. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMScreenUtils.h +4 -0
  11. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMScreenUtils.m +23 -0
  12. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMSnapshotUtils.h +18 -0
  13. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMSnapshotUtils.m +43 -28
  14. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMWindowUtils.h +111 -0
  15. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMWindowUtils.m +780 -0
  16. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMXCTAccessibility.h +61 -0
  17. package/WebDriverAgentMac/WebDriverAgentLib/Utilities/AMXCTAccessibility.m +108 -0
  18. package/WebDriverAgentMac/WebDriverAgentMac.xcodeproj/project.pbxproj +20 -0
  19. package/build/lib/commands/native-record-screen.d.ts +2 -0
  20. package/build/lib/commands/native-record-screen.d.ts.map +1 -1
  21. package/build/lib/commands/native-record-screen.js.map +1 -1
  22. package/build/lib/commands/windows.d.ts +22 -0
  23. package/build/lib/commands/windows.d.ts.map +1 -0
  24. package/build/lib/commands/windows.js +32 -0
  25. package/build/lib/commands/windows.js.map +1 -0
  26. package/build/lib/driver.d.ts +15 -0
  27. package/build/lib/driver.d.ts.map +1 -1
  28. package/build/lib/driver.js +55 -32
  29. package/build/lib/driver.js.map +1 -1
  30. package/build/lib/execute-method-map.d.ts +9 -0
  31. package/build/lib/execute-method-map.d.ts.map +1 -1
  32. package/build/lib/execute-method-map.js +9 -0
  33. package/build/lib/execute-method-map.js.map +1 -1
  34. package/build/lib/session-claim-handler.d.ts +41 -0
  35. package/build/lib/session-claim-handler.d.ts.map +1 -0
  36. package/build/lib/session-claim-handler.js +236 -0
  37. package/build/lib/session-claim-handler.js.map +1 -0
  38. package/build/lib/types.d.ts +26 -0
  39. package/build/lib/types.d.ts.map +1 -1
  40. package/build/lib/wda-mac.d.ts +6 -2
  41. package/build/lib/wda-mac.d.ts.map +1 -1
  42. package/build/lib/wda-mac.js +39 -9
  43. package/build/lib/wda-mac.js.map +1 -1
  44. package/lib/commands/native-record-screen.ts +2 -0
  45. package/lib/commands/windows.ts +44 -0
  46. package/lib/driver.ts +59 -31
  47. package/lib/execute-method-map.ts +9 -0
  48. package/lib/session-claim-handler.ts +306 -0
  49. package/lib/types.ts +28 -0
  50. package/lib/wda-mac.ts +41 -10
  51. package/node_modules/appium-xcode/CHANGELOG.md +7 -0
  52. package/node_modules/appium-xcode/package.json +4 -4
  53. package/node_modules/async-lock/AUTHORS +21 -0
  54. package/node_modules/async-lock/History.md +132 -0
  55. package/node_modules/async-lock/LICENSE +22 -0
  56. package/node_modules/async-lock/README.md +177 -0
  57. package/node_modules/async-lock/index.js +2 -0
  58. package/node_modules/async-lock/lib/index.js +312 -0
  59. package/node_modules/async-lock/package.json +48 -0
  60. package/node_modules/bare-fs/binding.c +1 -1
  61. package/node_modules/bare-fs/index.js +30 -6
  62. package/node_modules/bare-fs/package.json +1 -1
  63. package/node_modules/bare-fs/prebuilds/android-arm/bare-fs.bare +0 -0
  64. package/node_modules/bare-fs/prebuilds/android-arm64/bare-fs.bare +0 -0
  65. package/node_modules/bare-fs/prebuilds/android-ia32/bare-fs.bare +0 -0
  66. package/node_modules/bare-fs/prebuilds/android-x64/bare-fs.bare +0 -0
  67. package/node_modules/bare-fs/prebuilds/darwin-arm64/bare-fs.bare +0 -0
  68. package/node_modules/bare-fs/prebuilds/darwin-x64/bare-fs.bare +0 -0
  69. package/node_modules/bare-fs/prebuilds/ios-arm64/bare-fs.bare +0 -0
  70. package/node_modules/bare-fs/prebuilds/ios-arm64-simulator/bare-fs.bare +0 -0
  71. package/node_modules/bare-fs/prebuilds/ios-x64-simulator/bare-fs.bare +0 -0
  72. package/node_modules/bare-fs/prebuilds/linux-arm64/bare-fs.bare +0 -0
  73. package/node_modules/bare-fs/prebuilds/linux-x64/bare-fs.bare +0 -0
  74. package/node_modules/bare-fs/prebuilds/win32-arm64/bare-fs.bare +0 -0
  75. package/node_modules/bare-fs/prebuilds/win32-x64/bare-fs.bare +0 -0
  76. package/node_modules/bare-url/README.md +4 -0
  77. package/node_modules/bare-url/index.d.ts +3 -0
  78. package/node_modules/bare-url/index.js +42 -18
  79. package/node_modules/bare-url/lib/errors.js +4 -0
  80. package/node_modules/bare-url/package.json +1 -1
  81. package/node_modules/bare-url/prebuilds/android-arm/bare-url.bare +0 -0
  82. package/node_modules/bare-url/prebuilds/android-arm64/bare-url.bare +0 -0
  83. package/node_modules/bare-url/prebuilds/android-ia32/bare-url.bare +0 -0
  84. package/node_modules/bare-url/prebuilds/android-x64/bare-url.bare +0 -0
  85. package/node_modules/bare-url/prebuilds/darwin-arm64/bare-url.bare +0 -0
  86. package/node_modules/bare-url/prebuilds/darwin-x64/bare-url.bare +0 -0
  87. package/node_modules/bare-url/prebuilds/ios-arm64/bare-url.bare +0 -0
  88. package/node_modules/bare-url/prebuilds/ios-arm64-simulator/bare-url.bare +0 -0
  89. package/node_modules/bare-url/prebuilds/ios-x64-simulator/bare-url.bare +0 -0
  90. package/node_modules/bare-url/prebuilds/linux-arm64/bare-url.bare +0 -0
  91. package/node_modules/bare-url/prebuilds/linux-x64/bare-url.bare +0 -0
  92. package/node_modules/bare-url/prebuilds/win32-arm64/bare-url.bare +0 -0
  93. package/node_modules/bare-url/prebuilds/win32-x64/bare-url.bare +0 -0
  94. package/node_modules/readable-stream/lib/internal/shims/process.js +19 -0
  95. package/node_modules/readable-stream/lib/internal/streams/destroy.js +1 -1
  96. package/node_modules/readable-stream/lib/internal/streams/duplexify.js +1 -1
  97. package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +1 -1
  98. package/node_modules/readable-stream/lib/internal/streams/from.js +1 -1
  99. package/node_modules/readable-stream/lib/internal/streams/legacy.js +6 -1
  100. package/node_modules/readable-stream/lib/internal/streams/pipeline.js +1 -1
  101. package/node_modules/readable-stream/lib/internal/streams/readable.js +1 -1
  102. package/node_modules/readable-stream/lib/internal/streams/writable.js +1 -1
  103. package/node_modules/readable-stream/package.json +3 -2
  104. package/node_modules/tar-stream/package.json +7 -7
  105. package/package.json +6 -3
  106. package/scripts/setup-accessibility.mjs +175 -0
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appium-xcode",
3
- "version": "7.2.2",
3
+ "version": "7.2.3",
4
4
  "description": "Interact with Xcode",
5
5
  "keywords": [
6
6
  "appium",
@@ -44,18 +44,18 @@
44
44
  "dependencies": {
45
45
  "@appium/support": "^7.2.8",
46
46
  "asyncbox": "^6.4.3",
47
- "semver": "7.8.5",
47
+ "semver": "^7.8.5",
48
48
  "teen_process": "^4.2.1"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@appium/tsconfig": "^1.2.1",
52
52
  "@appium/types": "^1.7.1",
53
- "@types/node": "26.6.3",
53
+ "@types/node": "^26.6.3",
54
54
  "@types/semver": "^7.8.0"
55
55
  },
56
56
  "engines": {
57
57
  "node": "^20.19.0 || ^22.12.0 || >=24.0.0",
58
58
  "npm": ">=10"
59
59
  },
60
- "gitHead": "c89121221cab1f00280a93f49e601edb7f387cbf"
60
+ "gitHead": "da9df509454bca268192150e4d3302d7f000a5c9"
61
61
  }
@@ -0,0 +1,21 @@
1
+ # Authors ordered by first contribution.
2
+
3
+ rain1017 (https://github.com/rain1017)
4
+ JonathanPicques (https://github.com/JonathanPicques)
5
+ Rogier Schouten (https://github.com/rogierschouten)
6
+ erikvold (https://github.com/erikvold)
7
+ Christian Passet (https://github.com/thePunIssuer)
8
+ Tanguy Krotoff (https://github.com/tkrotoff)
9
+ jtyers (https://github.com/jtyers)
10
+ Paul Roub (https://github.com/paulroub)
11
+ Geoff Whatley (https://github.com/meatwallace)
12
+ Patryk L. (https://github.com/LosTigeros)
13
+ Julian Saunderson (https://github.com/bitrivers)
14
+ Philipp Keck (https://github.com/Philipp91)
15
+ abozaralizadeh (https://github.com/abozaralizadeh)
16
+ ThePiz (https://github.com/ThePiz)
17
+ Luke Stead (https://github.com/luke-stead-sonocent)
18
+ Simeon Borko (https://github.com/simeonborko)
19
+ Tim Schmidt (https://github.com/taschmidt)
20
+ Motty Milshtein (https://github.com/mottymilshtein)
21
+
@@ -0,0 +1,132 @@
1
+ 1.4.0
2
+ ==================
3
+ * add execution timeout (thank you @mottymilshtein)
4
+
5
+ 1.3.2
6
+ ==================
7
+ * Handle errors from user-supplied callback better (Thank you @simeonborko)
8
+ * Ensure the array parameter does not get mutated for batch acquire (Thank you @taschmidt)
9
+
10
+ 1.3.1
11
+ ==================
12
+ * Show queue name in errors (thank you @luke-stead-sonocent)
13
+
14
+ 1.3.0
15
+ ==================
16
+ * Add maxOccupationTime option (Thank you @abozaralizadeh and @ThePiz)
17
+
18
+ 1.2.8
19
+ ==================
20
+ * Fix #37 process not set when acquiring lock (Thank you @Philipp91)
21
+
22
+ 1.2.7
23
+ ==================
24
+ DO NOT USE, erroneous publish
25
+
26
+ 1.2.6
27
+ ==================
28
+ * Fix `maxPending = Infinity` no longer allowed (thank you @coderaiser)
29
+
30
+ 1.2.5
31
+ ==================
32
+ * Allow `maxPending = 0` (thank you @bmrpatel)
33
+ * Upgrade dependencies
34
+
35
+ 1.2.4
36
+ ==================
37
+ * Be robust to lock names that are also Object keys - simpler solution, extra tests.
38
+
39
+ 1.2.3
40
+ ==================
41
+ * Be robust to lock names that are also Object keys.
42
+ * Upgrade dependencies
43
+
44
+ 1.2.2
45
+ ==================
46
+ * Fix grunt-env accidentally having become a dependency (#25)
47
+
48
+ 1.2.1
49
+ ==================
50
+ * Remove empty postinstall script (#24)
51
+ * Fixed some vulnerable dev dependencies
52
+
53
+ 1.2.0
54
+ ==================
55
+ * ES5 compatibility (#21)
56
+
57
+ 1.1.4
58
+ ==================
59
+ * Fix for #17, update dependencies
60
+
61
+ 1.1.3
62
+ ==================
63
+ * Fix for #14
64
+
65
+ 1.1.1
66
+ ==================
67
+ * Fix result Promise not resolving when locking empty key array
68
+
69
+ 1.1.0 / 2017-10-17
70
+ ==================
71
+ * Add option to add waiters to the front of the queue
72
+
73
+ 1.1.0 / 2017-10-17
74
+ ==================
75
+ * Add option to add waiters to the front of the queue
76
+
77
+ 1.0.0 / 2017-06-29
78
+ ==================
79
+ * Remove dependency on Q by using the global Promise variable by default. Thank you @erikvold (https://github.com/erikvold) for the PR
80
+
81
+ 0.3.10 / 2017-06-27
82
+ ==================
83
+ * Remove dependencies on Q-specific nodify(), try(), and defer() methods so that you can inject e.g. standard ES6 promises using `new AsyncLock({ Promise: Promise })`
84
+
85
+ 0.3.9 / 2016-11-30
86
+ ==================
87
+ * Referred to MIT license in package.json
88
+ * Update NPM module dependencies, remove no-longer-maintained blanket code coverage
89
+ * Change author to rogierschouten
90
+ * Fix invalid JSDoc comments (thanks @JonathanPicques)
91
+ * Bugfix: TypeError when locking multiple keys at once
92
+
93
+ 0.3.7-0.3.8
94
+ ==================
95
+ Unknown, previous author did not commit this to Github.
96
+
97
+ 0.3.6 / 2015-09-07
98
+ ==================
99
+ * Performance improvement
100
+
101
+ 0.3.5 / 2015-06-15
102
+ ==================
103
+ * Performance improvement
104
+
105
+ 0.3.4 / 2015-06-09
106
+ ==================
107
+ * Bug fix
108
+
109
+ 0.3.3 / 2015-05-19
110
+ ==================
111
+ * Bug fix
112
+
113
+ 0.3.2 / 2015-05-08
114
+ ==================
115
+ * Set default timeout to never
116
+
117
+ 0.3.1 / 2015-04-15
118
+ ==================
119
+ * Use your own promise
120
+
121
+ 0.3.0 / 2015-03-06
122
+ ==================
123
+ * Domain reentrant
124
+
125
+ 0.2.0 / 2015-02-21
126
+ ==================
127
+ * Support promise mode
128
+ * Pending task limit
129
+
130
+ 0.1.0 / 2015-01-13
131
+ ==================
132
+ * Initial version
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rogier Schouten <github@workingcode.ninja>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,177 @@
1
+ # async-lock
2
+
3
+ Lock on asynchronous code
4
+
5
+ [![Build Status](https://travis-ci.org/rogierschouten/async-lock.svg?branch=master)](https://travis-ci.org/rogierschouten/async-lock)
6
+
7
+ * ES6 promise supported
8
+ * Multiple keys lock supported
9
+ * Timeout supported
10
+ * Occupation time limit supported
11
+ * Execution time limit supported
12
+ * Pending task limit supported
13
+ * Domain reentrant supported
14
+ * 100% code coverage
15
+
16
+ ## Disclaimer
17
+
18
+ I did not create this package, and I will not add any features to it myself. I was granted the ownership because it was no longer being
19
+ maintained, and I volunteered to fix a bug.
20
+
21
+ If you have a new feature you would like to have incorporated, please send me a PR and I will be happy to work with you and get it merged.
22
+ For any bugs, PRs are most welcome but when possible I will try to get them resolved as soon as possible.
23
+
24
+ ## Why do you need locking on single threaded nodejs?
25
+
26
+ Nodejs is single threaded, and the code execution never gets interrupted inside an event loop, so locking is unnecessary? This is true ONLY IF your critical section can be executed inside a single event loop.
27
+ However, if you have any async code inside your critical section (it can be simply triggered by any I/O operation, or timer), your critical logic will across multiple event loops, therefore it's not concurrency safe!
28
+
29
+ Consider the following code
30
+ ```js
31
+ redis.get('key', function(err, value) {
32
+ redis.set('key', value * 2);
33
+ });
34
+ ```
35
+ The above code simply multiply a redis key by 2.
36
+ However, if two users run concurrently, the execution order may like this
37
+ ```
38
+ user1: redis.get('key') -> 1
39
+ user2: redis.get('key') -> 1
40
+ user1: redis.set('key', 1 x 2) -> 2
41
+ user2: redis.set('key', 1 x 2) -> 2
42
+ ```
43
+ Obviously it's not what you expected
44
+
45
+
46
+ With asyncLock, you can easily write your async critical section
47
+ ```js
48
+ lock.acquire('key', function(cb) {
49
+ // Concurrency safe
50
+ redis.get('key', function(err, value) {
51
+ redis.set('key', value * 2, cb);
52
+ });
53
+ }, function(err, ret) {
54
+ });
55
+ ```
56
+
57
+ ## Get Started
58
+
59
+ ```js
60
+ var AsyncLock = require('async-lock');
61
+ var lock = new AsyncLock();
62
+
63
+ /**
64
+ * @param {String|Array} key resource key or keys to lock
65
+ * @param {function} fn execute function
66
+ * @param {function} cb (optional) callback function, otherwise will return a promise
67
+ * @param {Object} opts (optional) options
68
+ */
69
+ lock.acquire(key, function(done) {
70
+ // async work
71
+ done(err, ret);
72
+ }, function(err, ret) {
73
+ // lock released
74
+ }, opts);
75
+
76
+ // Promise mode
77
+ lock.acquire(key, function() {
78
+ // return value or promise
79
+ }, opts).then(function() {
80
+ // lock released
81
+ });
82
+ ```
83
+
84
+ ## Error Handling
85
+
86
+ ```js
87
+ // Callback mode
88
+ lock.acquire(key, function(done) {
89
+ done(new Error('error'));
90
+ }, function(err, ret) {
91
+ console.log(err.message) // output: error
92
+ });
93
+
94
+ // Promise mode
95
+ lock.acquire(key, function() {
96
+ throw new Error('error');
97
+ }).catch(function(err) {
98
+ console.log(err.message) // output: error
99
+ });
100
+ ```
101
+
102
+ ## Acquire multiple keys
103
+
104
+ ```js
105
+ lock.acquire([key1, key2], fn, cb);
106
+ ```
107
+
108
+ ## Domain reentrant lock
109
+
110
+ Lock is reentrant in the same domain
111
+
112
+ ```js
113
+ var domain = require('domain');
114
+ var lock = new AsyncLock({domainReentrant : true});
115
+
116
+ var d = domain.create();
117
+ d.run(function() {
118
+ lock.acquire('key', function() {
119
+ //Enter lock
120
+ return lock.acquire('key', function() {
121
+ //Enter same lock twice
122
+ });
123
+ });
124
+ });
125
+ ```
126
+
127
+ ## Options
128
+
129
+ ```js
130
+ // Specify timeout - max amount of time an item can remain in the queue before acquiring the lock
131
+ var lock = new AsyncLock({timeout: 5000});
132
+ lock.acquire(key, fn, function(err, ret) {
133
+ // timed out error will be returned here if lock not acquired in given time
134
+ });
135
+
136
+ // Specify max occupation time - max amount of time allowed between entering the queue and completing execution
137
+ var lock = new AsyncLock({maxOccupationTime: 3000});
138
+ lock.acquire(key, fn, function(err, ret) {
139
+ // occupation time exceeded error will be returned here if job not completed in given time
140
+ });
141
+
142
+ // Specify max execution time - max amount of time allowed between acquiring the lock and completing execution
143
+ var lock = new AsyncLock({maxExecutionTime: 3000});
144
+ lock.acquire(key, fn, function(err, ret) {
145
+ // execution time exceeded error will be returned here if job not completed in given time
146
+ });
147
+
148
+ // Set max pending tasks - max number of tasks allowed in the queue at a time
149
+ var lock = new AsyncLock({maxPending: 1000});
150
+ lock.acquire(key, fn, function(err, ret) {
151
+ // Handle too much pending error
152
+ })
153
+
154
+ // Whether there is any running or pending async function
155
+ lock.isBusy();
156
+
157
+ // Use your own promise library instead of the global Promise variable
158
+ var lock = new AsyncLock({Promise: require('bluebird')}); // Bluebird
159
+ var lock = new AsyncLock({Promise: require('q')}); // Q
160
+
161
+ // Add a task to the front of the queue waiting for a given lock
162
+ lock.acquire(key, fn1, cb); // runs immediately
163
+ lock.acquire(key, fn2, cb); // added to queue
164
+ lock.acquire(key, priorityFn, cb, {skipQueue: true}); // jumps queue and runs before fn2
165
+ ```
166
+
167
+ ## Changelog
168
+
169
+ See [Changelog](./History.md)
170
+
171
+ ## Issues
172
+
173
+ See [issue tracker](https://github.com/rogierschouten/async-lock/issues).
174
+
175
+ ## License
176
+
177
+ MIT, see [LICENSE](./LICENSE)
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+ module.exports = require('./lib');
@@ -0,0 +1,312 @@
1
+ 'use strict';
2
+
3
+ var AsyncLock = function (opts) {
4
+ opts = opts || {};
5
+
6
+ this.Promise = opts.Promise || Promise;
7
+
8
+ // format: {key : [fn, fn]}
9
+ // queues[key] = null indicates no job running for key
10
+ this.queues = Object.create(null);
11
+
12
+ // lock is reentrant for same domain
13
+ this.domainReentrant = opts.domainReentrant || false;
14
+ if (this.domainReentrant) {
15
+ if (typeof process === 'undefined' || typeof process.domain === 'undefined') {
16
+ throw new Error(
17
+ 'Domain-reentrant locks require `process.domain` to exist. Please flip `opts.domainReentrant = false`, ' +
18
+ 'use a NodeJS version that still implements Domain, or install a browser polyfill.');
19
+ }
20
+ // domain of current running func {key : fn}
21
+ this.domains = Object.create(null);
22
+ }
23
+
24
+ this.timeout = opts.timeout || AsyncLock.DEFAULT_TIMEOUT;
25
+ this.maxOccupationTime = opts.maxOccupationTime || AsyncLock.DEFAULT_MAX_OCCUPATION_TIME;
26
+ this.maxExecutionTime = opts.maxExecutionTime || AsyncLock.DEFAULT_MAX_EXECUTION_TIME;
27
+ if (opts.maxPending === Infinity || (Number.isInteger(opts.maxPending) && opts.maxPending >= 0)) {
28
+ this.maxPending = opts.maxPending;
29
+ } else {
30
+ this.maxPending = AsyncLock.DEFAULT_MAX_PENDING;
31
+ }
32
+ };
33
+
34
+ AsyncLock.DEFAULT_TIMEOUT = 0; //Never
35
+ AsyncLock.DEFAULT_MAX_OCCUPATION_TIME = 0; //Never
36
+ AsyncLock.DEFAULT_MAX_EXECUTION_TIME = 0; //Never
37
+ AsyncLock.DEFAULT_MAX_PENDING = 1000;
38
+
39
+ /**
40
+ * Acquire Locks
41
+ *
42
+ * @param {String|Array} key resource key or keys to lock
43
+ * @param {function} fn async function
44
+ * @param {function} cb callback function, otherwise will return a promise
45
+ * @param {Object} opts options
46
+ */
47
+ AsyncLock.prototype.acquire = function (key, fn, cb, opts) {
48
+ if (Array.isArray(key)) {
49
+ return this._acquireBatch(key, fn, cb, opts);
50
+ }
51
+
52
+ if (typeof (fn) !== 'function') {
53
+ throw new Error('You must pass a function to execute');
54
+ }
55
+
56
+ // faux-deferred promise using new Promise() (as Promise.defer is deprecated)
57
+ var deferredResolve = null;
58
+ var deferredReject = null;
59
+ var deferred = null;
60
+
61
+ if (typeof (cb) !== 'function') {
62
+ opts = cb;
63
+ cb = null;
64
+
65
+ // will return a promise
66
+ deferred = new this.Promise(function(resolve, reject) {
67
+ deferredResolve = resolve;
68
+ deferredReject = reject;
69
+ });
70
+ }
71
+
72
+ opts = opts || {};
73
+
74
+ var resolved = false;
75
+ var timer = null;
76
+ var occupationTimer = null;
77
+ var executionTimer = null;
78
+ var self = this;
79
+
80
+ var done = function (locked, err, ret) {
81
+
82
+ if (occupationTimer) {
83
+ clearTimeout(occupationTimer);
84
+ occupationTimer = null;
85
+ }
86
+
87
+ if (executionTimer) {
88
+ clearTimeout(executionTimer);
89
+ executionTimer = null;
90
+ }
91
+
92
+ if (locked) {
93
+ if (!!self.queues[key] && self.queues[key].length === 0) {
94
+ delete self.queues[key];
95
+ }
96
+ if (self.domainReentrant) {
97
+ delete self.domains[key];
98
+ }
99
+ }
100
+
101
+ if (!resolved) {
102
+ if (!deferred) {
103
+ if (typeof (cb) === 'function') {
104
+ cb(err, ret);
105
+ }
106
+ }
107
+ else {
108
+ //promise mode
109
+ if (err) {
110
+ deferredReject(err);
111
+ }
112
+ else {
113
+ deferredResolve(ret);
114
+ }
115
+ }
116
+ resolved = true;
117
+ }
118
+
119
+ if (locked) {
120
+ //run next func
121
+ if (!!self.queues[key] && self.queues[key].length > 0) {
122
+ self.queues[key].shift()();
123
+ }
124
+ }
125
+ };
126
+
127
+ var exec = function (locked) {
128
+ if (resolved) { // may due to timed out
129
+ return done(locked);
130
+ }
131
+
132
+ if (timer) {
133
+ clearTimeout(timer);
134
+ timer = null;
135
+ }
136
+
137
+ if (self.domainReentrant && locked) {
138
+ self.domains[key] = process.domain;
139
+ }
140
+
141
+ var maxExecutionTime = opts.maxExecutionTime || self.maxExecutionTime;
142
+ if (maxExecutionTime) {
143
+ executionTimer = setTimeout(function () {
144
+ if (!!self.queues[key]) {
145
+ done(locked, new Error('Maximum execution time is exceeded ' + key));
146
+ }
147
+ }, maxExecutionTime);
148
+ }
149
+
150
+ // Callback mode
151
+ if (fn.length === 1) {
152
+ var called = false;
153
+ try {
154
+ fn(function (err, ret) {
155
+ if (!called) {
156
+ called = true;
157
+ done(locked, err, ret);
158
+ }
159
+ });
160
+ } catch (err) {
161
+ // catching error thrown in user function fn
162
+ if (!called) {
163
+ called = true;
164
+ done(locked, err);
165
+ }
166
+ }
167
+ }
168
+ else {
169
+ // Promise mode
170
+ self._promiseTry(function () {
171
+ return fn();
172
+ })
173
+ .then(function(ret){
174
+ done(locked, undefined, ret);
175
+ }, function(error){
176
+ done(locked, error);
177
+ });
178
+ }
179
+ };
180
+
181
+ if (self.domainReentrant && !!process.domain) {
182
+ exec = process.domain.bind(exec);
183
+ }
184
+
185
+ var maxPending = opts.maxPending || self.maxPending;
186
+
187
+ if (!self.queues[key]) {
188
+ self.queues[key] = [];
189
+ exec(true);
190
+ }
191
+ else if (self.domainReentrant && !!process.domain && process.domain === self.domains[key]) {
192
+ // If code is in the same domain of current running task, run it directly
193
+ // Since lock is re-enterable
194
+ exec(false);
195
+ }
196
+ else if (self.queues[key].length >= maxPending) {
197
+ done(false, new Error('Too many pending tasks in queue ' + key));
198
+ }
199
+ else {
200
+ var taskFn = function () {
201
+ exec(true);
202
+ };
203
+ if (opts.skipQueue) {
204
+ self.queues[key].unshift(taskFn);
205
+ } else {
206
+ self.queues[key].push(taskFn);
207
+ }
208
+
209
+ var timeout = opts.timeout || self.timeout;
210
+ if (timeout) {
211
+ timer = setTimeout(function () {
212
+ timer = null;
213
+ done(false, new Error('async-lock timed out in queue ' + key));
214
+ }, timeout);
215
+ }
216
+ }
217
+
218
+ var maxOccupationTime = opts.maxOccupationTime || self.maxOccupationTime;
219
+ if (maxOccupationTime) {
220
+ occupationTimer = setTimeout(function () {
221
+ if (!!self.queues[key]) {
222
+ done(false, new Error('Maximum occupation time is exceeded in queue ' + key));
223
+ }
224
+ }, maxOccupationTime);
225
+ }
226
+
227
+ if (deferred) {
228
+ return deferred;
229
+ }
230
+ };
231
+
232
+ /*
233
+ * Below is how this function works:
234
+ *
235
+ * Equivalent code:
236
+ * self.acquire(key1, function(cb){
237
+ * self.acquire(key2, function(cb){
238
+ * self.acquire(key3, fn, cb);
239
+ * }, cb);
240
+ * }, cb);
241
+ *
242
+ * Equivalent code:
243
+ * var fn3 = getFn(key3, fn);
244
+ * var fn2 = getFn(key2, fn3);
245
+ * var fn1 = getFn(key1, fn2);
246
+ * fn1(cb);
247
+ */
248
+ AsyncLock.prototype._acquireBatch = function (keys, fn, cb, opts) {
249
+ if (typeof (cb) !== 'function') {
250
+ opts = cb;
251
+ cb = null;
252
+ }
253
+
254
+ var self = this;
255
+ var getFn = function (key, fn) {
256
+ return function (cb) {
257
+ self.acquire(key, fn, cb, opts);
258
+ };
259
+ };
260
+
261
+ var fnx = keys.reduceRight(function (prev, key) {
262
+ return getFn(key, prev);
263
+ }, fn);
264
+
265
+ if (typeof (cb) === 'function') {
266
+ fnx(cb);
267
+ }
268
+ else {
269
+ return new this.Promise(function (resolve, reject) {
270
+ // check for promise mode in case keys is empty array
271
+ if (fnx.length === 1) {
272
+ fnx(function (err, ret) {
273
+ if (err) {
274
+ reject(err);
275
+ }
276
+ else {
277
+ resolve(ret);
278
+ }
279
+ });
280
+ } else {
281
+ resolve(fnx());
282
+ }
283
+ });
284
+ }
285
+ };
286
+
287
+ /*
288
+ * Whether there is any running or pending asyncFunc
289
+ *
290
+ * @param {String} key
291
+ */
292
+ AsyncLock.prototype.isBusy = function (key) {
293
+ if (!key) {
294
+ return Object.keys(this.queues).length > 0;
295
+ }
296
+ else {
297
+ return !!this.queues[key];
298
+ }
299
+ };
300
+
301
+ /**
302
+ * Promise.try() implementation to become independent of Q-specific methods
303
+ */
304
+ AsyncLock.prototype._promiseTry = function(fn) {
305
+ try {
306
+ return this.Promise.resolve(fn());
307
+ } catch (e) {
308
+ return this.Promise.reject(e);
309
+ }
310
+ };
311
+
312
+ module.exports = AsyncLock;