diff --git a/.babelrc b/.babelrc index bf9a6a9c..e1a02f8b 100644 --- a/.babelrc +++ b/.babelrc @@ -8,13 +8,16 @@ } } ], - "@emotion/babel-preset-css-prop", + "@emotion/babel-preset-css-prop" ], "plugins": [ - ["module-resolver", { - "alias": { - "~": "./" + [ + "module-resolver", + { + "alias": { + "~": "./" + } } - }] + ] ] } diff --git a/.gitignore b/.gitignore index b6086542..4f3f5300 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ DS_STORE package-lock.json node_modules dist +nodedistribution /**/*/package-lock.json /**/*/.DS_STORE diff --git a/distribution/main.js b/distribution/main.js new file mode 100644 index 00000000..12fa4721 --- /dev/null +++ b/distribution/main.js @@ -0,0 +1,188 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./electron/main.js"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./electron sync recursive": +/*!***********************!*\ + !*** ./electron sync ***! + \***********************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +function webpackEmptyContext(req) { + var e = new Error("Cannot find module '" + req + "'"); + e.code = 'MODULE_NOT_FOUND'; + throw e; +} +webpackEmptyContext.keys = function() { return []; }; +webpackEmptyContext.resolve = webpackEmptyContext; +module.exports = webpackEmptyContext; +webpackEmptyContext.id = "./electron sync recursive"; + +/***/ }), + +/***/ "./electron/main.js": +/*!**************************!*\ + !*** ./electron/main.js ***! + \**************************/ +/*! no static exports found */ +/***/ (function(module, exports, __webpack_require__) { + +/* WEBPACK VAR INJECTION */(function(__dirname) {// Modules to control application life and create native browser window +var _require = __webpack_require__(/*! electron */ "electron"), + app = _require.app, + BrowserWindow = _require.BrowserWindow; + +var path = __webpack_require__(/*! path */ "path"); // const { fork } = require("child_process"); +// const ps = fork(path.join(__dirname, "..", "index.js")); + + +function createWindow() { + // Create the browser window. + var mainWindow = new BrowserWindow({ + width: 800, + height: 600, + webPreferences: { + preload: path.join(__dirname, "preload.js"), + nodeIntegration: true + } + }); // Open the DevTools. + + mainWindow.webContents.openDevTools(); // and load the index.html of the app. + + mainWindow.loadURL("http://localhost:1337"); + console.log("window created"); + + __webpack_require__("./electron sync recursive")(path.join(__dirname, "../nodedistribution/", "main.js")); +} // This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. + + +app.whenReady().then(function () { + createWindow(); + app.on("activate", function () { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (BrowserWindow.getAllWindows().length === 0) createWindow(); + }); +}); // Quit when all windows are closed, except on macOS. There, it's common +// for applications and their menu bar to stay active until the user quits +// explicitly with Cmd + Q. + +app.on("window-all-closed", function () { + if (process.platform !== "darwin") app.quit(); +}); // In this file you can include the rest of your app's specific main process +// code. You can also put them in separate files and require them here. +/* WEBPACK VAR INJECTION */}.call(this, "/")) + +/***/ }), + +/***/ "electron": +/*!***************************!*\ + !*** external "electron" ***! + \***************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = require("electron"); + +/***/ }), + +/***/ "path": +/*!***********************!*\ + !*** external "path" ***! + \***********************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +module.exports = require("path"); + +/***/ }) + +/******/ }); +//# sourceMappingURL=main.js.map \ No newline at end of file diff --git a/distribution/main.js.map b/distribution/main.js.map new file mode 100644 index 00000000..849e75e8 --- /dev/null +++ b/distribution/main.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["webpack:///webpack/bootstrap","webpack:///./electron sync","webpack:///./electron/main.js","webpack:///external \"electron\"","webpack:///external \"path\""],"names":["require","app","BrowserWindow","path","createWindow","mainWindow","width","height","webPreferences","preload","join","__dirname","nodeIntegration","webContents","openDevTools","loadURL","console","log","whenReady","then","on","getAllWindows","length","process","platform","quit"],"mappings":";QAAA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;;QAEA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;;;QAGA;QACA;;QAEA;QACA;;QAEA;QACA;QACA;QACA,0CAA0C,gCAAgC;QAC1E;QACA;;QAEA;QACA;QACA;QACA,wDAAwD,kBAAkB;QAC1E;QACA,iDAAiD,cAAc;QAC/D;;QAEA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA;QACA,yCAAyC,iCAAiC;QAC1E,gHAAgH,mBAAmB,EAAE;QACrI;QACA;;QAEA;QACA;QACA;QACA,2BAA2B,0BAA0B,EAAE;QACvD,iCAAiC,eAAe;QAChD;QACA;QACA;;QAEA;QACA,sDAAsD,+DAA+D;;QAErH;QACA;;;QAGA;QACA;;;;;;;;;;;;AClFA;AACA;AACA;AACA;AACA;AACA,uCAAuC,WAAW;AAClD;AACA;AACA,qD;;;;;;;;;;;ACRA;eAC+BA,mBAAO,CAAC,0BAAD,C;IAA9BC,G,YAAAA,G;IAAKC,a,YAAAA,a;;AACb,IAAMC,IAAI,GAAGH,mBAAO,CAAC,kBAAD,CAApB,C,CAEA;AACA;;;AAEA,SAASI,YAAT,GAAwB;AACtB;AACA,MAAMC,UAAU,GAAG,IAAIH,aAAJ,CAAkB;AACnCI,SAAK,EAAE,GAD4B;AAEnCC,UAAM,EAAE,GAF2B;AAGnCC,kBAAc,EAAE;AACdC,aAAO,EAAEN,IAAI,CAACO,IAAL,CAAUC,SAAV,EAAqB,YAArB,CADK;AAEdC,qBAAe,EAAE;AAFH;AAHmB,GAAlB,CAAnB,CAFsB,CAUtB;;AACAP,YAAU,CAACQ,WAAX,CAAuBC,YAAvB,GAXsB,CAYtB;;AACAT,YAAU,CAACU,OAAX,CAAmB,uBAAnB;AACAC,SAAO,CAACC,GAAR,CAAY,gBAAZ;;AACAjB,mDAAQG,IAAI,CAACO,IAAL,CAAUC,SAAV,EAAqB,sBAArB,EAA6C,SAA7C,CAAD,CAAP;AACD,C,CAED;AACA;AACA;;;AACAV,GAAG,CAACiB,SAAJ,GAAgBC,IAAhB,CAAqB,YAAM;AACzBf,cAAY;AAEZH,KAAG,CAACmB,EAAJ,CAAO,UAAP,EAAmB,YAAY;AAC7B;AACA;AACA,QAAIlB,aAAa,CAACmB,aAAd,GAA8BC,MAA9B,KAAyC,CAA7C,EAAgDlB,YAAY;AAC7D,GAJD;AAKD,CARD,E,CAUA;AACA;AACA;;AACAH,GAAG,CAACmB,EAAJ,CAAO,mBAAP,EAA4B,YAAY;AACtC,MAAIG,OAAO,CAACC,QAAR,KAAqB,QAAzB,EAAmCvB,GAAG,CAACwB,IAAJ;AACpC,CAFD,E,CAIA;AACA,uE;;;;;;;;;;;;AC9CA,qC;;;;;;;;;;;ACAA,iC","file":"main.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = \"./electron/main.js\");\n","function webpackEmptyContext(req) {\n\tvar e = new Error(\"Cannot find module '\" + req + \"'\");\n\te.code = 'MODULE_NOT_FOUND';\n\tthrow e;\n}\nwebpackEmptyContext.keys = function() { return []; };\nwebpackEmptyContext.resolve = webpackEmptyContext;\nmodule.exports = webpackEmptyContext;\nwebpackEmptyContext.id = \"./electron sync recursive\";","// Modules to control application life and create native browser window\nconst { app, BrowserWindow } = require(\"electron\");\nconst path = require(\"path\");\n\n// const { fork } = require(\"child_process\");\n// const ps = fork(path.join(__dirname, \"..\", \"index.js\"));\n\nfunction createWindow() {\n // Create the browser window.\n const mainWindow = new BrowserWindow({\n width: 800,\n height: 600,\n webPreferences: {\n preload: path.join(__dirname, \"preload.js\"),\n nodeIntegration: true,\n },\n });\n // Open the DevTools.\n mainWindow.webContents.openDevTools();\n // and load the index.html of the app.\n mainWindow.loadURL(\"http://localhost:1337\");\n console.log(\"window created\");\n require(path.join(__dirname, \"../nodedistribution/\", \"main.js\"));\n}\n\n// This method will be called when Electron has finished\n// initialization and is ready to create browser windows.\n// Some APIs can only be used after this event occurs.\napp.whenReady().then(() => {\n createWindow();\n\n app.on(\"activate\", function () {\n // On macOS it's common to re-create a window in the app when the\n // dock icon is clicked and there are no other windows open.\n if (BrowserWindow.getAllWindows().length === 0) createWindow();\n });\n});\n\n// Quit when all windows are closed, except on macOS. There, it's common\n// for applications and their menu bar to stay active until the user quits\n// explicitly with Cmd + Q.\napp.on(\"window-all-closed\", function () {\n if (process.platform !== \"darwin\") app.quit();\n});\n\n// In this file you can include the rest of your app's specific main process\n// code. You can also put them in separate files and require them here.\n","module.exports = require(\"electron\");","module.exports = require(\"path\");"],"sourceRoot":""} \ No newline at end of file diff --git a/electron/main.js b/electron/main.js index 589701f2..a8d373fb 100644 --- a/electron/main.js +++ b/electron/main.js @@ -2,8 +2,8 @@ const { app, BrowserWindow } = require("electron"); const path = require("path"); -const { fork } = require("child_process"); -const ps = fork(path.join(__dirname, "..", "index.js")); +// const { fork } = require("child_process"); +// const ps = fork(path.join(__dirname, "..", "index.js")); function createWindow() { // Create the browser window. @@ -12,14 +12,15 @@ function createWindow() { height: 600, webPreferences: { preload: path.join(__dirname, "preload.js"), + nodeIntegration: true, }, }); + // Open the DevTools. + mainWindow.webContents.openDevTools(); // and load the index.html of the app. mainWindow.loadURL("http://localhost:1337"); console.log("window created"); - - // Open the DevTools. - mainWindow.webContents.openDevTools(); + require(path.join(__dirname, "../nodedistribution/", "main.js")); } // This method will be called when Electron has finished diff --git a/node_common/utilities.js b/node_common/utilities.js index e3fe05ed..7c260287 100644 --- a/node_common/utilities.js +++ b/node_common/utilities.js @@ -1,30 +1,30 @@ -import * as Constants from "~/node_common/constants"; +import * as Constants from '../node_common/constants'; -import FS from "fs-extra"; +import FS from 'fs-extra'; export const resetFileSystem = async () => { - console.log("[ prototype ] deleting old token and library data "); + console.log('[ prototype ] deleting old token and library data '); if (FS.existsSync(`./.data`)) { - FS.removeSync("./.data", { recursive: true }); + FS.removeSync('./.data', { recursive: true }); } - console.log("[ prototype ] deleting old avatar data "); + console.log('[ prototype ] deleting old avatar data '); if (FS.existsSync(Constants.AVATAR_STORAGE_URL)) { FS.removeSync(Constants.AVATAR_STORAGE_URL, { recursive: true }); } - console.log("[ prototype ] deleting old file data "); + console.log('[ prototype ] deleting old file data '); if (FS.existsSync(Constants.FILE_STORAGE_URL)) { FS.removeSync(Constants.FILE_STORAGE_URL, { recursive: true }); } - console.log("[ prototype ] creating new avatar folder "); + console.log('[ prototype ] creating new avatar folder '); FS.mkdirSync(Constants.AVATAR_STORAGE_URL, { recursive: true }); - FS.writeFileSync(`${Constants.AVATAR_STORAGE_URL}.gitkeep`, ""); + FS.writeFileSync(`${Constants.AVATAR_STORAGE_URL}.gitkeep`, ''); - console.log("[ prototype ] creating new local file folder "); + console.log('[ prototype ] creating new local file folder '); FS.mkdirSync(Constants.FILE_STORAGE_URL, { recursive: true }); - FS.writeFileSync(`${Constants.FILE_STORAGE_URL}.gitkeep`, ""); + FS.writeFileSync(`${Constants.FILE_STORAGE_URL}.gitkeep`, ''); return true; }; @@ -66,7 +66,7 @@ export const emitState = async ({ state, client, PG }) => { }); if (client) { - client.send(JSON.stringify({ action: "UPDATE_VIEWER", data })); + client.send(JSON.stringify({ action: 'UPDATE_VIEWER', data })); } return data; @@ -74,18 +74,18 @@ export const emitState = async ({ state, client, PG }) => { export const getFileName = (s) => { let target = s; - if (target.endsWith("/")) { + if (target.endsWith('/')) { target = target.substring(0, target.length - 1); } - return target.substr(target.lastIndexOf("/") + 1); + return target.substr(target.lastIndexOf('/') + 1); }; export const createFile = ({ id, data }) => { return { - decorator: "FILE", + decorator: 'FILE', id: id, - icon: "PNG", + icon: 'PNG', file: getFileName(id), miner: null, job_id: null, @@ -103,10 +103,10 @@ export const createFile = ({ id, data }) => { export const createFolder = ({ id }) => { return { - decorator: "FOLDER", + decorator: 'FOLDER', id, folderId: id, - icon: "FOLDER", + icon: 'FOLDER', file: getFileName(id), name: getFileName(id), pageTitle: null, @@ -131,7 +131,7 @@ export const refreshLibrary = async ({ state, PG, FFS }) => { if (state.library[i].children[j].job_id) { if (state.library[i].children[j].storage_status === 1) { console.log( - "[ prototype ] update file", + '[ prototype ] update file', state.library[i].children[j] ); state.library[i].children[j].storage_status = 2; @@ -140,11 +140,10 @@ export const refreshLibrary = async ({ state, PG, FFS }) => { } PG.ffs.watchJobs((job) => { - // NOTE(jim): FFS is undefined? - console.log("[ prototype ] job status", job.status); - if (job.status >= 5) { + console.log('[ prototype ] job status', job.status); + if (job.status === FFS.JobStatus.JOB_STATUS_SUCCESS) { console.log( - "[ prototype ] update file", + '[ prototype ] update file', state.library[i].children[j] ); state.library[i].children[j].storage_status = 6; @@ -157,7 +156,7 @@ export const refreshLibrary = async ({ state, PG, FFS }) => { if (write) { FS.writeFileSync( - "./.data/library.json", + './.data/library.json', JSON.stringify({ library: state.library }) ); } diff --git a/package.json b/package.json index 9eee152e..c0a9ea17 100644 --- a/package.json +++ b/package.json @@ -6,12 +6,14 @@ "engines": { "node": ">=11 <12" }, - "main": "electron/main.js", + "main": "./electron/main.js", "scripts": { "dev": "node . --unhandled-rejections=strict", + "dev:node": "NODE_ENV=development webpack --config webpack.node.config.js --mode development", "build": "next build", - "start": "NODE_ENV=production node . --unhandled-rejections=strict", - "electron": "electron .", + "start": "electron ./electron/main.js", + "electron": "electron ./electron/main.js ", + "electronPack": "electron-builder --dir", "electronDist": "electron-builder" }, "build": { @@ -53,7 +55,16 @@ "ws": "^7.3.0" }, "devDependencies": { + "@rollup/plugin-node-resolve": "^8.1.0", + "babel-loader": "^8.1.0", "electron": "^9.0.5", - "electron-builder": "^22.7.0" + "electron-builder": "^22.7.0", + "rollup": "^2.18.1", + "rollup-plugin-babel": "^4.4.0", + "rollup-plugin-commonjs": "^10.1.0", + "rollup-plugin-terser": "^6.1.0", + "webpack": "^4.43.0", + "webpack-cli": "^3.3.12", + "webpack-node-externals": "^1.7.2" } } diff --git a/server.js b/server.js index 418d25ad..fab56191 100644 --- a/server.js +++ b/server.js @@ -1,28 +1,28 @@ -import * as Middleware from "~/common/middleware"; -import * as Strings from "~/common/strings"; -import * as Utilities from "~/node_common/utilities"; -import * as Constants from "~/node_common/constants"; +import * as Middleware from './common/middleware'; +import * as Strings from './common/strings'; +import * as Utilities from './node_common/utilities'; +import * as Constants from './node_common/constants'; -import { createPow, ffs } from "@textile/powergate-client"; +import { createPow, ffs } from '@textile/powergate-client'; // NOTE(jim): // https://github.com/textileio/js-powergate-client const PowerGate = createPow({ host: Constants.POWERGATE_HOST }); -import FS from "fs-extra"; -import WebSocketServer from "ws"; -import express from "express"; -import formidable from "formidable"; -import next from "next"; -import bodyParser from "body-parser"; -import compression from "compression"; -import { v4 as uuid } from "uuid"; +import FS from 'fs-extra'; +import WebSocketServer from 'ws'; +import express from 'express'; +import formidable from 'formidable'; +import next from 'next'; +import bodyParser from 'body-parser'; +import compression from 'compression'; +import { v4 as uuid } from 'uuid'; // TODO(jim): Support multiple desktop applications. let client = null; let state = null; -const production = process.env.NODE_ENV === "production"; +const production = process.env.NODE_ENV === 'production'; const port = process.env.PORT || 1337; const wsPort = process.env.WS_PORT || 2448; const resetData = process.env.npm_config_reset_data; @@ -32,14 +32,14 @@ const nextRequestHandler = app.getRequestHandler(); const setIntervalViewerUpdatesUnsafe = async () => { if (client) { try { - console.log("[ prototype ] polling: new viewer state."); + console.log('[ prototype ] polling: new viewer state.'); state = await Utilities.emitState({ state, client, PG: PowerGate, }); - console.log("[ prototype ] polling: new library state."); + console.log('[ prototype ] polling: new library state.'); state = await Utilities.refreshLibrary({ state, PG: PowerGate, @@ -54,7 +54,7 @@ const setIntervalViewerUpdatesUnsafe = async () => { }; app.prepare().then(async () => { - console.log("[ prototype ] initializing "); + console.log('[ prototype ] initializing '); state = { production, @@ -79,7 +79,7 @@ app.prepare().then(async () => { const updates = await Utilities.refresh({ PG: PowerGate }); state = await Utilities.updateStateData(state, updates); - console.log("[ prototype ] updated without token"); + console.log('[ prototype ] updated without token'); // NOTE(jim): This is a configuration folder with all of the client tokens. // TODO(jim): Unnecessary if we use a local and remote postgres. @@ -89,20 +89,20 @@ app.prepare().then(async () => { // NOTE(jim): This will create a token for authentication with powergate. // TODO(jim): Roll this up into Postgres instead. - if (!FS.existsSync("./.data/powergate-token")) { + if (!FS.existsSync('./.data/powergate-token')) { const FFS = await PowerGate.ffs.create(); state.token = FFS.token ? FFS.token : null; // NOTE(jim): Write a new token file. if (state.token) { - FS.writeFileSync("./.data/powergate-token", state.token); + FS.writeFileSync('./.data/powergate-token', state.token); } } else { - state.token = FS.readFileSync("./.data/powergate-token", "utf8"); + state.token = FS.readFileSync('./.data/powergate-token', 'utf8'); } if (state.token) { - console.log("[ prototype ] powergate token:", state.token); + console.log('[ prototype ] powergate token:', state.token); PowerGate.setToken(state.token); } @@ -110,32 +110,32 @@ app.prepare().then(async () => { PG: PowerGate, }); state = await Utilities.updateStateData(state, tokenUpdates); - console.log("[ prototype ] updated with token"); + console.log('[ prototype ] updated with token'); // NOTE(jim): Local library retrieval or creation // TODO(jim): Needs to support nested folders in the future. // TODO(jim): May consider a move to buckets. - if (!FS.existsSync("./.data/library.json")) { + if (!FS.existsSync('./.data/library.json')) { const librarySchema = { library: [ { ...Utilities.createFolder({ id: Constants.FILE_STORAGE_URL }), - file: "Files", - name: "Files", + file: 'Files', + name: 'Files', }, ], }; - FS.writeFileSync("./.data/library.json", JSON.stringify(librarySchema)); + FS.writeFileSync('./.data/library.json', JSON.stringify(librarySchema)); state.library = librarySchema.library; } else { - const parsedLibrary = FS.readFileSync("./.data/library.json", "utf8"); + const parsedLibrary = FS.readFileSync('./.data/library.json', 'utf8'); state.library = JSON.parse(parsedLibrary).library; } // NOTE(jim): Local settings retrieval or creation // TODO(jim): Move this to postgres later. - if (!FS.existsSync("./.data/local-settings.json")) { + if (!FS.existsSync('./.data/local-settings.json')) { const localSettingsSchema = { local: { photo: null, @@ -145,14 +145,14 @@ app.prepare().then(async () => { }; FS.writeFileSync( - "./.data/local-settings.json", + './.data/local-settings.json', JSON.stringify(localSettingsSchema) ); state.local = localSettingsSchema.local; } else { const parsedLocal = FS.readFileSync( - "./.data/local-settings.json", - "utf8" + './data/local-settings.json', + 'utf8' ); state.local = JSON.parse(parsedLocal).local; } @@ -160,11 +160,11 @@ app.prepare().then(async () => { console.log(e); console.log('[ prototype ] "/" -- WILL REDIRECT TO /SYSTEM '); console.log( - "[ prototype ] SLATE WILL NOT RUN LOCALLY UNTIL YOU HAVE " + '[ prototype ] SLATE WILL NOT RUN LOCALLY UNTIL YOU HAVE ' ); - console.log("[ prototype ] PROPERLY CONFIGURED POWERGATE AND "); + console.log('[ prototype ] PROPERLY CONFIGURED POWERGATE AND '); console.log( - "[ prototype ] CONNECTED TO THE FILECOIN NETWORK (DEVNET/TESTNET) " + '[ prototype ] CONNECTED TO THE FILECOIN NETWORK (DEVNET/TESTNET) ' ); } } @@ -172,15 +172,15 @@ app.prepare().then(async () => { const server = express(); const WSS = new WebSocketServer.Server({ port: wsPort }); - WSS.on("connection", (s) => { + WSS.on('connection', (s) => { // TODO(jim): Suppport more than one client. client = s; - s.on("close", function () { - s.send(JSON.stringify({ action: null, data: "closed" })); + s.on('close', function () { + s.send(JSON.stringify({ action: null, data: 'closed' })); }); - s.send(JSON.stringify({ action: null, data: "connected" })); + s.send(JSON.stringify({ action: null, data: 'connected' })); }); if (production) { @@ -188,7 +188,7 @@ app.prepare().then(async () => { } server.use(Middleware.CORS); - server.use("/public", express.static("public")); + server.use('/public', express.static('public')); server.use(bodyParser.json()); server.use( bodyParser.urlencoded({ @@ -196,7 +196,7 @@ app.prepare().then(async () => { }) ); - server.post("/_/viewer", async (req, res) => { + server.post('/_/viewer', async (req, res) => { let data = state; if (!production) { @@ -213,7 +213,7 @@ app.prepare().then(async () => { return res.status(200).send({ success: true, data }); }); - server.post("/_/deals/storage", async (req, res) => { + server.post('/_/deals/storage', async (req, res) => { if (Strings.isEmpty(req.body.src)) { return res.status(500).send({ success: false }); } @@ -239,7 +239,7 @@ app.prepare().then(async () => { // NOTE(jim): Writes the updated deal state. if (write) { FS.writeFileSync( - "./.data/library.json", + './.data/library.json', JSON.stringify({ library: state.library }) ); } @@ -248,7 +248,7 @@ app.prepare().then(async () => { return res.status(200).send({ success: true, cid, jobId }); }); - server.post("/_/storage/:file", async (req, res) => { + server.post('/_/storage/:file', async (req, res) => { const form = formidable({ multiples: true, uploadDir: Constants.FILE_STORAGE_URL, @@ -260,10 +260,10 @@ app.prepare().then(async () => { } else { // TODO(jim): Need to support other file types. if (!files.image) { - console.error("[ prototype ] File type unspported", files); + console.error('[ prototype ] File type unspported', files); return res .status(500) - .send({ error: "File type unsupported", files }); + .send({ error: 'File type unsupported', files }); } const newPath = form.uploadDir + req.params.file; @@ -287,7 +287,7 @@ app.prepare().then(async () => { // NOTE(jim): Writes the added file. if (pushed) { FS.writeFileSync( - "./.data/library.json", + './.data/library.json', JSON.stringify({ library: state.library }) ); } @@ -303,7 +303,7 @@ app.prepare().then(async () => { }); }); - server.post("/_/upload/avatar", async (req, res) => { + server.post('/_/upload/avatar', async (req, res) => { const form = formidable({ multiples: true, uploadDir: Constants.AVATAR_STORAGE_URL, @@ -320,7 +320,7 @@ app.prepare().then(async () => { // NOTE(jim): updates avatar photo. state.local.photo = `/static/system/${newName}`; FS.writeFileSync( - "./.data/local-settings.json", + './.data/local-settings.json', JSON.stringify({ local: { ...state.local } }) ); @@ -335,7 +335,7 @@ app.prepare().then(async () => { }); }); - server.post("/_/settings", async (req, res) => { + server.post('/_/settings', async (req, res) => { let data; try { data = await PowerGate.ffs.setDefaultConfig(req.body.config); @@ -347,18 +347,18 @@ app.prepare().then(async () => { return res.status(200).send({ success: true, data }); }); - server.post("/_/local-settings", async (req, res) => { + server.post('/_/local-settings', async (req, res) => { state.local = { ...state.local, ...req.body.local }; FS.writeFileSync( - "./.data/local-settings.json", + './.data/local-settings.json', JSON.stringify({ local: { ...state.local } }) ); state = await Utilities.emitState({ state, client, PG: PowerGate }); return res.status(200).send({ success: true }); }); - server.post("/_/wallet/create", async (req, res) => { + server.post('/_/wallet/create', async (req, res) => { let data; try { data = await PowerGate.ffs.newAddr( @@ -374,7 +374,7 @@ app.prepare().then(async () => { return res.status(200).send({ success: true, data }); }); - server.post("/_/wallet/send", async (req, res) => { + server.post('/_/wallet/send', async (req, res) => { let data; try { data = await PowerGate.ffs.sendFil( @@ -392,18 +392,18 @@ app.prepare().then(async () => { .send({ success: true, data: { ...data, ...req.body } }); }); - server.get("/", async (req, res) => { + server.get('/', async (req, res) => { if (!state.token) { - return res.redirect("/system"); + return res.redirect('/system'); } - return app.render(req, res, "/", { + return app.render(req, res, '/', { production, wsPort, }); }); - server.get("*", async (req, res) => { + server.get('*', async (req, res) => { return nextRequestHandler(req, res, req.url); }); diff --git a/webpack.node.config.js b/webpack.node.config.js new file mode 100644 index 00000000..aced580f --- /dev/null +++ b/webpack.node.config.js @@ -0,0 +1,27 @@ +const path = require("path"); +const nodeExternals = require("webpack-node-externals"); + +module.exports = { + resolve: { + extensions: [".js"], + }, + entry: "./index.js", + target: "node", + module: { + rules: [ + { + test: /\.(js)$/, + exclude: /node_modules/, + use: { + loader: "babel-loader", + }, + }, + ], + }, + output: { + libraryTarget: "commonjs", + path: path.resolve(__dirname, "./nodedistribution"), + filename: "[name].js", + }, + externals: [nodeExternals()], +};