From 327eef2d3285b679552f5dcba8b2154cf90dbd8a Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Tue, 17 Nov 2026 21:24:41 -0700 Subject: [PATCH] feat(apps): puter.apps.get() accepts a URL (#4686) A string containing :// is looked up as a URL instead of a name. The puter-apps driver's read takes id.url and resolves it through appUidFromOrigin, so it returns the same app a page on that origin acts as. --- src/backend/drivers/apps/AppDriver.bench.ts | 4 ++ src/backend/drivers/apps/AppDriver.js | 19 ++++- src/backend/drivers/apps/AppDriver.test.ts | 70 +++++++++++++++++++ src/docs/src/Apps/get.md | 49 ++++++++++++- src/docs/src/changelog.md | 1 + src/docs/src/examples.js | 6 ++ .../playground/examples/app-get-by-url.html | 19 +++++ src/puter-js/src/modules/apps/get.js | 13 ++-- src/puter-js/tests/api/suites/apps.suite.ts | 26 +++++++ 9 files changed, 199 insertions(+), 8 deletions(-) create mode 100644 src/docs/src/playground/examples/app-get-by-url.html diff --git a/src/backend/drivers/apps/AppDriver.bench.ts b/src/backend/drivers/apps/AppDriver.bench.ts index e6c81d48c..6b605bada 100644 --- a/src/backend/drivers/apps/AppDriver.bench.ts +++ b/src/backend/drivers/apps/AppDriver.bench.ts @@ -46,6 +46,10 @@ describe('apps', () => { await apps('read', { id: { name: 'bench-app-0' } }); }); + env.scenario('read an app by its URL', async () => { + await apps('read', { id: { url: 'https://bench-app-0.test/page' } }); + }); + env.scenario('look up an app by name on launch', async () => { await env.request('/apps/bench-app-0'); }); diff --git a/src/backend/drivers/apps/AppDriver.js b/src/backend/drivers/apps/AppDriver.js index 9d04bebc2..806d4fe23 100644 --- a/src/backend/drivers/apps/AppDriver.js +++ b/src/backend/drivers/apps/AppDriver.js @@ -304,7 +304,10 @@ export class AppDriver extends PuterDriver { async read({ uid, id, params = {}, ...rest } = {}) { const actor = requireContextActor(); - const app = await this.#resolve({ uid, id }); + const app = + !uid && id?.url !== undefined + ? await this.#getByUrl(id.url) + : await this.#resolve({ uid, id }); if (!app) throw new HttpError(404, 'App not found', { legacyCode: 'not_found', @@ -813,6 +816,20 @@ export class AppDriver extends PuterDriver { return null; } + /** + * The app a page at `url` acts as: the origin identity app tokens use, so + * only the URL's origin counts. + */ + async #getByUrl(url) { + if (typeof url !== 'string' || !url) { + throw new HttpError(400, '`id.url` must be a non-empty string', { + legacyCode: 'bad_request', + }); + } + const uid = await this.services.auth.appUidFromOrigin(url); + return this.appStore.getByUid(uid); + } + /** * Uid lookup with canonical-uid alias fallback. When two app rows have been * merged (see {@link #maybeJoinOwnedHostedIndexUrlApp}), the source uid is diff --git a/src/backend/drivers/apps/AppDriver.test.ts b/src/backend/drivers/apps/AppDriver.test.ts index 3689fa8f2..ce324054b 100644 --- a/src/backend/drivers/apps/AppDriver.test.ts +++ b/src/backend/drivers/apps/AppDriver.test.ts @@ -546,6 +546,76 @@ describe('AppDriver.read', () => { expect(fetched.name).toBe(name); }); + it('reads via id object with `{ url }` by the URL origin', async () => { + const owner = await makeUser(); + const reader = await makeUser(); + const indexUrl = uniqueIndexUrl(); + const created = await withActor(owner.actor, () => + driver.create({ + object: { + name: uniqueName('by-url'), + title: 't', + index_url: indexUrl, + }, + }), + ); + + const fetched = await withActor(reader.actor, () => + driver.read({ id: { url: `${indexUrl}some/page?q=1#top` } }), + ); + expect(fetched.uid).toBe(created.uid); + }); + + it('reads the app created for an unregistered origin by its URL', async () => { + const { actor } = await makeUser(); + const origin = new URL(uniqueIndexUrl()).origin; + const uid = await server.services.auth.appUidFromOrigin(origin); + await server.stores.app.createFromOrigin(uid, origin, { + ownerUserId: null, + }); + + const fetched = await withActor(actor, () => + driver.read({ id: { url: `${origin}/page` } }), + ); + expect(fetched.uid).toBe(uid); + expect(fetched.name).toBe(uid); + }); + + it('returns 404 for a URL whose origin no app serves', async () => { + const { actor } = await makeUser(); + const indexUrl = `${uniqueIndexUrl()}tools/`; + await withActor(actor, () => + driver.create({ + object: { + name: uniqueName('below-root'), + title: 't', + index_url: indexUrl, + }, + }), + ); + + for (const url of [uniqueIndexUrl(), indexUrl]) { + await expect( + withActor(actor, () => driver.read({ id: { url } })), + ).rejects.toMatchObject({ + statusCode: 404, + legacyCode: 'not_found', + }); + } + }); + + it('rejects a `{ url }` that is not a web URL with 400', async () => { + const { actor } = await makeUser(); + for (const url of [42, '', 'file:///index.html', 'not a url']) { + await expect( + withActor(actor, () => driver.read({ id: { url } })), + ).rejects.toMatchObject({ + statusCode: 400, + legacyCode: 'bad_request', + }); + } + }); + it('returns 404 for a missing app', async () => { const { actor } = await makeUser(); await expect( diff --git a/src/docs/src/Apps/get.md b/src/docs/src/Apps/get.md index 206d4622e..fddd1accc 100755 --- a/src/docs/src/Apps/get.md +++ b/src/docs/src/Apps/get.md @@ -4,7 +4,7 @@ description: Retrieve details of your Puter app. platforms: [websites, apps, nodejs, workers] --- -Returns an app with the given name. +Returns an app with the given name or URL. If the app does not exist, the promise will be rejected. @@ -13,6 +13,8 @@ If the app does not exist, the promise will be rejected. ```js puter.apps.get(name) puter.apps.get(name, options) +puter.apps.get(url) +puter.apps.get(url, options) ``` ## Parameters @@ -21,6 +23,10 @@ puter.apps.get(name, options) The name of the app to get. +#### `url` (required) + +A URL on the app's website, such as `https://my-app.puter.site/about`. Puter.js treats any string that contains `://` as a URL. Only the URL's origin is matched, so this returns the same app that a page on that origin acts as when it calls Puter.js. See [Getting an app by URL](#getting-an-app-by-url). + #### `options` (Object) (optional) An object containing the following properties: @@ -32,7 +38,22 @@ An object containing the following properties: ## Return value -A `Promise` that will resolve to the [`App`](/Objects/app/) object with the given name. +A `Promise` that will resolve to the [`App`](/Objects/app/) object with the given name or URL. + +## Errors + +| Code | When | +|------|------| +| `not_found` | No app has this name, or no app runs at this URL's origin. | +| `bad_request` | The URL isn't an `http://`, `https://`, or browser extension URL. | +| `app_blocked` | The URL's origin is blocked from using Puter. | +| `forbidden` | The app is protected and you don't have access to it. | + +## Getting an app by URL + +A URL matches the app whose index URL is the root of that URL's origin. For example, `https://my-app.puter.site/about?tab=2` returns the app whose index URL is `https://my-app.puter.site/` or `https://my-app.puter.site/index.html`. An app whose index URL is below the root, such as `https://example.com/tools/`, isn't returned for a URL. + +A website that isn't registered as an app gets an app the first time a user signs in to Puter from one of its pages, so you can get it by URL after that. The `name` and `title` of that app are both its `uid`. ## Examples @@ -62,3 +83,27 @@ A `Promise` that will resolve to the [`App`](/Objects/app/) object with the give ``` + +### Get an app by its URL + +```html;app-get-by-url + + + + + + +``` diff --git a/src/docs/src/changelog.md b/src/docs/src/changelog.md index e98b6ffdb..a1a27108d 100644 --- a/src/docs/src/changelog.md +++ b/src/docs/src/changelog.md @@ -8,6 +8,7 @@ New features, changes, and fixes in Puter.js and the Puter APIs, grouped by day, ## November 17, 2026 +- Added URL lookups to [`puter.apps.get()`](/Apps/get/). Pass a URL such as `https://my-app.puter.site/about` instead of a name to get the app at that URL's origin. See [Getting an app by URL](/Apps/get/#getting-an-app-by-url). - Fixed [`puter.ui.contextMenu()`](/UI/contextMenu/) menus with a submenu inside a submenu staying open after an item in the inner submenu was chosen. The inner submenu also closes when the submenu it belongs to closes. ## November 16, 2026 diff --git a/src/docs/src/examples.js b/src/docs/src/examples.js index 644090426..84debf817 100644 --- a/src/docs/src/examples.js +++ b/src/docs/src/examples.js @@ -976,6 +976,12 @@ const examples = [ slug: 'app-get', source: '/playground/examples/app-get.html', }, + { + title: 'Get an app by its URL', + description: 'Look up the app at a website\'s origin with Puter.js apps API. Run and experiment with this example directly in the playground.', + slug: 'app-get-by-url', + source: '/playground/examples/app-get-by-url.html', + }, ], }, { diff --git a/src/docs/src/playground/examples/app-get-by-url.html b/src/docs/src/playground/examples/app-get-by-url.html new file mode 100644 index 000000000..ef61fbd2f --- /dev/null +++ b/src/docs/src/playground/examples/app-get-by-url.html @@ -0,0 +1,19 @@ + + + + + + diff --git a/src/puter-js/src/modules/apps/get.js b/src/puter-js/src/modules/apps/get.js index 302349bd8..1afd29957 100644 --- a/src/puter-js/src/modules/apps/get.js +++ b/src/puter-js/src/modules/apps/get.js @@ -5,12 +5,13 @@ import { addUserIteration } from './lib/appUsers.js'; /** @typedef {import('./types.js').AppListOptions} AppListOptions */ /** - * Returns the app with the given name. Rejects if the app does not exist. - * The options object (`stats_period`, `icon_size`) may be passed as the second - * argument, or as the sole argument. + * Returns the app with the given name, or the app at a URL's origin when given + * a URL (any string with `://`, which no app name contains). Rejects if the app + * does not exist. The options object (`stats_period`, `icon_size`) may be + * passed as the second argument, or as the sole argument. * * @this {import('./index.js').AppsModule} - * @param {string | AppListOptions} nameOrOptions + * @param {string | AppListOptions} nameOrOptions The app's name or a URL. * @param {AppListOptions} [options] * @returns {Promise} */ @@ -20,7 +21,9 @@ export async function get (nameOrOptions, options) { const driverArgs = {}; if ( typeof nameOrOptions === 'string' ) { if ( typeof options === 'object' && options !== null ) driverArgs.params = options; - driverArgs.id = { name: nameOrOptions }; + driverArgs.id = nameOrOptions.includes('://') + ? { url: nameOrOptions } + : { name: nameOrOptions }; } if ( typeof nameOrOptions === 'object' && nameOrOptions !== null ) { driverArgs.params = nameOrOptions; diff --git a/src/puter-js/tests/api/suites/apps.suite.ts b/src/puter-js/tests/api/suites/apps.suite.ts index 8ee2e0e26..e2e64055d 100644 --- a/src/puter-js/tests/api/suites/apps.suite.ts +++ b/src/puter-js/tests/api/suites/apps.suite.ts @@ -173,6 +173,32 @@ export default suite('apps', { ); }, + 'get with a URL returns the app at its origin': async (t) => { + const app = await t.puter.apps.create( + 'apps-suite-by-url', + 'https://apps-suite-by-url.example.com/', + ); + const fetched = await t.puter.apps.get( + 'https://apps-suite-by-url.example.com/about?tab=1', + ); + t.assert.equal(fetched.uid, app.uid); + t.assert.equal(fetched.name, 'apps-suite-by-url'); + }, + + 'get with a URL no app serves rejects with not_found': async (t) => { + const error = (await t.assert.rejects(() => + t.puter.apps.get('https://apps-suite-no-app.example.com/'), + )) as { code?: string }; + t.assert.equal(error.code, 'not_found'); + }, + + 'get with a URL that is not a web URL rejects with bad_request': async (t) => { + const error = (await t.assert.rejects(() => + t.puter.apps.get('file:///index.html'), + )) as { code?: string }; + t.assert.equal(error.code, 'bad_request'); + }, + 'delete removes the app': async (t) => { await t.puter.apps.create( 'apps-suite-delete',