turbo-stream 2.2.1 → 2.2.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.
- package/dist/flatten.js +118 -114
- package/dist/turbo-stream.mjs +307 -193
- package/dist/unflatten.js +211 -95
- package/package.json +1 -1
package/dist/flatten.js
CHANGED
|
@@ -28,135 +28,139 @@ exports.flatten = flatten;
|
|
|
28
28
|
function stringify(input, index) {
|
|
29
29
|
const { deferred, plugins } = this;
|
|
30
30
|
const str = this.stringified;
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (!input) {
|
|
52
|
-
str[index] = `${utils_js_1.NULL}`;
|
|
31
|
+
const stack = [[input, index]];
|
|
32
|
+
while (stack.length > 0) {
|
|
33
|
+
const [input, index] = stack.pop();
|
|
34
|
+
const partsForObj = (obj) => Object.keys(obj)
|
|
35
|
+
.map((k) => `"${flatten.call(this, k)}":${flatten.call(this, obj[k])}`)
|
|
36
|
+
.join(",");
|
|
37
|
+
switch (typeof input) {
|
|
38
|
+
case "boolean":
|
|
39
|
+
case "number":
|
|
40
|
+
case "string":
|
|
41
|
+
str[index] = JSON.stringify(input);
|
|
42
|
+
break;
|
|
43
|
+
case "bigint":
|
|
44
|
+
str[index] = `["${utils_js_1.TYPE_BIGINT}","${input}"]`;
|
|
45
|
+
break;
|
|
46
|
+
case "symbol": {
|
|
47
|
+
const keyFor = Symbol.keyFor(input);
|
|
48
|
+
if (!keyFor)
|
|
49
|
+
throw new Error("Cannot encode symbol unless created with Symbol.for()");
|
|
50
|
+
str[index] = `["${utils_js_1.TYPE_SYMBOL}",${JSON.stringify(keyFor)}]`;
|
|
53
51
|
break;
|
|
54
52
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
53
|
+
case "object": {
|
|
54
|
+
if (!input) {
|
|
55
|
+
str[index] = `${utils_js_1.NULL}`;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
const isArray = Array.isArray(input);
|
|
59
|
+
let pluginHandled = false;
|
|
60
|
+
if (!isArray && plugins) {
|
|
61
|
+
for (const plugin of plugins) {
|
|
62
|
+
const pluginResult = plugin(input);
|
|
63
|
+
if (Array.isArray(pluginResult)) {
|
|
64
|
+
pluginHandled = true;
|
|
65
|
+
const [pluginIdentifier, ...rest] = pluginResult;
|
|
66
|
+
str[index] = `[${JSON.stringify(pluginIdentifier)}`;
|
|
67
|
+
if (rest.length > 0) {
|
|
68
|
+
str[index] += `,${rest
|
|
69
|
+
.map((v) => flatten.call(this, v))
|
|
70
|
+
.join(",")}`;
|
|
71
|
+
}
|
|
72
|
+
str[index] += "]";
|
|
73
|
+
break;
|
|
68
74
|
}
|
|
69
|
-
str[index] += "]";
|
|
70
|
-
break;
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
str[index] = `${result}]`;
|
|
82
|
-
}
|
|
83
|
-
else if (input instanceof Date) {
|
|
84
|
-
str[index] = `["${utils_js_1.TYPE_DATE}",${input.getTime()}]`;
|
|
85
|
-
}
|
|
86
|
-
else if (input instanceof URL) {
|
|
87
|
-
str[index] = `["${utils_js_1.TYPE_URL}",${JSON.stringify(input.href)}]`;
|
|
88
|
-
}
|
|
89
|
-
else if (input instanceof RegExp) {
|
|
90
|
-
str[index] = `["${utils_js_1.TYPE_REGEXP}",${JSON.stringify(input.source)},${JSON.stringify(input.flags)}]`;
|
|
91
|
-
}
|
|
92
|
-
else if (input instanceof Set) {
|
|
93
|
-
if (input.size > 0) {
|
|
94
|
-
str[index] = `["${utils_js_1.TYPE_SET}",${[...input]
|
|
95
|
-
.map((val) => flatten.call(this, val))
|
|
96
|
-
.join(",")}]`;
|
|
77
|
+
if (!pluginHandled) {
|
|
78
|
+
let result = isArray ? "[" : "{";
|
|
79
|
+
if (isArray) {
|
|
80
|
+
for (let i = 0; i < input.length; i++)
|
|
81
|
+
result +=
|
|
82
|
+
(i ? "," : "") +
|
|
83
|
+
(i in input ? flatten.call(this, input[i]) : utils_js_1.HOLE);
|
|
84
|
+
str[index] = `${result}]`;
|
|
97
85
|
}
|
|
98
|
-
else {
|
|
99
|
-
str[index] = `["${utils_js_1.
|
|
86
|
+
else if (input instanceof Date) {
|
|
87
|
+
str[index] = `["${utils_js_1.TYPE_DATE}",${input.getTime()}]`;
|
|
100
88
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (input.size > 0) {
|
|
104
|
-
str[index] = `["${utils_js_1.TYPE_MAP}",${[...input]
|
|
105
|
-
.flatMap(([k, v]) => [
|
|
106
|
-
flatten.call(this, k),
|
|
107
|
-
flatten.call(this, v),
|
|
108
|
-
])
|
|
109
|
-
.join(",")}]`;
|
|
89
|
+
else if (input instanceof URL) {
|
|
90
|
+
str[index] = `["${utils_js_1.TYPE_URL}",${JSON.stringify(input.href)}]`;
|
|
110
91
|
}
|
|
111
|
-
else {
|
|
112
|
-
str[index] = `["${utils_js_1.
|
|
92
|
+
else if (input instanceof RegExp) {
|
|
93
|
+
str[index] = `["${utils_js_1.TYPE_REGEXP}",${JSON.stringify(input.source)},${JSON.stringify(input.flags)}]`;
|
|
113
94
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
95
|
+
else if (input instanceof Set) {
|
|
96
|
+
if (input.size > 0) {
|
|
97
|
+
str[index] = `["${utils_js_1.TYPE_SET}",${[...input]
|
|
98
|
+
.map((val) => flatten.call(this, val))
|
|
99
|
+
.join(",")}]`;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
str[index] = `["${utils_js_1.TYPE_SET}"]`;
|
|
103
|
+
}
|
|
123
104
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
pluginHandled = true;
|
|
146
|
-
const [pluginIdentifier, ...rest] = pluginResult;
|
|
147
|
-
str[index] = `[${JSON.stringify(pluginIdentifier)}`;
|
|
148
|
-
if (rest.length > 0) {
|
|
149
|
-
str[index] += `,${rest
|
|
150
|
-
.map((v) => flatten.call(this, v))
|
|
151
|
-
.join(",")}`;
|
|
105
|
+
else if (input instanceof Map) {
|
|
106
|
+
if (input.size > 0) {
|
|
107
|
+
str[index] = `["${utils_js_1.TYPE_MAP}",${[...input]
|
|
108
|
+
.flatMap(([k, v]) => [
|
|
109
|
+
flatten.call(this, k),
|
|
110
|
+
flatten.call(this, v),
|
|
111
|
+
])
|
|
112
|
+
.join(",")}]`;
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
str[index] = `["${utils_js_1.TYPE_MAP}"]`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if (input instanceof Promise) {
|
|
119
|
+
str[index] = `["${utils_js_1.TYPE_PROMISE}",${index}]`;
|
|
120
|
+
deferred[index] = input;
|
|
121
|
+
}
|
|
122
|
+
else if (input instanceof Error) {
|
|
123
|
+
str[index] = `["${utils_js_1.TYPE_ERROR}",${JSON.stringify(input.message)}`;
|
|
124
|
+
if (input.name !== "Error") {
|
|
125
|
+
str[index] += `,${JSON.stringify(input.name)}`;
|
|
152
126
|
}
|
|
153
127
|
str[index] += "]";
|
|
154
|
-
|
|
128
|
+
}
|
|
129
|
+
else if (Object.getPrototypeOf(input) === null) {
|
|
130
|
+
str[index] = `["${utils_js_1.TYPE_NULL_OBJECT}",{${partsForObj(input)}}]`;
|
|
131
|
+
}
|
|
132
|
+
else if (isPlainObject(input)) {
|
|
133
|
+
str[index] = `{${partsForObj(input)}}`;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
throw new Error("Cannot encode object with prototype");
|
|
155
137
|
}
|
|
156
138
|
}
|
|
139
|
+
break;
|
|
157
140
|
}
|
|
158
|
-
|
|
159
|
-
|
|
141
|
+
default: {
|
|
142
|
+
const isArray = Array.isArray(input);
|
|
143
|
+
let pluginHandled = false;
|
|
144
|
+
if (!isArray && plugins) {
|
|
145
|
+
for (const plugin of plugins) {
|
|
146
|
+
const pluginResult = plugin(input);
|
|
147
|
+
if (Array.isArray(pluginResult)) {
|
|
148
|
+
pluginHandled = true;
|
|
149
|
+
const [pluginIdentifier, ...rest] = pluginResult;
|
|
150
|
+
str[index] = `[${JSON.stringify(pluginIdentifier)}`;
|
|
151
|
+
if (rest.length > 0) {
|
|
152
|
+
str[index] += `,${rest
|
|
153
|
+
.map((v) => flatten.call(this, v))
|
|
154
|
+
.join(",")}`;
|
|
155
|
+
}
|
|
156
|
+
str[index] += "]";
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (!pluginHandled) {
|
|
162
|
+
throw new Error("Cannot encode function or unexpected type");
|
|
163
|
+
}
|
|
160
164
|
}
|
|
161
165
|
}
|
|
162
166
|
}
|
package/dist/turbo-stream.mjs
CHANGED
|
@@ -74,115 +74,119 @@ function flatten(input) {
|
|
|
74
74
|
function stringify(input, index) {
|
|
75
75
|
const { deferred, plugins } = this;
|
|
76
76
|
const str = this.stringified;
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (!input) {
|
|
98
|
-
str[index] = `${NULL}`;
|
|
77
|
+
const stack = [[input, index]];
|
|
78
|
+
while (stack.length > 0) {
|
|
79
|
+
const [input2, index2] = stack.pop();
|
|
80
|
+
const partsForObj = (obj) => Object.keys(obj).map((k) => `"${flatten.call(this, k)}":${flatten.call(this, obj[k])}`).join(",");
|
|
81
|
+
switch (typeof input2) {
|
|
82
|
+
case "boolean":
|
|
83
|
+
case "number":
|
|
84
|
+
case "string":
|
|
85
|
+
str[index2] = JSON.stringify(input2);
|
|
86
|
+
break;
|
|
87
|
+
case "bigint":
|
|
88
|
+
str[index2] = `["${TYPE_BIGINT}","${input2}"]`;
|
|
89
|
+
break;
|
|
90
|
+
case "symbol": {
|
|
91
|
+
const keyFor = Symbol.keyFor(input2);
|
|
92
|
+
if (!keyFor)
|
|
93
|
+
throw new Error(
|
|
94
|
+
"Cannot encode symbol unless created with Symbol.for()"
|
|
95
|
+
);
|
|
96
|
+
str[index2] = `["${TYPE_SYMBOL}",${JSON.stringify(keyFor)}]`;
|
|
99
97
|
break;
|
|
100
98
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
99
|
+
case "object": {
|
|
100
|
+
if (!input2) {
|
|
101
|
+
str[index2] = `${NULL}`;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
const isArray = Array.isArray(input2);
|
|
105
|
+
let pluginHandled = false;
|
|
106
|
+
if (!isArray && plugins) {
|
|
107
|
+
for (const plugin of plugins) {
|
|
108
|
+
const pluginResult = plugin(input2);
|
|
109
|
+
if (Array.isArray(pluginResult)) {
|
|
110
|
+
pluginHandled = true;
|
|
111
|
+
const [pluginIdentifier, ...rest] = pluginResult;
|
|
112
|
+
str[index2] = `[${JSON.stringify(pluginIdentifier)}`;
|
|
113
|
+
if (rest.length > 0) {
|
|
114
|
+
str[index2] += `,${rest.map((v) => flatten.call(this, v)).join(",")}`;
|
|
115
|
+
}
|
|
116
|
+
str[index2] += "]";
|
|
117
|
+
break;
|
|
112
118
|
}
|
|
113
|
-
str[index] += "]";
|
|
114
|
-
break;
|
|
115
119
|
}
|
|
116
120
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
121
|
+
if (!pluginHandled) {
|
|
122
|
+
let result = isArray ? "[" : "{";
|
|
123
|
+
if (isArray) {
|
|
124
|
+
for (let i = 0; i < input2.length; i++)
|
|
125
|
+
result += (i ? "," : "") + (i in input2 ? flatten.call(this, input2[i]) : HOLE);
|
|
126
|
+
str[index2] = `${result}]`;
|
|
127
|
+
} else if (input2 instanceof Date) {
|
|
128
|
+
str[index2] = `["${TYPE_DATE}",${input2.getTime()}]`;
|
|
129
|
+
} else if (input2 instanceof URL) {
|
|
130
|
+
str[index2] = `["${TYPE_URL}",${JSON.stringify(input2.href)}]`;
|
|
131
|
+
} else if (input2 instanceof RegExp) {
|
|
132
|
+
str[index2] = `["${TYPE_REGEXP}",${JSON.stringify(
|
|
133
|
+
input2.source
|
|
134
|
+
)},${JSON.stringify(input2.flags)}]`;
|
|
135
|
+
} else if (input2 instanceof Set) {
|
|
136
|
+
if (input2.size > 0) {
|
|
137
|
+
str[index2] = `["${TYPE_SET}",${[...input2].map((val) => flatten.call(this, val)).join(",")}]`;
|
|
138
|
+
} else {
|
|
139
|
+
str[index2] = `["${TYPE_SET}"]`;
|
|
140
|
+
}
|
|
141
|
+
} else if (input2 instanceof Map) {
|
|
142
|
+
if (input2.size > 0) {
|
|
143
|
+
str[index2] = `["${TYPE_MAP}",${[...input2].flatMap(([k, v]) => [
|
|
144
|
+
flatten.call(this, k),
|
|
145
|
+
flatten.call(this, v)
|
|
146
|
+
]).join(",")}]`;
|
|
147
|
+
} else {
|
|
148
|
+
str[index2] = `["${TYPE_MAP}"]`;
|
|
149
|
+
}
|
|
150
|
+
} else if (input2 instanceof Promise) {
|
|
151
|
+
str[index2] = `["${TYPE_PROMISE}",${index2}]`;
|
|
152
|
+
deferred[index2] = input2;
|
|
153
|
+
} else if (input2 instanceof Error) {
|
|
154
|
+
str[index2] = `["${TYPE_ERROR}",${JSON.stringify(input2.message)}`;
|
|
155
|
+
if (input2.name !== "Error") {
|
|
156
|
+
str[index2] += `,${JSON.stringify(input2.name)}`;
|
|
157
|
+
}
|
|
158
|
+
str[index2] += "]";
|
|
159
|
+
} else if (Object.getPrototypeOf(input2) === null) {
|
|
160
|
+
str[index2] = `["${TYPE_NULL_OBJECT}",{${partsForObj(input2)}}]`;
|
|
161
|
+
} else if (isPlainObject(input2)) {
|
|
162
|
+
str[index2] = `{${partsForObj(input2)}}`;
|
|
144
163
|
} else {
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
} else if (input instanceof Promise) {
|
|
148
|
-
str[index] = `["${TYPE_PROMISE}",${index}]`;
|
|
149
|
-
deferred[index] = input;
|
|
150
|
-
} else if (input instanceof Error) {
|
|
151
|
-
str[index] = `["${TYPE_ERROR}",${JSON.stringify(input.message)}`;
|
|
152
|
-
if (input.name !== "Error") {
|
|
153
|
-
str[index] += `,${JSON.stringify(input.name)}`;
|
|
164
|
+
throw new Error("Cannot encode object with prototype");
|
|
154
165
|
}
|
|
155
|
-
str[index] += "]";
|
|
156
|
-
} else if (Object.getPrototypeOf(input) === null) {
|
|
157
|
-
str[index] = `["${TYPE_NULL_OBJECT}",{${partsForObj(input)}}]`;
|
|
158
|
-
} else if (isPlainObject(input)) {
|
|
159
|
-
str[index] = `{${partsForObj(input)}}`;
|
|
160
|
-
} else {
|
|
161
|
-
throw new Error("Cannot encode object with prototype");
|
|
162
166
|
}
|
|
167
|
+
break;
|
|
163
168
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
str[
|
|
169
|
+
default: {
|
|
170
|
+
const isArray = Array.isArray(input2);
|
|
171
|
+
let pluginHandled = false;
|
|
172
|
+
if (!isArray && plugins) {
|
|
173
|
+
for (const plugin of plugins) {
|
|
174
|
+
const pluginResult = plugin(input2);
|
|
175
|
+
if (Array.isArray(pluginResult)) {
|
|
176
|
+
pluginHandled = true;
|
|
177
|
+
const [pluginIdentifier, ...rest] = pluginResult;
|
|
178
|
+
str[index2] = `[${JSON.stringify(pluginIdentifier)}`;
|
|
179
|
+
if (rest.length > 0) {
|
|
180
|
+
str[index2] += `,${rest.map((v) => flatten.call(this, v)).join(",")}`;
|
|
181
|
+
}
|
|
182
|
+
str[index2] += "]";
|
|
183
|
+
break;
|
|
178
184
|
}
|
|
179
|
-
str[index] += "]";
|
|
180
|
-
break;
|
|
181
185
|
}
|
|
182
186
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
187
|
+
if (!pluginHandled) {
|
|
188
|
+
throw new Error("Cannot encode function or unexpected type");
|
|
189
|
+
}
|
|
186
190
|
}
|
|
187
191
|
}
|
|
188
192
|
}
|
|
@@ -202,114 +206,224 @@ function unflatten(parsed) {
|
|
|
202
206
|
if (!Array.isArray(parsed) || !parsed.length)
|
|
203
207
|
throw new SyntaxError();
|
|
204
208
|
const startIndex = values.length;
|
|
205
|
-
|
|
209
|
+
for (const value of parsed) {
|
|
210
|
+
values.push(value);
|
|
211
|
+
}
|
|
206
212
|
hydrated.length = values.length;
|
|
207
213
|
return hydrate.call(this, startIndex);
|
|
208
214
|
}
|
|
209
215
|
function hydrate(index) {
|
|
210
216
|
const { hydrated, values, deferred, plugins } = this;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
217
|
+
let result;
|
|
218
|
+
const stack = [
|
|
219
|
+
[
|
|
220
|
+
index,
|
|
221
|
+
(v) => {
|
|
222
|
+
result = v;
|
|
223
|
+
}
|
|
224
|
+
]
|
|
225
|
+
];
|
|
226
|
+
let postRun = [];
|
|
227
|
+
while (stack.length > 0) {
|
|
228
|
+
const [index2, set] = stack.pop();
|
|
229
|
+
switch (index2) {
|
|
230
|
+
case UNDEFINED:
|
|
231
|
+
set(void 0);
|
|
232
|
+
continue;
|
|
233
|
+
case NULL:
|
|
234
|
+
set(null);
|
|
235
|
+
continue;
|
|
236
|
+
case NAN:
|
|
237
|
+
set(NaN);
|
|
238
|
+
continue;
|
|
239
|
+
case POSITIVE_INFINITY:
|
|
240
|
+
set(Infinity);
|
|
241
|
+
continue;
|
|
242
|
+
case NEGATIVE_INFINITY:
|
|
243
|
+
set(-Infinity);
|
|
244
|
+
continue;
|
|
245
|
+
case NEGATIVE_ZERO:
|
|
246
|
+
set(-0);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (hydrated[index2]) {
|
|
250
|
+
set(hydrated[index2]);
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
const value = values[index2];
|
|
254
|
+
if (!value || typeof value !== "object") {
|
|
255
|
+
hydrated[index2] = value;
|
|
256
|
+
set(value);
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (Array.isArray(value)) {
|
|
260
|
+
if (typeof value[0] === "string") {
|
|
261
|
+
const [type, b, c] = value;
|
|
262
|
+
switch (type) {
|
|
263
|
+
case TYPE_DATE:
|
|
264
|
+
set(hydrated[index2] = new Date(b));
|
|
265
|
+
continue;
|
|
266
|
+
case TYPE_URL:
|
|
267
|
+
set(hydrated[index2] = new URL(b));
|
|
268
|
+
continue;
|
|
269
|
+
case TYPE_BIGINT:
|
|
270
|
+
set(hydrated[index2] = BigInt(b));
|
|
271
|
+
continue;
|
|
272
|
+
case TYPE_REGEXP:
|
|
273
|
+
set(hydrated[index2] = new RegExp(b, c));
|
|
274
|
+
continue;
|
|
275
|
+
case TYPE_SYMBOL:
|
|
276
|
+
set(hydrated[index2] = Symbol.for(b));
|
|
277
|
+
continue;
|
|
278
|
+
case TYPE_SET:
|
|
279
|
+
const newSet = /* @__PURE__ */ new Set();
|
|
280
|
+
hydrated[index2] = newSet;
|
|
281
|
+
for (let i = 1; i < value.length; i++)
|
|
282
|
+
stack.push([
|
|
283
|
+
value[i],
|
|
284
|
+
(v) => {
|
|
285
|
+
newSet.add(v);
|
|
286
|
+
}
|
|
287
|
+
]);
|
|
288
|
+
set(newSet);
|
|
289
|
+
continue;
|
|
290
|
+
case TYPE_MAP:
|
|
291
|
+
const map = /* @__PURE__ */ new Map();
|
|
292
|
+
hydrated[index2] = map;
|
|
293
|
+
for (let i = 1; i < value.length; i += 2) {
|
|
294
|
+
const r = [];
|
|
295
|
+
stack.push([
|
|
296
|
+
value[i + 1],
|
|
297
|
+
(v) => {
|
|
298
|
+
r[1] = v;
|
|
299
|
+
}
|
|
300
|
+
]);
|
|
301
|
+
stack.push([
|
|
302
|
+
value[i],
|
|
303
|
+
(k) => {
|
|
304
|
+
r[0] = k;
|
|
305
|
+
}
|
|
306
|
+
]);
|
|
307
|
+
postRun.push(() => {
|
|
308
|
+
map.set(r[0], r[1]);
|
|
309
|
+
});
|
|
288
310
|
}
|
|
311
|
+
set(map);
|
|
312
|
+
continue;
|
|
313
|
+
case TYPE_NULL_OBJECT:
|
|
314
|
+
const obj = /* @__PURE__ */ Object.create(null);
|
|
315
|
+
hydrated[index2] = obj;
|
|
316
|
+
for (const key in b) {
|
|
317
|
+
const r = [];
|
|
318
|
+
stack.push([
|
|
319
|
+
b[key],
|
|
320
|
+
(v) => {
|
|
321
|
+
r[1] = v;
|
|
322
|
+
}
|
|
323
|
+
]);
|
|
324
|
+
stack.push([
|
|
325
|
+
Number(key),
|
|
326
|
+
(k) => {
|
|
327
|
+
r[0] = k;
|
|
328
|
+
}
|
|
329
|
+
]);
|
|
330
|
+
postRun.push(() => {
|
|
331
|
+
obj[r[0]] = r[1];
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
set(obj);
|
|
335
|
+
continue;
|
|
336
|
+
case TYPE_PROMISE:
|
|
337
|
+
if (hydrated[b]) {
|
|
338
|
+
set(hydrated[index2] = hydrated[b]);
|
|
339
|
+
} else {
|
|
340
|
+
const d = new Deferred();
|
|
341
|
+
deferred[b] = d;
|
|
342
|
+
set(hydrated[index2] = d.promise);
|
|
343
|
+
}
|
|
344
|
+
continue;
|
|
345
|
+
case TYPE_ERROR:
|
|
346
|
+
const [, message, errorType] = value;
|
|
347
|
+
let error = errorType && globalObj && globalObj[errorType] ? new globalObj[errorType](message) : new Error(message);
|
|
348
|
+
hydrated[index2] = error;
|
|
349
|
+
set(error);
|
|
350
|
+
continue;
|
|
351
|
+
case TYPE_PREVIOUS_RESOLVED:
|
|
352
|
+
set(hydrated[index2] = hydrated[b]);
|
|
353
|
+
continue;
|
|
354
|
+
default:
|
|
355
|
+
if (Array.isArray(plugins)) {
|
|
356
|
+
const r = [];
|
|
357
|
+
const vals = value.slice(1);
|
|
358
|
+
for (let i = 0; i < vals.length; i++) {
|
|
359
|
+
const v = vals[i];
|
|
360
|
+
stack.push([
|
|
361
|
+
v,
|
|
362
|
+
(v2) => {
|
|
363
|
+
r[i] = v2;
|
|
364
|
+
}
|
|
365
|
+
]);
|
|
366
|
+
}
|
|
367
|
+
postRun.push(() => {
|
|
368
|
+
for (const plugin of plugins) {
|
|
369
|
+
const result2 = plugin(value[0], ...r);
|
|
370
|
+
if (result2) {
|
|
371
|
+
set(hydrated[index2] = result2.value);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
throw new SyntaxError();
|
|
376
|
+
});
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
throw new SyntaxError();
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
const array = [];
|
|
383
|
+
hydrated[index2] = array;
|
|
384
|
+
for (let i = 0; i < value.length; i++) {
|
|
385
|
+
const n = value[i];
|
|
386
|
+
if (n !== HOLE) {
|
|
387
|
+
stack.push([
|
|
388
|
+
n,
|
|
389
|
+
(v) => {
|
|
390
|
+
array[i] = v;
|
|
391
|
+
}
|
|
392
|
+
]);
|
|
289
393
|
}
|
|
290
|
-
|
|
394
|
+
}
|
|
395
|
+
set(array);
|
|
396
|
+
continue;
|
|
291
397
|
}
|
|
292
398
|
} else {
|
|
293
|
-
const
|
|
294
|
-
hydrated[
|
|
295
|
-
for (
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
399
|
+
const object = {};
|
|
400
|
+
hydrated[index2] = object;
|
|
401
|
+
for (const key in value) {
|
|
402
|
+
const r = [];
|
|
403
|
+
stack.push([
|
|
404
|
+
value[key],
|
|
405
|
+
(v) => {
|
|
406
|
+
r[1] = v;
|
|
407
|
+
}
|
|
408
|
+
]);
|
|
409
|
+
stack.push([
|
|
410
|
+
Number(key),
|
|
411
|
+
(k) => {
|
|
412
|
+
r[0] = k;
|
|
413
|
+
}
|
|
414
|
+
]);
|
|
415
|
+
postRun.push(() => {
|
|
416
|
+
object[r[0]] = r[1];
|
|
417
|
+
});
|
|
299
418
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
} else {
|
|
303
|
-
const object = {};
|
|
304
|
-
hydrated[index] = object;
|
|
305
|
-
for (const key in value) {
|
|
306
|
-
object[hydrate.call(this, Number(key))] = hydrate.call(
|
|
307
|
-
this,
|
|
308
|
-
value[key]
|
|
309
|
-
);
|
|
419
|
+
set(object);
|
|
420
|
+
continue;
|
|
310
421
|
}
|
|
311
|
-
return object;
|
|
312
422
|
}
|
|
423
|
+
while (postRun.length > 0) {
|
|
424
|
+
postRun.pop()();
|
|
425
|
+
}
|
|
426
|
+
return result;
|
|
313
427
|
}
|
|
314
428
|
|
|
315
429
|
// src/turbo-stream.ts
|
package/dist/unflatten.js
CHANGED
|
@@ -14,114 +14,230 @@ function unflatten(parsed) {
|
|
|
14
14
|
if (!Array.isArray(parsed) || !parsed.length)
|
|
15
15
|
throw new SyntaxError();
|
|
16
16
|
const startIndex = values.length;
|
|
17
|
-
|
|
17
|
+
for (const value of parsed) {
|
|
18
|
+
values.push(value);
|
|
19
|
+
}
|
|
18
20
|
hydrated.length = values.length;
|
|
19
21
|
return hydrate.call(this, startIndex);
|
|
20
22
|
}
|
|
21
23
|
exports.unflatten = unflatten;
|
|
22
24
|
function hydrate(index) {
|
|
23
25
|
const { hydrated, values, deferred, plugins } = this;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
26
|
+
let result;
|
|
27
|
+
const stack = [
|
|
28
|
+
[
|
|
29
|
+
index,
|
|
30
|
+
(v) => {
|
|
31
|
+
result = v;
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
];
|
|
35
|
+
let postRun = [];
|
|
36
|
+
while (stack.length > 0) {
|
|
37
|
+
const [index, set] = stack.pop();
|
|
38
|
+
switch (index) {
|
|
39
|
+
case utils_js_1.UNDEFINED:
|
|
40
|
+
set(undefined);
|
|
41
|
+
continue;
|
|
42
|
+
case utils_js_1.NULL:
|
|
43
|
+
set(null);
|
|
44
|
+
continue;
|
|
45
|
+
case utils_js_1.NAN:
|
|
46
|
+
set(NaN);
|
|
47
|
+
continue;
|
|
48
|
+
case utils_js_1.POSITIVE_INFINITY:
|
|
49
|
+
set(Infinity);
|
|
50
|
+
continue;
|
|
51
|
+
case utils_js_1.NEGATIVE_INFINITY:
|
|
52
|
+
set(-Infinity);
|
|
53
|
+
continue;
|
|
54
|
+
case utils_js_1.NEGATIVE_ZERO:
|
|
55
|
+
set(-0);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (hydrated[index]) {
|
|
59
|
+
set(hydrated[index]);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const value = values[index];
|
|
63
|
+
if (!value || typeof value !== "object") {
|
|
64
|
+
hydrated[index] = value;
|
|
65
|
+
set(value);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
if (typeof value[0] === "string") {
|
|
70
|
+
const [type, b, c] = value;
|
|
71
|
+
switch (type) {
|
|
72
|
+
case utils_js_1.TYPE_DATE:
|
|
73
|
+
set((hydrated[index] = new Date(b)));
|
|
74
|
+
continue;
|
|
75
|
+
case utils_js_1.TYPE_URL:
|
|
76
|
+
set((hydrated[index] = new URL(b)));
|
|
77
|
+
continue;
|
|
78
|
+
case utils_js_1.TYPE_BIGINT:
|
|
79
|
+
set((hydrated[index] = BigInt(b)));
|
|
80
|
+
continue;
|
|
81
|
+
case utils_js_1.TYPE_REGEXP:
|
|
82
|
+
set((hydrated[index] = new RegExp(b, c)));
|
|
83
|
+
continue;
|
|
84
|
+
case utils_js_1.TYPE_SYMBOL:
|
|
85
|
+
set((hydrated[index] = Symbol.for(b)));
|
|
86
|
+
continue;
|
|
87
|
+
case utils_js_1.TYPE_SET:
|
|
88
|
+
const newSet = new Set();
|
|
89
|
+
hydrated[index] = newSet;
|
|
90
|
+
for (let i = 1; i < value.length; i++)
|
|
91
|
+
stack.push([
|
|
92
|
+
value[i],
|
|
93
|
+
(v) => {
|
|
94
|
+
newSet.add(v);
|
|
95
|
+
},
|
|
96
|
+
]);
|
|
97
|
+
set(newSet);
|
|
98
|
+
continue;
|
|
99
|
+
case utils_js_1.TYPE_MAP:
|
|
100
|
+
const map = new Map();
|
|
101
|
+
hydrated[index] = map;
|
|
102
|
+
for (let i = 1; i < value.length; i += 2) {
|
|
103
|
+
const r = [];
|
|
104
|
+
stack.push([
|
|
105
|
+
value[i + 1],
|
|
106
|
+
(v) => {
|
|
107
|
+
r[1] = v;
|
|
108
|
+
},
|
|
109
|
+
]);
|
|
110
|
+
stack.push([
|
|
111
|
+
value[i],
|
|
112
|
+
(k) => {
|
|
113
|
+
r[0] = k;
|
|
114
|
+
},
|
|
115
|
+
]);
|
|
116
|
+
postRun.push(() => {
|
|
117
|
+
map.set(r[0], r[1]);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
set(map);
|
|
121
|
+
continue;
|
|
122
|
+
case utils_js_1.TYPE_NULL_OBJECT:
|
|
123
|
+
const obj = Object.create(null);
|
|
124
|
+
hydrated[index] = obj;
|
|
125
|
+
for (const key in b) {
|
|
126
|
+
const r = [];
|
|
127
|
+
stack.push([
|
|
128
|
+
b[key],
|
|
129
|
+
(v) => {
|
|
130
|
+
r[1] = v;
|
|
131
|
+
},
|
|
132
|
+
]);
|
|
133
|
+
stack.push([
|
|
134
|
+
Number(key),
|
|
135
|
+
(k) => {
|
|
136
|
+
r[0] = k;
|
|
137
|
+
},
|
|
138
|
+
]);
|
|
139
|
+
postRun.push(() => {
|
|
140
|
+
obj[r[0]] = r[1];
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
set(obj);
|
|
144
|
+
continue;
|
|
145
|
+
case utils_js_1.TYPE_PROMISE:
|
|
146
|
+
if (hydrated[b]) {
|
|
147
|
+
set((hydrated[index] = hydrated[b]));
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
const d = new utils_js_1.Deferred();
|
|
151
|
+
deferred[b] = d;
|
|
152
|
+
set((hydrated[index] = d.promise));
|
|
103
153
|
}
|
|
154
|
+
continue;
|
|
155
|
+
case utils_js_1.TYPE_ERROR:
|
|
156
|
+
const [, message, errorType] = value;
|
|
157
|
+
let error = errorType && globalObj && globalObj[errorType]
|
|
158
|
+
? new globalObj[errorType](message)
|
|
159
|
+
: new Error(message);
|
|
160
|
+
hydrated[index] = error;
|
|
161
|
+
set(error);
|
|
162
|
+
continue;
|
|
163
|
+
case utils_js_1.TYPE_PREVIOUS_RESOLVED:
|
|
164
|
+
set((hydrated[index] = hydrated[b]));
|
|
165
|
+
continue;
|
|
166
|
+
default:
|
|
167
|
+
// Run plugins at the end so we have a chance to resolve primitives
|
|
168
|
+
// without running into a loop
|
|
169
|
+
if (Array.isArray(plugins)) {
|
|
170
|
+
const r = [];
|
|
171
|
+
const vals = value.slice(1);
|
|
172
|
+
for (let i = 0; i < vals.length; i++) {
|
|
173
|
+
const v = vals[i];
|
|
174
|
+
stack.push([
|
|
175
|
+
v,
|
|
176
|
+
(v) => {
|
|
177
|
+
r[i] = v;
|
|
178
|
+
},
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
181
|
+
postRun.push(() => {
|
|
182
|
+
for (const plugin of plugins) {
|
|
183
|
+
const result = plugin(value[0], ...r);
|
|
184
|
+
if (result) {
|
|
185
|
+
set((hydrated[index] = result.value));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
throw new SyntaxError();
|
|
190
|
+
});
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
throw new SyntaxError();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const array = [];
|
|
198
|
+
hydrated[index] = array;
|
|
199
|
+
for (let i = 0; i < value.length; i++) {
|
|
200
|
+
const n = value[i];
|
|
201
|
+
if (n !== utils_js_1.HOLE) {
|
|
202
|
+
stack.push([
|
|
203
|
+
n,
|
|
204
|
+
(v) => {
|
|
205
|
+
array[i] = v;
|
|
206
|
+
},
|
|
207
|
+
]);
|
|
104
208
|
}
|
|
105
|
-
|
|
209
|
+
}
|
|
210
|
+
set(array);
|
|
211
|
+
continue;
|
|
106
212
|
}
|
|
107
213
|
}
|
|
108
214
|
else {
|
|
109
|
-
const
|
|
110
|
-
hydrated[index] =
|
|
111
|
-
for (
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
215
|
+
const object = {};
|
|
216
|
+
hydrated[index] = object;
|
|
217
|
+
for (const key in value) {
|
|
218
|
+
const r = [];
|
|
219
|
+
stack.push([
|
|
220
|
+
value[key],
|
|
221
|
+
(v) => {
|
|
222
|
+
r[1] = v;
|
|
223
|
+
},
|
|
224
|
+
]);
|
|
225
|
+
stack.push([
|
|
226
|
+
Number(key),
|
|
227
|
+
(k) => {
|
|
228
|
+
r[0] = k;
|
|
229
|
+
},
|
|
230
|
+
]);
|
|
231
|
+
postRun.push(() => {
|
|
232
|
+
object[r[0]] = r[1];
|
|
233
|
+
});
|
|
115
234
|
}
|
|
116
|
-
|
|
235
|
+
set(object);
|
|
236
|
+
continue;
|
|
117
237
|
}
|
|
118
238
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
hydrated[index] = object;
|
|
122
|
-
for (const key in value) {
|
|
123
|
-
object[hydrate.call(this, Number(key))] = hydrate.call(this, value[key]);
|
|
124
|
-
}
|
|
125
|
-
return object;
|
|
239
|
+
while (postRun.length > 0) {
|
|
240
|
+
postRun.pop()();
|
|
126
241
|
}
|
|
242
|
+
return result;
|
|
127
243
|
}
|
package/package.json
CHANGED