functionalscript 0.11.0 → 0.11.1

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/cas/module.f.d.ts CHANGED
@@ -8,16 +8,37 @@ import type { Vec } from "../types/bit_vec/module.f.ts";
8
8
  import { type Effect, type Operation } from "../types/effects/module.f.ts";
9
9
  import { type Fs, type NodeOp } from "../types/effects/node/module.f.ts";
10
10
  export type KvStore<O extends Operation> = {
11
+ /** Reads a value by key; returns `undefined` when the key does not exist. */
11
12
  readonly read: (key: Vec) => Effect<O, Vec | undefined>;
13
+ /** Writes a key/value pair to the underlying storage. */
12
14
  readonly write: (key: Vec, value: Vec) => Effect<O, void>;
15
+ /** Lists all keys available in the store. */
13
16
  readonly list: () => Effect<O, readonly Vec[]>;
14
17
  };
18
+ /** A key/value tuple where index `0` is the key and index `1` is the value. */
15
19
  export type Kv = readonly [Vec, Vec];
20
+ /**
21
+ * Creates a filesystem-backed key/value store under the provided root path.
22
+ */
16
23
  export declare const fileKvStore: (path: string) => KvStore<Fs>;
17
24
  export type Cas<O extends Operation> = {
25
+ /** Reads content by hash; returns `undefined` when not found. */
18
26
  readonly read: (key: Vec) => Effect<O, Vec | undefined>;
27
+ /** Stores content and returns its computed hash. */
19
28
  readonly write: (value: Vec) => Effect<O, Vec>;
29
+ /** Lists all stored content hashes. */
20
30
  readonly list: () => Effect<O, readonly Vec[]>;
21
31
  };
32
+ /**
33
+ * Builds a content-addressable storage facade from a SHA-2 implementation.
34
+ */
22
35
  export declare const cas: (sha2: Sha2) => <O extends Operation>(_: KvStore<O>) => Cas<O>;
36
+ /**
37
+ * Runs the CAS CLI.
38
+ *
39
+ * Supported subcommands:
40
+ * - `add <path>`: stores file content and prints the hash.
41
+ * - `get <hash> <path>`: restores content by hash into a file.
42
+ * - `list`: prints all known hashes.
43
+ */
23
44
  export declare const main: (args: readonly string[]) => Effect<NodeOp, number>;
package/cas/module.f.js CHANGED
@@ -13,12 +13,16 @@ import { unwrap } from "../types/result/module.f.js";
13
13
  const o = { withFileTypes: true };
14
14
  const split = (s) => [s.substring(0, 2), s.substring(2)];
15
15
  const prefix = '.cas';
16
+ /** Converts a content key to its sharded relative CAS file path. */
16
17
  const toPath = (key) => {
17
18
  const s = vecToCBase32(key);
18
19
  const [a, bc] = split(s);
19
20
  const [b, c] = split(bc);
20
21
  return `${prefix}/${a}/${b}/${c}`;
21
22
  };
23
+ /**
24
+ * Creates a filesystem-backed key/value store under the provided root path.
25
+ */
22
26
  export const fileKvStore = (path) => ({
23
27
  read: (key) => begin
24
28
  .step(() => readFile(toPath(key)))
@@ -41,6 +45,9 @@ export const fileKvStore = (path) => ({
41
45
  ? cBase32ToVec(parentPath.substring(prefix.length).replaceAll('/', '') + name)
42
46
  : null)))),
43
47
  });
48
+ /**
49
+ * Builds a content-addressable storage facade from a SHA-2 implementation.
50
+ */
44
51
  export const cas = (sha2) => {
45
52
  const compute = computeSync(sha2);
46
53
  return ({ read, write, list }) => ({
@@ -54,9 +61,18 @@ export const cas = (sha2) => {
54
61
  list,
55
62
  });
56
63
  };
64
+ /** Prints an error message and returns exit code `1`. */
57
65
  const e = (s) => begin
58
66
  .step(() => error(s))
59
67
  .step(() => pure(1));
68
+ /**
69
+ * Runs the CAS CLI.
70
+ *
71
+ * Supported subcommands:
72
+ * - `add <path>`: stores file content and prints the hash.
73
+ * - `get <hash> <path>`: restores content by hash into a file.
74
+ * - `list`: prints all known hashes.
75
+ */
60
76
  export const main = (args) => {
61
77
  const c = cas(sha256)(fileKvStore('.'));
62
78
  const [cmd, ...options] = args;
@@ -7,7 +7,9 @@ import type { Array16, Array8 } from '../../types/array/module.f.ts';
7
7
  import { type Vec } from '../../types/bit_vec/module.f.ts';
8
8
  import type { Fold } from '../../types/function/operator/module.f.ts';
9
9
  import { type List } from '../../types/list/module.f.ts';
10
+ /** 8-word SHA-2 state vector. */
10
11
  export type V8 = Array8<bigint>;
12
+ /** 16-word SHA-2 message schedule chunk. */
11
13
  export type V16 = Array16<bigint>;
12
14
  /**
13
15
  * Type definition for the state of the SHA-2 algorithm.
@@ -75,8 +77,16 @@ export type Sha2 = {
75
77
  */
76
78
  readonly end: (state: State) => Vec;
77
79
  };
80
+ /**
81
+ * Computes a SHA-2 hash from a list of message chunks.
82
+ *
83
+ * @param sha2 A SHA-2 algorithm configuration.
84
+ * @returns A function that hashes the full list of chunks.
85
+ */
78
86
  export declare const computeSync: ({ append, init, end }: Sha2) => (list: List<Vec>) => Vec;
87
+ /** 32-bit SHA-2 base configuration shared by SHA-224 and SHA-256. */
79
88
  export declare const base32: Base;
89
+ /** 64-bit SHA-2 base configuration shared by SHA-384, SHA-512, SHA-512/224 and SHA-512/256. */
80
90
  export declare const base64: Base;
81
91
  /** SHA-256 */
82
92
  export declare const sha256: Sha2;
@@ -167,10 +167,17 @@ const sha2 = ({ append, end, chunkLength }, hash, hashLength) => ({
167
167
  append,
168
168
  end: end(hashLength),
169
169
  });
170
+ /**
171
+ * Computes a SHA-2 hash from a list of message chunks.
172
+ *
173
+ * @param sha2 A SHA-2 algorithm configuration.
174
+ * @returns A function that hashes the full list of chunks.
175
+ */
170
176
  export const computeSync = ({ append, init, end }) => {
171
177
  const f = fold(append)(init);
172
178
  return (list) => end(f(list));
173
179
  };
180
+ /** 32-bit SHA-2 base configuration shared by SHA-224 and SHA-256. */
174
181
  export const base32 = base({
175
182
  logBitLen: 5n,
176
183
  k: [
@@ -196,6 +203,7 @@ export const base32 = base({
196
203
  ss0: [7n, 18n, 3n],
197
204
  ss1: [17n, 19n, 10n],
198
205
  });
206
+ /** 64-bit SHA-2 base configuration shared by SHA-384, SHA-512, SHA-512/224 and SHA-512/256. */
199
207
  export const base64 = base({
200
208
  logBitLen: 6n,
201
209
  k: [
@@ -14,10 +14,25 @@ export type All = {
14
14
  readonly int2octets: (x: bigint) => Vec;
15
15
  readonly bits2octets: (b: Vec) => Vec;
16
16
  };
17
+ /**
18
+ * Builds RFC6979 helper conversions for a subgroup order.
19
+ *
20
+ * @param q - Subgroup order.
21
+ * @returns Conversion helpers used by deterministic nonce generation.
22
+ */
17
23
  export declare const all: (q: bigint) => All;
24
+ /**
25
+ * Builds RFC6979 helper conversions from curve parameters.
26
+ */
18
27
  export declare const fromCurve: (c: Curve) => All;
19
28
  export declare const concat: (...x: readonly Vec[]) => Vec;
29
+ /**
30
+ * Computes deterministic ECDSA nonce `k` as described by RFC6979.
31
+ */
20
32
  export declare const computeK: (_: All) => (_: Sha2) => (x: bigint) => (m: Vec) => bigint;
21
33
  type Signature = Array2<bigint>;
34
+ /**
35
+ * Signs a message bit vector and returns an ECDSA `(r, s)` signature pair.
36
+ */
22
37
  export declare const sign: (c: Curve) => (hf: Sha2) => (x: bigint) => (m: Vec) => Signature;
23
38
  export {};
@@ -5,6 +5,12 @@ import { computeSync } from "../sha2/module.f.js";
5
5
  // qlen to rlen
6
6
  const roundUp8 = roundUp(8n);
7
7
  const divUp8 = divUp(8n);
8
+ /**
9
+ * Builds RFC6979 helper conversions for a subgroup order.
10
+ *
11
+ * @param q - Subgroup order.
12
+ * @returns Conversion helpers used by deterministic nonce generation.
13
+ */
8
14
  export const all = (q) => {
9
15
  const qlen = bitLength(q);
10
16
  const bits2int = (b) => {
@@ -22,11 +28,17 @@ export const all = (q) => {
22
28
  bits2octets: b => int2octets(bits2int(b) % q),
23
29
  };
24
30
  };
31
+ /**
32
+ * Builds RFC6979 helper conversions from curve parameters.
33
+ */
25
34
  export const fromCurve = (c) => all(c.nf.p);
26
35
  const x01 = vec8(0x01n);
27
36
  const x00 = vec8(0x00n);
28
37
  const ltov = listToVec(msb);
29
38
  export const concat = (...x) => ltov(x);
39
+ /**
40
+ * Computes deterministic ECDSA nonce `k` as described by RFC6979.
41
+ */
30
42
  export const computeK = ({ q, bits2int, qlen, int2octets, bits2octets }) => hf => {
31
43
  // TODO: Look at https://www.rfc-editor.org/rfc/rfc6979#section-3.3 to reformulate
32
44
  // it using `HMAC_DRBG`.
@@ -96,6 +108,9 @@ export const computeK = ({ q, bits2int, qlen, int2octets, bits2octets }) => hf =
96
108
  }
97
109
  };
98
110
  };
111
+ /**
112
+ * Signs a message bit vector and returns an ECDSA `(r, s)` signature pair.
113
+ */
99
114
  export const sign = (c) => (hf) => (x) => (m) => {
100
115
  // 2.4 Signature Generation
101
116
  const { nf: { p: q, div }, g } = c;
@@ -0,0 +1,2 @@
1
+ import { index4 } from '../module.f.ts';
2
+ export default index4;
@@ -0,0 +1,2 @@
1
+ import { index4 } from "../module.f.js";
2
+ export default index4;
package/dev/module.f.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * @module
5
5
  */
6
6
  import { type Io } from '../io/module.f.ts';
7
- import { type All, type Readdir } from '../types/effects/node/module.f.ts';
7
+ import { type All, type NodeProgram, type Readdir } from '../types/effects/node/module.f.ts';
8
8
  import { type Effect } from '../types/effects/module.f.ts';
9
9
  export declare const todo: () => never;
10
10
  export type Module = {
@@ -15,6 +15,5 @@ export type ModuleMap = {
15
15
  };
16
16
  export declare const env: (io: Io) => (v: string) => string | undefined;
17
17
  export declare const allFiles2: (s: string) => Effect<Readdir | All, readonly string[]>;
18
- export declare const allFiles: (io: Io) => (s: string) => Promise<readonly string[]>;
19
18
  export declare const loadModuleMap: (io: Io) => Promise<ModuleMap>;
20
- export declare const index: (io: Io) => Promise<number>;
19
+ export declare const index4: NodeProgram;
package/dev/module.f.js CHANGED
@@ -5,8 +5,7 @@
5
5
  */
6
6
  import { fromIo } from "../io/module.f.js";
7
7
  import { updateVersion } from "./version/module.f.js";
8
- import { encodeUtf8 } from "../types/uint8array/module.f.js";
9
- import { all, readdir, readFile, writeFile } from "../types/effects/node/module.f.js";
8
+ import { all, both, readdir, readFile, writeFile } from "../types/effects/node/module.f.js";
10
9
  import { utf8, utf8ToString } from "../text/module.f.js";
11
10
  import { unwrap } from "../types/result/module.f.js";
12
11
  import { begin, pure } from "../types/effects/module.f.js";
@@ -45,31 +44,7 @@ export const allFiles2 = (s) => {
45
44
  .step(v => pure(v.flat()));
46
45
  return load(s);
47
46
  };
48
- export const allFiles = (io) => (s) => {
49
- const { fs: { promises: { readdir } } } = io;
50
- const load = async (p) => {
51
- let result = [];
52
- for (const i of await readdir(p, { withFileTypes: true })) {
53
- const { name } = i;
54
- if (name.startsWith('.')) {
55
- continue;
56
- }
57
- const file = `${p}/${name}`;
58
- if (i.isDirectory()) {
59
- if (name === 'node_modules') {
60
- continue;
61
- }
62
- result = [...result, ...await load(file)];
63
- continue;
64
- }
65
- if (name.endsWith('.js') || name.endsWith('.ts')) {
66
- result = [...result, file];
67
- }
68
- }
69
- return result;
70
- };
71
- return load(s);
72
- };
47
+ const allFiles = (io) => (s) => fromIo(io)(allFiles2(s));
73
48
  export const loadModuleMap = async (io) => {
74
49
  const { fs: { existsSync }, asyncImport } = io;
75
50
  let map = [];
@@ -89,19 +64,17 @@ const index2 = begin
89
64
  .step(() => updateVersion)
90
65
  .step(() => readFile(denoJson))
91
66
  .step(v => pure(JSON.parse(utf8ToString(unwrap(v)))));
92
- const allFiles2a = (jsr_json) => begin
67
+ const allFiles2aa = begin
93
68
  .step(() => allFiles2('.'))
94
69
  .step(files => {
95
- // console.log(files)
96
70
  const list = files.filter(v => v.endsWith('/module.f.ts') || v.endsWith('/module.ts'));
97
71
  const exportsA = list.map(v => [v, `./${v.substring(2)}`]);
98
- const exports = Object.fromEntries(exportsA);
72
+ return pure(Object.fromEntries(exportsA));
73
+ });
74
+ const index3 = both(index2)(allFiles2aa)
75
+ .step(([jsr_json, exports]) => {
99
76
  const json = JSON.stringify({ ...jsr_json, exports }, null, 2);
100
77
  return writeFile(denoJson, utf8(json));
101
78
  })
102
79
  .step(() => pure(0));
103
- export const index = async (io) => {
104
- const runner = fromIo(io);
105
- const jsr_json = await runner(index2);
106
- return await runner(allFiles2a(jsr_json));
107
- };
80
+ export const index4 = () => index3;
@@ -2,12 +2,30 @@ import { type List } from '../types/list/module.f.ts';
2
2
  type Tag = string;
3
3
  type Element1 = readonly [Tag, ...Node[]];
4
4
  type Element2 = readonly [Tag, Attributes, ...Node[]];
5
+ /**
6
+ * A FunctionalScript representation of an HTML element.
7
+ *
8
+ * - `[tag, ...children]` for elements without attributes.
9
+ * - `[tag, attributes, ...children]` for elements with attributes.
10
+ */
5
11
  export type Element = Element1 | Element2;
6
12
  type Attributes = {
7
13
  readonly [k in string]: string;
8
14
  };
9
15
  export type Node = Element | string;
16
+ /**
17
+ * Converts a FunctionalScript element into a list of HTML string chunks.
18
+ *
19
+ * Chunks are returned instead of a single string to support composition with
20
+ * other list/string helpers in this codebase.
21
+ */
10
22
  export declare const element: (e: Element) => List<string>;
23
+ /**
24
+ * Builds a complete HTML document by prepending `<!DOCTYPE html>`.
25
+ */
11
26
  export declare const html: (_: Element) => List<string>;
27
+ /**
28
+ * Renders an HTML element tree to a final string.
29
+ */
12
30
  export declare const htmlToString: (_: Element) => string;
13
31
  export {};
package/html/module.f.js CHANGED
@@ -62,6 +62,12 @@ const parseElement = (e) => {
62
62
  [tag, item1, list] :
63
63
  [tag, {}, [item1, ...list]];
64
64
  };
65
+ /**
66
+ * Converts a FunctionalScript element into a list of HTML string chunks.
67
+ *
68
+ * Chunks are returned instead of a single string to support composition with
69
+ * other list/string helpers in this codebase.
70
+ */
65
71
  export const element = (e) => {
66
72
  const [tag, a, n] = parseElement(e);
67
73
  const open = flat([[`<`, tag], attributes(a), [`>`]]);
@@ -70,5 +76,11 @@ export const element = (e) => {
70
76
  }
71
77
  return flat([open, rawText.includes(tag) ? [rawMap(n)] : nodes(n), ['</', tag, '>']]);
72
78
  };
79
+ /**
80
+ * Builds a complete HTML document by prepending `<!DOCTYPE html>`.
81
+ */
73
82
  export const html = compose(element)(listConcat(['<!DOCTYPE html>']));
83
+ /**
84
+ * Renders an HTML element tree to a final string.
85
+ */
74
86
  export const htmlToString = compose(html)(stringConcat);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -11,7 +11,7 @@
11
11
  "prepack": "tsc --NoEmit false",
12
12
  "test20": "npm run prepack && node --test",
13
13
  "test": "tsc && node --test --experimental-strip-types --experimental-test-coverage --test-coverage-include=**/module.f.ts",
14
- "index": "node ./dev/index/module.ts",
14
+ "index": "node ./fjs/module.ts r ./dev/index/module.f.ts",
15
15
  "fst": "node ./fjs/module.ts t",
16
16
  "fjs": "node ./fjs/module.ts",
17
17
  "ci-update": "node ./fjs/module.ts r ./ci/module.f.ts",
@@ -13,68 +13,3 @@ export const doFull = (cmd, param, cont) => ({
13
13
  });
14
14
  export const do_ = (cmd) => (param) => doFull(cmd, param, pure);
15
15
  export const begin = pure(undefined);
16
- //----------------------------------------------------------------------
17
- /*
18
- export type Operations = {
19
- readonly [command in string]: readonly [input: unknown, output: unknown]
20
- }
21
-
22
- export type Effect<O extends Operations, T> = Pure<O, T> | Do<O, T>
23
-
24
- export type Pure<O, T> = readonly [T]
25
-
26
- export type One<O extends Operations, T, K extends keyof O & string> =
27
- readonly [K, O[K][0], (input: O[K][1]) => Effect<O, T>]
28
-
29
- export type Do<O extends Operations, T> = { readonly [K in keyof O & string]: One<O, T, K> }[keyof O & string]
30
-
31
- export const pure = <O extends Operations, T>(value: T): Pure<O, T> => [value]
32
-
33
- const doFull = <O extends Operations, K extends keyof O & string, T>(
34
- cmd: K,
35
- payload: O[K][0],
36
- cont: (input: O[K][1]) => Effect<O, T>
37
- ): Do<O, T> =>
38
- [cmd, payload, cont]
39
-
40
- export const do_ = <O extends Operations, K extends keyof O & string>(
41
- cmd: K,
42
- payload: O[K][0]
43
- ): Do<O, O[K][1]> =>
44
- doFull(cmd, payload, pure)
45
-
46
- export type ToAsyncOperationMap<O extends Operations> = {
47
- readonly [K in keyof O]: (payload: O[K][0]) => Promise<O[K][1]>
48
- }
49
-
50
- export const step =
51
- <O extends Operations, T>(e: Effect<O, T>) =>
52
- <O1 extends Operations, R>(f: (_: T) => Effect<O1, R>): Effect<O | O1, R> =>
53
- {
54
- if (e.length === 1) {
55
- const [value] = e
56
- return f(value)
57
- }
58
- const [cmd, payload, cont] = e
59
- return doFull(cmd, payload, x => step(cont(x))(f))
60
- }
61
-
62
- export const map =
63
- <O extends Operations, T>(e: Effect<O, T>) =>
64
- <R>(f: (_: T) => R): Effect<O, R> =>
65
- step(e)(x => pure(f(x)))
66
-
67
- export type Fluent<O extends Operations, T> = {
68
- readonly effect: Effect<O, T>
69
- readonly step: <O1 extends Operations, R>(f: (_: T) => Effect<O1, R>) => Fluent<O | O1, R>
70
- }
71
-
72
- const wrap = <O extends Operations, T>(effect: Effect<O, T>): Fluent<O, T> => ({
73
- effect,
74
- step: x => wrap(step(effect)(x)),
75
- })
76
-
77
- export const fluent: Fluent<{}, void> = wrap(pure(undefined))
78
-
79
- const empty: Effect<{}, readonly never[]> = pure([])
80
- */
@@ -11,6 +11,7 @@ export type All = ['all', <T>(_: readonly Effect<never, T>[]) => readonly T[]];
11
11
  * @returns
12
12
  */
13
13
  export declare const all: <O extends Operation, T>(...a: readonly Effect<O, T>[]) => Effect<O | All, readonly T[]>;
14
+ export declare const both: <O0 extends Operation, T0>(a: Effect<O0, T0>) => <O1 extends Operation, T1>(b: Effect<O1, T1>) => Effect<O0 | O1 | All, readonly [T0, T1]>;
14
15
  export type Fetch = ['fetch', (_: string) => IoResult<Vec>];
15
16
  export declare const fetch: Func<Fetch>;
16
17
  export type MakeDirectoryOptions = {
@@ -6,10 +6,8 @@ import { do_ } from "../module.f.js";
6
6
  * @param a
7
7
  * @returns
8
8
  */
9
- export const all = (...a) => {
10
- const result = do_('all')(a);
11
- return result;
12
- };
9
+ export const all = (...a) => do_('all')(a);
10
+ export const both = (a) => (b) => all(a, b);
13
11
  export const fetch = do_('fetch');
14
12
  export const mkdir = (...p) => do_('mkdir')(p);
15
13
  export const readFile = do_('readFile');
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { index } from "../module.f.js";
2
- import { legacyRun } from "../../io/module.js";
3
- legacyRun(index);