@hxa-rn/rn-fetch-blob 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +290 -0
  3. package/harmony/fetch_blob/LICENSE +21 -0
  4. package/harmony/fetch_blob/NOTICE +33 -0
  5. package/harmony/fetch_blob/OAT.xml +38 -0
  6. package/harmony/fetch_blob/build-profile.json5 +16 -0
  7. package/harmony/fetch_blob/hvigorfile.ts +2 -0
  8. package/harmony/fetch_blob/index.ets +1 -0
  9. package/harmony/fetch_blob/oh-package.json5 +14 -0
  10. package/harmony/fetch_blob/src/main/cpp/CMakeLists.txt +15 -0
  11. package/harmony/fetch_blob/src/main/cpp/FetchBlobPackage.h +13 -0
  12. package/harmony/fetch_blob/src/main/cpp/generated/RNOH/generated/BaseFetchBlobPackage.h +72 -0
  13. package/harmony/fetch_blob/src/main/cpp/generated/RNOH/generated/turbo_modules/RNFetchBlob.cpp +55 -0
  14. package/harmony/fetch_blob/src/main/cpp/generated/RNOH/generated/turbo_modules/RNFetchBlob.h +16 -0
  15. package/harmony/fetch_blob/src/main/ets/FetchBlobTurboModulesFactory.ets +18 -0
  16. package/harmony/fetch_blob/src/main/ets/Logger.ts +40 -0
  17. package/harmony/fetch_blob/src/main/ets/RNFetchBlobConfig.ts +45 -0
  18. package/harmony/fetch_blob/src/main/ets/RNFetchBlobFS.ts +673 -0
  19. package/harmony/fetch_blob/src/main/ets/RNFetchBlobImpl.ts +246 -0
  20. package/harmony/fetch_blob/src/main/ets/RNFetchBlobReq.ts +530 -0
  21. package/harmony/fetch_blob/src/main/ets/RNFetchBlobStream.ts +169 -0
  22. package/harmony/fetch_blob/src/main/ets/RNFetchBlobTurboModule.ts +192 -0
  23. package/harmony/fetch_blob/src/main/ets/generated/index.ets +5 -0
  24. package/harmony/fetch_blob/src/main/ets/generated/ts.ts +5 -0
  25. package/harmony/fetch_blob/src/main/ets/generated/turboModules/RNFetchBlob.ts +92 -0
  26. package/harmony/fetch_blob/src/main/ets/generated/turboModules/ts.ts +5 -0
  27. package/harmony/fetch_blob/src/main/module.json5 +22 -0
  28. package/harmony/fetch_blob/src/main/resources/base/element/string.json +8 -0
  29. package/harmony/fetch_blob/src/main/resources/en_US/element/string.json +8 -0
  30. package/harmony/fetch_blob/src/main/resources/zh_CN/element/string.json +8 -0
  31. package/harmony/fetch_blob.har +0 -0
  32. package/package.json +59 -0
  33. package/src/android.js +62 -0
  34. package/src/class/RNFetchBlobFile.js +17 -0
  35. package/src/class/RNFetchBlobReadStream.js +82 -0
  36. package/src/class/RNFetchBlobSession.js +74 -0
  37. package/src/class/RNFetchBlobWriteStream.js +57 -0
  38. package/src/class/StatefulPromise.js +7 -0
  39. package/src/fs.js +432 -0
  40. package/src/index.d.ts +690 -0
  41. package/src/index.js +577 -0
  42. package/src/ios.js +54 -0
  43. package/src/json-stream.js +45 -0
  44. package/src/lib/oboe-browser.min.js +1 -0
  45. package/src/polyfill/Blob.js +362 -0
  46. package/src/polyfill/Event.js +11 -0
  47. package/src/polyfill/EventTarget.js +79 -0
  48. package/src/polyfill/Fetch.js +213 -0
  49. package/src/polyfill/File.js +27 -0
  50. package/src/polyfill/FileReader.js +90 -0
  51. package/src/polyfill/ProgressEvent.js +32 -0
  52. package/src/polyfill/XMLHttpRequest.js +446 -0
  53. package/src/polyfill/XMLHttpRequestEventTarget.js +118 -0
  54. package/src/polyfill/index.js +11 -0
  55. package/src/specs/v1/NativeRNFetchBlob.ts +85 -0
  56. package/src/types.js +64 -0
  57. package/src/utils/log.js +40 -0
  58. package/src/utils/unicode.js +7 -0
  59. package/src/utils/uri.js +28 -0
  60. package/src/utils/uuid.js +4 -0
@@ -0,0 +1,57 @@
1
+ // Copyright 2016 wkh237@github. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ import {
6
+ DeviceEventEmitter,
7
+ NativeAppEventEmitter,
8
+ } from 'react-native'
9
+ import NativeRNFetchBlob from '../specs/v1/NativeRNFetchBlob'
10
+
11
+ const RNFetchBlob = NativeRNFetchBlob
12
+
13
+ export default class RNFetchBlobWriteStream {
14
+
15
+ id : string;
16
+ encoding : string;
17
+ append : boolean;
18
+
19
+ constructor(streamId:string, encoding:string, append:boolean) {
20
+ this.id = streamId
21
+ this.encoding = encoding
22
+ this.append = append
23
+ }
24
+
25
+ write(data:string): Promise<RNFetchBlobWriteStream> {
26
+ return new Promise((resolve, reject) => {
27
+ try {
28
+ let method = this.encoding === 'ascii' ? 'writeArrayChunk' : 'writeChunk'
29
+ if(this.encoding.toLocaleLowerCase() === 'ascii' && !Array.isArray(data)) {
30
+ reject(new Error('ascii input data must be an Array'))
31
+ return
32
+ }
33
+ RNFetchBlob[method](this.id, data, (error) => {
34
+ if(error)
35
+ reject(new Error(error))
36
+ else
37
+ resolve(this)
38
+ })
39
+ } catch(err) {
40
+ reject(new Error(err))
41
+ }
42
+ })
43
+ }
44
+
45
+ close() {
46
+ return new Promise((resolve, reject) => {
47
+ try {
48
+ NativeRNFetchBlob.closeStream(this.id, () => {
49
+ resolve()
50
+ })
51
+ } catch (err) {
52
+ reject(new Error(err))
53
+ }
54
+ })
55
+ }
56
+
57
+ }
@@ -0,0 +1,7 @@
1
+ // Copyright 2016 wkh237@github. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ export default class StatefulPromise extends Promise {
6
+
7
+ }
package/src/fs.js ADDED
@@ -0,0 +1,432 @@
1
+ // Copyright 2016 wkh237@github. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // import type {RNFetchBlobConfig, RNFetchBlobNative, RNFetchBlobStream} from './types'
6
+
7
+ import {Platform} from 'react-native'
8
+ import NativeRNFetchBlob from './specs/v1/NativeRNFetchBlob'
9
+ import RNFetchBlobSession from './class/RNFetchBlobSession'
10
+ import RNFetchBlobWriteStream from './class/RNFetchBlobWriteStream'
11
+ import RNFetchBlobReadStream from './class/RNFetchBlobReadStream'
12
+ import RNFetchBlobFile from './class/RNFetchBlobFile'
13
+
14
+ const RNFetchBlob: RNFetchBlobNative = NativeRNFetchBlob
15
+
16
+ const dirs = {
17
+ DocumentDir : NativeRNFetchBlob.getConstants().DocumentDir,
18
+ CacheDir : NativeRNFetchBlob.getConstants().CacheDir,
19
+ PictureDir : NativeRNFetchBlob.getConstants().PictureDir,
20
+ MusicDir : NativeRNFetchBlob.getConstants().MusicDir,
21
+ MovieDir : NativeRNFetchBlob.getConstants().MovieDir,
22
+ DownloadDir : NativeRNFetchBlob.getConstants().DownloadDir,
23
+ DCIMDir : NativeRNFetchBlob.getConstants().DCIMDir,
24
+ SDCardDir: NativeRNFetchBlob.getConstants().SDCardDir, // Depracated
25
+ SDCardApplicationDir: NativeRNFetchBlob.getConstants().SDCardApplicationDir, // Deprecated
26
+ MainBundleDir : NativeRNFetchBlob.getConstants().MainBundleDir,
27
+ LibraryDir : NativeRNFetchBlob.getConstants().LibraryDir
28
+ }
29
+
30
+ function addCode(code: string, error: Error): Error {
31
+ error.code = code
32
+ return error
33
+ }
34
+
35
+ /**
36
+ * Get a file cache session
37
+ * @param {string} name Stream ID
38
+ * @return {RNFetchBlobSession}
39
+ */
40
+ function session(name: string): RNFetchBlobSession {
41
+ let s = RNFetchBlobSession.getSession(name)
42
+ if (s)
43
+ return new RNFetchBlobSession(name)
44
+ else {
45
+ RNFetchBlobSession.setSession(name, [])
46
+ return new RNFetchBlobSession(name, [])
47
+ }
48
+ }
49
+
50
+ function asset(path: string): string {
51
+ if (Platform.OS === 'ios') {
52
+ // path from camera roll
53
+ if (/^assets-library\:\/\//.test(path))
54
+ return path
55
+ }
56
+ return 'bundle-assets://' + path
57
+ }
58
+
59
+ function createFile(path: string, data: string, encoding: 'base64' | 'ascii' | 'utf8' = 'utf8'): Promise<string> {
60
+ if (encoding.toLowerCase() === 'ascii') {
61
+ return Array.isArray(data) ?
62
+ NativeRNFetchBlob.createFileASCII(path, data) :
63
+ Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')))
64
+ }
65
+ else {
66
+ return NativeRNFetchBlob.createFile(path, data, encoding)
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Create write stream to a file.
72
+ * @param {string} path Target path of file stream.
73
+ * @param {string} encoding Encoding of input data.
74
+ * @param {boolean} [append] A flag represent if data append to existing ones.
75
+ * @return {Promise<RNFetchBlobWriteStream>} A promise resolves a `WriteStream` object.
76
+ */
77
+ function writeStream(
78
+ path: string,
79
+ encoding?: 'utf8' | 'ascii' | 'base64' = 'utf8',
80
+ append?: boolean = false,
81
+ ): Promise<RNFetchBlobWriteStream> {
82
+ if (typeof path !== 'string') {
83
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
84
+ }
85
+ return new Promise((resolve, reject) => {
86
+ NativeRNFetchBlob.writeStream(path, encoding, append, (errCode, errMsg, streamId: string) => {
87
+ if (errMsg) {
88
+ const err = new Error(errMsg)
89
+ err.code = errCode
90
+ reject(err)
91
+ }
92
+ else
93
+ resolve(new RNFetchBlobWriteStream(streamId, encoding))
94
+ })
95
+ })
96
+ }
97
+
98
+ /**
99
+ * Create file stream from file at `path`.
100
+ * @param {string} path The file path.
101
+ * @param {string} encoding Data encoding, should be one of `base64`, `utf8`, `ascii`
102
+ * @param {boolean} bufferSize Size of stream buffer.
103
+ * @param {number} [tick=10] Interval in milliseconds between reading chunks of data
104
+ * @return {RNFetchBlobStream} RNFetchBlobStream stream instance.
105
+ */
106
+ function readStream(
107
+ path: string,
108
+ encoding: 'utf8' | 'ascii' | 'base64' = 'utf8',
109
+ bufferSize?: number,
110
+ tick?: number = 10
111
+ ): Promise<RNFetchBlobReadStream> {
112
+ if (typeof path !== 'string') {
113
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
114
+ }
115
+ return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize, tick))
116
+ }
117
+
118
+ /**
119
+ * Create a directory.
120
+ * @param {string} path Path of directory to be created
121
+ * @return {Promise}
122
+ */
123
+ function mkdir(path: string): Promise {
124
+ if (typeof path !== 'string') {
125
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
126
+ }
127
+ return NativeRNFetchBlob.mkdir(path)
128
+ }
129
+
130
+ /**
131
+ * Returns the path for the app group.
132
+ * @param {string} groupName Name of app group
133
+ * @return {Promise}
134
+ */
135
+ function pathForAppGroup(groupName: string): Promise {
136
+ return NativeRNFetchBlob.pathForAppGroup(groupName)
137
+ }
138
+
139
+ /**
140
+ * Returns the path for the app group synchronous.
141
+ * @param {string} groupName Name of app group
142
+ * @return {string} Path of App Group dir
143
+ */
144
+ function syncPathAppGroup(groupName: string): string {
145
+ if (Platform.OS === 'ios') {
146
+ return NativeRNFetchBlob.syncPathAppGroup(groupName);
147
+ } else {
148
+ return '';
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Wrapper method of readStream.
154
+ * @param {string} path Path of the file.
155
+ * @param {'base64' | 'utf8' | 'ascii'} encoding Encoding of read stream.
156
+ * @return {Promise<Array<number> | string>}
157
+ */
158
+ function readFile(path: string, encoding: string = 'utf8'): Promise<any> {
159
+ if (typeof path !== 'string') {
160
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
161
+ }
162
+ return NativeRNFetchBlob.readFile(path, encoding)
163
+ }
164
+
165
+ /**
166
+ * Write data to file.
167
+ * @param {string} path Path of the file.
168
+ * @param {string | number[]} data Data to write to the file.
169
+ * @param {string} encoding Encoding of data (Optional).
170
+ * @return {Promise}
171
+ */
172
+ function writeFile(path: string, data: string | Array<number>, encoding: ?string = 'utf8'): Promise {
173
+ if (typeof path !== 'string') {
174
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
175
+ }
176
+ if (encoding.toLocaleLowerCase() === 'ascii') {
177
+ if (!Array.isArray(data)) {
178
+ return Promise.reject(addCode('EINVAL', new TypeError('"data" must be an Array when encoding is "ascii"')))
179
+ }
180
+ else
181
+ return NativeRNFetchBlob.writeFileArray(path, data, false)
182
+ }
183
+ else {
184
+ if (typeof data !== 'string') {
185
+ return Promise.reject(addCode('EINVAL', new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`)))
186
+ }
187
+ else
188
+ return NativeRNFetchBlob.writeFile(path, encoding, data, false)
189
+ }
190
+ }
191
+
192
+ function appendFile(path: string, data: string | Array<number>, encoding?: string = 'utf8'): Promise<number> {
193
+ if (typeof path !== 'string') {
194
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
195
+ }
196
+ if (encoding.toLocaleLowerCase() === 'ascii') {
197
+ if (!Array.isArray(data)) {
198
+ return Promise.reject(addCode('EINVAL', new TypeError('`data` of ASCII file must be an array with 0..255 numbers')))
199
+ }
200
+ else
201
+ return NativeRNFetchBlob.writeFileArray(path, data, true)
202
+ }
203
+ else {
204
+ if (typeof data !== 'string') {
205
+ return Promise.reject(addCode('EINVAL'), new TypeError(`"data" must be a String when encoding is "utf8" or "base64", but it is "${typeof data}"`))
206
+ }
207
+ else
208
+ return NativeRNFetchBlob.writeFile(path, encoding, data, true)
209
+ }
210
+ }
211
+
212
+ /**
213
+ * Show statistic data of a path.
214
+ * @param {string} path Target path
215
+ * @return {RNFetchBlobFile}
216
+ */
217
+ function stat(path: string): Promise<RNFetchBlobFile> {
218
+ return new Promise((resolve, reject) => {
219
+ if (typeof path !== 'string') {
220
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
221
+ }
222
+ NativeRNFetchBlob.stat(path, (err, stat) => {
223
+ if (err)
224
+ reject(new Error(err))
225
+ else {
226
+ if (stat) {
227
+ stat.size = parseInt(stat.size)
228
+ stat.lastModified = parseInt(stat.lastModified)
229
+ }
230
+ resolve(stat)
231
+ }
232
+ })
233
+ })
234
+ }
235
+
236
+ /**
237
+ * Android only method, request media scanner to scan the file.
238
+ * @param {Array<Object<string, string>>} pairs Array contains Key value pairs with key `path` and `mime`.
239
+ * @return {Promise}
240
+ */
241
+ function scanFile(pairs: any): Promise {
242
+ return new Promise((resolve, reject) => {
243
+ if (pairs === undefined) {
244
+ return reject(addCode('EINVAL', new TypeError('Missing argument')))
245
+ }
246
+ NativeRNFetchBlob.scanFile(pairs, (err) => {
247
+ if (err)
248
+ reject(addCode('EUNSPECIFIED', new Error(err)))
249
+ else
250
+ resolve()
251
+ })
252
+ })
253
+ }
254
+
255
+ function hash(path: string, algorithm: string): Promise<string> {
256
+ if (typeof path !== 'string' || typeof algorithm !== 'string') {
257
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "algorithm"')))
258
+ }
259
+ return NativeRNFetchBlob.hash(path, algorithm)
260
+ }
261
+
262
+ function cp(path: string, dest: string): Promise<boolean> {
263
+ return new Promise((resolve, reject) => {
264
+ if (typeof path !== 'string' || typeof dest !== 'string') {
265
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')))
266
+ }
267
+ NativeRNFetchBlob.cp(path, dest, (err, res) => {
268
+ if (err)
269
+ reject(addCode('EUNSPECIFIED', new Error(err)))
270
+ else
271
+ resolve(res)
272
+ })
273
+ })
274
+ }
275
+
276
+ function mv(path: string, dest: string): Promise<boolean> {
277
+ return new Promise((resolve, reject) => {
278
+ if (typeof path !== 'string' || typeof dest !== 'string') {
279
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" and/or "destination"')))
280
+ }
281
+ NativeRNFetchBlob.mv(path, dest, (err, res) => {
282
+ if (err)
283
+ reject(addCode('EUNSPECIFIED', new Error(err)))
284
+ else
285
+ resolve(res)
286
+ })
287
+ })
288
+ }
289
+
290
+ function lstat(path: string): Promise<Array<RNFetchBlobFile>> {
291
+ return new Promise((resolve, reject) => {
292
+ if (typeof path !== 'string') {
293
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
294
+ }
295
+ NativeRNFetchBlob.lstat(path, (err, stat) => {
296
+ if (err)
297
+ reject(addCode('EUNSPECIFIED', new Error(err)))
298
+ else
299
+ resolve(stat)
300
+ })
301
+ })
302
+ }
303
+
304
+ function ls(path: string): Promise<Array<String>> {
305
+ if (typeof path !== 'string') {
306
+ return Promise.reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
307
+ }
308
+ return NativeRNFetchBlob.ls(path)
309
+ }
310
+
311
+ /**
312
+ * Remove file at path.
313
+ * @param {string} path:string Path of target file.
314
+ * @return {Promise}
315
+ */
316
+ function unlink(path: string): Promise {
317
+ return new Promise((resolve, reject) => {
318
+ if (typeof path !== 'string') {
319
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
320
+ }
321
+ NativeRNFetchBlob.unlink(path, (err) => {
322
+ if (err) {
323
+ reject(addCode('EUNSPECIFIED', new Error(err)))
324
+ }
325
+ else
326
+ resolve()
327
+ })
328
+ })
329
+ }
330
+
331
+ /**
332
+ * Check if file exists and if it is a folder.
333
+ * @param {string} path Path to check
334
+ * @return {Promise<boolean>}
335
+ */
336
+ function exists(path: string): Promise<boolean> {
337
+ return new Promise((resolve, reject) => {
338
+ if (typeof path !== 'string') {
339
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
340
+ }
341
+ try {
342
+ NativeRNFetchBlob.exists(path, (exist) => {
343
+ resolve(exist)
344
+ })
345
+ }catch (err){
346
+ reject(addCode('EUNSPECIFIED', new Error(err)))
347
+ }
348
+ })
349
+
350
+ }
351
+
352
+ function slice(src: string, dest: string, start: number, end: number): Promise {
353
+ if (typeof src !== 'string' || typeof dest !== 'string') {
354
+ return reject(addCode('EINVAL', new TypeError('Missing argument "src" and/or "destination"')))
355
+ }
356
+
357
+ let p = Promise.resolve()
358
+ let size = 0
359
+
360
+ function normalize(num, size) {
361
+ if (num < 0)
362
+ return Math.max(0, size + num)
363
+ if (!num && num !== 0)
364
+ return size
365
+ return num
366
+ }
367
+
368
+ if (start < 0 || end < 0 || !start || !end) {
369
+ p = p.then(() => stat(src))
370
+ .then((stat) => {
371
+ size = Math.floor(stat.size)
372
+ start = normalize(start || 0, size)
373
+ end = normalize(end, size)
374
+ })
375
+ }
376
+ return p.then(() => NativeRNFetchBlob.slice(src, dest, start, end))
377
+ }
378
+
379
+ function isDir(path: string): Promise<bool> {
380
+ return new Promise((resolve, reject) => {
381
+ if (typeof path !== 'string') {
382
+ return reject(addCode('EINVAL', new TypeError('Missing argument "path" ')))
383
+ }
384
+ try {
385
+ NativeRNFetchBlob.exists(path, (exist, isDir) => {
386
+ resolve(isDir)
387
+ })
388
+ }catch (err){
389
+ reject(addCode('EUNSPECIFIED', new Error(err)))
390
+ }
391
+ })
392
+
393
+ }
394
+
395
+ function df(): Promise<{ free: number, total: number }> {
396
+ return new Promise((resolve, reject) => {
397
+ NativeRNFetchBlob.df((err, stat) => {
398
+ if (err)
399
+ reject(addCode('EUNSPECIFIED', new Error(err)))
400
+ else
401
+ resolve(stat)
402
+ })
403
+ })
404
+ }
405
+
406
+ export default {
407
+ RNFetchBlobSession,
408
+ unlink,
409
+ mkdir,
410
+ session,
411
+ ls,
412
+ readStream,
413
+ mv,
414
+ cp,
415
+ writeStream,
416
+ writeFile,
417
+ appendFile,
418
+ pathForAppGroup,
419
+ syncPathAppGroup,
420
+ readFile,
421
+ hash,
422
+ exists,
423
+ createFile,
424
+ isDir,
425
+ stat,
426
+ lstat,
427
+ scanFile,
428
+ dirs,
429
+ slice,
430
+ asset,
431
+ df
432
+ }