Skip to content

Commit d7e8902

Browse files
committed
feat: implement os and better module access
Signed-off-by: Sam Gammon <sam@elide.ventures>
1 parent a8d1eb0 commit d7e8902

File tree

5 files changed

+336
-7
lines changed

5 files changed

+336
-7
lines changed

elide/runtime/js/modules/fs/fs.ts

+59
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,65 @@ export function access(path: string | Buffer | URL, mode: number = constants.F_O
3030
console.log('access fs', {path, mode, callback})
3131
}
3232

33+
/**
34+
* Read a file
35+
*
36+
* Asynchronously reads the entire contents of a file. The encoding option is ignored if data is a buffer.
37+
*
38+
* @param path Path to the file
39+
* @param options Options for reading the file
40+
* @param callback Callback function to dispatch with the file contents
41+
*/
42+
export function readFile(path: string | Buffer | URL, options: { encoding: string; flag?: string; }, callback: (err: NodeJS.ErrnoException, data: string) => void): void {
43+
console.log('readFile fs', {path, options, callback})
44+
}
45+
46+
/**
47+
* Read a file synchronously
48+
*
49+
* Synchronously reads the entire contents of a file. The encoding option is ignored if data is a buffer.
50+
*
51+
* @param path Path to the file
52+
* @param options Options for reading the file
53+
* @returns The file contents
54+
*/
55+
export function readFileSync(path: string | Buffer | URL, options: { encoding: string; flag?: string; }): string {
56+
console.log('readFileSync fs', {path, options})
57+
return ''
58+
}
59+
60+
/**
61+
* Write a file
62+
*
63+
* Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.
64+
*
65+
* @param path Path to the file
66+
* @param data Data to write to the file
67+
* @param options Options for writing the file
68+
* @param callback Callback function to dispatch with the file contents
69+
*/
70+
export function writeFile(path: string | Buffer | URL, data: any, options: { encoding: string; mode?: number; flag?: string; }, callback: (err: NodeJS.ErrnoException) => void): void {
71+
console.log('writeFile fs', {path, data, options, callback})
72+
}
73+
74+
/**
75+
* Write a file synchronously
76+
*
77+
* Synchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.
78+
*
79+
* @param path Path to the file
80+
* @param data Data to write to the file
81+
* @param options Options for writing the file
82+
*/
83+
export function writeFileSync(path: string | Buffer | URL, data: any, options: { encoding: string; mode?: number; flag?: string; }): void {
84+
console.log('writeFileSync fs', {path, data, options})
85+
}
86+
3387
export default {
3488
access,
89+
constants,
90+
readFile,
91+
readFileSync,
92+
writeFile,
93+
writeFileSync,
3594
};

elide/runtime/js/modules/fs/promises.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
* Provides a shim which offers a `fs/promises` module implementation that is compatible with Node.js-style imports.
55
*/
66

7-
export default {
8-
hi: "hey"
9-
};
7+
export default {};
8+

elide/runtime/js/modules/os/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
export {};
2+
export * from "./os";

elide/runtime/js/modules/os/os.ts

+265-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,266 @@
1+
/**
2+
* Intrinsic: OS.
3+
*
4+
* Provides a shim which offers a `os` module implementation that is compatible with Node.js-style imports.
5+
*/
16

2-
export {};
7+
function intrinsic(): any {
8+
// @ts-expect-error intrinsic symbol
9+
const api = __Elide_node_os__;
10+
if (!api) {
11+
throw new Error(`The 'os' module failed to load the intrinsic API.`);
12+
}
13+
return api || {};
14+
}
15+
16+
/**
17+
* Resolves operating system constants for the current platform.
18+
*/
19+
export const constants = {};
20+
21+
/**
22+
* Returns the operating system's end-of-line value.
23+
*/
24+
export const EOL = intrinsic().EOL;
25+
26+
/**
27+
* Returns the operating system's null device path.
28+
*/
29+
export const devNull = intrinsic().devNull;
30+
31+
/**
32+
* Returns the number of logical CPUs available to the current process.
33+
*
34+
* @returns The number of logical CPUs available to the current process
35+
*/
36+
export function availableParallelism(): number {
37+
return intrinsic().availableParallelism() as number;
38+
}
39+
40+
/**
41+
* Returns the operating system CPU architecture for which the Elide binary was compiled.
42+
*
43+
* Possible values follow the Node API:
44+
* 'arm', 'arm64', 'ia32', 'loong64', 'mips', 'mipsel', 'ppc', 'ppc64', 'riscv64', 's390', 's390x', and 'x64'.
45+
*
46+
* @returns The operating system CPU architecture for which the Elide binary was compiled
47+
*/
48+
export function arch(): string {
49+
return intrinsic().arch() as string;
50+
}
51+
52+
/**
53+
* Describes CPU info as part of a return value from `cpus`.
54+
*/
55+
export type CpuInfo = {
56+
model: string;
57+
speed: number;
58+
times: {
59+
user: number;
60+
nice: number;
61+
sys: number;
62+
idle: number;
63+
irq: number;
64+
};
65+
};
66+
67+
/**
68+
* Returns an array of objects containing information about each logical CPU core.
69+
*
70+
* @returns An array of objects containing information about each logical CPU core
71+
*/
72+
export function cpus(): CpuInfo[] {
73+
return intrinsic().cpus() as CpuInfo[];
74+
}
75+
76+
/**
77+
* Returns the endianness of the CPU for which the Elide binary was compiled.
78+
*
79+
* Possible values are 'BE' for big endian and 'LE' for little endian.
80+
*
81+
* @returns The endianness of the CPU for which the Elide binary was compiled
82+
*/
83+
export function endianness(): "BE" | "LE" {
84+
return intrinsic().endianness() as "BE" | "LE";
85+
}
86+
87+
/**
88+
* Returns the amount of free system memory in bytes.
89+
*
90+
* @returns The amount of free system memory in bytes
91+
*/
92+
export function freemem(): number {
93+
return intrinsic().freemem() as number;
94+
}
95+
96+
/**
97+
* Returns the priority value for the provided process ID, or the current process, if no ID is given.
98+
*
99+
* @param pid Process ID to get the priority for
100+
* @returns The priority value for the provided process ID
101+
*/
102+
export function getPriority(pid?: number): number {
103+
return intrinsic().getPriority(pid) as number;
104+
}
105+
106+
/**
107+
* Returns the home directory of the current user.
108+
*
109+
* @returns The home directory of the current user
110+
*/
111+
export function homedir(): string {
112+
return intrinsic().homedir() as string;
113+
}
114+
115+
/**
116+
* Returns the hostname of the operating system.
117+
*
118+
* @returns The hostname of the operating system
119+
*/
120+
export function hostname(): string {
121+
return intrinsic().hostname() as string;
122+
}
123+
124+
/**
125+
* Returns the load average of the system.
126+
*
127+
* @returns Load averages
128+
*/
129+
export function loadavg(): number[] {
130+
return intrinsic().loadavg() as number[];
131+
}
132+
133+
/**
134+
* Returns the machine type.
135+
*
136+
* @returns The machine type
137+
*/
138+
export function machine(): string {
139+
return intrinsic().machine() as string;
140+
}
141+
142+
/**
143+
* Object shape of a network interface info payload returned by `networkInterfaces`.
144+
*/
145+
export type NetworkInterfaceInfo = {
146+
address: string;
147+
netmask: string;
148+
family: string;
149+
mac: string;
150+
internal: boolean;
151+
cidr: string;
152+
scopeid?: number;
153+
}
154+
155+
/**
156+
* Returns network interfaces.
157+
*
158+
* @returns Network interfaces
159+
*/
160+
export function networkInterfaces(): { [key: string]: NetworkInterfaceInfo[] } {
161+
return intrinsic().networkInterfaces() as { [key: string]: NetworkInterfaceInfo[] };
162+
}
163+
164+
/**
165+
* Returns the operating system platform.
166+
*
167+
* Possible values are 'aix', 'android', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.
168+
*
169+
* @returns The operating system platform
170+
*/
171+
export function platform(): string {
172+
return intrinsic().platform() as string;
173+
}
174+
175+
/**
176+
* Returns the operating system as a string.
177+
*
178+
* @returns The operating system as a string
179+
*/
180+
export function release(): string {
181+
return intrinsic().release() as string;
182+
}
183+
184+
/**
185+
* Attempts to set the priority of the provided process ID, or the current process, if no ID is given.
186+
*
187+
* @param pidOrPriority Process ID to set the priority for, or the priority value to set if the value
188+
* should be set for the current process. Default is `0`.
189+
* @param priority Priority value to set for the provided process ID
190+
*/
191+
export function setPriority(pidOrPriority: number, priority?: number): void {
192+
intrinsic().setPriority(pidOrPriority, priority);
193+
}
194+
195+
/**
196+
* Returns the operating system's default directory for temporary files as a string.
197+
*
198+
* @returns The operating system's default directory for temporary files
199+
*/
200+
export function tmpdir(): string {
201+
return intrinsic().tmpdir() as string;
202+
}
203+
204+
/**
205+
* Returns the total amount of system memory in bytes.
206+
*
207+
* @returns The total amount of system memory in bytes
208+
*/
209+
export function totalmem(): number {
210+
return intrinsic().totalmem() as number;
211+
}
212+
213+
/**
214+
* Returns the operating system type as a string.
215+
*
216+
* @returns The operating system type as a string
217+
*/
218+
export function type(): string {
219+
return intrinsic().type() as string;
220+
}
221+
222+
/**
223+
* Returns the system uptime in seconds.
224+
*
225+
* @returns The system uptime in seconds
226+
*/
227+
export function uptime(): number {
228+
return intrinsic().uptime() as number;
229+
}
230+
231+
/**
232+
* Options which can be passed into the `userInfo` method.
233+
*/
234+
export type UserInfoOptions = {
235+
encoding?: BufferEncoding;
236+
}
237+
238+
/**
239+
* Object shape of a user info payload returned by `userInfo`.
240+
*/
241+
export type UserInfo = {
242+
username: string;
243+
uid: number;
244+
gid: number;
245+
shell: string;
246+
homedir: string;
247+
}
248+
249+
/**
250+
* Returns information about the currently effective user.
251+
*
252+
* @param options Options for retrieving user information
253+
* @returns Information about the currently effective user
254+
*/
255+
export function userInfo(options?: UserInfoOptions): UserInfo {
256+
return intrinsic().userInfo(options) as UserInfo;
257+
}
258+
259+
/**
260+
* Returns the operating system version as a string.
261+
*
262+
* @returns The operating system version as a string
263+
*/
264+
export function version(): string {
265+
return intrinsic().version() as string;
266+
}

elide/runtime/js/modules/process/process.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
* Provides a shim which offers a `process` module implementation that is compatible with Node.js-style imports.
55
*/
66

7-
const internals: any = globalThis['__Elide_node_process__'];
7+
function intrinsic(): any {
8+
// @ts-expect-error intrinsic symbol
9+
const api = __Elide_node_process__;
10+
if (!api) {
11+
throw new Error(`The 'process' module failed to load the intrinsic API.`);
12+
}
13+
return api || {};
14+
}
815

916
/**
1017
* Intrinsic: Process.
@@ -84,7 +91,7 @@ export interface NodeProcessAPI {
8491
}
8592

8693
function getProcessAPI(): NodeProcessAPI {
87-
return internals as NodeProcessAPI || {};
94+
return intrinsic() as NodeProcessAPI || {};
8895
}
8996

9097
/**

0 commit comments

Comments
 (0)