1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | const Blob = require('Blob');
|
14 | const BlobRegistry = require('BlobRegistry');
|
15 | const {BlobModule} = require('NativeModules');
|
16 |
|
17 | import type {BlobData, BlobOptions} from 'BlobTypes';
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function uuidv4(): string {
|
27 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
|
28 | const r = (Math.random() * 16) | 0,
|
29 | v = c == 'x' ? r : (r & 0x3) | 0x8;
|
30 | return v.toString(16);
|
31 | });
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | class BlobManager {
|
38 | |
39 |
|
40 |
|
41 | static isAvailable = !!BlobModule;
|
42 |
|
43 | |
44 |
|
45 |
|
46 | static createFromParts(
|
47 | parts: Array<Blob | string>,
|
48 | options?: BlobOptions,
|
49 | ): Blob {
|
50 | const blobId = uuidv4();
|
51 | const items = parts.map(part => {
|
52 | if (
|
53 | part instanceof ArrayBuffer ||
|
54 | (global.ArrayBufferView && part instanceof global.ArrayBufferView)
|
55 | ) {
|
56 | throw new Error(
|
57 | "Creating blobs from 'ArrayBuffer' and 'ArrayBufferView' are not supported",
|
58 | );
|
59 | }
|
60 | if (part instanceof Blob) {
|
61 | return {
|
62 | data: part.data,
|
63 | type: 'blob',
|
64 | };
|
65 | } else {
|
66 | return {
|
67 | data: String(part),
|
68 | type: 'string',
|
69 | };
|
70 | }
|
71 | });
|
72 | const size = items.reduce((acc, curr) => {
|
73 | if (curr.type === 'string') {
|
74 | return acc + global.unescape(encodeURI(curr.data)).length;
|
75 | } else {
|
76 | return acc + curr.data.size;
|
77 | }
|
78 | }, 0);
|
79 |
|
80 | BlobModule.createFromParts(items, blobId);
|
81 |
|
82 | return BlobManager.createFromOptions({
|
83 | blobId,
|
84 | offset: 0,
|
85 | size,
|
86 | type: options ? options.type : '',
|
87 | lastModified: options ? options.lastModified : Date.now(),
|
88 | });
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 | static createFromOptions(options: BlobData): Blob {
|
96 | BlobRegistry.register(options.blobId);
|
97 | return Object.assign(Object.create(Blob.prototype), {data: options});
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 | static release(blobId: string): void {
|
104 | BlobRegistry.unregister(blobId);
|
105 | if (BlobRegistry.has(blobId)) {
|
106 | return;
|
107 | }
|
108 | BlobModule.release(blobId);
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 | static addNetworkingHandler(): void {
|
116 | BlobModule.addNetworkingHandler();
|
117 | }
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 | static addWebSocketHandler(socketId: number): void {
|
124 | BlobModule.addWebSocketHandler(socketId);
|
125 | }
|
126 |
|
127 | |
128 |
|
129 |
|
130 |
|
131 | static removeWebSocketHandler(socketId: number): void {
|
132 | BlobModule.removeWebSocketHandler(socketId);
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 | static sendOverSocket(blob: Blob, socketId: number): void {
|
139 | BlobModule.sendOverSocket(blob.data, socketId);
|
140 | }
|
141 | }
|
142 |
|
143 | module.exports = BlobManager;
|