-
Notifications
You must be signed in to change notification settings - Fork 0
feat: preview from source bus #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ddc4a6a
feat: preview from source bus
tripodsan f9f1b78
chore: make tests happy
tripodsan d2cc796
chore: update
tripodsan 1093b79
feat: add media handler
tripodsan ff7c6ca
Merge branch 'main' into preview-sourcebus
tripodsan 08c7670
chore: more tests
tripodsan f5827cc
Merge branch 'main' into preview-sourcebus
tripodsan 2a4e224
feat: add json support
tripodsan 7f8534d
feat: add sourcebus file support
tripodsan ac53696
wip
tripodsan cf23616
feat: add list
tripodsan 0cd6d43
chore: fix typo
tripodsan cb0bd3b
chore: cleanup
tripodsan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { Response } from '@adobe/fetch'; | ||
| import { HelixStorage } from '@adobe/helix-shared-storage'; | ||
| import { validateSource } from './sourcebus-utils.js'; | ||
|
|
||
| /** | ||
| * Fetches file data from the source bus | ||
| * | ||
| * @param {import('../support/AdminContext').AdminContext} ctx context | ||
| * @param {import('../support/RequestInfo').RequestInfo} info request info | ||
| * @param {object} [opts] options | ||
| * @param {object} [opts.source] content source | ||
| * @param {string} [opts.lastModified] last modified | ||
| * @param {number} [opts.fetchTimeout] fetch timeout | ||
| * @returns {Promise<Response>} response | ||
| */ | ||
| export async function handleFile(ctx, info, opts) { | ||
| const { | ||
| org, site, sourcePath, error: errorResp, | ||
| } = await validateSource(ctx, info, opts); | ||
| if (errorResp) { | ||
| return errorResp; | ||
| } | ||
|
|
||
| // load content from source bus | ||
| const sourceBus = HelixStorage.fromContext(ctx).sourceBus(); | ||
| const meta = {}; | ||
| const body = await sourceBus.get(`${org}/${site}${sourcePath}`, meta); | ||
| if (!body) { | ||
| return new Response('', { status: 404 }); | ||
| } | ||
|
|
||
| return new Response(body, { | ||
| status: 200, | ||
| headers: { | ||
| 'content-type': meta.ContentType, | ||
| 'last-modified': meta.LastModified?.toUTCString(), | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { Response } from '@adobe/fetch'; | ||
| import { HelixStorage } from '@adobe/helix-shared-storage'; | ||
| import { errorResponse } from '../support/utils.js'; | ||
| import { assertValidSheetJSON } from './utils.js'; | ||
| import { validateSource } from './sourcebus-utils.js'; | ||
| import { error } from './errors.js'; | ||
|
|
||
| function parseSheetJSON(data) { | ||
| let json; | ||
| try { | ||
| json = JSON.parse(data); | ||
| } catch { | ||
| throw Error('invalid sheet json; failed to parse'); | ||
| } | ||
|
|
||
| assertValidSheetJSON(json); | ||
| return json; | ||
| } | ||
|
|
||
| /** | ||
| * Fetches a JSON as sheet/multisheet from the source bus | ||
| * | ||
| * @param {import('../support/AdminContext').AdminContext} ctx context | ||
| * @param {import('../support/RequestInfo').RequestInfo} info request info | ||
| * @param {object} [opts] options | ||
| * @param {object} [opts.source] content source | ||
| * @param {string} [opts.lastModified] last modified | ||
| * @param {number} [opts.fetchTimeout] fetch timeout | ||
| * @returns {Promise<Response>} response | ||
| */ | ||
| export async function handleJSON(ctx, info, opts) { | ||
| const { log } = ctx; | ||
| const { | ||
| org, site, sourcePath, error: errorResp, | ||
| } = await validateSource(ctx, info, opts); | ||
| if (errorResp) { | ||
| return errorResp; | ||
| } | ||
|
|
||
| // load content from source bus | ||
| const sourceBus = HelixStorage.fromContext(ctx).sourceBus(); | ||
| const meta = {}; | ||
| const body = await sourceBus.get(`${org}/${site}${sourcePath}`, meta); | ||
| if (!body) { | ||
| return new Response('', { status: 404 }); | ||
| } | ||
|
|
||
| let json; | ||
| try { | ||
| json = parseSheetJSON(body); | ||
| } catch (e) { | ||
| return errorResponse(log, 400, error( | ||
| 'JSON fetched from markup \'$1\' is invalid: $2', | ||
| sourcePath, | ||
| e.message, | ||
| )); | ||
| } | ||
|
|
||
| return new Response(JSON.stringify(json), { | ||
| status: 200, | ||
| headers: { | ||
| 'content-type': 'application/json', | ||
| 'last-modified': meta.LastModified?.toUTCString(), | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { splitByExtension } from '@adobe/helix-shared-string'; | ||
| import { HelixStorage } from '@adobe/helix-shared-storage'; | ||
| import { basename, dirname } from 'path'; | ||
| import { Forest } from './Forest.js'; | ||
| import { error } from './errors.js'; | ||
| import { StatusCodeError } from '../support/StatusCodeError.js'; | ||
|
|
||
| export class SourceForest extends Forest { | ||
| constructor(ctx, info) { | ||
| super(ctx.log); | ||
| this.ctx = ctx; | ||
| this.bucket = HelixStorage.fromContext(ctx).sourceBus(); | ||
| this.org = info.org; | ||
| this.site = info.site; | ||
| } | ||
|
|
||
| /** | ||
| * List items below a root item. | ||
| * @returns {Promise<object[]>} | ||
| */ | ||
| async listFolder(source, rootPath, relPath) { | ||
| const key = `${this.org}/${this.site}${relPath}`; | ||
| const listing = await this.bucket.list(key); | ||
| return listing.map((item) => { | ||
| /* | ||
| "key": "org/site/documents/index.html", | ||
| "lastModified": "2025-01-01T12:34:56.000Z", | ||
| "contentLength": 32768, | ||
| "contentType": "text/html", | ||
| "path": "/index.html" | ||
| */ | ||
| const path = `${rootPath}${relPath}${item.path}`; | ||
| const name = basename(item.path); | ||
| if (name === '.props') { | ||
| return null; | ||
| } | ||
| const [baseName, ext] = splitByExtension(path); // eg: /documents/index , .html | ||
| const ret = { | ||
| ...item, | ||
| path, | ||
| file: true, | ||
| resourcePath: path, | ||
| name: basename(path), | ||
tripodsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ext, | ||
| }; | ||
| if (name === 'index.html') { | ||
| ret.path = `${dirname(path)}/`; | ||
| ret.resourcePath = `${baseName}.md`; | ||
| ret.ext = '.md'; | ||
| } else if (ext === 'html') { | ||
| ret.path = baseName; | ||
| ret.resourcePath = `${baseName}.md`; | ||
| ret.ext = '.md'; | ||
| } | ||
| return ret; | ||
| }).filter((item) => !!item); | ||
| } | ||
| } | ||
| /** | ||
| * Fetches file data from the external source. | ||
| * the paths can specify the files that should be included in the list. if a path ends with `/*` | ||
| * its entire subtree is retrieved. | ||
| * | ||
| * @type {import('./contentproxy.js').FetchList} | ||
| * @param {import('../support/AdminContext').AdminContext} ctx context | ||
| * @param {PathInfo} info | ||
| * @param {import('../support/RequestInfo').RequestInfo} info request info | ||
| * @param {string[]} paths | ||
| * @param {ProgressCallback} progressCB | ||
| * @returns {Promise<ResourceInfo[]>} the list of resources | ||
| */ | ||
| export async function list(ctx, info, paths, progressCB) { | ||
| const { config: { content: { source } } } = ctx; | ||
| const sourceUrl = new URL(source.url); | ||
|
|
||
| // extract org and site from url.pathname, format: https://api.aem.live/<org>/sites/<site>/source | ||
| // e.g. /adobe/sites/foo/source | ||
| const pathMatch = sourceUrl.pathname.match(/^\/([^/]+)\/sites\/([^/]+)\/source$/); | ||
tripodsan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!pathMatch) { | ||
| const { message } = error( | ||
| 'Source url must be in the format: https://api.aem.live/<org>/sites/<site>/source. Got: $1', | ||
| sourceUrl.href, | ||
| ); | ||
| throw new StatusCodeError(message, 400); | ||
| } else { | ||
| const [, org, site] = pathMatch; // eslint-disable-line prefer-destructuring | ||
| if (org !== info.org || site !== info.site) { | ||
| const { message } = error( | ||
| 'Source bus is not allowed for org: $1, site: $2', | ||
| info.org, | ||
| info.site, | ||
| ); | ||
| throw new StatusCodeError(message, 400); | ||
| } | ||
| } | ||
|
|
||
| const forest = new SourceForest(ctx, info); | ||
| const itemList = await forest.generate(source, paths, progressCB); | ||
|
|
||
| return itemList.map((item) => { | ||
| if (item.status) { | ||
| return item; | ||
| } | ||
| return { | ||
| path: item.path, | ||
| resourcePath: item.resourcePath, | ||
| source: { | ||
| name: item.name, | ||
| contentType: item.contentType, | ||
| lastModified: Date.parse(item.lastModified), | ||
| size: item.contentLength, | ||
| type: 'source', | ||
| }, | ||
| }; | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.