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,1277 @@
1
+ "use strict";
2
+ const JSZip = require("jszip")
3
+ /**
4
+ * [mkanki]{@link https://github.com/nornagon/mkanki}
5
+ * @copyright Copyright (c) 2018 Jeremy Apthorp
6
+ * @license AGPL-3.0 License
7
+ *
8
+ *
9
+ * [genanki]{@link https://github.com/kerrickstaley/genanki}
10
+ * @copyright Copyright (c) Kerrick Staley 2021
11
+ * @license The MIT License
12
+ *
13
+ *
14
+ * [genanki-js]{@link https://github.com/krmanik/genanki-js}
15
+ * @copyright Copyright (c) 2021 Mani
16
+ * @license AGPL-3.0 License
17
+ */
18
+ const BASE91_TABLE = [
19
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
20
+ 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
21
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4',
22
+ '5', '6', '7', '8', '9', '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', ':',
23
+ ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~']
24
+
25
+ function ankiHash(fields) {
26
+ const str = fields.join('__')
27
+ const h = sha256.create();
28
+ h.update(str)
29
+ const hex = h.digest()
30
+
31
+ // The first 8 bytes of the digest form a big-endian unsigned 64-bit integer,
32
+ // which is converted to the weird base91 format that Anki uses.
33
+ // Long division over the byte array is used instead of BigInt, because
34
+ // BigInt (and the `0n` literal syntax) is not supported on older Safari / iOS.
35
+ const digits = hex.slice(0, 8)
36
+ let rv_reversed = []
37
+ while (digits.some(d => d !== 0)) {
38
+ let remainder = 0
39
+ for (let i = 0; i < digits.length; i++) {
40
+ const acc = remainder * 256 + digits[i]
41
+ digits[i] = Math.floor(acc / 91)
42
+ remainder = acc % 91
43
+ }
44
+ rv_reversed.push(BASE91_TABLE[remainder])
45
+ }
46
+
47
+ return rv_reversed.reverse().join('')
48
+ }
49
+ const MODEL_STD = 0
50
+ const MODEL_CLOZE = 1
51
+
52
+ class Model {
53
+ constructor(props) {
54
+ this.props = {
55
+ ...defaultModel,
56
+ ...props,
57
+ flds: props.flds.map((f, i) => ({ ...defaultField, ord: i, ...f })),
58
+ tmpls: props.tmpls.map((t, i) => ({ ...defaultTemplate, ord: i, name: `Card ${i + 1}`, ...t })),
59
+ mod: new Date().getTime()
60
+ }
61
+ this.fieldNameToOrd = {}
62
+ this.props.flds.forEach(f => { this.fieldNameToOrd[f.name] = f.ord })
63
+ }
64
+
65
+ note(fields, tags, guid = null) {
66
+ if (Array.isArray(fields)) {
67
+ if (fields.length !== this.props.flds.length) {
68
+ throw new Error(`Expected ${this.props.flds.length} fields for model '${this.props.name}' but got ${fields.length}`)
69
+ }
70
+ return new Note(this, fields, tags, guid)
71
+ } else {
72
+ const field_names = Object.keys(fields)
73
+ const fields_list = []
74
+ field_names.forEach(field_name => {
75
+ const ord = this.fieldNameToOrd[field_name]
76
+ if (ord == null) throw new Error(`Field '${field_name}' does not exist in the model`)
77
+ fields_list[ord] = fields[field_name]
78
+ })
79
+ return new Note(this, fields_list, tags, guid)
80
+ }
81
+ }
82
+ }
83
+
84
+ class ClozeModel extends Model {
85
+ constructor(props) {
86
+ // `tmpl` is only used to build the single cloze template,
87
+ // it must not end up in the note type json
88
+ const { tmpl, ...rest } = props
89
+ super({
90
+ type: MODEL_CLOZE,
91
+ css: `
92
+ .card {
93
+ font-family: arial;
94
+ font-size: 20px;
95
+ text-align: center;
96
+ color: black;
97
+ background-color: white;
98
+ }
99
+
100
+ .cloze {
101
+ font-weight: bold;
102
+ color: blue;
103
+ }
104
+ `,
105
+ tmpls: [{ name: "Cloze", ...tmpl }],
106
+ ...rest
107
+ })
108
+ }
109
+ }
110
+ const defaultModel = {
111
+ sortf: 0, // sort field
112
+ did: 1, // deck id
113
+ latexPre: `\\documentclass[12pt]{article}
114
+ \\special{papersize=3in,5in}
115
+ \\usepackage[utf8]{inputenc}
116
+ \\usepackage{amssymb,amsmath}
117
+ \\pagestyle{empty}
118
+ \\setlength{\\parindent}{0in}
119
+ \\begin{document}`,
120
+ latexPost: "\\end{document}",
121
+ mod: 0, // modification time
122
+ usn: 0, // unsure, something to do with sync?
123
+ vers: [], // seems to be unused
124
+ type: MODEL_STD,
125
+ css: `.card {
126
+ font-family: arial;
127
+ font-size: 20px;
128
+ text-align: center;
129
+ color: black;
130
+ background-color: white;
131
+ }`,
132
+ /* also:
133
+ name: string,
134
+ flds: [Field],
135
+ tmpls: [Template],
136
+ tags: [??],
137
+ id: string
138
+ */
139
+ tags: [],
140
+ }
141
+
142
+ const defaultField = {
143
+ name: "",
144
+ ord: null,
145
+ sticky: false,
146
+ rtl: false,
147
+ font: "Arial",
148
+ size: 20,
149
+ media: [],
150
+ }
151
+
152
+ const defaultTemplate = {
153
+ name: "",
154
+ ord: null,
155
+ qfmt: "",
156
+ afmt: "",
157
+ did: null,
158
+ bqfmt: "",
159
+ bafmt: "",
160
+ }
161
+
162
+ // whether new cards should be mixed with reviews, or shown first or last
163
+ const NEW_CARDS_DISTRIBUTE = 0
164
+ const NEW_CARDS_LAST = 1
165
+ const NEW_CARDS_FIRST = 2
166
+
167
+ const defaultConf = {
168
+ // review options
169
+ 'activeDecks': [1],
170
+ 'curDeck': 1,
171
+ 'newSpread': NEW_CARDS_DISTRIBUTE,
172
+ 'collapseTime': 1200,
173
+ 'timeLim': 0,
174
+ 'estTimes': true,
175
+ 'dueCounts': true,
176
+ // other config
177
+ 'curModel': null,
178
+ 'nextPos': 1,
179
+ 'sortType': "noteFld",
180
+ 'sortBackwards': false,
181
+ 'addToCur': true, // add new to currently selected deck?
182
+ 'dayLearnFirst': false,
183
+ }
184
+
185
+
186
+ // new card insertion order
187
+ const NEW_CARDS_RANDOM = 0
188
+ const NEW_CARDS_DUE = 1
189
+
190
+ const STARTING_FACTOR = 2500
191
+
192
+ const defaultDeckConf = {
193
+ 'name': "Default",
194
+ 'new': {
195
+ 'delays': [1, 10],
196
+ 'ints': [1, 4, 7], // 7 is not currently used
197
+ 'initialFactor': STARTING_FACTOR,
198
+ 'separate': true,
199
+ 'order': NEW_CARDS_DUE,
200
+ 'perDay': 20,
201
+ // may not be set on old decks
202
+ 'bury': false,
203
+ },
204
+ 'lapse': {
205
+ 'delays': [10],
206
+ 'mult': 0,
207
+ 'minInt': 1,
208
+ 'leechFails': 8,
209
+ // type 0=suspend, 1=tagonly
210
+ 'leechAction': 0,
211
+ },
212
+ 'rev': {
213
+ 'perDay': 200,
214
+ 'ease4': 1.3,
215
+ 'fuzz': 0.05,
216
+ 'minSpace': 1, // not currently used
217
+ 'ivlFct': 1,
218
+ 'maxIvl': 36500,
219
+ // may not be set on old decks
220
+ 'bury': false,
221
+ 'hardFactor': 1.2,
222
+ },
223
+ 'maxTaken': 60,
224
+ 'timer': 0,
225
+ 'autoplay': true,
226
+ 'replayq': true,
227
+ 'mod': 0,
228
+ 'usn': 0,
229
+ }
230
+
231
+ const defaultDeck = {
232
+ newToday: [0, 0], // currentDay, count
233
+ revToday: [0, 0],
234
+ lrnToday: [0, 0],
235
+ timeToday: [0, 0], // time in ms
236
+ conf: 1,
237
+ usn: 0,
238
+ desc: "",
239
+ dyn: 0, // anki uses int/bool interchangably here
240
+ collapsed: false,
241
+ // added in beta11
242
+ extendNew: 10,
243
+ extendRev: 50,
244
+ }
245
+ class Deck {
246
+ constructor(id, name, desc="") {
247
+ this.id = id
248
+ this.name = name
249
+ this.desc = desc
250
+ this.notes = []
251
+ }
252
+
253
+ addNote(note) {
254
+ this.notes.push(note)
255
+ }
256
+ }
257
+ class Note {
258
+ constructor(model, fields, tags = null, guid = null) {
259
+ this.model = model
260
+ this.fields = fields
261
+ this.tags = tags
262
+ this._guid = guid
263
+ }
264
+
265
+ get guid() {
266
+ return this._guid ? this._guid : ankiHash(this.fields);
267
+ }
268
+
269
+ get cards() {
270
+ if (this.model.props.type === MODEL_STD) {
271
+ const isEmpty = f => {
272
+ return !f || f.toString().trim().length === 0
273
+ }
274
+ const rv = []
275
+ for (const [card_ord, any_or_all, required_field_ords] of this.model.props.req) {
276
+ const op = any_or_all === "any" ? "some" : "every"
277
+ if (required_field_ords[op](f => !isEmpty(this.fields[f]))) {
278
+ rv.push(card_ord)
279
+ }
280
+ }
281
+ return rv
282
+ } else {
283
+ // the below logic is copied from anki's ModelManager._availClozeOrds
284
+ const ords = new Set()
285
+ const matches = []
286
+ const curliesRe = /{{[^}]*?cloze:(?:[^}]?:)*(.+?)}}/g
287
+ const percentRe = /<%cloze:(.+?)%>/g
288
+ const { qfmt } = this.model.props.tmpls[0] // cloze models only have 1 template
289
+ let m;
290
+ while (m = curliesRe.exec(qfmt))
291
+ matches.push(m[1])
292
+ while (m = percentRe.exec(qfmt))
293
+ matches.push(m[1])
294
+ const map = {}
295
+ this.model.props.flds.forEach((fld, i) => {
296
+ map[fld.name] = [i, fld]
297
+ })
298
+ for (const fname of matches) {
299
+ if (!(fname in map)) continue
300
+ const ord = map[fname][0]
301
+ // matches {{c1::text}} and {{c1,2::text}} (one deletion shown on several cards)
302
+ const re = /{{c(\d+(?:,\d+)*)::[\s\S]+?}}/g
303
+ while (m = re.exec(this.fields[ord])) {
304
+ for (const n of m[1].split(',')) {
305
+ const i = parseInt(n)
306
+ if (i > 0)
307
+ ords.add(i - 1)
308
+ }
309
+ }
310
+ }
311
+ if (ords.size === 0) {
312
+ // empty clozes use first ord
313
+ return [0]
314
+ }
315
+ return Array.from(ords)
316
+ }
317
+ }
318
+ }
319
+ // Dependencies used to create the package.
320
+ // When genanki.js is loaded with a <script> tag, the global `SQL` (or `initSqlJs`),
321
+ // `JSZip` and `saveAs` (FileSaver.js, optional) are used.
322
+ // The npm builds (ES module / CommonJS) set these from the installed packages.
323
+ const genankiDeps = {
324
+ SQL: null, // initialized sql.js, see setSqlJs()
325
+ sqlPromise: null, // Promise<SQL> while sql.js is being loaded
326
+ loadSqlJs: null, // () => Promise<SQL>
327
+ JSZip: null,
328
+ saveAs: null,
329
+ }
330
+
331
+ /**
332
+ * Use an initialized sql.js instance for all packages.
333
+ * `SQL` is the value `initSqlJs()` resolves to.
334
+ */
335
+ function setSqlJs(SQL) {
336
+ genankiDeps.SQL = SQL
337
+ }
338
+
339
+ function resolveSqlJs(packageSQL) {
340
+ if (packageSQL) return Promise.resolve(packageSQL)
341
+ if (genankiDeps.SQL) return Promise.resolve(genankiDeps.SQL)
342
+ if (typeof SQL !== "undefined" && SQL) return Promise.resolve(SQL)
343
+
344
+ if (!genankiDeps.sqlPromise) {
345
+ let load = genankiDeps.loadSqlJs
346
+ if (!load && typeof initSqlJs === "function") {
347
+ load = () => initSqlJs()
348
+ }
349
+ if (!load) {
350
+ return Promise.reject(new Error("sql.js is not loaded. Add sql.js to the page and assign the result " +
351
+ "of initSqlJs() to a global `SQL` variable, or pass it with setSqlJs(SQL)."))
352
+ }
353
+ genankiDeps.sqlPromise = load().then(sql => {
354
+ genankiDeps.SQL = sql
355
+ return sql
356
+ }, err => {
357
+ genankiDeps.sqlPromise = null
358
+ throw err
359
+ })
360
+ }
361
+ return genankiDeps.sqlPromise
362
+ }
363
+
364
+ function resolveJSZip() {
365
+ if (genankiDeps.JSZip) return genankiDeps.JSZip
366
+ if (typeof JSZip !== "undefined" && JSZip) return JSZip
367
+ throw new Error("JSZip is not loaded. Add jszip.min.js to the page.")
368
+ }
369
+
370
+ function saveFile(content, filename) {
371
+ const saver = genankiDeps.saveAs || (typeof saveAs === "function" ? saveAs : null)
372
+ if (saver) {
373
+ saver(content, filename)
374
+ return
375
+ }
376
+
377
+ // FileSaver.js is optional, download with a temporary link instead
378
+ if (typeof document !== "undefined" && typeof URL !== "undefined" && URL.createObjectURL) {
379
+ const url = URL.createObjectURL(content)
380
+ const a = document.createElement("a")
381
+ a.href = url
382
+ a.download = filename
383
+ a.style.display = "none"
384
+ document.body.appendChild(a)
385
+ a.click()
386
+ document.body.removeChild(a)
387
+ setTimeout(() => URL.revokeObjectURL(url), 40000)
388
+ return
389
+ }
390
+
391
+ throw new Error("writeToFile() can only download files in a browser. " +
392
+ "Use package.generate('nodebuffer') or package.generate('uint8array') and write the result to a file.")
393
+ }
394
+
395
+ class Package {
396
+ constructor() {
397
+ this.decks = []
398
+ this.media = []
399
+ this.SQL = null
400
+ }
401
+
402
+ /**
403
+ * Use an initialized sql.js instance for this package.
404
+ */
405
+ setSqlJs(SQL) {
406
+ this.SQL = SQL
407
+ return this
408
+ }
409
+
410
+ addDeck(deck) {
411
+ // adding the same deck twice would duplicate all of its notes
412
+ if (!this.decks.includes(deck)) {
413
+ this.decks.push(deck)
414
+ }
415
+ }
416
+
417
+ addMedia(data, name) {
418
+ this.media.push({ name, data })
419
+ }
420
+
421
+ addMediaFile(filename, name = null) {
422
+ this.media.push({ name: name || filename, filename })
423
+ }
424
+
425
+ /**
426
+ * Create the .apkg file content.
427
+ * `type` is any JSZip output type: "blob" (default), "uint8array", "arraybuffer", "base64",
428
+ * or "nodebuffer" (Node.js only).
429
+ * Returns a Promise.
430
+ */
431
+ generate(type = "blob") {
432
+ return resolveSqlJs(this.SQL).then(SQL => {
433
+ const ZipClass = resolveJSZip()
434
+
435
+ const db = new SQL.Database()
436
+ let data
437
+ try {
438
+ db.run(APKG_SCHEMA)
439
+ this.write(db)
440
+ data = db.export()
441
+ } finally {
442
+ db.close()
443
+ }
444
+
445
+ const zip = new ZipClass()
446
+ zip.file("collection.anki2", data)
447
+
448
+ const media_info = {}
449
+
450
+ this.media.forEach((m, i) => {
451
+ if (m.filename != null) {
452
+ zip.file(i.toString(), m.filename)
453
+ } else {
454
+ zip.file(i.toString(), m.data)
455
+ }
456
+
457
+ media_info[i] = m.name
458
+ })
459
+
460
+ zip.file('media', JSON.stringify(media_info))
461
+
462
+ return zip.generateAsync({ type, mimeType: "application/apkg" })
463
+ })
464
+ }
465
+
466
+ /**
467
+ * Download the package as `filename` (browser only).
468
+ * Returns a Promise that resolves with the generated Blob.
469
+ */
470
+ writeToFile(filename) {
471
+ return this.generate("blob").then(content => {
472
+ saveFile(content, filename)
473
+ return content
474
+ })
475
+ }
476
+
477
+
478
+ write(db) {
479
+ const now = new Date
480
+ const models = {}
481
+ const decks = {}
482
+
483
+ // AnkiDroid failed to import subdeck, So add a Default deck
484
+ decks["1"] = {...defaultDeck, id: 1, name: "Default"}
485
+
486
+ this.decks.forEach(d => {
487
+ d.notes.forEach(n => models[n.model.props.id] = n.model.props)
488
+ decks[d.id] = {
489
+ ...defaultDeck,
490
+ id: d.id,
491
+ name: d.name,
492
+ desc: d.desc,
493
+ }
494
+ })
495
+
496
+ const col = [
497
+ null, // id
498
+ (+now / 1000) | 0, // crt
499
+ +now, // mod
500
+ +now, // scm
501
+ 11, // ver
502
+ 0, // dty
503
+ 0, // usn
504
+ 0, // ls
505
+ JSON.stringify(defaultConf), // conf
506
+ JSON.stringify(models), // models
507
+ JSON.stringify(decks), // decks
508
+ JSON.stringify({ 1: { id: 1, ...defaultDeckConf } }), // dconf
509
+ JSON.stringify({}), // tags
510
+ ]
511
+
512
+ db.prepare(`INSERT INTO col
513
+ (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
514
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(col)
515
+
516
+ const insert_notes = db.prepare(`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
517
+ VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, '')`)
518
+
519
+ const insert_cards = db.prepare(`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
520
+ VALUES (null, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, 0, 0, 0, 0, '')`)
521
+
522
+ for (const deck of this.decks) {
523
+ for (const note of deck.notes) {
524
+ var tags = note.tags == null ? '' : note.tags.join(' ')
525
+ insert_notes.run(
526
+ [
527
+ note.guid, // guid
528
+ note.model.props.id, // mid
529
+ (+now / 1000) | 0, // mod
530
+ -1, // usn
531
+ tags, // tags
532
+ note.fields.join('\x1f'), // flds
533
+ 0, // sfld
534
+ ])
535
+
536
+ var rowID = db.exec("select last_insert_rowid();")
537
+ var note_id = rowID[0]['values'][0][0];
538
+
539
+ for (const card_ord of note.cards) {
540
+ insert_cards.run(
541
+ [
542
+ note_id, // nid
543
+ deck.id, // did
544
+ card_ord, // ord
545
+ (+now / 1000) | 0, // mod
546
+ -1, // usn
547
+ 0, // type 0=new, 1=learning, 2=due
548
+ 0, // queue -1 for suspended
549
+ ])
550
+ }
551
+ }
552
+ }
553
+
554
+ insert_notes.free()
555
+ insert_cards.free()
556
+ }
557
+ }
558
+ var sha256 = (function () {
559
+ var module = { exports: {} }, define, process;
560
+ /**
561
+ * [js-sha256]{@link https://github.com/emn178/js-sha256}
562
+ *
563
+ * @version 0.9.0
564
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
565
+ * @copyright Chen, Yi-Cyuan 2014-2017
566
+ * @license MIT
567
+ */
568
+ /*jslint bitwise: true */
569
+ (function () {
570
+ 'use strict';
571
+
572
+ var ERROR = 'input is invalid type';
573
+ var WINDOW = typeof window === 'object';
574
+ var root = WINDOW ? window : {};
575
+ if (root.JS_SHA256_NO_WINDOW) {
576
+ WINDOW = false;
577
+ }
578
+ var WEB_WORKER = !WINDOW && typeof self === 'object';
579
+ var NODE_JS = !root.JS_SHA256_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
580
+ if (NODE_JS) {
581
+ root = global;
582
+ } else if (WEB_WORKER) {
583
+ root = self;
584
+ }
585
+ var COMMON_JS = !root.JS_SHA256_NO_COMMON_JS && typeof module === 'object' && module.exports;
586
+ var AMD = typeof define === 'function' && define.amd;
587
+ var ARRAY_BUFFER = !root.JS_SHA256_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
588
+ var HEX_CHARS = '0123456789abcdef'.split('');
589
+ var EXTRA = [-2147483648, 8388608, 32768, 128];
590
+ var SHIFT = [24, 16, 8, 0];
591
+ var K = [
592
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
593
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
594
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
595
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
596
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
597
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
598
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
599
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
600
+ ];
601
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
602
+
603
+ var blocks = [];
604
+
605
+ if (root.JS_SHA256_NO_NODE_JS || !Array.isArray) {
606
+ Array.isArray = function (obj) {
607
+ return Object.prototype.toString.call(obj) === '[object Array]';
608
+ };
609
+ }
610
+
611
+ if (ARRAY_BUFFER && (root.JS_SHA256_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
612
+ ArrayBuffer.isView = function (obj) {
613
+ return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
614
+ };
615
+ }
616
+
617
+ var createOutputMethod = function (outputType, is224) {
618
+ return function (message) {
619
+ return new Sha256(is224, true).update(message)[outputType]();
620
+ };
621
+ };
622
+
623
+ var createMethod = function (is224) {
624
+ var method = createOutputMethod('hex', is224);
625
+ if (NODE_JS) {
626
+ method = nodeWrap(method, is224);
627
+ }
628
+ method.create = function () {
629
+ return new Sha256(is224);
630
+ };
631
+ method.update = function (message) {
632
+ return method.create().update(message);
633
+ };
634
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
635
+ var type = OUTPUT_TYPES[i];
636
+ method[type] = createOutputMethod(type, is224);
637
+ }
638
+ return method;
639
+ };
640
+
641
+ var nodeWrap = function (method, is224) {
642
+ var crypto = eval("require('crypto')");
643
+ var Buffer = eval("require('buffer').Buffer");
644
+ var algorithm = is224 ? 'sha224' : 'sha256';
645
+ var nodeMethod = function (message) {
646
+ if (typeof message === 'string') {
647
+ return crypto.createHash(algorithm).update(message, 'utf8').digest('hex');
648
+ } else {
649
+ if (message === null || message === undefined) {
650
+ throw new Error(ERROR);
651
+ } else if (message.constructor === ArrayBuffer) {
652
+ message = new Uint8Array(message);
653
+ }
654
+ }
655
+ if (Array.isArray(message) || ArrayBuffer.isView(message) ||
656
+ message.constructor === Buffer) {
657
+ return crypto.createHash(algorithm).update(new Buffer(message)).digest('hex');
658
+ } else {
659
+ return method(message);
660
+ }
661
+ };
662
+ return nodeMethod;
663
+ };
664
+
665
+ var createHmacOutputMethod = function (outputType, is224) {
666
+ return function (key, message) {
667
+ return new HmacSha256(key, is224, true).update(message)[outputType]();
668
+ };
669
+ };
670
+
671
+ var createHmacMethod = function (is224) {
672
+ var method = createHmacOutputMethod('hex', is224);
673
+ method.create = function (key) {
674
+ return new HmacSha256(key, is224);
675
+ };
676
+ method.update = function (key, message) {
677
+ return method.create(key).update(message);
678
+ };
679
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
680
+ var type = OUTPUT_TYPES[i];
681
+ method[type] = createHmacOutputMethod(type, is224);
682
+ }
683
+ return method;
684
+ };
685
+
686
+ function Sha256(is224, sharedMemory) {
687
+ if (sharedMemory) {
688
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
689
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
690
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
691
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
692
+ this.blocks = blocks;
693
+ } else {
694
+ this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
695
+ }
696
+
697
+ if (is224) {
698
+ this.h0 = 0xc1059ed8;
699
+ this.h1 = 0x367cd507;
700
+ this.h2 = 0x3070dd17;
701
+ this.h3 = 0xf70e5939;
702
+ this.h4 = 0xffc00b31;
703
+ this.h5 = 0x68581511;
704
+ this.h6 = 0x64f98fa7;
705
+ this.h7 = 0xbefa4fa4;
706
+ } else { // 256
707
+ this.h0 = 0x6a09e667;
708
+ this.h1 = 0xbb67ae85;
709
+ this.h2 = 0x3c6ef372;
710
+ this.h3 = 0xa54ff53a;
711
+ this.h4 = 0x510e527f;
712
+ this.h5 = 0x9b05688c;
713
+ this.h6 = 0x1f83d9ab;
714
+ this.h7 = 0x5be0cd19;
715
+ }
716
+
717
+ this.block = this.start = this.bytes = this.hBytes = 0;
718
+ this.finalized = this.hashed = false;
719
+ this.first = true;
720
+ this.is224 = is224;
721
+ }
722
+
723
+ Sha256.prototype.update = function (message) {
724
+ if (this.finalized) {
725
+ return;
726
+ }
727
+ var notString, type = typeof message;
728
+ if (type !== 'string') {
729
+ if (type === 'object') {
730
+ if (message === null) {
731
+ throw new Error(ERROR);
732
+ } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
733
+ message = new Uint8Array(message);
734
+ } else if (!Array.isArray(message)) {
735
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
736
+ throw new Error(ERROR);
737
+ }
738
+ }
739
+ } else {
740
+ throw new Error(ERROR);
741
+ }
742
+ notString = true;
743
+ }
744
+ var code, index = 0, i, length = message.length, blocks = this.blocks;
745
+
746
+ while (index < length) {
747
+ if (this.hashed) {
748
+ this.hashed = false;
749
+ blocks[0] = this.block;
750
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
751
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
752
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
753
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
754
+ }
755
+
756
+ if (notString) {
757
+ for (i = this.start; index < length && i < 64; ++index) {
758
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
759
+ }
760
+ } else {
761
+ for (i = this.start; index < length && i < 64; ++index) {
762
+ code = message.charCodeAt(index);
763
+ if (code < 0x80) {
764
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
765
+ } else if (code < 0x800) {
766
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
767
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
768
+ } else if (code < 0xd800 || code >= 0xe000) {
769
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
770
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
771
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
772
+ } else {
773
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
774
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
775
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
776
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
777
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
778
+ }
779
+ }
780
+ }
781
+
782
+ this.lastByteIndex = i;
783
+ this.bytes += i - this.start;
784
+ if (i >= 64) {
785
+ this.block = blocks[16];
786
+ this.start = i - 64;
787
+ this.hash();
788
+ this.hashed = true;
789
+ } else {
790
+ this.start = i;
791
+ }
792
+ }
793
+ if (this.bytes > 4294967295) {
794
+ this.hBytes += this.bytes / 4294967296 << 0;
795
+ this.bytes = this.bytes % 4294967296;
796
+ }
797
+ return this;
798
+ };
799
+
800
+ Sha256.prototype.finalize = function () {
801
+ if (this.finalized) {
802
+ return;
803
+ }
804
+ this.finalized = true;
805
+ var blocks = this.blocks, i = this.lastByteIndex;
806
+ blocks[16] = this.block;
807
+ blocks[i >> 2] |= EXTRA[i & 3];
808
+ this.block = blocks[16];
809
+ if (i >= 56) {
810
+ if (!this.hashed) {
811
+ this.hash();
812
+ }
813
+ blocks[0] = this.block;
814
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
815
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
816
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
817
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
818
+ }
819
+ blocks[14] = this.hBytes << 3 | this.bytes >>> 29;
820
+ blocks[15] = this.bytes << 3;
821
+ this.hash();
822
+ };
823
+
824
+ Sha256.prototype.hash = function () {
825
+ var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4, f = this.h5, g = this.h6,
826
+ h = this.h7, blocks = this.blocks, j, s0, s1, maj, t1, t2, ch, ab, da, cd, bc;
827
+
828
+ for (j = 16; j < 64; ++j) {
829
+ // rightrotate
830
+ t1 = blocks[j - 15];
831
+ s0 = ((t1 >>> 7) | (t1 << 25)) ^ ((t1 >>> 18) | (t1 << 14)) ^ (t1 >>> 3);
832
+ t1 = blocks[j - 2];
833
+ s1 = ((t1 >>> 17) | (t1 << 15)) ^ ((t1 >>> 19) | (t1 << 13)) ^ (t1 >>> 10);
834
+ blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1 << 0;
835
+ }
836
+
837
+ bc = b & c;
838
+ for (j = 0; j < 64; j += 4) {
839
+ if (this.first) {
840
+ if (this.is224) {
841
+ ab = 300032;
842
+ t1 = blocks[0] - 1413257819;
843
+ h = t1 - 150054599 << 0;
844
+ d = t1 + 24177077 << 0;
845
+ } else {
846
+ ab = 704751109;
847
+ t1 = blocks[0] - 210244248;
848
+ h = t1 - 1521486534 << 0;
849
+ d = t1 + 143694565 << 0;
850
+ }
851
+ this.first = false;
852
+ } else {
853
+ s0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
854
+ s1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
855
+ ab = a & b;
856
+ maj = ab ^ (a & c) ^ bc;
857
+ ch = (e & f) ^ (~e & g);
858
+ t1 = h + s1 + ch + K[j] + blocks[j];
859
+ t2 = s0 + maj;
860
+ h = d + t1 << 0;
861
+ d = t1 + t2 << 0;
862
+ }
863
+ s0 = ((d >>> 2) | (d << 30)) ^ ((d >>> 13) | (d << 19)) ^ ((d >>> 22) | (d << 10));
864
+ s1 = ((h >>> 6) | (h << 26)) ^ ((h >>> 11) | (h << 21)) ^ ((h >>> 25) | (h << 7));
865
+ da = d & a;
866
+ maj = da ^ (d & b) ^ ab;
867
+ ch = (h & e) ^ (~h & f);
868
+ t1 = g + s1 + ch + K[j + 1] + blocks[j + 1];
869
+ t2 = s0 + maj;
870
+ g = c + t1 << 0;
871
+ c = t1 + t2 << 0;
872
+ s0 = ((c >>> 2) | (c << 30)) ^ ((c >>> 13) | (c << 19)) ^ ((c >>> 22) | (c << 10));
873
+ s1 = ((g >>> 6) | (g << 26)) ^ ((g >>> 11) | (g << 21)) ^ ((g >>> 25) | (g << 7));
874
+ cd = c & d;
875
+ maj = cd ^ (c & a) ^ da;
876
+ ch = (g & h) ^ (~g & e);
877
+ t1 = f + s1 + ch + K[j + 2] + blocks[j + 2];
878
+ t2 = s0 + maj;
879
+ f = b + t1 << 0;
880
+ b = t1 + t2 << 0;
881
+ s0 = ((b >>> 2) | (b << 30)) ^ ((b >>> 13) | (b << 19)) ^ ((b >>> 22) | (b << 10));
882
+ s1 = ((f >>> 6) | (f << 26)) ^ ((f >>> 11) | (f << 21)) ^ ((f >>> 25) | (f << 7));
883
+ bc = b & c;
884
+ maj = bc ^ (b & d) ^ cd;
885
+ ch = (f & g) ^ (~f & h);
886
+ t1 = e + s1 + ch + K[j + 3] + blocks[j + 3];
887
+ t2 = s0 + maj;
888
+ e = a + t1 << 0;
889
+ a = t1 + t2 << 0;
890
+ }
891
+
892
+ this.h0 = this.h0 + a << 0;
893
+ this.h1 = this.h1 + b << 0;
894
+ this.h2 = this.h2 + c << 0;
895
+ this.h3 = this.h3 + d << 0;
896
+ this.h4 = this.h4 + e << 0;
897
+ this.h5 = this.h5 + f << 0;
898
+ this.h6 = this.h6 + g << 0;
899
+ this.h7 = this.h7 + h << 0;
900
+ };
901
+
902
+ Sha256.prototype.hex = function () {
903
+ this.finalize();
904
+
905
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
906
+ h6 = this.h6, h7 = this.h7;
907
+
908
+ var hex = HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
909
+ HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
910
+ HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
911
+ HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
912
+ HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
913
+ HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
914
+ HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
915
+ HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
916
+ HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
917
+ HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
918
+ HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
919
+ HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
920
+ HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
921
+ HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
922
+ HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
923
+ HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
924
+ HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
925
+ HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
926
+ HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
927
+ HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F] +
928
+ HEX_CHARS[(h5 >> 28) & 0x0F] + HEX_CHARS[(h5 >> 24) & 0x0F] +
929
+ HEX_CHARS[(h5 >> 20) & 0x0F] + HEX_CHARS[(h5 >> 16) & 0x0F] +
930
+ HEX_CHARS[(h5 >> 12) & 0x0F] + HEX_CHARS[(h5 >> 8) & 0x0F] +
931
+ HEX_CHARS[(h5 >> 4) & 0x0F] + HEX_CHARS[h5 & 0x0F] +
932
+ HEX_CHARS[(h6 >> 28) & 0x0F] + HEX_CHARS[(h6 >> 24) & 0x0F] +
933
+ HEX_CHARS[(h6 >> 20) & 0x0F] + HEX_CHARS[(h6 >> 16) & 0x0F] +
934
+ HEX_CHARS[(h6 >> 12) & 0x0F] + HEX_CHARS[(h6 >> 8) & 0x0F] +
935
+ HEX_CHARS[(h6 >> 4) & 0x0F] + HEX_CHARS[h6 & 0x0F];
936
+ if (!this.is224) {
937
+ hex += HEX_CHARS[(h7 >> 28) & 0x0F] + HEX_CHARS[(h7 >> 24) & 0x0F] +
938
+ HEX_CHARS[(h7 >> 20) & 0x0F] + HEX_CHARS[(h7 >> 16) & 0x0F] +
939
+ HEX_CHARS[(h7 >> 12) & 0x0F] + HEX_CHARS[(h7 >> 8) & 0x0F] +
940
+ HEX_CHARS[(h7 >> 4) & 0x0F] + HEX_CHARS[h7 & 0x0F];
941
+ }
942
+ return hex;
943
+ };
944
+
945
+ Sha256.prototype.toString = Sha256.prototype.hex;
946
+
947
+ Sha256.prototype.digest = function () {
948
+ this.finalize();
949
+
950
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4, h5 = this.h5,
951
+ h6 = this.h6, h7 = this.h7;
952
+
953
+ var arr = [
954
+ (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
955
+ (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
956
+ (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
957
+ (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
958
+ (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF,
959
+ (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, h5 & 0xFF,
960
+ (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, h6 & 0xFF
961
+ ];
962
+ if (!this.is224) {
963
+ arr.push((h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, h7 & 0xFF);
964
+ }
965
+ return arr;
966
+ };
967
+
968
+ Sha256.prototype.array = Sha256.prototype.digest;
969
+
970
+ Sha256.prototype.arrayBuffer = function () {
971
+ this.finalize();
972
+
973
+ var buffer = new ArrayBuffer(this.is224 ? 28 : 32);
974
+ var dataView = new DataView(buffer);
975
+ dataView.setUint32(0, this.h0);
976
+ dataView.setUint32(4, this.h1);
977
+ dataView.setUint32(8, this.h2);
978
+ dataView.setUint32(12, this.h3);
979
+ dataView.setUint32(16, this.h4);
980
+ dataView.setUint32(20, this.h5);
981
+ dataView.setUint32(24, this.h6);
982
+ if (!this.is224) {
983
+ dataView.setUint32(28, this.h7);
984
+ }
985
+ return buffer;
986
+ };
987
+
988
+ function HmacSha256(key, is224, sharedMemory) {
989
+ var i, type = typeof key;
990
+ if (type === 'string') {
991
+ var bytes = [], length = key.length, index = 0, code;
992
+ for (i = 0; i < length; ++i) {
993
+ code = key.charCodeAt(i);
994
+ if (code < 0x80) {
995
+ bytes[index++] = code;
996
+ } else if (code < 0x800) {
997
+ bytes[index++] = (0xc0 | (code >> 6));
998
+ bytes[index++] = (0x80 | (code & 0x3f));
999
+ } else if (code < 0xd800 || code >= 0xe000) {
1000
+ bytes[index++] = (0xe0 | (code >> 12));
1001
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
1002
+ bytes[index++] = (0x80 | (code & 0x3f));
1003
+ } else {
1004
+ code = 0x10000 + (((code & 0x3ff) << 10) | (key.charCodeAt(++i) & 0x3ff));
1005
+ bytes[index++] = (0xf0 | (code >> 18));
1006
+ bytes[index++] = (0x80 | ((code >> 12) & 0x3f));
1007
+ bytes[index++] = (0x80 | ((code >> 6) & 0x3f));
1008
+ bytes[index++] = (0x80 | (code & 0x3f));
1009
+ }
1010
+ }
1011
+ key = bytes;
1012
+ } else {
1013
+ if (type === 'object') {
1014
+ if (key === null) {
1015
+ throw new Error(ERROR);
1016
+ } else if (ARRAY_BUFFER && key.constructor === ArrayBuffer) {
1017
+ key = new Uint8Array(key);
1018
+ } else if (!Array.isArray(key)) {
1019
+ if (!ARRAY_BUFFER || !ArrayBuffer.isView(key)) {
1020
+ throw new Error(ERROR);
1021
+ }
1022
+ }
1023
+ } else {
1024
+ throw new Error(ERROR);
1025
+ }
1026
+ }
1027
+
1028
+ if (key.length > 64) {
1029
+ key = (new Sha256(is224, true)).update(key).array();
1030
+ }
1031
+
1032
+ var oKeyPad = [], iKeyPad = [];
1033
+ for (i = 0; i < 64; ++i) {
1034
+ var b = key[i] || 0;
1035
+ oKeyPad[i] = 0x5c ^ b;
1036
+ iKeyPad[i] = 0x36 ^ b;
1037
+ }
1038
+
1039
+ Sha256.call(this, is224, sharedMemory);
1040
+
1041
+ this.update(iKeyPad);
1042
+ this.oKeyPad = oKeyPad;
1043
+ this.inner = true;
1044
+ this.sharedMemory = sharedMemory;
1045
+ }
1046
+ HmacSha256.prototype = new Sha256();
1047
+
1048
+ HmacSha256.prototype.finalize = function () {
1049
+ Sha256.prototype.finalize.call(this);
1050
+ if (this.inner) {
1051
+ this.inner = false;
1052
+ var innerHash = this.array();
1053
+ Sha256.call(this, this.is224, this.sharedMemory);
1054
+ this.update(this.oKeyPad);
1055
+ this.update(innerHash);
1056
+ Sha256.prototype.finalize.call(this);
1057
+ }
1058
+ };
1059
+
1060
+ var exports = createMethod();
1061
+ exports.sha256 = exports;
1062
+ exports.sha224 = createMethod(true);
1063
+ exports.sha256.hmac = createHmacMethod();
1064
+ exports.sha224.hmac = createHmacMethod(true);
1065
+
1066
+ if (COMMON_JS) {
1067
+ module.exports = exports;
1068
+ } else {
1069
+ root.sha256 = exports.sha256;
1070
+ root.sha224 = exports.sha224;
1071
+ if (AMD) {
1072
+ define(function () {
1073
+ return exports;
1074
+ });
1075
+ }
1076
+ }
1077
+ })();
1078
+ return module.exports;
1079
+ })();
1080
+ /*
1081
+ * [genanki]{@link https://github.com/kerrickstaley/genanki}
1082
+ * @copyright Copyright (c) Kerrick Staley 2021
1083
+ * @license The MIT License
1084
+ */
1085
+
1086
+ const APKG_SCHEMA = `
1087
+ PRAGMA foreign_keys=OFF;
1088
+ BEGIN TRANSACTION;
1089
+
1090
+ CREATE TABLE col (
1091
+ id integer primary key,
1092
+ crt integer not null,
1093
+ mod integer not null,
1094
+ scm integer not null,
1095
+ ver integer not null,
1096
+ dty integer not null,
1097
+ usn integer not null,
1098
+ ls integer not null,
1099
+ conf text not null,
1100
+ models text not null,
1101
+ decks text not null,
1102
+ dconf text not null,
1103
+ tags text not null
1104
+ );
1105
+ CREATE TABLE notes (
1106
+ id integer primary key, /* 0 */
1107
+ guid text not null, /* 1 */
1108
+ mid integer not null, /* 2 */
1109
+ mod integer not null, /* 3 */
1110
+ usn integer not null, /* 4 */
1111
+ tags text not null, /* 5 */
1112
+ flds text not null, /* 6 */
1113
+ sfld integer not null, /* 7 */
1114
+ csum integer not null, /* 8 */
1115
+ flags integer not null, /* 9 */
1116
+ data text not null /* 10 */
1117
+ );
1118
+ CREATE TABLE cards (
1119
+ id integer primary key, /* 0 */
1120
+ nid integer not null, /* 1 */
1121
+ did integer not null, /* 2 */
1122
+ ord integer not null, /* 3 */
1123
+ mod integer not null, /* 4 */
1124
+ usn integer not null, /* 5 */
1125
+ type integer not null, /* 6 */
1126
+ queue integer not null, /* 7 */
1127
+ due integer not null, /* 8 */
1128
+ ivl integer not null, /* 9 */
1129
+ factor integer not null, /* 10 */
1130
+ reps integer not null, /* 11 */
1131
+ lapses integer not null, /* 12 */
1132
+ left integer not null, /* 13 */
1133
+ odue integer not null, /* 14 */
1134
+ odid integer not null, /* 15 */
1135
+ flags integer not null, /* 16 */
1136
+ data text not null /* 17 */
1137
+ );
1138
+ CREATE TABLE revlog (
1139
+ id integer primary key,
1140
+ cid integer not null,
1141
+ usn integer not null,
1142
+ ease integer not null,
1143
+ ivl integer not null,
1144
+ lastIvl integer not null,
1145
+ factor integer not null,
1146
+ time integer not null,
1147
+ type integer not null
1148
+ );
1149
+ CREATE TABLE graves (
1150
+ usn integer not null,
1151
+ oid integer not null,
1152
+ type integer not null
1153
+ );
1154
+ CREATE INDEX ix_notes_usn on notes (usn);
1155
+ CREATE INDEX ix_cards_usn on cards (usn);
1156
+ CREATE INDEX ix_revlog_usn on revlog (usn);
1157
+ CREATE INDEX ix_cards_nid on cards (nid);
1158
+ CREATE INDEX ix_cards_sched on cards (did, queue, due);
1159
+ CREATE INDEX ix_revlog_cid on revlog (cid);
1160
+ CREATE INDEX ix_notes_csum on notes (csum);
1161
+ COMMIT;
1162
+ `;
1163
+ var APKG_COL = `
1164
+ INSERT INTO col VALUES(
1165
+ null,
1166
+ 1411124400,
1167
+ 1425279151694,
1168
+ 1425279151690,
1169
+ 11,
1170
+ 0,
1171
+ 0,
1172
+ 0,
1173
+ '{
1174
+ "activeDecks": [
1175
+ 1
1176
+ ],
1177
+ "addToCur": true,
1178
+ "collapseTime": 1200,
1179
+ "curDeck": 1,
1180
+ "curModel": "1425279151691",
1181
+ "dueCounts": true,
1182
+ "estTimes": true,
1183
+ "newBury": true,
1184
+ "newSpread": 0,
1185
+ "nextPos": 1,
1186
+ "sortBackwards": false,
1187
+ "sortType": "noteFld",
1188
+ "timeLim": 0
1189
+ }',
1190
+ '{}',
1191
+ '{
1192
+ "1": {
1193
+ "collapsed": false,
1194
+ "conf": 1,
1195
+ "desc": "",
1196
+ "dyn": 0,
1197
+ "extendNew": 10,
1198
+ "extendRev": 50,
1199
+ "id": 1,
1200
+ "lrnToday": [
1201
+ 0,
1202
+ 0
1203
+ ],
1204
+ "mod": 1425279151,
1205
+ "name": "Default",
1206
+ "newToday": [
1207
+ 0,
1208
+ 0
1209
+ ],
1210
+ "revToday": [
1211
+ 0,
1212
+ 0
1213
+ ],
1214
+ "timeToday": [
1215
+ 0,
1216
+ 0
1217
+ ],
1218
+ "usn": 0
1219
+ }
1220
+ }',
1221
+ '{
1222
+ "1": {
1223
+ "autoplay": true,
1224
+ "id": 1,
1225
+ "lapse": {
1226
+ "delays": [
1227
+ 10
1228
+ ],
1229
+ "leechAction": 0,
1230
+ "leechFails": 8,
1231
+ "minInt": 1,
1232
+ "mult": 0
1233
+ },
1234
+ "maxTaken": 60,
1235
+ "mod": 0,
1236
+ "name": "Default",
1237
+ "new": {
1238
+ "bury": true,
1239
+ "delays": [
1240
+ 1,
1241
+ 10
1242
+ ],
1243
+ "initialFactor": 2500,
1244
+ "ints": [
1245
+ 1,
1246
+ 4,
1247
+ 7
1248
+ ],
1249
+ "order": 1,
1250
+ "perDay": 20,
1251
+ "separate": true
1252
+ },
1253
+ "replayq": true,
1254
+ "rev": {
1255
+ "bury": true,
1256
+ "ease4": 1.3,
1257
+ "fuzz": 0.05,
1258
+ "ivlFct": 1,
1259
+ "maxIvl": 36500,
1260
+ "minSpace": 1,
1261
+ "perDay": 100
1262
+ },
1263
+ "timer": 0,
1264
+ "usn": 0
1265
+ }
1266
+ }',
1267
+ '{}'
1268
+ );
1269
+ `;
1270
+
1271
+ genankiDeps.JSZip = JSZip
1272
+ // sql.js is only loaded when the first package is generated.
1273
+ // The sql-wasm-inline build is used because it embeds the .wasm file, so no
1274
+ // locateFile configuration is needed. Call setSqlJs(SQL) to use another build.
1275
+ genankiDeps.loadSqlJs = () => Promise.resolve().then(() => require("sql.js/dist/sql-wasm-inline.js")).then(m => (m.default || m)())
1276
+
1277
+ module.exports = { Model, ClozeModel, Deck, Note, Package, setSqlJs, ankiHash, sha256, MODEL_STD, MODEL_CLOZE }