flexus-net
Advanced tools
Comparing version 1.1.1 to 2.0.0
10
net.js
var isChromeApp = typeof chrome == 'object' && typeof chrome.runtime.id == 'string'; | ||
var isWinRT = typeof WinRTError != 'undefined'; | ||
if (isChromeApp) { | ||
var isNWJS = typeof nw == 'object' && typeof nw.App == 'object'; | ||
if (isNWJS) { | ||
// this is NWJS which combines Chrome and Node. JSPM looks at it as a browser, ignoring @node:net in package.json | ||
// This way we get the native Node 'net' anyway. I just can't use directly require('net') since transpiler would mess it up | ||
module.exports = global.require('net'); | ||
} else if (isChromeApp) { | ||
module.exports = require('chrome-net'); | ||
} else if (isWinRT) { | ||
console.warn('TODO: implement flexus-net for WinRT') | ||
module.exports = require('winrt-net'); | ||
} else { | ||
console.warn('flexus-net not implemented in browser') | ||
} |
{ | ||
"name": "flexus-net", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "truly multiplatform wrapper for node net module", | ||
"author": "Mike Kovařík <kenr.mk@gmail.com>", | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -11,2 +13,16 @@ "node", | ||
"wrapper", | ||
"windows", | ||
"winrt app", | ||
"winrt store app", | ||
"winrt api", | ||
"StreamSocket", | ||
"StreamSocketListener", | ||
"Windows.Networking.Sockets", | ||
"Windows.Networking.Sockets.StreamSocket", | ||
"Windows.Networking.Sockets.StreamSocketListener", | ||
"uwp", | ||
"uwa", | ||
"winrt", | ||
"windows universal app", | ||
"windows universal platform", | ||
"chrome", | ||
@@ -23,4 +39,2 @@ "chrome app", | ||
], | ||
"author": "Mike Kovařík", | ||
"license": "ISC", | ||
"main": "./net.js", | ||
@@ -33,4 +47,5 @@ "map": { | ||
"dependencies": { | ||
"winrt-net": "latest", | ||
"chrome-net": "latest" | ||
} | ||
} |
# flexus-net | ||
## What and why? | ||
### Use the Node `net` API in Chrome and Windows Apps | ||
This is (for now) just a simple wrapper module that makes lifes easier when installing Node.js `net` module with JSPM for usage in browser-like app enviroments. Namely in Chrome packages apps and Windows modern apps. | ||
This module lets you use the **Node.js [`net`](https://nodejs.org/api/net.html) module** API in [**Chrome** Packaged Apps](https://developer.chrome.com/apps/about_apps) and [**Windows** Universal Apps](https://msdn.microsoft.com/en-us/windows/uwp/get-started/whats-a-uwp). | ||
Chrome provides quirky and overly complex [`chrome.sockets.tcp`](https://developer.chrome.com/apps/sockets_tcp) API for TCP socket communication which is wrapped by wonderful module [`chrome-net`](https://github.com/feross/chrome-net/) to provide the same API as Node.js `net`. | ||
And `flexus-net` basically just wraps `chrome-net` with intention to implement similar wrapper for Windows API in the future. | ||
## What and why? | ||
This is just a simple wrapper module that makes lifes easier when installing Node.js `net` module with JSPM for usage in browser-like app enviroments. Namely in Chrome Packages Apps and Windows Universal Apps. | ||
## installation | ||
Chrome provides quirky and overly complex [`chrome.sockets.tcp`](https://developer.chrome.com/apps/sockets_tcp) API for TCP socket communication which is wrapped by wonderful module [`chrome-net`](https://github.com/feross/chrome-net/) to provide the same API as Node.js `net`. The same goes for Windows and it's [`Windows.Networking.Sockets.StreamSocket`](https://msdn.microsoft.com/library/windows/apps/br226882) and [`StreamSocketListener`](https://msdn.microsoft.com/library/windows/apps/br226906) and the module [`winrt-net`](https://github.com/KenRmk/winrt-net). | ||
``` | ||
jspm install npm:flexus-net | ||
``` | ||
And `flexus-net` basically just wraps [`chrome-net`](https://github.com/feross/chrome-net) and [`winrt-net`](https://github.com/KenRmk/winrt-net). | ||
## tips & tricks | ||
And it also works with [NWJS](http://nwjs.io/) where the native Node `net` is used as expected. | ||
You can rename the module from `flexus-net` to `net` in your JSPM config file and then use it simply as | ||
```js | ||
import net from 'net'; | ||
## Installation & Usage | ||
### 1) Install the module through JSPM | ||
``` | ||
or | ||
```js | ||
var net = require('net'); | ||
jspm install npm:flexus-net | ||
``` | ||
Sweet. Now how to do it? | ||
### 2) Rename `winrt-net` to `net` in your SystemJS/JSPM config file | ||
#### Why? | ||
JSPM has it's own module that gets installed whenever you or your dependecy uses `net` module. And it does next to nothing because browsers don't do TCP. | ||
#### how? | ||
In JSPM config file there is property `map` with names and mappings of all modules. This is an example of JSPM 0.17 `jspm.config.js` | ||
@@ -38,3 +32,3 @@ | ||
map: { | ||
"flexus-net": "npm:flexus-net@1.1.1", | ||
"winrt-net": "npm:winrt-net@1.0.0", | ||
"events": "github:jspm/nodelibs-events@0.2.0-alpha", | ||
@@ -45,15 +39,59 @@ "process": "github:jspm/nodelibs-process@0.2.0-alpha", | ||
where you change the name like so | ||
you change the name like so | ||
```js | ||
map: { | ||
"net": "npm:flexus-net@1.1.1", | ||
"net": "npm:winrt-net@1.0.0", | ||
... | ||
``` | ||
Now you can write your app with `net` module like you would in Node and JSPM handles picking the right module for given enviroment. When runnig in Chrome App the `chrome-net` is used in background or when run in Node using `JSPM run path_to_your.js` the native module is used. | ||
and that forces JSPM to load this module whenever there is `require('net')` or `import net from 'net'` in your code or dependencies. Now you can write your app with `net` module like you would in Node and JSPM handles picking the right module for given enviroment. When runnig in Chrome App the `chrome-net` is used, in Windows App the `winrt-net` is used or when running NWJS or in Node (using `JSPM run path_to_your.js`) the native module is used. | ||
### Usage | ||
Example TCP client: | ||
```js | ||
import net from 'net'; | ||
import net from 'net'; | ||
var port = 22112; | ||
var server = net.createServer(function(socket) { | ||
console.log('connection', socket.remoteAddress + ":" + socket.remotePort); | ||
socket.on('data', function (data) { | ||
console.log(data.toString()) | ||
}) | ||
}) | ||
.listen(port) | ||
.on('listening', function () { | ||
console.log('listening') | ||
}); | ||
``` | ||
Example TCP server: | ||
```js | ||
import net from 'net'; | ||
var client = net.connect(22112, 'localhost') | ||
client.write('Hello server') | ||
client.on('data', function (data) { | ||
console.log('received data', data.toString()); | ||
}) | ||
``` | ||
Or you can skip step 2) and just use `import net from 'flexus-net';` | ||
See nodejs.org for full API documentation: [net](https://nodejs.org/api/net.html) | ||
## JSPM & Browserify | ||
This project was built for and tested using JSPM. | ||
I'm not using browserify nor do I know how to set up a project for it and currently I don't have enough time to look into it now. So I don't know if this module works in browserify but I'll gladly accept any contributions towards this cause. | ||
## thanks to | ||
Feross Aboukhadijeh, John Hiesey & Jan Schä for creating [`chrome-net`](https://github.com/feross/chrome-net/) and Guy Bedford for [`JSPM`](https://github.com/jspm/jspm-cli) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
6587
4
13
95
0
2
+ Addedwinrt-net@latest
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@4.9.2(transitive)
+ Addedevents@1.1.1(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedwinrt-net@1.0.6(transitive)