functionalscript 0.11.5 → 0.11.7

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.js CHANGED
@@ -53,14 +53,16 @@ export const fromIo = ({ console: { error, log }, fs: { promises: { mkdir, readF
53
53
  .map(v => ({ name: v.name, parentPath: normalize(v.parentPath), isFile: v.isFile() }))),
54
54
  writeFile: ([path, data]) => tc(() => writeFile(path, fromVec(data))),
55
55
  createServer: async (requestListener) => {
56
+ const erl = requestListener;
56
57
  const nodeRl = async (req, res) => {
58
+ const reqBody = await collect(req);
57
59
  const { method, url, headers } = req;
58
- const { status, headers: outHeaders, body: outBody } = requestListener({
60
+ const { status, headers: outHeaders, body: outBody } = await result(erl({
59
61
  method,
60
62
  url,
61
63
  headers,
62
- body: listToVec(await collect(req))
63
- });
64
+ body: listToVec(reqBody)
65
+ }));
64
66
  res
65
67
  .writeHead(status, outHeaders)
66
68
  .end(fromVec(outBody));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.11.5",
3
+ "version": "0.11.7",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "**/*.js",
@@ -169,7 +169,7 @@ export declare const msb: BitOrder;
169
169
  * @param list The list of unsigned 8-bit integers to be converted.
170
170
  * @returns The resulting vector based on the provided bit order.
171
171
  */
172
- export declare const u8ListToVec: (bo: BitOrder) => (list: List<number>) => Vec;
172
+ export declare const u8ListToVec: ({ concat }: BitOrder) => (list: List<number>) => Vec;
173
173
  /**
174
174
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
175
175
  *
@@ -23,7 +23,7 @@
23
23
  */
24
24
  import { bitLength, mask, max, min, xor } from "../bigint/module.f.js";
25
25
  import { flip } from "../function/module.f.js";
26
- import { fold } from "../list/module.f.js";
26
+ import { entries, fold, iterable } from "../list/module.f.js";
27
27
  import { asBase, asNominal } from "../nominal/module.f.js";
28
28
  import { repeat as mRepeat } from "../monoid/module.f.js";
29
29
  import { cmp } from "../function/compare/module.f.js";
@@ -169,7 +169,6 @@ export const msb = {
169
169
  concat: flip(lsb.concat),
170
170
  xor: op(msbNorm)(xor)
171
171
  };
172
- const appendU8 = ({ concat }) => (u8) => (a) => concat(a)(vec8(BigInt(u8)));
173
172
  /**
174
173
  * Converts a list of unsigned 8-bit integers to a bit vector using the provided bit order.
175
174
  *
@@ -177,7 +176,34 @@ const appendU8 = ({ concat }) => (u8) => (a) => concat(a)(vec8(BigInt(u8)));
177
176
  * @param list The list of unsigned 8-bit integers to be converted.
178
177
  * @returns The resulting vector based on the provided bit order.
179
178
  */
180
- export const u8ListToVec = (bo) => fold(appendU8(bo))(empty);
179
+ export const u8ListToVec = ({ concat }) => (list) => {
180
+ // much faster than: `fold(appendU8(bo))(empty)(list)`
181
+ // where `appendU8` is defined as
182
+ // ```
183
+ // const appendU8 = ({ concat }: BitOrder) => (u8: number) => (a: Vec) =>
184
+ // concat(a)(vec8(BigInt(u8)))
185
+ // ```
186
+ let result = [];
187
+ for (const b of iterable(list)) {
188
+ let v = vec8(BigInt(b));
189
+ let i = 0;
190
+ while (true) {
191
+ if (result.length <= i) {
192
+ result = [...result, v];
193
+ break;
194
+ }
195
+ const old = result[i];
196
+ if (old === empty) {
197
+ result = result.toSpliced(i, 1, v);
198
+ break;
199
+ }
200
+ result = result.toSpliced(i, 1, empty);
201
+ v = concat(old)(v);
202
+ i++;
203
+ }
204
+ }
205
+ return result.reduce((p, c) => concat(c)(p), empty);
206
+ };
181
207
  /**
182
208
  * Converts a bit vector to a list of unsigned 8-bit integers based on the provided bit order.
183
209
  *
@@ -38,5 +38,6 @@ declare const _default: {
38
38
  msbXor: () => void;
39
39
  repeat: () => void;
40
40
  msbCmp: () => void;
41
+ u8ListToVec: () => void;
41
42
  };
42
43
  export default _default;
@@ -1,6 +1,7 @@
1
1
  import { mask } from "../bigint/module.f.js";
2
2
  import { asBase, asNominal } from "../nominal/module.f.js";
3
- import { length, empty, uint, vec, lsb, msb, repeat, vec8, msbCmp } from "./module.f.js";
3
+ import { length, empty, uint, vec, lsb, msb, repeat, vec8, msbCmp, u8ListToVec } from "./module.f.js";
4
+ import { repeat as listRepeat } from "../list/module.f.js";
4
5
  const unsafeVec = (a) => asNominal(a);
5
6
  // 0x8 = 0b1000 = 0 + 8
6
7
  // 0x9 = 0b1001 = 1 + 8
@@ -442,5 +443,9 @@ export default {
442
443
  c(vec(4n)(0x5n))(vec(5n)(0x5n))(1); // 0b0101_ < 0b00101
443
444
  c(vec(5n)(0x5n))(vec(4n)(0x5n))(-1); // 0b00101 < 0b0101_
444
445
  c(vec(4n)(0x5n))(vec(5n)(0xan))(-1); // 0b0101_ < 0b01010
446
+ },
447
+ u8ListToVec: () => {
448
+ // 131_072 is too much for Bun
449
+ const x = u8ListToVec(msb)(listRepeat(0x12)(131_071));
445
450
  }
446
451
  };
@@ -62,9 +62,9 @@ export type ServerResponse = {
62
62
  readonly headers: Headers;
63
63
  readonly body: Vec;
64
64
  };
65
- export type RequestListener = (_: IncomingMessage) => ServerResponse;
66
- export type CreateServer = ['createServer', (_: RequestListener) => Server];
67
- export declare const createServer: Func<CreateServer>;
65
+ export type RequestListener<O extends Operation> = (_: IncomingMessage) => Effect<O, ServerResponse>;
66
+ export type CreateServer = ['createServer', (_: RequestListener<Operation>) => Server];
67
+ export declare const createServer: <O extends Operation>(listener: RequestListener<O>) => Effect<O | CreateServer, Server>;
68
68
  export type Listen = ['listen', (_: readonly [Server, number]) => void];
69
69
  export declare const listen: RestFunc<Listen>;
70
70
  export type Http = CreateServer | Listen;
@@ -1,4 +1,5 @@
1
1
  import { doRest, do_ } from "../module.f.js";
2
+ const doAll = do_('all');
2
3
  /**
3
4
  * To run the operation `O` should be known by the runner/engine.
4
5
  * This is the reason why we merge `O` with `All` in the resulted `Effect`.
@@ -6,7 +7,7 @@ import { doRest, do_ } from "../module.f.js";
6
7
  * @param a
7
8
  * @returns
8
9
  */
9
- export const all = (...a) => do_('all')(a);
10
+ export const all = (...a) => doAll(a);
10
11
  export const both = (a) => (b) => all(a, b);
11
12
  export const fetch = do_('fetch');
12
13
  export const mkdir = doRest('mkdir');
@@ -7,10 +7,13 @@ import { htmlToString } from "../html/module.f.js";
7
7
  import { writeFile } from "../types/effects/node/module.f.js";
8
8
  import { utf8 } from "../text/module.f.js";
9
9
  import { begin, pure } from "../types/effects/module.f.js";
10
- const html = ['body',
11
- ['a', { href: 'https://github.com/functionalscript/functionalscript' }, 'GitHub Repository']
12
- ];
10
+ const html = utf8(htmlToString(['body',
11
+ ['a',
12
+ { href: 'https://github.com/functionalscript/functionalscript' },
13
+ 'GitHub Repository'
14
+ ]
15
+ ]));
13
16
  const program = begin
14
- .step(() => writeFile('index.html', utf8(htmlToString(html))))
17
+ .step(() => writeFile('index.html', html))
15
18
  .step(() => pure(0));
16
19
  export default () => program;