@peerbit/server 5.10.4 → 5.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/server",
3
- "version": "5.10.4",
3
+ "version": "5.10.5",
4
4
  "author": "dao.xyz",
5
5
  "repository": {
6
6
  "type": "git",
@@ -66,8 +66,8 @@
66
66
  "shx": "^0.3.4",
67
67
  "@types/libsodium-wrappers": "^0.7.14",
68
68
  "uuid": "^10.0.0",
69
- "@peerbit/test-utils": "2.3.10",
70
- "@peerbit/test-lib": "0.0.1"
69
+ "@peerbit/test-lib": "0.0.1",
70
+ "@peerbit/test-utils": "2.3.10"
71
71
  },
72
72
  "dependencies": {
73
73
  "axios": "^1.4.0",
@@ -89,12 +89,12 @@
89
89
  "memory-level": "^3.1.0",
90
90
  "multiformats": "^13.4.1",
91
91
  "abstract-level": "^3.1.0",
92
- "@peerbit/blocks": "3.1.4",
93
92
  "peerbit": "4.4.10",
93
+ "@peerbit/blocks": "3.1.4",
94
94
  "@peerbit/crypto": "2.4.0",
95
- "@peerbit/time": "2.3.0",
95
+ "@peerbit/program": "5.5.0",
96
96
  "@peerbit/pubsub": "4.1.1",
97
- "@peerbit/program": "5.5.0"
97
+ "@peerbit/time": "2.3.0"
98
98
  },
99
99
  "optionalDependencies": {
100
100
  "@aws-sdk/client-ec2": "^3.390.0",
package/src/server.ts CHANGED
@@ -60,6 +60,56 @@ const getInstallDir = (): string | null => {
60
60
  );
61
61
  };
62
62
 
63
+ const packageIsInstalledInDir = (installDir: string, packageName: string) => {
64
+ const packageJsonPath = path.join(
65
+ installDir,
66
+ "node_modules",
67
+ ...packageName.split("/"),
68
+ "package.json",
69
+ );
70
+ return fs.existsSync(packageJsonPath);
71
+ };
72
+
73
+ const reinstallMissingSessionImports = async (
74
+ session: Session | undefined,
75
+ ): Promise<void> => {
76
+ if (!session) return;
77
+
78
+ const installDir = getInstallDir();
79
+ if (!installDir) return;
80
+
81
+ const imports = await session.imports.all();
82
+ if (imports.length === 0) return;
83
+
84
+ let permission = "";
85
+ try {
86
+ fs.accessSync(installDir, fs.constants.W_OK);
87
+ } catch (_e) {
88
+ permission = "sudo";
89
+ }
90
+
91
+ for (const [packageName] of imports) {
92
+ // If already present, don't touch it.
93
+ if (packageIsInstalledInDir(installDir, packageName)) {
94
+ continue;
95
+ }
96
+
97
+ try {
98
+ console.log(
99
+ `Reinstalling dependency from previous session: ${packageName}`,
100
+ );
101
+ execSync(
102
+ `${permission} npm install ${packageName} --prefix ${installDir} --no-save --no-package-lock`,
103
+ );
104
+ } catch (e: any) {
105
+ console.error(
106
+ `Failed to reinstall dependency from previous session: ${packageName}`,
107
+ e?.message?.toString?.() || e,
108
+ );
109
+ }
110
+ }
111
+ };
112
+
63
113
  // eslint-disable-next-line @typescript-eslint/naming-convention
64
114
  const __dirname = dirname(fileURLToPath(import.meta.url));
65
115
 
@@ -153,7 +203,14 @@ export const startServerWithNode = async (properties: {
153
203
  );
154
204
  if (!properties.newSession) {
155
205
  for (const [string] of await session.imports.all()) {
156
- await import(string);
206
+ try {
207
+ await import(string);
208
+ } catch (error) {
209
+ console.error(
210
+ `Failed to import dependency '${string}' from previous session`,
211
+ error,
212
+ );
213
+ }
157
214
  }
158
215
  for (const [address, args] of await session.programs.all()) {
159
216
  // TODO args
@@ -742,7 +799,7 @@ export const startApiServer = async (
742
799
  res.writeHead(200);
743
800
  res.end(JSON.stringify({ version: resolvedVersion }));
744
801
 
745
- process.nextTick(() => {
802
+ process.nextTick(async () => {
746
803
  try {
747
804
  const installDir = getInstallDir();
748
805
  let permission = "";
@@ -769,6 +826,14 @@ export const startApiServer = async (
769
826
  } catch (e) {
770
827
  console.error("Self-update installation failed:", e);
771
828
  }
829
+ try {
830
+ await reinstallMissingSessionImports(properties?.session);
831
+ } catch (e) {
832
+ console.error(
833
+ "Failed to reinstall dependencies from previous session:",
834
+ e,
835
+ );
836
+ }
772
837
  restart();
773
838
  });
774
839
  } catch (error: any) {