run-selfhosted.js 5.9 KB

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