@supabase-cache-helpers/storage-core
Version:
A collection of cache utilities for working with the Supabase Storage API. It is not meant to be used standalone.
185 lines (177 loc) • 6.04 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/directory-fetcher.ts
var fetchDirectory = (fileApi, path) => __async(null, null, function* () {
const { data, error } = yield fileApi.list(path);
if (error) throw error;
if (!Array.isArray(data)) return [];
return data;
});
// src/url-fetcher.ts
var createUrlFetcher = (mode, config) => {
return (fileApi, path) => __async(null, null, function* () {
var _a;
let params = {};
if (config == null ? void 0 : config.ensureExistence) {
const { data: exists } = yield fileApi.exists(path);
if (!exists) return;
const { data: fileInfo } = yield fileApi.info(
`${path}?bust=${Date.now()}`
);
if (!fileInfo) return;
const value = fileInfo.lastModified || fileInfo.updatedAt;
if (!value) return;
params = {
updated_at: value
};
}
let url;
if (mode === "private") {
const { data, error } = yield fileApi.createSignedUrl(
path,
(_a = config == null ? void 0 : config.expiresIn) != null ? _a : 1800,
config
);
if (error) throw error;
url = data.signedUrl;
} else if (mode === "public") {
const { data } = fileApi.getPublicUrl(path, config);
url = data.publicUrl;
} else {
throw new Error(`Invalid mode: ${mode}`);
}
const fileURL = new URL(url);
Object.entries(params).forEach(([key, value]) => {
fileURL.searchParams.append(key, value);
});
return fileURL.toString();
});
};
// src/directory-urls-fetcher.ts
var createDirectoryUrlsFetcher = (mode, config) => {
const fetchUrl = createUrlFetcher(mode, config);
return (fileApi, path) => __async(null, null, function* () {
const files = yield fetchDirectory(fileApi, path);
const filesWithUrls = [];
for (const f of files) {
const url = yield fetchUrl(fileApi, `${path}/${f.name}`);
if (url) filesWithUrls.push(__spreadProps(__spreadValues({}, f), { url }));
}
return filesWithUrls;
});
};
// src/lib/get-minimal-paths.ts
var getMinimalPaths = (paths) => paths.reduce((paths2, path) => {
const matchingPaths = paths2.filter((p) => p.startsWith(path));
return [...paths2.filter((p) => !matchingPaths.includes(p)), path];
}, []);
// src/mutate-paths.ts
var mutatePaths = (_0, _1, _2) => __async(null, [_0, _1, _2], function* (bucketId, paths, { cacheKeys, decode, mutate }) {
const minimalPaths = getMinimalPaths(paths);
if (minimalPaths.length === 0) return;
yield Promise.all(
cacheKeys.map((key) => __async(null, null, function* () {
const decodedKey = decode(key);
if (!decodedKey) return false;
if (decodedKey.bucketId !== bucketId) return false;
if (minimalPaths.find(
(p) => p.startsWith(decodedKey.path) || decodedKey.path.startsWith(p)
)) {
mutate(key);
}
}))
);
});
// src/remove-files.ts
var createRemoveFilesFetcher = (fileApi) => (paths) => __async(null, null, function* () {
const { data, error } = yield fileApi.remove(paths);
if (error) throw error;
return data;
});
// src/remove-directory.ts
var createRemoveDirectoryFetcher = (fileApi) => {
const removeFiles = createRemoveFilesFetcher(fileApi);
return (path) => __async(null, null, function* () {
const files = yield fetchDirectory(fileApi, path);
return yield removeFiles(files.map((f) => [path, f.name].join("/")));
});
};
// src/upload.ts
var defaultBuildFileName = ({ path, fileName }) => [path, fileName].filter(Boolean).join("/");
var isUploadFileInput = (i) => Boolean(i.data);
var createUploadFetcher = (fileApi, config) => {
var _a;
const buildFileName = (_a = config == null ? void 0 : config.buildFileName) != null ? _a : defaultBuildFileName;
return (files, path) => __async(null, null, function* () {
const inputFiles = [];
for (let i = 0; i < files.length; i++) {
inputFiles.push(files[i]);
}
const uploading = inputFiles.map((f) => __async(null, null, function* () {
const res = yield fileApi.upload(
buildFileName({
path,
fileName: f.name
}).replace(
// remove double "/"
/\/+/g,
"/"
),
isUploadFileInput(f) ? f.data : f,
__spreadValues({
contentType: f.type,
metadata: isUploadFileInput(f) ? f.metadata : void 0
}, config)
);
return res;
}));
return yield Promise.all(uploading);
});
};
export {
createDirectoryUrlsFetcher,
createRemoveDirectoryFetcher,
createRemoveFilesFetcher,
createUploadFetcher,
createUrlFetcher,
fetchDirectory,
mutatePaths
};
//# sourceMappingURL=index.js.map