Skip to content

Commit

Permalink
Merge pull request #3 from tradle/master
Browse files Browse the repository at this point in the history
feat(update): merge fixes
  • Loading branch information
DomiR authored Mar 29, 2019
2 parents 2c3abc4 + 9970884 commit ea86d4c
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
10 changes: 4 additions & 6 deletions UdpSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ var {
var Sockets = NativeModules.UdpSockets
var base64 = require('base64-js')
var ipRegex = require('ip-regex')
var normalizeBindOptions = require('./normalizeBindOptions')
// RFC 952 hostname format
var hostnameRegex = /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/;
var hostnameRegex = /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;
var noop = function () {}
var instances = 0
var STATE = {
Expand Down Expand Up @@ -76,15 +77,12 @@ UdpSocket.prototype._debug = function() {
}
}

UdpSocket.prototype.bind = function(port, address, callback) {
UdpSocket.prototype.bind = function(...args) {
var self = this

if (this._state !== STATE.UNBOUND) throw new Error('Socket is already bound')

if (typeof address === 'function') {
callback = address
address = undefined
}
let { port, address, callback } = normalizeBindOptions(...args)

if (!address) address = '0.0.0.0'

Expand Down
17 changes: 12 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
def DEFAULT_TARGET_SDK_VERSION = 27
def DEFAULT_SUPPORT_LIB_VERSION = "28.0.0"

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
versionCode 1
versionName "0.2.0"
}
Expand All @@ -25,7 +30,9 @@ android {
}

dependencies {
def supportLibVersion = project.hasProperty('supportLibVersion') ? project.supportLibVersion : DEFAULT_SUPPORT_LIB_VERSION

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.facebook.react:react-native:0.11.+'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile 'com.facebook.react:react-native:+'
}
6 changes: 0 additions & 6 deletions android/src/main/java/com/tradle/react/UdpSocketsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.tradle.react;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
Expand All @@ -30,11 +29,6 @@ public List<NativeModule> createNativeModules(
return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(
ReactApplicationContext reactContext) {
Expand Down
21 changes: 21 additions & 0 deletions normalizeBindOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = function normalizeBindOptions(...args) {
const [arg1, arg2] = args
const lastArg = args[args.length - 1]
let options = {}

if (typeof arg1 === 'object') {
options = arg1
} else if (typeof arg1 === 'number') {
options.port = arg1
} else if (typeof arg1 === 'string') {
options.address = arg1
}
if (typeof arg2 === 'string') {
options.address = arg2
}
if (typeof lastArg === 'function') {
options.callback = lastArg
}

return options
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "react-native-udp",
"version": "2.3.1",
"version": "2.5.1",
"description": "node's dgram API for react-native",
"main": "UdpSockets.js",
"scripts": {
"start": "exit 1"
"start": "exit 1",
"test": "mocha"
},
"browser": {
"dgram": "./UdpSockets.js"
Expand Down Expand Up @@ -38,5 +39,8 @@
"inherits": "^2.0.1",
"ip-regex": "^1.0.3",
"util": "^0.10.3"
},
"devDependencies": {
"mocha": "^6.0.1"
}
}
16 changes: 16 additions & 0 deletions react-native-udp.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'json'
package_json = JSON.parse(File.read('package.json'))

Pod::Spec.new do |s|

s.name = package_json["name"]
s.version = package_json["version"]
s.summary = package_json["description"]
s.homepage = package_json["homepage"]
s.license = package_json["license"]
s.author = { package_json["author"] => package_json["author"] }
s.platform = :ios, "7.0"
s.source = { :git => package_json["repository"]["url"].gsub(/(http.*)/).first, :tag => "v#{s.version}" }
s.source_files = 'ios/**/*.{h,m}'

end
55 changes: 55 additions & 0 deletions test/normalizeBindOptions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const assert = require('assert')
const normalizeBindOptions = require('../normalizeBindOptions')
const prettify = obj => JSON.stringify(obj, null, 2)

describe('normalizeBindOptions', () => {

it('should support all combinations of arguments for [port], [address], [callback]', () => {
const args = [
{ name: 'port', value: 1234 },
{ name: 'address', value: '1.2.3.4' },
{ name: 'callback', value: () => {} },
]

// check every combination of arguments (retaining order)
// use a bit mask to decide whether to keep an argument
for (let i = 0, numCombinations = Math.pow(2, args.length); i < numCombinations; i++) {
const usedArgs = args.filter((arg, bit) => (i + 1) & 1 << bit)
const expected = {}
usedArgs.forEach(arg => expected[arg.name] = arg.value)
const usedArgsValues = usedArgs.map(arg => arg.value)

const result = normalizeBindOptions(...usedArgsValues)
try {
assert.deepEqual(result, expected)
} catch (err) {
throw new Error(`for args: ${prettify(usedArgsValues)}\nexpected: ${prettify(expected)}\ngot ${prettify(result)}`)
}
}
})

it('should support all combinations of arguments for [options], [callback]', () => {
const callback = () => {}
const inOut = [
[
[{ port: 123 }, callback], { port: 123, callback }
],
[
[{ port: 123 }], { port: 123 }
],
[
[callback], { callback }
],
]

for (const [args, expected] of inOut) {
let result
try {
result = normalizeBindOptions(...args)
assert.deepEqual(result, expected)
} catch (err) {
throw new Error(`for args: ${prettify(args)}\nexpected: ${prettify(expected)}\ngot ${prettify(result)}`)
}
}
})
})

0 comments on commit ea86d4c

Please sign in to comment.