@metapages/metapage 1.9.2 → 1.9.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/index.js +2694 -1899
- package/dist/index.js.map +1 -1
- package/dist/metapage/data.d.ts +3 -3
- package/dist/metapage/data.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/metapage/data.ts +251 -204
package/dist/metapage/data.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ export type DataRefSerializedFile = DataRefSerializedBlob & {
|
|
|
18
18
|
name: string;
|
|
19
19
|
lastModified?: number;
|
|
20
20
|
};
|
|
21
|
+
export declare const possiblySerializeValueToDataref: <T>(value: T) => Promise<string | T>;
|
|
22
|
+
export declare const possiblyDeserializeDatarefToValue: (value: any) => Promise<any>;
|
|
23
|
+
export declare const possiblyDeserializeDatarefToFile: (value: any) => Promise<File | undefined>;
|
|
21
24
|
export declare const valueToFile: (value: any, fileName: string, options?: FilePropertyBag) => Promise<File>;
|
|
22
|
-
export declare const possiblySerializeValueToDataref: <T>(value: T) => Promise<DataRefSerialized | T>;
|
|
23
|
-
export declare const possiblyDeserializeDatarefToValue: (value: any) => any;
|
|
24
|
-
export declare const possiblyDeserializeDatarefToFile: (value: any) => File | undefined;
|
|
25
25
|
//# sourceMappingURL=data.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/metapage/data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/metapage/data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,QAAQ,CAAC;AAkC3C,eAAO,MAAM,eAAe,WAClB,iBAAiB,KACxB,QAAQ,iBAAiB,CAa3B,CAAC;AAOF,eAAO,MAAM,iBAAiB,WACpB,iBAAiB,KACxB,QAAQ,iBAAiB,CAa3B,CAAC;AAGF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,EAAE,EAAE,IAAI,CAAC;IACT,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,iBAAiB,GAAG;IAC5D,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,iBAAiB,GAAG;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,qBAAqB,GAAG;IAC1D,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CAAC;AAoBF,eAAO,MAAM,+BAA+B,sCAgC3C,CAAC;AASF,eAAO,MAAM,iCAAiC,UACrC,GAAG,KACT,QAAQ,GAAG,CAsBb,CAAC;AAMF,eAAO,MAAM,gCAAgC,UACpC,GAAG,KACT,QAAQ,IAAI,GAAG,SAAS,CAmB1B,CAAC;AAEF,eAAO,MAAM,WAAW,UACf,GAAG,YACA,MAAM,YACN,eAAe,KACxB,QAAQ,IAAI,CAoCd,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metapages/metapage",
|
|
3
3
|
"public": true,
|
|
4
|
-
"version": "1.9.
|
|
4
|
+
"version": "1.9.3",
|
|
5
5
|
"description": "Connect web pages together",
|
|
6
6
|
"repository": "https://github.com/metapages/metapage",
|
|
7
7
|
"homepage": "https://metapage.io/",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
],
|
|
24
24
|
"source": "src/index.ts",
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@metapages/dataref": "^2.0.1",
|
|
26
27
|
"@metapages/hash-query": "^0.9.8",
|
|
27
28
|
"base64-arraybuffer": "^1.0.2",
|
|
28
29
|
"compare-versions": "^6.1.1",
|
package/src/metapage/data.ts
CHANGED
|
@@ -1,57 +1,85 @@
|
|
|
1
1
|
import { MetaframeInputMap } from "./v0_4";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { decode } from "base64-arraybuffer";
|
|
3
|
+
import {
|
|
4
|
+
typedArrayToDataUrl,
|
|
5
|
+
bufferToDataUrl,
|
|
6
|
+
dataUrlToBuffer,
|
|
7
|
+
dataUrlToTypedArray,
|
|
8
|
+
isDataUrl,
|
|
9
|
+
getParameters,
|
|
10
|
+
getMimeType,
|
|
11
|
+
type TypedArrayType,
|
|
12
|
+
} from "@metapages/dataref";
|
|
13
|
+
|
|
14
|
+
/** Cheap sync check: does this value need serialization? */
|
|
15
|
+
const needsSerialization = (value: any): boolean =>
|
|
16
|
+
value instanceof ArrayBuffer ||
|
|
17
|
+
value instanceof Blob || // File extends Blob, so this catches both
|
|
18
|
+
ArrayBuffer.isView(value);
|
|
19
|
+
|
|
20
|
+
/** Cheap sync check: does this value need deserialization? */
|
|
21
|
+
const needsDeserialization = (value: any): boolean => {
|
|
22
|
+
if (typeof value === "string") return value.startsWith("data:");
|
|
23
|
+
if (value && typeof value === "object") {
|
|
24
|
+
if (value._s === true && value._c) return true;
|
|
25
|
+
if (value.ref === "base64" && value.c) return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
4
29
|
|
|
5
30
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
31
|
+
* Serialize all values in inputs (one level deep).
|
|
32
|
+
* Binary types (TypedArray, ArrayBuffer, Blob, File) become data URL strings.
|
|
33
|
+
* Returns the original object if nothing needs serialization.
|
|
9
34
|
*/
|
|
10
35
|
export const serializeInputs = async (
|
|
11
36
|
inputs: MetaframeInputMap,
|
|
12
37
|
): Promise<MetaframeInputMap> => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
38
|
+
const keys = Object.keys(inputs);
|
|
39
|
+
const keysToSerialize: string[] = [];
|
|
40
|
+
for (const key of keys) {
|
|
41
|
+
if (needsSerialization(inputs[key])) keysToSerialize.push(key);
|
|
42
|
+
}
|
|
43
|
+
if (keysToSerialize.length === 0) return inputs;
|
|
44
|
+
|
|
45
|
+
const result: MetaframeInputMap = { ...inputs };
|
|
46
|
+
for (const key of keysToSerialize) {
|
|
47
|
+
result[key] = await possiblySerializeValueToDataref(inputs[key]);
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
21
50
|
};
|
|
22
51
|
|
|
23
52
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
53
|
+
* Deserialize all values in inputs (one level deep).
|
|
54
|
+
* Handles v2 data URLs, legacy metapage _s/_c format, and dataref v1 ref/c format.
|
|
55
|
+
* Returns the original object if nothing needs deserialization.
|
|
27
56
|
*/
|
|
28
57
|
export const deserializeInputs = async (
|
|
29
58
|
inputs: MetaframeInputMap,
|
|
30
59
|
): Promise<MetaframeInputMap> => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
60
|
+
const keys = Object.keys(inputs);
|
|
61
|
+
const keysToDeserialize: string[] = [];
|
|
62
|
+
for (const key of keys) {
|
|
63
|
+
if (needsDeserialization(inputs[key])) keysToDeserialize.push(key);
|
|
64
|
+
}
|
|
65
|
+
if (keysToDeserialize.length === 0) return inputs;
|
|
66
|
+
|
|
67
|
+
const result: MetaframeInputMap = { ...inputs };
|
|
68
|
+
for (const key of keysToDeserialize) {
|
|
69
|
+
result[key] = await possiblyDeserializeDatarefToValue(inputs[key]);
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
41
72
|
};
|
|
42
73
|
|
|
74
|
+
// Legacy types kept for backwards-compatible deserialization
|
|
43
75
|
export type DataRefSerialized = {
|
|
44
|
-
// This means it's a serialized DataRef
|
|
45
76
|
_s: true;
|
|
46
|
-
// constructor
|
|
47
77
|
_c: string;
|
|
48
|
-
// value is base64 encoded
|
|
49
78
|
value: string;
|
|
50
79
|
size: number;
|
|
51
80
|
};
|
|
52
81
|
|
|
53
82
|
export type DataRefSerializedTypedArray = DataRefSerialized & {
|
|
54
|
-
// Typed arrays are from ArrayBufferView
|
|
55
83
|
byteLength: number;
|
|
56
84
|
byteOffset: number;
|
|
57
85
|
};
|
|
@@ -65,12 +93,124 @@ export type DataRefSerializedFile = DataRefSerializedBlob & {
|
|
|
65
93
|
lastModified?: number;
|
|
66
94
|
};
|
|
67
95
|
|
|
96
|
+
const TYPED_ARRAY_NAMES: Set<string> = new Set([
|
|
97
|
+
"Int8Array",
|
|
98
|
+
"Uint8Array",
|
|
99
|
+
"Uint8ClampedArray",
|
|
100
|
+
"Int16Array",
|
|
101
|
+
"Uint16Array",
|
|
102
|
+
"Int32Array",
|
|
103
|
+
"Uint32Array",
|
|
104
|
+
"Float32Array",
|
|
105
|
+
"Float64Array",
|
|
106
|
+
"BigInt64Array",
|
|
107
|
+
"BigUint64Array",
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Serialize a value to a data URL string if it's a binary type.
|
|
112
|
+
* Non-binary values pass through unchanged.
|
|
113
|
+
*/
|
|
114
|
+
export const possiblySerializeValueToDataref = async <T>(
|
|
115
|
+
value: T,
|
|
116
|
+
): Promise<T | string> => {
|
|
117
|
+
if (
|
|
118
|
+
value instanceof Int8Array ||
|
|
119
|
+
value instanceof Uint8Array ||
|
|
120
|
+
value instanceof Uint8ClampedArray ||
|
|
121
|
+
value instanceof Int16Array ||
|
|
122
|
+
value instanceof Uint16Array ||
|
|
123
|
+
value instanceof Int32Array ||
|
|
124
|
+
value instanceof Uint32Array ||
|
|
125
|
+
value instanceof Float32Array ||
|
|
126
|
+
value instanceof Float64Array
|
|
127
|
+
) {
|
|
128
|
+
return typedArrayToDataUrl(value, value.constructor.name as TypedArrayType);
|
|
129
|
+
} else if (value instanceof File) {
|
|
130
|
+
const arrayBuffer = await value.arrayBuffer();
|
|
131
|
+
const bytes = new Uint8Array(arrayBuffer);
|
|
132
|
+
const base64 = btoa(String.fromCharCode(...bytes));
|
|
133
|
+
const mimeType = value.type || "application/octet-stream";
|
|
134
|
+
const name = encodeURIComponent(value.name);
|
|
135
|
+
return `data:${mimeType};file=true;name=${name};lastModified=${value.lastModified};base64,${base64}`;
|
|
136
|
+
} else if (value instanceof Blob) {
|
|
137
|
+
const arrayBuffer = await value.arrayBuffer();
|
|
138
|
+
const bytes = new Uint8Array(arrayBuffer);
|
|
139
|
+
const base64 = btoa(String.fromCharCode(...bytes));
|
|
140
|
+
const mimeType = value.type || "application/octet-stream";
|
|
141
|
+
return `data:${mimeType};blob=true;base64,${base64}`;
|
|
142
|
+
} else if (value instanceof ArrayBuffer) {
|
|
143
|
+
return bufferToDataUrl(value);
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Deserialize a value from any supported format:
|
|
150
|
+
* 1. v2 data URL strings (new format)
|
|
151
|
+
* 2. Legacy metapage format ({ _s: true, _c: "..." })
|
|
152
|
+
* 3. dataref v1 format ({ ref: "base64", c: "..." })
|
|
153
|
+
* 4. Anything else passes through unchanged
|
|
154
|
+
*/
|
|
155
|
+
export const possiblyDeserializeDatarefToValue = async (
|
|
156
|
+
value: any,
|
|
157
|
+
): Promise<any> => {
|
|
158
|
+
// 1. v2 data URL string
|
|
159
|
+
if (isDataUrl(value)) {
|
|
160
|
+
return deserializeDataUrl(value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!value || typeof value !== "object") {
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 2. Legacy metapage format: { _s: true, _c: "ConstructorName", value: "base64..." }
|
|
168
|
+
if (value._s === true && value._c) {
|
|
169
|
+
return deserializeLegacyMetapage(value);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 3. dataref v1 format: { ref: "base64", c: "ConstructorName", value: "base64..." }
|
|
173
|
+
if (value.ref === "base64" && value.c) {
|
|
174
|
+
return deserializeDatarefV1(value);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 4. Pass through
|
|
178
|
+
return value;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Attempt to deserialize a value to a File. Returns undefined if the value
|
|
183
|
+
* is not a recognized serialized format.
|
|
184
|
+
*/
|
|
185
|
+
export const possiblyDeserializeDatarefToFile = async (
|
|
186
|
+
value: any,
|
|
187
|
+
): Promise<File | undefined> => {
|
|
188
|
+
const deserialized = await possiblyDeserializeDatarefToValue(value);
|
|
189
|
+
if (deserialized instanceof File) {
|
|
190
|
+
return deserialized;
|
|
191
|
+
}
|
|
192
|
+
if (deserialized instanceof Blob) {
|
|
193
|
+
return new File([deserialized], "file", { type: deserialized.type });
|
|
194
|
+
}
|
|
195
|
+
if (deserialized instanceof ArrayBuffer) {
|
|
196
|
+
return new File([deserialized], "file", {
|
|
197
|
+
type: "application/octet-stream",
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (ArrayBuffer.isView(deserialized)) {
|
|
201
|
+
return new File([deserialized], "file", {
|
|
202
|
+
type: "application/octet-stream",
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return undefined;
|
|
206
|
+
};
|
|
207
|
+
|
|
68
208
|
export const valueToFile = async (
|
|
69
209
|
value: any,
|
|
70
210
|
fileName: string,
|
|
71
211
|
options?: FilePropertyBag,
|
|
72
212
|
): Promise<File> => {
|
|
73
|
-
value = possiblyDeserializeDatarefToValue(value);
|
|
213
|
+
value = await possiblyDeserializeDatarefToValue(value);
|
|
74
214
|
options = options || {};
|
|
75
215
|
if (!options.type) {
|
|
76
216
|
options.type = "application/octet-stream";
|
|
@@ -82,26 +222,15 @@ export const valueToFile = async (
|
|
|
82
222
|
if (value instanceof File || value instanceof Blob) {
|
|
83
223
|
const buffer = await value.arrayBuffer();
|
|
84
224
|
if (value instanceof File) {
|
|
85
|
-
options.type =
|
|
225
|
+
options.type = value.type;
|
|
86
226
|
}
|
|
87
227
|
return new File([buffer], fileName, options);
|
|
88
228
|
}
|
|
89
|
-
if (
|
|
90
|
-
value
|
|
91
|
-
value instanceof Uint8Array ||
|
|
92
|
-
value instanceof Uint8ClampedArray ||
|
|
93
|
-
value instanceof Int16Array ||
|
|
94
|
-
value instanceof Uint16Array ||
|
|
95
|
-
value instanceof Int32Array ||
|
|
96
|
-
value instanceof Uint32Array ||
|
|
97
|
-
value instanceof Float32Array ||
|
|
98
|
-
value instanceof Float64Array
|
|
99
|
-
) {
|
|
100
|
-
const typedValue = value as ArrayBufferView;
|
|
101
|
-
return new File([typedValue.buffer], fileName, options);
|
|
229
|
+
if (ArrayBuffer.isView(value)) {
|
|
230
|
+
return new File([value.buffer], fileName, options);
|
|
102
231
|
}
|
|
103
232
|
if (typeof value === "string") {
|
|
104
|
-
|
|
233
|
+
const blob = new Blob([value], { type: "text/plain" });
|
|
105
234
|
options.type = "text/plain";
|
|
106
235
|
return new File([blob], fileName, options);
|
|
107
236
|
}
|
|
@@ -113,176 +242,94 @@ export const valueToFile = async (
|
|
|
113
242
|
return new File([blob], fileName, options);
|
|
114
243
|
}
|
|
115
244
|
|
|
116
|
-
|
|
117
|
-
var blob = new Blob([value as string], { type: "text/plain" });
|
|
245
|
+
const blob = new Blob([value as string], { type: "text/plain" });
|
|
118
246
|
options.type = "text/plain";
|
|
119
247
|
return new File([blob], fileName, options);
|
|
120
248
|
};
|
|
121
249
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
): Promise<
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
value instanceof Uint16Array ||
|
|
131
|
-
value instanceof Int32Array ||
|
|
132
|
-
value instanceof Uint32Array ||
|
|
133
|
-
value instanceof Float32Array ||
|
|
134
|
-
value instanceof Float64Array
|
|
135
|
-
) {
|
|
136
|
-
const typedValue = value as ArrayBufferView;
|
|
137
|
-
const replacement: DataRefSerializedTypedArray = {
|
|
138
|
-
_s: true,
|
|
139
|
-
_c: value.constructor.name,
|
|
140
|
-
value: encode(typedValue.buffer),
|
|
141
|
-
byteLength: typedValue.byteLength,
|
|
142
|
-
byteOffset: typedValue.byteOffset,
|
|
143
|
-
size: typedValue.byteLength,
|
|
144
|
-
};
|
|
145
|
-
return Promise.resolve(replacement);
|
|
146
|
-
} else if (value instanceof File) {
|
|
147
|
-
const typedValue = value as File;
|
|
148
|
-
const arrayBuffer = await typedValue.arrayBuffer();
|
|
149
|
-
const replacement: DataRefSerializedFile = {
|
|
150
|
-
_s: true,
|
|
151
|
-
_c: File.name,
|
|
152
|
-
value: encode(arrayBuffer),
|
|
153
|
-
name: typedValue.name,
|
|
154
|
-
fileType: typedValue.type,
|
|
155
|
-
lastModified: typedValue.lastModified,
|
|
156
|
-
size: arrayBuffer.byteLength,
|
|
157
|
-
};
|
|
158
|
-
return replacement;
|
|
159
|
-
} else if (value instanceof Blob) {
|
|
160
|
-
const typedValue = value as Blob;
|
|
161
|
-
const arrayBuffer = await typedValue.arrayBuffer();
|
|
162
|
-
const replacement: DataRefSerializedBlob = {
|
|
163
|
-
_s: true,
|
|
164
|
-
_c: Blob.name,
|
|
165
|
-
value: encode(arrayBuffer),
|
|
166
|
-
fileType: typedValue.type,
|
|
167
|
-
size: arrayBuffer.byteLength,
|
|
168
|
-
};
|
|
169
|
-
return replacement;
|
|
170
|
-
} else if (value instanceof ArrayBuffer) {
|
|
171
|
-
const typedValue = value as ArrayBuffer;
|
|
172
|
-
const replacement: DataRefSerialized = {
|
|
173
|
-
_s: true,
|
|
174
|
-
_c: ArrayBuffer.name,
|
|
175
|
-
value: encode(typedValue),
|
|
176
|
-
size: typedValue.byteLength,
|
|
177
|
-
};
|
|
178
|
-
return Promise.resolve(replacement);
|
|
250
|
+
// --- Internal helpers ---
|
|
251
|
+
|
|
252
|
+
async function deserializeDataUrl(dataUrl: string): Promise<any> {
|
|
253
|
+
const params = getParameters(dataUrl);
|
|
254
|
+
|
|
255
|
+
// TypedArray: has "type" parameter (e.g. type=Float32Array)
|
|
256
|
+
if (params.type && TYPED_ARRAY_NAMES.has(params.type)) {
|
|
257
|
+
return dataUrlToTypedArray(dataUrl);
|
|
179
258
|
}
|
|
180
|
-
return Promise.resolve(value);
|
|
181
|
-
};
|
|
182
259
|
|
|
183
|
-
|
|
184
|
-
if (
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
)
|
|
190
|
-
|
|
191
|
-
return
|
|
260
|
+
// File: has "file=true" parameter
|
|
261
|
+
if (params.file === "true") {
|
|
262
|
+
const name = params.name ? decodeURIComponent(params.name) : "file";
|
|
263
|
+
const lastModified = params.lastModified
|
|
264
|
+
? parseInt(params.lastModified, 10)
|
|
265
|
+
: undefined;
|
|
266
|
+
const mimeType = getMimeType(dataUrl);
|
|
267
|
+
const buffer = await dataUrlToBuffer(dataUrl);
|
|
268
|
+
return new File([buffer], name, { type: mimeType, lastModified });
|
|
192
269
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
const
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
});
|
|
200
|
-
return blob;
|
|
201
|
-
} else if (_c === File.name) {
|
|
202
|
-
const serializedRefFile = value as DataRefSerializedFile;
|
|
203
|
-
const file = new File(
|
|
204
|
-
[decode(serializedRef.value)],
|
|
205
|
-
serializedRefFile.name,
|
|
206
|
-
{
|
|
207
|
-
type: serializedRefFile.fileType,
|
|
208
|
-
lastModified: serializedRefFile.lastModified,
|
|
209
|
-
},
|
|
210
|
-
);
|
|
211
|
-
return file;
|
|
212
|
-
} else if (_c === ArrayBuffer.name) {
|
|
213
|
-
const arrayBuffer: ArrayBuffer = decode(serializedRef.value);
|
|
214
|
-
return arrayBuffer;
|
|
270
|
+
|
|
271
|
+
// Blob: has "blob=true" parameter
|
|
272
|
+
if (params.blob === "true") {
|
|
273
|
+
const mimeType = getMimeType(dataUrl);
|
|
274
|
+
const buffer = await dataUrlToBuffer(dataUrl);
|
|
275
|
+
return new Blob([buffer], { type: mimeType });
|
|
215
276
|
}
|
|
216
|
-
// Assume typed array
|
|
217
|
-
const serializedRefTypedArray = value as DataRefSerializedTypedArray;
|
|
218
277
|
|
|
219
|
-
|
|
220
|
-
|
|
278
|
+
// Plain ArrayBuffer
|
|
279
|
+
return dataUrlToBuffer(dataUrl);
|
|
280
|
+
}
|
|
221
281
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return
|
|
230
|
-
|
|
282
|
+
function deserializeLegacyMetapage(value: DataRefSerialized): any {
|
|
283
|
+
const _c = value._c;
|
|
284
|
+
if (_c === "Blob") {
|
|
285
|
+
const v = value as DataRefSerializedBlob;
|
|
286
|
+
return new Blob([decode(value.value)], { type: v.fileType });
|
|
287
|
+
} else if (_c === "File") {
|
|
288
|
+
const v = value as DataRefSerializedFile;
|
|
289
|
+
return new File([decode(value.value)], v.name, {
|
|
290
|
+
type: v.fileType,
|
|
291
|
+
lastModified: v.lastModified,
|
|
292
|
+
});
|
|
293
|
+
} else if (_c === "ArrayBuffer") {
|
|
294
|
+
return decode(value.value);
|
|
295
|
+
}
|
|
296
|
+
// Typed array
|
|
297
|
+
const arrayBuffer = decode(value.value);
|
|
298
|
+
if (TYPED_ARRAY_NAMES.has(_c)) {
|
|
299
|
+
try {
|
|
300
|
+
// @ts-ignore
|
|
301
|
+
return new globalThis[_c](arrayBuffer);
|
|
302
|
+
} catch (_) {}
|
|
303
|
+
}
|
|
231
304
|
return value;
|
|
232
|
-
}
|
|
305
|
+
}
|
|
233
306
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
return undefined;
|
|
245
|
-
}
|
|
246
|
-
const serializedRef = value as DataRefSerialized;
|
|
247
|
-
const _c: string = serializedRef._c;
|
|
248
|
-
if (_c === Blob.name) {
|
|
249
|
-
const serializedRefBlob = value as DataRefSerializedBlob;
|
|
250
|
-
const blob = new Blob([decode(serializedRef.value)], {
|
|
251
|
-
type: serializedRefBlob.fileType,
|
|
252
|
-
});
|
|
253
|
-
return new File([blob], "file", {
|
|
254
|
-
type: blob.type,
|
|
307
|
+
function deserializeDatarefV1(value: {
|
|
308
|
+
ref: string;
|
|
309
|
+
c: string;
|
|
310
|
+
value: string;
|
|
311
|
+
[key: string]: any;
|
|
312
|
+
}): any {
|
|
313
|
+
const _c = value.c;
|
|
314
|
+
if (_c === "Blob") {
|
|
315
|
+
return new Blob([decode(value.value)], {
|
|
316
|
+
type: value.fileType || undefined,
|
|
255
317
|
});
|
|
256
|
-
} else if (_c === File
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
serializedRefFile.name,
|
|
261
|
-
{
|
|
262
|
-
type: serializedRefFile.fileType,
|
|
263
|
-
lastModified: serializedRefFile.lastModified,
|
|
264
|
-
},
|
|
265
|
-
);
|
|
266
|
-
return file;
|
|
267
|
-
} else if (_c === ArrayBuffer.name) {
|
|
268
|
-
const arrayBuffer: ArrayBuffer = decode(serializedRef.value);
|
|
269
|
-
return new File([arrayBuffer], "file", {
|
|
270
|
-
type: "application/octet-stream",
|
|
318
|
+
} else if (_c === "File") {
|
|
319
|
+
return new File([decode(value.value)], value.name || "file", {
|
|
320
|
+
type: value.fileType || undefined,
|
|
321
|
+
lastModified: value.lastModified || undefined,
|
|
271
322
|
});
|
|
323
|
+
} else if (_c === "ArrayBuffer") {
|
|
324
|
+
return decode(value.value);
|
|
272
325
|
}
|
|
273
|
-
//
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
return new File([typedArray], "file", {
|
|
284
|
-
type: "application/octet-stream",
|
|
285
|
-
});
|
|
286
|
-
} catch (e) {}
|
|
287
|
-
return undefined;
|
|
288
|
-
};
|
|
326
|
+
// Typed array
|
|
327
|
+
const arrayBuffer = decode(value.value);
|
|
328
|
+
if (TYPED_ARRAY_NAMES.has(_c)) {
|
|
329
|
+
try {
|
|
330
|
+
// @ts-ignore
|
|
331
|
+
return new globalThis[_c](arrayBuffer);
|
|
332
|
+
} catch (_) {}
|
|
333
|
+
}
|
|
334
|
+
return value;
|
|
335
|
+
}
|