Ben Hsieh hace 8 años
padre
commit
ad7f642844
Se han modificado 7 ficheros con 196 adiciones y 12 borrados
  1. 3
    1
      src/index.js
  2. 77
    0
      src/polyfill/Blob.js
  3. 16
    0
      src/polyfill/File.js
  4. 6
    0
      src/polyfill/index.js
  5. 1
    0
      test.sh
  6. 81
    0
      test/test-firebase.js
  7. 12
    11
      test/test-init.js

+ 3
- 1
src/index.js Ver fichero

@@ -34,6 +34,7 @@ const {
34 34
   mv,
35 35
   cp
36 36
 } = fs
37
+import polyfill from './polyfill'
37 38
 
38 39
 const emitter = DeviceEventEmitter
39 40
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
@@ -298,5 +299,6 @@ export default {
298 299
   config,
299 300
   session,
300 301
   fs,
301
-  wrap
302
+  wrap,
303
+  polyfill
302 304
 }

+ 77
- 0
src/polyfill/Blob.js Ver fichero

@@ -0,0 +1,77 @@
1
+/**
2
+ * @author wkh237
3
+ * @since 2016/07/18
4
+ * @description
5
+ * Web API Blob object polyfill.
6
+ */
7
+import fs from '../fs.js'
8
+
9
+export default class Blob {
10
+
11
+  cacheName:string;
12
+  type:string;
13
+  size:number;
14
+
15
+  // legacy constructor
16
+  constructor(data:any, mime:?string) {
17
+    this.cacheName = getBlobName()
18
+    this.type = mime
19
+
20
+    let encode = 'utf8'
21
+    // plain text content
22
+    if(mime === 'text/plain') {
23
+      this.size = data ? data.length : 0
24
+    }
25
+    else if(typeof data === 'string') {
26
+      // content from file
27
+      if(data.startsWith('RNFetchBlob-file://') {
28
+        enode = 'uri'
29
+      }
30
+      // BASE64 encoded
31
+      else {
32
+        encode = 'base64'
33
+      }
34
+    }
35
+    // create cache entry for Blob object
36
+    fs.createFile(this.cacheName, data, encode)
37
+  }
38
+
39
+  /**
40
+   * Write data to Blob object
41
+   * @param  {string | Array} data Data that will write to Blob object
42
+   * @param  {string} encoding Encoding of data to be written
43
+   * @return {Promise}
44
+   */
45
+  write(data:string | Array<number>, encoding:'base64' | 'utf8' | 'ascii' | 'uri'):Promise {
46
+    return fs.write(this.cacheName, data, encoding)
47
+  }
48
+
49
+  /**
50
+   * Create a Blob object which is sliced from current object
51
+   * @param  {number} start    Start byte number
52
+   * @param  {number} end      End byte number
53
+   * @param  {string} contentType Optional, content type of new Blob object
54
+   * @return {Blob}
55
+   */
56
+  slice(start:?number, end:?number, encoding:?string):Blob {
57
+    return fs.slice(this.cacheName, getBlobName(), contentType, start, end)
58
+  }
59
+
60
+  /**
61
+   * Release the resource of the Blob object.
62
+   * @return {Promise}
63
+   */
64
+  close() {
65
+    fs.unlink(this.cacheName)
66
+  }
67
+
68
+
69
+}
70
+
71
+/**
72
+ * Get a temp filename for Blob object
73
+ * @return {string} Temporary filename
74
+ */
75
+function getBlobName() {
76
+
77
+}

+ 16
- 0
src/polyfill/File.js Ver fichero

@@ -0,0 +1,16 @@
1
+/**
2
+ * @author wkh237
3
+ * @since 2016/07/18
4
+ * @description
5
+ * Web API File object polyfill.
6
+ */
7
+import fs from '../fs.js'
8
+import Blob from './Blob.js'
9
+
10
+export default class File extends Blob {
11
+
12
+  constructor() {
13
+    
14
+  }
15
+
16
+}

+ 6
- 0
src/polyfill/index.js Ver fichero

@@ -0,0 +1,6 @@
1
+import Blob from './Blob.js'
2
+import File from './File.js'
3
+
4
+export default {
5
+  Blob, File
6
+}

+ 1
- 0
test.sh Ver fichero

@@ -38,6 +38,7 @@ node -e "var fs=require('fs'); var pkg = JSON.parse(fs.readFileSync('./RNFetchBl
38 38
 # install module
39 39
 cd "${TEST_APP_PATH}"
40 40
 npm install --save "${CWD}/src"
41
+npm install --save firebase
41 42
 rnpm link
42 43
 
43 44
 # copy android assets

+ 81
- 0
test/test-firebase.js Ver fichero

@@ -0,0 +1,81 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+import Timer from 'react-timer-mixin'
5
+import firebase from 'firebase'
6
+
7
+import {
8
+  StyleSheet,
9
+  Text,
10
+  View,
11
+  ScrollView,
12
+  CameraRoll,
13
+  Platform,
14
+  Dimensions,
15
+  Image,
16
+} from 'react-native';
17
+
18
+const fs = RNFetchBlob.fs
19
+const { Assert, Comparer, Info, prop } = RNTest
20
+const describe = RNTest.config({
21
+  group : 'firebase',
22
+  run : true,
23
+  expand : true,
24
+  timeout : 300000000,
25
+})
26
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
27
+const  dirs = RNFetchBlob.fs.dirs
28
+
29
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
30
+let file = RNTest.prop('image')
31
+
32
+// Initialize Firebase
33
+var config = {
34
+  apiKey: "AIzaSyCnoNvJu2tYYHe87Sm-FrW7j-G-c0MPWGQ",
35
+  authDomain: "rnfb-test-app.firebaseapp.com",
36
+  databaseURL: "https://rnfb-test-app.firebaseio.com",
37
+  storageBucket: "rnfb-test-app.appspot.com",
38
+};
39
+firebase.initializeApp(config);
40
+
41
+describe('firebase login', (report, done) => {
42
+
43
+  firebase.auth().signInWithEmailAndPassword('xeiyan@gmail.com', 'rnfbtest1024')
44
+    .catch((err) => {
45
+      console.log('firebase sigin failed', err)
46
+    })
47
+  firebase.auth().onAuthStateChanged((user) => {
48
+    report(<Assert key="login success"
49
+      expect={user !== undefined}
50
+      actual={user}/>,
51
+    <Info key="user content">
52
+      <Text>{JSON.stringify(user)}</Text>
53
+    </Info>)
54
+    done()
55
+  })
56
+})
57
+
58
+
59
+describe('upload file to firebase', (report, done) => {
60
+
61
+  try {
62
+    
63
+    let storage = firebase.storage().ref()
64
+    let task = storage.file(`testdata/firebase-test-${Platform.OS}.png`).put(webFile, metadata)
65
+
66
+    task.on('state_change', null, (err) => {
67
+
68
+    }, () => {
69
+      report(<Assert key="upload success"
70
+        expect={true}
71
+        actual={true}/>,
72
+      <Info key="uploaded file stat" >
73
+        <Text>{task.snapshot.totalBytes}</Text>
74
+        <Text>{JSON.stringify(task.snapshot.metadata)}</Text>
75
+      </Info>)
76
+    })
77
+  } catch(ex) {
78
+    console.log('firebase polyfill error', ex)
79
+  }
80
+
81
+})

+ 12
- 11
test/test-init.js Ver fichero

@@ -18,8 +18,8 @@ const { Assert, Comparer, Info, prop } = RNTest
18 18
 // test environment variables
19 19
 
20 20
 prop('FILENAME', `${Platform.OS}-0.7.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.0.11:8123')
22
-prop('TEST_SERVER_URL_SSL', 'https://192.168.0.11:8124')
21
+prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
+prop('TEST_SERVER_URL_SSL', 'https://192.168.16.70:8124')
23 23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24 24
 prop('styles', {
25 25
   image : {
@@ -58,12 +58,13 @@ describe('GET image from server', (report, done) => {
58 58
 })
59 59
 
60 60
 
61
-require('./test-0.1.x-0.4.x')
62
-require('./test-0.5.1')
63
-require('./test-0.5.2')
64
-require('./test-0.6.0')
65
-require('./test-0.6.2')
66
-require('./test-0.6.3')
67
-require('./test-0.7.0')
68
-require('./test-fs')
69
-require('./test-android')
61
+// require('./test-0.1.x-0.4.x')
62
+// require('./test-0.5.1')
63
+// require('./test-0.5.2')
64
+// require('./test-0.6.0')
65
+// require('./test-0.6.2')
66
+// require('./test-0.6.3')
67
+// require('./test-0.7.0')
68
+// require('./test-fs')
69
+require('./test-firebase')
70
+// require('./test-android')