@heyputer/puter.js 2.0.4 → 2.0.6

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.
@@ -1,13 +1,12 @@
1
- const readFileSync = require('node:fs');
1
+ const { readFileSync } = require('node:fs');
2
2
  const vm = require('node:vm');
3
3
 
4
4
  /**
5
5
  * Method for loading puter.js in Node.js environment
6
6
  * @param {string} authToken - Optional auth token to initialize puter with
7
- * @returns {Promise<import('../index').Puter>} The `puter` object from puter.js
7
+ * @returns {Promise<import('../index.d.ts').puter>} The `puter` object from puter.js
8
8
  */
9
9
  const safeLoadPuterJs = (authToken) => {
10
-
11
10
  const goodContext = {};
12
11
  Object.getOwnPropertyNames(globalThis).forEach(name => {
13
12
  try {
@@ -1,13 +1,13 @@
1
- import putility from "@heyputer/putility";
2
- import { PuterAPIFilesystem } from "../lib/filesystem/APIFS.js";
3
- import { CachedFilesystem } from "../lib/filesystem/CacheFS.js";
4
- import { ProxyFilesystem, TFilesystem } from "../lib/filesystem/definitions.js";
1
+ import putility from '@heyputer/putility';
2
+ import { PuterAPIFilesystem } from '../lib/filesystem/APIFS.js';
3
+ import { CachedFilesystem } from '../lib/filesystem/CacheFS.js';
4
+ import { ProxyFilesystem, TFilesystem } from '../lib/filesystem/definitions.js';
5
+ import { PostMessageFilesystem } from '../lib/filesystem/PostMessageFS.js';
5
6
  import io from '../lib/socket.io/socket.io.esm.min.js';
6
- import { PostMessageFilesystem } from "../lib/filesystem/PostMessageFS.js";
7
7
 
8
8
  export class FilesystemService extends putility.concepts.Service {
9
9
  static PROPERTIES = {
10
- // filesystem:
10
+ // filesystem:
11
11
  };
12
12
 
13
13
  static DEPENDS = ['api-access'];
@@ -19,13 +19,13 @@ export class FilesystemService extends putility.concepts.Service {
19
19
  re-initialize the socket connection whenever the
20
20
  authentication token or API origin is changed.
21
21
  `,
22
- async do () {
22
+ async do() {
23
23
  this.initializeSocket();
24
- }
25
- }
26
- ]
24
+ },
25
+ },
26
+ ];
27
27
 
28
- _init () {
28
+ _init() {
29
29
  const env = this._.context.env;
30
30
 
31
31
  if ( env === 'app' ) {
@@ -40,14 +40,14 @@ export class FilesystemService extends putility.concepts.Service {
40
40
  this.initializeSocket();
41
41
  }
42
42
 
43
- init_app_fs_ () {
43
+ init_app_fs_() {
44
44
  this.fs_nocache_ = new PostMessageFilesystem({
45
45
  messageTarget: globalThis.parent,
46
46
  rpc: this._.context.util.rpc,
47
47
  }).as(TFilesystem);
48
48
  this.filesystem = this.fs_nocache_;
49
49
  }
50
- init_top_fs_ () {
50
+ init_top_fs_() {
51
51
  const api_info = this._.context.services.get('api-access').get_api_info();
52
52
  this.fs_nocache_ = new PuterAPIFilesystem({ api_info }).as(TFilesystem);
53
53
  this.fs_cache_ = new CachedFilesystem({ delegate: this.fs_nocache_ }).as(TFilesystem);
@@ -56,15 +56,19 @@ export class FilesystemService extends putility.concepts.Service {
56
56
  this.filesystem = this.fs_proxy_.as(TFilesystem);
57
57
  }
58
58
 
59
- cache_on () {
59
+ cache_on() {
60
60
  this.fs_proxy_.delegate = this.fs_cache_;
61
61
  }
62
- cache_off () {
62
+ cache_off() {
63
63
  this.fs_proxy_.delegate = this.fs_nocache_;
64
64
  }
65
65
 
66
- async initializeSocket () {
67
- if (this.socket) {
66
+ async initializeSocket() {
67
+ if ( globalThis.puter.env === 'nodejs' ){
68
+ return;
69
+ }
70
+
71
+ if ( this.socket ) {
68
72
  this.socket.disconnect();
69
73
  }
70
74
 
@@ -77,7 +81,7 @@ export class FilesystemService extends putility.concepts.Service {
77
81
  }
78
82
 
79
83
  this.socket = io(api_info.api_origin, {
80
- auth: { auth_token: api_info.auth_token }
84
+ auth: { auth_token: api_info.auth_token },
81
85
  });
82
86
 
83
87
  this.bindSocketEvents();
@@ -85,38 +89,52 @@ export class FilesystemService extends putility.concepts.Service {
85
89
 
86
90
  bindSocketEvents() {
87
91
  this.socket.on('connect', () => {
88
- if(puter.debugMode)
92
+ if ( puter.debugMode )
93
+ {
89
94
  console.log('FileSystem Socket: Connected', this.socket.id);
95
+ }
90
96
  });
91
97
 
92
98
  this.socket.on('disconnect', () => {
93
- if(puter.debugMode)
99
+ if ( puter.debugMode )
100
+ {
94
101
  console.log('FileSystem Socket: Disconnected');
102
+ }
95
103
  });
96
104
 
97
105
  this.socket.on('reconnect', (attempt) => {
98
- if(puter.debugMode)
106
+ if ( puter.debugMode )
107
+ {
99
108
  console.log('FileSystem Socket: Reconnected', this.socket.id);
109
+ }
100
110
  });
101
111
 
102
112
  this.socket.on('reconnect_attempt', (attempt) => {
103
- if(puter.debugMode)
113
+ if ( puter.debugMode )
114
+ {
104
115
  console.log('FileSystem Socket: Reconnection Attemps', attempt);
116
+ }
105
117
  });
106
118
 
107
119
  this.socket.on('reconnect_error', (error) => {
108
- if(puter.debugMode)
120
+ if ( puter.debugMode )
121
+ {
109
122
  console.log('FileSystem Socket: Reconnection Error', error);
123
+ }
110
124
  });
111
125
 
112
126
  this.socket.on('reconnect_failed', () => {
113
- if(puter.debugMode)
127
+ if ( puter.debugMode )
128
+ {
114
129
  console.log('FileSystem Socket: Reconnection Failed');
130
+ }
115
131
  });
116
132
 
117
133
  this.socket.on('error', (error) => {
118
- if(puter.debugMode)
134
+ if ( puter.debugMode )
135
+ {
119
136
  console.error('FileSystem Socket Error:', error);
137
+ }
120
138
  });
121
139
  }
122
140
  }