@primate/core 0.5.0 → 0.6.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 (297) hide show
  1. package/lib/private/App.d.ts +263 -26
  2. package/lib/private/App.js +42 -14
  3. package/lib/private/Flags.d.ts +8 -4
  4. package/lib/private/Module.d.ts +4 -16
  5. package/lib/private/Module.js +1 -17
  6. package/lib/private/app/EnvSchema.d.ts +5 -0
  7. package/lib/private/app/EnvSchema.js +2 -0
  8. package/lib/private/app/Facade.browser.d.ts +11 -0
  9. package/lib/private/app/Facade.browser.js +19 -0
  10. package/lib/private/app/Facade.d.ts +272 -27
  11. package/lib/private/app/Facade.js +23 -0
  12. package/lib/private/build/client/index.js +8 -3
  13. package/lib/private/build/client/plugin/app-request.d.ts +4 -0
  14. package/lib/private/build/client/plugin/app-request.js +19 -0
  15. package/lib/private/build/client/plugin/view.d.ts +4 -0
  16. package/lib/private/build/client/plugin/view.js +13 -0
  17. package/lib/private/build/hook.d.ts +1 -2
  18. package/lib/private/build/hook.js +21 -16
  19. package/lib/private/build/index.d.ts +2 -1
  20. package/lib/private/build/index.js +20 -21
  21. package/lib/private/build/server/index.js +7 -9
  22. package/lib/private/build/server/plugin/app-request.d.ts +4 -0
  23. package/lib/private/build/server/plugin/app-request.js +19 -0
  24. package/lib/private/build/server/plugin/assets.js +1 -1
  25. package/lib/private/build/server/plugin/live-reload.js +9 -9
  26. package/lib/private/build/server/plugin/native-addons.js +4 -7
  27. package/lib/private/build/server/plugin/route.js +2 -2
  28. package/lib/private/build/server/plugin/store.js +9 -31
  29. package/lib/private/build/server/plugin/stores.js +11 -7
  30. package/lib/private/build/server/plugin/view.js +1 -1
  31. package/lib/private/build/shared/plugin/app-request.d.ts +4 -0
  32. package/lib/private/build/shared/plugin/app-request.js +19 -0
  33. package/lib/private/client/Data.d.ts +3 -2
  34. package/lib/private/{frontend → client}/Render.d.ts +1 -1
  35. package/lib/private/{frontend → client}/ViewResponse.d.ts +1 -1
  36. package/lib/private/client/app.js +1 -2
  37. package/lib/private/client/boot.d.ts +5 -0
  38. package/lib/private/client/boot.js +64 -0
  39. package/lib/private/client/create-form.d.ts +1 -0
  40. package/lib/private/client/create-form.js +19 -20
  41. package/lib/private/client/extract-issues.js +2 -1
  42. package/lib/private/client/http.d.ts +13 -0
  43. package/lib/private/client/http.js +57 -0
  44. package/lib/private/client/index.d.ts +23 -0
  45. package/lib/private/client/index.js +16 -0
  46. package/lib/private/client/navigate.d.ts +13 -0
  47. package/lib/private/client/navigate.js +67 -0
  48. package/lib/private/client/root.d.ts +9 -0
  49. package/lib/private/client/root.js +11 -0
  50. package/lib/private/client/submit.d.ts +2 -0
  51. package/lib/private/client/submit.js +41 -0
  52. package/lib/private/config/index.d.ts +7 -2
  53. package/lib/private/config/index.js +3 -2
  54. package/lib/private/config/schema.d.ts +82 -26
  55. package/lib/private/config/schema.js +17 -4
  56. package/lib/private/cookie.d.ts +12 -6
  57. package/lib/private/db/DB.d.ts +21 -5
  58. package/lib/private/db/DB.js +1 -0
  59. package/lib/private/db/MemoryDB.d.ts +2 -4
  60. package/lib/private/db/MemoryDB.js +40 -22
  61. package/lib/private/db/common.d.ts +7 -0
  62. package/lib/private/db/common.js +31 -0
  63. package/lib/private/db/errors.d.ts +104 -0
  64. package/lib/private/db/errors.js +237 -0
  65. package/lib/private/db/migrate/apply.d.ts +2 -0
  66. package/lib/private/db/migrate/apply.js +32 -0
  67. package/lib/private/db/migrate/bundle.d.ts +3 -0
  68. package/lib/private/db/migrate/bundle.js +22 -0
  69. package/lib/private/db/migrate/create.d.ts +2 -0
  70. package/lib/private/db/migrate/create.js +154 -0
  71. package/lib/private/db/migrate/index.d.ts +10 -0
  72. package/lib/private/db/migrate/index.js +6 -0
  73. package/lib/private/db/migrate/status.d.ts +2 -0
  74. package/lib/private/db/migrate/status.js +38 -0
  75. package/lib/private/db/migrate/store.d.ts +5 -0
  76. package/lib/private/db/migrate/store.js +33 -0
  77. package/lib/private/db/sql.js +3 -3
  78. package/lib/private/db/test.js +461 -95
  79. package/lib/private/errors.d.ts +88 -0
  80. package/lib/private/errors.js +211 -0
  81. package/lib/private/frontend.d.ts +72 -0
  82. package/lib/private/frontend.js +245 -0
  83. package/lib/private/i18n/Formatter.js +2 -2
  84. package/lib/private/i18n/errors.d.ts +16 -0
  85. package/lib/private/i18n/errors.js +27 -0
  86. package/lib/private/i18n/module.d.ts +3 -0
  87. package/lib/private/i18n/module.js +115 -0
  88. package/lib/private/i18n/schema.d.ts +10 -5
  89. package/lib/private/i18n/validate.js +5 -7
  90. package/lib/private/index.d.ts +14 -0
  91. package/lib/private/index.js +2 -0
  92. package/lib/private/log.js +6 -5
  93. package/lib/private/module/Setup.d.ts +21 -0
  94. package/lib/private/module/Setup.js +2 -0
  95. package/lib/private/module/create.d.ts +16 -0
  96. package/lib/private/module/create.js +28 -0
  97. package/lib/private/orm/ExtractSchema.d.ts +9 -0
  98. package/lib/private/orm/ExtractSchema.js +2 -0
  99. package/lib/private/orm/ForeignKey.d.ts +5 -2
  100. package/lib/private/orm/ForeignKey.js +3 -0
  101. package/lib/private/orm/PrimaryKey.d.ts +5 -3
  102. package/lib/private/orm/PrimaryKey.js +9 -6
  103. package/lib/private/orm/StoreInput.d.ts +10 -0
  104. package/lib/private/orm/StoreInput.js +2 -0
  105. package/lib/private/orm/key.d.ts +6 -6
  106. package/lib/private/orm/key.js +7 -3
  107. package/lib/private/orm/parse.d.ts +5 -4
  108. package/lib/private/orm/parse.js +2 -2
  109. package/lib/private/orm/relation.d.ts +2 -2
  110. package/lib/private/orm/{Store.d.ts → store.d.ts} +33 -17
  111. package/lib/private/orm/{Store.js → store.js} +91 -67
  112. package/lib/private/paths.js +3 -3
  113. package/lib/private/request/RequestBag.d.ts +4 -0
  114. package/lib/private/request/RequestBag.js +5 -2
  115. package/lib/private/request/RequestBody.d.ts +6 -9
  116. package/lib/private/request/RequestBody.js +49 -54
  117. package/lib/private/request/RequestFacade.d.ts +3 -8
  118. package/lib/private/request/RequestPublic.d.ts +9 -0
  119. package/lib/private/request/RequestPublic.js +2 -0
  120. package/lib/private/request/RequestView.d.ts +11 -0
  121. package/lib/private/request/RequestView.js +3 -0
  122. package/lib/private/request/handle.d.ts +4 -0
  123. package/lib/private/request/handle.js +18 -0
  124. package/lib/private/request/parse.js +1 -0
  125. package/lib/private/request/route.js +8 -9
  126. package/lib/private/request/router.js +21 -49
  127. package/lib/private/request/storage.d.ts +4 -0
  128. package/lib/private/request/storage.js +5 -0
  129. package/lib/private/response/ResponseFunction.d.ts +1 -1
  130. package/lib/private/response/binary.js +1 -1
  131. package/lib/private/response/error.d.ts +1 -1
  132. package/lib/private/response/error.js +1 -1
  133. package/lib/private/response/json.d.ts +1 -1
  134. package/lib/private/response/json.js +1 -1
  135. package/lib/private/response/redirect.d.ts +5 -5
  136. package/lib/private/response/redirect.js +8 -9
  137. package/lib/private/response/respond.js +9 -7
  138. package/lib/private/response/sse.d.ts +1 -1
  139. package/lib/private/response/sse.js +1 -1
  140. package/lib/private/response/text.d.ts +1 -1
  141. package/lib/private/response/text.js +1 -1
  142. package/lib/private/response/view.d.ts +1 -1
  143. package/lib/private/response/view.js +6 -13
  144. package/lib/private/response/ws.d.ts +1 -1
  145. package/lib/private/route/router.d.ts +3 -3
  146. package/lib/private/route/router.js +7 -10
  147. package/lib/private/serve/App.d.ts +6 -7
  148. package/lib/private/serve/App.js +52 -43
  149. package/lib/private/serve/Init.d.ts +2 -2
  150. package/lib/private/serve/dev-module.d.ts +2 -0
  151. package/lib/private/serve/dev-module.js +34 -0
  152. package/lib/private/serve/hook.d.ts +1 -2
  153. package/lib/private/serve/hook.js +2 -3
  154. package/lib/private/serve/index.d.ts +1 -1
  155. package/lib/private/serve/index.js +32 -2
  156. package/lib/private/server/TAG.d.ts +3 -0
  157. package/lib/private/server/TAG.js +2 -0
  158. package/lib/private/server/index.d.ts +5 -0
  159. package/lib/private/server/index.js +6 -0
  160. package/lib/private/session/SessionHandle.js +2 -1
  161. package/lib/private/session/index.d.ts +1 -1
  162. package/lib/private/session/module.d.ts +3 -0
  163. package/lib/private/session/module.js +114 -0
  164. package/lib/private/session/schema.d.ts +17 -9
  165. package/lib/private/session/schema.js +9 -5
  166. package/lib/private/target/Manager.js +6 -12
  167. package/lib/public/client.d.ts +2 -13
  168. package/lib/public/client.js +1 -9
  169. package/lib/public/db/errors.d.ts +2 -0
  170. package/lib/public/db/errors.js +2 -0
  171. package/lib/public/db/migrate.d.ts +2 -0
  172. package/lib/public/db/migrate.js +2 -0
  173. package/lib/public/db.d.ts +3 -3
  174. package/lib/public/frontend.d.ts +3 -0
  175. package/lib/public/frontend.js +2 -0
  176. package/lib/public/index.d.ts +2 -0
  177. package/lib/public/index.js +2 -0
  178. package/lib/public/orm/store.d.ts +2 -0
  179. package/lib/public/orm/store.js +2 -0
  180. package/lib/public/request/server.d.ts +5 -0
  181. package/lib/public/request/server.js +7 -0
  182. package/lib/public/response.d.ts +4 -4
  183. package/lib/public/server.d.ts +3 -0
  184. package/lib/public/server.js +2 -0
  185. package/package.json +30 -25
  186. package/lib/private/AppError.d.ts +0 -4
  187. package/lib/private/AppError.js +0 -8
  188. package/lib/private/backend/Module.d.ts +0 -18
  189. package/lib/private/backend/Module.js +0 -19
  190. package/lib/private/backend/TAG.d.ts +0 -3
  191. package/lib/private/backend/TAG.js +0 -2
  192. package/lib/private/build/server/plugin/db-default.d.ts +0 -4
  193. package/lib/private/build/server/plugin/db-default.js +0 -45
  194. package/lib/private/build/server/plugin/store-wrap.d.ts +0 -4
  195. package/lib/private/build/server/plugin/store-wrap.js +0 -33
  196. package/lib/private/client/spa/index.d.ts +0 -6
  197. package/lib/private/client/spa/index.js +0 -200
  198. package/lib/private/db/error.d.ts +0 -81
  199. package/lib/private/db/error.js +0 -199
  200. package/lib/private/db/symbol/wrap.d.ts +0 -3
  201. package/lib/private/db/symbol/wrap.js +0 -2
  202. package/lib/private/fail.d.ts +0 -3
  203. package/lib/private/fail.js +0 -5
  204. package/lib/private/frontend/Module.d.ts +0 -62
  205. package/lib/private/frontend/Module.js +0 -255
  206. package/lib/private/i18n/Module.d.ts +0 -16
  207. package/lib/private/i18n/Module.js +0 -133
  208. package/lib/private/module/BuildHook.d.ts +0 -5
  209. package/lib/private/module/BuildHook.js +0 -2
  210. package/lib/private/module/NextBuild.d.ts +0 -5
  211. package/lib/private/module/NextBuild.js +0 -2
  212. package/lib/private/module/NextServe.d.ts +0 -5
  213. package/lib/private/module/NextServe.js +0 -2
  214. package/lib/private/orm/Set.d.ts +0 -11
  215. package/lib/private/orm/Set.js +0 -2
  216. package/lib/private/orm/foreign.d.ts +0 -4
  217. package/lib/private/orm/foreign.js +0 -5
  218. package/lib/private/orm/primary.d.ts +0 -5
  219. package/lib/private/orm/primary.js +0 -5
  220. package/lib/private/orm/types.d.ts +0 -18
  221. package/lib/private/orm/types.js +0 -2
  222. package/lib/private/orm/wrap.d.ts +0 -5
  223. package/lib/private/orm/wrap.js +0 -5
  224. package/lib/private/reducer.d.ts +0 -24
  225. package/lib/private/reducer.js +0 -10
  226. package/lib/private/serve/module/Dev.d.ts +0 -11
  227. package/lib/private/serve/module/Dev.js +0 -32
  228. package/lib/private/serve/module/Handle.d.ts +0 -10
  229. package/lib/private/serve/module/Handle.js +0 -15
  230. package/lib/private/session/SessionModule.d.ts +0 -14
  231. package/lib/private/session/SessionModule.js +0 -122
  232. package/lib/public/App.d.ts +0 -2
  233. package/lib/public/App.js +0 -2
  234. package/lib/public/AppError.d.ts +0 -2
  235. package/lib/public/AppError.js +0 -2
  236. package/lib/public/BuildApp.d.ts +0 -2
  237. package/lib/public/BuildApp.js +0 -2
  238. package/lib/public/BuildHook.d.ts +0 -2
  239. package/lib/public/BuildHook.js +0 -2
  240. package/lib/public/Mode.d.ts +0 -2
  241. package/lib/public/Mode.js +0 -2
  242. package/lib/public/Module.d.ts +0 -2
  243. package/lib/public/Module.js +0 -2
  244. package/lib/public/Next.d.ts +0 -2
  245. package/lib/public/Next.js +0 -2
  246. package/lib/public/NextBuild.d.ts +0 -2
  247. package/lib/public/NextBuild.js +0 -2
  248. package/lib/public/NextHandle.d.ts +0 -2
  249. package/lib/public/NextHandle.js +0 -2
  250. package/lib/public/NextRoute.d.ts +0 -3
  251. package/lib/public/NextRoute.js +0 -2
  252. package/lib/public/NextServe.d.ts +0 -2
  253. package/lib/public/NextServe.js +0 -2
  254. package/lib/public/ServeApp.d.ts +0 -2
  255. package/lib/public/ServeApp.js +0 -2
  256. package/lib/public/Target.d.ts +0 -2
  257. package/lib/public/Target.js +0 -2
  258. package/lib/public/backend/Module.d.ts +0 -2
  259. package/lib/public/backend/Module.js +0 -2
  260. package/lib/public/backend/TAG.d.ts +0 -2
  261. package/lib/public/backend/TAG.js +0 -2
  262. package/lib/public/client/Data.d.ts +0 -2
  263. package/lib/public/client/Data.js +0 -2
  264. package/lib/public/client/spa.d.ts +0 -2
  265. package/lib/public/client/spa.js +0 -2
  266. package/lib/public/db/error.d.ts +0 -2
  267. package/lib/public/db/error.js +0 -2
  268. package/lib/public/fail.d.ts +0 -2
  269. package/lib/public/fail.js +0 -2
  270. package/lib/public/frontend/Module.d.ts +0 -2
  271. package/lib/public/frontend/Module.js +0 -2
  272. package/lib/public/frontend/Publish.d.ts +0 -2
  273. package/lib/public/frontend/Publish.js +0 -2
  274. package/lib/public/frontend/Render.d.ts +0 -2
  275. package/lib/public/frontend/Render.js +0 -2
  276. package/lib/public/frontend/ViewResponse.d.ts +0 -2
  277. package/lib/public/frontend/ViewResponse.js +0 -2
  278. package/lib/public/orm/Store.d.ts +0 -2
  279. package/lib/public/orm/Store.js +0 -2
  280. package/lib/public/orm/wrap.d.ts +0 -2
  281. package/lib/public/orm/wrap.js +0 -2
  282. package/lib/public/request.d.ts +0 -4
  283. package/lib/public/request.js +0 -2
  284. /package/lib/private/{frontend → client}/Publish.d.ts +0 -0
  285. /package/lib/private/{frontend → client}/Publish.js +0 -0
  286. /package/lib/private/{frontend → client}/Render.js +0 -0
  287. /package/lib/private/{frontend → client}/ServerData.d.ts +0 -0
  288. /package/lib/private/{frontend → client}/ServerData.js +0 -0
  289. /package/lib/private/{frontend → client}/ServerView.d.ts +0 -0
  290. /package/lib/private/{frontend → client}/ServerView.js +0 -0
  291. /package/lib/private/{frontend → client}/View.d.ts +0 -0
  292. /package/lib/private/{frontend → client}/View.js +0 -0
  293. /package/lib/private/{frontend → client}/ViewOptions.d.ts +0 -0
  294. /package/lib/private/{frontend → client}/ViewOptions.js +0 -0
  295. /package/lib/private/{frontend → client}/ViewResponse.js +0 -0
  296. /package/lib/private/client/{spa/storage.d.ts → storage.d.ts} +0 -0
  297. /package/lib/private/client/{spa/storage.js → storage.js} +0 -0
@@ -0,0 +1,34 @@
1
+ import log from "#log";
2
+ import create from "#module/create";
3
+ import c from "@rcompat/cli/color";
4
+ function pass(address, request) {
5
+ return fetch(address, {
6
+ body: request.body,
7
+ duplex: "half",
8
+ headers: request.headers,
9
+ method: request.method,
10
+ });
11
+ }
12
+ export default function dev() {
13
+ let paths;
14
+ let reload;
15
+ return create({
16
+ name: "builtin/dev",
17
+ setup({ onServe, onHandle }) {
18
+ onServe(app => {
19
+ const { host, port } = app.livereload;
20
+ const assets = app.assets.map(asset => asset.src);
21
+ reload = `http://${host}:${port}`;
22
+ paths = ["/esbuild"].concat(assets);
23
+ log.print(`↻ live reload ${c.dim(reload)}\n`);
24
+ });
25
+ onHandle((request, next) => {
26
+ const { pathname } = new URL(request.url);
27
+ return paths.includes(pathname)
28
+ ? pass(`${reload}${pathname}`, request.original)
29
+ : next(request);
30
+ });
31
+ },
32
+ });
33
+ }
34
+ //# sourceMappingURL=dev-module.js.map
@@ -1,4 +1,3 @@
1
1
  import type ServeApp from "#serve/App";
2
- declare const _default: (app: ServeApp) => Promise<ServeApp>;
3
- export default _default;
2
+ export default function run_serve_hooks(app: ServeApp): Promise<ServeApp>;
4
3
  //# sourceMappingURL=hook.d.ts.map
@@ -1,7 +1,6 @@
1
- import reducer from "#reducer";
2
- async function post(app) {
1
+ export default async function run_serve_hooks(app) {
2
+ await app.serve_hooks(app);
3
3
  await app.start();
4
4
  return app;
5
5
  }
6
- export default async (app) => post(await reducer(app.modules, app, "serve"));
7
6
  //# sourceMappingURL=hook.js.map
@@ -1,5 +1,5 @@
1
1
  import ServeApp from "#serve/App";
2
2
  import type ServeInit from "#serve/Init";
3
- declare const _default: (root: string, options: ServeInit) => Promise<ServeApp>;
3
+ declare const _default: (root: string, options: ServeInit) => Promise<ServeApp | undefined>;
4
4
  export default _default;
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1,10 +1,40 @@
1
- import { s_attach } from "#app/Facade";
1
+ import { s_attach, s_config } from "#app/Facade";
2
+ import E from "#db/errors";
3
+ import log from "#log";
4
+ import key from "#orm/key";
5
+ import store from "#orm/store";
2
6
  import ServeApp from "#serve/App";
3
7
  import serve_hook from "#serve/hook";
8
+ import p from "pema";
4
9
  export default async (root, options) => {
5
10
  const facade = options.facade;
6
11
  const app = await new ServeApp(root, options).init();
7
12
  facade[s_attach](app);
8
- return serve_hook(app);
13
+ const config = facade[s_config];
14
+ const migrations = config.db.migrations;
15
+ try {
16
+ if (migrations !== undefined) {
17
+ const { table, db } = migrations;
18
+ const Migration = store({
19
+ name: table,
20
+ db,
21
+ schema: {
22
+ id: key.primary(p.u16, { generate: false }),
23
+ applied: p.date,
24
+ },
25
+ });
26
+ await Migration.table.create();
27
+ const last = await Migration.find({ limit: 1, sort: { id: "desc" } });
28
+ const last_id = last.length === 0 ? 0 : last[0].id;
29
+ const build_json = app.root.join("build.json");
30
+ const { migration_version } = await build_json.json();
31
+ if (last_id < migration_version)
32
+ throw E.unapplied_migrations();
33
+ }
34
+ return serve_hook(app);
35
+ }
36
+ catch (cause) {
37
+ log.error(cause);
38
+ }
9
39
  };
10
40
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ declare const _default: "0.5";
2
+ export default _default;
3
+ //# sourceMappingURL=TAG.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default "0.5";
2
+ //# sourceMappingURL=TAG.js.map
@@ -0,0 +1,5 @@
1
+ declare const server: {
2
+ TAG: string;
3
+ };
4
+ export default server;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,6 @@
1
+ import TAG from "#server/TAG";
2
+ const server = {
3
+ TAG,
4
+ };
5
+ export default server;
6
+ //# sourceMappingURL=index.js.map
@@ -1,3 +1,4 @@
1
+ import E from "#errors";
1
2
  import kSerialize from "#session/k-serialize";
2
3
  import assert from "@rcompat/assert";
3
4
  import is from "@rcompat/is";
@@ -11,7 +12,7 @@ export default class SessionHandle {
11
12
  assert.maybe.dict(data);
12
13
  assert.maybe.object(schema);
13
14
  assert.maybe.function(schema?.parse);
14
- assert.true((id === undefined) === (data === undefined), "both `id` and `data` must be defined or undefined");
15
+ assert.true((id === undefined) === (data === undefined), E.session_id_data());
15
16
  this.#id = id;
16
17
  this.#data = data;
17
18
  this.#schema = schema?.parse
@@ -1,4 +1,4 @@
1
- import type Store from "#orm/Store";
1
+ import type { Store } from "#orm/store";
2
2
  import configSchema from "#session/schema";
3
3
  import type SessionFacade from "#session/SessionFacade";
4
4
  import type { InferStore, StoreSchema } from "pema";
@@ -0,0 +1,3 @@
1
+ import type Config from "#session/Config";
2
+ export default function session_module(config: Config): import("#module/create").Created;
3
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1,114 @@
1
+ import E from "#errors";
2
+ import create from "#module/create";
3
+ import kSerialize from "#session/k-serialize";
4
+ import SessionHandle from "#session/SessionHandle";
5
+ import storage from "#session/storage";
6
+ import p from "pema";
7
+ function cookie(name, value, options) {
8
+ const parts = [`${name}=${value}`];
9
+ parts.push(`Path=${options.path}`);
10
+ parts.push(`SameSite=${options.sameSite}`);
11
+ if (options.maxAge !== undefined)
12
+ parts.push(`Max-Age=${options.maxAge}`);
13
+ if (options.httpOnly)
14
+ parts.push("HttpOnly");
15
+ if (options.secure)
16
+ parts.push("Secure");
17
+ return parts.join("; ");
18
+ }
19
+ ;
20
+ export default function session_module(config) {
21
+ let secure;
22
+ const store = config.store;
23
+ const props = store.type.properties;
24
+ if (!("session_id" in props))
25
+ throw E.session_missing_id();
26
+ try {
27
+ props.session_id.parse(crypto.randomUUID());
28
+ }
29
+ catch {
30
+ throw E.session_id_string();
31
+ }
32
+ return create({
33
+ name: "builtin/session",
34
+ setup({ onServe, onHandle }) {
35
+ onServe(async (app) => {
36
+ secure = app.secure;
37
+ await store.table.create();
38
+ });
39
+ onHandle(async (request, next) => {
40
+ const { name, ...cookie_config } = config.cookie;
41
+ const sid = request.cookies.try(name);
42
+ // Look up session by session_id
43
+ const existing = sid !== undefined
44
+ ? await store.find({ where: { session_id: sid }, limit: 1 })
45
+ : [];
46
+ const exists = existing.length > 0;
47
+ let data = undefined;
48
+ let db_id = undefined;
49
+ if (exists) {
50
+ const record = existing[0];
51
+ const { id: _id, session_id: _sid, ...rest } = record;
52
+ data = rest;
53
+ db_id = _id;
54
+ }
55
+ const session_type = p.omit(store.type, "id", "session_id");
56
+ const session = new SessionHandle(sid, data, session_type);
57
+ const response = await new Promise((resolve, reject) => {
58
+ storage().run(session, async () => {
59
+ try {
60
+ resolve(await next(request));
61
+ }
62
+ catch (e) {
63
+ reject(e);
64
+ }
65
+ });
66
+ });
67
+ const snap = session[kSerialize]();
68
+ // no cookie coming in, no session created
69
+ if (sid === undefined && !snap.exists)
70
+ return response;
71
+ const options = { ...cookie_config, secure };
72
+ // no session existed -> either create or noop
73
+ if (!exists) {
74
+ if (!snap.exists) {
75
+ // stale cookie -> clear
76
+ if (sid !== undefined) {
77
+ response.headers.append("set-cookie", cookie(name, "", {
78
+ ...options, maxAge: 0,
79
+ }));
80
+ }
81
+ return response;
82
+ }
83
+ // create new session
84
+ await store.insert({ session_id: snap.id, ...snap.data });
85
+ response.headers.append("set-cookie", cookie(name, snap.id, options));
86
+ return response;
87
+ }
88
+ // from here: session existed (db_id is defined)
89
+ // fast-path: session exists, same id and not dirty -> noop
90
+ if (snap.exists && snap.id === sid && !snap.dirty)
91
+ return response;
92
+ // current absent -> destroy + clear cookie
93
+ if (!snap.exists) {
94
+ await store.delete(db_id);
95
+ response.headers.append("set-cookie", cookie(name, "", {
96
+ ...options, maxAge: 0,
97
+ }));
98
+ return response;
99
+ }
100
+ // session recreated in route -> destroy old, create new, set cookie
101
+ if (snap.id !== sid) {
102
+ await store.delete(db_id);
103
+ await store.insert({ session_id: snap.id, ...snap.data });
104
+ response.headers.append("set-cookie", cookie(name, snap.id, options));
105
+ return response;
106
+ }
107
+ // dirty -> replace session contents
108
+ await store.update(db_id, { set: snap.data });
109
+ return response;
110
+ });
111
+ },
112
+ });
113
+ }
114
+ //# sourceMappingURL=module.js.map
@@ -1,12 +1,20 @@
1
- import Store from "#orm/Store";
2
- declare const _default: import("pema").ObjectType<{
3
- cookie: import("pema").ObjectType<{
4
- httpOnly: import("pema").DefaultType<import("pema").BooleanType, true>;
5
- name: import("pema").DefaultType<import("pema").StringType, "session_id">;
6
- path: import("pema").DefaultType<import("pema").StringType, "/">;
7
- sameSite: import("pema").DefaultType<import("pema").UnionType<[import("pema").LiteralType<"Strict">, import("pema").LiteralType<"Lax">, import("pema").LiteralType<"None">]>, "Lax" | "None" | "Strict">;
8
- }>;
9
- store: import("pema").DefaultType<import("pema").ConstructorType<typeof Store>, Store<import("../orm/types.js").StoreInput, import("@rcompat/type").Dict<import("../orm/relation.js").Relation>>>;
1
+ import { Store } from "#orm/store";
2
+ declare const _default: import("pema").ObjectType<import("pema").NormalizeSchemaObject<{
3
+ readonly cookie: {
4
+ readonly httpOnly: import("pema").DefaultType<import("pema").BooleanType, true>;
5
+ readonly name: import("pema").DefaultType<import("pema").StringType, "session_id">;
6
+ readonly path: import("pema").DefaultType<import("pema").StringType, "/">;
7
+ readonly sameSite: import("pema").DefaultType<import("pema").UnionType<[import("pema").LiteralType<"Strict">, import("pema").LiteralType<"Lax">, import("pema").LiteralType<"None">]>, "Lax" | "None" | "Strict">;
8
+ };
9
+ readonly store: import("pema").DefaultType<import("pema").ConstructorType<typeof Store>, Store<import("../orm/StoreInput.js").default, import("@rcompat/type").Dict<import("../orm/relation.js").Relation>>>;
10
+ }>, {
11
+ cookie: {
12
+ httpOnly: boolean;
13
+ name: string;
14
+ path: string;
15
+ sameSite: "Lax" | "None" | "Strict";
16
+ };
17
+ store: Store<import("../orm/StoreInput.js").default, import("@rcompat/type").Dict<import("../orm/relation.js").Relation>>;
10
18
  }>;
11
19
  export default _default;
12
20
  //# sourceMappingURL=schema.d.ts.map
@@ -1,6 +1,6 @@
1
1
  import MemoryDB from "#db/MemoryDB";
2
2
  import key from "#orm/key";
3
- import Store from "#orm/Store";
3
+ import store, { Store } from "#orm/store";
4
4
  import p from "pema";
5
5
  export default p({
6
6
  cookie: {
@@ -10,10 +10,14 @@ export default p({
10
10
  sameSite: p.union("Strict", "Lax", "None").default("Lax"),
11
11
  },
12
12
  store: p.constructor(Store).default(() => {
13
- return new Store({
14
- id: key.primary(p.string),
15
- session_id: p.string.uuid(),
16
- }, { db: new MemoryDB(), name: "session" });
13
+ return store({
14
+ schema: {
15
+ id: key.primary(p.uuid),
16
+ session_id: p.uuid,
17
+ },
18
+ db: new MemoryDB(),
19
+ name: "session",
20
+ });
17
21
  }),
18
22
  });
19
23
  //# sourceMappingURL=schema.js.map
@@ -1,4 +1,4 @@
1
- import fail from "#fail";
1
+ import E from "#errors";
2
2
  const web = {
3
3
  name: "web",
4
4
  runner: () => { },
@@ -25,20 +25,14 @@ export default class TargetManager {
25
25
  return this.#targets.find(target => target.name === this.#name);
26
26
  }
27
27
  set(name) {
28
- if (!this.has(name)) {
29
- let message = "no target {0}, available targets {1}";
30
- if (this.#targets.length === 1) {
31
- message += "\n - add {2} for more targets";
32
- }
33
- const targets = this.#targets.map(p => p.name).join(", ");
34
- throw fail(message, name, targets, "@primate/native");
35
- }
28
+ const targets = this.#targets;
29
+ if (!this.has(name))
30
+ throw E.target_missing(name, targets.map(p => p.name));
36
31
  this.#name = name;
37
32
  }
38
33
  add(target) {
39
- if (this.has(target.name)) {
40
- throw fail("cannot add target {0} twice", target.name);
41
- }
34
+ if (this.has(target.name))
35
+ throw E.target_duplicate(target.name);
42
36
  this.#targets.push(target);
43
37
  }
44
38
  async run() {
@@ -1,14 +1,3 @@
1
- import createForm, { type FormInit, type FormView } from "#client/create-form";
2
- import toValidated from "#client/to-validated";
3
- import validateField from "#client/validate-field";
4
- import type ValidateInit from "#client/ValidateInit";
5
- import type ValidateUpdater from "#client/ValidateUpdater";
6
- import type ValidationError from "#client/ValidationError";
7
- declare const client: {
8
- createForm: typeof createForm;
9
- validateField: typeof validateField;
10
- toValidated: typeof toValidated;
11
- };
12
- export default client;
13
- export type { FormInit, FormView, ValidateInit, ValidateUpdater, ValidationError, };
1
+ export type * from "#client/index";
2
+ export { default } from "#client/index";
14
3
  //# sourceMappingURL=client.d.ts.map
@@ -1,10 +1,2 @@
1
- import createForm from "#client/create-form";
2
- import toValidated from "#client/to-validated";
3
- import validateField from "#client/validate-field";
4
- const client = {
5
- createForm, // headless form controller
6
- validateField, // low-level single-value transport
7
- toValidated, // adapter to create field stores/hooks
8
- };
9
- export default client;
1
+ export { default } from "#client/index";
10
2
  //# sourceMappingURL=client.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#db/errors";
2
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#db/errors";
2
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#db/migrate/index";
2
+ //# sourceMappingURL=migrate.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#db/migrate/index";
2
+ //# sourceMappingURL=migrate.js.map
@@ -1,12 +1,12 @@
1
1
  export type { default as As } from "#db/As";
2
2
  export type { default as DataDict } from "#db/DataDict";
3
- export type { default as DB } from "#db/DB";
3
+ export type { default as DB, Schema } from "#db/DB";
4
4
  export type { default as PK } from "#db/PK";
5
+ export type { default as ReadArgs } from "#db/ReadArgs";
6
+ export type { default as ReadRelationsArgs } from "#db/ReadRelationsArgs";
5
7
  export type { default as Sort } from "#db/Sort";
6
8
  export type { default as TypeMap } from "#db/TypeMap";
7
9
  export type { default as Types } from "#db/Types";
8
10
  export type { default as With } from "#db/With";
9
- export type { default as ReadArgs } from "#db/ReadArgs";
10
- export type { default as ReadRelationsArgs } from "#db/ReadRelationsArgs";
11
11
  export { default } from "#db/common";
12
12
  //# sourceMappingURL=db.d.ts.map
@@ -0,0 +1,3 @@
1
+ export { default } from "#frontend";
2
+ export type * from "#frontend";
3
+ //# sourceMappingURL=frontend.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#frontend";
2
+ //# sourceMappingURL=frontend.js.map
@@ -0,0 +1,2 @@
1
+ export type * from "#index";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#orm/store";
2
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#orm/store";
2
+ //# sourceMappingURL=store.js.map
@@ -0,0 +1,5 @@
1
+ declare const _default: {
2
+ get(): import("../index.js").RequestView | undefined;
3
+ };
4
+ export default _default;
5
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1,7 @@
1
+ import storage from "#request/storage";
2
+ export default {
3
+ get() {
4
+ return storage().getStore()?.toJSON();
5
+ },
6
+ };
7
+ //# sourceMappingURL=server.js.map
@@ -6,17 +6,17 @@ export type { default as ResponseLike } from "#response/ResponseLike";
6
6
  declare const _default: {
7
7
  binary: typeof binary;
8
8
  error: typeof error;
9
- json: (body: unknown, init?: ResponseInit) => (app: import("./ServeApp.js").default) => import("@rcompat/type").MaybePromise<Response>;
9
+ json: (body: unknown, init?: ResponseInit) => (app: import("./index.js").ServeApp) => import("@rcompat/type").MaybePromise<Response>;
10
10
  redirect: (location: string, status?: 302 | 301 | 300 | 304 | 308 | 303 | 307) => import("#response/ResponseFunction").default;
11
11
  sse: (body: {
12
12
  close?(): undefined;
13
13
  open(events: {
14
14
  send(name: string, data: unknown): undefined;
15
15
  }): undefined;
16
- }, init?: ResponseInit) => (app: import("./ServeApp.js").default) => import("@rcompat/type").MaybePromise<Response>;
17
- text: (body: string, init?: ResponseInit) => (app: import("./ServeApp.js").default) => import("@rcompat/type").MaybePromise<Response>;
16
+ }, init?: ResponseInit) => (app: import("./index.js").ServeApp) => import("@rcompat/type").MaybePromise<Response>;
17
+ text: (body: string, init?: ResponseInit) => (app: import("./index.js").ServeApp) => import("@rcompat/type").MaybePromise<Response>;
18
18
  view: typeof view;
19
- ws: (actions: import("@rcompat/http/Actions").default) => import("#response/ResponseFunction").default;
19
+ ws: (actions: import("@rcompat/http").Actions) => import("#response/ResponseFunction").default;
20
20
  };
21
21
  export default _default;
22
22
  //# sourceMappingURL=response.d.ts.map
@@ -0,0 +1,3 @@
1
+ export type * from "#server/index";
2
+ export { default } from "#server/index";
3
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1,2 @@
1
+ export { default } from "#server/index";
2
+ //# sourceMappingURL=server.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primate/core",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "The universal web framework",
5
5
  "homepage": "https://primate.run",
6
6
  "bugs": "https://github.com/primate-run/primate/issues",
@@ -18,49 +18,54 @@
18
18
  "!/**/*.spec.*"
19
19
  ],
20
20
  "dependencies": {
21
- "@rcompat/args": "^0.11.0",
22
- "@rcompat/assert": "^0.6.0",
23
- "@rcompat/cli": "^0.14.0",
24
- "@rcompat/crypto": "^0.14.0",
25
- "@rcompat/dict": "^0.3.1",
26
- "@rcompat/fn": "^0.3.0",
27
- "@rcompat/fs": "^0.25.2",
28
- "@rcompat/http": "^0.21.0",
29
- "@rcompat/io": "^0.3.0",
30
- "@rcompat/is": "^0.4.2",
31
- "@rcompat/kv": "^0.6.0",
32
- "@rcompat/runtime": "^0.9.0",
33
- "@rcompat/string": "^0.13.0",
34
- "@rcompat/type": "^0.9.0",
35
- "esbuild": "^0.27.3",
36
- "pema": "^0.5.0"
21
+ "@rcompat/assert": "^0.8.1",
22
+ "@rcompat/cli": "^0.18.1",
23
+ "@rcompat/crypto": "^0.16.1",
24
+ "@rcompat/dict": "^0.5.1",
25
+ "@rcompat/env": "^0.17.2",
26
+ "@rcompat/error": "^0.3.1",
27
+ "@rcompat/fn": "^0.5.1",
28
+ "@rcompat/fs": "^0.28.1",
29
+ "@rcompat/http": "^0.25.1",
30
+ "@rcompat/io": "^0.5.1",
31
+ "@rcompat/is": "^0.6.1",
32
+ "@rcompat/kv": "^0.8.1",
33
+ "@rcompat/runtime": "^0.11.1",
34
+ "@rcompat/string": "^0.16.1",
35
+ "@rcompat/type": "^0.11.1",
36
+ "esbuild": "^0.28.0",
37
+ "pema": "^0.6.0"
37
38
  },
38
39
  "imports": {
39
40
  "#i18n/index": {
40
- "apekit": "./src/private/i18n/index/client.ts",
41
+ "@primate/source": "./src/private/i18n/index/client.ts",
41
42
  "browser": "./lib/private/i18n/index/client.js",
42
43
  "default": "./lib/private/i18n/index/server.js"
43
44
  },
44
45
  "#*": {
45
- "apekit": "./src/private/*.ts",
46
+ "@primate/source": "./src/private/*.ts",
46
47
  "default": "./lib/private/*.js"
47
48
  }
48
49
  },
49
50
  "exports": {
50
- "./error": "./lib/error/index.js",
51
- "./dispatch": "./lib/dispatch/index.js",
52
- "./frontend": {
53
- "apekit": "./src/public/Frontend.ts"
51
+ "./AppFacade": {
52
+ "@primate/source": "./src/public/AppFacade.ts",
53
+ "browser": "./lib/private/app/Facade.browser.js",
54
+ "default": "./lib/public/AppFacade.js"
54
55
  },
55
56
  "./*": {
56
- "apekit": "./src/public/*.ts",
57
+ "@primate/source": "./src/public/*.ts",
57
58
  "default": "./lib/public/*.js"
59
+ },
60
+ ".": {
61
+ "@primate/source": "./src/public/index.ts",
62
+ "default": "./lib/public/index.js"
58
63
  }
59
64
  },
60
65
  "scripts": {
61
66
  "build": "npm run clean && tsc && cp -a src/private/defaults lib/private",
62
67
  "clean": "rm -rf ./lib",
63
68
  "lint": "eslint .",
64
- "test": "npm run build && npx proby"
69
+ "test": "npx proby"
65
70
  }
66
71
  }
@@ -1,4 +0,0 @@
1
- export default class AppError extends Error {
2
- constructor(message: string, ...params: unknown[]);
3
- }
4
- //# sourceMappingURL=AppError.d.ts.map
@@ -1,8 +0,0 @@
1
- import mark from "@rcompat/cli/mark";
2
- export default class AppError extends Error {
3
- constructor(message, ...params) {
4
- super(mark(message, ...params));
5
- }
6
- }
7
- ;
8
- //# sourceMappingURL=AppError.js.map
@@ -1,18 +0,0 @@
1
- import Module from "#Module";
2
- export default abstract class BackendModule extends Module {
3
- #private;
4
- abstract defaultExtension: string;
5
- static schema: import("pema").ObjectType<{
6
- fileExtension: import("pema").OptionalType<import("pema").StringType>;
7
- }>;
8
- static options: {
9
- fileExtension: string | undefined;
10
- };
11
- static input: {
12
- fileExtension?: string | undefined;
13
- } | undefined;
14
- constructor(options?: typeof BackendModule.schema.input);
15
- get fileExtension(): string;
16
- get package(): string;
17
- }
18
- //# sourceMappingURL=Module.d.ts.map