crelte 0.5.15 → 0.6.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blocks/Blocks.svelte +5 -4
- package/dist/blocks/Blocks.svelte.d.ts +4 -19
- package/dist/blocks/Blocks.svelte.d.ts.map +1 -1
- package/dist/crelte.d.ts +2 -2
- package/dist/crelte.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/init/InternalApp.d.ts.map +1 -1
- package/dist/init/InternalApp.js +2 -2
- package/dist/init/server.js +4 -4
- package/dist/loadData/loadData.d.ts +1 -1
- package/dist/loadData/loadData.d.ts.map +1 -1
- package/dist/queries/Queries.d.ts +9 -26
- package/dist/queries/Queries.d.ts.map +1 -1
- package/dist/queries/Queries.js +10 -20
- package/dist/queries/gql.d.ts +2 -2
- package/dist/queries/gql.d.ts.map +1 -1
- package/dist/queries/gql.js +3 -2
- package/dist/queries/index.d.ts +13 -13
- package/dist/queries/index.d.ts.map +1 -1
- package/dist/queries/index.js +2 -2
- package/dist/queries/vars.d.ts +3 -3
- package/dist/queries/vars.d.ts.map +1 -1
- package/dist/queries/vars.js +3 -3
- package/dist/routing/route/BaseRoute.d.ts.map +1 -1
- package/dist/routing/router/ClientRouter.d.ts.map +1 -1
- package/dist/routing/router/ClientRouter.js +3 -2
- package/dist/server/CrelteServer.d.ts +1 -1
- package/dist/server/CrelteServer.d.ts.map +1 -1
- package/dist/server/ServerRouter.d.ts +1 -1
- package/dist/server/ServerRouter.d.ts.map +1 -1
- package/dist/server/queries/QueriesCaching.d.ts +0 -1
- package/dist/server/queries/QueriesCaching.d.ts.map +1 -1
- package/dist/server/queries/QueriesCaching.js +0 -2
- package/dist/server/queries/QueryGqlRoute.d.ts +2 -0
- package/dist/server/queries/QueryGqlRoute.d.ts.map +1 -1
- package/dist/server/queries/QueryGqlRoute.js +16 -5
- package/dist/server/queries/QueryHandleRoute.d.ts +1 -2
- package/dist/server/queries/QueryHandleRoute.d.ts.map +1 -1
- package/dist/server/queries/QueryHandleRoute.js +2 -2
- package/dist/server/queries/queries.d.ts.map +1 -1
- package/dist/server/queries/queries.js +2 -1
- package/dist/server/queries/routes.d.ts +3 -4
- package/dist/server/queries/routes.d.ts.map +1 -1
- package/dist/server/queries/routes.js +3 -3
- package/dist/server/shared.d.ts +3 -3
- package/dist/server/shared.d.ts.map +1 -1
- package/dist/server/shared.js +6 -6
- package/dist/ssr/SsrCache.d.ts +2 -2
- package/dist/ssr/SsrCache.d.ts.map +1 -1
- package/dist/ssr/SsrCache.js +10 -16
- package/dist/std/encdec/escaping.d.ts +2 -0
- package/dist/std/encdec/escaping.d.ts.map +1 -0
- package/dist/std/encdec/escaping.js +41 -0
- package/dist/std/encdec/index.d.ts +13 -0
- package/dist/std/encdec/index.d.ts.map +1 -0
- package/dist/std/encdec/index.js +15 -0
- package/dist/std/index.d.ts.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +17 -0
- package/dist/vite/index.d.ts +1 -5
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +35 -76
- package/package.json +12 -7
- package/src/blocks/Blocks.svelte +4 -3
- package/src/crelte.ts +2 -2
- package/src/index.ts +2 -2
- package/src/init/InternalApp.ts +3 -4
- package/src/init/server.ts +4 -4
- package/src/loadData/loadData.ts +0 -2
- package/src/queries/Queries.ts +19 -35
- package/src/queries/gql.ts +7 -3
- package/src/queries/index.ts +19 -16
- package/src/queries/vars.ts +5 -7
- package/src/routing/route/BaseRoute.ts +1 -7
- package/src/routing/router/ClientRouter.ts +3 -1
- package/src/server/CrelteServer.ts +1 -1
- package/src/server/ServerRouter.ts +1 -1
- package/src/server/queries/QueriesCaching.ts +0 -2
- package/src/server/queries/QueryGqlRoute.ts +17 -10
- package/src/server/queries/QueryHandleRoute.ts +2 -6
- package/src/server/queries/queries.ts +2 -3
- package/src/server/queries/routes.ts +9 -6
- package/src/server/shared.ts +8 -8
- package/src/ssr/SsrCache.ts +13 -23
- package/src/std/encdec/escaping.ts +47 -0
- package/src/std/encdec/index.ts +16 -0
- package/src/std/index.ts +1 -6
- package/src/utils.ts +21 -0
- package/src/vite/index.ts +40 -85
- package/types.d.ts +1 -1
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
validateVars,
|
|
11
11
|
ValidIfFn,
|
|
12
12
|
} from './routes.js';
|
|
13
|
+
import { cryptoSha1Hash } from '../../utils.js';
|
|
13
14
|
|
|
14
15
|
export type QueryGqlArgs = {
|
|
15
16
|
vars: Record<string, QueryVar> | null;
|
|
@@ -23,6 +24,7 @@ export type QueryGqlArgs = {
|
|
|
23
24
|
export default class QueryGqlRoute {
|
|
24
25
|
name: string;
|
|
25
26
|
query: string;
|
|
27
|
+
queryHash: string;
|
|
26
28
|
vars: Record<string, QueryVar> | null;
|
|
27
29
|
validIfFn: ValidIfFn | null;
|
|
28
30
|
cacheIfFn: CacheIfFn | null;
|
|
@@ -39,6 +41,7 @@ export default class QueryGqlRoute {
|
|
|
39
41
|
|
|
40
42
|
this.name = name;
|
|
41
43
|
this.query = query;
|
|
44
|
+
this.queryHash = query;
|
|
42
45
|
this.vars = args.vars;
|
|
43
46
|
this.validIfFn = args.validIfFn;
|
|
44
47
|
this.cacheIfFn = args.cacheIfFn;
|
|
@@ -56,6 +59,10 @@ export default class QueryGqlRoute {
|
|
|
56
59
|
else this.fillBasicDefaults();
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
async init() {
|
|
63
|
+
this.queryHash = await cryptoSha1Hash(this.query);
|
|
64
|
+
}
|
|
65
|
+
|
|
59
66
|
private fillEntryDefaults() {
|
|
60
67
|
if (this.vars) return;
|
|
61
68
|
|
|
@@ -121,16 +128,11 @@ export default class QueryGqlRoute {
|
|
|
121
128
|
let vars: Record<string, any>;
|
|
122
129
|
try {
|
|
123
130
|
const reqVars = await csr.req.json();
|
|
124
|
-
vars = validateVars(
|
|
125
|
-
this.vars,
|
|
126
|
-
reqVars,
|
|
127
|
-
this.validIfFn,
|
|
128
|
-
caching.router,
|
|
129
|
-
);
|
|
131
|
+
vars = validateVars(this.vars, reqVars, this.validIfFn, csr);
|
|
130
132
|
|
|
131
|
-
if ('qName'
|
|
133
|
+
if (['qName', 'qHash', 'xCraftSite'].some(name => name in vars))
|
|
132
134
|
throw new Error(
|
|
133
|
-
'qName and xCraftSite are reserved variable names',
|
|
135
|
+
'qName, qHash and xCraftSite are reserved variable names',
|
|
134
136
|
);
|
|
135
137
|
} catch (e) {
|
|
136
138
|
return newError(e, 400);
|
|
@@ -158,7 +160,12 @@ export default class QueryGqlRoute {
|
|
|
158
160
|
let cacheKey: string | null = null;
|
|
159
161
|
const useCache = !previewToken && caching.isEnabled();
|
|
160
162
|
if (useCache) {
|
|
161
|
-
cacheKey = await calcKey({
|
|
163
|
+
cacheKey = await calcKey({
|
|
164
|
+
...vars,
|
|
165
|
+
qName: this.name,
|
|
166
|
+
qHash: this.queryHash,
|
|
167
|
+
xCraftSite,
|
|
168
|
+
});
|
|
162
169
|
const cached = await caching.getCache(cacheKey);
|
|
163
170
|
|
|
164
171
|
if (logInfo) console.log(`${logInfo} ${cached ? 'hit' : 'miss'}`);
|
|
@@ -223,7 +230,7 @@ export default class QueryGqlRoute {
|
|
|
223
230
|
|
|
224
231
|
// now we have a valid json resp.
|
|
225
232
|
// should we cache it?
|
|
226
|
-
if (cacheKey && this.cacheIfFn?.(jsonResp.data, vars)) {
|
|
233
|
+
if (cacheKey && this.cacheIfFn?.(jsonResp.data, vars, csr)) {
|
|
227
234
|
try {
|
|
228
235
|
await caching.setCache(cacheKey, jsonResp);
|
|
229
236
|
if (logInfo) console.log(logInfo + ' set cache');
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { QueryVar } from '../../queries/vars.js';
|
|
2
2
|
import CrelteServerRequest from '../CrelteServer.js';
|
|
3
|
-
import ServerRouter from '../ServerRouter.js';
|
|
4
3
|
import { HandleFn, newError, validateVars, ValidIfFn } from './routes.js';
|
|
5
4
|
|
|
6
5
|
// only internal
|
|
@@ -22,14 +21,11 @@ export default class QueryHandleRoute {
|
|
|
22
21
|
this.vars = vars;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
async handle(
|
|
26
|
-
cs: ServerRouter,
|
|
27
|
-
csr: CrelteServerRequest,
|
|
28
|
-
): Promise<Response> {
|
|
24
|
+
async handle(csr: CrelteServerRequest): Promise<Response> {
|
|
29
25
|
let vars: Record<string, any>;
|
|
30
26
|
try {
|
|
31
27
|
const reqVars = await csr.req.json();
|
|
32
|
-
vars = validateVars(this.vars, reqVars, this.validIfFn,
|
|
28
|
+
vars = validateVars(this.vars, reqVars, this.validIfFn, csr);
|
|
33
29
|
} catch (e) {
|
|
34
30
|
return newError(e, 400);
|
|
35
31
|
}
|
|
@@ -132,6 +132,7 @@ export async function initQueryRoutes(
|
|
|
132
132
|
);
|
|
133
133
|
|
|
134
134
|
const route = new QueryGqlRoute(name, rb.query, rb);
|
|
135
|
+
await route.init();
|
|
135
136
|
|
|
136
137
|
router.post('/queries/' + route.name, csr =>
|
|
137
138
|
route.handle(caching, csr),
|
|
@@ -147,9 +148,7 @@ export async function initQueryRoutes(
|
|
|
147
148
|
rb.vars,
|
|
148
149
|
);
|
|
149
150
|
|
|
150
|
-
router.post('/queries/' + route.name, csr =>
|
|
151
|
-
route.handle(caching.router, csr),
|
|
152
|
-
);
|
|
151
|
+
router.post('/queries/' + route.name, csr => route.handle(csr));
|
|
153
152
|
} else {
|
|
154
153
|
throw new Error(
|
|
155
154
|
`query js/ts ${name} file needs to either have ` +
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { QueryVar } from '../../queries/vars.js';
|
|
2
2
|
import CrelteServerRequest from '../CrelteServer.js';
|
|
3
|
-
import ServerRouter from '../ServerRouter.js';
|
|
4
3
|
|
|
5
|
-
export type CacheIfFn = (
|
|
4
|
+
export type CacheIfFn = (
|
|
5
|
+
response: any,
|
|
6
|
+
vars: Record<string, any>,
|
|
7
|
+
csr: CrelteServerRequest,
|
|
8
|
+
) => boolean;
|
|
6
9
|
|
|
7
10
|
/// Either throw or return a boolean
|
|
8
11
|
export type ValidIfFn = (
|
|
9
12
|
vars: Record<string, any>,
|
|
10
|
-
|
|
13
|
+
csr: CrelteServerRequest,
|
|
11
14
|
) => void | boolean;
|
|
12
15
|
|
|
13
16
|
/// Anything other than returning undefined will replace the response
|
|
@@ -33,7 +36,7 @@ export function validateVars(
|
|
|
33
36
|
qvars: Record<string, QueryVar> | null,
|
|
34
37
|
vars: any,
|
|
35
38
|
validIfFn: ValidIfFn | null,
|
|
36
|
-
|
|
39
|
+
csr: CrelteServerRequest,
|
|
37
40
|
): Record<string, any> {
|
|
38
41
|
if (!vars || typeof vars !== 'object')
|
|
39
42
|
throw new Error('expected an object as vars');
|
|
@@ -43,12 +46,12 @@ export function validateVars(
|
|
|
43
46
|
const nVars: Record<string, any> = {};
|
|
44
47
|
|
|
45
48
|
for (const [k, v] of Object.entries(qvars)) {
|
|
46
|
-
nVars[k] = v.validValue(vars[k],
|
|
49
|
+
nVars[k] = v.validValue(vars[k], csr);
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
if (validIfFn) {
|
|
50
53
|
// or throw
|
|
51
|
-
const valid = validIfFn(nVars,
|
|
54
|
+
const valid = validIfFn(nVars, csr);
|
|
52
55
|
if (typeof valid === 'boolean' && !valid)
|
|
53
56
|
throw new Error('invalid variables for query');
|
|
54
57
|
}
|
package/src/server/shared.ts
CHANGED
|
@@ -27,7 +27,7 @@ export type EnvData = {
|
|
|
27
27
|
env: Map<string, string>;
|
|
28
28
|
endpointUrl: string;
|
|
29
29
|
endpointToken?: string;
|
|
30
|
-
|
|
30
|
+
craftBaseCpUrl: string;
|
|
31
31
|
frontendUrl: string;
|
|
32
32
|
viteEnv: Map<string, string>;
|
|
33
33
|
};
|
|
@@ -47,8 +47,8 @@ export async function initEnvData(os: Platform): Promise<EnvData> {
|
|
|
47
47
|
|
|
48
48
|
const endpointToken = env.get('ENDPOINT_TOKEN');
|
|
49
49
|
|
|
50
|
-
const
|
|
51
|
-
if (!
|
|
50
|
+
const craftBaseCpUrl = env.get('CRAFT_BASE_CP_URL');
|
|
51
|
+
if (!craftBaseCpUrl) throw new Error('CRAFT_BASE_CP_URL not set');
|
|
52
52
|
|
|
53
53
|
const frontendUrl = env.get('FRONTEND_URL');
|
|
54
54
|
if (!frontendUrl) throw new Error('FRONTEND_URL not set');
|
|
@@ -62,7 +62,7 @@ export async function initEnvData(os: Platform): Promise<EnvData> {
|
|
|
62
62
|
env,
|
|
63
63
|
endpointUrl,
|
|
64
64
|
endpointToken,
|
|
65
|
-
|
|
65
|
+
craftBaseCpUrl,
|
|
66
66
|
frontendUrl,
|
|
67
67
|
viteEnv,
|
|
68
68
|
};
|
|
@@ -145,7 +145,7 @@ export type RenderRequest = {
|
|
|
145
145
|
htmlTemplate: string;
|
|
146
146
|
ssrManifest: Record<string, string[]>;
|
|
147
147
|
endpoint: string;
|
|
148
|
-
|
|
148
|
+
craftUrl: string;
|
|
149
149
|
frontend: string;
|
|
150
150
|
viteEnv: Map<string, string>;
|
|
151
151
|
sites: SiteFromGraphQl[];
|
|
@@ -170,7 +170,7 @@ export async function modRender(
|
|
|
170
170
|
htmlTemplate: template,
|
|
171
171
|
ssrManifest: {},
|
|
172
172
|
endpoint: env.endpointUrl,
|
|
173
|
-
|
|
173
|
+
craftUrl: env.craftBaseCpUrl,
|
|
174
174
|
frontend: env.frontendUrl,
|
|
175
175
|
viteEnv: env.viteEnv,
|
|
176
176
|
sites,
|
|
@@ -199,7 +199,7 @@ export type RenderErrorRequest = {
|
|
|
199
199
|
ssrManifest: Record<string, string[]>;
|
|
200
200
|
acceptLang: string | null;
|
|
201
201
|
endpoint: string;
|
|
202
|
-
|
|
202
|
+
craftUrl: string;
|
|
203
203
|
frontend: string;
|
|
204
204
|
viteEnv: Map<string, string>;
|
|
205
205
|
};
|
|
@@ -239,7 +239,7 @@ export async function modRenderError(
|
|
|
239
239
|
ssrManifest: {},
|
|
240
240
|
acceptLang,
|
|
241
241
|
endpoint: env.endpointUrl,
|
|
242
|
-
|
|
242
|
+
craftUrl: env.craftBaseCpUrl,
|
|
243
243
|
frontend: env.frontendUrl,
|
|
244
244
|
viteEnv: env.viteEnv,
|
|
245
245
|
...opts,
|
package/src/ssr/SsrCache.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { uneval } from 'devalue';
|
|
2
|
+
import { cryptoSha1Hash } from '../utils.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Calculates a key based on the json representation of the data
|
|
3
6
|
*
|
|
@@ -9,20 +12,7 @@ export async function calcKey(data: any): Promise<string> {
|
|
|
9
12
|
// specifically in the craft preview locally
|
|
10
13
|
if (!crypto?.subtle) return json;
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
const encoder = new TextEncoder();
|
|
14
|
-
const dataBuffer = encoder.encode(json);
|
|
15
|
-
|
|
16
|
-
// Use the Web Crypto API to hash the data with SHA-1
|
|
17
|
-
const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
|
|
18
|
-
|
|
19
|
-
// Convert the ArrayBuffer to a hex string
|
|
20
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer)); // Create an array from the buffer
|
|
21
|
-
const hashHex = hashArray
|
|
22
|
-
.map(b => b.toString(16).padStart(2, '0'))
|
|
23
|
-
.join(''); // Convert to hex string
|
|
24
|
-
|
|
25
|
-
return hashHex;
|
|
15
|
+
return await cryptoSha1Hash(json);
|
|
26
16
|
}
|
|
27
17
|
|
|
28
18
|
/**
|
|
@@ -147,15 +137,15 @@ export default class SsrCache {
|
|
|
147
137
|
delete window._SSR_STORE;
|
|
148
138
|
}
|
|
149
139
|
|
|
150
|
-
private exportAsJson(): string {
|
|
151
|
-
return JSON.stringify(Array.from(this.store.entries())).replace(
|
|
152
|
-
/</g,
|
|
153
|
-
'\\u003c',
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
140
|
/** @hidden */
|
|
158
|
-
|
|
159
|
-
return
|
|
141
|
+
z_toData(): any {
|
|
142
|
+
return this.store;
|
|
160
143
|
}
|
|
161
144
|
}
|
|
145
|
+
|
|
146
|
+
export function ssrCacheExportToHead(ssr: SsrCache) {
|
|
147
|
+
const data = Array.from(ssr.z_toData().entries());
|
|
148
|
+
|
|
149
|
+
// safety, uneval already escapes data: https://github.com/sveltejs/devalue#xss-mitigation
|
|
150
|
+
return `\n\t\t<script>window._SSR_STORE = ${uneval(data)};</script>`;
|
|
151
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Code from Svelte (https://github.com/sveltejs/svelte/blob/b4968584df6be22222f58c273bdd8e04307ad36b/packages/svelte/src/escaping.js)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2016-24 these people (https://github.com/sveltejs/svelte/graphs/contributors)
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ATTR_REGEX = /[&"<]/g;
|
|
26
|
+
const CONTENT_REGEX = /[&<]/g;
|
|
27
|
+
|
|
28
|
+
export function escape_html<V>(value: V, is_attr: boolean = false) {
|
|
29
|
+
const str = String(value ?? '');
|
|
30
|
+
|
|
31
|
+
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
|
|
32
|
+
pattern.lastIndex = 0;
|
|
33
|
+
|
|
34
|
+
let escaped = '';
|
|
35
|
+
let last = 0;
|
|
36
|
+
|
|
37
|
+
while (pattern.test(str)) {
|
|
38
|
+
const i = pattern.lastIndex - 1;
|
|
39
|
+
const ch = str[i];
|
|
40
|
+
escaped +=
|
|
41
|
+
str.substring(last, i) +
|
|
42
|
+
(ch === '&' ? '&' : ch === '"' ? '"' : '<');
|
|
43
|
+
last = i + 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return escaped + str.substring(last);
|
|
47
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { escape_html } from './escaping.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert newlines to <br> tags and escape HTML.
|
|
5
|
+
*
|
|
6
|
+
* #### Warning
|
|
7
|
+
* When used in an attribute, the `isAttr` parameter needs to be set to `true`.
|
|
8
|
+
*
|
|
9
|
+
* #### Example
|
|
10
|
+
* ```svelte
|
|
11
|
+
* {@html nl2br("Hello\nWorld")}
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function nl2br(str: string, isAttr: boolean = false): string {
|
|
15
|
+
return escape_html(str, isAttr).replace(/\n/g, '<br>');
|
|
16
|
+
}
|
package/src/std/index.ts
CHANGED
|
@@ -31,12 +31,7 @@ export interface Cloneable {
|
|
|
31
31
|
* A type that represents a value that can be cloned.
|
|
32
32
|
*/
|
|
33
33
|
export type CloneableOrPrimitive =
|
|
34
|
-
|
|
|
35
|
-
| string
|
|
36
|
-
| number
|
|
37
|
-
| boolean
|
|
38
|
-
| null
|
|
39
|
-
| undefined;
|
|
34
|
+
Cloneable | string | number | boolean | null | undefined;
|
|
40
35
|
|
|
41
36
|
/**
|
|
42
37
|
* Creates a clone of the provided value. The values need to be either
|
package/src/utils.ts
CHANGED
|
@@ -34,3 +34,24 @@ export function urlWithPath(url: string, path?: string): URL {
|
|
|
34
34
|
u.pathname = path ?? '';
|
|
35
35
|
return u;
|
|
36
36
|
}
|
|
37
|
+
|
|
38
|
+
export async function cryptoSha1Hash(str: string): Promise<string> {
|
|
39
|
+
// Convert the string data to an ArrayBuffer
|
|
40
|
+
const encoder = new TextEncoder();
|
|
41
|
+
const dataBuffer = encoder.encode(str);
|
|
42
|
+
|
|
43
|
+
// Use the Web Crypto API to hash the data with SHA-1
|
|
44
|
+
const hashBuffer = await crypto.subtle.digest('SHA-1', dataBuffer);
|
|
45
|
+
|
|
46
|
+
const bytes = new Uint8Array(hashBuffer);
|
|
47
|
+
// @ts-ignore using 2025 baseline feature
|
|
48
|
+
if (typeof bytes.toHex === 'function') return bytes.toHex();
|
|
49
|
+
|
|
50
|
+
// Convert the ArrayBuffer to a hex string
|
|
51
|
+
const hashArray = Array.from(bytes); // Create an array from the buffer
|
|
52
|
+
const hashHex = hashArray
|
|
53
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
54
|
+
.join(''); // Convert to hex string
|
|
55
|
+
|
|
56
|
+
return hashHex;
|
|
57
|
+
}
|
package/src/vite/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { relative } from 'node:path';
|
|
2
|
-
import MagicString from 'magic-string';
|
|
3
2
|
import {
|
|
4
3
|
ConfigEnv,
|
|
5
4
|
Plugin,
|
|
@@ -19,7 +18,6 @@ import {
|
|
|
19
18
|
modRenderError,
|
|
20
19
|
} from '../server/shared.js';
|
|
21
20
|
import ServerRouter from '../server/ServerRouter.js';
|
|
22
|
-
import sveltePackage from 'svelte/package.json' with { type: 'json' };
|
|
23
21
|
import { initQueryRoutes } from '../server/queries/queries.js';
|
|
24
22
|
import {
|
|
25
23
|
PLATFORM,
|
|
@@ -29,6 +27,11 @@ import {
|
|
|
29
27
|
import { SiteFromGraphQl } from '../routing/Site.js';
|
|
30
28
|
import { ServerResponse } from 'node:http';
|
|
31
29
|
import QueryError from '../queries/QueryError.js';
|
|
30
|
+
import {
|
|
31
|
+
CodeTransform,
|
|
32
|
+
generateTransform,
|
|
33
|
+
rolldownString,
|
|
34
|
+
} from 'rolldown-string';
|
|
32
35
|
|
|
33
36
|
const VIRT_MOD_ID = 'virtual:crelte/svelteComponents';
|
|
34
37
|
const RESOLVED_VIRT_MOD_ID = '\0' + VIRT_MOD_ID;
|
|
@@ -45,43 +48,33 @@ function usedSsrComponents(
|
|
|
45
48
|
code: string,
|
|
46
49
|
id: string,
|
|
47
50
|
options: { ssr?: boolean } | undefined,
|
|
48
|
-
|
|
49
|
-
): TransformResult | undefined {
|
|
51
|
+
): CodeTransform | undefined {
|
|
50
52
|
if (!options?.ssr || !id.endsWith('.svelte')) return;
|
|
51
53
|
|
|
52
54
|
const file = relative('.', id);
|
|
53
55
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
idx = code.indexOf(defFnSign);
|
|
58
|
-
if (idx < 0) return;
|
|
59
|
-
|
|
60
|
-
const withPropsIdx = code.indexOf('($$renderer, $$props) {', idx);
|
|
61
|
-
const noPropsIdx = code.indexOf('($$renderer) {', idx);
|
|
62
|
-
idx = withPropsIdx >= 0 ? withPropsIdx : noPropsIdx;
|
|
63
|
-
if (idx < 0) {
|
|
64
|
-
console.error('could not find svelte5 init', id);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
56
|
+
const defFnSign = 'export default function ';
|
|
57
|
+
let idx = code.indexOf(defFnSign);
|
|
58
|
+
if (idx < 0) return;
|
|
67
59
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (idx < 0) return;
|
|
75
|
-
idx += 1; // \n
|
|
76
|
-
} else {
|
|
77
|
-
const initFnSign =
|
|
78
|
-
'create_ssr_component(($$result, $$props, $$bindings, slots) => {';
|
|
79
|
-
idx = code.indexOf(initFnSign);
|
|
80
|
-
if (idx < 0) return;
|
|
81
|
-
idx += initFnSign.length;
|
|
60
|
+
const withPropsIdx = code.indexOf('($$renderer, $$props) {', idx);
|
|
61
|
+
const noPropsIdx = code.indexOf('($$renderer) {', idx);
|
|
62
|
+
idx = withPropsIdx >= 0 ? withPropsIdx : noPropsIdx;
|
|
63
|
+
if (idx < 0) {
|
|
64
|
+
console.error('could not find svelte5 init', id);
|
|
65
|
+
return;
|
|
82
66
|
}
|
|
83
67
|
|
|
84
|
-
const
|
|
68
|
+
const renderCompSign = '$$renderer.component(($$renderer) => {';
|
|
69
|
+
const renderCompIdx = code.indexOf(renderCompSign, idx);
|
|
70
|
+
if (renderCompIdx >= 0) idx = renderCompIdx;
|
|
71
|
+
|
|
72
|
+
// Find the end of the line where the pattern was found
|
|
73
|
+
idx = code.indexOf('\n', idx);
|
|
74
|
+
if (idx < 0) return;
|
|
75
|
+
idx += 1; // \n
|
|
76
|
+
|
|
77
|
+
const s = rolldownString(code, id, options);
|
|
85
78
|
|
|
86
79
|
s.prepend(`import { getContext as __modulesGetContext } from 'svelte';\n`);
|
|
87
80
|
|
|
@@ -95,14 +88,7 @@ if (ctx && ctx instanceof Set) {
|
|
|
95
88
|
`;
|
|
96
89
|
s.appendLeft(idx, ctxAdd);
|
|
97
90
|
|
|
98
|
-
return
|
|
99
|
-
map: s.generateMap({
|
|
100
|
-
source: id,
|
|
101
|
-
includeContent: true,
|
|
102
|
-
hires: 'boundary',
|
|
103
|
-
}),
|
|
104
|
-
code: s.toString(),
|
|
105
|
-
};
|
|
91
|
+
return generateTransform(s, id);
|
|
106
92
|
}
|
|
107
93
|
|
|
108
94
|
// transform graphql files into a GraphQlQuery which can called with
|
|
@@ -121,9 +107,7 @@ function graphQlFiles(
|
|
|
121
107
|
|
|
122
108
|
if (!ssr) {
|
|
123
109
|
return `
|
|
124
|
-
export default {
|
|
125
|
-
queryName: ${JSON.stringify(name)}
|
|
126
|
-
};
|
|
110
|
+
export default ${JSON.stringify(name)};
|
|
127
111
|
`;
|
|
128
112
|
}
|
|
129
113
|
|
|
@@ -132,9 +116,7 @@ export default {
|
|
|
132
116
|
.replace(/\u2029/g, '\\u2029');
|
|
133
117
|
|
|
134
118
|
return `
|
|
135
|
-
export default {
|
|
136
|
-
queryName: ${JSON.stringify(name)}
|
|
137
|
-
};
|
|
119
|
+
export default ${JSON.stringify(name)};
|
|
138
120
|
|
|
139
121
|
export const query = {
|
|
140
122
|
path: ${JSON.stringify(path)},
|
|
@@ -143,30 +125,14 @@ export const query = {
|
|
|
143
125
|
`;
|
|
144
126
|
}
|
|
145
127
|
|
|
146
|
-
function isSvelte5(): boolean {
|
|
147
|
-
if (sveltePackage && sveltePackage.version) {
|
|
148
|
-
return sveltePackage.version.startsWith('5.');
|
|
149
|
-
}
|
|
150
|
-
// Fallback or error handling if version cannot be determined
|
|
151
|
-
console.warn('Could not determine Svelte version. Assuming not Svelte 5.');
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
128
|
// outside of crelte because each build executes crelte again
|
|
156
129
|
let isSsrBuild = false;
|
|
157
130
|
|
|
158
|
-
export
|
|
159
|
-
/** If not specified tries to detect which version is installed */
|
|
160
|
-
svelte4?: boolean;
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
export default function crelte(opts?: CrelteOptions): Plugin {
|
|
131
|
+
export default function crelte(): Plugin {
|
|
164
132
|
let viteConfig: ResolvedConfig;
|
|
165
133
|
let viteConfigEnv: ConfigEnv;
|
|
166
134
|
let initialConfig: UserConfig;
|
|
167
135
|
|
|
168
|
-
const svelte5 = opts?.svelte4 ? false : isSvelte5();
|
|
169
|
-
|
|
170
136
|
return {
|
|
171
137
|
name: 'crelte',
|
|
172
138
|
|
|
@@ -229,33 +195,22 @@ export default function crelte(opts?: CrelteOptions): Plugin {
|
|
|
229
195
|
}
|
|
230
196
|
|
|
231
197
|
if (id === RESOLVED_VIRT_MOD_ID) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
);
|
|
243
|
-
} else {
|
|
244
|
-
return (
|
|
245
|
-
`export function internalSvelteMount(comp, options) {` +
|
|
246
|
-
`return new comp({ ...options, hydrate: true });` +
|
|
247
|
-
`}` +
|
|
248
|
-
`export function internalSvelteRender(comp, options) {` +
|
|
249
|
-
`return comp.render(options.props, options);` +
|
|
250
|
-
`}`
|
|
251
|
-
);
|
|
252
|
-
}
|
|
198
|
+
return (
|
|
199
|
+
`import { hydrate } from 'svelte';` +
|
|
200
|
+
`import { render } from 'svelte/server';` +
|
|
201
|
+
`export function internalSvelteMount(comp, options) {` +
|
|
202
|
+
`return hydrate(comp, options);` +
|
|
203
|
+
`}` +
|
|
204
|
+
`export function internalSvelteRender(comp, options) {` +
|
|
205
|
+
`return render(comp, options);` +
|
|
206
|
+
`}`
|
|
207
|
+
);
|
|
253
208
|
}
|
|
254
209
|
},
|
|
255
210
|
|
|
256
211
|
transform(code, id, options) {
|
|
257
212
|
return (
|
|
258
|
-
usedSsrComponents(code, id, options
|
|
213
|
+
usedSsrComponents(code, id, options) ||
|
|
259
214
|
graphQlFiles(code, id, !!options?.ssr)
|
|
260
215
|
);
|
|
261
216
|
},
|
package/types.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ declare module '*.graphql' {
|
|
|
2
2
|
// todo this would be a GraphQL query object
|
|
3
3
|
// but node doesn't import GraphQl and the type might change?
|
|
4
4
|
// this just removes the anoying typescript error
|
|
5
|
-
const value:
|
|
5
|
+
const value: string;
|
|
6
6
|
export default value;
|
|
7
7
|
|
|
8
8
|
export const query: {
|