1 | const fs = require('fs-extra'),
|
2 | Utils = require('./services/utils/service'),
|
3 | utils = new Utils(),
|
4 | path = require('path'),
|
5 | package = require('../package.json'),
|
6 | md5 = require('md5'),
|
7 | minify = require('uglify-es').minify;
|
8 |
|
9 | module.exports = {
|
10 | package: package,
|
11 | protocol: package.protocol,
|
12 | version: package.version,
|
13 | __cachedBrowserClient: null,
|
14 | __createBrowserClient: function(options) {
|
15 | var package = require('../package.json');
|
16 | var protocol = package.protocol;
|
17 | var buf = fs.readFileSync(path.resolve(__dirname, './client.js'));
|
18 |
|
19 | var constantsbuf =
|
20 | '\r\nthis.CONSTANTS = ' +
|
21 | fs
|
22 | .readFileSync(path.resolve(__dirname, './constants.js'), 'utf8')
|
23 | .replace('module.exports = ', '') +
|
24 | '\r\n';
|
25 |
|
26 | var utilsbuf =
|
27 | '\r\nthis.utils = ' +
|
28 | fs
|
29 | .readFileSync(path.resolve(__dirname, './services/utils/shared.js'), 'utf8')
|
30 | .replace('module.exports = ', '') +
|
31 | '\r\n';
|
32 |
|
33 | var clientScript = buf
|
34 | .toString()
|
35 | .replace('{{protocol}}', protocol)
|
36 | .replace('{{version}}', package.version)
|
37 | .replace('//{{constants}}', constantsbuf)
|
38 | .replace('//{{utils}}', utilsbuf);
|
39 |
|
40 | if (process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() === 'production') {
|
41 | this.__cachedBrowserClient =
|
42 | '//happn client v' +
|
43 | package.version +
|
44 | '\r\n' +
|
45 | '//protocol v' +
|
46 | protocol +
|
47 | '\r\n' +
|
48 | '//id ' +
|
49 | options.id +
|
50 | '\r\n' +
|
51 | clientScript;
|
52 | } else {
|
53 | this.__cachedBrowserClient =
|
54 | '//happn client v' +
|
55 | package.version +
|
56 | '\r\n' +
|
57 | '//protocol v' +
|
58 | protocol +
|
59 | '\r\n' +
|
60 | clientScript;
|
61 | }
|
62 |
|
63 | if (options.min) {
|
64 | const minified = minify(this.__cachedBrowserClient);
|
65 | if (minified.error) throw minified.error;
|
66 | this.__cachedBrowserClient = minified.code;
|
67 | }
|
68 | },
|
69 |
|
70 | browserClient: function(options) {
|
71 | if (!options) options = {};
|
72 | var clientDirPath;
|
73 | var clientFilePath;
|
74 | var dirPath = require('homedir')();
|
75 |
|
76 | if (process.env.NODE_ENV && process.env.NODE_ENV.toLowerCase() === 'production') {
|
77 | if (!options.id) options.id = Date.now().toString();
|
78 | }
|
79 |
|
80 | clientDirPath = `${utils.removeLast(dirPath, path.sep)}${path.sep}.happner${path.sep}`;
|
81 | clientFilePath = clientDirPath + 'happn-3-browser-client-' + this.version + '.js';
|
82 |
|
83 | if (options.overwrite) {
|
84 | this.__cachedBrowserClient = null;
|
85 | try {
|
86 | fs.unlinkSync(clientFilePath);
|
87 | } catch (e) {
|
88 |
|
89 | }
|
90 | }
|
91 |
|
92 |
|
93 | if (options.contentsOnly && this.__cachedBrowserClient) return this.__cachedBrowserClient;
|
94 |
|
95 |
|
96 |
|
97 | if (!process.env.NODE_ENV || process.env.NODE_ENV.toLowerCase() !== 'production') {
|
98 | this.__createBrowserClient(options);
|
99 |
|
100 | if (utils.fileExists(clientFilePath)) {
|
101 | var oldMd5 = md5(fs.readFileSync(clientFilePath, 'utf8'));
|
102 | var newMd5 = md5(this.__cachedBrowserClient);
|
103 |
|
104 | if (oldMd5 !== newMd5) {
|
105 | try {
|
106 | fs.unlinkSync(clientFilePath);
|
107 | } catch (e) {
|
108 |
|
109 | }
|
110 | }
|
111 | }
|
112 | }
|
113 |
|
114 | if (utils.fileExists(clientFilePath)) {
|
115 | if (!options.contentsOnly) return clientFilePath;
|
116 | this.__cachedBrowserClient = fs.readFileSync(clientFilePath, 'utf8').toString();
|
117 | return this.__cachedBrowserClient;
|
118 | }
|
119 |
|
120 | if (!this.__cachedBrowserClient) this.__createBrowserClient(options);
|
121 | if (!dirPath) return this.__cachedBrowserClient;
|
122 | fs.ensureDirSync(clientDirPath);
|
123 | fs.writeFileSync(clientFilePath, this.__cachedBrowserClient, 'utf8');
|
124 | if (!options.contentsOnly) return clientFilePath;
|
125 | return this.__cachedBrowserClient;
|
126 | }
|
127 | };
|