|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule BlobManager |
| 10 | + * @flow |
| 11 | + * @format |
| 12 | + */ |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +const Blob = require('Blob'); |
| 17 | +const BlobRegistry = require('BlobRegistry'); |
| 18 | +const {BlobModule} = require('NativeModules'); |
| 19 | + |
| 20 | +import type {BlobData, BlobOptions} from 'BlobTypes'; |
| 21 | + |
| 22 | +/*eslint-disable no-bitwise */ |
| 23 | +/*eslint-disable eqeqeq */ |
| 24 | + |
| 25 | +/** |
| 26 | + * Based on the rfc4122-compliant solution posted at |
| 27 | + * http://stackoverflow.com/questions/105034 |
| 28 | + */ |
| 29 | +function uuidv4(): string { |
| 30 | + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { |
| 31 | + const r = (Math.random() * 16) | 0, |
| 32 | + v = c == 'x' ? r : (r & 0x3) | 0x8; |
| 33 | + return v.toString(16); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Module to manage blobs. Wrapper around the native blob module. |
| 39 | + */ |
| 40 | +class BlobManager { |
| 41 | + /** |
| 42 | + * If the native blob module is available. |
| 43 | + */ |
| 44 | + static isAvailable = !!BlobModule; |
| 45 | + |
| 46 | + /** |
| 47 | + * Create blob from existing array of blobs. |
| 48 | + */ |
| 49 | + static createFromParts( |
| 50 | + parts: Array<Blob | string>, |
| 51 | + options?: BlobOptions, |
| 52 | + ): Blob { |
| 53 | + const blobId = uuidv4(); |
| 54 | + const items = parts.map(part => { |
| 55 | + if ( |
| 56 | + part instanceof ArrayBuffer || |
| 57 | + (global.ArrayBufferView && part instanceof global.ArrayBufferView) |
| 58 | + ) { |
| 59 | + throw new Error( |
| 60 | + "Creating blobs from 'ArrayBuffer' and 'ArrayBufferView' are not supported", |
| 61 | + ); |
| 62 | + } |
| 63 | + if (part instanceof Blob) { |
| 64 | + return { |
| 65 | + data: part.data, |
| 66 | + type: 'blob', |
| 67 | + }; |
| 68 | + } else { |
| 69 | + return { |
| 70 | + data: String(part), |
| 71 | + type: 'string', |
| 72 | + }; |
| 73 | + } |
| 74 | + }); |
| 75 | + const size = items.reduce((acc, curr) => { |
| 76 | + if (curr.type === 'string') { |
| 77 | + return acc + global.unescape(encodeURI(curr.data)).length; |
| 78 | + } else { |
| 79 | + return acc + curr.data.size; |
| 80 | + } |
| 81 | + }, 0); |
| 82 | + |
| 83 | + BlobModule.createFromParts(items, blobId); |
| 84 | + |
| 85 | + return BlobManager.createFromOptions({ |
| 86 | + blobId, |
| 87 | + offset: 0, |
| 88 | + size, |
| 89 | + type: options ? options.type : '', |
| 90 | + lastModified: options ? options.lastModified : Date.now(), |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create blob instance from blob data from native. |
| 96 | + * Used internally by modules like XHR, WebSocket, etc. |
| 97 | + */ |
| 98 | + static createFromOptions(options: BlobData): Blob { |
| 99 | + BlobRegistry.register(options.blobId); |
| 100 | + return Object.assign(Object.create(Blob.prototype), {data: options}); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Deallocate resources for a blob. |
| 105 | + */ |
| 106 | + static release(blobId: string): void { |
| 107 | + BlobRegistry.unregister(blobId); |
| 108 | + if (BlobRegistry.has(blobId)) { |
| 109 | + return; |
| 110 | + } |
| 111 | + BlobModule.release(blobId); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Inject the blob content handler in the networking module to support blob |
| 116 | + * requests and responses. |
| 117 | + */ |
| 118 | + static addNetworkingHandler(): void { |
| 119 | + BlobModule.addNetworkingHandler(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Indicate the websocket should return a blob for incoming binary |
| 124 | + * messages. |
| 125 | + */ |
| 126 | + static addWebSocketHandler(socketId: number): void { |
| 127 | + BlobModule.addWebSocketHandler(socketId); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Indicate the websocket should no longer return a blob for incoming |
| 132 | + * binary messages. |
| 133 | + */ |
| 134 | + static removeWebSocketHandler(socketId: number): void { |
| 135 | + BlobModule.removeWebSocketHandler(socketId); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Send a blob message to a websocket. |
| 140 | + */ |
| 141 | + static sendOverSocket(blob: Blob, socketId: number): void { |
| 142 | + BlobModule.sendOverSocket(blob.data, socketId); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +module.exports = BlobManager; |
0 commit comments