123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', { value: true });
-
- var app = require('@firebase/app');
- var tslib = require('tslib');
- var util = require('@firebase/util');
- var component = require('@firebase/component');
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Constants used in the Firebase Storage library.
- */
- /**
- * Domain name for firebase storage.
- */
- var DEFAULT_HOST = 'firebasestorage.googleapis.com';
- /**
- * The key in Firebase config json for the storage bucket.
- */
- var CONFIG_STORAGE_BUCKET_KEY = 'storageBucket';
- /**
- * 2 minutes
- *
- * The timeout for all operations except upload.
- */
- var DEFAULT_MAX_OPERATION_RETRY_TIME = 2 * 60 * 1000;
- /**
- * 10 minutes
- *
- * The timeout for upload.
- */
- var DEFAULT_MAX_UPLOAD_RETRY_TIME = 10 * 60 * 1000;
- /**
- * 1 second
- */
- var DEFAULT_MIN_SLEEP_TIME_MILLIS = 1000;
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An error returned by the Firebase Storage SDK.
- * @public
- */
- var StorageError = /** @class */ (function (_super) {
- tslib.__extends(StorageError, _super);
- /**
- * @param code - A StorageErrorCode string to be prefixed with 'storage/' and
- * added to the end of the message.
- * @param message - Error message.
- * @param status_ - Corresponding HTTP Status Code
- */
- function StorageError(code, message, status_) {
- if (status_ === void 0) { status_ = 0; }
- var _this = _super.call(this, prependCode(code), "Firebase Storage: ".concat(message, " (").concat(prependCode(code), ")")) || this;
- _this.status_ = status_;
- /**
- * Stores custom error data unque to StorageError.
- */
- _this.customData = { serverResponse: null };
- _this._baseMessage = _this.message;
- // Without this, `instanceof StorageError`, in tests for example,
- // returns false.
- Object.setPrototypeOf(_this, StorageError.prototype);
- return _this;
- }
- Object.defineProperty(StorageError.prototype, "status", {
- get: function () {
- return this.status_;
- },
- set: function (status) {
- this.status_ = status;
- },
- enumerable: false,
- configurable: true
- });
- /**
- * Compares a StorageErrorCode against this error's code, filtering out the prefix.
- */
- StorageError.prototype._codeEquals = function (code) {
- return prependCode(code) === this.code;
- };
- Object.defineProperty(StorageError.prototype, "serverResponse", {
- /**
- * Optional response message that was added by the server.
- */
- get: function () {
- return this.customData.serverResponse;
- },
- set: function (serverResponse) {
- this.customData.serverResponse = serverResponse;
- if (this.customData.serverResponse) {
- this.message = "".concat(this._baseMessage, "\n").concat(this.customData.serverResponse);
- }
- else {
- this.message = this._baseMessage;
- }
- },
- enumerable: false,
- configurable: true
- });
- return StorageError;
- }(util.FirebaseError));
- function prependCode(code) {
- return 'storage/' + code;
- }
- function unknown() {
- var message = 'An unknown error occurred, please check the error payload for ' +
- 'server response.';
- return new StorageError("unknown" /* StorageErrorCode.UNKNOWN */, message);
- }
- function objectNotFound(path) {
- return new StorageError("object-not-found" /* StorageErrorCode.OBJECT_NOT_FOUND */, "Object '" + path + "' does not exist.");
- }
- function quotaExceeded(bucket) {
- return new StorageError("quota-exceeded" /* StorageErrorCode.QUOTA_EXCEEDED */, "Quota for bucket '" +
- bucket +
- "' exceeded, please view quota on " +
- 'https://firebase.google.com/pricing/.');
- }
- function unauthenticated() {
- var message = 'User is not authenticated, please authenticate using Firebase ' +
- 'Authentication and try again.';
- return new StorageError("unauthenticated" /* StorageErrorCode.UNAUTHENTICATED */, message);
- }
- function unauthorizedApp() {
- return new StorageError("unauthorized-app" /* StorageErrorCode.UNAUTHORIZED_APP */, 'This app does not have permission to access Firebase Storage on this project.');
- }
- function unauthorized(path) {
- return new StorageError("unauthorized" /* StorageErrorCode.UNAUTHORIZED */, "User does not have permission to access '" + path + "'.");
- }
- function retryLimitExceeded() {
- return new StorageError("retry-limit-exceeded" /* StorageErrorCode.RETRY_LIMIT_EXCEEDED */, 'Max retry time for operation exceeded, please try again.');
- }
- function canceled() {
- return new StorageError("canceled" /* StorageErrorCode.CANCELED */, 'User canceled the upload/download.');
- }
- function invalidUrl(url) {
- return new StorageError("invalid-url" /* StorageErrorCode.INVALID_URL */, "Invalid URL '" + url + "'.");
- }
- function invalidDefaultBucket(bucket) {
- return new StorageError("invalid-default-bucket" /* StorageErrorCode.INVALID_DEFAULT_BUCKET */, "Invalid default bucket '" + bucket + "'.");
- }
- function noDefaultBucket() {
- return new StorageError("no-default-bucket" /* StorageErrorCode.NO_DEFAULT_BUCKET */, 'No default bucket ' +
- "found. Did you set the '" +
- CONFIG_STORAGE_BUCKET_KEY +
- "' property when initializing the app?");
- }
- function cannotSliceBlob() {
- return new StorageError("cannot-slice-blob" /* StorageErrorCode.CANNOT_SLICE_BLOB */, 'Cannot slice blob for upload. Please retry the upload.');
- }
- function serverFileWrongSize() {
- return new StorageError("server-file-wrong-size" /* StorageErrorCode.SERVER_FILE_WRONG_SIZE */, 'Server recorded incorrect upload file size, please retry the upload.');
- }
- function noDownloadURL() {
- return new StorageError("no-download-url" /* StorageErrorCode.NO_DOWNLOAD_URL */, 'The given file does not have any download URLs.');
- }
- function missingPolyFill(polyFill) {
- return new StorageError("unsupported-environment" /* StorageErrorCode.UNSUPPORTED_ENVIRONMENT */, "".concat(polyFill, " is missing. Make sure to install the required polyfills. See https://firebase.google.com/docs/web/environments-js-sdk#polyfills for more information."));
- }
- /**
- * @internal
- */
- function invalidArgument(message) {
- return new StorageError("invalid-argument" /* StorageErrorCode.INVALID_ARGUMENT */, message);
- }
- function appDeleted() {
- return new StorageError("app-deleted" /* StorageErrorCode.APP_DELETED */, 'The Firebase app was deleted.');
- }
- /**
- * @param name - The name of the operation that was invalid.
- *
- * @internal
- */
- function invalidRootOperation(name) {
- return new StorageError("invalid-root-operation" /* StorageErrorCode.INVALID_ROOT_OPERATION */, "The operation '" +
- name +
- "' cannot be performed on a root reference, create a non-root " +
- "reference using child, such as .child('file.png').");
- }
- /**
- * @param format - The format that was not valid.
- * @param message - A message describing the format violation.
- */
- function invalidFormat(format, message) {
- return new StorageError("invalid-format" /* StorageErrorCode.INVALID_FORMAT */, "String does not match format '" + format + "': " + message);
- }
- /**
- * @param message - A message describing the internal error.
- */
- function internalError(message) {
- throw new StorageError("internal-error" /* StorageErrorCode.INTERNAL_ERROR */, 'Internal error: ' + message);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Firebase Storage location data.
- *
- * @internal
- */
- var Location = /** @class */ (function () {
- function Location(bucket, path) {
- this.bucket = bucket;
- this.path_ = path;
- }
- Object.defineProperty(Location.prototype, "path", {
- get: function () {
- return this.path_;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Location.prototype, "isRoot", {
- get: function () {
- return this.path.length === 0;
- },
- enumerable: false,
- configurable: true
- });
- Location.prototype.fullServerUrl = function () {
- var encode = encodeURIComponent;
- return '/b/' + encode(this.bucket) + '/o/' + encode(this.path);
- };
- Location.prototype.bucketOnlyServerUrl = function () {
- var encode = encodeURIComponent;
- return '/b/' + encode(this.bucket) + '/o';
- };
- Location.makeFromBucketSpec = function (bucketString, host) {
- var bucketLocation;
- try {
- bucketLocation = Location.makeFromUrl(bucketString, host);
- }
- catch (e) {
- // Not valid URL, use as-is. This lets you put bare bucket names in
- // config.
- return new Location(bucketString, '');
- }
- if (bucketLocation.path === '') {
- return bucketLocation;
- }
- else {
- throw invalidDefaultBucket(bucketString);
- }
- };
- Location.makeFromUrl = function (url, host) {
- var location = null;
- var bucketDomain = '([A-Za-z0-9.\\-_]+)';
- function gsModify(loc) {
- if (loc.path.charAt(loc.path.length - 1) === '/') {
- loc.path_ = loc.path_.slice(0, -1);
- }
- }
- var gsPath = '(/(.*))?$';
- var gsRegex = new RegExp('^gs://' + bucketDomain + gsPath, 'i');
- var gsIndices = { bucket: 1, path: 3 };
- function httpModify(loc) {
- loc.path_ = decodeURIComponent(loc.path);
- }
- var version = 'v[A-Za-z0-9_]+';
- var firebaseStorageHost = host.replace(/[.]/g, '\\.');
- var firebaseStoragePath = '(/([^?#]*).*)?$';
- var firebaseStorageRegExp = new RegExp("^https?://".concat(firebaseStorageHost, "/").concat(version, "/b/").concat(bucketDomain, "/o").concat(firebaseStoragePath), 'i');
- var firebaseStorageIndices = { bucket: 1, path: 3 };
- var cloudStorageHost = host === DEFAULT_HOST
- ? '(?:storage.googleapis.com|storage.cloud.google.com)'
- : host;
- var cloudStoragePath = '([^?#]*)';
- var cloudStorageRegExp = new RegExp("^https?://".concat(cloudStorageHost, "/").concat(bucketDomain, "/").concat(cloudStoragePath), 'i');
- var cloudStorageIndices = { bucket: 1, path: 2 };
- var groups = [
- { regex: gsRegex, indices: gsIndices, postModify: gsModify },
- {
- regex: firebaseStorageRegExp,
- indices: firebaseStorageIndices,
- postModify: httpModify
- },
- {
- regex: cloudStorageRegExp,
- indices: cloudStorageIndices,
- postModify: httpModify
- }
- ];
- for (var i = 0; i < groups.length; i++) {
- var group = groups[i];
- var captures = group.regex.exec(url);
- if (captures) {
- var bucketValue = captures[group.indices.bucket];
- var pathValue = captures[group.indices.path];
- if (!pathValue) {
- pathValue = '';
- }
- location = new Location(bucketValue, pathValue);
- group.postModify(location);
- break;
- }
- }
- if (location == null) {
- throw invalidUrl(url);
- }
- return location;
- };
- return Location;
- }());
-
- /**
- * A request whose promise always fails.
- */
- var FailRequest = /** @class */ (function () {
- function FailRequest(error) {
- this.promise_ = Promise.reject(error);
- }
- /** @inheritDoc */
- FailRequest.prototype.getPromise = function () {
- return this.promise_;
- };
- /** @inheritDoc */
- FailRequest.prototype.cancel = function (_appDelete) {
- };
- return FailRequest;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Accepts a callback for an action to perform (`doRequest`),
- * and then a callback for when the backoff has completed (`backoffCompleteCb`).
- * The callback sent to start requires an argument to call (`onRequestComplete`).
- * When `start` calls `doRequest`, it passes a callback for when the request has
- * completed, `onRequestComplete`. Based on this, the backoff continues, with
- * another call to `doRequest` and the above loop continues until the timeout
- * is hit, or a successful response occurs.
- * @description
- * @param doRequest Callback to perform request
- * @param backoffCompleteCb Callback to call when backoff has been completed
- */
- function start(doRequest,
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- backoffCompleteCb, timeout) {
- // TODO(andysoto): make this code cleaner (probably refactor into an actual
- // type instead of a bunch of functions with state shared in the closure)
- var waitSeconds = 1;
- // Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
- // TODO: find a way to exclude Node type definition for storage because storage only works in browser
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var retryTimeoutId = null;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- var globalTimeoutId = null;
- var hitTimeout = false;
- var cancelState = 0;
- function canceled() {
- return cancelState === 2;
- }
- var triggeredCallback = false;
- function triggerCallback() {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- if (!triggeredCallback) {
- triggeredCallback = true;
- backoffCompleteCb.apply(null, args);
- }
- }
- function callWithDelay(millis) {
- retryTimeoutId = setTimeout(function () {
- retryTimeoutId = null;
- doRequest(responseHandler, canceled());
- }, millis);
- }
- function clearGlobalTimeout() {
- if (globalTimeoutId) {
- clearTimeout(globalTimeoutId);
- }
- }
- function responseHandler(success) {
- var args = [];
- for (var _i = 1; _i < arguments.length; _i++) {
- args[_i - 1] = arguments[_i];
- }
- if (triggeredCallback) {
- clearGlobalTimeout();
- return;
- }
- if (success) {
- clearGlobalTimeout();
- triggerCallback.call.apply(triggerCallback, tslib.__spreadArray([null, success], args, false));
- return;
- }
- var mustStop = canceled() || hitTimeout;
- if (mustStop) {
- clearGlobalTimeout();
- triggerCallback.call.apply(triggerCallback, tslib.__spreadArray([null, success], args, false));
- return;
- }
- if (waitSeconds < 64) {
- /* TODO(andysoto): don't back off so quickly if we know we're offline. */
- waitSeconds *= 2;
- }
- var waitMillis;
- if (cancelState === 1) {
- cancelState = 2;
- waitMillis = 0;
- }
- else {
- waitMillis = (waitSeconds + Math.random()) * 1000;
- }
- callWithDelay(waitMillis);
- }
- var stopped = false;
- function stop(wasTimeout) {
- if (stopped) {
- return;
- }
- stopped = true;
- clearGlobalTimeout();
- if (triggeredCallback) {
- return;
- }
- if (retryTimeoutId !== null) {
- if (!wasTimeout) {
- cancelState = 2;
- }
- clearTimeout(retryTimeoutId);
- callWithDelay(0);
- }
- else {
- if (!wasTimeout) {
- cancelState = 1;
- }
- }
- }
- callWithDelay(0);
- globalTimeoutId = setTimeout(function () {
- hitTimeout = true;
- stop(true);
- }, timeout);
- return stop;
- }
- /**
- * Stops the retry loop from repeating.
- * If the function is currently "in between" retries, it is invoked immediately
- * with the second parameter as "true". Otherwise, it will be invoked once more
- * after the current invocation finishes iff the current invocation would have
- * triggered another retry.
- */
- function stop(id) {
- id(false);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function isJustDef(p) {
- return p !== void 0;
- }
- // eslint-disable-next-line @typescript-eslint/ban-types
- function isFunction(p) {
- return typeof p === 'function';
- }
- function isNonArrayObject(p) {
- return typeof p === 'object' && !Array.isArray(p);
- }
- function isString(p) {
- return typeof p === 'string' || p instanceof String;
- }
- function isNativeBlob(p) {
- return isNativeBlobDefined() && p instanceof Blob;
- }
- function isNativeBlobDefined() {
- // Note: The `isNode()` check can be removed when `node-fetch` adds native Blob support
- // PR: https://github.com/node-fetch/node-fetch/pull/1664
- return typeof Blob !== 'undefined' && !util.isNode();
- }
- function validateNumber(argument, minValue, maxValue, value) {
- if (value < minValue) {
- throw invalidArgument("Invalid value for '".concat(argument, "'. Expected ").concat(minValue, " or greater."));
- }
- if (value > maxValue) {
- throw invalidArgument("Invalid value for '".concat(argument, "'. Expected ").concat(maxValue, " or less."));
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function makeUrl(urlPart, host, protocol) {
- var origin = host;
- if (protocol == null) {
- origin = "https://".concat(host);
- }
- return "".concat(protocol, "://").concat(origin, "/v0").concat(urlPart);
- }
- function makeQueryString(params) {
- var encode = encodeURIComponent;
- var queryPart = '?';
- for (var key in params) {
- if (params.hasOwnProperty(key)) {
- var nextPart = encode(key) + '=' + encode(params[key]);
- queryPart = queryPart + nextPart + '&';
- }
- }
- // Chop off the extra '&' or '?' on the end
- queryPart = queryPart.slice(0, -1);
- return queryPart;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Error codes for requests made by the the XhrIo wrapper.
- */
- var ErrorCode;
- (function (ErrorCode) {
- ErrorCode[ErrorCode["NO_ERROR"] = 0] = "NO_ERROR";
- ErrorCode[ErrorCode["NETWORK_ERROR"] = 1] = "NETWORK_ERROR";
- ErrorCode[ErrorCode["ABORT"] = 2] = "ABORT";
- })(ErrorCode || (ErrorCode = {}));
-
- /**
- * @license
- * Copyright 2022 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Checks the status code to see if the action should be retried.
- *
- * @param status Current HTTP status code returned by server.
- * @param additionalRetryCodes additional retry codes to check against
- */
- function isRetryStatusCode(status, additionalRetryCodes) {
- // The codes for which to retry came from this page:
- // https://cloud.google.com/storage/docs/exponential-backoff
- var isFiveHundredCode = status >= 500 && status < 600;
- var extraRetryCodes = [
- // Request Timeout: web server didn't receive full request in time.
- 408,
- // Too Many Requests: you're getting rate-limited, basically.
- 429
- ];
- var isExtraRetryCode = extraRetryCodes.indexOf(status) !== -1;
- var isAdditionalRetryCode = additionalRetryCodes.indexOf(status) !== -1;
- return isFiveHundredCode || isExtraRetryCode || isAdditionalRetryCode;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Handles network logic for all Storage Requests, including error reporting and
- * retries with backoff.
- *
- * @param I - the type of the backend's network response.
- * @param - O the output type used by the rest of the SDK. The conversion
- * happens in the specified `callback_`.
- */
- var NetworkRequest = /** @class */ (function () {
- function NetworkRequest(url_, method_, headers_, body_, successCodes_, additionalRetryCodes_, callback_, errorCallback_, timeout_, progressCallback_, connectionFactory_, retry) {
- if (retry === void 0) { retry = true; }
- var _this = this;
- this.url_ = url_;
- this.method_ = method_;
- this.headers_ = headers_;
- this.body_ = body_;
- this.successCodes_ = successCodes_;
- this.additionalRetryCodes_ = additionalRetryCodes_;
- this.callback_ = callback_;
- this.errorCallback_ = errorCallback_;
- this.timeout_ = timeout_;
- this.progressCallback_ = progressCallback_;
- this.connectionFactory_ = connectionFactory_;
- this.retry = retry;
- this.pendingConnection_ = null;
- this.backoffId_ = null;
- this.canceled_ = false;
- this.appDelete_ = false;
- this.promise_ = new Promise(function (resolve, reject) {
- _this.resolve_ = resolve;
- _this.reject_ = reject;
- _this.start_();
- });
- }
- /**
- * Actually starts the retry loop.
- */
- NetworkRequest.prototype.start_ = function () {
- var _this = this;
- var doTheRequest = function (backoffCallback, canceled) {
- if (canceled) {
- backoffCallback(false, new RequestEndStatus(false, null, true));
- return;
- }
- var connection = _this.connectionFactory_();
- _this.pendingConnection_ = connection;
- var progressListener = function (progressEvent) {
- var loaded = progressEvent.loaded;
- var total = progressEvent.lengthComputable ? progressEvent.total : -1;
- if (_this.progressCallback_ !== null) {
- _this.progressCallback_(loaded, total);
- }
- };
- if (_this.progressCallback_ !== null) {
- connection.addUploadProgressListener(progressListener);
- }
- // connection.send() never rejects, so we don't need to have a error handler or use catch on the returned promise.
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- connection
- .send(_this.url_, _this.method_, _this.body_, _this.headers_)
- .then(function () {
- if (_this.progressCallback_ !== null) {
- connection.removeUploadProgressListener(progressListener);
- }
- _this.pendingConnection_ = null;
- var hitServer = connection.getErrorCode() === ErrorCode.NO_ERROR;
- var status = connection.getStatus();
- if (!hitServer ||
- (isRetryStatusCode(status, _this.additionalRetryCodes_) &&
- _this.retry)) {
- var wasCanceled = connection.getErrorCode() === ErrorCode.ABORT;
- backoffCallback(false, new RequestEndStatus(false, null, wasCanceled));
- return;
- }
- var successCode = _this.successCodes_.indexOf(status) !== -1;
- backoffCallback(true, new RequestEndStatus(successCode, connection));
- });
- };
- /**
- * @param requestWentThrough - True if the request eventually went
- * through, false if it hit the retry limit or was canceled.
- */
- var backoffDone = function (requestWentThrough, status) {
- var resolve = _this.resolve_;
- var reject = _this.reject_;
- var connection = status.connection;
- if (status.wasSuccessCode) {
- try {
- var result = _this.callback_(connection, connection.getResponse());
- if (isJustDef(result)) {
- resolve(result);
- }
- else {
- resolve();
- }
- }
- catch (e) {
- reject(e);
- }
- }
- else {
- if (connection !== null) {
- var err = unknown();
- err.serverResponse = connection.getErrorText();
- if (_this.errorCallback_) {
- reject(_this.errorCallback_(connection, err));
- }
- else {
- reject(err);
- }
- }
- else {
- if (status.canceled) {
- var err = _this.appDelete_ ? appDeleted() : canceled();
- reject(err);
- }
- else {
- var err = retryLimitExceeded();
- reject(err);
- }
- }
- }
- };
- if (this.canceled_) {
- backoffDone(false, new RequestEndStatus(false, null, true));
- }
- else {
- this.backoffId_ = start(doTheRequest, backoffDone, this.timeout_);
- }
- };
- /** @inheritDoc */
- NetworkRequest.prototype.getPromise = function () {
- return this.promise_;
- };
- /** @inheritDoc */
- NetworkRequest.prototype.cancel = function (appDelete) {
- this.canceled_ = true;
- this.appDelete_ = appDelete || false;
- if (this.backoffId_ !== null) {
- stop(this.backoffId_);
- }
- if (this.pendingConnection_ !== null) {
- this.pendingConnection_.abort();
- }
- };
- return NetworkRequest;
- }());
- /**
- * A collection of information about the result of a network request.
- * @param opt_canceled - Defaults to false.
- */
- var RequestEndStatus = /** @class */ (function () {
- function RequestEndStatus(wasSuccessCode, connection, canceled) {
- this.wasSuccessCode = wasSuccessCode;
- this.connection = connection;
- this.canceled = !!canceled;
- }
- return RequestEndStatus;
- }());
- function addAuthHeader_(headers, authToken) {
- if (authToken !== null && authToken.length > 0) {
- headers['Authorization'] = 'Firebase ' + authToken;
- }
- }
- function addVersionHeader_(headers, firebaseVersion) {
- headers['X-Firebase-Storage-Version'] =
- 'webjs/' + (firebaseVersion !== null && firebaseVersion !== void 0 ? firebaseVersion : 'AppManager');
- }
- function addGmpidHeader_(headers, appId) {
- if (appId) {
- headers['X-Firebase-GMPID'] = appId;
- }
- }
- function addAppCheckHeader_(headers, appCheckToken) {
- if (appCheckToken !== null) {
- headers['X-Firebase-AppCheck'] = appCheckToken;
- }
- }
- function makeRequest(requestInfo, appId, authToken, appCheckToken, requestFactory, firebaseVersion, retry) {
- if (retry === void 0) { retry = true; }
- var queryPart = makeQueryString(requestInfo.urlParams);
- var url = requestInfo.url + queryPart;
- var headers = Object.assign({}, requestInfo.headers);
- addGmpidHeader_(headers, appId);
- addAuthHeader_(headers, authToken);
- addVersionHeader_(headers, firebaseVersion);
- addAppCheckHeader_(headers, appCheckToken);
- return new NetworkRequest(url, requestInfo.method, headers, requestInfo.body, requestInfo.successCodes, requestInfo.additionalRetryCodes, requestInfo.handler, requestInfo.errorHandler, requestInfo.timeout, requestInfo.progressCallback, requestFactory, retry);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function getBlobBuilder() {
- if (typeof BlobBuilder !== 'undefined') {
- return BlobBuilder;
- }
- else if (typeof WebKitBlobBuilder !== 'undefined') {
- return WebKitBlobBuilder;
- }
- else {
- return undefined;
- }
- }
- /**
- * Concatenates one or more values together and converts them to a Blob.
- *
- * @param args The values that will make up the resulting blob.
- * @return The blob.
- */
- function getBlob$1() {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- var BlobBuilder = getBlobBuilder();
- if (BlobBuilder !== undefined) {
- var bb = new BlobBuilder();
- for (var i = 0; i < args.length; i++) {
- bb.append(args[i]);
- }
- return bb.getBlob();
- }
- else {
- if (isNativeBlobDefined()) {
- return new Blob(args);
- }
- else {
- throw new StorageError("unsupported-environment" /* StorageErrorCode.UNSUPPORTED_ENVIRONMENT */, "This browser doesn't seem to support creating Blobs");
- }
- }
- }
- /**
- * Slices the blob. The returned blob contains data from the start byte
- * (inclusive) till the end byte (exclusive). Negative indices cannot be used.
- *
- * @param blob The blob to be sliced.
- * @param start Index of the starting byte.
- * @param end Index of the ending byte.
- * @return The blob slice or null if not supported.
- */
- function sliceBlob(blob, start, end) {
- if (blob.webkitSlice) {
- return blob.webkitSlice(start, end);
- }
- else if (blob.mozSlice) {
- return blob.mozSlice(start, end);
- }
- else if (blob.slice) {
- return blob.slice(start, end);
- }
- return null;
- }
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /** Converts a Base64 encoded string to a binary string. */
- function decodeBase64(encoded) {
- if (typeof atob === 'undefined') {
- throw missingPolyFill('base-64');
- }
- return atob(encoded);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An enumeration of the possible string formats for upload.
- * @public
- */
- var StringFormat = {
- /**
- * Indicates the string should be interpreted "raw", that is, as normal text.
- * The string will be interpreted as UTF-16, then uploaded as a UTF-8 byte
- * sequence.
- * Example: The string 'Hello! \\ud83d\\ude0a' becomes the byte sequence
- * 48 65 6c 6c 6f 21 20 f0 9f 98 8a
- */
- RAW: 'raw',
- /**
- * Indicates the string should be interpreted as base64-encoded data.
- * Padding characters (trailing '='s) are optional.
- * Example: The string 'rWmO++E6t7/rlw==' becomes the byte sequence
- * ad 69 8e fb e1 3a b7 bf eb 97
- */
- BASE64: 'base64',
- /**
- * Indicates the string should be interpreted as base64url-encoded data.
- * Padding characters (trailing '='s) are optional.
- * Example: The string 'rWmO--E6t7_rlw==' becomes the byte sequence
- * ad 69 8e fb e1 3a b7 bf eb 97
- */
- BASE64URL: 'base64url',
- /**
- * Indicates the string is a data URL, such as one obtained from
- * canvas.toDataURL().
- * Example: the string 'data:application/octet-stream;base64,aaaa'
- * becomes the byte sequence
- * 69 a6 9a
- * (the content-type "application/octet-stream" is also applied, but can
- * be overridden in the metadata object).
- */
- DATA_URL: 'data_url'
- };
- var StringData = /** @class */ (function () {
- function StringData(data, contentType) {
- this.data = data;
- this.contentType = contentType || null;
- }
- return StringData;
- }());
- /**
- * @internal
- */
- function dataFromString(format, stringData) {
- switch (format) {
- case StringFormat.RAW:
- return new StringData(utf8Bytes_(stringData));
- case StringFormat.BASE64:
- case StringFormat.BASE64URL:
- return new StringData(base64Bytes_(format, stringData));
- case StringFormat.DATA_URL:
- return new StringData(dataURLBytes_(stringData), dataURLContentType_(stringData));
- // do nothing
- }
- // assert(false);
- throw unknown();
- }
- function utf8Bytes_(value) {
- var b = [];
- for (var i = 0; i < value.length; i++) {
- var c = value.charCodeAt(i);
- if (c <= 127) {
- b.push(c);
- }
- else {
- if (c <= 2047) {
- b.push(192 | (c >> 6), 128 | (c & 63));
- }
- else {
- if ((c & 64512) === 55296) {
- // The start of a surrogate pair.
- var valid = i < value.length - 1 && (value.charCodeAt(i + 1) & 64512) === 56320;
- if (!valid) {
- // The second surrogate wasn't there.
- b.push(239, 191, 189);
- }
- else {
- var hi = c;
- var lo = value.charCodeAt(++i);
- c = 65536 | ((hi & 1023) << 10) | (lo & 1023);
- b.push(240 | (c >> 18), 128 | ((c >> 12) & 63), 128 | ((c >> 6) & 63), 128 | (c & 63));
- }
- }
- else {
- if ((c & 64512) === 56320) {
- // Invalid low surrogate.
- b.push(239, 191, 189);
- }
- else {
- b.push(224 | (c >> 12), 128 | ((c >> 6) & 63), 128 | (c & 63));
- }
- }
- }
- }
- }
- return new Uint8Array(b);
- }
- function percentEncodedBytes_(value) {
- var decoded;
- try {
- decoded = decodeURIComponent(value);
- }
- catch (e) {
- throw invalidFormat(StringFormat.DATA_URL, 'Malformed data URL.');
- }
- return utf8Bytes_(decoded);
- }
- function base64Bytes_(format, value) {
- switch (format) {
- case StringFormat.BASE64: {
- var hasMinus = value.indexOf('-') !== -1;
- var hasUnder = value.indexOf('_') !== -1;
- if (hasMinus || hasUnder) {
- var invalidChar = hasMinus ? '-' : '_';
- throw invalidFormat(format, "Invalid character '" +
- invalidChar +
- "' found: is it base64url encoded?");
- }
- break;
- }
- case StringFormat.BASE64URL: {
- var hasPlus = value.indexOf('+') !== -1;
- var hasSlash = value.indexOf('/') !== -1;
- if (hasPlus || hasSlash) {
- var invalidChar = hasPlus ? '+' : '/';
- throw invalidFormat(format, "Invalid character '" + invalidChar + "' found: is it base64 encoded?");
- }
- value = value.replace(/-/g, '+').replace(/_/g, '/');
- break;
- }
- // do nothing
- }
- var bytes;
- try {
- bytes = decodeBase64(value);
- }
- catch (e) {
- if (e.message.includes('polyfill')) {
- throw e;
- }
- throw invalidFormat(format, 'Invalid character found');
- }
- var array = new Uint8Array(bytes.length);
- for (var i = 0; i < bytes.length; i++) {
- array[i] = bytes.charCodeAt(i);
- }
- return array;
- }
- var DataURLParts = /** @class */ (function () {
- function DataURLParts(dataURL) {
- this.base64 = false;
- this.contentType = null;
- var matches = dataURL.match(/^data:([^,]+)?,/);
- if (matches === null) {
- throw invalidFormat(StringFormat.DATA_URL, "Must be formatted 'data:[<mediatype>][;base64],<data>");
- }
- var middle = matches[1] || null;
- if (middle != null) {
- this.base64 = endsWith(middle, ';base64');
- this.contentType = this.base64
- ? middle.substring(0, middle.length - ';base64'.length)
- : middle;
- }
- this.rest = dataURL.substring(dataURL.indexOf(',') + 1);
- }
- return DataURLParts;
- }());
- function dataURLBytes_(dataUrl) {
- var parts = new DataURLParts(dataUrl);
- if (parts.base64) {
- return base64Bytes_(StringFormat.BASE64, parts.rest);
- }
- else {
- return percentEncodedBytes_(parts.rest);
- }
- }
- function dataURLContentType_(dataUrl) {
- var parts = new DataURLParts(dataUrl);
- return parts.contentType;
- }
- function endsWith(s, end) {
- var longEnough = s.length >= end.length;
- if (!longEnough) {
- return false;
- }
- return s.substring(s.length - end.length) === end;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @param opt_elideCopy - If true, doesn't copy mutable input data
- * (e.g. Uint8Arrays). Pass true only if you know the objects will not be
- * modified after this blob's construction.
- *
- * @internal
- */
- var FbsBlob = /** @class */ (function () {
- function FbsBlob(data, elideCopy) {
- var size = 0;
- var blobType = '';
- if (isNativeBlob(data)) {
- this.data_ = data;
- size = data.size;
- blobType = data.type;
- }
- else if (data instanceof ArrayBuffer) {
- if (elideCopy) {
- this.data_ = new Uint8Array(data);
- }
- else {
- this.data_ = new Uint8Array(data.byteLength);
- this.data_.set(new Uint8Array(data));
- }
- size = this.data_.length;
- }
- else if (data instanceof Uint8Array) {
- if (elideCopy) {
- this.data_ = data;
- }
- else {
- this.data_ = new Uint8Array(data.length);
- this.data_.set(data);
- }
- size = data.length;
- }
- this.size_ = size;
- this.type_ = blobType;
- }
- FbsBlob.prototype.size = function () {
- return this.size_;
- };
- FbsBlob.prototype.type = function () {
- return this.type_;
- };
- FbsBlob.prototype.slice = function (startByte, endByte) {
- if (isNativeBlob(this.data_)) {
- var realBlob = this.data_;
- var sliced = sliceBlob(realBlob, startByte, endByte);
- if (sliced === null) {
- return null;
- }
- return new FbsBlob(sliced);
- }
- else {
- var slice = new Uint8Array(this.data_.buffer, startByte, endByte - startByte);
- return new FbsBlob(slice, true);
- }
- };
- FbsBlob.getBlob = function () {
- var args = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- args[_i] = arguments[_i];
- }
- if (isNativeBlobDefined()) {
- var blobby = args.map(function (val) {
- if (val instanceof FbsBlob) {
- return val.data_;
- }
- else {
- return val;
- }
- });
- return new FbsBlob(getBlob$1.apply(null, blobby));
- }
- else {
- var uint8Arrays = args.map(function (val) {
- if (isString(val)) {
- return dataFromString(StringFormat.RAW, val).data;
- }
- else {
- // Blobs don't exist, so this has to be a Uint8Array.
- return val.data_;
- }
- });
- var finalLength_1 = 0;
- uint8Arrays.forEach(function (array) {
- finalLength_1 += array.byteLength;
- });
- var merged_1 = new Uint8Array(finalLength_1);
- var index_1 = 0;
- uint8Arrays.forEach(function (array) {
- for (var i = 0; i < array.length; i++) {
- merged_1[index_1++] = array[i];
- }
- });
- return new FbsBlob(merged_1, true);
- }
- };
- FbsBlob.prototype.uploadData = function () {
- return this.data_;
- };
- return FbsBlob;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Returns the Object resulting from parsing the given JSON, or null if the
- * given string does not represent a JSON object.
- */
- function jsonObjectOrNull(s) {
- var obj;
- try {
- obj = JSON.parse(s);
- }
- catch (e) {
- return null;
- }
- if (isNonArrayObject(obj)) {
- return obj;
- }
- else {
- return null;
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * @fileoverview Contains helper methods for manipulating paths.
- */
- /**
- * @return Null if the path is already at the root.
- */
- function parent(path) {
- if (path.length === 0) {
- return null;
- }
- var index = path.lastIndexOf('/');
- if (index === -1) {
- return '';
- }
- var newPath = path.slice(0, index);
- return newPath;
- }
- function child(path, childPath) {
- var canonicalChildPath = childPath
- .split('/')
- .filter(function (component) { return component.length > 0; })
- .join('/');
- if (path.length === 0) {
- return canonicalChildPath;
- }
- else {
- return path + '/' + canonicalChildPath;
- }
- }
- /**
- * Returns the last component of a path.
- * '/foo/bar' -> 'bar'
- * '/foo/bar/baz/' -> 'baz/'
- * '/a' -> 'a'
- */
- function lastComponent(path) {
- var index = path.lastIndexOf('/', path.length - 2);
- if (index === -1) {
- return path;
- }
- else {
- return path.slice(index + 1);
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function noXform_(metadata, value) {
- return value;
- }
- var Mapping = /** @class */ (function () {
- function Mapping(server, local, writable, xform) {
- this.server = server;
- this.local = local || server;
- this.writable = !!writable;
- this.xform = xform || noXform_;
- }
- return Mapping;
- }());
- var mappings_ = null;
- function xformPath(fullPath) {
- if (!isString(fullPath) || fullPath.length < 2) {
- return fullPath;
- }
- else {
- return lastComponent(fullPath);
- }
- }
- function getMappings() {
- if (mappings_) {
- return mappings_;
- }
- var mappings = [];
- mappings.push(new Mapping('bucket'));
- mappings.push(new Mapping('generation'));
- mappings.push(new Mapping('metageneration'));
- mappings.push(new Mapping('name', 'fullPath', true));
- function mappingsXformPath(_metadata, fullPath) {
- return xformPath(fullPath);
- }
- var nameMapping = new Mapping('name');
- nameMapping.xform = mappingsXformPath;
- mappings.push(nameMapping);
- /**
- * Coerces the second param to a number, if it is defined.
- */
- function xformSize(_metadata, size) {
- if (size !== undefined) {
- return Number(size);
- }
- else {
- return size;
- }
- }
- var sizeMapping = new Mapping('size');
- sizeMapping.xform = xformSize;
- mappings.push(sizeMapping);
- mappings.push(new Mapping('timeCreated'));
- mappings.push(new Mapping('updated'));
- mappings.push(new Mapping('md5Hash', null, true));
- mappings.push(new Mapping('cacheControl', null, true));
- mappings.push(new Mapping('contentDisposition', null, true));
- mappings.push(new Mapping('contentEncoding', null, true));
- mappings.push(new Mapping('contentLanguage', null, true));
- mappings.push(new Mapping('contentType', null, true));
- mappings.push(new Mapping('metadata', 'customMetadata', true));
- mappings_ = mappings;
- return mappings_;
- }
- function addRef(metadata, service) {
- function generateRef() {
- var bucket = metadata['bucket'];
- var path = metadata['fullPath'];
- var loc = new Location(bucket, path);
- return service._makeStorageReference(loc);
- }
- Object.defineProperty(metadata, 'ref', { get: generateRef });
- }
- function fromResource(service, resource, mappings) {
- var metadata = {};
- metadata['type'] = 'file';
- var len = mappings.length;
- for (var i = 0; i < len; i++) {
- var mapping = mappings[i];
- metadata[mapping.local] = mapping.xform(metadata, resource[mapping.server]);
- }
- addRef(metadata, service);
- return metadata;
- }
- function fromResourceString(service, resourceString, mappings) {
- var obj = jsonObjectOrNull(resourceString);
- if (obj === null) {
- return null;
- }
- var resource = obj;
- return fromResource(service, resource, mappings);
- }
- function downloadUrlFromResourceString(metadata, resourceString, host, protocol) {
- var obj = jsonObjectOrNull(resourceString);
- if (obj === null) {
- return null;
- }
- if (!isString(obj['downloadTokens'])) {
- // This can happen if objects are uploaded through GCS and retrieved
- // through list, so we don't want to throw an Error.
- return null;
- }
- var tokens = obj['downloadTokens'];
- if (tokens.length === 0) {
- return null;
- }
- var encode = encodeURIComponent;
- var tokensList = tokens.split(',');
- var urls = tokensList.map(function (token) {
- var bucket = metadata['bucket'];
- var path = metadata['fullPath'];
- var urlPart = '/b/' + encode(bucket) + '/o/' + encode(path);
- var base = makeUrl(urlPart, host, protocol);
- var queryString = makeQueryString({
- alt: 'media',
- token: token
- });
- return base + queryString;
- });
- return urls[0];
- }
- function toResourceString(metadata, mappings) {
- var resource = {};
- var len = mappings.length;
- for (var i = 0; i < len; i++) {
- var mapping = mappings[i];
- if (mapping.writable) {
- resource[mapping.server] = metadata[mapping.local];
- }
- }
- return JSON.stringify(resource);
- }
-
- /**
- * @license
- * Copyright 2019 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var PREFIXES_KEY = 'prefixes';
- var ITEMS_KEY = 'items';
- function fromBackendResponse(service, bucket, resource) {
- var listResult = {
- prefixes: [],
- items: [],
- nextPageToken: resource['nextPageToken']
- };
- if (resource[PREFIXES_KEY]) {
- for (var _i = 0, _a = resource[PREFIXES_KEY]; _i < _a.length; _i++) {
- var path = _a[_i];
- var pathWithoutTrailingSlash = path.replace(/\/$/, '');
- var reference = service._makeStorageReference(new Location(bucket, pathWithoutTrailingSlash));
- listResult.prefixes.push(reference);
- }
- }
- if (resource[ITEMS_KEY]) {
- for (var _b = 0, _c = resource[ITEMS_KEY]; _b < _c.length; _b++) {
- var item = _c[_b];
- var reference = service._makeStorageReference(new Location(bucket, item['name']));
- listResult.items.push(reference);
- }
- }
- return listResult;
- }
- function fromResponseString(service, bucket, resourceString) {
- var obj = jsonObjectOrNull(resourceString);
- if (obj === null) {
- return null;
- }
- var resource = obj;
- return fromBackendResponse(service, bucket, resource);
- }
-
- /**
- * Contains a fully specified request.
- *
- * @param I - the type of the backend's network response.
- * @param O - the output response type used by the rest of the SDK.
- */
- var RequestInfo = /** @class */ (function () {
- function RequestInfo(url, method,
- /**
- * Returns the value with which to resolve the request's promise. Only called
- * if the request is successful. Throw from this function to reject the
- * returned Request's promise with the thrown error.
- * Note: The XhrIo passed to this function may be reused after this callback
- * returns. Do not keep a reference to it in any way.
- */
- handler, timeout) {
- this.url = url;
- this.method = method;
- this.handler = handler;
- this.timeout = timeout;
- this.urlParams = {};
- this.headers = {};
- this.body = null;
- this.errorHandler = null;
- /**
- * Called with the current number of bytes uploaded and total size (-1 if not
- * computable) of the request body (i.e. used to report upload progress).
- */
- this.progressCallback = null;
- this.successCodes = [200];
- this.additionalRetryCodes = [];
- }
- return RequestInfo;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Throws the UNKNOWN StorageError if cndn is false.
- */
- function handlerCheck(cndn) {
- if (!cndn) {
- throw unknown();
- }
- }
- function metadataHandler(service, mappings) {
- function handler(xhr, text) {
- var metadata = fromResourceString(service, text, mappings);
- handlerCheck(metadata !== null);
- return metadata;
- }
- return handler;
- }
- function listHandler(service, bucket) {
- function handler(xhr, text) {
- var listResult = fromResponseString(service, bucket, text);
- handlerCheck(listResult !== null);
- return listResult;
- }
- return handler;
- }
- function downloadUrlHandler(service, mappings) {
- function handler(xhr, text) {
- var metadata = fromResourceString(service, text, mappings);
- handlerCheck(metadata !== null);
- return downloadUrlFromResourceString(metadata, text, service.host, service._protocol);
- }
- return handler;
- }
- function sharedErrorHandler(location) {
- function errorHandler(xhr, err) {
- var newErr;
- if (xhr.getStatus() === 401) {
- if (
- // This exact message string is the only consistent part of the
- // server's error response that identifies it as an App Check error.
- xhr.getErrorText().includes('Firebase App Check token is invalid')) {
- newErr = unauthorizedApp();
- }
- else {
- newErr = unauthenticated();
- }
- }
- else {
- if (xhr.getStatus() === 402) {
- newErr = quotaExceeded(location.bucket);
- }
- else {
- if (xhr.getStatus() === 403) {
- newErr = unauthorized(location.path);
- }
- else {
- newErr = err;
- }
- }
- }
- newErr.status = xhr.getStatus();
- newErr.serverResponse = err.serverResponse;
- return newErr;
- }
- return errorHandler;
- }
- function objectErrorHandler(location) {
- var shared = sharedErrorHandler(location);
- function errorHandler(xhr, err) {
- var newErr = shared(xhr, err);
- if (xhr.getStatus() === 404) {
- newErr = objectNotFound(location.path);
- }
- newErr.serverResponse = err.serverResponse;
- return newErr;
- }
- return errorHandler;
- }
- function getMetadata$2(service, location, mappings) {
- var urlPart = location.fullServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'GET';
- var timeout = service.maxOperationRetryTime;
- var requestInfo = new RequestInfo(url, method, metadataHandler(service, mappings), timeout);
- requestInfo.errorHandler = objectErrorHandler(location);
- return requestInfo;
- }
- function list$2(service, location, delimiter, pageToken, maxResults) {
- var urlParams = {};
- if (location.isRoot) {
- urlParams['prefix'] = '';
- }
- else {
- urlParams['prefix'] = location.path + '/';
- }
- if (delimiter && delimiter.length > 0) {
- urlParams['delimiter'] = delimiter;
- }
- if (pageToken) {
- urlParams['pageToken'] = pageToken;
- }
- if (maxResults) {
- urlParams['maxResults'] = maxResults;
- }
- var urlPart = location.bucketOnlyServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'GET';
- var timeout = service.maxOperationRetryTime;
- var requestInfo = new RequestInfo(url, method, listHandler(service, location.bucket), timeout);
- requestInfo.urlParams = urlParams;
- requestInfo.errorHandler = sharedErrorHandler(location);
- return requestInfo;
- }
- function getBytes$1(service, location, maxDownloadSizeBytes) {
- var urlPart = location.fullServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol) + '?alt=media';
- var method = 'GET';
- var timeout = service.maxOperationRetryTime;
- var requestInfo = new RequestInfo(url, method, function (_, data) { return data; }, timeout);
- requestInfo.errorHandler = objectErrorHandler(location);
- if (maxDownloadSizeBytes !== undefined) {
- requestInfo.headers['Range'] = "bytes=0-".concat(maxDownloadSizeBytes);
- requestInfo.successCodes = [200 /* OK */, 206 /* Partial Content */];
- }
- return requestInfo;
- }
- function getDownloadUrl(service, location, mappings) {
- var urlPart = location.fullServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'GET';
- var timeout = service.maxOperationRetryTime;
- var requestInfo = new RequestInfo(url, method, downloadUrlHandler(service, mappings), timeout);
- requestInfo.errorHandler = objectErrorHandler(location);
- return requestInfo;
- }
- function updateMetadata$2(service, location, metadata, mappings) {
- var urlPart = location.fullServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'PATCH';
- var body = toResourceString(metadata, mappings);
- var headers = { 'Content-Type': 'application/json; charset=utf-8' };
- var timeout = service.maxOperationRetryTime;
- var requestInfo = new RequestInfo(url, method, metadataHandler(service, mappings), timeout);
- requestInfo.headers = headers;
- requestInfo.body = body;
- requestInfo.errorHandler = objectErrorHandler(location);
- return requestInfo;
- }
- function deleteObject$2(service, location) {
- var urlPart = location.fullServerUrl();
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'DELETE';
- var timeout = service.maxOperationRetryTime;
- function handler(_xhr, _text) { }
- var requestInfo = new RequestInfo(url, method, handler, timeout);
- requestInfo.successCodes = [200, 204];
- requestInfo.errorHandler = objectErrorHandler(location);
- return requestInfo;
- }
- function determineContentType_(metadata, blob) {
- return ((metadata && metadata['contentType']) ||
- (blob && blob.type()) ||
- 'application/octet-stream');
- }
- function metadataForUpload_(location, blob, metadata) {
- var metadataClone = Object.assign({}, metadata);
- metadataClone['fullPath'] = location.path;
- metadataClone['size'] = blob.size();
- if (!metadataClone['contentType']) {
- metadataClone['contentType'] = determineContentType_(null, blob);
- }
- return metadataClone;
- }
- /**
- * Prepare RequestInfo for uploads as Content-Type: multipart.
- */
- function multipartUpload(service, location, mappings, blob, metadata) {
- var urlPart = location.bucketOnlyServerUrl();
- var headers = {
- 'X-Goog-Upload-Protocol': 'multipart'
- };
- function genBoundary() {
- var str = '';
- for (var i = 0; i < 2; i++) {
- str = str + Math.random().toString().slice(2);
- }
- return str;
- }
- var boundary = genBoundary();
- headers['Content-Type'] = 'multipart/related; boundary=' + boundary;
- var metadata_ = metadataForUpload_(location, blob, metadata);
- var metadataString = toResourceString(metadata_, mappings);
- var preBlobPart = '--' +
- boundary +
- '\r\n' +
- 'Content-Type: application/json; charset=utf-8\r\n\r\n' +
- metadataString +
- '\r\n--' +
- boundary +
- '\r\n' +
- 'Content-Type: ' +
- metadata_['contentType'] +
- '\r\n\r\n';
- var postBlobPart = '\r\n--' + boundary + '--';
- var body = FbsBlob.getBlob(preBlobPart, blob, postBlobPart);
- if (body === null) {
- throw cannotSliceBlob();
- }
- var urlParams = { name: metadata_['fullPath'] };
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'POST';
- var timeout = service.maxUploadRetryTime;
- var requestInfo = new RequestInfo(url, method, metadataHandler(service, mappings), timeout);
- requestInfo.urlParams = urlParams;
- requestInfo.headers = headers;
- requestInfo.body = body.uploadData();
- requestInfo.errorHandler = sharedErrorHandler(location);
- return requestInfo;
- }
- /**
- * @param current The number of bytes that have been uploaded so far.
- * @param total The total number of bytes in the upload.
- * @param opt_finalized True if the server has finished the upload.
- * @param opt_metadata The upload metadata, should
- * only be passed if opt_finalized is true.
- */
- var ResumableUploadStatus = /** @class */ (function () {
- function ResumableUploadStatus(current, total, finalized, metadata) {
- this.current = current;
- this.total = total;
- this.finalized = !!finalized;
- this.metadata = metadata || null;
- }
- return ResumableUploadStatus;
- }());
- function checkResumeHeader_(xhr, allowed) {
- var status = null;
- try {
- status = xhr.getResponseHeader('X-Goog-Upload-Status');
- }
- catch (e) {
- handlerCheck(false);
- }
- var allowedStatus = allowed || ['active'];
- handlerCheck(!!status && allowedStatus.indexOf(status) !== -1);
- return status;
- }
- function createResumableUpload(service, location, mappings, blob, metadata) {
- var urlPart = location.bucketOnlyServerUrl();
- var metadataForUpload = metadataForUpload_(location, blob, metadata);
- var urlParams = { name: metadataForUpload['fullPath'] };
- var url = makeUrl(urlPart, service.host, service._protocol);
- var method = 'POST';
- var headers = {
- 'X-Goog-Upload-Protocol': 'resumable',
- 'X-Goog-Upload-Command': 'start',
- 'X-Goog-Upload-Header-Content-Length': "".concat(blob.size()),
- 'X-Goog-Upload-Header-Content-Type': metadataForUpload['contentType'],
- 'Content-Type': 'application/json; charset=utf-8'
- };
- var body = toResourceString(metadataForUpload, mappings);
- var timeout = service.maxUploadRetryTime;
- function handler(xhr) {
- checkResumeHeader_(xhr);
- var url;
- try {
- url = xhr.getResponseHeader('X-Goog-Upload-URL');
- }
- catch (e) {
- handlerCheck(false);
- }
- handlerCheck(isString(url));
- return url;
- }
- var requestInfo = new RequestInfo(url, method, handler, timeout);
- requestInfo.urlParams = urlParams;
- requestInfo.headers = headers;
- requestInfo.body = body;
- requestInfo.errorHandler = sharedErrorHandler(location);
- return requestInfo;
- }
- /**
- * @param url From a call to fbs.requests.createResumableUpload.
- */
- function getResumableUploadStatus(service, location, url, blob) {
- var headers = { 'X-Goog-Upload-Command': 'query' };
- function handler(xhr) {
- var status = checkResumeHeader_(xhr, ['active', 'final']);
- var sizeString = null;
- try {
- sizeString = xhr.getResponseHeader('X-Goog-Upload-Size-Received');
- }
- catch (e) {
- handlerCheck(false);
- }
- if (!sizeString) {
- // null or empty string
- handlerCheck(false);
- }
- var size = Number(sizeString);
- handlerCheck(!isNaN(size));
- return new ResumableUploadStatus(size, blob.size(), status === 'final');
- }
- var method = 'POST';
- var timeout = service.maxUploadRetryTime;
- var requestInfo = new RequestInfo(url, method, handler, timeout);
- requestInfo.headers = headers;
- requestInfo.errorHandler = sharedErrorHandler(location);
- return requestInfo;
- }
- /**
- * Any uploads via the resumable upload API must transfer a number of bytes
- * that is a multiple of this number.
- */
- var RESUMABLE_UPLOAD_CHUNK_SIZE = 256 * 1024;
- /**
- * @param url From a call to fbs.requests.createResumableUpload.
- * @param chunkSize Number of bytes to upload.
- * @param status The previous status.
- * If not passed or null, we start from the beginning.
- * @throws fbs.Error If the upload is already complete, the passed in status
- * has a final size inconsistent with the blob, or the blob cannot be sliced
- * for upload.
- */
- function continueResumableUpload(location, service, url, blob, chunkSize, mappings, status, progressCallback) {
- // TODO(andysoto): standardize on internal asserts
- // assert(!(opt_status && opt_status.finalized));
- var status_ = new ResumableUploadStatus(0, 0);
- if (status) {
- status_.current = status.current;
- status_.total = status.total;
- }
- else {
- status_.current = 0;
- status_.total = blob.size();
- }
- if (blob.size() !== status_.total) {
- throw serverFileWrongSize();
- }
- var bytesLeft = status_.total - status_.current;
- var bytesToUpload = bytesLeft;
- if (chunkSize > 0) {
- bytesToUpload = Math.min(bytesToUpload, chunkSize);
- }
- var startByte = status_.current;
- var endByte = startByte + bytesToUpload;
- var uploadCommand = '';
- if (bytesToUpload === 0) {
- uploadCommand = 'finalize';
- }
- else if (bytesLeft === bytesToUpload) {
- uploadCommand = 'upload, finalize';
- }
- else {
- uploadCommand = 'upload';
- }
- var headers = {
- 'X-Goog-Upload-Command': uploadCommand,
- 'X-Goog-Upload-Offset': "".concat(status_.current)
- };
- var body = blob.slice(startByte, endByte);
- if (body === null) {
- throw cannotSliceBlob();
- }
- function handler(xhr, text) {
- // TODO(andysoto): Verify the MD5 of each uploaded range:
- // the 'x-range-md5' header comes back with status code 308 responses.
- // We'll only be able to bail out though, because you can't re-upload a
- // range that you previously uploaded.
- var uploadStatus = checkResumeHeader_(xhr, ['active', 'final']);
- var newCurrent = status_.current + bytesToUpload;
- var size = blob.size();
- var metadata;
- if (uploadStatus === 'final') {
- metadata = metadataHandler(service, mappings)(xhr, text);
- }
- else {
- metadata = null;
- }
- return new ResumableUploadStatus(newCurrent, size, uploadStatus === 'final', metadata);
- }
- var method = 'POST';
- var timeout = service.maxUploadRetryTime;
- var requestInfo = new RequestInfo(url, method, handler, timeout);
- requestInfo.headers = headers;
- requestInfo.body = body.uploadData();
- requestInfo.progressCallback = progressCallback || null;
- requestInfo.errorHandler = sharedErrorHandler(location);
- return requestInfo;
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * An event that is triggered on a task.
- * @internal
- */
- var TaskEvent = {
- /**
- * For this event,
- * <ul>
- * <li>The `next` function is triggered on progress updates and when the
- * task is paused/resumed with an `UploadTaskSnapshot` as the first
- * argument.</li>
- * <li>The `error` function is triggered if the upload is canceled or fails
- * for another reason.</li>
- * <li>The `complete` function is triggered if the upload completes
- * successfully.</li>
- * </ul>
- */
- STATE_CHANGED: 'state_changed'
- };
- // type keys = keyof TaskState
- /**
- * Represents the current state of a running upload.
- * @internal
- */
- var TaskState = {
- /** The task is currently transferring data. */
- RUNNING: 'running',
- /** The task was paused by the user. */
- PAUSED: 'paused',
- /** The task completed successfully. */
- SUCCESS: 'success',
- /** The task was canceled. */
- CANCELED: 'canceled',
- /** The task failed with an error. */
- ERROR: 'error'
- };
- function taskStateFromInternalTaskState(state) {
- switch (state) {
- case "running" /* InternalTaskState.RUNNING */:
- case "pausing" /* InternalTaskState.PAUSING */:
- case "canceling" /* InternalTaskState.CANCELING */:
- return TaskState.RUNNING;
- case "paused" /* InternalTaskState.PAUSED */:
- return TaskState.PAUSED;
- case "success" /* InternalTaskState.SUCCESS */:
- return TaskState.SUCCESS;
- case "canceled" /* InternalTaskState.CANCELED */:
- return TaskState.CANCELED;
- case "error" /* InternalTaskState.ERROR */:
- return TaskState.ERROR;
- default:
- // TODO(andysoto): assert(false);
- return TaskState.ERROR;
- }
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var Observer = /** @class */ (function () {
- function Observer(nextOrObserver, error, complete) {
- var asFunctions = isFunction(nextOrObserver) || error != null || complete != null;
- if (asFunctions) {
- this.next = nextOrObserver;
- this.error = error !== null && error !== void 0 ? error : undefined;
- this.complete = complete !== null && complete !== void 0 ? complete : undefined;
- }
- else {
- var observer = nextOrObserver;
- this.next = observer.next;
- this.error = observer.error;
- this.complete = observer.complete;
- }
- }
- return Observer;
- }());
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Returns a function that invokes f with its arguments asynchronously as a
- * microtask, i.e. as soon as possible after the current script returns back
- * into browser code.
- */
- // eslint-disable-next-line @typescript-eslint/ban-types
- function async(f) {
- return function () {
- var argsToForward = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- argsToForward[_i] = arguments[_i];
- }
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- Promise.resolve().then(function () { return f.apply(void 0, argsToForward); });
- };
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /** An override for the text-based Connection. Used in tests. */
- var textFactoryOverride = null;
- /**
- * Network layer for browsers. We use this instead of goog.net.XhrIo because
- * goog.net.XhrIo is hyuuuuge and doesn't work in React Native on Android.
- */
- var XhrConnection = /** @class */ (function () {
- function XhrConnection() {
- var _this = this;
- this.sent_ = false;
- this.xhr_ = new XMLHttpRequest();
- this.initXhr();
- this.errorCode_ = ErrorCode.NO_ERROR;
- this.sendPromise_ = new Promise(function (resolve) {
- _this.xhr_.addEventListener('abort', function () {
- _this.errorCode_ = ErrorCode.ABORT;
- resolve();
- });
- _this.xhr_.addEventListener('error', function () {
- _this.errorCode_ = ErrorCode.NETWORK_ERROR;
- resolve();
- });
- _this.xhr_.addEventListener('load', function () {
- resolve();
- });
- });
- }
- XhrConnection.prototype.send = function (url, method, body, headers) {
- if (this.sent_) {
- throw internalError('cannot .send() more than once');
- }
- this.sent_ = true;
- this.xhr_.open(method, url, true);
- if (headers !== undefined) {
- for (var key in headers) {
- if (headers.hasOwnProperty(key)) {
- this.xhr_.setRequestHeader(key, headers[key].toString());
- }
- }
- }
- if (body !== undefined) {
- this.xhr_.send(body);
- }
- else {
- this.xhr_.send();
- }
- return this.sendPromise_;
- };
- XhrConnection.prototype.getErrorCode = function () {
- if (!this.sent_) {
- throw internalError('cannot .getErrorCode() before sending');
- }
- return this.errorCode_;
- };
- XhrConnection.prototype.getStatus = function () {
- if (!this.sent_) {
- throw internalError('cannot .getStatus() before sending');
- }
- try {
- return this.xhr_.status;
- }
- catch (e) {
- return -1;
- }
- };
- XhrConnection.prototype.getResponse = function () {
- if (!this.sent_) {
- throw internalError('cannot .getResponse() before sending');
- }
- return this.xhr_.response;
- };
- XhrConnection.prototype.getErrorText = function () {
- if (!this.sent_) {
- throw internalError('cannot .getErrorText() before sending');
- }
- return this.xhr_.statusText;
- };
- /** Aborts the request. */
- XhrConnection.prototype.abort = function () {
- this.xhr_.abort();
- };
- XhrConnection.prototype.getResponseHeader = function (header) {
- return this.xhr_.getResponseHeader(header);
- };
- XhrConnection.prototype.addUploadProgressListener = function (listener) {
- if (this.xhr_.upload != null) {
- this.xhr_.upload.addEventListener('progress', listener);
- }
- };
- XhrConnection.prototype.removeUploadProgressListener = function (listener) {
- if (this.xhr_.upload != null) {
- this.xhr_.upload.removeEventListener('progress', listener);
- }
- };
- return XhrConnection;
- }());
- var XhrTextConnection = /** @class */ (function (_super) {
- tslib.__extends(XhrTextConnection, _super);
- function XhrTextConnection() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- XhrTextConnection.prototype.initXhr = function () {
- this.xhr_.responseType = 'text';
- };
- return XhrTextConnection;
- }(XhrConnection));
- function newTextConnection() {
- return textFactoryOverride ? textFactoryOverride() : new XhrTextConnection();
- }
- var XhrBytesConnection = /** @class */ (function (_super) {
- tslib.__extends(XhrBytesConnection, _super);
- function XhrBytesConnection() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- XhrBytesConnection.prototype.initXhr = function () {
- this.xhr_.responseType = 'arraybuffer';
- };
- return XhrBytesConnection;
- }(XhrConnection));
- function newBytesConnection() {
- return new XhrBytesConnection();
- }
- var XhrBlobConnection = /** @class */ (function (_super) {
- tslib.__extends(XhrBlobConnection, _super);
- function XhrBlobConnection() {
- return _super !== null && _super.apply(this, arguments) || this;
- }
- XhrBlobConnection.prototype.initXhr = function () {
- this.xhr_.responseType = 'blob';
- };
- return XhrBlobConnection;
- }(XhrConnection));
- function newBlobConnection() {
- return new XhrBlobConnection();
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Represents a blob being uploaded. Can be used to pause/resume/cancel the
- * upload and manage callbacks for various events.
- * @internal
- */
- var UploadTask = /** @class */ (function () {
- /**
- * @param ref - The firebaseStorage.Reference object this task came
- * from, untyped to avoid cyclic dependencies.
- * @param blob - The blob to upload.
- */
- function UploadTask(ref, blob, metadata) {
- if (metadata === void 0) { metadata = null; }
- var _this = this;
- /**
- * Number of bytes transferred so far.
- */
- this._transferred = 0;
- this._needToFetchStatus = false;
- this._needToFetchMetadata = false;
- this._observers = [];
- this._error = undefined;
- this._uploadUrl = undefined;
- this._request = undefined;
- this._chunkMultiplier = 1;
- this._resolve = undefined;
- this._reject = undefined;
- this._ref = ref;
- this._blob = blob;
- this._metadata = metadata;
- this._mappings = getMappings();
- this._resumable = this._shouldDoResumable(this._blob);
- this._state = "running" /* InternalTaskState.RUNNING */;
- this._errorHandler = function (error) {
- _this._request = undefined;
- _this._chunkMultiplier = 1;
- if (error._codeEquals("canceled" /* StorageErrorCode.CANCELED */)) {
- _this._needToFetchStatus = true;
- _this.completeTransitions_();
- }
- else {
- var backoffExpired = _this.isExponentialBackoffExpired();
- if (isRetryStatusCode(error.status, [])) {
- if (backoffExpired) {
- error = retryLimitExceeded();
- }
- else {
- _this.sleepTime = Math.max(_this.sleepTime * 2, DEFAULT_MIN_SLEEP_TIME_MILLIS);
- _this._needToFetchStatus = true;
- _this.completeTransitions_();
- return;
- }
- }
- _this._error = error;
- _this._transition("error" /* InternalTaskState.ERROR */);
- }
- };
- this._metadataErrorHandler = function (error) {
- _this._request = undefined;
- if (error._codeEquals("canceled" /* StorageErrorCode.CANCELED */)) {
- _this.completeTransitions_();
- }
- else {
- _this._error = error;
- _this._transition("error" /* InternalTaskState.ERROR */);
- }
- };
- this.sleepTime = 0;
- this.maxSleepTime = this._ref.storage.maxUploadRetryTime;
- this._promise = new Promise(function (resolve, reject) {
- _this._resolve = resolve;
- _this._reject = reject;
- _this._start();
- });
- // Prevent uncaught rejections on the internal promise from bubbling out
- // to the top level with a dummy handler.
- this._promise.then(null, function () { });
- }
- UploadTask.prototype.isExponentialBackoffExpired = function () {
- return this.sleepTime > this.maxSleepTime;
- };
- UploadTask.prototype._makeProgressCallback = function () {
- var _this = this;
- var sizeBefore = this._transferred;
- return function (loaded) { return _this._updateProgress(sizeBefore + loaded); };
- };
- UploadTask.prototype._shouldDoResumable = function (blob) {
- return blob.size() > 256 * 1024;
- };
- UploadTask.prototype._start = function () {
- var _this = this;
- if (this._state !== "running" /* InternalTaskState.RUNNING */) {
- // This can happen if someone pauses us in a resume callback, for example.
- return;
- }
- if (this._request !== undefined) {
- return;
- }
- if (this._resumable) {
- if (this._uploadUrl === undefined) {
- this._createResumable();
- }
- else {
- if (this._needToFetchStatus) {
- this._fetchStatus();
- }
- else {
- if (this._needToFetchMetadata) {
- // Happens if we miss the metadata on upload completion.
- this._fetchMetadata();
- }
- else {
- this.pendingTimeout = setTimeout(function () {
- _this.pendingTimeout = undefined;
- _this._continueUpload();
- }, this.sleepTime);
- }
- }
- }
- }
- else {
- this._oneShotUpload();
- }
- };
- UploadTask.prototype._resolveToken = function (callback) {
- var _this = this;
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- Promise.all([
- this._ref.storage._getAuthToken(),
- this._ref.storage._getAppCheckToken()
- ]).then(function (_a) {
- var authToken = _a[0], appCheckToken = _a[1];
- switch (_this._state) {
- case "running" /* InternalTaskState.RUNNING */:
- callback(authToken, appCheckToken);
- break;
- case "canceling" /* InternalTaskState.CANCELING */:
- _this._transition("canceled" /* InternalTaskState.CANCELED */);
- break;
- case "pausing" /* InternalTaskState.PAUSING */:
- _this._transition("paused" /* InternalTaskState.PAUSED */);
- break;
- }
- });
- };
- // TODO(andysoto): assert false
- UploadTask.prototype._createResumable = function () {
- var _this = this;
- this._resolveToken(function (authToken, appCheckToken) {
- var requestInfo = createResumableUpload(_this._ref.storage, _this._ref._location, _this._mappings, _this._blob, _this._metadata);
- var createRequest = _this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
- _this._request = createRequest;
- createRequest.getPromise().then(function (url) {
- _this._request = undefined;
- _this._uploadUrl = url;
- _this._needToFetchStatus = false;
- _this.completeTransitions_();
- }, _this._errorHandler);
- });
- };
- UploadTask.prototype._fetchStatus = function () {
- var _this = this;
- // TODO(andysoto): assert(this.uploadUrl_ !== null);
- var url = this._uploadUrl;
- this._resolveToken(function (authToken, appCheckToken) {
- var requestInfo = getResumableUploadStatus(_this._ref.storage, _this._ref._location, url, _this._blob);
- var statusRequest = _this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
- _this._request = statusRequest;
- statusRequest.getPromise().then(function (status) {
- status = status;
- _this._request = undefined;
- _this._updateProgress(status.current);
- _this._needToFetchStatus = false;
- if (status.finalized) {
- _this._needToFetchMetadata = true;
- }
- _this.completeTransitions_();
- }, _this._errorHandler);
- });
- };
- UploadTask.prototype._continueUpload = function () {
- var _this = this;
- var chunkSize = RESUMABLE_UPLOAD_CHUNK_SIZE * this._chunkMultiplier;
- var status = new ResumableUploadStatus(this._transferred, this._blob.size());
- // TODO(andysoto): assert(this.uploadUrl_ !== null);
- var url = this._uploadUrl;
- this._resolveToken(function (authToken, appCheckToken) {
- var requestInfo;
- try {
- requestInfo = continueResumableUpload(_this._ref._location, _this._ref.storage, url, _this._blob, chunkSize, _this._mappings, status, _this._makeProgressCallback());
- }
- catch (e) {
- _this._error = e;
- _this._transition("error" /* InternalTaskState.ERROR */);
- return;
- }
- var uploadRequest = _this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken,
- /*retry=*/ false // Upload requests should not be retried as each retry should be preceded by another query request. Which is handled in this file.
- );
- _this._request = uploadRequest;
- uploadRequest.getPromise().then(function (newStatus) {
- _this._increaseMultiplier();
- _this._request = undefined;
- _this._updateProgress(newStatus.current);
- if (newStatus.finalized) {
- _this._metadata = newStatus.metadata;
- _this._transition("success" /* InternalTaskState.SUCCESS */);
- }
- else {
- _this.completeTransitions_();
- }
- }, _this._errorHandler);
- });
- };
- UploadTask.prototype._increaseMultiplier = function () {
- var currentSize = RESUMABLE_UPLOAD_CHUNK_SIZE * this._chunkMultiplier;
- // Max chunk size is 32M.
- if (currentSize * 2 < 32 * 1024 * 1024) {
- this._chunkMultiplier *= 2;
- }
- };
- UploadTask.prototype._fetchMetadata = function () {
- var _this = this;
- this._resolveToken(function (authToken, appCheckToken) {
- var requestInfo = getMetadata$2(_this._ref.storage, _this._ref._location, _this._mappings);
- var metadataRequest = _this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
- _this._request = metadataRequest;
- metadataRequest.getPromise().then(function (metadata) {
- _this._request = undefined;
- _this._metadata = metadata;
- _this._transition("success" /* InternalTaskState.SUCCESS */);
- }, _this._metadataErrorHandler);
- });
- };
- UploadTask.prototype._oneShotUpload = function () {
- var _this = this;
- this._resolveToken(function (authToken, appCheckToken) {
- var requestInfo = multipartUpload(_this._ref.storage, _this._ref._location, _this._mappings, _this._blob, _this._metadata);
- var multipartRequest = _this._ref.storage._makeRequest(requestInfo, newTextConnection, authToken, appCheckToken);
- _this._request = multipartRequest;
- multipartRequest.getPromise().then(function (metadata) {
- _this._request = undefined;
- _this._metadata = metadata;
- _this._updateProgress(_this._blob.size());
- _this._transition("success" /* InternalTaskState.SUCCESS */);
- }, _this._errorHandler);
- });
- };
- UploadTask.prototype._updateProgress = function (transferred) {
- var old = this._transferred;
- this._transferred = transferred;
- // A progress update can make the "transferred" value smaller (e.g. a
- // partial upload not completed by server, after which the "transferred"
- // value may reset to the value at the beginning of the request).
- if (this._transferred !== old) {
- this._notifyObservers();
- }
- };
- UploadTask.prototype._transition = function (state) {
- if (this._state === state) {
- return;
- }
- switch (state) {
- case "canceling" /* InternalTaskState.CANCELING */:
- case "pausing" /* InternalTaskState.PAUSING */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.RUNNING ||
- // this.state_ === InternalTaskState.PAUSING);
- this._state = state;
- if (this._request !== undefined) {
- this._request.cancel();
- }
- else if (this.pendingTimeout) {
- clearTimeout(this.pendingTimeout);
- this.pendingTimeout = undefined;
- this.completeTransitions_();
- }
- break;
- case "running" /* InternalTaskState.RUNNING */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.PAUSED ||
- // this.state_ === InternalTaskState.PAUSING);
- var wasPaused = this._state === "paused" /* InternalTaskState.PAUSED */;
- this._state = state;
- if (wasPaused) {
- this._notifyObservers();
- this._start();
- }
- break;
- case "paused" /* InternalTaskState.PAUSED */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.PAUSING);
- this._state = state;
- this._notifyObservers();
- break;
- case "canceled" /* InternalTaskState.CANCELED */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.PAUSED ||
- // this.state_ === InternalTaskState.CANCELING);
- this._error = canceled();
- this._state = state;
- this._notifyObservers();
- break;
- case "error" /* InternalTaskState.ERROR */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.RUNNING ||
- // this.state_ === InternalTaskState.PAUSING ||
- // this.state_ === InternalTaskState.CANCELING);
- this._state = state;
- this._notifyObservers();
- break;
- case "success" /* InternalTaskState.SUCCESS */:
- // TODO(andysoto):
- // assert(this.state_ === InternalTaskState.RUNNING ||
- // this.state_ === InternalTaskState.PAUSING ||
- // this.state_ === InternalTaskState.CANCELING);
- this._state = state;
- this._notifyObservers();
- break;
- }
- };
- UploadTask.prototype.completeTransitions_ = function () {
- switch (this._state) {
- case "pausing" /* InternalTaskState.PAUSING */:
- this._transition("paused" /* InternalTaskState.PAUSED */);
- break;
- case "canceling" /* InternalTaskState.CANCELING */:
- this._transition("canceled" /* InternalTaskState.CANCELED */);
- break;
- case "running" /* InternalTaskState.RUNNING */:
- this._start();
- break;
- }
- };
- Object.defineProperty(UploadTask.prototype, "snapshot", {
- /**
- * A snapshot of the current task state.
- */
- get: function () {
- var externalState = taskStateFromInternalTaskState(this._state);
- return {
- bytesTransferred: this._transferred,
- totalBytes: this._blob.size(),
- state: externalState,
- metadata: this._metadata,
- task: this,
- ref: this._ref
- };
- },
- enumerable: false,
- configurable: true
- });
- /**
- * Adds a callback for an event.
- * @param type - The type of event to listen for.
- * @param nextOrObserver -
- * The `next` function, which gets called for each item in
- * the event stream, or an observer object with some or all of these three
- * properties (`next`, `error`, `complete`).
- * @param error - A function that gets called with a `StorageError`
- * if the event stream ends due to an error.
- * @param completed - A function that gets called if the
- * event stream ends normally.
- * @returns
- * If only the event argument is passed, returns a function you can use to
- * add callbacks (see the examples above). If more than just the event
- * argument is passed, returns a function you can call to unregister the
- * callbacks.
- */
- UploadTask.prototype.on = function (type, nextOrObserver, error, completed) {
- var _this = this;
- // Note: `type` isn't being used. Its type is also incorrect. TaskEvent should not be a string.
- var observer = new Observer(nextOrObserver || undefined, error || undefined, completed || undefined);
- this._addObserver(observer);
- return function () {
- _this._removeObserver(observer);
- };
- };
- /**
- * This object behaves like a Promise, and resolves with its snapshot data
- * when the upload completes.
- * @param onFulfilled - The fulfillment callback. Promise chaining works as normal.
- * @param onRejected - The rejection callback.
- */
- UploadTask.prototype.then = function (onFulfilled, onRejected) {
- // These casts are needed so that TypeScript can infer the types of the
- // resulting Promise.
- return this._promise.then(onFulfilled, onRejected);
- };
- /**
- * Equivalent to calling `then(null, onRejected)`.
- */
- UploadTask.prototype.catch = function (onRejected) {
- return this.then(null, onRejected);
- };
- /**
- * Adds the given observer.
- */
- UploadTask.prototype._addObserver = function (observer) {
- this._observers.push(observer);
- this._notifyObserver(observer);
- };
- /**
- * Removes the given observer.
- */
- UploadTask.prototype._removeObserver = function (observer) {
- var i = this._observers.indexOf(observer);
- if (i !== -1) {
- this._observers.splice(i, 1);
- }
- };
- UploadTask.prototype._notifyObservers = function () {
- var _this = this;
- this._finishPromise();
- var observers = this._observers.slice();
- observers.forEach(function (observer) {
- _this._notifyObserver(observer);
- });
- };
- UploadTask.prototype._finishPromise = function () {
- if (this._resolve !== undefined) {
- var triggered = true;
- switch (taskStateFromInternalTaskState(this._state)) {
- case TaskState.SUCCESS:
- async(this._resolve.bind(null, this.snapshot))();
- break;
- case TaskState.CANCELED:
- case TaskState.ERROR:
- var toCall = this._reject;
- async(toCall.bind(null, this._error))();
- break;
- default:
- triggered = false;
- break;
- }
- if (triggered) {
- this._resolve = undefined;
- this._reject = undefined;
- }
- }
- };
- UploadTask.prototype._notifyObserver = function (observer) {
- var externalState = taskStateFromInternalTaskState(this._state);
- switch (externalState) {
- case TaskState.RUNNING:
- case TaskState.PAUSED:
- if (observer.next) {
- async(observer.next.bind(observer, this.snapshot))();
- }
- break;
- case TaskState.SUCCESS:
- if (observer.complete) {
- async(observer.complete.bind(observer))();
- }
- break;
- case TaskState.CANCELED:
- case TaskState.ERROR:
- if (observer.error) {
- async(observer.error.bind(observer, this._error))();
- }
- break;
- default:
- // TODO(andysoto): assert(false);
- if (observer.error) {
- async(observer.error.bind(observer, this._error))();
- }
- }
- };
- /**
- * Resumes a paused task. Has no effect on a currently running or failed task.
- * @returns True if the operation took effect, false if ignored.
- */
- UploadTask.prototype.resume = function () {
- var valid = this._state === "paused" /* InternalTaskState.PAUSED */ ||
- this._state === "pausing" /* InternalTaskState.PAUSING */;
- if (valid) {
- this._transition("running" /* InternalTaskState.RUNNING */);
- }
- return valid;
- };
- /**
- * Pauses a currently running task. Has no effect on a paused or failed task.
- * @returns True if the operation took effect, false if ignored.
- */
- UploadTask.prototype.pause = function () {
- var valid = this._state === "running" /* InternalTaskState.RUNNING */;
- if (valid) {
- this._transition("pausing" /* InternalTaskState.PAUSING */);
- }
- return valid;
- };
- /**
- * Cancels a currently running or paused task. Has no effect on a complete or
- * failed task.
- * @returns True if the operation took effect, false if ignored.
- */
- UploadTask.prototype.cancel = function () {
- var valid = this._state === "running" /* InternalTaskState.RUNNING */ ||
- this._state === "pausing" /* InternalTaskState.PAUSING */;
- if (valid) {
- this._transition("canceling" /* InternalTaskState.CANCELING */);
- }
- return valid;
- };
- return UploadTask;
- }());
-
- /**
- * @license
- * Copyright 2019 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Provides methods to interact with a bucket in the Firebase Storage service.
- * @internal
- * @param _location - An fbs.location, or the URL at
- * which to base this object, in one of the following forms:
- * gs://<bucket>/<object-path>
- * http[s]://firebasestorage.googleapis.com/
- * <api-version>/b/<bucket>/o/<object-path>
- * Any query or fragment strings will be ignored in the http[s]
- * format. If no value is passed, the storage object will use a URL based on
- * the project ID of the base firebase.App instance.
- */
- var Reference = /** @class */ (function () {
- function Reference(_service, location) {
- this._service = _service;
- if (location instanceof Location) {
- this._location = location;
- }
- else {
- this._location = Location.makeFromUrl(location, _service.host);
- }
- }
- /**
- * Returns the URL for the bucket and path this object references,
- * in the form gs://<bucket>/<object-path>
- * @override
- */
- Reference.prototype.toString = function () {
- return 'gs://' + this._location.bucket + '/' + this._location.path;
- };
- Reference.prototype._newRef = function (service, location) {
- return new Reference(service, location);
- };
- Object.defineProperty(Reference.prototype, "root", {
- /**
- * A reference to the root of this object's bucket.
- */
- get: function () {
- var location = new Location(this._location.bucket, '');
- return this._newRef(this._service, location);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "bucket", {
- /**
- * The name of the bucket containing this reference's object.
- */
- get: function () {
- return this._location.bucket;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "fullPath", {
- /**
- * The full path of this object.
- */
- get: function () {
- return this._location.path;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "name", {
- /**
- * The short name of this object, which is the last component of the full path.
- * For example, if fullPath is 'full/path/image.png', name is 'image.png'.
- */
- get: function () {
- return lastComponent(this._location.path);
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "storage", {
- /**
- * The `StorageService` instance this `StorageReference` is associated with.
- */
- get: function () {
- return this._service;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(Reference.prototype, "parent", {
- /**
- * A `StorageReference` pointing to the parent location of this `StorageReference`, or null if
- * this reference is the root.
- */
- get: function () {
- var newPath = parent(this._location.path);
- if (newPath === null) {
- return null;
- }
- var location = new Location(this._location.bucket, newPath);
- return new Reference(this._service, location);
- },
- enumerable: false,
- configurable: true
- });
- /**
- * Utility function to throw an error in methods that do not accept a root reference.
- */
- Reference.prototype._throwIfRoot = function (name) {
- if (this._location.path === '') {
- throw invalidRootOperation(name);
- }
- };
- return Reference;
- }());
- /**
- * Download the bytes at the object's location.
- * @returns A Promise containing the downloaded bytes.
- */
- function getBytesInternal(ref, maxDownloadSizeBytes) {
- ref._throwIfRoot('getBytes');
- var requestInfo = getBytes$1(ref.storage, ref._location, maxDownloadSizeBytes);
- return ref.storage
- .makeRequestWithTokens(requestInfo, newBytesConnection)
- .then(function (bytes) {
- return maxDownloadSizeBytes !== undefined
- ? // GCS may not honor the Range header for small files
- bytes.slice(0, maxDownloadSizeBytes)
- : bytes;
- });
- }
- /**
- * Download the bytes at the object's location.
- * @returns A Promise containing the downloaded blob.
- */
- function getBlobInternal(ref, maxDownloadSizeBytes) {
- ref._throwIfRoot('getBlob');
- var requestInfo = getBytes$1(ref.storage, ref._location, maxDownloadSizeBytes);
- return ref.storage
- .makeRequestWithTokens(requestInfo, newBlobConnection)
- .then(function (blob) {
- return maxDownloadSizeBytes !== undefined
- ? // GCS may not honor the Range header for small files
- blob.slice(0, maxDownloadSizeBytes)
- : blob;
- });
- }
- /**
- * Uploads data to this object's location.
- * The upload is not resumable.
- *
- * @param ref - StorageReference where data should be uploaded.
- * @param data - The data to upload.
- * @param metadata - Metadata for the newly uploaded data.
- * @returns A Promise containing an UploadResult
- */
- function uploadBytes$1(ref, data, metadata) {
- ref._throwIfRoot('uploadBytes');
- var requestInfo = multipartUpload(ref.storage, ref._location, getMappings(), new FbsBlob(data, true), metadata);
- return ref.storage
- .makeRequestWithTokens(requestInfo, newTextConnection)
- .then(function (finalMetadata) {
- return {
- metadata: finalMetadata,
- ref: ref
- };
- });
- }
- /**
- * Uploads data to this object's location.
- * The upload can be paused and resumed, and exposes progress updates.
- * @public
- * @param ref - StorageReference where data should be uploaded.
- * @param data - The data to upload.
- * @param metadata - Metadata for the newly uploaded data.
- * @returns An UploadTask
- */
- function uploadBytesResumable$1(ref, data, metadata) {
- ref._throwIfRoot('uploadBytesResumable');
- return new UploadTask(ref, new FbsBlob(data), metadata);
- }
- /**
- * Uploads a string to this object's location.
- * The upload is not resumable.
- * @public
- * @param ref - StorageReference where string should be uploaded.
- * @param value - The string to upload.
- * @param format - The format of the string to upload.
- * @param metadata - Metadata for the newly uploaded string.
- * @returns A Promise containing an UploadResult
- */
- function uploadString$1(ref, value, format, metadata) {
- if (format === void 0) { format = StringFormat.RAW; }
- ref._throwIfRoot('uploadString');
- var data = dataFromString(format, value);
- var metadataClone = tslib.__assign({}, metadata);
- if (metadataClone['contentType'] == null && data.contentType != null) {
- metadataClone['contentType'] = data.contentType;
- }
- return uploadBytes$1(ref, data.data, metadataClone);
- }
- /**
- * List all items (files) and prefixes (folders) under this storage reference.
- *
- * This is a helper method for calling list() repeatedly until there are
- * no more results. The default pagination size is 1000.
- *
- * Note: The results may not be consistent if objects are changed while this
- * operation is running.
- *
- * Warning: listAll may potentially consume too many resources if there are
- * too many results.
- * @public
- * @param ref - StorageReference to get list from.
- *
- * @returns A Promise that resolves with all the items and prefixes under
- * the current storage reference. `prefixes` contains references to
- * sub-directories and `items` contains references to objects in this
- * folder. `nextPageToken` is never returned.
- */
- function listAll$1(ref) {
- var accumulator = {
- prefixes: [],
- items: []
- };
- return listAllHelper(ref, accumulator).then(function () { return accumulator; });
- }
- /**
- * Separated from listAll because async functions can't use "arguments".
- * @param ref
- * @param accumulator
- * @param pageToken
- */
- function listAllHelper(ref, accumulator, pageToken) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var opt, nextPage;
- var _a, _b;
- return tslib.__generator(this, function (_c) {
- switch (_c.label) {
- case 0:
- opt = {
- // maxResults is 1000 by default.
- pageToken: pageToken
- };
- return [4 /*yield*/, list$1(ref, opt)];
- case 1:
- nextPage = _c.sent();
- (_a = accumulator.prefixes).push.apply(_a, nextPage.prefixes);
- (_b = accumulator.items).push.apply(_b, nextPage.items);
- if (!(nextPage.nextPageToken != null)) return [3 /*break*/, 3];
- return [4 /*yield*/, listAllHelper(ref, accumulator, nextPage.nextPageToken)];
- case 2:
- _c.sent();
- _c.label = 3;
- case 3: return [2 /*return*/];
- }
- });
- });
- }
- /**
- * List items (files) and prefixes (folders) under this storage reference.
- *
- * List API is only available for Firebase Rules Version 2.
- *
- * GCS is a key-blob store. Firebase Storage imposes the semantic of '/'
- * delimited folder structure.
- * Refer to GCS's List API if you want to learn more.
- *
- * To adhere to Firebase Rules's Semantics, Firebase Storage does not
- * support objects whose paths end with "/" or contain two consecutive
- * "/"s. Firebase Storage List API will filter these unsupported objects.
- * list() may fail if there are too many unsupported objects in the bucket.
- * @public
- *
- * @param ref - StorageReference to get list from.
- * @param options - See ListOptions for details.
- * @returns A Promise that resolves with the items and prefixes.
- * `prefixes` contains references to sub-folders and `items`
- * contains references to objects in this folder. `nextPageToken`
- * can be used to get the rest of the results.
- */
- function list$1(ref, options) {
- if (options != null) {
- if (typeof options.maxResults === 'number') {
- validateNumber('options.maxResults',
- /* minValue= */ 1,
- /* maxValue= */ 1000, options.maxResults);
- }
- }
- var op = options || {};
- var requestInfo = list$2(ref.storage, ref._location,
- /*delimiter= */ '/', op.pageToken, op.maxResults);
- return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
- }
- /**
- * A `Promise` that resolves with the metadata for this object. If this
- * object doesn't exist or metadata cannot be retreived, the promise is
- * rejected.
- * @public
- * @param ref - StorageReference to get metadata from.
- */
- function getMetadata$1(ref) {
- ref._throwIfRoot('getMetadata');
- var requestInfo = getMetadata$2(ref.storage, ref._location, getMappings());
- return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
- }
- /**
- * Updates the metadata for this object.
- * @public
- * @param ref - StorageReference to update metadata for.
- * @param metadata - The new metadata for the object.
- * Only values that have been explicitly set will be changed. Explicitly
- * setting a value to null will remove the metadata.
- * @returns A `Promise` that resolves
- * with the new metadata for this object.
- * See `firebaseStorage.Reference.prototype.getMetadata`
- */
- function updateMetadata$1(ref, metadata) {
- ref._throwIfRoot('updateMetadata');
- var requestInfo = updateMetadata$2(ref.storage, ref._location, metadata, getMappings());
- return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
- }
- /**
- * Returns the download URL for the given Reference.
- * @public
- * @returns A `Promise` that resolves with the download
- * URL for this object.
- */
- function getDownloadURL$1(ref) {
- ref._throwIfRoot('getDownloadURL');
- var requestInfo = getDownloadUrl(ref.storage, ref._location, getMappings());
- return ref.storage
- .makeRequestWithTokens(requestInfo, newTextConnection)
- .then(function (url) {
- if (url === null) {
- throw noDownloadURL();
- }
- return url;
- });
- }
- /**
- * Deletes the object at this location.
- * @public
- * @param ref - StorageReference for object to delete.
- * @returns A `Promise` that resolves if the deletion succeeds.
- */
- function deleteObject$1(ref) {
- ref._throwIfRoot('deleteObject');
- var requestInfo = deleteObject$2(ref.storage, ref._location);
- return ref.storage.makeRequestWithTokens(requestInfo, newTextConnection);
- }
- /**
- * Returns reference for object obtained by appending `childPath` to `ref`.
- *
- * @param ref - StorageReference to get child of.
- * @param childPath - Child path from provided ref.
- * @returns A reference to the object obtained by
- * appending childPath, removing any duplicate, beginning, or trailing
- * slashes.
- *
- */
- function _getChild$1(ref, childPath) {
- var newPath = child(ref._location.path, childPath);
- var location = new Location(ref._location.bucket, newPath);
- return new Reference(ref.storage, location);
- }
-
- /**
- * @license
- * Copyright 2017 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- function isUrl(path) {
- return /^[A-Za-z]+:\/\//.test(path);
- }
- /**
- * Returns a firebaseStorage.Reference for the given url.
- */
- function refFromURL(service, url) {
- return new Reference(service, url);
- }
- /**
- * Returns a firebaseStorage.Reference for the given path in the default
- * bucket.
- */
- function refFromPath(ref, path) {
- if (ref instanceof FirebaseStorageImpl) {
- var service = ref;
- if (service._bucket == null) {
- throw noDefaultBucket();
- }
- var reference = new Reference(service, service._bucket);
- if (path != null) {
- return refFromPath(reference, path);
- }
- else {
- return reference;
- }
- }
- else {
- // ref is a Reference
- if (path !== undefined) {
- return _getChild$1(ref, path);
- }
- else {
- return ref;
- }
- }
- }
- function ref$1(serviceOrRef, pathOrUrl) {
- if (pathOrUrl && isUrl(pathOrUrl)) {
- if (serviceOrRef instanceof FirebaseStorageImpl) {
- return refFromURL(serviceOrRef, pathOrUrl);
- }
- else {
- throw invalidArgument('To use ref(service, url), the first argument must be a Storage instance.');
- }
- }
- else {
- return refFromPath(serviceOrRef, pathOrUrl);
- }
- }
- function extractBucket(host, config) {
- var bucketString = config === null || config === void 0 ? void 0 : config[CONFIG_STORAGE_BUCKET_KEY];
- if (bucketString == null) {
- return null;
- }
- return Location.makeFromBucketSpec(bucketString, host);
- }
- function connectStorageEmulator$1(storage, host, port, options) {
- if (options === void 0) { options = {}; }
- storage.host = "".concat(host, ":").concat(port);
- storage._protocol = 'http';
- var mockUserToken = options.mockUserToken;
- if (mockUserToken) {
- storage._overrideAuthToken =
- typeof mockUserToken === 'string'
- ? mockUserToken
- : util.createMockUserToken(mockUserToken, storage.app.options.projectId);
- }
- }
- /**
- * A service that provides Firebase Storage Reference instances.
- * @param opt_url - gs:// url to a custom Storage Bucket
- *
- * @internal
- */
- var FirebaseStorageImpl = /** @class */ (function () {
- function FirebaseStorageImpl(
- /**
- * FirebaseApp associated with this StorageService instance.
- */
- app, _authProvider,
- /**
- * @internal
- */
- _appCheckProvider,
- /**
- * @internal
- */
- _url, _firebaseVersion) {
- this.app = app;
- this._authProvider = _authProvider;
- this._appCheckProvider = _appCheckProvider;
- this._url = _url;
- this._firebaseVersion = _firebaseVersion;
- this._bucket = null;
- /**
- * This string can be in the formats:
- * - host
- * - host:port
- */
- this._host = DEFAULT_HOST;
- this._protocol = 'https';
- this._appId = null;
- this._deleted = false;
- this._maxOperationRetryTime = DEFAULT_MAX_OPERATION_RETRY_TIME;
- this._maxUploadRetryTime = DEFAULT_MAX_UPLOAD_RETRY_TIME;
- this._requests = new Set();
- if (_url != null) {
- this._bucket = Location.makeFromBucketSpec(_url, this._host);
- }
- else {
- this._bucket = extractBucket(this._host, this.app.options);
- }
- }
- Object.defineProperty(FirebaseStorageImpl.prototype, "host", {
- /**
- * The host string for this service, in the form of `host` or
- * `host:port`.
- */
- get: function () {
- return this._host;
- },
- set: function (host) {
- this._host = host;
- if (this._url != null) {
- this._bucket = Location.makeFromBucketSpec(this._url, host);
- }
- else {
- this._bucket = extractBucket(host, this.app.options);
- }
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(FirebaseStorageImpl.prototype, "maxUploadRetryTime", {
- /**
- * The maximum time to retry uploads in milliseconds.
- */
- get: function () {
- return this._maxUploadRetryTime;
- },
- set: function (time) {
- validateNumber('time',
- /* minValue=*/ 0,
- /* maxValue= */ Number.POSITIVE_INFINITY, time);
- this._maxUploadRetryTime = time;
- },
- enumerable: false,
- configurable: true
- });
- Object.defineProperty(FirebaseStorageImpl.prototype, "maxOperationRetryTime", {
- /**
- * The maximum time to retry operations other than uploads or downloads in
- * milliseconds.
- */
- get: function () {
- return this._maxOperationRetryTime;
- },
- set: function (time) {
- validateNumber('time',
- /* minValue=*/ 0,
- /* maxValue= */ Number.POSITIVE_INFINITY, time);
- this._maxOperationRetryTime = time;
- },
- enumerable: false,
- configurable: true
- });
- FirebaseStorageImpl.prototype._getAuthToken = function () {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var auth, tokenData;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- if (this._overrideAuthToken) {
- return [2 /*return*/, this._overrideAuthToken];
- }
- auth = this._authProvider.getImmediate({ optional: true });
- if (!auth) return [3 /*break*/, 2];
- return [4 /*yield*/, auth.getToken()];
- case 1:
- tokenData = _a.sent();
- if (tokenData !== null) {
- return [2 /*return*/, tokenData.accessToken];
- }
- _a.label = 2;
- case 2: return [2 /*return*/, null];
- }
- });
- });
- };
- FirebaseStorageImpl.prototype._getAppCheckToken = function () {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var appCheck, result;
- return tslib.__generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- appCheck = this._appCheckProvider.getImmediate({ optional: true });
- if (!appCheck) return [3 /*break*/, 2];
- return [4 /*yield*/, appCheck.getToken()];
- case 1:
- result = _a.sent();
- // TODO: What do we want to do if there is an error getting the token?
- // Context: appCheck.getToken() will never throw even if an error happened. In the error case, a dummy token will be
- // returned along with an error field describing the error. In general, we shouldn't care about the error condition and just use
- // the token (actual or dummy) to send requests.
- return [2 /*return*/, result.token];
- case 2: return [2 /*return*/, null];
- }
- });
- });
- };
- /**
- * Stop running requests and prevent more from being created.
- */
- FirebaseStorageImpl.prototype._delete = function () {
- if (!this._deleted) {
- this._deleted = true;
- this._requests.forEach(function (request) { return request.cancel(); });
- this._requests.clear();
- }
- return Promise.resolve();
- };
- /**
- * Returns a new firebaseStorage.Reference object referencing this StorageService
- * at the given Location.
- */
- FirebaseStorageImpl.prototype._makeStorageReference = function (loc) {
- return new Reference(this, loc);
- };
- /**
- * @param requestInfo - HTTP RequestInfo object
- * @param authToken - Firebase auth token
- */
- FirebaseStorageImpl.prototype._makeRequest = function (requestInfo, requestFactory, authToken, appCheckToken, retry) {
- var _this = this;
- if (retry === void 0) { retry = true; }
- if (!this._deleted) {
- var request_1 = makeRequest(requestInfo, this._appId, authToken, appCheckToken, requestFactory, this._firebaseVersion, retry);
- this._requests.add(request_1);
- // Request removes itself from set when complete.
- request_1.getPromise().then(function () { return _this._requests.delete(request_1); }, function () { return _this._requests.delete(request_1); });
- return request_1;
- }
- else {
- return new FailRequest(appDeleted());
- }
- };
- FirebaseStorageImpl.prototype.makeRequestWithTokens = function (requestInfo, requestFactory) {
- return tslib.__awaiter(this, void 0, void 0, function () {
- var _a, authToken, appCheckToken;
- return tslib.__generator(this, function (_b) {
- switch (_b.label) {
- case 0: return [4 /*yield*/, Promise.all([
- this._getAuthToken(),
- this._getAppCheckToken()
- ])];
- case 1:
- _a = _b.sent(), authToken = _a[0], appCheckToken = _a[1];
- return [2 /*return*/, this._makeRequest(requestInfo, requestFactory, authToken, appCheckToken).getPromise()];
- }
- });
- });
- };
- return FirebaseStorageImpl;
- }());
-
- var name = "@firebase/storage";
- var version = "0.10.1";
-
- /**
- * @license
- * Copyright 2020 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Type constant for Firebase Storage.
- */
- var STORAGE_TYPE = 'storage';
-
- /**
- * Downloads the data at the object's location. Returns an error if the object
- * is not found.
- *
- * To use this functionality, you have to whitelist your app's origin in your
- * Cloud Storage bucket. See also
- * https://cloud.google.com/storage/docs/configuring-cors
- *
- * @public
- * @param ref - StorageReference where data should be downloaded.
- * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
- * retrieve.
- * @returns A Promise containing the object's bytes
- */
- function getBytes(ref, maxDownloadSizeBytes) {
- ref = util.getModularInstance(ref);
- return getBytesInternal(ref, maxDownloadSizeBytes);
- }
- /**
- * Uploads data to this object's location.
- * The upload is not resumable.
- * @public
- * @param ref - {@link StorageReference} where data should be uploaded.
- * @param data - The data to upload.
- * @param metadata - Metadata for the data to upload.
- * @returns A Promise containing an UploadResult
- */
- function uploadBytes(ref, data, metadata) {
- ref = util.getModularInstance(ref);
- return uploadBytes$1(ref, data, metadata);
- }
- /**
- * Uploads a string to this object's location.
- * The upload is not resumable.
- * @public
- * @param ref - {@link StorageReference} where string should be uploaded.
- * @param value - The string to upload.
- * @param format - The format of the string to upload.
- * @param metadata - Metadata for the string to upload.
- * @returns A Promise containing an UploadResult
- */
- function uploadString(ref, value, format, metadata) {
- ref = util.getModularInstance(ref);
- return uploadString$1(ref, value, format, metadata);
- }
- /**
- * Uploads data to this object's location.
- * The upload can be paused and resumed, and exposes progress updates.
- * @public
- * @param ref - {@link StorageReference} where data should be uploaded.
- * @param data - The data to upload.
- * @param metadata - Metadata for the data to upload.
- * @returns An UploadTask
- */
- function uploadBytesResumable(ref, data, metadata) {
- ref = util.getModularInstance(ref);
- return uploadBytesResumable$1(ref, data, metadata);
- }
- /**
- * A `Promise` that resolves with the metadata for this object. If this
- * object doesn't exist or metadata cannot be retreived, the promise is
- * rejected.
- * @public
- * @param ref - {@link StorageReference} to get metadata from.
- */
- function getMetadata(ref) {
- ref = util.getModularInstance(ref);
- return getMetadata$1(ref);
- }
- /**
- * Updates the metadata for this object.
- * @public
- * @param ref - {@link StorageReference} to update metadata for.
- * @param metadata - The new metadata for the object.
- * Only values that have been explicitly set will be changed. Explicitly
- * setting a value to null will remove the metadata.
- * @returns A `Promise` that resolves with the new metadata for this object.
- */
- function updateMetadata(ref, metadata) {
- ref = util.getModularInstance(ref);
- return updateMetadata$1(ref, metadata);
- }
- /**
- * List items (files) and prefixes (folders) under this storage reference.
- *
- * List API is only available for Firebase Rules Version 2.
- *
- * GCS is a key-blob store. Firebase Storage imposes the semantic of '/'
- * delimited folder structure.
- * Refer to GCS's List API if you want to learn more.
- *
- * To adhere to Firebase Rules's Semantics, Firebase Storage does not
- * support objects whose paths end with "/" or contain two consecutive
- * "/"s. Firebase Storage List API will filter these unsupported objects.
- * list() may fail if there are too many unsupported objects in the bucket.
- * @public
- *
- * @param ref - {@link StorageReference} to get list from.
- * @param options - See {@link ListOptions} for details.
- * @returns A `Promise` that resolves with the items and prefixes.
- * `prefixes` contains references to sub-folders and `items`
- * contains references to objects in this folder. `nextPageToken`
- * can be used to get the rest of the results.
- */
- function list(ref, options) {
- ref = util.getModularInstance(ref);
- return list$1(ref, options);
- }
- /**
- * List all items (files) and prefixes (folders) under this storage reference.
- *
- * This is a helper method for calling list() repeatedly until there are
- * no more results. The default pagination size is 1000.
- *
- * Note: The results may not be consistent if objects are changed while this
- * operation is running.
- *
- * Warning: `listAll` may potentially consume too many resources if there are
- * too many results.
- * @public
- * @param ref - {@link StorageReference} to get list from.
- *
- * @returns A `Promise` that resolves with all the items and prefixes under
- * the current storage reference. `prefixes` contains references to
- * sub-directories and `items` contains references to objects in this
- * folder. `nextPageToken` is never returned.
- */
- function listAll(ref) {
- ref = util.getModularInstance(ref);
- return listAll$1(ref);
- }
- /**
- * Returns the download URL for the given {@link StorageReference}.
- * @public
- * @param ref - {@link StorageReference} to get the download URL for.
- * @returns A `Promise` that resolves with the download
- * URL for this object.
- */
- function getDownloadURL(ref) {
- ref = util.getModularInstance(ref);
- return getDownloadURL$1(ref);
- }
- /**
- * Deletes the object at this location.
- * @public
- * @param ref - {@link StorageReference} for object to delete.
- * @returns A `Promise` that resolves if the deletion succeeds.
- */
- function deleteObject(ref) {
- ref = util.getModularInstance(ref);
- return deleteObject$1(ref);
- }
- function ref(serviceOrRef, pathOrUrl) {
- serviceOrRef = util.getModularInstance(serviceOrRef);
- return ref$1(serviceOrRef, pathOrUrl);
- }
- /**
- * @internal
- */
- function _getChild(ref, childPath) {
- return _getChild$1(ref, childPath);
- }
- /**
- * Gets a {@link FirebaseStorage} instance for the given Firebase app.
- * @public
- * @param app - Firebase app to get {@link FirebaseStorage} instance for.
- * @param bucketUrl - The gs:// url to your Firebase Storage Bucket.
- * If not passed, uses the app's default Storage Bucket.
- * @returns A {@link FirebaseStorage} instance.
- */
- function getStorage(app$1, bucketUrl) {
- if (app$1 === void 0) { app$1 = app.getApp(); }
- app$1 = util.getModularInstance(app$1);
- var storageProvider = app._getProvider(app$1, STORAGE_TYPE);
- var storageInstance = storageProvider.getImmediate({
- identifier: bucketUrl
- });
- var emulator = util.getDefaultEmulatorHostnameAndPort('storage');
- if (emulator) {
- connectStorageEmulator.apply(void 0, tslib.__spreadArray([storageInstance], emulator, false));
- }
- return storageInstance;
- }
- /**
- * Modify this {@link FirebaseStorage} instance to communicate with the Cloud Storage emulator.
- *
- * @param storage - The {@link FirebaseStorage} instance
- * @param host - The emulator host (ex: localhost)
- * @param port - The emulator port (ex: 5001)
- * @param options - Emulator options. `options.mockUserToken` is the mock auth
- * token to use for unit testing Security Rules.
- * @public
- */
- function connectStorageEmulator(storage, host, port, options) {
- if (options === void 0) { options = {}; }
- connectStorageEmulator$1(storage, host, port, options);
- }
-
- /**
- * @license
- * Copyright 2021 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /**
- * Downloads the data at the object's location. Returns an error if the object
- * is not found.
- *
- * To use this functionality, you have to whitelist your app's origin in your
- * Cloud Storage bucket. See also
- * https://cloud.google.com/storage/docs/configuring-cors
- *
- * This API is not available in Node.
- *
- * @public
- * @param ref - StorageReference where data should be downloaded.
- * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
- * retrieve.
- * @returns A Promise that resolves with a Blob containing the object's bytes
- */
- function getBlob(ref, maxDownloadSizeBytes) {
- ref = util.getModularInstance(ref);
- return getBlobInternal(ref, maxDownloadSizeBytes);
- }
- /**
- * Downloads the data at the object's location. Raises an error event if the
- * object is not found.
- *
- * This API is only available in Node.
- *
- * @public
- * @param ref - StorageReference where data should be downloaded.
- * @param maxDownloadSizeBytes - If set, the maximum allowed size in bytes to
- * retrieve.
- * @returns A stream with the object's data as bytes
- */
- function getStream(ref, maxDownloadSizeBytes) {
- throw new Error('getStream() is only supported by NodeJS builds');
- }
-
- /**
- * Cloud Storage for Firebase
- *
- * @packageDocumentation
- */
- function factory(container, _a) {
- var url = _a.instanceIdentifier;
- var app$1 = container.getProvider('app').getImmediate();
- var authProvider = container.getProvider('auth-internal');
- var appCheckProvider = container.getProvider('app-check-internal');
- return new FirebaseStorageImpl(app$1, authProvider, appCheckProvider, url, app.SDK_VERSION);
- }
- function registerStorage() {
- app._registerComponent(new component.Component(STORAGE_TYPE, factory, "PUBLIC" /* ComponentType.PUBLIC */).setMultipleInstances(true));
- //RUNTIME_ENV will be replaced during the compilation to "node" for nodejs and an empty string for browser
- app.registerVersion(name, version, '');
- // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
- app.registerVersion(name, version, 'cjs5');
- }
- registerStorage();
-
- exports.StringFormat = StringFormat;
- exports._FbsBlob = FbsBlob;
- exports._Location = Location;
- exports._TaskEvent = TaskEvent;
- exports._TaskState = TaskState;
- exports._UploadTask = UploadTask;
- exports._dataFromString = dataFromString;
- exports._getChild = _getChild;
- exports._invalidArgument = invalidArgument;
- exports._invalidRootOperation = invalidRootOperation;
- exports.connectStorageEmulator = connectStorageEmulator;
- exports.deleteObject = deleteObject;
- exports.getBlob = getBlob;
- exports.getBytes = getBytes;
- exports.getDownloadURL = getDownloadURL;
- exports.getMetadata = getMetadata;
- exports.getStorage = getStorage;
- exports.getStream = getStream;
- exports.list = list;
- exports.listAll = listAll;
- exports.ref = ref;
- exports.updateMetadata = updateMetadata;
- exports.uploadBytes = uploadBytes;
- exports.uploadBytesResumable = uploadBytesResumable;
- exports.uploadString = uploadString;
- //# sourceMappingURL=index.browser.cjs.js.map
|