fast-sdk
v1.2.0
Published
This library provides javascript wrappers to access Fast.io API and toolkit for uploading large files.
Downloads
21
Readme
Fast.io SDK
This library provides javascript wrappers to access Fast.io API and toolkit for uploading large files.
Installation
npm i fast-sdk
yarn add fast-sdk
fast-sdk
exports API object, getAuthToken
, setAuthToken
utils and Uploader class.
API
Before running the API methods you should authorize first, and the authorization is happening only by SSO. For authorizing using SSO we have three options Google SSO, Microsoft SSO, and Facebook SSO. because all three ways are similar we explain the Google SSO only.
- First: you should request a
redirect URL
from the server:
const googleRedirectUrl = await api.auth.getGoogleRedirectURL()
one of the results from this request is the redirect_url
property which should be used in a new tab for going into the Google authorization process.
const strWindowFeatures = 'toolbar=no, menubar=no, width=650, height=700';
window.open(googleRedirectUrl.redirect_url, "Google Login", strWindowFeatures)
This is an example of opening a new tab for beginning the authorization process. (The authorization process is handled by Google and after the user authorization, Google redirects the user to a redirect URL
that we specified for it)
Second: When the user reaches the page that we wrote for the after-authorizing by Google. (The page that we tell Google to redirect the users to it)
- first, we should grab the search section in the URL and create an object. (the SDK has a function for that):
import { useLocation } from 'react-router' import { Utilities } from 'fast-sdk' const location = useLocation() const urlQueries = Utilities.URLToObject(location.search)
- then we should send the result object to the server:
import { api, setAuthToken } from 'fast-sdk' api.auth.getGoogleSSOToken(urlQueries).then((res) => { if(res.result){ const token = res.token setAuthToken(token) } })
Third: SDK exposes
getAuthToken
andsetAuthToken
utils so you can set authorization token manually:
import { getAuthToken, setAuthToken } from 'fast-sdk'
if (!getAuthToken())
setAuthToken('your token')
You can do all of the above processes for the Facebook SSO, and the Microsoft SSO with relative methods.
As SDK is written in Typescript, your IDE should be able to suggest all existing API methods and show their parameters.
WebStorm:
VSCode:
Editor also will show signatures of parameters.
WebStorm:
VSCode:
Multipart file uploader
Example For Uploader Class:
import { Uploader } from 'fast-sdk'
const uploader = new Uploader({
threads: 5,
retry: 1,
})
Uploader options:
threads
: (optional, 5 by default) - The number of chunks that can be uploaded in parallel.
retry
: (optional, 1 by default) - The number of retries after failing API request.
Uploader Item
The Uploader
instance should be passed to the UploaderItem
constructor along with the file input and an Encryption Key
.
You can use the below example for uploading a file to the server:
const uploaderItem = new UploaderItem(uploader, file, "ENCRYPTION KEY")
uploaderItem.iv = "IV"
// This is the IV (Initialization vector) for AES encryption
let sessionId = ""
if(uploaderItem.encryptedSize)
sessionId = await uploaderItem.getSessionId(uploaderItem.encryptedSize)
uploaderItem.session_id = sessionId;
await uploaderItem.chunkFile()
/* This method will chunk the file into pieces (based on the bucket-list algorithm with [100, 50, 25, 10, 3, 1] -in MegaBytes- as buckets) and stores them into the uploaderItem.chunks */
await uploaderItem.upload()
The process of uploading is as follows:
- You should have an
Encryption Key
and theIV
(Initialization vector) and give them to theUploaderItem
as parameters. - The
encryptedSize
is needed for thegetSessionId
function. (This function returns thesession_id
that will be used in all the next steps) - You should update the
UploaderItem
'ssession_id
. - You can now call the
chunkFile
function in order to chunk the file. - Calling the
upload
method uploads all the chunks to the server.
Properties:
parallelTasksAmount
: The number of endpoints that we could send data to, at the same time.updateProgressFunc
(Optional): The callback function which has called every time the progress has been updated.chunks
: The chunks of the file. (calculated after The file has been put into theUploaderItem
class)chunkSchema
: The Schema of theUploaderItem
file chunks.iv
: TheIV
(Initialize Vector) for the encryption.session_id
: The session id for upload process.error
: All the errors that have occurred during the process of the upload.
Methods:
chunkFile
updateFile
: updates theUploaderItem
file.syncWithIndexedDB
upload
getSessionId
chunkFile
process:
- The file is chunked into smaller pieces.
- The chunks are stored in the
indexedDB
database. - The chunks are encrypted with the
AES
algorithm withCBC
mode and thePKcs7
padding. - The
hash
of the encrypted file (with theSHA256
algorithm) is calculated. - The
rollingHash
of the above hash is calculated. - The above caclulated values (
encryptedFile
withWordArray
type,hash
androllingHash
) are stored to the relativeUploaderChunk
in theindexedDB
database.
Writing tests
Create account for testing and put it's email and password to .env file.
This account is used in most of tests, it should have at least one site created.
To create a site you need to connect a storage which were not connected to any other fast.io account yet.
I suggest to create a new dropbox user, they doesn't require email confirmation, so random email works.
Upload any image to the side header.
Add some files and folders to site root.
Put site name to .env as SERVER_WITH_CONTENT_NAME