Skip to content

Commit

Permalink
Cherry-picks previous token updates to merge into master (#19)
Browse files Browse the repository at this point in the history
* cherry-pick changes from #8

* fix(json): extraneous comma

* fix(tokens): remove extraneous cafe-based properties
  • Loading branch information
carsonfarmer committed Feb 12, 2019
1 parent 1dc4acd commit 30c5613
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 34 deletions.
98 changes: 98 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
version: 2.1

jobs:
lint:
working_directory: ~/react-native-sdk
docker:
- image: circleci/node:8
steps:
- restore_cache: # If any cache exists, it'll way speedup these checkouts
keys:
- source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
- source-v2-{{ arch }}-{{ .Branch }}-
- source-v2-{{ arch }}-

- checkout

- save_cache:
key: source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
paths:
- ".git"

- restore_cache:
key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}

- restore_cache:
key: node-v1-{{ checksum "package.json" }}-{{ arch }}

- run: yarn install

- run:
name: lint
command: |
yarn lint
# cache our yarn cache
- save_cache:
key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}
paths:
- ~/.cache/yarn

# cache our node_modules
- save_cache:
key: node-v1-{{ checksum "package.json" }}-{{ arch }}
paths:
- node_modules
tests:
working_directory: ~/textile-mobile
docker:
- image: circleci/node:8
steps:
- restore_cache: # If any cache exists, it'll way speedup these checkouts
keys:
- source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
- source-v2-{{ arch }}-{{ .Branch }}-
- source-v2-{{ arch }}-

- checkout

- save_cache:
key: source-v2-{{ arch }}-{{ .Branch }}-{{ .Revision }}
paths:
- ".git"

- restore_cache:
key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}

- restore_cache:
key: node-v1-{{ checksum "package.json" }}-{{ arch }}

- run: yarn install

- run:
name: jest tests
command: |
mkdir -p test-results/jest
yarn run test
environment:
JEST_JUNIT_OUTPUT: test-results/jest/junit.xml

- store_test_results:
path: test-results

- store_artifacts:
path: test-results

workflows:
version: 2
dev:
jobs:
- lint:
filters:
branches:
ignore:
- master
- tests:
filters:
branches:
ignore:
- master
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# not in npmignore
dist/

# Android

*.keystore
Expand Down
86 changes: 86 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Android

*.keystore
*.jks
android/**/*.settings
android/**/*.project
android/**/*.classpath
example/**/*.settings
example/**/*.project
example/**/*.classpath

# OSX
#
.DS_Store

.tmp

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# Visual Studio Code
#
.vscode/

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

**/fastlane/report.xml
**/fastlane/Preview.html
**/fastlane/screenshots
**/fastlane/test_output

# Bundle artifact
*.jsbundle
sourcemap.js

# Jest
#
.jest/
coverage/

# React Native Config
.env
.env.production
.env.staging
.env.default
4 changes: 2 additions & 2 deletions android/src/main/java/io/textile/rnmobile/TextileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,12 @@ public void run() {
}

@ReactMethod
public void registerCafe(final String peerId, final Promise promise) {
public void registerCafe(final String peerId, final String token, final Promise promise) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
node.registerCafe(peerId);
node.registerCafe(peerId, token);
promise.resolve(null);
}
catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions ios/TextileNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ - (dispatch_queue_t)methodQueue {
[self fulfillWithResult:base64 error:error resolver:resolve rejecter:reject];
}

RCT_EXPORT_METHOD(registerCafe:(NSString*)peerId resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
RCT_EXPORT_METHOD(registerCafe:(NSString*)peerId token:(NSString*)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error;
[self.node registerCafe:peerId error:&error];
[self.node registerCafe:peerId token:token error:&error];
[self fulfillWithResult:nil error:error resolver:resolve rejecter:reject];
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Textile/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ class API {
return CafeSession.decode(buffer)
}

registerCafe = async (peerId: string): Promise<void> => {
await TextileNode.registerCafe(peerId)
registerCafe = async (peerId: string, token: string): Promise<void> => {
await TextileNode.registerCafe(peerId, token)
}

removeThread = async (id_: string): Promise<string> => {
Expand Down
8 changes: 6 additions & 2 deletions lib/Textile/Models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { AppStateStatus } from 'react-native'

export interface TextileConfig {
RELEASE_TYPE?: string
TEXTILE_CAFE_GATEWAY_URL?: string
TEXTILE_CAFE_OVERRIDE?: string
SELF_MANAGE_APP_STATE?: boolean
}

export interface CafeConfig {
TEXTILE_CAFE_GATEWAY_URL: string
TEXTILE_CAFE_TOKEN: string
TEXTILE_CAFE_OVERRIDE?: string
}

export enum NodeState {
'nonexistent' = 'nonexistent',
'creating' = 'creating',
Expand Down
2 changes: 1 addition & 1 deletion lib/Textile/__tests__/Api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import API from '../Api'
import API from '../API'
import { IDirectory } from '@textile/react-native-protobufs'

const threadId = 'QmdNgTtH468cqZFzXCi4sVSWTbJMWQbhYb8cBVyikP9LzW'
Expand Down
57 changes: 36 additions & 21 deletions lib/Textile/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AppState, AppStateStatus, DeviceEventEmitter } from 'react-native'
import {
CafeConfig,
DiscoveredCafes,
TextileAppStateStatus,
TextileOptions,
Expand Down Expand Up @@ -36,6 +37,7 @@ class Textile extends API {
_store = new TextileStore()
_nativeEvents = NativeEvents
_config: TextileConfig = {}
_cafe?: CafeConfig
_initialized = false

repoPath = `${RNFS.DocumentDirectoryPath}/textile-go`
Expand Down Expand Up @@ -66,11 +68,14 @@ class Textile extends API {

// setup should only be run where the class will remain persistent so that
// listeners will be wired in to one instance only,
setup = async (config?: TextileConfig) => {
setup = async (config?: TextileConfig, cafe?: CafeConfig) => {
// if config provided, set it
if (config) {
this._config = config
}
if (cafe) {
this._cafe = cafe
}

return this.initializeAppState()
}
Expand Down Expand Up @@ -111,13 +116,15 @@ class Textile extends API {

await this.start()

const sessions = await this.cafeSessions()
if (!sessions || !sessions.values || sessions.values.length < 1) {
const cafeOverride = this._config.TEXTILE_CAFE_OVERRIDE
if (cafeOverride) {
await this.registerCafe(cafeOverride as string)
} else if (this._config.TEXTILE_CAFE_GATEWAY_URL) {
await this.discoverAndRegisterCafes()
if (this._cafe) {
const sessions = await this.cafeSessions()
if (!sessions || !sessions.values || sessions.values.length < 1) {
const cafeOverride = this._cafe.TEXTILE_CAFE_OVERRIDE
if (cafeOverride) {
await this.registerCafe(cafeOverride as string, this._cafe.TEXTILE_CAFE_TOKEN)
} else if (this._cafe.TEXTILE_CAFE_GATEWAY_URL) {
await this.discoverAndRegisterCafes()
}
}
}
await this.updateNodeState(NodeState.started)
Expand Down Expand Up @@ -177,10 +184,14 @@ class Textile extends API {
discoverAndRegisterCafes = async () => {
this.isInitializedCheck()
try {
const cafes = await createTimeout(10000, this.discoverCafes())
const discoveredCafes = cafes as DiscoveredCafes
await this.registerCafe(discoveredCafes.primary.url)
await this.registerCafe(discoveredCafes.secondary.url)
if (this._cafe) {
const cafes = await createTimeout(10000, this.discoverCafes())
const discoveredCafes = cafes as DiscoveredCafes
await this.registerCafe(discoveredCafes.primary.url, this._cafe.TEXTILE_CAFE_TOKEN || '')
await this.registerCafe(discoveredCafes.secondary.url, this._cafe.TEXTILE_CAFE_TOKEN || '')
} else {
TextileEvents.newError('no cafe config provided', 'cafeConfigError')
}
} catch (error) {
// When this happens, you should retry the discover and register...
TextileEvents.newError('cafe discovery timed out, internet connection needed', 'cafeDiscoveryError')
Expand Down Expand Up @@ -372,16 +383,20 @@ class Textile extends API {
}

private discoverCafes = async () => {
if (!this._initialized) {
TextileEvents.nonInitializedError()
return
}
const response = await fetch(`${this._config.TEXTILE_CAFE_GATEWAY_URL}/cafes`, { method: 'GET' })
if (response.status < 200 || response.status > 299) {
throw new Error(`Status code error: ${response.statusText}`)
if (this._cafe) {
if (!this._initialized) {
TextileEvents.nonInitializedError()
return
}
const response = await fetch(`${this._cafe.TEXTILE_CAFE_GATEWAY_URL}/cafes`, { method: 'GET' })
if (response.status < 200 || response.status > 299) {
throw new Error(`Status code error: ${response.statusText}`)
}
const discoveredCafes = await response.json() as DiscoveredCafes
return discoveredCafes
} else {
TextileEvents.newError('no cafe config provided', 'cafeConfigError')
}
const discoveredCafes = await response.json() as DiscoveredCafes
return discoveredCafes
}

private updateNodeStateError = async (error: Error) => {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
],
"scripts": {
"build": "rm -rf dist/ && tsc",
"lint": "tslint -c tslint.json --project .",
"test": "jest --no-watchman",
"ios-example": "yarn; yarn build; yarn install --production; cd example; yarn upgrade @textile/react-native-sdk; yarn; react-native run-ios"
"lint": "rm -rf dist/ && tsc; tslint -c tslint.json --project .",
"test": "rm -rf dist/ && tsc; jest --no-watchman"
},
"keywords": [
"react-native"
Expand Down Expand Up @@ -63,7 +62,7 @@
"typescript": "^3.1.1"
},
"dependencies": {
"@textile/go-mobile": "1.0.0-rc31",
"@textile/go-mobile": "1.0.0-rc33",
"@textile/react-native-protobufs": "1.1.0",
"buffer": "^5.2.1",
"prop-types": "^15.6.2"
Expand Down

0 comments on commit 30c5613

Please sign in to comment.