browsed 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Gemfile.lock +2 -2
  4. data/README.md +12 -17
  5. data/bin/browse +13 -10
  6. data/lib/browsed.rb +9 -2
  7. data/lib/browsed/browser.rb +82 -0
  8. data/lib/browsed/chrome.rb +21 -12
  9. data/lib/browsed/client.rb +48 -130
  10. data/lib/browsed/constants.rb +7 -1
  11. data/lib/browsed/errors.rb +1 -0
  12. data/lib/browsed/extensions/hash.rb +27 -0
  13. data/lib/browsed/firefox.rb +18 -17
  14. data/lib/browsed/manager.rb +5 -5
  15. data/lib/browsed/{poltergeist.rb → phantomjs.rb} +2 -2
  16. data/lib/browsed/proxies.rb +26 -0
  17. data/lib/browsed/{extensions → proxies}/chrome/proxy_authentication.rb +3 -3
  18. data/lib/browsed/proxies/proxy-chain-server/node_modules/.bin/portastic +1 -0
  19. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/LICENSE +21 -0
  20. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/README.md +57 -0
  21. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/changelog.md +1 -0
  22. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.js +3824 -0
  23. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.core.min.js +31 -0
  24. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.js +5666 -0
  25. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
  26. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/any.js +21 -0
  27. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/assert.js +55 -0
  28. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/async.js +165 -0
  29. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bind.js +67 -0
  30. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/bluebird.js +11 -0
  31. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/call_get.js +123 -0
  32. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/cancel.js +129 -0
  33. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/catch_filter.js +42 -0
  34. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/context.js +69 -0
  35. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/debuggability.js +934 -0
  36. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/direct_resolve.js +46 -0
  37. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/each.js +30 -0
  38. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/errors.js +116 -0
  39. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/es5.js +80 -0
  40. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/filter.js +12 -0
  41. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/finally.js +146 -0
  42. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/generators.js +223 -0
  43. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/join.js +168 -0
  44. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/map.js +168 -0
  45. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/method.js +55 -0
  46. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeback.js +51 -0
  47. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/nodeify.js +58 -0
  48. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise.js +789 -0
  49. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promise_array.js +185 -0
  50. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/promisify.js +314 -0
  51. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/props.js +118 -0
  52. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/queue.js +73 -0
  53. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/race.js +49 -0
  54. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/reduce.js +172 -0
  55. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/schedule.js +62 -0
  56. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/settle.js +43 -0
  57. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/some.js +148 -0
  58. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/synchronous_inspection.js +103 -0
  59. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/thenables.js +86 -0
  60. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/timers.js +93 -0
  61. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/using.js +226 -0
  62. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/js/release/util.js +389 -0
  63. data/lib/browsed/proxies/proxy-chain-server/node_modules/bluebird/package.json +102 -0
  64. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/CHANGELOG.md +408 -0
  65. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/LICENSE +22 -0
  66. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/Readme.md +428 -0
  67. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/index.js +1224 -0
  68. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/package.json +70 -0
  69. data/lib/browsed/proxies/proxy-chain-server/node_modules/commander/typings/index.d.ts +310 -0
  70. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.coveralls.yml +1 -0
  71. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.eslintrc +11 -0
  72. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.npmignore +9 -0
  73. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/.travis.yml +14 -0
  74. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/CHANGELOG.md +362 -0
  75. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/LICENSE +19 -0
  76. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/Makefile +50 -0
  77. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/README.md +312 -0
  78. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/component.json +19 -0
  79. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/karma.conf.js +70 -0
  80. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/node.js +1 -0
  81. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/package.json +88 -0
  82. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/browser.js +185 -0
  83. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/debug.js +202 -0
  84. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/index.js +10 -0
  85. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/inspector-log.js +15 -0
  86. data/lib/browsed/proxies/proxy-chain-server/node_modules/debug/src/node.js +248 -0
  87. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/index.js +152 -0
  88. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/license.md +21 -0
  89. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/package.json +69 -0
  90. data/lib/browsed/proxies/proxy-chain-server/node_modules/ms/readme.md +51 -0
  91. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/LICENSE +13 -0
  92. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/README.md +148 -0
  93. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/bin/portastic +65 -0
  94. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/history.md +7 -0
  95. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/index.js +4 -0
  96. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/monitor.js +59 -0
  97. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/lib/portastic.js +167 -0
  98. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/LICENSE +21 -0
  99. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/README.md +677 -0
  100. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/changelog.md +1730 -0
  101. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.js +4892 -0
  102. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/browser/bluebird.min.js +31 -0
  103. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/any.js +21 -0
  104. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/assert.js +55 -0
  105. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/async.js +150 -0
  106. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bind.js +72 -0
  107. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/bluebird.js +11 -0
  108. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/call_get.js +123 -0
  109. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/cancel.js +48 -0
  110. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/captured_trace.js +493 -0
  111. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/catch_filter.js +66 -0
  112. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/context.js +38 -0
  113. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/debuggability.js +162 -0
  114. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/direct_resolve.js +63 -0
  115. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/each.js +12 -0
  116. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/errors.js +111 -0
  117. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/es5.js +80 -0
  118. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/filter.js +12 -0
  119. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/finally.js +98 -0
  120. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/generators.js +136 -0
  121. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/join.js +107 -0
  122. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/map.js +133 -0
  123. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/method.js +44 -0
  124. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/nodeify.js +59 -0
  125. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/progress.js +76 -0
  126. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise.js +759 -0
  127. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_array.js +142 -0
  128. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promise_resolver.js +123 -0
  129. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/promisify.js +307 -0
  130. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/props.js +79 -0
  131. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/queue.js +90 -0
  132. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/race.js +47 -0
  133. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/reduce.js +148 -0
  134. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/schedule.js +35 -0
  135. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/settle.js +40 -0
  136. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/some.js +125 -0
  137. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/synchronous_inspection.js +94 -0
  138. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/thenables.js +84 -0
  139. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/timers.js +64 -0
  140. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/using.js +213 -0
  141. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/js/main/util.js +321 -0
  142. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/node_modules/bluebird/package.json +103 -0
  143. data/lib/browsed/proxies/proxy-chain-server/node_modules/portastic/package.json +69 -0
  144. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/CHANGELOG.md +109 -0
  145. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/LICENSE +201 -0
  146. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/README.md +278 -0
  147. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/anonymize_proxy.js +100 -0
  148. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_base.js +319 -0
  149. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_custom_response.js +96 -0
  150. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_forward.js +173 -0
  151. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_chain.js +132 -0
  152. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_direct.js +89 -0
  153. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/handler_tunnel_tcp_chain.js +271 -0
  154. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/index.js +26 -0
  155. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/run_locally.js +66 -0
  156. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/server.js +595 -0
  157. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tcp_tunnel.js +138 -0
  158. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/testing_tcp_service.js +37 -0
  159. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/build/tools.js +220 -0
  160. data/lib/browsed/proxies/proxy-chain-server/node_modules/proxy-chain/package.json +104 -0
  161. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/LICENSE +23 -0
  162. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/README.md +28 -0
  163. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/package.json +81 -0
  164. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js +5 -0
  165. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore-min.js.map +1 -0
  166. data/lib/browsed/proxies/proxy-chain-server/node_modules/underscore/underscore.js +1692 -0
  167. data/lib/browsed/proxies/proxy-chain-server/package-lock.json +63 -0
  168. data/lib/browsed/proxies/proxy-chain-server/package.json +15 -0
  169. data/lib/browsed/proxies/proxy-chain-server/server.js +25 -0
  170. data/lib/browsed/proxies/proxy_chain.rb +36 -0
  171. data/lib/browsed/version.rb +1 -1
  172. metadata +160 -4
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ module.exports = function(Promise, INTERNAL, tryConvertToPromise,
3
+ apiRejection) {
4
+ var util = require("./util.js");
5
+ var isArray = util.isArray;
6
+
7
+ function toResolutionValue(val) {
8
+ switch(val) {
9
+ case -2: return [];
10
+ case -3: return {};
11
+ }
12
+ }
13
+
14
+ function PromiseArray(values) {
15
+ var promise = this._promise = new Promise(INTERNAL);
16
+ var parent;
17
+ if (values instanceof Promise) {
18
+ parent = values;
19
+ promise._propagateFrom(parent, 1 | 4);
20
+ }
21
+ this._values = values;
22
+ this._length = 0;
23
+ this._totalResolved = 0;
24
+ this._init(undefined, -2);
25
+ }
26
+ PromiseArray.prototype.length = function () {
27
+ return this._length;
28
+ };
29
+
30
+ PromiseArray.prototype.promise = function () {
31
+ return this._promise;
32
+ };
33
+
34
+ PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
35
+ var values = tryConvertToPromise(this._values, this._promise);
36
+ if (values instanceof Promise) {
37
+ values = values._target();
38
+ this._values = values;
39
+ if (values._isFulfilled()) {
40
+ values = values._value();
41
+ if (!isArray(values)) {
42
+ var err = new Promise.TypeError("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
43
+ this.__hardReject__(err);
44
+ return;
45
+ }
46
+ } else if (values._isPending()) {
47
+ values._then(
48
+ init,
49
+ this._reject,
50
+ undefined,
51
+ this,
52
+ resolveValueIfEmpty
53
+ );
54
+ return;
55
+ } else {
56
+ this._reject(values._reason());
57
+ return;
58
+ }
59
+ } else if (!isArray(values)) {
60
+ this._promise._reject(apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a")._reason());
61
+ return;
62
+ }
63
+
64
+ if (values.length === 0) {
65
+ if (resolveValueIfEmpty === -5) {
66
+ this._resolveEmptyArray();
67
+ }
68
+ else {
69
+ this._resolve(toResolutionValue(resolveValueIfEmpty));
70
+ }
71
+ return;
72
+ }
73
+ var len = this.getActualLength(values.length);
74
+ this._length = len;
75
+ this._values = this.shouldCopyValues() ? new Array(len) : this._values;
76
+ var promise = this._promise;
77
+ for (var i = 0; i < len; ++i) {
78
+ var isResolved = this._isResolved();
79
+ var maybePromise = tryConvertToPromise(values[i], promise);
80
+ if (maybePromise instanceof Promise) {
81
+ maybePromise = maybePromise._target();
82
+ if (isResolved) {
83
+ maybePromise._ignoreRejections();
84
+ } else if (maybePromise._isPending()) {
85
+ maybePromise._proxyPromiseArray(this, i);
86
+ } else if (maybePromise._isFulfilled()) {
87
+ this._promiseFulfilled(maybePromise._value(), i);
88
+ } else {
89
+ this._promiseRejected(maybePromise._reason(), i);
90
+ }
91
+ } else if (!isResolved) {
92
+ this._promiseFulfilled(maybePromise, i);
93
+ }
94
+ }
95
+ };
96
+
97
+ PromiseArray.prototype._isResolved = function () {
98
+ return this._values === null;
99
+ };
100
+
101
+ PromiseArray.prototype._resolve = function (value) {
102
+ this._values = null;
103
+ this._promise._fulfill(value);
104
+ };
105
+
106
+ PromiseArray.prototype.__hardReject__ =
107
+ PromiseArray.prototype._reject = function (reason) {
108
+ this._values = null;
109
+ this._promise._rejectCallback(reason, false, true);
110
+ };
111
+
112
+ PromiseArray.prototype._promiseProgressed = function (progressValue, index) {
113
+ this._promise._progress({
114
+ index: index,
115
+ value: progressValue
116
+ });
117
+ };
118
+
119
+
120
+ PromiseArray.prototype._promiseFulfilled = function (value, index) {
121
+ this._values[index] = value;
122
+ var totalResolved = ++this._totalResolved;
123
+ if (totalResolved >= this._length) {
124
+ this._resolve(this._values);
125
+ }
126
+ };
127
+
128
+ PromiseArray.prototype._promiseRejected = function (reason, index) {
129
+ this._totalResolved++;
130
+ this._reject(reason);
131
+ };
132
+
133
+ PromiseArray.prototype.shouldCopyValues = function () {
134
+ return true;
135
+ };
136
+
137
+ PromiseArray.prototype.getActualLength = function (len) {
138
+ return len;
139
+ };
140
+
141
+ return PromiseArray;
142
+ };
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var util = require("./util.js");
3
+ var maybeWrapAsError = util.maybeWrapAsError;
4
+ var errors = require("./errors.js");
5
+ var TimeoutError = errors.TimeoutError;
6
+ var OperationalError = errors.OperationalError;
7
+ var haveGetters = util.haveGetters;
8
+ var es5 = require("./es5.js");
9
+
10
+ function isUntypedError(obj) {
11
+ return obj instanceof Error &&
12
+ es5.getPrototypeOf(obj) === Error.prototype;
13
+ }
14
+
15
+ var rErrorKey = /^(?:name|message|stack|cause)$/;
16
+ function wrapAsOperationalError(obj) {
17
+ var ret;
18
+ if (isUntypedError(obj)) {
19
+ ret = new OperationalError(obj);
20
+ ret.name = obj.name;
21
+ ret.message = obj.message;
22
+ ret.stack = obj.stack;
23
+ var keys = es5.keys(obj);
24
+ for (var i = 0; i < keys.length; ++i) {
25
+ var key = keys[i];
26
+ if (!rErrorKey.test(key)) {
27
+ ret[key] = obj[key];
28
+ }
29
+ }
30
+ return ret;
31
+ }
32
+ util.markAsOriginatingFromRejection(obj);
33
+ return obj;
34
+ }
35
+
36
+ function nodebackForPromise(promise) {
37
+ return function(err, value) {
38
+ if (promise === null) return;
39
+
40
+ if (err) {
41
+ var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
42
+ promise._attachExtraTrace(wrapped);
43
+ promise._reject(wrapped);
44
+ } else if (arguments.length > 2) {
45
+ var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
46
+ promise._fulfill(args);
47
+ } else {
48
+ promise._fulfill(value);
49
+ }
50
+
51
+ promise = null;
52
+ };
53
+ }
54
+
55
+
56
+ var PromiseResolver;
57
+ if (!haveGetters) {
58
+ PromiseResolver = function (promise) {
59
+ this.promise = promise;
60
+ this.asCallback = nodebackForPromise(promise);
61
+ this.callback = this.asCallback;
62
+ };
63
+ }
64
+ else {
65
+ PromiseResolver = function (promise) {
66
+ this.promise = promise;
67
+ };
68
+ }
69
+ if (haveGetters) {
70
+ var prop = {
71
+ get: function() {
72
+ return nodebackForPromise(this.promise);
73
+ }
74
+ };
75
+ es5.defineProperty(PromiseResolver.prototype, "asCallback", prop);
76
+ es5.defineProperty(PromiseResolver.prototype, "callback", prop);
77
+ }
78
+
79
+ PromiseResolver._nodebackForPromise = nodebackForPromise;
80
+
81
+ PromiseResolver.prototype.toString = function () {
82
+ return "[object PromiseResolver]";
83
+ };
84
+
85
+ PromiseResolver.prototype.resolve =
86
+ PromiseResolver.prototype.fulfill = function (value) {
87
+ if (!(this instanceof PromiseResolver)) {
88
+ throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
89
+ }
90
+ this.promise._resolveCallback(value);
91
+ };
92
+
93
+ PromiseResolver.prototype.reject = function (reason) {
94
+ if (!(this instanceof PromiseResolver)) {
95
+ throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
96
+ }
97
+ this.promise._rejectCallback(reason);
98
+ };
99
+
100
+ PromiseResolver.prototype.progress = function (value) {
101
+ if (!(this instanceof PromiseResolver)) {
102
+ throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
103
+ }
104
+ this.promise._progress(value);
105
+ };
106
+
107
+ PromiseResolver.prototype.cancel = function (err) {
108
+ this.promise.cancel(err);
109
+ };
110
+
111
+ PromiseResolver.prototype.timeout = function () {
112
+ this.reject(new TimeoutError("timeout"));
113
+ };
114
+
115
+ PromiseResolver.prototype.isResolved = function () {
116
+ return this.promise.isResolved();
117
+ };
118
+
119
+ PromiseResolver.prototype.toJSON = function () {
120
+ return this.promise.toJSON();
121
+ };
122
+
123
+ module.exports = PromiseResolver;
@@ -0,0 +1,307 @@
1
+ "use strict";
2
+ module.exports = function(Promise, INTERNAL) {
3
+ var THIS = {};
4
+ var util = require("./util.js");
5
+ var nodebackForPromise = require("./promise_resolver.js")
6
+ ._nodebackForPromise;
7
+ var withAppended = util.withAppended;
8
+ var maybeWrapAsError = util.maybeWrapAsError;
9
+ var canEvaluate = util.canEvaluate;
10
+ var TypeError = require("./errors").TypeError;
11
+ var defaultSuffix = "Async";
12
+ var defaultPromisified = {__isPromisified__: true};
13
+ var noCopyProps = [
14
+ "arity", "length",
15
+ "name",
16
+ "arguments",
17
+ "caller",
18
+ "callee",
19
+ "prototype",
20
+ "__isPromisified__"
21
+ ];
22
+ var noCopyPropsPattern = new RegExp("^(?:" + noCopyProps.join("|") + ")$");
23
+
24
+ var defaultFilter = function(name) {
25
+ return util.isIdentifier(name) &&
26
+ name.charAt(0) !== "_" &&
27
+ name !== "constructor";
28
+ };
29
+
30
+ function propsFilter(key) {
31
+ return !noCopyPropsPattern.test(key);
32
+ }
33
+
34
+ function isPromisified(fn) {
35
+ try {
36
+ return fn.__isPromisified__ === true;
37
+ }
38
+ catch (e) {
39
+ return false;
40
+ }
41
+ }
42
+
43
+ function hasPromisified(obj, key, suffix) {
44
+ var val = util.getDataPropertyOrDefault(obj, key + suffix,
45
+ defaultPromisified);
46
+ return val ? isPromisified(val) : false;
47
+ }
48
+ function checkValid(ret, suffix, suffixRegexp) {
49
+ for (var i = 0; i < ret.length; i += 2) {
50
+ var key = ret[i];
51
+ if (suffixRegexp.test(key)) {
52
+ var keyWithoutAsyncSuffix = key.replace(suffixRegexp, "");
53
+ for (var j = 0; j < ret.length; j += 2) {
54
+ if (ret[j] === keyWithoutAsyncSuffix) {
55
+ throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/iWrZbw\u000a"
56
+ .replace("%s", suffix));
57
+ }
58
+ }
59
+ }
60
+ }
61
+ }
62
+
63
+ function promisifiableMethods(obj, suffix, suffixRegexp, filter) {
64
+ var keys = util.inheritedDataKeys(obj);
65
+ var ret = [];
66
+ for (var i = 0; i < keys.length; ++i) {
67
+ var key = keys[i];
68
+ var value = obj[key];
69
+ var passesDefaultFilter = filter === defaultFilter
70
+ ? true : defaultFilter(key, value, obj);
71
+ if (typeof value === "function" &&
72
+ !isPromisified(value) &&
73
+ !hasPromisified(obj, key, suffix) &&
74
+ filter(key, value, obj, passesDefaultFilter)) {
75
+ ret.push(key, value);
76
+ }
77
+ }
78
+ checkValid(ret, suffix, suffixRegexp);
79
+ return ret;
80
+ }
81
+
82
+ var escapeIdentRegex = function(str) {
83
+ return str.replace(/([$])/, "\\$");
84
+ };
85
+
86
+ var makeNodePromisifiedEval;
87
+ if (!false) {
88
+ var switchCaseArgumentOrder = function(likelyArgumentCount) {
89
+ var ret = [likelyArgumentCount];
90
+ var min = Math.max(0, likelyArgumentCount - 1 - 3);
91
+ for(var i = likelyArgumentCount - 1; i >= min; --i) {
92
+ ret.push(i);
93
+ }
94
+ for(var i = likelyArgumentCount + 1; i <= 3; ++i) {
95
+ ret.push(i);
96
+ }
97
+ return ret;
98
+ };
99
+
100
+ var argumentSequence = function(argumentCount) {
101
+ return util.filledRange(argumentCount, "_arg", "");
102
+ };
103
+
104
+ var parameterDeclaration = function(parameterCount) {
105
+ return util.filledRange(
106
+ Math.max(parameterCount, 3), "_arg", "");
107
+ };
108
+
109
+ var parameterCount = function(fn) {
110
+ if (typeof fn.length === "number") {
111
+ return Math.max(Math.min(fn.length, 1023 + 1), 0);
112
+ }
113
+ return 0;
114
+ };
115
+
116
+ makeNodePromisifiedEval =
117
+ function(callback, receiver, originalName, fn) {
118
+ var newParameterCount = Math.max(0, parameterCount(fn) - 1);
119
+ var argumentOrder = switchCaseArgumentOrder(newParameterCount);
120
+ var shouldProxyThis = typeof callback === "string" || receiver === THIS;
121
+
122
+ function generateCallForArgumentCount(count) {
123
+ var args = argumentSequence(count).join(", ");
124
+ var comma = count > 0 ? ", " : "";
125
+ var ret;
126
+ if (shouldProxyThis) {
127
+ ret = "ret = callback.call(this, {{args}}, nodeback); break;\n";
128
+ } else {
129
+ ret = receiver === undefined
130
+ ? "ret = callback({{args}}, nodeback); break;\n"
131
+ : "ret = callback.call(receiver, {{args}}, nodeback); break;\n";
132
+ }
133
+ return ret.replace("{{args}}", args).replace(", ", comma);
134
+ }
135
+
136
+ function generateArgumentSwitchCase() {
137
+ var ret = "";
138
+ for (var i = 0; i < argumentOrder.length; ++i) {
139
+ ret += "case " + argumentOrder[i] +":" +
140
+ generateCallForArgumentCount(argumentOrder[i]);
141
+ }
142
+
143
+ ret += " \n\
144
+ default: \n\
145
+ var args = new Array(len + 1); \n\
146
+ var i = 0; \n\
147
+ for (var i = 0; i < len; ++i) { \n\
148
+ args[i] = arguments[i]; \n\
149
+ } \n\
150
+ args[i] = nodeback; \n\
151
+ [CodeForCall] \n\
152
+ break; \n\
153
+ ".replace("[CodeForCall]", (shouldProxyThis
154
+ ? "ret = callback.apply(this, args);\n"
155
+ : "ret = callback.apply(receiver, args);\n"));
156
+ return ret;
157
+ }
158
+
159
+ var getFunctionCode = typeof callback === "string"
160
+ ? ("this != null ? this['"+callback+"'] : fn")
161
+ : "fn";
162
+
163
+ return new Function("Promise",
164
+ "fn",
165
+ "receiver",
166
+ "withAppended",
167
+ "maybeWrapAsError",
168
+ "nodebackForPromise",
169
+ "tryCatch",
170
+ "errorObj",
171
+ "notEnumerableProp",
172
+ "INTERNAL","'use strict'; \n\
173
+ var ret = function (Parameters) { \n\
174
+ 'use strict'; \n\
175
+ var len = arguments.length; \n\
176
+ var promise = new Promise(INTERNAL); \n\
177
+ promise._captureStackTrace(); \n\
178
+ var nodeback = nodebackForPromise(promise); \n\
179
+ var ret; \n\
180
+ var callback = tryCatch([GetFunctionCode]); \n\
181
+ switch(len) { \n\
182
+ [CodeForSwitchCase] \n\
183
+ } \n\
184
+ if (ret === errorObj) { \n\
185
+ promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\
186
+ } \n\
187
+ return promise; \n\
188
+ }; \n\
189
+ notEnumerableProp(ret, '__isPromisified__', true); \n\
190
+ return ret; \n\
191
+ "
192
+ .replace("Parameters", parameterDeclaration(newParameterCount))
193
+ .replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
194
+ .replace("[GetFunctionCode]", getFunctionCode))(
195
+ Promise,
196
+ fn,
197
+ receiver,
198
+ withAppended,
199
+ maybeWrapAsError,
200
+ nodebackForPromise,
201
+ util.tryCatch,
202
+ util.errorObj,
203
+ util.notEnumerableProp,
204
+ INTERNAL
205
+ );
206
+ };
207
+ }
208
+
209
+ function makeNodePromisifiedClosure(callback, receiver, _, fn) {
210
+ var defaultThis = (function() {return this;})();
211
+ var method = callback;
212
+ if (typeof method === "string") {
213
+ callback = fn;
214
+ }
215
+ function promisified() {
216
+ var _receiver = receiver;
217
+ if (receiver === THIS) _receiver = this;
218
+ var promise = new Promise(INTERNAL);
219
+ promise._captureStackTrace();
220
+ var cb = typeof method === "string" && this !== defaultThis
221
+ ? this[method] : callback;
222
+ var fn = nodebackForPromise(promise);
223
+ try {
224
+ cb.apply(_receiver, withAppended(arguments, fn));
225
+ } catch(e) {
226
+ promise._rejectCallback(maybeWrapAsError(e), true, true);
227
+ }
228
+ return promise;
229
+ }
230
+ util.notEnumerableProp(promisified, "__isPromisified__", true);
231
+ return promisified;
232
+ }
233
+
234
+ var makeNodePromisified = canEvaluate
235
+ ? makeNodePromisifiedEval
236
+ : makeNodePromisifiedClosure;
237
+
238
+ function promisifyAll(obj, suffix, filter, promisifier) {
239
+ var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$");
240
+ var methods =
241
+ promisifiableMethods(obj, suffix, suffixRegexp, filter);
242
+
243
+ for (var i = 0, len = methods.length; i < len; i+= 2) {
244
+ var key = methods[i];
245
+ var fn = methods[i+1];
246
+ var promisifiedKey = key + suffix;
247
+ if (promisifier === makeNodePromisified) {
248
+ obj[promisifiedKey] =
249
+ makeNodePromisified(key, THIS, key, fn, suffix);
250
+ } else {
251
+ var promisified = promisifier(fn, function() {
252
+ return makeNodePromisified(key, THIS, key, fn, suffix);
253
+ });
254
+ util.notEnumerableProp(promisified, "__isPromisified__", true);
255
+ obj[promisifiedKey] = promisified;
256
+ }
257
+ }
258
+ util.toFastProperties(obj);
259
+ return obj;
260
+ }
261
+
262
+ function promisify(callback, receiver) {
263
+ return makeNodePromisified(callback, receiver, undefined, callback);
264
+ }
265
+
266
+ Promise.promisify = function (fn, receiver) {
267
+ if (typeof fn !== "function") {
268
+ throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
269
+ }
270
+ if (isPromisified(fn)) {
271
+ return fn;
272
+ }
273
+ var ret = promisify(fn, arguments.length < 2 ? THIS : receiver);
274
+ util.copyDescriptors(fn, ret, propsFilter);
275
+ return ret;
276
+ };
277
+
278
+ Promise.promisifyAll = function (target, options) {
279
+ if (typeof target !== "function" && typeof target !== "object") {
280
+ throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/9ITlV0\u000a");
281
+ }
282
+ options = Object(options);
283
+ var suffix = options.suffix;
284
+ if (typeof suffix !== "string") suffix = defaultSuffix;
285
+ var filter = options.filter;
286
+ if (typeof filter !== "function") filter = defaultFilter;
287
+ var promisifier = options.promisifier;
288
+ if (typeof promisifier !== "function") promisifier = makeNodePromisified;
289
+
290
+ if (!util.isIdentifier(suffix)) {
291
+ throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/8FZo5V\u000a");
292
+ }
293
+
294
+ var keys = util.inheritedDataKeys(target);
295
+ for (var i = 0; i < keys.length; ++i) {
296
+ var value = target[keys[i]];
297
+ if (keys[i] !== "constructor" &&
298
+ util.isClass(value)) {
299
+ promisifyAll(value.prototype, suffix, filter, promisifier);
300
+ promisifyAll(value, suffix, filter, promisifier);
301
+ }
302
+ }
303
+
304
+ return promisifyAll(target, suffix, filter, promisifier);
305
+ };
306
+ };
307
+