|
| 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