passenger 5.0.21 → 5.0.22

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of passenger might be problematic. Click here for more details.

Files changed (203) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.asc +7 -7
  3. data.tar.gz.asc +7 -7
  4. data/CHANGELOG +38 -2
  5. data/CONTRIBUTORS +3 -0
  6. data/README.md +5 -1
  7. data/bin/passenger-status +2 -7
  8. data/build/agent.rb +2 -0
  9. data/build/cxx_dependency_map.rb +1464 -162
  10. data/build/node_tests.rb +1 -1
  11. data/build/packaging.rb +1 -1
  12. data/dev/ci/run_travis.sh +1 -1
  13. data/dev/vagrant/provision.sh +1 -1
  14. data/npm-shrinkwrap.json +635 -125
  15. data/package.json +1 -1
  16. data/resources/templates/standalone/config.erb +33 -161
  17. data/resources/templates/standalone/global.erb +15 -0
  18. data/resources/templates/standalone/http.erb +40 -0
  19. data/resources/templates/standalone/mass_deployment_default_server.erb +11 -0
  20. data/resources/templates/standalone/rails_asset_pipeline.erb +22 -0
  21. data/resources/templates/standalone/server.erb +58 -0
  22. data/src/agent/Core/ApiServer.h +37 -32
  23. data/src/agent/Core/ApplicationPool/BasicGroupInfo.h +1 -1
  24. data/src/agent/Core/ApplicationPool/BasicProcessInfo.h +6 -0
  25. data/src/agent/Core/ApplicationPool/Common.h +1 -1
  26. data/src/agent/Core/ApplicationPool/Group/OutOfBandWork.cpp +1 -1
  27. data/src/agent/Core/ApplicationPool/Group/ProcessListManagement.cpp +4 -0
  28. data/src/agent/Core/ApplicationPool/Implementation.cpp +2 -2
  29. data/src/agent/Core/ApplicationPool/Options.h +18 -1
  30. data/src/agent/Core/ApplicationPool/Pool.h +1 -1
  31. data/src/agent/Core/ApplicationPool/Pool/AnalyticsCollection.cpp +6 -6
  32. data/src/agent/Core/ApplicationPool/Pool/GeneralUtils.cpp +3 -3
  33. data/src/agent/Core/ApplicationPool/Pool/InitializationAndShutdown.cpp +1 -1
  34. data/src/agent/Core/ApplicationPool/Pool/Miscellaneous.cpp +6 -0
  35. data/src/agent/Core/ApplicationPool/Process.h +6 -1
  36. data/src/agent/Core/ApplicationPool/Socket.h +9 -12
  37. data/src/agent/Core/Controller.h +422 -0
  38. data/src/agent/Core/{RequestHandler → Controller}/AppResponse.h +2 -0
  39. data/src/agent/Core/{RequestHandler → Controller}/BufferBody.cpp +27 -5
  40. data/src/agent/Core/{RequestHandler → Controller}/CheckoutSession.cpp +61 -31
  41. data/src/agent/Core/{RequestHandler → Controller}/Client.h +5 -2
  42. data/src/agent/Core/{RequestHandler → Controller}/ForwardResponse.cpp +93 -42
  43. data/src/agent/Core/{RequestHandler → Controller}/Hooks.cpp +107 -60
  44. data/src/agent/Core/Controller/Implementation.cpp +38 -0
  45. data/src/agent/Core/{RequestHandler → Controller}/InitRequest.cpp +134 -80
  46. data/src/agent/Core/Controller/InitializationAndShutdown.cpp +165 -0
  47. data/src/agent/Core/{RequestHandler/Utils.cpp → Controller/InternalUtils.cpp} +49 -32
  48. data/src/agent/Core/Controller/Miscellaneous.cpp +116 -0
  49. data/src/agent/Core/{RequestHandler → Controller}/Request.h +6 -4
  50. data/src/agent/Core/{RequestHandler → Controller}/SendRequest.cpp +205 -130
  51. data/src/agent/Core/Controller/StateInspectionAndConfiguration.cpp +161 -0
  52. data/src/agent/Core/{RequestHandler → Controller}/TurboCaching.h +3 -0
  53. data/src/agent/Core/CoreMain.cpp +62 -51
  54. data/src/agent/Core/OptionParser.h +24 -0
  55. data/src/agent/Core/ResponseCache.h +5 -5
  56. data/src/agent/Core/SpawningKit/Config.h +2 -2
  57. data/src/agent/Core/SpawningKit/SmartSpawner.h +1 -1
  58. data/src/agent/Core/UnionStation/{Core.h → Context.h} +14 -18
  59. data/src/agent/Core/UnionStation/StopwatchLog.h +3 -2
  60. data/src/agent/Core/UnionStation/Transaction.h +7 -7
  61. data/src/agent/Shared/ApiServerUtils.h +9 -1
  62. data/src/agent/UstRouter/ApiServer.h +5 -2
  63. data/src/agent/UstRouter/Controller.h +27 -9
  64. data/src/agent/UstRouter/UstRouterMain.cpp +1 -0
  65. data/src/agent/Watchdog/ApiServer.h +5 -2
  66. data/src/apache2_module/ConfigurationCommands.cpp +7 -0
  67. data/src/apache2_module/ConfigurationFields.hpp +2 -0
  68. data/src/apache2_module/ConfigurationSetters.cpp +24 -0
  69. data/src/apache2_module/CreateDirConfig.cpp +1 -0
  70. data/src/apache2_module/Hooks.cpp +6 -5
  71. data/src/apache2_module/MergeDirConfig.cpp +7 -0
  72. data/src/apache2_module/SetHeaders.cpp +5 -0
  73. data/src/cxx_supportlib/Constants.h +5 -3
  74. data/src/cxx_supportlib/SafeLibev.h +2 -1
  75. data/src/cxx_supportlib/UnionStationFilterSupport.h +2 -1
  76. data/src/cxx_supportlib/Utils/ReleaseableScopedPointer.h +70 -0
  77. data/src/cxx_supportlib/vendor-modified/boost/libs/regex/src/regex_raw_buffer.cpp +6 -6
  78. data/src/helper-scripts/node-loader.js +59 -0
  79. data/src/nginx_module/CacheLocationConfig.c +48 -0
  80. data/src/nginx_module/ConfigurationCommands.c +20 -0
  81. data/src/nginx_module/ConfigurationFields.h +4 -0
  82. data/src/nginx_module/CreateLocationConfig.c +8 -0
  83. data/src/nginx_module/MergeLocationConfig.c +12 -0
  84. data/src/nodejs_supportlib/phusion_passenger/log_express.js +106 -0
  85. data/src/nodejs_supportlib/phusion_passenger/log_mongodb.js +203 -0
  86. data/src/nodejs_supportlib/phusion_passenger/ustreporter.js +227 -0
  87. data/src/nodejs_supportlib/phusion_passenger/ustrouter_connector.js +446 -0
  88. data/src/nodejs_supportlib/vendor-copy/codify/codify.js +44 -0
  89. data/src/nodejs_supportlib/vendor-copy/codify/package.json +29 -0
  90. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/context.js +200 -0
  91. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/async-listener/glue.js +488 -0
  92. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/async-listener/index.js +407 -0
  93. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/async-listener/node_modules/shimmer/index.js +90 -0
  94. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/async-listener/node_modules/shimmer/package.json +40 -0
  95. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/async-listener/package.json +54 -0
  96. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/emitter-listener/listener.js +160 -0
  97. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/emitter-listener/node_modules/shimmer/index.js +90 -0
  98. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/emitter-listener/node_modules/shimmer/package.json +40 -0
  99. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/node_modules/emitter-listener/package.json +46 -0
  100. data/src/nodejs_supportlib/vendor-copy/continuation-local-storage/package.json +56 -0
  101. data/src/nodejs_supportlib/vendor-copy/network-byte-order/lib/index.js +102 -0
  102. data/src/nodejs_supportlib/vendor-copy/network-byte-order/package.json +51 -0
  103. data/src/nodejs_supportlib/vendor-copy/winston/LICENSE +19 -0
  104. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston.js +165 -0
  105. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/common.js +483 -0
  106. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/config.js +62 -0
  107. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/config/cli-config.js +35 -0
  108. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/config/npm-config.js +27 -0
  109. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/config/syslog-config.js +31 -0
  110. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/container.js +127 -0
  111. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/exception.js +56 -0
  112. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/logger.js +701 -0
  113. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports.js +34 -0
  114. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/console.js +128 -0
  115. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/daily-rotate-file.js +601 -0
  116. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/file.js +675 -0
  117. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/http.js +232 -0
  118. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/memory.js +89 -0
  119. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/transport.js +135 -0
  120. data/src/nodejs_supportlib/vendor-copy/winston/lib/winston/transports/webhook.js +146 -0
  121. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/async/LICENSE +19 -0
  122. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/async/lib/async.js +1283 -0
  123. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/async/package.json +66 -0
  124. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/async/support/sync-package-managers.js +53 -0
  125. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/MIT-LICENSE.txt +23 -0
  126. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/colors.js +176 -0
  127. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/custom/trap.js +45 -0
  128. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/custom/zalgo.js +104 -0
  129. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/extendStringPrototype.js +118 -0
  130. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/index.js +12 -0
  131. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/maps/america.js +12 -0
  132. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/maps/rainbow.js +13 -0
  133. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/maps/random.js +8 -0
  134. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/maps/zebra.js +5 -0
  135. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/styles.js +77 -0
  136. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/lib/system/supports-colors.js +61 -0
  137. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/package.json +35 -0
  138. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/safe.js +9 -0
  139. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/colors/themes/generic-logging.js +12 -0
  140. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/cycle/cycle.js +170 -0
  141. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/cycle/package.json +30 -0
  142. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/eyes/LICENSE +20 -0
  143. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/eyes/lib/eyes.js +236 -0
  144. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/eyes/package.json +42 -0
  145. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/isstream/LICENSE.md +11 -0
  146. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/isstream/isstream.js +27 -0
  147. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/isstream/package.json +42 -0
  148. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/pkginfo/LICENSE +19 -0
  149. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/pkginfo/lib/pkginfo.js +136 -0
  150. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/pkginfo/package.json +56 -0
  151. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/stack-trace/License +19 -0
  152. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/stack-trace/lib/stack-trace.js +111 -0
  153. data/src/nodejs_supportlib/vendor-copy/winston/node_modules/stack-trace/package.json +33 -0
  154. data/src/nodejs_supportlib/vendor-copy/winston/package.json +87 -0
  155. data/src/ruby_supportlib/phusion_passenger.rb +3 -4
  156. data/src/ruby_supportlib/phusion_passenger/admin_tools/instance.rb +19 -2
  157. data/src/ruby_supportlib/phusion_passenger/apache2/config_options.rb +8 -0
  158. data/src/ruby_supportlib/phusion_passenger/config/list_instances_command.rb +2 -6
  159. data/src/ruby_supportlib/phusion_passenger/config/reopen_logs_command.rb +13 -0
  160. data/src/ruby_supportlib/phusion_passenger/config/restart_app_command.rb +3 -0
  161. data/src/ruby_supportlib/phusion_passenger/config/utils.rb +9 -7
  162. data/src/ruby_supportlib/phusion_passenger/config/validate_install_command.rb +8 -1
  163. data/src/ruby_supportlib/phusion_passenger/constants.rb +3 -1
  164. data/src/ruby_supportlib/phusion_passenger/native_support.rb +20 -4
  165. data/src/ruby_supportlib/phusion_passenger/nginx/config_options.rb +8 -0
  166. data/src/ruby_supportlib/phusion_passenger/packaging.rb +1 -0
  167. data/src/ruby_supportlib/phusion_passenger/platform_info/apache_detector.rb +1 -1
  168. data/src/ruby_supportlib/phusion_passenger/platform_info/operating_system.rb +11 -4
  169. data/src/ruby_supportlib/phusion_passenger/rack/thread_handler_extension.rb +1 -1
  170. data/src/ruby_supportlib/phusion_passenger/standalone/config_options_list.rb +647 -0
  171. data/src/ruby_supportlib/phusion_passenger/standalone/config_utils.rb +229 -15
  172. data/src/ruby_supportlib/phusion_passenger/standalone/start_command.rb +35 -323
  173. data/src/ruby_supportlib/phusion_passenger/standalone/start_command/builtin_engine.rb +36 -12
  174. data/src/ruby_supportlib/phusion_passenger/standalone/start_command/nginx_engine.rb +123 -14
  175. data/src/ruby_supportlib/phusion_passenger/standalone/status_command.rb +32 -17
  176. data/src/ruby_supportlib/phusion_passenger/standalone/stop_command.rb +32 -21
  177. data/src/ruby_supportlib/phusion_passenger/standalone/version_command.rb +5 -5
  178. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/README.md +2 -2
  179. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/Rakefile +5 -1
  180. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core.rb +68 -24
  181. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/api.rb +9 -1
  182. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/context.rb +9 -7
  183. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/request_reporter.rb +3 -2
  184. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/request_reporter/basics.rb +8 -5
  185. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/simple_json.rb +395 -0
  186. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb +10 -7
  187. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/utils.rb +14 -0
  188. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/version_data.rb +2 -2
  189. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/ruby_versions.yml +4 -2
  190. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/ruby_versions.yml.example +2 -2
  191. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/ruby_versions.yml.travis +2 -2
  192. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/ruby_versions.yml.travis-with-sudo +16 -0
  193. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/Gemfile +1 -1
  194. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/Gemfile.lock +2 -2
  195. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/README.md +1 -1
  196. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/lib/union_station_hooks_rails.rb +44 -17
  197. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/lib/union_station_hooks_rails/active_support_cache_subscriber.rb +16 -7
  198. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/lib/union_station_hooks_rails/version_data.rb +2 -2
  199. data/src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_rails/union_station_hooks_rails.gemspec +1 -1
  200. metadata +99 -16
  201. metadata.gz.asc +7 -7
  202. data/resources/oss-binaries.phusionpassenger.com.crt +0 -248
  203. data/src/agent/Core/RequestHandler.h +0 -471
@@ -0,0 +1,29 @@
1
+ {
2
+ "author": {
3
+ "name": "Andrew J. Stone",
4
+ "email": "andrew.j.stone.1@bozuko.com"
5
+ },
6
+ "name": "codify",
7
+ "description": "Turn integers into base36 codes",
8
+ "version": "0.3.0",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git://github.com/bozuko/codify.git"
12
+ },
13
+ "main": "codify.js",
14
+ "engines": {
15
+ "node": "*"
16
+ },
17
+ "dependencies": {},
18
+ "devDependencies": {},
19
+ "readme": "Codify lets you transform integers to base36 strings and vice versa. All strings use uppercase letters.\n\n## Example\n\n var codify = require('codify');\n var code = codify.toCode(10); // 'A'\n var testInt = codify.toInt(code); // 10\n\n // Zero Extend\n code = codify.toCode(0, 5); // '00000'",
20
+ "readmeFilename": "README.md",
21
+ "bugs": {
22
+ "url": "https://github.com/bozuko/codify/issues"
23
+ },
24
+ "homepage": "https://github.com/bozuko/codify",
25
+ "_id": "codify@0.3.0",
26
+ "_shasum": "5489ce3b1c087fab99536d25afbb4b14e4d762f1",
27
+ "_from": "codify@^0.3.0",
28
+ "_resolved": "https://registry.npmjs.org/codify/-/codify-0.3.0.tgz"
29
+ }
@@ -0,0 +1,200 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var wrapEmitter = require('emitter-listener');
5
+
6
+ /*
7
+ *
8
+ * CONSTANTS
9
+ *
10
+ */
11
+ var CONTEXTS_SYMBOL = 'cls@contexts';
12
+ var ERROR_SYMBOL = 'error@context';
13
+
14
+ // load polyfill if native support is unavailable
15
+ if (!process.addAsyncListener) require('async-listener');
16
+
17
+ function Namespace(name) {
18
+ this.name = name;
19
+ // changed in 2.7: no default context
20
+ this.active = null;
21
+ this._set = [];
22
+ this.id = null;
23
+ }
24
+
25
+ Namespace.prototype.set = function (key, value) {
26
+ if (!this.active) {
27
+ throw new Error("No context available. ns.run() or ns.bind() must be called first.");
28
+ }
29
+
30
+ this.active[key] = value;
31
+ return value;
32
+ };
33
+
34
+ Namespace.prototype.get = function (key) {
35
+ if (!this.active) return undefined;
36
+
37
+ return this.active[key];
38
+ };
39
+
40
+ Namespace.prototype.createContext = function () {
41
+ return Object.create(this.active);
42
+ };
43
+
44
+ Namespace.prototype.run = function (fn) {
45
+ var context = this.createContext();
46
+ this.enter(context);
47
+ try {
48
+ fn(context);
49
+ return context;
50
+ }
51
+ catch (exception) {
52
+ exception[ERROR_SYMBOL] = context;
53
+ throw exception;
54
+ }
55
+ finally {
56
+ this.exit(context);
57
+ }
58
+ };
59
+
60
+ Namespace.prototype.bind = function (fn, context) {
61
+ if (!context) {
62
+ if (!this.active) {
63
+ context = Object.create(this.active);
64
+ }
65
+ else {
66
+ context = this.active;
67
+ }
68
+ }
69
+
70
+ var self = this;
71
+ return function () {
72
+ self.enter(context);
73
+ try {
74
+ return fn.apply(this, arguments);
75
+ }
76
+ catch (exception) {
77
+ exception[ERROR_SYMBOL] = context;
78
+ throw exception;
79
+ }
80
+ finally {
81
+ self.exit(context);
82
+ }
83
+ };
84
+ };
85
+
86
+ Namespace.prototype.enter = function (context) {
87
+ assert.ok(context, "context must be provided for entering");
88
+
89
+ this._set.push(this.active);
90
+ this.active = context;
91
+ };
92
+
93
+ Namespace.prototype.exit = function (context) {
94
+ assert.ok(context, "context must be provided for exiting");
95
+
96
+ // Fast path for most exits that are at the top of the stack
97
+ if (this.active === context) {
98
+ assert.ok(this._set.length, "can't remove top context");
99
+ this.active = this._set.pop();
100
+ return;
101
+ }
102
+
103
+ // Fast search in the stack using lastIndexOf
104
+ var index = this._set.lastIndexOf(context);
105
+
106
+ assert.ok(index >= 0, "context not currently entered; can't exit");
107
+ assert.ok(index, "can't remove top context");
108
+
109
+ this._set.splice(index, 1);
110
+ };
111
+
112
+ Namespace.prototype.bindEmitter = function (emitter) {
113
+ assert.ok(emitter.on && emitter.addListener && emitter.emit, "can only bind real EEs");
114
+
115
+ var namespace = this;
116
+ var thisSymbol = 'context@' + this.name;
117
+
118
+ // Capture the context active at the time the emitter is bound.
119
+ function attach(listener) {
120
+ if (!listener) return;
121
+ if (!listener[CONTEXTS_SYMBOL]) listener[CONTEXTS_SYMBOL] = Object.create(null);
122
+
123
+ listener[CONTEXTS_SYMBOL][thisSymbol] = {
124
+ namespace : namespace,
125
+ context : namespace.active
126
+ };
127
+ }
128
+
129
+ // At emit time, bind the listener within the correct context.
130
+ function bind(unwrapped) {
131
+ if (!(unwrapped && unwrapped[CONTEXTS_SYMBOL])) return unwrapped;
132
+
133
+ var wrapped = unwrapped;
134
+ var contexts = unwrapped[CONTEXTS_SYMBOL];
135
+ Object.keys(contexts).forEach(function (name) {
136
+ var thunk = contexts[name];
137
+ wrapped = thunk.namespace.bind(wrapped, thunk.context);
138
+ });
139
+ return wrapped;
140
+ }
141
+
142
+ wrapEmitter(emitter, attach, bind);
143
+ };
144
+
145
+ /**
146
+ * If an error comes out of a namespace, it will have a context attached to it.
147
+ * This function knows how to find it.
148
+ *
149
+ * @param {Error} exception Possibly annotated error.
150
+ */
151
+ Namespace.prototype.fromException = function (exception) {
152
+ return exception[ERROR_SYMBOL];
153
+ };
154
+
155
+ function get(name) {
156
+ return process.namespaces[name];
157
+ }
158
+
159
+ function create(name) {
160
+ assert.ok(name, "namespace must be given a name!");
161
+
162
+ var namespace = new Namespace(name);
163
+ namespace.id = process.addAsyncListener({
164
+ create : function () { return namespace.active; },
165
+ before : function (context, storage) { if (storage) namespace.enter(storage); },
166
+ after : function (context, storage) { if (storage) namespace.exit(storage); },
167
+ error : function (storage) { if (storage) namespace.exit(storage); }
168
+ });
169
+
170
+ process.namespaces[name] = namespace;
171
+ return namespace;
172
+ }
173
+
174
+ function destroy(name) {
175
+ var namespace = get(name);
176
+
177
+ assert.ok(namespace, "can't delete nonexistent namespace!");
178
+ assert.ok(namespace.id, "don't assign to process.namespaces directly!");
179
+
180
+ process.removeAsyncListener(namespace.id);
181
+ process.namespaces[name] = null;
182
+ }
183
+
184
+ function reset() {
185
+ // must unregister async listeners
186
+ if (process.namespaces) {
187
+ Object.keys(process.namespaces).forEach(function (name) {
188
+ destroy(name);
189
+ });
190
+ }
191
+ process.namespaces = Object.create(null);
192
+ }
193
+ if (!process.namespaces) reset(); // call immediately to set up
194
+
195
+ module.exports = {
196
+ getNamespace : get,
197
+ createNamespace : create,
198
+ destroyNamespace : destroy,
199
+ reset : reset
200
+ };
@@ -0,0 +1,488 @@
1
+ var wrap = require('shimmer').wrap;
2
+
3
+ /*
4
+ *
5
+ * CONSTANTS
6
+ *
7
+ */
8
+ var HAS_CREATE_AL = 1 << 0;
9
+ var HAS_BEFORE_AL = 1 << 1;
10
+ var HAS_AFTER_AL = 1 << 2;
11
+ var HAS_ERROR_AL = 1 << 3;
12
+
13
+ /**
14
+ * There is one list of currently active listeners that is mutated in place by
15
+ * addAsyncListener and removeAsyncListener. This complicates error-handling,
16
+ * for reasons that are discussed below.
17
+ */
18
+ var listeners = [];
19
+
20
+ /**
21
+ * There can be multiple listeners with the same properties, so disambiguate
22
+ * them by assigning them an ID at creation time.
23
+ */
24
+ var uid = 0;
25
+
26
+ /**
27
+ * Ensure that errors coming from within listeners are handed off to domains,
28
+ * process._fatalException, or uncaughtException without being treated like
29
+ * user errors.
30
+ */
31
+ var inAsyncTick = false;
32
+
33
+ /**
34
+ * Because asynchronous contexts can be nested, and errors can come from anywhere
35
+ * in the stack, a little extra work is required to keep track of where in the
36
+ * nesting we are. Because JS arrays are frequently mutated in place
37
+ */
38
+ var listenerStack = [];
39
+
40
+ /**
41
+ * The error handler on a listener can capture errors thrown during synchronous
42
+ * execution immediately after the listener is added. To capture both
43
+ * synchronous and asynchronous errors, the error handler just uses the
44
+ * "global" list of active listeners, and the rest of the code ensures that the
45
+ * listener list is correct by using a stack of listener lists during
46
+ * asynchronous execution.
47
+ */
48
+ var asyncCatcher;
49
+
50
+ /**
51
+ * The guts of the system -- called each time an asynchronous event happens
52
+ * while one or more listeners are active.
53
+ */
54
+ var asyncWrap;
55
+
56
+ /**
57
+ * Simple helper function that's probably faster than using Array
58
+ * filter methods and can be inlined.
59
+ */
60
+ function union(dest, added) {
61
+ var destLength = dest.length;
62
+ var addedLength = added.length;
63
+ var returned = [];
64
+
65
+ if (destLength === 0 && addedLength === 0) return returned;
66
+
67
+ for (var j = 0; j < destLength; j++) returned[j] = dest[j];
68
+
69
+ if (addedLength === 0) return returned;
70
+
71
+ for (var i = 0; i < addedLength; i++) {
72
+ var missing = true;
73
+ for (j = 0; j < destLength; j++) {
74
+ if (dest[j].uid === added[i].uid) {
75
+ missing = false;
76
+ break;
77
+ }
78
+ }
79
+ if (missing) returned.push(added[i]);
80
+ }
81
+
82
+ return returned;
83
+ }
84
+
85
+ /*
86
+ * For performance, split error-handlers and asyncCatcher up into two separate
87
+ * code paths.
88
+ */
89
+
90
+ // 0.9+
91
+ if (process._fatalException) {
92
+ /**
93
+ * Error handlers on listeners can throw, the the catcher needs to be able to
94
+ * discriminate between exceptions thrown by user code, and exceptions coming
95
+ * from within the catcher itself. Use a global to keep track of which state
96
+ * the catcher is currently in.
97
+ */
98
+ var inErrorTick = false;
99
+
100
+ /**
101
+ * Throwing always happens synchronously. If the current array of values for
102
+ * the current list of asyncListeners is put in a module-scoped variable right
103
+ * before a call that can throw, it will always be correct when the error
104
+ * handlers are run.
105
+ */
106
+ var errorValues;
107
+
108
+ asyncCatcher = function asyncCatcher(er) {
109
+ var length = listeners.length;
110
+ if (inErrorTick || length === 0) return false;
111
+
112
+ var handled = false;
113
+
114
+ /*
115
+ * error handlers
116
+ */
117
+ inErrorTick = true;
118
+ for (var i = 0; i < length; ++i) {
119
+ var listener = listeners[i];
120
+ if ((listener.flags & HAS_ERROR_AL) === 0) continue;
121
+
122
+ var value = errorValues && errorValues[listener.uid];
123
+ handled = listener.error(value, er) || handled;
124
+ }
125
+ inErrorTick = false;
126
+
127
+ /* Test whether there are any listener arrays on the stack. In the case of
128
+ * synchronous throws when the listener is active, there may have been
129
+ * none pushed yet.
130
+ */
131
+ if (listenerStack.length > 0) listeners = listenerStack.pop();
132
+ errorValues = undefined;
133
+
134
+ return handled && !inAsyncTick;
135
+ };
136
+
137
+ asyncWrap = function asyncWrap(original, list, length) {
138
+ var values = [];
139
+
140
+ /*
141
+ * listeners
142
+ */
143
+ inAsyncTick = true;
144
+ for (var i = 0; i < length; ++i) {
145
+ var listener = list[i];
146
+ values[listener.uid] = listener.data;
147
+
148
+ if ((listener.flags & HAS_CREATE_AL) === 0) continue;
149
+
150
+ var value = listener.create(listener.data);
151
+ if (value !== undefined) values[listener.uid] = value;
152
+ }
153
+ inAsyncTick = false;
154
+
155
+ /* One of the main differences between this polyfill and the core
156
+ * asyncListener support is that core avoids creating closures by putting a
157
+ * lot of the state managemnt on the C++ side of Node (and of course also it
158
+ * bakes support for async listeners into the Node C++ API through the
159
+ * AsyncWrap class, which means that it doesn't monkeypatch basically every
160
+ * async method like this does).
161
+ */
162
+ return function () {
163
+ // put the current values where the catcher can see them
164
+ errorValues = values;
165
+
166
+ /* More than one listener can end up inside these closures, so save the
167
+ * current listeners on a stack.
168
+ */
169
+ listenerStack.push(listeners);
170
+
171
+ /* Activate both the listeners that were active when the closure was
172
+ * created and the listeners that were previously active.
173
+ */
174
+ listeners = union(list, listeners);
175
+
176
+ /*
177
+ * before handlers
178
+ */
179
+ inAsyncTick = true;
180
+ for (var i = 0; i < length; ++i) {
181
+ if ((list[i].flags & HAS_BEFORE_AL) > 0) {
182
+ list[i].before(this, values[list[i].uid]);
183
+ }
184
+ }
185
+ inAsyncTick = false;
186
+
187
+ // save the return value to pass to the after callbacks
188
+ var returned = original.apply(this, arguments);
189
+
190
+ /*
191
+ * after handlers (not run if original throws)
192
+ */
193
+ inAsyncTick = true;
194
+ for (i = 0; i < length; ++i) {
195
+ if ((list[i].flags & HAS_AFTER_AL) > 0) {
196
+ list[i].after(this, values[list[i].uid]);
197
+ }
198
+ }
199
+ inAsyncTick = false;
200
+
201
+ // back to the previous listener list on the stack
202
+ listeners = listenerStack.pop();
203
+ errorValues = undefined;
204
+
205
+ return returned;
206
+ };
207
+ };
208
+
209
+ wrap(process, '_fatalException', function (_fatalException) {
210
+ return function _asyncFatalException(er) {
211
+ return asyncCatcher(er) || _fatalException(er);
212
+ };
213
+ });
214
+ }
215
+ // 0.8 and below
216
+ else {
217
+ /**
218
+ * If an error handler in asyncWrap throws, the process must die. Under 0.8
219
+ * and earlier the only way to put a bullet through the head of the process
220
+ * is to rethrow from inside the exception handler, so rethrow and set
221
+ * errorThrew to tell the uncaughtHandler what to do.
222
+ */
223
+ var errorThrew = false;
224
+
225
+ /**
226
+ * Under Node 0.8, this handler *only* handles synchronously thrown errors.
227
+ * This simplifies it, which almost but not quite makes up for the hit taken
228
+ * by putting everything in a try-catch.
229
+ */
230
+ asyncCatcher = function uncaughtCatcher(er) {
231
+ // going down hard
232
+ if (errorThrew) throw er;
233
+
234
+ var handled = false;
235
+
236
+ /*
237
+ * error handlers
238
+ */
239
+ var length = listeners.length;
240
+ for (var i = 0; i < length; ++i) {
241
+ var listener = listeners[i];
242
+ if ((listener.flags & HAS_ERROR_AL) === 0) continue;
243
+ handled = listener.error(null, er) || handled;
244
+ }
245
+
246
+ /* Rethrow if one of the before / after handlers fire, which will bring the
247
+ * process down immediately.
248
+ */
249
+ if (!handled && inAsyncTick) throw er;
250
+ };
251
+
252
+ asyncWrap = function asyncWrap(original, list, length) {
253
+ var values = [];
254
+
255
+ /*
256
+ * listeners
257
+ */
258
+ inAsyncTick = true;
259
+ for (var i = 0; i < length; ++i) {
260
+ var listener = list[i];
261
+ values[listener.uid] = listener.data;
262
+
263
+ if ((listener.flags & HAS_CREATE_AL) === 0) continue;
264
+
265
+ var value = listener.create(listener.data);
266
+ if (value !== undefined) values[listener.uid] = value;
267
+ }
268
+ inAsyncTick = false;
269
+
270
+ /* One of the main differences between this polyfill and the core
271
+ * asyncListener support is that core avoids creating closures by putting a
272
+ * lot of the state managemnt on the C++ side of Node (and of course also it
273
+ * bakes support for async listeners into the Node C++ API through the
274
+ * AsyncWrap class, which means that it doesn't monkeypatch basically every
275
+ * async method like this does).
276
+ */
277
+ return function () {
278
+ /*jshint maxdepth:4*/
279
+
280
+ // after() handlers don't run if threw
281
+ var threw = false;
282
+
283
+ // ...unless the error is handled
284
+ var handled = false;
285
+
286
+ /* More than one listener can end up inside these closures, so save the
287
+ * current listeners on a stack.
288
+ */
289
+ listenerStack.push(listeners);
290
+
291
+ /* Activate both the listeners that were active when the closure was
292
+ * created and the listeners that were previously active.
293
+ */
294
+ listeners = union(list, listeners);
295
+
296
+ /*
297
+ * before handlers
298
+ */
299
+ inAsyncTick = true;
300
+ for (var i = 0; i < length; ++i) {
301
+ if ((list[i].flags & HAS_BEFORE_AL) > 0) {
302
+ list[i].before(this, values[list[i].uid]);
303
+ }
304
+ }
305
+ inAsyncTick = false;
306
+
307
+ // save the return value to pass to the after callbacks
308
+ var returned;
309
+ try {
310
+ returned = original.apply(this, arguments);
311
+ }
312
+ catch (er) {
313
+ threw = true;
314
+ for (var i = 0; i < length; ++i) {
315
+ if ((listeners[i].flags & HAS_ERROR_AL) == 0) continue;
316
+ try {
317
+ handled = listeners[i].error(values[list[i].uid], er) || handled;
318
+ }
319
+ catch (x) {
320
+ errorThrew = true;
321
+ throw x;
322
+ }
323
+ }
324
+
325
+ if (!handled) {
326
+ // having an uncaughtException handler here alters crash semantics
327
+ process.removeListener('uncaughtException', asyncCatcher);
328
+ process._originalNextTick(function () {
329
+ process.addListener('uncaughtException', asyncCatcher);
330
+ });
331
+
332
+ throw er;
333
+ }
334
+ }
335
+ finally {
336
+ /*
337
+ * after handlers (not run if original throws)
338
+ */
339
+ if (!threw || handled) {
340
+ inAsyncTick = true;
341
+ for (i = 0; i < length; ++i) {
342
+ if ((list[i].flags & HAS_AFTER_AL) > 0) {
343
+ list[i].after(this, values[list[i].uid]);
344
+ }
345
+ }
346
+ inAsyncTick = false;
347
+ }
348
+
349
+ // back to the previous listener list on the stack
350
+ listeners = listenerStack.pop();
351
+ }
352
+
353
+
354
+ return returned;
355
+ };
356
+ };
357
+
358
+ // will be the first to fire if async-listener is the first module loaded
359
+ process.addListener('uncaughtException', asyncCatcher);
360
+ }
361
+
362
+ // for performance in the case where there are no handlers, just the listener
363
+ function simpleWrap(original, list, length) {
364
+ inAsyncTick = true;
365
+ for (var i = 0; i < length; ++i) {
366
+ var listener = list[i];
367
+ if (listener.create) listener.create(listener.data);
368
+ }
369
+ inAsyncTick = false;
370
+
371
+ // still need to make sure nested async calls are made in the context
372
+ // of the listeners active at their creation
373
+ return function () {
374
+ listenerStack.push(listeners);
375
+ listeners = union(list, listeners);
376
+
377
+ var returned = original.apply(this, arguments);
378
+
379
+ listeners = listenerStack.pop();
380
+
381
+ return returned;
382
+ };
383
+ }
384
+
385
+ /**
386
+ * Called each time an asynchronous function that's been monkeypatched in
387
+ * index.js is called. If there are no listeners, return the function
388
+ * unwrapped. If there are any asyncListeners and any of them have callbacks,
389
+ * pass them off to asyncWrap for later use, otherwise just call the listener.
390
+ */
391
+ function wrapCallback(original) {
392
+ var length = listeners.length;
393
+
394
+ // no context to capture, so avoid closure creation
395
+ if (length === 0) return original;
396
+
397
+ // capture the active listeners as of when the wrapped function was called
398
+ var list = listeners.slice();
399
+
400
+ for (var i = 0; i < length; ++i) {
401
+ if (list[i].flags > 0) return asyncWrap(original, list, length);
402
+ }
403
+
404
+ return simpleWrap(original, list, length);
405
+ }
406
+
407
+ function AsyncListener(callbacks, data) {
408
+ if (typeof callbacks.create === 'function') {
409
+ this.create = callbacks.create;
410
+ this.flags |= HAS_CREATE_AL;
411
+ }
412
+
413
+ if (typeof callbacks.before === 'function') {
414
+ this.before = callbacks.before;
415
+ this.flags |= HAS_BEFORE_AL;
416
+ }
417
+
418
+ if (typeof callbacks.after === 'function') {
419
+ this.after = callbacks.after;
420
+ this.flags |= HAS_AFTER_AL;
421
+ }
422
+
423
+ if (typeof callbacks.error === 'function') {
424
+ this.error = callbacks.error;
425
+ this.flags |= HAS_ERROR_AL;
426
+ }
427
+
428
+ this.uid = ++uid;
429
+ this.data = data === undefined ? null : data;
430
+ }
431
+ AsyncListener.prototype.create = undefined;
432
+ AsyncListener.prototype.before = undefined;
433
+ AsyncListener.prototype.after = undefined;
434
+ AsyncListener.prototype.error = undefined;
435
+ AsyncListener.prototype.data = undefined;
436
+ AsyncListener.prototype.uid = 0;
437
+ AsyncListener.prototype.flags = 0;
438
+
439
+ function createAsyncListener(callbacks, data) {
440
+ if (typeof callbacks !== 'object' || !callbacks) {
441
+ throw new TypeError('callbacks argument must be an object');
442
+ }
443
+
444
+ if (callbacks instanceof AsyncListener) {
445
+ return callbacks;
446
+ }
447
+ else {
448
+ return new AsyncListener(callbacks, data);
449
+ }
450
+ }
451
+
452
+ function addAsyncListener(callbacks, data) {
453
+ var listener;
454
+ if (!(callbacks instanceof AsyncListener)) {
455
+ listener = createAsyncListener(callbacks, data);
456
+ }
457
+ else {
458
+ listener = callbacks;
459
+ }
460
+
461
+ // Make sure the listener isn't already in the list.
462
+ var registered = false;
463
+ for (var i = 0; i < listeners.length; i++) {
464
+ if (listener === listeners[i]) {
465
+ registered = true;
466
+ break;
467
+ }
468
+ }
469
+
470
+ if (!registered) listeners.push(listener);
471
+
472
+ return listener;
473
+ }
474
+
475
+ function removeAsyncListener(listener) {
476
+ for (var i = 0; i < listeners.length; i++) {
477
+ if (listener === listeners[i]) {
478
+ listeners.splice(i, 1);
479
+ break;
480
+ }
481
+ }
482
+ }
483
+
484
+ process.createAsyncListener = createAsyncListener;
485
+ process.addAsyncListener = addAsyncListener;
486
+ process.removeAsyncListener = removeAsyncListener;
487
+
488
+ module.exports = wrapCallback;