@primate/core 0.1.0 → 0.1.2

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/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Primate core",
5
5
  "homepage": "https://primatejs.com",
6
6
  "bugs": "https://github.com/primatejs/primate/issues",
@@ -21,14 +21,14 @@
21
21
  "@rcompat/build": "^0.4.0",
22
22
  "@rcompat/cli": "^0.5.1",
23
23
  "@rcompat/crypto": "^0.5.0",
24
- "@rcompat/fs": "^0.4.0",
24
+ "@rcompat/fs": "^0.4.1",
25
25
  "@rcompat/function": "^0.4.0",
26
- "@rcompat/http": "^0.5.1",
26
+ "@rcompat/http": "^0.5.2",
27
27
  "@rcompat/invariant": "^0.5.0",
28
28
  "@rcompat/object": "^0.5.0",
29
29
  "@rcompat/package": "^0.7.0",
30
30
  "@rcompat/platform": "^0.3.0",
31
- "@rcompat/stdio": "^0.4.0",
31
+ "@rcompat/stdio": "^0.5.0",
32
32
  "@rcompat/sync": "^0.3.0"
33
33
  },
34
34
  "type": "module",
@@ -8,6 +8,7 @@ import join from "@rcompat/fs/join";
8
8
  import exclude from "@rcompat/object/exclude";
9
9
  import stringify from "@rcompat/object/stringify";
10
10
  import manifest from "@rcompat/package/manifest";
11
+ import root from "@rcompat/package/root";
11
12
  import copy_includes from "./copy_includes.js";
12
13
  import $router from "./router.js";
13
14
 
@@ -163,7 +164,12 @@ const post = async (app, mode, target) => {
163
164
  await write_bootstrap(build_number, app, mode);
164
165
 
165
166
  // copy config file
166
- await app.root.join(config_filename).copy(app.path.build.join(config_filename));
167
+ const local_config = app.root.join(config_filename);
168
+ const build_config = app.path.build.join(config_filename);
169
+ const root_base = await root(import.meta.url);
170
+ const default_config = root_base.join("/src/private/config.js");
171
+ (await local_config.exists() ? local_config : default_config)
172
+ .copy(build_config);
167
173
 
168
174
  const manifest_data = await manifest();
169
175
  // create package.json
@@ -12,18 +12,27 @@ import platform from "@rcompat/platform";
12
12
  import app from "./app.js";
13
13
  import { build, init } from "./hook/exports.js";
14
14
 
15
+ const empty_config = config => config === undefined || empty(config);
16
+
15
17
  const get_config = async project_root => {
16
18
  const local_config = project_root.join(config_filename);
17
- return await local_config.exists()
18
- ? tryreturn(async _ => {
19
+ const exists = await local_config.exists()
20
+ if (exists) {
21
+ try {
19
22
  const imported = await local_config.import("default");
20
23
 
21
- empty(imported) && empty_config_file(local_config);
24
+ empty_config(imported) && empty_config_file(local_config);
22
25
 
23
26
  return imported;
24
- }).orelse(({ message }) =>
25
- error_in_config_file(message, `${platform} ${local_config}`))
26
- : config;
27
+ } catch (error) {
28
+ if (error.level === undefined) {
29
+ error_in_config_file(error.message, `${platform} ${local_config}`);
30
+ } else {
31
+ throw error;
32
+ }
33
+ }
34
+ }
35
+ return config;
27
36
  };
28
37
 
29
38
  export default async (mode, target) => tryreturn(async _ => {
@@ -22,13 +22,15 @@ export default async app => {
22
22
  const pages = await Promise.all((await collect(d, html, { recursive: true }))
23
23
  .map(async file => `${file}`.replace(`${d}/`, _ => "")));
24
24
  const pages_str = pages.map(page =>
25
- `"${page}": await file("./${location.pages}/${page}").text(),`).join("\n");
25
+ `"${page}": await join(import.meta.url, "../${location.pages}/${page}").text(),`).join("\n");
26
26
 
27
27
  const assets_scripts = `
28
28
  import file from "@rcompat/fs/file";
29
29
  import join from "@rcompat/fs/join";
30
30
  import stringify from "@rcompat/object/stringify";
31
31
  import crypto from "@rcompat/crypto";
32
+ import { OK } from "@rcompat/http/status";
33
+ import { resolve } from "@rcompat/http/mime";
32
34
 
33
35
  const encoder = new TextEncoder();
34
36
  const hash = async (data, algorithm = "sha-384") => {
@@ -63,13 +65,28 @@ export default async app => {
63
65
  const pages = {
64
66
  ${pages_str}
65
67
  };
68
+ const buildroot = file(import.meta.url).join("..");
69
+
70
+ const serve_asset = asset => new Response(asset.stream(), {
71
+ status: OK,
72
+ headers: {
73
+ "Content-Type": resolve(asset.name),
74
+ },
75
+ });
66
76
 
67
77
  const loader = {
68
78
  page(name) {
69
79
  return pages[name] ?? pages["${app.get("pages.app")}"];
70
80
  },
71
- asset(pathname) {
72
- return assets.find(asset => asset.src === pathname);
81
+ async asset(pathname) {
82
+ const root_asset = buildroot.join(\`client/\${pathname}\`);
83
+ if (await await root_asset.isFile) {
84
+ return serve_asset(asset);
85
+ }
86
+ const static_asset = buildroot.join(\`client/static/\${pathname}\`);
87
+ if (await static_asset.isFile) {
88
+ return serve_asset(static_asset);
89
+ }
73
90
  },
74
91
  };
75
92
  const target = "web";
@@ -1,4 +1,3 @@
1
- import log from "#log";
2
1
  import client_error from "@primate/core/handler/error";
3
2
  import cascade from "@rcompat/async/cascade";
4
3
  import tryreturn from "@rcompat/async/tryreturn";
@@ -71,21 +70,8 @@ export default app => {
71
70
  });
72
71
  };
73
72
 
74
- const as_asset = async (pathname, code) => new Response(code, {
75
- status: OK,
76
- headers: {
77
- "Content-Type": resolve(pathname),
78
- // Etag: await path.modified(),
79
- },
80
- });
81
-
82
- const handle = async request => {
83
- const { pathname } = request.url;
84
-
85
- const asset = app.loader.asset(pathname)?.code;
86
-
87
- return asset === undefined ? as_route(request) : as_asset(pathname, asset);
88
- };
73
+ const handle = async request =>
74
+ (await app.loader.asset(request.url.pathname)) ?? as_route(request);
89
75
  // first hook
90
76
  const pass = (request, next) => next({
91
77
  ...request,