@heyputer/puter.js 2.0.0 → 2.0.1
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/README.md +3 -1
- package/index.d.ts +479 -0
- package/package.json +15 -4
- package/APACHE_LICENSE.txt +0 -201
- package/doc/devlog.md +0 -49
- package/src/bg.png +0 -0
- package/src/bg.webp +0 -0
- package/src/lib/APICallLogger.js +0 -110
- package/src/lib/EventListener.js +0 -51
- package/src/lib/RequestError.js +0 -6
- package/src/lib/filesystem/APIFS.js +0 -73
- package/src/lib/filesystem/CacheFS.js +0 -243
- package/src/lib/filesystem/PostMessageFS.js +0 -40
- package/src/lib/filesystem/definitions.js +0 -39
- package/src/lib/path.js +0 -509
- package/src/lib/polyfills/localStorage.js +0 -92
- package/src/lib/polyfills/xhrshim.js +0 -233
- package/src/lib/socket.io/socket.io.esm.min.js +0 -7
- package/src/lib/socket.io/socket.io.esm.min.js.map +0 -1
- package/src/lib/socket.io/socket.io.js +0 -4385
- package/src/lib/socket.io/socket.io.js.map +0 -1
- package/src/lib/socket.io/socket.io.min.js +0 -7
- package/src/lib/socket.io/socket.io.min.js.map +0 -1
- package/src/lib/socket.io/socket.io.msgpack.min.js +0 -7
- package/src/lib/socket.io/socket.io.msgpack.min.js.map +0 -1
- package/src/lib/utils.js +0 -620
- package/src/lib/xdrpc.js +0 -104
- package/src/modules/AI.js +0 -680
- package/src/modules/Apps.js +0 -215
- package/src/modules/Auth.js +0 -171
- package/src/modules/Debug.js +0 -39
- package/src/modules/Drivers.js +0 -278
- package/src/modules/FSItem.js +0 -139
- package/src/modules/FileSystem/index.js +0 -187
- package/src/modules/FileSystem/operations/copy.js +0 -64
- package/src/modules/FileSystem/operations/deleteFSEntry.js +0 -59
- package/src/modules/FileSystem/operations/getReadUrl.js +0 -42
- package/src/modules/FileSystem/operations/mkdir.js +0 -62
- package/src/modules/FileSystem/operations/move.js +0 -75
- package/src/modules/FileSystem/operations/read.js +0 -46
- package/src/modules/FileSystem/operations/readdir.js +0 -102
- package/src/modules/FileSystem/operations/rename.js +0 -58
- package/src/modules/FileSystem/operations/sign.js +0 -103
- package/src/modules/FileSystem/operations/space.js +0 -40
- package/src/modules/FileSystem/operations/stat.js +0 -95
- package/src/modules/FileSystem/operations/symlink.js +0 -55
- package/src/modules/FileSystem/operations/upload.js +0 -440
- package/src/modules/FileSystem/operations/write.js +0 -65
- package/src/modules/FileSystem/utils/getAbsolutePathForApp.js +0 -21
- package/src/modules/Hosting.js +0 -138
- package/src/modules/KV.js +0 -301
- package/src/modules/OS.js +0 -95
- package/src/modules/Perms.js +0 -109
- package/src/modules/PuterDialog.js +0 -481
- package/src/modules/Threads.js +0 -75
- package/src/modules/UI.js +0 -1555
- package/src/modules/Util.js +0 -38
- package/src/modules/Workers.js +0 -120
- package/src/modules/networking/PSocket.js +0 -87
- package/src/modules/networking/PTLS.js +0 -100
- package/src/modules/networking/PWispHandler.js +0 -89
- package/src/modules/networking/parsers.js +0 -157
- package/src/modules/networking/requests.js +0 -282
- package/src/services/APIAccess.js +0 -46
- package/src/services/FSRelay.js +0 -20
- package/src/services/Filesystem.js +0 -122
- package/src/services/NoPuterYet.js +0 -20
- package/src/services/XDIncoming.js +0 -44
- package/test/ai.test.js +0 -214
- package/test/fs.test.js +0 -798
- package/test/index.html +0 -1183
- package/test/kv.test.js +0 -548
- package/test/txt2speech.test.js +0 -178
- package/webpack.config.js +0 -25
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import * as utils from '../../../lib/utils.js';
|
|
2
|
-
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
3
|
-
|
|
4
|
-
const readdir = async function (...args) {
|
|
5
|
-
let options;
|
|
6
|
-
|
|
7
|
-
// If first argument is an object, it's the options
|
|
8
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
9
|
-
options = args[0];
|
|
10
|
-
} else {
|
|
11
|
-
// Otherwise, we assume separate arguments are provided
|
|
12
|
-
options = {
|
|
13
|
-
path: args[0],
|
|
14
|
-
success: args[1],
|
|
15
|
-
error: args[2],
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return new Promise(async (resolve, reject) => {
|
|
20
|
-
// consistency levels
|
|
21
|
-
if(!options.consistency){
|
|
22
|
-
options.consistency = 'strong';
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Either path or uid is required
|
|
26
|
-
if(!options.path && !options.uid){
|
|
27
|
-
throw new Error({ code: 'NO_PATH_OR_UID', message: 'Either path or uid must be provided.' });
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Generate cache key based on path or uid
|
|
31
|
-
let cacheKey;
|
|
32
|
-
if(options.path){
|
|
33
|
-
cacheKey = 'readdir:' + options.path;
|
|
34
|
-
}else if(options.uid){
|
|
35
|
-
cacheKey = 'readdir:' + options.uid;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if(options.consistency === 'eventual'){
|
|
39
|
-
// Check cache
|
|
40
|
-
const cachedResult = await puter._cache.get(cacheKey);
|
|
41
|
-
if(cachedResult){
|
|
42
|
-
resolve(cachedResult);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// If auth token is not provided and we are in the web environment,
|
|
48
|
-
// try to authenticate with Puter
|
|
49
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
50
|
-
try{
|
|
51
|
-
await puter.ui.authenticateWithPuter();
|
|
52
|
-
}catch(e){
|
|
53
|
-
// if authentication fails, throw an error
|
|
54
|
-
reject('Authentication failed.');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// create xhr object
|
|
59
|
-
const xhr = utils.initXhr('/readdir', this.APIOrigin, undefined, "post", "text/plain;actually=json");
|
|
60
|
-
|
|
61
|
-
// set up event handlers for load and error events
|
|
62
|
-
utils.setupXhrEventHandlers(xhr, options.success, options.error, async (result) => {
|
|
63
|
-
// Calculate the size of the result for cache eligibility check
|
|
64
|
-
const resultSize = JSON.stringify(result).length;
|
|
65
|
-
|
|
66
|
-
// Cache the result if it's not bigger than MAX_CACHE_SIZE
|
|
67
|
-
const MAX_CACHE_SIZE = 20 * 1024 * 1024;
|
|
68
|
-
const EXPIRE_TIME = 60 * 60; // 1 hour
|
|
69
|
-
|
|
70
|
-
if(resultSize <= MAX_CACHE_SIZE){
|
|
71
|
-
// UPSERT the cache
|
|
72
|
-
await puter._cache.set(cacheKey, result, { EX: EXPIRE_TIME });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// set each individual item's cache
|
|
76
|
-
for(const item of result){
|
|
77
|
-
await puter._cache.set('item:' + item.id, item, { EX: EXPIRE_TIME });
|
|
78
|
-
await puter._cache.set('item:' + item.path, item, { EX: EXPIRE_TIME });
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
resolve(result);
|
|
82
|
-
}, reject);
|
|
83
|
-
|
|
84
|
-
// Build request payload - support both path and uid parameters
|
|
85
|
-
const payload = {
|
|
86
|
-
no_thumbs: options.no_thumbs,
|
|
87
|
-
no_assocs: options.no_assocs,
|
|
88
|
-
auth_token: this.authToken
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
// Add either uid or path to the payload
|
|
92
|
-
if (options.uid) {
|
|
93
|
-
payload.uid = options.uid;
|
|
94
|
-
} else if (options.path) {
|
|
95
|
-
payload.path = getAbsolutePathForApp(options.path);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
xhr.send(JSON.stringify(payload));
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export default readdir;
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import * as utils from '../../../lib/utils.js';
|
|
2
|
-
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
3
|
-
|
|
4
|
-
const rename = function (...args) {
|
|
5
|
-
let options;
|
|
6
|
-
|
|
7
|
-
// If first argument is an object, it's the options
|
|
8
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
9
|
-
options = args[0];
|
|
10
|
-
} else {
|
|
11
|
-
// Otherwise, we assume separate arguments are provided
|
|
12
|
-
options = {
|
|
13
|
-
path: args[0],
|
|
14
|
-
new_name: args[1],
|
|
15
|
-
success: args[2],
|
|
16
|
-
error: args[3],
|
|
17
|
-
// Add more if needed...
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return new Promise(async (resolve, reject) => {
|
|
22
|
-
// If auth token is not provided and we are in the web environment,
|
|
23
|
-
// try to authenticate with Puter
|
|
24
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
25
|
-
try{
|
|
26
|
-
await puter.ui.authenticateWithPuter();
|
|
27
|
-
}catch(e){
|
|
28
|
-
// if authentication fails, throw an error
|
|
29
|
-
reject('Authentication failed.');
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// create xhr object
|
|
34
|
-
const xhr = utils.initXhr('/rename', this.APIOrigin, this.authToken);
|
|
35
|
-
|
|
36
|
-
// set up event handlers for load and error events
|
|
37
|
-
utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
|
|
38
|
-
|
|
39
|
-
let dataToSend = {
|
|
40
|
-
original_client_socket_id: options.excludeSocketID || options.original_client_socket_id,
|
|
41
|
-
new_name: options.new_name || options.newName,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
if (options.uid !== undefined) {
|
|
45
|
-
dataToSend.uid = options.uid;
|
|
46
|
-
} else if (options.path !== undefined) {
|
|
47
|
-
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
|
48
|
-
// in that case, we need to prepend the app's root directory to it
|
|
49
|
-
dataToSend.path = getAbsolutePathForApp(options.path);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
xhr.send(JSON.stringify(dataToSend));
|
|
53
|
-
// todo: EXTREMELY NAIVE CACHE PURGE
|
|
54
|
-
puter._cache.flushall();
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export default rename;
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import * as utils from '../../../lib/utils.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Signs a file system entry or entries and optionally calls a provided callback with the result.
|
|
6
|
-
* If a single item is passed, it is converted into an array.
|
|
7
|
-
* Sends a POST request to the server to sign the items.
|
|
8
|
-
*
|
|
9
|
-
* @param {(Object|Object[])} items - The file system entry or entries to be signed. Can be a single object or an array of objects.
|
|
10
|
-
* @param {function} [callback] - Optional callback function to be invoked with the result of the signing.
|
|
11
|
-
* @returns {(Object|Object[])} If a single item was passed, returns a single object. If multiple items were passed, returns an array of objects.
|
|
12
|
-
* @throws {Error} If the AJAX request fails.
|
|
13
|
-
* @async
|
|
14
|
-
*/
|
|
15
|
-
const sign = function(...args){
|
|
16
|
-
let options;
|
|
17
|
-
|
|
18
|
-
// Otherwise, we assume separate arguments are provided
|
|
19
|
-
options = {
|
|
20
|
-
app_uid: args[0],
|
|
21
|
-
items: args[1],
|
|
22
|
-
success: args[2],
|
|
23
|
-
error: args[3],
|
|
24
|
-
// Add more if needed...
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
return new Promise(async (resolve, reject) => {
|
|
28
|
-
// If auth token is not provided and we are in the web environment,
|
|
29
|
-
// try to authenticate with Puter
|
|
30
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
31
|
-
try{
|
|
32
|
-
await puter.ui.authenticateWithPuter();
|
|
33
|
-
}catch(e){
|
|
34
|
-
// if authentication fails, throw an error
|
|
35
|
-
reject('Authentication failed.');
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
let items = options.items;
|
|
41
|
-
|
|
42
|
-
// if only a single item is passed, convert it to array
|
|
43
|
-
// so that the code below can work with arrays
|
|
44
|
-
if(!Array.isArray(items)){
|
|
45
|
-
items = [items]
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// create xhr object
|
|
49
|
-
const xhr = utils.initXhr('/sign', this.APIOrigin, this.authToken);
|
|
50
|
-
|
|
51
|
-
// response
|
|
52
|
-
xhr.addEventListener('load', async function(e){
|
|
53
|
-
const resp = await utils.parseResponse(this);
|
|
54
|
-
// error
|
|
55
|
-
if(this.status !== 200){
|
|
56
|
-
// if error callback is provided, call it
|
|
57
|
-
if(options.error && typeof options.error === 'function')
|
|
58
|
-
options.error(resp)
|
|
59
|
-
// reject promise
|
|
60
|
-
return reject(resp)
|
|
61
|
-
}
|
|
62
|
-
// success
|
|
63
|
-
else{
|
|
64
|
-
let res = resp;
|
|
65
|
-
let result;
|
|
66
|
-
let token = res.token;
|
|
67
|
-
// if only a single item was passed, return a single object
|
|
68
|
-
if(items.length == 1){
|
|
69
|
-
result = {...(res.signatures[0])};
|
|
70
|
-
}
|
|
71
|
-
// if multiple items were passed, return an array of objects
|
|
72
|
-
else{
|
|
73
|
-
let obj=[];
|
|
74
|
-
for(let i=0; i<res.signatures.length; i++){
|
|
75
|
-
obj.push({...res.signatures[i]});
|
|
76
|
-
}
|
|
77
|
-
result = obj;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// if success callback is provided, call it
|
|
81
|
-
if(options.success && typeof options.success === 'function')
|
|
82
|
-
options.success({token: token, items: result});
|
|
83
|
-
// resolve with success
|
|
84
|
-
return resolve({token: token, items: result});
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
xhr.upload.addEventListener('progress', function(e){
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
// error
|
|
92
|
-
xhr.addEventListener('error', function(e){
|
|
93
|
-
return utils.handle_error(options.error, reject, this);
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
xhr.send(JSON.stringify({
|
|
97
|
-
app_uid: options.app_uid,
|
|
98
|
-
items: items
|
|
99
|
-
}));
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export default sign;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import * as utils from '../../../lib/utils.js';
|
|
2
|
-
|
|
3
|
-
const space = function (...args) {
|
|
4
|
-
let options;
|
|
5
|
-
|
|
6
|
-
// If first argument is an object, it's the options
|
|
7
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
8
|
-
options = args[0];
|
|
9
|
-
} else {
|
|
10
|
-
// Otherwise, we assume separate arguments are provided
|
|
11
|
-
options = {
|
|
12
|
-
success: args[0],
|
|
13
|
-
error: args[1],
|
|
14
|
-
// Add more if needed...
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return new Promise(async (resolve, reject) => {
|
|
19
|
-
// If auth token is not provided and we are in the web environment,
|
|
20
|
-
// try to authenticate with Puter
|
|
21
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
22
|
-
try{
|
|
23
|
-
await puter.ui.authenticateWithPuter();
|
|
24
|
-
}catch(e){
|
|
25
|
-
// if authentication fails, throw an error
|
|
26
|
-
reject('Authentication failed.');
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// create xhr object
|
|
31
|
-
const xhr = utils.initXhr('/df', this.APIOrigin, this.authToken);
|
|
32
|
-
|
|
33
|
-
// set up event handlers for load and error events
|
|
34
|
-
utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
|
|
35
|
-
|
|
36
|
-
xhr.send();
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export default space;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import * as utils from '../../../lib/utils.js';
|
|
2
|
-
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
3
|
-
|
|
4
|
-
const stat = async function (...args) {
|
|
5
|
-
let options;
|
|
6
|
-
|
|
7
|
-
// If first argument is an object, it's the options
|
|
8
|
-
if (typeof args[0] === 'object' && args[0] !== null) {
|
|
9
|
-
options = args[0];
|
|
10
|
-
} else {
|
|
11
|
-
// Otherwise, we assume separate arguments are provided
|
|
12
|
-
options = {
|
|
13
|
-
path: args[0],
|
|
14
|
-
options: typeof args[1] === 'object' ? args[1] : {},
|
|
15
|
-
success: typeof args[1] === 'object' ? args[2] : args[1],
|
|
16
|
-
error: typeof args[1] === 'object' ? args[3] : args[2],
|
|
17
|
-
// Add more if needed...
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return new Promise(async (resolve, reject) => {
|
|
22
|
-
// consistency levels
|
|
23
|
-
if(!options.consistency){
|
|
24
|
-
options.consistency = 'strong';
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// If auth token is not provided and we are in the web environment,
|
|
28
|
-
// try to authenticate with Puter
|
|
29
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
30
|
-
try{
|
|
31
|
-
await puter.ui.authenticateWithPuter();
|
|
32
|
-
}catch(e){
|
|
33
|
-
// if authentication fails, throw an error
|
|
34
|
-
reject('Authentication failed.');
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Generate cache key based on path or uid
|
|
39
|
-
let cacheKey;
|
|
40
|
-
if(options.path){
|
|
41
|
-
cacheKey = 'item:' + options.path;
|
|
42
|
-
}else if(options.uid){
|
|
43
|
-
cacheKey = 'item:' + options.uid;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if(options.consistency === 'eventual' && !options.returnSubdomains && !options.returnPermissions && !options.returnVersions && !options.returnSize){
|
|
47
|
-
// Check cache
|
|
48
|
-
const cachedResult = await puter._cache.get(cacheKey);
|
|
49
|
-
if(cachedResult){
|
|
50
|
-
resolve(cachedResult);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// create xhr object
|
|
56
|
-
const xhr = utils.initXhr('/stat', this.APIOrigin, undefined, "post", "text/plain;actually=json");
|
|
57
|
-
|
|
58
|
-
// set up event handlers for load and error events
|
|
59
|
-
utils.setupXhrEventHandlers(xhr, options.success, options.error, async (result) => {
|
|
60
|
-
// Calculate the size of the result for cache eligibility check
|
|
61
|
-
const resultSize = JSON.stringify(result).length;
|
|
62
|
-
|
|
63
|
-
// Cache the result if it's not bigger than MAX_CACHE_SIZE
|
|
64
|
-
const MAX_CACHE_SIZE = 20 * 1024 * 1024;
|
|
65
|
-
const EXPIRE_TIME = 60 * 60; // 1 hour
|
|
66
|
-
|
|
67
|
-
if(resultSize <= MAX_CACHE_SIZE){
|
|
68
|
-
// UPSERT the cache
|
|
69
|
-
await puter._cache.set('item:' + result.path, result, { EX: EXPIRE_TIME });
|
|
70
|
-
await puter._cache.set('item:' + result.uid, result, { EX: EXPIRE_TIME });
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
resolve(result);
|
|
74
|
-
}, reject);
|
|
75
|
-
|
|
76
|
-
let dataToSend = {};
|
|
77
|
-
if (options.uid !== undefined) {
|
|
78
|
-
dataToSend.uid = options.uid;
|
|
79
|
-
} else if (options.path !== undefined) {
|
|
80
|
-
// If dirPath is not provided or it's not starting with a slash, it means it's a relative path
|
|
81
|
-
// in that case, we need to prepend the app's root directory to it
|
|
82
|
-
dataToSend.path = getAbsolutePathForApp(options.path);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
dataToSend.return_subdomains = options.returnSubdomains;
|
|
86
|
-
dataToSend.return_permissions = options.returnPermissions;
|
|
87
|
-
dataToSend.return_versions = options.returnVersions;
|
|
88
|
-
dataToSend.return_size = options.returnSize;
|
|
89
|
-
dataToSend.auth_token = this.authToken;
|
|
90
|
-
|
|
91
|
-
xhr.send(JSON.stringify(dataToSend));
|
|
92
|
-
})
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export default stat;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
|
|
2
|
-
import pathLib from '../../../lib/path.js';
|
|
3
|
-
|
|
4
|
-
// This only works for absolute symlinks for now
|
|
5
|
-
const symlink = async function (target, linkPath) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// If auth token is not provided and we are in the web environment,
|
|
9
|
-
// try to authenticate with Puter
|
|
10
|
-
if(!puter.authToken && puter.env === 'web'){
|
|
11
|
-
try{
|
|
12
|
-
await puter.ui.authenticateWithPuter();
|
|
13
|
-
}catch(e){
|
|
14
|
-
// if authentication fails, throw an error
|
|
15
|
-
throw 'Authentication failed.';
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// convert path to absolute path
|
|
20
|
-
linkPath = getAbsolutePathForApp(linkPath);
|
|
21
|
-
target = getAbsolutePathForApp(target);
|
|
22
|
-
const name = pathLib.basename(linkPath);
|
|
23
|
-
const linkDir = pathLib.dirname(linkPath)
|
|
24
|
-
|
|
25
|
-
const op =
|
|
26
|
-
{
|
|
27
|
-
op: 'symlink',
|
|
28
|
-
path: linkDir,
|
|
29
|
-
name: name,
|
|
30
|
-
target: target
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const formData = new FormData();
|
|
34
|
-
formData.append('operation', JSON.stringify(op));
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
const response = await fetch(this.APIOrigin + "/batch", {
|
|
38
|
-
method: 'POST',
|
|
39
|
-
headers: { 'Authorization': `Bearer ${puter.authToken}` },
|
|
40
|
-
body: formData
|
|
41
|
-
});
|
|
42
|
-
if (response.status !== 200) {
|
|
43
|
-
const error = await response.text();
|
|
44
|
-
console.error("[symlink] fetch error: ", error);
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.error("[symlink] fetch error: ", e);
|
|
49
|
-
throw e;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export default symlink;
|