crelte 0.5.8 → 0.5.9

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 (175) hide show
  1. package/dist/blocks/Blocks.d.ts +36 -0
  2. package/dist/blocks/Blocks.d.ts.map +1 -1
  3. package/dist/blocks/Blocks.js +42 -0
  4. package/dist/blocks/Blocks.svelte +2 -16
  5. package/dist/blocks/Blocks.svelte.d.ts +20 -37
  6. package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
  7. package/dist/blocks/index.d.ts +2 -2
  8. package/dist/blocks/index.d.ts.map +1 -1
  9. package/dist/blocks/index.js +2 -2
  10. package/dist/cookies/ServerCookies.d.ts +1 -1
  11. package/dist/cookies/ServerCookies.js +1 -1
  12. package/dist/cookies/index.d.ts +1 -1
  13. package/dist/crelte.d.ts +21 -10
  14. package/dist/crelte.d.ts.map +1 -1
  15. package/dist/index.d.ts +19 -14
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +17 -13
  18. package/dist/init/client.d.ts +2 -2
  19. package/dist/init/client.d.ts.map +1 -1
  20. package/dist/init/client.js +3 -2
  21. package/dist/init/server.d.ts +7 -6
  22. package/dist/init/server.d.ts.map +1 -1
  23. package/dist/init/server.js +6 -6
  24. package/dist/loadData/Globals.d.ts +3 -3
  25. package/dist/loadData/Globals.js +3 -3
  26. package/dist/loadData/loadData.d.ts +8 -18
  27. package/dist/loadData/loadData.d.ts.map +1 -1
  28. package/dist/loadData/loadData.js +2 -2
  29. package/dist/node/index.d.ts +5 -0
  30. package/dist/node/index.d.ts.map +1 -1
  31. package/dist/node/index.js +84 -35
  32. package/dist/plugins/Events.d.ts +4 -4
  33. package/dist/plugins/Plugins.d.ts +2 -2
  34. package/dist/plugins/index.d.ts +2 -3
  35. package/dist/plugins/index.d.ts.map +1 -1
  36. package/dist/plugins/index.js +2 -1
  37. package/dist/queries/Queries.d.ts +9 -0
  38. package/dist/queries/Queries.d.ts.map +1 -1
  39. package/dist/queries/Queries.js +29 -17
  40. package/dist/queries/QueryError.d.ts +2 -0
  41. package/dist/queries/QueryError.d.ts.map +1 -1
  42. package/dist/queries/QueryError.js +6 -1
  43. package/dist/queries/gql.d.ts +2 -2
  44. package/dist/queries/gql.js +2 -2
  45. package/dist/queries/index.d.ts +7 -5
  46. package/dist/queries/index.d.ts.map +1 -1
  47. package/dist/queries/vars.d.ts +7 -7
  48. package/dist/queries/vars.d.ts.map +1 -1
  49. package/dist/queries/vars.js +10 -8
  50. package/dist/routing/LoadRunner.d.ts +1 -1
  51. package/dist/routing/LoadRunner.js +1 -1
  52. package/dist/routing/index.d.ts +4 -4
  53. package/dist/routing/index.d.ts.map +1 -1
  54. package/dist/routing/route/BaseRoute.d.ts +25 -25
  55. package/dist/routing/route/BaseRoute.js +24 -24
  56. package/dist/routing/route/Request.d.ts +4 -4
  57. package/dist/routing/route/Request.js +4 -4
  58. package/dist/routing/route/Route.d.ts +2 -2
  59. package/dist/routing/route/Route.js +2 -2
  60. package/dist/routing/router/BaseRouter.d.ts +4 -4
  61. package/dist/routing/router/BaseRouter.js +3 -3
  62. package/dist/routing/router/ClientRouter.d.ts +1 -1
  63. package/dist/routing/router/ClientRouter.js +1 -1
  64. package/dist/routing/router/Router.d.ts +27 -25
  65. package/dist/routing/router/Router.d.ts.map +1 -1
  66. package/dist/routing/router/Router.js +24 -21
  67. package/dist/routing/router/ServerRouter.d.ts +1 -1
  68. package/dist/routing/router/ServerRouter.js +1 -1
  69. package/dist/server/CrelteServer.d.ts +5 -5
  70. package/dist/server/CrelteServer.d.ts.map +1 -1
  71. package/dist/server/CrelteServer.js +4 -4
  72. package/dist/server/Request.d.ts +7 -1
  73. package/dist/server/Request.d.ts.map +1 -1
  74. package/dist/server/Request.js +7 -1
  75. package/dist/server/ServerRouter.d.ts +1 -1
  76. package/dist/server/ServerRouter.d.ts.map +1 -1
  77. package/dist/server/index.d.ts +4 -4
  78. package/dist/server/index.d.ts.map +1 -1
  79. package/dist/server/index.js +1 -1
  80. package/dist/server/shared.d.ts +7 -13
  81. package/dist/server/shared.d.ts.map +1 -1
  82. package/dist/server/shared.js +14 -16
  83. package/dist/ssr/SsrCache.d.ts +56 -4
  84. package/dist/ssr/SsrCache.d.ts.map +1 -1
  85. package/dist/ssr/SsrCache.js +94 -20
  86. package/dist/std/index.d.ts +13 -0
  87. package/dist/std/index.d.ts.map +1 -1
  88. package/dist/std/index.js +13 -0
  89. package/dist/std/rand/index.d.ts +34 -0
  90. package/dist/std/rand/index.d.ts.map +1 -0
  91. package/dist/std/rand/index.js +45 -0
  92. package/dist/std/rand/internal.d.ts +8 -0
  93. package/dist/std/rand/internal.d.ts.map +1 -0
  94. package/dist/std/rand/internal.js +15 -0
  95. package/dist/std/stores/Writable.d.ts +1 -1
  96. package/dist/std/stores/Writable.js +1 -1
  97. package/dist/translations/index.d.ts +28 -0
  98. package/dist/translations/index.d.ts.map +1 -0
  99. package/dist/translations/index.js +31 -0
  100. package/dist/translations/loadTranslations.d.ts +23 -0
  101. package/dist/translations/loadTranslations.d.ts.map +1 -0
  102. package/dist/translations/loadTranslations.js +26 -0
  103. package/dist/translations/loader/GlobalLoader.d.ts +34 -0
  104. package/dist/translations/loader/GlobalLoader.d.ts.map +1 -0
  105. package/dist/translations/loader/GlobalLoader.js +45 -0
  106. package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts +10 -0
  107. package/dist/translations/loader/fileLoader/ClientFileLoader.d.ts.map +1 -0
  108. package/dist/translations/loader/fileLoader/ClientFileLoader.js +24 -0
  109. package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts +9 -0
  110. package/dist/translations/loader/fileLoader/NodeFileLoader.d.ts.map +1 -0
  111. package/dist/translations/loader/fileLoader/NodeFileLoader.js +21 -0
  112. package/dist/translations/loader/fileLoader/fileLoader.d.ts +5 -0
  113. package/dist/translations/loader/fileLoader/fileLoader.d.ts.map +1 -0
  114. package/dist/translations/loader/fileLoader/fileLoader.js +16 -0
  115. package/dist/translations/loader/index.d.ts +7 -0
  116. package/dist/translations/loader/index.d.ts.map +1 -0
  117. package/dist/translations/loader/index.js +1 -0
  118. package/dist/translations/translationsPlugin.d.ts +64 -0
  119. package/dist/translations/translationsPlugin.d.ts.map +1 -0
  120. package/dist/translations/translationsPlugin.js +110 -0
  121. package/dist/translations/utils.d.ts +8 -0
  122. package/dist/translations/utils.d.ts.map +1 -0
  123. package/dist/translations/utils.js +15 -0
  124. package/dist/vite/index.d.ts.map +1 -1
  125. package/dist/vite/index.js +30 -38
  126. package/package.json +9 -1
  127. package/src/blocks/Blocks.svelte +4 -63
  128. package/src/blocks/Blocks.ts +53 -0
  129. package/src/blocks/index.ts +2 -2
  130. package/src/cookies/ServerCookies.ts +1 -1
  131. package/src/cookies/index.ts +1 -1
  132. package/src/crelte.ts +21 -10
  133. package/src/index.ts +19 -14
  134. package/src/init/client.ts +3 -2
  135. package/src/init/server.ts +14 -8
  136. package/src/loadData/Globals.ts +3 -3
  137. package/src/loadData/loadData.ts +8 -18
  138. package/src/node/index.ts +100 -39
  139. package/src/plugins/Events.ts +4 -4
  140. package/src/plugins/Plugins.ts +2 -2
  141. package/src/plugins/index.ts +2 -3
  142. package/src/queries/Queries.ts +35 -18
  143. package/src/queries/QueryError.ts +10 -1
  144. package/src/queries/gql.ts +2 -2
  145. package/src/queries/index.ts +7 -4
  146. package/src/queries/vars.ts +12 -8
  147. package/src/routing/LoadRunner.ts +1 -1
  148. package/src/routing/index.ts +13 -3
  149. package/src/routing/route/BaseRoute.ts +25 -25
  150. package/src/routing/route/Request.ts +4 -4
  151. package/src/routing/route/Route.ts +2 -2
  152. package/src/routing/router/BaseRouter.ts +4 -4
  153. package/src/routing/router/ClientRouter.ts +1 -1
  154. package/src/routing/router/Router.ts +28 -25
  155. package/src/routing/router/ServerRouter.ts +1 -1
  156. package/src/server/CrelteServer.ts +5 -5
  157. package/src/server/Request.ts +7 -1
  158. package/src/server/ServerRouter.ts +1 -2
  159. package/src/server/index.ts +17 -3
  160. package/src/server/shared.ts +28 -33
  161. package/src/ssr/SsrCache.ts +104 -22
  162. package/src/std/index.ts +14 -0
  163. package/src/std/rand/index.ts +55 -0
  164. package/src/std/rand/internal.ts +17 -0
  165. package/src/std/stores/Writable.ts +1 -1
  166. package/src/translations/index.ts +56 -0
  167. package/src/translations/loadTranslations.ts +33 -0
  168. package/src/translations/loader/GlobalLoader.ts +62 -0
  169. package/src/translations/loader/fileLoader/ClientFileLoader.ts +40 -0
  170. package/src/translations/loader/fileLoader/NodeFileLoader.ts +32 -0
  171. package/src/translations/loader/fileLoader/fileLoader.ts +19 -0
  172. package/src/translations/loader/index.ts +8 -0
  173. package/src/translations/translationsPlugin.ts +145 -0
  174. package/src/translations/utils.ts +17 -0
  175. package/src/vite/index.ts +43 -39
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Calculates a key based on the json representation of the data
3
+ *
4
+ * If available hashes the data using SHA-1
5
+ */
1
6
  export declare function calcKey(data: any): Promise<string>;
2
7
  /**
3
8
  * A simple cache for server side rendering
@@ -9,9 +14,9 @@ export default class SsrCache {
9
14
  private store;
10
15
  constructor();
11
16
  /**
12
- * check if the value is in the cache else calls the fn
17
+ * Check if a key exists in the cache
13
18
  */
14
- load<T>(key: string, fn: () => Promise<T>): Promise<any>;
19
+ has(key: string): boolean;
15
20
  /**
16
21
  * Get a value from the cache
17
22
  */
@@ -20,10 +25,57 @@ export default class SsrCache {
20
25
  * Set a value in the cache
21
26
  */
22
27
  set<T>(key: string, val: T): T;
28
+ /**
29
+ * check if the value is in the cache else calls the fn
30
+ *
31
+ * See also {@link getOrInsertLoaded}
32
+ */
33
+ getOrInsertComputed<T>(key: string, fn: () => T): T;
34
+ /**
35
+ * check if the value is in the cache else calls the fn
36
+ *
37
+ * See also {@link getOrInsertComputed}
38
+ *
39
+ * @deprecated use {@link getOrInsertLoaded} instead
40
+ */
41
+ load<T>(key: string, fn: () => Promise<T>): Promise<T>;
42
+ /**
43
+ * check if the value is in the cache else calls the fn
44
+ *
45
+ * See also {@link getOrInsertComputed}
46
+ */
47
+ getOrInsertLoaded<T>(key: string, fn: () => Promise<T>): Promise<T>;
48
+ /**
49
+ * One-shot SSR handoff value.
50
+ *
51
+ * Intended use: call this once per request (per key) during SSR to generate
52
+ * a value that must match between server render and client hydration.
53
+ *
54
+ * On the server, the value is generated once per key and stored for hydration.
55
+ * Subsequent calls with the same key during SSR currently return the same value,
56
+ * but this behaviour is an implementation detail and may change in the future.
57
+ * Consumers should rely on calling this at most once per key during SSR.
58
+ *
59
+ * On the client, the stored value is returned exactly once and removed.
60
+ * Subsequent calls return a fresh value and are not cached.
61
+ *
62
+ * Warning: this function is designed to be called once per key during SSR.
63
+ * Calling it multiple times may lead to unexpected behaviour if the server-side
64
+ * implementation changes.
65
+ *
66
+ * See also {@link getOrInsertComputed}
67
+ */
68
+ takeOnce<T>(key: string, fn: () => T): T;
69
+ /**
70
+ * Remove a value from the cache and return it
71
+ */
72
+ remove<T>(key: string): T | null;
73
+ /** @hidden */
74
+ z_clear(): void;
23
75
  /** @hidden */
24
- clear(): void;
76
+ z_importFromHead(): void;
25
77
  private exportAsJson;
26
78
  /** @hidden */
27
- exportToHead(): string;
79
+ z_exportToHead(): string;
28
80
  }
29
81
  //# sourceMappingURL=SsrCache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SsrCache.d.ts","sourceRoot":"","sources":["../../src/ssr/SsrCache.ts"],"names":[],"mappings":"AAAA,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,mBAoBtC;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC5B,OAAO,CAAC,KAAK,CAAsB;;IAYnC;;OAEG;IACG,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC;IAO/C;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAI7B;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAI9B,cAAc;IACd,KAAK;IAIL,OAAO,CAAC,YAAY;IAIpB,cAAc;IACd,YAAY,IAAI,MAAM;CAGtB"}
1
+ {"version":3,"file":"SsrCache.d.ts","sourceRoot":"","sources":["../../src/ssr/SsrCache.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAoBxD;AAED;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IAC5B,OAAO,CAAC,KAAK,CAAmB;;IAMhC;;OAEG;IACH,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIzB;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAI7B;;OAEG;IACH,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC;IAK9B;;;;OAIG;IACH,mBAAmB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC;IAKnD;;;;;;OAMG;IACG,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAI5D;;;;OAIG;IACG,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAKzE;;;;;;;;;;;;;;;;;;;OAmBG;IACH,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC;IAcxC;;OAEG;IACH,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAMhC,cAAc;IACd,OAAO;IAIP,cAAc;IACd,gBAAgB;IAOhB,OAAO,CAAC,YAAY;IAOpB,cAAc;IACd,cAAc,IAAI,MAAM;CAGxB"}
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Calculates a key based on the json representation of the data
3
+ *
4
+ * If available hashes the data using SHA-1
5
+ */
1
6
  export async function calcKey(data) {
2
7
  const json = JSON.stringify(data);
3
8
  // this should only happen in an unsecure context
@@ -25,44 +30,113 @@ export async function calcKey(data) {
25
30
  export default class SsrCache {
26
31
  store;
27
32
  constructor() {
28
- this.store = {};
29
- // @ts-ignore
30
- if (typeof window !== 'undefined' && window.SSR_STORE) {
31
- // @ts-ignore
32
- this.store = window.SSR_STORE;
33
- }
33
+ this.store = new Map();
34
34
  }
35
35
  /**
36
- * check if the value is in the cache else calls the fn
36
+ * Check if a key exists in the cache
37
37
  */
38
- async load(key, fn) {
39
- if (key in this.store)
40
- return this.store[key];
41
- const v = await fn();
42
- this.set(key, v);
43
- return v;
38
+ has(key) {
39
+ return this.store.has(key);
44
40
  }
45
41
  /**
46
42
  * Get a value from the cache
47
43
  */
48
44
  get(key) {
49
- return this.store[key] ?? null;
45
+ return this.store.get(key) ?? null;
50
46
  }
51
47
  /**
52
48
  * Set a value in the cache
53
49
  */
54
50
  set(key, val) {
55
- return (this.store[key] = val);
51
+ this.store.set(key, val);
52
+ return val;
53
+ }
54
+ /**
55
+ * check if the value is in the cache else calls the fn
56
+ *
57
+ * See also {@link getOrInsertLoaded}
58
+ */
59
+ getOrInsertComputed(key, fn) {
60
+ if (this.store.has(key))
61
+ return this.store.get(key);
62
+ return this.set(key, fn());
63
+ }
64
+ /**
65
+ * check if the value is in the cache else calls the fn
66
+ *
67
+ * See also {@link getOrInsertComputed}
68
+ *
69
+ * @deprecated use {@link getOrInsertLoaded} instead
70
+ */
71
+ async load(key, fn) {
72
+ return this.getOrInsertLoaded(key, fn);
73
+ }
74
+ /**
75
+ * check if the value is in the cache else calls the fn
76
+ *
77
+ * See also {@link getOrInsertComputed}
78
+ */
79
+ async getOrInsertLoaded(key, fn) {
80
+ if (this.store.has(key))
81
+ return this.store.get(key);
82
+ return this.set(key, await fn());
83
+ }
84
+ /**
85
+ * One-shot SSR handoff value.
86
+ *
87
+ * Intended use: call this once per request (per key) during SSR to generate
88
+ * a value that must match between server render and client hydration.
89
+ *
90
+ * On the server, the value is generated once per key and stored for hydration.
91
+ * Subsequent calls with the same key during SSR currently return the same value,
92
+ * but this behaviour is an implementation detail and may change in the future.
93
+ * Consumers should rely on calling this at most once per key during SSR.
94
+ *
95
+ * On the client, the stored value is returned exactly once and removed.
96
+ * Subsequent calls return a fresh value and are not cached.
97
+ *
98
+ * Warning: this function is designed to be called once per key during SSR.
99
+ * Calling it multiple times may lead to unexpected behaviour if the server-side
100
+ * implementation changes.
101
+ *
102
+ * See also {@link getOrInsertComputed}
103
+ */
104
+ takeOnce(key, fn) {
105
+ if (import.meta.env.SSR) {
106
+ if (this.store.has(key)) {
107
+ console.warn(`SsrCache.takeOnce called multiple times for key "${key}" during SSR.`);
108
+ return this.store.get(key);
109
+ }
110
+ return this.set(key, fn());
111
+ }
112
+ if (this.store.has(key))
113
+ return this.remove(key);
114
+ return fn();
115
+ }
116
+ /**
117
+ * Remove a value from the cache and return it
118
+ */
119
+ remove(key) {
120
+ const val = this.get(key);
121
+ this.store.delete(key);
122
+ return val;
56
123
  }
57
124
  /** @hidden */
58
- clear() {
59
- this.store = {};
125
+ z_clear() {
126
+ this.store.clear();
127
+ }
128
+ /** @hidden */
129
+ z_importFromHead() {
130
+ // @ts-ignore
131
+ this.store = new Map(window._SSR_STORE ?? []);
132
+ // @ts-ignore
133
+ delete window._SSR_STORE;
60
134
  }
61
135
  exportAsJson() {
62
- return JSON.stringify(this.store).replace(/</g, '\\u003c');
136
+ return JSON.stringify(Array.from(this.store.entries())).replace(/</g, '\\u003c');
63
137
  }
64
138
  /** @hidden */
65
- exportToHead() {
66
- return `\n\t\t<script>window.SSR_STORE = ${this.exportAsJson()};</script>`;
139
+ z_exportToHead() {
140
+ return `\n\t\t<script>window._SSR_STORE = ${this.exportAsJson()};</script>`;
67
141
  }
68
142
  }
@@ -1,3 +1,16 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Crelte Standard library
5
+ *
6
+ * Provides standard utility functions and types for Crelte applications.
7
+ *
8
+ * ### Modules:
9
+ * - {@link std/intl}
10
+ * - {@link std/stores}
11
+ * - {@link std/sync}
12
+ * - {@link std/rand}
13
+ */
1
14
  /**
2
15
  * Delays for a specified amount of time.
3
16
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/std/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEjD;AAED,MAAM,WAAW,SAAS;IACzB,KAAK,IAAI,IAAI,CAAC;CACd;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC7B,SAAS,GACT,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,CAAC;AAEb;;;GAGG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,oBAAoB,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAIjE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/std/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEjD;AAED,MAAM,WAAW,SAAS;IACzB,KAAK,IAAI,IAAI,CAAC;CACd;AAED;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC7B,SAAS,GACT,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,CAAC;AAEb;;;GAGG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,oBAAoB,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAIjE"}
package/dist/std/index.js CHANGED
@@ -1,3 +1,16 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Crelte Standard library
5
+ *
6
+ * Provides standard utility functions and types for Crelte applications.
7
+ *
8
+ * ### Modules:
9
+ * - {@link std/intl}
10
+ * - {@link std/stores}
11
+ * - {@link std/sync}
12
+ * - {@link std/rand}
13
+ */
1
14
  /**
2
15
  * Delays for a specified amount of time.
3
16
  *
@@ -0,0 +1,34 @@
1
+ import { Crelte } from '../../crelte.js';
2
+ /**
3
+ * Returns a random number between 0 (inclusive) and 1 (exclusive).
4
+ *
5
+ * In SSR mode, the same key will always return the same random number.
6
+ * In client mode, the first call will return the number generated during SSR.
7
+ */
8
+ export declare function random(key: string, crelte?: Crelte): number;
9
+ /**
10
+ * Create a deterministic random number generator.
11
+ *
12
+ * Intended use: create this once during component initialization when a sequence
13
+ * of random values must be consistent between server-side rendering and client
14
+ * hydration.
15
+ *
16
+ * The generator is seeded during SSR using the provided key. During hydration,
17
+ * the same seed is reused so that the client produces the same sequence of random
18
+ * numbers as the server for the initial render.
19
+ *
20
+ * After hydration, the generator continues independently on the client.
21
+ *
22
+ * @returns A function that returns a pseudo-random number in the range
23
+ * [0, 1).
24
+ *
25
+ * #### Example
26
+ * ```ts
27
+ * const rng = createRng('lucky-number');
28
+ *
29
+ * const val1 = rng(); // same on server and client
30
+ * const val2 = rng(); // same on server and client
31
+ * ```
32
+ */
33
+ export declare function createRng(key: string, crelte?: Crelte): () => number;
34
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/std/rand/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGzC;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,MAAoB,GAAG,MAAM,CAExE;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,MAAM,EACX,MAAM,GAAE,MAAoB,GAC1B,MAAM,MAAM,CAcd"}
@@ -0,0 +1,45 @@
1
+ import { getCrelte } from '../../index.js';
2
+ /**
3
+ * Returns a random number between 0 (inclusive) and 1 (exclusive).
4
+ *
5
+ * In SSR mode, the same key will always return the same random number.
6
+ * In client mode, the first call will return the number generated during SSR.
7
+ */
8
+ export function random(key, crelte = getCrelte()) {
9
+ return crelte.ssrCache.takeOnce('_rand_' + key, () => Math.random());
10
+ }
11
+ /**
12
+ * Create a deterministic random number generator.
13
+ *
14
+ * Intended use: create this once during component initialization when a sequence
15
+ * of random values must be consistent between server-side rendering and client
16
+ * hydration.
17
+ *
18
+ * The generator is seeded during SSR using the provided key. During hydration,
19
+ * the same seed is reused so that the client produces the same sequence of random
20
+ * numbers as the server for the initial render.
21
+ *
22
+ * After hydration, the generator continues independently on the client.
23
+ *
24
+ * @returns A function that returns a pseudo-random number in the range
25
+ * [0, 1).
26
+ *
27
+ * #### Example
28
+ * ```ts
29
+ * const rng = createRng('lucky-number');
30
+ *
31
+ * const val1 = rng(); // same on server and client
32
+ * const val2 = rng(); // same on server and client
33
+ * ```
34
+ */
35
+ export function createRng(key, crelte = getCrelte()) {
36
+ const seed = crelte.ssrCache.takeOnce('_rng_' + key, () => Math.random());
37
+ // park miller algorithm
38
+ const MOD = 2147483647;
39
+ const MUL = 16807;
40
+ let state = Math.floor(seed * (MOD - 1)) + 1;
41
+ return () => {
42
+ state = (state * MUL) % MOD;
43
+ return (state - 1) / (MOD - 1);
44
+ };
45
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a random token of a specified length.
3
+ *
4
+ * @param length - The desired length of the token.
5
+ * @returns A random token.
6
+ */
7
+ export declare function randomToken(length?: number): string;
8
+ //# sourceMappingURL=internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../../../src/std/rand/internal.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,MAAM,GAAE,MAAU,GAAG,MAAM,CAMtD"}
@@ -0,0 +1,15 @@
1
+ const ALPHABET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
2
+ const ALPHABET_LENGTH = ALPHABET.length;
3
+ /**
4
+ * Generates a random token of a specified length.
5
+ *
6
+ * @param length - The desired length of the token.
7
+ * @returns A random token.
8
+ */
9
+ export function randomToken(length = 8) {
10
+ let s = '';
11
+ for (let i = 0; i < length; i++) {
12
+ s += ALPHABET[Math.floor(Math.random() * ALPHABET_LENGTH)];
13
+ }
14
+ return s;
15
+ }
@@ -20,7 +20,7 @@ export default class Writable<T> {
20
20
  * The function get's called once with the current value and then when the
21
21
  * values changes
22
22
  *
23
- * ## Note
23
+ * #### Note
24
24
  * This does not check for equality like svelte.
25
25
  *
26
26
  * @return a function which should be called to unsubscribe
@@ -23,7 +23,7 @@ export default class Writable {
23
23
  * The function get's called once with the current value and then when the
24
24
  * values changes
25
25
  *
26
- * ## Note
26
+ * #### Note
27
27
  * This does not check for equality like svelte.
28
28
  *
29
29
  * @return a function which should be called to unsubscribe
@@ -0,0 +1,28 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * ## Unstable
5
+ * The translations module is not yet stable. APIs may change without a major version bump.
6
+ */
7
+ import { createFileLoader } from './loader/fileLoader/fileLoader.js';
8
+ import { createGlobalLoader } from './loader/GlobalLoader.js';
9
+ import { type TranslationsLoader, type LoaderCreator } from './loader/index.js';
10
+ import loadTranslations from './loadTranslations.js';
11
+ import { createTranslations, getTranslationsPlugin, type TranslateFunction, type TranslateStore, type Translations, TranslationsPlugin, type TranslationsPluginOptions } from './translationsPlugin.js';
12
+ /**
13
+ * Creates a translate store for the given namespace.
14
+ *
15
+ * #### Example
16
+ * ```svelte
17
+ * <script>
18
+ * import { getTranslations } from 'crelte/translations';
19
+ *
20
+ * const t = getTranslations();
21
+ * </script>
22
+ *
23
+ * <h1>{$t('welcome_message')}</h1>
24
+ * ```
25
+ */
26
+ declare function getTranslations(namespace?: string): TranslateStore;
27
+ export { createTranslations, TranslationsPluginOptions, loadTranslations, getTranslations, getTranslationsPlugin, Translations, TranslationsPlugin, TranslationsLoader, LoaderCreator, TranslateStore, TranslateFunction, createFileLoader, createGlobalLoader, };
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/translations/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,gBAAgB,MAAM,uBAAuB,CAAC;AACrD,OAAO,EACN,kBAAkB,EAClB,qBAAqB,EACrB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,yBAAyB,EAC9B,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;;GAaG;AACH,iBAAS,eAAe,CAAC,SAAS,GAAE,MAAiB,GAAG,cAAc,CAGrE;AAED,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,gBAAgB,EAChB,eAAe,EACf,qBAAqB,EACrB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,iBAAiB,EAEjB,gBAAgB,EAChB,kBAAkB,GAClB,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * ## Unstable
5
+ * The translations module is not yet stable. APIs may change without a major version bump.
6
+ */
7
+ import { createFileLoader } from './loader/fileLoader/fileLoader.js';
8
+ import { createGlobalLoader } from './loader/GlobalLoader.js';
9
+ import loadTranslations from './loadTranslations.js';
10
+ import { createTranslations, getTranslationsPlugin, TranslationsPlugin, } from './translationsPlugin.js';
11
+ /**
12
+ * Creates a translate store for the given namespace.
13
+ *
14
+ * #### Example
15
+ * ```svelte
16
+ * <script>
17
+ * import { getTranslations } from 'crelte/translations';
18
+ *
19
+ * const t = getTranslations();
20
+ * </script>
21
+ *
22
+ * <h1>{$t('welcome_message')}</h1>
23
+ * ```
24
+ */
25
+ function getTranslations(namespace = 'common') {
26
+ const plugin = getTranslationsPlugin();
27
+ return plugin.z_createTranslateStore(namespace);
28
+ }
29
+ export { createTranslations, loadTranslations, getTranslations, getTranslationsPlugin, TranslationsPlugin,
30
+ // loaders
31
+ createFileLoader, createGlobalLoader, };
@@ -0,0 +1,23 @@
1
+ import { CrelteRequest } from '../crelte.js';
2
+ import { TranslateStore } from './translationsPlugin.js';
3
+ /**
4
+ * Creates a translate store and loads the specified namespace.
5
+ *
6
+ * #### Example
7
+ * ```svelte
8
+ * <script module>
9
+ * import { loadTranslations } from 'crelte/translations';
10
+ * export const loadData = {
11
+ * t: cr => loadTranslations(cr, 'customNamespace')
12
+ * };
13
+ * </script>
14
+ *
15
+ * <script>
16
+ * let { t } = $props();
17
+ * </script>
18
+ *
19
+ * <h1>{$t('welcome_message')}</h1>
20
+ * ```
21
+ */
22
+ export default function loadTranslations(cr: CrelteRequest, namespace: string): Promise<TranslateStore>;
23
+ //# sourceMappingURL=loadTranslations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loadTranslations.d.ts","sourceRoot":"","sources":["../../src/translations/loadTranslations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAyB,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEhF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAA8B,gBAAgB,CAC7C,EAAE,EAAE,aAAa,EACjB,SAAS,EAAE,MAAM,GACf,OAAO,CAAC,cAAc,CAAC,CAOzB"}
@@ -0,0 +1,26 @@
1
+ import { getTranslationsPlugin } from './translationsPlugin.js';
2
+ /**
3
+ * Creates a translate store and loads the specified namespace.
4
+ *
5
+ * #### Example
6
+ * ```svelte
7
+ * <script module>
8
+ * import { loadTranslations } from 'crelte/translations';
9
+ * export const loadData = {
10
+ * t: cr => loadTranslations(cr, 'customNamespace')
11
+ * };
12
+ * </script>
13
+ *
14
+ * <script>
15
+ * let { t } = $props();
16
+ * </script>
17
+ *
18
+ * <h1>{$t('welcome_message')}</h1>
19
+ * ```
20
+ */
21
+ export default async function loadTranslations(cr, namespace) {
22
+ const plugin = getTranslationsPlugin(cr);
23
+ // we don't need the return value here as it's cached
24
+ await plugin.load(cr, namespace);
25
+ return plugin.z_createTranslateStore(namespace);
26
+ }
@@ -0,0 +1,34 @@
1
+ import { Translations } from '../translationsPlugin.js';
2
+ import { Crelte, CrelteRequest } from '../../crelte.js';
3
+ import { LoaderCreator, TranslationsLoader } from '../index.js';
4
+ export default class GlobalLoader implements TranslationsLoader {
5
+ private handle;
6
+ constructor(_crelte: Crelte, opts: {
7
+ handle?: string;
8
+ });
9
+ load(cr: CrelteRequest, namespace: string): Promise<Translations>;
10
+ }
11
+ /**
12
+ * Creates a loader that loads translations from a global set.
13
+ *
14
+ * ```ts
15
+ * import { createTranslations, createGlobalLoader } from 'crelte/translations';
16
+ *
17
+ * createTranslations({ loader: createGlobalLoader() });
18
+ * ```
19
+ *
20
+ * Then in you're global export a `translations` globalSet with namespaces
21
+ * as fields, at least `common`.
22
+ *
23
+ * ```graphql
24
+ * translations: globalSet(handle: "translations", siteId: $siteId) {
25
+ * ... on translations_GlobalSet {
26
+ * common
27
+ * }
28
+ * }
29
+ */
30
+ export declare function createGlobalLoader(opts?: {
31
+ /** the handle for the global set containing the namespaces (default = translations) */
32
+ handle?: string;
33
+ }): LoaderCreator;
34
+ //# sourceMappingURL=GlobalLoader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GlobalLoader.d.ts","sourceRoot":"","sources":["../../../src/translations/loader/GlobalLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEhE,MAAM,CAAC,OAAO,OAAO,YAAa,YAAW,kBAAkB;IAC9D,OAAO,CAAC,MAAM,CAAS;gBAEX,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;IAIhD,IAAI,CAAC,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAsBvE;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CACjC,IAAI,GAAE;IACL,uFAAuF;IACvF,MAAM,CAAC,EAAE,MAAM,CAAC;CACX,GACJ,aAAa,CAEf"}
@@ -0,0 +1,45 @@
1
+ export default class GlobalLoader {
2
+ handle;
3
+ constructor(_crelte, opts) {
4
+ this.handle = opts.handle ?? 'translations';
5
+ }
6
+ async load(cr, namespace) {
7
+ const globalSet = await cr.globals.getAsync(this.handle);
8
+ if (!globalSet || typeof globalSet !== 'object')
9
+ throw new Error(`missing globals \`${this.handle}\``);
10
+ const data = globalSet[namespace];
11
+ if (!data)
12
+ throw new Error(`could not find \`${namespace}\` in globals \`${this.handle}\``);
13
+ if (typeof data !== 'string')
14
+ return data;
15
+ try {
16
+ return JSON.parse(data);
17
+ }
18
+ catch (e) {
19
+ throw new Error(`could not parse \`${this.handle}.${namespace}\` as json \n\n` +
20
+ (e instanceof Error ? e.message : ''));
21
+ }
22
+ }
23
+ }
24
+ /**
25
+ * Creates a loader that loads translations from a global set.
26
+ *
27
+ * ```ts
28
+ * import { createTranslations, createGlobalLoader } from 'crelte/translations';
29
+ *
30
+ * createTranslations({ loader: createGlobalLoader() });
31
+ * ```
32
+ *
33
+ * Then in you're global export a `translations` globalSet with namespaces
34
+ * as fields, at least `common`.
35
+ *
36
+ * ```graphql
37
+ * translations: globalSet(handle: "translations", siteId: $siteId) {
38
+ * ... on translations_GlobalSet {
39
+ * common
40
+ * }
41
+ * }
42
+ */
43
+ export function createGlobalLoader(opts = {}) {
44
+ return crelte => new GlobalLoader(crelte, opts);
45
+ }
@@ -0,0 +1,10 @@
1
+ import { CrelteRequest } from '../../../crelte.js';
2
+ import SsrCache from '../../../ssr/SsrCache.js';
3
+ import { Translations } from '../../translationsPlugin.js';
4
+ import { TranslationsLoader } from '../index.js';
5
+ export default class ClientFileLoader implements TranslationsLoader {
6
+ private readonly runNumber;
7
+ constructor(cache: SsrCache);
8
+ load(cr: CrelteRequest, namespace: string): Promise<Translations>;
9
+ }
10
+ //# sourceMappingURL=ClientFileLoader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClientFileLoader.d.ts","sourceRoot":"","sources":["../../../../src/translations/loader/fileLoader/ClientFileLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,QAAQ,MAAM,0BAA0B,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGjD,MAAM,CAAC,OAAO,OAAO,gBAAiB,YAAW,kBAAkB;IAClE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,KAAK,EAAE,QAAQ;IAIrB,IAAI,CAAC,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CA0BvE"}
@@ -0,0 +1,24 @@
1
+ import { SSR_RUN_NUMBER_KEY, translationFilePath } from './fileLoader.js';
2
+ export default class ClientFileLoader {
3
+ runNumber;
4
+ constructor(cache) {
5
+ this.runNumber = cache.get(SSR_RUN_NUMBER_KEY) ?? '';
6
+ }
7
+ async load(cr, namespace) {
8
+ const lang = cr.site.language;
9
+ try {
10
+ const resp = await fetch(`${translationFilePath(lang, namespace)}?run=${this.runNumber}`);
11
+ if (!resp.ok) {
12
+ throw new Error(`Failed to fetch translations file for ${lang}/${namespace}`);
13
+ }
14
+ // todo: validate data
15
+ const data = await resp.json();
16
+ return data;
17
+ }
18
+ catch (e) {
19
+ console.error(e);
20
+ throw new Error('There is something wrong with your translations file. \n\n' +
21
+ (e instanceof Error ? e.message : ''));
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,9 @@
1
+ import { Translations } from '../../translationsPlugin.js';
2
+ import SsrCache from '../../../ssr/SsrCache.js';
3
+ import { CrelteRequest } from '../../../crelte.js';
4
+ import { TranslationsLoader } from '../index.js';
5
+ export default class NodeFileLoader implements TranslationsLoader {
6
+ constructor(cache: SsrCache);
7
+ load(cr: CrelteRequest, namespace: string): Promise<Translations>;
8
+ }
9
+ //# sourceMappingURL=NodeFileLoader.d.ts.map