From be59f57d684ac417b9d72d833fc663f3ea37c87c Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 17 Mar 2025 13:18:06 -0400 Subject: [PATCH] dev: update puter.js imports to work in cli This is a step towards getting puter.js to run in node instead of just in the browser. In node, all the relative imports need to end with the file extension, and the "type" option in package.json needs to match the import mechanism used. It was also necessary to migrate webpack.config.js to ESM syntax, which I would not have done if the tooling didn't force me to. --- src/puter-js/package.json | 1 + src/puter-js/src/clitest.js | 1 + src/puter-js/src/index.js | 2 +- src/puter-js/src/lib/filesystem/APIFS.js | 2 +- src/puter-js/src/lib/filesystem/CacheFS.js | 6 +++--- src/puter-js/src/lib/filesystem/PostMessageFS.js | 2 +- src/puter-js/src/lib/http.js | 2 +- src/puter-js/src/modules/PuterDialog.js | 2 +- src/puter-js/src/modules/Util.js | 2 +- src/puter-js/src/modules/networking/PTLS.js | 2 +- src/puter-js/src/services/Filesystem.js | 6 +++--- src/puter-js/webpack.config.js | 15 ++++++++++++--- 12 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 src/puter-js/src/clitest.js diff --git a/src/puter-js/package.json b/src/puter-js/package.json index 3fac79ee9..7451ae763 100644 --- a/src/puter-js/package.json +++ b/src/puter-js/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type": "module", "scripts": { "start-server": "npx http-server --cors -c-1", "start-webpack": "webpack && webpack --output-filename puter.dev.js --watch --devtool source-map", diff --git a/src/puter-js/src/clitest.js b/src/puter-js/src/clitest.js new file mode 100644 index 000000000..10ce2935c --- /dev/null +++ b/src/puter-js/src/clitest.js @@ -0,0 +1 @@ +import puter from './index.js'; diff --git a/src/puter-js/src/index.js b/src/puter-js/src/index.js index 7a241f0e0..e4014f2df 100644 --- a/src/puter-js/src/index.js +++ b/src/puter-js/src/index.js @@ -33,7 +33,7 @@ import Perms from './modules/Perms.js'; // (using defaultGUIOrigin breaks locally-hosted apps) const PROD_ORIGIN = 'https://puter.com'; -window.puter = (function() { +export default window.puter = (function() { 'use strict'; class Puter{ diff --git a/src/puter-js/src/lib/filesystem/APIFS.js b/src/puter-js/src/lib/filesystem/APIFS.js index 308a99eb8..baea88b1c 100644 --- a/src/puter-js/src/lib/filesystem/APIFS.js +++ b/src/puter-js/src/lib/filesystem/APIFS.js @@ -1,6 +1,6 @@ import * as utils from '../utils.js'; import putility from "@heyputer/putility"; -import { TeePromise } from "@heyputer/putility/src/libs/promise"; +import { TeePromise } from "@heyputer/putility/src/libs/promise.js"; import getAbsolutePathForApp from '../../modules/FileSystem/utils/getAbsolutePathForApp.js'; import { TFilesystem } from './definitions.js'; diff --git a/src/puter-js/src/lib/filesystem/CacheFS.js b/src/puter-js/src/lib/filesystem/CacheFS.js index 9e90b88cc..f2541d0e1 100644 --- a/src/puter-js/src/lib/filesystem/CacheFS.js +++ b/src/puter-js/src/lib/filesystem/CacheFS.js @@ -1,7 +1,7 @@ import putility from "@heyputer/putility"; -import { RWLock } from "@heyputer/putility/src/libs/promise"; -import { ProxyFilesystem, TFilesystem } from "./definitions"; -import { uuidv4 } from "../utils"; +import { RWLock } from "@heyputer/putility/src/libs/promise.js"; +import { ProxyFilesystem, TFilesystem } from "./definitions.js"; +import { uuidv4 } from "../utils.js"; export const ROOT_UUID = '00000000-0000-0000-0000-000000000000'; const TTL = 5 * 1000; diff --git a/src/puter-js/src/lib/filesystem/PostMessageFS.js b/src/puter-js/src/lib/filesystem/PostMessageFS.js index 74e995b44..00e974d1f 100644 --- a/src/puter-js/src/lib/filesystem/PostMessageFS.js +++ b/src/puter-js/src/lib/filesystem/PostMessageFS.js @@ -1,5 +1,5 @@ import putility from "@heyputer/putility"; -import { TFilesystem } from "./definitions"; +import { TFilesystem } from "./definitions.js"; const example = { "id": "f485f1ba-de07-422c-8c4b-c2da057d4a44", diff --git a/src/puter-js/src/lib/http.js b/src/puter-js/src/lib/http.js index 935f6d810..ec3ac2bb0 100644 --- a/src/puter-js/src/lib/http.js +++ b/src/puter-js/src/lib/http.js @@ -1,5 +1,5 @@ import putility from "@heyputer/putility"; -import EventListener from "./EventListener"; +import EventListener from "./EventListener.js"; // TODO: this inheritance is an anti-pattern; we should use // a trait or mixin for event emitters. diff --git a/src/puter-js/src/modules/PuterDialog.js b/src/puter-js/src/modules/PuterDialog.js index 1c1790318..00a337f9f 100644 --- a/src/puter-js/src/modules/PuterDialog.js +++ b/src/puter-js/src/modules/PuterDialog.js @@ -417,4 +417,4 @@ class PuterDialog extends HTMLElement { } customElements.define('puter-dialog', PuterDialog); -module.exports = PuterDialog; \ No newline at end of file +export default PuterDialog; diff --git a/src/puter-js/src/modules/Util.js b/src/puter-js/src/modules/Util.js index ef7b3e594..f3c5aa525 100644 --- a/src/puter-js/src/modules/Util.js +++ b/src/puter-js/src/modules/Util.js @@ -1,4 +1,4 @@ -import { $SCOPE, CallbackManager, Dehydrator, Hydrator } from "../lib/xdrpc"; +import { $SCOPE, CallbackManager, Dehydrator, Hydrator } from "../lib/xdrpc.js"; /** * The Util module exposes utilities within puter.js itself. diff --git a/src/puter-js/src/modules/networking/PTLS.js b/src/puter-js/src/modules/networking/PTLS.js index ed90cafc5..526a9c5de 100644 --- a/src/puter-js/src/modules/networking/PTLS.js +++ b/src/puter-js/src/modules/networking/PTLS.js @@ -2,7 +2,7 @@ * This file uses https://github.com/MercuryWorkshop/rustls-wasm authored by GitHub:@r58Playz under the MIT License */ -import { PSocket } from "./PSocket"; +import { PSocket } from "./PSocket.js"; let rustls = undefined; diff --git a/src/puter-js/src/services/Filesystem.js b/src/puter-js/src/services/Filesystem.js index 699dc9abe..7b9832ac1 100644 --- a/src/puter-js/src/services/Filesystem.js +++ b/src/puter-js/src/services/Filesystem.js @@ -1,7 +1,7 @@ import putility from "@heyputer/putility"; -import { PuterAPIFilesystem } from "../lib/filesystem/APIFS"; -import { CachedFilesystem } from "../lib/filesystem/CacheFS"; -import { ProxyFilesystem, TFilesystem } from "../lib/filesystem/definitions"; +import { PuterAPIFilesystem } from "../lib/filesystem/APIFS.js"; +import { CachedFilesystem } from "../lib/filesystem/CacheFS.js"; +import { ProxyFilesystem, TFilesystem } from "../lib/filesystem/definitions.js"; import io from '../lib/socket.io/socket.io.esm.min.js'; import { PostMessageFilesystem } from "../lib/filesystem/PostMessageFS.js"; diff --git a/src/puter-js/webpack.config.js b/src/puter-js/webpack.config.js index 4f160a0a0..8cd71caf3 100644 --- a/src/puter-js/webpack.config.js +++ b/src/puter-js/webpack.config.js @@ -1,9 +1,18 @@ -const path = require('path'); -const webpack = require('webpack'); +import path from 'node:path'; +import webpack from 'webpack'; + +// '__dirname' isn't defined by default in ES modules. +// We didn't really want to migrate this file to ESM because +// it's config for tooling that only runs in node, but alas +// if package.json says "type": "module" then we have to use +// ESM syntax everywhere unless we rename this to a .cjs file +// and add an extra flag everywhere we use webpack. +import { fileURLToPath } from 'url'; +const __dirname = path.dirname(fileURLToPath(import.meta.url)); console.log('ENV CHECK!!!', process.env.PUTER_ORIGIN, process.env.PUTER_API_ORIGIN); -module.exports = { +export default { entry: './src/index.js', output: { filename: 'puter.js',