@heyputer/puter.js 2.1.4 → 2.1.7
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/dist/puter.cjs +2 -2
- package/index.d.ts +41 -15
- package/package.json +1 -1
- package/src/index.js +116 -79
- package/src/lib/APICallLogger.js +20 -21
- package/src/lib/EventListener.js +10 -10
- package/src/lib/filesystem/APIFS.js +11 -19
- package/src/lib/filesystem/CacheFS.js +25 -25
- package/src/lib/filesystem/PostMessageFS.js +11 -11
- package/src/lib/filesystem/definitions.js +11 -10
- package/src/lib/path.js +505 -446
- package/src/lib/polyfills/fileReaderPoly.js +40 -0
- package/src/lib/polyfills/localStorage.js +30 -33
- package/src/lib/polyfills/xhrshim.js +206 -207
- package/src/lib/utils.js +160 -151
- package/src/lib/xdrpc.js +9 -9
- package/src/modules/AI.js +473 -292
- package/src/modules/Apps.js +56 -56
- package/src/modules/Auth.js +17 -17
- package/src/modules/Debug.js +1 -1
- package/src/modules/Drivers.js +41 -41
- package/src/modules/FSItem.js +64 -62
- package/src/modules/FileSystem/index.js +22 -23
- package/src/modules/FileSystem/operations/copy.js +7 -7
- package/src/modules/FileSystem/operations/deleteFSEntry.js +14 -12
- package/src/modules/FileSystem/operations/getReadUrl.js +16 -14
- package/src/modules/FileSystem/operations/mkdir.js +11 -11
- package/src/modules/FileSystem/operations/move.js +12 -12
- package/src/modules/FileSystem/operations/read.js +10 -10
- package/src/modules/FileSystem/operations/readdir.js +28 -28
- package/src/modules/FileSystem/operations/rename.js +11 -11
- package/src/modules/FileSystem/operations/sign.js +33 -30
- package/src/modules/FileSystem/operations/space.js +7 -7
- package/src/modules/FileSystem/operations/stat.js +25 -25
- package/src/modules/FileSystem/operations/symlink.js +15 -17
- package/src/modules/FileSystem/operations/upload.js +151 -122
- package/src/modules/FileSystem/operations/write.js +16 -12
- package/src/modules/FileSystem/utils/getAbsolutePathForApp.js +10 -6
- package/src/modules/Hosting.js +29 -29
- package/src/modules/KV.js +23 -23
- package/src/modules/OS.js +15 -15
- package/src/modules/Perms.js +19 -21
- package/src/modules/PuterDialog.js +46 -48
- package/src/modules/Threads.js +17 -20
- package/src/modules/UI.js +156 -156
- package/src/modules/Util.js +3 -3
- package/src/modules/Workers.js +52 -49
- package/src/modules/networking/PSocket.js +38 -38
- package/src/modules/networking/PTLS.js +54 -47
- package/src/modules/networking/PWispHandler.js +49 -47
- package/src/modules/networking/parsers.js +110 -108
- package/src/modules/networking/requests.js +67 -78
- package/src/services/APIAccess.js +9 -9
- package/src/services/FSRelay.js +6 -6
- package/src/services/Filesystem.js +8 -8
- package/src/services/NoPuterYet.js +2 -2
- package/src/services/XDIncoming.js +1 -1
package/src/modules/FSItem.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import path from
|
|
2
|
-
|
|
3
|
-
class FSItem{
|
|
4
|
-
constructor(options){
|
|
5
|
-
this.readURL
|
|
6
|
-
this.writeURL
|
|
7
|
-
this.metadataURL
|
|
8
|
-
this.name
|
|
9
|
-
this.uid
|
|
10
|
-
this.id
|
|
11
|
-
this.uuid
|
|
12
|
-
this.path
|
|
13
|
-
this.size
|
|
14
|
-
this.accessed
|
|
15
|
-
this.modified
|
|
16
|
-
this.created
|
|
17
|
-
this.isDirectory
|
|
18
|
-
|
|
1
|
+
import path from '../lib/path.js';
|
|
2
|
+
|
|
3
|
+
class FSItem {
|
|
4
|
+
constructor (options) {
|
|
5
|
+
this.readURL = options.readURL ?? options.read_url;
|
|
6
|
+
this.writeURL = options.writeURL ?? options.write_url;
|
|
7
|
+
this.metadataURL = options.metadataURL ?? options.metadata_url;
|
|
8
|
+
this.name = options.name ?? options.fsentry_name;
|
|
9
|
+
this.uid = options.uid ?? options.uuid ?? options.fsentry_uid ?? options.fsentry_id ?? options.fsentry_uuid ?? options.id;
|
|
10
|
+
this.id = this.uid;
|
|
11
|
+
this.uuid = this.uid;
|
|
12
|
+
this.path = options.path ?? options.fsentry_path;
|
|
13
|
+
this.size = options.size ?? options.fsentry_size;
|
|
14
|
+
this.accessed = options.accessed ?? options.fsentry_accessed;
|
|
15
|
+
this.modified = options.modified ?? options.fsentry_modified;
|
|
16
|
+
this.created = options.created ?? options.fsentry_created;
|
|
17
|
+
this.isDirectory = (options.isDirectory || options.is_dir || options.fsentry_is_dir) ? true : false;
|
|
18
|
+
|
|
19
19
|
// We add some properties to '_internalProperties' to make it clear
|
|
20
20
|
// that they are not meant to be accessed outside of puter.js;
|
|
21
21
|
// this permits us to change or remove these properties in the future.
|
|
@@ -24,7 +24,7 @@ class FSItem{
|
|
|
24
24
|
enumerable: false,
|
|
25
25
|
value: internalProperties,
|
|
26
26
|
});
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
// Currently 'signature' and 'expires' are not provided in 'options',
|
|
29
29
|
// but they can be inferred by writeURL or readURL.
|
|
30
30
|
internalProperties.signature = options.signature ?? (() => {
|
|
@@ -35,7 +35,7 @@ class FSItem{
|
|
|
35
35
|
const url = new URL(this.writeURL ?? this.readURL);
|
|
36
36
|
return url.searchParams.get('expires');
|
|
37
37
|
})();
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
// This computed property gives us an object in the format output by
|
|
40
40
|
// the `/sign` endpoint, which can be passed to `launch_app` to
|
|
41
41
|
// allow apps to open a file in another app or another instance.
|
|
@@ -54,86 +54,88 @@ class FSItem{
|
|
|
54
54
|
uid: this.uid,
|
|
55
55
|
// /sign outputs another property called "type", but we don't
|
|
56
56
|
// have that information here, so it's omitted.
|
|
57
|
-
})
|
|
57
|
+
}),
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
write = async function(data){
|
|
62
|
-
return puter.fs.write(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
);
|
|
70
|
-
}
|
|
60
|
+
|
|
61
|
+
write = async function (data) {
|
|
62
|
+
return puter.fs.write(this.path,
|
|
63
|
+
new File([data], this.name),
|
|
64
|
+
{
|
|
65
|
+
overwrite: true,
|
|
66
|
+
dedupeName: false,
|
|
67
|
+
});
|
|
68
|
+
};
|
|
71
69
|
|
|
72
70
|
// Watches for changes to the item, and calls the callback function
|
|
73
71
|
// with the new data when a change is detected.
|
|
74
|
-
watch = function(callback){
|
|
72
|
+
watch = function (callback) {
|
|
75
73
|
// todo - implement
|
|
76
|
-
}
|
|
74
|
+
};
|
|
77
75
|
|
|
78
|
-
open = function(callback){
|
|
76
|
+
open = function (callback) {
|
|
79
77
|
// todo - implement
|
|
80
|
-
}
|
|
78
|
+
};
|
|
81
79
|
|
|
82
80
|
// Set wallpaper
|
|
83
|
-
setAsWallpaper = function(options, callback){
|
|
81
|
+
setAsWallpaper = function (options, callback) {
|
|
84
82
|
// todo - implement
|
|
85
|
-
}
|
|
83
|
+
};
|
|
86
84
|
|
|
87
|
-
rename = function(new_name){
|
|
85
|
+
rename = function (new_name) {
|
|
88
86
|
return puter.fs.rename(this.uid, new_name);
|
|
89
|
-
}
|
|
87
|
+
};
|
|
90
88
|
|
|
91
|
-
move = function(dest_path, overwrite=false, new_name){
|
|
89
|
+
move = function (dest_path, overwrite = false, new_name) {
|
|
92
90
|
return puter.fs.move(this.path, dest_path, overwrite, new_name);
|
|
93
|
-
}
|
|
91
|
+
};
|
|
94
92
|
|
|
95
|
-
copy = function(destination_directory, auto_rename=false, overwrite=false){
|
|
93
|
+
copy = function (destination_directory, auto_rename = false, overwrite = false) {
|
|
96
94
|
return puter.fs.copy(this.path, destination_directory, auto_rename, overwrite);
|
|
97
|
-
}
|
|
95
|
+
};
|
|
98
96
|
|
|
99
|
-
delete = function(){
|
|
97
|
+
delete = function () {
|
|
100
98
|
return puter.fs.delete(this.path);
|
|
101
|
-
}
|
|
99
|
+
};
|
|
102
100
|
|
|
103
|
-
versions = async function(){
|
|
101
|
+
versions = async function () {
|
|
104
102
|
// todo - implement
|
|
105
|
-
}
|
|
103
|
+
};
|
|
106
104
|
|
|
107
|
-
trash = function(){
|
|
105
|
+
trash = function () {
|
|
108
106
|
// todo make sure puter allows for moving to trash by default
|
|
109
107
|
// todo implement trashing
|
|
110
|
-
}
|
|
108
|
+
};
|
|
111
109
|
|
|
112
|
-
mkdir = async function(name, auto_rename = false){
|
|
110
|
+
mkdir = async function (name, auto_rename = false) {
|
|
113
111
|
// Don't proceed if this is not a directory, throw error
|
|
114
|
-
if(!this.isDirectory)
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
if ( ! this.isDirectory )
|
|
113
|
+
{
|
|
114
|
+
throw new Error('mkdir() can only be called on a directory');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
117
|
// mkdir
|
|
118
118
|
return puter.fs.mkdir(path.join(this.path, name));
|
|
119
|
-
}
|
|
119
|
+
};
|
|
120
120
|
|
|
121
|
-
metadata = async function(){
|
|
121
|
+
metadata = async function () {
|
|
122
122
|
// todo - implement
|
|
123
|
-
}
|
|
123
|
+
};
|
|
124
124
|
|
|
125
|
-
readdir = async function(){
|
|
125
|
+
readdir = async function () {
|
|
126
126
|
// Don't proceed if this is not a directory, throw error
|
|
127
|
-
if(!this.isDirectory)
|
|
127
|
+
if ( ! this.isDirectory )
|
|
128
|
+
{
|
|
128
129
|
throw new Error('readdir() can only be called on a directory');
|
|
130
|
+
}
|
|
129
131
|
|
|
130
132
|
// readdir
|
|
131
133
|
return puter.fs.readdir(this.path);
|
|
132
|
-
}
|
|
134
|
+
};
|
|
133
135
|
|
|
134
|
-
read = async function(){
|
|
136
|
+
read = async function () {
|
|
135
137
|
return puter.fs.read(this.path);
|
|
136
|
-
}
|
|
138
|
+
};
|
|
137
139
|
}
|
|
138
140
|
|
|
139
141
|
export default FSItem;
|
|
@@ -3,7 +3,7 @@ import * as utils from '../../lib/utils.js';
|
|
|
3
3
|
import path from '../../lib/path.js';
|
|
4
4
|
|
|
5
5
|
// Constants
|
|
6
|
-
//
|
|
6
|
+
//
|
|
7
7
|
// The last valid time of the local cache.
|
|
8
8
|
const LAST_VALID_TS = 'last_valid_ts';
|
|
9
9
|
|
|
@@ -27,7 +27,6 @@ import FSItem from '../FSItem.js';
|
|
|
27
27
|
import deleteFSEntry from './operations/deleteFSEntry.js';
|
|
28
28
|
import getReadURL from './operations/getReadUrl.js';
|
|
29
29
|
|
|
30
|
-
|
|
31
30
|
export class PuterJSFileSystemModule extends AdvancedBase {
|
|
32
31
|
|
|
33
32
|
space = space;
|
|
@@ -69,7 +68,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
69
68
|
* @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
|
|
70
69
|
* @param {string} appID - ID of the app to use.
|
|
71
70
|
*/
|
|
72
|
-
constructor(context) {
|
|
71
|
+
constructor (context) {
|
|
73
72
|
super();
|
|
74
73
|
this.authToken = context.authToken;
|
|
75
74
|
this.APIOrigin = context.APIOrigin;
|
|
@@ -99,7 +98,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
99
98
|
* @memberof FileSystem
|
|
100
99
|
* @returns {void}
|
|
101
100
|
*/
|
|
102
|
-
initializeSocket() {
|
|
101
|
+
initializeSocket () {
|
|
103
102
|
if ( this.socket ) {
|
|
104
103
|
this.socket.disconnect();
|
|
105
104
|
}
|
|
@@ -114,7 +113,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
114
113
|
this.bindSocketEvents();
|
|
115
114
|
}
|
|
116
115
|
|
|
117
|
-
bindSocketEvents() {
|
|
116
|
+
bindSocketEvents () {
|
|
118
117
|
// this.socket.on('cache.updated', (msg) => {
|
|
119
118
|
// // check original_client_socket_id and if it matches this.socket.id, don't post update
|
|
120
119
|
// if (msg.original_client_socket_id !== this.socket.id) {
|
|
@@ -135,11 +134,11 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
135
134
|
|
|
136
135
|
this.socket.on('item.added', (item) => {
|
|
137
136
|
// remove readdir cache for parent
|
|
138
|
-
puter._cache.del(
|
|
139
|
-
console.log(
|
|
137
|
+
puter._cache.del(`readdir:${ path.dirname(item.path)}`);
|
|
138
|
+
console.log(`deleted cache for readdir:${ path.dirname(item.path)}`);
|
|
140
139
|
// remove item cache for parent directory
|
|
141
|
-
puter._cache.del(
|
|
142
|
-
console.log(
|
|
140
|
+
puter._cache.del(`item:${ path.dirname(item.path)}`);
|
|
141
|
+
console.log(`deleted cache for item:${ path.dirname(item.path)}`);
|
|
143
142
|
});
|
|
144
143
|
|
|
145
144
|
this.socket.on('item.updated', (item) => {
|
|
@@ -209,11 +208,11 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
209
208
|
* @memberof [FileSystem]
|
|
210
209
|
* @returns {void}
|
|
211
210
|
*/
|
|
212
|
-
setAuthToken(authToken) {
|
|
211
|
+
setAuthToken (authToken) {
|
|
213
212
|
this.authToken = authToken;
|
|
214
213
|
|
|
215
214
|
// Check cache timestamp and purge if needed (only in GUI environment)
|
|
216
|
-
if (this.context.env === 'gui') {
|
|
215
|
+
if ( this.context.env === 'gui' ) {
|
|
217
216
|
this.checkCacheAndPurge();
|
|
218
217
|
// Start background task to update LAST_VALID_TS every 1 second
|
|
219
218
|
this.startCacheUpdateTimer();
|
|
@@ -230,7 +229,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
230
229
|
* @memberof [Apps]
|
|
231
230
|
* @returns {void}
|
|
232
231
|
*/
|
|
233
|
-
setAPIOrigin(APIOrigin) {
|
|
232
|
+
setAPIOrigin (APIOrigin) {
|
|
234
233
|
this.APIOrigin = APIOrigin;
|
|
235
234
|
// reset socket
|
|
236
235
|
this.initializeSocket();
|
|
@@ -242,7 +241,7 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
242
241
|
* @memberof PuterJSFileSystemModule
|
|
243
242
|
* @returns {void}
|
|
244
243
|
*/
|
|
245
|
-
invalidateCache() {
|
|
244
|
+
invalidateCache () {
|
|
246
245
|
// Action: Update last valid time
|
|
247
246
|
// Set to 0, which means the cache is not up to date.
|
|
248
247
|
localStorage.setItem(LAST_VALID_TS, '0');
|
|
@@ -255,10 +254,10 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
255
254
|
* @memberof PuterJSFileSystemModule
|
|
256
255
|
* @returns {Promise<number>} The timestamp from the server
|
|
257
256
|
*/
|
|
258
|
-
async getCacheTimestamp() {
|
|
257
|
+
async getCacheTimestamp () {
|
|
259
258
|
return new Promise((resolve, reject) => {
|
|
260
259
|
const xhr = utils.initXhr('/cache/last-change-timestamp', this.APIOrigin, this.authToken, 'get', 'application/json');
|
|
261
|
-
|
|
260
|
+
|
|
262
261
|
// set up event handlers for load and error events
|
|
263
262
|
utils.setupXhrEventHandlers(xhr, undefined, undefined, async (result) => {
|
|
264
263
|
try {
|
|
@@ -280,18 +279,18 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
280
279
|
* @memberof PuterJSFileSystemModule
|
|
281
280
|
* @returns {void}
|
|
282
281
|
*/
|
|
283
|
-
async checkCacheAndPurge() {
|
|
282
|
+
async checkCacheAndPurge () {
|
|
284
283
|
try {
|
|
285
284
|
const serverTimestamp = await this.getCacheTimestamp();
|
|
286
285
|
const localValidTs = parseInt(localStorage.getItem(LAST_VALID_TS)) || 0;
|
|
287
|
-
|
|
288
|
-
if (serverTimestamp - localValidTs > 2000) {
|
|
286
|
+
|
|
287
|
+
if ( serverTimestamp - localValidTs > 2000 ) {
|
|
289
288
|
console.log('Cache is not up to date, purging cache');
|
|
290
289
|
// Server has newer data, purge local cache
|
|
291
290
|
puter._cache.flushall();
|
|
292
291
|
localStorage.setItem(LAST_VALID_TS, '0');
|
|
293
292
|
}
|
|
294
|
-
} catch (error) {
|
|
293
|
+
} catch ( error ) {
|
|
295
294
|
// If we can't get the server timestamp, silently fail
|
|
296
295
|
// This ensures the socket initialization doesn't break
|
|
297
296
|
console.error('Error checking cache timestamp:', error);
|
|
@@ -305,8 +304,8 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
305
304
|
* @memberof PuterJSFileSystemModule
|
|
306
305
|
* @returns {void}
|
|
307
306
|
*/
|
|
308
|
-
startCacheUpdateTimer() {
|
|
309
|
-
if (this.context.env !== 'gui') {
|
|
307
|
+
startCacheUpdateTimer () {
|
|
308
|
+
if ( this.context.env !== 'gui' ) {
|
|
310
309
|
return;
|
|
311
310
|
}
|
|
312
311
|
|
|
@@ -325,8 +324,8 @@ export class PuterJSFileSystemModule extends AdvancedBase {
|
|
|
325
324
|
* @memberof PuterJSFileSystemModule
|
|
326
325
|
* @returns {void}
|
|
327
326
|
*/
|
|
328
|
-
stopCacheUpdateTimer() {
|
|
329
|
-
if (this.cacheUpdateTimer) {
|
|
327
|
+
stopCacheUpdateTimer () {
|
|
328
|
+
if ( this.cacheUpdateTimer ) {
|
|
330
329
|
clearInterval(this.cacheUpdateTimer);
|
|
331
330
|
this.cacheUpdateTimer = null;
|
|
332
331
|
}
|
|
@@ -5,7 +5,7 @@ const copy = function (...args) {
|
|
|
5
5
|
let options;
|
|
6
6
|
|
|
7
7
|
// If first argument is an object, it's the options
|
|
8
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
8
|
+
if ( typeof args[0] === 'object' && args[0] !== null ) {
|
|
9
9
|
options = args[0];
|
|
10
10
|
} else {
|
|
11
11
|
// Otherwise, we assume separate arguments are provided
|
|
@@ -24,12 +24,12 @@ const copy = function (...args) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
return new Promise(async (resolve, reject) => {
|
|
27
|
-
// If auth token is not provided and we are in the web environment,
|
|
27
|
+
// If auth token is not provided and we are in the web environment,
|
|
28
28
|
// try to authenticate with Puter
|
|
29
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
30
|
-
try{
|
|
29
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
30
|
+
try {
|
|
31
31
|
await puter.ui.authenticateWithPuter();
|
|
32
|
-
}catch(e){
|
|
32
|
+
} catch (e) {
|
|
33
33
|
// if authentication fails, throw an error
|
|
34
34
|
reject('Authentication failed.');
|
|
35
35
|
}
|
|
@@ -55,7 +55,7 @@ const copy = function (...args) {
|
|
|
55
55
|
// if user is copying an item to where its source is, change the name so there is no conflict
|
|
56
56
|
dedupe_name: (options.dedupe_name || options.dedupeName),
|
|
57
57
|
}));
|
|
58
|
-
})
|
|
59
|
-
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
60
|
|
|
61
61
|
export default copy;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import * as utils from '../../../lib/utils.js';
|
|
2
2
|
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
3
3
|
|
|
4
|
-
// why is this called deleteFSEntry instead of just delete?
|
|
4
|
+
// why is this called deleteFSEntry instead of just delete?
|
|
5
5
|
// because delete is a reserved keyword in javascript
|
|
6
|
-
const deleteFSEntry = async function(...args) {
|
|
6
|
+
const deleteFSEntry = async function (...args) {
|
|
7
7
|
let options;
|
|
8
8
|
|
|
9
9
|
// If first argument is an object, it's the options
|
|
10
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
10
|
+
if ( typeof args[0] === 'object' && args[0] !== null ) {
|
|
11
11
|
options = args[0];
|
|
12
|
-
}
|
|
12
|
+
}
|
|
13
13
|
// Otherwise, we assume separate arguments are provided
|
|
14
14
|
else {
|
|
15
15
|
options = {
|
|
@@ -22,16 +22,18 @@ const deleteFSEntry = async function(...args) {
|
|
|
22
22
|
// If paths is a string, convert to array
|
|
23
23
|
// this is to make it easier for the user to provide a single path without having to wrap it in an array
|
|
24
24
|
let paths = options.paths;
|
|
25
|
-
if(typeof paths === 'string')
|
|
25
|
+
if ( typeof paths === 'string' )
|
|
26
|
+
{
|
|
26
27
|
paths = [paths];
|
|
28
|
+
}
|
|
27
29
|
|
|
28
30
|
return new Promise(async (resolve, reject) => {
|
|
29
|
-
// If auth token is not provided and we are in the web environment,
|
|
31
|
+
// If auth token is not provided and we are in the web environment,
|
|
30
32
|
// try to authenticate with Puter
|
|
31
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
32
|
-
try{
|
|
33
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
34
|
+
try {
|
|
33
35
|
await puter.ui.authenticateWithPuter();
|
|
34
|
-
}catch(e){
|
|
36
|
+
} catch (e) {
|
|
35
37
|
// if authentication fails, throw an error
|
|
36
38
|
reject('Authentication failed.');
|
|
37
39
|
}
|
|
@@ -46,14 +48,14 @@ const deleteFSEntry = async function(...args) {
|
|
|
46
48
|
// convert paths to absolute paths
|
|
47
49
|
paths = paths.map((path) => {
|
|
48
50
|
return getAbsolutePathForApp(path);
|
|
49
|
-
})
|
|
51
|
+
});
|
|
50
52
|
|
|
51
53
|
xhr.send(JSON.stringify({
|
|
52
54
|
paths: paths,
|
|
53
55
|
descendants_only: (options.descendants_only || options.descendantsOnly) ?? false,
|
|
54
56
|
recursive: options.recursive ?? true,
|
|
55
57
|
}));
|
|
56
|
-
})
|
|
57
|
-
}
|
|
58
|
+
});
|
|
59
|
+
};
|
|
58
60
|
|
|
59
61
|
export default deleteFSEntry;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as utils from '../../../lib/utils.js';
|
|
2
|
-
import stat from
|
|
2
|
+
import stat from './stat.js';
|
|
3
3
|
|
|
4
|
-
const getReadURL = async function (path, expiresIn =
|
|
4
|
+
const getReadURL = async function (path, expiresIn = '24h') {
|
|
5
5
|
return new Promise(async (resolve, reject) => {
|
|
6
|
-
// If auth token is not provided and we are in the web environment,
|
|
6
|
+
// If auth token is not provided and we are in the web environment,
|
|
7
7
|
// try to authenticate with Puter
|
|
8
|
-
if (!puter.authToken && puter.env === 'web') {
|
|
8
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
9
9
|
try {
|
|
10
10
|
await puter.ui.authenticateWithPuter();
|
|
11
11
|
} catch (e) {
|
|
@@ -14,29 +14,31 @@ const getReadURL = async function (path, expiresIn = "24h") {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
try {
|
|
17
|
-
const { uid, is_dir } = (await stat.call(this, path))
|
|
18
|
-
if (is_dir) {
|
|
19
|
-
reject(
|
|
17
|
+
const { uid, is_dir } = (await stat.call(this, path));
|
|
18
|
+
if ( is_dir ) {
|
|
19
|
+
reject('Cannot create readUrl for directory');
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const xhr = utils.initXhr('/auth/create-access-token', this.APIOrigin, this.authToken);
|
|
24
|
-
|
|
25
|
-
utils.setupXhrEventHandlers(xhr, () => {
|
|
26
|
-
|
|
24
|
+
|
|
25
|
+
utils.setupXhrEventHandlers(xhr, () => {
|
|
26
|
+
}, () => {
|
|
27
|
+
}, ({ token }) => {
|
|
28
|
+
resolve(`${this.APIOrigin}/token-read?uid=${encodeURIComponent(uid)}&token=${encodeURIComponent(token)}`);
|
|
27
29
|
}, reject);
|
|
28
30
|
|
|
29
31
|
xhr.send(JSON.stringify({
|
|
30
32
|
expiresIn,
|
|
31
33
|
permissions: [
|
|
32
34
|
`fs:${uid}:read`,
|
|
33
|
-
]
|
|
34
|
-
}))
|
|
35
|
+
],
|
|
36
|
+
}));
|
|
35
37
|
|
|
36
38
|
} catch (e) {
|
|
37
|
-
reject(e)
|
|
39
|
+
reject(e);
|
|
38
40
|
}
|
|
39
41
|
});
|
|
40
|
-
}
|
|
42
|
+
};
|
|
41
43
|
|
|
42
44
|
export default getReadURL;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import path from
|
|
1
|
+
import path from '../../../lib/path.js';
|
|
2
2
|
import * as utils from '../../../lib/utils.js';
|
|
3
3
|
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
4
4
|
|
|
@@ -6,9 +6,9 @@ const mkdir = function (...args) {
|
|
|
6
6
|
let options = {};
|
|
7
7
|
|
|
8
8
|
// If first argument is a string and the second is an object, or if the first is an object
|
|
9
|
-
if ((typeof args[0] === 'string' && typeof args[1] === 'object' && !(args[1] instanceof Function)) || (typeof args[0] === 'object' && args[0] !== null)) {
|
|
9
|
+
if ( (typeof args[0] === 'string' && typeof args[1] === 'object' && !(args[1] instanceof Function)) || (typeof args[0] === 'object' && args[0] !== null) ) {
|
|
10
10
|
// If it's a string followed by an object, it means path then options
|
|
11
|
-
if (typeof args[0] === 'string') {
|
|
11
|
+
if ( typeof args[0] === 'string' ) {
|
|
12
12
|
options.path = args[0];
|
|
13
13
|
// Merge the options
|
|
14
14
|
Object.assign(options, args[1]);
|
|
@@ -17,7 +17,7 @@ const mkdir = function (...args) {
|
|
|
17
17
|
} else {
|
|
18
18
|
options = args[0];
|
|
19
19
|
}
|
|
20
|
-
} else if (typeof args[0] === 'string') {
|
|
20
|
+
} else if ( typeof args[0] === 'string' ) {
|
|
21
21
|
// it means it's a path then functions (success and optionally error)
|
|
22
22
|
options.path = args[0];
|
|
23
23
|
options.success = args[1];
|
|
@@ -25,12 +25,12 @@ const mkdir = function (...args) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
return new Promise(async (resolve, reject) => {
|
|
28
|
-
// If auth token is not provided and we are in the web environment,
|
|
28
|
+
// If auth token is not provided and we are in the web environment,
|
|
29
29
|
// try to authenticate with Puter
|
|
30
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
31
|
-
try{
|
|
30
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
31
|
+
try {
|
|
32
32
|
await puter.ui.authenticateWithPuter();
|
|
33
|
-
}catch(e){
|
|
33
|
+
} catch (e) {
|
|
34
34
|
// if authentication fails, throw an error
|
|
35
35
|
reject('Authentication failed.');
|
|
36
36
|
}
|
|
@@ -46,14 +46,14 @@ const mkdir = function (...args) {
|
|
|
46
46
|
|
|
47
47
|
xhr.send(JSON.stringify({
|
|
48
48
|
parent: path.dirname(options.path),
|
|
49
|
-
path: path.basename(options.path),
|
|
49
|
+
path: path.basename(options.path),
|
|
50
50
|
overwrite: options.overwrite ?? false,
|
|
51
51
|
dedupe_name: (options.rename || options.dedupeName) ?? false,
|
|
52
52
|
shortcut_to: options.shortcutTo,
|
|
53
53
|
original_client_socket_id: this.socket.id,
|
|
54
54
|
create_missing_parents: (options.recursive || options.createMissingParents) ?? false,
|
|
55
55
|
}));
|
|
56
|
-
})
|
|
57
|
-
}
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
58
|
|
|
59
59
|
export default mkdir;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import path from
|
|
1
|
+
import path from '../../../lib/path.js';
|
|
2
2
|
import * as utils from '../../../lib/utils.js';
|
|
3
3
|
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
4
|
-
import stat from
|
|
4
|
+
import stat from './stat.js';
|
|
5
5
|
|
|
6
6
|
const move = function (...args) {
|
|
7
7
|
let options;
|
|
8
8
|
// If first argument is an object, it's the options
|
|
9
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
9
|
+
if ( typeof args[0] === 'object' && args[0] !== null ) {
|
|
10
10
|
options = args[0];
|
|
11
11
|
} else {
|
|
12
12
|
// Otherwise, we assume separate arguments are provided
|
|
@@ -22,12 +22,12 @@ const move = function (...args) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
return new Promise(async (resolve, reject) => {
|
|
25
|
-
// If auth token is not provided and we are in the web environment,
|
|
25
|
+
// If auth token is not provided and we are in the web environment,
|
|
26
26
|
// try to authenticate with Puter
|
|
27
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
28
|
-
try{
|
|
27
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
28
|
+
try {
|
|
29
29
|
await puter.ui.authenticateWithPuter();
|
|
30
|
-
}catch(e){
|
|
30
|
+
} catch (e) {
|
|
31
31
|
// if authentication fails, throw an error
|
|
32
32
|
reject('Authentication failed.');
|
|
33
33
|
}
|
|
@@ -37,12 +37,12 @@ const move = function (...args) {
|
|
|
37
37
|
options.source = getAbsolutePathForApp(options.source);
|
|
38
38
|
options.destination = getAbsolutePathForApp(options.destination);
|
|
39
39
|
|
|
40
|
-
if (!options.new_name) {
|
|
40
|
+
if ( ! options.new_name ) {
|
|
41
41
|
// Handler to check if dest is supposed to be a file or a folder
|
|
42
42
|
try {
|
|
43
43
|
const destStats = await stat.bind(this)(options.destination); // this is meant to error if it doesn't exist
|
|
44
|
-
if (!destStats.is_dir) {
|
|
45
|
-
throw
|
|
44
|
+
if ( ! destStats.is_dir ) {
|
|
45
|
+
throw 'is not directory'; // just a wuick way to just to the catch
|
|
46
46
|
}
|
|
47
47
|
} catch (e) {
|
|
48
48
|
options.new_name = path.basename(options.destination);
|
|
@@ -65,7 +65,7 @@ const move = function (...args) {
|
|
|
65
65
|
new_metadata: (options.new_metadata || options.newMetadata),
|
|
66
66
|
original_client_socket_id: options.excludeSocketID,
|
|
67
67
|
}));
|
|
68
|
-
})
|
|
69
|
-
}
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
70
|
|
|
71
71
|
export default move;
|
|
@@ -5,26 +5,26 @@ const read = function (...args) {
|
|
|
5
5
|
let options;
|
|
6
6
|
|
|
7
7
|
// If first argument is an object, it's the options
|
|
8
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
8
|
+
if ( typeof args[0] === 'object' && args[0] !== null ) {
|
|
9
9
|
options = args[0];
|
|
10
10
|
} else {
|
|
11
11
|
// Otherwise, we assume separate arguments are provided
|
|
12
12
|
options = {
|
|
13
13
|
path: typeof args[0] === 'string' ? args[0] : (typeof args[0] === 'object' && args[0] !== null ? args[0].path : args[0]),
|
|
14
|
-
...(typeof(args[1]) ===
|
|
14
|
+
...(typeof (args[1]) === 'object' ? args[1] : {
|
|
15
15
|
success: args[1],
|
|
16
16
|
error: args[2],
|
|
17
|
-
})
|
|
17
|
+
}),
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
return new Promise(async (resolve, reject) => {
|
|
22
|
-
// If auth token is not provided and we are in the web environment,
|
|
22
|
+
// If auth token is not provided and we are in the web environment,
|
|
23
23
|
// try to authenticate with Puter
|
|
24
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
25
|
-
try{
|
|
24
|
+
if ( !puter.authToken && puter.env === 'web' ) {
|
|
25
|
+
try {
|
|
26
26
|
await puter.ui.authenticateWithPuter();
|
|
27
|
-
}catch(e){
|
|
27
|
+
} catch (e) {
|
|
28
28
|
// if authentication fails, throw an error
|
|
29
29
|
reject('Authentication failed.');
|
|
30
30
|
}
|
|
@@ -34,13 +34,13 @@ const read = function (...args) {
|
|
|
34
34
|
options.path = getAbsolutePathForApp(options.path);
|
|
35
35
|
|
|
36
36
|
// create xhr object
|
|
37
|
-
const xhr = utils.initXhr(
|
|
37
|
+
const xhr = utils.initXhr(`/read?${ new URLSearchParams({ file: options.path, ...(options.offset ? { offset: options.offset } : {}), ...(options.byte_count ? { byte_count: options.byte_count } : {}) }).toString()}`, this.APIOrigin, this.authToken, 'get', 'application/json;charset=UTF-8', 'blob');
|
|
38
38
|
|
|
39
39
|
// set up event handlers for load and error events
|
|
40
40
|
utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
|
|
41
41
|
|
|
42
42
|
xhr.send();
|
|
43
|
-
})
|
|
44
|
-
}
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
45
|
|
|
46
46
|
export default read;
|