-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
executable file
·307 lines (279 loc) · 8.97 KB
/
index.js
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
/*
* rfg-api.js
* https://github.com/RealFaviconGenerator/rfg-api.js
*
* Copyright (c) 2014 Philippe Bernard & Hayden Bleasel
* Licensed under the MIT license.
*/
/*jslint node:true*/
module.exports.init = function() {
'use strict';
var exports = {};
var https = require('https');
var fs = require('fs');
var unzip = require('node-unzip-2');
var metaparser = require('metaparser');
var fstream = require('fstream');
var mkdirp = require('mkdirp');
var axios = require('axios');
exports.fileToBase64 = function(file, callback) {
fs.readFile(file, { encoding: null }, function(error, file) {
if (error) {
callback(error);
}
else {
callback(undefined, file.toString('base64'));
}
});
};
exports.fileToBase64Sync = function(file) {
return fs.readFileSync(file, { encoding: null }).toString('base64');
};
exports.generateFavicon = function(request, dest, callback) {
mkdirp(dest, function() {
axios.post(
"https://realfavicongenerator.net/api/favicon", {
"favicon_generation": request
}
)
.then(function(response) {
var data = response.data;
var writeStream = fstream.Writer(dest);
writeStream.on('close', function() {
callback(undefined, data.favicon_generation_result);
});
var parserStream = unzip.Parse();
var request = https.get(data.favicon_generation_result.favicon.package_url, function (response) {
response.pipe(parserStream).pipe(writeStream);
});
})
.catch(function(error) {
var err = (
error &&
error.data &&
error.data.favicon_generation_result &&
error.data.favicon_generation_result.result &&
error.data.favicon_generation_result.result.error_message)
? error.data.favicon_generation_result.result.error_message
: error;
callback(err);
});
});
};
exports.injectFaviconMarkups = function(fileContent, htmlCode, opts, callback) {
var defaultRemove = [
'link[rel="mask-icon"]',
'link[rel="shortcut icon"]',
'link[rel="icon"]',
'link[rel^="apple-touch-icon"]',
'link[rel="manifest"]',
'link[rel="yandex-tableau-widget"]',
'meta[name^="msapplication"]',
'meta[name="mobile-web-app-capable"]',
'meta[name="theme-color"]',
'meta[property="og:image"]'
];
var add = typeof html_code === 'string' ? [htmlCode] : htmlCode;
var remove = defaultRemove;
if (opts) {
if (opts.add) {
add = add.concat(typeof opts.add === 'string' ? [opts.add] : opts.add);
}
if (opts.remove) {
remove = remove.concat(typeof opts.remove === 'string' ? [opts.remove] : opts.remove);
}
if (opts.keep) {
if (typeof opts.keep === 'string') {
opts.keep = [opts.keep];
}
for (var m in opts.keep) {
var idx = remove.indexOf(opts.keep[m]);
if (idx >= 0) {
remove.splice(idx, 1);
}
}
}
}
metaparser({
data: fileContent,
add: add,
remove: remove,
callback: function(error, html) {
return callback(error, html);
}
});
};
exports.camelCaseToUnderscore = function(s) {
// Regex will also insert an underscore before a digit if that digit is preceded by a lowercase letter and followed
// by one or more additional digits, so it will insert an underscore before the "8" and the "1" in
// "windows80Ie10Tile" (yielding "windows_80_ie_10_tile", but NOT before the "6" in "ios6AndPriorIcons" (yielding
// "ios6_and_prior_icons").
return s.replace(/(?:^|\.?)([A-Z]|(?<=[a-z])\d(?=\d+))/g, function(x,y) {
return "_" + y.toLowerCase()
}).replace(/^_/, "");
}
exports.camelCaseToUnderscoreRequest = function(request) {
if (request === undefined) {
return undefined;
}
if (request.constructor === Array) {
for (var i = 0; i < request.length; i++) {
request[i] = exports.camelCaseToUnderscoreRequest(request[i]);
}
}
else if (request.constructor === String) {
return exports.camelCaseToUnderscore(request);
}
else if (request.constructor === Object) {
var keys = Object.keys(request);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var uKey = exports.camelCaseToUnderscore(keys[j]);
// Special case for some keys: content should be passed as is
var keysToIgnore = [
'scaling_algorithm',
'name',
'content',
'param_name',
'param_value',
'description',
'app_description',
'developer_name',
'app_name',
'existing_manifest',
'background_color',
'theme_color',
'desktop_browser',
'master_picture'
];
var newContent = (keysToIgnore.indexOf(uKey) >= 0)
? request[key]
: exports.camelCaseToUnderscoreRequest(request[key]);
if (key !== uKey) {
request[uKey] = newContent;
delete request[key];
}
else {
request[key] = newContent;
}
}
}
return request;
}
function startsWith(str, prefix) {
return str.lastIndexOf(prefix, 0) === 0;
}
exports.isUrl = function(urlOrPath) {
return startsWith(urlOrPath, 'http://') ||
startsWith(urlOrPath, 'https://') ||
startsWith(urlOrPath, '//');
}
exports.isBase64 = function(content) {
return /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/.test(content);
}
exports.normalizeMasterPicture = function(masterPicture) {
var masterPictureObject = {};
if (masterPicture.constructor === Object) {
if ((masterPicture.type === 'inline') || (masterPicture.content !== undefined)) {
masterPictureObject.type = 'inline';
masterPictureObject.content =
exports.isBase64(masterPicture.content)
? masterPicture.content
: exports.fileToBase64Sync(masterPicture.content);
}
else if (masterPicture.url) {
masterPictureObject.type = 'url';
masterPictureObject.url = masterPicture.url;
}
}
else if (exports.isUrl(masterPicture)) {
masterPictureObject.type = 'url';
masterPictureObject.url = masterPicture;
}
else {
masterPictureObject.type = 'inline';
masterPictureObject.content = exports.fileToBase64Sync(masterPicture);
}
return masterPictureObject;
}
exports.normalizeAllMasterPictures = function(request) {
if (request.constructor === Array) {
for (var i = 0; i < request.length; i++) {
request[i] = exports.normalizeAllMasterPictures(request[i]);
}
return request;
}
else if (request.constructor === Object) {
var keys = Object.keys(request);
for (var j = 0; j < keys.length; j++) {
if (keys[j] === 'master_picture') {
request[keys[j]] = exports.normalizeMasterPicture(request[keys[j]]);
}
else {
request[keys[j]] = exports.normalizeAllMasterPictures(request[keys[j]]);
}
}
return request;
}
else {
return request;
}
}
// opts should contain:
// - apiKey
// - masterPicture (can be a URL or a path to a local file)
// - iconsPath (or undefined if the files are in the root)
// - design
// - settings
// - versioning
exports.createRequest = function(opts) {
// Build favicon generation request
var request = {};
request.api_key = opts.apiKey;
// Master picture
request.master_picture = exports.normalizeMasterPicture(opts.masterPicture);
// Path
request.files_location = {};
if (opts.iconsPath === undefined) {
request.files_location.type = 'root';
}
else {
request.files_location.type = 'path';
request.files_location.path = opts.iconsPath;
}
// Design
request.favicon_design = exports.normalizeAllMasterPictures(
exports.camelCaseToUnderscoreRequest(opts.design));
// Settings
if (opts.settings) {
request.settings = exports.camelCaseToUnderscoreRequest(opts.settings);
}
// Versioning
if (opts.versioning) {
request.versioning = exports.camelCaseToUnderscoreRequest(opts.versioning);
}
return request;
};
exports.changeLog = function(sinceVersion, callback) {
var versionParam = (sinceVersion == undefined) ? '' : "?since=" + sinceVersion;
axios.get("https://realfavicongenerator.net/api/versions" + versionParam)
.then(function(response) {
callback(undefined, response.data);
})
.catch(function(error) {
callback(error);
});
};
exports.escapeJSONSpecialChars = function(json) {
return json
.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
};
return exports;
};