Skip to content

Commit

Permalink
chore: refactor external resource builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zdm committed Nov 11, 2024
1 parent df01384 commit 9ef214a
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 22 deletions.
41 changes: 31 additions & 10 deletions lib/external-resource-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default class ExternalRecourceBuilder {
#tag;
#name;
#githubToken;
#etag;
#buildDate;
#expires;
#meta;

constructor ( id, { githubtoken } = {} ) {
if ( id ) {
Expand Down Expand Up @@ -93,6 +97,22 @@ export default class ExternalRecourceBuilder {
return this.#name;
}

get etag () {
return this.#etag;
}

get buildDate () {
return this.#buildDate;
}

get expires () {
return this.#expires;
}

get meta () {
return this.#meta;
}

// public
async build ( { force, cache } = {} ) {
return this.#build( { force, cache } );
Expand Down Expand Up @@ -167,16 +187,18 @@ export default class ExternalRecourceBuilder {
if ( !res.ok ) return res;
var index = res.data;

this.#etag = index[ this.#name ]?.etag;
this.#buildDate = index[ this.#name ]?.buildDate
? new Date( index[ this.#name ].buildDate )
: null;
this.#expires = index[ this.#name ]?.expires
? new Date( index[ this.#name ].expires )
: null;
this.#meta = index[ this.#name ]?.meta;

// get etag
try {
res = result.try( await this._getEtag( {
"etag": index[ this.#name ]?.etag,
"buildDate": index[ this.#name ]?.buildDate,
"expires": index[ this.#name ]?.expires
? new Date( index[ this.#name ].expires )
: null,
"meta": index[ this.#name ]?.meta,
} ) );
res = result.try( await this._getEtag() );
}
catch ( e ) {
res = result.catch( e, { "log": false } );
Expand Down Expand Up @@ -210,7 +232,7 @@ export default class ExternalRecourceBuilder {
}
if ( !res.ok ) return res;

const meta = res.data;
const meta = res.data ?? null;

const tmp = new TmpDir();

Expand Down Expand Up @@ -253,7 +275,6 @@ export default class ExternalRecourceBuilder {
index[ this.#name ].buildDate = new Date();
index[ this.#name ].expires = expires;
index[ this.#name ].meta = meta;
if ( !index[ this.#name ].meta ) delete index[ this.#name ].meta;

// upload index
res = await this.#uploadAsset(
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Http extends ExternalRecourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
const res = await fetch( await this.#getUrl(), {
"method": "head",
} );
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/geolite2-asn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class TLD extends ExternalResourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
if ( !MAXMIND_LICENSE_KEY ) return result( [ 500, `Maxmind license key not found` ] );

const res = await this._getLastModified( url );
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/geolite2-city.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class TLD extends ExternalResourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
if ( !MAXMIND_LICENSE_KEY ) return result( [ 500, `Maxmind license key not found` ] );

const res = await this._getLastModified( url );
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/geolite2-country.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class TLD extends ExternalRecourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
if ( !MAXMIND_LICENSE_KEY ) return result( [ 500, `Maxmind license key not found` ] );

const res = await this._getLastModified( url );
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/google-geotargets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class GoogleGeotargets extends ExternalResourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
const res = await this.#prepare();
if ( !res.ok ) return res;

Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Http extends ExternalRecourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
return result( 200, JSON.stringify( resources ) );
}

Expand Down
6 changes: 3 additions & 3 deletions lib/external-resources/local.softvisio.net.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default class Datasets extends ExternalResourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, expires, meta } ) {
if ( expires ) {
if ( UPDATE_INTERVAL.toDate() < expires ) {
async _getEtag () {
if ( this.expires ) {
if ( UPDATE_INTERVAL.toDate() < this.expires ) {
return result( 200 );
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/public-suffixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class PublicSuffix extends ExternalRecourceBuilder {
}

// proteted
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
const res = await this.#getData();
if ( !res.ok ) return res;

Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/subnets.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class TLD extends ExternalRecourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
const res = await this.#build();
if ( !res.ok ) return res;

Expand Down
2 changes: 1 addition & 1 deletion lib/external-resources/tld.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class TLD extends ExternalRecourceBuilder {
}

// protected
async _getEtag ( { etag, buildDate, meta } ) {
async _getEtag () {
const res = await this.#getData();
if ( !res.ok ) return res;

Expand Down

0 comments on commit 9ef214a

Please sign in to comment.