Adding in hash handling for datasource plugins.

This commit is contained in:
mike12345567 2022-08-18 15:21:55 +01:00
parent 9d22f83e4e
commit e53f86c0b2
2 changed files with 15 additions and 3 deletions

View File

@ -93,7 +93,11 @@ module.exports = {
for (let plugin of plugins) {
if (plugin.name === integration) {
// need to use commonJS require due to its dynamic runtime nature
return getDatasourcePlugin(plugin.name, plugin.jsUrl)
return getDatasourcePlugin(
plugin.name,
plugin.jsUrl,
plugin.schema?.hash
)
}
}
},

View File

@ -351,13 +351,20 @@ exports.extractPluginTarball = async file => {
return { metadata, directory: path }
}
exports.getDatasourcePlugin = async (name, url) => {
exports.getDatasourcePlugin = async (name, url, hash) => {
if (!fs.existsSync(DATASOURCE_PATH)) {
fs.mkdirSync(DATASOURCE_PATH)
}
const filename = join(DATASOURCE_PATH, name)
const metadataName = `${filename}.bbmetadata`
if (fs.existsSync(filename)) {
return require(filename)
const currentHash = fs.readFileSync(metadataName, "utf8")
// if hash is the same return the file, otherwise remove it and re-download
if (currentHash === hash) {
return require(filename)
} else {
fs.unlinkSync(filename)
}
}
const fullUrl = checkSlashesInUrl(
`${env.MINIO_URL}/${ObjectStoreBuckets.PLUGINS}/${url}`
@ -366,6 +373,7 @@ exports.getDatasourcePlugin = async (name, url) => {
if (response.status === 200) {
const content = await response.text()
fs.writeFileSync(filename, content)
fs.writeFileSync(metadataName, hash)
require(filename)
} else {
throw new Error(