functionalscript 0.11.2 → 0.11.4
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/io/module.f.d.ts +15 -4
- package/io/module.f.js +22 -4
- package/io/module.js +3 -3
- package/io/virtual/module.f.js +1 -1
- package/package.json +1 -1
- package/text/sgr/module.f.d.ts +23 -0
- package/text/sgr/module.f.js +21 -0
- package/text/utf8/module.f.d.ts +5 -0
- package/text/utf8/module.f.js +5 -0
- package/types/effects/module.f.d.ts +6 -2
- package/types/effects/module.f.js +1 -0
- package/types/effects/node/module.f.d.ts +19 -6
- package/types/effects/node/module.f.js +5 -4
- package/types/effects/node/test.f.js +0 -1
- package/types/uint8array/module.f.d.ts +2 -0
- package/types/uint8array/module.f.js +3 -1
package/io/module.f.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Effect } from '../types/effects/module.f.ts';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Headers, NodeOp } from '../types/effects/node/module.f.ts';
|
|
3
3
|
import { type Result } from '../types/result/module.f.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Represents a directory entry (file or directory) in the filesystem
|
|
@@ -82,7 +82,18 @@ export type TryCatch = <T>(f: () => T) => Result<T, unknown>;
|
|
|
82
82
|
export type Server = {
|
|
83
83
|
readonly listen: (port: number) => void;
|
|
84
84
|
};
|
|
85
|
-
export type
|
|
85
|
+
export type Readable = AsyncIterable<Uint8Array>;
|
|
86
|
+
export type IncomingMessage = Readable & {
|
|
87
|
+
readonly method: string;
|
|
88
|
+
readonly url: string;
|
|
89
|
+
readonly headers: Headers;
|
|
90
|
+
};
|
|
91
|
+
export type ServerResponse = {
|
|
92
|
+
readonly writeHead: (status: number, headers: Record<string, string>) => ServerResponse;
|
|
93
|
+
readonly end: (body: Uint8Array) => void;
|
|
94
|
+
};
|
|
95
|
+
export type RequestListener = (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
96
|
+
export type Http = {
|
|
86
97
|
readonly createServer: (_: RequestListener) => Server;
|
|
87
98
|
};
|
|
88
99
|
/**
|
|
@@ -97,7 +108,7 @@ export type Io = {
|
|
|
97
108
|
readonly fetch: (url: string) => Promise<Response>;
|
|
98
109
|
readonly tryCatch: TryCatch;
|
|
99
110
|
readonly asyncTryCatch: <T>(f: () => Promise<T>) => Promise<Result<T, unknown>>;
|
|
100
|
-
readonly
|
|
111
|
+
readonly http: Http;
|
|
101
112
|
};
|
|
102
113
|
/**
|
|
103
114
|
* The environment variables.
|
|
@@ -113,4 +124,4 @@ export type Run = (f: App) => Promise<never>;
|
|
|
113
124
|
*/
|
|
114
125
|
export declare const run: (io: Io) => Run;
|
|
115
126
|
export type EffectToPromise = <T>(effect: Effect<NodeOp, T>) => Promise<T>;
|
|
116
|
-
export declare const fromIo: ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch,
|
|
127
|
+
export declare const fromIo: ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, http: { createServer }, }: Io) => EffectToPromise;
|
package/io/module.f.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { todo } from "../dev/module.f.js";
|
|
2
1
|
import { normalize } from "../path/module.f.js";
|
|
3
2
|
import {} from "../types/effects/module.f.js";
|
|
4
3
|
import { asyncRun } from "../types/effects/module.js";
|
|
5
4
|
import { asBase, asNominal } from "../types/nominal/module.f.js";
|
|
6
5
|
import { error, ok } from "../types/result/module.f.js";
|
|
7
|
-
import { fromVec, toVec } from "../types/uint8array/module.f.js";
|
|
6
|
+
import { fromVec, listToVec, toVec } from "../types/uint8array/module.f.js";
|
|
8
7
|
/**
|
|
9
8
|
* Runs a function and exits the process with the returned code
|
|
10
9
|
* Handles errors by exiting with code 1
|
|
@@ -29,7 +28,14 @@ const tc = async (f) => {
|
|
|
29
28
|
return error(e);
|
|
30
29
|
}
|
|
31
30
|
};
|
|
32
|
-
|
|
31
|
+
const collect = async (v) => {
|
|
32
|
+
let result = [];
|
|
33
|
+
for await (const a of v) {
|
|
34
|
+
result = [...result, a];
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, http: { createServer }, }) => {
|
|
33
39
|
const result = asyncRun({
|
|
34
40
|
all: async (effects) => await Promise.all(effects.map(result)),
|
|
35
41
|
error: async (message) => error(message),
|
|
@@ -47,7 +53,19 @@ export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readF
|
|
|
47
53
|
.map(v => ({ name: v.name, parentPath: normalize(v.parentPath), isFile: v.isFile() }))),
|
|
48
54
|
writeFile: ([path, data]) => tc(() => writeFile(path, fromVec(data))),
|
|
49
55
|
createServer: async (requestListener) => {
|
|
50
|
-
const
|
|
56
|
+
const nodeRl = async (req, res) => {
|
|
57
|
+
const { method, url, headers } = req;
|
|
58
|
+
const { status, headers: outHeaders, body: outBody } = requestListener({
|
|
59
|
+
method,
|
|
60
|
+
url,
|
|
61
|
+
headers,
|
|
62
|
+
body: listToVec(await collect(req))
|
|
63
|
+
});
|
|
64
|
+
res
|
|
65
|
+
.writeHead(status, outHeaders)
|
|
66
|
+
.end(fromVec(outBody));
|
|
67
|
+
};
|
|
68
|
+
const server = asNominal(createServer(nodeRl));
|
|
51
69
|
return server;
|
|
52
70
|
},
|
|
53
71
|
listen: async ([server, port]) => {
|
package/io/module.js
CHANGED
|
@@ -6,10 +6,10 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
6
6
|
}
|
|
7
7
|
return path;
|
|
8
8
|
};
|
|
9
|
-
import
|
|
9
|
+
import http from 'node:http';
|
|
10
10
|
import { fromIo, run } from "./module.f.js";
|
|
11
11
|
import fs from 'node:fs';
|
|
12
|
-
import process from
|
|
12
|
+
import process from 'node:process';
|
|
13
13
|
import { concat } from "../path/module.f.js";
|
|
14
14
|
import { error, ok } from "../types/result/module.f.js";
|
|
15
15
|
const prefix = 'file:///';
|
|
@@ -40,7 +40,7 @@ export const io = {
|
|
|
40
40
|
return error(e);
|
|
41
41
|
}
|
|
42
42
|
},
|
|
43
|
-
|
|
43
|
+
http,
|
|
44
44
|
};
|
|
45
45
|
export const legacyRun = run(io);
|
|
46
46
|
export const ioRun = (io) => {
|
package/io/virtual/module.f.js
CHANGED
package/package.json
CHANGED
package/text/sgr/module.f.d.ts
CHANGED
|
@@ -21,17 +21,40 @@ export declare const csi: (end: End) => Csi;
|
|
|
21
21
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
|
|
22
22
|
*/
|
|
23
23
|
export declare const sgr: Csi;
|
|
24
|
+
/** Resets all SGR styles to terminal defaults. */
|
|
24
25
|
export declare const reset: string;
|
|
26
|
+
/** Enables bold/intense text rendering when supported by the terminal. */
|
|
25
27
|
export declare const bold: string;
|
|
28
|
+
/** Applies red foreground color to subsequent text. */
|
|
26
29
|
export declare const fgRed: string;
|
|
30
|
+
/** Applies green foreground color to subsequent text. */
|
|
27
31
|
export declare const fgGreen: string;
|
|
28
32
|
export type Stdout = {
|
|
33
|
+
/** Writes a string to the output stream. */
|
|
29
34
|
readonly write: (s: string) => void;
|
|
30
35
|
};
|
|
36
|
+
/** Stateful writer that updates previously printed text in-place. */
|
|
31
37
|
export type WriteText = (text: string) => WriteText;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a stateful text writer that rewrites the previous value using backspaces.
|
|
40
|
+
*
|
|
41
|
+
* @param stdout - Destination output stream.
|
|
42
|
+
* @returns A recursive writer that replaces prior text on each call.
|
|
43
|
+
*/
|
|
32
44
|
export declare const createConsoleText: (stdout: Stdout) => WriteText;
|
|
33
45
|
export type CsiConsole = (s: string) => void;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a TTY-aware console function.
|
|
48
|
+
*
|
|
49
|
+
* For TTY destinations, ANSI SGR sequences are preserved.
|
|
50
|
+
* For non-TTY destinations, ANSI SGR sequences are stripped.
|
|
51
|
+
*
|
|
52
|
+
* @param io - Runtime IO bindings.
|
|
53
|
+
* @returns A function that targets a writable stream.
|
|
54
|
+
*/
|
|
34
55
|
export declare const console: ({ fs: { writeSync } }: Io) => (w: Writable) => CsiConsole;
|
|
56
|
+
/** Writes to process stdout using a TTY-aware CSI console. */
|
|
35
57
|
export declare const stdio: (io: Io) => CsiConsole;
|
|
58
|
+
/** Writes to process stderr using a TTY-aware CSI console. */
|
|
36
59
|
export declare const stderr: (io: Io) => CsiConsole;
|
|
37
60
|
export {};
|
package/text/sgr/module.f.js
CHANGED
|
@@ -19,9 +19,13 @@ export const csi = (end) => code => `${begin}${code.toString()}${end}`;
|
|
|
19
19
|
* https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
|
|
20
20
|
*/
|
|
21
21
|
export const sgr = csi('m');
|
|
22
|
+
/** Resets all SGR styles to terminal defaults. */
|
|
22
23
|
export const reset = sgr(0);
|
|
24
|
+
/** Enables bold/intense text rendering when supported by the terminal. */
|
|
23
25
|
export const bold = sgr(1);
|
|
26
|
+
/** Applies red foreground color to subsequent text. */
|
|
24
27
|
export const fgRed = sgr(31);
|
|
28
|
+
/** Applies green foreground color to subsequent text. */
|
|
25
29
|
export const fgGreen = sgr(32);
|
|
26
30
|
const { max } = Math;
|
|
27
31
|
const replace = (old) => (text) => {
|
|
@@ -29,6 +33,12 @@ const replace = (old) => (text) => {
|
|
|
29
33
|
const suffixLength = max(0, len - text.length);
|
|
30
34
|
return backspace.repeat(len) + text + " ".repeat(suffixLength) + backspace.repeat(suffixLength);
|
|
31
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Creates a stateful text writer that rewrites the previous value using backspaces.
|
|
38
|
+
*
|
|
39
|
+
* @param stdout - Destination output stream.
|
|
40
|
+
* @returns A recursive writer that replaces prior text on each call.
|
|
41
|
+
*/
|
|
32
42
|
export const createConsoleText = (stdout) => {
|
|
33
43
|
const f = (old) => (text) => {
|
|
34
44
|
stdout.write(replace(old)(text));
|
|
@@ -36,11 +46,22 @@ export const createConsoleText = (stdout) => {
|
|
|
36
46
|
};
|
|
37
47
|
return f('');
|
|
38
48
|
};
|
|
49
|
+
/**
|
|
50
|
+
* Creates a TTY-aware console function.
|
|
51
|
+
*
|
|
52
|
+
* For TTY destinations, ANSI SGR sequences are preserved.
|
|
53
|
+
* For non-TTY destinations, ANSI SGR sequences are stripped.
|
|
54
|
+
*
|
|
55
|
+
* @param io - Runtime IO bindings.
|
|
56
|
+
* @returns A function that targets a writable stream.
|
|
57
|
+
*/
|
|
39
58
|
export const console = ({ fs: { writeSync } }) => (w) => {
|
|
40
59
|
const { isTTY } = w;
|
|
41
60
|
return isTTY
|
|
42
61
|
? (s) => writeSync(w.fd, s + '\n')
|
|
43
62
|
: (s) => writeSync(w.fd, s.replace(/\x1b\[[0-9;]*m/g, '') + '\n');
|
|
44
63
|
};
|
|
64
|
+
/** Writes to process stdout using a TTY-aware CSI console. */
|
|
45
65
|
export const stdio = (io) => console(io)(io.process.stdout);
|
|
66
|
+
/** Writes to process stderr using a TTY-aware CSI console. */
|
|
46
67
|
export const stderr = (io) => console(io)(io.process.stderr);
|
package/text/utf8/module.f.d.ts
CHANGED
package/text/utf8/module.f.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UTF-8 byte-level encoding and decoding utilities for FunctionalScript streams.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import { flat, flatMap, stateScan } from "../../types/list/module.f.js";
|
|
2
7
|
/**
|
|
3
8
|
* Error mask constant used to represent invalid code points or encoding errors in UTF-8.
|
|
@@ -16,10 +16,14 @@ export type DoK<O extends Operation, T, K extends O[0]> = DoKPR<O, T, K, Pr<O, K
|
|
|
16
16
|
export type Do<O extends Operation, T> = DoK<O, T, O[0]>;
|
|
17
17
|
export declare const pure: <T>(v: T) => Effect<never, T>;
|
|
18
18
|
export declare const doFull: <O extends Operation, T, K extends O[0]>(cmd: K, param: Pr<O, K>[0], cont: (input: Pr<O, K>[1]) => Effect<O, T>) => Effect<O, T>;
|
|
19
|
-
export
|
|
19
|
+
export type Param<O extends Operation> = F<O>[0];
|
|
20
|
+
export type Return<O extends Operation> = F<O>[1];
|
|
21
|
+
export declare const do_: <O extends Operation>(cmd: O[0]) => (param: Param<O>) => Effect<O, Return<O>>;
|
|
22
|
+
export declare const doRest: <O extends Operation>(cmd: O[0]) => (...param: Param<O>) => Effect<O, Return<O>>;
|
|
20
23
|
export declare const begin: Effect<never, void>;
|
|
21
24
|
export type ToAsyncOperationMap<O extends Operation> = {
|
|
22
25
|
readonly [K in O[0]]: (payload: Pr<O, K>[0]) => Promise<Pr<O, K>[1]>;
|
|
23
26
|
};
|
|
24
27
|
export type F<O extends Operation> = Pr<O, O[0]>;
|
|
25
|
-
export type Func<O extends Operation> = (_:
|
|
28
|
+
export type Func<O extends Operation> = (_: Param<O>) => Effect<O, Return<O>>;
|
|
29
|
+
export type RestFunc<O extends Operation> = (..._: Param<O>) => Effect<O, Return<O>>;
|
|
@@ -12,4 +12,5 @@ export const doFull = (cmd, param, cont) => ({
|
|
|
12
12
|
step: (f) => doFull(cmd, param, x => cont(x).step(f)),
|
|
13
13
|
});
|
|
14
14
|
export const do_ = (cmd) => (param) => doFull(cmd, param, pure);
|
|
15
|
+
export const doRest = (cmd) => (...param) => do_(cmd)(param);
|
|
15
16
|
export const begin = pure(undefined);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Vec } from '../../bit_vec/module.f.ts';
|
|
2
2
|
import type { Nominal } from '../../nominal/module.f.ts';
|
|
3
3
|
import type { Result } from '../../result/module.f.ts';
|
|
4
|
-
import { type Effect, type Func, type Operation, type ToAsyncOperationMap } from '../module.f.ts';
|
|
4
|
+
import { type Effect, type Func, type Operation, type RestFunc, type ToAsyncOperationMap } from '../module.f.ts';
|
|
5
5
|
export type IoResult<T> = Result<T, unknown>;
|
|
6
6
|
export type All = ['all', <T>(_: readonly Effect<never, T>[]) => readonly T[]];
|
|
7
7
|
/**
|
|
@@ -20,7 +20,7 @@ export type MakeDirectoryOptions = {
|
|
|
20
20
|
};
|
|
21
21
|
export type MkdirParam = readonly [string, MakeDirectoryOptions?];
|
|
22
22
|
export type Mkdir = readonly ['mkdir', (_: MkdirParam) => IoResult<void>];
|
|
23
|
-
export declare const mkdir:
|
|
23
|
+
export declare const mkdir: RestFunc<Mkdir>;
|
|
24
24
|
export type ReadFile = readonly ['readFile', (_: string) => IoResult<Vec>];
|
|
25
25
|
export declare const readFile: Func<ReadFile>;
|
|
26
26
|
/**
|
|
@@ -37,10 +37,10 @@ export type ReaddirOptions = {
|
|
|
37
37
|
};
|
|
38
38
|
export type ReaddirParam = readonly [string, ReaddirOptions];
|
|
39
39
|
export type Readdir = readonly ['readdir', (_: ReaddirParam) => IoResult<readonly Dirent[]>];
|
|
40
|
-
export declare const readdir:
|
|
40
|
+
export declare const readdir: RestFunc<Readdir>;
|
|
41
41
|
export type WriteFileParam = readonly [string, Vec];
|
|
42
42
|
export type WriteFile = readonly ['writeFile', (_: WriteFileParam) => IoResult<void>];
|
|
43
|
-
export declare const writeFile:
|
|
43
|
+
export declare const writeFile: RestFunc<WriteFile>;
|
|
44
44
|
export type Fs = Mkdir | ReadFile | Readdir | WriteFile;
|
|
45
45
|
export type Error = ['error', (_: string) => void];
|
|
46
46
|
export declare const error: Func<Error>;
|
|
@@ -48,12 +48,25 @@ export type Log = ['log', (_: string) => void];
|
|
|
48
48
|
export declare const log: Func<Log>;
|
|
49
49
|
export type Console = Log | Error;
|
|
50
50
|
export type Server = Nominal<'server', `160855c4f69310fece3273c1853ac32de43dee1eb41bf59d821917f8eebe9272`, unknown>;
|
|
51
|
-
export type
|
|
52
|
-
|
|
51
|
+
export type Headers = {
|
|
52
|
+
readonly [k in string]: string;
|
|
53
|
+
};
|
|
54
|
+
export type IncomingMessage = {
|
|
55
|
+
readonly method: string;
|
|
56
|
+
readonly url: string;
|
|
57
|
+
readonly headers: Headers;
|
|
58
|
+
readonly body: Vec;
|
|
59
|
+
};
|
|
60
|
+
export type ServerResponse = {
|
|
61
|
+
readonly status: number;
|
|
62
|
+
readonly headers: Headers;
|
|
63
|
+
readonly body: Vec;
|
|
64
|
+
};
|
|
53
65
|
export type RequestListener = (_: IncomingMessage) => ServerResponse;
|
|
54
66
|
export type CreateServer = ['createServer', (_: RequestListener) => Server];
|
|
55
67
|
export declare const createServer: Func<CreateServer>;
|
|
56
68
|
export type Listen = ['listen', (_: readonly [Server, number]) => void];
|
|
69
|
+
export declare const listen: RestFunc<Listen>;
|
|
57
70
|
export type Http = CreateServer | Listen;
|
|
58
71
|
export type NodeOp = All | Fetch | Console | Fs | Http;
|
|
59
72
|
export type NodeEffect<T> = Effect<NodeOp, T>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { do_ } from "../module.f.js";
|
|
1
|
+
import { doRest, do_ } from "../module.f.js";
|
|
2
2
|
/**
|
|
3
3
|
* To run the operation `O` should be known by the runner/engine.
|
|
4
4
|
* This is the reason why we merge `O` with `All` in the resulted `Effect`.
|
|
@@ -9,10 +9,11 @@ import { do_ } from "../module.f.js";
|
|
|
9
9
|
export const all = (...a) => do_('all')(a);
|
|
10
10
|
export const both = (a) => (b) => all(a, b);
|
|
11
11
|
export const fetch = do_('fetch');
|
|
12
|
-
export const mkdir = (
|
|
12
|
+
export const mkdir = doRest('mkdir');
|
|
13
13
|
export const readFile = do_('readFile');
|
|
14
|
-
export const readdir = (
|
|
15
|
-
export const writeFile = (
|
|
14
|
+
export const readdir = doRest('readdir');
|
|
15
|
+
export const writeFile = doRest('writeFile');
|
|
16
16
|
export const error = do_('error');
|
|
17
17
|
export const log = do_('log');
|
|
18
18
|
export const createServer = do_('createServer');
|
|
19
|
+
export const listen = doRest('listen');
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { empty, isVec, uint, vec8 } from "../../bit_vec/module.f.js";
|
|
2
|
-
import { run } from "../mock/module.f.js";
|
|
3
2
|
import { pure } from "../module.f.js";
|
|
4
3
|
import { fetch, mkdir, readdir, readFile, writeFile } from "./module.f.js";
|
|
5
4
|
import { emptyState, virtual } from "./virtual/module.f.js";
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { type Vec } from "../bit_vec/module.f.ts";
|
|
2
|
+
import { type List } from "../list/module.f.ts";
|
|
2
3
|
/**
|
|
3
4
|
* Converts a Uint8Array into an MSB-first bit vector.
|
|
4
5
|
*/
|
|
5
6
|
export declare const toVec: (input: Uint8Array) => Vec;
|
|
7
|
+
export declare const listToVec: (input: List<Uint8Array>) => Vec;
|
|
6
8
|
/**
|
|
7
9
|
* Converts an MSB-first bit vector into a Uint8Array.
|
|
8
10
|
*/
|
|
@@ -6,13 +6,15 @@
|
|
|
6
6
|
import { utf8, utf8ToString } from "../../text/module.f.js";
|
|
7
7
|
import { msb, u8List, u8ListToVec } from "../bit_vec/module.f.js";
|
|
8
8
|
import { compose } from "../function/module.f.js";
|
|
9
|
-
import { fromArrayLike, iterable } from "../list/module.f.js";
|
|
9
|
+
import { flat, fromArrayLike, iterable, map } from "../list/module.f.js";
|
|
10
10
|
const u8ListToVecMsb = u8ListToVec(msb);
|
|
11
11
|
const u8ListMsb = u8List(msb);
|
|
12
12
|
/**
|
|
13
13
|
* Converts a Uint8Array into an MSB-first bit vector.
|
|
14
14
|
*/
|
|
15
15
|
export const toVec = (input) => u8ListToVecMsb(fromArrayLike(input));
|
|
16
|
+
const m = map(fromArrayLike);
|
|
17
|
+
export const listToVec = (input) => u8ListToVecMsb(flat(m(input)));
|
|
16
18
|
/**
|
|
17
19
|
* Converts an MSB-first bit vector into a Uint8Array.
|
|
18
20
|
*/
|