Skip to content

Commit

Permalink
fix: better handling of when source isStable
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Dec 20, 2023
1 parent 312f401 commit 2198266
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
20 changes: 6 additions & 14 deletions shared/packages/worker/src/worker/accessorHandlers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
return { success: true }
}
async getPackageActualVersion(): Promise<Expectation.Version.HTTPFile> {
if (this.accessor.isStable) {
// We assume the it is stable, so we won't even check the headers:
return {
type: Expectation.Version.Type.HTTP_FILE,

contentType: '',
contentLength: 0,
modified: 0,
etags: [`stable:${this.fullUrl}`],
}
}

const header = await this.fetchHeader()

return this.convertHeadersToVersion(header.headers)
Expand Down Expand Up @@ -259,7 +247,10 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
}
}
private async fetchHeader() {
const { headers, status, statusText } = await this.worker.cacheTemporaryData(
const ttl = this.accessor.isStable
? 1000 * 60 * 60 * 24 // 1 day
: 1000 // a second
const { headers, status, statusText } = await this.worker.cacheData(
this.type,
this.fullUrl,
async () => {
Expand All @@ -281,7 +272,8 @@ export class HTTPAccessorHandle<Metadata> extends GenericAccessorHandle<Metadata
status: response.status,
statusText: response.statusText,
}
}
},
ttl
)

return {
Expand Down
11 changes: 3 additions & 8 deletions shared/packages/worker/src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,11 @@ export abstract class GenericWorker {
}
}
/**
* Store and access temporary data. Useful to debounce / rate limit external calls
* Store and access data. Useful to debounce / rate limit external calls
* @example
* const data = await this.worker.cacheTemporaryData(this.type, url, () => getData(url))
* */
public async cacheTemporaryData<T>(
accessorType: string,
key: string,
cb: () => Promise<T>,
ttl = 1000
): Promise<T> {
public async cacheData<T>(accessorType: string, key: string, cb: () => Promise<T>, ttl = 1000): Promise<T> {
// Check if data is in cache:
if (!this._accessorTemporaryCache[accessorType]) this._accessorTemporaryCache[accessorType] = {}
const cache = this._accessorTemporaryCache[accessorType]
Expand All @@ -192,7 +187,7 @@ export abstract class GenericWorker {
this._accessorTemporaryCacheCleanupTimeout = setTimeout(() => {
this._accessorTemporaryCacheCleanupTimeout = undefined
this._accessorTemporaryCacheCleanup()
}, ttl + 1)
}, 1000)
}
return data
}
Expand Down

0 comments on commit 2198266

Please sign in to comment.