genanki-js 2.0.0 → 2.1.0

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.
@@ -0,0 +1,518 @@
1
+ /**
2
+ * [js-sha256]{@link https://github.com/emn178/js-sha256}
3
+ *
4
+ * @version 0.9.0
5
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
6
+ * @copyright Chen, Yi-Cyuan 2014-2017
7
+ * @license MIT
8
+ */
9
+ /*jslint bitwise: true */
10
+ (function () {
11
+ 'use strict';
12
+
13
+ var ERROR = 'input is invalid type';
14
+ var WINDOW = typeof window === 'object';
15
+ var root = WINDOW ? window : {};
16
+ if (root.JS_SHA256_NO_WINDOW) {
17
+ WINDOW = false;
18
+ }
19
+ var WEB_WORKER = !WINDOW && typeof self === 'object';
20
+ var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
21
+ if (NODE_JS) {
22
+ root = global;
23
+ } else if (WEB_WORKER) {
24
+ root = self;
25
+ }
26
+ var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
27
+ var AMD = typeof define === 'function' && define.amd;
28
+ var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
29
+ var HEX_CHARS = '0123456789abcdef'.split('');
30
+ var EXTRA = [-2147483648, 8388608, 32768, 128];
31
+ var SHIFT = [24, 16, 8, 0];
32
+ var K = [
33
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
34
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
35
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
36
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
37
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
38
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
39
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
40
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
41
+ ];
42
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
43
+
44
+ var blocks = [];
45
+
46
+ if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
47
+ Array.isArray = function (obj) {
48
+ return Object.prototype.toString.call(obj) === '[object Array]';
49
+ };
50
+ }
51
+
52
+ if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
53
+ ArrayBuffer.isView = function (obj) {
54
+ return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
55
+ };
56
+ }
57
+
58
+ var createOutputMethod = function (outputType, is224) {
59
+ return function (message) {
60
+ return new Sha256(is224, true).update(message)[outputType]();
61
+ };
62
+ };
63
+
64
+ var createMethod = function (is224) {
65
+ var method = createOutputMethod('hex', is224);
66
+ if (NODE_JS) {
67
+ method = nodeWrap(method, is224);
68
+ }
69
+ method.create = function () {
70
+ return new Sha256(is224);
71
+ };
72
+ method.update = function (message) {
73
+ return method.create().update(message);
74
+ };
75
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
76
+ var type = OUTPUT_TYPES[i];
77
+ method[type] = createOutputMethod(type, is224);
78
+ }
79
+ return method;
80
+ };
81
+
82
+ var nodeWrap = function (method, is224) {
83
+ var crypto = eval("require('crypto')");
84
+ var Buffer = eval("require('buffer').Buffer");
85
+ var algorithm = is224 ? 'sha224' : 'sha256';
86
+ var nodeMethod = function (message) {
87
+ if (typeof message === 'string') {
88
+ return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
89
+ } else {
90
+ if (message === null || message === undefined) {
91
+ throw new Error(ERROR);
92
+ } else if (message.constructor === ArrayBuffer) {
93
+ message = new Uint8Array(message);
94
+ }
95
+ }
96
+ if (Array.isArray(message) || ArrayBuffer.isView(message) ||
97
+ message.constructor === Buffer) {
98
+ return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
99
+ } else {
100
+ return method(message);
101
+ }
102
+ };
103
+ return nodeMethod;
104
+ };
105
+
106
+ var createHmacOutputMethod = function (outputType, is224) {
107
+ return function (key, message) {
108
+ return new HmacSha256(key, is224, true).update(message)[outputType]();
109
+ };
110
+ };
111
+
112
+ var createHmacMethod = function (is224) {
113
+ var method = createHmacOutputMethod('hex', is224);
114
+ method.create = function (key) {
115
+ return new HmacSha256(key, is224);
116
+ };
117
+ method.update = function (key, message) {
118
+ return method.create(key).update(message);
119
+ };
120
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
121
+ var type = OUTPUT_TYPES[i];
122
+ method[type] = createHmacOutputMethod(type, is224);
123
+ }
124
+ return method;
125
+ };
126
+
127
+ function Sha256(is224, sharedMemory) {
128
+ if (sharedMemory) {
129
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
130
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
131
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
132
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
133
+ this.blocks = blocks;
134
+ } else {
135
+ this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
136
+ }
137
+
138
+ if (is224) {
139
+ this.h0 = 0xc1059ed8;
140
+ this.h1 = 0x367cd507;
141
+ this.h2 = 0x3070dd17;
142
+ this.h3 = 0xf70e5939;
143
+ this.h4 = 0xffc00b31;
144
+ this.h5 = 0x68581511;
145
+ this.h6 = 0x64f98fa7;
146
+ this.h7 = 0xbefa4fa4;
147
+ } else { // 256
148
+ this.h0 = 0x6a09e667;
149
+ this.h1 = 0xbb67ae85;
150
+ this.h2 = 0x3c6ef372;
151
+ this.h3 = 0xa54ff53a;
152
+ this.h4 = 0x510e527f;
153
+ this.h5 = 0x9b05688c;
154
+ this.h6 = 0x1f83d9ab;
155
+ this.h7 = 0x5be0cd19;
156
+ }
157
+
158
+ this.block = this.start = this.bytes = this.hBytes = 0;
159
+ this.finalized = this.hashed = false;
160
+ this.first = true;
161
+ this.is224 = is224;
162
+ }
163
+
164
+ Sha256.prototype.update = function (message) {
165
+ if (this.finalized) {
166
+ return;
167
+ }
168
+ var notString, type = typeof message;
169
+ if (type !== 'string') {
170
+ if (type === 'object') {
171
+ if (message === null) {
172
+ throw new Error(ERROR);
173
+ } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
174
+ message = new Uint8Array(message);
175
+ } else if (!Array.isArray(message)) {
176
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
177
+ throw new Error(ERROR);
178
+ }
179
+ }
180
+ } else {
181
+ throw new Error(ERROR);
182
+ }
183
+ notString = true;
184
+ }
185
+ var code, index = 0, i, length = message.length, blocks = this.blocks;
186
+
187
+ while (index < length) {
188
+ if (this.hashed) {
189
+ this.hashed = false;
190
+ blocks[0] = this.block;
191
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
192
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
193
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
194
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
195
+ }
196
+
197
+ if (notString) {
198
+ for (i = this.start; index < length && i < 64; ++index) {
199
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
200
+ }
201
+ } else {
202
+ for (i = this.start; index < length && i < 64; ++index) {
203
+ code = message.charCodeAt(index);
204
+ if (code < 0x80) {
205
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
206
+ } else if (code < 0x800) {
207
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
208
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
209
+ } else if (code < 0xd800 || code >= 0xe000) {
210
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
211
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
212
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
213
+ } else {
214
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
215
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
216
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
217
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
218
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
219
+ }
220
+ }
221
+ }
222
+
223
+ this.lastByteIndex = i;
224
+ this.bytes += i - this.start;
225
+ if (i >= 64) {
226
+ this.block = blocks[16];
227
+ this.start = i - 64;
228
+ this.hash();
229
+ this.hashed = true;
230
+ } else {
231
+ this.start = i;
232
+ }
233
+ }
234
+ if (this.bytes > 4294967295) {
235
+ this.hBytes += this.bytes / 4294967296 << 0;
236
+ this.bytes = this.bytes % 4294967296;
237
+ }
238
+ return this;
239
+ };
240
+
241
+ Sha256.prototype.finalize = function () {
242
+ if (this.finalized) {
243
+ return;
244
+ }
245
+ this.finalized = true;
246
+ var blocks = this.blocks, i = this.lastByteIndex;
247
+ blocks[16] = this.block;
248
+ blocks[i >> 2] |= EXTRA[i & 3];
249
+ this.block = blocks[16];
250
+ if (i >= 56) {
251
+ if (!this.hashed) {
252
+ this.hash();
253
+ }
254
+ blocks[0] = this.block;
255
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
256
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
257
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
258
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
259
+ }
260
+ blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
261
+ blocks[15] = this.bytes << 3;
262
+ this.hash();
263
+ };
264
+
265
+ Sha256.prototype.hash = function () {
266
+ var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
267
+ h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
268
+
269
+ for (j = 16; j < 64; ++j) {
270
+ // rightrotate
271
+ t1 = blocks[j - 15];
272
+ s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
273
+ t1 = blocks[j - 2];
274
+ s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
275
+ blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
276
+ }
277
+
278
+ bc = b & c;
279
+ for (j = 0; j < 64; j += 4) {
280
+ if (this.first) {
281
+ if (this.is224) {
282
+ ab = 300032;
283
+ t1 = blocks[0] - 1413257819;
284
+ h = t1 - 150054599 << 0;
285
+ d = t1 + 24177077 << 0;
286
+ } else {
287
+ ab = 704751109;
288
+ t1 = blocks[0] - 210244248;
289
+ h = t1 - 1521486534 << 0;
290
+ d = t1 + 143694565 << 0;
291
+ }
292
+ this.first = false;
293
+ } else {
294
+ s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
295
+ s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
296
+ ab = a & b;
297
+ maj = ab ^ (a & c) ^ bc;
298
+ ch = (e & f) ^ (~e & g);
299
+ t1 = h + s1 + ch + K[j] + blocks[j];
300
+ t2 = s0 + maj;
301
+ h = d + t1 << 0;
302
+ d = t1 + t2 << 0;
303
+ }
304
+ s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
305
+ s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
306
+ da = d & a;
307
+ maj = da ^ (d & b) ^ ab;
308
+ ch = (h & e) ^ (~h & f);
309
+ t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
310
+ t2 = s0 + maj;
311
+ g = c + t1 << 0;
312
+ c = t1 + t2 << 0;
313
+ s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
314
+ s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
315
+ cd = c & d;
316
+ maj = cd ^ (c & a) ^ da;
317
+ ch = (g & h) ^ (~g & e);
318
+ t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
319
+ t2 = s0 + maj;
320
+ f = b + t1 << 0;
321
+ b = t1 + t2 << 0;
322
+ s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
323
+ s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
324
+ bc = b & c;
325
+ maj = bc ^ (b & d) ^ cd;
326
+ ch = (f & g) ^ (~f & h);
327
+ t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
328
+ t2 = s0 + maj;
329
+ e = a + t1 << 0;
330
+ a = t1 + t2 << 0;
331
+ }
332
+
333
+ this.h0 = this.h0 + a << 0;
334
+ this.h1 = this.h1 + b << 0;
335
+ this.h2 = this.h2 + c << 0;
336
+ this.h3 = this.h3 + d << 0;
337
+ this.h4 = this.h4 + e << 0;
338
+ this.h5 = this.h5 + f << 0;
339
+ this.h6 = this.h6 + g << 0;
340
+ this.h7 = this.h7 + h << 0;
341
+ };
342
+
343
+ Sha256.prototype.hex = function () {
344
+ this.finalize();
345
+
346
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
347
+ h6 = this.h6, h7 = this.h7;
348
+
349
+ var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
350
+ HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
351
+ HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
352
+ HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
353
+ HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
354
+ HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
355
+ HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
356
+ HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
357
+ HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
358
+ HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
359
+ HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
360
+ HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
361
+ HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
362
+ HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
363
+ HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
364
+ HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
365
+ HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
366
+ HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
367
+ HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
368
+ HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
369
+ HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
370
+ HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
371
+ HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
372
+ HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
373
+ HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
374
+ HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
375
+ HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
376
+ HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
377
+ if (!this.is224) {
378
+ hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
379
+ HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
380
+ HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
381
+ HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
382
+ }
383
+ return hex;
384
+ };
385
+
386
+ Sha256.prototype.toString = Sha256.prototype.hex;
387
+
388
+ Sha256.prototype.digest = function () {
389
+ this.finalize();
390
+
391
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
392
+ h6 = this.h6, h7 = this.h7;
393
+
394
+ var arr = [
395
+ (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
396
+ (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
397
+ (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
398
+ (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
399
+ (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
400
+ (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
401
+ (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
402
+ ];
403
+ if (!this.is224) {
404
+ arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
405
+ }
406
+ return arr;
407
+ };
408
+
409
+ Sha256.prototype.array = Sha256.prototype.digest;
410
+
411
+ Sha256.prototype.arrayBuffer = function () {
412
+ this.finalize();
413
+
414
+ var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
415
+ var dataView = new DataView(buffer);
416
+ dataView.setUint32(0, this.h0);
417
+ dataView.setUint32(4, this.h1);
418
+ dataView.setUint32(8, this.h2);
419
+ dataView.setUint32(12, this.h3);
420
+ dataView.setUint32(16, this.h4);
421
+ dataView.setUint32(20, this.h5);
422
+ dataView.setUint32(24, this.h6);
423
+ if (!this.is224) {
424
+ dataView.setUint32(28, this.h7);
425
+ }
426
+ return buffer;
427
+ };
428
+
429
+ function HmacSha256(key, is224, sharedMemory) {
430
+ var i, type = typeof key;
431
+ if (type === 'string') {
432
+ var bytes = [], length = key.length, index = 0, code;
433
+ for (i = 0; i < length; ++i) {
434
+ code = key.charCodeAt(i);
435
+ if (code < 0x80) {
436
+ bytes[index++] = code;
437
+ } else if (code < 0x800) {
438
+ bytes[index++] = (0xc0 | (code >> 6));
439
+ bytes[index++] = (0x80 | (code & 0x3f));
440
+ } else if (code < 0xd800 || code >= 0xe000) {
441
+ bytes[index++] = (0xe0 | (code >> 12));
442
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
443
+ bytes[index++] = (0x80 | (code & 0x3f));
444
+ } else {
445
+ code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
446
+ bytes[index++] = (0xf0 | (code >> 18));
447
+ bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
448
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
449
+ bytes[index++] = (0x80 | (code & 0x3f));
450
+ }
451
+ }
452
+ key = bytes;
453
+ } else {
454
+ if (type === 'object') {
455
+ if (key === null) {
456
+ throw new Error(ERROR);
457
+ } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
458
+ key = new Uint8Array(key);
459
+ } else if (!Array.isArray(key)) {
460
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
461
+ throw new Error(ERROR);
462
+ }
463
+ }
464
+ } else {
465
+ throw new Error(ERROR);
466
+ }
467
+ }
468
+
469
+ if (key.length > 64) {
470
+ key = (new Sha256(is224, true)).update(key).array();
471
+ }
472
+
473
+ var oKeyPad = [], iKeyPad = [];
474
+ for (i = 0; i < 64; ++i) {
475
+ var b = key[i] || 0;
476
+ oKeyPad[i] = 0x5c ^ b;
477
+ iKeyPad[i] = 0x36 ^ b;
478
+ }
479
+
480
+ Sha256.call(this, is224, sharedMemory);
481
+
482
+ this.update(iKeyPad);
483
+ this.oKeyPad = oKeyPad;
484
+ this.inner = true;
485
+ this.sharedMemory = sharedMemory;
486
+ }
487
+ HmacSha256.prototype = new Sha256();
488
+
489
+ HmacSha256.prototype.finalize = function () {
490
+ Sha256.prototype.finalize.call(this);
491
+ if (this.inner) {
492
+ this.inner = false;
493
+ var innerHash = this.array();
494
+ Sha256.call(this, this.is224, this.sharedMemory);
495
+ this.update(this.oKeyPad);
496
+ this.update(innerHash);
497
+ Sha256.prototype.finalize.call(this);
498
+ }
499
+ };
500
+
501
+ var exports = createMethod();
502
+ exports.sha256 = exports;
503
+ exports.sha224 = createMethod(true);
504
+ exports.sha256.hmac = createHmacMethod();
505
+ exports.sha224.hmac = createHmacMethod(true);
506
+
507
+ if (COMMON_JS) {
508
+ module.exports = exports;
509
+ } else {
510
+ root.sha256 = exports.sha256;
511
+ root.sha224 = exports.sha224;
512
+ if (AMD) {
513
+ define(function () {
514
+ return exports;
515
+ });
516
+ }
517
+ }
518
+ })();
package/package.json CHANGED
@@ -1,36 +1,55 @@
1
- {
2
- "name": "genanki-js",
3
- "version": "2.0.0",
4
- "description": "A JavaScript implementation for generating Anki decks in browser client side",
5
- "main": "dist/genanki.js",
6
- "directories": {
7
- "doc": "docs"
8
- },
9
- "scripts": {
10
- "test": "echo \"Error: no test specified\" && exit 1"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/krmanik/genanki-js.git"
15
- },
16
- "keywords": [
17
- "genanki",
18
- "anki"
19
- ],
20
- "author": "Mani",
21
- "license": "AGPL-3.0-or-later",
22
- "bugs": {
23
- "url": "https://github.com/krmanik/genanki-js/issues"
24
- },
25
- "homepage": "https://github.com/krmanik/genanki-js#readme",
26
- "dependencies": {
27
- "big-integer": "^1.6.51",
28
- "file-saver": "^2.0.5",
29
- "gulp": "^4.0.2",
30
- "gulp-concat": "^2.6.1",
31
- "gulp-minify": "^3.1.0",
32
- "js-sha256": "^0.9.0",
33
- "jszip": "^3.9.1",
34
- "sql.js": "^1.6.2"
35
- }
36
- }
1
+ {
2
+ "name": "genanki-js",
3
+ "version": "2.1.0",
4
+ "description": "A JavaScript implementation for generating Anki decks in browser client side",
5
+ "main": "dist/genanki.cjs",
6
+ "module": "dist/genanki.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/genanki.mjs",
10
+ "require": "./dist/genanki.cjs"
11
+ },
12
+ "./dist/*": "./dist/*",
13
+ "./package.json": "./package.json"
14
+ },
15
+ "unpkg": "dist/genanki.js",
16
+ "jsdelivr": "dist/genanki.js",
17
+ "files": [
18
+ "dist",
19
+ "genanki",
20
+ "License.md"
21
+ ],
22
+ "directories": {
23
+ "doc": "docs"
24
+ },
25
+ "scripts": {
26
+ "build": "node scripts/build.js",
27
+ "test": "node --test test/*.test.mjs test/*.test.cjs",
28
+ "vendor": "node scripts/vendor.js"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/krmanik/genanki-js.git"
33
+ },
34
+ "keywords": [
35
+ "genanki",
36
+ "anki",
37
+ "apkg",
38
+ "flashcards"
39
+ ],
40
+ "author": "Mani",
41
+ "license": "AGPL-3.0-or-later",
42
+ "bugs": {
43
+ "url": "https://github.com/krmanik/genanki-js/issues"
44
+ },
45
+ "homepage": "https://github.com/krmanik/genanki-js#readme",
46
+ "dependencies": {
47
+ "jszip": "^3.10.1",
48
+ "sql.js": "^1.15.0"
49
+ },
50
+ "devDependencies": {
51
+ "codemirror": "^5.65.21",
52
+ "eruda": "^3.4.3",
53
+ "file-saver": "^2.0.5"
54
+ }
55
+ }
package/Changelog.md DELETED
@@ -1,13 +0,0 @@
1
- ## 2.0.0
2
- Now, import genanki as node modules.
3
- View [Documentation](https://krmanik.github.io/genanki-js/)
4
-
5
- ## v1.0.5
6
- genanki zip file contains two folder
7
-
8
- - `dist` : This folder contains `genanki.js` file. If project already configured with [sql.js](https://github.com/sql-js/sql.js), [FileSaver.js](https://github.com/eligrey/FileSaver.js) and [JSZip](https://github.com/Stuk/jszip) then just add `genanki.js` file to project.
9
-
10
- - `sample` : This folder contains latest [sql.js](https://github.com/sql-js/sql.js), [FileSaver.js](https://github.com/eligrey/FileSaver.js), [JSZip](https://github.com/Stuk/jszip) and [genanki.js](https://github.com/krmanik/genanki-js). It is ready to use folder.
11
-
12
-
13
- View [Documentation](https://krmanik.github.io/genanki-js/)
@@ -1 +0,0 @@
1
- import{saveAs}from"file-saver";import{sha256}from"js-sha256";import JSZip from"jszip";import bigInt from"big-integer";const BASE91_TABLE=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","!","#","$","%","&","(",")","*","+",",","-",".","/",":",";","<","=",">","?","@","[","]","^","_","`","{","|","}","~"];function ankiHash(n){const e=n.join("__"),t=sha256.create();t.update(e);const o=t.digest();let l=bigInt();for(let n=0;n<8;n++)l*=bigInt("256"),l+=bigInt(o[n]);let s=[];for(;l>0;)s.push(BASE91_TABLE[l%bigInt("91")]),l/=bigInt("91");return s.reverse().join("")}const MODEL_STD=0,MODEL_CLOZE=1;export class Model{constructor(n){this.props={...defaultModel,...n,flds:n.flds.map((n,e)=>({...defaultField,ord:e,...n})),tmpls:n.tmpls.map((n,e)=>({...defaultTemplate,ord:e,name:`Card ${e+1}`,...n})),mod:(new Date).getTime()},this.fieldNameToOrd={},this.props.flds.forEach(n=>{this.fieldNameToOrd[n.name]=n.ord})}note(n,e,t=null){if(Array.isArray(n)){if(n.length!==this.props.flds.length)throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${n.length}`);return new Note(this,n,e,t)}{const o=Object.keys(n),l=[];return o.forEach(e=>{const t=this.fieldNameToOrd[e];if(null==t)throw new Error(`Field '${e}' does not exist in the model`);l[t]=n[e]}),new Note(this,l,e,t)}}}export class ClozeModel extends Model{constructor(n){super({type:MODEL_CLOZE,css:"\n .card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n }\n \n .cloze {\n font-weight: bold;\n color: blue;\n }\n ",tmpls:[{name:"Cloze",...n.tmpl}],...n})}}export const defaultModel={sortf:0,did:1,latexPre:"\\documentclass[12pt]{article}\n \\special{papersize=3in,5in}\n \\usepackage[utf8]{inputenc}\n \\usepackage{amssymb,amsmath}\n \\pagestyle{empty}\n \\setlength{\\parindent}{0in}\n \\begin{document}",latexPost:"\\end{document}",mod:0,usn:0,vers:[],type:MODEL_STD,css:".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n }",tags:[]};export const defaultField={name:"",ord:null,sticky:!1,rtl:!1,font:"Arial",size:20,media:[]};export const defaultTemplate={name:"",ord:null,qfmt:"",afmt:"",did:null,bqfmt:"",bafmt:""};const NEW_CARDS_DISTRIBUTE=0,NEW_CARDS_LAST=1,NEW_CARDS_FIRST=2;export const defaultConf={activeDecks:[1],curDeck:1,newSpread:0,collapseTime:1200,timeLim:0,estTimes:!0,dueCounts:!0,curModel:null,nextPos:1,sortType:"noteFld",sortBackwards:!1,addToCur:!0,dayLearnFirst:!1};const NEW_CARDS_RANDOM=0,NEW_CARDS_DUE=1,STARTING_FACTOR=2500;export const defaultDeckConf={name:"Default",new:{delays:[1,10],ints:[1,4,7],initialFactor:2500,separate:!0,order:1,perDay:20,bury:!1},lapse:{delays:[10],mult:0,minInt:1,leechFails:8,leechAction:0},rev:{perDay:200,ease4:1.3,fuzz:.05,minSpace:1,ivlFct:1,maxIvl:36500,bury:!1,hardFactor:1.2},maxTaken:60,timer:0,autoplay:!0,replayq:!0,mod:0,usn:0};export const defaultDeck={newToday:[0,0],revToday:[0,0],lrnToday:[0,0],timeToday:[0,0],conf:1,usn:0,desc:"",dyn:0,collapsed:!1,extendNew:10,extendRev:50};export class Deck{constructor(n,e,t=""){this.id=n,this.name=e,this.desc=t,this.notes=[]}addNote(n){this.notes.push(n)}}export class Note{constructor(n,e,t=null,o=null){this.model=n,this.fields=e,this.tags=t,this._guid=o}get guid(){return this._guid?this._guid:ankiHash(this.fields)}get cards(){if(this.model.props.type===MODEL_STD){const n=n=>!n||0===n.toString().trim().length,e=[];for(const[t,o,l]of this.model.props.req){l["any"===o?"some":"every"](e=>!n(this.fields[e]))&&e.push(t)}return e}{const n=new Set,e=[],t=/{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g,o=/<%cloze:(.+?)%>/g,{qfmt:l}=this.model.props.tmpls[0];let s;for(;s=t.exec(l);)e.push(s[1]);for(;s=o.exec(l);)e.push(s[1]);const i={};this.model.props.flds.forEach((n,e)=>{i[n.name]=[e,n]});for(const t of e){if(!(t in i))continue;const e=i[t][0],o=/{{c(\d+)::.+?}}/gs;for(;s=o.exec(this.fields[e]);){const e=parseInt(s[1]);e>0&&n.add(e-1)}}return 0===n.size?[0]:Array.from(n)}}}export class Package{constructor(){this.db=null,this.decks=[],this.media=[]}setSqlJs(n){this.db=n}addDeck(n){this.decks.push(n)}addMedia(n,e){this.media.push({name:e,data:n})}addMediaFile(n,e=null){this.media.push({name:e||n,filename:n})}writeToFile(n){let e=this.db;e.run(APKG_SCHEMA),this.write(e);let t=new JSZip;const o=e.export(),l=new Uint8Array(o).buffer;t.file("collection.anki2",l);const s={};this.media.forEach((n,e)=>{null!=n.filename?t.file(e.toString(),n.filename):t.file(e.toString(),n.data),s[e]=n.name}),t.file("media",JSON.stringify(s)),t.generateAsync({type:"blob",mimeType:"application/apkg"}).then(function(e){saveAs(e,n)})}write(n){const e=new Date,t={},o={};o[1]={...defaultDeck,id:1,name:"Default"},this.decks.forEach(n=>{n.notes.forEach(n=>t[n.model.props.id]=n.model.props),o[n.id]={...defaultDeck,id:n.id,name:n.name,desc:n.desc}});const l=[null,+e/1e3|0,+e,+e,11,0,0,0,JSON.stringify(defaultConf),JSON.stringify(t),JSON.stringify(o),JSON.stringify({1:{id:1,...defaultDeckConf}}),JSON.stringify({})];n.prepare("INSERT INTO col\n (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").run(l);const s=n.prepare("INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data) \n VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')"),i=n.prepare("INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data) \n VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')");for(const t of this.decks)for(const o of t.notes){let l=null==o.tags?"":o.tags.join(" ");s.run([o.guid,o.model.props.id,+e/1e3|0,-1,l,o.fields.join(""),0]);let r=n.exec("select last_insert_rowid();")[0].values[0][0];for(const n of o.cards)i.run([r,t.id,n,+e/1e3|0,-1,0,0])}}}export const APKG_SCHEMA="\nPRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n\nCREATE TABLE col (\n id integer primary key,\n crt integer not null,\n mod integer not null,\n scm integer not null,\n ver integer not null,\n dty integer not null,\n usn integer not null,\n ls integer not null,\n conf text not null,\n models text not null,\n decks text not null,\n dconf text not null,\n tags text not null\n);\nCREATE TABLE notes (\n id integer primary key, /* 0 */\n guid text not null, /* 1 */\n mid integer not null, /* 2 */\n mod integer not null, /* 3 */\n usn integer not null, /* 4 */\n tags text not null, /* 5 */\n flds text not null, /* 6 */\n sfld integer not null, /* 7 */\n csum integer not null, /* 8 */\n flags integer not null, /* 9 */\n data text not null /* 10 */\n);\nCREATE TABLE cards (\n id integer primary key, /* 0 */\n nid integer not null, /* 1 */\n did integer not null, /* 2 */\n ord integer not null, /* 3 */\n mod integer not null, /* 4 */\n usn integer not null, /* 5 */\n type integer not null, /* 6 */\n queue integer not null, /* 7 */\n due integer not null, /* 8 */\n ivl integer not null, /* 9 */\n factor integer not null, /* 10 */\n reps integer not null, /* 11 */\n lapses integer not null, /* 12 */\n left integer not null, /* 13 */\n odue integer not null, /* 14 */\n odid integer not null, /* 15 */\n flags integer not null, /* 16 */\n data text not null /* 17 */\n);\nCREATE TABLE revlog (\n id integer primary key,\n cid integer not null,\n usn integer not null,\n ease integer not null,\n ivl integer not null,\n lastIvl integer not null,\n factor integer not null,\n time integer not null,\n type integer not null\n);\nCREATE TABLE graves (\n usn integer not null,\n oid integer not null,\n type integer not null\n);\nCREATE INDEX ix_notes_usn on notes (usn);\nCREATE INDEX ix_cards_usn on cards (usn);\nCREATE INDEX ix_revlog_usn on revlog (usn);\nCREATE INDEX ix_cards_nid on cards (nid);\nCREATE INDEX ix_cards_sched on cards (did, queue, due);\nCREATE INDEX ix_revlog_cid on revlog (cid);\nCREATE INDEX ix_notes_csum on notes (csum);\nCOMMIT;\n";
package/genanki/import.js DELETED
@@ -1,4 +0,0 @@
1
- import { saveAs } from "file-saver";
2
- import { sha256 } from "js-sha256";
3
- import JSZip from "jszip";
4
- import bigInt from "big-integer";
package/gulpfile.js DELETED
@@ -1,28 +0,0 @@
1
- const {src, dest} = require('gulp');
2
- const concat = require('gulp-concat');
3
- const minify = require("gulp-minify");
4
-
5
- const jsFiles = [
6
- './genanki/license.js',
7
- './genanki/import.js',
8
- './genanki/anki_hash.js',
9
- './genanki/model.js',
10
- './genanki/default.js',
11
- './genanki/deck.js',
12
- './genanki/note.js',
13
- './genanki/package.js',
14
- './genanki/apkg_schema.js'
15
- ]
16
-
17
- const bundleJs = () => {
18
- return src(jsFiles)
19
- // bundle all js files in genanki folder in genanki.js file
20
- .pipe(concat('genanki.js'))
21
- .pipe(minify({}))
22
- // write genanki.js to dist, demo and sample folder
23
- .pipe(dest('./dist'))
24
- //.pipe(dest('./docs/demo/js/anki'))
25
- //.pipe(dest('./sample/js/anki'))
26
- }
27
-
28
- exports.bundleJs = bundleJs;