more async/await
This commit is contained in:
parent
a40b583d03
commit
5739de290f
125
wikizimmer.js
125
wikizimmer.js
|
@ -361,53 +361,56 @@ class WikiItem {
|
||||||
return command.urlBlacklist.some( patt => this.url.includes( patt ))
|
return command.urlBlacklist.some( patt => this.url.includes( patt ))
|
||||||
}
|
}
|
||||||
|
|
||||||
load () {
|
async load () {
|
||||||
return http({
|
let resp
|
||||||
|
try {
|
||||||
|
resp = await http({
|
||||||
url: this.urlReplacements(),
|
url: this.urlReplacements(),
|
||||||
encoding: null,
|
encoding: null,
|
||||||
priority: this.loadPriority
|
priority: this.loadPriority
|
||||||
}
|
})
|
||||||
)
|
} catch ( error ) {
|
||||||
.catch( error => {
|
|
||||||
if ( ! command.downloadErrors || error.options.external || error.statusCode == 404 || error.statusCode == 400 ) {
|
if ( ! command.downloadErrors || error.options.external || error.statusCode == 404 || error.statusCode == 400 ) {
|
||||||
return Promise.reject( error )
|
throw error
|
||||||
}
|
}
|
||||||
fatal( 'Fatal load error' )
|
fatal( 'Fatal load error' )
|
||||||
return Promise.reject( new Error( 'Load error' ))
|
//~ return Promise.reject( new Error( 'Load error' ))
|
||||||
})
|
}
|
||||||
.then( resp => {
|
let data = resp.body
|
||||||
const data = resp.body
|
|
||||||
|
|
||||||
this.url = resp.request.href // possibly redirected
|
this.url = resp.request.href // possibly redirected
|
||||||
this.headers = resp.headers
|
this.headers = resp.headers
|
||||||
if ( ! this.revision ) {
|
if ( ! this.revision ) {
|
||||||
const modified = this.headers[ 'last-modified' ] // "Tue, 27 Jun 2017 14:37:49 GMT"
|
const modified = this.headers[ 'last-modified' ] // "Tue, 27 Jun 2017 14:37:49 GMT"
|
||||||
const dateBasedRevision = Math.round(( Date.parse( modified ) - Date.parse( '2000-01-01' )) / 1000 )
|
const dateBasedRevision = Math.round(( Date.parse( modified ) - Date.parse( '2000-01-01' )) / 1000 ) || 0
|
||||||
this.revision = dateBasedRevision
|
this.revision = dateBasedRevision
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentType = resp.headers[ "content-type" ]
|
||||||
|
let csplit = contentType.split( ';' )
|
||||||
|
this.mimeType = csplit[ 0 ]
|
||||||
|
|
||||||
|
if ( this.mimeType.split( '/' )[ 0 ] == 'text' ) {
|
||||||
|
this.encoding = 'utf-8'
|
||||||
|
if ( csplit.length > 1 && csplit[ 1 ].includes( 'charset=' )) {
|
||||||
|
this.encoding = csplit[ 1 ].split( '=' )[ 1 ]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const contentType = resp.headers[ "content-type" ]
|
if ( this.mimeType == 'application/x-www-form-urlencoded' ) {
|
||||||
let csplit = contentType.split( ';' )
|
return mimeFromData( data )
|
||||||
this.mimeType = csplit[ 0 ]
|
.then( mimeType => {
|
||||||
|
this.mimeType = mimeType
|
||||||
|
return data
|
||||||
|
})
|
||||||
|
.catch( err => data )
|
||||||
|
}
|
||||||
|
|
||||||
if ( this.mimeType.split( '/' )[ 0 ] == 'text' ) {
|
if ( Buffer.isBuffer( data ) && this.encoding != null ) {
|
||||||
this.encoding = 'utf-8'
|
data = iconv.decode( data, this.encoding )
|
||||||
if ( csplit.length > 1 && csplit[ 1 ].includes( 'charset=' )) {
|
}
|
||||||
this.encoding = csplit[ 1 ].split( '=' )[ 1 ]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( this.mimeType == 'application/x-www-form-urlencoded' ) {
|
return data
|
||||||
return mimeFromData( data )
|
|
||||||
.then( mimeType => {
|
|
||||||
this.mimeType = mimeType
|
|
||||||
return data
|
|
||||||
})
|
|
||||||
.catch( err => data )
|
|
||||||
}
|
|
||||||
|
|
||||||
return data
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
basePath () {
|
basePath () {
|
||||||
|
@ -477,18 +480,18 @@ class WikiItem {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
save () {
|
async save () {
|
||||||
if ( this.blackListed() )
|
if ( this.blackListed() )
|
||||||
return ''
|
return ''
|
||||||
return Promise.resolve()
|
try {
|
||||||
.then( () => this.getData())
|
const data = await this.getData()
|
||||||
.then( data => this.storeData( data ))
|
await this.storeData( data )
|
||||||
.then( () => this.storeMetadata() )
|
await this.storeMetadata()
|
||||||
.then( () => this.localPath() )
|
return this.localPath()
|
||||||
.catch( err => {
|
} catch ( err ) {
|
||||||
warning( 'Save error', err.name, this.url, '->', this.localPath())
|
warning( 'Save error', err.name, this.url, '->', this.localPath())
|
||||||
return ''
|
return ''
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1177,15 +1180,15 @@ function loadCss( dom ) {
|
||||||
return css.save()
|
return css.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
function initMetadataStorage ( samplePageDOM ) {
|
async function initWikiDb () {
|
||||||
|
|
||||||
let dbName = osPath.join( wiki.saveDir, 'metadata.db' )
|
let dbName = osPath.join( wiki.saveDir, 'metadata.db' )
|
||||||
|
|
||||||
return fs.unlink( dbName )
|
try {
|
||||||
.catch( () => null )
|
await fs.unlink( dbName )
|
||||||
.then( () => sqlite.open( dbName ))
|
} catch (err) {
|
||||||
.then( db => {
|
}
|
||||||
wiki.db = db
|
wiki.db = await sqlite.open( dbName )
|
||||||
return wiki.db.exec(
|
return wiki.db.exec(
|
||||||
'PRAGMA synchronous = OFF;' +
|
'PRAGMA synchronous = OFF;' +
|
||||||
//~ 'PRAGMA journal_mode = OFF;' +
|
//~ 'PRAGMA journal_mode = OFF;' +
|
||||||
|
@ -1224,22 +1227,22 @@ function closeMetadataStorage () {
|
||||||
return wiki.db.close()
|
return wiki.db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
function core ( samplePage ) {
|
async function core ( samplePage ) {
|
||||||
if ( command.userAgent ) {
|
if ( command.userAgent ) {
|
||||||
UserAgent = command.userAgent == 'firefox' ? UserAgentFirefox : command.userAgent
|
UserAgent = command.userAgent == 'firefox' ? UserAgentFirefox : command.userAgent
|
||||||
}
|
}
|
||||||
log( 'UserAgent', UserAgent )
|
log( 'UserAgent', UserAgent )
|
||||||
|
|
||||||
processSamplePage( samplePage, command.rmdir )
|
await loadPreRequisites( )
|
||||||
.then( initMetadataStorage )
|
const sampleDom = await processSamplePage( samplePage, command.rmdir )
|
||||||
.then( loadCss )
|
await initWikiDb()
|
||||||
.then( getSiteInfo )
|
await loadCss( sampleDom )
|
||||||
.then( loadTemplate )
|
await getSiteInfo()
|
||||||
.then( getPages )
|
await getPages()
|
||||||
.then( saveMetadata )
|
await saveMetadata()
|
||||||
.then( saveMimeTypes )
|
await saveMimeTypes()
|
||||||
.then( closeMetadataStorage )
|
await closeMetadataStorage()
|
||||||
.catch( err => log( err )) // handleError
|
//~ .catch( err => log( err )) // handleError
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
function main () {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user