primate 0.15.4 → 0.15.6

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/package.json CHANGED
@@ -1,10 +1,15 @@
1
1
  {
2
2
  "name": "primate",
3
- "version": "0.15.4",
3
+ "version": "0.15.6",
4
4
  "description": "Expressive, minimal and extensible framework for JavaScript",
5
5
  "homepage": "https://primatejs.com",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
7
7
  "license": "MIT",
8
+ "files": [
9
+ "src/**",
10
+ "!src/**/*.spec.js",
11
+ "!readme/**"
12
+ ],
8
13
  "bin": "src/bin.js",
9
14
  "repository": "https://github.com/primatejs/primate",
10
15
  "scripts": {
@@ -18,9 +23,6 @@
18
23
  "devDependencies": {
19
24
  "maximin": "^0.1.2"
20
25
  },
21
- "engines": {
22
- "node": ">=17.9.0"
23
- },
24
26
  "type": "module",
25
27
  "exports": "./src/exports.js"
26
28
  }
package/src/Logger.js ADDED
@@ -0,0 +1,98 @@
1
+ import {assert, is} from "runtime-compat/dyndef";
2
+
3
+ const colors = {
4
+ black: msg => `\x1b[0m${msg}\x1b[0m`,
5
+ bold: msg => `\x1b[1m${msg}\x1b[0m`,
6
+ red: msg => `\x1b[31m${msg}\x1b[0m`,
7
+ green: msg => `\x1b[32m${msg}\x1b[0m`,
8
+ yellow: msg => `\x1b[33m${msg}\x1b[0m`,
9
+ blue: msg => `\x1b[34m${msg}\x1b[0m`,
10
+ gray: msg => `\x1b[2m${msg}\x1b[0m`,
11
+ };
12
+
13
+ const error = 0;
14
+ const warn = 1;
15
+ const info = 2;
16
+
17
+ // Error natively provided
18
+ const Warn = class Warn extends Error {};
19
+ const Info = class Info extends Error {};
20
+
21
+ const levels = new Map([
22
+ [Error, error],
23
+ [Warn, warn],
24
+ [Info, info],
25
+ ]);
26
+
27
+ const print = (...messages) => process.stdout.write(messages.join(" "));
28
+
29
+ const Logger = class Logger {
30
+ #level; #traceStack;
31
+
32
+ constructor({level = Error, traceStack = false} = {}) {
33
+ assert(level !== undefined && levels.get(level) <= info);
34
+ is(traceStack).boolean();
35
+ this.#level = level;
36
+ this.#traceStack = traceStack;
37
+ }
38
+
39
+ static get Error() {
40
+ return Error;
41
+ }
42
+
43
+ static get Warn() {
44
+ return Warn;
45
+ }
46
+
47
+ static get Info() {
48
+ return Info;
49
+ }
50
+
51
+ #print(pre, error) {
52
+ if (error instanceof Error) {
53
+ print(colors.bold(pre), error.message, "\n");
54
+ if (this.#traceStack) {
55
+ console.log(error);
56
+ }
57
+ } else {
58
+ print(colors.bold(pre), error, "\n");
59
+ }
60
+ }
61
+
62
+ get level() {
63
+ return levels.get(this.#level);
64
+ }
65
+
66
+ info(message) {
67
+ if (this.level >= levels.get(Info)) {
68
+ this.#print(colors.green("--"), message);
69
+ }
70
+ }
71
+
72
+ warn(message) {
73
+ if (this.level >= levels.get(Warn)) {
74
+ this.#print(colors.yellow("??"), message);
75
+ }
76
+ }
77
+
78
+ error(message) {
79
+ if (this.level >= levels.get(Error)) {
80
+ this.#print(colors.red("!!"), message);
81
+ }
82
+ }
83
+
84
+ auto(message) {
85
+ if (message instanceof Info) {
86
+ return this.info(message.message);
87
+ }
88
+ if (message instanceof Warn) {
89
+ return this.warn(message.message);
90
+ }
91
+
92
+ return this.error(message);
93
+ }
94
+ };
95
+
96
+ export default Logger;
97
+
98
+ export {colors, levels, print};
@@ -1,7 +1,6 @@
1
1
  import {Path} from "runtime-compat/fs";
2
- import package_json from "../../package.json" assert {type: "json"};
3
2
 
4
- const createModule = async () => {
3
+ const createModule = async env => {
5
4
  const space = 2;
6
5
  try {
7
6
  // will throw if cannot find a package.json up the filesystem hierarchy
@@ -11,7 +10,7 @@ const createModule = async () => {
11
10
  name: "primate-app",
12
11
  private: true,
13
12
  dependencies: {
14
- primate: `^${package_json.version}`,
13
+ primate: `^${env.version}`,
15
14
  },
16
15
  scripts: {
17
16
  start: "npx primate",
@@ -37,7 +36,7 @@ const createConfig = async env => {
37
36
  };
38
37
 
39
38
  export default async env => {
40
- await createModule();
39
+ await createModule(env);
41
40
  await createConfig(env);
42
41
  };
43
42
 
@@ -1,3 +1,3 @@
1
1
  export default env => {
2
- env.log.info("available commands: create start");
2
+ env.log.info("available commands: create dev serve");
3
3
  };
package/src/config.js CHANGED
@@ -4,9 +4,8 @@ import {File, Path} from "runtime-compat/fs";
4
4
  import cache from "./cache.js";
5
5
  import extend from "./extend.js";
6
6
  import defaults from "./primate.config.js";
7
- import * as log from "./log.js";
7
+ import {colors, print, default as Logger} from "./Logger.js";
8
8
  import * as handlers from "./handlers/exports.js";
9
- import package_json from "../package.json" assert {type: "json"};
10
9
 
11
10
  const qualify = (root, paths) =>
12
11
  Object.keys(paths).reduce((sofar, key) => {
@@ -58,13 +57,17 @@ export default async (filename = "primate.config.js") => {
58
57
  const root = await getRoot();
59
58
  const config = await getConfig(root, filename);
60
59
 
60
+ const {name, version} = JSON.parse(await new Path(import.meta.url)
61
+ .directory.directory.join("package.json").file.read());
62
+
61
63
  const env = {
62
64
  ...config,
65
+ name, version,
63
66
  resources: [],
64
67
  entrypoints: [],
65
68
  paths: qualify(root, config.paths),
66
69
  root,
67
- log: {...log, error: error => log.error(error, config)},
70
+ log: new Logger(config.logger),
68
71
  register: (name, handler) => {
69
72
  env.handlers[name] = handler;
70
73
  },
@@ -86,16 +89,17 @@ export default async (filename = "primate.config.js") => {
86
89
  .replace("%body%", () => body)
87
90
  .replace("%head%", () => `${head}${heads}`);
88
91
  },
89
- publish: async ({src, code, type = "", inline = false, main}) => {
92
+ publish: async ({src, code, type = "", inline = false}) => {
90
93
  const integrity = await hash(code);
91
- env.resources.push({src, code, type, inline, integrity, main});
94
+ env.resources.push({src, code, type, inline, integrity});
92
95
  return integrity;
93
96
  },
94
97
  bootstrap: ({type, code}) => {
95
98
  env.entrypoints.push({type, code});
96
99
  },
97
100
  };
98
- env.log.info(`${package_json.name} \x1b[34m${package_json.version}\x1b[0m`);
101
+ print(colors.blue(colors.bold(name)), colors.blue(version), "");
102
+ print(colors.gray(`at http://${config.http.host}:${config.http.port}`), "\n");
99
103
  const {modules} = config;
100
104
  // modules may load other modules
101
105
  const loads = await Promise.all(modules
@@ -0,0 +1 @@
1
+ export default class InfoError extends Error {}
package/src/exports.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import run from "./run.js";
2
2
 
3
3
  export * from "./handlers/exports.js";
4
+ export {default as Logger} from "./Logger.js";
4
5
  export default command => run(command);
@@ -1,6 +1,10 @@
1
+ import {Logger} from "primate";
2
+
1
3
  export default {
2
4
  base: "/",
3
- debug: false,
5
+ logger: {
6
+ level: Logger.Warn,
7
+ },
4
8
  http: {
5
9
  host: "localhost",
6
10
  port: 6161,
package/src/route.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import {Path} from "runtime-compat/fs";
2
- import RouteError from "./errors/Route.js";
2
+ import Logger from "./Logger.js";
3
3
 
4
4
  // insensitive-case equal
5
5
  const ieq = (left, right) => left.toLowerCase() === right.toLowerCase();
@@ -23,7 +23,7 @@ export default async env => {
23
23
  const {pathname, searchParams} = url;
24
24
  const params = Object.fromEntries(searchParams);
25
25
  const verb = find(method, pathname, {handler: () => {
26
- throw new RouteError(`no ${method.toUpperCase()} route to ${pathname}`);
26
+ throw new Logger.Info(`no ${method.toUpperCase()} route to ${pathname}`);
27
27
  }});
28
28
  const path = pathname.split("/").filter(part => part !== "");
29
29
  const named = verb.path?.exec(pathname)?.groups ?? {};
package/src/serve.js CHANGED
@@ -5,6 +5,7 @@ import mimes from "./mimes.js";
5
5
  import {http404} from "./handlers/http.js";
6
6
  import {isResponse} from "./duck.js";
7
7
  import respond from "./respond.js";
8
+ import {colors, print} from "./Logger.js";
8
9
 
9
10
  const regex = /\.([a-z1-9]*)$/u;
10
11
  const mime = filename => mimes[filename.match(regex)[1]] ?? mimes.binary;
@@ -46,7 +47,7 @@ export default env => {
46
47
  input => handler(input, acc));
47
48
  return await respond(await handlers({request, env}))(env, headers);
48
49
  } catch (error) {
49
- env.log.error(error);
50
+ env.log.auto(error);
50
51
  return http404()(env, headers);
51
52
  }
52
53
  };
@@ -89,7 +90,7 @@ export default env => {
89
90
  try {
90
91
  return await _serve(request);
91
92
  } catch (error) {
92
- env.log.error(error);
93
+ env.log.auto(error);
93
94
  return new Response(null, {status: statuses.InternalServerError});
94
95
  }
95
96
  };
@@ -135,6 +136,4 @@ export default env => {
135
136
 
136
137
  return handlers({original: request, pathname: pathname + search, body});
137
138
  }, http);
138
-
139
- env.log.info(`running on ${http.host}:${http.port}`);
140
139
  };
package/src/log.js DELETED
@@ -1,29 +0,0 @@
1
- const colors = {
2
- red: 31,
3
- green: 32,
4
- yellow: 33,
5
- blue: 34,
6
- };
7
- const reset = 0;
8
-
9
- const Log = {
10
- paint: (color, message) => {
11
- process.stdout.write(`\x1b[${color}m${message}\x1b[0m`);
12
- return log;
13
- },
14
- nl: () => log.paint(reset, "\n"),
15
- };
16
-
17
- const log = new Proxy(Log, {
18
- get: (target, property) => target[property] ?? (message =>
19
- log.paint(colors[property] ?? reset, message).paint(reset, " ")),
20
- });
21
-
22
- export const info = (...args) => log.green("[info]").reset(...args).nl();
23
-
24
- export const warn = (...args) => log.yellow("[warn]").reset(...args).nl();
25
-
26
- export const error = (originalError, env) => {
27
- log.red("[error]").reset(originalError.message).nl();
28
- env.debug && console.log(originalError);
29
- };