Skip to content

Commit 6e27f16

Browse files
Guide on adding files & Issue 52 (#54)
* Guide on adding files & Issue: Handling events guides should be backend #52 * Optimised images with calibre/image-actions * Highlight box on images. * Optimised images with calibre/image-actions * File specification syntax. --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 7b84925 commit 6e27f16

6 files changed

Lines changed: 191 additions & 6 deletions

File tree

.vitepress/sidebars/guides.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ export const guidesSidebar: DefaultTheme.SidebarItem[] = [
3333
text: "Customizing Context Menus",
3434
link: "/guides/components/menu",
3535
},
36-
{
37-
text: "Handling Backend Events",
38-
link: "/guides/components/backend_events",
39-
},
4036
{
4137
text: "Using the Component Library",
4238
link: "/guides/components/styling",
@@ -50,6 +46,10 @@ export const guidesSidebar: DefaultTheme.SidebarItem[] = [
5046
text: "Creating and Calling a Custom Function",
5147
link: "/guides/components/rpc",
5248
},
49+
{
50+
text: "Handling Backend Events",
51+
link: "/guides/components/backend_events",
52+
},
5353
{
5454
text: "Sending HTTP Requests",
5555
link: "/guides/components/request",
@@ -83,6 +83,10 @@ export const guidesSidebar: DefaultTheme.SidebarItem[] = [
8383
{
8484
text: "Shared",
8585
items: [
86+
{
87+
text: "Adding Files",
88+
link: "/guides/components/files",
89+
},
8690
{
8791
text: "Using Environment Variables",
8892
link: "/guides/components/env",

src/_images/backend_assets_key.png

6.04 KB
Loading
9.09 KB
Loading

src/_images/view_file_frontend.png

33.1 KB
Loading

src/guides/components/fetch.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ To view the entire script, expand the following:
103103
<details>
104104
<summary>Full Script</summary>
105105

106-
``` js
106+
``` ts
107107
import type { SDK, DefineAPI } from "caido:plugin";
108108
import { Request as FetchRequest, fetch } from "caido:http";
109109

@@ -188,7 +188,7 @@ To view how the endpoint can be called with a frontend plugin, expand the follow
188188
<details>
189189
<summary>Full Script</summary>
190190

191-
``` js
191+
``` ts
192192
import type { Caido } from "@caido/sdk-frontend";
193193
import type { API } from "../../backend/src/index.ts";
194194

src/guides/components/files.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# Adding Files
2+
3+
To include additional files in your Caido plugins, you can add the `assets` property key to either the frontend or backend component objects in the `caido.config.ts` file. The key value is an array that stores the locations of any files accessible to the plugin.
4+
5+
In this guide we'll cover how to add a file to a plugin named `myfile.txt`.
6+
7+
## Shared Steps
8+
9+
In the root directory of your plugin package, create a new directory named `assets`. Within this directory, create the `myfile.txt` file, write content to it, and save it. The file can now be referenced with:
10+
11+
``` ts
12+
assets : ["./assets/myfile.txt"]
13+
```
14+
15+
::: tip TIPS
16+
17+
Glob syntax (`*`) is supported to reference multiple files:
18+
19+
- `/path/*.txt`: Will include all `.txt` files in the path directory.
20+
- `/**/file.txt`: Will include any `file.txt` within any directory.
21+
22+
Files and directories are included differently:
23+
24+
- For a file, it will copy it at the root of the output assets directory.
25+
- For a directory, it will copy it recursively in the output assets directory.
26+
:::
27+
28+
::: info
29+
The `assets` key can also be added to a plugin's `manifest.json` file. However, multiple locations can not be defined with this method.
30+
:::
31+
32+
## Adding Files to the Frontend Component
33+
34+
Open the `caido.config.ts` file and add the property to the `frontend` component object:
35+
36+
<img alt="Event output." src="/_images/frontend_assets_key.png" center/>
37+
38+
### /packages/frontend/src/index.ts
39+
40+
To read the file, the `sdk.assets.get()` method can be called.
41+
42+
``` ts
43+
const file = await sdk.assets.get("myfile.txt");
44+
```
45+
46+
::: tip
47+
To view the entire frontend script, expand the following:
48+
49+
<details>
50+
<summary>Full Script</summary>
51+
52+
``` ts
53+
import "./styles/index.css";
54+
55+
import type { FrontendSDK } from "./types";
56+
57+
// Note that the init function is async to account for fetching the files.
58+
export const init = async (sdk: FrontendSDK) => {
59+
60+
const root = document.createElement("div");
61+
Object.assign(root.style, {
62+
height: "100%",
63+
width: "100%",
64+
});
65+
66+
root.id = `plugin--frontend-vanilla`;
67+
68+
const parent = document.createElement("div");
69+
parent.classList.add("h-full", "flex", "justify-center", "items-center");
70+
71+
const container = document.createElement("div");
72+
container.classList.add("flex", "flex-col", "gap-1", "p-4");
73+
74+
const file = await sdk.assets.get("myfile.txt");
75+
// For large files or to process in chunks, use file.asReadableStream() instead.
76+
const content = await file.asString();
77+
container.textContent = content;
78+
79+
parent.appendChild(container);
80+
81+
root.appendChild(parent);
82+
83+
sdk.navigation.addPage("/view-file-plugin", {
84+
body: root,
85+
});
86+
87+
sdk.sidebar.registerItem("View File Plugin", "/view-file-plugin");
88+
};
89+
```
90+
91+
</details>
92+
:::
93+
94+
## Adding Files to the Backend Component
95+
96+
Open the `caido.config.ts` file and add the property to the `backend` component object:
97+
98+
<img alt="Event output." src="/_images/backend_assets_key.png" center/>
99+
100+
### /packages/backend/src/index.ts
101+
102+
By [creating a custom backend function](./rpc.md) to read the file, we can later call it from the frontend:
103+
104+
``` ts
105+
import type { DefineAPI, SDK } from "caido:plugin";
106+
import { readFile } from 'fs/promises';
107+
import path from "path";
108+
109+
const readMyFile = async (sdk: SDK) => {
110+
try {
111+
const filePath = path.join(sdk.meta.assetsPath(), "myfile.txt");
112+
const contents = await readFile(filePath, { encoding: 'utf8' });
113+
sdk.console.log(contents);
114+
return contents;
115+
} catch (err: any) {
116+
sdk.console.error(err.message);
117+
throw err;
118+
}
119+
};
120+
121+
export type API = DefineAPI<{
122+
readMyFile: typeof readMyFile;
123+
}>;
124+
125+
export function init(sdk: SDK<API>) {
126+
sdk.api.register("readMyFile", readMyFile);
127+
}
128+
```
129+
130+
::: tip
131+
To view the entire frontend script, expand the following:
132+
133+
<details>
134+
<summary>Full Script</summary>
135+
136+
``` ts
137+
import "./styles/index.css";
138+
139+
import type { FrontendSDK } from "./types";
140+
141+
export const init = async (sdk: FrontendSDK) => {
142+
const root = document.createElement("div");
143+
Object.assign(root.style, {
144+
height: "100%",
145+
width: "100%",
146+
});
147+
148+
root.id = `plugin--frontend-vanilla`;
149+
150+
const parent = document.createElement("div");
151+
parent.classList.add("h-full", "flex", "justify-center", "items-center");
152+
153+
const container = document.createElement("div");
154+
container.classList.add("flex", "flex-col", "gap-1", "p-4");
155+
156+
try {
157+
// Call the backend readMyFile() function to read myfile.txt.
158+
const content = await sdk.backend.readMyFile();
159+
container.textContent = content;
160+
} catch (error: any) {
161+
container.textContent = `Error reading file: ${error.message}`;
162+
}
163+
164+
parent.appendChild(container);
165+
166+
root.appendChild(parent);
167+
168+
sdk.navigation.addPage("/view-file-plugin", {
169+
body: root,
170+
});
171+
172+
sdk.sidebar.registerItem("View File Plugin", "/view-file-plugin");
173+
};
174+
```
175+
176+
</details>
177+
:::
178+
179+
## The Result
180+
181+
<img alt="Event output." src="/_images/view_file_frontend.png" center/>

0 commit comments

Comments
 (0)