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.
Files changed (56) hide show
  1. package/README.md +11 -5
  2. package/package.json +9 -3
  3. package/source/client/Action.js +20 -22
  4. package/source/client/Client.js +13 -17
  5. package/source/client/Context.js +10 -16
  6. package/source/client/Element.js +21 -15
  7. package/source/client/Session.js +27 -0
  8. package/source/client/View.js +1 -2
  9. package/source/client/document.js +1 -1
  10. package/source/preset/primate.json +2 -6
  11. package/source/server/Action.js +56 -75
  12. package/source/server/App.js +27 -8
  13. package/source/server/Bundler.js +13 -16
  14. package/source/server/Context.js +63 -56
  15. package/source/server/{promises/Eager.js → EagerPromise.js} +4 -2
  16. package/source/server/File.js +6 -2
  17. package/source/server/Projector.js +86 -0
  18. package/source/server/Router.js +15 -24
  19. package/source/server/Session.js +9 -33
  20. package/source/server/attributes.js +3 -0
  21. package/source/server/conf.js +20 -26
  22. package/source/server/{Crypto.js → crypto.js} +0 -0
  23. package/source/server/domain/Domain.js +60 -48
  24. package/source/server/domain/Field.js +34 -36
  25. package/source/server/domain/Predicate.js +24 -0
  26. package/source/server/domain/domains.js +16 -0
  27. package/source/server/errors/InternalServer.js +1 -1
  28. package/source/server/errors/Predicate.js +1 -1
  29. package/source/server/errors.js +0 -1
  30. package/source/server/exports.js +10 -9
  31. package/source/server/{utils/extend_object.js → extend_object.js} +0 -0
  32. package/source/server/invariants.js +23 -10
  33. package/source/server/sanitize.js +10 -0
  34. package/source/server/servers/Dynamic.js +19 -13
  35. package/source/server/servers/Server.js +1 -1
  36. package/source/server/servers/Static.js +25 -20
  37. package/source/server/servers/content-security-policy.json +0 -1
  38. package/source/server/store/Store.js +13 -0
  39. package/source/server/types/Array.js +2 -2
  40. package/source/server/types/Boolean.js +2 -2
  41. package/source/server/types/Date.js +2 -2
  42. package/source/server/types/Domain.js +1 -6
  43. package/source/server/types/Instance.js +1 -1
  44. package/source/server/types/Number.js +2 -2
  45. package/source/server/types/Object.js +2 -2
  46. package/source/server/types/Primitive.js +1 -1
  47. package/source/server/types/Storeable.js +12 -19
  48. package/source/server/types/String.js +2 -2
  49. package/source/server/view/TreeNode.js +2 -0
  50. package/source/server/view/View.js +6 -1
  51. package/source/client/Base.js +0 -5
  52. package/source/server/Base.js +0 -35
  53. package/source/server/errors/Fallback.js +0 -1
  54. package/source/server/fallback.js +0 -11
  55. package/source/server/promises/Meta.js +0 -42
  56. 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 Instance from "./Instance.js";
1
+ import InstanceType from "./Instance.js";
2
2
  import errors from "./errors/Array.json" assert {"type": "json"};
3
3
 
4
- export default class extends Instance {
4
+ export default class ArrayType extends InstanceType {
5
5
  static get instance() {
6
6
  return Array;
7
7
  }
@@ -1,8 +1,8 @@
1
- import Primitive from "./Primitive.js";
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 Primitive {
5
+ export default class BooleanType extends PrimitiveType {
6
6
  static get type() {
7
7
  return "boolean";
8
8
  }
@@ -1,7 +1,7 @@
1
- import Instance from "./Instance.js";
1
+ import InstanceType from "./Instance.js";
2
2
  import errors from "./errors/Date.json" assert {"type": "json"};
3
3
 
4
- export default class extends Instance {
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 Primitive from "./Primitive.js";
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 Primitive {
5
+ export default class NumberType extends PrimitiveType {
6
6
  static get type() {
7
7
  return "number";
8
8
  }
@@ -1,7 +1,7 @@
1
- import Instance from "./Instance.js";
1
+ import InstanceType from "./Instance.js";
2
2
  import errors from "./errors/Object.json" assert {"type": "json"};
3
3
 
4
- export default class extends Instance {
4
+ export default class ObjectType extends InstanceType {
5
5
  static get instance() {
6
6
  return Object;
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import Storeable from "./Storeable.js";
2
2
 
3
- export default class extends Storeable {
3
+ export default class PrimitiveType extends Storeable {
4
4
  static is(value) {
5
5
  return typeof value === this.type;
6
6
  }
@@ -1,13 +1,13 @@
1
1
  import {PredicateError} from "../errors.js";
2
2
 
3
- export default class {
4
- static async verify(property, value, predicates, type) {
5
- const coerced = this.coerce(value);
6
- if (!await this.is(coerced, type)) {
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 this.has(property, coerced, predicates);
10
- return coerced;
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(property, value, predicates) {
22
- for (const predicate of predicates) {
23
- if (typeof predicate === "object") {
24
- await predicate.function(property, ...predicate.params);
25
- } else {
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 Primitive from "./Primitive.js";
1
+ import PrimitiveType from "./Primitive.js";
2
2
  import errors from "./errors/String.json" assert {"type": "json"};
3
3
 
4
- export default class extends Primitive {
4
+ export default class StringType extends PrimitiveType {
5
5
  static get type() {
6
6
  return "string";
7
7
  }
@@ -147,7 +147,9 @@ export default class TreeNode {
147
147
  put(found_node, right);
148
148
  }
149
149
  };
150
+ //const flattened = [...new Set(flatten)];
150
151
  flatten.forEach(part => put(tree, part));
152
+ //return {tree, flattened};
151
153
  return tree;
152
154
  }
153
155
 
@@ -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
  }
@@ -1,5 +0,0 @@
1
- export default class Base {
2
- Class() {
3
- return this.constructor;
4
- }
5
- }
@@ -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
- }
@@ -1,2 +0,0 @@
1
- export {default as EagerPromise, eager} from "./promises/Eager.js";
2
- export {default as MetaPromise} from "./promises/Meta.js";