Repository navigation
Expand file tree
/
Copy pathlib.js
More file actions
314 lines (284 loc) · 14.3 KB
/
Copy pathlib.js
File metadata and controls
314 lines (284 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
const qs = require('querystring');
const crypto = require('crypto');
const fs = require('fs');
const fetch = require('node-fetch');
const packageJson = require('./package.json');
const { stringify } = require('javascript-stringify');
/*
* ChartJSImage URL builder
* @typedef ChartJSImage
*/
function ChartJSImage({secret, protocol, host, port, timeout, userAgent} = {}, previous = {}) {
if (!(this instanceof ChartJSImage)) {
return new ChartJSImage({secret, protocol, host, port, timeout, userAgent}, previous);
}
this._protocol = protocol || 'https';
this._host = host || 'image-charts.com';
this._port = port || 443;
this._pathname = '/chart.js/2.8.0';
this._timeout = typeof timeout !== 'undefined' ? timeout : 5000;
this._query = {};
this._secret = secret;
this._userAgent = userAgent;
Object.assign(this, previous);
}
ChartJSImage.prototype._clone = function (param, value) {
return new this.constructor({}, {
...this,
_query:{
...this._query,
[param]: value
}
});
};
/**
* Javascript/JSON definition of the chart. Use a Chart.js configuration object.
*
* A configuration over one of these limits answers 400 with the error code IS_VALIDATION_ERROR:
* - data.labels, data.xLabels, data.yLabels, data.datasets[i].data, options.scales.xAxes and options.scales.yAxes are arrays, or null.
* - No object of the configuration holds a length key.
* - At most 20 axes, 1,000 datasets, 10,000 labels per label list and 10,000 points in total.
* - borderDash and zeroLineBorderDash: at most 16 entries, at least 1 pixel per dash or space on average, and a line width at most 2 times the pattern. Dashed datasets run through at most 1,000 points in total.
* - minRotation and maxRotation: from -360 to 360 degrees.
* - datalabels with display 'auto': at most 3,000 labels.
* - All the radial scales together: at most 1,000 spokes, 100,000 ring corners and 1,000 ticks. A scale with display false counts none of them, and gridLines.display false counts no corner.
* - Ticks: at most 1,000 per linear, logarithmic, time or radial axis, and 11,000 over all axes.
* - Border widths, line widths and radii: at most 50 pixels. padding: at most 100 pixels.
* - Each text: at most 10,000 characters.
* - options.ic.insideLabel: a text of at most 3,000 characters. minFontSize and maxFontSize are brought within 1 to 999 pixels.
*
* options.animation is ignored for a PNG chart.
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().c("{type:'bar',data:{labels:['Q1','Q2','Q3','Q4'],datasets:[{label:'Users',data:[50,60,70,180]},{label:'Revenue',data:[100,200,300,400]}]}}");
*
* @param {string} value - Javascript/JSON definition of the chart. Use a Chart.js configuration object.
A configuration over one of these limits answers 400 with the error code IS_VALIDATION_ERROR:
- data.labels, data.xLabels, data.yLabels, data.datasets[i].data, options.scales.xAxes and options.scales.yAxes are arrays, or null.
- No object of the configuration holds a length key.
- At most 20 axes, 1,000 datasets, 10,000 labels per label list and 10,000 points in total.
- borderDash and zeroLineBorderDash: at most 16 entries, at least 1 pixel per dash or space on average, and a line width at most 2 times the pattern. Dashed datasets run through at most 1,000 points in total.
- minRotation and maxRotation: from -360 to 360 degrees.
- datalabels with display 'auto': at most 3,000 labels.
- All the radial scales together: at most 1,000 spokes, 100,000 ring corners and 1,000 ticks. A scale with display false counts none of them, and gridLines.display false counts no corner.
- Ticks: at most 1,000 per linear, logarithmic, time or radial axis, and 11,000 over all axes.
- Border widths, line widths and radii: at most 50 pixels. padding: at most 100 pixels.
- Each text: at most 10,000 characters.
- options.ic.insideLabel: a text of at most 3,000 characters. minFontSize and maxFontSize are brought within 1 to 999 pixels.
options.animation is ignored for a PNG chart.
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.c = function(value) {
return this._clone('c', stringify(value));
};
/**
* Javascript/JSON definition of the chart. Use a Chart.js configuration object.
*
* A configuration over one of these limits answers 400 with the error code IS_VALIDATION_ERROR:
* - data.labels, data.xLabels, data.yLabels, data.datasets[i].data, options.scales.xAxes and options.scales.yAxes are arrays, or null.
* - No object of the configuration holds a length key.
* - At most 20 axes, 1,000 datasets, 10,000 labels per label list and 10,000 points in total.
* - borderDash and zeroLineBorderDash: at most 16 entries, at least 1 pixel per dash or space on average, and a line width at most 2 times the pattern. Dashed datasets run through at most 1,000 points in total.
* - minRotation and maxRotation: from -360 to 360 degrees.
* - datalabels with display 'auto': at most 3,000 labels.
* - All the radial scales together: at most 1,000 spokes, 100,000 ring corners and 1,000 ticks. A scale with display false counts none of them, and gridLines.display false counts no corner.
* - Ticks: at most 1,000 per linear, logarithmic, time or radial axis, and 11,000 over all axes.
* - Border widths, line widths and radii: at most 50 pixels. padding: at most 100 pixels.
* - Each text: at most 10,000 characters.
* - options.ic.insideLabel: a text of at most 3,000 characters. minFontSize and maxFontSize are brought within 1 to 999 pixels.
*
* options.animation is ignored for a PNG chart.
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().chart("{type:'bar',data:{labels:['Q1','Q2','Q3','Q4'],datasets:[{label:'Users',data:[50,60,70,180]},{label:'Revenue',data:[100,200,300,400]}]}}");
*
* @param {string} value - Javascript/JSON definition of the chart. Use a Chart.js configuration object.
A configuration over one of these limits answers 400 with the error code IS_VALIDATION_ERROR:
- data.labels, data.xLabels, data.yLabels, data.datasets[i].data, options.scales.xAxes and options.scales.yAxes are arrays, or null.
- No object of the configuration holds a length key.
- At most 20 axes, 1,000 datasets, 10,000 labels per label list and 10,000 points in total.
- borderDash and zeroLineBorderDash: at most 16 entries, at least 1 pixel per dash or space on average, and a line width at most 2 times the pattern. Dashed datasets run through at most 1,000 points in total.
- minRotation and maxRotation: from -360 to 360 degrees.
- datalabels with display 'auto': at most 3,000 labels.
- All the radial scales together: at most 1,000 spokes, 100,000 ring corners and 1,000 ticks. A scale with display false counts none of them, and gridLines.display false counts no corner.
- Ticks: at most 1,000 per linear, logarithmic, time or radial axis, and 11,000 over all axes.
- Border widths, line widths and radii: at most 50 pixels. padding: at most 100 pixels.
- Each text: at most 10,000 characters.
- options.ic.insideLabel: a text of at most 3,000 characters. minFontSize and maxFontSize are brought within 1 to 999 pixels.
options.animation is ignored for a PNG chart.
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.chart = function(value) {
return this._clone('chart', stringify(value));
};
/**
* Width of the chart, from 1 to 999 pixels
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().width("400");
* @default "500"
* @param {integer} value - Width of the chart, from 1 to 999 pixels
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.width = function(value) {
return this._clone('width', value);
};
/**
* Height of the chart, from 1 to 999 pixels
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().height("300");
* @default "300"
* @param {integer} value - Height of the chart, from 1 to 999 pixels
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.height = function(value) {
return this._clone('height', value);
};
/**
* Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as "bkg"
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().backgroundColor("black");
* const chart = ChartJSImage().backgroundColor("rgb(255,255,120)");
* const chart = ChartJSImage().backgroundColor("%23ff00ff");
*
* @param {string} value - Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as "bkg"
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.backgroundColor = function(value) {
return this._clone('backgroundColor', value);
};
/**
* Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as "bkg"
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().bkg("black");
* const chart = ChartJSImage().bkg("rgb(255,255,120)");
* const chart = ChartJSImage().bkg("%23ff00ff");
*
* @param {string} value - Background of the chart canvas. Accepts rgb (rgb(255,255,120)), colors (red), and url-encoded hex values (%23ff00ff). Abbreviated as "bkg"
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.bkg = function(value) {
return this._clone('bkg', value);
};
/**
* Encoding of your "chart" parameter. Accepted values are url and base64.
* [Reference documentation]{@link }
* @example
* const chart = ChartJSImage().encoding("url");
* const chart = ChartJSImage().encoding("base64");
* @default "url"
* @param {string} value - Encoding of your "chart" parameter. Accepted values are url and base64.
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.encoding = function(value) {
return this._clone('encoding', value);
};
/**
* image-charts enterprise `account_id`
* [Reference documentation]{@link https://documentation.image-charts.com/enterprise/}
* @example
* const chart = ChartJSImage().icac("accountId");
*
* @param {string} value - image-charts enterprise `account_id`
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.icac = function(value) {
return this._clone('icac', value);
};
/**
* HMAC-SHA256 signature required to activate paid features
* [Reference documentation]{@link https://documentation.image-charts.com/enterprise/}
* @example
* const chart = ChartJSImage().ichm("0785cf22a0381c2e0239e27c126de4181f501d117c2c81745611e9db928b0376");
*
* @param {string} value - HMAC-SHA256 signature required to activate paid features
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.ichm = function(value) {
return this._clone('ichm', value);
};
/**
* Retina is a marketing term coined by Apple that refers to devices and monitors that have a resolution and pixel density so high — roughly 300 or more pixels per inch – that a person is unable to discern the individual pixels at a normal viewing distance.
* In order to generate beautiful charts for these Retina displays, Image-Charts supports a retina mode that can be activated through the icretina=1 parameter
* [Reference documentation]{@link https://documentation.image-charts.com/reference/retina/}
* @example
* const chart = ChartJSImage().icretina("1");
*
* @param {string} value - retina mode
* @return {ChartJSImage.constructor}
*/
ChartJSImage.prototype.icretina = function(value) {
return this._clone('icretina', value);
};
/**
* Get the full ChartJSImage API url (signed and encoded if necessary)
* @return {string} full generated url
*/
ChartJSImage.prototype.toURL = function () {
const url = new URL(`${this._protocol}://${this._host}`);
/* istanbul ignore else */
if(this._port) {url.port = this._port}
url.pathname = this._pathname;
const searchParams = new URLSearchParams(this._query);
if (this._query.icac && this._secret && this._secret.length) {
searchParams.append('ichm', crypto.createHmac('sha256', this._secret).update(searchParams.toString()).digest('hex'));
}
url.search = searchParams.toString();
return url.toString();
};
/**
* Do a request to ChartJSImage API with current configuration and yield a promise of a NodeJS buffer
* @return {Promise<Buffer>} binary image represented as a NodeJS Buffer wrapped inside a promise
*/
ChartJSImage.prototype.toBuffer = function () {
const defaultUserAgent = `javascript-chart.js-image/${packageJson.version}` + (this._query.icac ? ' ' + `(${this._query.icac})` : '');
const _options = {
timeout: this._timeout,
headers: { 'User-Agent': this._userAgent || defaultUserAgent }
};
return fetch(this.toURL(), _options).then(res => {
return res.buffer().then(buff => {
if(res.status >= 200 && res.status < 300){
buff._response = res;
buff._request = _options;
return buff;
}
const validation_message = res.headers.get('x-ic-error-validation');
const validation_code = res.headers.get('x-ic-error-code');
let message = validation_message ? JSON.parse(validation_message).map(x => x.message).join('\n').trim() : '';
/* istanbul ignore next */
message = message.length > 0 || !validation_code ? message : validation_code;
/* istanbul ignore next */
message = message.length > 0 ? message : res.statusText;
const err = new Error(message);
/* istanbul ignore next */
err.code = validation_code || res.statusText;
err.statusCode = res.statusCode;
err._response = res;
err._request = _options;
return Promise.reject(err);
});
})
};
/**
* Do a request to ChartJSImage API with current configuration and writes the content inside a file
* @return {Promise}
*/
ChartJSImage.prototype.toFile = function (file) {
return this.toBuffer().then(buffer =>
new Promise((resolve, reject) =>
fs.writeFile(file, buffer, (err) => err ? reject(err) : resolve())))
};
/**
* Do a request to ChartJSImageAPI with current configuration and yield a promise of a base64 encoded data URI
* @return {Promise<String>} base64 data URI wrapped inside a promise
*/
ChartJSImage.prototype.toDataURI = function () {
const encoding = 'base64';
const mimetype = this._query.chan ? 'image/gif' : 'image/png';
return this.toBuffer().then(buffer => `data:${mimetype};${encoding},${buffer.toString(encoding)}`);
};
module.exports = ChartJSImage;