Skip to content

Commit e836ffb

Browse files
authored
Cheerp x idb device (#211)
1 parent 22bbe20 commit e836ffb

File tree

6 files changed

+160
-35
lines changed

6 files changed

+160
-35
lines changed

sites/cheerpx/src/content/docs/11-guides/File-System-support.md

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ For a full server setup and additional details, check our [basic server guide](/
133133

134134
## IDBDevice
135135

136-
IDBDevice provides a persistent, read-write filesystem using the browser's IndexedDB. It's ideal for storing data that should persist between sessions.
136+
[`IDBDevice`](/docs/reference/CheerpX.IDBDevice) provides a persistent, read-write filesystem using the browser's IndexedDB. It's ideal for storing data that should persist between sessions.
137137

138138
### Usage
139139

140-
Create an IDBDevice using the `CheerpX.IDBDevice.create()` method:
140+
[`Create`](/docs/reference/CheerpX.IDBDevice/create) an [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) using the `CheerpX.IDBDevice.create()` method:
141141

142142
```javascript
143143
const idbDevice = await CheerpX.IDBDevice.create("dbName");
@@ -151,38 +151,13 @@ This setup creates a virtual filesystem at `/files` that is backed by IndexedDB.
151151

152152
### Reading files from JavaScript
153153

154-
You can read files from an `IDBDevice` in JavaScript using the `readFileAsBlob` method:
154+
You can read files from an [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) in JavaScript using the [`readFileAsBlob`](/docs/reference/CheerpX.IDBDevice/readFileAsBlob) method:
155155

156156
```javascript
157157
await idbDevice.readFileAsBlob("/filename");
158158
```
159159

160-
### `idbDevice.readFileAsBlob`
161-
162-
`CheerpX.IDBDevice` provides a method to read back files as JavaScript accessible data.
163-
164-
```js
165-
await idbDevice.readFileAsBlob(filename: string): Promise<Blob>
166-
```
167-
168-
**Parameters**:
169-
170-
- **filename**: A string representing the path to the file within the device, starting with a `/` (e.g., "/filename"). Do not include the mount point.
171-
172-
**Returns**:
173-
174-
The method returns a Promise that resolves to a standard JavaScript Blob object.
175-
176-
Example:
177-
178-
```js
179-
const idbDevice = await CheerpX.IDBDevice.create("files");
180-
// Use CheerpX to write something to the device
181-
const outputBlob = await idbDevice.readFileAsBlob("/filename");
182-
```
183-
184-
> [!note] Note
185-
> The `readFileAsBlob` API returns a standard JavaScript Blob object. You can convert it to a string if needed, but you can also convert it to an `ArrayBuffer` or to a URL via `URL.createObjectURL`.
160+
For more details on reading files using [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) and redirecting output, see the [Input/Output](/docs/guides/input-output#reading-files-using-idbdevice-and-redirecting-output) guide.
186161

187162
## DataDevice
188163

@@ -243,7 +218,7 @@ CheerpX supports ext2 filesystems, which can be configured as an overlay device.
243218

244219
### Usage
245220

246-
Create an ext2 filesystem by combining a `HttpBytesDevice` to acess disk blocks, an `IDBDevice` to cache and persist data and a `OverlayDevice` to combine the two.
221+
Create an ext2 filesystem by combining a `HttpBytesDevice` to access disk blocks, an `IDBDevice` to cache and persist data and a `OverlayDevice` to combine the two.
247222

248223
```javascript
249224
// Create an HttpBytesDevice for streaming disk blocks via HTTP

sites/cheerpx/src/content/docs/11-guides/input-output.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ For more details on customizing the console, see [CheerpX Custom console](/docs/
4343

4444
## Reading Files Using IDBDevice and Redirecting Output
4545

46-
`IDBDevice` provides a persistent, read-write filesystem using the browser’s IndexedDB. It’s ideal for storing data that should persist between sessions. You can use the `readFileAsBlob` method to read files from an `IDBDevice` as Blob objects.
46+
[`IDBDevice`](/docs/reference/CheerpX.IDBDevice) provides a persistent, read-write filesystem using the browser’s IndexedDB. It’s ideal for storing data that should persist between sessions. You can use the [`readFileAsBlob`](/docs/reference/CheerpX.IDBDevice/readFileAsBlob) method to read files from an [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) as Blob objects.
4747

4848
To make a file accessible, you can copy files using commands within the virtual machine. Here’s an example on how to do it:
4949

@@ -86,8 +86,6 @@ console.log(await outputBlob.text());
8686
> [!note] Note
8787
> This method has a significant limitation: it doesn't provide streaming output. The entire program needs to finish execution before you can read the output file. This means you won't see real-time output, and for long-running programs, you'll have to wait until completion to see any results. For real-time output, consider using the _custom_ console approach, which allows for streaming output.
8888
89-
For more on `IDBDevice` operations, see the [CheerpX IDBDevice].
90-
9189
## Accessing JS Data in the Filesystem via DataDevice
9290

9391
The `DataDevice` API exposes JavaScript data via the filesystem. This device provides read-only access to a `Uint8Array` and a JavaScript `String`. It is particularly useful for transferring data from JavaScript to programs running in CheerpX.
@@ -96,6 +94,5 @@ For more information, see the [CheerpX DataDevice].
9694

9795
[CheerpX documentations]: https://cheerpx.io/docs/overview
9896
[CheerpX DataDevice]: https://cheerpx.io/docs/guides/File-System-support#datadevice
99-
[CheerpX IDBDevice]: https://cheerpx.io/docs/guides/File-System-support#idbdevice
10097
[Frequently Asked Questions]: https://cheerpx.io/docs/faq
10198
[xterm.js]: https://xtermjs.org/
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: create
3+
description: Create a CheerpX IDBDevice instance to store data in the browser's IndexedDB with read and write access.
4+
---
5+
6+
```ts
7+
namespace CheerpX {
8+
class IDBDevice {
9+
static async create(dbName: string): Promise<IDBDevice>;
10+
}
11+
}
12+
```
13+
14+
## Parameters
15+
16+
- **dbName (`string`)** - The name of `IndexedDB` where the filesystem will be stored.
17+
18+
## Returns
19+
20+
`CheerpX.IDBDevice.create` returns a [Promise] that gives you an `IDBDevice` instance. You can use this instance to create a virtual filesystem stored in `IndexedDB` within your CheerpX environment.
21+
22+
## Example
23+
24+
Create an `IDBDevice` instance for persistent storage.
25+
26+
```js {4}
27+
// Create a read-only block device for a disk image stored on the HTTP server
28+
const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2");
29+
// Make the block device read-write using a persistent IndexedDB overlay
30+
const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice");
31+
const overlayDevice = await CheerpX.OverlayDevice.create(
32+
blockDevice,
33+
idbDevice
34+
);
35+
// Initialize the CheerpX environment
36+
const mountPoints = [
37+
// Use the disk image as the filesystem root
38+
{ type: "ext2", path: "/", dev: overlayDevice },
39+
];
40+
const cx = await CheerpX.Linux.create({
41+
mounts: mountPoints,
42+
});
43+
```
44+
45+
If you’d like to learn more about related topics, check out these guides:
46+
47+
- [Files and filesystems](/docs/guides/File-System-support) – Managing files and storage in CheerpX.
48+
- [Input and output](/docs/guides/input-output) – Handling data flow in your environment.
49+
50+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: readFileAsBlob
3+
description: Reads a file from an IDBDevice and returns it as a Javascript Blob object.
4+
---
5+
6+
```js
7+
namespace CheerpX {
8+
class IDBDevice {
9+
async readFileAsBlob(filename: string): Promise<Blob>;
10+
}
11+
}
12+
```
13+
14+
## Parameters
15+
16+
- **filename (`string`)** - The path to the file within the `IDBDevice`, starting with a `/` (e.g., `/filename`). Do not include the mount point.
17+
18+
## Returns
19+
20+
`CheerpX.IDBDevice.readFileAsBlob` returns a [Promise] that resolves to a Javascript `Blob` object. This object represents the file's data, which can be further manipulated or converted as needed.
21+
22+
## Example
23+
24+
```js {4, 15}
25+
// Create a read-only block device for a disk image stored on the HTTP server
26+
const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2");
27+
// Make the block device read-write using a persistent IndexedDB overlay
28+
const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice");
29+
const overlayDevice = await CheerpX.OverlayDevice.create(
30+
blockDevice,
31+
idbDevice
32+
);
33+
// Initialize the CheerpX environment
34+
const mountPoints = [
35+
// Use the disk image as the filesystem root
36+
{ type: "ext2", path: "/", dev: overlayDevice },
37+
];
38+
const cx = await CheerpX.Linux.create({
39+
mounts: mountPoints,
40+
});
41+
42+
const outputBlob = await idbDevice.readFileAsBlob("/fileName");
43+
console.log(outputBlob);
44+
```
45+
46+
> [!tip] View stored files in DevTools
47+
> To see your stored files, open Developer Tools, go to the Application tab, and check Storage > IndexedDB. Here, you can browse and inspect your files easily! <br>
48+
> Want to learn more? Check out these related guides: <br>
49+
>
50+
> - [Files and filesystems](/docs/guides/File-System-support) <br>
51+
> - [Input and output](/docs/guides/input-output)
52+
53+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: reset
3+
description: Reset the IDBDevice instance, clearing all its stored data in the associated IndexedDB.
4+
---
5+
6+
```ts
7+
namespace CheerpX {
8+
class IDBDevice {
9+
async reset(): Promise<void>;
10+
}
11+
}
12+
```
13+
14+
## Parameters
15+
16+
None.
17+
18+
## Returns
19+
20+
`CheerpX.IDBDevice.reset` returns a [Promise] that resolves once the `IDBDevice` has been reset. This operation clears all stored data, effectively restoring the `IDBDevice` to an empty state.
21+
22+
## Example
23+
24+
Reset an `IDBDevice` instance before mounting it.
25+
26+
```ts {8}
27+
// Create a read-only block device for a disk image stored on the HTTP server
28+
const blockDevice = await CheerpX.HttpBytesDevice.create("/cheerpXImage.ext2");
29+
30+
// Make the block device read-write using a persistent IndexedDB overlay
31+
const idbDevice = await CheerpX.IDBDevice.create("block_idbDevice");
32+
33+
// Reset the IndexedDB storage
34+
await idbDevice.reset();
35+
36+
const overlayDevice = await CheerpX.OverlayDevice.create(
37+
blockDevice,
38+
idbDevice
39+
);
40+
// Initialize the CheerpX environment
41+
const mountPoints = [
42+
// Use the disk image as the filesystem root
43+
{ type: "ext2", path: "/", dev: overlayDevice },
44+
];
45+
const cx = await CheerpX.Linux.create({
46+
mounts: mountPoints,
47+
});
48+
```
49+
50+
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

sites/cheerpx/src/content/docs/20-faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Yes, `WebDevice` can handle third-party origins as paths, but it's important to
2222

2323
## Why can't I execute files directly from a `DataDevice`?
2424

25-
`DataDevice` in CheerpX does not have full support for Linux mode bits, and in particular it lacks the _**executable**_ bit. This means you can read data from it, but you cannot execute files directly from it. To execute files that are in a DataDevice, you need to first copy the files to a filesystem with complete support for mode bits, such as `IDBDevice` (IndexedDB) or Ext2.
25+
`DataDevice` in CheerpX does not have full support for Linux mode bits, and in particular it lacks the _**executable**_ bit. This means you can read data from it, but you cannot execute files directly from it. To execute files that are in a `DataDevice`, you need to first copy the files to a filesystem with complete support for mode bits, such as [`IDBDevice`](/docs/reference/CheerpX.IDBDevice) (IndexedDB) or Ext2.
2626

2727
## Why can't CheerpX do what v86 does in terms of disk access and networking?
2828

0 commit comments

Comments
 (0)