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

Improving compatability with MacOS #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipobscure/sea",
"version": "1.0.5",
"version": "1.0.6",
"description": "Node SEA Builder",
"main": "dist/resolutions.js",
"bin": {
Expand Down Expand Up @@ -37,7 +37,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"glob": "^10.3.10",
"glob": "11.0.0",
"minimatch": "^9.0.3",
"postject": "^1.0.0-alpha.6",
"terser": "^5.26.0"
Expand Down
56 changes: 43 additions & 13 deletions scripts/sea.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
#!/usr/bin/env node

async function main() {
console.error('writing configuration');
await (require('../dist/configure.js').configure());
console.error('assembling bundle');
await (require('../dist/bundlefiles.js').bundle());
console.error('building executable');
await (require('../dist/injection.js').inject());
console.error('done');
}

if (require.main === module) main();
#!/usr/bin/env node

const { exec } = require("node:child_process");
const { platform } = require("node:os");


async function codesign() {
let osPlatform = platform();
if ( osPlatform == "darwin" ){
const fs = require('fs');
const Path = require('path');
const { base } = require("../dist/packagebase.js");
const PackageBase = base(process.cwd());
const PKG = JSON.parse(fs.readFileSync(Path.join(PackageBase, 'package.json'), 'utf-8'));
const exe_name = `${PKG?.sea?.executable ?? 'bundle'}`;
console.log(`signing ${exe_name}`);
exec(`codesign --sign - ${exe_name}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`codesign err: ${stderr}`);
return;
}
console.log(`codesign: ${stdout}`);
});
}
}

async function main() {
console.error('writing configuration');
await (require('../dist/configure.js').configure());
console.error('assembling bundle');
await (require('../dist/bundlefiles.js').bundle());
console.error('building executable');
await (require('../dist/injection.js').inject());
console.error('codesign');
await codesign();
console.error('done');
}

if (require.main === module) main();
4 changes: 2 additions & 2 deletions src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export async function configure() {
main: File.runtime,
output: File.bundle,
disableExperimentalSEAWarning: true,
useSnapshot: false,
useCodeCache: true,
useSnapshot: File.useSnapshot,
useCodeCache: File.useCodeCache,
assets: {
resolv: File.resolutions,
bundle: File.blobs,
Expand Down
4 changes: 4 additions & 0 deletions src/filenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function names(PKG: any) {
const runtime = `${name}.js`;
const bundle = `${name}.sea`;
const executable = `${name}${isWindows ? '.exe' : ''}`;
const useSnapshot = PKG?.sea?.useSnapshot ?? false;
const useCodeCache = PKG?.sea?.useCodeCache ?? false;
const files = {
config,
blobs,
Expand All @@ -19,6 +21,8 @@ export default function names(PKG: any) {
runtime,
bundle,
executable,
useSnapshot,
useCodeCache,
};
return files;
}
27 changes: 21 additions & 6 deletions src/injection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { readFileSync } from 'node:fs';
import { readFileSync, statSync, chmodSync, constants as fsConstants } from 'node:fs';
import * as Path from 'node:path';
import * as FS from 'node:fs/promises';
import * as Proc from 'node:child_process';
import * as OS from 'node:os';

//@ts-ignore
import { inject } from 'postject';
Expand All @@ -17,14 +18,28 @@ export async function inject() {
await exec(process.execPath, '--experimental-sea-config', Path.join(PackageBase, File.config));

console.error(`copying ${process.execPath} to ${File.executable}`);
await FS.copyFile(process.execPath, Path.join(PackageBase, File.executable));
let newExeFilePath = Path.join(PackageBase, File.executable);
await FS.copyFile(process.execPath, newExeFilePath);

let newFileExeStat = statSync(newExeFilePath);
let newFileExeStatMode = newFileExeStat.mode;
chmodSync(Path.join(PackageBase, File.executable), newFileExeStatMode | fsConstants.S_IWUSR);

console.error(`injecting ${File.bundle} into ${File.executable}`);
const runtime = await FS.readFile(Path.join(PackageBase, File.bundle));
//@ts-ignore
await inject(Path.join(PackageBase, File.executable), 'NODE_SEA_BLOB', runtime, {
sentinelFuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
});
const osPlatform = OS.platform();
if ( osPlatform == "darwin") {
//@ts-ignore
await inject(Path.join(PackageBase, File.executable), 'NODE_SEA_BLOB', runtime, {
sentinelFuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
machoSegmentName: 'NODE_SEA',
});
} else {
//@ts-ignore
await inject(Path.join(PackageBase, File.executable), 'NODE_SEA_BLOB', runtime, {
sentinelFuse: 'NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
});
}
}

function exec(cmd: string, ...args: string[]) {
Expand Down
7 changes: 1 addition & 6 deletions src/resolutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ const resolutions: Record<string, Record<string, string>> = {};

function replacementResolveFilename(this: Module, request: string, parent: Module, isMain: boolean) {
const resolved = _resolveFilename.call(this, request, parent, isMain);
const parentfile = parent?.filename
? '/' +
Path.relative(PackageBase, parent?.filename)
.split(Path.sep)
.join('/')
: '<main>';
const parentfile = parent?.filename ? '/' + Path.relative(PackageBase, parent?.filename).split(Path.sep).join('/') : '<main>';
const cache = (resolutions[parentfile] = resolutions[parentfile] ?? {});
const specifier = (request === process.argv[1] ? '<main>' : request).split(Path.sep).join('/');
if (!Module.isBuiltin(request)) {
Expand Down
2 changes: 0 additions & 2 deletions src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ function resolve(parent?: string, specifier?: string) {
}
}
function patch(id: string, exports: object) {
// @ts-expect-error
exports.__esModule = true;
switch (id) {
case 'node:fs':
return patchFS(exports as typeof FS);
Expand Down