From bec8a2cd1b2c84350979567d4f82d498b394ac1d Mon Sep 17 00:00:00 2001 From: AleksanderBodurri Date: Thu, 4 Mar 2021 15:15:09 -0500 Subject: [PATCH] refactor(devtools): implement programatic JSON download logic to replace dependency on filesaver library --- .../ng-devtools/src/lib/file-api-service.ts | 8 +- .../src/lib/vendor/filesaver/LICENSE.md | 11 --- .../src/lib/vendor/filesaver/index.ts | 81 ------------------- 3 files changed, 5 insertions(+), 95 deletions(-) delete mode 100644 projects/ng-devtools/src/lib/vendor/filesaver/LICENSE.md delete mode 100644 projects/ng-devtools/src/lib/vendor/filesaver/index.ts diff --git a/projects/ng-devtools/src/lib/file-api-service.ts b/projects/ng-devtools/src/lib/file-api-service.ts index a9190208fd2..022f29a28cd 100644 --- a/projects/ng-devtools/src/lib/file-api-service.ts +++ b/projects/ng-devtools/src/lib/file-api-service.ts @@ -1,6 +1,5 @@ import { Subject } from 'rxjs'; import { Injectable } from '@angular/core'; -import { saveAs } from './vendor/filesaver'; import { toISO8601Compact } from './vendor/chromium/date-utilities'; @Injectable({ @@ -23,7 +22,10 @@ export class FileApiService { } saveObjectAsJSON(object: object): void { - const blob = new Blob([JSON.stringify(object)], { type: 'application/json' }); - saveAs(blob, `NgDevTools-Profile-${toISO8601Compact(new Date())}.json`); + const downloadLink = document.createElement('a'); + downloadLink.download = `NgDevTools-Profile-${toISO8601Compact(new Date())}.json`; + downloadLink.href = URL.createObjectURL(new Blob([JSON.stringify(object)], { type: 'application/json' })); + downloadLink.click(); + setTimeout(() => URL.revokeObjectURL(downloadLink.href)); } } diff --git a/projects/ng-devtools/src/lib/vendor/filesaver/LICENSE.md b/projects/ng-devtools/src/lib/vendor/filesaver/LICENSE.md deleted file mode 100644 index 0fc696d0da0..00000000000 --- a/projects/ng-devtools/src/lib/vendor/filesaver/LICENSE.md +++ /dev/null @@ -1,11 +0,0 @@ -The MIT License - -Copyright © 2016 [Eli Grey][1]. - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -[1]: http://eligrey.com diff --git a/projects/ng-devtools/src/lib/vendor/filesaver/index.ts b/projects/ng-devtools/src/lib/vendor/filesaver/index.ts deleted file mode 100644 index 95518c1344d..00000000000 --- a/projects/ng-devtools/src/lib/vendor/filesaver/index.ts +++ /dev/null @@ -1,81 +0,0 @@ -// tslint:disable -/* - * FileSaver.js - * A saveAs() FileSaver implementation. - * - * By Eli Grey, http://eligrey.com - * - * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT) - * source : http://purl.eligrey.com/github/FileSaver.js - */ - -// The one and only way of getting global scope in all environments -// https://stackoverflow.com/q/3277182/1008999 -var _global = globalThis; - -function download(url, name) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', url); - xhr.responseType = 'blob'; - xhr.onload = function () { - saveAs(xhr.response, name); - }; - xhr.onerror = function () { - console.error('could not download file'); - }; - xhr.send(); -} - -function corsEnabled(url) { - var xhr = new XMLHttpRequest(); - // use sync to avoid popup blocker - xhr.open('HEAD', url, false); - try { - xhr.send(); - } catch (e) {} - return xhr.status >= 200 && xhr.status <= 299; -} - -// `a.click()` doesn't work for all browsers (#465) -function click(node, _?) { - try { - node.dispatchEvent(new MouseEvent('click')); - } catch (e) { - var evt = document.createEvent('MouseEvents'); - evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null); - node.dispatchEvent(evt); - } -} - -function saveAs(blob, name) { - var URL = _global.URL || _global.webkitURL; - var a = document.createElement('a'); - name = name || blob.name || 'download'; - - a.download = name; - a.rel = 'noopener'; // tabnabbing - - // TODO: detect chrome extensions & packaged apps - // a.target = '_blank' - - if (typeof blob === 'string') { - // Support regular links - a.href = blob; - if (a.origin !== location.origin) { - corsEnabled(a.href) ? download(blob, name) : click(a, (a.target = '_blank')); - } else { - click(a); - } - } else { - // Support blobs - a.href = URL.createObjectURL(blob); - setTimeout(function () { - URL.revokeObjectURL(a.href); - }, 4e4); // 40s - setTimeout(function () { - click(a); - }, 0); - } -} - -export { saveAs };