|
| 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 Blob |
| 10 | + * @flow |
| 11 | + */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +const invariant = require('fbjs/lib/invariant'); |
| 16 | +const uuid = require('uuid'); |
| 17 | + |
| 18 | +const { BlobModule } = require('NativeModules'); |
| 19 | + |
| 20 | +import type { BlobProps } from 'BlobTypes'; |
| 21 | + |
| 22 | +/** |
| 23 | + * Opaque JS representation of some binary data in native. |
| 24 | + * |
| 25 | + * The API is modeled after the W3C Blob API, with one caveat |
| 26 | + * regarding explicit deallocation. Refer to the `close()` |
| 27 | + * method for further details. |
| 28 | + * |
| 29 | + * Example usage in a React component: |
| 30 | + * |
| 31 | + * class WebSocketImage extends React.Component { |
| 32 | + * state = {blob: null}; |
| 33 | + * componentDidMount() { |
| 34 | + * let ws = this.ws = new WebSocket(...); |
| 35 | + * ws.binaryType = 'blob'; |
| 36 | + * ws.onmessage = (event) => { |
| 37 | + * if (this.state.blob) { |
| 38 | + * this.state.blob.close(); |
| 39 | + * } |
| 40 | + * this.setState({blob: event.data}); |
| 41 | + * }; |
| 42 | + * } |
| 43 | + * componentUnmount() { |
| 44 | + * if (this.state.blob) { |
| 45 | + * this.state.blob.close(); |
| 46 | + * } |
| 47 | + * this.ws.close(); |
| 48 | + * } |
| 49 | + * render() { |
| 50 | + * if (!this.state.blob) { |
| 51 | + * return <View />; |
| 52 | + * } |
| 53 | + * return <Image source={{uri: URL.createObjectURL(this.state.blob)}} />; |
| 54 | + * } |
| 55 | + * } |
| 56 | + * |
| 57 | + * Reference: https://developer.mozilla.org/en-US/docs/Web/API/Blob |
| 58 | + */ |
| 59 | +class Blob { |
| 60 | + /** |
| 61 | + * Size of the data contained in the Blob object, in bytes. |
| 62 | + */ |
| 63 | + size: number; |
| 64 | + /* |
| 65 | + * String indicating the MIME type of the data contained in the Blob. |
| 66 | + * If the type is unknown, this string is empty. |
| 67 | + */ |
| 68 | + type: string; |
| 69 | + |
| 70 | + /* |
| 71 | + * Unique id to identify the blob on native side (non-standard) |
| 72 | + */ |
| 73 | + blobId: string; |
| 74 | + /* |
| 75 | + * Offset to indicate part of blob, used when sliced (non-standard) |
| 76 | + */ |
| 77 | + offset: number; |
| 78 | + |
| 79 | + /** |
| 80 | + * Construct blob instance from blob data from native. |
| 81 | + * Used internally by modules like XHR, WebSocket, etc. |
| 82 | + */ |
| 83 | + static create(props: BlobProps): Blob { |
| 84 | + return Object.assign(Object.create(Blob.prototype), props); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Constructor for JS consumers. |
| 89 | + * Currently we only support creating Blobs from other Blobs. |
| 90 | + * Reference: https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob |
| 91 | + */ |
| 92 | + constructor(parts: Array<Blob>, options: any) { |
| 93 | + const blobId = uuid(); |
| 94 | + let size = 0; |
| 95 | + parts.forEach((part) => { |
| 96 | + invariant(part instanceof Blob, 'Can currently only create a Blob from other Blobs'); |
| 97 | + size += part.size; |
| 98 | + }); |
| 99 | + BlobModule.createFromParts(parts, blobId); |
| 100 | + return Blob.create({ |
| 101 | + blobId, |
| 102 | + offset: 0, |
| 103 | + size, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + /* |
| 108 | + * This method is used to create a new Blob object containing |
| 109 | + * the data in the specified range of bytes of the source Blob. |
| 110 | + * Reference: https://developer.mozilla.org/en-US/docs/Web/API/Blob/slice |
| 111 | + */ |
| 112 | + slice(start?: number, end?: number): Blob { |
| 113 | + let offset = this.offset; |
| 114 | + let size = this.size; |
| 115 | + if (typeof start === 'number') { |
| 116 | + if (start > size) { |
| 117 | + start = size; |
| 118 | + } |
| 119 | + offset += start; |
| 120 | + size -= start; |
| 121 | + |
| 122 | + if (typeof end === 'number') { |
| 123 | + if (end < 0) { |
| 124 | + end = this.size + end; |
| 125 | + } |
| 126 | + size = end - start; |
| 127 | + } |
| 128 | + } |
| 129 | + return Blob.create({ |
| 130 | + blobId: this.blobId, |
| 131 | + offset, |
| 132 | + size, |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * This method is in the standard, but not actually implemented by |
| 138 | + * any browsers at this point. It's important for how Blobs work in |
| 139 | + * React Native, however, since we cannot de-allocate resources automatically, |
| 140 | + * so consumers need to explicitly de-allocate them. |
| 141 | + * |
| 142 | + * Note that the semantics around Blobs created via `blob.slice()` |
| 143 | + * and `new Blob([blob])` are different. `blob.slice()` creates a |
| 144 | + * new *view* onto the same binary data, so calling `close()` on any |
| 145 | + * of those views is enough to deallocate the data, whereas |
| 146 | + * `new Blob([blob, ...])` actually copies the data in memory. |
| 147 | + */ |
| 148 | + close() { |
| 149 | + BlobModule.release(this.blobId); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +module.exports = Blob; |
0 commit comments