New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

reload

Package Overview
Dependencies
Maintainers
3
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reload - npm Package Compare versions

Comparing version 1.1.7 to 2.0.0

expressSampleApp/package-lock.json

39

CHANGELOG.md

@@ -0,1 +1,40 @@

2.0.0 / 2017-07-09
------------------
See V2.0.0 PR https://github.com/alallier/reload/pull/118
### Added
* Added object based parameters (Issue [#77](https://github.com/alallier/reload/issues/77) / Originally solved in PR [#101](https://github.com/alallier/reload/pull/101) and refactored in [#104](https://github.com/alallier/reload/pull/104))
* Added port configuration (Issue [#60](https://github.com/alallier/reload/issues/60) / Originally solved in PR [#68](https://github.com/alallier/reload/pull/68) and refactored in [#104](https://github.com/alallier/reload/pull/104))
* Added timestamp to reload command line reloading (Issue [#7](https://github.com/alallier/reload/issues/7) / PR [#78](https://github.com/alallier/reload/pull/78))
* Added node 8 support (Issue [#106](https://github.com/alallier/reload/issues/106) / PR [#119](https://github.com/alallier/reload/pull/119))
* Added table of contents to README (Issue [#103](https://github.com/alallier/reload/issues/103) / PR [#105](https://github.com/alallier/reload/pull/105))
* Added return API to README (PR [#121](https://github.com/alallier/reload/pull/121))
### Modified
* Abstracted reload call to an index.js file. Index file now calls `reload.js` source file. This is to abstract the reload command line calling with a third argument that is now private and not apart of the public API (PR [#117](https://github.com/alallier/reload/pull/117))
* Update dependencies to latest and add package-lock.json files (PR [#109](https://github.com/alallier/reload/pull/109))
* Audited and refactored return API (Issue [#120](https://github.com/alallier/reload/issues/120) / PR [#121](https://github.com/alallier/reload/pull/121))
### Removed
* Drop support for server and just use ports (Issue [#102](https://github.com/alallier/reload/issues/102) / PR [#104](https://github.com/alallier/reload/pull/104))
* Removed support of node 0.1 and 0.12 (Issue [#73](https://github.com/alallier/reload/issues/73) / PR [#86](https://github.com/alallier/reload/pull/86))
* Separate server and app initialization into two parts. (This was originally fixed in PR [#71](https://github.com/alallier/reload/pull/71) but was reversed in PR [#104](https://github.com/alallier/reload/pull/104) when the decision to drop server was made.)
### API Breaking Changes
This version makes breaking changes to the reload API. The only required argument to reload now is `app`. This makes reload a lot [easier](https://github.com/jprichardson/reload/pull/104) to use. Reload takes a maximum of two arguments `app` and an `opts` (options) object with the following optional parameters, `port`, `route`, and `verbose`. Reload runs on default port `9856` unless otherwise specified in the `opts` object.
#### How to upgrade from Version 1 to Version 2
Before Version 2 reload always attached to your server's port by passing the server in a argument to reload. We have now dropped support for server and reload runs on ports only. Reload now has one required parameter `app` and one optional parameter `opts` an object of reload options. Below are two upgrade examples for the only two possible 1.x configurations.
Upgrade with required arguments: `reload(server, app)` becomes `reload(app)`
Upgrade with both required arguments and the one optional argument: `reload(server, app, true)` becomes `reload(app, {verbose: true})`
It is important to note that reload **only** uses ports now. So upgrading using the examples above will have reload run on it's default port `9856`. If you want to run reload on a different port you need to specify a port in the `opts` object like: `reload(app, {port: 9852})`
Most people can just use the default settings, allowing `reload(app)` to work in most cases.
Please refer to the full API in the [README](README.md#api-for-express).
1.1.7 / 2017-06-28

@@ -2,0 +41,0 @@ ------------------

17

expressSampleApp/package.json

@@ -9,19 +9,12 @@ {

"dependencies": {
"express": "^4.14.0",
"path": "^0.12.7",
"reload": "*",
"body-parser": "^1.15.2",
"morgan": "^1.7.0",
"supervisor": "^0.11.0",
"watch": "0.19.1"
"express": "^4.15.3",
"body-parser": "^1.17.2",
"morgan": "^1.8.2",
"supervisor": "^0.12.0"
},
"devDependencies": {},
"private": true,
"repository": {
"private-repo": "git+ssh://somewhere:port/folder/on/server"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
"start": "./node_modules/.bin/supervisor -k -e html -i .git/,node_modules/ -- server.js"
}
}
var express = require('express')
var http = require('http')
var path = require('path')
var reload = require('reload')
var reload = require('../../reload')
var bodyParser = require('body-parser')

@@ -23,3 +23,3 @@ var logger = require('morgan')

// Reload code here
reload(server, app)
reload(app)

@@ -26,0 +26,0 @@ server.listen(app.get('port'), function () {

(function refresh () {
var verboseLogging = false
var socketUrl = window.location.origin.replace(/^http(s?):\/\//, 'ws$1://')
var socketUrl = window.location.origin.replace() // This is dynamically populated by the reload.js file before it is sent to the browser
var socket

@@ -5,0 +5,0 @@

@@ -20,2 +20,9 @@ var express = require('express')

var reloadOpts = {
port: port,
verbose: verbose
}
var time
var router = express.Router()

@@ -55,7 +62,7 @@

// Reload call and configurations.
reload(server, app, verbose)
reload(app, reloadOpts, server)
server.listen(app.get('port'), function () {
if (!fs.existsSync(runFile)) {
fs.writeFile(runFile, '')
fs.writeFileSync(runFile)

@@ -67,3 +74,4 @@ // If openBrowser, open the browser with the given start page above, at a hostname (localhost default or specified).

} else {
console.log(clc.green('restarting...'))
time = new Date()
console.log(clc.green('Server restarted at ' + time.toTimeString().slice(0, 8)))
}

@@ -70,0 +78,0 @@ })

@@ -1,29 +0,89 @@

var path = require('path')
var fs = require('fs')
module.exports = function reload (app, opts, server) {
// Requires
var path = require('path')
var fs = require('fs')
var RELOAD_FILE = path.join(__dirname, './reload-client.js')
// Parameters variables
var httpServerOrPort
var expressApp
var verboseLogging
var port
module.exports = function reload (httpServer, expressApp, verboseLogging) {
// Application variables
var RELOAD_FILE = path.join(__dirname, './reload-client.js')
var reloadCode = fs.readFileSync(RELOAD_FILE, 'utf8')
var route
var connections = new Set()
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({ server: httpServer })
// Websocket server variables
var ws = require('ws')
var WebSocketServer = ws.Server
var wss
wss.on('connection', (ws) => {
connections.add(ws)
ws.on('close', function () {
connections.delete(ws)
})
// General variables
var socketPortSpecified
var argumentZero = arguments[0]
var reloadJsMatch
if (verboseLogging) {
console.log('Reload client connected to server')
opts = opts || {}
if (arguments.length > 0 && (typeof (argumentZero) === 'object' || typeof (argumentZero) === 'function')) {
if (typeof (argumentZero) === 'object') { // If old arguments passed handle old arguments, the old arguments and their order were: httpServerOrPort, expressApp, and verboseLogging
console.warn('Deprecated Warning: You supplied reload old arguments, please upgrade to the new parameters see: https://github.com/alallier/reload/tree/master#api-for-express')
if (arguments.length < 2) {
throw new TypeError('Lack of/invalid arguments provided to reload. It is recommended to update to the new arguments anyways, this would be a good time to do so.', 'reload.js', 7)
}
httpServerOrPort = argumentZero
expressApp = arguments[1]
verboseLogging = arguments[2]
route = '/reload/reload.js'
} else { // Setup options or use defaults
expressApp = argumentZero
port = opts.port || 9856
route = opts.route
if (route) {
// If reload.js is found in the route option strip it. We will concat it for user to ensure no case errors or order problems.
reloadJsMatch = route.match(/reload\.js/i)
if (reloadJsMatch) {
route = route.split(reloadJsMatch)[0]
}
/*
* Concat their provided path (minus `reload.js` if they specified it) with a `/` if they didn't provide one and `reload.js. This allows for us to ensure case, order, and use of `/` is correct
* For example these route's are all valid:
* 1. `newRoutePath` -> Their route + `/` + reload.js
* 2. `newRoutePath/` -> Their route + reload.js
* 3. `newRoutePath/reload.js` -> (Strip reload.js above) so now: Their route + reload.js
* 4. `newRoutePath/rEload.js` -> (Strip reload.js above) so now: Their route + reload.js
* 5. `newRoutePathreload.js` -> (Strip reload.js above) so now: Their route + `/` + reload.js
* 6. `newRoutePath/reload.js/rEload.js/... reload.js n number of times -> (Strip above removes all reload.js occurrences at the end of the specified route) so now: Their route + 'reload.js`
*/
route = route + (route.slice(-1) === '/' ? '' : '/') + 'reload.js'
} else {
route = '/reload/reload.js'
}
verboseLogging = opts.verbose === true || opts.verbose === 'true' || false
if (port) {
socketPortSpecified = port
httpServerOrPort = port
}
if (server) {
socketPortSpecified = null
httpServerOrPort = server
}
}
})
} else {
throw new TypeError('Lack of/invalid arguments provided to reload', 'reload.js', 7)
}
// Application setup
if (verboseLogging) {
reloadCode = reloadCode.replace('verboseLogging = false', 'verboseLogging = true')
}
reloadCode = reloadCode.replace('window.location.origin.replace()', 'window.location.origin.replace(/(^http(s?):\\/\\/)(.*:)(.*)/,' + (socketPortSpecified ? '\'ws$2://$3' + socketPortSpecified : '\'ws$2://$3$4') + '\')')
expressApp.get('/reload/reload.js', function (req, res) {
expressApp.get(route, function (req, res) {
res.type('text/javascript')

@@ -33,24 +93,33 @@ res.send(reloadCode)

// Websocket server setup
// Use custom user specified port
if (socketPortSpecified) {
wss = new WebSocketServer({ port: httpServerOrPort })
} else { // Attach to server, using server's port. Kept here to support legacy arguments.
wss = new WebSocketServer({ server: httpServerOrPort })
}
wss.on('connection', (ws) => {
if (verboseLogging) {
console.log('Reload client connected to server')
}
})
function sendMessage (message) {
if (verboseLogging) {
console.log('Sending message to ' + (connections.size) + ' connections: ' + message)
console.log('Sending message to ' + (wss.clients.size) + ' connection(s): ' + message)
}
for (let conn of connections) {
conn.send(message, function (error) {
if (error) {
console.error(error)
}
})
}
wss.clients.forEach(function each (client) {
if (client.readyState === ws.OPEN) {
client.send(message)
}
})
}
// Return an object, so that the user can manually reload the server by calling the returned function reload. Using the web socket connection from above, we provide a function called reload which passes the command 'reload' to the function sendMessage. sendMessage sends the message 'reload' over the socket (if the socket is connected) to the client. The client then recieves the messages checks to see if the message is reload and then reloads the page.
// Return an object, right now it contains only a function reload. When this function is called it calls the `sendMessage` function with the message 'reload' which reloads all connected clients.
return {
'connections': connections,
'server': reload,
'reload': function () {
sendMessage('reload')
},
'sendMessage': sendMessage
}
}
}
{
"name": "reload",
"version": "1.1.7",
"version": "2.0.0",
"description": "Node.js module to refresh and reload your code in your browser when your code changes. No browser plugins required.",

@@ -22,9 +22,8 @@ "repository": {

"dependencies": {
"supervisor": "~0.11.0",
"supervisor": "~0.12.0",
"commander": "~2.9.0",
"express": "~4.14.0",
"cli-color": "~1.1.0",
"express": "~4.15.3",
"cli-color": "~1.2.0",
"open": "~0.0.5",
"death": "~1.0.0",
"ws": "~1.1.1",
"ws": "~3.0.0",
"minimist": "~1.2.0"

@@ -35,3 +34,3 @@ },

},
"main": "./lib/reload.js",
"main": "./index.js",
"scripts": {

@@ -38,0 +37,0 @@ "test": "npm run standard",

@@ -5,3 +5,3 @@ reload

[![Build Status](https://travis-ci.org/alallier/reload.svg?branch=master)](https://travis-ci.org/alallier/reload)
[![Build status](https://ci.appveyor.com/api/projects/status/4uuui532bpht2ff7?svg=true)](https://ci.appveyor.com/project/alallier/reload)
[![Build status](https://ci.appveyor.com/api/projects/status/4uuui532bpht2ff7/branch/master?svg=true)](https://ci.appveyor.com/project/alallier/reload/branch/master)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

@@ -12,2 +12,21 @@ [![NPM version](https://img.shields.io/npm/v/reload.svg)](https://www.npmjs.com/package/reload)

Table Of Contents
---
* [Why](#why)
* [How does it work?](#how-does-it-work)
* [Installation](#installation)
* [Two ways to use reload](#two-ways-to-use-reload)
* [Using reload in Express](#using-reload-in-express)
* [Express Example](#express-example)
* [Manually firing server-side reload events](#manually-firing-server-side-reload-events)
* [API for Express](#api-for-express)
* [Parameters](#parameters)
* [Table of reload parameters](#table-of-reload-parameters)
* [Table of options for reload opts parameter](#table-of-options-for-reload-opts-parameter)
* **[Updating to version 2](#upgrading-to-version-2)**
* [Returns](#returns)
* [Using reload as a command line application](#using-reload-as-a-command-line-application)
* [Usage for Command Line Application](#usage-for-command-line-application)
* [License](#license)
Why?

@@ -28,2 +47,7 @@ ----

Updating from version 2 from version 1
---
Looking for a quick guide to updating reload to version 2? Please refer to our update section [below](#upgrading-to-version-2).
Installation

@@ -56,3 +80,3 @@ ---

var path = require('path')
var reload = require('reload')
var reload = require('../../reload')
var bodyParser = require('body-parser')

@@ -67,5 +91,5 @@ var logger = require('morgan')

app.use(logger('dev'))
app.use(bodyParser.json()) //parses json, multi-part (file), url-encoded
app.use(bodyParser.json()) // Parses json, multi-part (file), url-encoded
app.get('/', function(req, res) {
app.get('/', function (req, res) {
res.sendFile(path.join(publicDir, 'index.html'))

@@ -77,7 +101,8 @@ })

// Reload code here
reload(server, app)
reload(app);
server.listen(app.get('port'), function(){
console.log("Web server listening on port " + app.get('port'));
});
server.listen(app.get('port'), function () {
console.log('Web server listening on port ' + app.get('port'))
})
```

@@ -93,3 +118,3 @@

<body>
<h1>Reload Express Sample App12</h1>
<h1>Reload Express Sample App</h1>
<!-- All you have to do is include the reload script and have it be on every page of your project -->

@@ -108,3 +133,3 @@ <script src="/reload/reload.js"></script>

```javascript
reloadServer = reload(server, app);
reloadServer = reload(app);
watch.watchTree(__dirname + "/public", function (f, curr, prev) {

@@ -119,9 +144,40 @@ // Fire server-side reload event

```
reload(httpServer, expressApp, [verbose])
reload(app, opts)
```
- `httpServer`: The Node.js http server from the module `http`.
- `expressApp`: The express app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express.
- `verbose`: If set to true, will show logging on the server and client side
#### Parameters
##### Table of reload parameters
| Parameter Name | Type | Description | Optional |
|----------------|----------|---------------------------------------------------------------------------------------------------------------------|----------|
| app | {object} | The app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express. | |
| opts | {object} | An optional object of options for reload. Refer to table [below](#table-of-options-for-reload-opts-parameter) on possible options | ✓ |
##### Table of options for reload opts parameter
| Parameter Name | Type | Description | Optional | Default |
|----------------|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|---------|
| port | {number} | Port to run reload on. | ✓ | `9856` |
| route | {string} | Route that reload should use to serve the client side script file. Changing the route will require the script tag URL to change. Reload will always strip any occurrence of reload.js and append reload.js for you. This is to ensure case, order, and use of / is correct. For example specifying newRoutePath as the route will give reload a route of newRoutePath/reload.js. (Recommend not modifying). | ✓ | `reload` |
| verbose | {boolean} | If set to true, will show logging on the server and client side. | ✓ | `false` |
##### Upgrading to version 2
Reload dropped support for server. The only required parameter for reload is `app`.
* Upgrade with required arguments: `reload(server, app)` becomes `reload(app)`
* Upgrade with required arguments and the one optional argument: `reload(server, app, true)` becomes `reload(app, {verbose: true})`
To read more about the API breaking changes please refer to the [changelog](CHANGELOG.md#api-breaking-changes).
#### Returns
An **object** containing:
| Name | Type | Description |
|--------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| reload | function | A function that when called reloads all connected clients. For more information see [manually firing server-side reload events](#manually-firing-server-side-reload-events). |
Using reload as a command line application

@@ -161,4 +217,4 @@ ---

-p, --port [port] The port to bind to. Can be set with PORT env variable as well. Defaults to 8080
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
-v, --verbose Turns on logging on the server and client side. Defaults to false.
-s, --start-page [start-page] Specify a start page. Defaults to index.html.
-v, --verbose Turns on logging on the server and client side. Defaults to false.
```

@@ -179,2 +235,2 @@

Alexander J. Lallier <mralexlallier@gmail.com>
Alexander J. Lallier <mralexlallier@gmail.com>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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