| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
from ... import util, dt |
| 13 |
import array |
| 14 |
import logging |
| 15 |
import math |
| 16 |
|
| 17 |
|
| 18 |
ARRAY_TYPES = { |
| 19 |
"Int8Array" : "b", |
| 20 |
"Uint8Array" : "B", |
| 21 |
"Uint8ClampedArray": "B", |
| 22 |
"Int16Array" : "h", |
| 23 |
"Uint16Array" : "H", |
| 24 |
"Int32Array" : "i", |
| 25 |
"Uint32Array" : "I", |
| 26 |
"Float32Array" : "f", |
| 27 |
"Float64Array" : "d", |
| 28 |
"BigInt64Array" : "l", |
| 29 |
"BigUint64Array" : "L", |
| 30 |
"ArrayBuffer" : "B", |
| 31 |
} |
| 32 |
|
| 33 |
REVIVERS = { |
| 34 |
"EmptyShallowRef": util.json_loads, |
| 35 |
"EmptyRef" : util.json_loads, |
| 36 |
"NuxtError" : util.identity, |
| 37 |
"ShallowRef" : util.identity, |
| 38 |
"ShallowReactive": util.identity, |
| 39 |
"Ref" : util.identity, |
| 40 |
"Reactive" : util.identity, |
| 41 |
"skipHydrate" : util.identity, |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
def resolve(data): |
| 46 |
|
| 47 |
resolved = { |
| 48 |
-1: None, |
| 49 |
-2: None, |
| 50 |
-3: math.nan, |
| 51 |
-4: math.inf, |
| 52 |
-5: -math.inf, |
| 53 |
-6: -0.0, |
| 54 |
} |
| 55 |
|
| 56 |
retval = [None] |
| 57 |
stack = [(retval, 0, 0)] |
| 58 |
|
| 59 |
while stack: |
| 60 |
target, index, source = stack.pop() |
| 61 |
|
| 62 |
if isinstance(source, tuple): |
| 63 |
name, source, reviver = source |
| 64 |
try: |
| 65 |
target[index] = reviver(target[index]) |
| 66 |
except Exception as exc: |
| 67 |
error(TypeError(f"failed to parse {source} as {name!r}: " |
| 68 |
f"{exc.__class__.__name__}: {exc}")) |
| 69 |
target[index] = None |
| 70 |
continue |
| 71 |
if source in resolved: |
| 72 |
target[index] = resolved[source] |
| 73 |
continue |
| 74 |
if source < 0: |
| 75 |
error(IndexError(f"invalid index: {source!r}")) |
| 76 |
continue |
| 77 |
|
| 78 |
try: |
| 79 |
value = data[source] |
| 80 |
except Exception as exc: |
| 81 |
error(exc) |
| 82 |
continue |
| 83 |
|
| 84 |
if isinstance(value, list): |
| 85 |
if value and isinstance(value[0], str): |
| 86 |
type = value[0] |
| 87 |
|
| 88 |
if type in REVIVERS: |
| 89 |
value = value[1] |
| 90 |
if value == source: |
| 91 |
|
| 92 |
error(IndexError(f"{type!r} cannot point to itself " |
| 93 |
f"(index: {source})")) |
| 94 |
continue |
| 95 |
stack.append((target, index, ( |
| 96 |
type, value, REVIVERS[type]))) |
| 97 |
stack.append((target, index, value)) |
| 98 |
continue |
| 99 |
|
| 100 |
elif type in ARRAY_TYPES: |
| 101 |
data = util.b64rdecode(value[1]) |
| 102 |
result = array.array(ARRAY_TYPES[type], data).tolist() |
| 103 |
|
| 104 |
elif type == "Date": |
| 105 |
try: |
| 106 |
result = dt.parse_iso(value[1]) |
| 107 |
except Exception: |
| 108 |
error(ValueError(f"invalid date: {value[1]!r}")) |
| 109 |
result = None |
| 110 |
|
| 111 |
elif type == "Set": |
| 112 |
result = [None] * (len(value) - 1) |
| 113 |
for offset, new in enumerate(value[1:]): |
| 114 |
stack.append((result, offset, new)) |
| 115 |
|
| 116 |
elif type == "Map": |
| 117 |
result = [] |
| 118 |
for i in range(1, len(value), 2): |
| 119 |
pair = [None, None] |
| 120 |
stack.append((pair, 0, value[i])) |
| 121 |
stack.append((pair, 1, value[i+1])) |
| 122 |
result.append(pair) |
| 123 |
|
| 124 |
elif type == "RegExp": |
| 125 |
result = util.re(value[1]) |
| 126 |
|
| 127 |
elif type == "Object": |
| 128 |
result = value[1] |
| 129 |
|
| 130 |
elif type == "BigInt": |
| 131 |
result = int(value[1]) |
| 132 |
|
| 133 |
elif type == "null": |
| 134 |
result = {} |
| 135 |
for i in range(1, len(value), 2): |
| 136 |
stack.append((result, value[i], value[i+1])) |
| 137 |
|
| 138 |
else: |
| 139 |
error(TypeError(f"invalid type at {source}: {type!r}")) |
| 140 |
result = None |
| 141 |
else: |
| 142 |
result = [None] * len(value) |
| 143 |
for offset, new in enumerate(value): |
| 144 |
stack.append((result, offset, new)) |
| 145 |
|
| 146 |
elif isinstance(value, dict): |
| 147 |
result = {} |
| 148 |
for key, new in value.items(): |
| 149 |
stack.append((result, key, new)) |
| 150 |
|
| 151 |
else: |
| 152 |
result = value |
| 153 |
|
| 154 |
target[index] = resolved[source] = result |
| 155 |
|
| 156 |
return retval[0] |
| 157 |
|
| 158 |
|
| 159 |
def error(exc): |
| 160 |
logging.getLogger("nuxt").error("%s: %s", exc.__class__.__name__, exc) |