Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added support for shutting down server and bonjour service / onError callback #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ string or an object containing:
be necessary in a perfect world, but some versions of Windows doesn't
like connecting to a server running a different version of the IPP
protocol than it self (default: `true`)
- `onerror` - Function. Callback for catching [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) errors. Useful for catching server initialization errors if there are network or configuration problems.

Note that the IPP standard specifies port 631 as the default IPP port,
but most IPP clients are fine with connecting to another port.
Expand Down Expand Up @@ -165,6 +166,14 @@ An array of all jobs handled by the printer.

An instance of [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server).

#### `printer.destroy`

A function to shut down the printer server and bonjour service.

```js
printer.destroy();
```

### Class: Job

A job is a readable stream containing the document to be printed. In
Expand Down
24 changes: 24 additions & 0 deletions lib/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var util = require('util')
var os = require('os')
var http = require('http')
var serverDestroy = require('server-destroy')
var Bonjour = require('bonjour')
var ipp = require('ipp-encoder')
var debug = require('debug')(require('../package').name)
Expand All @@ -21,11 +22,34 @@ module.exports = function (printer) {
else server.on('listening', onlistening)
} else {
server = printer.server = http.createServer(onrequest)
server.on('error', onerror)
server.listen(printer.port, onlistening)
}

// Enable destroy method in http server
serverDestroy(server)

// Enhance printer object
printer.destroy = destroy

return printer

function onerror (err) {
if (printer.onerror) {
return printer.onerror(err)
}

throw err
}

function destroy () {
printer.stop()
bonjour.destroy()
server.destroy()

debug('Printer server destroyed')
}

function onrequest (req, res) {
debug('HTTP request: %s %s', req.method, req.url)

Expand Down
3 changes: 3 additions & 0 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function Printer (opts) {
this.server = opts.server
this.fallback = opts.fallback

// Events
this.onerror = opts.onerror

bind(this)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"pump": "^1.0.1",
"rc": "^1.1.6",
"readable-stream": "^2.0.5",
"server-destroy": "^1.0.1",
"unique-concat": "^0.2.2"
},
"devDependencies": {
Expand Down