UNPKG

5.54 kB JavaScript View Raw
1/**
2 * react-router v8.5.1
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import { SUPPORTED_ERROR_TYPES } from "../../lib/router/utils.js";
12import { Deferred } from "./utils.js";
13//#region vendor/turbo-stream-v2/unflatten.ts
14const globalObj = typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : void 0;
15function unflatten(parsed) {
16 const { hydrated, values } = this;
17 if (typeof parsed === "number") return hydrate.call(this, parsed);
18 if (!Array.isArray(parsed) || !parsed.length) throw new SyntaxError();
19 const startIndex = values.length;
20 for (const value of parsed) values.push(value);
21 hydrated.length = values.length;
22 return hydrate.call(this, startIndex);
23}
24function hydrate(index) {
25 const { hydrated, values, deferred, plugins } = this;
26 let result;
27 const stack = [[index, (v) => {
28 result = v;
29 }]];
30 let postRun = [];
31 while (stack.length > 0) {
32 const [index, set] = stack.pop();
33 switch (index) {
34 case -7:
35 set(void 0);
36 continue;
37 case -5:
38 set(null);
39 continue;
40 case -2:
41 set(NaN);
42 continue;
43 case -6:
44 set(Infinity);
45 continue;
46 case -3:
47 set(-Infinity);
48 continue;
49 case -4:
50 set(-0);
51 continue;
52 }
53 validateIndex(index, values.length);
54 if (hydrated[index]) {
55 set(hydrated[index]);
56 continue;
57 }
58 const value = values[index];
59 if (!value || typeof value !== "object") {
60 hydrated[index] = value;
61 set(value);
62 continue;
63 }
64 if (Array.isArray(value)) if (typeof value[0] === "string") {
65 const [type, b, c] = value;
66 switch (type) {
67 case "D":
68 set(hydrated[index] = new Date(b));
69 continue;
70 case "U":
71 set(hydrated[index] = new URL(b));
72 continue;
73 case "B":
74 set(hydrated[index] = BigInt(b));
75 continue;
76 case "R":
77 set(hydrated[index] = new RegExp(b, c));
78 continue;
79 case "Y":
80 set(hydrated[index] = Symbol.for(b));
81 continue;
82 case "S":
83 const newSet = /* @__PURE__ */ new Set();
84 hydrated[index] = newSet;
85 for (let i = value.length - 1; i > 0; i--) stack.push([value[i], (v) => {
86 newSet.add(v);
87 }]);
88 set(newSet);
89 continue;
90 case "M":
91 const map = /* @__PURE__ */ new Map();
92 hydrated[index] = map;
93 for (let i = value.length - 2; i > 0; i -= 2) {
94 const r = [];
95 stack.push([value[i + 1], (v) => {
96 r[1] = v;
97 }]);
98 stack.push([value[i], (k) => {
99 r[0] = k;
100 }]);
101 postRun.push(() => {
102 map.set(r[0], r[1]);
103 });
104 }
105 set(map);
106 continue;
107 case "N":
108 const obj = Object.create(null);
109 hydrated[index] = obj;
110 for (const key of Object.keys(b).reverse()) {
111 const r = [];
112 stack.push([b[key], (v) => {
113 r[1] = v;
114 }]);
115 stack.push([Number(key.slice(1)), (k) => {
116 r[0] = k;
117 }]);
118 postRun.push(() => {
119 obj[r[0]] = r[1];
120 });
121 }
122 set(obj);
123 continue;
124 case "P":
125 validateIndex(b, values.length);
126 if (hydrated[b]) set(hydrated[index] = hydrated[b]);
127 else {
128 const d = new Deferred();
129 const promise = d.promise;
130 promise.status = "pending";
131 promise.then((value) => {
132 promise.status = "fulfilled";
133 promise.value = value;
134 }, (reason) => {
135 promise.status = "rejected";
136 promise.reason = reason;
137 });
138 deferred[b] = d;
139 set(hydrated[index] = promise);
140 }
141 continue;
142 case "E":
143 const [, message, errorType] = value;
144 let error = errorType && globalObj && SUPPORTED_ERROR_TYPES.includes(errorType) && errorType in globalObj && typeof globalObj[errorType] === "function" ? new globalObj[errorType](message) : new Error(message);
145 hydrated[index] = error;
146 set(error);
147 continue;
148 case "Z":
149 validateIndex(b, values.length);
150 set(hydrated[index] = hydrated[b]);
151 continue;
152 default:
153 if (Array.isArray(plugins)) {
154 const r = [];
155 const vals = value.slice(1);
156 for (let i = 0; i < vals.length; i++) {
157 const v = vals[i];
158 stack.push([v, (v) => {
159 r[i] = v;
160 }]);
161 }
162 postRun.push(() => {
163 for (const plugin of plugins) {
164 const result = plugin(value[0], ...r);
165 if (result) {
166 set(hydrated[index] = result.value);
167 return;
168 }
169 }
170 throw new SyntaxError();
171 });
172 continue;
173 }
174 throw new SyntaxError();
175 }
176 } else {
177 const array = [];
178 hydrated[index] = array;
179 for (let i = 0; i < value.length; i++) {
180 const n = value[i];
181 if (n !== -1) stack.push([n, (v) => {
182 array[i] = v;
183 }]);
184 }
185 set(array);
186 continue;
187 }
188 else {
189 const object = {};
190 hydrated[index] = object;
191 for (const key of Object.keys(value).reverse()) {
192 const r = [];
193 stack.push([value[key], (v) => {
194 r[1] = v;
195 }]);
196 stack.push([Number(key.slice(1)), (k) => {
197 r[0] = k;
198 }]);
199 postRun.push(() => {
200 Object.defineProperty(object, r[0], {
201 value: r[1],
202 enumerable: true,
203 configurable: true,
204 writable: true
205 });
206 });
207 }
208 set(object);
209 continue;
210 }
211 }
212 while (postRun.length > 0) postRun.pop()();
213 return result;
214}
215function validateIndex(index, length) {
216 if (!Number.isSafeInteger(index) || index < 0 || index >= length) throw new SyntaxError();
217}
218//#endregion
219export { unflatten };