Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
enricogior committed Oct 2, 2017
0 parents commit 3fa38e8
Show file tree
Hide file tree
Showing 165 changed files with 67,993 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
node_modules/
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Node.js for Mobile Apps Cordova plugin

## Installation

```bash
cordova plugin add nodejs-mobile-cordova
```

## Requirements

- Cordova 7.0.0 or higher
- for iOS apps: iOS 11 or higher

## Supported Platforms

- Android (ARMv7a, x86)
- iOS (ARM64)

## Cordova Methods

- `nodejs.start`
- `nodejs.startWithScript`
- `nodejs.channel.setListener`
- `nodejs.channel.send`


### nodejs.start

```js
nodejs.start(scriptFileName, callback);
```

### nodejs.startWithScript

```js
nodejs.startWithScript(scriptBody, callback);
```

### nodejs.channel.setListener

```js
nodejs.channel.setListener(listenerCallback);
```

### nodejs.channel.send

```js
nodejs.channel.send(message);
```

## Node.js Methods

```js
var cordova = require('cordova-bridge');
```

- `cordova.channel.send`
- `cordova.channel.on`

### cordova.channel.send

```
cordova.channel.send(message);
```

### cordova.channel.on

```
cordova.channel.on('message', listnerCallback);
```


## Usage

This is an example to build an iOS app.
In macOS, using `Terminal`:
```
cordova create MyApp
cd MyApp
cordova platform add ios
cordova plugin add nodejs-mobile-cordova
cordova plugin add cordova-plugin-console
```
Copy the sample project files, you will be asked to overwrite the default `./www/js/index.js` file.
```
plugins/nodejs-mobile-cordova/install/sample-project/copy-sample-project.sh
overwrite www/js/index.js? (y/n [n]) y
```
Open the Cordova app project in Xcode:
```
open platforms/ios/HelloCordova.xcodeproj
```
Switch to Xcode:
* select HelloCordova to view the project settings
* in the `General` settings:
* in the `Signing` section select a team to sign the app
* in `Deployment Info` section select `Deployment Targert` `11.0` or higher

Go back to `Terminal` to build the Cordova app
```
cordova build ios --device
```
Switch to Xcode:
* select a target device for the project
* run the project
* enlarge the `Console` area, at the end of the console log it should show:

```
2017-10-02 18:49:18.606100+0200 HelloCordova[2182:1463518] NodeJs Engine Started
[node] received: Hello from Cordova!
2017-10-02 18:49:18.690132+0200 HelloCordova[2182:1463518] [cordova] received: Replying to this message: Hello from Cordova!
```

53 changes: 53 additions & 0 deletions install/hooks/android/before-plugin-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var fs = require('fs');
var zlib = require('zlib');

const nodeProjectFolder = 'www/nodejs-project';
const libFolderPath = 'plugins/nodejs-mobile-cordova/libs/libnode/android/';
const path_armv7 = libFolderPath + 'armeabi-v7a/';
const path_x86 = libFolderPath + 'x86/'
const lib_name = 'libnode.so';
const lib_name_gz = lib_name + '.gz';

function unzip(libFolderPath, callback) {
const input_filename = libFolderPath + lib_name_gz;
const output_filename = libFolderPath + lib_name;

if (fs.existsSync(input_filename)) {
const input = fs.createReadStream(input_filename);
const output = fs.createWriteStream(output_filename);

const ungzip = zlib.createGunzip();
input.pipe(ungzip)
.pipe(output)
.on('close', function(){ fs.unlinkSync(input_filename); callback() });
}
}

function unzipAll(callback) {
unzip(path_armv7, function() {
unzip(path_x86, function() {
callback(null);
});
});
}

module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();

// Create the node project folder if it doesn't exist
if (!fs.existsSync(nodeProjectFolder)) {
fs.mkdirSync(nodeProjectFolder);
}

// Unzip the libnode.so files for each architecture
unzipAll(function(err) {
if (err) {
deferral.reject(err);
} else {
deferral.resolve();
}
});

return deferral.promise;
}
37 changes: 37 additions & 0 deletions install/hooks/ios/before-plugin-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var fs = require('fs');
var targz2 = require('tar.gz2');

const nodeProjectFolder = 'www/nodejs-project';
const libnodeFolderPath = 'plugins/nodejs-mobile-cordova/libs/libnode/ios/';
const libnodeFileName = 'libnode.framework';
const libnodeFilePath = libnodeFolderPath + libnodeFileName;
const zipFileName = libnodeFileName + '.tar.zip';
const zipFilePath = libnodeFolderPath + zipFileName

module.exports = function(context) {
var Q = context.requireCordovaModule('q');
var deferral = new Q.defer();

// Create the node project folder if it doesn't exist
if (!fs.existsSync(nodeProjectFolder)) {
fs.mkdirSync(nodeProjectFolder);
}

// Unzip and untar the libnode.Framework
if (fs.existsSync(zipFilePath)) {
targz2().extract(zipFilePath, libnodeFolderPath, function(err) {
if (err) {
deferral.reject(err);
} else {
fs.unlinkSync(zipFilePath);
deferral.resolve();
}
});
} else if (!fs.existsSync(libnodeFilePath)) {
deferral.reject(new Error(libnodeFileName + ' is missing'));
} else {
deferral.resolve();
}

return deferral.promise;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const EventEmitter = require('events');
const NativeBridge = process.binding('cordova_bridge');

class ChannelEmitter extends EventEmitter { }
ChannelEmitter.prototype.send = function (msg) {
NativeBridge.send(msg);
};

const channel = new ChannelEmitter();

NativeBridge.setListener(function (msg) {
channel.emit('message', msg);
});

exports.channel = channel;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "cordova-bridge",
"version": "0.1.0",
"description": "NodeJS for Mobile Native Bridge",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "JaneaSystems Inc.",
"license": "MIT"
}
12 changes: 12 additions & 0 deletions install/sample-project/copy-sample-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
SRC_PATH="plugins/nodejs-mobile-cordova/install/sample-project"

function copy {
cp -i "$SRC_PATH/$1" $1
}

mkdir -p "www/nodejs-project"

copy "www/js/index.js"
copy "www/nodejs-project/main.js"
copy "www/nodejs-project/package.json"
46 changes: 46 additions & 0 deletions install/sample-project/www/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},

// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
startNodeProject();
},

// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
}
};

app.initialize();

function channelListener(msg) {
console.log("[cordova] received:", msg);
}

function startNodeProject() {
nodejs.channel.setListener(channelListener);
nodejs.start("main.js",
function(err) {
if (err) {
console.log(err);
} else {
console.log ("NodeJs Engine Started");
nodejs.channel.send("Hello from Cordova!");
}
});
}
6 changes: 6 additions & 0 deletions install/sample-project/www/nodejs-project/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const cordova = require('cordova-bridge');

cordova.channel.on('message', function (msg) {
console.log('[node] received:', msg);
cordova.channel.send('Replying to this message: ' + msg);
});
11 changes: 11 additions & 0 deletions install/sample-project/www/nodejs-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "nodejs-mobile-sample-project",
"version": "0.1.0",
"description": "NodeJS for Mobile sample project",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Janea Systems Inc.",
"license": "MIT"
}
Binary file added libs/libnode/android/armeabi-v7a/libnode.so.gz
Binary file not shown.
Binary file added libs/libnode/android/x86/libnode.so.gz
Binary file not shown.
54 changes: 54 additions & 0 deletions libs/libnode/include/node/android-ifaddrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 1995, 1999
* Berkeley Software Design, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* BSDI ifaddrs.h,v 2.5 2000/02/23 14:51:59 dab Exp
*/

#ifndef _IFADDRS_H_
#define _IFADDRS_H_

struct ifaddrs {
struct ifaddrs *ifa_next;
char *ifa_name;
unsigned int ifa_flags;
struct sockaddr *ifa_addr;
struct sockaddr *ifa_netmask;
struct sockaddr *ifa_dstaddr;
void *ifa_data;
};

/*
* This may have been defined in <net/if.h>. Note that if <net/if.h> is
* to be included it must be included before this header file.
*/
#ifndef ifa_broadaddr
#define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
#endif

#include <sys/cdefs.h>

__BEGIN_DECLS
extern int getifaddrs(struct ifaddrs **ifap);
extern void freeifaddrs(struct ifaddrs *ifa);
__END_DECLS

#endif
Loading

0 comments on commit 3fa38e8

Please sign in to comment.