@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
@@ -1,19 +0,0 @@
1
- import Module from "#Module";
2
- import p from "pema";
3
- export default class BackendModule extends Module {
4
- #options;
5
- static schema = p({ fileExtension: p.string.optional() });
6
- static options = BackendModule.schema.infer;
7
- static input = BackendModule.schema.input;
8
- constructor(options) {
9
- super();
10
- this.#options = BackendModule.schema.parse(options);
11
- }
12
- get fileExtension() {
13
- return this.#options.fileExtension ?? this.defaultExtension;
14
- }
15
- get package() {
16
- return `@primate/${this.name}`;
17
- }
18
- }
19
- //# sourceMappingURL=Module.js.map
@@ -1,3 +0,0 @@
1
- declare const _default: "0.4";
2
- export default _default;
3
- //# sourceMappingURL=TAG.d.ts.map
@@ -1,2 +0,0 @@
1
- export default "0.4";
2
- //# sourceMappingURL=TAG.js.map
@@ -1,4 +0,0 @@
1
- import type BuildApp from "#build/App";
2
- import type { Plugin } from "esbuild";
3
- export default function plugin_server_db_default(app: BuildApp): Plugin;
4
- //# sourceMappingURL=db-default.d.ts.map
@@ -1,45 +0,0 @@
1
- import fail from "#fail";
2
- export default function plugin_server_db_default(app) {
3
- const resolveDir = app.root.path;
4
- const base = app.path.config.join("db");
5
- const default_db = {
6
- contents: "import db from \"primate/db\"; export default db();",
7
- loader: "js",
8
- resolveDir,
9
- };
10
- return {
11
- name: "primate/server/db-default",
12
- setup(build) {
13
- build.onResolve({ filter: /^app:db/ }, () => {
14
- return { path: "db-default", namespace: "primate-db" };
15
- });
16
- build.onLoad({ filter: /.*/, namespace: "primate-db" }, async () => {
17
- if (!await base.exists())
18
- return default_db;
19
- const dbs = await base.files({
20
- recursive: true,
21
- filter: f => f.name.endsWith(".ts") || f.name.endsWith(".js"),
22
- });
23
- if (dbs.length === 0)
24
- return default_db;
25
- const by_name = {};
26
- for (const d of dbs)
27
- by_name[d.name] = d;
28
- const pick = (stem) => by_name[`${stem}.ts`] ?? by_name[`${stem}.js`];
29
- let db = pick("index") ?? pick("default");
30
- if (db === undefined) {
31
- if (dbs.length === 1)
32
- db = dbs[0];
33
- else
34
- throw fail("multiple database drivers, add index or default.(t|j)s; found {0}", dbs.map(f => f.name).join(", "));
35
- }
36
- return {
37
- contents: `export { default } from ${JSON.stringify(db.path)};`,
38
- loader: "js",
39
- resolveDir,
40
- };
41
- });
42
- },
43
- };
44
- }
45
- //# sourceMappingURL=db-default.js.map
@@ -1,4 +0,0 @@
1
- import type BuildApp from "#build/App";
2
- import type { Plugin } from "esbuild";
3
- export default function plugin_server_store_wrap(app: BuildApp): Plugin;
4
- //# sourceMappingURL=store-wrap.d.ts.map
@@ -1,33 +0,0 @@
1
- import fs from "@rcompat/fs";
2
- export default function plugin_server_store_wrap(app) {
3
- return {
4
- name: "primate/server/store-wrap",
5
- setup(build) {
6
- build.onLoad({ filter: /.*/ }, async (args) => {
7
- // only touch the default namespace
8
- if (args.namespace !== "file")
9
- return null;
10
- // only TS/JS
11
- if (!/\.([tj]s)$/.test(args.path))
12
- return null;
13
- const file = fs.ref(args.path);
14
- const storesRoot = app.path.stores.path;
15
- // only files under app/stores
16
- if (!file.path.startsWith(storesRoot + "/"))
17
- return null;
18
- // compute logical name: stores/foo/bar.ts → "foo/bar"
19
- const name = file
20
- .debase(app.path.stores)
21
- .path.replace(/^[\\/]/, "")
22
- .replace(/\.(ts|js)$/, "");
23
- const contents = `export { default } from "app:store/${name}";`;
24
- return {
25
- contents,
26
- loader: "js",
27
- resolveDir: app.root.path,
28
- };
29
- });
30
- },
31
- };
32
- }
33
- //# sourceMappingURL=store-wrap.js.map
@@ -1,6 +0,0 @@
1
- import type ClientData from "#client/Data";
2
- import type { Dict } from "@rcompat/type";
3
- type Updater<T extends Dict> = (json: ClientData<T>, after?: () => void) => void;
4
- declare const _default: <T extends Dict>(updater: Updater<T>) => void;
5
- export default _default;
6
- //# sourceMappingURL=index.d.ts.map
@@ -1,200 +0,0 @@
1
- import storage from "#client/spa/storage";
2
- import MIME from "@rcompat/http/mime";
3
- const { document } = globalThis;
4
- const headers = {
5
- Accept: MIME.APPLICATION_JSON,
6
- };
7
- const get_by_id_or_name = (name) => document.getElementById(name) ?? document.getElementsByName(name)[0];
8
- const scroll = globalThis.scrollTo;
9
- const scroll_hash = (hash) => {
10
- if (hash === "") {
11
- scroll(0, 0);
12
- }
13
- else {
14
- // https://html.spec.whatwg.org/browsing-the-web.html#scroll-to-fragid
15
- // first try id, then name
16
- get_by_id_or_name(hash.slice(1)).scrollIntoView();
17
- }
18
- };
19
- const sameorigin = (url) => url.origin === globalThis.location.origin;
20
- const getLocation = (response, base) => {
21
- // only readable when not opaqueredirect
22
- if (response.type === "opaqueredirect")
23
- return null;
24
- const location = response.headers.get("Location");
25
- return location ? new URL(location, base) : null;
26
- };
27
- // Follows same-origin redirects in "manual" mode up to a small limit
28
- async function refetch(input, init = {}, maxHops = 5) {
29
- let url = new URL(input.toString(), globalThis.location.href);
30
- let hops = 0;
31
- while (hops < maxHops) {
32
- const response = await fetch(url.pathname + url.search, {
33
- ...init, redirect: "manual",
34
- });
35
- // cross-origin redirect -> bail
36
- if (response.type === "opaqueredirect") {
37
- return { requested: url, response: response };
38
- }
39
- // same-origin redirect we can see?
40
- const location = getLocation(response, url.toString());
41
- if (location && (response.status >= 300 && response.status < 400)) {
42
- // would go cross-origin -> bail
43
- if (!sameorigin(location))
44
- return { requested: url, response: response };
45
- // follow internally without touching history
46
- url = location;
47
- hops++;
48
- continue;
49
- }
50
- // not a redirect -> return it
51
- return { requested: url, response };
52
- }
53
- throw new Error("Too many redirects");
54
- }
55
- function is_json(response) {
56
- const raw = response.headers.get("content-type") || "";
57
- const mime = raw.split(";")[0].trim();
58
- return mime === MIME.APPLICATION_JSON;
59
- }
60
- ;
61
- const handle = async (response, updater) => {
62
- // if it's JSON, process SPA update and keep history under our control
63
- if (is_json(response)) {
64
- updater(await response.json());
65
- return true; // handled in SPA
66
- }
67
- // not JSON ⇒ we will hard-navigate outside the SPA
68
- return false;
69
- };
70
- const goto = async ({ hash, pathname }, updater, state = false) => {
71
- try {
72
- // save before loading next
73
- const { scrollTop } = globalThis.document.scrollingElement;
74
- const { hash: currentHash, pathname: currentPathname } = globalThis.location;
75
- const { requested, response } = await refetch(pathname, { headers });
76
- if (await handle(response, updater)) {
77
- if (state) {
78
- storage.new({ hash: currentHash, pathname: currentPathname, scrollTop });
79
- const url = response.redirected ? response.url : `${pathname}${hash}`;
80
- history.pushState({}, "", url);
81
- }
82
- return;
83
- }
84
- const target = (response.type !== "opaqueredirect")
85
- ? requested.toString() + hash
86
- : new URL(pathname + hash, globalThis.location.href).toString();
87
- globalThis.location.assign(target);
88
- }
89
- catch (error) {
90
- console.warn(error);
91
- }
92
- };
93
- const submit = async (pathname, body, method, updater) => {
94
- try {
95
- const { requested, response } = await refetch(pathname, { body, headers, method });
96
- if (await handle(response, updater)) {
97
- history.replaceState({}, "", requested.pathname + requested.search);
98
- return;
99
- }
100
- const target = (response.type !== "opaqueredirect")
101
- ? requested.toString()
102
- : new URL(pathname, globalThis.location.href).toString();
103
- globalThis.location.assign(target);
104
- }
105
- catch (error) {
106
- console.warn(error);
107
- }
108
- };
109
- const go = async (href, updater, event) => {
110
- const url = new URL(href);
111
- const { hash, pathname } = url;
112
- const current = globalThis.location.pathname;
113
- // hosts must match
114
- if (url.host === globalThis.location.host) {
115
- // prevent event
116
- event?.preventDefault();
117
- // pathname differs
118
- if (current !== pathname) {
119
- await goto(url, props => updater(props, () => {
120
- scroll_hash(hash);
121
- globalThis.dispatchEvent(new Event("updated"));
122
- }), true);
123
- }
124
- // different hash on same page, jump to hash
125
- if (hash !== globalThis.location.hash) {
126
- const { scrollTop } = globalThis.document.scrollingElement;
127
- storage.new({
128
- hash: globalThis.location.hash,
129
- pathname: current,
130
- scrollTop,
131
- stop: true,
132
- });
133
- history.pushState(null, "", `${current}${hash}`);
134
- scroll_hash(hash);
135
- }
136
- }
137
- // external redirect
138
- };
139
- export default (updater) => {
140
- if (document.contentType === "application/json") {
141
- location.reload();
142
- return;
143
- }
144
- globalThis.addEventListener("pageshow", event => {
145
- if (event.persisted) {
146
- globalThis.location.reload();
147
- }
148
- });
149
- globalThis.addEventListener("load", _ => {
150
- history.scrollRestoration = "manual";
151
- if (globalThis.location.hash !== "") {
152
- scroll_hash(globalThis.location.hash);
153
- }
154
- });
155
- globalThis.addEventListener("beforeunload", _ => {
156
- history.scrollRestoration = "auto";
157
- });
158
- globalThis.addEventListener("popstate", _ => {
159
- const state = storage.peek() ?? { scrollTop: 0 };
160
- const { pathname } = globalThis.location;
161
- let { scrollTop } = state;
162
- if (state.stop) {
163
- storage.back();
164
- if (state.hash) {
165
- scroll_hash(state.hash);
166
- }
167
- else {
168
- scroll(0, state.scrollTop);
169
- }
170
- return;
171
- }
172
- const back = state.pathname === pathname;
173
- if (back) {
174
- storage.back();
175
- }
176
- else {
177
- scrollTop = storage.forward().scrollTop;
178
- }
179
- goto(globalThis.location, props => updater(props, () => scroll(0, scrollTop ?? 0)));
180
- });
181
- globalThis.addEventListener("click", event => {
182
- const target = event.target.closest("a");
183
- if (target?.tagName === "A" && target.href !== "") {
184
- go(target.href, updater, event);
185
- }
186
- });
187
- globalThis.addEventListener("submit", (event) => {
188
- event.preventDefault();
189
- const target = event.target;
190
- const { enctype } = target;
191
- const action = target.action ?? globalThis.location.pathname;
192
- const url = new URL(action);
193
- const data = new FormData(target);
194
- const form = enctype === MIME.MULTIPART_FORM_DATA
195
- ? data
196
- : new URLSearchParams(data);
197
- submit(url.pathname, form, target.method, updater);
198
- });
199
- };
200
- //# sourceMappingURL=index.js.map
@@ -1,81 +0,0 @@
1
- declare function db_missing(): import("../AppError.js").default;
2
- declare function store_name_required(): import("../AppError.js").default;
3
- declare function unregistered_schema(): import("../AppError.js").default;
4
- declare function record_not_found(field: string, value: string | number | bigint): import("../AppError.js").default;
5
- declare function key_duplicate(key: string): import("../AppError.js").default;
6
- declare function pk_undefined(store: string): import("../AppError.js").default;
7
- declare function pk_immutable(pk: string): import("../AppError.js").default;
8
- declare function pk_duplicate(pk: string): import("../AppError.js").default;
9
- declare function pk_invalid(pk: unknown): import("../AppError.js").default;
10
- declare function pk_required(table: string): import("../AppError.js").default;
11
- type Context = "select" | "where" | "sort" | "insert" | "set";
12
- declare function field_unknown(field: string, context: Context): import("../AppError.js").default;
13
- declare function field_duplicate(field: string, context: Context): import("../AppError.js").default;
14
- declare function field_required(operator: string): import("../AppError.js").default;
15
- declare function field_undefined(field: string, context: Context): import("../AppError.js").default;
16
- declare function fields_unknown(fields: string[]): import("../AppError.js").default;
17
- declare function null_not_allowed(field: string): import("../AppError.js").default;
18
- declare function wrong_type(type: "string" | "number" | "bigint" | "boolean" | "url" | "date" | "blob", field: string, got: unknown, op?: string): import("../AppError.js").default;
19
- declare function operator_unknown(field: string, operator: string): import("../AppError.js").default;
20
- declare function operator_empty(field: string): import("../AppError.js").default;
21
- declare function operator_scalar(field: string): import("../AppError.js").default;
22
- declare function sort_empty(): import("../AppError.js").default;
23
- declare function sort_invalid(): import("../AppError.js").default;
24
- declare function sort_invalid_value(field: string, value: unknown): import("../AppError.js").default;
25
- declare function select_empty(): import("../AppError.js").default;
26
- declare function select_invalid(): import("../AppError.js").default;
27
- declare function limit_invalid(): import("../AppError.js").default;
28
- declare function select_invalid_value(index: number, x: unknown): import("../AppError.js").default;
29
- declare function where_required(): import("../AppError.js").default;
30
- declare function where_invalid(): import("../AppError.js").default;
31
- declare function where_invalid_value(field: string, value: unknown): import("../AppError.js").default;
32
- declare function set_empty(): import("../AppError.js").default;
33
- declare function relation_unknown(relation: string): import("../AppError.js").default;
34
- declare function relation_requires_pk(type: "target" | "parent"): import("../AppError.js").default;
35
- declare function option_unknown(option: string): import("../AppError.js").default;
36
- declare function identifier_invalid(identifier: string): import("../AppError.js").default;
37
- declare function count_with_invalid(): import("../AppError.js").default;
38
- declare function count_overflow(table: string, count: unknown): import("../AppError.js").default;
39
- declare const errors: {
40
- option_unknown: typeof option_unknown;
41
- identifier_invalid: typeof identifier_invalid;
42
- count_with_invalid: typeof count_with_invalid;
43
- count_overflow: typeof count_overflow;
44
- relation_unknown: typeof relation_unknown;
45
- relation_requires_pk: typeof relation_requires_pk;
46
- sort_empty: typeof sort_empty;
47
- sort_invalid: typeof sort_invalid;
48
- sort_invalid_value: typeof sort_invalid_value;
49
- select_empty: typeof select_empty;
50
- select_invalid: typeof select_invalid;
51
- select_invalid_value: typeof select_invalid_value;
52
- where_required: typeof where_required;
53
- where_invalid: typeof where_invalid;
54
- where_invalid_value: typeof where_invalid_value;
55
- set_empty: typeof set_empty;
56
- limit_invalid: typeof limit_invalid;
57
- operator_unknown: typeof operator_unknown;
58
- operator_empty: typeof operator_empty;
59
- wrong_type: typeof wrong_type;
60
- operator_scalar: typeof operator_scalar;
61
- null_not_allowed: typeof null_not_allowed;
62
- field_unknown: typeof field_unknown;
63
- field_duplicate: typeof field_duplicate;
64
- field_required: typeof field_required;
65
- field_undefined: typeof field_undefined;
66
- fields_unknown: typeof fields_unknown;
67
- pk_undefined: typeof pk_undefined;
68
- pk_immutable: typeof pk_immutable;
69
- pk_duplicate: typeof pk_duplicate;
70
- pk_invalid: typeof pk_invalid;
71
- pk_required: typeof pk_required;
72
- db_missing: typeof db_missing;
73
- store_name_required: typeof store_name_required;
74
- unregistered_schema: typeof unregistered_schema;
75
- record_not_found: typeof record_not_found;
76
- key_duplicate: typeof key_duplicate;
77
- };
78
- export type Code = keyof typeof errors;
79
- export declare const Code: { [K in Code]: K; };
80
- export default errors;
81
- //# sourceMappingURL=error.d.ts.map
@@ -1,199 +0,0 @@
1
- import fail from "#fail";
2
- function coded(fns) {
3
- return Object.fromEntries(Object.entries(fns).map(([key, fn]) => [
4
- key,
5
- (...args) => {
6
- const err = fn(...args);
7
- err.code = key;
8
- return err;
9
- },
10
- ]));
11
- }
12
- function db_missing() {
13
- return fail("database missing");
14
- }
15
- function store_name_required() {
16
- return fail("store name required");
17
- }
18
- function unregistered_schema() {
19
- return fail("no store registered for schema");
20
- }
21
- function record_not_found(field, value) {
22
- return fail("no record with {0} = {1}", field, value);
23
- }
24
- function key_duplicate(key) {
25
- return fail("key {0} already exists", key);
26
- }
27
- const STORE = coded({
28
- db_missing,
29
- store_name_required,
30
- unregistered_schema,
31
- record_not_found,
32
- key_duplicate,
33
- });
34
- function pk_undefined(store) {
35
- return fail("{0}: store has no primary key", store);
36
- }
37
- function pk_immutable(pk) {
38
- return fail("primary key {0} cannot be updated", pk);
39
- }
40
- function pk_duplicate(pk) {
41
- return fail("primary key {0} already exists", pk);
42
- }
43
- function pk_invalid(pk) {
44
- return fail("pk must be string, number or bigint, got {0}", kind_of(pk));
45
- }
46
- function pk_required(table) {
47
- return fail("pk is required but has generate={0} in table {1}", false, table);
48
- }
49
- const PK = coded({
50
- pk_undefined,
51
- pk_immutable,
52
- pk_duplicate,
53
- pk_invalid,
54
- pk_required,
55
- });
56
- function field_unknown(field, context) {
57
- return fail("{0}: unknown field on {1}", field, context);
58
- }
59
- function field_duplicate(field, context) {
60
- return fail("{0}: duplicate field on {1}", field, context);
61
- }
62
- function field_required(operator) {
63
- return fail("{0}: at least one field required", operator);
64
- }
65
- function field_undefined(field, context) {
66
- return fail("{0}: undefined value on {1}", field, context);
67
- }
68
- function fields_unknown(fields) {
69
- return fail("unknown fields {0}", fields.join(", "));
70
- }
71
- const FIELD = coded({
72
- field_unknown,
73
- field_duplicate,
74
- field_required,
75
- field_undefined,
76
- fields_unknown,
77
- });
78
- function null_not_allowed(field) {
79
- return fail("{0}: null not allowed", field);
80
- }
81
- const NULL = coded({
82
- null_not_allowed,
83
- });
84
- function kind_of(x) {
85
- if (x === null)
86
- return "null";
87
- if (x instanceof Date)
88
- return "Date";
89
- if (x instanceof URL)
90
- return "URL";
91
- if (x instanceof Blob)
92
- return "Blob";
93
- return typeof x;
94
- }
95
- function wrong_type(type, field, got, op = "value") {
96
- return fail("{0}: {1} requires {2}, got {3}", field, op, type, kind_of(got));
97
- }
98
- function operator_unknown(field, operator) {
99
- return fail("{0}: unknown operator {1}", field, operator);
100
- }
101
- function operator_empty(field) {
102
- return fail("{0}: empty operator", field);
103
- }
104
- function operator_scalar(field) {
105
- return fail("{0}: operator requires scalar value", field);
106
- }
107
- const OPERATOR = coded({
108
- operator_unknown,
109
- operator_empty,
110
- wrong_type,
111
- operator_scalar,
112
- });
113
- function sort_empty() {
114
- return fail("empty sort");
115
- }
116
- function sort_invalid() {
117
- return fail("sort invalid");
118
- }
119
- function sort_invalid_value(field, value) {
120
- return fail("{0}: invalid sort value, received {1}", field, kind_of(value));
121
- }
122
- function select_empty() {
123
- return fail("empty select");
124
- }
125
- function select_invalid() {
126
- return fail("invalid select");
127
- }
128
- function limit_invalid() {
129
- return fail("invalid limit");
130
- }
131
- function select_invalid_value(index, x) {
132
- return fail("select[{0}]: must be string, received {1}", index, kind_of(x));
133
- }
134
- function where_required() {
135
- return fail("where required");
136
- }
137
- function where_invalid() {
138
- return fail("where invalid");
139
- }
140
- function where_invalid_value(field, value) {
141
- return fail("{0}: invalid where value, received {1}", field, kind_of(value));
142
- }
143
- function set_empty() {
144
- return fail("empty set on update");
145
- }
146
- const QUERY = coded({
147
- sort_empty,
148
- sort_invalid,
149
- sort_invalid_value,
150
- select_empty,
151
- select_invalid,
152
- select_invalid_value,
153
- where_required,
154
- where_invalid,
155
- where_invalid_value,
156
- set_empty,
157
- limit_invalid,
158
- });
159
- function relation_unknown(relation) {
160
- return fail("unknown relation {0}", relation);
161
- }
162
- function relation_requires_pk(type) {
163
- return fail("relation loading requires {0} primary key", type);
164
- }
165
- const RELATION = coded({
166
- relation_unknown,
167
- relation_requires_pk,
168
- });
169
- function option_unknown(option) {
170
- return fail("unknown option {0}", option);
171
- }
172
- function identifier_invalid(identifier) {
173
- return fail("invalid identifier {0}", identifier);
174
- }
175
- function count_with_invalid() {
176
- return fail("count and with are mutually exclusive");
177
- }
178
- function count_overflow(table, count) {
179
- return fail("{0}: count overflow, received {1} (max {2})", table, count, Number.MAX_SAFE_INTEGER);
180
- }
181
- const MISC = coded({
182
- option_unknown,
183
- identifier_invalid,
184
- count_with_invalid,
185
- count_overflow,
186
- });
187
- const errors = {
188
- ...STORE,
189
- ...PK,
190
- ...FIELD,
191
- ...NULL,
192
- ...OPERATOR,
193
- ...QUERY,
194
- ...RELATION,
195
- ...MISC,
196
- };
197
- export const Code = Object.fromEntries(Object.keys(errors).map(k => [k, k]));
198
- export default errors;
199
- //# sourceMappingURL=error.js.map
@@ -1,3 +0,0 @@
1
- declare const _default: symbol;
2
- export default _default;
3
- //# sourceMappingURL=wrap.d.ts.map
@@ -1,2 +0,0 @@
1
- export default Symbol("@primate/core/db/wrap");
2
- //# sourceMappingURL=wrap.js.map
@@ -1,3 +0,0 @@
1
- import AppError from "#AppError";
2
- export default function fail(message: string, ...params: unknown[]): AppError;
3
- //# sourceMappingURL=fail.d.ts.map
@@ -1,5 +0,0 @@
1
- import AppError from "#AppError";
2
- export default function fail(message, ...params) {
3
- return new AppError(message, ...params);
4
- }
5
- //# sourceMappingURL=fail.js.map