gfsl
v2.0.0
Published
A generic file system library for nodejs
Maintainers
Readme
gfsl (Generic file system library)
A generic file system library for nodejs
The documentation is still a work in progress. JS docs are however included.
Custome file downloader templates
By default we'll use node's bundled fetch instance, but if you need a custom downloader, you can use the following as a template
Node fetch
import fetch from "node-fetch";
/**Uses node fetch as a downloader */
function setNodeFetchDownloader(fetchInstance: typeof fetch) {
setFileDownloader((self, url, settings) => {
const file = createWriteStream(self.sysPath());
return new Promise((resolve, reject) => {
fetchInstance(url, { signal: settings?.signal })
.then((res) => {
if (!res.ok) {
if (settings?.noRetry) {
file.close();
self.rm();
} else reject(res.status);
}
res.body.pipe(file, { end: true });
file.on("close", resolve);
})
.catch(reject);
});
});
}Axios
import type { AxiosInstance } from "axios";
/**Uses a dynamic axios instance as a downloader */
function setDynamicAxiosDownloader(axiosInstance: () => AxiosInstance) {
setFileDownloader((self, url, settings) => {
const file = createWriteStream(self.sysPath());
return new Promise((resolve, reject) => {
axiosInstance()
.get(url, { responseType: "stream", signal: settings?.signal })
.then((res) => {
if (res.status > 299) {
if (settings?.noRetry) {
file.close();
self.rm();
} else reject(res.status);
}
res.data.pipe(file, { end: true });
file.on("close", resolve);
})
.catch(reject);
});
});
}
/**Uses a static axios as a downloader */
export function setAxiosDownloader(axiosInstance: AxiosInstance) {
setDynamicAxiosDownloader(() => axiosInstance);
}