Animakuro CDN
Repository of the CDN: https://github.com/sonyamoonglade/cdn-go
Also we are developing same library for golang: https://gitlab.com/mr.anderson20050/golang-cdn-lib
Install
npm i @4nd3rs0n/animakuro-cdn
Use client
Initialise client
import {} from
let client = new
Use it
//
Bucket manager
Bucket is like a place where files stored on the CDN. Buckets can have modules for work with some types of files. Also buckets optionally can be protected with keys from writing/deleting/riding/. You can have multiple keys for one bucket.
Bucket manager is used to store and manage buckets with their keys, add new buckets, load existing, and maybe will have some other features latter.
Bucket modules is used to do some manipulations with some types of files. For example module "image" allows to compress or resize and maybe latter even crop images on side of CDN. One bucket can have only one module.
// You can register buckets with their keys when initializing client.
let cdnClient = new CdnClient("cdn.example.com", {name: "site-content"})
// You can add one bucket or array of buckets
let cdnClient2 = new CdnClient("cdn.examle.com", [ // You can also load multiple buckets at time
{
name: "qwerty",
keys: {
read: "asdfg",
write: "zxcvb",
delete: "uiop"
}
},
{name: "images"}, // Public bucket
{name: "qwerty-public-reading",
keys: {
write: "gksakgjr",
delete: "uvdksakl"
}
}
])
// Or even load buckets from env varible
let cdnClient3 = new CdnClient("cdn.example.com", JSON.parse(process.env.BUCKETS))
// You can also register buckets using methods. It's working exactly the same
cdnClient.registerBucket([{
{name: "images"}, // Public bucket
{name: "qwerty-public-reading",
keys: {
write: "gksakgjr",
delete: "uvdksakl"
}},
{}
}])
cdnClient.newBucket({
name: "some-new-bucket",
// keys: {} // You can don't add keys
// if you whant to create public for reading writing and deleting bucket.
// Or you can only some keys. For example you can add keys for writing and deleting. Then anyone can read files without key
}) // New buckets are automaticly registered. So you don't need to call registerBuckets.
cdnClient.getBucket(<bucket name>) // returns bucket with keys or undefined
// returns bucket with keys or throws error if bucket is unregistered
cdnClient.getBucketOrErr(<bucket name>)
// You can't use unregistered buckets if
cdnClient.sendFileFromData(someFormData, "unregistered-buckets")
cdnClient.sendFileFromData(someFormData, "unregistered-buckets")
There is also may be people that for some resons don't what to use bucket manager. They can
Possible errors
No bucket ${bucketName} found. You should add it with calling CdnClient.addExistingBucket first There is some people that whant to avoid of using bucket manager for some reasons. They can simply add empty string for public or real key for private butckets into "key" parameter like in the example lower:
If you are passing value into "key" parameter -- CdnClient don't use
Upload file to a cdn
Use functions
In this lib you also can just call methods without initializing class. There is a list of methods that library has:
Upload file into CDN
File from disk
import { SendFiles } from "animakuro-cdn";
const url = "https://cdn.example.com"; // DON'T ADD "/" AT THE END!
const someAsyncFunc = async () => {
// SendFilesFromStorage(<paths to files>, <CDN url>, <bucket>, <isAsync (default: false)>)
let responce = await SendFilesFromStorage("./file.txt", url, "site-content", true)
/*
- CDN url is a address of CDN.
- "site-content" in this code is a default bucket of animakuro CDN. Buckets are the place where CDN store files. Some of the buckets can be private (can write something there only providing key). So changing `site-content` will change location of your files on the CDN. You can set it for exaple to "images".
- isAsync can be used for async file loading. Default value is false. It's useful to set it true if you have large or many files.
*/
// You can also use array as a parameter
let responce = await SendFilesFromStorage(["./file.txt", "file2.txt", "file3.txt"], url, "site-content")
}
File from stream
import { SendFiles } from "animakuro-cdn";
const someAsyncFunc = async () => {
// SendFilesFromStream(<streams>, <CDN url>, <bucket>)
let responce = await SendFilesFromStream(fs.createReadStream(filename), url, "site-content")
// You can also use array as a parameter
let responce = await SendFilesFromStorage([someReadStrem(), someReadStream()], url, "site-content")
}
File from formData
import FormData from 'form-data';
const someAsyncFunc = async () => {
const responce = await SendFilesFromForm(stream, url, "site-content")
}
Get file from CDN
To a file:
import { DownloadFile, GetFileStream, DownloadFile } from "animakuro-cdn";
DownloadFile("../path/to/save.txt", id, url, bucket)
let stream = await GetFileStream(id, url, bucket)
Remove file from cdn
import { RemoveFile } from "animakuro-cdn";
RemoveFile(id, url, bucket)