Spaces:
Sleeping
Sleeping
File size: 6,438 Bytes
61d39e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter 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://www.gnu.org/licenses/>.
*/
// surrounding_box function
//
// It's really hard to see an error message without using
// the surrounding_box function to highlight its location.
// The implementation of this in packages/backend might not
// work in older versions of node, so we instead re-implement
// it here.
import console from 'node:console';
import process from 'node:process';
try {
await import('dotenv/config');
} catch (e) {
// dotenv is optional
}
const surrounding_box = (col, lines) => {
const lengths = lines.map(line => line.length);
const max_length = Math.max(...lengths);
const c = str => `\x1b[${col}m${str}\x1b[0m`;
const bar = c(Array(max_length + 4).fill('━').join(''));
for ( let i = 0 ; i < lines.length ; i++ ) {
while ( lines[i].length < max_length ) {
lines[i] += ' ';
}
lines[i] = `${c('┃ ')} ${lines[i]} ${c(' ┃')}`;
}
lines.unshift(`${c('┏')}${bar}${c('┓')}`);
lines.push(`${c('┗')}${bar}${c('┛')}`);
};
// node version check
{
// Keeping track of WHY certain versions don't work
const ver_info = [
{ under: 14, reasons: ['optional chaining is not available'] },
{ under: 16, reasons: ['disk usage package ABI mismatch'] },
];
const lowest_allowed = Math.max(...ver_info.map(r => r.under));
// ACTUAL VERSION CHECK
const [major, minor] = process.versions.node.split('.').map(Number);
if ( major < lowest_allowed ) {
const lines = [];
lines.push(`Please use a version of Node.js ${lowest_allowed} or newer.`);
lines.push(`Issues with node ${process.versions.node}:`);
// We also show the user the reasons in case they want to know
for ( const { under, reasons } of ver_info ) {
if ( major < under ) {
lines.push(` - ${reasons.join(', ')}`);
}
}
surrounding_box('31;1', lines);
console.error(lines.join('\n'));
process.exit(1);
}
}
// Annoying polyfill for inconsistency in different node versions
if ( ! import.meta.filename ) {
Object.defineProperty(import.meta, 'filename', {
get: () => import.meta.url.slice('file://'.length),
})
}
const main = async () => {
const {
Kernel,
EssentialModules,
DatabaseModule,
LocalDiskStorageModule,
MemoryStorageModule,
SelfHostedModule,
BroadcastModule,
TestDriversModule,
TestConfigModule,
PuterAIModule,
InternetModule,
DevelopmentModule,
DNSModule,
PerfMonModule,
} = (await import('@heyputer/backend')).default;
const k = new Kernel({
entry_path: import.meta.filename
});
for ( const mod of EssentialModules ) {
k.add_module(new mod());
}
k.add_module(new DatabaseModule());
k.add_module(new LocalDiskStorageModule());
k.add_module(new MemoryStorageModule());
k.add_module(new SelfHostedModule());
k.add_module(new BroadcastModule());
k.add_module(new TestDriversModule());
k.add_module(new TestConfigModule());
k.add_module(new PuterAIModule());
k.add_module(new InternetModule());
k.add_module(new DNSModule());
if ( process.env.PERFMON ) {
k.add_module(new PerfMonModule());
}
if ( process.env.UNSAFE_PUTER_DEV ) {
k.add_module(new DevelopmentModule());
}
k.boot();
};
const early_init_errors = [
{
text: `Cannot find package '@heyputer/backend'`,
notes: [
'this usually happens if you forget `npm install`'
],
suggestions: [
'try running `npm install`'
],
technical_notes: [
'@heyputer/backend is in an npm workspace'
]
},
{
text: `Cannot find package`,
notes: [
'this usually happens if you forget `npm install`'
],
suggestions: [
'try running `npm install`'
],
},
{
text: 'Cannot write to path',
notes: [
'this usually happens when /var/puter isn\'t chown\'d to the right UID'
],
suggestions: [
'check issue #645 on our github'
]
}
];
// null coalescing operator
const nco = (...args) => {
for ( const arg of args ) {
if ( arg !== undefined && arg !== null ) {
return arg;
}
}
return undefined;
}
const _print_error_help = (error_help) => {
const lines = [];
lines.push(nco(error_help.title, error_help.text));
for ( const note of (nco(error_help.notes, [])) ) {
lines.push(`📝 ${note}`)
}
if ( error_help.suggestions ) {
lines.push('Suggestions:');
for ( const suggestion of error_help.suggestions ) {
lines.push(`- ${suggestion}`);
}
}
if ( error_help.technical_notes ) {
lines.push('Technical Notes:');
for ( const note of error_help.technical_notes ) {
lines.push(`- ${note}`);
}
}
surrounding_box('31;1', lines);
console.error(lines.join('\n'));
}
(async () => {
try {
await main();
} catch (e) {
for ( const error_help of early_init_errors ) {
const message = e && e.message;
if ( e.message && e.message.includes(error_help.text) ) {
_print_error_help(error_help);
break;
}
}
throw e;
}
})();
|