Skip to content

Commit d8ce822

Browse files
authored
Merge pull request #206 from dosyago/win-br
Win br
2 parents 1a25478 + dac4145 commit d8ce822

File tree

9 files changed

+146
-128
lines changed

9 files changed

+146
-128
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
*.blob
2+
.\build\*
13
22120-arc
24

35
.*.swp

exec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import path from 'path';
2+
import {execSync} from 'child_process';
3+
4+
const runPath = path.resolve(process.argv[2]);
5+
execSync(`"${runPath}"`,{stdio:'inherit'});
6+

package-lock.json

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"scripts": {
1212
"start": "node --max-old-space-size=4096 src/app.js",
13-
"build": "bash ./scripts/build_only.sh",
13+
"build": "node exec.js \"./scripts/build_only.sh\"",
1414
"clean": "rm package-lock.json; rm -rf node_modules; rm -rf build/*",
1515
"test": "node --watch src/app.js",
1616
"inspect": "node --inspect-brk=127.0.0.1:9999 src/app.js",
@@ -54,6 +54,7 @@
5454
"@eslint/js": "latest",
5555
"esbuild": "0.23.0",
5656
"eslint": "latest",
57-
"globals": "latest"
57+
"globals": "latest",
58+
"postject": "^1.0.0-alpha.6"
5859
}
5960
}

scripts/build_only.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
set -x
3+
#set -x
44
source $HOME/.nvm/nvm.sh
55

66
rm -rf build
@@ -29,10 +29,16 @@ echo "#!/usr/bin/env node" > build/global/downloadnet.cjs
2929
cat build/cjs/dn.cjs >> build/global/downloadnet.cjs
3030
chmod +x build/global/downloadnet.cjs
3131
if [[ "$OSTYPE" == darwin* ]]; then
32+
echo "Using macOS builder..." >&2
3233
./stampers/macos.sh dn build/cjs/dn.cjs build/bin/
33-
elif [[ "$OSTYPE" == win* ]]; then
34-
./stampers/win.sh dn build/cjs/dn.cjs build/bin/
34+
elif [[ "$(node.exe -p process.platform)" == win* ]]; then
35+
echo "Using windows builder..." >&2
36+
./stampers/win.bat dn-win.exe ./build/cjs/dn.cjs ./build/bin/
3537
else
38+
echo "Using linux builder..." >&2
3639
./stampers/nix.sh dn build/cjs/dn.cjs build/bin/
3740
fi
41+
echo "Done"
42+
43+
read -p "Any key to exit"
3844

src/app.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const LAUNCH_OPTS = {
3838
ignoreDefaultFlags: true
3939
}
4040
const KILL_ON = (browser) => ({
41-
win32: `taskkill /IM ${browser}.exe /F`,
41+
win32: `taskkill /IM ${browser} /F`,
4242
darwin: `kill $(pgrep -i ${browser})`,
4343
freebsd: `pkill -15 ${browser}`,
4444
linux: `pkill -15 ${browser}`,
@@ -65,17 +65,17 @@ async function start() {
6565
process.on('SIGBREAK', code => cleanup(code, 'signal', {exit:true}));
6666
process.on('SIGABRT', code => cleanup(code, 'signal', {exit:true}));
6767

68-
console.log(`Importing dependencies...`);
68+
console.log(`Checking browsers...`);
6969
const {launch:ChromeLaunch} = ChromeLauncher;
7070

7171
const list = await psList();
7272

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

75-
const chromeOpen = list.find(({name,cmd}) => name?.match?.(/^(chrome|google chrome)/gi) || cmd?.match?.(/[\/\\]chrome/gi));
75+
const chromeOpen = list.find(({name,cmd}) => name?.match?.(/^(chrome|google chrome|google-chrome)/gi) || cmd?.match?.(/[\/\\]chrome/gi));
7676
const vivaldiOpen = list.find(({name,cmd}) => name?.match?.(/^vivaldi/gi) || cmd?.match?.(/[\/\\]vivaldi/gi));
7777
const braveOpen = list.find(({name,cmd}) => name?.match?.(/^brave/gi) || cmd?.match?.(/[\/\\]brave/gi));
78-
const edgeOpen = list.find(({name,cmd}) => name?.match?.(/^edge/gi) || cmd?.match?.(/[\/\\]edge/gi));
78+
const edgeOpen = list.find(({name,cmd}) => name?.match?.(/^(edge|msedge)/gi) || cmd?.match?.(/[\/\\](msedge|edge)/gi));
7979
const browserOpen = chromeOpen || vivaldiOpen || braveOpen || edgeOpen;
8080
const browsers = [{chromeOpen}, {vivaldiOpen}, {braveOpen}, {edgeOpen}];
8181
DEBUG.showList && console.log({browserOpen, browsers});
@@ -88,20 +88,20 @@ async function start() {
8888
if ( !status[keyName] ) continue;
8989
DEBUG.showList && console.log(status);
9090
const openBrowserCode = keyName.replace('Open', '');
91-
Browser = openBrowserCode;
91+
Browser = status[keyName].name;
9292
console.info(`\n\n [ATTENTION!] Seems ${openBrowserCode} is already open.\n\n`);
9393
if ( DEBUG.askFirst ) {
9494
const question = util.promisify(rl.question).bind(rl);
9595
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`);
9696
const answer = await question(`Would you like to shutdown ${openBrowserCode} browser now (y/N) ? `);
9797
if ( answer?.match(/^y/i) ) {
98-
await killBrowser(openBrowserCode);
98+
await killBrowser(Browser);
9999
shutOne = true;
100100
} else {
101101
console.log(`OK, not shutting it!\n`);
102102
}
103103
} else {
104-
await killBrowser(openBrowserCode);
104+
await killBrowser(Browser);
105105
}
106106
}
107107
if ( !shutOne ) {

src/archivist.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
]);
178178
const NEVER_CACHE = new Set([
179179
`${GO_SECURE ? 'https://localhost' : 'http://127.0.0.1'}:${args.server_port}`,
180+
`http://localhost:${args.server_port}`,
180181
`http://localhost:${args.chrome_port}`,
181182
`http://127.0.0.1:${args.chrome_port}`,
182183
`https://127.0.0.1:${args.chrome_port}`,
@@ -187,7 +188,7 @@
187188
const CACHE_FILE = args.cache_file;
188189
const INDEX_FILE = args.index_file;
189190
const NO_FILE = args.no_file;
190-
const TBL = /:\/\//g;
191+
const TBL = /(:\/\/|:|@)/g;
191192
const UNCACHED_BODY = b64('We have not saved this data');
192193
const UNCACHED_CODE = 404;
193194
const UNCACHED_HEADERS = [
@@ -776,24 +777,39 @@
776777
}
777778

778779
async function saveResponseData(key, url, response) {
779-
const origin = (new URL(url).origin);
780-
let originDir = State.Cache.get(origin);
781-
if ( ! originDir ) {
782-
originDir = Path.resolve(library_path(), origin.replace(TBL, '_'));
783-
try {
784-
await Fs.promises.mkdir(originDir, {recursive:true});
785-
} catch(e) {
786-
console.warn(`Issue with origin directory ${Path.dirname(responsePath)}`, e);
780+
try {
781+
const origin = (new URL(url).origin);
782+
let originDir = State.Cache.get(origin);
783+
if ( ! originDir ) {
784+
originDir = Path.resolve(library_path(), origin.replace(TBL, '_'));
785+
try {
786+
Fs.mkdirSync(originDir, {recursive:true});
787+
} catch(e) {
788+
console.warn(`Issue with origin directory ${originDir}`, e);
789+
}
790+
State.Cache.set(origin, originDir);
791+
} else {
792+
if ( originDir.includes(':\\\\') ) {
793+
originDir = originDir.split(/:\\\\/, 2);
794+
originDir[1] = originDir[1]?.replace?.(TBL, '_');
795+
originDir = originDir.join(':\\\\');
796+
}
787797
}
788-
State.Cache.set(origin, originDir);
789-
}
790798

791-
const fileName = `${await sha1(key)}.json`;
799+
const fileName = `${await sha1(key)}.json`;
792800

793-
const responsePath = Path.resolve(originDir, fileName);
794-
await Fs.promises.writeFile(responsePath, JSON.stringify(response,null,2));
801+
const responsePath = Path.resolve(originDir, fileName);
802+
try {
803+
await Fs.promises.writeFile(responsePath, JSON.stringify(response,null,2));
804+
} catch(e) {
805+
console.warn(`Issue with origin directory or file: ${responsePath}`, e);
806+
}
795807

796-
return responsePath;
808+
return responsePath;
809+
} catch(e) {
810+
console.warn(`Could not save response data`, e);
811+
return '';
812+
}
797813
}
798814

799815
async function sha1(key) {

stampers/win.bat

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@echo off
2+
setlocal
3+
4+
:: Check for required arguments
5+
if "%~3"=="" (
6+
echo Usage: %0 executable_name js_source_file output_folder
7+
exit /b 1
8+
)
9+
10+
:: Define variables from command line arguments
11+
set "EXE_NAME=%~1"
12+
set "JS_SOURCE_FILE=%~2"
13+
set "OUTPUT_FOLDER=%~3"
14+
set "SEA_CONFIG=sea-config.json"
15+
16+
echo "Exe name: %EXE_NAME%"
17+
echo "JS source: %JS_SOURCE_FILE%"
18+
echo "Output folder: %OUTPUT_FOLDER%"
19+
echo "SEA Config file: %SEA_CONFIG%"
20+
21+
set /p "user_input=Press enter to continue"
22+
23+
:: Ensure output folder exists
24+
if not exist "%OUTPUT_FOLDER%" mkdir "%OUTPUT_FOLDER%"
25+
26+
:: Create configuration file for SEA
27+
(
28+
echo {
29+
echo "main": "%JS_SOURCE_FILE%",
30+
echo "output": "sea-prep.blob",
31+
echo "disableExperimentalSEAWarning": true,
32+
echo "useCodeCache": true,
33+
echo "assets": {
34+
echo "index.html": "public/index.html",
35+
echo "favicon.ico": "public/favicon.ico",
36+
echo "top.html": "public/top.html",
37+
echo "style.css": "public/style.css",
38+
echo "injection.js": "public/injection.js",
39+
echo "redirector.html": "public/redirector.html"
40+
echo }
41+
echo }
42+
) > "%OUTPUT_FOLDER%\%SEA_CONFIG%"
43+
44+
:: Generate the blob to be injected
45+
node --experimental-sea-config "%OUTPUT_FOLDER%\%SEA_CONFIG%"
46+
47+
:: Copy the node executable and rename
48+
node -e "require('fs').copyFileSync(process.execPath, '%OUTPUT_FOLDER%\%EXE_NAME%')"
49+
50+
:: Optionally, remove signature from the binary (use signtool if necessary, or skip this step)
51+
signtool remove /s "%OUTPUT_FOLDER%\%EXE_NAME%"
52+
53+
:: Inject the blob into the copied binary
54+
npx postject "%OUTPUT_FOLDER%\%EXE_NAME%" NODE_SEA_BLOB sea-prep.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
55+
56+
:: Clean up
57+
echo Application built successfully.
58+
59+
:end
60+

stampers/win.ps1

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)