Skip to content
File

Blob: src/pyodide/types/filesystem.d.ts

typescript155 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5interface Reader {
6 read: (offset: number, dest: Uint8Array) => number;
7}
8 
9interface TarFSInfo {
10 children: Map<string, TarFSInfo> | undefined;
11 mode: number;
12 type: string;
13 modtime: number;
14 size: number;
15 path: string;
16 name: string;
17 parts: string[];
18 contentsOffset?: number;
19 reader: Reader | null;
20}
21 
22declare type MetadataDirInfo = Map<string, MetadataFSInfo>;
23declare type MetadataFSInfo = MetadataDirInfo | number; // file infos are numbers and dir infos are maps
24 
25interface FSLookupResult<Info> {
26 node: FSNode<Info>;
27}
28 
29interface FS {
30 mkdir: (dirname: string) => void;
31 mkdirTree: (dirname: string) => void;
32 writeFile: (
33 fname: string,
34 contents: Uint8Array | string,
35 options?: { canOwn: boolean }
36 ) => void;
37 readFile: (fname: string, options?: { encoding?: string }) => Uint8Array;
38 mount(fs: object, options: { info?: any }, path: string): void;
39 createNode<Info>(
40 parent: FSNode<Info> | null,
41 name: string,
42 mode: number
43 ): FSNode<Info>;
44 lookupPath<Info>(path: string): FSLookupResult<Info>;
45 open<Info>(nodeOrPath: FSNode<Info> | string, flags?: number): FSStream<Info>;
46 read<Info>(
47 stream: FSStream<Info>,
48 buffer: Uint8Array,
49 offset: number,
50 length: number,
51 position: number
52 ): number;
53 close<Info>(stream: FSStream<Info>): void;
54 isFile: (mode: number) => boolean;
55 readdir: (path: string) => string[];
56 genericErrors: { 44: Error };
57 sitePackages: string;
58 sessionSitePackages: string;
59 ErrnoError: { new (errno: number): Error };
60}
61 
62interface FSOps<Info> {
63 getNodeMode: (
64 parent: FSNode<Info> | null,
65 name: string,
66 info: Info
67 ) => {
68 permissions: number;
69 isDir: boolean;
70 };
71 setNodeAttributes: (
72 node: FSNode<Info>,
73 info: Info | undefined,
74 isDir: boolean
75 ) => void;
76 readdir: (node: FSNode<Info>) => string[];
77 lookup: (parent: FSNode<Info>, name: string) => Info;
78 read: (
79 stream: FSStream<Info>,
80 position: number,
81 buffer: Uint8Array
82 ) => number;
83}
84 
85interface MountOpts<Info> {
86 opts: { info: Info };
87}
88 
89interface FSNodeOps<Info> {
90 getattr: (node: FSNode<Info>) => FSAttr;
91 readdir: (node: FSNode<Info>) => string[];
92 lookup: (parent: FSNode<Info>, name: string) => FSNode<Info>;
93}
94 
95interface FSStream<Info> {
96 node: FSNode<Info>;
97 position: number;
98}
99 
100interface FSStreamOps<Info> {
101 llseek: (stream: FSStream<Info>, offset: number, whence: number) => number;
102 read: (
103 stream: FSStream<Info>,
104 buffer: Uint8Array,
105 offset: number,
106 length: number,
107 position: number
108 ) => number;
109}
110 
111interface FSMount {
112 type: EmscriptenFS<any>;
113}
114 
115interface FSNode<Info> {
116 id: number;
117 usedBytes: number;
118 mode: number;
119 modtime: number;
120 node_ops: FSNodeOps<Info>;
121 stream_ops: FSStreamOps<Info>;
122 mount: FSMount;
123 info: Info;
124 contentsOffset?: number | undefined;
125 tree?: MetadataDirInfo;
126 index?: number;
127}
128 
129interface FSAttr {
130 dev: number;
131 ino: number;
132 mode: number;
133 nlink: number;
134 uid: number;
135 gid: number;
136 rdev: number;
137 size: number;
138 atime: Date;
139 mtime: Date;
140 ctime: Date;
141 blksize: number;
142 blocks: number;
143}
144 
145interface EmscriptenFS<Info> {
146 mount: (mount: MountOpts<Info>) => FSNode<Info>;
147 createNode: (
148 parent: FSNode<Info> | null,
149 name: string,
150 info: Info
151 ) => FSNode<Info>;
152 node_ops: FSNodeOps<Info>;
153 stream_ops: FSStreamOps<Info>;
154}