@storm-software/cloudflare-tools
Version:
A Nx plugin package that contains various executors, generators, and utilities that assist in managing Cloudflare services.
69 lines (57 loc) • 3.49 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunkOZPXCCZBjs = require('./chunk-OZPXCCZB.js');
// src/utils/r2-bucket-helpers.ts
var _libstorage = require('@aws-sdk/lib-storage');
var _crypto = require('crypto');
var _prettybytes = require('pretty-bytes'); var _prettybytes2 = _interopRequireDefault(_prettybytes);
async function uploadFile(client, bucketName, bucketPath, fileName, version, fileContent, contentType = "application/octet-stream", isDryRun = false) {
const key = _optionalChain([(!_optionalChain([bucketPath, 'optionalAccess', _ => _.trim, 'call', _2 => _2()]) || _optionalChain([bucketPath, 'optionalAccess', _3 => _3.trim, 'call', _4 => _4()]) === "/" ? fileName : _chunkOZPXCCZBjs.joinPaths.call(void 0, bucketPath.trim(), fileName)), 'optionalAccess', _5 => _5.replace, 'call', _6 => _6(/^\/+/g, "")]) || "";
_chunkOZPXCCZBjs.writeDebug.call(void 0,
`Uploading ${key} (content-type: ${contentType}, size: ${_prettybytes2.default.call(void 0,
Buffer.byteLength(fileContent, getEncoding(contentType))
)}) to the ${bucketName} R2 bucket`
);
try {
if (!isDryRun) {
const upload = new (0, _libstorage.Upload)({
client,
params: {
Bucket: bucketName,
Key: key,
Body: Buffer.from(fileContent, getEncoding(contentType)),
ContentType: contentType,
Metadata: {
version,
checksum: _crypto.createHash.call(void 0, "sha256").update(fileContent).digest("base64")
}
}
});
await upload.done();
} else {
_chunkOZPXCCZBjs.writeWarning.call(void 0, "[Dry run]: Skipping upload to the R2 bucket.");
}
} catch (error) {
_chunkOZPXCCZBjs.writeError.call(void 0, `Failed to upload ${key} to the ${bucketName} R2 bucket.`);
throw error;
}
}
function getInternalDependencies(projectName, graph) {
const allDeps = _nullishCoalesce(graph.dependencies[projectName], () => ( []));
return Array.from(
allDeps.reduce(
(acc, node) => {
const found = graph.nodes[node.target];
if (found) acc.push(found);
return acc;
},
[]
)
);
}
function isTextFile(mimeType) {
return mimeType.startsWith("text/") || mimeType === "application/json" || mimeType === "application/javascript" || mimeType === "application/xml" || mimeType === "application/x-yaml" || mimeType === "application/xhtml+xml" || mimeType === "application/x-httpd-php" || mimeType === "image/svg+xml";
}
function getEncoding(mimeType) {
return isTextFile(mimeType) ? "utf8" : "binary";
}
exports.uploadFile = uploadFile; exports.getInternalDependencies = getInternalDependencies; exports.isTextFile = isTextFile; exports.getEncoding = getEncoding;