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

upload2cdn

v1.7.3

Published

upload files to cdn

Downloads

8

Readme

upload2cdn

Intro

Simple tool to upload files to cdn

Environment requirement

node >= 8

Install

npm install upload2cdn

Notice

This tool does not provide a service as uploading to cdn.

In fact, it actually depends on such service.

Dependency

upload2cdn relies on the existence a cdn object with an upload method described as below.

type cdnUrl = string
interface cdnRes {
  [localPath: string]: cdnUrl
}
// this is what cdn package looks like
interface cdn {
  upload: (localPaths: string[]) => Promise<cdnRes>
}

If typescript syntax is unfamiliar, here is another description in vanilla javascript.

/**
 * @param {string[]} localPath: list of paths of local files
 * @return Promise<cdnRes>: resolved Promise with structure like {localPath: cdnUrl}
 */
function upload(localPath) {
  // code
}
const cdn = {
  upload
}

Usage

const { upload } = require('upload2cdn')
const cdn = require('some-cdn-package')
upload(cdn, {
  src: path.resolve('./src')
  dist: path.resolve('./dist')
  assets: path.resolve('./src')
})

src, dist, assets work best with absolute path!

Configuration

upload(cdn, option)

For option, valid fields are showed below

src: string

Where your template files would be (with reference to local js/css files)

assets: string

Where all assets (js/css/images) are, most likely the same as src property.

[dist]: string

Where to emit newer template with cdn reference.

Only use this when there is a need to separate origin templates with ones using cdn reference.

Happens when original templates are static, aka not produced by any building tools.

[urlCb]: (cdnUrl: string) => string

Further alter cdn url here.

const urlCb = input => input.replace(/^https/, 'http')

[passToCdn]: object

Extra config to pass to cdn.upload method. Something Like cdn.upload(location, passToCdn).

const cdn = require("any-cdn-package");
// If the cdn package can set up https through a config object
const passToCdn = {
  https: true
}
cdn.upload(files, passToCdn);

// then you can use passToCdn object to turn on https just like this
const { upload } = require("upload2cdn");
upload(cdn, {
  passToCdn
});

[enableCache=true]: boolean

Using cache to speed up, aka skip some uploading work.

[cacheLocation]: string

Place to put cache file.

Use this only when you want to manage the cache file by VCS, which is unlikely.

[onFinish]: () => any

Called when things are done.

Or you can simply await for upload, and then do your own thing.

[beforeUpload]: (fileContent: string, fileLocation: string) => string

Compression can be done here. Two arguments are fileContent and fileLocation (with extension name of course). You need to return the compression result as string.

// if you want to compress js before upload
const UglifyJs = require('uglify-js')
const path = require('path')
const beforeUpload = (content, location) => {
  if (path.extname(location) === '.js') {
    return UglifyJs.minify(content).code
  }
  return content
}

[sliceLimit=10]: number

Uploading files is not done by once. By using sliceLimit, you can limit the number of files being uploaded at once.

[files]: string[]

When using, it basically means overriding assets field, and only use the files you provide as assets.

Should be an array of absolute locations.

[preProcess]: (fileContent: string, fileLocation: string) => string

Give you the chance to update files' content before any further process. Runs at the very beginning (including all valid files in src and assets), prior to any upload related process.

[extraTypes]: string[]

Extra types needed to cdn. Like json (Do not prefix with .)

[shouldReplace]: (slice: string, fileLocation: string) => boolean

Should replace the matching local path with the cdn url. slice is a 20 characters + matched path + 20 characters

[shapeExtra]: (extra: string[]) => string[]

If there are extraTypes, then will try to upload them twice. The meaning of the second time is to handle the potential dependency among them. And shapeExtra gives you a chance to change the order of those extra files.

Probably do not need to use this.

License

MIT

Copyright (c) 2017-present, Yuchen Liu