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