@heyputer/puter.js 2.0.5 → 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.
package/index.d.ts CHANGED
@@ -488,7 +488,7 @@ export {
488
488
  KVPair, LaunchAppOptions, MenubarOptions,
489
489
  MenuItem, MkdirOptions,
490
490
  MoveOptions, Networking,
491
- Permissions, ReaddirOptions, ReadOptions, SpaceInfo, StatsPeriod, Subdomain, ThemeData, ToolCall, ToolDefinition, Txt2ImgOptions,
491
+ Permissions, Puter, ReaddirOptions, ReadOptions, SpaceInfo, StatsPeriod, Subdomain, ThemeData, ToolCall, ToolDefinition, Txt2ImgOptions,
492
492
  Txt2SpeechOptions, UI, UpdateAppAttributes, User, WindowOptions, WorkerDeployment,
493
493
  WorkerExecOptions,
494
494
  WorkerInfo, Workers, WriteOptions
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heyputer/puter.js",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "description": "Puter.js - A JavaScript library for interacting with Puter services.",
5
5
  "main": "src/index.js",
6
6
  "types": "index.d.ts",
@@ -1,23 +1,23 @@
1
1
  import io from '../../lib/socket.io/socket.io.esm.min.js';
2
2
 
3
3
  // Operations
4
- import space from "./operations/space.js";
5
- import mkdir from "./operations/mkdir.js";
6
- import copy from "./operations/copy.js";
7
- import rename from "./operations/rename.js";
8
- import upload from "./operations/upload.js";
9
- import read from "./operations/read.js";
10
- import move from "./operations/move.js";
11
- import write from "./operations/write.js";
12
- import sign from "./operations/sign.js";
13
- import symlink from './operations/symlink.js';
4
+ import copy from './operations/copy.js';
5
+ import mkdir from './operations/mkdir.js';
6
+ import move from './operations/move.js';
7
+ import read from './operations/read.js';
14
8
  import readdir from './operations/readdir.js';
9
+ import rename from './operations/rename.js';
10
+ import sign from './operations/sign.js';
11
+ import space from './operations/space.js';
15
12
  import stat from './operations/stat.js';
16
- // Why is this called deleteFSEntry instead of just delete? because delete is
13
+ import symlink from './operations/symlink.js';
14
+ import upload from './operations/upload.js';
15
+ import write from './operations/write.js';
16
+ // Why is this called deleteFSEntry instead of just delete? because delete is
17
17
  // a reserved keyword in javascript
18
- import deleteFSEntry from "./operations/deleteFSEntry.js";
19
18
  import { AdvancedBase } from '../../../../putility/index.js';
20
19
  import FSItem from '../FSItem.js';
20
+ import deleteFSEntry from './operations/deleteFSEntry.js';
21
21
  import getReadURL from './operations/getReadUrl.js';
22
22
 
23
23
  export class PuterJSFileSystemModule extends AdvancedBase {
@@ -39,7 +39,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
39
39
  readdir = readdir;
40
40
  stat = stat;
41
41
 
42
- FSItem = FSItem
42
+ FSItem = FSItem;
43
43
 
44
44
  static NARI_METHODS = {
45
45
  // stat: {
@@ -50,7 +50,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
50
50
  // return svc_fs.filesystem.stat(parameters);
51
51
  // }
52
52
  // },
53
- }
53
+ };
54
54
 
55
55
  /**
56
56
  * Creates a new instance with the given authentication token, API origin, and app ID,
@@ -61,7 +61,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
61
61
  * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
62
62
  * @param {string} appID - ID of the app to use.
63
63
  */
64
- constructor (context) {
64
+ constructor(context) {
65
65
  super();
66
66
  this.authToken = context.authToken;
67
67
  this.APIOrigin = context.APIOrigin;
@@ -81,7 +81,6 @@ export class PuterJSFileSystemModule extends AdvancedBase {
81
81
  });
82
82
  }
83
83
 
84
-
85
84
  /**
86
85
  * Initializes the socket connection to the server using the current API origin.
87
86
  * If a socket connection already exists, it disconnects it before creating a new one.
@@ -92,17 +91,21 @@ export class PuterJSFileSystemModule extends AdvancedBase {
92
91
  * @returns {void}
93
92
  */
94
93
  initializeSocket() {
95
- if (this.socket) {
94
+ if ( globalThis.puter.env === 'nodejs' ){
95
+ return;
96
+ }
97
+ if ( this.socket ) {
96
98
  this.socket.disconnect();
97
99
  }
98
100
 
99
101
  this.socket = io(this.APIOrigin, {
100
102
  auth: {
101
103
  auth_token: this.authToken,
102
- }
104
+ },
103
105
  });
104
106
 
105
107
  this.bindSocketEvents();
108
+
106
109
  }
107
110
 
108
111
  bindSocketEvents() {
@@ -117,16 +120,20 @@ export class PuterJSFileSystemModule extends AdvancedBase {
117
120
  this.socket.on('item.moved', (item) => {
118
121
  // todo: NAIVE PURGE
119
122
  puter._cache.flushall();
120
- });
123
+ });
121
124
 
122
125
  this.socket.on('connect', () => {
123
- if(puter.debugMode)
126
+ if ( puter.debugMode )
127
+ {
124
128
  console.log('FileSystem Socket: Connected', this.socket.id);
129
+ }
125
130
  });
126
131
 
127
132
  this.socket.on('disconnect', () => {
128
- if(puter.debugMode)
133
+ if ( puter.debugMode )
134
+ {
129
135
  console.log('FileSystem Socket: Disconnected');
136
+ }
130
137
 
131
138
  // todo: NAIVE PURGE
132
139
  // purge cache on disconnect since we may have become out of sync
@@ -134,28 +141,38 @@ export class PuterJSFileSystemModule extends AdvancedBase {
134
141
  });
135
142
 
136
143
  this.socket.on('reconnect', (attempt) => {
137
- if(puter.debugMode)
144
+ if ( puter.debugMode )
145
+ {
138
146
  console.log('FileSystem Socket: Reconnected', this.socket.id);
147
+ }
139
148
  });
140
149
 
141
150
  this.socket.on('reconnect_attempt', (attempt) => {
142
- if(puter.debugMode)
151
+ if ( puter.debugMode )
152
+ {
143
153
  console.log('FileSystem Socket: Reconnection Attemps', attempt);
154
+ }
144
155
  });
145
156
 
146
157
  this.socket.on('reconnect_error', (error) => {
147
- if(puter.debugMode)
158
+ if ( puter.debugMode )
159
+ {
148
160
  console.log('FileSystem Socket: Reconnection Error', error);
161
+ }
149
162
  });
150
163
 
151
164
  this.socket.on('reconnect_failed', () => {
152
- if(puter.debugMode)
165
+ if ( puter.debugMode )
166
+ {
153
167
  console.log('FileSystem Socket: Reconnection Failed');
168
+ }
154
169
  });
155
170
 
156
171
  this.socket.on('error', (error) => {
157
- if(puter.debugMode)
172
+ if ( puter.debugMode )
173
+ {
158
174
  console.error('FileSystem Socket Error:', error);
175
+ }
159
176
  });
160
177
  }
161
178
 
@@ -166,7 +183,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
166
183
  * @memberof [FileSystem]
167
184
  * @returns {void}
168
185
  */
169
- setAuthToken (authToken) {
186
+ setAuthToken(authToken) {
170
187
  this.authToken = authToken;
171
188
  // reset socket
172
189
  this.initializeSocket();
@@ -174,12 +191,12 @@ export class PuterJSFileSystemModule extends AdvancedBase {
174
191
 
175
192
  /**
176
193
  * Sets the API origin and resets the socket connection with the updated API origin.
177
- *
194
+ *
178
195
  * @param {string} APIOrigin - The new API origin.
179
196
  * @memberof [Apps]
180
197
  * @returns {void}
181
198
  */
182
- setAPIOrigin (APIOrigin) {
199
+ setAPIOrigin(APIOrigin) {
183
200
  this.APIOrigin = APIOrigin;
184
201
  // reset socket
185
202
  this.initializeSocket();
@@ -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
  }