UNPKG

4.92 kBJavaScriptView Raw
1/**
2 * react-router v8.0.0
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 if (hydrated[index]) {
54 set(hydrated[index]);
55 continue;
56 }
57 const value = values[index];
58 if (!value || typeof value !== "object") {
59 hydrated[index] = value;
60 set(value);
61 continue;
62 }
63 if (Array.isArray(value)) if (typeof value[0] === "string") {
64 const [type, b, c] = value;
65 switch (type) {
66 case "D":
67 set(hydrated[index] = new Date(b));
68 continue;
69 case "U":
70 set(hydrated[index] = new URL(b));
71 continue;
72 case "B":
73 set(hydrated[index] = BigInt(b));
74 continue;
75 case "R":
76 set(hydrated[index] = new RegExp(b, c));
77 continue;
78 case "Y":
79 set(hydrated[index] = Symbol.for(b));
80 continue;
81 case "S":
82 const newSet = /* @__PURE__ */ new Set();
83 hydrated[index] = newSet;
84 for (let i = value.length - 1; i > 0; i--) stack.push([value[i], (v) => {
85 newSet.add(v);
86 }]);
87 set(newSet);
88 continue;
89 case "M":
90 const map = /* @__PURE__ */ new Map();
91 hydrated[index] = map;
92 for (let i = value.length - 2; i > 0; i -= 2) {
93 const r = [];
94 stack.push([value[i + 1], (v) => {
95 r[1] = v;
96 }]);
97 stack.push([value[i], (k) => {
98 r[0] = k;
99 }]);
100 postRun.push(() => {
101 map.set(r[0], r[1]);
102 });
103 }
104 set(map);
105 continue;
106 case "N":
107 const obj = Object.create(null);
108 hydrated[index] = obj;
109 for (const key of Object.keys(b).reverse()) {
110 const r = [];
111 stack.push([b[key], (v) => {
112 r[1] = v;
113 }]);
114 stack.push([Number(key.slice(1)), (k) => {
115 r[0] = k;
116 }]);
117 postRun.push(() => {
118 obj[r[0]] = r[1];
119 });
120 }
121 set(obj);
122 continue;
123 case "P":
124 if (hydrated[b]) set(hydrated[index] = hydrated[b]);
125 else {
126 const d = new Deferred();
127 deferred[b] = d;
128 set(hydrated[index] = d.promise);
129 }
130 continue;
131 case "E":
132 const [, message, errorType] = value;
133 let error = errorType && globalObj && SUPPORTED_ERROR_TYPES.includes(errorType) && errorType in globalObj && typeof globalObj[errorType] === "function" ? new globalObj[errorType](message) : new Error(message);
134 hydrated[index] = error;
135 set(error);
136 continue;
137 case "Z":
138 set(hydrated[index] = hydrated[b]);
139 continue;
140 default:
141 if (Array.isArray(plugins)) {
142 const r = [];
143 const vals = value.slice(1);
144 for (let i = 0; i < vals.length; i++) {
145 const v = vals[i];
146 stack.push([v, (v) => {
147 r[i] = v;
148 }]);
149 }
150 postRun.push(() => {
151 for (const plugin of plugins) {
152 const result = plugin(value[0], ...r);
153 if (result) {
154 set(hydrated[index] = result.value);
155 return;
156 }
157 }
158 throw new SyntaxError();
159 });
160 continue;
161 }
162 throw new SyntaxError();
163 }
164 } else {
165 const array = [];
166 hydrated[index] = array;
167 for (let i = 0; i < value.length; i++) {
168 const n = value[i];
169 if (n !== -1) stack.push([n, (v) => {
170 array[i] = v;
171 }]);
172 }
173 set(array);
174 continue;
175 }
176 else {
177 const object = {};
178 hydrated[index] = object;
179 for (const key of Object.keys(value).reverse()) {
180 const r = [];
181 stack.push([value[key], (v) => {
182 r[1] = v;
183 }]);
184 stack.push([Number(key.slice(1)), (k) => {
185 r[0] = k;
186 }]);
187 postRun.push(() => {
188 object[r[0]] = r[1];
189 });
190 }
191 set(object);
192 continue;
193 }
194 }
195 while (postRun.length > 0) postRun.pop()();
196 return result;
197}
198//#endregion
199export { unflatten };