npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lufi-api

v0.2.0

Published

A TypeScript API to work with Lufi upload server

Downloads

71

Readme

lufi-api

Introduction

Lufi API provides functions needed to interract with Lufi server in your project. It's built in Typescript and transpiled to Javascript using deno

Table of contents

Installation

You can use deno to install dependencies:

deno install

Then you can build it for browser by using:

deno task build

Finally, copy content of dist folder to your project.

Usage

Imports

You can import lufi in your project using:

import { lufi } from "lufi"

Since Lufi is using neverthrow, you can either add neverthrow as a dependency of your project or use types and functions of neverthrow provided by Lufi.

import { err, errAsync, ok, okAsync, ResultAsync } from "lufi"

Upload files

You can upload one or multiple files by using the upload() function. The upload is asynchronous and so this function will return a list of upload jobs (one by file).

lufi.upload(serverUrl: URL, filesToUpload: File[], delay?: number = 0, delAtFirstView?: boolean, zipped?: boolean, zipName?: string, password?: string, algo?: CryptoAlgorithm
);

Example using deno:

// Transform Uint8Array files provided by readFileSync() into a File API object
const files = [
  new File([new Blob([Deno.readFileSync("file1.txt")])], "file1.txt"),
  new File([new Blob([Deno.readFileSync("file2.jpg")])], "file2.jpg"),
]

// Run upload jobs and wait for each job completion.
lufi.upload("http://my.local.lufi.server/", files)
.andThen((jobs) => ResultAsync.combine(jobs.map((job) => job.waitForCompletion())))
.orElse((error) => console.error(error))
.then(() => console.debug("Success!"));

waitForJobCompletion() will wait for the job to terminate before executing the next task.

You can also follow the progress of the upload by using the job function onProgress():

// ...

lufi.upload("http://my.local.lufi.server/", files)
.andThen((jobs) => ResultAsync.combine(jobs.map((job) => { 
  job.onProgress(() => {
    console.debug(`Uploaded ${job.lufiFile.chunksReady} / ${job.lufiFile.totalChunks} chunks of ${job.lufiFile.name}`);
  })

  return job.waitForCompletion();
})))
.orElse((error) => console.error(error))
.then(() => console.debug("Success!"));

This will print in the console a message each time a chunk is successfuly uploaded to the server. As you can see, you can access the lufiFile object from the job. This object contains all informations you need about your file.

If you indicate you upload your files with zipped set on true, Lufi API will automatically compress it in a zip before upload:

// ...
lufi.upload(serverUrl, files, undefined, undefined, true, "my-archive.zip")
.andThen((jobs) => job[0].waitForCompletion())
.andThen((job) => {
  console.debug(`File uploaded! Download link: ${job.lufiFile.downloadUrl()}`);

  return okAsync(undefined);
})
.orElse((error) => console.error(error));

In this example, there will be only one job in the jobs array, so we don't need to map through it and directly work with the first job.

Download a file

You can download a file by using the download() function.

lufi.download(downloadUrl: URL, password?: string);

Example:

const downloadDir = "./tmp/";
const lufiUrl = new URL("https://you.lufi.server/r/T_9Eaz7zcK#5EY3AekzavcUhHXmUPzgZDRC257LwfOMWzp26MsEVqI=")
const password = "My awesome and very secured password. 101z"

lufi.download(lufiUrl, password).andThen((job) => {
  let downloadedChunks = 0;
  job.onProgress(() => {
    downloadedChunks++;

    console.debug(`Downloaded chunk ${downloadedChunks}/${job.lufiFile.totalChunks}`)
  })

  return job.waitForCompletion();
})
.orElse(async (error) => console.error(error))
.then((job) => {
  // Use Deno to write file to the system.
  if (job.isOk()) {
    const fileBuffer = await job.value.tmpFile.arrayBuffer();
    Deno.writeFile(downloadDir + job.value.lufiFile.name, new Uint8Array(fileBuffer));
  }
});

Pause/Resume a job

You can pause/resume an upload/download job using pause() or resume()

Example:

lufi.download(downloadUrl)
  .andThen((job) => lufi.pause(job))
  .andThen((job) => {
    console.debug("Job has been paused");

    return okAsync(job);
  })
  .andThen((job) => lufi.resume(job))
  .andThen((job) => lufi.waitForCompletion())
  .andThen((job) => {
    console.debug(`Upload of ${job.lufiFile.name} is now complete!`);

    return okAsync(undefined);
  })
  .orElse((error) => console.error(error));
// ...

Cancel an upload

To ask Lufi server to cancel the upload you can use cancel().

// ...
lufi.upload(serverUrl, [file])
.andThen((job) => job.cancel(job))
.andThen((job) => {
  console.debug(`Upload of ${job.lufiFile.name} has been canceled`));

  return okAsync(undefined);
}
.orElse((error) => console.error(error));

Retrieve informations

You can get informations about a file from the Lufi server by using infos():

lufi.infos(downloadUrl)
.andThen((job) => {
  console.debug(`The number of days after creation before the file will be deleted is: ${job.lufiFile.delay}`);
  
  return okAsync(undefined);
})
.orElse((error) => console.error(error));

Zipping files

You can manually work with zips using addFilesToArchive() and compress() functions:

// ...
const zipName = "my-archive.zip"

lufi.addFilesToArchive(files)
.andThen((archiveEntries) => lufi.compress(archiveEntries, zipName))
.andThen((zipFile) => {
  console.debug(`Archive size is: ${zipFile.size} `);

  return okAsync(undefined);
})
.orElse((error) => console.error(error));

Note that zipping is not working in deno runtime for now. deno needs to fix an issue with workers.

Unzipping files

You can decompress a Zip archive using decompress().

// ...
lufi.decompress(zipFile)
.andThen((files) => {
  files.forEach((file) => {
    console.debug(`Archive contains the file ${file.name}`);
  })

  return okAsync(undefined);
})
.orElse((error) => console.error(error));

Running tests

You can run the test suite by using deno task test.

Before running tests, please, think about downloading file-100mb.txt and file-1gb.txt into the test/ folder. We do not provide it to do not occupate a lot of space just for test files. You can use this website to generate it.

Note that Zipping tests are skipped until deno fixes an issue with fflate workers.