-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extract screenshots from CWS and Firefox addons
- Loading branch information
Showing
11 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
SERVER_ORIGIN=http://localhost:3000 |
This file contains 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 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 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 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,15 @@ | ||
import type { ChromeService } from "../services/chrome-service"; | ||
import { RouteHandler } from "../utils/rest-router"; | ||
|
||
export const getChromeScreenshot = | ||
(chrome: ChromeService): RouteHandler<{ id: string; index: string }> => | ||
async (params) => { | ||
const extension = await chrome.getExtension(params.id); | ||
const index = Number(params.index); | ||
const screenshot = extension?.screenshots.find( | ||
(screenshot) => screenshot.index == index, | ||
); | ||
|
||
if (screenshot == null) return new Response(null, { status: 404 }); | ||
return Response.redirect(screenshot.rawUrl); | ||
}; |
This file contains 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 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 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 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,42 @@ | ||
import * as radix3 from "radix3"; | ||
|
||
export type RouteHandler<TParams = {}> = ( | ||
params: TParams, | ||
url: URL, | ||
req: Request, | ||
) => Response | Promise<Response>; | ||
|
||
export interface Route { | ||
method: string; | ||
handler: RouteHandler; | ||
} | ||
|
||
export function createRestRouter() { | ||
const r = radix3.createRouter<Route>(); | ||
const router = { | ||
get(path: string, handler: RouteHandler) { | ||
r.insert(path, { method: "GET", handler }); | ||
return router; | ||
}, | ||
post(path: string, handler: RouteHandler) { | ||
r.insert(path, { method: "POST", handler }); | ||
return router; | ||
}, | ||
any(path: string, handler: RouteHandler) { | ||
r.insert(path, { method: "ANY", handler }); | ||
return router; | ||
}, | ||
on(method: string, path: string, handler: RouteHandler) { | ||
r.insert(path, { method, handler }); | ||
return router; | ||
}, | ||
async fetch(url: URL, req: Request): Promise<Response> { | ||
const match = r.lookup(url.pathname); | ||
if (match && (req.method === match.method || match.method === "ANY")) { | ||
return await match.handler(match.params ?? {}, url, req); | ||
} | ||
return new Response(null, { status: 404 }); | ||
}, | ||
}; | ||
return router; | ||
} |
This file contains 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,7 @@ | ||
export function buildScreenshotUrl( | ||
type: "chrome" | "firefox", | ||
id: string, | ||
index: number, | ||
) { | ||
return `${process.env.SERVER_ORIGIN}/api/rest/${type}/${id}/screenshots/${index}`; | ||
} |