-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
107 lines (100 loc) · 2.35 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
var debug = console.debug || console.log;
module.exports = {
load: function(_options, callback) {
var options = Object.assign({
sync: false,
libUrl: 'https://cdnjs.cloudflare.com/ajax/libs/zxcvbn/4.4.2/zxcvbn.js',
libIntegrity: 'sha384-jhGcGHNZytnBnH1wbEM3KxJYyRDy9Q0QLKjE65xk+aMqXFCdvFuYIjzMWAAWBBtR'
}, _options);
if (options.sync) {
return syncMode(options);
}
else {
return asyncMode(options, callback);
}
}
};
var LOAD_SUCCESS = +1;
var LOAD_ERROR = -1;
var hasLoaded = false;
var loadResult = 0;
function addScriptTag(url, integrity, loadCallback, errorCallback) {
if (!hasLoaded) {
hasLoaded = true;
var head = global.document.getElementsByTagName('head')[0];
var script = global.document.createElement('script');
script.type = 'text/javascript';
if (integrity) {
script.integrity = integrity;
script.crossOrigin = 'anonymous';
}
script.async = true;
script.onload = function() {
loadResult = LOAD_SUCCESS;
loadCallback();
};
script.onerror = function() {
loadResult = LOAD_ERROR;
errorCallback();
};
script.src = url;
head.appendChild(script);
}
else {
if (loadResult === LOAD_SUCCESS) {
loadCallback();
}
else if (loadResult === LOAD_ERROR) {
errorCallback();
}
}
}
var library = null;
function syncMode(options) {
addScriptTag(options.libUrl, options.libIntegrity, function() {
debug('zxcvbn loaded');
library = global.zxcvbn;
}, function() {
console.error(new Error('Cannot load zxcvbn'));
});
return function(password, user_inputs) {
if (library) {
return library(password, user_inputs);
}
else {
return {
password: password,
user_inputs: user_inputs,
guesses: -1,
guesses_log10: -1,
crack_times_seconds: -1,
crack_times_display: -1,
score: -1,
feedback: {
suggestions: [],
warning: '',
},
sequence: [],
calc_time: 0
};
}
};
}
function asyncMode(options, callback) {
if (typeof(callback) === 'function') {
addScriptTag(options.libUrl, options.libIntegrity, function() {
callback(null, global.zxcvbn);
}, function() {
callback(new Error('Cannot load zxcvbn'));
});
}
else {
return new Promise(function(resolve, reject) {
addScriptTag(options.libUrl, options.libIntegrity, function() {
resolve(global.zxcvbn);
}, function() {
reject(new Error('Cannot load zxcvbn'));
});
});
}
}