@solidjs/start 2.0.0-alpha.1 → 2.0.0-alpha.3

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 (119) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/config/constants.d.ts +1 -1
  3. package/dist/config/constants.js +1 -1
  4. package/dist/config/dev-server.js +18 -0
  5. package/dist/config/env.d.ts +19 -0
  6. package/dist/config/env.js +81 -0
  7. package/dist/config/fs-routes/fs-watcher.js +14 -20
  8. package/dist/config/fs-routes/index.js +2 -2
  9. package/dist/config/fs-routes/router.js +7 -8
  10. package/dist/config/index.d.ts +16 -1
  11. package/dist/config/index.js +30 -29
  12. package/dist/config/lazy.js +35 -5
  13. package/dist/config/manifest.js +40 -6
  14. package/dist/config/vite-utils.d.ts +6 -1
  15. package/dist/config/vite-utils.js +7 -3
  16. package/dist/directives/bubble-function-declaration.d.ts +3 -0
  17. package/dist/directives/bubble-function-declaration.js +23 -0
  18. package/dist/directives/compile.d.ts +9 -0
  19. package/dist/directives/compile.js +40 -0
  20. package/dist/directives/generate-unique-name.d.ts +3 -0
  21. package/dist/directives/generate-unique-name.js +16 -0
  22. package/dist/directives/get-descriptive-name.d.ts +2 -0
  23. package/dist/directives/get-descriptive-name.js +37 -0
  24. package/dist/directives/get-import-identifier.d.ts +4 -0
  25. package/dist/directives/get-import-identifier.js +19 -0
  26. package/dist/directives/get-root-statement-path.d.ts +2 -0
  27. package/dist/directives/get-root-statement-path.js +12 -0
  28. package/dist/directives/index.d.ts +14 -0
  29. package/dist/directives/index.js +169 -0
  30. package/dist/directives/is-statement-top-level.d.ts +3 -0
  31. package/dist/directives/is-statement-top-level.js +9 -0
  32. package/dist/directives/paths.d.ts +9 -0
  33. package/dist/directives/paths.js +36 -0
  34. package/dist/directives/plugin.d.ts +21 -0
  35. package/dist/directives/plugin.js +302 -0
  36. package/dist/directives/remove-unused-variables.d.ts +3 -0
  37. package/dist/directives/remove-unused-variables.js +53 -0
  38. package/dist/directives/types.d.ts +10 -0
  39. package/dist/directives/xxhash32.d.ts +6 -0
  40. package/dist/directives/xxhash32.js +193 -0
  41. package/dist/{server/server-runtime.d.ts → fns/client.d.ts} +1 -1
  42. package/dist/fns/client.js +106 -0
  43. package/dist/fns/handler.d.ts +5 -0
  44. package/dist/{server/server-functions-handler.js → fns/handler.js} +55 -96
  45. package/dist/fns/registration.d.ts +2 -0
  46. package/dist/fns/registration.js +12 -0
  47. package/dist/fns/serialization.d.ts +6 -0
  48. package/dist/fns/serialization.js +208 -0
  49. package/dist/fns/server.d.ts +7 -0
  50. package/dist/{server/server-fns-runtime.js → fns/server.js} +7 -1
  51. package/dist/fns/shared.d.ts +17 -0
  52. package/dist/fns/shared.js +105 -0
  53. package/dist/http/index.d.ts +1 -1
  54. package/dist/middleware/index.d.ts +20 -0
  55. package/dist/middleware/index.js +21 -1
  56. package/dist/server/StartServer.jsx +2 -0
  57. package/dist/server/assets/PatchVirtualDevStyles.d.ts +4 -0
  58. package/dist/server/assets/PatchVirtualDevStyles.jsx +28 -0
  59. package/dist/server/collect-styles.js +9 -21
  60. package/dist/server/handler.d.ts +61 -3
  61. package/dist/server/handler.js +18 -15
  62. package/dist/server/spa/StartServer.jsx +2 -0
  63. package/dist/server/spa/handler.d.ts +30 -1
  64. package/dist/server/types.d.ts +1 -1
  65. package/dist/shared/dev-overlay/CodeView.jsx +4 -4
  66. package/dist/shared/dev-overlay/DevOverlayDialog.jsx +34 -34
  67. package/dist/shared/dev-overlay/index.jsx +2 -2
  68. package/dist/shared/dev-overlay/styles.css +40 -40
  69. package/package.json +27 -20
  70. package/src/config/constants.ts +1 -1
  71. package/src/config/dev-server.ts +12 -0
  72. package/src/config/env.ts +105 -0
  73. package/src/config/fs-routes/fs-watcher.ts +17 -24
  74. package/src/config/fs-routes/index.ts +2 -2
  75. package/src/config/fs-routes/router.ts +7 -8
  76. package/src/config/index.ts +44 -40
  77. package/src/config/lazy.ts +36 -5
  78. package/src/config/manifest.ts +40 -9
  79. package/src/config/vite-utils.ts +8 -4
  80. package/src/directives/bubble-function-declaration.ts +33 -0
  81. package/src/directives/compile.ts +54 -0
  82. package/src/directives/generate-unique-name.ts +22 -0
  83. package/src/directives/get-descriptive-name.ts +39 -0
  84. package/src/directives/get-import-identifier.ts +34 -0
  85. package/src/directives/get-root-statement-path.ts +14 -0
  86. package/src/directives/index.ts +232 -0
  87. package/src/directives/is-statement-top-level.ts +13 -0
  88. package/src/directives/paths.ts +63 -0
  89. package/src/directives/plugin.ts +389 -0
  90. package/src/directives/remove-unused-variables.ts +55 -0
  91. package/src/directives/types.ts +12 -0
  92. package/src/directives/xxhash32.ts +214 -0
  93. package/src/env.d.ts +1 -1
  94. package/src/fns/client.ts +141 -0
  95. package/src/fns/handler.spec.ts +85 -0
  96. package/src/{server/server-functions-handler.ts → fns/handler.ts} +65 -117
  97. package/src/fns/registration.ts +19 -0
  98. package/src/fns/serialization.ts +250 -0
  99. package/src/{server/server-fns-runtime.ts → fns/server.ts} +17 -2
  100. package/src/fns/shared.ts +116 -0
  101. package/src/middleware/index.ts +21 -1
  102. package/src/server/StartServer.tsx +2 -0
  103. package/src/server/assets/PatchVirtualDevStyles.tsx +31 -0
  104. package/src/server/collect-styles.ts +9 -26
  105. package/src/server/handler.ts +20 -16
  106. package/src/server/spa/StartServer.tsx +2 -0
  107. package/src/server/types.ts +1 -1
  108. package/src/shared/dev-overlay/CodeView.tsx +5 -7
  109. package/src/shared/dev-overlay/DevOverlayDialog.tsx +34 -34
  110. package/src/shared/dev-overlay/get-source-map.ts +1 -1
  111. package/src/shared/dev-overlay/index.tsx +4 -4
  112. package/src/shared/dev-overlay/styles.css +40 -40
  113. package/tsconfig.json +1 -0
  114. package/dist/server/fetchEvent.spec.js +0 -65
  115. package/dist/server/server-fns-runtime.d.ts +0 -1
  116. package/dist/server/server-functions-handler.d.ts +0 -2
  117. package/dist/server/server-runtime.js +0 -194
  118. package/src/server/server-runtime.ts +0 -240
  119. /package/dist/{server/fetchEvent.spec.d.ts → directives/types.js} +0 -0
@@ -0,0 +1,55 @@
1
+ import type * as babel from "@babel/core";
2
+ import * as t from "@babel/types";
3
+ import { isPathValid } from "./paths.ts";
4
+
5
+ export function removeUnusedVariables(program: babel.NodePath<t.Program>) {
6
+ // TODO(Alexis):
7
+ // This implementation is simple but slow
8
+ // We repeat removing unused variables from each pass
9
+ // until no potential unused variables are left.
10
+ // There might be a simpler implementation.
11
+ let dirty = true;
12
+
13
+ while (dirty) {
14
+ dirty = false;
15
+ program.traverse({
16
+ BindingIdentifier(path) {
17
+ const binding = path.scope.getBinding(path.node.name);
18
+
19
+ if (binding) {
20
+ switch (binding.kind) {
21
+ case "const":
22
+ case "let":
23
+ case "var":
24
+ case "hoisted":
25
+ case "module":
26
+ if (binding.references === 0 && !binding.path.removed) {
27
+ if (isPathValid(binding.path.parentPath, t.isImportDeclaration)) {
28
+ const parent = binding.path.parentPath;
29
+ if (parent.node.specifiers.length === 1) {
30
+ parent.remove();
31
+ } else {
32
+ binding.path.remove();
33
+ }
34
+ } else {
35
+ binding.path.remove();
36
+ }
37
+ dirty = true;
38
+ }
39
+ break;
40
+ case "local":
41
+ case "param":
42
+ case "unknown":
43
+ break;
44
+ }
45
+ }
46
+ },
47
+ VariableDeclaration(path) {
48
+ if (path.node.declarations.length === 0) {
49
+ path.remove();
50
+ }
51
+ },
52
+ });
53
+ program.scope.crawl();
54
+ }
55
+ }
@@ -0,0 +1,12 @@
1
+ export interface NamedImportDefinition {
2
+ kind: "named";
3
+ name: string;
4
+ source: string;
5
+ }
6
+
7
+ export interface DefaultImportDefinition {
8
+ kind: "default";
9
+ source: string;
10
+ }
11
+
12
+ export type ImportDefinition = DefaultImportDefinition | NamedImportDefinition;
@@ -0,0 +1,214 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Copyright (c) 2019 Jason Dent
4
+ * https://github.com/Jason3S/xxhash
5
+ */
6
+ const PRIME32_1 = 2654435761;
7
+ const PRIME32_2 = 2246822519;
8
+ const PRIME32_3 = 3266489917;
9
+ const PRIME32_4 = 668265263;
10
+ const PRIME32_5 = 374761393;
11
+
12
+ function toUtf8(text: string): Uint8Array {
13
+ const bytes: number[] = [];
14
+ for (let i = 0, n = text.length; i < n; ++i) {
15
+ const c = text.charCodeAt(i);
16
+ if (c < 0x80) {
17
+ bytes.push(c);
18
+ } else if (c < 0x800) {
19
+ bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
20
+ } else if (c < 0xd800 || c >= 0xe000) {
21
+ bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
22
+ } else {
23
+ const cp =
24
+ 0x10000 + (((c & 0x3ff) << 10) | (text.charCodeAt(++i) & 0x3ff));
25
+ bytes.push(
26
+ 0xf0 | ((cp >> 18) & 0x7),
27
+ 0x80 | ((cp >> 12) & 0x3f),
28
+ 0x80 | ((cp >> 6) & 0x3f),
29
+ 0x80 | (cp & 0x3f),
30
+ );
31
+ }
32
+ }
33
+ return new Uint8Array(bytes);
34
+ }
35
+ /**
36
+ *
37
+ * @param buffer - byte array or string
38
+ * @param seed - optional seed (32-bit unsigned);
39
+ */
40
+ export default function xxHash32(
41
+ buffer: Uint8Array | string,
42
+ seed = 0,
43
+ ): number {
44
+ buffer = typeof buffer === 'string' ? toUtf8(buffer) : buffer;
45
+ const b = buffer;
46
+
47
+ /*
48
+ Step 1. Initialize internal accumulators
49
+ Each accumulator gets an initial value based on optional seed input.
50
+ Since the seed is optional, it can be 0.
51
+ ```
52
+ u32 acc1 = seed + PRIME32_1 + PRIME32_2;
53
+ u32 acc2 = seed + PRIME32_2;
54
+ u32 acc3 = seed + 0;
55
+ u32 acc4 = seed - PRIME32_1;
56
+ ```
57
+ Special case : input is less than 16 bytes
58
+ When input is too small (< 16 bytes), the algorithm will not process any stripe.
59
+ Consequently, it will not make use of parallel accumulators.
60
+ In which case, a simplified initialization is performed, using a single accumulator :
61
+ u32 acc = seed + PRIME32_5;
62
+ The algorithm then proceeds directly to step 4.
63
+ */
64
+
65
+ let acc = (seed + PRIME32_5) & 0xffffffff;
66
+ let offset = 0;
67
+
68
+ if (b.length >= 16) {
69
+ const accN = [
70
+ (seed + PRIME32_1 + PRIME32_2) & 0xffffffff,
71
+ (seed + PRIME32_2) & 0xffffffff,
72
+ (seed + 0) & 0xffffffff,
73
+ (seed - PRIME32_1) & 0xffffffff,
74
+ ];
75
+
76
+ /*
77
+ Step 2. Process stripes
78
+ A stripe is a contiguous segment of 16 bytes. It is evenly divided into 4 lanes,
79
+ of 4 bytes each. The first lane is used to update accumulator 1, the second lane
80
+ is used to update accumulator 2, and so on. Each lane read its associated 32-bit
81
+ value using little-endian convention. For each {lane, accumulator}, the update
82
+ process is called a round, and applies the following formula :
83
+ ```
84
+ accN = accN + (laneN * PRIME32_2);
85
+ accN = accN <<< 13;
86
+ accN = accN * PRIME32_1;
87
+ ```
88
+ This shuffles the bits so that any bit from input lane impacts several bits in
89
+ output accumulator. All operations are performed modulo 2^32.
90
+ Input is consumed one full stripe at a time. Step 2 is looped as many times as
91
+ necessary to consume the whole input, except the last remaining bytes which cannot
92
+ form a stripe (< 16 bytes). When that happens, move to step 3.
93
+ */
94
+
95
+ const b = buffer;
96
+ const limit = b.length - 16;
97
+ let lane = 0;
98
+ for (offset = 0; (offset & 0xfffffff0) <= limit; offset += 4) {
99
+ const i = offset;
100
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
101
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
102
+ const laneNP = laneN0 * PRIME32_2 + ((laneN1 * PRIME32_2) << 16);
103
+ let acc = (accN[lane] + laneNP) & 0xffffffff;
104
+ acc = (acc << 13) | (acc >>> 19);
105
+ const acc0 = acc & 0xffff;
106
+ const acc1 = acc >>> 16;
107
+ accN[lane] = (acc0 * PRIME32_1 + ((acc1 * PRIME32_1) << 16)) & 0xffffffff;
108
+ lane = (lane + 1) & 0x3;
109
+ }
110
+
111
+ /*
112
+ Step 3. Accumulator convergence
113
+ All 4 lane accumulators from previous steps are merged to produce a
114
+ single remaining accumulator
115
+ of same width (32-bit). The associated formula is as follows :
116
+ ```
117
+ acc = (acc1 <<< 1) + (acc2 <<< 7) + (acc3 <<< 12) + (acc4 <<< 18);
118
+ ```
119
+ */
120
+ acc =
121
+ (((accN[0] << 1) | (accN[0] >>> 31)) +
122
+ ((accN[1] << 7) | (accN[1] >>> 25)) +
123
+ ((accN[2] << 12) | (accN[2] >>> 20)) +
124
+ ((accN[3] << 18) | (accN[3] >>> 14))) &
125
+ 0xffffffff;
126
+ }
127
+
128
+ /*
129
+ Step 4. Add input length
130
+ The input total length is presumed known at this stage.
131
+ This step is just about adding the length to
132
+ accumulator, so that it participates to final mixing.
133
+ ```
134
+ acc = acc + (u32)inputLength;
135
+ ```
136
+ */
137
+ acc = (acc + buffer.length) & 0xffffffff;
138
+
139
+ /*
140
+ Step 5. Consume remaining input
141
+ There may be up to 15 bytes remaining to consume from the input.
142
+ The final stage will digest them according
143
+ to following pseudo-code :
144
+ ```
145
+ while (remainingLength >= 4) {
146
+ lane = read_32bit_little_endian(input_ptr);
147
+ acc = acc + lane * PRIME32_3;
148
+ acc = (acc <<< 17) * PRIME32_4;
149
+ input_ptr += 4; remainingLength -= 4;
150
+ }
151
+ ```
152
+ This process ensures that all input bytes are present in the final mix.
153
+ */
154
+
155
+ const limit = buffer.length - 4;
156
+ for (; offset <= limit; offset += 4) {
157
+ const i = offset;
158
+ const laneN0 = b[i + 0] + (b[i + 1] << 8);
159
+ const laneN1 = b[i + 2] + (b[i + 3] << 8);
160
+ const laneP = laneN0 * PRIME32_3 + ((laneN1 * PRIME32_3) << 16);
161
+ acc = (acc + laneP) & 0xffffffff;
162
+ acc = (acc << 17) | (acc >>> 15);
163
+ acc =
164
+ ((acc & 0xffff) * PRIME32_4 + (((acc >>> 16) * PRIME32_4) << 16)) &
165
+ 0xffffffff;
166
+ }
167
+
168
+ /*
169
+ ```
170
+ while (remainingLength >= 1) {
171
+ lane = read_byte(input_ptr);
172
+ acc = acc + lane * PRIME32_5;
173
+ acc = (acc <<< 11) * PRIME32_1;
174
+ input_ptr += 1; remainingLength -= 1;
175
+ }
176
+ ```
177
+ */
178
+
179
+ for (; offset < b.length; ++offset) {
180
+ const lane = b[offset];
181
+ acc += lane * PRIME32_5;
182
+ acc = (acc << 11) | (acc >>> 21);
183
+ acc =
184
+ ((acc & 0xffff) * PRIME32_1 + (((acc >>> 16) * PRIME32_1) << 16)) &
185
+ 0xffffffff;
186
+ }
187
+
188
+ /*
189
+ Step 6. Final mix (avalanche)
190
+ The final mix ensures that all input bits have a chance to impact any bit in
191
+ the output digest, resulting in an unbiased distribution. This is also called
192
+ avalanche effect.
193
+ ```
194
+ acc = acc xor (acc >> 15);
195
+ acc = acc * PRIME32_2;
196
+ acc = acc xor (acc >> 13);
197
+ acc = acc * PRIME32_3;
198
+ acc = acc xor (acc >> 16);
199
+ ```
200
+ */
201
+
202
+ acc ^= acc >>> 15;
203
+ acc =
204
+ (((acc & 0xffff) * PRIME32_2) & 0xffffffff) +
205
+ (((acc >>> 16) * PRIME32_2) << 16);
206
+ acc ^= acc >>> 13;
207
+ acc =
208
+ (((acc & 0xffff) * PRIME32_3) & 0xffffffff) +
209
+ (((acc >>> 16) * PRIME32_3) << 16);
210
+ acc ^= acc >>> 16;
211
+
212
+ // turn any negatives back into a positive number;
213
+ return acc < 0 ? acc + 4294967296 : acc;
214
+ }
package/src/env.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  // This file contains global type definitions that are internal to SolidStart and are not exported
2
2
 
3
- declare module "solidstart:server-fn-manifest" {
3
+ declare module "solid-start:server-fn-manifest" {
4
4
  type ServerFn = (...args: Array<any>) => Promise<any>;
5
5
  export function getServerFnById(id: string): Promise<ServerFn>;
6
6
  }
@@ -0,0 +1,141 @@
1
+ import { type Component } from "solid-js";
2
+ import {
3
+ // serializeToJSONStream,
4
+ serializeToJSONString,
5
+ } from "./serialization.ts";
6
+ import {
7
+ BODY_FORMAT_KEY,
8
+ BodyFormat,
9
+ extractBody,
10
+ getHeadersAndBody,
11
+ } from "./shared.ts";
12
+
13
+ let INSTANCE = 0;
14
+
15
+ function createRequest(base: string, id: string, instance: string, options: RequestInit) {
16
+ return fetch(base, {
17
+ method: "POST",
18
+ ...options,
19
+ headers: {
20
+ ...options.headers,
21
+ "X-Server-Id": id,
22
+ "X-Server-Instance": instance,
23
+ },
24
+ });
25
+ }
26
+
27
+ async function initializeResponse(
28
+ base: string,
29
+ id: string,
30
+ instance: string,
31
+ options: RequestInit,
32
+ args: any[],
33
+ ) {
34
+ // No args, skip serialization
35
+ if (args.length === 0) {
36
+ return createRequest(base, id, instance, options);
37
+ }
38
+ // For single arguments, we can directly encode as body
39
+ if (args.length === 1) {
40
+ const body = args[0];
41
+ const result = getHeadersAndBody(body);
42
+ if (result) {
43
+ return createRequest(base, id, instance, {
44
+ ...options,
45
+ body: result.body,
46
+ headers: {
47
+ ...options.headers,
48
+ ...result.headers,
49
+ },
50
+ });
51
+ }
52
+ }
53
+ // Fallback to seroval
54
+ return createRequest(base, id, instance, {
55
+ ...options,
56
+ // TODO(Alexis): move to serializeToJSONStream
57
+ body: await serializeToJSONString(args),
58
+ // duplex: 'half',
59
+ // body: serializeToJSONStream(args),
60
+ headers: {
61
+ ...options.headers,
62
+ "Content-Type": "text/plain",
63
+ [BODY_FORMAT_KEY]: BodyFormat.Seroval,
64
+ },
65
+ });
66
+ }
67
+
68
+ async function fetchServerFunction(
69
+ base: string,
70
+ id: string,
71
+ options: Omit<RequestInit, "body">,
72
+ args: any[],
73
+ ) {
74
+ const instance = `server-fn:${INSTANCE++}`;
75
+
76
+ const response = await initializeResponse(base, id, instance, options, args);
77
+
78
+ if (
79
+ response.headers.has("Location") ||
80
+ response.headers.has("X-Revalidate") ||
81
+ response.headers.has("X-Single-Flight")
82
+ ) {
83
+ if (response.body) {
84
+ /* @ts-ignore-next-line */
85
+ response.customBody = async () => {
86
+ return await extractBody(instance, true, response.clone());
87
+ };
88
+ }
89
+ return response;
90
+ }
91
+
92
+ const result = await extractBody(instance, true, response.clone());
93
+ if (response.headers.has("X-Error")) {
94
+ throw result;
95
+ }
96
+ return result;
97
+ }
98
+
99
+ export function cloneServerReference(id: string) {
100
+ let baseURL = import.meta.env.BASE_URL ?? "/";
101
+ if (!baseURL.endsWith("/")) baseURL += "/";
102
+
103
+ const fn = (...args: any[]) => fetchServerFunction(`${baseURL}_server`, id, {}, args);
104
+
105
+ return new Proxy(fn, {
106
+ get(target, prop, receiver) {
107
+ if (prop === "url") {
108
+ return `${baseURL}_server?id=${encodeURIComponent(id)}`;
109
+ }
110
+ if (prop === "GET") {
111
+ return receiver.withOptions({ method: "GET" });
112
+ }
113
+ if (prop === "withOptions") {
114
+ const url = `${baseURL}_server?id=${encodeURIComponent(id)}`;
115
+ return (options: RequestInit) => {
116
+ const fn = async (...args: any[]) => {
117
+ const encodeArgs = options.method && options.method.toUpperCase() === "GET";
118
+ return fetchServerFunction(
119
+ encodeArgs
120
+ ? url +
121
+ (args.length
122
+ ? `&args=${encodeURIComponent(await serializeToJSONString(args))}`
123
+ : "")
124
+ : `${baseURL}_server`,
125
+ id,
126
+ options,
127
+ encodeArgs ? [] : args,
128
+ );
129
+ };
130
+ fn.url = url;
131
+ return fn;
132
+ };
133
+ }
134
+ return (target as any)[prop];
135
+ },
136
+ });
137
+ }
138
+
139
+ export function createClientReference(Component: Component<any>, id: string) {
140
+ return Component;
141
+ }
@@ -0,0 +1,85 @@
1
+ import { describe, expect, it, vi, beforeEach } from "vitest";
2
+ import type { FetchEvent } from "./types.ts";
3
+
4
+ vi.mock("h3", () => ({
5
+ parseCookies: vi.fn(() => ({})),
6
+ parseSetCookie: vi.fn(),
7
+ }));
8
+
9
+ vi.mock("solid-js/web", () => ({
10
+ renderToString: vi.fn(),
11
+ provideRequestEvent: vi.fn((_event, fn) => fn()),
12
+ }));
13
+
14
+ vi.mock("solid-start:server-fn-manifest", () => ({
15
+ getServerFnById: vi.fn(),
16
+ }));
17
+
18
+ vi.mock("./handler.ts", () => ({
19
+ createPageEvent: vi.fn(),
20
+ }));
21
+
22
+ vi.mock("./fetchEvent.ts", () => ({
23
+ getFetchEvent: vi.fn(),
24
+ mergeResponseHeaders: vi.fn(),
25
+ }));
26
+
27
+ function createMockFetchEvent(headers: Record<string, string> = {}): FetchEvent {
28
+ return {
29
+ request: new Request("http://localhost/test", { headers }),
30
+ response: {
31
+ headers: {
32
+ getSetCookie: () => [],
33
+ },
34
+ },
35
+ nativeEvent: {},
36
+ locals: {},
37
+ } as FetchEvent;
38
+ }
39
+
40
+ describe("createSingleFlightHeaders", () => {
41
+ let createSingleFlightHeaders: (sourceEvent: FetchEvent) => Headers;
42
+
43
+ beforeEach(async () => {
44
+ vi.clearAllMocks();
45
+ const module = await import("./server-functions-handler.ts");
46
+ createSingleFlightHeaders = module.createSingleFlightHeaders;
47
+ });
48
+
49
+ it("should create a new Headers object instead of returning the original", () => {
50
+ const sourceEvent = createMockFetchEvent({
51
+ "content-type": "application/json",
52
+ });
53
+
54
+ const result = createSingleFlightHeaders(sourceEvent);
55
+
56
+ expect(result).not.toBe(sourceEvent.request.headers);
57
+ });
58
+
59
+ it("should not mutate the original request headers", () => {
60
+ const originalHeaders = new Headers({
61
+ "content-type": "application/json",
62
+ "cookie": "session=abc123",
63
+ "cf-ray": "abc123",
64
+ "cf-cache-status": "HIT",
65
+ });
66
+ const sourceEvent: FetchEvent = {
67
+ request: new Request("http://localhost/test", { headers: originalHeaders }),
68
+ response: {
69
+ headers: {
70
+ getSetCookie: () => [],
71
+ },
72
+ },
73
+ nativeEvent: {},
74
+ locals: {},
75
+ } as FetchEvent;
76
+
77
+ const originalCookieHeader = sourceEvent.request.headers.get("cookie");
78
+ const originalCfRay = sourceEvent.request.headers.get("cf-ray");
79
+
80
+ createSingleFlightHeaders(sourceEvent);
81
+
82
+ expect(sourceEvent.request.headers.get("cookie")).toBe(originalCookieHeader);
83
+ expect(sourceEvent.request.headers.get("cf-ray")).toBe(originalCfRay);
84
+ });
85
+ });