2024-02-09 04:00:45 +08:00
|
|
|
import { syscall } from "../syscall.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { AttachmentMeta, FileMeta, PageMeta } from "../types.ts";
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-09 15:27:58 +08:00
|
|
|
/**
|
|
|
|
* Exposes the space with its pages, attachments and plugs.
|
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Lists all pages (files ending in .md) in the space.
|
|
|
|
* @param unfiltered
|
|
|
|
* @returns a list of all pages in the space represented as PageMeta objects
|
|
|
|
*/
|
|
|
|
export function listPages(): Promise<PageMeta[]> {
|
|
|
|
return syscall("space.listPages");
|
2023-05-24 02:53:53 +08:00
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Get metadata for a page in the space.
|
|
|
|
* @param name the name of the page to get metadata for
|
|
|
|
* @returns the metadata for the page
|
|
|
|
*/
|
2023-05-24 02:53:53 +08:00
|
|
|
export function getPageMeta(name: string): Promise<PageMeta> {
|
|
|
|
return syscall("space.getPageMeta", name);
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Read a page from the space as text.
|
|
|
|
* @param name the name of the page to read
|
|
|
|
* @returns the text of the page
|
|
|
|
*/
|
2023-05-24 02:53:53 +08:00
|
|
|
export function readPage(
|
|
|
|
name: string,
|
|
|
|
): Promise<string> {
|
|
|
|
return syscall("space.readPage", name);
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Write a page to the space.
|
|
|
|
* @param name the name of the page to write
|
|
|
|
* @param text the text of the page to write
|
|
|
|
* @returns the metadata for the written page
|
|
|
|
*/
|
2023-05-24 02:53:53 +08:00
|
|
|
export function writePage(name: string, text: string): Promise<PageMeta> {
|
|
|
|
return syscall("space.writePage", name, text);
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Delete a page from the space.
|
|
|
|
* @param name the name of the page to delete
|
|
|
|
*/
|
2023-05-24 02:53:53 +08:00
|
|
|
export function deletePage(name: string): Promise<void> {
|
|
|
|
return syscall("space.deletePage", name);
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* List all plugs in the space.
|
|
|
|
* @returns a list of all plugs in the space represented as FileMeta objects
|
|
|
|
*/
|
2023-12-07 01:44:48 +08:00
|
|
|
export function listPlugs(): Promise<FileMeta[]> {
|
2023-05-24 02:53:53 +08:00
|
|
|
return syscall("space.listPlugs");
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Lists all attachments in the space (all files not ending in .md).
|
|
|
|
* @returns a list of all attachments in the space represented as AttachmentMeta objects
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function listAttachments(): Promise<AttachmentMeta[]> {
|
2023-05-24 02:53:53 +08:00
|
|
|
return syscall("space.listAttachments");
|
|
|
|
}
|
2022-10-10 20:50:21 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Get metadata for an attachment in the space.
|
|
|
|
* @param name the path of the attachment to get metadata for
|
|
|
|
* @returns the metadata for the attachment
|
|
|
|
*/
|
2023-05-24 02:53:53 +08:00
|
|
|
export function getAttachmentMeta(name: string): Promise<AttachmentMeta> {
|
|
|
|
return syscall("space.getAttachmentMeta", name);
|
|
|
|
}
|
2023-01-13 22:41:29 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
/**
|
|
|
|
* Read an attachment from the space
|
|
|
|
* @param name path of the attachment to read
|
|
|
|
* @returns the attachment data encoded as a data URL
|
|
|
|
*/
|
|
|
|
export function readAttachment(
|
|
|
|
name: string,
|
2023-08-15 13:56:29 +08:00
|
|
|
): Promise<Uint8Array> {
|
2023-05-24 02:53:53 +08:00
|
|
|
return syscall("space.readAttachment", name);
|
|
|
|
}
|
2023-01-13 22:41:29 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
/**
|
|
|
|
* Writes an attachment to the space
|
|
|
|
* @param name path of the attachment to write
|
|
|
|
* @param data data itself
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function writeAttachment(
|
|
|
|
name: string,
|
2023-05-26 20:04:32 +08:00
|
|
|
data: Uint8Array,
|
2023-05-24 02:53:53 +08:00
|
|
|
): Promise<AttachmentMeta> {
|
2023-05-26 20:04:32 +08:00
|
|
|
return syscall("space.writeAttachment", name, data);
|
2022-10-10 20:50:21 +08:00
|
|
|
}
|
2023-01-13 22:41:29 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
/**
|
|
|
|
* Deletes an attachment from the space
|
|
|
|
* @param name path of the attachment to delete
|
|
|
|
*/
|
|
|
|
export function deleteAttachment(name: string): Promise<void> {
|
|
|
|
return syscall("space.deleteAttachment", name);
|
|
|
|
}
|
2023-08-20 23:51:00 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
// Lower level-file operations
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all files in the space (pages, attachments and plugs).
|
|
|
|
* @returns a list of all files in the space represented as FileMeta objects
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function listFiles(): Promise<FileMeta[]> {
|
|
|
|
return syscall("space.listFiles");
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Read a file from the space as a Uint8Array.
|
|
|
|
* @param name the name of the file to read
|
|
|
|
* @returns the data of the file
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function readFile(name: string): Promise<Uint8Array> {
|
|
|
|
return syscall("space.readFile", name);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Get metadata for a file in the space.
|
|
|
|
* @param name the name of the file to get metadata for
|
|
|
|
* @returns the metadata for the file
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function getFileMeta(name: string): Promise<FileMeta> {
|
|
|
|
return syscall("space.getFileMeta", name);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Write a file to the space.
|
|
|
|
* @param name the name of the file to write
|
|
|
|
* @param data the data of the file to write
|
|
|
|
* @returns the metadata for the written file
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function writeFile(
|
|
|
|
name: string,
|
|
|
|
data: Uint8Array,
|
|
|
|
): Promise<FileMeta> {
|
|
|
|
return syscall("space.writeFile", name, data);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Delete a file from the space.
|
|
|
|
* @param name the name of the file to delete
|
|
|
|
*/
|
2023-08-20 23:51:00 +08:00
|
|
|
export function deleteFile(name: string): Promise<void> {
|
|
|
|
return syscall("space.deleteFile", name);
|
|
|
|
}
|
2024-08-24 18:35:09 +08:00
|
|
|
|
|
|
|
export function fileExists(name: string): Promise<boolean> {
|
|
|
|
return syscall("space.fileExists", name);
|
|
|
|
}
|