@serve.zone/interfaces 20.0.0 → 20.1.0
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/changelog.md +14 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/data/isolatedrestore.js +7 -82
- package/dist_ts/platform/index.d.ts +3 -1
- package/dist_ts/platform/index.js +4 -2
- package/dist_ts/platform/storagemigration.d.ts +346 -0
- package/dist_ts/platform/storagemigration.golden.d.ts +49 -0
- package/dist_ts/platform/storagemigration.golden.js +207 -0
- package/dist_ts/platform/storagemigration.js +1554 -0
- package/dist_ts/private/canonicaljson.d.ts +14 -0
- package/dist_ts/private/canonicaljson.js +154 -0
- package/package.json +1 -1
- package/readme.md +126 -0
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/data/isolatedrestore.ts +12 -93
- package/ts/platform/index.ts +3 -0
- package/ts/platform/storagemigration.golden.ts +241 -0
- package/ts/platform/storagemigration.ts +3082 -0
- package/ts/private/canonicaljson.ts +221 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
export const strictCanonicalJsonRules = Object.freeze({
|
|
2
|
+
version: 1 as const,
|
|
3
|
+
encoding: 'utf-8' as const,
|
|
4
|
+
objectKeyOrdering: 'utf-16-code-unit-ascending' as const,
|
|
5
|
+
arrayOrdering: 'preserved-dense' as const,
|
|
6
|
+
unicodeNormalization: 'preserved' as const,
|
|
7
|
+
numberEncoding: 'safe-integer-json-no-negative-zero' as const,
|
|
8
|
+
digest: 'sha256-lowercase-hex' as const,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export type TCanonicalJsonFailure = (reasonArg: string) => never;
|
|
12
|
+
|
|
13
|
+
const assertUnicodeScalarString = (
|
|
14
|
+
valueArg: string,
|
|
15
|
+
fieldNameArg: string,
|
|
16
|
+
failArg: TCanonicalJsonFailure,
|
|
17
|
+
): void => {
|
|
18
|
+
for (let index = 0; index < valueArg.length; index++) {
|
|
19
|
+
const codeUnit = valueArg.charCodeAt(index);
|
|
20
|
+
if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
|
|
21
|
+
const followingCodeUnit = valueArg.charCodeAt(index + 1);
|
|
22
|
+
if (
|
|
23
|
+
!Number.isInteger(followingCodeUnit) ||
|
|
24
|
+
followingCodeUnit < 0xdc00 ||
|
|
25
|
+
followingCodeUnit > 0xdfff
|
|
26
|
+
) {
|
|
27
|
+
failArg(`${fieldNameArg} must not contain an unpaired UTF-16 surrogate`);
|
|
28
|
+
}
|
|
29
|
+
index++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (codeUnit >= 0xdc00 && codeUnit <= 0xdfff) {
|
|
33
|
+
failArg(`${fieldNameArg} must not contain an unpaired UTF-16 surrogate`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const readStrictRecord = (
|
|
39
|
+
valueArg: object,
|
|
40
|
+
fieldNameArg: string,
|
|
41
|
+
failArg: TCanonicalJsonFailure,
|
|
42
|
+
): Record<string, unknown> => {
|
|
43
|
+
const prototype = Object.getPrototypeOf(valueArg);
|
|
44
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
45
|
+
return failArg(`${fieldNameArg} must be a plain object`);
|
|
46
|
+
}
|
|
47
|
+
const record = valueArg as Record<string, unknown>;
|
|
48
|
+
const ownKeys = Reflect.ownKeys(record);
|
|
49
|
+
if (ownKeys.some((keyArg) => typeof keyArg !== 'string')) {
|
|
50
|
+
return failArg(`${fieldNameArg} must not contain symbol keys`);
|
|
51
|
+
}
|
|
52
|
+
for (const key of ownKeys as string[]) {
|
|
53
|
+
const descriptor = Object.getOwnPropertyDescriptor(record, key);
|
|
54
|
+
if (
|
|
55
|
+
!descriptor ||
|
|
56
|
+
!descriptor.enumerable ||
|
|
57
|
+
!Object.hasOwn(descriptor, 'value')
|
|
58
|
+
) {
|
|
59
|
+
return failArg(
|
|
60
|
+
`${fieldNameArg} must contain enumerable data properties only`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return record;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const readStrictArray = (
|
|
68
|
+
valueArg: unknown[],
|
|
69
|
+
fieldNameArg: string,
|
|
70
|
+
failArg: TCanonicalJsonFailure,
|
|
71
|
+
): unknown[] => {
|
|
72
|
+
if (Object.getPrototypeOf(valueArg) !== Array.prototype) {
|
|
73
|
+
return failArg(`${fieldNameArg} must be a dense plain array`);
|
|
74
|
+
}
|
|
75
|
+
for (let index = 0; index < valueArg.length; index++) {
|
|
76
|
+
if (!Object.hasOwn(valueArg, index)) {
|
|
77
|
+
return failArg(`${fieldNameArg} must not contain sparse entries`);
|
|
78
|
+
}
|
|
79
|
+
const descriptor = Object.getOwnPropertyDescriptor(valueArg, String(index));
|
|
80
|
+
if (
|
|
81
|
+
!descriptor ||
|
|
82
|
+
!descriptor.enumerable ||
|
|
83
|
+
!Object.hasOwn(descriptor, 'value')
|
|
84
|
+
) {
|
|
85
|
+
return failArg(`${fieldNameArg} must contain enumerable data entries only`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const key of Reflect.ownKeys(valueArg)) {
|
|
89
|
+
if (key === 'length') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (
|
|
93
|
+
typeof key !== 'string' ||
|
|
94
|
+
!/^(?:0|[1-9][0-9]*)$/.test(key) ||
|
|
95
|
+
Number(key) >= valueArg.length
|
|
96
|
+
) {
|
|
97
|
+
return failArg(`${fieldNameArg} must not contain extra properties`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return valueArg;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const canonicalizeStrictJson = (
|
|
104
|
+
valueArg: unknown,
|
|
105
|
+
failArg: TCanonicalJsonFailure,
|
|
106
|
+
fieldNameArg = 'canonical JSON input',
|
|
107
|
+
): string => {
|
|
108
|
+
const ancestors = new Set<object>();
|
|
109
|
+
|
|
110
|
+
const visit = (currentValueArg: unknown, currentFieldNameArg: string): string => {
|
|
111
|
+
if (currentValueArg === null) {
|
|
112
|
+
return 'null';
|
|
113
|
+
}
|
|
114
|
+
if (typeof currentValueArg === 'boolean') {
|
|
115
|
+
return currentValueArg ? 'true' : 'false';
|
|
116
|
+
}
|
|
117
|
+
if (typeof currentValueArg === 'string') {
|
|
118
|
+
assertUnicodeScalarString(currentValueArg, currentFieldNameArg, failArg);
|
|
119
|
+
return JSON.stringify(currentValueArg);
|
|
120
|
+
}
|
|
121
|
+
if (typeof currentValueArg === 'number') {
|
|
122
|
+
if (
|
|
123
|
+
!Number.isSafeInteger(currentValueArg) ||
|
|
124
|
+
Object.is(currentValueArg, -0)
|
|
125
|
+
) {
|
|
126
|
+
return failArg(`${currentFieldNameArg} must contain safe integers only`);
|
|
127
|
+
}
|
|
128
|
+
return String(currentValueArg);
|
|
129
|
+
}
|
|
130
|
+
if (!currentValueArg || typeof currentValueArg !== 'object') {
|
|
131
|
+
return failArg(`${currentFieldNameArg} contains a non-JSON value`);
|
|
132
|
+
}
|
|
133
|
+
if (ancestors.has(currentValueArg)) {
|
|
134
|
+
return failArg(`${currentFieldNameArg} contains a cycle`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
ancestors.add(currentValueArg);
|
|
138
|
+
try {
|
|
139
|
+
if (Array.isArray(currentValueArg)) {
|
|
140
|
+
const array = readStrictArray(
|
|
141
|
+
currentValueArg,
|
|
142
|
+
currentFieldNameArg,
|
|
143
|
+
failArg,
|
|
144
|
+
);
|
|
145
|
+
return `[${array
|
|
146
|
+
.map((entryArg, indexArg) =>
|
|
147
|
+
visit(entryArg, `${currentFieldNameArg}[${indexArg}]`),
|
|
148
|
+
)
|
|
149
|
+
.join(',')}]`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const record = readStrictRecord(
|
|
153
|
+
currentValueArg,
|
|
154
|
+
currentFieldNameArg,
|
|
155
|
+
failArg,
|
|
156
|
+
);
|
|
157
|
+
const keys = Object.keys(record).sort((leftArg, rightArg) => {
|
|
158
|
+
return leftArg < rightArg ? -1 : leftArg > rightArg ? 1 : 0;
|
|
159
|
+
});
|
|
160
|
+
return `{${keys
|
|
161
|
+
.map((keyArg) => {
|
|
162
|
+
assertUnicodeScalarString(
|
|
163
|
+
keyArg,
|
|
164
|
+
`${currentFieldNameArg} key`,
|
|
165
|
+
failArg,
|
|
166
|
+
);
|
|
167
|
+
return `${JSON.stringify(keyArg)}:${visit(
|
|
168
|
+
record[keyArg],
|
|
169
|
+
`${currentFieldNameArg}.${keyArg}`,
|
|
170
|
+
)}`;
|
|
171
|
+
})
|
|
172
|
+
.join(',')}}`;
|
|
173
|
+
} finally {
|
|
174
|
+
ancestors.delete(currentValueArg);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
return visit(valueArg, fieldNameArg);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export const createSha256Hex = async (
|
|
182
|
+
contentsArg: Uint8Array,
|
|
183
|
+
failArg: TCanonicalJsonFailure,
|
|
184
|
+
): Promise<string> => {
|
|
185
|
+
const subtle = globalThis.crypto?.subtle;
|
|
186
|
+
if (!subtle) {
|
|
187
|
+
return failArg('Web Crypto SHA-256 is unavailable in this runtime');
|
|
188
|
+
}
|
|
189
|
+
const digest = await subtle.digest('SHA-256', new Uint8Array(contentsArg));
|
|
190
|
+
return Array.from(new Uint8Array(digest), (byteArg) =>
|
|
191
|
+
byteArg.toString(16).padStart(2, '0'),
|
|
192
|
+
).join('');
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const createCanonicalJsonSha256Hex = async (
|
|
196
|
+
canonicalJsonArg: string,
|
|
197
|
+
failArg: TCanonicalJsonFailure,
|
|
198
|
+
): Promise<string> => {
|
|
199
|
+
return createSha256Hex(new TextEncoder().encode(canonicalJsonArg), failArg);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export const deepFreezeValue = <T>(
|
|
203
|
+
valueArg: T,
|
|
204
|
+
seenArg = new WeakSet<object>(),
|
|
205
|
+
): T => {
|
|
206
|
+
if (!valueArg || typeof valueArg !== 'object') {
|
|
207
|
+
return valueArg;
|
|
208
|
+
}
|
|
209
|
+
const object = valueArg as object;
|
|
210
|
+
if (ArrayBuffer.isView(object) || seenArg.has(object)) {
|
|
211
|
+
return valueArg;
|
|
212
|
+
}
|
|
213
|
+
seenArg.add(object);
|
|
214
|
+
for (const key of Reflect.ownKeys(object)) {
|
|
215
|
+
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
216
|
+
if (descriptor && Object.hasOwn(descriptor, 'value')) {
|
|
217
|
+
deepFreezeValue(descriptor.value, seenArg);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return Object.freeze(valueArg);
|
|
221
|
+
};
|