Installations
npm install node-upnp-utils
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
20.11.1
NPM Version
10.2.4
Score
73.1
Supply Chain
98.4
Quality
78.1
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
futomi
Download Statistics
Total Downloads
10,577
Last Day
6
Last Week
152
Last Month
712
Last Year
4,427
GitHub Statistics
18 Stars
31 Commits
3 Forks
4 Watching
1 Branches
4 Contributors
Bundle Size
105.23 kB
Minified
24.47 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.3
Package Id
node-upnp-utils@1.0.3
Unpacked Size
49.13 kB
Size
11.61 kB
File Count
5
NPM Version
10.2.4
Node Version
20.11.1
Publised On
30 Jun 2024
Total Downloads
Cumulative downloads
Total Downloads
10,577
Last day
100%
6
Compared to previous day
Last week
74.7%
152
Compared to previous week
Last month
165.7%
712
Compared to previous month
Last year
135.5%
4,427
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
node-upnp-utils
The node-upnp-utils is a SSDP (Simple Service Discovery Protocol) client implementation. It allows you to discover UPnP devices or services in the same subnet and to fetch device descriptions (XML) from the discovered devices.
Note that this module does not work well on Windows by the default Windows setting. See the section "Running on Windows" for detail.
Dependencies
Installation
$ npm install xml2js
$ npm install node-upnp-utils
Table of Contents
- Usage
- Methods
- Events
- The structure of discovered device
- Running on Windows
- Release Note
- References
- License
Usage
Discover UPnP devices or services
1const upnp = require('node-upnp-utils'); 2 3(async () => { 4 // discover devices 5 const device_list = await upnp.discover(); 6 7 for (const device of device_list) { 8 console.log('------------------------------------'); 9 console.log(' * ' + device['address']); 10 if (device['description']) { 11 console.log(' * ' + device['description']['device']['manufacturer']); 12 console.log(' * ' + device['description']['device']['modelName']); 13 } 14 console.log(' * ' + device['headers']['LOCATION']); 15 console.log(' * ' + device['headers']['USN']); 16 } 17})();
The sample code above shows the discovered devices or services as follows:
------------------------------------
* 192.168.11.29
* Sony
* BRAVIA 4K GB
* http://192.168.11.29:8008/ssdp/device-desc.xml
* uuid:2ea85932-fa00-ddd4-4a18-8209f8eade79::upnp:rootdevice
------------------------------------
* 192.168.11.37
* SHARP
* AQUOS-4KTVJ17
* http://192.168.11.37:8008/ssdp/device-desc.xml
* uuid:457d6192-c077-27eb-53f6-a62b2fac68e6::upnp:rootdevice
------------------------------------
* 192.168.11.40
* Justin Maggard
* Windows Media Connect compatible (MiniDLNA)
* http://192.168.11.40:8200/rootDesc.xml
* uuid:4d696e69-444c-164e-9d41-000c294ea6f0::upnp:rootdevice
Discover and monitor UPnP devices or services
1const upnp = require('node-upnp-utils'); 2 3// Set an event listener for 'added' event 4upnp.on('added', (device) => { 5 // This callback function will be called whenever a device or a service is found. 6 console.log('[added] ------------------------------------'); 7 console.log(' * ' + device['address']); 8 if (device['description']) { 9 console.log(' * ' + device['description']['device']['friendlyName']); 10 } 11 console.log(' * ' + device['headers']['USN']); 12}); 13 14// Set an event listener for 'deleted' event 15upnp.on('deleted', (device) => { 16 // This callback function will be called whenever an device was deleted. 17 console.log('[deleted] ------------------------------------'); 18 console.log(' * ' + device['address']); 19 if (device['description']) { 20 console.log(' * ' + device['description']['device']['friendlyName']); 21 } 22 console.log(' * ' + device['headers']['USN']); 23}); 24 25// Start the discovery process 26upnp.startDiscovery();
Get the current active devices or services
1const upnp = require('node-upnp-utils'); 2 3(async () => { 4 // Start the discovery process 5 await upnp.startDiscovery(); 6 7 // Wait for 10 seconds 8 await upnp.wait(10000); 9 10 // Stop the discovery process 11 await upnp.stopDiscovery(); 12 13 // Get and show the discovered devices (services) 14 const device_list = upnp.getActiveDeviceList(); 15 for (const device of device_list) { 16 console.log('------------------------------------'); 17 console.log(' * ' + device['address']); 18 if (device['description']) { 19 console.log(' * ' + device['description']['device']['manufacturer']); 20 console.log(' * ' + device['description']['device']['modelName']); 21 } 22 console.log(' * ' + device['headers']['LOCATION']); 23 console.log(' * ' + device['headers']['USN']); 24 } 25})();
Methods
discover()
method
The discover()
method sends M-SEARCH messages, then gathers information of UPnP devices or services in the same subnet, then returns a list of the discovered devices or services. This method returns a Promise
object. In the await
syntax, this method returns an Array
object containing the discovered devices or services.
If this method is called during the discovery process is ongoing, an Exception will be thrown.
Arguments
discover(params)
params (optional)
This value must be an object containing the properties as follows:
Property | Type | Required | Description |
---|---|---|---|
mx | Integer | Optional | MX header of M-Search. This value must be an integer in the range of 1 to 120. The default value is 3 (seconds). |
st | String | Optional | ST header of M-Search. The default value is upnp:rootdevice . |
wait | Integer | Optional | This method waits the M-Search responses for the specified number of seconds. The value must be in the range of 1 to 120. The default value is 5 seconds. |
1const device_list = await upnp.discover({ 2 st: 'urn:dial-multiscreen-org:service:dial:1', 3 wait: 10 4});
startDiscovery()
method
The startDiscovery()
method sends a M-SEARCH messages, then gathers information of UPnP devices or services in the same subnet. Besides, it monitors NOTIFY events until the stopDiscovery()
method is called. This method returns a Promise
object. In the await
syntax, this method returns nothing.
Whenever a device or service is newly found, then this method requests the UPnP device description (XML), then emits added
event on this instance until the stopDiscovery()
method is called.
Whenever a byebye
notification is received from an added device or service, this method emits deleted
event on this instance until the stopDiscovery()
method is called.
If this method is called during the discovery process is ongoing, an Exception will be thrown.
Arguments
startDiscovery(params)
params (optional)
This value must be an object containing the properties as follows:
Property | Required | Description |
---|---|---|
mx | Optional | The MX header of M-Search. This value must be an integer. The default value is 3 (seconds). |
st | Optional | The ST header of M-Search. The default value is upnp:rootdevice . |
1await upnp.startDiscovery({
2 mx:3,
3 st:'urn:schemas-upnp-org:service:ContentDirectory:1'
4});
stopDiscovery()
method
The stopDiscovery()
method stops the discovery process started by startDiscovery()
method. If the discovery process is not active, this method does nothing. This method returns a Promise
object. In the await
syntax, this method returns nothing.
Arguments
stopDiscovery(callback)
callback (optional)
Note that this argument is deprecated. It will be deleted in the future.
When this method stops the discovery process, the callback will be called. No argument will be passed to the callback.
1await this.stopDiscovery();
getActiveDeviceList()
method
The getActiveDeviceList
method returns an Array
object representing the active devices (services) discovered by the discover()
method or the startDiscovery
method.
1const device_list = this.getActiveDeviceList();
invokeAction()
method
Note that this method is deprecated. It will be deleted in the future.
This method sends the specified SOAP string to the specified device.
Arguments
invokeAction(params, callback)
params (required)
This value must be an object having the properties as follows:
property | required | description |
---|---|---|
url | required | This value is the URL of the targeted device (service). |
soap | required | This value is the SOAP string which you want to post. |
action | optional | This value is the value of the SOAPAction header. If this value was not specified, this method will search the SOAPAction value from the specified SOAP string. |
cookies | optional | This value is an array of cookies. e.x. ['key1:value1', 'key2:value2'] |
callback (required)
When the response (XML) from the targeted device comes, the callback will invoked.
Four arguments will be passed to the callback.
The 1st argument is an error object. It will be null if no error occurred.
The 2nd argument is an object representing the response converted from XML to JavaScript object. If xml2js module is not installed, this value will be null.
The 3rd argument is an XML string representing the response.
The 4th argument is an http.IncomingMessage object.
wait()
method
The wait()
method waits for the specified milliseconds. This method returns a Promise
object. In the await
syntax, this method returns nothing.
Arguments
wait(msec)
msec (required)
This value must be an integer grater than 0.
1await upnp.wait(1000); // Wait 1 second.
Events
added
event
Whenever a new device or service is discovered, an added
event is fired on the UPnP
object. An object is passed to the callback function, which represents the device or service. See the section "The structure of discovered device" for the detail of the structre of the object.
Note that you have to invoke the startDiscovery
method in order to listen to the event.
1upnp.on('added', (device) => { 2 console.log(JSON.stringify(device, null, ' ')); 3}); 4 5upnp.startDiscovery();
deleted
event
Whenever this module detects any discovered device or service leave the local netwrork, an deleted
event is fired on the UPnP
object. An object is passed to the callback function, which represents the device or service. See the section "The structure of discovered device" for the detail of the structre of the object. Note that you have to invoke the startDiscovery
method in order to listen to the event.
1upnp.on('deleted', (device) => { 2 console.log(JSON.stringify(device, null, ' ')); 3}); 4 5upnp.startDiscovery();
The structure of discovered device
The structure of the object representing a device or service is as follows:
1{ 2 "address": "192.168.11.40", 3 "headers": { 4 "$": "HTTP/1.1 200 OK", 5 "CACHE-CONTROL": "max-age=130", 6 "DATE": "Tue, 03 Oct 2023 10:54:46 GMT", 7 "ST": "urn:schemas-upnp-org:service:ContentDirectory:1", 8 "USN": "uuid:4d696e69-444c-164e-9d41-000c294ea6f0::urn:schemas-upnp-org:service:ContentDirectory:1", 9 "SERVER": "Ubuntu DLNADOC/1.50 UPnP/1.0 MiniDLNA/1.3.0", 10 "LOCATION": "http://192.168.11.40:8200/rootDesc.xml", 11 "CONTENT-LENGTH": "0" 12 }, 13 "expire": 1696330616771, 14 "dheaders": { 15 "content-type": "text/xml; charset=\"utf-8\"", 16 "connection": "close", 17 "content-length": "2221", 18 "server": "Ubuntu DLNADOC/1.50 UPnP/1.0 MiniDLNA/1.3.0", 19 "date": "Tue, 03 Oct 2023 10:54:46 GMT", 20 "ext": "" 21 }, 22 "description": { 23 "$": { 24 "xmlns": "urn:schemas-upnp-org:device-1-0" 25 }, 26 "specVersion": { 27 "major": "1", 28 "minor": "0" 29 }, 30 "device": { 31 "deviceType": "urn:schemas-upnp-org:device:MediaServer:1", 32 "friendlyName": "futomi-virtual-machine: minidlna", 33 "manufacturer": "Justin Maggard", 34 "manufacturerURL": "http://www.netgear.com/", 35 "modelDescription": "MiniDLNA on Linux", 36 "modelName": "Windows Media Connect compatible (MiniDLNA)", 37 "modelNumber": "1.3.0", 38 "modelURL": "http://www.netgear.com", 39 "serialNumber": "00000000", 40 "UDN": "uuid:4d696e69-444c-164e-9d41-000c294ea6f0", 41 "dlna:X_DLNADOC": { 42 "_": "DMS-1.50", 43 "$": { 44 "xmlns:dlna": "urn:schemas-dlna-org:device-1-0" 45 } 46 }, 47 "presentationURL": "/", 48 "iconList": { 49 "icon": [ 50 { 51 "mimetype": "image/png", 52 "width": "48", 53 "height": "48", 54 "depth": "24", 55 "url": "/icons/sm.png" 56 }, 57 { 58 "mimetype": "image/png", 59 "width": "120", 60 "height": "120", 61 "depth": "24", 62 "url": "/icons/lrg.png" 63 }, 64 { 65 "mimetype": "image/jpeg", 66 "width": "48", 67 "height": "48", 68 "depth": "24", 69 "url": "/icons/sm.jpg" 70 }, 71 { 72 "mimetype": "image/jpeg", 73 "width": "120", 74 "height": "120", 75 "depth": "24", 76 "url": "/icons/lrg.jpg" 77 } 78 ] 79 }, 80 "serviceList": { 81 "service": [ 82 { 83 "serviceType": "urn:schemas-upnp-org:service:ContentDirectory:1", 84 "serviceId": "urn:upnp-org:serviceId:ContentDirectory", 85 "controlURL": "/ctl/ContentDir", 86 "eventSubURL": "/evt/ContentDir", 87 "SCPDURL": "/ContentDir.xml" 88 }, 89 { 90 "serviceType": "urn:schemas-upnp-org:service:ConnectionManager:1", 91 "serviceId": "urn:upnp-org:serviceId:ConnectionManager", 92 "controlURL": "/ctl/ConnectionMgr", 93 "eventSubURL": "/evt/ConnectionMgr", 94 "SCPDURL": "/ConnectionMgr.xml" 95 }, 96 { 97 "serviceType": "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1", 98 "serviceId": "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar", 99 "controlURL": "/ctl/X_MS_MediaReceiverRegistrar", 100 "eventSubURL": "/evt/X_MS_MediaReceiverRegistrar", 101 "SCPDURL": "/X_MS_MediaReceiverRegistrar.xml" 102 } 103 ] 104 } 105 } 106 }, 107 "descriptionXML": "<?xml version=\"1.0\"?>..." 108}
Properties | Type | Description |
---|---|---|
address | String | IP address of the device. |
headers | Object | This object represents the M-SEARCH response header. |
dheaders | Object | This object represents the HTTP response header of the device description. |
description | Object | This object represents the XML description fetched from the device. The structure of this object depends on the type of device. The node-upnp-utils is agnostic on the structure of this object. If failed to fetch the XML, this property would not exist. |
descriptionXML | String | This string is the XML itself fetched from the device. If failed to fetch the XML, this property would not exist. |
Running on Windows
This module does not work well on Windows by the default Windows setting.
On Windows, the "SSDP Discovery" service (SSDPSRV) is enabled by default. This service prevents the discovery process of this module. If you want to discover UPnP devices using this module, stop the "SSDP Discovery" service (SSDPSRV).
Release Note
- v1.0.3 (2024-06-30)
- Improved the fetch process of UPnP device descriptions.
- v1.0.2 (2024-06-29)
- Fixed the bug that the script using this module was not terminated even though the discovery process was finished.
- v1.0.1 (2024-06-29)
- Ignored network interfaces assigned a global IP address in the M-SEARCH process.
- v1.0.0 (2023-10-03)
- Rewrote all codes in modern coding style using
class
,async
,await
, etc. - Supported multi-homed environment.
- Added the
discover()
method. - Added the
dheaders
property in the object representing a discovered device or service. - Added the
wait()
method. - Deprecated the
invokeAction()
method. - Deprecated the
callback
argument of thestopDiscovery()
method.
- Rewrote all codes in modern coding style using
References
License
The MIT License (MIT)
Copyright (c) 2017 - 2024 Futomi Hatano
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 6/21 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 15 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More