Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: phNode, node inspector, phoenix node fs integration #1226

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,11 @@ module.exports = {
"blockBindings": true,
"classes": true
}
}
},
"overrides": [{
"files": ["src-node/**/*.js", "src-node/*.js"],
"env": {
"node": true
}
}]
};
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@floating-ui/dom": "^0.5.4",
"@fortawesome/fontawesome-free": "^6.1.2",
"@highlightjs/cdn-assets": "^11.5.1",
"@phcode/fs": "^2.0.5",
"@phcode/fs": "^2.0.6",
"@pixelbrackets/gfm-stylesheet": "^1.1.0",
"@prettier/plugin-php": "0.18.9",
"bootstrap": "^5.1.3",
Expand Down
142 changes: 141 additions & 1 deletion src-node/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,141 @@
console.log("hello world");
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2014 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/

const readline = require('readline');
const http = require('http');
const net = require('net');
const PhoenixFS = require('@phcode/fs/dist/phoenix-fs');

function randomNonce(byteLength) {
const randomBuffer = new Uint8Array(byteLength);
crypto.getRandomValues(randomBuffer);

// Define the character set for the random string
const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';

// Convert the ArrayBuffer to a case-sensitive random string with numbers
let randomId = '';
Array.from(randomBuffer).forEach(byte => {
randomId += charset[byte % charset.length];
});

return randomId;
}

const COMMAND_RESPONSE_PREFIX = 'phnodeResp:';
// Generate a random 64-bit url. This should take 100 million+ of years to crack with current http connection speed.
const PHOENIX_FS_URL = `/PhoenixFS${randomNonce(8)}`;
const PHOENIX_NODE_URL = `/PhoenixNode${randomNonce(8)}`;

const savedConsoleLog = console.log;

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

let serverPortResolve;
const serverPortPromise = new Promise((resolve) => { serverPortResolve = resolve; });

let orphanExitTimer = setTimeout(()=>{
process.exit(1);
}, 60000);

function _sendResponse(responseMessage, commandID) {
savedConsoleLog(COMMAND_RESPONSE_PREFIX + JSON.stringify({
message: responseMessage,
commandID
}) + "\n");
}

function processCommand(line) {
try{
let jsonCmd = JSON.parse(line);
switch (jsonCmd.commandCode) {
case "terminate": process.exit(0); return;
case "heartBeat":
clearTimeout(orphanExitTimer);
orphanExitTimer = setTimeout(()=>{
process.exit(1);
}, 60000);
return;
case "ping": _sendResponse("pong", jsonCmd.commandID); return;
case "setDebugMode":
if(jsonCmd.commandData) {
console.log = savedConsoleLog;
} else {
console.log = function () {}; // swallow logs
}
_sendResponse("done", jsonCmd.commandID); return;
case "setPhoenixFSDebugMode":
PhoenixFS.setDebugMode(jsonCmd.commandData);
_sendResponse("done", jsonCmd.commandID); return;
case "getEndpoints":
serverPortPromise.then(port =>{
_sendResponse({
port,
phoenixFSURL: `ws://localhost:${port}${PHOENIX_FS_URL}`,
phoenixNodeURL: `ws://localhost:${port}${PHOENIX_NODE_URL}`
}, jsonCmd.commandID);
});
return;
default: console.error("unknown command: "+ line);
}
} catch (e) {
console.error(e);
}
}

rl.on('line', (line) => {
processCommand(line);
});

function getFreePort() {
return new Promise((resolve)=>{
const server = net.createServer();

server.listen(0, () => {
const port = server.address().port;
server.close(() => {
resolve(port);
});
});
});
}

// Create an HTTP server
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('WebSocket server running');
});

getFreePort().then((port) => {
console.log('Server Opened on port: ', port);

PhoenixFS.CreatePhoenixFsServer(server, PHOENIX_FS_URL);
// Start the HTTP server on port 3000
server.listen(port, () => {
serverPortResolve(port);
console.log(`Server running on http://localhost:${port}`);
console.log(`Phoenix node tauri FS url is ws://localhost:${port}${PHOENIX_FS_URL}`);
});

});
8 changes: 4 additions & 4 deletions src-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"IMPORTANT!!": "Adding things here will bloat up the package size",
"dependencies": {
"@phcode/fs": "^2.0.5",
"@phcode/fs": "^2.0.6",
"npm": "10.1.0"
}
}
2 changes: 2 additions & 0 deletions src/document/DocumentCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,7 @@ define(function (require, exports, module) {

// Defer for a more successful reload - issue #11539
window.setTimeout(function () {
window.PhNodeEngine && window.PhNodeEngine.terminateNode();
window.location.href = href;
}, 1000);
}).fail(function () {
Expand Down Expand Up @@ -1900,6 +1901,7 @@ define(function (require, exports, module) {
event.preventDefault();
_handleWindowGoingAway(null, closeSuccess=>{
console.log('close success: ', closeSuccess);
window.PhNodeEngine.terminateNode();
Phoenix.app.closeWindow();
}, closeFail=>{
console.log('close fail: ', closeFail);
Expand Down
13 changes: 13 additions & 0 deletions src/extensions/default/DebugCommands/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ define(function (require, exports, module) {
DEBUG_RELOAD_WITHOUT_USER_EXTS = "debug.reloadWithoutUserExts",
DEBUG_SWITCH_LANGUAGE = "debug.switchLanguage",
DEBUG_ENABLE_LOGGING = "debug.enableLogging",
DEBUG_ENABLE_PHNODE_INSPECTOR = "debug.enablePhNodeInspector",
DEBUG_LIVE_PREVIEW_LOGGING = "debug.livePreviewLogging",
DEBUG_OPEN_VFS = "debug.openVFS",
DEBUG_OPEN_EXTENSION_FOLDER = "debug.openExtensionFolders",
Expand Down Expand Up @@ -688,13 +689,19 @@ define(function (require, exports, module) {
CommandManager.get(DEBUG_LIVE_PREVIEW_LOGGING).setEnabled(isLogging);
logger.loggingOptions.logLivePreview = window.isLoggingEnabled(LOG_LIVE_PREVIEW_KEY);
CommandManager.get(DEBUG_LIVE_PREVIEW_LOGGING).setChecked(logger.loggingOptions.logLivePreview);
CommandManager.get(DEBUG_ENABLE_PHNODE_INSPECTOR).setChecked(window.PhNodeEngine && window.PhNodeEngine.isInspectEnabled());
}

function _handleLogging() {
window.toggleLoggingKey(LOG_TO_CONSOLE_KEY);
_updateLogToConsoleMenuItemChecked();
}

function _handlePhNodeInspectEnable() {
window.PhNodeEngine.setInspectEnabled(!window.PhNodeEngine.isInspectEnabled());
_updateLogToConsoleMenuItemChecked();
}

function _handleLivePreviewLogging() {
window.toggleLoggingKey(LOG_LIVE_PREVIEW_KEY);
_updateLogToConsoleMenuItemChecked();
Expand Down Expand Up @@ -749,6 +756,7 @@ define(function (require, exports, module) {
CommandManager.register(switchLanguageStr, DEBUG_SWITCH_LANGUAGE, handleSwitchLanguage);

CommandManager.register(Strings.CMD_ENABLE_LOGGING, DEBUG_ENABLE_LOGGING, _handleLogging);
CommandManager.register(Strings.CMD_ENABLE_PHNODE_INSPECTOR, DEBUG_ENABLE_PHNODE_INSPECTOR, _handlePhNodeInspectEnable);
CommandManager.register(Strings.CMD_ENABLE_LIVE_PREVIEW_LOGS, DEBUG_LIVE_PREVIEW_LOGGING, _handleLivePreviewLogging);
CommandManager.register(Strings.CMD_OPEN_VFS, DEBUG_OPEN_VFS, _openVFS);
CommandManager.register(Strings.CMD_OPEN_EXTENSIONS_FOLDER, DEBUG_OPEN_EXTENSION_FOLDER, _openExtensionsFolder);
Expand All @@ -771,6 +779,9 @@ define(function (require, exports, module) {
debugMenu.addMenuItem(DEBUG_SHOW_PERF_DATA);
debugMenu.addMenuDivider();
debugMenu.addMenuItem(DEBUG_ENABLE_LOGGING);
debugMenu.addMenuItem(DEBUG_ENABLE_PHNODE_INSPECTOR, undefined, undefined, undefined, {
hideWhenCommandDisabled: true
});
debugMenu.addMenuItem(DEBUG_LIVE_PREVIEW_LOGGING);
debugMenu.addMenuDivider();
debugMenu.addMenuItem(DEBUG_OPEN_VFS);
Expand All @@ -785,6 +796,8 @@ define(function (require, exports, module) {
.setEnabled(extensionDevelopment.isProjectLoadedAsExtension());
CommandManager.get(DEBUG_OPEN_EXTENSION_FOLDER)
.setEnabled(Phoenix.browser.isTauri); // only show in tauri
CommandManager.get(DEBUG_ENABLE_PHNODE_INSPECTOR)
.setEnabled(Phoenix.browser.isTauri); // only show in tauri
CommandManager.get(DEBUG_OPEN_VIRTUAL_SERVER)
.setEnabled(!Phoenix.browser.isTauri); // don't show in tauri as there is no virtual server in tauri

Expand Down
2 changes: 1 addition & 1 deletion src/filesystem/impls/appshell/AppshellFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ define(function (require, exports, module) {
path = _normalise_path(path);
if (typeof mode === "function") {
callback = mode;
mode = parseInt("0755", 8);
mode = 0o755;
}
appshell.fs.mkdirs(path, mode, true, function (err) {
if (err) {
Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@
<script src="phoenix/virtualfs.js" defer></script>
<script src="phoenix/shell.js" type="module" defer></script>
<script src="phoenix/virtual-server-loader.js" type="module" defer></script>
<script src="node-loader.js" defer></script>
<!-- end javascript module scripts-->

<!-- CSS/LESS -->
Expand Down
1 change: 1 addition & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ define({
"CMD_RUN_UNIT_TESTS": "Run Tests",
"CMD_SHOW_PERF_DATA": "Show Performance Data",
"CMD_ENABLE_LOGGING": "Enable Detailed Logs",
"CMD_ENABLE_PHNODE_INSPECTOR": "Enable PhNode Inspector",
"CMD_ENABLE_LIVE_PREVIEW_LOGS": "Live Preview Logs",
"CMD_OPEN_BRACKETS_SOURCE": "Open {APP_NAME} Source",

Expand Down
Loading
Loading