No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.esm2017.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import firebase from '@firebase/app-compat';
  2. import { _getChild, uploadBytesResumable, _dataFromString, _UploadTask, _FbsBlob, StringFormat, listAll, list, getMetadata, updateMetadata, getDownloadURL, deleteObject, _invalidRootOperation, _invalidArgument, ref, _Location, connectStorageEmulator, _TaskState, _TaskEvent } from '@firebase/storage';
  3. import { Component } from '@firebase/component';
  4. /**
  5. * @license
  6. * Copyright 2020 Google LLC
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. class UploadTaskSnapshotCompat {
  21. constructor(_delegate, task, ref) {
  22. this._delegate = _delegate;
  23. this.task = task;
  24. this.ref = ref;
  25. }
  26. get bytesTransferred() {
  27. return this._delegate.bytesTransferred;
  28. }
  29. get metadata() {
  30. return this._delegate.metadata;
  31. }
  32. get state() {
  33. return this._delegate.state;
  34. }
  35. get totalBytes() {
  36. return this._delegate.totalBytes;
  37. }
  38. }
  39. /**
  40. * @license
  41. * Copyright 2020 Google LLC
  42. *
  43. * Licensed under the Apache License, Version 2.0 (the "License");
  44. * you may not use this file except in compliance with the License.
  45. * You may obtain a copy of the License at
  46. *
  47. * http://www.apache.org/licenses/LICENSE-2.0
  48. *
  49. * Unless required by applicable law or agreed to in writing, software
  50. * distributed under the License is distributed on an "AS IS" BASIS,
  51. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  52. * See the License for the specific language governing permissions and
  53. * limitations under the License.
  54. */
  55. class UploadTaskCompat {
  56. constructor(_delegate, _ref) {
  57. this._delegate = _delegate;
  58. this._ref = _ref;
  59. this.cancel = this._delegate.cancel.bind(this._delegate);
  60. this.catch = this._delegate.catch.bind(this._delegate);
  61. this.pause = this._delegate.pause.bind(this._delegate);
  62. this.resume = this._delegate.resume.bind(this._delegate);
  63. }
  64. get snapshot() {
  65. return new UploadTaskSnapshotCompat(this._delegate.snapshot, this, this._ref);
  66. }
  67. then(onFulfilled, onRejected) {
  68. return this._delegate.then(snapshot => {
  69. if (onFulfilled) {
  70. return onFulfilled(new UploadTaskSnapshotCompat(snapshot, this, this._ref));
  71. }
  72. }, onRejected);
  73. }
  74. on(type, nextOrObserver, error, completed) {
  75. let wrappedNextOrObserver = undefined;
  76. if (!!nextOrObserver) {
  77. if (typeof nextOrObserver === 'function') {
  78. wrappedNextOrObserver = (taskSnapshot) => nextOrObserver(new UploadTaskSnapshotCompat(taskSnapshot, this, this._ref));
  79. }
  80. else {
  81. wrappedNextOrObserver = {
  82. next: !!nextOrObserver.next
  83. ? (taskSnapshot) => nextOrObserver.next(new UploadTaskSnapshotCompat(taskSnapshot, this, this._ref))
  84. : undefined,
  85. complete: nextOrObserver.complete || undefined,
  86. error: nextOrObserver.error || undefined
  87. };
  88. }
  89. }
  90. return this._delegate.on(type, wrappedNextOrObserver, error || undefined, completed || undefined);
  91. }
  92. }
  93. class ListResultCompat {
  94. constructor(_delegate, _service) {
  95. this._delegate = _delegate;
  96. this._service = _service;
  97. }
  98. get prefixes() {
  99. return this._delegate.prefixes.map(ref => new ReferenceCompat(ref, this._service));
  100. }
  101. get items() {
  102. return this._delegate.items.map(ref => new ReferenceCompat(ref, this._service));
  103. }
  104. get nextPageToken() {
  105. return this._delegate.nextPageToken || null;
  106. }
  107. }
  108. /**
  109. * @license
  110. * Copyright 2020 Google LLC
  111. *
  112. * Licensed under the Apache License, Version 2.0 (the "License");
  113. * you may not use this file except in compliance with the License.
  114. * You may obtain a copy of the License at
  115. *
  116. * http://www.apache.org/licenses/LICENSE-2.0
  117. *
  118. * Unless required by applicable law or agreed to in writing, software
  119. * distributed under the License is distributed on an "AS IS" BASIS,
  120. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  121. * See the License for the specific language governing permissions and
  122. * limitations under the License.
  123. */
  124. class ReferenceCompat {
  125. constructor(_delegate, storage) {
  126. this._delegate = _delegate;
  127. this.storage = storage;
  128. }
  129. get name() {
  130. return this._delegate.name;
  131. }
  132. get bucket() {
  133. return this._delegate.bucket;
  134. }
  135. get fullPath() {
  136. return this._delegate.fullPath;
  137. }
  138. toString() {
  139. return this._delegate.toString();
  140. }
  141. /**
  142. * @returns A reference to the object obtained by
  143. * appending childPath, removing any duplicate, beginning, or trailing
  144. * slashes.
  145. */
  146. child(childPath) {
  147. const reference = _getChild(this._delegate, childPath);
  148. return new ReferenceCompat(reference, this.storage);
  149. }
  150. get root() {
  151. return new ReferenceCompat(this._delegate.root, this.storage);
  152. }
  153. /**
  154. * @returns A reference to the parent of the
  155. * current object, or null if the current object is the root.
  156. */
  157. get parent() {
  158. const reference = this._delegate.parent;
  159. if (reference == null) {
  160. return null;
  161. }
  162. return new ReferenceCompat(reference, this.storage);
  163. }
  164. /**
  165. * Uploads a blob to this object's location.
  166. * @param data - The blob to upload.
  167. * @returns An UploadTask that lets you control and
  168. * observe the upload.
  169. */
  170. put(data, metadata) {
  171. this._throwIfRoot('put');
  172. return new UploadTaskCompat(uploadBytesResumable(this._delegate, data, metadata), this);
  173. }
  174. /**
  175. * Uploads a string to this object's location.
  176. * @param value - The string to upload.
  177. * @param format - The format of the string to upload.
  178. * @returns An UploadTask that lets you control and
  179. * observe the upload.
  180. */
  181. putString(value, format = StringFormat.RAW, metadata) {
  182. this._throwIfRoot('putString');
  183. const data = _dataFromString(format, value);
  184. const metadataClone = Object.assign({}, metadata);
  185. if (metadataClone['contentType'] == null && data.contentType != null) {
  186. metadataClone['contentType'] = data.contentType;
  187. }
  188. return new UploadTaskCompat(new _UploadTask(this._delegate, new _FbsBlob(data.data, true), metadataClone), this);
  189. }
  190. /**
  191. * List all items (files) and prefixes (folders) under this storage reference.
  192. *
  193. * This is a helper method for calling list() repeatedly until there are
  194. * no more results. The default pagination size is 1000.
  195. *
  196. * Note: The results may not be consistent if objects are changed while this
  197. * operation is running.
  198. *
  199. * Warning: listAll may potentially consume too many resources if there are
  200. * too many results.
  201. *
  202. * @returns A Promise that resolves with all the items and prefixes under
  203. * the current storage reference. `prefixes` contains references to
  204. * sub-directories and `items` contains references to objects in this
  205. * folder. `nextPageToken` is never returned.
  206. */
  207. listAll() {
  208. return listAll(this._delegate).then(r => new ListResultCompat(r, this.storage));
  209. }
  210. /**
  211. * List items (files) and prefixes (folders) under this storage reference.
  212. *
  213. * List API is only available for Firebase Rules Version 2.
  214. *
  215. * GCS is a key-blob store. Firebase Storage imposes the semantic of '/'
  216. * delimited folder structure. Refer to GCS's List API if you want to learn more.
  217. *
  218. * To adhere to Firebase Rules's Semantics, Firebase Storage does not
  219. * support objects whose paths end with "/" or contain two consecutive
  220. * "/"s. Firebase Storage List API will filter these unsupported objects.
  221. * list() may fail if there are too many unsupported objects in the bucket.
  222. *
  223. * @param options - See ListOptions for details.
  224. * @returns A Promise that resolves with the items and prefixes.
  225. * `prefixes` contains references to sub-folders and `items`
  226. * contains references to objects in this folder. `nextPageToken`
  227. * can be used to get the rest of the results.
  228. */
  229. list(options) {
  230. return list(this._delegate, options || undefined).then(r => new ListResultCompat(r, this.storage));
  231. }
  232. /**
  233. * A `Promise` that resolves with the metadata for this object. If this
  234. * object doesn't exist or metadata cannot be retreived, the promise is
  235. * rejected.
  236. */
  237. getMetadata() {
  238. return getMetadata(this._delegate);
  239. }
  240. /**
  241. * Updates the metadata for this object.
  242. * @param metadata - The new metadata for the object.
  243. * Only values that have been explicitly set will be changed. Explicitly
  244. * setting a value to null will remove the metadata.
  245. * @returns A `Promise` that resolves
  246. * with the new metadata for this object.
  247. * @see firebaseStorage.Reference.prototype.getMetadata
  248. */
  249. updateMetadata(metadata) {
  250. return updateMetadata(this._delegate, metadata);
  251. }
  252. /**
  253. * @returns A `Promise` that resolves with the download
  254. * URL for this object.
  255. */
  256. getDownloadURL() {
  257. return getDownloadURL(this._delegate);
  258. }
  259. /**
  260. * Deletes the object at this location.
  261. * @returns A `Promise` that resolves if the deletion succeeds.
  262. */
  263. delete() {
  264. this._throwIfRoot('delete');
  265. return deleteObject(this._delegate);
  266. }
  267. _throwIfRoot(name) {
  268. if (this._delegate._location.path === '') {
  269. throw _invalidRootOperation(name);
  270. }
  271. }
  272. }
  273. /**
  274. * @license
  275. * Copyright 2020 Google LLC
  276. *
  277. * Licensed under the Apache License, Version 2.0 (the "License");
  278. * you may not use this file except in compliance with the License.
  279. * You may obtain a copy of the License at
  280. *
  281. * http://www.apache.org/licenses/LICENSE-2.0
  282. *
  283. * Unless required by applicable law or agreed to in writing, software
  284. * distributed under the License is distributed on an "AS IS" BASIS,
  285. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  286. * See the License for the specific language governing permissions and
  287. * limitations under the License.
  288. */
  289. /**
  290. * A service that provides firebaseStorage.Reference instances.
  291. * @param opt_url gs:// url to a custom Storage Bucket
  292. */
  293. class StorageServiceCompat {
  294. constructor(app, _delegate) {
  295. this.app = app;
  296. this._delegate = _delegate;
  297. }
  298. get maxOperationRetryTime() {
  299. return this._delegate.maxOperationRetryTime;
  300. }
  301. get maxUploadRetryTime() {
  302. return this._delegate.maxUploadRetryTime;
  303. }
  304. /**
  305. * Returns a firebaseStorage.Reference for the given path in the default
  306. * bucket.
  307. */
  308. ref(path) {
  309. if (isUrl(path)) {
  310. throw _invalidArgument('ref() expected a child path but got a URL, use refFromURL instead.');
  311. }
  312. return new ReferenceCompat(ref(this._delegate, path), this);
  313. }
  314. /**
  315. * Returns a firebaseStorage.Reference object for the given absolute URL,
  316. * which must be a gs:// or http[s]:// URL.
  317. */
  318. refFromURL(url) {
  319. if (!isUrl(url)) {
  320. throw _invalidArgument('refFromURL() expected a full URL but got a child path, use ref() instead.');
  321. }
  322. try {
  323. _Location.makeFromUrl(url, this._delegate.host);
  324. }
  325. catch (e) {
  326. throw _invalidArgument('refFromUrl() expected a valid full URL but got an invalid one.');
  327. }
  328. return new ReferenceCompat(ref(this._delegate, url), this);
  329. }
  330. setMaxUploadRetryTime(time) {
  331. this._delegate.maxUploadRetryTime = time;
  332. }
  333. setMaxOperationRetryTime(time) {
  334. this._delegate.maxOperationRetryTime = time;
  335. }
  336. useEmulator(host, port, options = {}) {
  337. connectStorageEmulator(this._delegate, host, port, options);
  338. }
  339. }
  340. function isUrl(path) {
  341. return /^[A-Za-z]+:\/\//.test(path);
  342. }
  343. const name = "@firebase/storage-compat";
  344. const version = "0.2.1";
  345. /**
  346. * @license
  347. * Copyright 2020 Google LLC
  348. *
  349. * Licensed under the Apache License, Version 2.0 (the "License");
  350. * you may not use this file except in compliance with the License.
  351. * You may obtain a copy of the License at
  352. *
  353. * http://www.apache.org/licenses/LICENSE-2.0
  354. *
  355. * Unless required by applicable law or agreed to in writing, software
  356. * distributed under the License is distributed on an "AS IS" BASIS,
  357. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  358. * See the License for the specific language governing permissions and
  359. * limitations under the License.
  360. */
  361. /**
  362. * Type constant for Firebase Storage.
  363. */
  364. const STORAGE_TYPE = 'storage-compat';
  365. function factory(container, { instanceIdentifier: url }) {
  366. // Dependencies
  367. const app = container.getProvider('app-compat').getImmediate();
  368. const storageExp = container
  369. .getProvider('storage')
  370. .getImmediate({ identifier: url });
  371. const storageServiceCompat = new StorageServiceCompat(app, storageExp);
  372. return storageServiceCompat;
  373. }
  374. function registerStorage(instance) {
  375. const namespaceExports = {
  376. // no-inline
  377. TaskState: _TaskState,
  378. TaskEvent: _TaskEvent,
  379. StringFormat,
  380. Storage: StorageServiceCompat,
  381. Reference: ReferenceCompat
  382. };
  383. instance.INTERNAL.registerComponent(new Component(STORAGE_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */)
  384. .setServiceProps(namespaceExports)
  385. .setMultipleInstances(true));
  386. instance.registerVersion(name, version);
  387. }
  388. registerStorage(firebase);
  389. export { registerStorage };
  390. //# sourceMappingURL=index.esm2017.js.map