Skip to content

Commit

Permalink
Merge pull request #206 from dosyago/win-br
Browse files Browse the repository at this point in the history
Win br
  • Loading branch information
o0101 authored Aug 29, 2024
2 parents 1a25478 + dac4145 commit d8ce822
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 128 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.blob
.\build\*
22120-arc

.*.swp
Expand Down
6 changes: 6 additions & 0 deletions exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import path from 'path';
import {execSync} from 'child_process';

const runPath = path.resolve(process.argv[2]);
execSync(`"${runPath}"`,{stdio:'inherit'});

29 changes: 28 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scripts": {
"start": "node --max-old-space-size=4096 src/app.js",
"build": "bash ./scripts/build_only.sh",
"build": "node exec.js \"./scripts/build_only.sh\"",
"clean": "rm package-lock.json; rm -rf node_modules; rm -rf build/*",
"test": "node --watch src/app.js",
"inspect": "node --inspect-brk=127.0.0.1:9999 src/app.js",
Expand Down Expand Up @@ -54,6 +54,7 @@
"@eslint/js": "latest",
"esbuild": "0.23.0",
"eslint": "latest",
"globals": "latest"
"globals": "latest",
"postject": "^1.0.0-alpha.6"
}
}
12 changes: 9 additions & 3 deletions scripts/build_only.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -x
#set -x
source $HOME/.nvm/nvm.sh

rm -rf build
Expand Down Expand Up @@ -29,10 +29,16 @@ echo "#!/usr/bin/env node" > build/global/downloadnet.cjs
cat build/cjs/dn.cjs >> build/global/downloadnet.cjs
chmod +x build/global/downloadnet.cjs
if [[ "$OSTYPE" == darwin* ]]; then
echo "Using macOS builder..." >&2
./stampers/macos.sh dn build/cjs/dn.cjs build/bin/
elif [[ "$OSTYPE" == win* ]]; then
./stampers/win.sh dn build/cjs/dn.cjs build/bin/
elif [[ "$(node.exe -p process.platform)" == win* ]]; then
echo "Using windows builder..." >&2
./stampers/win.bat dn-win.exe ./build/cjs/dn.cjs ./build/bin/
else
echo "Using linux builder..." >&2
./stampers/nix.sh dn build/cjs/dn.cjs build/bin/
fi
echo "Done"

read -p "Any key to exit"

14 changes: 7 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const LAUNCH_OPTS = {
ignoreDefaultFlags: true
}
const KILL_ON = (browser) => ({
win32: `taskkill /IM ${browser}.exe /F`,
win32: `taskkill /IM ${browser} /F`,
darwin: `kill $(pgrep -i ${browser})`,
freebsd: `pkill -15 ${browser}`,
linux: `pkill -15 ${browser}`,
Expand All @@ -65,17 +65,17 @@ async function start() {
process.on('SIGBREAK', code => cleanup(code, 'signal', {exit:true}));
process.on('SIGABRT', code => cleanup(code, 'signal', {exit:true}));

console.log(`Importing dependencies...`);
console.log(`Checking browsers...`);
const {launch:ChromeLaunch} = ChromeLauncher;

const list = await psList();

DEBUG.showList && console.log({list});

const chromeOpen = list.find(({name,cmd}) => name?.match?.(/^(chrome|google chrome)/gi) || cmd?.match?.(/[\/\\]chrome/gi));
const chromeOpen = list.find(({name,cmd}) => name?.match?.(/^(chrome|google chrome|google-chrome)/gi) || cmd?.match?.(/[\/\\]chrome/gi));
const vivaldiOpen = list.find(({name,cmd}) => name?.match?.(/^vivaldi/gi) || cmd?.match?.(/[\/\\]vivaldi/gi));
const braveOpen = list.find(({name,cmd}) => name?.match?.(/^brave/gi) || cmd?.match?.(/[\/\\]brave/gi));
const edgeOpen = list.find(({name,cmd}) => name?.match?.(/^edge/gi) || cmd?.match?.(/[\/\\]edge/gi));
const edgeOpen = list.find(({name,cmd}) => name?.match?.(/^(edge|msedge)/gi) || cmd?.match?.(/[\/\\](msedge|edge)/gi));
const browserOpen = chromeOpen || vivaldiOpen || braveOpen || edgeOpen;
const browsers = [{chromeOpen}, {vivaldiOpen}, {braveOpen}, {edgeOpen}];
DEBUG.showList && console.log({browserOpen, browsers});
Expand All @@ -88,20 +88,20 @@ async function start() {
if ( !status[keyName] ) continue;
DEBUG.showList && console.log(status);
const openBrowserCode = keyName.replace('Open', '');
Browser = openBrowserCode;
Browser = status[keyName].name;
console.info(`\n\n [ATTENTION!] Seems ${openBrowserCode} is already open.\n\n`);
if ( DEBUG.askFirst ) {
const question = util.promisify(rl.question).bind(rl);
console.info(`\nDo you want to use it for your archiving? The reason we ask is, because if you don't shut down ${openBrowserCode} and restart it under DownloadNet control you will not be able to use it to save or serve your archives.\n\n`);
const answer = await question(`Would you like to shutdown ${openBrowserCode} browser now (y/N) ? `);
if ( answer?.match(/^y/i) ) {
await killBrowser(openBrowserCode);
await killBrowser(Browser);
shutOne = true;
} else {
console.log(`OK, not shutting it!\n`);
}
} else {
await killBrowser(openBrowserCode);
await killBrowser(Browser);
}
}
if ( !shutOne ) {
Expand Down
46 changes: 31 additions & 15 deletions src/archivist.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
]);
const NEVER_CACHE = new Set([
`${GO_SECURE ? 'https://localhost' : 'http://127.0.0.1'}:${args.server_port}`,
`http://localhost:${args.server_port}`,
`http://localhost:${args.chrome_port}`,
`http://127.0.0.1:${args.chrome_port}`,
`https://127.0.0.1:${args.chrome_port}`,
Expand All @@ -187,7 +188,7 @@
const CACHE_FILE = args.cache_file;
const INDEX_FILE = args.index_file;
const NO_FILE = args.no_file;
const TBL = /:\/\//g;
const TBL = /(:\/\/|:|@)/g;
const UNCACHED_BODY = b64('We have not saved this data');
const UNCACHED_CODE = 404;
const UNCACHED_HEADERS = [
Expand Down Expand Up @@ -776,24 +777,39 @@
}

async function saveResponseData(key, url, response) {
const origin = (new URL(url).origin);
let originDir = State.Cache.get(origin);
if ( ! originDir ) {
originDir = Path.resolve(library_path(), origin.replace(TBL, '_'));
try {
await Fs.promises.mkdir(originDir, {recursive:true});
} catch(e) {
console.warn(`Issue with origin directory ${Path.dirname(responsePath)}`, e);
try {
const origin = (new URL(url).origin);
let originDir = State.Cache.get(origin);
if ( ! originDir ) {
originDir = Path.resolve(library_path(), origin.replace(TBL, '_'));
try {
Fs.mkdirSync(originDir, {recursive:true});
} catch(e) {
console.warn(`Issue with origin directory ${originDir}`, e);
}
State.Cache.set(origin, originDir);
} else {
if ( originDir.includes(':\\\\') ) {
originDir = originDir.split(/:\\\\/, 2);
originDir[1] = originDir[1]?.replace?.(TBL, '_');
originDir = originDir.join(':\\\\');
}
}
State.Cache.set(origin, originDir);
}

const fileName = `${await sha1(key)}.json`;
const fileName = `${await sha1(key)}.json`;

const responsePath = Path.resolve(originDir, fileName);
await Fs.promises.writeFile(responsePath, JSON.stringify(response,null,2));
const responsePath = Path.resolve(originDir, fileName);
try {
await Fs.promises.writeFile(responsePath, JSON.stringify(response,null,2));
} catch(e) {
console.warn(`Issue with origin directory or file: ${responsePath}`, e);
}

return responsePath;
return responsePath;
} catch(e) {
console.warn(`Could not save response data`, e);
return '';
}
}

async function sha1(key) {
Expand Down
60 changes: 60 additions & 0 deletions stampers/win.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
@echo off
setlocal

:: Check for required arguments
if "%~3"=="" (
echo Usage: %0 executable_name js_source_file output_folder
exit /b 1
)

:: Define variables from command line arguments
set "EXE_NAME=%~1"
set "JS_SOURCE_FILE=%~2"
set "OUTPUT_FOLDER=%~3"
set "SEA_CONFIG=sea-config.json"

echo "Exe name: %EXE_NAME%"
echo "JS source: %JS_SOURCE_FILE%"
echo "Output folder: %OUTPUT_FOLDER%"
echo "SEA Config file: %SEA_CONFIG%"

set /p "user_input=Press enter to continue"

:: Ensure output folder exists
if not exist "%OUTPUT_FOLDER%" mkdir "%OUTPUT_FOLDER%"

:: Create configuration file for SEA
(
echo {
echo "main": "%JS_SOURCE_FILE%",
echo "output": "sea-prep.blob",
echo "disableExperimentalSEAWarning": true,
echo "useCodeCache": true,
echo "assets": {
echo "index.html": "public/index.html",
echo "favicon.ico": "public/favicon.ico",
echo "top.html": "public/top.html",
echo "style.css": "public/style.css",
echo "injection.js": "public/injection.js",
echo "redirector.html": "public/redirector.html"
echo }
echo }
) > "%OUTPUT_FOLDER%\%SEA_CONFIG%"

:: Generate the blob to be injected
node --experimental-sea-config "%OUTPUT_FOLDER%\%SEA_CONFIG%"

:: Copy the node executable and rename
node -e "require('fs').copyFileSync(process.execPath, '%OUTPUT_FOLDER%\%EXE_NAME%')"

:: Optionally, remove signature from the binary (use signtool if necessary, or skip this step)
signtool remove /s "%OUTPUT_FOLDER%\%EXE_NAME%"

:: Inject the blob into the copied binary
npx postject "%OUTPUT_FOLDER%\%EXE_NAME%" NODE_SEA_BLOB sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2

:: Clean up
echo Application built successfully.

:end

100 changes: 0 additions & 100 deletions stampers/win.ps1

This file was deleted.

0 comments on commit d8ce822

Please sign in to comment.