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

cordova-code-swap

v2.0.8

Published

Allows fetching updates to js-part of app

Downloads

39

Readme

Cordova code swap

This library makes it possible to swap out the files that are used in your cordova webview.

It contains the code to be run on the client, a CLI and a API for programmatic usage.

Installation

npm install --save cordova-code-swap

Prerequisites

<!-- Required to copy files -->
<plugin name="cordova-plugin-file" spec="4.3.1" />

<!-- Required to download updates -->
<plugin name="cordova-plugin-file-transfer" spec="1.6.1" />

Getting started

The CLI can be installed globally with npm i -g cordova-code-swap, but using the locally installed version through the script tag in package.json is recommended.

Dev mode

1: Run the CLI

$ ccs -i 'www/**/*' -o 'www' -d your-ip-address -r 'www/,' -a

2: Add client side code

Add this as the first code that is run in your cordova app:

var ccs = require('cordova-code-swap');
var ccsOptions = {
	debug: { reloadServer: 'http://your-ip-address:port' } // this address is the output of the CLI
};
document.addEventListener('deviceready', function(){
	ccs.initialize(ccsOptions)
	    .then(function() {
	        // run your code
	    });
});

Production mode

1: Run the CLI

$ ccs -i 'www/**/*' -o 'www' -r 'www/,'

2: Add client side code

Add this as the first code that is run in your cordova app. The code below will make the app download updates on the first run, and install them the next time the app is started.

var ccs = require('cordova-code-swap');
var urlToUpdateEndpoint = 'https://example.com/my-app/';

document.addEventListener('deviceready', function(){
	ccs.initialize()
	    .then(function() { return ccs.install(); })
	    .finally(function() {
	        ccs.lookForUpdates(urlToUpdateEndpoint).then(download => download());
	        // run your code
	    });
});

Client API

ccs.initialize(...) -> Promise

Must be run before anything else!

Checks if a version has been downloaded earlier and should be loaded and returns a promise.

  • Changes the window.location.href to the downloaded .html file if the iframe option is false. The returned promise never resolves so no further client code can be run while it is switching to the installed version.
  • If running with the iframe options set to true, it will resolve with the url to the currently installed update.

ccs.lookForUpdates(...) -> Promise

Checks if the update server has an update and returns a promise.

If it does not, it rejects the promise with the error cordova-code-swap: No new updates found. If it encounters an error, it rejects the promise with the error. If it finds an update, the promise is resolved with a download function.

[lookForUpdates].then(function(download) { return download(); }) -> Promise

Downloads the update and returns a promise.

If it encounters an error, it rejects the promise with the error. If it finished downloading without errors, the promise is resolved with an install function.

The download function can be called when you want to download the updated files. The download function has the property .updateInfo tacked on. This contains the JSON-object it received from the server when looking for updates and can be used to better determine if you want to call download() or not. The download.updateInfo object can e.g. look like this:

{
	"content_url": "https://example.com/my-app-name/", //(or "content_url": "/relative/to/chcp.json")
	"release": "1.3.3",
	"installTime": "afterRestart",
	"min_native_interface": 50
}

[download].then(function(install) { return install(); }) -> Promise

Returns a promise.

Switches to the new version by saving the current entry point in localStorage and changing window.location.href to the downloaded files.

ccs.install() -> Promise

Returns a promise.

If no update is previously downloaded it will reject the promise with the error: cordova-code-swap: Tried to install update, but no updates have been previously downloaded.. If an update is downloaded and pending installation, it will install that update and immediately switch to it.

Client options

Instance options

var instanceOptions = {
	// how many of previously installed versions it should keep. Default is 1.
	backupCount: 1,

	// Setting iframe to true causes .initialize() and .install() to return
	// the url of where the downloaded .html entry point recides.
	// This can be useful if you want to run the downloaded app inside an iframe.
	iframe: false,

	// Debug mode is useful for local development, but must be disabled before
	// submitting to the app-store/google-play
	debug: {
		// The server which it will look for real time updates from
		reloadServer: 'http://my-ip:port/',
		
		// When in debug mode, this function will be called with the
		// new url to use in the iframe each time the app has
		// installed an update
		onIframeUpdate: function(url){},

		// This setting will make the plugin swap out files in the
		// same folder on each update to preserve code-breakpoints.
		preserveBreakpoints: true
	}
}
ccs.initialize(instanceOptions);

Update options

var updateOptions = {
	// Path to your .html file, relative to the www-folder. Default is index.html
	entryFile: 'index.html',

	// Headers that will be sent with the requests to the update server
	headers: {
		key: value
	},

	// Timeout before it aborts requests to the update server in milliseconds.
	// Default is 30 seconds.
	timeout: 30000
}
ccs.lookForUpdates('http://example.com/', updateOptions);

Usage - all options exposed

var ccs = require('cordova-code-swap');
var urlToUpdateEndpoint = 'https://example.com/my-app/';
var myNativeVersion = getNativeVersionSomehow();
var updateOptions = {
	entryFile: 'index.html',
	headers: {
		'x-request-from': 'Cordova-Code-Swap'
	},
	timeout: 10000
};
var instanceOptions = {
	backupCount: 1,
	iframe: true,
	debug: {
		reloadServer: 'http://my-ip:port/',
		onIframeUpdate: function(){},
		preserveBreakpoints: true
	}
};

function progressCallback(percentageAsInt, downloadedFileName) {
    console.log(percentageAsInt + '% done', 'Downloaded ' + downloadedFileName);
};

document.addEventListener('deviceready', function(){
	ccs.initialize(instanceOptions) // must always be run before anything else
      .then(function() {
          return ccs.lookForUpdates(urlToUpdateEndpoint, updateOptions)
      })
      .catch(function(err) { /*handle the error*/ })
      .then(function(download) {
          if (download.updateInfo.min_native_interface > myNativeVersion) {
              throw new Error('Update received from the server requires newer native version of the app to be installed.');
          }
          return download(progressCallback);
      })
      .catch(function(downloadErr) { /*handle the error*/ })
      .then(function(install) { return install() })
      .catch(function(installErr) { /*handle the error*/ });

	// optional
	ccs.install(); // <-- can be used after e.g. restarting the app if there is a downloaded update that has not been installed yet.
});

Programmatic API

If you don't want to use the CLI, but instead want to e.g. use it in a build system, this library offers a programmatic interface.

var ccs = require('cordova-code-swap/utils/createCCSFiles');
var ccsOptions = {
	// the config that will be merged into chcp.json
	config: {},

    // "a" will be replaced with "b" in the generated manifest file paths
    replace: ['a', 'b'],

    // the ip:port the debug server will be run on, watches files and generates new manifest upon change
    dev: '',

    // make the app update every time the manifest updates (when running with dev option)
    autoUpdate: false
};
var inputGlob = ['www/**/*']; // array of minimatch strings that will be included in the manifest file
var outputFolder = 'www'; // path to where chcp.json and chcp.manifest will be output to

ccs(inputGlob, outputFolder, ccsOptions) // returns promise that resolves with an emit function that tells the app to update
	.then(function(emitUpdateNotification) {
		// useful when not using ccsOptions.autoUpdate
		emitUpdateNotification();
	});