primate 0.8.0 → 0.9.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 (68) hide show
  1. package/LICENSE +17 -23
  2. package/README.md +202 -210
  3. package/README.template.md +181 -0
  4. package/bin/primate.js +5 -0
  5. package/exports.js +3 -26
  6. package/html.js +13 -0
  7. package/module.json +10 -0
  8. package/package.json +14 -16
  9. package/{source → src}/Bundler.js +2 -2
  10. package/{source → src}/cache.js +0 -0
  11. package/src/conf.js +25 -0
  12. package/{source → src}/errors/InternalServer.js +0 -0
  13. package/{source → src}/errors/Predicate.js +0 -0
  14. package/src/errors/Route.js +1 -0
  15. package/{source → src}/errors.js +0 -0
  16. package/{source/extend_object.js → src/extend.js} +3 -3
  17. package/src/extend.spec.js +111 -0
  18. package/src/handlers/http.js +1 -0
  19. package/src/handlers/http404.js +7 -0
  20. package/src/handlers/json.js +7 -0
  21. package/src/handlers/stream.js +7 -0
  22. package/src/handlers/text.js +14 -0
  23. package/{source → src}/http-codes.json +0 -0
  24. package/src/log.js +22 -0
  25. package/{source → src}/mimes.json +0 -0
  26. package/{source → src}/preset/primate.json +0 -0
  27. package/{source → src}/preset/stores/default.js +0 -0
  28. package/src/route.js +61 -0
  29. package/src/run.js +26 -0
  30. package/src/serve.js +90 -0
  31. package/source/App.js +0 -34
  32. package/source/EagerPromise.js +0 -49
  33. package/source/Router.js +0 -31
  34. package/source/Server.js +0 -93
  35. package/source/Session.js +0 -26
  36. package/source/attributes.js +0 -14
  37. package/source/conf.js +0 -26
  38. package/source/domain/Domain.js +0 -285
  39. package/source/domain/Field.js +0 -113
  40. package/source/domain/Predicate.js +0 -24
  41. package/source/handlers/DOM/Node.js +0 -179
  42. package/source/handlers/DOM/Parser.js +0 -135
  43. package/source/handlers/html.js +0 -29
  44. package/source/handlers/http.js +0 -6
  45. package/source/handlers/json.js +0 -6
  46. package/source/handlers/redirect.js +0 -10
  47. package/source/invariants.js +0 -36
  48. package/source/map_entries.js +0 -6
  49. package/source/sanitize.js +0 -8
  50. package/source/store/Memory.js +0 -60
  51. package/source/store/Store.js +0 -30
  52. package/source/types/Array.js +0 -33
  53. package/source/types/Boolean.js +0 -29
  54. package/source/types/Date.js +0 -20
  55. package/source/types/Domain.js +0 -11
  56. package/source/types/Instance.js +0 -8
  57. package/source/types/Number.js +0 -45
  58. package/source/types/Object.js +0 -12
  59. package/source/types/Primitive.js +0 -7
  60. package/source/types/Storeable.js +0 -44
  61. package/source/types/String.js +0 -49
  62. package/source/types/errors/Array.json +0 -7
  63. package/source/types/errors/Boolean.json +0 -5
  64. package/source/types/errors/Date.json +0 -3
  65. package/source/types/errors/Number.json +0 -9
  66. package/source/types/errors/Object.json +0 -3
  67. package/source/types/errors/String.json +0 -11
  68. package/source/types.js +0 -6
@@ -1,29 +0,0 @@
1
- import PrimitiveType from "./Primitive.js";
2
- import {boolish} from "../attributes.js";
3
- import errors from "./errors/Boolean.json" assert {"type": "json"};
4
-
5
- export default class BooleanType extends PrimitiveType {
6
- static get type() {
7
- return "boolean";
8
- }
9
-
10
- static get instance() {
11
- return Boolean;
12
- }
13
-
14
- static get errors() {
15
- return errors;
16
- }
17
-
18
- static coerce(value) {
19
- return boolish(value) ? value === "true" : value;
20
- }
21
-
22
- static true(value) {
23
- return value === true;
24
- }
25
-
26
- static false(value) {
27
- return value === false;
28
- }
29
- }
@@ -1,20 +0,0 @@
1
- import InstanceType from "./Instance.js";
2
- import errors from "./errors/Date.json" assert {"type": "json"};
3
-
4
- export default class DateType extends InstanceType {
5
- static get instance() {
6
- return Date;
7
- }
8
-
9
- static get errors() {
10
- return errors;
11
- }
12
-
13
- static is(value) {
14
- return value instanceof this.instance;
15
- }
16
-
17
- static deserialize(value) {
18
- return value instanceof this.instance ? value : new this.instance(value);
19
- }
20
- }
@@ -1,11 +0,0 @@
1
- import Storeable from "./Storeable.js";
2
-
3
- export default class DomainType extends Storeable {
4
- static type_error({name}) {
5
- return `Must be a ${name}`;
6
- }
7
-
8
- static async is(_id, DomainClass) {
9
- return await DomainClass.count({_id}) === 1;
10
- }
11
- }
@@ -1,8 +0,0 @@
1
- import Storeable from "./Storeable.js";
2
-
3
- export default class InstanceType extends Storeable {
4
- static is(value) {
5
- // no subclassing allowed, as [] instanceof Object === true et al.
6
- return value?.constructor === this.instance;
7
- }
8
- }
@@ -1,45 +0,0 @@
1
- import PrimitiveType from "./Primitive.js";
2
- import {numeric} from "../attributes.js";
3
- import errors from "./errors/Number.json" assert {"type": "json"};
4
-
5
- export default class NumberType extends PrimitiveType {
6
- static get type() {
7
- return "number";
8
- }
9
-
10
- static get instance() {
11
- return Number;
12
- }
13
-
14
- static get errors() {
15
- return errors;
16
- }
17
-
18
- static coerce(value) {
19
- return numeric(value) ? Number(value) : value;
20
- }
21
-
22
- static integer(value) {
23
- return Number.isInteger(value);
24
- }
25
-
26
- static positive(value) {
27
- return value > 0;
28
- }
29
-
30
- static negative(value) {
31
- return value < 0;
32
- }
33
-
34
- static between(value, min, max) {
35
- return value >= min && value <= max;
36
- }
37
-
38
- static min(value, minimum) {
39
- return value >= Number(minimum);
40
- }
41
-
42
- static max(value, maximum) {
43
- return value >= Number(maximum);
44
- }
45
- }
@@ -1,12 +0,0 @@
1
- import InstanceType from "./Instance.js";
2
- import errors from "./errors/Object.json" assert {"type": "json"};
3
-
4
- export default class ObjectType extends InstanceType {
5
- static get instance() {
6
- return Object;
7
- }
8
-
9
- static get errors() {
10
- return errors;
11
- }
12
- }
@@ -1,7 +0,0 @@
1
- import Storeable from "./Storeable.js";
2
-
3
- export default class PrimitiveType extends Storeable {
4
- static is(value) {
5
- return typeof value === this.type;
6
- }
7
- }
@@ -1,44 +0,0 @@
1
- import {PredicateError} from "../errors.js";
2
-
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
- throw new PredicateError(this.type_error(type));
8
- }
9
- await Promise.all(predicates.map(predicate =>
10
- predicate.check(property, document, this)));
11
- }
12
-
13
- static type_error() {
14
- return this.errors.type;
15
- }
16
-
17
- static is() {
18
- throw new Error("must be implemented");
19
- }
20
-
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]);
26
- }
27
- throw new PredicateError(error);
28
- }
29
- }
30
-
31
- static coerce(value) {
32
- return value;
33
- }
34
-
35
- // noop for builtin types
36
- static serialize(value) {
37
- return value;
38
- }
39
-
40
- // noop for builtin types
41
- static deserialize(value) {
42
- return value;
43
- }
44
- }
@@ -1,49 +0,0 @@
1
- import PrimitiveType from "./Primitive.js";
2
- import errors from "./errors/String.json" assert {"type": "json"};
3
-
4
- export default class StringType extends PrimitiveType {
5
- static get type() {
6
- return "string";
7
- }
8
-
9
- static get instance() {
10
- return String;
11
- }
12
-
13
- static get errors() {
14
- return errors;
15
- }
16
-
17
- static length(value, length) {
18
- return value.length === Number(length);
19
- }
20
-
21
- static min(value, minimum) {
22
- return value.length >= Number(minimum);
23
- }
24
-
25
- static max(value, maximum) {
26
- return value.length <= Number(maximum);
27
- }
28
-
29
- static between(value, minimum, maximum) {
30
- const length = value.length;
31
- return length >= Number(minimum) && length <= Number(maximum);
32
- }
33
-
34
- static lowercase(value) {
35
- return value === value.toLowerCase();
36
- }
37
-
38
- static uppercase(value) {
39
- return value === value.toUpperCase();
40
- }
41
-
42
- static alphanumeric(value) {
43
- return /^[a-z0-9]+$/iu.test(value);
44
- }
45
-
46
- static regex(value, pattern) {
47
- return new RegExp(pattern, "u").test(value);
48
- }
49
- }
@@ -1,7 +0,0 @@
1
- {
2
- "type": "Must be an array",
3
- "length": "Must be $1 items in length",
4
- "min": "Must be at least $1 items in length",
5
- "max": "Must be at most $1 items in length",
6
- "between": "Must be between $1 and $2 items in length"
7
- }
@@ -1,5 +0,0 @@
1
- {
2
- "type": "Must be a boolean",
3
- "true": "Must be true",
4
- "false": "Must be false"
5
- }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "Must be a date"
3
- }
@@ -1,9 +0,0 @@
1
- {
2
- "type": "Must be a number",
3
- "integer": "Must be an integer",
4
- "positive": "Must be a positive number",
5
- "negative": "Must be a negative number",
6
- "between": "Must be between $1 and $2",
7
- "min": "Must be at least $1",
8
- "max": "Must be at most $1"
9
- }
@@ -1,3 +0,0 @@
1
- {
2
- "type": "Must be an object"
3
- }
@@ -1,11 +0,0 @@
1
- {
2
- "type": "Must be a string",
3
- "length": "Must be $1 characters in length",
4
- "min": "Must be at least $1 characters in length",
5
- "max": "Must be at most $1 characters in length",
6
- "between": "Must be between $1 and $2 characters in length",
7
- "lowercase": "Must be lowercase",
8
- "uppercase": "Must be uppercase",
9
- "alphanumeric": "Must be alphanumeric",
10
- "regex": "Must adhere to the regular expression pattern '$1'"
11
- }
package/source/types.js DELETED
@@ -1,6 +0,0 @@
1
- export {default as ArrayType} from "./types/Array.js";
2
- export {default as BooleanType} from "./types/Boolean.js";
3
- export {default as DateType} from "./types/Date.js";
4
- export {default as NumberType} from "./types/Number.js";
5
- export {default as ObjectType} from "./types/Object.js";
6
- export {default as StringType} from "./types/String.js";