primate 0.24.0 → 0.25.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "primate",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "description": "Expressive, minimal and extensible web framework",
5
5
  "homepage": "https://primatejs.com",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
@@ -18,7 +18,7 @@
18
18
  "directory": "packages/primate"
19
19
  },
20
20
  "dependencies": {
21
- "runtime-compat": "^0.29.0"
21
+ "runtime-compat": "^0.32.3"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=18"
package/src/Logger.js CHANGED
@@ -1,7 +1,8 @@
1
- import {assert, is} from "runtime-compat/invariant";
2
- import {blue, bold, green, red, yellow, dim} from "runtime-compat/colors";
3
- import {map} from "runtime-compat/object";
1
+ import { assert, is } from "runtime-compat/invariant";
2
+ import { blue, bold, green, red, yellow, dim } from "runtime-compat/colors";
3
+ import { map } from "runtime-compat/object";
4
4
  import console from "runtime-compat/console";
5
+ import { stdout } from "runtime-compat/stdio";
5
6
 
6
7
  const levels = {
7
8
  Error: 0,
@@ -9,20 +10,23 @@ const levels = {
9
10
  Info: 2,
10
11
  };
11
12
 
12
- const print = (...messages) => process.stdout.write(messages.join(" "));
13
+ const print = (...messages) => stdout.write(messages.join(" "));
13
14
  const bye = _ => print(dim(yellow("~~ bye\n")));
14
15
  const mark = (format, ...params) => params.reduce((formatted, param, i) =>
15
16
  formatted.replace(`{${i}}`, bold(param)), format);
16
17
 
17
- const reference = "https://primatejs.com/reference/errors";
18
+ const reference = (module, error) => {
19
+ const base = module ? `modules/${module}` : "guide/logging";
20
+ return `https://primatejs.com/${base}#${hyphenate(error)}`;
21
+ };
18
22
 
19
- const hyphenate = classCased => classCased
23
+ const hyphenate = class_cased => class_cased
20
24
  .split("")
21
25
  .map(letter => letter.replace(/[A-Z]/u, upper => `-${upper.toLowerCase()}`))
22
26
  .join("")
23
27
  .slice(1);
24
28
 
25
- const throwable = ({message, level, fix}, name, module) => ({
29
+ const throwable = ({ message, level, fix }, name, module) => ({
26
30
  new(...args) {
27
31
  const error = new Error(mark(message, ...args));
28
32
  error.level = Logger[level];
@@ -35,9 +39,9 @@ const throwable = ({message, level, fix}, name, module) => ({
35
39
  throw this.new(...args);
36
40
  },
37
41
  warn(logger, ...args) {
38
- const error = {level: Logger[level], message: mark(message, ...args),
39
- fix: mark(fix, ...args)};
40
- logger.auto({...error, name, module});
42
+ const error = { level: Logger[level], message: mark(message, ...args),
43
+ fix: mark(fix, ...args) };
44
+ logger.auto({ ...error, name, module });
41
45
  },
42
46
  });
43
47
 
@@ -48,7 +52,7 @@ const Logger = class Logger {
48
52
  return map(errors, ([key, value]) => [key, throwable(value, key, module)]);
49
53
  }
50
54
 
51
- constructor({level = levels.Error, trace = false} = {}) {
55
+ constructor({ level = levels.Error, trace = false } = {}) {
52
56
  assert(level !== undefined && level <= levels.Info);
53
57
  is(trace).boolean();
54
58
  this.#level = level;
@@ -68,11 +72,11 @@ const Logger = class Logger {
68
72
  }
69
73
 
70
74
  #print(pre, color, message, error = {}) {
71
- const {fix, module, name, level} = error;
75
+ const { fix, module, name, level } = error;
72
76
  print(color(pre), `${module !== undefined ? `${color(module)} ` : ""}${message}`, "\n");
73
77
  if (fix) {
74
78
  print(blue("++"), fix);
75
- name && print(dim(`\n -> ${reference}/${module ?? "primate"}#${hyphenate(name)}`), "\n");
79
+ name && print(dim(`\n -> ${reference(module, name)}`), "\n");
76
80
  }
77
81
  if (level === levels.Error || level === undefined && error.message) {
78
82
  this.#trace && console.log(error);
@@ -96,7 +100,7 @@ const Logger = class Logger {
96
100
  }
97
101
 
98
102
  auto(error) {
99
- const {message} = error;
103
+ const { message } = error;
100
104
  const matches = map(levels, ([name, level]) => [level, name.toLowerCase()]);
101
105
  return this[matches[error.level] ?? "error"](message, error);
102
106
  }
@@ -104,4 +108,4 @@ const Logger = class Logger {
104
108
 
105
109
  export default Logger;
106
110
 
107
- export {print, bye, mark};
111
+ export { print, bye, mark };
package/src/app.js CHANGED
@@ -1,20 +1,20 @@
1
1
  import crypto from "runtime-compat/crypto";
2
- import {tryreturn} from "runtime-compat/async";
3
- import {File, Path} from "runtime-compat/fs";
4
- import {bold, blue} from "runtime-compat/colors";
5
- import {is} from "runtime-compat/invariant";
6
- import {transform, valmap} from "runtime-compat/object";
7
- import {globify} from "runtime-compat/string";
2
+ import { tryreturn } from "runtime-compat/async";
3
+ import { File, Path } from "runtime-compat/fs";
4
+ import { bold, blue } from "runtime-compat/colors";
5
+ import { is } from "runtime-compat/invariant";
6
+ import { transform, valmap } from "runtime-compat/object";
7
+ import { globify } from "runtime-compat/string";
8
8
  import * as runtime from "runtime-compat/meta";
9
9
 
10
10
  import errors from "./errors.js";
11
- import {print} from "./Logger.js";
11
+ import { print } from "./Logger.js";
12
12
  import dispatch from "./dispatch.js";
13
13
  import to_sorted from "./to_sorted.js";
14
14
  import * as handlers from "./handlers/exports.js";
15
15
  import * as loaders from "./loaders/exports.js";
16
16
 
17
- const {DoubleFileExtension} = errors;
17
+ const { DoubleFileExtension } = errors;
18
18
 
19
19
  // use user-provided file or fall back to default
20
20
  const index = (base, page, fallback) =>
@@ -27,30 +27,30 @@ const attribute = attributes => Object.keys(attributes).length > 0
27
27
  ? " ".concat(Object.entries(attributes)
28
28
  .map(([key, value]) => `${key}="${value}"`).join(" "))
29
29
  : "";
30
- const tag = ({name, attributes = {}, code = "", close = true}) =>
30
+ const tag = ({ name, attributes = {}, code = "", close = true }) =>
31
31
  `<${name}${attribute(attributes)}${close ? `>${code}</${name}>` : "/>"}`;
32
32
  const tags = {
33
33
  // inline: <script type integrity>...</script>
34
34
  // outline: <script type integrity src></script>
35
- script({inline, code, type, integrity, src}) {
35
+ script({ inline, code, type, integrity, src }) {
36
36
  return inline
37
- ? tag({name: "script", attributes: {type, integrity}, code})
38
- : tag({name: "script", attributes: {type, integrity, src}});
37
+ ? tag({ name: "script", attributes: { type, integrity }, code })
38
+ : tag({ name: "script", attributes: { type, integrity, src } });
39
39
  },
40
40
  // inline: <style>...</style>
41
41
  // outline: <link rel="stylesheet" href/>
42
- style({inline, code, href, rel = "stylesheet"}) {
42
+ style({ inline, code, href, rel = "stylesheet" }) {
43
43
  return inline
44
- ? tag({name: "style", code})
45
- : tag({name: "link", attributes: {rel, href}, close: false});
44
+ ? tag({ name: "style", code })
45
+ : tag({ name: "link", attributes: { rel, href }, close: false });
46
46
  },
47
47
  };
48
48
 
49
- const {name, version} = await new Path(import.meta.url).up(2)
49
+ const { name, version } = await new Path(import.meta.url).up(2)
50
50
  .join(runtime.manifest).json();
51
51
 
52
52
  export default async (log, root, config) => {
53
- const {http} = config;
53
+ const { http } = config;
54
54
  const secure = http?.ssl !== undefined;
55
55
  const path = valmap(config.location, value => root.join(value));
56
56
 
@@ -81,18 +81,18 @@ export default async (log, root, config) => {
81
81
  error: {
82
82
  default: await error.exists ? (await import(error)).default : undefined,
83
83
  },
84
- handlers: {...handlers},
84
+ handlers: { ...handlers },
85
85
  types,
86
86
  routes,
87
87
  layout: {
88
- depth: Math.max(...routes.map(({layouts}) => layouts.length)) + 1,
88
+ depth: Math.max(...routes.map(({ layouts }) => layouts.length)) + 1,
89
89
  },
90
90
  dispatch: dispatch(types),
91
91
  modules: await loaders.modules(log, root, config),
92
92
  ...runtime,
93
93
  // copy files to build folder, potentially transforming them
94
94
  async stage(source, directory, filter) {
95
- const {paths, mapper} = this.config.build.transform;
95
+ const { paths, mapper } = this.config.build.transform;
96
96
  is(paths).array();
97
97
  is(mapper).function();
98
98
 
@@ -112,46 +112,46 @@ export default async (log, root, config) => {
112
112
  }
113
113
  }));
114
114
  },
115
- headers({script = "", style = ""} = {}) {
115
+ headers({ script = "", style = "" } = {}) {
116
116
  const csp = Object.keys(http.csp).reduce((policy, key) =>
117
117
  `${policy}${key} ${http.csp[key]};`, "")
118
118
  .replace("script-src 'self'", `script-src 'self' ${script} ${
119
119
  this.assets
120
- .filter(({type}) => type !== "style")
120
+ .filter(({ type }) => type !== "style")
121
121
  .map(asset => `'${asset.integrity}'`).join(" ")
122
122
  }`)
123
123
  .replace("style-src 'self'", `style-src 'self' ${style} ${
124
124
  this.assets
125
- .filter(({type}) => type === "style")
125
+ .filter(({ type }) => type === "style")
126
126
  .map(asset => `'${asset.integrity}'`).join(" ")
127
127
  }`);
128
128
 
129
- return {"Content-Security-Policy": csp, "Referrer-Policy": "same-origin"};
129
+ return { "Content-Security-Policy": csp, "Referrer-Policy": "same-origin" };
130
130
  },
131
131
  runpath(...directories) {
132
132
  return this.path.build.join(...directories);
133
133
  },
134
- async render({body = "", head = "", page = config.pages.index} = {}) {
135
- const {location: {pages}} = this.config;
134
+ async render({ body = "", head = "", page = config.pages.index } = {}) {
135
+ const { location: { pages } } = this.config;
136
136
 
137
137
  const html = await index(this.runpath(pages), page, config.pages.index);
138
138
 
139
139
  const heads = to_sorted(this.assets,
140
- ({type}) => -1 * (type === "importmap"))
141
- .map(({src, code, type, inline, integrity}) =>
140
+ ({ type }) => -1 * (type === "importmap"))
141
+ .map(({ src, code, type, inline, integrity }) =>
142
142
  type === "style"
143
- ? tags.style({inline, code, href: src})
144
- : tags.script({inline, code, type, integrity, src})
143
+ ? tags.style({ inline, code, href: src })
144
+ : tags.script({ inline, code, type, integrity, src }),
145
145
  ).join("\n").concat("\n", head);
146
146
  return html.replace("%body%", _ => body).replace("%head%", _ => heads);
147
147
  },
148
148
  async inline(code, type) {
149
149
  const integrity = await this.hash(code);
150
150
  const tag_name = type === "style" ? "style" : "script";
151
- const head = tags[tag_name]({code, type, inline: true, integrity});
152
- return {head, csp: `'${integrity}'`};
151
+ const head = tags[tag_name]({ code, type, inline: true, integrity });
152
+ return { head, csp: `'${integrity}'` };
153
153
  },
154
- async publish({src, code, type = "", inline = false, copy = true}) {
154
+ async publish({ src, code, type = "", inline = false, copy = true }) {
155
155
  if (!inline && copy) {
156
156
  const base = this.runpath(this.config.location.client).join(src);
157
157
  await base.directory.file.create();
@@ -167,8 +167,8 @@ export default async (log, root, config) => {
167
167
  });
168
168
  }
169
169
  },
170
- export({type, code}) {
171
- this.exports.push({type, code});
170
+ export({ type, code }) {
171
+ this.exports.push({ type, code });
172
172
  },
173
173
  register(extension, handler) {
174
174
  is(this.handlers[extension]).undefined(DoubleFileExtension.new(extension));
@@ -180,13 +180,13 @@ export default async (log, root, config) => {
180
180
  return `${prefix}-${btoa(String.fromCharCode(...new Uint8Array(bytes)))}`;
181
181
  },
182
182
  async import(module, deep_import) {
183
- const {http: {static: {root}}, location: {client}} = this.config;
183
+ const { http: { static: { root } }, location: { client } } = this.config;
184
184
 
185
185
  const parts = module.split("/");
186
186
  const path = [this.library, ...parts];
187
187
  const pkg = await Path.resolve().join(...path, this.manifest).json();
188
188
  const exports = pkg.exports === undefined
189
- ? {[module]: `/${module}/${pkg.main}`}
189
+ ? { [module]: `/${module}/${pkg.main}` }
190
190
  : transform(pkg.exports, entry => entry
191
191
  .filter(([, export$]) =>
192
192
  export$.browser?.[deep_import] !== undefined
@@ -206,7 +206,7 @@ export default async (log, root, config) => {
206
206
  await dependency.file.copy(to);
207
207
  this.importmaps = {
208
208
  ...valmap(exports, value => new Path(root, this.library, value).path),
209
- ...this.importmaps};
209
+ ...this.importmaps };
210
210
  },
211
211
  };
212
212
  };
@@ -1,4 +1,4 @@
1
- import {default as dev} from "./dev.js";
2
- import {default as serve} from "./serve.js";
1
+ import { default as dev } from "./dev.js";
2
+ import { default as serve } from "./serve.js";
3
3
 
4
- export default name => ({dev, serve})[name] ?? dev;
4
+ export default name => ({ dev, serve })[name] ?? dev;
package/src/cwd.js CHANGED
@@ -1,3 +1,3 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "runtime-compat/fs";
2
2
 
3
3
  export default (meta, up = 1) => new Path(meta.url).up(up);
@@ -1,4 +1,4 @@
1
- import {identity} from "runtime-compat/function";
1
+ import { identity } from "runtime-compat/function";
2
2
  import Logger from "../Logger.js";
3
3
 
4
4
  export default {
package/src/dispatch.js CHANGED
@@ -1,7 +1,7 @@
1
- import {is, maybe} from "runtime-compat/invariant";
2
- import {tryreturn} from "runtime-compat/sync";
3
- import {map} from "runtime-compat/object";
4
- import {camelcased} from "runtime-compat/string";
1
+ import { is, maybe } from "runtime-compat/invariant";
2
+ import { tryreturn } from "runtime-compat/sync";
3
+ import { map } from "runtime-compat/object";
4
+ import { camelcased } from "runtime-compat/string";
5
5
  import errors from "./errors.js";
6
6
  import validate from "./validate.js";
7
7
 
@@ -10,7 +10,7 @@ export default (patches = {}) => (value, raw, cased = true) => {
10
10
  ...map(patches, ([name, patch]) => [`get${camelcased(name)}`, property => {
11
11
  is(property).defined(`\`${name}\` called without property`);
12
12
  return tryreturn(_ => validate(patch, value[property], property))
13
- .orelse(({message}) => errors.MismatchedType.throw(message));
13
+ .orelse(({ message }) => errors.MismatchedType.throw(message));
14
14
  }]),
15
15
  get(property) {
16
16
  maybe(property).string();
package/src/errors.js CHANGED
@@ -1,4 +1,4 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "runtime-compat/fs";
2
2
  import Logger from "./Logger.js";
3
3
 
4
4
  const json = await new Path(import.meta.url).up(1).join("errors.json").json();
package/src/exports.js CHANGED
@@ -2,8 +2,8 @@ import run from "./run.js";
2
2
 
3
3
  export * from "./handlers/exports.js";
4
4
 
5
- export {default as Logger} from "./Logger.js";
5
+ export { default as Logger } from "./Logger.js";
6
6
 
7
- export {URL, Response, Status, MediaType} from "runtime-compat/http";
7
+ export { URL, Response, Status, MediaType } from "runtime-compat/http";
8
8
 
9
9
  export default command => run(command);
@@ -1,9 +1,9 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
2
 
3
- export default (body = "Not Found", {status = Status.NOT_FOUND, page} = {}) =>
3
+ export default (body = "Not Found", { status = Status.NOT_FOUND, page } = {}) =>
4
4
  async app => new Response(await app.render({
5
5
  body,
6
- page: page ?? app.config.pages.error}), {
6
+ page: page ?? app.config.pages.error }), {
7
7
  status,
8
- headers: {...app.headers(), "Content-Type": MediaType.TEXT_HTML},
8
+ headers: { ...app.headers(), "Content-Type": MediaType.TEXT_HTML },
9
9
  });
@@ -1,7 +1,7 @@
1
- export {default as text} from "./text.js";
2
- export {default as json} from "./json.js";
3
- export {default as stream} from "./stream.js";
4
- export {default as redirect} from "./redirect.js";
5
- export {default as html} from "./html.js";
6
- export {default as view} from "./view.js";
7
- export {default as error} from "./error.js";
1
+ export { default as text } from "./text.js";
2
+ export { default as json } from "./json.js";
3
+ export { default as stream } from "./stream.js";
4
+ export { default as redirect } from "./redirect.js";
5
+ export { default as html } from "./html.js";
6
+ export { default as view } from "./view.js";
7
+ export { default as error } from "./error.js";
@@ -1,29 +1,29 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
2
 
3
3
  const script_re = /(?<=<script)>(?<code>.*?)(?=<\/script>)/gus;
4
4
  const style_re = /(?<=<style)>(?<code>.*?)(?=<\/style>)/gus;
5
5
  const remove = /<(?<tag>script|style)>.*?<\/\k<tag>>/gus;
6
6
 
7
7
  export default (name, options = {}) => {
8
- const {status = Status.OK, partial = false} = options;
8
+ const { status = Status.OK, partial = false } = options;
9
9
 
10
10
  return async app => {
11
11
  const html = await app.path.components.join(name).text();
12
12
  const scripts = await Promise.all([...html.matchAll(script_re)]
13
- .map(({groups: {code}}) => app.inline(code, "module")));
13
+ .map(({ groups: { code } }) => app.inline(code, "module")));
14
14
  const styles = await Promise.all([...html.matchAll(style_re)]
15
- .map(({groups: {code}}) => app.inline(code, "style")));
15
+ .map(({ groups: { code } }) => app.inline(code, "style")));
16
16
  const assets = [...scripts, ...styles];
17
17
 
18
18
  const body = html.replaceAll(remove, _ => "");
19
19
  const head = assets.map(asset => asset.head).join("\n");
20
20
  const script = scripts.map(asset => asset.csp).join(" ");
21
21
  const style = styles.map(asset => asset.csp).join(" ");
22
- const headers = {script, style};
22
+ const headers = { script, style };
23
23
 
24
- return new Response(partial ? body : await app.render({body, head}), {
24
+ return new Response(partial ? body : await app.render({ body, head }), {
25
25
  status,
26
- headers: {...app.headers(headers), "Content-Type": MediaType.TEXT_HTML},
26
+ headers: { ...app.headers(headers), "Content-Type": MediaType.TEXT_HTML },
27
27
  });
28
28
  };
29
29
  };
@@ -1,7 +1,7 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
2
 
3
- export default (body, {status = Status.OK} = {}) => app =>
3
+ export default (body, { status = Status.OK } = {}) => app =>
4
4
  new Response(JSON.stringify(body), {
5
5
  status,
6
- headers: {...app.headers(), "Content-Type": MediaType.APPLICATION_JSON},
6
+ headers: { ...app.headers(), "Content-Type": MediaType.APPLICATION_JSON },
7
7
  });
@@ -1,8 +1,8 @@
1
- import {Response, Status} from "runtime-compat/http";
1
+ import { Response, Status } from "runtime-compat/http";
2
2
 
3
- export default (Location, {status = Status.FOUND} = {}) => app =>
3
+ export default (Location, { status = Status.FOUND } = {}) => app =>
4
4
  /* no body */
5
5
  new Response(null, {
6
6
  status,
7
- headers: {...app.headers(), Location},
7
+ headers: { ...app.headers(), Location },
8
8
  });
@@ -1,8 +1,8 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
2
 
3
- export default (body, {status = Status.OK} = {}) => app =>
3
+ export default (body, { status = Status.OK } = {}) => app =>
4
4
  new Response(body, {
5
5
  status,
6
- headers: {...app.headers(), "Content-Type":
7
- MediaType.APPLICATION_OCTET_STREAM},
6
+ headers: { ...app.headers(), "Content-Type":
7
+ MediaType.APPLICATION_OCTET_STREAM },
8
8
  });
@@ -1,7 +1,7 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
2
 
3
- export default (body, {status = Status.OK} = {}) => app =>
3
+ export default (body, { status = Status.OK } = {}) => app =>
4
4
  new Response(body, {
5
5
  status,
6
- headers: {...app.headers(), "Content-Type": MediaType.TEXT_PLAIN},
6
+ headers: { ...app.headers(), "Content-Type": MediaType.TEXT_PLAIN },
7
7
  });
@@ -1,3 +1,3 @@
1
- import {cascade} from "runtime-compat/async";
1
+ import { cascade } from "runtime-compat/async";
2
2
 
3
3
  export default async app => (await cascade(app.modules.bundle))(app);
@@ -1,5 +1,5 @@
1
- import {Path} from "runtime-compat/fs";
2
- import {cascade} from "runtime-compat/async";
1
+ import { Path } from "runtime-compat/fs";
2
+ import { cascade } from "runtime-compat/async";
3
3
  import copy_includes from "./copy_includes.js";
4
4
  import cwd from "../cwd.js";
5
5
 
@@ -7,7 +7,7 @@ const html = /^.*.html$/u;
7
7
  const defaults = cwd(import.meta, 2).join("defaults");
8
8
 
9
9
  const pre = async app => {
10
- const {config: {location}, path} = app;
10
+ const { config: { location }, path } = app;
11
11
 
12
12
  // remove build directory in case exists
13
13
  if (await path.build.exists) {
@@ -1,9 +1,9 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "runtime-compat/fs";
2
2
 
3
3
  export default async (app, type, post = () => undefined) => {
4
- const {config} = app;
5
- const {build} = config;
6
- const {includes} = build;
4
+ const { config } = app;
5
+ const { build } = config;
6
+ const { includes } = build;
7
7
 
8
8
  const reserved = Object.values(app.config.location);
9
9
 
@@ -1,9 +1,9 @@
1
- export {default as init} from "./init.js";
2
- export {default as register} from "./register.js";
3
- export {default as compile} from "./compile.js";
4
- export {default as publish} from "./publish.js";
5
- export {default as bundle} from "./bundle.js";
6
- export {default as route} from "./route.js";
7
- export {default as handle} from "./handle.js";
8
- export {default as parse} from "./parse.js";
9
- export {default as serve} from "./serve.js";
1
+ export { default as init } from "./init.js";
2
+ export { default as register } from "./register.js";
3
+ export { default as compile } from "./compile.js";
4
+ export { default as publish } from "./publish.js";
5
+ export { default as bundle } from "./bundle.js";
6
+ export { default as route } from "./route.js";
7
+ export { default as handle } from "./handle.js";
8
+ export { default as parse } from "./parse.js";
9
+ export { default as serve } from "./serve.js";
@@ -1,52 +1,59 @@
1
- import {Response, Status, MediaType} from "runtime-compat/http";
2
- import {cascade, tryreturn} from "runtime-compat/async";
3
- import {respond} from "./respond/exports.js";
4
- import {invalid} from "./route.js";
5
- import {error as clientError} from "../handlers/exports.js";
1
+ import { Response, Status, MediaType } from "runtime-compat/http";
2
+ import { cascade, tryreturn } from "runtime-compat/async";
3
+ import { respond } from "./respond/exports.js";
4
+ import { invalid } from "./route.js";
5
+ import { error as clientError } from "../handlers/exports.js";
6
6
  import _errors from "../errors.js";
7
- const {NoFileForPath} = _errors;
7
+ const { NoFileForPath } = _errors;
8
8
 
9
- const guardError = Symbol("guardError");
9
+ const guard_error = Symbol("guard_error");
10
+ const guard = (app, guards) => async (request, next) => {
11
+ // handle guards
12
+ try {
13
+ guards.every(guard => {
14
+ const result = guard(request);
15
+ if (result === true) {
16
+ return true;
17
+ }
18
+ const error = new Error();
19
+ error.result = result;
20
+ error.type = guard_error;
21
+ throw error;
22
+ });
23
+
24
+ return next(request);
25
+ } catch (error) {
26
+ if (error.type === guard_error) {
27
+ return (await respond(error.result))(app);
28
+ }
29
+ // rethrow if not guard error
30
+ throw error;
31
+ }
32
+ };
10
33
 
11
34
  export default app => {
12
- const {config: {http: {static: {root}}, location}} = app;
35
+ const { config: { http: { static: { root } }, location } } = app;
13
36
 
14
37
  const as_route = async request => {
15
- const {pathname} = request.url;
38
+ const { pathname } = request.url;
16
39
  // if NoFileForPath is thrown, this will remain undefined
17
40
  let error_handler = app.error.default;
18
41
 
19
42
  return tryreturn(async _ => {
20
- const {path, guards, errors, layouts, handler} = invalid(pathname)
43
+ const { path, guards, errors, layouts, handler } = invalid(pathname)
21
44
  ? NoFileForPath.throw(pathname, location.static)
22
45
  : await app.route(request);
23
46
  error_handler = errors?.at(-1);
24
47
 
25
- // handle guards
26
- try {
27
- guards.every(guard => {
28
- const result = guard(request);
29
- if (result === true) {
30
- return true;
31
- }
32
- const error = new Error();
33
- error.result = result;
34
- error.type = guardError;
35
- throw error;
36
- });
37
- } catch (error) {
38
- if (error.type === guardError) {
39
- return (await respond(error.result))(app);
40
- }
41
- // rethrow if not guard error
42
- throw error;
43
- }
48
+ const pathed = { ...request, path };
49
+
50
+ const hooks = [...app.modules.route, guard(app, guards)];
44
51
 
45
52
  // handle request
46
- const response = (await cascade(app.modules.route, handler))({...request, path});
53
+ const response = (await cascade(hooks, handler))(pathed);
47
54
  return (await respond(await response))(app, {
48
55
  layouts: await Promise.all(layouts.map(layout => layout(request))),
49
- }, request);
56
+ }, pathed);
50
57
  }).orelse(async error => {
51
58
  app.log.auto(error);
52
59
 
@@ -66,7 +73,7 @@ export default app => {
66
73
 
67
74
  const client = app.runpath(location.client);
68
75
  const handle = async request => {
69
- const {pathname} = request.url;
76
+ const { pathname } = request.url;
70
77
  if (pathname.startsWith(root)) {
71
78
  const debased = pathname.replace(root, _ => "");
72
79
  // try static first
package/src/hooks/init.js CHANGED
@@ -1,3 +1,3 @@
1
- import {cascade} from "runtime-compat/async";
1
+ import { cascade } from "runtime-compat/async";
2
2
 
3
3
  export default async app => (await cascade(app.modules.init))(app);
@@ -1,12 +1,12 @@
1
- import {URL, MediaType} from "runtime-compat/http";
2
- import {tryreturn} from "runtime-compat/sync";
3
- import {stringify} from "runtime-compat/streams";
4
- import {from, valmap} from "runtime-compat/object";
1
+ import { URL, MediaType } from "runtime-compat/http";
2
+ import { tryreturn } from "runtime-compat/sync";
3
+ import { stringify } from "runtime-compat/streams";
4
+ import { from, valmap } from "runtime-compat/object";
5
5
  import errors from "../errors.js";
6
6
 
7
- const {APPLICATION_FORM_URLENCODED, APPLICATION_JSON} = MediaType;
7
+ const { APPLICATION_FORM_URLENCODED, APPLICATION_JSON } = MediaType;
8
8
 
9
- const {decodeURIComponent: decode} = globalThis;
9
+ const { decodeURIComponent: decode } = globalThis;
10
10
  const deslash = url => url.replaceAll(/(?<!http:)\/{2,}/gu, _ => "/");
11
11
 
12
12
  const contents = {
@@ -21,12 +21,12 @@ const content = (type, body) =>
21
21
  .orelse(_ => errors.CannotParseBody.throw(body, type));
22
22
 
23
23
  export default dispatch => async original => {
24
- const {headers} = original;
24
+ const { headers } = original;
25
25
  const url = new URL(deslash(decode(original.url)));
26
26
  const body = await stringify(original.body);
27
27
  const cookies = headers.get("cookie");
28
28
 
29
- return {original, url,
29
+ return { original, url,
30
30
  ...valmap({
31
31
  body: [content(headers.get("content-type"), body), body],
32
32
  query: [from(url.searchParams), url.search],
@@ -1,18 +1,18 @@
1
- import {Path} from "runtime-compat/fs";
2
- import {cascade} from "runtime-compat/async";
3
- import {stringify} from "runtime-compat/object";
1
+ import { Path } from "runtime-compat/fs";
2
+ import { cascade } from "runtime-compat/async";
3
+ import { stringify } from "runtime-compat/object";
4
4
  import copy_includes from "./copy_includes.js";
5
5
 
6
6
  const post = async app => {
7
- const {config: {location, http: {static: {root}}}, path} = app;
7
+ const { config: { location, http: { static: { root } } }, path } = app;
8
8
 
9
9
  {
10
10
  // after hook, publish a zero assumptions app.js (no css imports)
11
11
  const src = new Path(root, app.config.build.index);
12
12
 
13
13
  await app.publish({
14
- code: app.exports.filter(({type}) => type === "script")
15
- .map(({code}) => code).join(""),
14
+ code: app.exports.filter(({ type }) => type === "script")
15
+ .map(({ code }) => code).join(""),
16
16
  src,
17
17
  type: "module",
18
18
  });
@@ -24,9 +24,9 @@ const post = async app => {
24
24
  await app.stage(path.components, to, /^.*.js$/u);
25
25
  }
26
26
 
27
- const imports = {...app.importmaps, app: src.path};
27
+ const imports = { ...app.importmaps, app: src.path };
28
28
  const type = "importmap";
29
- await app.publish({inline: true, code: stringify({imports}), type});
29
+ await app.publish({ inline: true, code: stringify({ imports }), type });
30
30
  }
31
31
 
32
32
  if (await path.static.exists) {
@@ -40,9 +40,9 @@ const post = async app => {
40
40
  const src = file.debase(path.static);
41
41
  const type = file.extension === ".css" ? "style" : "module";
42
42
  // already copied in `app.stage`
43
- await app.publish({src, code, type, copy: false});
44
- type === "style" && app.export({type,
45
- code: `import "./${location.static}${src}";`});
43
+ await app.publish({ src, code, type, copy: false });
44
+ type === "style" && app.export({ type,
45
+ code: `import "./${location.static}${src}";` });
46
46
  }));
47
47
  }
48
48
 
@@ -51,8 +51,8 @@ const post = async app => {
51
51
  await copy_includes(app, location.client, async to =>
52
52
  Promise.all((await to.collect(/\.js$/u)).map(async script => {
53
53
  const src = new Path(root, script.path.replace(client, _ => ""));
54
- await app.publish({src, code: await script.text(), type: "module"});
55
- }))
54
+ await app.publish({ src, code: await script.text(), type: "module" });
55
+ })),
56
56
  );
57
57
 
58
58
  return app;
@@ -1,3 +1,3 @@
1
- import {cascade} from "runtime-compat/async";
1
+ import { cascade } from "runtime-compat/async";
2
2
 
3
3
  export default async app => (await cascade(app.modules.register))(app);
@@ -1,4 +1,4 @@
1
- import {Headers} from "runtime-compat/http";
1
+ import { Headers } from "runtime-compat/http";
2
2
 
3
3
  export const isResponse = value =>
4
4
  value.body !== undefined && value.headers instanceof Headers;
@@ -1,2 +1,2 @@
1
- export {isResponse} from "./duck.js";
2
- export {default as respond} from "./respond.js";
1
+ export { isResponse } from "./duck.js";
2
+ export { default as respond } from "./respond.js";
@@ -1,7 +1,7 @@
1
- import {Blob} from "runtime-compat/fs";
2
- import {URL} from "runtime-compat/http";
3
- import {text, json, stream, redirect} from "primate";
4
- import {isResponse as isResponseDuck} from "./duck.js";
1
+ import { Blob } from "runtime-compat/fs";
2
+ import { URL } from "runtime-compat/http";
3
+ import { text, json, stream, redirect } from "primate";
4
+ import { isResponse as isResponseDuck } from "./duck.js";
5
5
 
6
6
  const isText = value => {
7
7
  if (typeof value === "string") {
@@ -1,5 +1,5 @@
1
- import {from} from "runtime-compat/object";
2
- import {tryreturn} from "runtime-compat/sync";
1
+ import { from } from "runtime-compat/object";
2
+ import { tryreturn } from "runtime-compat/sync";
3
3
  import errors from "../errors.js";
4
4
  import validate from "../validate.js";
5
5
 
@@ -13,7 +13,7 @@ const deroot = pathname => pathname.endsWith("/") && pathname !== "/"
13
13
  ? pathname.slice(0, -1) : pathname;
14
14
 
15
15
  export default app => {
16
- const {types, routes, config: {types: {explicit}, location}} = app;
16
+ const { types, routes, config: { types: { explicit }, location } } = app;
17
17
 
18
18
  const to_path = (route, pathname) => app.dispatch(from(Object
19
19
  .entries(route.pathname.exec(pathname)?.groups ?? {})
@@ -21,7 +21,7 @@ export default app => {
21
21
  [types[name] === undefined || explicit ? name : `${name}$${name}`, value])
22
22
  .map(([name, value]) => [name.split("$"), value])
23
23
  .map(([[name, type], value]) =>
24
- [name, type === undefined ? value : validate(types[type], value, name)]
24
+ [name, type === undefined ? value : validate(types[type], value, name)],
25
25
  )));
26
26
 
27
27
  const is_type = (groups, pathname) => Object
@@ -32,22 +32,22 @@ export default app => {
32
32
  .map(([name, value]) => [name.split("$"), value])
33
33
  .map(([[name, type], value]) =>
34
34
  tryreturn(_ => [name, validate(types[type], value, name)])
35
- .orelse(({message}) => errors.MismatchedPath.throw(pathname, message)));
36
- const is_path = ({route, pathname}) => {
35
+ .orelse(({ message }) => errors.MismatchedPath.throw(pathname, message)));
36
+ const is_path = ({ route, pathname }) => {
37
37
  const result = route.pathname.exec(pathname);
38
38
  return result === null ? false : is_type(result.groups, pathname);
39
39
  };
40
- const is_method = ({route, method, pathname}) => ieq(route.method, method)
41
- && is_path({route, pathname});
40
+ const is_method = ({ route, method, pathname }) => ieq(route.method, method)
41
+ && is_path({ route, pathname });
42
42
  const find = (method, pathname) => routes.find(route =>
43
- is_method({route, method, pathname}));
43
+ is_method({ route, method, pathname }));
44
44
 
45
45
  const index = path => `${location.routes}${path === "/" ? "/index" : path}`;
46
46
 
47
- return ({original: {method}, url}) => {
47
+ return ({ original: { method }, url }) => {
48
48
  const pathname = deroot(url.pathname);
49
49
  const route = find(method, pathname) ?? errors.NoRouteToPath
50
50
  .throw(method.toLowerCase(), pathname, index(pathname));
51
- return {...route, path: to_path(route, pathname)};
51
+ return { ...route, path: to_path(route, pathname) };
52
52
  };
53
53
  };
@@ -1,6 +1,6 @@
1
- import {cascade} from "runtime-compat/async";
1
+ import { cascade } from "runtime-compat/async";
2
2
 
3
3
  export default async (app, server) => {
4
- app.log.info("running serve hooks", {module: "primate"});
5
- await (await cascade(app.modules.serve))({...app, server});
4
+ app.log.info("running serve hooks", { module: "primate" });
5
+ await (await cascade(app.modules.serve))({ ...app, server });
6
6
  };
@@ -1,5 +1,5 @@
1
- import {Path} from "runtime-compat/fs";
2
- import {identity} from "runtime-compat/function";
1
+ import { Path } from "runtime-compat/fs";
2
+ import { identity } from "runtime-compat/function";
3
3
  import errors from "../errors.js";
4
4
 
5
5
  const ending = ".js";
@@ -17,7 +17,7 @@ export default async ({
17
17
  warn = true,
18
18
  } = {}) => {
19
19
  const objects = directory === undefined ? [] : await Promise.all(
20
- (await Path.collect(directory, /^.*.js$/u, {recursive}))
20
+ (await Path.collect(directory, /^.*.js$/u, { recursive }))
21
21
  .filter(filter)
22
22
  .map(async path => [
23
23
  `${path}`.replace(directory, _ => "").slice(1, -ending.length),
@@ -1,3 +1,3 @@
1
- export {default as modules} from "./modules.js";
2
- export {default as routes} from "./routes.js";
3
- export {default as types} from "./types.js";
1
+ export { default as modules } from "./modules.js";
2
+ export { default as routes } from "./routes.js";
3
+ export { default as types } from "./types.js";
@@ -1,5 +1,5 @@
1
1
  import * as hooks from "../hooks/exports.js";
2
- import {doubled} from "./common.js";
2
+ import { doubled } from "./common.js";
3
3
  import errors from "../errors.js";
4
4
 
5
5
  const filter = (key, array) => array?.flatMap(m => m[key] ?? []) ?? [];
@@ -15,18 +15,21 @@ export default async (log, root, config) => {
15
15
  modules.some((module, n) => module.name === undefined &&
16
16
  errors.ModuleHasNoName.throw(n));
17
17
 
18
- const names = modules.map(({name}) => name);
18
+ const names = modules.map(({ name }) => name);
19
19
  new Set(names).size !== modules.length &&
20
20
  errors.DoubleModule.throw(doubled(names), root.join("primate.config.js"));
21
21
 
22
22
  const hookless = modules.filter(module => !Object.keys(module).some(key =>
23
- [...Object.keys(hooks), "load"].includes(key)));
23
+ [...Object.keys(hooks), "load", "context"].includes(key)));
24
24
  hookless.length > 0 && errors.ModuleHasNoHooks.warn(log,
25
- hookless.map(({name}) => name).join(", "));
25
+ hookless.map(({ name }) => name).join(", "));
26
26
 
27
27
  // collect modules
28
28
  const loaded = load(modules).flat(2);
29
29
 
30
- return Object.fromEntries(Object.keys(hooks)
31
- .map(hook => [hook, filter(hook, loaded)]));
30
+ return {
31
+ names: loaded.map(module => module.name),
32
+ ...Object.fromEntries([...Object.keys(hooks), "context"]
33
+ .map(hook => [hook, filter(hook, loaded)])),
34
+ };
32
35
  };
@@ -1,4 +1,4 @@
1
- export {default as routes} from "./routes.js";
2
- export {default as guards} from "./guards.js";
3
- export {default as errors} from "./errors.js";
4
- export {default as layouts} from "./layouts.js";
1
+ export { default as routes } from "./routes.js";
2
+ export { default as guards } from "./guards.js";
3
+ export { default as errors } from "./errors.js";
4
+ export { default as layouts } from "./layouts.js";
@@ -1,4 +1,4 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "runtime-compat/fs";
2
2
  import errors from "../../errors.js";
3
3
  import to_sorted from "../../to_sorted.js";
4
4
 
@@ -6,7 +6,7 @@ export default type => async (log, directory, load) => {
6
6
  const filter = path => new RegExp(`^\\+${type}.js$`, "u").test(path.name);
7
7
 
8
8
  const replace = new RegExp(`\\+${type}`, "u");
9
- const objects = to_sorted((await load({log, directory, filter, warn: false}))
9
+ const objects = to_sorted((await load({ log, directory, filter, warn: false }))
10
10
  .map(([name, object]) => [name.replace(replace, () => ""), object]),
11
11
  ([a], [b]) => a.length - b.length);
12
12
 
@@ -1,4 +1,4 @@
1
- import {doubled} from "../common.js";
1
+ import { doubled } from "../common.js";
2
2
  import errors from "../../errors.js";
3
3
 
4
4
  const normalize = route => {
@@ -13,7 +13,7 @@ const deindex = path => path.endsWith("index") ?
13
13
 
14
14
  export default async (log, directory, load) => {
15
15
  const filter = path => /^[^+].*.js$/u.test(path.name);
16
- const routes = (await load({log, directory, filter}))
16
+ const routes = (await load({ log, directory, filter }))
17
17
  .map(([path, handler]) => [deindex(path), handler]);
18
18
 
19
19
  const double = doubled(routes.map(([route]) => normalize(route)));
@@ -1,8 +1,8 @@
1
- import {tryreturn} from "runtime-compat/sync";
2
- import {from} from "runtime-compat/object";
1
+ import { tryreturn } from "runtime-compat/sync";
2
+ import { from } from "runtime-compat/object";
3
3
  import errors from "../errors.js";
4
- import {invalid} from "../hooks/route.js";
5
- import {default as fs, doubled} from "./common.js";
4
+ import { invalid } from "../hooks/route.js";
5
+ import { default as fs, doubled } from "./common.js";
6
6
  import * as get from "./routes/exports.js";
7
7
 
8
8
  const make = path => {
@@ -13,7 +13,7 @@ const make = path => {
13
13
 
14
14
  const route = path.replaceAll(/\{(?<named>.*?)\}/gu, (_, named) =>
15
15
  tryreturn(_ => {
16
- const {name, type} = /^(?<name>\w+)(?<type>=\w+)?$/u.exec(named).groups;
16
+ const { name, type } = /^(?<name>\w+)(?<type>=\w+)?$/u.exec(named).groups;
17
17
  const param = type === undefined ? name : `${name}$${type.slice(1)}`;
18
18
  return `(?<${param}>[^/]{1,}?)`;
19
19
  }).orelse(_ => errors.InvalidPathParameter.throw(named, path)));
@@ -1,11 +1,11 @@
1
- import {Path} from "runtime-compat/fs";
1
+ import { Path } from "runtime-compat/fs";
2
2
  import errors from "../errors.js";
3
3
  import fs from "./common.js";
4
4
 
5
5
  const filter = path => /^[a-z]/u.test(path.name);
6
6
 
7
7
  export default async (log, directory, load = fs) => {
8
- const types = await load({log, directory, name: "types", filter});
8
+ const types = await load({ log, directory, name: "types", filter });
9
9
 
10
10
  const resolve = name => new Path(directory, name);
11
11
  types.some(([name, type]) => typeof type !== "function"
package/src/run.js CHANGED
@@ -1,14 +1,14 @@
1
- import {tryreturn} from "runtime-compat/async";
2
- import {Path} from "runtime-compat/fs";
3
- import {extend} from "runtime-compat/object";
1
+ import { tryreturn } from "runtime-compat/async";
2
+ import { Path } from "runtime-compat/fs";
3
+ import { extend } from "runtime-compat/object";
4
4
  import app from "./app.js";
5
- import {default as Logger, bye} from "./Logger.js";
5
+ import { default as Logger, bye } from "./Logger.js";
6
6
  import errors from "./errors.js";
7
7
  import command from "./commands/exports.js";
8
8
  import defaults from "./defaults/primate.config.js";
9
9
 
10
- let logger = new Logger({level: Logger.Warn});
11
- const {runtime = "node"} = import.meta;
10
+ let logger = new Logger({ level: Logger.Warn });
11
+ const { runtime = "node" } = import.meta;
12
12
 
13
13
  const get_config = async root => {
14
14
  const name = "primate.config.js";
@@ -21,7 +21,7 @@ const get_config = async root => {
21
21
  errors.EmptyConfigFile.warn(logger, config);
22
22
 
23
23
  return extend(defaults, imported);
24
- }).orelse(({message}) =>
24
+ }).orelse(({ message }) =>
25
25
  errors.ErrorInConfigFile.throw(message, `${runtime} ${config}`))
26
26
  : defaults;
27
27
  };
package/src/start.js CHANGED
@@ -1,5 +1,5 @@
1
- import {serve, Response, Status} from "runtime-compat/http";
2
- import {cascade, tryreturn} from "runtime-compat/async";
1
+ import { serve, Response, Status } from "runtime-compat/http";
2
+ import { cascade, tryreturn } from "runtime-compat/async";
3
3
  import * as hooks from "./hooks/exports.js";
4
4
 
5
5
  export default async (app$, deactivated = []) => {
@@ -9,7 +9,7 @@ export default async (app$, deactivated = []) => {
9
9
  if (deactivated.includes(hook)) {
10
10
  continue;
11
11
  }
12
- app.log.info(`running ${hook} hooks`, {module: "primate"});
12
+ app.log.info(`running ${hook} hooks`, { module: "primate" });
13
13
  app = await hooks[hook](app);
14
14
  }
15
15
 
@@ -20,9 +20,9 @@ export default async (app$, deactivated = []) => {
20
20
  tryreturn(async _ => (await hooks.handle(app))(await app.parse(request)))
21
21
  .orelse(error => {
22
22
  app.log.auto(error);
23
- return new Response(null, {status: Status.INTERNAL_SERVER_ERROR});
23
+ return new Response(null, { status: Status.INTERNAL_SERVER_ERROR });
24
24
  }),
25
25
  app.config.http);
26
26
 
27
- await (await cascade(app.modules.serve))({...app, server});
27
+ await (await cascade(app.modules.serve))({ ...app, server });
28
28
  };
package/src/validate.js CHANGED
@@ -1,4 +1,4 @@
1
- import {is, maybe} from "runtime-compat/invariant";
1
+ import { is, maybe } from "runtime-compat/invariant";
2
2
 
3
3
  export default (type, value, name) => {
4
4
  maybe(type.validate).function();