diff --git a/apps/remix-ide-e2e/src/tests/migrateFileSystem.test.ts b/apps/remix-ide-e2e/src/tests/migrateFileSystem.test.ts index f10e6c713b..3d04a3edfc 100644 --- a/apps/remix-ide-e2e/src/tests/migrateFileSystem.test.ts +++ b/apps/remix-ide-e2e/src/tests/migrateFileSystem.test.ts @@ -1,25 +1,90 @@ 'use strict' import { NightwatchBrowser } from 'nightwatch' -import init from '../helpers/init' + module.exports = { - before: function (browser: NightwatchBrowser, done: VoidFunction) { - init(browser, done, 'http://127.0.0.1:8080?e2e_testmigration=true', false) + '@disabled': true, + 'Should load the testmigration url #group1': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow() + .waitForElementVisible('*[data-id="skipbackup-btn"]', 5000) + .click('*[data-id="skipbackup-btn"]') + .waitForElementVisible('[id="remixTourSkipbtn"]') + .click('[id="remixTourSkipbtn"]') }, - 'Should have README file with TEST README as content': function (browser: NightwatchBrowser) { - browser.waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) + 'Should load the testmigration url and refresh and still have test data #group7': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow() + .waitForElementVisible('*[data-id="skipbackup-btn"]', 5000) + .click('*[data-id="skipbackup-btn"]') + .waitForElementVisible('[id="remixTourSkipbtn"]') + .click('[id="remixTourSkipbtn"]').refresh() + }, + 'should have indexedDB storage in terminal #group1 #group7': function (browser: NightwatchBrowser) { + browser.assert.containsText('*[data-id="terminalJournal"]', 'indexedDB') + }, + 'Should fallback to localstorage with default data #group2': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration_fallback=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow() + .waitForElementVisible('[id="remixTourSkipbtn"]') + .click('[id="remixTourSkipbtn"]') + .waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) + .waitForElementVisible('div[data-id="filePanelFileExplorerTree"]') + .openFile('README.txt') + .getEditorValue((content) => { + browser.assert.ok(content.includes('Output from script will appear in remix terminal.')) + }) + .click('*[data-id="treeViewLitreeViewItemcontracts"]') + .openFile('contracts/1_Storage.sol') + .getEditorValue((content) => { + browser.assert.ok(content.includes('function retrieve() public view returns (uint256){')) + }) + }, + 'Should load the testmigration url with local storage anabled #group3': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration=true&e2e_testmigration_fallback=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow() + .waitForElementVisible('*[data-id="skipbackup-btn"]', 5000) + .click('*[data-id="skipbackup-btn"]') + .waitForElementVisible('[id="remixTourSkipbtn"]') + .click('[id="remixTourSkipbtn"]') + }, + 'Should generate error in migration by deleting indexedDB and falling back to local storage with test #group5': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow().execute(('delete window.indexedDB')) + .waitForElementVisible('*[data-id="skipbackup-btn"]', 5000) + .click('*[data-id="skipbackup-btn"]') + .waitForElementVisible('[id="remixTourSkipbtn"]') + .click('[id="remixTourSkipbtn"]') + }, + 'should have localstorage storage in terminal #group2 #group3 #group5': function (browser: NightwatchBrowser) { + browser.assert.containsText('*[data-id="terminalJournal"]', 'localstorage') + }, + 'Should have README file with TEST README as content #group1 #group3': function (browser: NightwatchBrowser) { + browser + .waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) .waitForElementVisible('div[data-id="filePanelFileExplorerTree"]') .openFile('TEST_README.txt') .getEditorValue((content) => { browser.assert.equal(content, 'TEST README') }) }, - 'Should have a workspace_test': function (browser: NightwatchBrowser) { + // these are test data entries + 'Should have a workspace_test #group1 #group3 #group5 #group7': function (browser: NightwatchBrowser) { browser.waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) .click('*[data-id="workspacesSelect"] option[value="workspace_test"]') .waitForElementVisible('*[data-id="treeViewLitreeViewItemtest_contracts"]') }, - 'Should have a sol file with test data': function (browser: NightwatchBrowser) { + 'Should have a sol file with test data #group1 #group3 #group5 #group7': function (browser: NightwatchBrowser) { browser.waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) .click('*[data-id="treeViewLitreeViewItemtest_contracts"]') .openFile('test_contracts/1_Storage.sol') @@ -27,7 +92,7 @@ module.exports = { browser.assert.equal(content, 'testing') }) }, - 'Should have a artifacts file with JSON test data': function (browser: NightwatchBrowser) { + 'Should have a artifacts file with JSON test data #group1 #group3 #group5 #group7': function (browser: NightwatchBrowser) { browser.waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) .click('*[data-id="treeViewLitreeViewItemtest_contracts/artifacts"]') .openFile('test_contracts/artifacts/Storage_metadata.json') @@ -35,5 +100,27 @@ module.exports = { const metadata = JSON.parse(content) browser.assert.equal(metadata.test, 'data') }) - } + }, + 'Should have a empty workspace #group1 #group3 #group5 #group7': function (browser: NightwatchBrowser) { + browser.waitForElementVisible('*[data-id="remixIdeSidePanel"]', 5000) + .click('*[data-id="workspacesSelect"] option[value="emptyspace"]') + }, + // end of test data entries + 'Should load with all storage blocked #group4': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testblock_storage=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow() + .assert.containsText('.alert-warning', 'Your browser does not support') + }, + 'Should with errors #group6': function (browser: NightwatchBrowser) { + browser.url('http://127.0.0.1:8080?e2e_testmigration=true') + .pause(6000) + .switchBrowserTab(0) + .maximizeWindow().execute('delete window.localStorage') + .waitForElementVisible('*[data-id="skipbackup-btn"]', 5000) + .click('*[data-id="skipbackup-btn"]') + .assert.containsText('.alert-danger', 'An unknown error') + }, + } diff --git a/apps/remix-ide/src/app/components/preload.tsx b/apps/remix-ide/src/app/components/preload.tsx new file mode 100644 index 0000000000..0aade77f75 --- /dev/null +++ b/apps/remix-ide/src/app/components/preload.tsx @@ -0,0 +1,130 @@ +import { RemixApp } from '@remix-ui/app' +import React, { useEffect, useRef, useState } from 'react' +import { render } from 'react-dom' +import * as packageJson from '../../../../../package.json' +import { fileSystem, fileSystems } from '../files/fileSystem' +import { indexedDBFileSystem } from '../files/filesystems/indexedDB' +import { localStorageFS } from '../files/filesystems/localStorage' +import { fileSystemUtility, migrationTestData } from '../files/filesystems/fileSystemUtility' +import './styles/preload.css' +const _paq = window._paq = window._paq || [] + +export const Preload = () => { + + const [supported, setSupported] = useState(true) + const [error, setError] = useState(false) + const [showDownloader, setShowDownloader] = useState(false) + const remixFileSystems = useRef(new fileSystems()) + const remixIndexedDB = useRef(new indexedDBFileSystem()) + const localStorageFileSystem = useRef(new localStorageFS()) + // url parameters to e2e test the fallbacks and error warnings + const testmigrationFallback = useRef(window.location.hash.includes('e2e_testmigration_fallback=true') && window.location.host === '127.0.0.1:8080' && window.location.protocol === 'http:') + const testmigrationResult = useRef(window.location.hash.includes('e2e_testmigration=true') && window.location.host === '127.0.0.1:8080' && window.location.protocol === 'http:') + const testBlockStorage = useRef(window.location.hash.includes('e2e_testblock_storage=true') && window.location.host === '127.0.0.1:8080' && window.location.protocol === 'http:') + + function loadAppComponent() { + import('../../app').then((AppComponent) => { + const appComponent = new AppComponent.default() + appComponent.run().then(() => { + render( + <> + + , + document.getElementById('root') + ) + }) + }).catch(err => { + _paq.push(['_trackEvent', 'Preload', 'error', err && err.message]) + console.log('Error loading Remix:', err) + setError(true) + }) + } + + const downloadBackup = async () => { + setShowDownloader(false) + const fsUtility = new fileSystemUtility() + await fsUtility.downloadBackup(remixFileSystems.current.fileSystems['localstorage']) + await migrateAndLoad() + } + + const migrateAndLoad = async () => { + setShowDownloader(false) + const fsUtility = new fileSystemUtility() + const migrationResult = await fsUtility.migrate(localStorageFileSystem.current, remixIndexedDB.current) + _paq.push(['_trackEvent', 'Migrate', 'result', migrationResult?'success' : 'fail']) + await setFileSystems() + } + + const setFileSystems = async() => { + const fsLoaded = await remixFileSystems.current.setFileSystem([(testmigrationFallback.current || testBlockStorage.current)? null: remixIndexedDB.current, testBlockStorage.current? null:localStorageFileSystem.current]) + if (fsLoaded) { + console.log(fsLoaded.name + ' activated') + _paq.push(['_trackEvent', 'Storage', 'activate', fsLoaded.name]) + loadAppComponent() + } else { + _paq.push(['_trackEvent', 'Storage', 'error', 'no supported storage']) + setSupported(false) + } + } + + const testmigration = async() => { + if (testmigrationResult.current) { + const fsUtility = new fileSystemUtility() + fsUtility.populateWorkspace(migrationTestData, remixFileSystems.current.fileSystems['localstorage'].fs) + } + } + + useEffect(() => { + async function loadStorage() { + await remixFileSystems.current.addFileSystem(remixIndexedDB.current) || _paq.push(['_trackEvent', 'Storage', 'error', 'indexedDB not supported']) + await remixFileSystems.current.addFileSystem(localStorageFileSystem.current) || _paq.push(['_trackEvent', 'Storage', 'error', 'localstorage not supported']) + await testmigration() + remixIndexedDB.current.loaded && await remixIndexedDB.current.checkWorkspaces() + localStorageFileSystem.current.loaded && await localStorageFileSystem.current.checkWorkspaces() + remixIndexedDB.current.loaded && ( (remixIndexedDB.current.hasWorkSpaces || !localStorageFileSystem.current.hasWorkSpaces)? await setFileSystems():setShowDownloader(true)) + !remixIndexedDB.current.loaded && await setFileSystems() + } + loadStorage() + }, []) + + return <> +
+
+ {logo} +
+ REMIX IDE +
+ v{packageJson.version} +
+
+ {!supported ? +
+ Your browser does not support any of the filesytems required by Remix. + Either change the settings in your browser or use a supported browser. +
: null} + {error ? +
+ An unknown error has occured loading the application. +
: null} + {showDownloader ? +
+ This app will be updated now. Please download a backup of your files now to make sure you don't lose your work. +

+ You don't need to do anything else, your files will be available when the app loads. +
{ await downloadBackup() }} data-id='downloadbackup-btn' className='btn btn-primary mt-1'>download backup
+
{ await migrateAndLoad() }} data-id='skipbackup-btn' className='btn btn-primary mt-1'>skip backup
+
: null} + {(supported && !error && !showDownloader) ? +
+ +
: null} +
+ +} + + +const logo = + + + + \ No newline at end of file diff --git a/apps/remix-ide/src/app/components/styles/preload.css b/apps/remix-ide/src/app/components/styles/preload.css new file mode 100644 index 0000000000..266cbfe834 --- /dev/null +++ b/apps/remix-ide/src/app/components/styles/preload.css @@ -0,0 +1,23 @@ +.preload-container { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + height: 100vh; +} + +.preload-info-container { + display: flex; + flex-direction: column; + text-align: center; + max-width: 400px; +} + +.preload-info-container .btn { + cursor: pointer; +} + +.preload-logo { + min-width: 200px; + padding-bottom: 1.5rem !important; +} \ No newline at end of file diff --git a/apps/remix-ide/src/app/files/fileSystem.ts b/apps/remix-ide/src/app/files/fileSystem.ts new file mode 100644 index 0000000000..80b7663680 --- /dev/null +++ b/apps/remix-ide/src/app/files/fileSystem.ts @@ -0,0 +1,72 @@ +export class fileSystem { + name: string + enabled: boolean + available: boolean + fs: any + fsCallBack: any; + hasWorkSpaces: boolean + loaded: boolean + load: () => Promise + test: () => Promise + + constructor() { + this.available = false + this.enabled = false + this.hasWorkSpaces = false + this.loaded = false + } + + checkWorkspaces = async () => { + try { + await this.fs.stat('.workspaces') + this.hasWorkSpaces = true + } catch (e) { + + } + } + + set = async () => { + const w = (window as any) + if (!this.loaded) return false + w.remixFileSystem = this.fs + w.remixFileSystem.name = this.name + w.remixFileSystemCallback = this.fsCallBack + return true + } +} + +export class fileSystems { + fileSystems: Record + constructor() { + this.fileSystems = {} + } + + addFileSystem = async (fs: fileSystem): Promise => { + try { + this.fileSystems[fs.name] = fs + await fs.test() && await fs.load() + console.log(fs.name + ' is loaded...') + return true + } catch (e) { + console.log(fs.name + ' not available...') + return false + } + } + /** + * sets filesystem using list as fallback + * @param {string[]} names + * @returns {Promise} + */ + setFileSystem = async (filesystems?: fileSystem[]): Promise => { + for (const fs of filesystems) { + if (fs && this.fileSystems[fs.name]) { + const result = await this.fileSystems[fs.name].set() + if (result) return this.fileSystems[fs.name] + } + } + return null + } + + +} + diff --git a/apps/remix-ide/src/app/files/filesystems/fileSystemUtility.ts b/apps/remix-ide/src/app/files/filesystems/fileSystemUtility.ts new file mode 100644 index 0000000000..0873a7d288 --- /dev/null +++ b/apps/remix-ide/src/app/files/filesystems/fileSystemUtility.ts @@ -0,0 +1,190 @@ +import { hashMessage } from "ethers/lib/utils" +import JSZip from "jszip" +import { fileSystem } from "../fileSystem" +const _paq = window._paq = window._paq || [] +export class fileSystemUtility { + migrate = async (fsFrom: fileSystem, fsTo: fileSystem) => { + try { + await fsFrom.checkWorkspaces() + await fsTo.checkWorkspaces() + + if (fsTo.hasWorkSpaces) { + console.log(`${fsTo.name} already has files`) + return true + } + + if (!fsFrom.hasWorkSpaces) { + console.log('no files to migrate') + return true + } + + const fromFiles = await this.copyFolderToJson('/', null, null, fsFrom.fs) + await this.populateWorkspace(fromFiles, fsTo.fs) + const toFiles = await this.copyFolderToJson('/', null, null, fsTo.fs) + + if (hashMessage(JSON.stringify(toFiles)) === hashMessage(JSON.stringify(fromFiles))) { + console.log('file migration successful') + return true + } else { + _paq.push(['_trackEvent', 'Migrate', 'error', 'hash mismatch']) + console.log('file migration failed falling back to ' + fsFrom.name) + fsTo.loaded = false + return false + } + } catch (err) { + console.log(err) + _paq.push(['_trackEvent', 'Migrate', 'error', err && err.message]) + console.log('file migration failed falling back to ' + fsFrom.name) + fsTo.loaded = false + return false + } + } + + downloadBackup = async (fs: fileSystem) => { + try { + const zip = new JSZip() + await fs.checkWorkspaces() + await this.copyFolderToJson('/', null, null, fs.fs, ({ path, content }) => { + zip.file(path, content) + }) + const blob = await zip.generateAsync({ type: 'blob' }) + const today = new Date() + const date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate() + const time = today.getHours() + 'h' + today.getMinutes() + 'min' + this.saveAs(blob, `remix-backup-at-${time}-${date}.zip`) + _paq.push(['_trackEvent','Backup','download','preload']) + } catch (err) { + _paq.push(['_trackEvent','Backup','error',err && err.message]) + console.log(err) + } + } + + populateWorkspace = async (json, fs) => { + for (const item in json) { + const isFolder = json[item].content === undefined + if (isFolder) { + await this.createDir(item, fs) + await this.populateWorkspace(json[item].children, fs) + } else { + await fs.writeFile(item, json[item].content, 'utf8') + } + } + } + + + /** + * copy the folder recursively + * @param {string} path is the folder to be copied over + * @param {Function} visitFile is a function called for each visited files + * @param {Function} visitFolder is a function called for each visited folders + */ + copyFolderToJson = async (path, visitFile, visitFolder, fs, cb = null) => { + visitFile = visitFile || (() => { }) + visitFolder = visitFolder || (() => { }) + return await this._copyFolderToJsonInternal(path, visitFile, visitFolder, fs, cb) + } + + /** + * copy the folder recursively (internal use) + * @param {string} path is the folder to be copied over + * @param {Function} visitFile is a function called for each visited files + * @param {Function} visitFolder is a function called for each visited folders + */ + async _copyFolderToJsonInternal(path, visitFile, visitFolder, fs, cb) { + visitFile = visitFile || function () { /* do nothing. */ } + visitFolder = visitFolder || function () { /* do nothing. */ } + + const json = {} + // path = this.removePrefix(path) + if (await fs.exists(path)) { + const items = await fs.readdir(path) + visitFolder({ path }) + if (items.length !== 0) { + for (const item of items) { + const file: any = {} + const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` + if ((await fs.stat(curPath)).isDirectory()) { + file.children = await this._copyFolderToJsonInternal(curPath, visitFile, visitFolder, fs, cb) + } else { + file.content = await fs.readFile(curPath, 'utf8') + if (cb) cb({ path: curPath, content: file.content }) + visitFile({ path: curPath, content: file.content }) + + } + json[curPath] = file + } + } + } + return json + } + + createDir = async (path, fs) => { + const paths = path.split('/') + if (paths.length && paths[0] === '') paths.shift() + let currentCheck = '' + for (const value of paths) { + currentCheck = currentCheck + (currentCheck ? '/' : '') + value + if (!await fs.exists(currentCheck)) { + await fs.mkdir(currentCheck) + } + } + } + + saveAs = (blob, name) => { + const node = document.createElement('a') + node.download = name + node.rel = 'noopener' + node.href = URL.createObjectURL(blob) + setTimeout(function () { URL.revokeObjectURL(node.href) }, 4E4) // 40s + setTimeout(function () { + try { + node.dispatchEvent(new MouseEvent('click')) + } catch (e) { + const evt = document.createEvent('MouseEvents') + evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, + 20, false, false, false, false, 0, null) + node.dispatchEvent(evt) + } + }, 0) // 40s + } +} + + +/* eslint-disable no-template-curly-in-string */ +export const migrationTestData = { + '.workspaces': { + children: { + '.workspaces/default_workspace': { + children: { + '.workspaces/default_workspace/README.txt': { + content: 'TEST README' + } + } + }, + '.workspaces/emptyspace': { + + }, + '.workspaces/workspace_test': { + children: { + '.workspaces/workspace_test/TEST_README.txt': { + content: 'TEST README' + }, + '.workspaces/workspace_test/test_contracts': { + children: { + '.workspaces/workspace_test/test_contracts/1_Storage.sol': { + content: 'testing' + }, + '.workspaces/workspace_test/test_contracts/artifacts': { + children: { + '.workspaces/workspace_test/test_contracts/artifacts/Storage_metadata.json': { + content: '{ "test": "data" }' + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/apps/remix-ide/src/app/files/filesystems/indexedDB.ts b/apps/remix-ide/src/app/files/filesystems/indexedDB.ts new file mode 100644 index 0000000000..5d20a52061 --- /dev/null +++ b/apps/remix-ide/src/app/files/filesystems/indexedDB.ts @@ -0,0 +1,91 @@ +import LightningFS from "@isomorphic-git/lightning-fs" +import { fileSystem } from "../fileSystem" + +export class IndexedDBStorage extends LightningFS { + base: LightningFS.PromisifedFS + addSlash: (file: string) => string + extended: { exists: (path: string) => Promise; rmdir: (path: any) => Promise; readdir: (path: any) => Promise; unlink: (path: any) => Promise; mkdir: (path: any) => Promise; readFile: (path: any, options: any) => Promise; rename: (from: any, to: any) => Promise; writeFile: (path: any, content: any, options: any) => Promise; stat: (path: any) => Promise; init(name: string, opt?: LightningFS.FSConstructorOptions): void; activate(): Promise; deactivate(): Promise; lstat(filePath: string): Promise; readlink(filePath: string): Promise; symlink(target: string, filePath: string): Promise } + constructor(name: string) { + super(name) + this.addSlash = (file) => { + if (!file.startsWith('/')) file = '/' + file + return file + } + this.base = this.promises + this.extended = { + ...this.promises, + exists: async (path: string) => { + return new Promise((resolve) => { + this.base.stat(this.addSlash(path)).then(() => resolve(true)).catch(() => resolve(false)) + }) + }, + rmdir: async (path) => { + return this.base.rmdir(this.addSlash(path)) + }, + readdir: async (path) => { + return this.base.readdir(this.addSlash(path)) + }, + unlink: async (path) => { + return this.base.unlink(this.addSlash(path)) + }, + mkdir: async (path) => { + return this.base.mkdir(this.addSlash(path)) + }, + readFile: async (path, options) => { + return this.base.readFile(this.addSlash(path), options) + }, + rename: async (from, to) => { + return this.base.rename(this.addSlash(from), this.addSlash(to)) + }, + writeFile: async (path, content, options) => { + return this.base.writeFile(this.addSlash(path), content, options) + }, + stat: async (path) => { + return this.base.stat(this.addSlash(path)) + } + } + } +} + + +export class indexedDBFileSystem extends fileSystem { + constructor() { + super() + this.name = 'indexedDB' + } + + load = async () => { + return new Promise((resolve, reject) => { + try { + const fs = new IndexedDBStorage('RemixFileSystem') + fs.init('RemixFileSystem') + this.fs = fs.extended + this.fsCallBack = fs + this.loaded = true + resolve(true) + } catch (e) { + reject(e) + } + }) + } + + test = async () => { + return new Promise((resolve, reject) => { + if (!window.indexedDB) { + this.available = false + reject('No indexedDB on window') + } + const request = window.indexedDB.open("RemixTestDataBase"); + request.onerror = () => { + this.available = false + reject('Error creating test database') + }; + request.onsuccess = () => { + window.indexedDB.deleteDatabase("RemixTestDataBase"); + this.available = true + resolve(true) + }; + }) + } + +} \ No newline at end of file diff --git a/apps/remix-ide/src/app/files/filesystems/localStorage.ts b/apps/remix-ide/src/app/files/filesystems/localStorage.ts new file mode 100644 index 0000000000..8346a37976 --- /dev/null +++ b/apps/remix-ide/src/app/files/filesystems/localStorage.ts @@ -0,0 +1,57 @@ +import { fileSystem } from "../fileSystem"; + +export class localStorageFS extends fileSystem { + + constructor() { + super() + this.name = 'localstorage' + } + load = async () => { + const me = this + return new Promise((resolve, reject) => { + try { + const w = window as any + w.BrowserFS.install(window) + w.BrowserFS.configure({ + fs: 'LocalStorage' + }, async function (e) { + if (e) { + console.log('BrowserFS Error: ' + e) + reject(e) + } else { + me.fs = { ...window.require('fs') } + me.fsCallBack = window.require('fs') + me.fs.readdir = me.fs.readdirSync + me.fs.readFile = me.fs.readFileSync + me.fs.writeFile = me.fs.writeFileSync + me.fs.stat = me.fs.statSync + me.fs.unlink = me.fs.unlinkSync + me.fs.rmdir = me.fs.rmdirSync + me.fs.mkdir = me.fs.mkdirSync + me.fs.rename = me.fs.renameSync + me.fs.exists = me.fs.existsSync + me.loaded = true + resolve(true) + } + }) + } catch (e) { + console.log('BrowserFS is not ready!') + reject(e) + } + }) + } + + test = async () => { + return new Promise((resolve, reject) => { + const test = 'test'; + try { + localStorage.setItem(test, test); + localStorage.removeItem(test); + resolve(true) + } catch(e) { + reject(e) + } + }) + } + +} \ No newline at end of file diff --git a/apps/remix-ide/src/app/plugins/storage.ts b/apps/remix-ide/src/app/plugins/storage.ts index 2bf50fdb58..d52e2302eb 100644 --- a/apps/remix-ide/src/app/plugins/storage.ts +++ b/apps/remix-ide/src/app/plugins/storage.ts @@ -4,7 +4,7 @@ const profile = { name: 'storage', displayName: 'Storage', description: 'Storage', - methods: ['getStorage'] + methods: ['getStorage', 'formatString'] }; export class StoragePlugin extends Plugin { @@ -13,10 +13,47 @@ export class StoragePlugin extends Plugin { } async getStorage() { - if ('storage' in navigator && 'estimate' in navigator.storage) { - return navigator.storage.estimate() + let storage = null + if ('storage' in navigator && 'estimate' in navigator.storage && (window as any).remixFileSystem.name !== 'localstorage') { + storage = navigator.storage.estimate() } else { - throw new Error("Can't get storage quota"); + storage ={ + usage: parseFloat(this.calculateLocalStorage()) * 1000, + quota: 5000000, + } } + const _paq = window._paq = window._paq || [] + _paq.push(['trackEvent', 'Storage', 'used', this.formatString(storage)]); + return storage + } + + formatString(storage) { + return `${this.formatBytes(storage.usage)} / ${this.formatBytes(storage.quota)}`; + } + + calculateLocalStorage() { + var _lsTotal = 0 + var _xLen; var _x + for (_x in localStorage) { + // eslint-disable-next-line no-prototype-builtins + if (!localStorage.hasOwnProperty(_x)) { + continue + } + _xLen = ((localStorage[_x].length + _x.length) * 2) + _lsTotal += _xLen + } + return (_lsTotal / 1024).toFixed(2) + } + + formatBytes(bytes: number, decimals = 2) { + if (bytes === 0) return '0 Bytes'; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } } diff --git a/apps/remix-ide/src/app/tabs/theme-module.js b/apps/remix-ide/src/app/tabs/theme-module.js index 78c8f1788d..2fd9e92e1a 100644 --- a/apps/remix-ide/src/app/tabs/theme-module.js +++ b/apps/remix-ide/src/app/tabs/theme-module.js @@ -31,17 +31,20 @@ export class ThemeModule extends Plugin { super(profile) this.events = new EventEmitter() this._deps = { - config: Registry.getInstance().get('config').api + config: Registry.getInstance().get('config') && Registry.getInstance().get('config').api } - this.themes = themes.reduce((acc, theme) => { - theme.url = window.location.origin + window.location.pathname + theme.url - return { ...acc, [theme.name.toLocaleLowerCase()]: theme } - }, {}) + this.themes = {} + themes.map((theme) => { + this.themes[theme.name.toLocaleLowerCase()] = { + ...theme, + url: window.location.origin + window.location.pathname + theme.url + } + }) this._paq = _paq let queryTheme = (new QueryParams()).get().theme queryTheme = queryTheme && queryTheme.toLocaleLowerCase() queryTheme = this.themes[queryTheme] ? queryTheme : null - let currentTheme = this._deps.config.get('settings/theme') + let currentTheme = (this._deps.config && this._deps.config.get('settings/theme')) || null currentTheme = currentTheme && currentTheme.toLocaleLowerCase() currentTheme = this.themes[currentTheme] ? currentTheme : null this.currentThemeState = { queryTheme, currentTheme } @@ -65,6 +68,7 @@ export class ThemeModule extends Plugin { initTheme (callback) { // callback is setTimeOut in app.js which is always passed if (callback) this.initCallback = callback if (this.active) { + document.getElementById('theme-link') ? document.getElementById('theme-link').remove():null const nextTheme = this.themes[this.active] // Theme document.documentElement.style.setProperty('--theme', nextTheme.quality) @@ -93,9 +97,9 @@ export class ThemeModule extends Plugin { _paq.push(['trackEvent', 'themeModule', 'switchTo', next]) const nextTheme = this.themes[next] // Theme if (!this.forced) this._deps.config.set('settings/theme', next) - document.getElementById('theme-link').remove() - const theme = document.createElement('link') + document.getElementById('theme-link') ? document.getElementById('theme-link').remove():null + const theme = document.createElement('link') theme.setAttribute('rel', 'stylesheet') theme.setAttribute('href', nextTheme.url) theme.setAttribute('id', 'theme-link') diff --git a/apps/remix-ide/src/assets/js/init.js b/apps/remix-ide/src/assets/js/init.js index f3cd64d79f..8265067b82 100644 --- a/apps/remix-ide/src/assets/js/init.js +++ b/apps/remix-ide/src/assets/js/init.js @@ -1,5 +1,5 @@ /* eslint-disable prefer-promise-reject-errors */ -function urlParams () { +function urlParams() { var qs = window.location.hash.substr(1) if (window.location.search.length > 0) { @@ -41,67 +41,12 @@ for (const k in assets[versionToLoad]) { } window.onload = () => { - // eslint-disable-next-line no-undef - class RemixFileSystem extends LightningFS { - constructor (...t) { - super(...t) - this.addSlash = (file) => { - if (!file.startsWith('/')) file = '/' + file - return file - } - this.base = this.promises - this.promises = { - ...this.promises, - - exists: async (path) => { - return new Promise((resolve, reject) => { - this.base.stat(this.addSlash(path)).then(() => resolve(true)).catch(() => resolve(false)) - }) - }, - rmdir: async (path) => { - return this.base.rmdir(this.addSlash(path)) - }, - readdir: async (path) => { - return this.base.readdir(this.addSlash(path)) - }, - unlink: async (path) => { - return this.base.unlink(this.addSlash(path)) - }, - mkdir: async (path) => { - return this.base.mkdir(this.addSlash(path)) - }, - readFile: async (path, options) => { - return this.base.readFile(this.addSlash(path), options) - }, - rename: async (from, to) => { - return this.base.rename(this.addSlash(from), this.addSlash(to)) - }, - writeFile: async (path, content, options) => { - return this.base.writeFile(this.addSlash(path), content, options) - }, - stat: async (path) => { - return this.base.stat(this.addSlash(path)) - } - } - } - } - - function loadApp () { + function loadApp() { const app = document.createElement('script') app.setAttribute('src', versions[versionToLoad]) document.body.appendChild(app) } - window.remixFileSystemCallback = new RemixFileSystem() - window.remixFileSystemCallback.init('RemixFileSystem').then(() => { - window.remixFileSystem = window.remixFileSystemCallback.promises - // check if .workspaces is present in indexeddb - window.remixFileSystem.stat('.workspaces').then((dir) => { - if (dir.isDirectory()) loadApp() - }).catch(() => { - // no indexeddb .workspaces -> run migration - // eslint-disable-next-line no-undef - migrateFilesFromLocalStorage(loadApp) - }) - }) + loadApp() + return } diff --git a/apps/remix-ide/src/assets/js/lightning-fs.min.js b/apps/remix-ide/src/assets/js/lightning-fs.min.js deleted file mode 100644 index a306197743..0000000000 --- a/apps/remix-ide/src/assets/js/lightning-fs.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.LightningFS=e():t.LightningFS=e()}(self,function(){return function(t){var e={};function i(n){if(e[n])return e[n].exports;var s=e[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,n){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(n,s,function(e){return t[e]}.bind(null,s));return n},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=3)}([function(t,e){function i(t){if(0===t.length)return".";let e=s(t);return e=e.reduce(r,[]),n(...e)}function n(...t){if(0===t.length)return"";let e=t.join("/");return e=e.replace(/\/{2,}/g,"/")}function s(t){if(0===t.length)return[];if("/"===t)return["/"];let e=t.split("/");return""===e[e.length-1]&&e.pop(),"/"===t[0]?e[0]="/":"."!==e[0]&&e.unshift("."),e}function r(t,e){if(0===t.length)return t.push(e),t;if("."===e)return t;if(".."===e){if(1===t.length){if("/"===t[0])throw new Error("Unable to normalize path - traverses above root directory");if("."===t[0])return t.push(e),t}return".."===t[t.length-1]?(t.push(".."),t):(t.pop(),t)}return t.push(e),t}t.exports={join:n,normalize:i,split:s,basename:function(t){if("/"===t)throw new Error(`Cannot get basename of "${t}"`);const e=t.lastIndexOf("/");return-1===e?t:t.slice(e+1)},dirname:function(t){const e=t.lastIndexOf("/");if(-1===e)throw new Error(`Cannot get dirname of "${t}"`);return 0===e?"/":t.slice(0,e)},resolve:function(...t){let e="";for(let s of t)e=s.startsWith("/")?s:i(n(e,s));return e}}},function(t,e){function i(t){return class extends Error{constructor(...e){super(...e),this.code=t,this.message?this.message=t+": "+this.message:this.message=t}}}const n=i("EEXIST"),s=i("ENOENT"),r=i("ENOTDIR"),o=i("ENOTEMPTY"),a=i("ETIMEDOUT");t.exports={EEXIST:n,ENOENT:s,ENOTDIR:r,ENOTEMPTY:o,ETIMEDOUT:a}},function(t,e,i){"use strict";i.r(e),i.d(e,"Store",function(){return n}),i.d(e,"get",function(){return o}),i.d(e,"set",function(){return a}),i.d(e,"update",function(){return h}),i.d(e,"del",function(){return c}),i.d(e,"clear",function(){return l}),i.d(e,"keys",function(){return u}),i.d(e,"close",function(){return d});class n{constructor(t="keyval-store",e="keyval"){this.storeName=e,this._dbName=t,this._storeName=e,this._init()}_init(){this._dbp||(this._dbp=new Promise((t,e)=>{const i=indexedDB.open(this._dbName);i.onerror=(()=>e(i.error)),i.onsuccess=(()=>t(i.result)),i.onupgradeneeded=(()=>{i.result.createObjectStore(this._storeName)})}))}_withIDBStore(t,e){return this._init(),this._dbp.then(i=>new Promise((n,s)=>{const r=i.transaction(this.storeName,t);r.oncomplete=(()=>n()),r.onabort=r.onerror=(()=>s(r.error)),e(r.objectStore(this.storeName))}))}_close(){return this._init(),this._dbp.then(t=>{t.close(),this._dbp=void 0})}}let s;function r(){return s||(s=new n),s}function o(t,e=r()){let i;return e._withIDBStore("readwrite",e=>{i=e.get(t)}).then(()=>i.result)}function a(t,e,i=r()){return i._withIDBStore("readwrite",i=>{i.put(e,t)})}function h(t,e,i=r()){return i._withIDBStore("readwrite",i=>{const n=i.get(t);n.onsuccess=(()=>{i.put(e(n.result),t)})})}function c(t,e=r()){return e._withIDBStore("readwrite",e=>{e.delete(t)})}function l(t=r()){return t._withIDBStore("readwrite",t=>{t.clear()})}function u(t=r()){const e=[];return t._withIDBStore("readwrite",t=>{(t.openKeyCursor||t.openCursor).call(t).onsuccess=function(){this.result&&(e.push(this.result.key),this.result.continue())}}).then(()=>e)}function d(t=r()){return t._close()}},function(t,e,i){const n=i(4),s=i(5);function r(t,e){"function"==typeof t&&(e=t);return[(...t)=>e(null,...t),e=n(e)]}t.exports=class{constructor(...t){this.promises=new s(...t),this.init=this.init.bind(this),this.readFile=this.readFile.bind(this),this.writeFile=this.writeFile.bind(this),this.unlink=this.unlink.bind(this),this.readdir=this.readdir.bind(this),this.mkdir=this.mkdir.bind(this),this.rmdir=this.rmdir.bind(this),this.rename=this.rename.bind(this),this.stat=this.stat.bind(this),this.lstat=this.lstat.bind(this),this.readlink=this.readlink.bind(this),this.symlink=this.symlink.bind(this),this.backFile=this.backFile.bind(this),this.du=this.du.bind(this)}init(t,e){return this.promises.init(t,e)}readFile(t,e,i){const[n,s]=r(e,i);this.promises.readFile(t,e).then(n).catch(s)}writeFile(t,e,i,n){const[s,o]=r(i,n);this.promises.writeFile(t,e,i).then(s).catch(o)}unlink(t,e,i){const[n,s]=r(e,i);this.promises.unlink(t,e).then(n).catch(s)}readdir(t,e,i){const[n,s]=r(e,i);this.promises.readdir(t,e).then(n).catch(s)}mkdir(t,e,i){const[n,s]=r(e,i);this.promises.mkdir(t,e).then(n).catch(s)}rmdir(t,e,i){const[n,s]=r(e,i);this.promises.rmdir(t,e).then(n).catch(s)}rename(t,e,i){const[n,s]=r(i);this.promises.rename(t,e).then(n).catch(s)}stat(t,e,i){const[n,s]=r(e,i);this.promises.stat(t).then(n).catch(s)}lstat(t,e,i){const[n,s]=r(e,i);this.promises.lstat(t).then(n).catch(s)}readlink(t,e,i){const[n,s]=r(e,i);this.promises.readlink(t).then(n).catch(s)}symlink(t,e,i){const[n,s]=r(i);this.promises.symlink(t,e).then(n).catch(s)}backFile(t,e,i){const[n,s]=r(e,i);this.promises.backFile(t,e).then(n).catch(s)}du(t,e){const[i,n]=r(e);this.promises.du(t).then(i).catch(n)}}},function(t,e){t.exports=function(t){var e,i;if("function"!=typeof t)throw new Error("expected a function but got "+t);return function(){return e?i:(e=!0,i=t.apply(this,arguments))}}},function(t,e,i){const n=i(6),s=i(16),r=i(0);function o(t,e,...i){return void 0!==e&&"function"!=typeof e||(e={}),"string"==typeof e&&(e={encoding:e}),[t=r.normalize(t),e,...i]}function a(t,e,i,...n){return void 0!==i&&"function"!=typeof i||(i={}),"string"==typeof i&&(i={encoding:i}),[t=r.normalize(t),e,i,...n]}function h(t,e,...i){return[r.normalize(t),r.normalize(e),...i]}t.exports=class{constructor(t,e={}){this.init=this.init.bind(this),this.readFile=this._wrap(this.readFile,o,!1),this.writeFile=this._wrap(this.writeFile,a,!0),this.unlink=this._wrap(this.unlink,o,!0),this.readdir=this._wrap(this.readdir,o,!1),this.mkdir=this._wrap(this.mkdir,o,!0),this.rmdir=this._wrap(this.rmdir,o,!0),this.rename=this._wrap(this.rename,h,!0),this.stat=this._wrap(this.stat,o,!1),this.lstat=this._wrap(this.lstat,o,!1),this.readlink=this._wrap(this.readlink,o,!1),this.symlink=this._wrap(this.symlink,h,!0),this.backFile=this._wrap(this.backFile,o,!0),this.du=this._wrap(this.du,o,!1),this._deactivationPromise=null,this._deactivationTimeout=null,this._activationPromise=null,this._operations=new Set,t&&this.init(t,e)}async init(...t){return this._initPromiseResolve&&await this._initPromise,this._initPromise=this._init(...t),this._initPromise}async _init(t,e={}){await this._gracefulShutdown(),this._activationPromise&&await this._deactivate(),this._backend&&this._backend.destroy&&await this._backend.destroy(),this._backend=e.backend||new n,this._backend.init&&await this._backend.init(t,e),this._initPromiseResolve&&(this._initPromiseResolve(),this._initPromiseResolve=null),e.defer||this.stat("/")}async _gracefulShutdown(){this._operations.size>0&&(this._isShuttingDown=!0,await new Promise(t=>this._gracefulShutdownResolve=t),this._isShuttingDown=!1,this._gracefulShutdownResolve=null)}_wrap(t,e,i){return async(...n)=>{n=e(...n);let s={name:t.name,args:n};this._operations.add(s);try{return await this._activate(),await t.apply(this,n)}finally{this._operations.delete(s),i&&this._backend.saveSuperblock(),0===this._operations.size&&(this._deactivationTimeout||clearTimeout(this._deactivationTimeout),this._deactivationTimeout=setTimeout(this._deactivate.bind(this),500))}}}async _activate(){this._initPromise||console.warn(new Error(`Attempted to use LightningFS ${this._name} before it was initialized.`)),await this._initPromise,this._deactivationTimeout&&(clearTimeout(this._deactivationTimeout),this._deactivationTimeout=null),this._deactivationPromise&&await this._deactivationPromise,this._deactivationPromise=null,this._activationPromise||(this._activationPromise=this._backend.activate?this._backend.activate():Promise.resolve()),await this._activationPromise}async _deactivate(){return this._activationPromise&&await this._activationPromise,this._deactivationPromise||(this._deactivationPromise=this._backend.deactivate?this._backend.deactivate():Promise.resolve()),this._activationPromise=null,this._gracefulShutdownResolve&&this._gracefulShutdownResolve(),this._deactivationPromise}async readFile(t,e){return this._backend.readFile(t,e)}async writeFile(t,e,i){return await this._backend.writeFile(t,e,i),null}async unlink(t,e){return await this._backend.unlink(t,e),null}async readdir(t,e){return this._backend.readdir(t,e)}async mkdir(t,e){return await this._backend.mkdir(t,e),null}async rmdir(t,e){return await this._backend.rmdir(t,e),null}async rename(t,e){return await this._backend.rename(t,e),null}async stat(t,e){const i=await this._backend.stat(t,e);return new s(i)}async lstat(t,e){const i=await this._backend.lstat(t,e);return new s(i)}async readlink(t,e){return this._backend.readlink(t,e)}async symlink(t,e){return await this._backend.symlink(t,e),null}async backFile(t,e){return await this._backend.backFile(t,e),null}async du(t){return this._backend.du(t)}}},function(t,e,i){const{encode:n,decode:s}=i(7),r=i(10),o=i(11),{ENOENT:a,ENOTEMPTY:h,ETIMEDOUT:c}=i(1),l=i(12),u=i(13),d=i(14),_=i(15),p=i(0);t.exports=class{constructor(){this.saveSuperblock=r(()=>{this._saveSuperblock()},500)}async init(t,{wipe:e,url:i,urlauto:n,fileDbName:s=t,fileStoreName:r=t+"_files",lockDbName:a=t+"_lock",lockStoreName:h=t+"_lock"}={}){this._name=t,this._idb=new l(s,r),this._mutex=navigator.locks?new _(t):new d(a,h),this._cache=new o(t),this._opts={wipe:e,url:i},this._needsWipe=!!e,i&&(this._http=new u(i),this._urlauto=!!n)}async activate(){if(this._cache.activated)return;this._needsWipe&&(this._needsWipe=!1,await this._idb.wipe(),await this._mutex.release({force:!0})),await this._mutex.has()||await this._mutex.wait();const t=await this._idb.loadSuperblock();if(t)this._cache.activate(t);else if(this._http){const t=await this._http.loadSuperblock();this._cache.activate(t),await this._saveSuperblock()}else this._cache.activate();if(!await this._mutex.has())throw new c}async deactivate(){await this._mutex.has()&&await this._saveSuperblock(),this._cache.deactivate();try{await this._mutex.release()}catch(t){console.log(t)}await this._idb.close()}async _saveSuperblock(){this._cache.activated&&(this._lastSavedAt=Date.now(),await this._idb.saveSuperblock(this._cache._root))}_writeStat(t,e,i){let n=p.split(p.dirname(t)),s=n.shift();for(let t of n){s=p.join(s,t);try{this._cache.mkdir(s,{mode:511})}catch(t){}}return this._cache.writeStat(t,e,i)}async readFile(t,e){const{encoding:i}=e;if(i&&"utf8"!==i)throw new Error('Only "utf8" encoding is supported in readFile');let n=null,r=null;try{r=this._cache.stat(t),n=await this._idb.readFile(r.ino)}catch(t){if(!this._urlauto)throw t}if(!n&&this._http){let e=this._cache.lstat(t);for(;"symlink"===e.type;)t=p.resolve(p.dirname(t),e.target),e=this._cache.lstat(t);n=await this._http.readFile(t)}if(n&&(r&&r.size==n.byteLength||(r=await this._writeStat(t,n.byteLength,{mode:r?r.mode:438}),this.saveSuperblock()),"utf8"===i&&(n=s(n))),!r)throw new a(t);return n}async writeFile(t,e,i){const{mode:s,encoding:r="utf8"}=i;if("string"==typeof e){if("utf8"!==r)throw new Error('Only "utf8" encoding is supported in writeFile');e=n(e)}const o=await this._cache.writeStat(t,e.byteLength,{mode:s});await this._idb.writeFile(o.ino,e)}async unlink(t,e){const i=this._cache.lstat(t);this._cache.unlink(t),"symlink"!==i.type&&await this._idb.unlink(i.ino)}readdir(t,e){return this._cache.readdir(t)}mkdir(t,e){const{mode:i=511}=e;this._cache.mkdir(t,{mode:i})}rmdir(t,e){if("/"===t)throw new h;this._cache.rmdir(t)}rename(t,e){this._cache.rename(t,e)}stat(t,e){return this._cache.stat(t)}lstat(t,e){return this._cache.lstat(t)}readlink(t,e){return this._cache.readlink(t)}symlink(t,e){this._cache.symlink(t,e)}async backFile(t,e){let i=await this._http.sizeFile(t);await this._writeStat(t,i,e)}du(t){return this._cache.du(t)}}},function(t,e,i){i(8),t.exports={encode:t=>(new TextEncoder).encode(t),decode:t=>(new TextDecoder).decode(t)}},function(t,e,i){(function(t){!function(t){function e(t){if("utf-8"!==(t=void 0===t?"utf-8":t))throw new RangeError("Failed to construct 'TextEncoder': The encoding label provided ('"+t+"') is invalid.")}function i(t,e){if(e=void 0===e?{fatal:!1}:e,"utf-8"!==(t=void 0===t?"utf-8":t))throw new RangeError("Failed to construct 'TextDecoder': The encoding label provided ('"+t+"') is invalid.");if(e.fatal)throw Error("Failed to construct 'TextDecoder': the 'fatal' option is unsupported.")}if(t.TextEncoder&&t.TextDecoder)return!1;Object.defineProperty(e.prototype,"encoding",{value:"utf-8"}),e.prototype.encode=function(t,e){if((e=void 0===e?{stream:!1}:e).stream)throw Error("Failed to encode: the 'stream' option is unsupported.");e=0;for(var i=t.length,n=0,s=Math.max(32,i+(i>>1)+7),r=new Uint8Array(s>>3<<3);e=o){if(e=o)continue}if(n+4>r.length&&(s+=8,s=(s*=1+e/t.length*2)>>3<<3,(a=new Uint8Array(s)).set(r),r=a),0==(4294967168&o))r[n++]=o;else{if(0==(4294965248&o))r[n++]=o>>6&31|192;else if(0==(4294901760&o))r[n++]=o>>12&15|224,r[n++]=o>>6&63|128;else{if(0!=(4292870144&o))continue;r[n++]=o>>18&7|240,r[n++]=o>>12&63|128,r[n++]=o>>6&63|128}r[n++]=63&o|128}}return r.slice(0,n)},Object.defineProperty(i.prototype,"encoding",{value:"utf-8"}),Object.defineProperty(i.prototype,"fatal",{value:!1}),Object.defineProperty(i.prototype,"ignoreBOM",{value:!1}),i.prototype.decode=function(t,e){if((e=void 0===e?{stream:!1}:e).stream)throw Error("Failed to decode: the 'stream' option is unsupported.");e=0;for(var i=(t=new Uint8Array(t)).length,n=[];e>>10&1023|55296),s=56320|1023&s),n.push(s)}}return String.fromCharCode.apply(null,n)},t.TextEncoder=e,t.TextDecoder=i}("undefined"!=typeof window?window:void 0!==t?t:this)}).call(this,i(9))},function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i},function(t,e){t.exports=function(t,e,i){var n;return function(){if(!e)return t.apply(this,arguments);var s=this,r=arguments,o=i&&!n;return clearTimeout(n),n=setTimeout(function(){if(n=null,!o)return t.apply(s,r)},e),o?t.apply(this,arguments):void 0}}},function(t,e,i){const n=i(0),{EEXIST:s,ENOENT:r,ENOTDIR:o,ENOTEMPTY:a}=i(1),h=0;t.exports=class{constructor(){}_makeRoot(t=new Map){return t.set(h,{mode:511,type:"dir",size:0,ino:0,mtimeMs:Date.now()}),t}activate(t=null){this._root=null===t?new Map([["/",this._makeRoot()]]):"string"==typeof t?new Map([["/",this._makeRoot(this.parse(t))]]):t}get activated(){return!!this._root}deactivate(){this._root=void 0}size(){return this._countInodes(this._root.get("/"))-1}_countInodes(t){let e=1;for(let[i,n]of t)i!==h&&(e+=this._countInodes(n));return e}autoinc(){return this._maxInode(this._root.get("/"))+1}_maxInode(t){let e=t.get(h).ino;for(let[i,n]of t)i!==h&&(e=Math.max(e,this._maxInode(n)));return e}print(t=this._root.get("/")){let e="";const i=(t,n)=>{for(let[s,r]of t){if(0===s)continue;let t=r.get(h),o=t.mode.toString(8);e+=`${"\t".repeat(n)}${s}\t${o}`,"file"===t.type?e+=`\t${t.size}\t${t.mtimeMs}\n`:(e+="\n",i(r,n+1))}};return i(t,0),e}parse(t){let e=0;function i(t){const i=++e,n=1===t.length?"dir":"file";let[s,r,o]=t;return s=parseInt(s,8),r=r?parseInt(r):0,o=o?parseInt(o):Date.now(),new Map([[h,{mode:s,type:n,size:r,mtimeMs:o,ino:i}]])}let n=t.trim().split("\n"),s=this._makeRoot(),r=[{indent:-1,node:s},{indent:0,node:null}];for(let t of n){let e=t.match(/^\t*/)[0].length;t=t.slice(e);let[n,...s]=t.split("\t"),o=i(s);if(e<=r[r.length-1].indent)for(;e<=r[r.length-1].indent;)r.pop();r.push({indent:e,node:o}),r[r.length-2].node.set(n,o)}return s}_lookup(t,e=!0){let i=this._root,s="/",o=n.split(t);for(let a=0;a1)throw new a;let i=this._lookup(n.dirname(t)),s=n.basename(t);i.delete(s)}readdir(t){let e=this._lookup(t);if("dir"!==e.get(h).type)throw new o;return[...e.keys()].filter(t=>"string"==typeof t)}writeStat(t,e,{mode:i}){let s;try{let e=this.stat(t);null==i&&(i=e.mode),s=e.ino}catch(t){}null==i&&(i=438),null==s&&(s=this.autoinc());let r=this._lookup(n.dirname(t)),o=n.basename(t),a={mode:i,type:"file",size:e,mtimeMs:Date.now(),ino:s},c=new Map;return c.set(h,a),r.set(o,c),a}unlink(t){let e=this._lookup(n.dirname(t)),i=n.basename(t);e.delete(i)}rename(t,e){let i=n.basename(e),s=this._lookup(t);this._lookup(n.dirname(e)).set(i,s),this.unlink(t)}stat(t){return this._lookup(t).get(h)}lstat(t){return this._lookup(t,!1).get(h)}readlink(t){return this._lookup(t,!1).get(h).target}symlink(t,e){let i,s;try{let t=this.stat(e);null===s&&(s=t.mode),i=t.ino}catch(t){}null==s&&(s=40960),null==i&&(i=this.autoinc());let r=this._lookup(n.dirname(e)),o=n.basename(e),a={mode:s,type:"symlink",target:t,size:0,mtimeMs:Date.now(),ino:i},c=new Map;return c.set(h,a),r.set(o,c),a}_du(t){let e=0;for(const[i,n]of t.entries())e+=i===h?n.size:this._du(n);return e}du(t){let e=this._lookup(t);return this._du(e)}}},function(t,e,i){const n=i(2);t.exports=class{constructor(t,e){this._database=t,this._storename=e,this._store=new n.Store(this._database,this._storename)}saveSuperblock(t){return n.set("!root",t,this._store)}loadSuperblock(){return n.get("!root",this._store)}readFile(t){return n.get(t,this._store)}writeFile(t,e){return n.set(t,e,this._store)}unlink(t){return n.del(t,this._store)}wipe(){return n.clear(this._store)}close(){return n.close(this._store)}}},function(t,e){t.exports=class{constructor(t){this._url=t}loadSuperblock(){return fetch(this._url+"/.superblock.txt").then(t=>t.ok?t.text():null)}async readFile(t){const e=await fetch(this._url+t);if(200===e.status)return e.arrayBuffer();throw new Error("ENOENT")}async sizeFile(t){const e=await fetch(this._url+t,{method:"HEAD"});if(200===e.status)return e.headers.get("content-length");throw new Error("ENOENT")}}},function(t,e,i){const n=i(2),s=t=>new Promise(e=>setTimeout(e,t));t.exports=class{constructor(t,e){this._id=Math.random(),this._database=t,this._storename=e,this._store=new n.Store(this._database,this._storename),this._lock=null}async has({margin:t=2e3}={}){if(this._lock&&this._lock.holder===this._id){const e=Date.now();return this._lock.expires>e+t||await this.renew()}return!1}async renew({ttl:t=5e3}={}){let e;return await n.update("lock",i=>{const n=Date.now()+t;return e=i&&i.holder===this._id,this._lock=e?{holder:this._id,expires:n}:i,this._lock},this._store),e}async acquire({ttl:t=5e3}={}){let e,i,s;if(await n.update("lock",n=>{const r=Date.now(),o=r+t;return i=n&&n.expires(e=t||n&&n.holder===this._id,i=void 0===n,s=n&&n.holder!==this._id,this._lock=e?void 0:n,this._lock),this._store),await n.close(this._store),!e&&!t){if(i)throw new Error("Mutex double-freed");if(s)throw new Error("Mutex lost ownership")}return e}}},function(t,e){t.exports=class{constructor(t){this._id=Math.random(),this._database=t,this._has=!1,this._release=null}async has(){return this._has}async acquire(){return new Promise(t=>{navigator.locks.request(this._database+"_lock",{ifAvailable:!0},e=>(this._has=!!e,t(!!e),new Promise(t=>{this._release=t})))})}async wait({timeout:t=6e5}={}){return new Promise((e,i)=>{const n=new AbortController;setTimeout(()=>{n.abort(),i(new Error("Mutex timeout"))},t),navigator.locks.request(this._database+"_lock",{signal:n.signal},t=>(this._has=!!t,e(!!t),new Promise(t=>{this._release=t})))})}async release({force:t=!1}={}){this._has=!1,this._release?this._release():t&&navigator.locks.request(this._database+"_lock",{steal:!0},t=>!0)}}},function(t,e){t.exports=class{constructor(t){this.type=t.type,this.mode=t.mode,this.size=t.size,this.ino=t.ino,this.mtimeMs=t.mtimeMs,this.ctimeMs=t.ctimeMs||t.mtimeMs,this.uid=1,this.gid=1,this.dev=1}isFile(){return"file"===this.type}isDirectory(){return"dir"===this.type}isSymbolicLink(){return"symlink"===this.type}}}])}); \ No newline at end of file diff --git a/apps/remix-ide/src/assets/js/migrate.js b/apps/remix-ide/src/assets/js/migrate.js deleted file mode 100644 index 1de4cd0cb1..0000000000 --- a/apps/remix-ide/src/assets/js/migrate.js +++ /dev/null @@ -1,139 +0,0 @@ -// eslint-disable-next-line no-unused-vars -async function migrateFilesFromLocalStorage (cb) { - let testmigration = false // migration loads test data into localstorage with browserfs - // indexeddb will be empty by this point, so there is no danger but do a check for the origin to load test data so it runs only locally - testmigration = window.location.hash.includes('e2e_testmigration=true') && window.location.host === '127.0.0.1:8080' && window.location.protocol === 'http:' - // eslint-disable-next-line no-undef - BrowserFS.install(window) - // eslint-disable-next-line no-undef - BrowserFS.configure({ - fs: 'LocalStorage' - }, async function (e) { - if (e) console.log(e) - - const browserFS = window.require('fs') - - /** - * copy the folder recursively (internal use) - * @param {string} path is the folder to be copied over - * @param {Function} visitFile is a function called for each visited files - * @param {Function} visitFolder is a function called for each visited folders - */ - async function _copyFolderToJsonInternal (path, visitFile, visitFolder, fs) { - visitFile = visitFile || (() => { }) - visitFolder = visitFolder || (() => { }) - return new Promise((resolve, reject) => { - const json = {} - if (fs.existsSync(path)) { - try { - const items = fs.readdirSync(path) - visitFolder({ path }) - if (items.length !== 0) { - items.forEach(async (item, index) => { - const file = {} - const curPath = `${path}${path.endsWith('/') ? '' : '/'}${item}` - if (fs.statSync(curPath).isDirectory()) { - file.children = await _copyFolderToJsonInternal(curPath, visitFile, visitFolder, fs) - } else { - file.content = fs.readFileSync(curPath, 'utf8') - visitFile({ path: curPath, content: file.content }) - } - json[curPath] = file - }) - } - } catch (e) { - console.log(e) - return reject(e) - } - } - return resolve(json) - }) - } - - /** - * copy the folder recursively - * @param {string} path is the folder to be copied over - * @param {Function} visitFile is a function called for each visited files - * @param {Function} visitFolder is a function called for each visited folders - */ - async function copyFolderToJson (path, visitFile, visitFolder, fs) { - visitFile = visitFile || (() => { }) - visitFolder = visitFolder || (() => { }) - return _copyFolderToJsonInternal(path, visitFile, visitFolder, fs) - } - - const populateWorkspace = async (json, fs) => { - for (const item in json) { - const isFolder = json[item].content === undefined - if (isFolder) { - await createDir(item, fs) - await populateWorkspace(json[item].children, fs) - } else { - try { - await fs.writeFile(item, json[item].content, 'utf8') - } catch (error) { - console.log(error) - } - } - } - } - - const createDir = async (path, fs) => { - const paths = path.split('/') - if (paths.length && paths[0] === '') paths.shift() - let currentCheck = '' - for (const value of paths) { - currentCheck = currentCheck + (currentCheck ? '/' : '') + value - if (!await fs.exists(currentCheck)) { - try { - await fs.mkdir(currentCheck) - } catch (error) { - console.log(error) - } - } - } - } - // - if (testmigration) await populateWorkspace(testData, browserFS) - const files = await copyFolderToJson('/', null, null, browserFS) - await populateWorkspace(files, window.remixFileSystem) - // eslint-disable-next-line no-undef - if (cb) cb() - }) -} - -/* eslint-disable no-template-curly-in-string */ -const testData = { - '.workspaces': { - children: { - '.workspaces/default_workspace': { - children: { - '.workspaces/default_workspace/README.txt': { - content: 'TEST README' - } - } - }, - '.workspaces/workspace_test': { - children: { - '.workspaces/workspace_test/TEST_README.txt': { - content: 'TEST README' - }, - '.workspaces/workspace_test/test_contracts': { - children: { - '.workspaces/workspace_test/test_contracts/1_Storage.sol': { - content: 'testing' - }, - '.workspaces/workspace_test/test_contracts/artifacts': { - children: { - '.workspaces/workspace_test/test_contracts/artifacts/Storage_metadata.json': { - content: '{ "test": "data" }' - } - } - } - } - } - } - } - } - } -} diff --git a/apps/remix-ide/src/index.html b/apps/remix-ide/src/index.html index 3587ad1233..1fa68cd3b5 100644 --- a/apps/remix-ide/src/index.html +++ b/apps/remix-ide/src/index.html @@ -28,8 +28,6 @@ - - - -