primate 0.2.0 → 0.5.0
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 +11 -5
- package/package.json +9 -3
- package/source/client/Action.js +20 -22
- package/source/client/Client.js +13 -17
- package/source/client/Context.js +10 -16
- package/source/client/Element.js +21 -15
- package/source/client/Session.js +27 -0
- package/source/client/View.js +1 -2
- package/source/client/document.js +1 -1
- package/source/preset/primate.json +2 -6
- package/source/server/Action.js +56 -75
- package/source/server/App.js +27 -8
- package/source/server/Bundler.js +13 -16
- package/source/server/Context.js +63 -56
- package/source/server/{promises/Eager.js → EagerPromise.js} +4 -2
- package/source/server/File.js +6 -2
- package/source/server/Projector.js +86 -0
- package/source/server/Router.js +15 -24
- package/source/server/Session.js +9 -33
- package/source/server/attributes.js +3 -0
- package/source/server/conf.js +20 -26
- package/source/server/{Crypto.js → crypto.js} +0 -0
- package/source/server/domain/Domain.js +60 -48
- package/source/server/domain/Field.js +34 -36
- package/source/server/domain/Predicate.js +24 -0
- package/source/server/domain/domains.js +16 -0
- package/source/server/errors/InternalServer.js +1 -1
- package/source/server/errors/Predicate.js +1 -1
- package/source/server/errors.js +0 -1
- package/source/server/exports.js +10 -9
- package/source/server/{utils/extend_object.js → extend_object.js} +0 -0
- package/source/server/invariants.js +23 -10
- package/source/server/sanitize.js +10 -0
- package/source/server/servers/Dynamic.js +19 -13
- package/source/server/servers/Server.js +1 -1
- package/source/server/servers/Static.js +25 -20
- package/source/server/servers/content-security-policy.json +0 -1
- package/source/server/store/Store.js +13 -0
- package/source/server/types/Array.js +2 -2
- package/source/server/types/Boolean.js +2 -2
- package/source/server/types/Date.js +2 -2
- package/source/server/types/Domain.js +1 -6
- package/source/server/types/Instance.js +1 -1
- package/source/server/types/Number.js +2 -2
- package/source/server/types/Object.js +2 -2
- package/source/server/types/Primitive.js +1 -1
- package/source/server/types/Storeable.js +12 -19
- package/source/server/types/String.js +2 -2
- package/source/server/view/TreeNode.js +2 -0
- package/source/server/view/View.js +6 -1
- package/source/client/Base.js +0 -5
- package/source/server/Base.js +0 -35
- package/source/server/errors/Fallback.js +0 -1
- package/source/server/fallback.js +0 -11
- package/source/server/promises/Meta.js +0 -42
- package/source/server/promises.js +0 -2
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {resolve} from "path";
|
|
2
|
+
const preset = "../../preset/data/stores";
|
|
3
|
+
|
|
1
4
|
export default class Store {
|
|
2
5
|
constructor(conf = {}) {
|
|
3
6
|
this.conf = conf;
|
|
@@ -14,4 +17,14 @@ export default class Store {
|
|
|
14
17
|
open() {
|
|
15
18
|
return this;
|
|
16
19
|
}
|
|
20
|
+
|
|
21
|
+
static async get(directory, file) {
|
|
22
|
+
let store;
|
|
23
|
+
try {
|
|
24
|
+
store = await import(resolve(`${directory}/${file}`));
|
|
25
|
+
} catch(error) {
|
|
26
|
+
store = await import(`${preset}/${file}`);
|
|
27
|
+
}
|
|
28
|
+
return store.default.open();
|
|
29
|
+
}
|
|
17
30
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import InstanceType from "./Instance.js";
|
|
2
2
|
import errors from "./errors/Array.json" assert {"type": "json"};
|
|
3
3
|
|
|
4
|
-
export default class extends
|
|
4
|
+
export default class ArrayType extends InstanceType {
|
|
5
5
|
static get instance() {
|
|
6
6
|
return Array;
|
|
7
7
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import PrimitiveType from "./Primitive.js";
|
|
2
2
|
import {boolish} from "../attributes.js";
|
|
3
3
|
import errors from "./errors/Boolean.json" assert {"type": "json"};
|
|
4
4
|
|
|
5
|
-
export default class extends
|
|
5
|
+
export default class BooleanType extends PrimitiveType {
|
|
6
6
|
static get type() {
|
|
7
7
|
return "boolean";
|
|
8
8
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import InstanceType from "./Instance.js";
|
|
2
2
|
import errors from "./errors/Date.json" assert {"type": "json"};
|
|
3
3
|
|
|
4
|
-
export default class extends
|
|
4
|
+
export default class DateType extends InstanceType {
|
|
5
5
|
static get instance() {
|
|
6
6
|
return Date;
|
|
7
7
|
}
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import Storeable from "./Storeable.js";
|
|
2
|
-
import Domain from "../domain/Domain.js";
|
|
3
|
-
|
|
4
|
-
export default class extends Storeable {
|
|
5
|
-
static get instance() {
|
|
6
|
-
return Domain;
|
|
7
|
-
}
|
|
8
2
|
|
|
3
|
+
export default class DomainType extends Storeable {
|
|
9
4
|
static type_error({name}) {
|
|
10
5
|
return `Must be a ${name}`;
|
|
11
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Storeable from "./Storeable.js";
|
|
2
2
|
|
|
3
|
-
export default class extends Storeable {
|
|
3
|
+
export default class InstanceType extends Storeable {
|
|
4
4
|
static is(value) {
|
|
5
5
|
// no subclassing allowed, as [] instanceof Object === true et al.
|
|
6
6
|
return value?.constructor === this.instance;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import PrimitiveType from "./Primitive.js";
|
|
2
2
|
import {numeric} from "../attributes.js";
|
|
3
3
|
import errors from "./errors/Number.json" assert {"type": "json"};
|
|
4
4
|
|
|
5
|
-
export default class extends
|
|
5
|
+
export default class NumberType extends PrimitiveType {
|
|
6
6
|
static get type() {
|
|
7
7
|
return "number";
|
|
8
8
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import InstanceType from "./Instance.js";
|
|
2
2
|
import errors from "./errors/Object.json" assert {"type": "json"};
|
|
3
3
|
|
|
4
|
-
export default class extends
|
|
4
|
+
export default class ObjectType extends InstanceType {
|
|
5
5
|
static get instance() {
|
|
6
6
|
return Object;
|
|
7
7
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {PredicateError} from "../errors.js";
|
|
2
2
|
|
|
3
|
-
export default class {
|
|
4
|
-
static async verify(property,
|
|
5
|
-
|
|
6
|
-
if (!await this.is(
|
|
3
|
+
export default class Storeable {
|
|
4
|
+
static async verify(property, document, predicates, type) {
|
|
5
|
+
document[property] = this.coerce(document[property]);
|
|
6
|
+
if (!await this.is(document[property], type)) {
|
|
7
7
|
throw new PredicateError(this.type_error(type));
|
|
8
8
|
}
|
|
9
|
-
await
|
|
10
|
-
|
|
9
|
+
await Promise.all(predicates.map(predicate =>
|
|
10
|
+
predicate.check(property, document, this)));
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
static type_error() {
|
|
@@ -18,20 +18,13 @@ export default class {
|
|
|
18
18
|
throw new Error("must be implemented");
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
static async has(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const [name, ...params] = predicate.split(":");
|
|
27
|
-
if (!this[name](value, ...params)) {
|
|
28
|
-
let error = this.errors[name];
|
|
29
|
-
for (let i = 0; i < params.length; i++) {
|
|
30
|
-
error = error.replace(`$${i+1}`, params[i]);
|
|
31
|
-
}
|
|
32
|
-
throw new PredicateError(error);
|
|
33
|
-
}
|
|
21
|
+
static async has(name, value, params) {
|
|
22
|
+
if (!this[name](value, ...params)) {
|
|
23
|
+
let error = this.errors[name];
|
|
24
|
+
for (let i = 0; i < params.length; i++) {
|
|
25
|
+
error = error.replace(`$${i+1}`, () => params[i]);
|
|
34
26
|
}
|
|
27
|
+
throw new PredicateError(error);
|
|
35
28
|
}
|
|
36
29
|
}
|
|
37
30
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import PrimitiveType from "./Primitive.js";
|
|
2
2
|
import errors from "./errors/String.json" assert {"type": "json"};
|
|
3
3
|
|
|
4
|
-
export default class extends
|
|
4
|
+
export default class StringType extends PrimitiveType {
|
|
5
5
|
static get type() {
|
|
6
6
|
return "string";
|
|
7
7
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Parser from "./Parser.js";
|
|
2
|
+
import File from "../File.js";
|
|
2
3
|
import {InternalServerError} from "../errors.js";
|
|
3
4
|
|
|
4
5
|
const $content = "${content}";
|
|
@@ -10,6 +11,10 @@ export default class View {
|
|
|
10
11
|
this.layouts = layouts;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
static async new(path, layouts) {
|
|
15
|
+
return new View(path, await File.read(`${path}.html`), layouts);
|
|
16
|
+
}
|
|
17
|
+
|
|
13
18
|
elements(layout) {
|
|
14
19
|
try {
|
|
15
20
|
return Parser.parse(layout === undefined
|
|
@@ -25,6 +30,6 @@ export default class View {
|
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
file(layout = "default") {
|
|
28
|
-
return this.layout(layout).replace($content, this.content);
|
|
33
|
+
return this.layout(layout).replace($content, () => this.content);
|
|
29
34
|
}
|
|
30
35
|
}
|
package/source/client/Base.js
DELETED
package/source/server/Base.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import conf from "./conf.js";
|
|
2
|
-
import log from "./log.js";
|
|
3
|
-
import package_json from "../../package.json" assert {"type": "json" };
|
|
4
|
-
|
|
5
|
-
const _conf = conf();
|
|
6
|
-
let routes;
|
|
7
|
-
try {
|
|
8
|
-
routes = await import(`${_conf.root}/routes.json`, {
|
|
9
|
-
"assert": {"type": "json"}
|
|
10
|
-
});
|
|
11
|
-
} catch (error) {
|
|
12
|
-
// local routes.json not required
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default class Base {
|
|
16
|
-
static get conf() {
|
|
17
|
-
return _conf;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
get conf() {
|
|
21
|
-
return this.Class().conf;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
get package() {
|
|
25
|
-
return package_json;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get routes() {
|
|
29
|
-
return routes?.default ?? [];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
Class() {
|
|
33
|
-
return this.constructor;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default class extends Error {}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const fallback = async (from, to, logic_from, logic_to) => {
|
|
2
|
-
try {
|
|
3
|
-
return await logic_from(from);
|
|
4
|
-
} catch (error) {
|
|
5
|
-
return logic_to(to);
|
|
6
|
-
}
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export default (from, to, logic) => logic !== undefined
|
|
10
|
-
? fallback(from, to, logic, logic)
|
|
11
|
-
: fallback(undefined, undefined, from, to);
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import Domain from "../domain/Domain.js";
|
|
2
|
-
|
|
3
|
-
const identity = value => value;
|
|
4
|
-
const by = {
|
|
5
|
-
"undefined": () => undefined,
|
|
6
|
-
"boolean": identity,
|
|
7
|
-
"string": identity,
|
|
8
|
-
"number": identity,
|
|
9
|
-
"array": identity,
|
|
10
|
-
"true_object": value => value instanceof Domain ? {} : value,
|
|
11
|
-
"object": value => by[Array.isArray(value) ? "array" : "true_object"](value),
|
|
12
|
-
};
|
|
13
|
-
const resolve_type = async (document, property) => {
|
|
14
|
-
if (document === undefined) {
|
|
15
|
-
return undefined;
|
|
16
|
-
}
|
|
17
|
-
const resolved = await document[property];
|
|
18
|
-
return by[typeof resolved](resolved);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export default async function resolve(document, projection = []) {
|
|
22
|
-
const resolved = {};
|
|
23
|
-
if (Array.isArray(document)) {
|
|
24
|
-
return Promise.all(document.map(sub => resolve(sub, projection)));
|
|
25
|
-
}
|
|
26
|
-
for (const property of projection) {
|
|
27
|
-
if (typeof property === "string") {
|
|
28
|
-
const result = await resolve_type(document, property);
|
|
29
|
-
if (result !== undefined) {
|
|
30
|
-
resolved[property] = result;
|
|
31
|
-
}
|
|
32
|
-
} else if (typeof property === "object") {
|
|
33
|
-
for (const property2 in property) {
|
|
34
|
-
const value = await document[property2];
|
|
35
|
-
if (value !== undefined) {
|
|
36
|
-
resolved[property2] = await resolve(value, property[property2]);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return resolved;
|
|
42
|
-
}
|