@heyputer/puter.js 1.0.1 → 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.
Files changed (74) hide show
  1. package/README.md +14 -43
  2. package/index.d.ts +479 -0
  3. package/package.json +13 -3
  4. package/APACHE_LICENSE.txt +0 -201
  5. package/doc/devlog.md +0 -49
  6. package/src/bg.png +0 -0
  7. package/src/bg.webp +0 -0
  8. package/src/lib/APICallLogger.js +0 -110
  9. package/src/lib/EventListener.js +0 -51
  10. package/src/lib/RequestError.js +0 -6
  11. package/src/lib/filesystem/APIFS.js +0 -73
  12. package/src/lib/filesystem/CacheFS.js +0 -243
  13. package/src/lib/filesystem/PostMessageFS.js +0 -40
  14. package/src/lib/filesystem/definitions.js +0 -39
  15. package/src/lib/path.js +0 -509
  16. package/src/lib/polyfills/localStorage.js +0 -92
  17. package/src/lib/polyfills/xhrshim.js +0 -233
  18. package/src/lib/socket.io/socket.io.esm.min.js +0 -7
  19. package/src/lib/socket.io/socket.io.esm.min.js.map +0 -1
  20. package/src/lib/socket.io/socket.io.js +0 -4385
  21. package/src/lib/socket.io/socket.io.js.map +0 -1
  22. package/src/lib/socket.io/socket.io.min.js +0 -7
  23. package/src/lib/socket.io/socket.io.min.js.map +0 -1
  24. package/src/lib/socket.io/socket.io.msgpack.min.js +0 -7
  25. package/src/lib/socket.io/socket.io.msgpack.min.js.map +0 -1
  26. package/src/lib/utils.js +0 -620
  27. package/src/lib/xdrpc.js +0 -104
  28. package/src/modules/AI.js +0 -680
  29. package/src/modules/Apps.js +0 -215
  30. package/src/modules/Auth.js +0 -171
  31. package/src/modules/Debug.js +0 -39
  32. package/src/modules/Drivers.js +0 -278
  33. package/src/modules/FSItem.js +0 -139
  34. package/src/modules/FileSystem/index.js +0 -187
  35. package/src/modules/FileSystem/operations/copy.js +0 -64
  36. package/src/modules/FileSystem/operations/deleteFSEntry.js +0 -59
  37. package/src/modules/FileSystem/operations/getReadUrl.js +0 -42
  38. package/src/modules/FileSystem/operations/mkdir.js +0 -62
  39. package/src/modules/FileSystem/operations/move.js +0 -75
  40. package/src/modules/FileSystem/operations/read.js +0 -46
  41. package/src/modules/FileSystem/operations/readdir.js +0 -102
  42. package/src/modules/FileSystem/operations/rename.js +0 -58
  43. package/src/modules/FileSystem/operations/sign.js +0 -103
  44. package/src/modules/FileSystem/operations/space.js +0 -40
  45. package/src/modules/FileSystem/operations/stat.js +0 -95
  46. package/src/modules/FileSystem/operations/symlink.js +0 -55
  47. package/src/modules/FileSystem/operations/upload.js +0 -440
  48. package/src/modules/FileSystem/operations/write.js +0 -65
  49. package/src/modules/FileSystem/utils/getAbsolutePathForApp.js +0 -21
  50. package/src/modules/Hosting.js +0 -138
  51. package/src/modules/KV.js +0 -301
  52. package/src/modules/OS.js +0 -95
  53. package/src/modules/Perms.js +0 -109
  54. package/src/modules/PuterDialog.js +0 -481
  55. package/src/modules/Threads.js +0 -75
  56. package/src/modules/UI.js +0 -1555
  57. package/src/modules/Util.js +0 -38
  58. package/src/modules/Workers.js +0 -120
  59. package/src/modules/networking/PSocket.js +0 -87
  60. package/src/modules/networking/PTLS.js +0 -100
  61. package/src/modules/networking/PWispHandler.js +0 -89
  62. package/src/modules/networking/parsers.js +0 -157
  63. package/src/modules/networking/requests.js +0 -282
  64. package/src/services/APIAccess.js +0 -46
  65. package/src/services/FSRelay.js +0 -20
  66. package/src/services/Filesystem.js +0 -122
  67. package/src/services/NoPuterYet.js +0 -20
  68. package/src/services/XDIncoming.js +0 -44
  69. package/test/ai.test.js +0 -214
  70. package/test/fs.test.js +0 -798
  71. package/test/index.html +0 -1183
  72. package/test/kv.test.js +0 -548
  73. package/test/txt2speech.test.js +0 -178
  74. package/webpack.config.js +0 -25
@@ -1,139 +0,0 @@
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
- // We add some properties to '_internalProperties' to make it clear
20
- // that they are not meant to be accessed outside of puter.js;
21
- // this permits us to change or remove these properties in the future.
22
- const internalProperties = {};
23
- Object.defineProperty(this, '_internalProperties', {
24
- enumerable: false,
25
- value: internalProperties,
26
- });
27
-
28
- // Currently 'signature' and 'expires' are not provided in 'options',
29
- // but they can be inferred by writeURL or readURL.
30
- internalProperties.signature = options.signature ?? (() => {
31
- const url = new URL(this.writeURL ?? this.readURL);
32
- return url.searchParams.get('signature');
33
- })();
34
- internalProperties.expires = options.expires ?? (() => {
35
- const url = new URL(this.writeURL ?? this.readURL);
36
- return url.searchParams.get('expires');
37
- })();
38
-
39
- // This computed property gives us an object in the format output by
40
- // the `/sign` endpoint, which can be passed to `launch_app` to
41
- // allow apps to open a file in another app or another instance.
42
- Object.defineProperty(internalProperties, 'file_signature', {
43
- get: () => ({
44
- read_url: this.readURL,
45
- write_url: this.writeURL,
46
- metadata_url: this.metadataURL,
47
- fsentry_accessed: this.accessed,
48
- fsentry_modified: this.modified,
49
- fsentry_created: this.created,
50
- fsentry_is_dir: this.isDirectory,
51
- fsentry_size: this.size,
52
- fsentry_name: this.name,
53
- path: this.path,
54
- uid: this.uid,
55
- // /sign outputs another property called "type", but we don't
56
- // have that information here, so it's omitted.
57
- })
58
- });
59
- }
60
-
61
- write = async function(data){
62
- return puter.fs.write(
63
- this.path,
64
- new File([data], this.name),
65
- {
66
- overwrite: true,
67
- dedupeName: false,
68
- },
69
- );
70
- }
71
-
72
- // Watches for changes to the item, and calls the callback function
73
- // with the new data when a change is detected.
74
- watch = function(callback){
75
- // todo - implement
76
- }
77
-
78
- open = function(callback){
79
- // todo - implement
80
- }
81
-
82
- // Set wallpaper
83
- setAsWallpaper = function(options, callback){
84
- // todo - implement
85
- }
86
-
87
- rename = function(new_name){
88
- return puter.fs.rename(this.uid, new_name);
89
- }
90
-
91
- move = function(dest_path, overwrite=false, new_name){
92
- return puter.fs.move(this.path, dest_path, overwrite, new_name);
93
- }
94
-
95
- copy = function(destination_directory, auto_rename=false, overwrite=false){
96
- return puter.fs.copy(this.path, destination_directory, auto_rename, overwrite);
97
- }
98
-
99
- delete = function(){
100
- return puter.fs.delete(this.path);
101
- }
102
-
103
- versions = async function(){
104
- // todo - implement
105
- }
106
-
107
- trash = function(){
108
- // todo make sure puter allows for moving to trash by default
109
- // todo implement trashing
110
- }
111
-
112
- mkdir = async function(name, auto_rename = false){
113
- // Don't proceed if this is not a directory, throw error
114
- if(!this.isDirectory)
115
- throw new Error('mkdir() can only be called on a directory');
116
-
117
- // mkdir
118
- return puter.fs.mkdir(path.join(this.path, name));
119
- }
120
-
121
- metadata = async function(){
122
- // todo - implement
123
- }
124
-
125
- readdir = async function(){
126
- // Don't proceed if this is not a directory, throw error
127
- if(!this.isDirectory)
128
- throw new Error('readdir() can only be called on a directory');
129
-
130
- // readdir
131
- return puter.fs.readdir(this.path);
132
- }
133
-
134
- read = async function(){
135
- return puter.fs.read(this.path);
136
- }
137
- }
138
-
139
- export default FSItem;
@@ -1,187 +0,0 @@
1
- import io from '../../lib/socket.io/socket.io.esm.min.js';
2
-
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';
14
- import readdir from './operations/readdir.js';
15
- import stat from './operations/stat.js';
16
- // Why is this called deleteFSEntry instead of just delete? because delete is
17
- // a reserved keyword in javascript
18
- import deleteFSEntry from "./operations/deleteFSEntry.js";
19
- import { AdvancedBase } from '../../../../putility/index.js';
20
- import FSItem from '../FSItem.js';
21
- import getReadURL from './operations/getReadUrl.js';
22
-
23
- export class PuterJSFileSystemModule extends AdvancedBase {
24
-
25
- space = space;
26
- mkdir = mkdir;
27
- copy = copy;
28
- rename = rename;
29
- upload = upload;
30
- read = read;
31
- // Why is this called deleteFSEntry instead of just delete? because delete is
32
- // a reserved keyword in javascript.
33
- delete = deleteFSEntry;
34
- move = move;
35
- write = write;
36
- sign = sign;
37
- symlink = symlink;
38
- getReadURL = getReadURL;
39
- readdir = readdir;
40
- stat = stat;
41
-
42
- FSItem = FSItem
43
-
44
- static NARI_METHODS = {
45
- // stat: {
46
- // positional: ['path'],
47
- // firstarg_options: true,
48
- // async fn (parameters) {
49
- // const svc_fs = await this.context.services.aget('filesystem');
50
- // return svc_fs.filesystem.stat(parameters);
51
- // }
52
- // },
53
- }
54
-
55
- /**
56
- * Creates a new instance with the given authentication token, API origin, and app ID,
57
- * and connects to the socket.
58
- *
59
- * @class
60
- * @param {string} authToken - Token used to authenticate the user.
61
- * @param {string} APIOrigin - Origin of the API server. Used to build the API endpoint URLs.
62
- * @param {string} appID - ID of the app to use.
63
- */
64
- constructor (context) {
65
- super();
66
- this.authToken = context.authToken;
67
- this.APIOrigin = context.APIOrigin;
68
- this.appID = context.appID;
69
- this.context = context;
70
- // Connect socket.
71
- this.initializeSocket();
72
-
73
- // We need to use `Object.defineProperty` instead of passing
74
- // `authToken` and `APIOrigin` because they will change.
75
- const api_info = {};
76
- Object.defineProperty(api_info, 'authToken', {
77
- get: () => this.authToken,
78
- });
79
- Object.defineProperty(api_info, 'APIOrigin', {
80
- get: () => this.APIOrigin,
81
- });
82
- }
83
-
84
-
85
- /**
86
- * Initializes the socket connection to the server using the current API origin.
87
- * If a socket connection already exists, it disconnects it before creating a new one.
88
- * Sets up various event listeners on the socket to handle different socket events like
89
- * connect, disconnect, reconnect, reconnect_attempt, reconnect_error, reconnect_failed, and error.
90
- *
91
- * @memberof FileSystem
92
- * @returns {void}
93
- */
94
- initializeSocket() {
95
- if (this.socket) {
96
- this.socket.disconnect();
97
- }
98
-
99
- this.socket = io(this.APIOrigin, {
100
- auth: {
101
- auth_token: this.authToken,
102
- }
103
- });
104
-
105
- this.bindSocketEvents();
106
- }
107
-
108
- bindSocketEvents() {
109
- this.socket.on('item.added', (item) => {
110
- // todo: NAIVE PURGE
111
- puter._cache.flushall();
112
- });
113
- this.socket.on('item.renamed', (item) => {
114
- // todo: NAIVE PURGE
115
- puter._cache.flushall();
116
- });
117
- this.socket.on('item.moved', (item) => {
118
- // todo: NAIVE PURGE
119
- puter._cache.flushall();
120
- });
121
-
122
- this.socket.on('connect', () => {
123
- if(puter.debugMode)
124
- console.log('FileSystem Socket: Connected', this.socket.id);
125
- });
126
-
127
- this.socket.on('disconnect', () => {
128
- if(puter.debugMode)
129
- console.log('FileSystem Socket: Disconnected');
130
-
131
- // todo: NAIVE PURGE
132
- // purge cache on disconnect since we may have become out of sync
133
- puter._cache.flushall();
134
- });
135
-
136
- this.socket.on('reconnect', (attempt) => {
137
- if(puter.debugMode)
138
- console.log('FileSystem Socket: Reconnected', this.socket.id);
139
- });
140
-
141
- this.socket.on('reconnect_attempt', (attempt) => {
142
- if(puter.debugMode)
143
- console.log('FileSystem Socket: Reconnection Attemps', attempt);
144
- });
145
-
146
- this.socket.on('reconnect_error', (error) => {
147
- if(puter.debugMode)
148
- console.log('FileSystem Socket: Reconnection Error', error);
149
- });
150
-
151
- this.socket.on('reconnect_failed', () => {
152
- if(puter.debugMode)
153
- console.log('FileSystem Socket: Reconnection Failed');
154
- });
155
-
156
- this.socket.on('error', (error) => {
157
- if(puter.debugMode)
158
- console.error('FileSystem Socket Error:', error);
159
- });
160
- }
161
-
162
- /**
163
- * Sets a new authentication token and resets the socket connection with the updated token.
164
- *
165
- * @param {string} authToken - The new authentication token.
166
- * @memberof [FileSystem]
167
- * @returns {void}
168
- */
169
- setAuthToken (authToken) {
170
- this.authToken = authToken;
171
- // reset socket
172
- this.initializeSocket();
173
- }
174
-
175
- /**
176
- * Sets the API origin and resets the socket connection with the updated API origin.
177
- *
178
- * @param {string} APIOrigin - The new API origin.
179
- * @memberof [Apps]
180
- * @returns {void}
181
- */
182
- setAPIOrigin (APIOrigin) {
183
- this.APIOrigin = APIOrigin;
184
- // reset socket
185
- this.initializeSocket();
186
- }
187
- }
@@ -1,64 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3
-
4
- const copy = 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
- source: args[0],
14
- destination: args[1],
15
- overwrite: args[2]?.overwrite,
16
- new_name: args[2]?.newName || args[2]?.new_name,
17
- create_missing_parents: args[2]?.createMissingParents || args[2]?.create_missing_parents,
18
- new_metadata: args[2]?.newMetadata || args[2]?.new_metadata,
19
- original_client_socket_id: args[2]?.excludeSocketID || args[2]?.original_client_socket_id,
20
- success: args[3],
21
- error: args[4],
22
- // Add more if needed...
23
- };
24
- }
25
-
26
- return new Promise(async (resolve, reject) => {
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
- // convert paths to absolute path
39
- options.source = getAbsolutePathForApp(options.source);
40
- options.destination = getAbsolutePathForApp(options.destination);
41
-
42
- // create xhr object
43
- const xhr = utils.initXhr('/copy', this.APIOrigin, this.authToken);
44
-
45
- // set up event handlers for load and error events
46
- utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
47
-
48
- xhr.send(JSON.stringify({
49
- original_client_socket_id: this.socket.id,
50
- socket_id: this.socket.id,
51
- source: options.source,
52
- destination: options.destination,
53
- overwrite: options.overwrite,
54
- new_name: (options.new_name || options.newName),
55
- // if user is copying an item to where its source is, change the name so there is no conflict
56
- dedupe_name: (options.dedupe_name || options.dedupeName),
57
- }));
58
-
59
- // todo: EXTREMELY NAIVE CACHE PURGE
60
- puter._cache.flushall();
61
- })
62
- }
63
-
64
- export default copy;
@@ -1,59 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3
-
4
- // why is this called deleteFSEntry instead of just delete?
5
- // because delete is a reserved keyword in javascript
6
- const deleteFSEntry = async function(...args) {
7
- let options;
8
-
9
- // If first argument is an object, it's the options
10
- if (typeof args[0] === 'object' && args[0] !== null) {
11
- options = args[0];
12
- }
13
- // Otherwise, we assume separate arguments are provided
14
- else {
15
- options = {
16
- paths: args[0],
17
- recursive: args[1]?.recursive ?? true,
18
- descendantsOnly: args[1]?.descendantsOnly ?? false,
19
- };
20
- }
21
-
22
- // If paths is a string, convert to array
23
- // this is to make it easier for the user to provide a single path without having to wrap it in an array
24
- let paths = options.paths;
25
- if(typeof paths === 'string')
26
- paths = [paths];
27
-
28
- return new Promise(async (resolve, reject) => {
29
- // If auth token is not provided and we are in the web environment,
30
- // try to authenticate with Puter
31
- if(!puter.authToken && puter.env === 'web'){
32
- try{
33
- await puter.ui.authenticateWithPuter();
34
- }catch(e){
35
- // if authentication fails, throw an error
36
- reject('Authentication failed.');
37
- }
38
- }
39
-
40
- // create xhr object
41
- const xhr = utils.initXhr('/delete', this.APIOrigin, this.authToken);
42
-
43
- // set up event handlers for load and error events
44
- utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
45
-
46
- // convert paths to absolute paths
47
- paths = paths.map((path) => {
48
- return getAbsolutePathForApp(path);
49
- })
50
-
51
- xhr.send(JSON.stringify({
52
- paths: paths,
53
- descendants_only: (options.descendants_only || options.descendantsOnly) ?? false,
54
- recursive: options.recursive ?? true,
55
- }));
56
- })
57
- }
58
-
59
- export default deleteFSEntry;
@@ -1,42 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import stat from "./stat.js";
3
-
4
- const getReadURL = async function (path, expiresIn = "24h") {
5
- return new Promise(async (resolve, reject) => {
6
- // If auth token is not provided and we are in the web environment,
7
- // try to authenticate with Puter
8
- if (!puter.authToken && puter.env === 'web') {
9
- try {
10
- await puter.ui.authenticateWithPuter();
11
- } catch (e) {
12
- // if authentication fails, throw an error
13
- reject('Authentication failed.');
14
- }
15
- }
16
- try {
17
- const { uid, is_dir } = (await stat.call(this, path))
18
- if (is_dir) {
19
- reject("Cannot create readUrl for directory");
20
- return;
21
- }
22
-
23
- const xhr = utils.initXhr('/auth/create-access-token', this.APIOrigin, this.authToken);
24
-
25
- utils.setupXhrEventHandlers(xhr, () => {}, () => {}, ({token})=> {
26
- resolve(`${this.APIOrigin}/token-read?uid=${encodeURIComponent(uid)}&token=${encodeURIComponent(token)}`)
27
- }, reject);
28
-
29
- xhr.send(JSON.stringify({
30
- expiresIn,
31
- permissions: [
32
- `fs:${uid}:read`,
33
- ]
34
- }))
35
-
36
- } catch (e) {
37
- reject(e)
38
- }
39
- });
40
- }
41
-
42
- export default getReadURL;
@@ -1,62 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3
- import path from "../../../lib/path.js"
4
-
5
- const mkdir = function (...args) {
6
- let options = {};
7
-
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)) {
10
- // If it's a string followed by an object, it means path then options
11
- if (typeof args[0] === 'string') {
12
- options.path = args[0];
13
- // Merge the options
14
- Object.assign(options, args[1]);
15
- options.success = args[2];
16
- options.error = args[3];
17
- } else {
18
- options = args[0];
19
- }
20
- } else if (typeof args[0] === 'string') {
21
- // it means it's a path then functions (success and optionally error)
22
- options.path = args[0];
23
- options.success = args[1];
24
- options.error = args[2];
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
- // create xhr object
40
- const xhr = utils.initXhr('/mkdir', this.APIOrigin, this.authToken);
41
-
42
- // set up event handlers for load and error events
43
- utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
44
-
45
- options.path = getAbsolutePathForApp(options.path);
46
-
47
- xhr.send(JSON.stringify({
48
- parent: path.dirname(options.path),
49
- path: path.basename(options.path),
50
- overwrite: options.overwrite ?? false,
51
- dedupe_name: (options.rename || options.dedupeName) ?? false,
52
- shortcut_to: options.shortcutTo,
53
- original_client_socket_id: this.socket.id,
54
- create_missing_parents: (options.recursive || options.createMissingParents) ?? false,
55
- }));
56
-
57
- // todo: EXTREMELY NAIVE CACHE PURGE
58
- puter._cache.flushall();
59
- })
60
- }
61
-
62
- export default mkdir;
@@ -1,75 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3
- import stat from "./stat.js"
4
- import path from "../../../lib/path.js"
5
-
6
- const move = function (...args) {
7
- let options;
8
- // If first argument is an object, it's the options
9
- if (typeof args[0] === 'object' && args[0] !== null) {
10
- options = args[0];
11
- } else {
12
- // Otherwise, we assume separate arguments are provided
13
- options = {
14
- source: args[0],
15
- destination: args[1],
16
- overwrite: args[2]?.overwrite,
17
- new_name: args[2]?.newName || args[2]?.new_name,
18
- create_missing_parents: args[2]?.createMissingParents || args[2]?.create_missing_parents,
19
- new_metadata: args[2]?.newMetadata || args[2]?.new_metadata,
20
- original_client_socket_id: args[2]?.excludeSocketID || args[2]?.original_client_socket_id,
21
- };
22
- }
23
-
24
- return new Promise(async (resolve, reject) => {
25
- // If auth token is not provided and we are in the web environment,
26
- // try to authenticate with Puter
27
- if(!puter.authToken && puter.env === 'web'){
28
- try{
29
- await puter.ui.authenticateWithPuter();
30
- }catch(e){
31
- // if authentication fails, throw an error
32
- reject('Authentication failed.');
33
- }
34
- }
35
-
36
- // convert source and destination to absolute path
37
- options.source = getAbsolutePathForApp(options.source);
38
- options.destination = getAbsolutePathForApp(options.destination);
39
-
40
- if (!options.new_name) {
41
- // Handler to check if dest is supposed to be a file or a folder
42
- try {
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 "is not directory" // just a wuick way to just to the catch
46
- }
47
- } catch (e) {
48
- options.new_name = path.basename(options.destination);
49
- options.destination = path.dirname(options.destination);
50
- }
51
- }
52
-
53
- // create xhr object
54
- const xhr = utils.initXhr('/move', this.APIOrigin, this.authToken);
55
-
56
- // set up event handlers for load and error events
57
- utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
58
-
59
- xhr.send(JSON.stringify({
60
- source: options.source,
61
- destination: options.destination,
62
- overwrite: options.overwrite,
63
- new_name: (options.new_name || options.newName),
64
- create_missing_parents: (options.create_missing_parents || options.createMissingParents),
65
- new_metadata: (options.new_metadata || options.newMetadata),
66
- original_client_socket_id: options.excludeSocketID,
67
- }));
68
-
69
- // todo: EXTREMELY NAIVE CACHE PURGE
70
- puter._cache.flushall();
71
-
72
- })
73
- }
74
-
75
- export default move;
@@ -1,46 +0,0 @@
1
- import * as utils from '../../../lib/utils.js';
2
- import getAbsolutePathForApp from '../utils/getAbsolutePathForApp.js';
3
-
4
- const read = 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: typeof args[0] === 'string' ? args[0] : (typeof args[0] === 'object' && args[0] !== null ? args[0].path : args[0]),
14
- ...(typeof(args[1]) === "object" ? args[1]: {
15
- success: args[1],
16
- error: args[2],
17
- })
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
- // convert path to absolute path
34
- options.path = getAbsolutePathForApp(options.path);
35
-
36
- // create xhr object
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
-
39
- // set up event handlers for load and error events
40
- utils.setupXhrEventHandlers(xhr, options.success, options.error, resolve, reject);
41
-
42
- xhr.send();
43
- })
44
- }
45
-
46
- export default read;