@davidsouther/jiffies 1.0.0-beta.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/README.md +60 -0
- package/build/assert.d.ts +23 -0
- package/build/assert.js +33 -0
- package/build/case.d.ts +1 -0
- package/build/case.js +5 -0
- package/build/components/button_bar.d.ts +8 -0
- package/build/components/button_bar.js +16 -0
- package/build/components/index.d.ts +1 -0
- package/build/components/index.js +1 -0
- package/build/components/inline_edit.d.ts +12 -0
- package/build/components/inline_edit.js +48 -0
- package/build/components/logger.d.ts +7 -0
- package/build/components/logger.js +22 -0
- package/build/components/select.d.ts +13 -0
- package/build/components/select.js +3 -0
- package/build/components/test.d.ts +1 -0
- package/build/components/test.js +2 -0
- package/build/components/virtual_scroll.d.ts +41 -0
- package/build/components/virtual_scroll.js +94 -0
- package/build/components/virtual_scroll.test.d.ts +1 -0
- package/build/components/virtual_scroll.test.js +21 -0
- package/build/context.d.ts +15 -0
- package/build/context.js +43 -0
- package/build/context.test.d.ts +1 -0
- package/build/context.test.js +46 -0
- package/build/debounce.d.ts +1 -0
- package/build/debounce.js +7 -0
- package/build/display.d.ts +5 -0
- package/build/display.js +3 -0
- package/build/dom/css/border.d.ts +11 -0
- package/build/dom/css/border.js +27 -0
- package/build/dom/css/constants.d.ts +31 -0
- package/build/dom/css/constants.js +28 -0
- package/build/dom/css/core.d.ts +5 -0
- package/build/dom/css/core.js +24 -0
- package/build/dom/css/fstyle.d.ts +5 -0
- package/build/dom/css/fstyle.js +32 -0
- package/build/dom/css/sizing.d.ts +5 -0
- package/build/dom/css/sizing.js +10 -0
- package/build/dom/dom.d.ts +27 -0
- package/build/dom/dom.js +94 -0
- package/build/dom/fc.d.ts +14 -0
- package/build/dom/fc.js +35 -0
- package/build/dom/fc.test.d.ts +1 -0
- package/build/dom/fc.test.js +21 -0
- package/build/dom/form/form.app.d.ts +1 -0
- package/build/dom/form/form.app.js +23 -0
- package/build/dom/form/form.d.ts +25 -0
- package/build/dom/form/form.js +25 -0
- package/build/dom/form/form.test.d.ts +0 -0
- package/build/dom/form/form.test.js +1 -0
- package/build/dom/html.d.ts +117 -0
- package/build/dom/html.js +114 -0
- package/build/dom/html.test.d.ts +1 -0
- package/build/dom/html.test.js +58 -0
- package/build/dom/router/link.d.ts +6 -0
- package/build/dom/router/link.js +3 -0
- package/build/dom/router/router.d.ts +12 -0
- package/build/dom/router/router.js +49 -0
- package/build/dom/svg.d.ts +64 -0
- package/build/dom/svg.js +65 -0
- package/build/dom/test.d.ts +1 -0
- package/build/dom/test.js +2 -0
- package/build/dom/types/css.d.ts +6612 -0
- package/build/dom/types/css.js +23 -0
- package/build/dom/types/dom.d.ts +0 -0
- package/build/dom/types/dom.js +1 -0
- package/build/dom/types/html.d.ts +616 -0
- package/build/dom/types/html.js +1 -0
- package/build/dom/xml.d.ts +1 -0
- package/build/dom/xml.js +5 -0
- package/build/equal.d.ts +4 -0
- package/build/equal.js +22 -0
- package/build/equal.test.d.ts +1 -0
- package/build/equal.test.js +20 -0
- package/build/flags.d.ts +7 -0
- package/build/flags.js +48 -0
- package/build/flags.test.d.ts +1 -0
- package/build/flags.test.js +35 -0
- package/build/generator.d.ts +1 -0
- package/build/generator.js +10 -0
- package/build/generator.test.d.ts +1 -0
- package/build/generator.test.js +24 -0
- package/build/index.d.ts +13 -0
- package/build/index.js +13 -0
- package/build/is_browser.d.ts +1 -0
- package/build/is_browser.js +1 -0
- package/build/loader.d.mts +22 -0
- package/build/loader.mjs +35 -0
- package/build/lock.d.ts +1 -0
- package/build/lock.js +23 -0
- package/build/lock.test.d.ts +1 -0
- package/build/lock.test.js +16 -0
- package/build/log.d.ts +26 -0
- package/build/log.js +34 -0
- package/build/parcel_resolver.d.ts +3 -0
- package/build/parcel_resolver.js +19 -0
- package/build/range.d.ts +1 -0
- package/build/range.js +7 -0
- package/build/result.d.ts +31 -0
- package/build/result.js +65 -0
- package/build/result.test.d.ts +1 -0
- package/build/result.test.js +71 -0
- package/build/safe.d.ts +1 -0
- package/build/safe.js +10 -0
- package/build/scope/describe.d.ts +14 -0
- package/build/scope/describe.js +52 -0
- package/build/scope/display/console.d.ts +2 -0
- package/build/scope/display/console.js +21 -0
- package/build/scope/display/dom.d.ts +3 -0
- package/build/scope/display/dom.js +26 -0
- package/build/scope/display/junit.d.ts +2 -0
- package/build/scope/display/junit.js +17 -0
- package/build/scope/execute.d.ts +12 -0
- package/build/scope/execute.js +85 -0
- package/build/scope/expect.d.ts +23 -0
- package/build/scope/expect.js +107 -0
- package/build/scope/fix.d.ts +4 -0
- package/build/scope/fix.js +22 -0
- package/build/scope/index.d.ts +3 -0
- package/build/scope/index.js +3 -0
- package/build/scope/scope.d.ts +17 -0
- package/build/scope/scope.js +1 -0
- package/build/server/http/apps.d.ts +5 -0
- package/build/server/http/apps.js +23 -0
- package/build/server/http/index.d.ts +21 -0
- package/build/server/http/index.js +71 -0
- package/build/server/http/response.d.ts +4 -0
- package/build/server/http/response.js +37 -0
- package/build/server/http/sitemap.d.ts +2 -0
- package/build/server/http/sitemap.js +42 -0
- package/build/server/http/static.d.ts +2 -0
- package/build/server/http/static.js +21 -0
- package/build/server/http/typescript.d.ts +5 -0
- package/build/server/http/typescript.js +40 -0
- package/build/server/main.d.ts +2 -0
- package/build/server/main.js +9 -0
- package/build/test.d.mts +2 -0
- package/build/test.mjs +23 -0
- package/build/test_all.d.ts +1 -0
- package/build/test_all.js +19 -0
- package/build/transpile.d.mts +3 -0
- package/build/transpile.mjs +18 -0
- package/package.json +36 -0
- package/src/404.html +14 -0
- package/src/assert.ts +50 -0
- package/src/case.ts +5 -0
- package/src/components/_notes +33 -0
- package/src/components/button_bar.ts +38 -0
- package/src/components/inline_edit.ts +77 -0
- package/src/components/logger.ts +36 -0
- package/src/components/select.ts +22 -0
- package/src/components/test.js +2 -0
- package/src/components/virtual_scroll.test.ts +27 -0
- package/src/components/virtual_scroll.ts +194 -0
- package/src/context.test.ts +58 -0
- package/src/context.ts +62 -0
- package/src/debounce.ts +7 -0
- package/src/display.ts +12 -0
- package/src/dom/README.md +102 -0
- package/src/dom/css/border.ts +47 -0
- package/src/dom/css/constants.ts +34 -0
- package/src/dom/css/core.ts +28 -0
- package/src/dom/css/fstyle.ts +42 -0
- package/src/dom/css/sizing.ts +11 -0
- package/src/dom/dom.ts +153 -0
- package/src/dom/fc.test.ts +43 -0
- package/src/dom/fc.ts +79 -0
- package/src/dom/form/form.app.ts +50 -0
- package/src/dom/form/form.test.ts +0 -0
- package/src/dom/form/form.ts +53 -0
- package/src/dom/form/index.html +14 -0
- package/src/dom/html.test.ts +72 -0
- package/src/dom/html.ts +129 -0
- package/src/dom/router/link.ts +14 -0
- package/src/dom/router/router.ts +69 -0
- package/src/dom/svg.ts +77 -0
- package/src/dom/test.ts +2 -0
- package/src/dom/types/css.ts +10106 -0
- package/src/dom/types/dom.ts +0 -0
- package/src/dom/types/html.ts +631 -0
- package/src/dom/xml.ts +12 -0
- package/src/equal.test.ts +23 -0
- package/src/equal.ts +32 -0
- package/src/favicon.ico +0 -0
- package/src/flags.test.ts +43 -0
- package/src/flags.ts +53 -0
- package/src/generator.test.ts +26 -0
- package/src/generator.ts +12 -0
- package/src/hooks/_notes +3 -0
- package/src/index.html +79 -0
- package/src/is_browser.js +1 -0
- package/src/loader.mjs +45 -0
- package/src/lock.test.ts +17 -0
- package/src/lock.ts +22 -0
- package/src/log.ts +61 -0
- package/src/observable/_notes +13 -0
- package/src/observable/observable._js +175 -0
- package/src/range.ts +7 -0
- package/src/result.test.ts +98 -0
- package/src/result.ts +107 -0
- package/src/safe.ts +12 -0
- package/src/scope/describe.ts +70 -0
- package/src/scope/display/console.ts +26 -0
- package/src/scope/display/dom.ts +36 -0
- package/src/scope/display/junit.ts +67 -0
- package/src/scope/execute.ts +108 -0
- package/src/scope/expect.ts +170 -0
- package/src/scope/fix.ts +29 -0
- package/src/scope/index.ts +11 -0
- package/src/scope/scope.ts +21 -0
- package/src/server/http/apps.ts +26 -0
- package/src/server/http/index.ts +119 -0
- package/src/server/http/response.ts +47 -0
- package/src/server/http/sitemap.ts +48 -0
- package/src/server/http/static.ts +27 -0
- package/src/server/http/typescript.ts +46 -0
- package/src/server/main.ts +13 -0
- package/src/test.mjs +29 -0
- package/src/test_all.ts +22 -0
- package/src/transpile.mjs +29 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isHTMLLogger, makeHTMLLogger } from "../../components/logger.js";
|
|
2
|
+
import { DEFAULT_LOGGER, LEVEL } from "../../log.js";
|
|
3
|
+
import { getTotalCases } from "../describe.js";
|
|
4
|
+
import { flattenResults } from "../execute.js";
|
|
5
|
+
export function displayStatistics(results, root = document.body) {
|
|
6
|
+
const { executed, failed } = results;
|
|
7
|
+
const logger = (() => {
|
|
8
|
+
try {
|
|
9
|
+
return makeHTMLLogger(`Executed ${executed} of ${getTotalCases()}; ${failed} failed.`);
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
return DEFAULT_LOGGER;
|
|
13
|
+
}
|
|
14
|
+
})();
|
|
15
|
+
logger.level = LEVEL.DEBUG;
|
|
16
|
+
const flat = flattenResults(results);
|
|
17
|
+
for (const { test, stack } of flat) {
|
|
18
|
+
if (stack) {
|
|
19
|
+
logger.info(test);
|
|
20
|
+
logger.debug(`${stack}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (isHTMLLogger(logger)) {
|
|
24
|
+
root.appendChild(logger.root);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { xml } from "../../dom/xml.js";
|
|
2
|
+
import { flattenResults } from "../execute.js";
|
|
3
|
+
const cases = (results) => Object.entries(results).filter(([key]) => !["executed", "passed", "failed"].includes(key));
|
|
4
|
+
export function asXML(results) {
|
|
5
|
+
return (`<?xml version="1.0" encoding="UTF-8" ?>` +
|
|
6
|
+
xml("testsuites", { tests: results.executed, failures: results.failed }, cases(results).map(([title, children]) => testsuite(title, children.executed, children.failed, flattenResults(children)))));
|
|
7
|
+
}
|
|
8
|
+
function testsuite(name, tests, failures, cases) {
|
|
9
|
+
const id = name.replace("s+", "_");
|
|
10
|
+
return xml("testsuite", { id, name, tests, failures }, cases.map(({ test, stack }) => testcase({ name: `${name} ${test}` }, stack ? [stack] : [])));
|
|
11
|
+
}
|
|
12
|
+
function testcase({ name, id = name.replace(/\s+/g, "_"), time = "0.00", }, failures) {
|
|
13
|
+
return xml("testcase", { id, name, time }, failures.map((stack) => failure({ text: stack })));
|
|
14
|
+
}
|
|
15
|
+
function failure({ text, message = text.split("\n")[0], }) {
|
|
16
|
+
return xml("failure", { message }, [`<![CDATA[${text}]]>`]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { TestResult, TestSummary } from "./scope.js";
|
|
2
|
+
export declare function execute(prefix?: string, cases?: import("./scope.js").TestCase): Promise<TestResult>;
|
|
3
|
+
export declare function getError({ error }: TestResult): number | TestResult | TestSummary | "unknown error";
|
|
4
|
+
export interface FlatResult {
|
|
5
|
+
test: string;
|
|
6
|
+
stack?: string | number | TestResult | TestSummary;
|
|
7
|
+
stats: {
|
|
8
|
+
executed: number;
|
|
9
|
+
failed: number;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export declare function flattenResults(results: TestResult, prefix?: string): FlatResult[];
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { afterall, aftereach, beforeall, beforeeach, rootCases, } from "./describe.js";
|
|
2
|
+
export async function execute(prefix = "", cases = rootCases()) {
|
|
3
|
+
const beforeallfn = cases[beforeall] ?? (() => { });
|
|
4
|
+
const beforeeachfn = cases[beforeeach] ?? (() => { });
|
|
5
|
+
const afterallfn = cases[afterall] ?? (() => { });
|
|
6
|
+
const aftereachfn = cases[aftereach] ?? (() => { });
|
|
7
|
+
const result = { executed: 0, passed: 0, failed: 0, total: 0 };
|
|
8
|
+
try {
|
|
9
|
+
await beforeallfn();
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
result["_beforeAll"] = { error: e };
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
for (const [title, block] of Object.entries(cases)) {
|
|
16
|
+
if (typeof title === "symbol") {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (block instanceof Function) {
|
|
20
|
+
try {
|
|
21
|
+
result.executed += 1;
|
|
22
|
+
await beforeeachfn();
|
|
23
|
+
await block();
|
|
24
|
+
await aftereachfn();
|
|
25
|
+
result.passed += 1;
|
|
26
|
+
result[title] = { passed: true };
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
result.failed += 1;
|
|
30
|
+
result[title] = { error: /** @type Error */ e };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else if (block) {
|
|
34
|
+
const run = await execute(title, block);
|
|
35
|
+
result.executed += run.executed;
|
|
36
|
+
result.passed += run.passed;
|
|
37
|
+
result.failed += run.failed;
|
|
38
|
+
result[title] = run;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
await afterallfn();
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
result["_afterAll"] = { error: e };
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
export function getError({ error }) {
|
|
50
|
+
if (typeof error == "string") {
|
|
51
|
+
return error;
|
|
52
|
+
}
|
|
53
|
+
else if (error.message) {
|
|
54
|
+
return error.stack;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return "unknown error";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function makeResult(test, result) {
|
|
61
|
+
if (result.error)
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
test,
|
|
65
|
+
stack: getError(result),
|
|
66
|
+
stats: { executed: 1, failed: 1 },
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
if (result.passed) {
|
|
70
|
+
return [{ test, stats: { executed: 1, failed: 0 } }];
|
|
71
|
+
}
|
|
72
|
+
return flattenResults(result, test);
|
|
73
|
+
}
|
|
74
|
+
export function flattenResults(results, prefix = "") {
|
|
75
|
+
const arrow = prefix == "" ? "" : " -> ";
|
|
76
|
+
let errorList = [];
|
|
77
|
+
for (const [title, result] of Object.entries(results).filter(([key]) => !["executed", "passed", "failed"].includes(key))) {
|
|
78
|
+
const test = `${prefix}${arrow}${title}`;
|
|
79
|
+
if (typeof result == "number")
|
|
80
|
+
continue;
|
|
81
|
+
const flatResult = makeResult(test, result);
|
|
82
|
+
errorList = errorList.concat(flatResult);
|
|
83
|
+
}
|
|
84
|
+
return errorList;
|
|
85
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class Matcher<T> {
|
|
2
|
+
actual: T;
|
|
3
|
+
constructor(actual: T);
|
|
4
|
+
get not(): Matcher<T>;
|
|
5
|
+
toBe(expected: T): void;
|
|
6
|
+
toEqual(expected: T): void;
|
|
7
|
+
toMatch(expected: RegExp | string): void;
|
|
8
|
+
toMatchObject(expected: Partial<T>): void;
|
|
9
|
+
toBeNull(): void;
|
|
10
|
+
toThrow(message?: string): void;
|
|
11
|
+
}
|
|
12
|
+
export declare class NotMatcher<T> {
|
|
13
|
+
actual: T;
|
|
14
|
+
constructor(actual: T);
|
|
15
|
+
get not(): Matcher<T>;
|
|
16
|
+
toBe(expected: T): void;
|
|
17
|
+
toEqual(expected: T): void;
|
|
18
|
+
toMatch(expected: RegExp | string): void;
|
|
19
|
+
toMatchObject(expected: Partial<T>): void;
|
|
20
|
+
toBeNull(): void;
|
|
21
|
+
toThrow(message?: string): void;
|
|
22
|
+
}
|
|
23
|
+
export declare function expect<T>(t: T): Matcher<T>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { assert } from "../assert.js";
|
|
2
|
+
import { equals } from "../equal.js";
|
|
3
|
+
export class Matcher {
|
|
4
|
+
actual;
|
|
5
|
+
constructor(actual) {
|
|
6
|
+
this.actual = actual;
|
|
7
|
+
}
|
|
8
|
+
get not() {
|
|
9
|
+
return new NotMatcher(this.actual);
|
|
10
|
+
}
|
|
11
|
+
toBe(expected) {
|
|
12
|
+
assert(this.actual === expected, () => `${this.actual} !== ${expected}`);
|
|
13
|
+
}
|
|
14
|
+
toEqual(expected) {
|
|
15
|
+
assert(equals(this.actual, expected), () => `Objects are not equivalent: ${JSON.stringify(this.actual)}, ${JSON.stringify(expected)}`);
|
|
16
|
+
}
|
|
17
|
+
toMatch(expected) {
|
|
18
|
+
assert(typeof this.actual === "string", () => "Must have string for regexp match");
|
|
19
|
+
// @ts-expect-error
|
|
20
|
+
const actual = this.actual;
|
|
21
|
+
if (typeof expected === "string") {
|
|
22
|
+
assert(actual.includes(expected), () => `${actual} does not include ${expected}`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
assert(expected.test(actual), () => `${actual} does not match ${expected}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
toMatchObject(expected) {
|
|
29
|
+
for (const [k, v] of Object.entries(expected)) {
|
|
30
|
+
// @ts-expect-error
|
|
31
|
+
const actual = this.actual[k];
|
|
32
|
+
assert(equals(actual, v), () => `Comparing ${k}, properties not equal: ${JSON.stringify(actual)}, ${JSON.stringify(v)}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
toBeNull() {
|
|
36
|
+
assert(this.actual === null, () => `Expected null, got ${JSON.stringify(this.actual)}`);
|
|
37
|
+
}
|
|
38
|
+
toThrow(message = "") {
|
|
39
|
+
let didThrow = false;
|
|
40
|
+
let result = undefined;
|
|
41
|
+
try {
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
result = this.actual();
|
|
44
|
+
}
|
|
45
|
+
catch ({ message: e }) {
|
|
46
|
+
assert(
|
|
47
|
+
// @ts-expect-error
|
|
48
|
+
(e ?? "").match(message), () => `Expected thrown message to match ${message}, got ${e}`);
|
|
49
|
+
didThrow = true;
|
|
50
|
+
}
|
|
51
|
+
assert(didThrow, () => `Expected throw but got ${JSON.stringify(result)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export class NotMatcher {
|
|
55
|
+
actual;
|
|
56
|
+
constructor(actual) {
|
|
57
|
+
this.actual = actual;
|
|
58
|
+
}
|
|
59
|
+
get not() {
|
|
60
|
+
return new Matcher(this.actual);
|
|
61
|
+
}
|
|
62
|
+
toBe(expected) {
|
|
63
|
+
assert(this.actual !== expected, () => `${this.actual} === ${expected}`);
|
|
64
|
+
}
|
|
65
|
+
toEqual(expected) {
|
|
66
|
+
assert(!equals(this.actual, expected), () => `Objects are equivalent: ${JSON.stringify(this.actual)}, ${JSON.stringify(expected)}`);
|
|
67
|
+
}
|
|
68
|
+
toMatch(expected) {
|
|
69
|
+
assert(typeof this.actual === "string", () => "Must have string for regexp match");
|
|
70
|
+
// @ts-expect-error
|
|
71
|
+
const actual = this.actual;
|
|
72
|
+
if (typeof expected === "string") {
|
|
73
|
+
assert(!actual.includes(expected), () => `${actual} includes ${expected}`);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
assert(!expected.test(actual), () => `${actual} matches ${expected}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
toMatchObject(expected) {
|
|
80
|
+
for (const [k, v] of Object.entries(expected)) {
|
|
81
|
+
// @ts-expect-error
|
|
82
|
+
const actual = this.actual[k];
|
|
83
|
+
assert(!equals(actual, v), () => `Comparing ${k}, properties equal: ${JSON.stringify(actual)}, ${JSON.stringify(v)}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
toBeNull() {
|
|
87
|
+
assert(this.actual !== null, () => `Expected not null`);
|
|
88
|
+
}
|
|
89
|
+
toThrow(message = "") {
|
|
90
|
+
let didThrow = false;
|
|
91
|
+
let result = undefined;
|
|
92
|
+
try {
|
|
93
|
+
// @ts-expect-error
|
|
94
|
+
result = this.actual();
|
|
95
|
+
}
|
|
96
|
+
catch ({ message: e }) {
|
|
97
|
+
assert(
|
|
98
|
+
// @ts-expect-error
|
|
99
|
+
(e ?? "").match(message), () => `Expected thrown message to match ${message}, got ${e}`);
|
|
100
|
+
didThrow = true;
|
|
101
|
+
}
|
|
102
|
+
assert(!didThrow, () => `Expected throw but got ${JSON.stringify(result)}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export function expect(t) {
|
|
106
|
+
return new Matcher(t);
|
|
107
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Given a value with numbers, attempt to fix all numbers to 1 decimal point.
|
|
3
|
+
*/
|
|
4
|
+
export function fix(n) {
|
|
5
|
+
if (typeof n === "number") {
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
return +n.toFixed(1);
|
|
8
|
+
}
|
|
9
|
+
if (n !== Object(n)) {
|
|
10
|
+
// A primitive
|
|
11
|
+
return n;
|
|
12
|
+
}
|
|
13
|
+
if (n instanceof Array) {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
return n.map(fix);
|
|
16
|
+
}
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
return mapreduce(fix, n);
|
|
19
|
+
}
|
|
20
|
+
function mapreduce(fn, iter) {
|
|
21
|
+
return Object.entries(iter).reduce((acc, [k, v]) => ((acc[k] = fn(v)), acc), {});
|
|
22
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface TestCase {
|
|
2
|
+
[k: string]: Function | TestCase;
|
|
3
|
+
[k: symbol]: Function;
|
|
4
|
+
}
|
|
5
|
+
export interface TestResult {
|
|
6
|
+
executed: number;
|
|
7
|
+
passed: number;
|
|
8
|
+
failed: number;
|
|
9
|
+
[k: string]: TestResult | TestSummary | number;
|
|
10
|
+
}
|
|
11
|
+
export declare type TestSummary = TestFailed | TestPassed;
|
|
12
|
+
export interface TestFailed {
|
|
13
|
+
error: unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface TestPassed {
|
|
16
|
+
passed: true;
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { fileResponse } from "./response.js";
|
|
4
|
+
/**
|
|
5
|
+
* Searches up the request path until the first index is found.
|
|
6
|
+
*/
|
|
7
|
+
export const findIndex = async ({ root }) => async (req) => {
|
|
8
|
+
let filename = path.join(root, req.url ?? "");
|
|
9
|
+
if (path.basename(filename).match(/\.[a-z]{1,3}$/)) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
while (filename.startsWith(root)) {
|
|
13
|
+
const index = path.join(filename, "index.html");
|
|
14
|
+
try {
|
|
15
|
+
const stat = await fs.stat(index);
|
|
16
|
+
return fileResponse(index, stat);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
filename = path.dirname(filename);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
4
|
+
import { IncomingMessage } from "http";
|
|
5
|
+
export interface StaticResponse {
|
|
6
|
+
status: 200 | 404 | 500;
|
|
7
|
+
content: Buffer;
|
|
8
|
+
contentType: string;
|
|
9
|
+
contentLength?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface ServerConfig {
|
|
12
|
+
root: string;
|
|
13
|
+
scopes?: Record<`@${string}`, string>;
|
|
14
|
+
}
|
|
15
|
+
export interface MiddlewareFactory {
|
|
16
|
+
(config: ServerConfig): Promise<StaticMiddleware>;
|
|
17
|
+
}
|
|
18
|
+
export interface StaticMiddleware {
|
|
19
|
+
(req: IncomingMessage): Promise<undefined | (() => Promise<StaticResponse>)>;
|
|
20
|
+
}
|
|
21
|
+
export declare const makeServer: (config: ServerConfig, middlewares?: MiddlewareFactory[]) => Promise<import("http").Server>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createServer, } from "http";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { info } from "../../log.js";
|
|
5
|
+
import { findIndex } from "./apps.js";
|
|
6
|
+
import { fileResponse } from "./response.js";
|
|
7
|
+
import { sitemap } from "./sitemap.js";
|
|
8
|
+
import { staticFileServer } from "./static.js";
|
|
9
|
+
import { tsFileServer } from "./typescript.js";
|
|
10
|
+
const notFound = async ({ root }) => async () => fileResponse(
|
|
11
|
+
// path.join(path.dirname(FLAGS.argv0), "404.html"),
|
|
12
|
+
path.join(root, "404.html"), undefined, 404);
|
|
13
|
+
const BASE_MIDDLEWARES = [
|
|
14
|
+
sitemap,
|
|
15
|
+
tsFileServer,
|
|
16
|
+
staticFileServer,
|
|
17
|
+
findIndex,
|
|
18
|
+
notFound,
|
|
19
|
+
];
|
|
20
|
+
const error = (res, message) => {
|
|
21
|
+
console.error(message);
|
|
22
|
+
res.statusCode = 500;
|
|
23
|
+
res.write(message);
|
|
24
|
+
res.end();
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
27
|
+
const sendContent = async (res, { content, contentType, contentLength }) => {
|
|
28
|
+
res.setHeader("content-length", `${contentLength}`);
|
|
29
|
+
res.setHeader("content-type", contentType);
|
|
30
|
+
await res.write(content);
|
|
31
|
+
res.end();
|
|
32
|
+
return true;
|
|
33
|
+
};
|
|
34
|
+
const log = (req) => {
|
|
35
|
+
const when = new Date().toISOString();
|
|
36
|
+
const who = req.socket.remoteAddress;
|
|
37
|
+
const what = req.url;
|
|
38
|
+
const how = `${req.method} ${what}`;
|
|
39
|
+
info("Request", { when, who, how });
|
|
40
|
+
};
|
|
41
|
+
export const makeServer = async (config, middlewares = []) => {
|
|
42
|
+
const handlers = await Promise.all([...middlewares, ...BASE_MIDDLEWARES].map(async (m) => m(config)));
|
|
43
|
+
const middlewareHandler = async (req, res) => {
|
|
44
|
+
log(req);
|
|
45
|
+
let handler;
|
|
46
|
+
try {
|
|
47
|
+
for (const middleware of handlers) {
|
|
48
|
+
handler = await middleware(req);
|
|
49
|
+
if (handler !== undefined) {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (handler) {
|
|
54
|
+
sendContent(res, await handler());
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
res.end();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
error(res, e.message + "\n" + e.stack);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
// TODO(https)
|
|
65
|
+
const server = createServer(middlewareHandler);
|
|
66
|
+
server.on("listening", () => {
|
|
67
|
+
const { address, port } = server.address();
|
|
68
|
+
info("Server listening", { address: `http://${address}:${port}` });
|
|
69
|
+
});
|
|
70
|
+
return server;
|
|
71
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Stats } from "fs";
|
|
2
|
+
import { StaticResponse } from ".";
|
|
3
|
+
export declare const fileResponse: (filename: string, stat?: Stats, status?: 200 | 404 | 500) => () => Promise<StaticResponse>;
|
|
4
|
+
export declare const contentResponse: (content: string, contentType: string, status?: 200 | 404 | 500) => () => Promise<StaticResponse>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
const MIME_TYPES = {
|
|
3
|
+
js: "text/javascript",
|
|
4
|
+
json: "text/javascript",
|
|
5
|
+
css: "text/css",
|
|
6
|
+
html: "text/html",
|
|
7
|
+
png: "image/png",
|
|
8
|
+
jpg: "image/jpeg",
|
|
9
|
+
jpeg: "image/jpeg",
|
|
10
|
+
svg: "image/svg+xml",
|
|
11
|
+
eot: "application/vnd.ms-fontobject",
|
|
12
|
+
ttf: "application/font-ttf",
|
|
13
|
+
woff: "application/font-woff",
|
|
14
|
+
woff2: "application/font-woff2",
|
|
15
|
+
};
|
|
16
|
+
const mime = (basename) => {
|
|
17
|
+
const extension = basename.substr(basename.lastIndexOf(".") + 1);
|
|
18
|
+
return MIME_TYPES[extension] ?? "application/octet-stream";
|
|
19
|
+
};
|
|
20
|
+
export const fileResponse = (filename, stat, status = 200) => async () => {
|
|
21
|
+
if (!stat) {
|
|
22
|
+
stat = await fs.stat(filename);
|
|
23
|
+
}
|
|
24
|
+
const content = await fs.readFile(filename);
|
|
25
|
+
const contentType = mime(filename);
|
|
26
|
+
const contentLength = stat.size;
|
|
27
|
+
return { status, contentType, contentLength, content };
|
|
28
|
+
};
|
|
29
|
+
export const contentResponse = (content, contentType, status = 200) => async () => {
|
|
30
|
+
const contentBuffer = Buffer.from(content, "utf-8");
|
|
31
|
+
return {
|
|
32
|
+
content: contentBuffer,
|
|
33
|
+
contentType,
|
|
34
|
+
status,
|
|
35
|
+
contentLength: contentBuffer.length,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { info } from "../../log.js";
|
|
4
|
+
import { contentResponse } from "./response.js";
|
|
5
|
+
const findSiteMap = async (root, prefix = root) => {
|
|
6
|
+
if (root.startsWith("node_modules")) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
const children = (await fs.readdir(root, { withFileTypes: true })).map(async (entry) => {
|
|
10
|
+
const next = path
|
|
11
|
+
.join(root, entry.name)
|
|
12
|
+
// Normalize separators for web
|
|
13
|
+
.replaceAll(path.sep, "/");
|
|
14
|
+
if (entry.isFile()) {
|
|
15
|
+
if (entry.name === "index.html") {
|
|
16
|
+
let index = next.replace(prefix, "");
|
|
17
|
+
info(`Adding to sitemap`, { index });
|
|
18
|
+
return [index];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (entry.isDirectory()) {
|
|
22
|
+
if (entry.name.startsWith(".")) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
const flattened = (await Promise.all(await findSiteMap(next, prefix))).flat();
|
|
26
|
+
return flattened;
|
|
27
|
+
}
|
|
28
|
+
return [];
|
|
29
|
+
});
|
|
30
|
+
return children;
|
|
31
|
+
};
|
|
32
|
+
export const sitemap = async ({ root }) => {
|
|
33
|
+
const apps = await (await Promise.all(await findSiteMap(root)))
|
|
34
|
+
.flat()
|
|
35
|
+
.filter((a) => a !== undefined);
|
|
36
|
+
return async (req) => {
|
|
37
|
+
if ((req.url ?? "").endsWith("sitemap.json")) {
|
|
38
|
+
return contentResponse(JSON.stringify(apps), "application/json");
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as path from "path";
|
|
2
|
+
import * as fs from "fs/promises";
|
|
3
|
+
import { fileResponse } from "./response.js";
|
|
4
|
+
export const staticFileServer = async ({ root, scopes = {} }) => async (req) => {
|
|
5
|
+
const scope = Object.entries(scopes).find(([s]) => req.url?.startsWith(`/${s}`));
|
|
6
|
+
const url = new URL(req.url ?? "", `http://localhost`);
|
|
7
|
+
const pathname = scope
|
|
8
|
+
? url.pathname.replace(scope[0], scope[1])
|
|
9
|
+
: url.pathname;
|
|
10
|
+
const filename = path.join(root, pathname);
|
|
11
|
+
// Expand url with found scope
|
|
12
|
+
console.log(scope, req.url, filename);
|
|
13
|
+
try {
|
|
14
|
+
const stat = await fs.stat(filename);
|
|
15
|
+
return stat.isDirectory() ? undefined : fileResponse(filename, stat);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.error(e);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { contentResponse } from "./response.js";
|
|
4
|
+
import { transpile } from "../../transpile.mjs";
|
|
5
|
+
function render(source) {
|
|
6
|
+
// Replace `from "@scope` with `from "/@scope`, for browsers
|
|
7
|
+
source = source
|
|
8
|
+
.replaceAll(`from "@`, 'from "/@')
|
|
9
|
+
.replaceAll(`import("@`, 'import("/@');
|
|
10
|
+
return contentResponse(source, "application/javascript");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Serves .js files statically. Finds .ts files and transpiles them to JS.
|
|
14
|
+
*/
|
|
15
|
+
export const tsFileServer = async ({ root, scopes = {} }) => async (req) => {
|
|
16
|
+
if (req.url?.endsWith(".js")) {
|
|
17
|
+
let scope = Object.entries(scopes).find(([s]) => req.url?.startsWith(`/${s}`));
|
|
18
|
+
// Expand url with found scope
|
|
19
|
+
let url = scope ? req.url.replace(scope[0], scope[1]) : req.url;
|
|
20
|
+
let filename = path.join(root, url);
|
|
21
|
+
try {
|
|
22
|
+
const stat = await fs.stat(filename);
|
|
23
|
+
if (stat.isFile()) {
|
|
24
|
+
const js = (await fs.readFile(filename)).toString("utf-8");
|
|
25
|
+
return render(js);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch { }
|
|
29
|
+
filename = filename.replace(/\.js$/, ".ts");
|
|
30
|
+
try {
|
|
31
|
+
const stat = await fs.stat(filename);
|
|
32
|
+
if (stat.isFile()) {
|
|
33
|
+
const js = await transpile(filename, () => fs.readFile(filename));
|
|
34
|
+
return render(js);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch { }
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node --experimental-loader ../loader.mjs
|
|
2
|
+
import { info } from "../log.js";
|
|
3
|
+
info("Starting server", { cwd: process.cwd() });
|
|
4
|
+
import { parse } from "../flags.js";
|
|
5
|
+
const FLAGS = parse(process.argv);
|
|
6
|
+
import { makeServer } from "./http/index.js";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
const server = await makeServer({ root: path.join(process.cwd(), "src") });
|
|
9
|
+
server.listen(FLAGS.asNumber("port", 8080), FLAGS.asString("host", "0.0.0.0"));
|
package/build/test.d.mts
ADDED