UNPKG

@tradle/aws-s3-client

Version:
124 lines 6.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapFolder = exports.wrapBucket = exports.Folder = exports.Bucket = void 0; const path_1 = __importDefault(require("path")); const lodash_1 = __importDefault(require("lodash")); const aws_common_utils_1 = require("@tradle/aws-common-utils"); const kv_1 = require("./kv"); class Bucket { constructor(opts) { this.opts = opts; this.folder = (prefix) => { return new Bucket(Object.assign(Object.assign({}, this.opts), { prefix: getFolderPath(this.prefix, prefix) })); }; this.sub = (prefix) => { return new Bucket(Object.assign(Object.assign({}, this.opts), { prefix: this._getKey(prefix) })); }; this.withPrefix = (prefix) => { return new Bucket(Object.assign(Object.assign({}, this.opts), { prefix: this.prefix + prefix })); }; this.get = (key) => this.client.get({ key: this._getKey(key), bucket: this.bucket }); this.maybeGet = (key) => this.get(key).catch(aws_common_utils_1.Errors.ignoreNotFound); this.getJSON = (key) => this.get(key).then(({ Body }) => JSON.parse(Body.toString())); this.maybeGetJSON = (key) => this.getJSON(key).catch(aws_common_utils_1.Errors.ignoreNotFound); this.list = (opts) => this.client.listBucket(Object.assign({ bucket: this.bucket }, opts)); this.listObjects = (opts) => this.client.listObjects(Object.assign({ bucket: this.bucket }, opts)); this.listWithPrefix = (prefix, listOpts) => { this.client.listBucketWithPrefix({ bucket: this.bucket, prefix: this._getKey(prefix), s3Opts: listOpts }); }; this.listObjectsWithKeyPrefix = (prefix, listOpts) => this.client.listObjectsWithKeyPrefix({ bucket: this.bucket, prefix: this._getKey(prefix), s3Opts: listOpts }); this.put = async (key, value, opts) => { await this.client.put(Object.assign({ key: this._getKey(key), value, bucket: this.bucket }, opts)); }; this.putJSON = (key, value, opts) => this.put(key, value, opts); this.gzipAndPut = (key, value, opts = {}) => this.client.gzipAndPut(Object.assign({ key: this._getKey(key), value, bucket: this.bucket }, opts)); this.head = (key) => this.client.head({ key: this._getKey(key), bucket: this.bucket }); this.exists = (key) => this.client.exists({ key: this._getKey(key), bucket: this.bucket }); this.has = this.exists; this.del = (key) => this.client.del({ key: this._getKey(key), bucket: this.bucket }); this.getCacheable = opts => this.client.getCacheable(Object.assign(Object.assign({}, opts), { key: this._getKey(opts.key), bucket: this.bucket })); this.create = () => this.client.createBucket({ bucket: this.bucket }); this.destroy = () => this.client.destroyBucket({ bucket: this.bucket }); this.clear = () => this.client.clearBucket({ bucket: this.bucket }); this.toString = () => this.bucket; this.getUrlForKey = (key) => this.client.getUrlForKey({ key: this._getKey(key), bucket: this.bucket }); this.forEach = opts => this.client.forEachItemInBucket(Object.assign({ bucket: this.bucket }, opts)); this.enableEncryption = (opts) => this.client.enableEncryption(Object.assign({ bucket: this.bucket }, opts)); this.disableEncryption = () => this.client.disableEncryption({ bucket: this.bucket }); this.getEncryption = () => this.client.getEncryption({ bucket: this.bucket }); this.putIfDifferent = async (key, value) => { key = this._getKey(key); let current; try { current = await this.get(key); } catch (err) { aws_common_utils_1.Errors.ignoreNotFound(err); } if (!lodash_1.default.isEqual(current, value)) { this.put(key, value); return true; } return false; }; this.makePublic = () => this.client.makePublic({ bucket: this.bucket }); this.empty = () => this.client.emptyBucket({ bucket: this.bucket }); this.copyFilesTo = ({ bucket, keys, prefix, acl }) => this.client.copyFilesBetweenBuckets({ source: this.bucket, target: bucket, keys, prefix, acl }); this.isPublic = () => this.client.isBucketPublic({ bucket: this.bucket }); this.makeKeysPublic = (keys) => this.client.makeKeysPublic({ bucket: this.bucket, keys }); this.createPresignedUrl = (key) => this.client.createPresignedUrl({ key: this._getKey(key), bucket: this.bucket }); this.kv = () => kv_1.createKVStore({ folder: this }); this.kvWithGzip = () => kv_1.createKVStore({ folder: this }); this.jsonKV = () => kv_1.createJsonKVStore({ folder: this }); this.jsonKVWithGzip = () => kv_1.createGzippedJsonKVStore({ folder: this }); this._getKey = (key) => this.prefix + key; const { bucket, client, prefix = '' } = opts; if (typeof bucket !== 'string') { throw new Error('expected string "bucket"'); } this.bucket = bucket; this.client = client; this.prefix = prefix; } get id() { return this.bucket; } get name() { return this.bucket; } } exports.Bucket = Bucket; exports.Folder = Bucket; const getFolderPath = (parent, folder) => { const fPath = path_1.default.join(parent, folder); return fPath.replace(/[/]+$/, '') + '/'; }; exports.wrapBucket = (opts) => new Bucket(opts); exports.wrapFolder = exports.wrapBucket; //# sourceMappingURL=bucket.js.map