From e011df1a2033a9d662da7fea992aafefc26d1e81 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 14 Oct 2024 15:19:18 -0400 Subject: [PATCH] move: PuterAPIFilesystem --- src/puter-js/src/modules/FileSystem/APIFS.js | 70 +++++++++++++++++++ .../src/modules/FileSystem/definitions.js | 68 ------------------ src/puter-js/src/modules/FileSystem/index.js | 3 +- 3 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 src/puter-js/src/modules/FileSystem/APIFS.js diff --git a/src/puter-js/src/modules/FileSystem/APIFS.js b/src/puter-js/src/modules/FileSystem/APIFS.js new file mode 100644 index 000000000..2f5dfce9b --- /dev/null +++ b/src/puter-js/src/modules/FileSystem/APIFS.js @@ -0,0 +1,70 @@ +import * as utils from '../../lib/utils.js'; +import putility from "@heyputer/putility"; +import { TeePromise } from "@heyputer/putility/src/libs/promise"; +import getAbsolutePathForApp from './utils/getAbsolutePathForApp.js'; +import { TFilesystem } from './definitions.js'; + +export class PuterAPIFilesystem extends putility.AdvancedBase { + constructor ({ api_info }) { + super(); + this.api_info = api_info; + } + + static IMPLEMENTS = { + [TFilesystem]: { + stat: async function (options) { + this.ensure_auth_(); + const tp = new TeePromise(); + + const xhr = new utils.initXhr('/stat', this.api_info.APIOrigin, this.api_info.authToken); + utils.setupXhrEventHandlers(xhr, undefined, undefined, + tp.resolve.bind(tp), + tp.reject.bind(tp), + ); + + let dataToSend = {}; + if (options.uid !== undefined) { + dataToSend.uid = options.uid; + } else if (options.path !== undefined) { + // If dirPath is not provided or it's not starting with a slash, it means it's a relative path + // in that case, we need to prepend the app's root directory to it + dataToSend.path = getAbsolutePathForApp(options.path); + } + + dataToSend.return_subdomains = options.returnSubdomains; + dataToSend.return_permissions = options.returnPermissions; + dataToSend.return_versions = options.returnVersions; + dataToSend.return_size = options.returnSize; + + xhr.send(JSON.stringify(dataToSend)); + + return await tp; + }, + readdir: async function (options) { + this.ensure_auth_(); + const tp = new TeePromise(); + + const xhr = new utils.initXhr('/readdir', this.api_info.APIOrigin, this.api_info.authToken); + utils.setupXhrEventHandlers(xhr, undefined, undefined, + tp.resolve.bind(tp), + tp.reject.bind(tp), + ); + + xhr.send(JSON.stringify({path: getAbsolutePathForApp(options.path)})); + + return await tp; + }, + } + } + + ensure_auth_ () { + // TODO: remove reference to global 'puter'; get 'env' via context + if ( ! this.api_info.authToken && puter.env === 'web' ) { + try { + this.ui.authenticateWithPuter(); + } catch (e) { + throw new Error('Authentication failed.'); + } + } + } +} diff --git a/src/puter-js/src/modules/FileSystem/definitions.js b/src/puter-js/src/modules/FileSystem/definitions.js index a9e59de0f..6687d908d 100644 --- a/src/puter-js/src/modules/FileSystem/definitions.js +++ b/src/puter-js/src/modules/FileSystem/definitions.js @@ -1,7 +1,4 @@ -import * as utils from '../../lib/utils.js'; import putility from "@heyputer/putility"; -import { TeePromise } from "@heyputer/putility/src/libs/promise"; -import getAbsolutePathForApp from './utils/getAbsolutePathForApp.js'; export const TFilesystem = 'TFilesystem'; @@ -20,71 +17,6 @@ export const IFilesystem = { }; -export class PuterAPIFilesystem extends putility.AdvancedBase { - constructor ({ api_info }) { - super(); - this.api_info = api_info; - } - - static IMPLEMENTS = { - [TFilesystem]: { - stat: async function (options) { - this.ensure_auth_(); - const tp = new TeePromise(); - - const xhr = new utils.initXhr('/stat', this.api_info.APIOrigin, this.api_info.authToken); - utils.setupXhrEventHandlers(xhr, undefined, undefined, - tp.resolve.bind(tp), - tp.reject.bind(tp), - ); - - let dataToSend = {}; - if (options.uid !== undefined) { - dataToSend.uid = options.uid; - } else if (options.path !== undefined) { - // If dirPath is not provided or it's not starting with a slash, it means it's a relative path - // in that case, we need to prepend the app's root directory to it - dataToSend.path = getAbsolutePathForApp(options.path); - } - - dataToSend.return_subdomains = options.returnSubdomains; - dataToSend.return_permissions = options.returnPermissions; - dataToSend.return_versions = options.returnVersions; - dataToSend.return_size = options.returnSize; - - xhr.send(JSON.stringify(dataToSend)); - - return await tp; - }, - readdir: async function (options) { - this.ensure_auth_(); - const tp = new TeePromise(); - - const xhr = new utils.initXhr('/readdir', this.api_info.APIOrigin, this.api_info.authToken); - utils.setupXhrEventHandlers(xhr, undefined, undefined, - tp.resolve.bind(tp), - tp.reject.bind(tp), - ); - - xhr.send(JSON.stringify({path: getAbsolutePathForApp(options.path)})); - - return await tp; - }, - } - } - - ensure_auth_ () { - // TODO: remove reference to global 'puter'; get 'env' via context - if ( ! this.api_info.authToken && puter.env === 'web' ) { - try { - this.ui.authenticateWithPuter(); - } catch (e) { - throw new Error('Authentication failed.'); - } - } - } -} - export class ProxyFilesystem extends putility.AdvancedBase { static PROPERTIES = { delegate: () => {}, diff --git a/src/puter-js/src/modules/FileSystem/index.js b/src/puter-js/src/modules/FileSystem/index.js index 4c68ad4e8..16c31ac36 100644 --- a/src/puter-js/src/modules/FileSystem/index.js +++ b/src/puter-js/src/modules/FileSystem/index.js @@ -13,9 +13,10 @@ import sign from "./operations/sign.js"; // Why is this called deleteFSEntry instead of just delete? because delete is // a reserved keyword in javascript import deleteFSEntry from "./operations/deleteFSEntry.js"; -import { ProxyFilesystem, PuterAPIFilesystem, TFilesystem } from './definitions.js'; +import { ProxyFilesystem, TFilesystem } from './definitions.js'; import { AdvancedBase } from '../../../../putility/index.js'; import { CachedFilesystem } from './CacheFS.js'; +import { PuterAPIFilesystem } from './APIFS.js'; export class PuterJSFileSystemModule extends AdvancedBase {