yavascript 0.0.0 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # yavascript
2
+
3
+ <!-- NOTE: this file is also used as help text by the CLI. Don't use markdown syntax below this line unless said syntax would also be clearly readable as plain text. -->
4
+
5
+ YavaScript is a bash-like script runner which is distributed as a single
6
+ statically-linked binary. Scripts are written in JavaScript. There are global
7
+ APIs available for all the things you'd normally want to do in a bash script,
8
+ such as:
9
+
10
+ - Running programs
11
+ - Accessing environment variables
12
+ - Reading and writing file contents
13
+ - Checking if files/folders exist
14
+ - Removing files/folders
15
+ - Reading and changing the current working directory
16
+ - Using globs to get large lists of files
17
+ - Printing stylized text to the terminal
18
+
19
+ Additionally, since it's JavaScript, you get some other features that are
20
+ either not present in bash or are cumbersome to use in bash, namely:
21
+
22
+ - Arrays (lists) and Objects (key/value dictionaries)
23
+ - Working with path strings
24
+ - Working with JSON
25
+ - Cross-file import/export using ECMAScript Modules
26
+ - Splitting strings on delimeters
27
+ - Pretty-printing of objects
28
+ - Getting the path to the currently-running script (via import.meta.url or `__filename`)
29
+ - Getting the absolute path to the root folder of the current git/mercurial repo (repoRoot function)
30
+
31
+ To view the APIs, consult the file yavascript.d.ts which was distributed with
32
+ this program, or online at https://github.com/suchipi/yavascript/blob/main/yavascript.d.ts.
33
+ This file contains TypeScript type definitions which can be given to your IDE
34
+ to assist you when writing scripts.
35
+
36
+ YavaScript is powered by a fork of the QuickJS JavaScript Engine, originally
37
+ written by Fabrice Bellard. QuickJS is a small, fast JavaScript engine
38
+ supporting the ES2020 specification.
39
+
40
+ - Original QuickJS engine: https://bellard.org/quickjs/
41
+ - The fork we use: https://github.com/suchipi/quickjs/
42
+
43
+ YavaScript is written with <3 by Lily Scott.
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,30 @@
1
+ var path = require("path");
2
+
3
+ var binaryPath;
4
+ if (process.platform === "win32") {
5
+ binaryPath = path.resolve(
6
+ __dirname,
7
+ "..",
8
+ "bin",
9
+ "windows",
10
+ "yavascript.exe"
11
+ );
12
+ } else if (process.platform === "darwin") {
13
+ if (process.arch.startsWith("arm")) {
14
+ binaryPath = path.resolve(
15
+ __dirname,
16
+ "..",
17
+ "bin",
18
+ "darwin-arm",
19
+ "yavascript"
20
+ );
21
+ } else {
22
+ binaryPath = path.resolve(__dirname, "..", "bin", "darwin", "yavascript");
23
+ }
24
+ } else if (process.platform === "linux") {
25
+ binaryPath = path.resolve(__dirname, "..", "bin", "linux", "yavascript");
26
+ } else {
27
+ throw new Error("Unsupported platform: " + process.platform);
28
+ }
29
+
30
+ module.exports = binaryPath;
package/lib/cli.js ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env node
2
+ var child_process = require("child_process");
3
+ var binaryPath = require("./binary-path");
4
+
5
+ try {
6
+ child_process.execFileSync(binaryPath, process.argv.slice(2), {
7
+ cwd: process.cwd(),
8
+ stdio: "inherit",
9
+ });
10
+ } catch (err) {
11
+ process.exitCode = 1;
12
+ }
package/lib/index.js ADDED
@@ -0,0 +1,7 @@
1
+ const binaryPath = require("./binary-path");
2
+ const version = require("./package.json").version;
3
+
4
+ module.exports = {
5
+ binaryPath,
6
+ version,
7
+ };
package/package.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "name": "yavascript",
3
- "version": "0.0.0",
4
- "main": "index.js",
5
- "license": "MIT"
3
+ "version": "0.0.3",
4
+ "main": "lib/index.js",
5
+ "bin": "lib/cli.js",
6
+ "types": "yavascript.d.ts",
7
+ "author": "Lily Scott <me@suchipi.com>",
8
+ "license": "MIT",
9
+ "repository": "suchipi/yavascript"
6
10
  }
@@ -0,0 +1,1031 @@
1
+
2
+ // ---------------
3
+ // YavaScript APIs
4
+ // ---------------
5
+
6
+ /** The name of the current file (whether script or module). Alias for `os.realpath(std.getFileNameFromStack())`. */
7
+ declare const __filename: string;
8
+
9
+ /** The name of the directory the current file is inside of. */
10
+ declare const __dirname: string;
11
+
12
+ /**
13
+ * An object representing the process's environment variables. You can read
14
+ * from it to read environment variables, write into it to set environment
15
+ * variables, and/or delete properties from it to unset environment variables.
16
+ * Any value you write will be coerced into a string.
17
+ */
18
+ declare const env: { [key: string]: string | undefined };
19
+
20
+ type BaseExecOptions = {
21
+ /** Sets the current working directory for the child process. */
22
+ cwd?: string;
23
+
24
+ /** Sets environment variables within the process. */
25
+ env?: { [key: string | number]: string | number | boolean };
26
+ };
27
+
28
+ interface Exec {
29
+ (args: Array<string>): void;
30
+
31
+ (args: Array<string>, options: Record<string, never>): void;
32
+
33
+ (
34
+ args: Array<string>,
35
+ options: BaseExecOptions & {
36
+ /**
37
+ * Whether an Error should be thrown when the process exits with a nonzero
38
+ * status code.
39
+ *
40
+ * Defaults to true.
41
+ */
42
+ failOnNonZeroStatus: true;
43
+ /**
44
+ * If true, stdout and stderr will be collected into strings and returned
45
+ * instead of being printed to the screen.
46
+ *
47
+ * Defaults to false.
48
+ */
49
+ captureOutput: false;
50
+ }
51
+ ): void;
52
+
53
+ (
54
+ args: Array<string>,
55
+ options: BaseExecOptions & {
56
+ /**
57
+ * Whether an Error should be thrown when the process exits with a nonzero
58
+ * status code.
59
+ *
60
+ * Defaults to true.
61
+ */
62
+ failOnNonZeroStatus: false;
63
+ /**
64
+ * If true, stdout and stderr will be collected into strings and returned
65
+ * instead of being printed to the screen.
66
+ *
67
+ * Defaults to false.
68
+ */
69
+ captureOutput: false;
70
+ }
71
+ ): { status: number };
72
+
73
+ (
74
+ args: Array<string>,
75
+ options: BaseExecOptions & {
76
+ /**
77
+ * Whether an Error should be thrown when the process exits with a nonzero
78
+ * status code.
79
+ *
80
+ * Defaults to true.
81
+ */
82
+ failOnNonZeroStatus: false;
83
+ }
84
+ ): { status: number };
85
+
86
+ (
87
+ args: Array<string>,
88
+ options: BaseExecOptions & {
89
+ /**
90
+ * Whether an Error should be thrown when the process exits with a nonzero
91
+ * status code.
92
+ *
93
+ * Defaults to true.
94
+ */
95
+ failOnNonZeroStatus: true;
96
+ /**
97
+ * If true, stdout and stderr will be collected into strings and returned
98
+ * instead of being printed to the screen.
99
+ *
100
+ * Defaults to false.
101
+ */
102
+ captureOutput: true;
103
+ }
104
+ ): { stdout: string; stderr: string };
105
+
106
+ (
107
+ args: Array<string>,
108
+ options: BaseExecOptions & {
109
+ /**
110
+ * If true, stdout and stderr will be collected into strings and returned
111
+ * instead of being printed to the screen.
112
+ *
113
+ * Defaults to false.
114
+ */
115
+ captureOutput: true;
116
+ }
117
+ ): { stdout: string; stderr: string };
118
+
119
+ (
120
+ args: Array<string>,
121
+ options: BaseExecOptions & {
122
+ /**
123
+ * Whether an Error should be thrown when the process exits with a nonzero
124
+ * status code.
125
+ *
126
+ * Defaults to true.
127
+ */
128
+ failOnNonZeroStatus: false;
129
+ captureOutput: true;
130
+ }
131
+ ): { stdout: string; stderr: string; status: number };
132
+ }
133
+
134
+ /** Run a child process using the provided arguments. The first value in the arguments array is the program to run. */
135
+ declare const exec: Exec;
136
+
137
+ /** Alias for `exec(args, { captureOutput: true })` */
138
+ declare function $(args: Array<string>): {
139
+ stdout: string;
140
+ stderr: string;
141
+ };
142
+
143
+ /** Read the contents of a file from disk, as a string. */
144
+ declare function readFile(path: string): string;
145
+
146
+ /** Write the contents of a string or ArrayBuffer to a file. */
147
+ declare function writeFile(path: string, data: string | ArrayBuffer): void;
148
+
149
+ /** Returns true if the path points to a directory, or if the path points to a symlink which points to a directory. */
150
+ declare function isDir(path: string): boolean;
151
+
152
+ /** Delete the file or directory at the specified path. If the directory isn't empty, its contents will be deleted, too. */
153
+ declare function remove(path: string): void;
154
+
155
+ /** Returns true if a file or directory exists at the specified path. */
156
+ declare function exists(path: string): boolean;
157
+
158
+ /** Change the process's current working directory to the specified path. */
159
+ declare function cd(path: string): void;
160
+
161
+ /** Return the process's current working directory. */
162
+ declare function pwd(): string;
163
+
164
+ /** Removes the final component from a path string. */
165
+ declare function dirname(path: string): string;
166
+
167
+ /**
168
+ * The separator character the host operative system uses between path
169
+ * components, ie. the slashes in a filepath. On windows, it's a backslash, and
170
+ * on all other OSes, it's a forward slash.
171
+ */
172
+ declare const OS_PATH_SEPARATOR: "/" | "\\";
173
+
174
+ /**
175
+ * Create a path string from one or more path or path component strings.
176
+ *
177
+ * Trailing slashes and duplicate path separators will be removed. Any slashes
178
+ * or backslashes that do not match the requested path separator character
179
+ * (which defaults to {@link OS_PATH_SEPARATOR}) will be converted to the
180
+ * requested path separator. If multiple strings are passed, they will be
181
+ * joined together using the requested path separator.
182
+ *
183
+ * This function does not resolve `..` or `.`. Use {@link realpath} for that.
184
+ *
185
+ * To request a path separator other than {@link OS_PATH_SEPARATOR}, pass an
186
+ * object like `{ separator: "/" }` as the final argument to `makePath`.
187
+ *
188
+ * @param parts strings containing path components that should be present in
189
+ * the returned path string.
190
+ */
191
+ declare function makePath(
192
+ ...parts: Array<string | { separator: string }>
193
+ ): string;
194
+
195
+ /**
196
+ * Returns the absolute path to the root folder of the git/hg repo.
197
+ *
198
+ * This is done by running `git rev-parse --show-toplevel` and `hg root`.
199
+ *
200
+ * If `relativeTo` is provided, the git and hg commands will be executed in that
201
+ */
202
+ declare function repoRoot(relativeTo?: string): string;
203
+
204
+ /**
205
+ * Returns whether the provided path is ignored by git.
206
+ */
207
+ declare function isGitignored(path: string): boolean
208
+
209
+ /**
210
+ * Return the contents of a directory, as absolute paths. `.` and `..` are
211
+ * omitted.
212
+ *
213
+ * Use the `relativePaths` option to get relative paths instead (relative to
214
+ * the parent directory).
215
+ */
216
+ declare function ls(
217
+ dir?: string,
218
+ options?: { relativePaths?: boolean }
219
+ ): Array<string>;
220
+
221
+ /** Get the absolute path given a relative path. Symlinks are also resolved. */
222
+ declare function realpath(path: string): string;
223
+
224
+ /** Read a symlink. */
225
+ declare function readlink(path: string): string;
226
+
227
+ /**
228
+ * Search the filesystem for files matching the specified glob patterns. Uses [minimatch](https://www.npmjs.com/package/minimatch) with its default options.
229
+ *
230
+ * `followSymlinks` defaults to false.
231
+ */
232
+ declare function glob(
233
+ dir: string,
234
+ patterns: Array<string>,
235
+ options?: { followSymlinks?: boolean }
236
+ ): Array<string>;
237
+
238
+ /** Print a value or values to stdout. */
239
+ declare const echo: typeof console.log;
240
+
241
+ /** Convert a value to a string, using the same logic as `echo` and `console.log`. */
242
+ declare function inspect(value: any): string;
243
+
244
+ /** Remove ANSI control characters from a string. */
245
+ declare function stripAnsi(input: string): string;
246
+
247
+ /** Wrap a string in double quotes, and escape any double-quotes inside with `\"`. */
248
+ declare function quote(input: string): string;
249
+
250
+ // Colors
251
+
252
+ /** Wrap a string with the ANSI control characters that will make it print as black text. */
253
+ declare function black(input: string | number): string;
254
+ /** Wrap a string with the ANSI control characters that will make it print as red text. */
255
+ declare function red(input: string | number): string;
256
+ /** Wrap a string with the ANSI control characters that will make it print as green text. */
257
+ declare function green(input: string | number): string;
258
+ /** Wrap a string with the ANSI control characters that will make it print as yellow text. */
259
+ declare function yellow(input: string | number): string;
260
+ /** Wrap a string with the ANSI control characters that will make it print as blue text. */
261
+ declare function blue(input: string | number): string;
262
+ /** Wrap a string with the ANSI control characters that will make it print as magenta text. */
263
+ declare function magenta(input: string | number): string;
264
+ /** Wrap a string with the ANSI control characters that will make it print as cyan text. */
265
+ declare function cyan(input: string | number): string;
266
+ /** Wrap a string with the ANSI control characters that will make it print as white text. */
267
+ declare function white(input: string | number): string;
268
+ /** Wrap a string with the ANSI control characters that will make it print as gray text. */
269
+ declare function gray(input: string | number): string;
270
+ /** Wrap a string with the ANSI control characters that will make it print as grey text. */
271
+ declare function grey(input: string | number): string;
272
+
273
+ // Background Colors
274
+
275
+ /** Wrap a string with the ANSI control characters that will make it have a black background. */
276
+ declare function bgBlack(input: string | number): string;
277
+ /** Wrap a string with the ANSI control characters that will make it have a red background. */
278
+ declare function bgRed(input: string | number): string;
279
+ /** Wrap a string with the ANSI control characters that will make it have a green background. */
280
+ declare function bgGreen(input: string | number): string;
281
+ /** Wrap a string with the ANSI control characters that will make it have a yellow background. */
282
+ declare function bgYellow(input: string | number): string;
283
+ /** Wrap a string with the ANSI control characters that will make it have a blue background. */
284
+ declare function bgBlue(input: string | number): string;
285
+ /** Wrap a string with the ANSI control characters that will make it have a magenta background. */
286
+ declare function bgMagenta(input: string | number): string;
287
+ /** Wrap a string with the ANSI control characters that will make it have a cyan background. */
288
+ declare function bgCyan(input: string | number): string;
289
+ /** Wrap a string with the ANSI control characters that will make it have a white background. */
290
+ declare function bgWhite(input: string | number): string;
291
+
292
+ // Modifiers
293
+
294
+ /** Wrap a string with the ANSI control character that resets all styling. */
295
+ declare function reset(input: string | number): string;
296
+ /** Wrap a string with the ANSI control characters that will make it print with a bold style. */
297
+ declare function bold(input: string | number): string;
298
+ /** Wrap a string with the ANSI control characters that will make it print with a dimmed style. */
299
+ declare function dim(input: string | number): string;
300
+ /** Wrap a string with the ANSI control characters that will make it print italicized. */
301
+ declare function italic(input: string | number): string;
302
+ /** Wrap a string with the ANSI control characters that will make it print underlined. */
303
+ declare function underline(input: string | number): string;
304
+ /** Wrap a string with ANSI control characters such that its foreground (text) and background colors are swapped. */
305
+ declare function inverse(input: string | number): string;
306
+ /** Wrap a string with ANSI control characters such that it is hidden. */
307
+ declare function hidden(input: string | number): string;
308
+ /** Wrap a string with the ANSI control characters that will make it print with a horizontal line through its center. */
309
+ declare function strikethrough(input: string | number): string;
310
+
311
+ // ------------------------------------------
312
+ // QuickJS APIs, which YavaScript builds upon
313
+ // ------------------------------------------
314
+
315
+ // Definitions of the globals and modules added by quickjs-libc
316
+
317
+ /**
318
+ * Provides the command line arguments. The first argument is the script name.
319
+ */
320
+ declare var scriptArgs: Array<string>;
321
+
322
+ /**
323
+ * Print the arguments separated by spaces and a trailing newline.
324
+ *
325
+ * Non-string args are coerced into a string via [ToString](https://tc39.es/ecma262/#sec-tostring).
326
+ * Objects can override the default `ToString` behavior by defining a `toString` method.
327
+ */
328
+ declare var print: (...args: Array<any>) => void;
329
+
330
+ /**
331
+ * Object that provides functions for logging information.
332
+ */
333
+ declare var console: {
334
+ /** Same as {@link print}(). */
335
+ log: typeof print;
336
+ };
337
+
338
+ declare module "std" {
339
+ /**
340
+ * Exit the process with the provided status code.
341
+ *
342
+ * @param statusCode The exit code; 0 for success, nonzero for failure.
343
+ */
344
+ export function exit(statusCode: number): void;
345
+
346
+ /**
347
+ * Evaluate the string `code` as a script (global eval).
348
+ *
349
+ * @param code - The code to evaluate.
350
+ * @param options - An optional object containing the following optional properties:
351
+ * @property backtraceBarrier - Boolean (default = false). If true, error backtraces do not list the stack frames below the evalScript.
352
+ * @returns The result of the evaluation.
353
+ */
354
+ export function evalScript(
355
+ code: string,
356
+ options?: { backtraceBarrier?: boolean }
357
+ ): any;
358
+
359
+ /**
360
+ * Evaluate the file `filename` as a script (global eval).
361
+ *
362
+ * @param filename - The relative or absolute path to the file to load. Relative paths are resolved relative to the process's current working directory.
363
+ * @returns The result of the evaluation.
364
+ */
365
+ export function loadScript(filename: string): any;
366
+
367
+ /**
368
+ * Evaluate the file `filename` as a module. Effectively a synchronous dynamic `import()`.
369
+ *
370
+ * @param filename - The relative or absolute path to the file to import. Relative paths are resolved relative to the file calling `importModule`, or `basename` if present.
371
+ * @param basename - If present and `filename` is a relative path, `filename` will be resolved relative to this basename.
372
+ * @returns The result of the evaluation (module namespace object).
373
+ */
374
+ export function importModule(
375
+ filename: string,
376
+ basename?: string
377
+ ): { [key: string]: any };
378
+
379
+ /**
380
+ * Load the file `filename` and return it as a string assuming UTF-8 encoding.
381
+ *
382
+ * @param filename - The relative or absolute path to the file to load. Relative paths are resolved relative to the process's current working directory.
383
+ */
384
+ export function loadFile(filename: string): string;
385
+
386
+ /**
387
+ * Read the script of module filename from an active stack frame, then return it as a string.
388
+ *
389
+ * If there isn't a valid filename for the specified stack frame, an error will be thrown.
390
+ *
391
+ * @param stackLevels - How many levels up the stack to search for a filename. Defaults to 0, which uses the current stack frame.
392
+ */
393
+ export function getFileNameFromStack(stackLevels: number): string;
394
+
395
+ /**
396
+ * Open a file (wrapper to the libc `fopen()`).
397
+ * Return the FILE object.
398
+ *
399
+ * @param filename - The relative or absolute path to the file to open. Relative paths are resolved relative to the process's current working directory.
400
+ * @param flags - A string containing any combination of the characters 'r', 'w', 'a', '+', and/or 'b'.
401
+ * @returns The opened FILE object.
402
+ */
403
+ export function open(filename: string, flags: string): FILE;
404
+
405
+ /**
406
+ * Open a process by creating a pipe (wrapper to the libc `popen()`).
407
+ * Return the FILE object.
408
+ *
409
+ * @param command - The command line to execute. Gets passed via `/bin/sh -c`.
410
+ * @param flags - A string containing any combination of the characters 'r', 'w', 'a', '+', and/or 'b'.
411
+ * @returns The opened FILE object.
412
+ */
413
+ export function popen(command: string, flags: string): FILE;
414
+
415
+ /**
416
+ * Open a file from a file handle (wrapper to the libc `fdopen()`).
417
+ * Return the FILE object.
418
+ *
419
+ * @param fd - The file handle to open.
420
+ * @param flags - A string containing any combination of the characters 'r', 'w', 'a', '+', and/or 'b'.
421
+ * @returns The opened FILE object.
422
+ */
423
+ export function fdopen(fd: number, flags: string): FILE;
424
+
425
+ /**
426
+ * Open a temporary file.
427
+ * Return the FILE object.
428
+ *
429
+ * @returns The opened FILE object.
430
+ */
431
+ export function tmpfile(): FILE;
432
+
433
+ /** Equivalent to `std.out.puts(str)`. */
434
+ export function puts(str: string): void;
435
+
436
+ /** Equivalent to `std.out.printf(fmt, ...args)` */
437
+ export function printf(fmt: string, ...args: Array<any>): void;
438
+
439
+ /** Equivalent to the libc sprintf(). */
440
+ export function sprintf(fmt: string, ...args: Array<any>): void;
441
+
442
+ /** Wrapper to the libc file stdin. */
443
+ var in_: FILE;
444
+
445
+ export { in_ as in };
446
+
447
+ /** Wrapper to the libc file stdout. */
448
+ export var out: FILE;
449
+
450
+ /** Wrapper to the libc file stderr. */
451
+ export var err: FILE;
452
+
453
+ /** Constant for {@link FILE.seek}. Declares that pointer offset should be relative to the beginning of the file. See also libc `fseek()`. */
454
+ export var SEEK_SET: number;
455
+
456
+ /** Constant for {@link FILE.seek}. Declares that the offset should be relative to the current position of the FILE handle. See also libc `fseek()`. */
457
+ export var SEEK_CUR: number;
458
+
459
+ /** Constant for {@link FILE.seek}. Declares that the offset should be relative to the end of the file. See also libc `fseek()`. */
460
+ export var SEEK_END: number;
461
+
462
+ /** Manually invoke the cycle removal algorithm (garbage collector). The cycle removal algorithm is automatically started when needed, so this function is useful in case of specific memory constraints or for testing. */
463
+ export function gc(): void;
464
+
465
+ /** Return the value of the environment variable `name` or `undefined` if it is not defined. */
466
+ export function getenv(name: string): string | undefined;
467
+
468
+ /** Set the value of the environment variable `name` to the string `value`. */
469
+ export function setenv(name: string, value: string): void;
470
+
471
+ /** Delete the environment variable `name`. */
472
+ export function unsetenv(name: string): void;
473
+
474
+ /** Return an object containing the environment variables as key-value pairs. */
475
+ export function getenviron(): { [key: string]: string | undefined };
476
+
477
+ interface UrlGet {
478
+ /**
479
+ * Download `url` using the `curl` command line utility. Returns string
480
+ * when the http status code is between 200 and 299, and throws otherwise.
481
+ *
482
+ * Pass an object with { full: true } as the second argument to get
483
+ * response headers and status code.
484
+ */
485
+ (url: string): string;
486
+
487
+ /**
488
+ * Download `url` using the `curl` command line utility. Returns string
489
+ * when the http status code is between 200 and 299, and throws otherwise.
490
+ *
491
+ * Pass an object with { full: true } as the second argument to get
492
+ * response headers and status code.
493
+ */
494
+ (url: string, options: { binary: false }): string;
495
+
496
+ /**
497
+ * Download `url` using the `curl` command line utility. Returns string
498
+ * when the http status code is between 200 and 299, and throws otherwise.
499
+ *
500
+ * Pass an object with { full: true } as the second argument to get
501
+ * response headers and status code.
502
+ */
503
+ (url: string, options: { full: false }): string;
504
+
505
+ /**
506
+ * Download `url` using the `curl` command line utility. Returns string
507
+ * when the http status code is between 200 and 299, and throws otherwise.
508
+ *
509
+ * Pass an object with { full: true } as the second argument to get
510
+ * response headers and status code.
511
+ */
512
+ (url: string, options: { binary: false; full: false }): string;
513
+
514
+ /**
515
+ * Download `url` using the `curl` command line utility. Returns
516
+ * ArrayBuffer when the http status code is between 200 and 299, and throws
517
+ * otherwise.
518
+ *
519
+ * Pass an object with { full: true } as the second argument to get
520
+ * response headers and status code.
521
+ */
522
+ (url: string, options: { binary: true }): ArrayBuffer;
523
+
524
+ /**
525
+ * Download `url` using the `curl` command line utility. Returns
526
+ * ArrayBuffer when the http status code is between 200 and 299, and throws
527
+ * otherwise.
528
+ *
529
+ * Pass an object with { full: true } as the second argument to get
530
+ * response headers and status code.
531
+ */
532
+ (url: string, options: { binary: true; full: false }): ArrayBuffer;
533
+
534
+ /**
535
+ * Download `url` using the `curl` command line utility.
536
+ *
537
+ * Returns an object with three properties:
538
+ *
539
+ * - `response`: response body content (string)
540
+ * - `responseHeaders`: headers separated by CRLF (string)
541
+ * - `status`: status code (number)
542
+ */
543
+ (url: string, options: { full: true }): {
544
+ status: number;
545
+ response: string;
546
+ responseHeaders: string;
547
+ };
548
+
549
+ /**
550
+ * Download `url` using the `curl` command line utility.
551
+ *
552
+ * Returns an object with three properties:
553
+ *
554
+ * - `response`: response body content (string)
555
+ * - `responseHeaders`: headers separated by CRLF (string)
556
+ * - `status`: status code (number)
557
+ */
558
+ (url: string, options: { full: true; binary: false }): {
559
+ status: number;
560
+ response: string;
561
+ responseHeaders: string;
562
+ };
563
+
564
+ /**
565
+ * Download `url` using the `curl` command line utility.
566
+ *
567
+ * Returns an object with three properties:
568
+ *
569
+ * - `response`: response body content (ArrayBuffer)
570
+ * - `responseHeaders`: headers separated by CRLF (string)
571
+ * - `status`: status code (number)
572
+ */
573
+ (url: string, options: { full: true; binary: true }): {
574
+ status: number;
575
+ response: ArrayBuffer;
576
+ responseHeaders: string;
577
+ };
578
+ }
579
+
580
+ export var urlGet: UrlGet;
581
+
582
+ /**
583
+ * Parse `str` using a superset of JSON.parse. The following extensions are accepted:
584
+ *
585
+ * - Single line and multiline comments
586
+ * - unquoted properties (ASCII-only Javascript identifiers)
587
+ * - trailing comma in array and object definitions
588
+ * - single quoted strings
589
+ * - `\f` and `\v` are accepted as space characters
590
+ * - leading plus in numbers
591
+ * - octal (0o prefix) and hexadecimal (0x prefix) numbers
592
+ */
593
+ export function parseExtJSON(str: string): any;
594
+
595
+ /** An object representing a file handle. */
596
+ export class FILE {
597
+ /** Close the file. */
598
+ close(): void;
599
+
600
+ /** Outputs the string with the UTF-8 encoding. */
601
+ puts(str: string): void;
602
+
603
+ /**
604
+ * Formatted printf.
605
+ *
606
+ * The same formats as the standard C library `printf` are supported. Integer format types (e.g. `%d`) truncate the Numbers or BigInts to 32 bits. Use the `l` modifier (e.g. `%ld`) to truncate to 64 bits.
607
+ */
608
+ printf(fmt: string, ...args: Array<any>): void;
609
+
610
+ /** Flush the buffered file. Wrapper for C `fflush`. */
611
+ flush(): void;
612
+
613
+ /** Sync the buffered file to disk. Wrapper for C `fsync`. */
614
+ sync(): void;
615
+
616
+ /**
617
+ * Seek to a given file position (whence is `std.SEEK_*`).
618
+ *
619
+ * `offset` can be a number or a bigint.
620
+ */
621
+ seek(
622
+ offset: number,
623
+ whence: typeof SEEK_SET | typeof SEEK_CUR | typeof SEEK_END
624
+ ): void;
625
+
626
+ /** Return the current file position. */
627
+ tell(): number;
628
+
629
+ /** Return the current file position as a bigint. */
630
+ tello(): BigInt;
631
+
632
+ /** Return true if end of file. */
633
+ eof(): boolean;
634
+
635
+ /** Return the associated OS handle. */
636
+ fileno(): number;
637
+
638
+ /** Read `length` bytes from the file to the ArrayBuffer `buffer` at byte position `position` (wrapper to the libc `fread`). Returns the number of bytes read, or `0` if the end of the file has been reached. */
639
+ read(buffer: ArrayBuffer, position: number, length: number): number;
640
+
641
+ /** Write `length` bytes from the file to the ArrayBuffer `buffer` at byte position `position` (wrapper to the libc `fwrite`). Returns the number of bytes written. */
642
+ write(buffer: ArrayBuffer, position: number, length: number): number;
643
+
644
+ /** Return the next line from the file, assuming UTF-8 encoding, excluding the trailing line feed. */
645
+ getline(): string;
646
+
647
+ /** Read `maxSize` bytes from the file and return them as a string assuming UTF-8 encoding. If `maxSize` is not present, the file is read up its end. */
648
+ readAsString(maxSize?: number): string;
649
+
650
+ /** Return the next byte from the file. Return -1 if the end of file is reached. */
651
+ getByte(): number;
652
+
653
+ /** Write one byte to the file. */
654
+ putByte(value: number): void;
655
+ }
656
+ }
657
+
658
+ declare module "os" {
659
+ /**
660
+ * Open a file handle. Returns a number; the file descriptor.
661
+ *
662
+ * @param filename - The path to the file to open.
663
+ * @param flags - Numeric flags that set the mode to use when opening the file. See `os.O_*`
664
+ * @param mode - Octal access mask. Defaults to 0o666.
665
+ */
666
+ export function open(filename: string, flags: number, mode?: number): number;
667
+
668
+ /** POSIX open flag, used in {@link open}. */
669
+ export var O_RDONLY: number;
670
+
671
+ /** POSIX open flag, used in {@link open}. */
672
+ export var O_WRONLY: number;
673
+
674
+ /** POSIX open flag, used in {@link open}. */
675
+ export var O_RDWR: number;
676
+
677
+ /** POSIX open flag, used in {@link open}. */
678
+ export var O_APPEND: number;
679
+
680
+ /** POSIX open flag, used in {@link open}. */
681
+ export var O_CREAT: number;
682
+
683
+ /** POSIX open flag, used in {@link open}. */
684
+ export var O_EXCL: number;
685
+
686
+ /** POSIX open flag, used in {@link open}. */
687
+ export var O_TRUNC: number;
688
+
689
+ /** Windows-specific open flag: open the file in text mode. The default is binary mode. Used in {@link open}. */
690
+ export var O_TEXT: number;
691
+
692
+ /** Close the file with descriptor `fd`. */
693
+ export function close(fd: number): void;
694
+
695
+ interface OsSeek {
696
+ /** Seek in the file. Use `std.SEEK_*` for `whence`. `offset` is either a number or a bigint. If `offset` is a bigint, a bigint is returned too. */
697
+ (fd: number, offset: number, whence: number): number;
698
+
699
+ /** Seek in the file. Use `std.SEEK_*` for `whence`. `offset` is either a number or a bigint. If `offset` is a bigint, a bigint is returned too. */
700
+ (fd: number, offset: BigInt, whence: number): BigInt;
701
+ }
702
+
703
+ /** Seek in the file. Use `std.SEEK_*` for `whence`. `offset` is either a number or a bigint. If `offset` is a bigint, a bigint is returned too. */
704
+ export var seek: OsSeek;
705
+
706
+ /** Read `length` bytes from the file with descriptor `fd` to the ArrayBuffer `buffer` at byte position `offset`. Return the number of read bytes. */
707
+ export function read(
708
+ fd: number,
709
+ buffer: ArrayBuffer,
710
+ offset: number,
711
+ length: number
712
+ ): number;
713
+
714
+ /** Write `length` bytes to the file with descriptor `fd` from the ArrayBuffer `buffer` at byte position `offset`. Return the number of written bytes. */
715
+ export function write(
716
+ fd: number,
717
+ buffer: ArrayBuffer,
718
+ offset: number,
719
+ length: number
720
+ ): number;
721
+
722
+ /** Return `true` if the file opened with descriptor `fd` is a TTY (terminal). */
723
+ export function isatty(fd: number): boolean;
724
+
725
+ /** Return the TTY size as `[width, height]` or `null` if not available. */
726
+ export function ttyGetWinSize(fd: number): null | [number, number];
727
+
728
+ /** Set the TTY in raw mode. */
729
+ export function ttySetRaw(fd: number): void;
730
+
731
+ /** Remove a file. */
732
+ export function remove(filename: string): void;
733
+
734
+ /** Rename a file. */
735
+ export function rename(oldname: string, newname: string): void;
736
+
737
+ /** Return the canonicalized absolute pathname of `path`. */
738
+ export function realpath(path: string): string;
739
+
740
+ /** Return the current working directory. */
741
+ export function getcwd(): string;
742
+
743
+ /** Change the current directory. */
744
+ export function chdir(path: string): void;
745
+
746
+ /** Create a directory at `path`. */
747
+ export function mkdir(path: string, mode?: number): void;
748
+
749
+ export type Stats = {
750
+ dev: number;
751
+ ino: number;
752
+ mode: number;
753
+ nlink: number;
754
+ uid: number;
755
+ gid: number;
756
+ rdev: number;
757
+ size: number;
758
+ blocks: number;
759
+ atime: number;
760
+ mtime: number;
761
+ ctime: number;
762
+ };
763
+
764
+ /**
765
+ * Return a stats object with the following fields:
766
+ *
767
+ * - `dev`
768
+ * - `ino`
769
+ * - `mode`
770
+ * - `nlink`
771
+ * - `uid`
772
+ * - `gid`
773
+ * - `rdev`
774
+ * - `size`
775
+ * - `blocks`
776
+ * - `atime`
777
+ * - `mtime`
778
+ * - `ctime`
779
+ *
780
+ * The times are specified in milliseconds since 1970. `lstat()` is the same as `stat()` except that it returns information about the link itself.
781
+ */
782
+ export function stat(path: string): Stats;
783
+
784
+ /**
785
+ * Return a stats object with the following fields:
786
+ *
787
+ * - `dev`
788
+ * - `ino`
789
+ * - `mode`
790
+ * - `nlink`
791
+ * - `uid`
792
+ * - `gid`
793
+ * - `rdev`
794
+ * - `size`
795
+ * - `blocks`
796
+ * - `atime`
797
+ * - `mtime`
798
+ * - `ctime`
799
+ *
800
+ * The times are specified in milliseconds since 1970. `lstat()` is the same as `stat()` except that it returns information about the link itself.
801
+ */
802
+ export function lstat(path: string): Stats;
803
+
804
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
805
+ export var S_IFMT: number;
806
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
807
+ export var S_IFIFO: number;
808
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
809
+ export var S_IFCHR: number;
810
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
811
+ export var S_IFDIR: number;
812
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
813
+ export var S_IFBLK: number;
814
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
815
+ export var S_IFREG: number;
816
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
817
+ export var S_IFSOCK: number;
818
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
819
+ export var S_IFLNK: number;
820
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
821
+ export var S_ISGID: number;
822
+ /** Constant to interpret the `mode` property returned by `stat()`. Has the same value as in the C system header `sys/stat.h`. */
823
+ export var S_ISUID: number;
824
+
825
+ /**
826
+ * Change the access and modification times of the file path.
827
+ *
828
+ * The times are specified in milliseconds since 1970.
829
+ */
830
+ export function utimes(path: string, atime: number, mtime: number): void;
831
+
832
+ /** Create a link at `linkpath` containing the string `target`. */
833
+ export function symlink(target: string, linkpath: string): void;
834
+
835
+ /** Return the link target. */
836
+ export function readlink(path: string): string;
837
+
838
+ /** Return an array of strings containing the filenames of the directory `path`. */
839
+ export function readdir(path: string): Array<string>;
840
+
841
+ /** Add a read handler to the file with descriptor `fd`. `func` is called each time there is data pending for `fd`. A single read handler per file handle is supported. Use `func = null` to remove the handler. */
842
+ export function setReadHandler(fd: number, func: null | (() => void)): void;
843
+
844
+ /** Add a write handler to the file with descriptor `fd`. `func` is called each time data can be written to `fd`. A single write handler per file handle is supported. Use `func = null` to remove the handler. */
845
+ export function setWriteHandler(fd: number, func: null | (() => void)): void;
846
+
847
+ /** Call the function `func` when the signal `signal` happens. Only a single handler per signal number is supported. Use `null` to set the default handler or `undefined` to ignore the signal. Signal handlers can only be defined in the main thread. */
848
+ export function signal(
849
+ signal: number,
850
+ func: null | undefined | (() => void)
851
+ ): void;
852
+
853
+ /** POSIX signal number. */
854
+ export var SIGINT: number;
855
+
856
+ /** POSIX signal number. */
857
+ export var SIGABRT: number;
858
+
859
+ /** POSIX signal number. */
860
+ export var SIGFPE: number;
861
+
862
+ /** POSIX signal number. */
863
+ export var SIGILL: number;
864
+
865
+ /** POSIX signal number. */
866
+ export var SIGSEGV: number;
867
+
868
+ /** POSIX signal number. */
869
+ export var SIGTERM: number;
870
+
871
+ /** Send the signal `sig` to the process `pid`. Use `os.SIG*` constants. */
872
+ export function kill(pid: number, sig: number): void;
873
+
874
+ export type ExecOptions = {
875
+ /** Boolean (default = true). If true, wait until the process is terminated. In this case, `exec` returns the exit code if positive or the negated signal number if the process was interrupted by a signal. If false, do not block and return the process id of the child. */
876
+ block?: boolean;
877
+
878
+ /** Boolean (default = true). If true, the file is searched in the `PATH` environment variable. */
879
+ usePath?: boolean;
880
+
881
+ /** String (default = `args[0]`). Set the file to be executed. */
882
+ file?: string;
883
+
884
+ /** String. If present, set the working directory of the new process. */
885
+ cwd?: string;
886
+
887
+ /** If present, set the file descriptor in the child for stdin. */
888
+ stdin?: number;
889
+
890
+ /** If present, set the file descriptor in the child for stdout. */
891
+ stdout?: number;
892
+
893
+ /** If present, set the file descriptor in the child for stderr. */
894
+ stderr?: number;
895
+
896
+ /** Object. If present, set the process environment from the object key-value pairs. Otherwise use the same environment as the current process. To get the current process's environment variables as on object, use `std.getenviron()`. */
897
+ env?: { [key: string | number]: string | number | boolean };
898
+
899
+ /** Integer. If present, the process uid with `setuid`. */
900
+ uid?: number;
901
+
902
+ /** Integer. If present, the process gid with `setgid`. */
903
+ gid?: number;
904
+ };
905
+
906
+ /** Execute a process with the arguments args, and the provided options (if any). */
907
+ export function exec(args: Array<string>, options?: ExecOptions): number;
908
+
909
+ /**
910
+ * `waitpid` Unix system call. Returns the array [ret, status].
911
+ *
912
+ * From man waitpid(2):
913
+ *
914
+ * waitpid(): on success, returns the process ID of the child whose state has changed; if WNOHANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned.
915
+ */
916
+ export function waitpid(pid: number, options?: number): [number, number];
917
+
918
+ /** Constant for the `options` argument of `waitpid`. */
919
+ export var WNOHANG: number;
920
+
921
+ /** `dup` Unix system call. */
922
+ export function dup(fd: number): number;
923
+
924
+ /** `dup2` Unix system call. */
925
+ export function dup2(oldfd: number, newfd: number): number;
926
+
927
+ /** `pipe` Unix system call. Return two handles as `[read_fd, write_fd]`. */
928
+ export function pipe(): null | [number, number];
929
+
930
+ /** Sleep for `delay_ms` milliseconds. */
931
+ export function sleep(delay_ms: number): void;
932
+
933
+ export type Timer = number & { __is: "Timer" };
934
+
935
+ /** Call the function func after delay ms. Return a handle to the timer. */
936
+ export function setTimeout(func: () => void, delay: number): Timer;
937
+
938
+ /** Cancel a timer. */
939
+ export function clearTimeout(handle: Timer): void;
940
+
941
+ /** Return a string representing the platform: "linux", "darwin", "win32" or "js". */
942
+ export var platform: "linux" | "darwin" | "win32" | "js";
943
+
944
+ /**
945
+ * Things that can be put into Worker.postMessage.
946
+ *
947
+ * NOTE: This is effectively the same stuff as supported by the structured
948
+ * clone algorithm, but without support for Map/Set (not supported in
949
+ * QuickJS yet).
950
+ */
951
+ export type StructuredClonable =
952
+ | string
953
+ | number
954
+ | boolean
955
+ | null
956
+ | undefined
957
+ | Boolean
958
+ | String
959
+ | Date
960
+ | RegExp
961
+ | ArrayBuffer
962
+ | Int8Array
963
+ | Uint8Array
964
+ | Uint8ClampedArray
965
+ | Int16Array
966
+ | Uint16Array
967
+ | Int32Array
968
+ | Uint32Array
969
+ | Float32Array
970
+ | Float64Array
971
+ | BigInt64Array
972
+ | BigUint64Array
973
+ | DataView
974
+ | Array<StructuredClonable>
975
+ | SharedArrayBuffer
976
+ // Map and Set not yet supported
977
+ // | Map<StructuredClonable, StructuredClonable>
978
+ // | Set<StructuredClonable>
979
+ | { [key: string | number]: StructuredClonable };
980
+
981
+ export class Worker {
982
+ /**
983
+ * Constructor to create a new thread (worker) with an API close to the
984
+ * `WebWorkers`. `moduleFilename` is a string specifying the module
985
+ * filename which is executed in the newly created thread. As for
986
+ * dynamically imported module, it is relative to the current script or
987
+ * module path. Threads normally don’t share any data and communicate
988
+ * between each other with messages. Nested workers are not supported.
989
+ */
990
+ constructor(moduleFilename: string);
991
+
992
+ /**
993
+ * In the created worker, Worker.parent represents the parent worker and is
994
+ * used to send or receive messages.
995
+ */
996
+ static parent: Worker;
997
+
998
+ /**
999
+ * Send a message to the corresponding worker. msg is cloned in the
1000
+ * destination worker using an algorithm similar to the HTML structured
1001
+ * clone algorithm. SharedArrayBuffer are shared between workers.
1002
+ *
1003
+ * Current limitations: Map and Set are not supported yet.
1004
+ */
1005
+ postMessage(msg: StructuredClonable): void;
1006
+
1007
+ /**
1008
+ * Set a function which is called each time a message is received. The
1009
+ * function is called with a single argument. It is an object with a data
1010
+ * property containing the received message. The thread is not terminated
1011
+ * if there is at least one non null onmessage handler.
1012
+ */
1013
+ onmessage: null | ((event: { data: StructuredClonable }) => void);
1014
+ }
1015
+
1016
+ /** constant for {@link access}(); test for read permission. */
1017
+ export var R_OK: number;
1018
+
1019
+ /** constant for {@link access}(); test for write permission. */
1020
+ export var W_OK: number;
1021
+
1022
+ /** constant for {@link access}(); test for execute (search) permission. */
1023
+ export var X_OK: number;
1024
+
1025
+ /** constant for {@link access}(); test for existence of file. */
1026
+ export var F_OK: number;
1027
+
1028
+ /** `access` Unix system call; checks if a file is readable, writable, executable, and/or exists (use {@link R_OK}, {@link W_OK}, {@link X_OK}, and/or {@link F_OK} for `accessMode`). Throws a descriptive error (with errno property) if the requested access is not available; otherwise, returns undefined. */
1029
+ export function access(path: string, accessMode: number): void;
1030
+ }
1031
+
package/index.js DELETED
File without changes