2015-08-19 16:55:11 +00:00
# autoUpdater
2013-08-14 22:43:35 +00:00
2015-03-04 22:36:56 +00:00
**This module has only been implemented for OS X.**
2015-06-26 04:17:52 +00:00
Check out [atom/grunt-electron-installer ](https://github.com/atom/grunt-electron-installer )
2015-07-23 16:57:42 +00:00
to build a Windows installer for your app.
2014-05-05 02:07:15 +00:00
2015-07-23 16:57:42 +00:00
The `auto-updater` module is a simple wrapper around the
2014-05-06 03:22:03 +00:00
[Squirrel.Mac ](https://github.com/Squirrel/Squirrel.Mac ) framework.
2014-05-05 02:07:15 +00:00
2014-07-25 16:14:49 +00:00
Squirrel.Mac requires that your `.app` folder is signed using the
[codesign ](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/codesign.1.html )
utility for updates to be installed.
2014-05-05 02:07:15 +00:00
## Squirrel
Squirrel is an OS X framework focused on making application updates **as safe
and transparent as updates to a website**.
Instead of publishing a feed of versions from which your app must select,
Squirrel updates to the version your server tells it to. This allows you to
intelligently update your clients based on the request you give to Squirrel.
Your request can include authentication details, custom headers or a request
body so that your server has the context it needs in order to supply the most
suitable update.
The update JSON Squirrel requests should be dynamically generated based on
2015-07-23 16:57:42 +00:00
criteria in the request and whether an update is required. Squirrel relies
on server-side support to determine whether an update is required. See
2014-05-05 02:07:15 +00:00
[Server Support ](#server-support ).
2015-07-23 16:57:42 +00:00
Squirrel's installer is designed to be fault tolerant and ensures that any
2014-05-05 02:07:15 +00:00
updates installed are valid.
## Update Requests
Squirrel is indifferent to the request the client application provides for
update checking. `Accept: application/json` is added to the request headers
because Squirrel is responsible for parsing the response.
For the requirements imposed on the responses and the body format of an update
2015-07-23 16:57:42 +00:00
response, see [Server Support ](#server-support ).
2014-05-05 02:07:15 +00:00
Your update request must *at least* include a version identifier so that the
server can determine whether an update for this specific version is required. It
2015-07-23 16:57:42 +00:00
may also include other identifying criteria, such as operating system version or
2014-05-05 02:07:15 +00:00
username, to allow the server to deliver as fine grained an update as you
would like.
How you include the version identifier or other criteria is specific to the
server that you are requesting updates from. A common approach is to use query
parameters, like this:
```javascript
2015-09-04 20:44:40 +00:00
// In the main process
2014-05-05 02:07:15 +00:00
var app = require('app');
var autoUpdater = require('auto-updater');
autoUpdater.setFeedUrl('http://mycompany.com/myapp/latest?version=' + app.getVersion());
```
## Server Support
Your server should determine whether an update is required based on the
[Update Request ](#update-requests ) your client issues.
2015-07-23 16:57:42 +00:00
If an update is required, your server should respond with a status code of
2014-05-05 02:07:15 +00:00
[200 OK ](http://tools.ietf.org/html/rfc2616#section-10.2.1 ) and include the
[update JSON ](#update-json-format ) in the body. Squirrel **will** download and
install this update, even if the version of the update is the same as the
currently running version. To save redundantly downloading the same version
multiple times your server must not inform the client to update.
If no update is required your server must respond with a status code of
[204 No Content ](http://tools.ietf.org/html/rfc2616#section-10.2.5 ). Squirrel
will check for an update again at the interval you specify.
## Update JSON Format
When an update is available, Squirrel expects the following schema in response
to the update request provided:
```json
{
"url": "http://mycompany.com/myapp/releases/myrelease",
"name": "My Release Name",
"notes": "Theses are some release notes innit",
2015-07-15 14:34:33 +00:00
"pub_date": "2013-09-18T12:29:53+01:00"
2014-05-05 02:07:15 +00:00
}
```
2015-07-23 16:57:42 +00:00
The only required key is "url"; the others are optional.
2014-05-05 02:07:15 +00:00
Squirrel will request "url" with `Accept: application/zip` and only supports
installing ZIP updates. If future update formats are supported their MIME type
will be added to the `Accept` header so that your server can return the
appropriate format.
2015-07-23 16:57:42 +00:00
`pub_date` (if present) must be formatted according to ISO 8601.
2013-08-14 22:43:35 +00:00
2015-08-30 22:07:36 +00:00
## Update server implementations
[Nuts ](https://github.com/GitbookIO/nuts ) is an open source implementation of the update server described above, it integrates beautifully with GitHub releases. Nuts manages downloads and updates, it’ s compatible with `Squirrel.Mac` and `Squirrel.Windows` so you get cross-platform support out of the box.
2015-08-19 16:55:11 +00:00
## Events
The `autoUpdater` object emits the following events:
### Event: 'error'
Returns:
2015-04-07 16:21:29 +00:00
* `event` Event
* `message` String
2015-07-23 16:57:42 +00:00
Emitted when there is an error while updating.
2015-04-07 16:21:29 +00:00
2015-08-19 16:55:11 +00:00
### Event: 'checking-for-update'
2013-08-14 22:43:35 +00:00
2015-07-28 15:51:41 +00:00
Emitted when checking if an update has started.
2013-08-14 22:43:35 +00:00
2015-08-19 16:55:11 +00:00
### Event: 'update-available'
2014-02-02 12:37:46 +00:00
2015-07-23 16:57:42 +00:00
Emitted when there is an available update. The update is downloaded
2014-02-02 12:37:46 +00:00
automatically.
2015-08-19 16:55:11 +00:00
### Event: 'update-not-available'
2013-08-14 22:43:35 +00:00
2014-02-02 12:37:46 +00:00
Emitted when there is no available update.
2013-08-14 22:43:35 +00:00
2015-08-19 16:55:11 +00:00
### Event: 'update-downloaded'
Returns:
2013-08-14 22:43:35 +00:00
* `event` Event
2014-02-02 12:37:46 +00:00
* `releaseNotes` String
* `releaseName` String
* `releaseDate` Date
* `updateUrl` String
2013-08-14 22:43:35 +00:00
* `quitAndUpdate` Function
2015-08-24 12:38:29 +00:00
Emitted when an update has been downloaded. Calling `quitAndUpdate()` will
restart the application and install the update.
2013-08-14 22:43:35 +00:00
2015-08-19 16:55:11 +00:00
## Methods
The `autoUpdater` object has the following methods:
### `autoUpdater.setFeedUrl(url)`
2013-08-14 22:43:35 +00:00
* `url` String
2015-07-23 16:57:42 +00:00
Set the `url` and initialize the auto updater. The `url` cannot be changed
2014-02-02 12:37:46 +00:00
once it is set.
2013-08-14 22:43:35 +00:00
2015-08-19 16:55:11 +00:00
### `autoUpdater.checkForUpdates()`
2013-08-14 22:43:35 +00:00
2015-07-23 16:57:42 +00:00
Ask the server whether there is an update. You must call `setFeedUrl` before
2014-02-02 12:37:46 +00:00
using this API.