Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

connect-assets-cdn

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-assets-cdn

A connect-assets helper for uploading built assets to a CDN (like Amazon S3)

  • 0.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

connect-assets-cdn Build Status

A helper module for connect-assets to upload your files to a CDN (like Amazon S3)

Installation

npm install connect-assets-cdn

Usage (Cake Task)

To create a Cake task to upload new assets, follow this example.

/s3.js

require("coffee-script");

var uploader = require("./src/s3AssetUpload");

uploader.upload(console.log, function(err, uploaded) {
    if(err) {
        return console.log("Error uploading: " + err.message);
        process.exit(1);
    }

    console.log("Completed");
    process.exit(0);
});

/src/s3AssetUpload.coffee

express = require 'express'
assets = require "connect-assets"
jsPaths = require "connect-assets-jspaths"

{AssetsCDN} = require "connect-assets-cdn"

# Either create a config file like here or set this to 
# an object with amazon key, secret and bucket values
# Also should have the assetsRoot; e.g. //s3.amazonaws.com/myBucket
config = require "./config"

primeCSS = (assets) ->
    # We only have one file to pre-compile for css
    # You might need to add more for your situation;
    # whatever you reference in your views should be 
    # referenced like this here
    assets.instance.options.helperContext.css "prod"

upload = (log, done) ->

    # Set up a fake express server so we can load connect-assets
    app = express()

    opts = 
        # TODO: Set your assetsRoot
        servePath: config.assetsRoot

    app.use assets opts

    # Prime the CSS files
    primeCSS assets
    # Prime the JS files
    jsPaths assets
    
    # TODO: Set these from your config
    {key, secret, bucket} = config.amazon

    # Create our cdn manager and upload
    cdn = new AssetsCDN {assets, key, secret, bucket, log}

    cdn.upload done

module.exports = {upload}

/Cakefile

task 's3', 'Upload builtAssets', ->
    # Set production NODE_ENV
    currentEnv = process.env
    currentEnv.NODE_ENV = "production"

    server = spawn 'node', ['s3.js'],
      env: currentEnv

    server.stdout.pipe process.stdout
    server.stderr.pipe process.stderr

Usage (Before Starting Server)

# You can also upload new assets before starting the server...

assets = require 'connect-assets'
{AssetsCDN} = require 'connect-assets-cdn'

# Snip ...

app.use assets
	servePath: '//s3.amazonaws.com/S3_Bucket'

# You should probably only upload for production
if process.env.NODE_ENV == "production"

	cdn = new AssetsCDN 
		assets: assets
		key: "S3_Key"
		secret: "S3_Secret"
		bucket: "S3_Bucket"

	cdn.upload (err, uploadedPaths) ->
		throw err if err

		app.listen 3000, (err) ->
			console.log "Server started"

TODO:

  • Better uploader tests

Thanks

Made possible thanks to connect-assets, knox.

Created by Jacob Gable. MIT License; no attribution required.

Keywords

FAQs

Package last updated on 01 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc