@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
84 lines (83 loc) • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.invalidateCache = exports.uploadFile = void 0;
const axios_1 = __importDefault(require("axios"));
const configstore_1 = __importDefault(require("configstore"));
const log_1 = require("diginext-utils/dist/xconsole/log");
const package_json_1 = __importDefault(require("../../package.json"));
const conf = new configstore_1.default(package_json_1.default.name);
const storage_1 = require("@google-cloud/storage");
const path_1 = __importDefault(require("path"));
const bucketName = "digitop-cdn-sg";
let prevInvalidateTime;
// Creates a client
const credFile = path_1.default.resolve(__dirname, "../cred/TOP Group K8S-c08c41784317.json");
// console.log(credFile);
/**
* TODO: INVALIDATE CACHE
* Reference: https://cloud.google.com/compute/docs/reference/rest/v1/urlMaps/invalidateCache
* Installation: npm install googleapis --save
* */
// const storage = new Storage({
// projectId: "top-group-k8s",
// keyFilename: credFile,
// });
const storage = new storage_1.Storage();
async function uploadFile(filename, destination) {
// Uploads a local file to the bucket
try {
await storage.bucket(bucketName).upload(filename, {
// chunkSize: 5 * 1024 * 1024, // 5MB
// Support for HTTP requests made with `Accept-Encoding: gzip`
gzip: true,
// By setting the option `destination`, you can change the name of the
// object you are uploading to a bucket.
destination: destination,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: "public,max-age=31536000", // 1 year
},
});
// log(`${filename} uploaded to ${bucketName}.`);
}
catch (e) {
throw new Error(`Unable to upload "${filename}" to "${destination}": ${e}`);
}
}
exports.uploadFile = uploadFile;
const invalidateCache = async (resourcePath) => {
let now = Date.now();
prevInvalidateTime = conf.get("prevInvalidateTime");
// log("prevInvalidateTime", now - prevInvalidateTime);
if (prevInvalidateTime) {
conf.set("prevInvalidateTime", now);
let timePassed = now - prevInvalidateTime;
let timeLeft = 60000 - timePassed;
if (timePassed < 59000) {
(0, log_1.logError)(`[CDN] Rate limit exceeded (1 times/minute) - ${Math.round(timeLeft / 1000)} seconds left...`);
}
}
conf.set("prevInvalidateTime", now);
const cacheApiUrl = "https://google-cdn-util.zii.vn/purge";
const inputData = { path: resourcePath };
try {
const { data } = await (0, axios_1.default)({ url: cacheApiUrl, data: inputData, method: "post" });
(0, log_1.log)(data);
if (data.status == 1) {
(0, log_1.logSuccess)("[CDN]", data.message);
}
else {
(0, log_1.logError)("[CDN]", data.message);
}
}
catch (e) {
(0, log_1.logError)("[CDN]", e);
}
};
exports.invalidateCache = invalidateCache;
// uploadFile().catch(console.error);