@contrast/agent 4.16.1 → 4.17.1

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 (267) hide show
  1. package/bin/VERSION +1 -1
  2. package/bin/linux/contrast-service +0 -0
  3. package/bin/mac/contrast-service +0 -0
  4. package/bin/windows/contrast-service.exe +0 -0
  5. package/esm.mjs +21 -11
  6. package/lib/assess/policy/propagators.json +4 -0
  7. package/lib/assess/policy/signatures.json +5 -0
  8. package/lib/assess/propagators/fastify-static/allowed-path.js +85 -0
  9. package/lib/cli-rewriter/index.js +1 -1
  10. package/lib/contrast.js +1 -1
  11. package/lib/core/rewrite/index.js +2 -2
  12. package/lib/protect/rules/cmd-injection/cmdinjection-rule.js +1 -1
  13. package/lib/protect/service.js +2 -0
  14. package/lib/util/trace-util.js +5 -4
  15. package/node_modules/@colors/colors/LICENSE +26 -0
  16. package/node_modules/@colors/colors/README.md +219 -0
  17. package/node_modules/@colors/colors/examples/normal-usage.js +83 -0
  18. package/node_modules/@colors/colors/examples/safe-string.js +80 -0
  19. package/node_modules/@colors/colors/index.d.ts +136 -0
  20. package/node_modules/@colors/colors/lib/colors.js +211 -0
  21. package/node_modules/@colors/colors/lib/custom/trap.js +46 -0
  22. package/node_modules/@colors/colors/lib/custom/zalgo.js +110 -0
  23. package/node_modules/@colors/colors/lib/extendStringPrototype.js +110 -0
  24. package/node_modules/@colors/colors/lib/index.js +13 -0
  25. package/node_modules/@colors/colors/lib/maps/america.js +10 -0
  26. package/node_modules/@colors/colors/lib/maps/rainbow.js +12 -0
  27. package/node_modules/@colors/colors/lib/maps/random.js +11 -0
  28. package/node_modules/@colors/colors/lib/maps/zebra.js +5 -0
  29. package/node_modules/@colors/colors/lib/styles.js +95 -0
  30. package/node_modules/@colors/colors/lib/system/has-flag.js +35 -0
  31. package/node_modules/@colors/colors/lib/system/supports-colors.js +151 -0
  32. package/node_modules/@colors/colors/package.json +49 -0
  33. package/node_modules/@colors/colors/safe.d.ts +48 -0
  34. package/node_modules/@colors/colors/safe.js +10 -0
  35. package/node_modules/@colors/colors/themes/generic-logging.js +12 -0
  36. package/node_modules/@dabh/diagnostics/README.md +16 -16
  37. package/node_modules/@dabh/diagnostics/package.json +9 -9
  38. package/node_modules/async/CHANGELOG.md +13 -0
  39. package/node_modules/async/all.js +74 -9
  40. package/node_modules/async/allLimit.js +3 -3
  41. package/node_modules/async/allSeries.js +3 -3
  42. package/node_modules/async/any.js +75 -9
  43. package/node_modules/async/anyLimit.js +3 -3
  44. package/node_modules/async/anySeries.js +3 -3
  45. package/node_modules/async/applyEach.js +2 -2
  46. package/node_modules/async/applyEachSeries.js +2 -2
  47. package/node_modules/async/asyncify.js +3 -3
  48. package/node_modules/async/auto.js +81 -15
  49. package/node_modules/async/autoInject.js +30 -4
  50. package/node_modules/async/cargo.js +1 -1
  51. package/node_modules/async/cargoQueue.js +1 -1
  52. package/node_modules/async/compose.js +1 -1
  53. package/node_modules/async/concat.js +72 -4
  54. package/node_modules/async/concatLimit.js +3 -3
  55. package/node_modules/async/concatSeries.js +2 -2
  56. package/node_modules/async/detect.js +43 -8
  57. package/node_modules/async/detectLimit.js +3 -3
  58. package/node_modules/async/detectSeries.js +3 -3
  59. package/node_modules/async/dir.js +1 -1
  60. package/node_modules/async/dist/async.js +1379 -168
  61. package/node_modules/async/dist/async.min.js +1 -1
  62. package/node_modules/async/dist/async.mjs +1372 -161
  63. package/node_modules/async/doDuring.js +3 -3
  64. package/node_modules/async/doUntil.js +2 -2
  65. package/node_modules/async/doWhilst.js +3 -3
  66. package/node_modules/async/during.js +3 -3
  67. package/node_modules/async/each.js +69 -28
  68. package/node_modules/async/eachLimit.js +4 -4
  69. package/node_modules/async/eachOf.js +85 -16
  70. package/node_modules/async/eachOfLimit.js +3 -3
  71. package/node_modules/async/eachOfSeries.js +2 -2
  72. package/node_modules/async/eachSeries.js +2 -2
  73. package/node_modules/async/ensureAsync.js +2 -2
  74. package/node_modules/async/every.js +74 -9
  75. package/node_modules/async/everyLimit.js +3 -3
  76. package/node_modules/async/everySeries.js +3 -3
  77. package/node_modules/async/filter.js +49 -9
  78. package/node_modules/async/filterLimit.js +3 -3
  79. package/node_modules/async/filterSeries.js +3 -3
  80. package/node_modules/async/find.js +43 -8
  81. package/node_modules/async/findLimit.js +3 -3
  82. package/node_modules/async/findSeries.js +3 -3
  83. package/node_modules/async/flatMap.js +72 -4
  84. package/node_modules/async/flatMapLimit.js +3 -3
  85. package/node_modules/async/flatMapSeries.js +2 -2
  86. package/node_modules/async/foldl.js +87 -11
  87. package/node_modules/async/foldr.js +2 -2
  88. package/node_modules/async/forEach.js +69 -28
  89. package/node_modules/async/forEachLimit.js +4 -4
  90. package/node_modules/async/forEachOf.js +85 -16
  91. package/node_modules/async/forEachOfLimit.js +3 -3
  92. package/node_modules/async/forEachOfSeries.js +2 -2
  93. package/node_modules/async/forEachSeries.js +2 -2
  94. package/node_modules/async/forever.js +4 -4
  95. package/node_modules/async/groupBy.js +62 -8
  96. package/node_modules/async/groupByLimit.js +3 -3
  97. package/node_modules/async/groupBySeries.js +2 -2
  98. package/node_modules/async/inject.js +87 -11
  99. package/node_modules/async/internal/applyEach.js +2 -2
  100. package/node_modules/async/internal/asyncEachOfLimit.js +1 -1
  101. package/node_modules/async/internal/consoleFunc.js +5 -1
  102. package/node_modules/async/internal/createTester.js +2 -2
  103. package/node_modules/async/internal/eachOfLimit.js +6 -6
  104. package/node_modules/async/internal/filter.js +2 -2
  105. package/node_modules/async/internal/iterator.js +5 -2
  106. package/node_modules/async/internal/map.js +1 -1
  107. package/node_modules/async/internal/parallel.js +3 -3
  108. package/node_modules/async/internal/queue.js +4 -4
  109. package/node_modules/async/internal/reject.js +2 -2
  110. package/node_modules/async/internal/setImmediate.js +6 -2
  111. package/node_modules/async/internal/wrapAsync.js +1 -1
  112. package/node_modules/async/log.js +1 -1
  113. package/node_modules/async/map.js +86 -6
  114. package/node_modules/async/mapLimit.js +3 -3
  115. package/node_modules/async/mapSeries.js +3 -3
  116. package/node_modules/async/mapValues.js +102 -12
  117. package/node_modules/async/mapValuesLimit.js +4 -4
  118. package/node_modules/async/mapValuesSeries.js +1 -1
  119. package/node_modules/async/memoize.js +3 -3
  120. package/node_modules/async/nextTick.js +3 -3
  121. package/node_modules/async/package.json +4 -6
  122. package/node_modules/async/parallel.js +96 -7
  123. package/node_modules/async/parallelLimit.js +2 -2
  124. package/node_modules/async/priorityQueue.js +11 -4
  125. package/node_modules/async/queue.js +4 -4
  126. package/node_modules/async/race.js +3 -3
  127. package/node_modules/async/reduce.js +87 -11
  128. package/node_modules/async/reduceRight.js +2 -2
  129. package/node_modules/async/reflect.js +2 -2
  130. package/node_modules/async/reflectAll.js +1 -1
  131. package/node_modules/async/reject.js +44 -10
  132. package/node_modules/async/rejectLimit.js +3 -3
  133. package/node_modules/async/rejectSeries.js +3 -3
  134. package/node_modules/async/retry.js +2 -2
  135. package/node_modules/async/retryable.js +4 -4
  136. package/node_modules/async/select.js +49 -9
  137. package/node_modules/async/selectLimit.js +3 -3
  138. package/node_modules/async/selectSeries.js +3 -3
  139. package/node_modules/async/seq.js +4 -4
  140. package/node_modules/async/series.js +112 -12
  141. package/node_modules/async/setImmediate.js +1 -1
  142. package/node_modules/async/some.js +75 -9
  143. package/node_modules/async/someLimit.js +3 -3
  144. package/node_modules/async/someSeries.js +3 -3
  145. package/node_modules/async/sortBy.js +121 -19
  146. package/node_modules/async/timeout.js +2 -2
  147. package/node_modules/async/times.js +1 -1
  148. package/node_modules/async/timesLimit.js +3 -3
  149. package/node_modules/async/timesSeries.js +1 -1
  150. package/node_modules/async/transform.js +111 -19
  151. package/node_modules/async/tryEach.js +3 -3
  152. package/node_modules/async/until.js +3 -3
  153. package/node_modules/async/waterfall.js +4 -4
  154. package/node_modules/async/whilst.js +3 -3
  155. package/node_modules/async/wrapSync.js +3 -3
  156. package/node_modules/color/README.md +9 -0
  157. package/node_modules/color/index.js +4 -1
  158. package/node_modules/color/package.json +8 -8
  159. package/node_modules/color-string/README.md +6 -2
  160. package/node_modules/color-string/index.js +21 -13
  161. package/node_modules/color-string/package.json +4 -4
  162. package/node_modules/colorspace/package.json +5 -5
  163. package/node_modules/fecha/dist/fecha.min.js +1 -1
  164. package/node_modules/fecha/dist/fecha.min.js.map +1 -0
  165. package/node_modules/fecha/lib/fecha.d.ts +2 -2
  166. package/node_modules/fecha/lib/fecha.js +35 -18
  167. package/node_modules/fecha/lib/fecha.js.map +1 -0
  168. package/node_modules/fecha/lib/fecha.umd.js +35 -18
  169. package/node_modules/fecha/lib/fecha.umd.js.map +1 -0
  170. package/node_modules/fecha/package.json +9 -10
  171. package/node_modules/fecha/src/fecha.ts +524 -0
  172. package/node_modules/logform/.eslintrc +1 -1
  173. package/node_modules/logform/CHANGELOG.md +24 -0
  174. package/node_modules/logform/README.md +15 -17
  175. package/node_modules/logform/browser.js +3 -1
  176. package/node_modules/logform/cli.js +1 -1
  177. package/node_modules/logform/colorize.js +2 -2
  178. package/node_modules/logform/dist/browser.js +8 -2
  179. package/node_modules/logform/dist/cli.js +2 -2
  180. package/node_modules/logform/dist/colorize.js +5 -5
  181. package/node_modules/logform/dist/errors.js +1 -1
  182. package/node_modules/logform/dist/format.js +9 -5
  183. package/node_modules/logform/dist/index.js +53 -20
  184. package/node_modules/logform/dist/json.js +7 -6
  185. package/node_modules/logform/dist/logstash.js +1 -1
  186. package/node_modules/logform/dist/pad-levels.js +4 -4
  187. package/node_modules/logform/dist/printf.js +1 -1
  188. package/node_modules/logform/dist/simple.js +1 -1
  189. package/node_modules/logform/dist/splat.js +2 -2
  190. package/node_modules/logform/dist/uncolorize.js +1 -1
  191. package/node_modules/logform/errors.js +1 -1
  192. package/node_modules/logform/index.d.ts +36 -0
  193. package/node_modules/logform/index.js +19 -20
  194. package/node_modules/logform/json.js +7 -7
  195. package/node_modules/logform/logstash.js +1 -1
  196. package/node_modules/logform/package.json +10 -9
  197. package/node_modules/logform/simple.js +1 -1
  198. package/node_modules/logform/uncolorize.js +1 -1
  199. package/node_modules/safe-stable-stringify/CHANGELOG.md +75 -0
  200. package/node_modules/safe-stable-stringify/LICENSE +21 -0
  201. package/node_modules/safe-stable-stringify/esm/package.json +4 -0
  202. package/node_modules/safe-stable-stringify/esm/wrapper.js +6 -0
  203. package/node_modules/safe-stable-stringify/index.d.ts +18 -0
  204. package/node_modules/safe-stable-stringify/index.js +618 -0
  205. package/node_modules/safe-stable-stringify/package.json +69 -0
  206. package/node_modules/safe-stable-stringify/readme.md +170 -0
  207. package/node_modules/safe-stable-stringify/tsconfig.json +22 -0
  208. package/node_modules/winston/LICENSE +18 -18
  209. package/node_modules/winston/README.md +1231 -1230
  210. package/node_modules/winston/dist/winston/common.js +10 -10
  211. package/node_modules/winston/dist/winston/config/index.js +17 -17
  212. package/node_modules/winston/dist/winston/container.js +46 -46
  213. package/node_modules/winston/dist/winston/create-logger.js +28 -24
  214. package/node_modules/winston/dist/winston/exception-handler.js +49 -49
  215. package/node_modules/winston/dist/winston/exception-stream.js +27 -27
  216. package/node_modules/winston/dist/winston/logger.js +152 -138
  217. package/node_modules/winston/dist/winston/profiler.js +22 -22
  218. package/node_modules/winston/dist/winston/rejection-handler.js +54 -54
  219. package/node_modules/winston/dist/winston/tail-file.js +14 -14
  220. package/node_modules/winston/dist/winston/transports/console.js +31 -31
  221. package/node_modules/winston/dist/winston/transports/file.js +101 -101
  222. package/node_modules/winston/dist/winston/transports/http.js +119 -41
  223. package/node_modules/winston/dist/winston/transports/index.js +17 -17
  224. package/node_modules/winston/dist/winston/transports/stream.js +24 -24
  225. package/node_modules/winston/dist/winston.js +91 -97
  226. package/node_modules/winston/index.d.ts +213 -193
  227. package/node_modules/winston/lib/winston/common.js +61 -61
  228. package/node_modules/winston/lib/winston/config/index.d.ts +99 -98
  229. package/node_modules/winston/lib/winston/config/index.js +35 -35
  230. package/node_modules/winston/lib/winston/container.js +114 -114
  231. package/node_modules/winston/lib/winston/create-logger.js +104 -104
  232. package/node_modules/winston/lib/winston/exception-handler.js +245 -245
  233. package/node_modules/winston/lib/winston/exception-stream.js +54 -54
  234. package/node_modules/winston/lib/winston/logger.js +676 -667
  235. package/node_modules/winston/lib/winston/profiler.js +51 -51
  236. package/node_modules/winston/lib/winston/rejection-handler.js +251 -251
  237. package/node_modules/winston/lib/winston/tail-file.js +124 -124
  238. package/node_modules/winston/lib/winston/transports/console.js +117 -117
  239. package/node_modules/winston/lib/winston/transports/file.js +695 -695
  240. package/node_modules/winston/lib/winston/transports/http.js +267 -202
  241. package/node_modules/winston/lib/winston/transports/index.d.ts +103 -100
  242. package/node_modules/winston/lib/winston/transports/index.js +56 -56
  243. package/node_modules/winston/lib/winston/transports/stream.js +63 -63
  244. package/node_modules/winston/lib/winston.js +176 -182
  245. package/node_modules/winston/node_modules/winston-transport/.babelrc +3 -0
  246. package/node_modules/winston/node_modules/winston-transport/.eslintrc +7 -0
  247. package/node_modules/winston/node_modules/winston-transport/.gitattributes +1 -0
  248. package/node_modules/winston/node_modules/winston-transport/.nyc_output/c3d7ddb9-cc26-466b-a4f6-993ad69e86f6.json +1 -0
  249. package/node_modules/winston/node_modules/winston-transport/.nyc_output/processinfo/c3d7ddb9-cc26-466b-a4f6-993ad69e86f6.json +1 -0
  250. package/node_modules/winston/node_modules/winston-transport/.nyc_output/processinfo/index.json +1 -0
  251. package/node_modules/winston/node_modules/winston-transport/CHANGELOG.md +126 -0
  252. package/node_modules/winston/node_modules/winston-transport/LICENSE +22 -0
  253. package/node_modules/winston/node_modules/winston-transport/README.md +50 -0
  254. package/node_modules/winston/node_modules/winston-transport/dist/index.js +215 -0
  255. package/node_modules/winston/node_modules/winston-transport/dist/legacy.js +116 -0
  256. package/node_modules/winston/node_modules/winston-transport/index.d.ts +39 -0
  257. package/node_modules/winston/node_modules/winston-transport/index.js +215 -0
  258. package/node_modules/winston/node_modules/winston-transport/legacy.js +119 -0
  259. package/node_modules/winston/node_modules/winston-transport/package.json +56 -0
  260. package/node_modules/winston/package.json +76 -75
  261. package/package.json +8 -6
  262. package/node_modules/@dabh/diagnostics/example.png +0 -0
  263. package/node_modules/color-string/CHANGELOG.md +0 -18
  264. package/node_modules/colorspace/test.js +0 -14
  265. package/node_modules/fecha/CHANGELOG.md +0 -67
  266. package/node_modules/logform/.travis.yml +0 -17
  267. package/node_modules/winston/CHANGELOG.md +0 -560
@@ -1,202 +1,267 @@
1
- /**
2
- * http.js: Transport for outputting to a json-rpcserver.
3
- *
4
- * (C) 2010 Charlie Robbins
5
- * MIT LICENCE
6
- */
7
-
8
- 'use strict';
9
-
10
- const http = require('http');
11
- const https = require('https');
12
- const { Stream } = require('readable-stream');
13
- const TransportStream = require('winston-transport');
14
-
15
- /**
16
- * Transport for outputting to a json-rpc server.
17
- * @type {Stream}
18
- * @extends {TransportStream}
19
- */
20
- module.exports = class Http extends TransportStream {
21
- /**
22
- * Constructor function for the Http transport object responsible for
23
- * persisting log messages and metadata to a terminal or TTY.
24
- * @param {!Object} [options={}] - Options for this instance.
25
- */
26
- constructor(options = {}) {
27
- super(options);
28
-
29
- this.options = options;
30
- this.name = options.name || 'http';
31
- this.ssl = !!options.ssl;
32
- this.host = options.host || 'localhost';
33
- this.port = options.port;
34
- this.auth = options.auth;
35
- this.path = options.path || '';
36
- this.agent = options.agent;
37
- this.headers = options.headers || {};
38
- this.headers['content-type'] = 'application/json';
39
-
40
- if (!this.port) {
41
- this.port = this.ssl ? 443 : 80;
42
- }
43
- }
44
-
45
- /**
46
- * Core logging method exposed to Winston.
47
- * @param {Object} info - TODO: add param description.
48
- * @param {function} callback - TODO: add param description.
49
- * @returns {undefined}
50
- */
51
- log(info, callback) {
52
- this._request(info, (err, res) => {
53
- if (res && res.statusCode !== 200) {
54
- err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`);
55
- }
56
-
57
- if (err) {
58
- this.emit('warn', err);
59
- } else {
60
- this.emit('logged', info);
61
- }
62
- });
63
-
64
- // Remark: (jcrugzz) Fire and forget here so requests dont cause buffering
65
- // and block more requests from happening?
66
- if (callback) {
67
- setImmediate(callback);
68
- }
69
- }
70
-
71
- /**
72
- * Query the transport. Options object is optional.
73
- * @param {Object} options - Loggly-like query options for this instance.
74
- * @param {function} callback - Continuation to respond to when complete.
75
- * @returns {undefined}
76
- */
77
- query(options, callback) {
78
- if (typeof options === 'function') {
79
- callback = options;
80
- options = {};
81
- }
82
-
83
- options = {
84
- method: 'query',
85
- params: this.normalizeQuery(options)
86
- };
87
-
88
- if (options.params.path) {
89
- options.path = options.params.path;
90
- delete options.params.path;
91
- }
92
-
93
- if (options.params.auth) {
94
- options.auth = options.params.auth;
95
- delete options.params.auth;
96
- }
97
-
98
- this._request(options, (err, res, body) => {
99
- if (res && res.statusCode !== 200) {
100
- err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`);
101
- }
102
-
103
- if (err) {
104
- return callback(err);
105
- }
106
-
107
- if (typeof body === 'string') {
108
- try {
109
- body = JSON.parse(body);
110
- } catch (e) {
111
- return callback(e);
112
- }
113
- }
114
-
115
- callback(null, body);
116
- });
117
- }
118
-
119
- /**
120
- * Returns a log stream for this transport. Options object is optional.
121
- * @param {Object} options - Stream options for this instance.
122
- * @returns {Stream} - TODO: add return description
123
- */
124
- stream(options = {}) {
125
- const stream = new Stream();
126
- options = {
127
- method: 'stream',
128
- params: options
129
- };
130
-
131
- if (options.params.path) {
132
- options.path = options.params.path;
133
- delete options.params.path;
134
- }
135
-
136
- if (options.params.auth) {
137
- options.auth = options.params.auth;
138
- delete options.params.auth;
139
- }
140
-
141
- let buff = '';
142
- const req = this._request(options);
143
-
144
- stream.destroy = () => req.destroy();
145
- req.on('data', data => {
146
- data = (buff + data).split(/\n+/);
147
- const l = data.length - 1;
148
-
149
- let i = 0;
150
- for (; i < l; i++) {
151
- try {
152
- stream.emit('log', JSON.parse(data[i]));
153
- } catch (e) {
154
- stream.emit('error', e);
155
- }
156
- }
157
-
158
- buff = data[l];
159
- });
160
- req.on('error', err => stream.emit('error', err));
161
-
162
- return stream;
163
- }
164
-
165
- /**
166
- * Make a request to a winstond server or any http server which can
167
- * handle json-rpc.
168
- * @param {function} options - Options to sent the request.
169
- * @param {function} callback - Continuation to respond to when complete.
170
- */
171
- _request(options, callback) {
172
- options = options || {};
173
-
174
- const auth = options.auth || this.auth;
175
- const path = options.path || this.path || '';
176
-
177
- delete options.auth;
178
- delete options.path;
179
-
180
- // Prepare options for outgoing HTTP request
181
- const headers = Object.assign({}, this.headers);
182
- if (auth && auth.bearer) {
183
- headers.Authorization = `Bearer ${auth.bearer}`;
184
- }
185
- const req = (this.ssl ? https : http).request({
186
- ...this.options,
187
- method: 'POST',
188
- host: this.host,
189
- port: this.port,
190
- path: `/${path.replace(/^\//, '')}`,
191
- headers: headers,
192
- auth: (auth && auth.username && auth.password) ? (`${auth.username}:${auth.password}`) : '',
193
- agent: this.agent
194
- });
195
-
196
- req.on('error', callback);
197
- req.on('response', res => (
198
- res.on('end', () => callback(null, res)).resume()
199
- ));
200
- req.end(Buffer.from(JSON.stringify(options), 'utf8'));
201
- }
202
- };
1
+ /**
2
+ * http.js: Transport for outputting to a json-rpcserver.
3
+ *
4
+ * (C) 2010 Charlie Robbins
5
+ * MIT LICENCE
6
+ */
7
+
8
+ 'use strict';
9
+
10
+ const http = require('http');
11
+ const https = require('https');
12
+ const { Stream } = require('readable-stream');
13
+ const TransportStream = require('winston-transport');
14
+ const jsonStringify = require('safe-stable-stringify');
15
+
16
+ /**
17
+ * Transport for outputting to a json-rpc server.
18
+ * @type {Stream}
19
+ * @extends {TransportStream}
20
+ */
21
+ module.exports = class Http extends TransportStream {
22
+ /**
23
+ * Constructor function for the Http transport object responsible for
24
+ * persisting log messages and metadata to a terminal or TTY.
25
+ * @param {!Object} [options={}] - Options for this instance.
26
+ */
27
+ // eslint-disable-next-line max-statements
28
+ constructor(options = {}) {
29
+ super(options);
30
+
31
+ this.options = options;
32
+ this.name = options.name || 'http';
33
+ this.ssl = !!options.ssl;
34
+ this.host = options.host || 'localhost';
35
+ this.port = options.port;
36
+ this.auth = options.auth;
37
+ this.path = options.path || '';
38
+ this.agent = options.agent;
39
+ this.headers = options.headers || {};
40
+ this.headers['content-type'] = 'application/json';
41
+ this.batch = options.batch || false;
42
+ this.batchInterval = options.batchInterval || 5000;
43
+ this.batchCount = options.batchCount || 10;
44
+ this.batchOptions = [];
45
+ this.batchTimeoutID = -1;
46
+ this.batchCallback = {};
47
+
48
+ if (!this.port) {
49
+ this.port = this.ssl ? 443 : 80;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Core logging method exposed to Winston.
55
+ * @param {Object} info - TODO: add param description.
56
+ * @param {function} callback - TODO: add param description.
57
+ * @returns {undefined}
58
+ */
59
+ log(info, callback) {
60
+ this._request(info, (err, res) => {
61
+ if (res && res.statusCode !== 200) {
62
+ err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`);
63
+ }
64
+
65
+ if (err) {
66
+ this.emit('warn', err);
67
+ } else {
68
+ this.emit('logged', info);
69
+ }
70
+ });
71
+
72
+ // Remark: (jcrugzz) Fire and forget here so requests dont cause buffering
73
+ // and block more requests from happening?
74
+ if (callback) {
75
+ setImmediate(callback);
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Query the transport. Options object is optional.
81
+ * @param {Object} options - Loggly-like query options for this instance.
82
+ * @param {function} callback - Continuation to respond to when complete.
83
+ * @returns {undefined}
84
+ */
85
+ query(options, callback) {
86
+ if (typeof options === 'function') {
87
+ callback = options;
88
+ options = {};
89
+ }
90
+
91
+ options = {
92
+ method: 'query',
93
+ params: this.normalizeQuery(options)
94
+ };
95
+
96
+ if (options.params.path) {
97
+ options.path = options.params.path;
98
+ delete options.params.path;
99
+ }
100
+
101
+ if (options.params.auth) {
102
+ options.auth = options.params.auth;
103
+ delete options.params.auth;
104
+ }
105
+
106
+ this._request(options, (err, res, body) => {
107
+ if (res && res.statusCode !== 200) {
108
+ err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`);
109
+ }
110
+
111
+ if (err) {
112
+ return callback(err);
113
+ }
114
+
115
+ if (typeof body === 'string') {
116
+ try {
117
+ body = JSON.parse(body);
118
+ } catch (e) {
119
+ return callback(e);
120
+ }
121
+ }
122
+
123
+ callback(null, body);
124
+ });
125
+ }
126
+
127
+ /**
128
+ * Returns a log stream for this transport. Options object is optional.
129
+ * @param {Object} options - Stream options for this instance.
130
+ * @returns {Stream} - TODO: add return description
131
+ */
132
+ stream(options = {}) {
133
+ const stream = new Stream();
134
+ options = {
135
+ method: 'stream',
136
+ params: options
137
+ };
138
+
139
+ if (options.params.path) {
140
+ options.path = options.params.path;
141
+ delete options.params.path;
142
+ }
143
+
144
+ if (options.params.auth) {
145
+ options.auth = options.params.auth;
146
+ delete options.params.auth;
147
+ }
148
+
149
+ let buff = '';
150
+ const req = this._request(options);
151
+
152
+ stream.destroy = () => req.destroy();
153
+ req.on('data', data => {
154
+ data = (buff + data).split(/\n+/);
155
+ const l = data.length - 1;
156
+
157
+ let i = 0;
158
+ for (; i < l; i++) {
159
+ try {
160
+ stream.emit('log', JSON.parse(data[i]));
161
+ } catch (e) {
162
+ stream.emit('error', e);
163
+ }
164
+ }
165
+
166
+ buff = data[l];
167
+ });
168
+ req.on('error', err => stream.emit('error', err));
169
+
170
+ return stream;
171
+ }
172
+
173
+ /**
174
+ * Make a request to a winstond server or any http server which can
175
+ * handle json-rpc.
176
+ * @param {function} options - Options to sent the request.
177
+ * @param {function} callback - Continuation to respond to when complete.
178
+ */
179
+ _request(options, callback) {
180
+ options = options || {};
181
+
182
+ const auth = options.auth || this.auth;
183
+ const path = options.path || this.path || '';
184
+
185
+ delete options.auth;
186
+ delete options.path;
187
+
188
+ if (this.batch) {
189
+ this._doBatch(options, callback, auth, path);
190
+ } else {
191
+ this._doRequest(options, callback, auth, path);
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Send or memorize the options according to batch configuration
197
+ * @param {function} options - Options to sent the request.
198
+ * @param {function} callback - Continuation to respond to when complete.
199
+ * @param {Object?} auth - authentication options
200
+ * @param {string} path - request path
201
+ */
202
+ _doBatch(options, callback, auth, path) {
203
+ this.batchOptions.push(options);
204
+ if (this.batchOptions.length === 1) {
205
+ // First message stored, it's time to start the timeout!
206
+ const me = this;
207
+ this.batchCallback = callback;
208
+ this.batchTimeoutID = setTimeout(function () {
209
+ // timeout is reached, send all messages to endpoint
210
+ me.batchTimeoutID = -1;
211
+ me._doBatchRequest(me.batchCallback, auth, path);
212
+ }, this.batchInterval);
213
+ }
214
+ if (this.batchOptions.length === this.batchCount) {
215
+ // max batch count is reached, send all messages to endpoint
216
+ this._doBatchRequest(this.batchCallback, auth, path);
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Initiate a request with the memorized batch options, stop the batch timeout
222
+ * @param {function} callback - Continuation to respond to when complete.
223
+ * @param {Object?} auth - authentication options
224
+ * @param {string} path - request path
225
+ */
226
+ _doBatchRequest(callback, auth, path) {
227
+ if (this.batchTimeoutID > 0) {
228
+ clearTimeout(this.batchTimeoutID);
229
+ this.batchTimeoutID = -1;
230
+ }
231
+ const batchOptionsCopy = this.batchOptions.slice();
232
+ this.batchOptions = [];
233
+ this._doRequest(batchOptionsCopy, callback, auth, path);
234
+ }
235
+
236
+ /**
237
+ * Make a request to a winstond server or any http server which can
238
+ * handle json-rpc.
239
+ * @param {function} options - Options to sent the request.
240
+ * @param {function} callback - Continuation to respond to when complete.
241
+ * @param {Object?} auth - authentication options
242
+ * @param {string} path - request path
243
+ */
244
+ _doRequest(options, callback, auth, path) {
245
+ // Prepare options for outgoing HTTP request
246
+ const headers = Object.assign({}, this.headers);
247
+ if (auth && auth.bearer) {
248
+ headers.Authorization = `Bearer ${auth.bearer}`;
249
+ }
250
+ const req = (this.ssl ? https : http).request({
251
+ ...this.options,
252
+ method: 'POST',
253
+ host: this.host,
254
+ port: this.port,
255
+ path: `/${path.replace(/^\//, '')}`,
256
+ headers: headers,
257
+ auth: (auth && auth.username && auth.password) ? (`${auth.username}:${auth.password}`) : '',
258
+ agent: this.agent
259
+ });
260
+
261
+ req.on('error', callback);
262
+ req.on('response', res => (
263
+ res.on('end', () => callback(null, res)).resume()
264
+ ));
265
+ req.end(Buffer.from(jsonStringify(options), 'utf8'));
266
+ }
267
+ };