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

attache-upload

v2.1.1

Published

Upload files to your attache server.

Downloads

926

Readme

attache-upload.js

NPM version build status js-standard-style

Upload files to your attache server.

Example

import {upload, presign} from 'attache-upload'

presign(presign_url)
  .then((presignResponse) => {

    // presignResponse:
    //
    // {
    //   url: "http://path_to_your_attache_server/upload",
    //   uuid: "5f0d4d62-e082-4cdf-a143-26258de59c47",
    //   expiration: 1461649282,
    //   hmac: "fd89637821afca1787dfe458be29bc87f0366122"
    // }

    return upload(presignResponse, fileObject)
  })
  .then((uploadResponse) => {

    // uploadResponse:
    //
    // {
    //   path: "54/4d/15/14/b4/09/29/01/36/42/2f/e2/3f/f0/42/15/some_file.jpg",
    //   content_type: "image/jpeg",
    //   geometry: "300x300",
    //   bytes: 19804
    // }

  })
  .catch((err) => {
    // Handle error
  })

Install

$ npm install --save attache-upload

API Documentation

presign(options)

  • presign_url - required, a URL to perform a presign request.
  • token - optional, X-CSRF-Token value.

On success, this request will return:

  • url - the URL to the /upload API of your attache serve
  • uuid - a uuid string
  • expiration - a unix timestamp of a future time
  • hmac - the HMAC-SHA1 of your SECRET_KEY
presign(presign_url, token)
  .then((presignResponse) => {
    // {
    //   url: "http://path_to_your_attache_server/upload",
    //   uuid: "5f0d4d62-e082-4cdf-a143-26258de59c47",
    //   expiration: 1461649282,
    //   hmac: "fd89637821afca1787dfe458be29bc87f0366122"
    // }
  })

upload(options)

  • presignResponse - required, response object passed in from presign request.
  • fileObject - required.
    An object containing a uid and file property.
    We use this uid as a reference to this object's XHR request, which can then be aborted at a later stage using abortXHRRequest('uid')
{
    uid: 'some_generated_uid', // String
    file: FILE // File object
}
  • onProgress - optional, onProgress function.

On success, this request will return:

  • path - a unique path for the uploaded file
  • content_type
  • geometry
  • bytes

function customProgressHandler (progressEvent, file) {
  console.log('Uploading ' + file.name + ' @ ' + progressEvent.percent + '%')
  //=> 'Uploading foo.jpg @ 100%'
}

presign(presign_url)
  .then((presignResponse) => {
    return upload(presignResponse, fileObject, customProgressHandler)
  })
  .then((uploadResponse) => {
    // {
    //   path: "54/4d/15/14/b4/09/29/01/36/42/2f/e2/3f/f0/42/15/some_file.jpg",
    //   content_type: "image/jpeg",
    //   geometry: "300x300",
    //   bytes: 19804
    // }
  })

getXHRRequests()

To access all existing XHR requests use getXHRRequests().

import {getXHRRequests} from 'attache-upload'

getXHRRequests()
// {
//   'some_uid': request(){},
//   'some_other_request':  request(){}
// }

abortXHRRequest(String)

To abort an existing XHR requests use destroyXHRRequest() passing in the id of the request.

import {getXHRRequests, abortXHRRequest} from 'attache-upload'

getXHRRequests()
// {
//   'some_uid': request(){},
//   'some_other_request':  request(){}
// }

abortXHRRequest('some_uid')

getXHRRequests()
// {
//   'some_other_request':  request(){}
// }

Error handling

Both presign the upload methods will return a custom error objects if either promise is rejected.
The XHR requests for each method will return a custom responseStatus error message if the response status is not between 200 and 300.
This allows us to check for specific errors in our upload process.

The custom error objects look like this:

{
  error: {original error object},
  message: 'Not Authorised'
  name: 'uploadRequest'
}

The 3 custom error names we cater for are:

  • presignRequest - Rejected presign
  • uploadRequest - Rejected upload
  • responseStatus - A failing XHR response status

All other errors should be left to bubble up and logged to the console.

Example

import {upload, presign} from 'attache-upload'

presign(presign_url)
  .then((presignResponse) => {
    return upload(presignResponse, fileObject)
  })
  .then((uploadResponse) => {
    return doSomethingWithResponse(uploadResponse)
  })
  .catch((err) => {
    // check is the error was with our upload process
    const {name} = err
    if (name === 'presignRequest' || name === 'uploadRequest' || name === 'responseStatus') {
      doSomethingWithErrorMessage(err)
    } else {
      // log and throw the error
      console.error(err)
      throw err
    }
  })

Generate a uid for your file object

Some files may have the same name, so it would be great if we had a unique way of identifying them and their XHR request. Creating a uid for your file object is as simple as:

import uid from 'uid'

/**
 * generateUniqueID
 * @param {String} name
 * @return {String}
 */

function generateUniqueID (name) {
  return uid(10) + '_' + name
}

const myUID = generateUniqueID(file.name)
//> hbswt489ts_image.jpg

Development

Build

Compile src scripts to lib:

$ npm run build

Tests

To run the test suite, first install the dependencies, then run npm test:

$ npm install
$ npm test