Skip to content

Commit

Permalink
Merge branch 'master' into gh-2104
Browse files Browse the repository at this point in the history
  • Loading branch information
retorquere committed May 30, 2022
2 parents 2bcb16f + 6e10ad1 commit 93ff13e
Show file tree
Hide file tree
Showing 88 changed files with 4,389 additions and 1,163 deletions.
16 changes: 9 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
const config = require('zotero-plugin/.eslintrc')

config.rules['@typescript-eslint/consistent-type-definitions'] = 'off'
config.rules['@typescript-eslint/member-ordering'] = 'off'
config.rules['max-classes-per-file'] = 'off'
config.rules['no-console'] = 'error'
config.rules['no-new-func'] = 'off'
config.rules['no-underscore-dangle'] = [ 'error', { "allowAfterThis": true } ]
config.rules['prefer-template'] = 'off'

config.rules['@typescript-eslint/no-unsafe-member-access'] = 'off'
config.rules['@typescript-eslint/no-unsafe-call'] = 'off'
config.rules['@typescript-eslint/prefer-regexp-exec'] = 'off'
config.rules['@typescript-eslint/consistent-type-assertions'] = 'off'
config.rules['@typescript-eslint/consistent-type-definitions'] = 'off'
config.rules['@typescript-eslint/explicit-module-boundary-types'] = 'error'
config.rules['@typescript-eslint/member-ordering'] = 'off'
config.rules['@typescript-eslint/no-implied-eval'] = 'off'
config.rules['@typescript-eslint/no-unsafe-assignment'] = 'off'
config.rules['@typescript-eslint/no-unsafe-argument'] = 'off'
config.rules['@typescript-eslint/no-unsafe-assignment'] = 'off'
config.rules['@typescript-eslint/no-unsafe-call'] = 'off'
config.rules['@typescript-eslint/no-unsafe-member-access'] = 'off'
config.rules['@typescript-eslint/prefer-regexp-exec'] = 'off'
config.rules['@typescript-eslint/restrict-template-expressions'] = 'off'
config.rules['@typescript-eslint/explicit-module-boundary-types'] = 'error'

config.rules['@typescript-eslint/ban-ts-comment'] = 'warn'
config.rules['@typescript-eslint/member-delimiter-style'] = [ 'error', {
Expand Down
71 changes: 49 additions & 22 deletions content/ErrorReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Preference } from './prefs'
import { defaults } from '../gen/preferences/meta'
import { Translators } from './translators'
import { log } from './logger'
import Tar from 'tar-js'
import { gzip } from 'pako'

import { DB } from './db/main'
import { DB as Cache } from './db/cache'
Expand All @@ -17,12 +15,24 @@ import * as s3 from './s3.json'
import * as PACKAGE from '../package.json'

const kB = 1024
const COMPRESSION_BEST = 9

// PR_RDONLY 0x01
const PR_WRONLY = 0x02
// const PR_RDWR = 0x04
const PR_CREATE_FILE =0x08
// define PR_APPEND 0x10
// define PR_TRUNCATE 0x20
// define PR_SYNC 0x40
// define PR_EXCL 0x80

export class ErrorReport {
private previewSize = 3 * kB // eslint-disable-line no-magic-numbers, yoda

private key: string
private timestamp: string
private zipfile: string

private bucket: string
private params: any
private globals: Record<string, any>
Expand All @@ -42,17 +52,17 @@ export class ErrorReport {
wizard.canRewind = false

try {
await Zotero.HTTP.request('PUT', `${this.bucket}/${this.key}-${this.timestamp}.tar.gz`, {
await Zotero.HTTP.request('PUT', `${this.bucket}/${OS.Path.basename(this.zipfile)}`, {
noCache: true,
// followRedirects: true,
// noCache: true,
// foreground: true,
headers: {
'x-amz-storage-class': 'STANDARD',
'x-amz-acl': 'bucket-owner-full-control',
'Content-Type': 'application/x-tar',
'Content-Type': 'application/zip',
},
body: this.tar(),
body: new Uint8Array(await OS.File.read(await this.zip(), {})),
})

wizard.advance()
Expand Down Expand Up @@ -108,27 +118,41 @@ export class ErrorReport {
}
}

public tar(): Uint8Array {
const tape = new Tar
public async zip(): Promise<string> {
if (!await OS.File.exists(this.zipfile)) {
const zipWriter = Components.classes['@mozilla.org/zipwriter;1'].createInstance(Components.interfaces.nsIZipWriter)
// 0x02 = Read and Write

tape.append(
`${this.key}/debug.txt`,
[ this.errorlog.info, this.cacheState, this.errorlog.errors, this.errorlog.debug ].filter(chunk => chunk).join('\n\n')
)
zipWriter.open(Zotero.File.pathToFile(this.zipfile), PR_WRONLY + PR_CREATE_FILE)
const converter = Components.classes['@mozilla.org/intl/scriptableunicodeconverter'].createInstance(Components.interfaces.nsIScriptableUnicodeConverter)
converter.charset = 'UTF-8'

if (this.errorlog.items) tape.append(`${this.key}/items.json`, this.errorlog.items)
function add(filename, body) { // eslint-disable-line no-inner-declarations, prefer-arrow/prefer-arrow-functions
const istream = converter.convertToInputStream(body)
zipWriter.addEntryStream(filename, Date.now(), COMPRESSION_BEST, istream, false)
istream.close()
}

if (this.globals.document.getElementById('better-bibtex-error-report-include-db').checked) {
tape.append(`${this.key}/database.json`, DB.serialize({ serializationMethod: 'pretty' }))
tape.append(`${this.key}/cache.json`, Cache.serialize({ serializationMethod: 'pretty' }))
}
add(
`${this.key}/debug.txt`,
[ this.errorlog.info, this.cacheState, this.errorlog.errors, this.errorlog.debug ].filter(chunk => chunk).join('\n\n')
)

if (this.errorlog.items) add(`${this.key}/items.json`, this.errorlog.items)

return gzip(tape.out) as Uint8Array
if (this.globals.document.getElementById('better-bibtex-error-report-include-db').checked) {
add(`${this.key}/database.json`, DB.serialize({ serializationMethod: 'pretty' }))
add(`${this.key}/cache.json`, Cache.serialize({ serializationMethod: 'pretty' }))
}

zipWriter.close()
}
return this.zipfile
}

public async save(): Promise<void> {
const filename = await pick('Logs', 'save', [['Tape Archive (*.tar.gz)', '*.tar.gz']], `${this.key}.tar.gz`)
if (filename) await OS.File.writeAtomic(filename, this.tar())
const filename = await pick('Logs', 'save', [['Zip Archive (*.zip)', '*.zip']], `${this.key}.zip`)
if (filename) await OS.File.copy(await this.zip(), filename)
}

private async ping(region: string) {
Expand All @@ -138,7 +162,7 @@ export class ErrorReport {
}

public async load(): Promise<void> {
this.key = this.timestamp = (new Date()).toISOString().replace(/\..*/, '').replace(/:/g, '.')
this.timestamp = (new Date()).toISOString().replace(/\..*/, '').replace(/:/g, '.')

const wizard = this.globals.document.getElementById('better-bibtex-error-report')

Expand Down Expand Up @@ -188,6 +212,8 @@ export class ErrorReport {
this.bucket = `http://${s3.bucket}-${region.short}.s3-${region.region}.amazonaws.com${region.tld || ''}`
this.key = `${Zotero.Utilities.generateObjectKey()}${this.params.scope ? '-refs' : ''}-${region.short}`

this.zipfile = OS.Path.join(Zotero.getTempDirectory().path, `${this.key}-${this.timestamp}.zip`)

continueButton.disabled = false
continueButton.focus()
}
Expand All @@ -196,6 +222,9 @@ export class ErrorReport {
wizard.getButton('cancel').disabled = false
}
}
public async unload(): Promise<void> {
if (await OS.File.exists(this.zipfile)) await OS.File.remove(this.zipfile, { ignoreAbsent: true })
}

private preview(text: string): string {
return text.length > this.previewSize ? `${text.substr(0, this.previewSize)} ...` : text
Expand Down Expand Up @@ -231,8 +260,6 @@ export class ErrorReport {
info += `Zotero.Debug.enabled: ${Zotero.Debug.enabled}\n`
info += `Zotero.Debug.enabled at start: ${Zotero.BetterBibTeX.debugEnabledAtStart}\n`

info += `LocaleDateOrder: ${Zotero.Date.getLocaleDateOrder()}\n`

info += `Total export workers started: ${Translators.workers.total}, currently running: ${Translators.workers.running.size}\n`

return info
Expand Down
10 changes: 10 additions & 0 deletions content/ErrorReport.xul
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,15 @@
Zotero.debug('failed to load ErrorReport' + err.message)
}
})
window.addEventListener('unload', async function() {
try {
Zotero.debug('unloading ErrorReport')
await Zotero.BetterBibTeX.ErrorReport.unload()
Zotero.debug('unloaded ErrorReport')
}
catch (err) {
Zotero.debug('failed to unload ErrorReport' + err.message)
}
})
</script>
</wizard>
10 changes: 5 additions & 5 deletions content/ExportOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { patch as $patch$, unpatch as $unpatch$ } from './monkey-patch'
import { patch as $patch$, unpatch as $unpatch$, Trampoline } from './monkey-patch'
import * as l10n from './l10n'

export class ExportOptions {
private globals: Record<string, any>
private DOM_OBSERVER: MutationObserver = null
private reset = true
private patched: Trampoline[] = []

public load(globals: Record<string, any>): void {
this.globals = globals
Expand All @@ -21,13 +22,13 @@ export class ExportOptions {
}
// eslint-disable-next-line prefer-rest-params
original.apply(this, arguments)
})
}, this.patched)

$patch$(this.globals.Zotero_File_Interface_Export, 'updateOptions', original => function(_options) {
// eslint-disable-next-line prefer-rest-params
original.apply(this, arguments)
self.warning()
})
}, this.patched)
}

public warning(): void {
Expand Down Expand Up @@ -59,8 +60,7 @@ export class ExportOptions {

public unload(): void {
this.DOM_OBSERVER.disconnect()
$unpatch$(this.globals.Zotero_File_Interface_Export, 'init', true)
$unpatch$(this.globals.Zotero_File_Interface_Export, 'updateOptions', true)
$unpatch$(this.patched)
}

mutex(e: Event): void {
Expand Down
8 changes: 4 additions & 4 deletions content/Preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class AutoExportPane {
break

case 'status':
if (ae.status === 'running' && Preference.workers && typeof progress === 'number') {
if (ae.status === 'running' && Preference.worker && typeof progress === 'number') {
(node as XUL.Textbox).value = progress < 0 ? `${this.label?.preparing || 'preparing'} ${-progress}%` : `${progress}%`
}
else {
Expand Down Expand Up @@ -541,12 +541,12 @@ export class PrefPane {

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
for (const state of (Array.from(this.globals.document.getElementsByClassName('better-bibtex-preferences-worker-state')) as XUL.Textbox[])) {
state.value = l10n.localize(`BetterBibTeX.workers.${Preference.workers ? 'status' : 'disabled'}`, {
state.value = l10n.localize(`BetterBibTeX.workers.${Preference.worker ? 'status' : 'disabled'}`, {
total: Translators.workers.total,
workers: Preference.workers,
workers: Preference.worker,
running: Translators.workers.running.size,
})
state.classList[Preference.workers ? 'remove' : 'add']('textbox-emph')
state.classList[Preference.worker ? 'remove' : 'add']('textbox-emph')
}

if (this.autoexport) this.autoexport.refresh()
Expand Down
2 changes: 0 additions & 2 deletions content/Preferences/export/misc.pug
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ groupbox
checkbox(preference="pref-better-bibtex-retainCache" label="&better-bibtex.Preferences.advanced.export.retainCache;")
image(tooltip="tooltip-retainCache" src="chrome://zotero-better-bibtex/skin/attention.svg" width="16" height="16")
hbox
label(bbt:preference="pref-better-bibtex-workers") &better-bibtex.Preferences.advanced.export.workers;
scale(min="1" max="10" preference="pref-better-bibtex-workers")
checkbox(preference="pref-better-bibtex-cache" label="&better-bibtex.Preferences.advanced.export.workers.cache;")
label.better-bibtex-preferences-worker-state
hbox
Expand Down
24 changes: 10 additions & 14 deletions content/Preferences/preferences.pug
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,16 @@ bbt:doc.
Any characters entered here will prefer a math-mode LaTeX-command counterpart over a math-mode,
if a math-mode command is available. Only useful when `mapUnicode` is `conservative`.

preference#pref-better-bibtex-workers(name="extensions.zotero.translators.better-bibtex.workers" bbt:affects="" type="int" default="1")
bbt:doc.
BBT can now perform its exports in a separate thread, and
should no longer block Zotero's UI pretty much regardless
of how large your library is. The default of 1 parallel
export should suit most needs, but if you have many
auto-exports set up, you may want to raise the maximum
parallel exports to prevent queueing of exports. It is
possible to turn background exports off by setting this
value to `0` in the hidden preferences; you will get the
old (blocking) behavior back, but you can't complain about
Zotero being laggy during auto-exports. All Zotero exports
are blocking, and it's a minor miracle I got background
exports to work at all.
preference(name="extensions.zotero.translators.better-bibtex.worker" bbt:affects="" type="bool" default="true")
bbt:doc.
BBT can perform its exports in a separate thread, and should no
longer block Zotero's UI pretty much regardless of how large your
library is. It is possible to turn background exports off by
setting this value to `false` in the hidden preferences; you will get
the old (blocking) behavior back, but you can't complain about
Zotero being laggy during auto-exports; all Zotero exports are
blocking, and it's a minor miracle I got background exports to
work at all.

preference#pref-better-bibtex-cache(name="extensions.zotero.translators.better-bibtex.cache" bbt:affects="*" type="bool" default="true")
bbt:doc.
Expand Down
15 changes: 10 additions & 5 deletions content/ZoteroPane.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from './logger'
import { TeXstudio } from './tex-studio'
import { repatch as $patch$ } from './monkey-patch'
import { patch as $patch$, unpatch as $unpatch$, Trampoline } from './monkey-patch'
import { clean_pane_persist } from './clean_pane_persist'
import { Preference } from './prefs'
import { AutoExport } from './auto-export'
Expand All @@ -13,9 +13,14 @@ import * as DateParser from './dateparser'

export class ZoteroPane {
private globals: Record<string, any>
private patched: Trampoline[] = []

public unload(): void {
$unpatch$(this.patched)
}

public load(): void {
const pane = Zotero.getActiveZoteroPane()
const pane = Zotero.getActiveZoteroPane() // TODO: this is problematic if there can be multiple

const globals = this.globals
$patch$(pane, 'buildCollectionContextMenu', original => async function() {
Expand Down Expand Up @@ -71,14 +76,14 @@ export class ZoteroPane {
catch (err) {
log.error('ZoteroPane.buildCollectionContextMenu:', err)
}
})
}, this.patched)

// Monkey patch because of https://groups.google.com/forum/#!topic/zotero-dev/zy2fSO1b0aQ
$patch$(pane, 'serializePersist', original => function() {
// eslint-disable-next-line prefer-rest-params
original.apply(this, arguments)
if (Zotero.BetterBibTeX.uninstalled) clean_pane_persist()
})
}, this.patched)
}

public pullExport(): void {
Expand Down Expand Up @@ -155,7 +160,7 @@ export class ZoteroPane {
const extra = Extra.get(item.getField('extra'), 'zotero', { tex: true })
for (const [k, v] of Object.entries(extra.extraFields.tex)) {
if (mapping[k]) {
const date = DateParser.parse(v.value, Zotero.BetterBibTeX.localeDateOrder)
const date = DateParser.parse(v.value)
if (date.type === 'date' && date.day) {
delete extra.extraFields.tex[k]
item.setField(mapping[k], new Date(date.year, date.month - 1, date.day, 0, -tzdiff).toISOString())
Expand Down
11 changes: 10 additions & 1 deletion content/ZoteroPane.xul
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,18 @@
Zotero.debug('BBT started, loading ZoteroPane')
Zotero.BetterBibTeX.ZoteroPane.load()
Zotero.debug('ZoteroPane started')
} catch (err) {
}
catch (err) {
Zotero.debug(`loading ZoteroPane error: ${err.message}\n${err.stack ||''}`)
}
})
window.addEventListener('unload', async function() {
try {
await Zotero.BetterBibTeX.unload()
}
catch (err) {
Zotero.debug(`unloading ZoteroPane error: ${err.message}\n${err.stack ||''}`)
}
})
</script>
</overlay>
Loading

0 comments on commit 93ff13e

Please sign in to comment.