|
|
|
@ -1,49 +1,49 @@ |
|
|
|
const fs = require("fs") |
|
|
|
const AWS = require("aws-sdk") |
|
|
|
const fetch = require("node-fetch") |
|
|
|
const { |
|
|
|
budibaseAppsDir, |
|
|
|
} = require("../../../utilities/budibaseDir") |
|
|
|
const { budibaseAppsDir } = require("../../../utilities/budibaseDir") |
|
|
|
|
|
|
|
async function invalidateCDN(cfDistribution, appId) { |
|
|
|
const cf = new AWS.CloudFront({}) |
|
|
|
|
|
|
|
return cf.createInvalidation({ |
|
|
|
DistributionId: cfDistribution, |
|
|
|
InvalidationBatch: { |
|
|
|
CallerReference: appId, |
|
|
|
Paths: { |
|
|
|
Quantity: 1, |
|
|
|
Items: [ |
|
|
|
`/assets/${appId}/*` |
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
}).promise() |
|
|
|
return cf |
|
|
|
.createInvalidation({ |
|
|
|
DistributionId: cfDistribution, |
|
|
|
InvalidationBatch: { |
|
|
|
CallerReference: appId, |
|
|
|
Paths: { |
|
|
|
Quantity: 1, |
|
|
|
Items: [`/assets/${appId}/*`], |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
.promise() |
|
|
|
} |
|
|
|
|
|
|
|
async function fetchTemporaryCredentials() { |
|
|
|
const response = await fetch(process.env.DEPLOYMENT_CREDENTIALS_URL, { |
|
|
|
method: "POST", |
|
|
|
body: JSON.stringify({ |
|
|
|
apiKey: process.env.BUDIBASE_API_KEY |
|
|
|
}) |
|
|
|
apiKey: process.env.BUDIBASE_API_KEY, |
|
|
|
}), |
|
|
|
}) |
|
|
|
|
|
|
|
if (response.status !== 200) { |
|
|
|
throw new Error(`Error fetching temporary credentials for api key: ${BUDIBASE_API_KEY}`) |
|
|
|
throw new Error( |
|
|
|
`Error fetching temporary credentials for api key: ${process.env.BUDIBASE_API_KEY}` |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
const json = await response.json() |
|
|
|
|
|
|
|
return json |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const CONTENT_TYPE_MAP = { |
|
|
|
html: "text/html", |
|
|
|
css: "text/css", |
|
|
|
js: "application/javascript" |
|
|
|
}; |
|
|
|
js: "application/javascript", |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Recursively walk a directory tree and execute a callback on all files. |
|
|
|
@ -52,9 +52,9 @@ const CONTENT_TYPE_MAP = { |
|
|
|
*/ |
|
|
|
function walkDir(dirPath, callback) { |
|
|
|
for (let filename of fs.readdirSync(dirPath)) { |
|
|
|
const filePath = `${dirPath}/${filename}` |
|
|
|
const filePath = `${dirPath}/${filename}` |
|
|
|
const stat = fs.lstatSync(filePath) |
|
|
|
|
|
|
|
|
|
|
|
if (stat.isFile()) { |
|
|
|
callback(filePath) |
|
|
|
} else { |
|
|
|
@ -63,24 +63,24 @@ function walkDir(dirPath, callback) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.uploadAppAssets = async function ({ appId }) { |
|
|
|
const { |
|
|
|
credentials, |
|
|
|
accountId, |
|
|
|
exports.uploadAppAssets = async function({ appId }) { |
|
|
|
const { |
|
|
|
credentials, |
|
|
|
accountId, |
|
|
|
bucket, |
|
|
|
cfDistribution, |
|
|
|
} = await fetchTemporaryCredentials() |
|
|
|
|
|
|
|
AWS.config.update({ |
|
|
|
accessKeyId: credentials.AccessKeyId, |
|
|
|
accessKeyId: credentials.AccessKeyId, |
|
|
|
secretAccessKey: credentials.SecretAccessKey, |
|
|
|
sessionToken: credentials.SessionToken |
|
|
|
}); |
|
|
|
sessionToken: credentials.SessionToken, |
|
|
|
}) |
|
|
|
|
|
|
|
const s3 = new AWS.S3({ |
|
|
|
params: { |
|
|
|
Bucket: bucket |
|
|
|
} |
|
|
|
Bucket: bucket, |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
const appAssetsPath = `${budibaseAppsDir()}/${appId}/public` |
|
|
|
@ -94,14 +94,16 @@ exports.uploadAppAssets = async function ({ appId }) { |
|
|
|
const fileExtension = [...filePath.split(".")].pop() |
|
|
|
const fileBytes = fs.readFileSync(filePath) |
|
|
|
|
|
|
|
const upload = s3.upload({ |
|
|
|
Key: filePath.replace(appAssetsPath, `assets/${appId}`), |
|
|
|
Body: fileBytes, |
|
|
|
ContentType: CONTENT_TYPE_MAP[fileExtension], |
|
|
|
Metadata: { |
|
|
|
accountId |
|
|
|
} |
|
|
|
}).promise() |
|
|
|
const upload = s3 |
|
|
|
.upload({ |
|
|
|
Key: filePath.replace(appAssetsPath, `assets/${appId}`), |
|
|
|
Body: fileBytes, |
|
|
|
ContentType: CONTENT_TYPE_MAP[fileExtension], |
|
|
|
Metadata: { |
|
|
|
accountId, |
|
|
|
}, |
|
|
|
}) |
|
|
|
.promise() |
|
|
|
|
|
|
|
uploads.push(upload) |
|
|
|
}) |
|
|
|
@ -109,9 +111,9 @@ exports.uploadAppAssets = async function ({ appId }) { |
|
|
|
|
|
|
|
try { |
|
|
|
await Promise.all(uploads) |
|
|
|
await invalidateCDN(cfDistribution, appId) |
|
|
|
await invalidateCDN(cfDistribution, appId) |
|
|
|
} catch (err) { |
|
|
|
console.error("Error uploading budibase app assets to s3", err) |
|
|
|
throw err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|