Webpack for node server

This commit is contained in:
uonai 2020-07-03 10:32:36 -05:00
parent fa9f4984bd
commit 59e8312d81
9 changed files with 326 additions and 95 deletions

View File

@ -8,13 +8,16 @@
}
}
],
"@emotion/babel-preset-css-prop",
"@emotion/babel-preset-css-prop"
],
"plugins": [
["module-resolver", {
"alias": {
"~": "./"
[
"module-resolver",
{
"alias": {
"~": "./"
}
}
}]
]
]
}

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ DS_STORE
package-lock.json
node_modules
dist
nodedistribution
/**/*/package-lock.json
/**/*/.DS_STORE

188
distribution/main.js Normal file
View File

@ -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

1
distribution/main.js.map Normal file

File diff suppressed because one or more lines are too long

View File

@ -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

View File

@ -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 })
);
}

View File

@ -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"
}
}

116
server.js
View File

@ -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);
});

27
webpack.node.config.js Normal file
View File

@ -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()],
};