Skip to content

Commit

Permalink
fix: remote files works with all http/s urls via node fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Dec 26, 2023
1 parent 5769cf8 commit 4a97fb2
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 97 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ module.exports = {
"$": false,
"brackets": false,
"Phoenix": false,
"iconv": false,
"Buffer": true,
"clearTimeout": false,
"console": false,
"define": false,
Expand Down
8 changes: 5 additions & 3 deletions docs/generatedApiDocs/NodeConnector-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Returns **[boolean][5]** Returns true if Node.js Engine is available.

Terminate the PhNodeEngine node if it is available. Else does nothing.

Returns **void**
Returns **[Promise][6]** promise that resolves when node process is terminated and exits.

## setInspectEnabled

Expand All @@ -187,7 +187,7 @@ Returns **[boolean][5]** True if inspect mode is enabled, false otherwise.

Retrieves the node inspector port for the Phoenix Node.js engine.

Returns **[number][6]** The inspection port number.
Returns **[number][7]** The inspection port number.

[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

Expand All @@ -199,4 +199,6 @@ Returns **[number][6]** The inspection port number.

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
1 change: 1 addition & 0 deletions src-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const net = require('net');
const PhoenixFS = require('@phcode/fs/dist/phoenix-fs');
const NodeConnector = require("./node-connector");
require("./test-connection");
require("./utils");
function randomNonce(byteLength) {
const randomBuffer = new Uint8Array(byteLength);
crypto.getRandomValues(randomBuffer);
Expand Down
4 changes: 2 additions & 2 deletions src-node/test-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function awaits(waitTimeMs){
});
}

exports.testDelayedNodeConnectorCreateExec = async function (connectorName) {
exports.testDelayedNodeConnectorCreateExec = async function () {
const newNodeConnName = "node_exec_Q_test";
const newExport = {};
const newNodeConn = NodeConnector.createNodeConnector(newNodeConnName, newExport);
Expand All @@ -201,7 +201,7 @@ exports.testDelayedNodeConnectorCreateExec = async function (connectorName) {
expectEqual(result, expectedResult);
};

exports.testDelayedNodeConnectorCreateEvent = async function (connectorName) {
exports.testDelayedNodeConnectorCreateEvent = async function () {
const newNodeConnName = "node_event_Q_test";
const newExport = {};
const newNodeConn = NodeConnector.createNodeConnector(newNodeConnName, newExport);
Expand Down
23 changes: 23 additions & 0 deletions src-node/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const NodeConnector = require("./node-connector");

const UTILS_NODE_CONNECTOR = "ph_utils";
NodeConnector.createNodeConnector(UTILS_NODE_CONNECTOR, exports);

async function getURLContent({url, options}) {
options = options || {
redirect: "follow",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
Host: "github.com",
Referer: "https://github.com/",
"Cache-Control": "no-cache"
}
};
const fetchResponse = await fetch(url, options);
const bufferContents = await fetchResponse.arrayBuffer();
return {
buffer: bufferContents
};
}

exports.getURLContent = getURLContent;
1 change: 1 addition & 0 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ define(function (require, exports, module) {
require("LiveDevelopment/main");
require("utils/NodeConnection");
require("utils/NodeDomain");
require("utils/NodeUtils");
require("utils/ColorUtils");
require("view/ThemeManager");
require("thirdparty/lodash");
Expand Down
41 changes: 32 additions & 9 deletions src/filesystem/RemoteFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
define(function (require, exports, module) {


var FileSystemError = require("filesystem/FileSystemError"),
FileSystemStats = require("filesystem/FileSystemStats");
const FileSystemError = require("filesystem/FileSystemError"),
FileSystemStats = require("filesystem/FileSystemStats"),
NodeUtils = require("utils/NodeUtils");

var SESSION_START_TIME = new Date();
const SESSION_START_TIME = new Date();

/**
* Create a new file stat. See the FileSystemStats class for more details.
Expand Down Expand Up @@ -164,19 +165,41 @@ define(function (require, exports, module) {
// no-op
};

function _nodeConnectorRead(url, encoding, successCB, errorCB) {
NodeUtils.fetchURLText(url, encoding)
.then(successCB)
.catch(err=>{
console.error("failed fetch url: ", url, err);
errorCB(err);
});
}

function isTauriResource(url) {
const startingURLs = [
"phtauri://", "https://phtauri.localhost", "asset://", "https://asset.localhost",
"tauri://", "https://tauri.localhost"
];
for(let start of startingURLs){
if(url.startsWith(start)){
return true;
}
}
return false;
}

function _remoteRead(url, encoding, successCB, errorCB) {
if(Phoenix.browser.isTauri && !isTauriResource(url)) {
_nodeConnectorRead(url, encoding, successCB, errorCB);
return;
}
let xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.responseType = "arraybuffer";

xmlhttp.onload = function(oEvent) {
var arrayBuffer = xmlhttp.response;

// if you want to access the bytes:
var byteArray = new Uint8Array(arrayBuffer);

const arrayBuffer = xmlhttp.response;
try {
successCB(new TextDecoder(encoding).decode(byteArray));
successCB(iconv.decode(Buffer.from(arrayBuffer), encoding));
} catch (err) {
errorCB(err);
}
Expand Down
9 changes: 0 additions & 9 deletions src/utils/Global.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ define(function (require, exports, module) {
console.log(err);
}

// Uncomment the following line to force all low level file i/o routines to complete
// asynchronously. This should only be done for testing/debugging.
// NOTE: Make sure this line is commented out again before committing!
//brackets.forceAsyncCallbacks = true;

// Load native shell when brackets is run in a native shell rather than the browser
// TODO: (issue #266) load conditionally
global.brackets.shellAPI = require("utils/ShellAPI");

global.brackets.nativeMenus = false;

// Locale-related APIs
Expand Down
41 changes: 41 additions & 0 deletions src/utils/NodeUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 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.
*
*/

/**
* Generic node util APIs connector. see `src-node/utils.js` for node peer
*/

define(function (require, exports, module) {
if(!Phoenix.browser.isTauri) {
// node not available in browser builds, return
return;
}
const UTILS_NODE_CONNECTOR = "ph_utils";
const NodeConnector = require('NodeConnector');
const utilsConnector = NodeConnector.createNodeConnector(UTILS_NODE_CONNECTOR, exports);

async function fetchURLText(url, encoding) {
const {buffer} = await utilsConnector.execPeer("getURLContent", {url});
return iconv.decode(Buffer.from(buffer), encoding);
}

exports.fetchURLText = fetchURLText;
});
74 changes: 0 additions & 74 deletions src/utils/ShellAPI.js

This file was deleted.

1 change: 1 addition & 0 deletions test/SpecRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ define(function (require, exports, module) {
require("utils/Global");
require("command/Menus");
require("utils/NodeDomain");
require("utils/NodeUtils");
require("utils/ColorUtils");
require("preferences/PreferencesBase");
require("JSUtils/Session");
Expand Down

0 comments on commit 4a97fb2

Please sign in to comment.