@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,62 +0,0 @@
1
- import type App from "#App";
2
- import type BuildApp from "#build/App";
3
- import type Render from "#frontend/Render";
4
- import type ServerView from "#frontend/ServerView";
5
- import type ViewResponse from "#frontend/ViewResponse";
6
- import Module from "#Module";
7
- import type Next from "#module/Next";
8
- import type NextBuild from "#module/NextBuild";
9
- import type NextServe from "#module/NextServe";
10
- import type ServeApp from "#serve/App";
11
- import type { FileRef } from "@rcompat/fs";
12
- import type { MaybePromise } from "@rcompat/type";
13
- export default abstract class FrontendModule<S = ServerView> extends Module {
14
- #private;
15
- abstract client: boolean;
16
- abstract layouts: boolean;
17
- abstract defaultExtensions: string[];
18
- render: Render<S>;
19
- root?: {
20
- create: (depth: number, i18n_active: boolean) => string;
21
- };
22
- compile: {
23
- client?: (text: string, file: FileRef, root: boolean) => MaybePromise<{
24
- css?: null | string;
25
- js: string;
26
- }>;
27
- server?: (text: string, file: FileRef) => MaybePromise<string>;
28
- };
29
- css?: {
30
- filter: RegExp;
31
- };
32
- conditions: string[];
33
- static schema: import("pema").ObjectType<{
34
- fileExtensions: import("pema").OptionalType<import("pema").ArrayType<import("pema").StringType>>;
35
- spa: import("pema").DefaultType<import("pema").BooleanType, true>;
36
- ssr: import("pema").DefaultType<import("pema").BooleanType, true>;
37
- }>;
38
- static options: {
39
- fileExtensions: string[] | undefined;
40
- spa: boolean;
41
- ssr: boolean;
42
- };
43
- static input: {
44
- fileExtensions?: string[] | undefined;
45
- spa?: boolean | undefined;
46
- ssr?: boolean | undefined;
47
- } | undefined;
48
- constructor(options?: typeof FrontendModule.schema.input);
49
- get fileExtensions(): string[];
50
- get package(): string;
51
- get rootname(): string;
52
- get ssr(): boolean;
53
- get spa(): boolean;
54
- normalize(name: string): Promise<string>;
55
- respond: ViewResponse;
56
- serve(app: ServeApp, next: NextServe): MaybePromise<ServeApp>;
57
- publish(app: BuildApp): void;
58
- init<T extends App>(app: T, next: Next<T>): MaybePromise<T>;
59
- prebuild(app: BuildApp): void;
60
- build(app: BuildApp, next: NextBuild): Promise<BuildApp>;
61
- }
62
- //# sourceMappingURL=Module.d.ts.map
@@ -1,255 +0,0 @@
1
- import fail from "#fail";
2
- import inline from "#inline";
3
- import location from "#location";
4
- import Module from "#Module";
5
- import hash from "@rcompat/crypto/hash";
6
- import fn from "@rcompat/fn";
7
- import fs from "@rcompat/fs";
8
- import MIME from "@rcompat/http/mime";
9
- import Status from "@rcompat/http/Status";
10
- import p from "pema";
11
- async function normalize(path, frontend) {
12
- const file = fs.ref(path);
13
- const basename = path.slice(0, -file.fullExtension.length);
14
- return `p_${await hash(`${basename}.${frontend}`)}`;
15
- }
16
- export default class FrontendModule extends Module {
17
- #options;
18
- render = async (view, props) => ({ body: await view(props) });
19
- root;
20
- compile = {};
21
- css;
22
- conditions = [];
23
- #mode = "development";
24
- static schema = p({
25
- fileExtensions: p.array(p.string).optional(),
26
- spa: p.boolean.default(true),
27
- ssr: p.boolean.default(true),
28
- });
29
- static options = FrontendModule.schema.infer;
30
- static input = FrontendModule.schema.input;
31
- constructor(options) {
32
- super();
33
- this.#options = FrontendModule.schema.parse(options);
34
- }
35
- get fileExtensions() {
36
- return this.#options.fileExtensions ?? this.defaultExtensions;
37
- }
38
- get package() {
39
- return `@primate/${this.name}`;
40
- }
41
- get rootname() {
42
- return `root_${this.name}`;
43
- }
44
- get ssr() {
45
- return this.#options.ssr && (this.#mode !== "development" || !this.client);
46
- }
47
- get spa() {
48
- return this.#options.spa;
49
- }
50
- #load(name, props, app) {
51
- if (!this.ssr)
52
- return { view: null, name, props };
53
- const view = app.loadView(name);
54
- return { view, name, props };
55
- }
56
- ;
57
- async #render(server, client, app) {
58
- const { body, head = "", headers = {} } = this.ssr
59
- ? await this.render(server.view, server.props)
60
- : { body: "", head: "" };
61
- if (!this.client) {
62
- if (app.mode === "development") {
63
- const app_asset = app.assets.find(asset => asset.src?.includes("app") && asset.src.endsWith(".js"));
64
- if (!app_asset)
65
- throw fail("Could not find app.js in assets");
66
- const app_script = `<script type="module" src="${app_asset.src}"></script>`;
67
- return { body, head: head.concat(app_script), headers };
68
- }
69
- return { body, head, headers };
70
- }
71
- const app_asset = app.assets.find(asset => asset.src?.includes("app") && asset.src.endsWith(".js"));
72
- if (!app_asset)
73
- throw fail("Could not find app.js in assets");
74
- const app_script = `<script type="module" src="${app_asset.src}"></script>`;
75
- const props = JSON.stringify({ frontend: this.name, ...client });
76
- const hydrated = await inline(props, MIME.APPLICATION_JSON, "hydration");
77
- const script_src = [hydrated.integrity];
78
- return {
79
- body,
80
- head: head.concat(app_script, hydrated.head),
81
- headers: app.headers({ "script-src": script_src }),
82
- };
83
- }
84
- normalize(name) {
85
- return normalize(name, this.name);
86
- }
87
- respond = (view, props = {}, options = {}) => async (app, { as_layout, layouts = [] } = {}, request) => {
88
- if (as_layout) {
89
- return this.#load(view, props, app);
90
- }
91
- const views = (await Promise.all(layouts
92
- .map(layout => layout(app, { as_layout: true }, request))))
93
- // set the actual page as the last view
94
- .concat(this.#load(view, props, app));
95
- const $request = request.toJSON();
96
- const $props = this.layouts
97
- ? {
98
- views: await fn.async.map(views, ({ name }) => this.normalize(name)),
99
- props: views.map(c => c.props),
100
- request: $request,
101
- }
102
- : { props, request: $request };
103
- const client = {
104
- view: this.layouts ? "root" : await this.normalize(view),
105
- spa: this.spa,
106
- ssr: this.ssr,
107
- mode: app.mode,
108
- ...$props,
109
- };
110
- if (this.spa && request.headers.get("Accept") === MIME.APPLICATION_JSON) {
111
- const json_body = JSON.stringify(client);
112
- return new Response(json_body, {
113
- headers: {
114
- ...app.headers(),
115
- "Content-Type": MIME.APPLICATION_JSON,
116
- "Content-Length": String(app.body_length(json_body)),
117
- "Cache-Control": "no-store",
118
- },
119
- status: options.status ?? Status.OK,
120
- });
121
- }
122
- if (!this.ssr) {
123
- const { head } = await this.#render({ view: null, props: {} }, client, app);
124
- return app.view({ body: "<div id=\"app\"></div>", head, ...options });
125
- }
126
- try {
127
- const server = this.layouts
128
- ? {
129
- view: app.loadView(this.rootname),
130
- props: {
131
- views: views.map(c => c.view),
132
- props: views.map(c => c.props),
133
- request: $request,
134
- },
135
- }
136
- : {
137
- view: app.loadView(view),
138
- props,
139
- request: $request,
140
- };
141
- const { body, head, headers } = await this.#render(server, client, app);
142
- return app.view({ body, head, headers, ...options });
143
- }
144
- catch (error) {
145
- const path = `${location.views}/${view}`;
146
- throw fail("error in view {0}\n{1}", path, error);
147
- }
148
- };
149
- serve(app, next) {
150
- this.fileExtensions.forEach(fe => app.register(fe, this.respond));
151
- return next(app);
152
- }
153
- publish(app) {
154
- if (app.mode === "development") {
155
- app.frontends.set(this.name, [...this.fileExtensions]);
156
- }
157
- if (this.compile.client) {
158
- const { compile, css, fileExtensions, name, root, conditions } = this;
159
- const _normalize = this.normalize.bind(this);
160
- if (this.client) {
161
- app.frontends.set(name, [...fileExtensions]);
162
- conditions.forEach(condition => app.conditions.add(condition));
163
- }
164
- app.plugin("client", {
165
- name,
166
- setup(build) {
167
- const resolveDir = app.root.path;
168
- const css_cache = new Map();
169
- if (root !== undefined) {
170
- const filter = new RegExp(`^${name}:root`);
171
- build.onResolve({ filter }, ({ path }) => {
172
- return { namespace: `${name}`, path };
173
- });
174
- build.onLoad({ filter }, async () => {
175
- const contents = (await compile.client(root.create(app.depth(), app.i18n_active), fs.ref("/tmp"), true)).js;
176
- return contents ? { contents, loader: "js", resolveDir } : null;
177
- });
178
- }
179
- if (css !== undefined) {
180
- build.onResolve({ filter: new RegExp(`^${name}:css:`) }, args => {
181
- return { path: args.path, namespace: `${name}-css` };
182
- });
183
- build.onLoad({ filter: /.*/, namespace: `${name}-css` }, args => {
184
- const contents = css_cache.get(args.path);
185
- return contents ? { contents, loader: "css" } : null;
186
- });
187
- }
188
- const views_filter = new RegExp(`^${name}:views`);
189
- const views_base = app.root.join(location.views);
190
- build.onResolve({ filter: views_filter }, ({ path }) => {
191
- return { namespace: `${name}`, path };
192
- });
193
- build.onLoad({ filter: views_filter }, async () => {
194
- const views = await views_base.files({
195
- recursive: true,
196
- filter: c => fileExtensions.includes(c.fullExtension),
197
- });
198
- let contents = "";
199
- for (const view of views) {
200
- const { path } = view.debase(views_base, "/");
201
- contents += `export { default as ${await _normalize(path)} }
202
- from "#view/${path}";\n`;
203
- }
204
- return { contents, resolveDir: app.root.path };
205
- });
206
- const filter = new RegExp(`(${fileExtensions.map(e => e.replace(".", "\\.")).join("|")})$`);
207
- build.onLoad({ filter }, async (args) => {
208
- const file = fs.ref(args.path);
209
- // compile file to JavaScript and potentially CSS
210
- const compiled = await compile.client(await file.text(), file, false);
211
- let contents = compiled.js;
212
- if (css
213
- && compiled.css !== null
214
- && compiled.css !== undefined
215
- && compiled.css !== "") {
216
- const css_path = `${name}:css:${args.path}`;
217
- css_cache.set(css_path, compiled.css);
218
- contents += `\nimport "${css_path}";`;
219
- }
220
- return { contents };
221
- });
222
- },
223
- });
224
- }
225
- }
226
- init(app, next) {
227
- this.#mode = app.mode;
228
- return next(app);
229
- }
230
- prebuild(app) {
231
- this.fileExtensions.forEach(e => {
232
- app.bind(e, async (file, { context }) => {
233
- if (context === "views" && !this.ssr)
234
- return "";
235
- // production: just compile to JS, don't bundle yet
236
- if (this.compile.server) {
237
- return await this.compile.server(await file.text(), file);
238
- }
239
- return await file.text();
240
- });
241
- });
242
- }
243
- async build(app, next) {
244
- this.prebuild(app);
245
- // compile root server
246
- if (this.root !== undefined && this.compile.server !== undefined) {
247
- const source = await this.compile.server(this.root.create(app.depth(), app.i18n_active), fs.ref(`root:${this.name}`));
248
- app.addRoot(this.rootname, source);
249
- }
250
- this.publish(app);
251
- return next(app);
252
- }
253
- ;
254
- }
255
- //# sourceMappingURL=Module.js.map
@@ -1,16 +0,0 @@
1
- import type Config from "#i18n/Config";
2
- import Module from "#Module";
3
- import type NextHandle from "#module/NextHandle";
4
- import type NextRoute from "#module/NextRoute";
5
- import type NextServe from "#module/NextServe";
6
- import type RequestFacade from "#request/RequestFacade";
7
- import type ServeApp from "#serve/App";
8
- export default class I18NModule extends Module {
9
- #private;
10
- name: string;
11
- constructor(config: Config);
12
- serve(app: ServeApp, next: NextServe): import("@rcompat/type").MaybePromise<ServeApp>;
13
- handle(request: RequestFacade, next: NextHandle): Promise<Response>;
14
- route(request: RequestFacade, next: NextRoute): import("@rcompat/type").MaybePromise<import("../response/ResponseLike.js").default>;
15
- }
16
- //# sourceMappingURL=Module.d.ts.map
@@ -1,133 +0,0 @@
1
- import AppError from "#AppError";
2
- import bye from "#bye";
3
- import cookie from "#cookie";
4
- import COOKIE_NAME from "#i18n/constant/COOKIE_NAME";
5
- import PERSIST_HEADER from "#i18n/constant/PERSIST_HEADER";
6
- import storage from "#i18n/storage";
7
- import log from "#log";
8
- import Module from "#Module";
9
- import Status from "@rcompat/http/Status";
10
- function toLowerCase(string) {
11
- return string.toLowerCase();
12
- }
13
- function pick(client, server) {
14
- const lower = server.map(toLowerCase);
15
- for (const raw of client.map(toLowerCase)) {
16
- const locale = raw.trim();
17
- if (!locale)
18
- continue;
19
- const exact = lower.indexOf(locale);
20
- if (exact !== -1)
21
- return server[exact];
22
- const base = locale.split("-")[0];
23
- const index = lower.findIndex(s => s === base || s.startsWith(`${base}-`));
24
- if (index !== -1)
25
- return server[index];
26
- }
27
- return undefined;
28
- }
29
- function fail(message, ...params) {
30
- const error = new AppError(`{0} ${message}`, "[i18n]", ...params);
31
- log.error(error);
32
- bye();
33
- process.exit(1);
34
- }
35
- export default class I18NModule extends Module {
36
- name = "builtin/i18n";
37
- #secure = false;
38
- #defaultLocale;
39
- #locales;
40
- #persist = "cookie";
41
- #currency;
42
- constructor(config) {
43
- super();
44
- const defaultLocale = config.defaultLocale;
45
- const locales = Object.keys(config.locales);
46
- if (locales.length < 1)
47
- fail("must have at least 1 locale");
48
- if (defaultLocale === undefined)
49
- fail("must have a default locale");
50
- if (!locales.includes(defaultLocale))
51
- fail("default locale not in locales");
52
- this.#defaultLocale = defaultLocale;
53
- this.#locales = locales;
54
- this.#persist = config.persist ?? "cookie";
55
- this.#currency = config.currency ?? "USD";
56
- }
57
- #configured(locale) {
58
- return this.#locales.includes(locale);
59
- }
60
- serve(app, next) {
61
- this.#secure = app.secure;
62
- return next(app);
63
- }
64
- async handle(request, next) {
65
- const requested = request.headers.try(PERSIST_HEADER);
66
- if (requested === undefined) {
67
- const locale = request.cookies.try(COOKIE_NAME);
68
- if (locale === undefined)
69
- return next(request);
70
- // if has cookie, run route with i18n
71
- return await new Promise((resolve, reject) => {
72
- storage().run({ locale }, async () => {
73
- try {
74
- resolve(await next(request));
75
- }
76
- catch (e) {
77
- reject(e);
78
- }
79
- });
80
- });
81
- }
82
- // only cookie-persistance is server-supported
83
- if (this.#persist !== "cookie")
84
- return new Response(null, {
85
- headers: {
86
- "Content-Length": String(0),
87
- },
88
- status: Status.NO_CONTENT,
89
- });
90
- // only accept configured locales
91
- if (!this.#configured(requested))
92
- return new Response(null, {
93
- headers: {
94
- "Content-Length": String(0),
95
- },
96
- status: Status.NO_CONTENT,
97
- });
98
- const header = cookie(COOKIE_NAME, requested, {
99
- secure: this.#secure,
100
- path: "/",
101
- sameSite: "Strict",
102
- });
103
- return new Response(null, {
104
- headers: {
105
- "Set-Cookie": header,
106
- "Content-Length": String(0),
107
- },
108
- status: Status.NO_CONTENT,
109
- });
110
- }
111
- route(request, next) {
112
- const server_locales = this.#locales;
113
- const accept_language = request.headers.try("Accept-Language") ?? "";
114
- const client_locales = accept_language
115
- .split(",")
116
- .map(s => s.split(";")[0].trim())
117
- .filter(Boolean);
118
- const mode = this.#persist;
119
- const cookie_locale = mode === "cookie"
120
- ? request.cookies.try(COOKIE_NAME)
121
- : undefined;
122
- const locale = cookie_locale ??
123
- pick(client_locales, server_locales) ??
124
- this.#defaultLocale;
125
- return next(request.set("i18n", {
126
- currency: this.#currency,
127
- mode,
128
- locale,
129
- locales: server_locales,
130
- }));
131
- }
132
- }
133
- //# sourceMappingURL=Module.js.map
@@ -1,5 +0,0 @@
1
- import type BuildApp from "#build/App";
2
- import type Hook from "#module/Hook";
3
- type BuildHook = Hook<BuildApp>;
4
- export type { BuildHook as default };
5
- //# sourceMappingURL=BuildHook.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=BuildHook.js.map
@@ -1,5 +0,0 @@
1
- import type BuildApp from "#build/App";
2
- import type Next from "#module/Next";
3
- type NextBuild = Next<BuildApp>;
4
- export type { NextBuild as default };
5
- //# sourceMappingURL=NextBuild.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=NextBuild.js.map
@@ -1,5 +0,0 @@
1
- import type Next from "#module/Next";
2
- import type ServeApp from "#serve/App";
3
- type NextServe = Next<ServeApp>;
4
- export type { NextServe as default };
5
- //# sourceMappingURL=NextServe.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=NextServe.js.map
@@ -1,11 +0,0 @@
1
- import type { ExtractSchema, StoreInput } from "#orm/types";
2
- import type { InferStore } from "pema";
3
- type X<T> = {
4
- [K in keyof T]: T[K];
5
- } & {};
6
- type OrNull<T> = {
7
- [P in keyof T]?: null | T[P];
8
- };
9
- type $Set<T extends StoreInput> = X<OrNull<InferStore<ExtractSchema<T>>>>;
10
- export type { $Set as default };
11
- //# sourceMappingURL=Set.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=Set.js.map
@@ -1,4 +0,0 @@
1
- import ForeignKey from "#orm/ForeignKey";
2
- import type { DataKey, Storable } from "pema";
3
- export default function foreign<T extends Storable<DataKey>>(type: T): ForeignKey<T>;
4
- //# sourceMappingURL=foreign.d.ts.map
@@ -1,5 +0,0 @@
1
- import ForeignKey from "#orm/ForeignKey";
2
- export default function foreign(type) {
3
- return new ForeignKey(type);
4
- }
5
- //# sourceMappingURL=foreign.js.map
@@ -1,5 +0,0 @@
1
- import type { Options } from "#orm/PrimaryKey";
2
- import PrimaryKey from "#orm/PrimaryKey";
3
- import type { DataKey, Storable } from "pema";
4
- export default function primary<T extends Storable<DataKey>>(type: T, options?: Options): PrimaryKey<T>;
5
- //# sourceMappingURL=primary.d.ts.map
@@ -1,5 +0,0 @@
1
- import PrimaryKey from "#orm/PrimaryKey";
2
- export default function primary(type, options) {
3
- return new PrimaryKey(type, options);
4
- }
5
- //# sourceMappingURL=primary.js.map
@@ -1,18 +0,0 @@
1
- import type ForeignKey from "#orm/ForeignKey";
2
- import type PrimaryKey from "#orm/PrimaryKey";
3
- import type { ManyRelation, OneRelation } from "#orm/relation";
4
- import type { Dict } from "@rcompat/type";
5
- import type { DataKey, InferStore, Storable } from "pema";
6
- type Relation = OneRelation<any, string> | ManyRelation<any, string>;
7
- type StoreField = Storable<DataKey> | PrimaryKey<Storable<DataKey>> | ForeignKey<Storable<DataKey>>;
8
- type StoreInput = Dict<StoreField>;
9
- type PrimaryKeyField<T extends StoreInput> = {
10
- [K in keyof T]: T[K] extends PrimaryKey<any> ? K : never;
11
- }[keyof T] & keyof InferRecord<T>;
12
- type ExtractSchema<T extends StoreInput> = {
13
- [K in keyof T]: T[K] extends PrimaryKey<infer P> ? P : T[K] extends ForeignKey<infer P> ? P : T[K] extends Storable<DataKey> ? T[K] : never;
14
- };
15
- type InferRecord<T extends StoreInput> = InferStore<ExtractSchema<T>>;
16
- type Insertable<T extends StoreInput> = Omit<InferRecord<T>, PrimaryKeyField<T>> & Partial<Pick<InferRecord<T>, PrimaryKeyField<T>>>;
17
- export type { ExtractSchema, InferRecord, Insertable, PrimaryKeyField, Relation, StoreField, StoreInput, };
18
- //# sourceMappingURL=types.d.ts.map
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1,5 +0,0 @@
1
- import type DB from "#db/DB";
2
- import type Store from "#orm/Store";
3
- import type { StoreSchema } from "pema";
4
- export default function wrapStore<T extends StoreSchema>(name: string, store: Store<T>, db: DB): Store<T, {}>;
5
- //# sourceMappingURL=wrap.d.ts.map
@@ -1,5 +0,0 @@
1
- import wrap from "#db/symbol/wrap";
2
- export default function wrapStore(name, store, db) {
3
- return store[wrap](name, db);
4
- }
5
- //# sourceMappingURL=wrap.js.map
@@ -1,24 +0,0 @@
1
- import type App from "#App";
2
- import type BuildApp from "#build/App";
3
- import type Module from "#Module";
4
- import type RequestFacade from "#request/RequestFacade";
5
- import type ResponseLike from "#response/ResponseLike";
6
- import type ServeApp from "#serve/App";
7
- type HookInput = {
8
- build: BuildApp;
9
- handle: RequestFacade;
10
- init: App;
11
- route: RequestFacade;
12
- serve: ServeApp;
13
- };
14
- type HookOutput = {
15
- build: BuildApp;
16
- handle: ResponseLike;
17
- init: App;
18
- route: ResponseLike;
19
- serve: ServeApp;
20
- };
21
- type Hook = keyof HookInput;
22
- export default function reducer<H extends Hook>(modules: Module[], dragon: HookInput[H], hook: H): Promise<HookOutput[H]>;
23
- export {};
24
- //# sourceMappingURL=reducer.d.ts.map
@@ -1,10 +0,0 @@
1
- export default async function reducer(modules, dragon, hook) {
2
- if (modules.length === 0) {
3
- return dragon;
4
- }
5
- const [head, ...tail] = modules;
6
- const method = head[hook].bind(head);
7
- return await method(dragon, next => tail.length === 0 ? Promise.resolve(next) : reducer(tail, next, hook));
8
- }
9
- ;
10
- //# sourceMappingURL=reducer.js.map
@@ -1,11 +0,0 @@
1
- import Module from "#Module";
2
- import type NextHandle from "#module/NextHandle";
3
- import type RequestFacade from "#request/RequestFacade";
4
- import type ServeApp from "#serve/App";
5
- export default class DevModule extends Module {
6
- #private;
7
- name: string;
8
- constructor(app: ServeApp);
9
- handle(request: RequestFacade, next: NextHandle): import("@rcompat/type").MaybePromise<Response>;
10
- }
11
- //# sourceMappingURL=Dev.d.ts.map
@@ -1,32 +0,0 @@
1
- import log from "#log";
2
- import Module from "#Module";
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
- // https://esbuild.github.io/api/#live-reload
13
- export default class DevModule extends Module {
14
- name = "builtin/dev";
15
- #paths;
16
- #reload;
17
- constructor(app) {
18
- super();
19
- const assets = app.assets.map(asset => asset.src);
20
- this.#paths = ["/esbuild"].concat(assets);
21
- const { host, port } = app.livereload;
22
- this.#reload = `http://${host}:${port}`;
23
- log.print(`↻ live reload ${c.dim(this.#reload)}\n`);
24
- }
25
- handle(request, next) {
26
- const { pathname } = new URL(request.url);
27
- return this.#paths.includes(pathname)
28
- ? pass(`${this.#reload}${pathname}`, request.original)
29
- : next(request);
30
- }
31
- }
32
- //# sourceMappingURL=Dev.js.map