functionalscript 0.11.1 → 0.11.2

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Effect } from '../types/effects/module.f.ts';
2
- import type { NodeOp } from '../types/effects/node/module.f.ts';
2
+ import type { NodeOp, RequestListener } 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
@@ -79,6 +79,12 @@ export type Process = {
79
79
  readonly stderr: Writable;
80
80
  };
81
81
  export type TryCatch = <T>(f: () => T) => Result<T, unknown>;
82
+ export type Server = {
83
+ readonly listen: (port: number) => void;
84
+ };
85
+ export type Https = {
86
+ readonly createServer: (_: RequestListener) => Server;
87
+ };
82
88
  /**
83
89
  * Core IO operations interface providing access to system resources
84
90
  */
@@ -91,6 +97,7 @@ export type Io = {
91
97
  readonly fetch: (url: string) => Promise<Response>;
92
98
  readonly tryCatch: TryCatch;
93
99
  readonly asyncTryCatch: <T>(f: () => Promise<T>) => Promise<Result<T, unknown>>;
100
+ readonly https: Https;
94
101
  };
95
102
  /**
96
103
  * The environment variables.
@@ -106,4 +113,4 @@ export type Run = (f: App) => Promise<never>;
106
113
  */
107
114
  export declare const run: (io: Io) => Run;
108
115
  export type EffectToPromise = <T>(effect: Effect<NodeOp, T>) => Promise<T>;
109
- export declare const fromIo: ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, }: Io) => EffectToPromise;
116
+ export declare const fromIo: ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, https: { createServer }, }: Io) => EffectToPromise;
package/io/module.f.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { todo } from "../dev/module.f.js";
2
2
  import { normalize } from "../path/module.f.js";
3
- import { pure } from "../types/effects/module.f.js";
3
+ import {} from "../types/effects/module.f.js";
4
4
  import { asyncRun } from "../types/effects/module.js";
5
+ import { asBase, asNominal } from "../types/nominal/module.f.js";
5
6
  import { error, ok } from "../types/result/module.f.js";
6
7
  import { fromVec, toVec } from "../types/uint8array/module.f.js";
7
8
  /**
@@ -28,9 +29,9 @@ const tc = async (f) => {
28
29
  return error(e);
29
30
  }
30
31
  };
31
- export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, }) => {
32
+ export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, https: { createServer }, }) => {
32
33
  const result = asyncRun({
33
- all: async (effects) => await Promise.all(effects.map(v => result(v))),
34
+ all: async (effects) => await Promise.all(effects.map(result)),
34
35
  error: async (message) => error(message),
35
36
  log: async (message) => log(message),
36
37
  fetch: async (url) => tc(async () => {
@@ -45,6 +46,14 @@ export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readF
45
46
  readdir: ([path, r]) => tc(async () => (await readdir(path, { ...r, withFileTypes: true }))
46
47
  .map(v => ({ name: v.name, parentPath: normalize(v.parentPath), isFile: v.isFile() }))),
47
48
  writeFile: ([path, data]) => tc(() => writeFile(path, fromVec(data))),
49
+ createServer: async (requestListener) => {
50
+ const server = asNominal(createServer(requestListener));
51
+ return server;
52
+ },
53
+ listen: async ([server, port]) => {
54
+ const s = asBase(server);
55
+ s.listen(port);
56
+ },
48
57
  });
49
58
  return result;
50
59
  };
package/io/module.js CHANGED
@@ -6,6 +6,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
+ import https from 'node:https';
9
10
  import { fromIo, run } from "./module.f.js";
10
11
  import fs from 'node:fs';
11
12
  import process from "node:process";
@@ -39,6 +40,7 @@ export const io = {
39
40
  return error(e);
40
41
  }
41
42
  },
43
+ https
42
44
  };
43
45
  export const legacyRun = run(io);
44
46
  export const ioRun = (io) => {
@@ -1,4 +1,5 @@
1
1
  import { at } from "../../types/ordered_map/module.f.js";
2
+ import { todo } from "../../dev/module.f.js";
2
3
  export const createVirtualIo = (files) => ({
3
4
  console: {
4
5
  log: (..._d) => { },
@@ -33,4 +34,7 @@ export const createVirtualIo = (files) => ({
33
34
  fetch: () => Promise.reject(),
34
35
  tryCatch: f => ['ok', f()],
35
36
  asyncTryCatch: async (f) => ['ok', await f()],
37
+ https: {
38
+ createServer: todo
39
+ }
36
40
  });
@@ -7,4 +7,10 @@ import { type Result } from '../../types/result/module.f.ts';
7
7
  import { type List } from '../../types/list/module.f.ts';
8
8
  import { type JsonToken } from '../tokenizer/module.f.ts';
9
9
  import { type Unknown } from '../module.f.ts';
10
+ /**
11
+ * Parses a list of JSON tokens into a JSON-compatible value.
12
+ *
13
+ * Returns `ok` with the parsed value on success, or `error` with a message
14
+ * when the token sequence is invalid or incomplete.
15
+ */
10
16
  export declare const parse: (tokenList: List<JsonToken>) => Result<Unknown, string>;
@@ -156,6 +156,12 @@ const foldOp = token => state => {
156
156
  case '{,': return parseObjectCommaOp(token)(state);
157
157
  }
158
158
  };
159
+ /**
160
+ * Parses a list of JSON tokens into a JSON-compatible value.
161
+ *
162
+ * Returns `ok` with the parsed value on success, or `error` with a message
163
+ * when the token sequence is invalid or incomplete.
164
+ */
159
165
  export const parse = tokenList => {
160
166
  const state = fold(foldOp)({ status: '', top: null, stack: null })(tokenList);
161
167
  switch (state.status) {
@@ -4,9 +4,21 @@
4
4
  * @module
5
5
  */
6
6
  import { type List } from '../../types/list/module.f.ts';
7
+ /**
8
+ * Serializes a string as a JSON string literal.
9
+ */
7
10
  export declare const stringSerialize: (_: string) => List<string>;
11
+ /**
12
+ * Serializes a number as a JSON number literal.
13
+ */
8
14
  export declare const numberSerialize: (_: number) => List<string>;
15
+ /**
16
+ * Shared serialized representation for `null`.
17
+ */
9
18
  export declare const nullSerialize: string[];
10
19
  export declare const boolSerialize: (_: boolean) => List<string>;
11
20
  export declare const objectWrap: (input: List<List<string>>) => List<string>;
21
+ /**
22
+ * Wraps serialized entries into a JSON array.
23
+ */
12
24
  export declare const arrayWrap: (input: List<List<string>>) => List<string>;
@@ -7,8 +7,17 @@ import { flat, reduce, empty } from "../../types/list/module.f.js";
7
7
  import {} from "../../types/object/module.f.js";
8
8
  import {} from "../../types/function/operator/module.f.js";
9
9
  const jsonStringify = JSON.stringify;
10
+ /**
11
+ * Serializes a string as a JSON string literal.
12
+ */
10
13
  export const stringSerialize = input => [jsonStringify(input)];
14
+ /**
15
+ * Serializes a number as a JSON number literal.
16
+ */
11
17
  export const numberSerialize = input => [jsonStringify(input)];
18
+ /**
19
+ * Shared serialized representation for `null`.
20
+ */
12
21
  export const nullSerialize = ['null'];
13
22
  const trueSerialize = ['true'];
14
23
  const falseSerialize = ['false'];
@@ -22,4 +31,7 @@ const wrap = open => close => {
22
31
  return input => flat([seqOpen, join(input), seqClose]);
23
32
  };
24
33
  export const objectWrap = wrap('{')('}');
34
+ /**
35
+ * Wraps serialized entries into a JSON array.
36
+ */
25
37
  export const arrayWrap = wrap('[')(']');
@@ -5,4 +5,11 @@ export type JsonToken = {
5
5
  } | {
6
6
  readonly kind: '{' | '}' | ':' | ',' | '[' | ']';
7
7
  } | StringToken | NumberToken | ErrorToken | EofToken;
8
+ /**
9
+ * Converts a stream of UTF-8 bytes into JSON tokens.
10
+ *
11
+ * The tokenizer accepts only JSON-compatible JavaScript tokens, ignores
12
+ * whitespace/newline tokens, and reports invalid token sequences as
13
+ * `{ kind: 'error' }` tokens.
14
+ */
8
15
  export declare const tokenize: (input: List<number>) => List<JsonToken>;
@@ -50,6 +50,13 @@ const scanToken = state => input => {
50
50
  default: return parseDefaultState(input);
51
51
  }
52
52
  };
53
+ /**
54
+ * Converts a stream of UTF-8 bytes into JSON tokens.
55
+ *
56
+ * The tokenizer accepts only JSON-compatible JavaScript tokens, ignores
57
+ * whitespace/newline tokens, and reports invalid token sequences as
58
+ * `{ kind: 'error' }` tokens.
59
+ */
53
60
  export const tokenize = (input) => {
54
61
  const jsTokens = jsTokenize(input)('');
55
62
  return flat(stateScan(scanToken)({ kind: 'def' })(concat(jsTokens)([null])));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.1",
3
+ "version": "0.11.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -4,6 +4,19 @@
4
4
  * @module
5
5
  */
6
6
  import type { Reduce, Unary } from "../types/function/operator/module.f.ts";
7
+ /**
8
+ * Splits a path into normalized segments.
9
+ *
10
+ * Empty (`""`) and current-directory (`"."`) segments are removed, parent-directory
11
+ * (`".."`) segments collapse the previous segment when possible, and Windows
12
+ * separators are converted to POSIX separators.
13
+ */
7
14
  export declare const parse: (path: string) => readonly string[];
15
+ /**
16
+ * Normalizes a path string by parsing and rejoining it with POSIX separators.
17
+ */
8
18
  export declare const normalize: Unary<string, string>;
19
+ /**
20
+ * Concatenates two path fragments and returns a normalized path.
21
+ */
9
22
  export declare const concat: Reduce<string>;
package/path/module.f.js CHANGED
@@ -21,14 +21,27 @@ const foldNormalizeOp = input => state => {
21
21
  }
22
22
  }
23
23
  };
24
+ /**
25
+ * Splits a path into normalized segments.
26
+ *
27
+ * Empty (`""`) and current-directory (`"."`) segments are removed, parent-directory
28
+ * (`".."`) segments collapse the previous segment when possible, and Windows
29
+ * separators are converted to POSIX separators.
30
+ */
24
31
  export const parse = (path) => {
25
32
  const split = path.replaceAll('\\', '/').split('/');
26
33
  return toArray(fold(foldNormalizeOp)([])(split));
27
34
  };
35
+ /**
36
+ * Normalizes a path string by parsing and rejoining it with POSIX separators.
37
+ */
28
38
  export const normalize = path => {
29
39
  const foldResult = parse(path);
30
40
  return join('/')(foldResult);
31
41
  };
42
+ /**
43
+ * Concatenates two path fragments and returns a normalized path.
44
+ */
32
45
  export const concat = a => b => {
33
46
  const s = stringConcat([a, '/', b]);
34
47
  return normalize(s);
@@ -1,5 +1,12 @@
1
+ /**
2
+ * Provides ASCII code point constants and helpers for creating numeric code points and inclusive ranges.
3
+ *
4
+ * @module
5
+ */
1
6
  import type { Range } from '../../types/range/module.f.ts';
7
+ /** Returns the code point value of the first character in a string. */
2
8
  export declare const one: (s: string) => number;
9
+ /** Returns an inclusive code point range from the first two characters in a string. */
3
10
  export declare const range: (s: string) => Range;
4
11
  /** 0x08 */
5
12
  export declare const backspace: number;
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Provides ASCII code point constants and helpers for creating numeric code points and inclusive ranges.
3
+ *
4
+ * @module
5
+ */
1
6
  const at = (s) => (i) => {
2
7
  const r = s.codePointAt(i);
3
8
  if (r === void 0) {
@@ -5,7 +10,9 @@ const at = (s) => (i) => {
5
10
  }
6
11
  return r;
7
12
  };
13
+ /** Returns the code point value of the first character in a string. */
8
14
  export const one = (s) => at(s)(0);
15
+ /** Returns an inclusive code point range from the first two characters in a string. */
9
16
  export const range = (s) => {
10
17
  const f = at(s);
11
18
  const f0 = f(0);
@@ -1,3 +1,9 @@
1
+ /**
2
+ * ANSI Control Sequence Introducer (CSI) and Select Graphic Rendition (SGR)
3
+ * helpers for writing formatted terminal output and TTY-aware console streams.
4
+ *
5
+ * @module
6
+ */
1
7
  import type { Io, Writable } from "../../io/module.f.ts";
2
8
  export declare const backspace: string;
3
9
  type End = 'm';
@@ -1,5 +1,9 @@
1
- // Co control codes
2
- // https://en.wikipedia.org/wiki/ANSI_escape_code#C0_control_codes
1
+ /**
2
+ * ANSI Control Sequence Introducer (CSI) and Select Graphic Rendition (SGR)
3
+ * helpers for writing formatted terminal output and TTY-aware console streams.
4
+ *
5
+ * @module
6
+ */
3
7
  export const backspace = '\x08';
4
8
  const begin = '\x1b[';
5
9
  /**
@@ -1,4 +1,5 @@
1
1
  import type { Vec } from '../../bit_vec/module.f.ts';
2
+ import type { Nominal } from '../../nominal/module.f.ts';
2
3
  import type { Result } from '../../result/module.f.ts';
3
4
  import { type Effect, type Func, type Operation, type ToAsyncOperationMap } from '../module.f.ts';
4
5
  export type IoResult<T> = Result<T, unknown>;
@@ -46,7 +47,15 @@ export declare const error: Func<Error>;
46
47
  export type Log = ['log', (_: string) => void];
47
48
  export declare const log: Func<Log>;
48
49
  export type Console = Log | Error;
49
- export type NodeOp = All | Fetch | Console | Fs;
50
+ export type Server = Nominal<'server', `160855c4f69310fece3273c1853ac32de43dee1eb41bf59d821917f8eebe9272`, unknown>;
51
+ export type IncomingMessage = {};
52
+ export type ServerResponse = {};
53
+ export type RequestListener = (_: IncomingMessage) => ServerResponse;
54
+ export type CreateServer = ['createServer', (_: RequestListener) => Server];
55
+ export declare const createServer: Func<CreateServer>;
56
+ export type Listen = ['listen', (_: readonly [Server, number]) => void];
57
+ export type Http = CreateServer | Listen;
58
+ export type NodeOp = All | Fetch | Console | Fs | Http;
50
59
  export type NodeEffect<T> = Effect<NodeOp, T>;
51
60
  export type NodeOperationMap = ToAsyncOperationMap<NodeOp>;
52
61
  export type NodeProgram = (argv: readonly string[]) => Effect<NodeOp, number>;
@@ -15,3 +15,4 @@ export const readdir = (...p) => do_('readdir')(p);
15
15
  export const writeFile = (...p) => do_('writeFile')(p);
16
16
  export const error = do_('error');
17
17
  export const log = do_('log');
18
+ export const createServer = do_('createServer');
@@ -3,11 +3,11 @@
3
3
  *
4
4
  * @module
5
5
  */
6
+ import { todo } from "../../../../dev/module.f.js";
6
7
  import { parse } from "../../../../path/module.f.js";
7
8
  import { isVec } from "../../../bit_vec/module.f.js";
8
9
  import { error, ok } from "../../../result/module.f.js";
9
10
  import { run } from "../../mock/module.f.js";
10
- import { pure } from "../../module.f.js";
11
11
  export const emptyState = {
12
12
  stdout: '',
13
13
  stderr: '',
@@ -115,5 +115,7 @@ const map = {
115
115
  readFile,
116
116
  readdir: (state, [path, { recursive }]) => readdir(path, recursive === true)(state, path),
117
117
  writeFile: (state, [path, payload]) => writeFile(payload)(state, path),
118
+ createServer: todo,
119
+ listen: todo,
118
120
  };
119
121
  export const virtual = run(map);
@@ -3,8 +3,20 @@
3
3
  *
4
4
  * @module
5
5
  */
6
+ /**
7
+ * Nominal type.
8
+ *
9
+ * It doesn't allow `===` between different nominal types.
10
+ * It doesn't allow `<`, `>`, `<=`, `>=` comparisons at all.
11
+ */
6
12
  export type Nominal<N extends string, R extends string, B> = symbol & {
7
13
  [k in N]: readonly [R, B];
8
14
  };
15
+ /**
16
+ * note: It should compiles into `identity` and no-ops at runtime.
17
+ */
9
18
  export declare const asNominal: <N extends string, R extends string, B>(b: B) => Nominal<N, R, B>;
19
+ /**
20
+ * note: It should compiles into `identity` and no-ops at runtime.
21
+ */
10
22
  export declare const asBase: <T extends string, R extends string, B>(n: Nominal<T, R, B>) => B;
@@ -1,4 +1,13 @@
1
- /// note: It should compiles into `identity` and no-ops at runtime.
1
+ /**
2
+ * Nominal typing helpers for branded TypeScript types.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * note: It should compiles into `identity` and no-ops at runtime.
8
+ */
2
9
  export const asNominal = (b) => b;
3
- /// note: It should compiles into `identity` and no-ops at runtime.
10
+ /**
11
+ * note: It should compiles into `identity` and no-ops at runtime.
12
+ */
4
13
  export const asBase = (n) => n;