primate 0.6.3 → 0.7.3

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 CHANGED
@@ -1,14 +1,8 @@
1
- # Primate, A JavaScript Framework
1
+ # Primate, a cross-runtime framework
2
2
 
3
- A full-stack Javascript framework, Primate relieves you of dealing with
4
- repetitive, error-prone tasks and lets you concentrate on writing effective,
5
- expressive code.
6
-
7
- ## Installing
8
-
9
- ```
10
- npm install primate
11
- ```
3
+ Primate is a full-stack cross-runtime Javascript framework (Node.js and Deno).
4
+ It relieves you of dealing with repetitive, error-prone tasks and lets you
5
+ concentrate on writing effective, expressive code.
12
6
 
13
7
  ## Highlights
14
8
 
@@ -17,15 +11,309 @@ npm install primate
17
11
  * Built-in support for sessions with secure cookies
18
12
  * Input verification using data domains
19
13
  * Many different data store modules: In-Memory (built-in),
20
- [File][primate-store-file], [JSON][primate-store-json],
21
- [MongoDB][primate-store-mongodb]
14
+ [File][primate-file-store], [JSON][primate-json-store],
15
+ [MongoDB][primate-mongodb-store]
22
16
  * Easy modelling of`1:1`, `1:n` and `n:m` relationships
23
17
  * Minimally opinionated with sane, overrideable defaults
24
- * No dependencies
18
+ * Supports both Node.js and Deno
25
19
 
26
20
  ## Getting started
27
21
 
28
- See the [getting started][getting-started] guide.
22
+ ### Prepare
23
+
24
+ Lay out your app
25
+
26
+ ```sh
27
+ $ mkdir -p primate-app/{routes,components,ssl} && cd primate-app
28
+ ```
29
+
30
+ Create a route for `/`
31
+
32
+ ```js
33
+ // routes/site.js
34
+
35
+ import {router, html} from "primate";
36
+
37
+ router.get("/", () => html`<site-index date=${new Date()} />`);
38
+ ```
39
+
40
+ Create a component for your route (in `components/site-index.html`)
41
+
42
+ ```html
43
+ Today's date is <span data-value="date"></span>.
44
+ ```
45
+
46
+ Generate SSL key/certificate
47
+
48
+ ```sh
49
+ openssl req -x509 -out ssl/default.crt -keyout ssl/default.key -newkey rsa:2048 -nodes -sha256 -batch
50
+ ```
51
+
52
+ Add an entry file
53
+
54
+ ```js
55
+ // app.js
56
+
57
+ import {app} from "primate";
58
+ app.run();
59
+ ```
60
+
61
+ ### Run on Node.js
62
+
63
+ Create a start script and enable ES modules (in `package.json`)
64
+
65
+ ```json
66
+ {
67
+ "scripts": {
68
+ "start": "node --experimental-json-modules app.js"
69
+ },
70
+ "type": "module"
71
+ }
72
+ ```
73
+
74
+ Install Primate
75
+
76
+ ```sh
77
+ $ npm install primate
78
+ ```
79
+
80
+ Run app
81
+
82
+ ```sh
83
+ $ npm start
84
+ ```
85
+
86
+ ### Run on Deno
87
+
88
+ Create an import map file (`import-map.json`)
89
+
90
+ ```json
91
+ {
92
+ "imports": {
93
+ "runtime-compat": "https://deno.land/x/runtime_compat/exports.js",
94
+ "primate": "https:/deno.land/x/primate/exports.js"
95
+ }
96
+ }
97
+ ```
98
+
99
+ Run app
100
+
101
+ ```
102
+ deno run --import-map=import-map.json app.js
103
+ ```
104
+
105
+ You will typically need the `allow-read`, `allow-write` and `allow-net`
106
+ permissions.
107
+
108
+ ## Table of Contents
109
+
110
+ * [Routes](#routes)
111
+ * [Handlers](#handlers)
112
+ * [Components](#components)
113
+ * [Domains](#domains)
114
+ * [Verification](#verification)
115
+
116
+ ## Routes
117
+
118
+ Create routes in the `routes` directory by importing and using the `router`
119
+ singleton. You can group your routes across several files or keep them
120
+ in one file.
121
+
122
+ ### `router[get|post](pathname, request => ...)`
123
+
124
+ Routes are tied to a pathname and execute their callback when the pathname is
125
+ encountered.
126
+
127
+ ```js
128
+ // in routes/some-file.js
129
+ import {router, json} from "primate";
130
+
131
+ // on matching the exact pathname /, returns {"foo": "bar"} as JSON
132
+ router.get("/", () => json`${{"foo": "bar"}}`);
133
+ ```
134
+
135
+ All routes must return a template function handler. See the
136
+ [section on handlers for common handlers](#handlers).
137
+
138
+ The callback has one parameter, the request data.
139
+
140
+ ### The `request` object
141
+
142
+ The request contains the `path`, a `/` separated array of the pathname.
143
+
144
+ ```js
145
+ import {router, html} from "primate";
146
+
147
+ router.get("/site/login", request => json`${{"path": request.path}}`);
148
+ // accessing /site/login -> {"path":["site","login"]}
149
+ ```
150
+
151
+ The HTTP request's body is available under `request.payload`.
152
+
153
+ ### Regular expressions in routes
154
+
155
+ All routes are treated as regular expressions.
156
+
157
+ ```js
158
+ import {router, json} from "primate";
159
+
160
+ router.get("/user/view/([0-9])+", request => json`${{"path": request.path}}`);
161
+ // accessing /user/view/1234 -> {"path":["site","login","1234"]}
162
+ // accessing /user/view/abcd -> error 404
163
+ ```
164
+
165
+ ### `router.alias(from, to)`
166
+
167
+ To reuse certain parts of a pathname, you can define aliases which will be
168
+ applied before matching.
169
+
170
+ ```js
171
+ import {router, json} from "primate";
172
+
173
+ router.alias("_id", "([0-9])+");
174
+
175
+ router.get("/user/view/_id", request => json`${{"path": request.path}}`);
176
+
177
+ router.get("/user/edit/_id", request => ...);
178
+ ```
179
+
180
+ ### `router.map(pathname, request => ...)`
181
+
182
+ You can reuse functionality across the same path but different HTTP verbs. This
183
+ function has the same signature as `router[get|post]`.
184
+
185
+ ```js
186
+ import {router, html, redirect} from "primate";
187
+
188
+ router.alias("_id", "([0-9])+");
189
+
190
+ router.map("/user/edit/_id", request => {
191
+ const user = {"name": "Donald"};
192
+ // return original request and user
193
+ return {...request, user};
194
+ });
195
+
196
+ router.get("/user/edit/_id", request => {
197
+ // show user edit form
198
+ return html`<user-edit user=${request.user} />`
199
+ });
200
+
201
+ router.post("/user/edit/_id", request => {
202
+ const {user} = request;
203
+ // verify form and save / show errors
204
+ return await user.save() ? redirect`/users` : html`<user-edit user=${user} />`
205
+ });
206
+ ```
207
+
208
+ ## Handlers
209
+
210
+ Handlers are tagged template functions usually associated with data.
211
+
212
+ ### ``html`<component-name attribute=${value} />` ``
213
+
214
+ Compiles and serves a component from the `components` directory and with the
215
+ specified attributes and their values. Returns an HTTP 200 response with the
216
+ `text/html` content type.
217
+
218
+ ### ``json`${{data}}` ``
219
+
220
+ Serves JSON `data`. Returns an HTTP 200 response with the `application/json`
221
+ content type.
222
+
223
+ ### ``redirect`${url}` ``
224
+
225
+ Redirects to `url`. Returns an HTTP 302 response.
226
+
227
+ ## Components
228
+
229
+ Create HTML components in the `components` directory. Use `data`-attributes to
230
+ show data within your component.
231
+
232
+ ```js
233
+ // in routes/user.js
234
+ import {router, html, redirect} from "primate";
235
+
236
+ router.alias("_id", "([0-9])+");
237
+
238
+ router.map("/user/edit/_id", request => {
239
+ const user = {"name": "Donald", "email": "donald@was.here"};
240
+ // return original request and user
241
+ return {...request, user};
242
+ });
243
+
244
+ router.get("/user/edit/_id", request => {
245
+ // show user edit form
246
+ return html`<user-edit user=${request.user} />`
247
+ });
248
+
249
+ router.post("/user/edit/_id", request => {
250
+ const {user, payload} = request;
251
+ // verify form and save / show errors
252
+ // this assumes `user` has a method `save` to verify data
253
+ return await user.save(payload) ? redirect`/users` : html`<user-edit user=${user} />`
254
+ });
255
+ ```
256
+
257
+ ```html
258
+ <!-- components/edit-user.html -->
259
+ <form method="post">
260
+ <h1>Edit user</h1>
261
+ <p>
262
+ <input name="user.name" data-value="user.name"></textarea>
263
+ </p>
264
+ <p>
265
+ <input name="user.email" data-value="user.email"></textarea>
266
+ </p>
267
+ <input type="submit" value="Save user" />
268
+ </form>
269
+ ```
270
+
271
+ ### Grouping objects with `data-for`
272
+
273
+ You can use the special attribute `data-for` to group objects.
274
+
275
+ ```html
276
+ <!-- components/edit-user.html -->
277
+ <form data-for="user" method="post">
278
+ <h1>Edit user</h1>
279
+ <p>
280
+ <input name="name" data-value="name" />
281
+ </p>
282
+ <p>
283
+ <input name="email" data-value="email" />
284
+ </p>
285
+ <input type="submit" value="Save user" />
286
+ </form>
287
+ ```
288
+
289
+ ### Expanding arrays
290
+
291
+ `data-for` can also be used to expand arrays.
292
+
293
+ ```js
294
+ // in routes/user.js
295
+ import {router, html} from "primate";
296
+
297
+ router.get("/users", request => {
298
+ const users = [
299
+ {"name": "Donald", "email": "donald@was.here"},
300
+ {"name": "Ryan", "email": "ryan@was.here"},
301
+ ];
302
+ return html`<user-index users=${users} />`;
303
+ });
304
+ ```
305
+
306
+ ```html
307
+ <!-- in components/user-index.html -->
308
+ <div data-for="users">
309
+ User <span data-value="name"></span>
310
+ Email <span data-value="email"></span>
311
+ </div>
312
+ ```
313
+
314
+ ## Resources
315
+
316
+ * [Getting started guide][getting-started]
29
317
 
30
318
  ## License
31
319
 
@@ -34,6 +322,6 @@ BSD-3-Clause
34
322
  [getting-started]: https://primatejs.com/getting-started
35
323
  [source-code]: https://github.com/primatejs/primate
36
324
  [issues]: https://github.com/primatejs/primate/issues
37
- [primate-store-file]: https://npmjs.com/primate-store-file
38
- [primate-store-json]: https://npmjs.com/primate-store-json
39
- [primate-store-mongodb]: https://npmjs.com/primate-store-mongodb
325
+ [primate-file-store]: https://npmjs.com/primate-file-store
326
+ [primate-json-store]: https://npmjs.com/primate-json-store
327
+ [primate-mongodb-store]: https://npmjs.com/primate-mongodb-store
package/exports.js ADDED
@@ -0,0 +1,28 @@
1
+ import conf from "./source/conf.js";
2
+ import App from "./source/App.js";
3
+
4
+ export {App};
5
+ export {default as Bundler} from "./source/Bundler.js";
6
+ export {default as EagerPromise, eager} from "./source/EagerPromise.js" ;
7
+
8
+ export {default as Domain} from "./source/domain/Domain.js";
9
+ export {default as Storeable} from "./source/types/Storeable.js";
10
+
11
+ export * from "./source/errors.js";
12
+ export * from "./source/invariants.js";
13
+
14
+ export {default as MemoryStore} from "./source/store/Memory.js";
15
+ export {default as Store} from "./source/store/Store.js";
16
+
17
+ export {default as extend_object} from "./source/extend_object.js";
18
+ export {default as sanitize} from "./source/sanitize.js";
19
+
20
+ export {default as html} from "./source/handlers/html.js";
21
+ export {default as json} from "./source/handlers/json.js";
22
+ export {default as redirect} from "./source/handlers/redirect.js";
23
+
24
+ export {default as router} from "./source/Router.js";
25
+
26
+ const app = new App(conf());
27
+
28
+ export {app};
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "primate",
3
- "version": "0.6.3",
3
+ "version": "0.7.3",
4
4
  "author": "Primate core team <core@primatejs.com>",
5
5
  "homepage": "https://primatejs.com",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
7
7
  "repository": "https://github.com/primatejs/primate",
8
- "description": "Full-stack JavaScript framework",
8
+ "description": "Cross-runtime JavaScript framework",
9
9
  "license": "BSD-3-Clause",
10
10
  "scripts": {
11
11
  "copy": "rm -rf output && mkdir output && cp source/* output -a",
@@ -14,6 +14,9 @@
14
14
  "coverage": "npm run instrument && nyc npm run debris",
15
15
  "test": "npm run copy && npm run debris"
16
16
  },
17
+ "dependencies": {
18
+ "runtime-compat": "^0.1.4"
19
+ },
17
20
  "devDependencies": {
18
21
  "debris": "^0.2.2",
19
22
  "eslint": "^8.13.0",
@@ -21,7 +24,7 @@
21
24
  "nyc": "^15.1.0"
22
25
  },
23
26
  "type": "module",
24
- "exports": "./source/exports.js",
27
+ "exports": "./exports.js",
25
28
  "engines": {
26
29
  "node": ">=17.9.0"
27
30
  }
package/source/App.js CHANGED
@@ -1,10 +1,7 @@
1
- import {resolve} from "path";
1
+ import {Path, File, log} from "runtime-compat";
2
2
  import Bundler from "./Bundler.js";
3
- import File from "./File.js";
4
- import Directory from "./Directory.js";
5
3
  import Router from "./Router.js";
6
4
  import Server from "./Server.js";
7
- import log from "./log.js";
8
5
  import package_json from "../package.json" assert {"type": "json"};
9
6
 
10
7
  export default class App {
@@ -14,10 +11,9 @@ export default class App {
14
11
 
15
12
  async run() {
16
13
  log.reset("Primate").yellow(package_json.version);
17
-
18
- const routes = await Directory.list(this.conf.paths.routes);
14
+ const routes = await File.list(this.conf.paths.routes);
19
15
  for (const route of routes) {
20
- await import(`${this.conf.paths.routes}/${route}`);
16
+ await import(`file://${this.conf.paths.routes}/${route}`);
21
17
  }
22
18
  await new Bundler(this.conf).bundle();
23
19
 
@@ -25,8 +21,10 @@ export default class App {
25
21
  "serve_from": this.conf.paths.public,
26
22
  "http": {
27
23
  ...this.conf.http,
28
- "key": File.read_sync(resolve(this.conf.http.ssl.key)),
29
- "cert": File.read_sync(resolve(this.conf.http.ssl.cert)),
24
+ "key": File.read_sync(Path.resolve(this.conf.http.ssl.key)),
25
+ "cert": File.read_sync(Path.resolve(this.conf.http.ssl.cert)),
26
+ "keyFile": Path.resolve(this.conf.http.ssl.key),
27
+ "certFile": Path.resolve(this.conf.http.ssl.cert),
30
28
  },
31
29
  };
32
30
  this.server = new Server(conf);
package/source/Bundler.js CHANGED
@@ -1,9 +1,7 @@
1
- import {dirname} from "path";
2
- import {fileURLToPath} from "url";
3
- import File from "./File.js";
1
+ import {Path, File} from "runtime-compat";
4
2
 
5
- const meta_url = fileURLToPath(import.meta.url);
6
- const directory = dirname(meta_url);
3
+ const meta_url = new Path(import.meta.url).path;
4
+ const directory = Path.dirname(meta_url);
7
5
  const preset = `${directory}/preset`;
8
6
 
9
7
  export default class Bundler {
@@ -14,25 +12,16 @@ export default class Bundler {
14
12
  this.scripts = [];
15
13
  }
16
14
 
17
- async copy_with_preset(subdirectory, to) {
18
- const {paths} = this.conf;
19
-
20
- // copy files preset files first
21
- await File.copy(`${preset}/${subdirectory}`, to);
22
-
23
- // copy any user code over it, not recreating the folder
24
- try {
25
- await File.copy(paths[subdirectory], to, false);
26
- } catch(error) {
27
- // directory doesn't exist
28
- }
29
- }
30
-
31
15
  async bundle() {
32
16
  const {paths} = this.conf;
33
17
 
18
+ // remove public directory in case exists
19
+ await File.remove(paths.public);
20
+ // create public directory
21
+ await File.create(paths.public);
22
+
34
23
  // copy static files to public
35
- await this.copy_with_preset("static", paths.public);
24
+ await File.copy(paths.static, paths.public);
36
25
 
37
26
  // read index.html from public, then remove it (we serve it dynamically)
38
27
  await File.remove(`${paths.public}/${this.index}`);
@@ -44,6 +44,6 @@ const last = -1;
44
44
  const eager = async (strings, ...keys) =>
45
45
  (await Promise.all(strings.slice(0, last).map(async (string, i) =>
46
46
  strings[i] + await keys[i]
47
- ))).join("") + strings[strings.length+last];
47
+ ))).join("") + strings.at(last);
48
48
 
49
49
  export {eager};
package/source/Server.js CHANGED
@@ -1,10 +1,5 @@
1
- import zlib from "zlib";
2
- import {createServer} from "https";
3
- import {join} from "path";
4
- import {parse} from "url";
1
+ import {Path, File, WebServer, log} from "runtime-compat";
5
2
  import Session from "./Session.js";
6
- import File from "./File.js";
7
- import log from "./log.js";
8
3
  import codes from "./http-codes.json" assert {"type": "json"};
9
4
  import mimes from "./mimes.json" assert {"type": "json"};
10
5
  import {http404} from "./handlers/http.js";
@@ -13,11 +8,8 @@ const regex = /\.([a-z1-9]*)$/u;
13
8
  const mime = filename => mimes[filename.match(regex)[1]] ?? mimes.binary;
14
9
 
15
10
  const stream = (from, response) => {
16
- response.setHeader("Content-Encoding", "br");
17
- response.writeHead(codes.OK);
18
- return from.pipe(zlib.createBrotliCompress())
19
- .pipe(response)
20
- .on("close", () => response.end());
11
+ response.setStatus(codes.OK);
12
+ return from.pipe(response).on("close", () => response.end());
21
13
  };
22
14
 
23
15
  export default class Server {
@@ -31,25 +23,20 @@ export default class Server {
31
23
  this.csp = Object.keys(csp).reduce((policy_string, key) =>
32
24
  policy_string + `${key} ${csp[key]};`, "");
33
25
 
34
- this.server = await createServer(http, async (request, response) => {
26
+ this.server = new WebServer(http, async (request, response) => {
35
27
  const session = await Session.get(request.headers.cookie);
36
28
  if (!session.has_cookie) {
37
29
  const {cookie} = session;
38
30
  response.setHeader("Set-Cookie", `${cookie}; SameSite=${same_site}`);
39
31
  }
40
32
  response.session = session;
41
- const buffers = [];
42
-
43
- for await (const chunk of request) {
44
- buffers.push(chunk);
45
- }
46
-
47
- const data = Buffer.concat(buffers).toString();
48
- const payload = Object.fromEntries(decodeURI(data).replaceAll("+", " ")
33
+ const text = await request.text();
34
+ const payload = Object.fromEntries(decodeURI(text).replaceAll("+", " ")
49
35
  .split("&")
50
36
  .map(part => part.split("="))
51
37
  .filter(([, value]) => value !== ""));
52
- this.try(parse(request.url).path, request, response, payload);
38
+ const {pathname, search} = request.url;
39
+ return this.try(pathname + search, request, response, payload);
53
40
  });
54
41
  }
55
42
 
@@ -58,20 +45,20 @@ export default class Server {
58
45
  await this.serve(url, request, response, payload);
59
46
  } catch (error) {
60
47
  console.log(error);
61
- response.writeHead(codes.InternalServerError);
48
+ response.setStatus(codes.InternalServerError);
62
49
  response.end();
63
50
  }
64
51
  }
65
52
 
66
53
  async serve_file(url, filename, file, response) {
67
54
  response.setHeader("Content-Type", mime(filename));
68
- response.setHeader("Etag", file.modified);
69
- await response.session.log("green", url);
55
+ response.setHeader("Etag", await file.modified);
56
+ //await response.session.log("green", url);
70
57
  return stream(file.read_stream, response);
71
58
  }
72
59
 
73
60
  async serve(url, request, response, payload) {
74
- const filename = join(this.conf.serve_from, url);
61
+ const filename = Path.join(this.conf.serve_from, url);
75
62
  const file = await new File(filename);
76
63
  return await file.is_file
77
64
  ? this.serve_file(url, filename, file, response, payload)
@@ -93,8 +80,9 @@ export default class Server {
93
80
  const {body, code} = result;
94
81
  response.setHeader("Content-Security-Policy", this.csp);
95
82
  response.setHeader("Referrer-Policy", "same-origin");
96
- response.writeHead(code);
97
- response.end(body);
83
+ response.setStatus(code);
84
+ response.setBody(body);
85
+ response.end();
98
86
  }
99
87
 
100
88
  listen() {
package/source/conf.js CHANGED
@@ -1,6 +1,5 @@
1
- import {join, resolve} from "path";
1
+ import {Path, File} from "runtime-compat";
2
2
  import cache from "./cache.js";
3
- import File from "./File.js";
4
3
  import extend_object from "./extend_object.js";
5
4
  import primate_json from "./preset/primate.json" assert {"type": "json" };
6
5
 
@@ -8,16 +7,16 @@ const qualify = (root, paths) =>
8
7
  Object.keys(paths).reduce((sofar, key) => {
9
8
  const value = paths[key];
10
9
  sofar[key] = typeof value === "string"
11
- ? join(root, value)
10
+ ? Path.join(root, value)
12
11
  : qualify(`${root}/${key}`, value);
13
12
  return sofar;
14
13
  }, {});
15
14
 
16
15
  export default (file = "primate.json") => cache("conf", file, () => {
17
16
  let conf = primate_json;
18
- const root = resolve();
17
+ const root = Path.resolve();
19
18
  try {
20
- conf = extend_object(conf, JSON.parse(File.read_sync(join(root, file))));
19
+ conf = extend_object(conf, JSON.parse(File.read_sync(Path.join(root, file))));
21
20
  } catch (error) {
22
21
  // local primate.json not required
23
22
  }
@@ -1,10 +1,10 @@
1
+ import {Crypto} from "runtime-compat";
1
2
  import Field from "./Field.js";
2
3
  import {PredicateError} from "../errors.js";
3
4
  import EagerPromise from "../EagerPromise.js";
4
5
  import Store from "../store/Store.js";
5
6
  import cache from "../cache.js";
6
7
  import DomainType from "../types/Domain.js";
7
- import {random} from "../crypto.js";
8
8
 
9
9
  const length = 12;
10
10
 
@@ -23,7 +23,7 @@ export default class Domain {
23
23
  this.define("_id", {
24
24
  "type": String,
25
25
  "predicates": ["unique"],
26
- "in": value => value ?? random(length).toString("hex"),
26
+ "in": value => value ?? Crypto.random(length).toString("hex"),
27
27
  });
28
28
  return new Proxy(this, {"get": (target, property, receiver) =>
29
29
  Reflect.get(target, property, receiver) ?? target.#proxy(property),
@@ -74,12 +74,19 @@ export default class Parser {
74
74
  }
75
75
  }
76
76
 
77
+ try_create_text_node() {
78
+ if (this.buffer.length > 0) {
79
+ const child = new Node(this.node, "span");
80
+ child.text = this.buffer;
81
+ this.buffer = "";
82
+ }
83
+ }
84
+
77
85
  // currently outside of a tag
78
86
  process_not_reading_tag() {
79
87
  // encountered '<'
80
88
  if (this.current === "<") {
81
- this.node.text = this.buffer;
82
- this.buffer = "";
89
+ this.try_create_text_node();
83
90
  // mark as inside tag
84
91
  this.reading_tag = true;
85
92
  if (this.next === "/") {
@@ -111,6 +118,8 @@ export default class Parser {
111
118
  if (this.balance !== 0) {
112
119
  throw Error(`unbalanced DOM tree: ${this.balance}`);
113
120
  }
121
+ // anything left at the end could be potentially a text node
122
+ this.try_create_text_node();
114
123
  return this.tree;
115
124
  }
116
125
 
@@ -1,6 +1,5 @@
1
+ import {File} from "runtime-compat";
1
2
  import Parser from "./DOM/Parser.js";
2
- import Directory from "../Directory.js";
3
- import File from "../File.js";
4
3
  import {index} from "../Bundler.js";
5
4
  import _conf from "../conf.js";
6
5
  const conf = _conf();
@@ -9,7 +8,7 @@ const last = -1;
9
8
  const {"paths": {"components": path}} = conf;
10
9
  const components = {};
11
10
  if (await File.exists(path)) {
12
- const names = await Directory.list(path);
11
+ const names = await File.list(path);
13
12
  for (const name of names) {
14
13
  components[name.slice(0, -5)] = await File.read(`${path}/${name}`);
15
14
  }
@@ -14,6 +14,9 @@ const assert = (predicate, error) => Boolean(predicate) || errored(error);
14
14
  const is = {
15
15
  "array": value => assert(Array.isArray(value), "must be array"),
16
16
  "string": value => assert(typeof value === "string", "must be string"),
17
+ "object": value => assert(typeof value === "object" && value !== null,
18
+ "must be object"),
19
+ "function": value => assert(typeof value === "function", "must be function"),
17
20
  "defined": (value, error) => assert(value !== undefined, error),
18
21
  "undefined": value => assert(value === undefined, "must be undefined"),
19
22
  "constructible": (value, error) => assert(constructible(value), error),
@@ -24,9 +27,10 @@ const is = {
24
27
  };
25
28
  const {defined} = is;
26
29
 
27
- const maybe = Object.keys(is).reduce((aggregator, property) => {
28
- aggregator[property] = value => nullish(value) || is[property](value);
29
- return aggregator;
30
- }, {});
30
+ // too early to use map_entries here, as it relies on invariants
31
+ const maybe = Object.fromEntries(Object.entries(is).map(([key, value]) =>
32
+ [key, (...args) => nullish(args[0]) || value(...args)]));
31
33
 
32
- export {assert, defined, is, maybe};
34
+ const invariant = predicate => predicate();
35
+
36
+ export {assert, defined, is, maybe, invariant};
@@ -0,0 +1,6 @@
1
+ import {is, invariant} from "./invariants.js";
2
+
3
+ export default (object = {}, mapper = (...args) => args) =>
4
+ invariant(() => is.object(object) && is.function(mapper))
5
+ && Object.fromEntries(Object.entries(object).map(([key, value]) =>
6
+ mapper(key, value)));
@@ -1,10 +1,8 @@
1
- export default (payload = {}) => Object.keys(payload)
2
- .map(key => ({key, "value": payload[key].toString().trim()}))
3
- .map(datum => {
4
- datum.value = datum.value === "" ? undefined : datum.value;
5
- return datum;
6
- })
7
- .reduce((data, {key, value}) => {
8
- data[key] = value;
9
- return data;
10
- }, {});
1
+ import {is} from "./invariants.js";
2
+ import map_entries from "./map_entries.js";
3
+
4
+ export default (dirty = {}) => is.object(dirty)
5
+ && map_entries(dirty, (key, value) => {
6
+ const trimmed = value.toString().trim();
7
+ return [key, trimmed === "" ? undefined : trimmed];
8
+ });
@@ -1,4 +1,4 @@
1
- import {resolve} from "path";
1
+ import {Path} from "runtime-compat";
2
2
  const preset = "../preset/stores";
3
3
 
4
4
  export default class Store {
@@ -21,7 +21,7 @@ export default class Store {
21
21
  static async get(directory, file) {
22
22
  let store;
23
23
  try {
24
- store = await import(resolve(`${directory}/${file}`));
24
+ store = await import(Path.resolve(`${directory}/${file}`));
25
25
  } catch(error) {
26
26
  store = await import(`${preset}/${file}`);
27
27
  }
@@ -1,35 +0,0 @@
1
- import fs from "fs";
2
- import {join} from "path";
3
-
4
- const options = {"encoding": "utf8"};
5
-
6
- const readdir = path => new Promise((resolve, reject) =>
7
- fs.readdir(path, options,
8
- (error, files) => error === null ? resolve(files) : reject(error)
9
- ));
10
-
11
- const rm = path => new Promise((resolve, reject) =>
12
- fs.rm(path, {"recursive": true, "force": true},
13
- error => error === null ? resolve(true) : reject(error)
14
- ));
15
-
16
- const mkdir = path => new Promise((resolve, reject) =>
17
- fs.mkdir(path, error => error === null ? resolve(true) : reject(error)));
18
-
19
- export default class Directory {
20
- static list(...args) {
21
- return readdir(join(...args));
22
- }
23
-
24
- static remove(...args) {
25
- return rm(join(...args));
26
- }
27
-
28
- static make(...args) {
29
- return mkdir(join(...args));
30
- }
31
-
32
- static async remake(...args) {
33
- await this.remove(...args) && this.make(...args);
34
- }
35
- }
package/source/File.js DELETED
@@ -1,121 +0,0 @@
1
- import fs from "fs";
2
- import {join} from "path";
3
- import Directory from "./Directory.js";
4
- import EagerPromise from "./EagerPromise.js";
5
-
6
- const array = maybe => Array.isArray(maybe) ? maybe : [maybe];
7
-
8
- const filter_files = (files, filter) =>
9
- files.filter(file => array(filter).some(ending => file.endsWith(ending)));
10
-
11
- export default class File {
12
- constructor(...args) {
13
- this.path = join(...args);
14
- return EagerPromise.resolve(new Promise(resolve => {
15
- fs.lstat(this.path, (error, stats) => {
16
- this.stats = stats;
17
- resolve(this);
18
- });
19
- }));
20
- }
21
-
22
- get modified() {
23
- return Math.round(this.stats.mtimeMs);
24
- }
25
-
26
- get exists() {
27
- return this.stats !== undefined;
28
- }
29
-
30
- get is_file() {
31
- return this.exists && !this.stats.isDirectory();
32
- }
33
-
34
- get stream() {
35
- return this.read_stream;
36
- }
37
-
38
- get read_stream() {
39
- return fs.createReadStream(this.path, {"flags": "r"});
40
- }
41
-
42
- get write_stream() {
43
- return fs.createWriteStream(this.path);
44
- }
45
-
46
- remove() {
47
- return new Promise((resolve, reject) => fs.rm(this.path,
48
- {"recursive": true, "force": true},
49
- error => error === null ? resolve(this) : reject(error)
50
- ));
51
- }
52
-
53
- create() {
54
- return new Promise((resolve, reject) => fs.mkdir(this.path, error =>
55
- error === null ? resolve(this) : reject(error)
56
- ));
57
- }
58
-
59
- async copy(to, recreate = true) {
60
- if (this.stats.isDirectory()) {
61
- if (recreate) {
62
- await new File(`${to}`).recreate();
63
- }
64
- // copy all files
65
- return Promise.all((await this.list()).map(file =>
66
- new File(`${this.path}/${file}`).copy(`${to}/${file}`)
67
- ));
68
- } else {
69
- return new Promise((resolve, reject) => fs.copyFile(this.path, to,
70
- error => error === null ? resolve(this) : reject(error)));
71
- }
72
- }
73
-
74
- async list(filter) {
75
- if (!this.exists) {
76
- return [];
77
- }
78
- const files = await Directory.list(this.path);
79
- return filter !== undefined ? filter_files(files, filter) : files;
80
- }
81
-
82
- async recreate() {
83
- return (await this.remove()).create();
84
- }
85
-
86
- read(options = {"encoding": "utf8"}) {
87
- return new Promise((resolve, reject) =>
88
- fs.readFile(this.path, options, (error, nonerror) =>
89
- error === null ? resolve(nonerror) : reject(error)));
90
- }
91
-
92
- write(data, options = {"encoding": "utf8"}) {
93
- return new Promise((resolve, reject) => fs.writeFile(this.path, data,
94
- options,
95
- error => error === null ? resolve(this) : reject(error)));
96
- }
97
-
98
- static read_sync(path, options = {"encoding": "utf8"}) {
99
- return fs.readFileSync(path, options);
100
- }
101
-
102
- static exists(...args) {
103
- return new File(...args).exists;
104
- }
105
-
106
- static read(...args) {
107
- return new File(...args).read();
108
- }
109
-
110
- static write(path, data, options) {
111
- return new File(path).write(data, options);
112
- }
113
-
114
- static remove(...args) {
115
- return new File(...args).remove();
116
- }
117
-
118
- static copy(from, to, recreate) {
119
- return new File(from).copy(to, recreate);
120
- }
121
- }
package/source/crypto.js DELETED
@@ -1,8 +0,0 @@
1
- import {createHash, randomBytes} from "crypto";
2
-
3
- export const algorithm = "sha256";
4
-
5
- export const hash = (payload, digest = "base64") =>
6
- createHash(algorithm).update(payload, "utf8").digest(digest);
7
-
8
- export const random = length => randomBytes(length);
package/source/exports.js DELETED
@@ -1,31 +0,0 @@
1
- import conf from "./conf.js";
2
- import App from "./App.js";
3
-
4
- export {App};
5
- export {default as Bundler} from "./Bundler.js";
6
- export {default as Directory} from "./Directory.js";
7
- export {default as File} from "./File.js";
8
- export {default as EagerPromise, eager} from "./EagerPromise.js" ;
9
-
10
- export {default as Domain} from "./domain/Domain.js";
11
- export {default as Storeable} from "./types/Storeable.js";
12
-
13
- export * from "./errors.js";
14
- export * from "./invariants.js";
15
-
16
- export {default as MemoryStore} from "./store/Memory.js";
17
- export {default as Store} from "./store/Store.js";
18
-
19
- export {default as log} from "./log.js";
20
- export {default as extend_object} from "./extend_object.js";
21
- export {default as sanitize} from "./sanitize.js";
22
-
23
- export {default as html} from "./handlers/html.js";
24
- export {default as json} from "./handlers/json.js";
25
- export {default as redirect} from "./handlers/redirect.js";
26
-
27
- export {default as router} from "./Router.js";
28
-
29
- const app = new App(conf());
30
-
31
- export {app};
package/source/log.js DELETED
@@ -1,22 +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 default log;
@@ -1,8 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <title>Primate app</title>
5
- <meta charset="utf-8" />
6
- </head>
7
- <body></body>
8
- </html>