functionalscript 0.11.3 → 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 CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Effect } from '../types/effects/module.f.ts';
2
- import type { NodeOp, RequestListener } from '../types/effects/node/module.f.ts';
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 Https = {
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 https: Https;
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, https: { createServer }, }: Io) => EffectToPromise;
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
- export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readFile, readdir, writeFile } }, fetch, https: { createServer }, }) => {
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 server = asNominal(createServer(requestListener));
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 https from 'node:https';
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 "node:process";
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
- https
43
+ http,
44
44
  };
45
45
  export const legacyRun = run(io);
46
46
  export const ioRun = (io) => {
@@ -34,7 +34,7 @@ export const createVirtualIo = (files) => ({
34
34
  fetch: () => Promise.reject(),
35
35
  tryCatch: f => ['ok', f()],
36
36
  asyncTryCatch: async (f) => ['ok', await f()],
37
- https: {
37
+ http: {
38
38
  createServer: todo
39
39
  }
40
40
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -1,3 +1,8 @@
1
+ /**
2
+ * UTF-8 byte-level encoding and decoding utilities for FunctionalScript streams.
3
+ *
4
+ * @module
5
+ */
1
6
  import { type List, type Thunk } from '../../types/list/module.f.ts';
2
7
  import type { Array1, Array2, Array3 } from '../../types/array/module.f.ts';
3
8
  /**
@@ -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.
@@ -48,8 +48,20 @@ 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 IncomingMessage = {};
52
- export type ServerResponse = {};
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>;
@@ -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
  */