@atproto/common-web 0.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/README.md +3 -0
- package/babel.config.js +1 -0
- package/build.js +23 -0
- package/dist/async.d.ts +24 -0
- package/dist/check.d.ts +18 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +4489 -0
- package/dist/index.js.map +7 -0
- package/dist/ipld.d.ts +9 -0
- package/dist/strings.d.ts +2 -0
- package/dist/tid.d.ts +20 -0
- package/dist/times.d.ts +4 -0
- package/dist/types.d.ts +19 -0
- package/dist/util.d.ts +16 -0
- package/jest.config.js +6 -0
- package/package.json +26 -0
- package/src/async.ts +126 -0
- package/src/check.ts +25 -0
- package/src/index.ts +10 -0
- package/src/ipld.ts +77 -0
- package/src/strings.ts +9 -0
- package/src/tid.ts +108 -0
- package/src/times.ts +4 -0
- package/src/types.ts +44 -0
- package/src/util.ts +108 -0
- package/tests/strings.test.ts +30 -0
- package/tests/tid.test.ts +18 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +9 -0
- package/update-pkg.js +14 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,4489 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from3, except, desc) => {
|
|
11
|
+
if (from3 && typeof from3 === "object" || typeof from3 === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from3))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from3[key], enumerable: !(desc = __getOwnPropDesc(from3, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports2 = {};
|
|
22
|
+
__export(src_exports2, {
|
|
23
|
+
AsyncBuffer: () => AsyncBuffer,
|
|
24
|
+
AsyncBufferFullError: () => AsyncBufferFullError,
|
|
25
|
+
DAY: () => DAY,
|
|
26
|
+
HOUR: () => HOUR,
|
|
27
|
+
MINUTE: () => MINUTE,
|
|
28
|
+
SECOND: () => SECOND,
|
|
29
|
+
TID: () => TID,
|
|
30
|
+
allComplete: () => allComplete,
|
|
31
|
+
asyncFilter: () => asyncFilter,
|
|
32
|
+
bailableWait: () => bailableWait,
|
|
33
|
+
check: () => check_exports,
|
|
34
|
+
chunkArray: () => chunkArray,
|
|
35
|
+
createDeferrable: () => createDeferrable,
|
|
36
|
+
createDeferrables: () => createDeferrables,
|
|
37
|
+
def: () => def,
|
|
38
|
+
errHasMsg: () => errHasMsg,
|
|
39
|
+
flattenUint8Arrays: () => flattenUint8Arrays,
|
|
40
|
+
graphemeLen: () => graphemeLen,
|
|
41
|
+
ipldToJson: () => ipldToJson,
|
|
42
|
+
isErrnoException: () => isErrnoException,
|
|
43
|
+
jsonToIpld: () => jsonToIpld,
|
|
44
|
+
noUndefinedVals: () => noUndefinedVals,
|
|
45
|
+
range: () => range,
|
|
46
|
+
readFromGenerator: () => readFromGenerator,
|
|
47
|
+
s32decode: () => s32decode,
|
|
48
|
+
s32encode: () => s32encode,
|
|
49
|
+
schema: () => schema,
|
|
50
|
+
streamToArray: () => streamToArray,
|
|
51
|
+
utf8Len: () => utf8Len,
|
|
52
|
+
util: () => util_exports,
|
|
53
|
+
wait: () => wait
|
|
54
|
+
});
|
|
55
|
+
module.exports = __toCommonJS(src_exports2);
|
|
56
|
+
|
|
57
|
+
// src/check.ts
|
|
58
|
+
var check_exports = {};
|
|
59
|
+
__export(check_exports, {
|
|
60
|
+
assure: () => assure,
|
|
61
|
+
is: () => is,
|
|
62
|
+
isObject: () => isObject
|
|
63
|
+
});
|
|
64
|
+
var is = (obj, def2) => {
|
|
65
|
+
return def2.safeParse(obj).success;
|
|
66
|
+
};
|
|
67
|
+
var assure = (def2, obj) => {
|
|
68
|
+
return def2.parse(obj);
|
|
69
|
+
};
|
|
70
|
+
var isObject = (obj) => {
|
|
71
|
+
return typeof obj === "object" && obj !== null;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/util.ts
|
|
75
|
+
var util_exports = {};
|
|
76
|
+
__export(util_exports, {
|
|
77
|
+
asyncFilter: () => asyncFilter,
|
|
78
|
+
bailableWait: () => bailableWait,
|
|
79
|
+
chunkArray: () => chunkArray,
|
|
80
|
+
errHasMsg: () => errHasMsg,
|
|
81
|
+
flattenUint8Arrays: () => flattenUint8Arrays,
|
|
82
|
+
isErrnoException: () => isErrnoException,
|
|
83
|
+
noUndefinedVals: () => noUndefinedVals,
|
|
84
|
+
range: () => range,
|
|
85
|
+
s32decode: () => s32decode,
|
|
86
|
+
s32encode: () => s32encode,
|
|
87
|
+
streamToArray: () => streamToArray,
|
|
88
|
+
wait: () => wait
|
|
89
|
+
});
|
|
90
|
+
var noUndefinedVals = (obj) => {
|
|
91
|
+
Object.keys(obj).forEach((k) => {
|
|
92
|
+
if (obj[k] === void 0) {
|
|
93
|
+
delete obj[k];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return obj;
|
|
97
|
+
};
|
|
98
|
+
var wait = (ms) => {
|
|
99
|
+
return new Promise((res) => setTimeout(res, ms));
|
|
100
|
+
};
|
|
101
|
+
var bailableWait = (ms) => {
|
|
102
|
+
let bail;
|
|
103
|
+
const waitPromise = new Promise((res) => {
|
|
104
|
+
const timeout = setTimeout(res, ms);
|
|
105
|
+
bail = () => {
|
|
106
|
+
clearTimeout(timeout);
|
|
107
|
+
res();
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
return { bail, wait: () => waitPromise };
|
|
111
|
+
};
|
|
112
|
+
var flattenUint8Arrays = (arrs) => {
|
|
113
|
+
const length2 = arrs.reduce((acc, cur) => {
|
|
114
|
+
return acc + cur.length;
|
|
115
|
+
}, 0);
|
|
116
|
+
const flattened = new Uint8Array(length2);
|
|
117
|
+
let offset = 0;
|
|
118
|
+
arrs.forEach((arr) => {
|
|
119
|
+
flattened.set(arr, offset);
|
|
120
|
+
offset += arr.length;
|
|
121
|
+
});
|
|
122
|
+
return flattened;
|
|
123
|
+
};
|
|
124
|
+
var streamToArray = async (stream) => {
|
|
125
|
+
const arrays = [];
|
|
126
|
+
for await (const chunk of stream) {
|
|
127
|
+
arrays.push(chunk);
|
|
128
|
+
}
|
|
129
|
+
return flattenUint8Arrays(arrays);
|
|
130
|
+
};
|
|
131
|
+
var S32_CHAR = "234567abcdefghijklmnopqrstuvwxyz";
|
|
132
|
+
var s32encode = (i) => {
|
|
133
|
+
let s = "";
|
|
134
|
+
while (i) {
|
|
135
|
+
const c = i % 32;
|
|
136
|
+
i = Math.floor(i / 32);
|
|
137
|
+
s = S32_CHAR.charAt(c) + s;
|
|
138
|
+
}
|
|
139
|
+
return s;
|
|
140
|
+
};
|
|
141
|
+
var s32decode = (s) => {
|
|
142
|
+
let i = 0;
|
|
143
|
+
for (const c of s) {
|
|
144
|
+
i = i * 32 + S32_CHAR.indexOf(c);
|
|
145
|
+
}
|
|
146
|
+
return i;
|
|
147
|
+
};
|
|
148
|
+
var asyncFilter = async (arr, fn) => {
|
|
149
|
+
const results = await Promise.all(arr.map((t) => fn(t)));
|
|
150
|
+
return arr.filter((_, i) => results[i]);
|
|
151
|
+
};
|
|
152
|
+
var isErrnoException = (err) => {
|
|
153
|
+
return !!err && err["code"];
|
|
154
|
+
};
|
|
155
|
+
var errHasMsg = (err, msg) => {
|
|
156
|
+
return !!err && typeof err === "object" && err["message"] === msg;
|
|
157
|
+
};
|
|
158
|
+
var chunkArray = (arr, chunkSize) => {
|
|
159
|
+
return arr.reduce((acc, cur, i) => {
|
|
160
|
+
const chunkI = Math.floor(i / chunkSize);
|
|
161
|
+
if (!acc[chunkI]) {
|
|
162
|
+
acc[chunkI] = [];
|
|
163
|
+
}
|
|
164
|
+
acc[chunkI].push(cur);
|
|
165
|
+
return acc;
|
|
166
|
+
}, []);
|
|
167
|
+
};
|
|
168
|
+
var range = (num) => {
|
|
169
|
+
const nums = [];
|
|
170
|
+
for (let i = 0; i < num; i++) {
|
|
171
|
+
nums.push(i);
|
|
172
|
+
}
|
|
173
|
+
return nums;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/async.ts
|
|
177
|
+
var readFromGenerator = async (gen, isDone, waitFor = Promise.resolve(), maxLength = Number.MAX_SAFE_INTEGER) => {
|
|
178
|
+
const evts = [];
|
|
179
|
+
let bail;
|
|
180
|
+
let hasBroke = false;
|
|
181
|
+
const awaitDone = async () => {
|
|
182
|
+
if (await isDone(evts.at(-1))) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
const bailable = bailableWait(20);
|
|
186
|
+
await bailable.wait();
|
|
187
|
+
bail = bailable.bail;
|
|
188
|
+
if (hasBroke)
|
|
189
|
+
return false;
|
|
190
|
+
return await awaitDone();
|
|
191
|
+
};
|
|
192
|
+
const breakOn = new Promise((resolve) => {
|
|
193
|
+
waitFor.then(() => {
|
|
194
|
+
awaitDone().then(() => resolve());
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
try {
|
|
198
|
+
while (evts.length < maxLength) {
|
|
199
|
+
const maybeEvt = await Promise.race([gen.next(), breakOn]);
|
|
200
|
+
if (!maybeEvt)
|
|
201
|
+
break;
|
|
202
|
+
const evt = maybeEvt;
|
|
203
|
+
if (evt.done)
|
|
204
|
+
break;
|
|
205
|
+
evts.push(evt.value);
|
|
206
|
+
}
|
|
207
|
+
} finally {
|
|
208
|
+
hasBroke = true;
|
|
209
|
+
bail && bail();
|
|
210
|
+
}
|
|
211
|
+
return evts;
|
|
212
|
+
};
|
|
213
|
+
var createDeferrable = () => {
|
|
214
|
+
let resolve;
|
|
215
|
+
const promise = new Promise((res) => {
|
|
216
|
+
resolve = () => res();
|
|
217
|
+
});
|
|
218
|
+
return { resolve, complete: promise };
|
|
219
|
+
};
|
|
220
|
+
var createDeferrables = (count) => {
|
|
221
|
+
const list = [];
|
|
222
|
+
for (let i = 0; i < count; i++) {
|
|
223
|
+
list.push(createDeferrable());
|
|
224
|
+
}
|
|
225
|
+
return list;
|
|
226
|
+
};
|
|
227
|
+
var allComplete = async (deferrables) => {
|
|
228
|
+
await Promise.all(deferrables.map((d) => d.complete));
|
|
229
|
+
};
|
|
230
|
+
var AsyncBuffer = class {
|
|
231
|
+
constructor(maxSize) {
|
|
232
|
+
this.maxSize = maxSize;
|
|
233
|
+
this.buffer = [];
|
|
234
|
+
this.promise = Promise.resolve();
|
|
235
|
+
this.resolve = () => null;
|
|
236
|
+
this.resetPromise();
|
|
237
|
+
}
|
|
238
|
+
get curr() {
|
|
239
|
+
return this.buffer;
|
|
240
|
+
}
|
|
241
|
+
get size() {
|
|
242
|
+
return this.buffer.length;
|
|
243
|
+
}
|
|
244
|
+
resetPromise() {
|
|
245
|
+
this.promise = new Promise((r) => this.resolve = r);
|
|
246
|
+
}
|
|
247
|
+
push(item) {
|
|
248
|
+
this.buffer.push(item);
|
|
249
|
+
this.resolve();
|
|
250
|
+
}
|
|
251
|
+
pushMany(items) {
|
|
252
|
+
items.forEach((i) => this.buffer.push(i));
|
|
253
|
+
this.resolve();
|
|
254
|
+
}
|
|
255
|
+
async *events() {
|
|
256
|
+
while (true) {
|
|
257
|
+
await this.promise;
|
|
258
|
+
if (this.maxSize && this.size > this.maxSize) {
|
|
259
|
+
throw new AsyncBufferFullError(this.maxSize);
|
|
260
|
+
}
|
|
261
|
+
const [first, ...rest] = this.buffer;
|
|
262
|
+
if (first) {
|
|
263
|
+
this.buffer = rest;
|
|
264
|
+
yield first;
|
|
265
|
+
} else {
|
|
266
|
+
this.resetPromise();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var AsyncBufferFullError = class extends Error {
|
|
272
|
+
constructor(maxSize) {
|
|
273
|
+
super(`ReachedMaxBufferSize: ${maxSize}`);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// src/tid.ts
|
|
278
|
+
var lastTimestamp = 0;
|
|
279
|
+
var timestampCount = 0;
|
|
280
|
+
var clockid = null;
|
|
281
|
+
var TID = class {
|
|
282
|
+
constructor(str) {
|
|
283
|
+
const noDashes = str.replace(/-/g, "");
|
|
284
|
+
if (noDashes.length !== 13) {
|
|
285
|
+
throw new Error(`Poorly formatted TID: ${noDashes.length} length`);
|
|
286
|
+
}
|
|
287
|
+
this.str = noDashes;
|
|
288
|
+
}
|
|
289
|
+
static next() {
|
|
290
|
+
const time = Math.max(Date.now(), lastTimestamp);
|
|
291
|
+
if (time === lastTimestamp) {
|
|
292
|
+
timestampCount++;
|
|
293
|
+
}
|
|
294
|
+
lastTimestamp = time;
|
|
295
|
+
const timestamp = time * 1e3 + timestampCount;
|
|
296
|
+
if (clockid === null) {
|
|
297
|
+
clockid = Math.floor(Math.random() * 32);
|
|
298
|
+
}
|
|
299
|
+
return TID.fromTime(timestamp, clockid);
|
|
300
|
+
}
|
|
301
|
+
static nextStr() {
|
|
302
|
+
return TID.next().toString();
|
|
303
|
+
}
|
|
304
|
+
static fromTime(timestamp, clockid2) {
|
|
305
|
+
const str = `${s32encode(timestamp)}${s32encode(clockid2).padStart(2, "2")}`;
|
|
306
|
+
return new TID(str);
|
|
307
|
+
}
|
|
308
|
+
static fromStr(str) {
|
|
309
|
+
return new TID(str);
|
|
310
|
+
}
|
|
311
|
+
static newestFirst(a, b) {
|
|
312
|
+
return a.compareTo(b) * -1;
|
|
313
|
+
}
|
|
314
|
+
static oldestFirst(a, b) {
|
|
315
|
+
return a.compareTo(b);
|
|
316
|
+
}
|
|
317
|
+
static is(str) {
|
|
318
|
+
try {
|
|
319
|
+
TID.fromStr(str);
|
|
320
|
+
return true;
|
|
321
|
+
} catch (err) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
timestamp() {
|
|
326
|
+
const substr = this.str.slice(0, 11);
|
|
327
|
+
return s32decode(substr);
|
|
328
|
+
}
|
|
329
|
+
clockid() {
|
|
330
|
+
const substr = this.str.slice(11, 13);
|
|
331
|
+
return s32decode(substr);
|
|
332
|
+
}
|
|
333
|
+
formatted() {
|
|
334
|
+
const str = this.toString();
|
|
335
|
+
return `${str.slice(0, 4)}-${str.slice(4, 7)}-${str.slice(
|
|
336
|
+
7,
|
|
337
|
+
11
|
|
338
|
+
)}-${str.slice(11, 13)}`;
|
|
339
|
+
}
|
|
340
|
+
toString() {
|
|
341
|
+
return this.str;
|
|
342
|
+
}
|
|
343
|
+
compareTo(other) {
|
|
344
|
+
if (this.str > other.str)
|
|
345
|
+
return 1;
|
|
346
|
+
if (this.str < other.str)
|
|
347
|
+
return -1;
|
|
348
|
+
return 0;
|
|
349
|
+
}
|
|
350
|
+
equals(other) {
|
|
351
|
+
return this.compareTo(other) === 0;
|
|
352
|
+
}
|
|
353
|
+
newerThan(other) {
|
|
354
|
+
return this.compareTo(other) > 0;
|
|
355
|
+
}
|
|
356
|
+
olderThan(other) {
|
|
357
|
+
return this.compareTo(other) < 0;
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// ../../node_modules/multiformats/esm/vendor/varint.js
|
|
362
|
+
var encode_1 = encode;
|
|
363
|
+
var MSB = 128;
|
|
364
|
+
var REST = 127;
|
|
365
|
+
var MSBALL = ~REST;
|
|
366
|
+
var INT = Math.pow(2, 31);
|
|
367
|
+
function encode(num, out, offset) {
|
|
368
|
+
out = out || [];
|
|
369
|
+
offset = offset || 0;
|
|
370
|
+
var oldOffset = offset;
|
|
371
|
+
while (num >= INT) {
|
|
372
|
+
out[offset++] = num & 255 | MSB;
|
|
373
|
+
num /= 128;
|
|
374
|
+
}
|
|
375
|
+
while (num & MSBALL) {
|
|
376
|
+
out[offset++] = num & 255 | MSB;
|
|
377
|
+
num >>>= 7;
|
|
378
|
+
}
|
|
379
|
+
out[offset] = num | 0;
|
|
380
|
+
encode.bytes = offset - oldOffset + 1;
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
var decode = read;
|
|
384
|
+
var MSB$1 = 128;
|
|
385
|
+
var REST$1 = 127;
|
|
386
|
+
function read(buf, offset) {
|
|
387
|
+
var res = 0, offset = offset || 0, shift = 0, counter = offset, b, l = buf.length;
|
|
388
|
+
do {
|
|
389
|
+
if (counter >= l) {
|
|
390
|
+
read.bytes = 0;
|
|
391
|
+
throw new RangeError("Could not decode varint");
|
|
392
|
+
}
|
|
393
|
+
b = buf[counter++];
|
|
394
|
+
res += shift < 28 ? (b & REST$1) << shift : (b & REST$1) * Math.pow(2, shift);
|
|
395
|
+
shift += 7;
|
|
396
|
+
} while (b >= MSB$1);
|
|
397
|
+
read.bytes = counter - offset;
|
|
398
|
+
return res;
|
|
399
|
+
}
|
|
400
|
+
var N1 = Math.pow(2, 7);
|
|
401
|
+
var N2 = Math.pow(2, 14);
|
|
402
|
+
var N3 = Math.pow(2, 21);
|
|
403
|
+
var N4 = Math.pow(2, 28);
|
|
404
|
+
var N5 = Math.pow(2, 35);
|
|
405
|
+
var N6 = Math.pow(2, 42);
|
|
406
|
+
var N7 = Math.pow(2, 49);
|
|
407
|
+
var N8 = Math.pow(2, 56);
|
|
408
|
+
var N9 = Math.pow(2, 63);
|
|
409
|
+
var length = function(value) {
|
|
410
|
+
return value < N1 ? 1 : value < N2 ? 2 : value < N3 ? 3 : value < N4 ? 4 : value < N5 ? 5 : value < N6 ? 6 : value < N7 ? 7 : value < N8 ? 8 : value < N9 ? 9 : 10;
|
|
411
|
+
};
|
|
412
|
+
var varint = {
|
|
413
|
+
encode: encode_1,
|
|
414
|
+
decode,
|
|
415
|
+
encodingLength: length
|
|
416
|
+
};
|
|
417
|
+
var _brrp_varint = varint;
|
|
418
|
+
var varint_default = _brrp_varint;
|
|
419
|
+
|
|
420
|
+
// ../../node_modules/multiformats/esm/src/varint.js
|
|
421
|
+
var decode2 = (data, offset = 0) => {
|
|
422
|
+
const code2 = varint_default.decode(data, offset);
|
|
423
|
+
return [
|
|
424
|
+
code2,
|
|
425
|
+
varint_default.decode.bytes
|
|
426
|
+
];
|
|
427
|
+
};
|
|
428
|
+
var encodeTo = (int, target, offset = 0) => {
|
|
429
|
+
varint_default.encode(int, target, offset);
|
|
430
|
+
return target;
|
|
431
|
+
};
|
|
432
|
+
var encodingLength = (int) => {
|
|
433
|
+
return varint_default.encodingLength(int);
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
// ../../node_modules/multiformats/esm/src/bytes.js
|
|
437
|
+
var empty = new Uint8Array(0);
|
|
438
|
+
var equals = (aa, bb) => {
|
|
439
|
+
if (aa === bb)
|
|
440
|
+
return true;
|
|
441
|
+
if (aa.byteLength !== bb.byteLength) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
for (let ii = 0; ii < aa.byteLength; ii++) {
|
|
445
|
+
if (aa[ii] !== bb[ii]) {
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return true;
|
|
450
|
+
};
|
|
451
|
+
var coerce = (o) => {
|
|
452
|
+
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array")
|
|
453
|
+
return o;
|
|
454
|
+
if (o instanceof ArrayBuffer)
|
|
455
|
+
return new Uint8Array(o);
|
|
456
|
+
if (ArrayBuffer.isView(o)) {
|
|
457
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength);
|
|
458
|
+
}
|
|
459
|
+
throw new Error("Unknown type, must be binary type");
|
|
460
|
+
};
|
|
461
|
+
var fromString = (str) => new TextEncoder().encode(str);
|
|
462
|
+
var toString = (b) => new TextDecoder().decode(b);
|
|
463
|
+
|
|
464
|
+
// ../../node_modules/multiformats/esm/src/hashes/digest.js
|
|
465
|
+
var create = (code2, digest2) => {
|
|
466
|
+
const size = digest2.byteLength;
|
|
467
|
+
const sizeOffset = encodingLength(code2);
|
|
468
|
+
const digestOffset = sizeOffset + encodingLength(size);
|
|
469
|
+
const bytes = new Uint8Array(digestOffset + size);
|
|
470
|
+
encodeTo(code2, bytes, 0);
|
|
471
|
+
encodeTo(size, bytes, sizeOffset);
|
|
472
|
+
bytes.set(digest2, digestOffset);
|
|
473
|
+
return new Digest(code2, size, digest2, bytes);
|
|
474
|
+
};
|
|
475
|
+
var decode3 = (multihash) => {
|
|
476
|
+
const bytes = coerce(multihash);
|
|
477
|
+
const [code2, sizeOffset] = decode2(bytes);
|
|
478
|
+
const [size, digestOffset] = decode2(bytes.subarray(sizeOffset));
|
|
479
|
+
const digest2 = bytes.subarray(sizeOffset + digestOffset);
|
|
480
|
+
if (digest2.byteLength !== size) {
|
|
481
|
+
throw new Error("Incorrect length");
|
|
482
|
+
}
|
|
483
|
+
return new Digest(code2, size, digest2, bytes);
|
|
484
|
+
};
|
|
485
|
+
var equals2 = (a, b) => {
|
|
486
|
+
if (a === b) {
|
|
487
|
+
return true;
|
|
488
|
+
} else {
|
|
489
|
+
return a.code === b.code && a.size === b.size && equals(a.bytes, b.bytes);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
var Digest = class {
|
|
493
|
+
constructor(code2, size, digest2, bytes) {
|
|
494
|
+
this.code = code2;
|
|
495
|
+
this.size = size;
|
|
496
|
+
this.digest = digest2;
|
|
497
|
+
this.bytes = bytes;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// ../../node_modules/multiformats/esm/src/bases/base58.js
|
|
502
|
+
var base58_exports = {};
|
|
503
|
+
__export(base58_exports, {
|
|
504
|
+
base58btc: () => base58btc,
|
|
505
|
+
base58flickr: () => base58flickr
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
// ../../node_modules/multiformats/esm/vendor/base-x.js
|
|
509
|
+
function base(ALPHABET, name2) {
|
|
510
|
+
if (ALPHABET.length >= 255) {
|
|
511
|
+
throw new TypeError("Alphabet too long");
|
|
512
|
+
}
|
|
513
|
+
var BASE_MAP = new Uint8Array(256);
|
|
514
|
+
for (var j = 0; j < BASE_MAP.length; j++) {
|
|
515
|
+
BASE_MAP[j] = 255;
|
|
516
|
+
}
|
|
517
|
+
for (var i = 0; i < ALPHABET.length; i++) {
|
|
518
|
+
var x = ALPHABET.charAt(i);
|
|
519
|
+
var xc = x.charCodeAt(0);
|
|
520
|
+
if (BASE_MAP[xc] !== 255) {
|
|
521
|
+
throw new TypeError(x + " is ambiguous");
|
|
522
|
+
}
|
|
523
|
+
BASE_MAP[xc] = i;
|
|
524
|
+
}
|
|
525
|
+
var BASE = ALPHABET.length;
|
|
526
|
+
var LEADER = ALPHABET.charAt(0);
|
|
527
|
+
var FACTOR = Math.log(BASE) / Math.log(256);
|
|
528
|
+
var iFACTOR = Math.log(256) / Math.log(BASE);
|
|
529
|
+
function encode5(source) {
|
|
530
|
+
if (source instanceof Uint8Array)
|
|
531
|
+
;
|
|
532
|
+
else if (ArrayBuffer.isView(source)) {
|
|
533
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
534
|
+
} else if (Array.isArray(source)) {
|
|
535
|
+
source = Uint8Array.from(source);
|
|
536
|
+
}
|
|
537
|
+
if (!(source instanceof Uint8Array)) {
|
|
538
|
+
throw new TypeError("Expected Uint8Array");
|
|
539
|
+
}
|
|
540
|
+
if (source.length === 0) {
|
|
541
|
+
return "";
|
|
542
|
+
}
|
|
543
|
+
var zeroes = 0;
|
|
544
|
+
var length2 = 0;
|
|
545
|
+
var pbegin = 0;
|
|
546
|
+
var pend = source.length;
|
|
547
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
548
|
+
pbegin++;
|
|
549
|
+
zeroes++;
|
|
550
|
+
}
|
|
551
|
+
var size = (pend - pbegin) * iFACTOR + 1 >>> 0;
|
|
552
|
+
var b58 = new Uint8Array(size);
|
|
553
|
+
while (pbegin !== pend) {
|
|
554
|
+
var carry = source[pbegin];
|
|
555
|
+
var i2 = 0;
|
|
556
|
+
for (var it1 = size - 1; (carry !== 0 || i2 < length2) && it1 !== -1; it1--, i2++) {
|
|
557
|
+
carry += 256 * b58[it1] >>> 0;
|
|
558
|
+
b58[it1] = carry % BASE >>> 0;
|
|
559
|
+
carry = carry / BASE >>> 0;
|
|
560
|
+
}
|
|
561
|
+
if (carry !== 0) {
|
|
562
|
+
throw new Error("Non-zero carry");
|
|
563
|
+
}
|
|
564
|
+
length2 = i2;
|
|
565
|
+
pbegin++;
|
|
566
|
+
}
|
|
567
|
+
var it2 = size - length2;
|
|
568
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
569
|
+
it2++;
|
|
570
|
+
}
|
|
571
|
+
var str = LEADER.repeat(zeroes);
|
|
572
|
+
for (; it2 < size; ++it2) {
|
|
573
|
+
str += ALPHABET.charAt(b58[it2]);
|
|
574
|
+
}
|
|
575
|
+
return str;
|
|
576
|
+
}
|
|
577
|
+
function decodeUnsafe(source) {
|
|
578
|
+
if (typeof source !== "string") {
|
|
579
|
+
throw new TypeError("Expected String");
|
|
580
|
+
}
|
|
581
|
+
if (source.length === 0) {
|
|
582
|
+
return new Uint8Array();
|
|
583
|
+
}
|
|
584
|
+
var psz = 0;
|
|
585
|
+
if (source[psz] === " ") {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
var zeroes = 0;
|
|
589
|
+
var length2 = 0;
|
|
590
|
+
while (source[psz] === LEADER) {
|
|
591
|
+
zeroes++;
|
|
592
|
+
psz++;
|
|
593
|
+
}
|
|
594
|
+
var size = (source.length - psz) * FACTOR + 1 >>> 0;
|
|
595
|
+
var b256 = new Uint8Array(size);
|
|
596
|
+
while (source[psz]) {
|
|
597
|
+
var carry = BASE_MAP[source.charCodeAt(psz)];
|
|
598
|
+
if (carry === 255) {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
var i2 = 0;
|
|
602
|
+
for (var it3 = size - 1; (carry !== 0 || i2 < length2) && it3 !== -1; it3--, i2++) {
|
|
603
|
+
carry += BASE * b256[it3] >>> 0;
|
|
604
|
+
b256[it3] = carry % 256 >>> 0;
|
|
605
|
+
carry = carry / 256 >>> 0;
|
|
606
|
+
}
|
|
607
|
+
if (carry !== 0) {
|
|
608
|
+
throw new Error("Non-zero carry");
|
|
609
|
+
}
|
|
610
|
+
length2 = i2;
|
|
611
|
+
psz++;
|
|
612
|
+
}
|
|
613
|
+
if (source[psz] === " ") {
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
var it4 = size - length2;
|
|
617
|
+
while (it4 !== size && b256[it4] === 0) {
|
|
618
|
+
it4++;
|
|
619
|
+
}
|
|
620
|
+
var vch = new Uint8Array(zeroes + (size - it4));
|
|
621
|
+
var j2 = zeroes;
|
|
622
|
+
while (it4 !== size) {
|
|
623
|
+
vch[j2++] = b256[it4++];
|
|
624
|
+
}
|
|
625
|
+
return vch;
|
|
626
|
+
}
|
|
627
|
+
function decode6(string2) {
|
|
628
|
+
var buffer = decodeUnsafe(string2);
|
|
629
|
+
if (buffer) {
|
|
630
|
+
return buffer;
|
|
631
|
+
}
|
|
632
|
+
throw new Error(`Non-${name2} character`);
|
|
633
|
+
}
|
|
634
|
+
return {
|
|
635
|
+
encode: encode5,
|
|
636
|
+
decodeUnsafe,
|
|
637
|
+
decode: decode6
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
var src = base;
|
|
641
|
+
var _brrp__multiformats_scope_baseX = src;
|
|
642
|
+
var base_x_default = _brrp__multiformats_scope_baseX;
|
|
643
|
+
|
|
644
|
+
// ../../node_modules/multiformats/esm/src/bases/base.js
|
|
645
|
+
var Encoder = class {
|
|
646
|
+
constructor(name2, prefix, baseEncode) {
|
|
647
|
+
this.name = name2;
|
|
648
|
+
this.prefix = prefix;
|
|
649
|
+
this.baseEncode = baseEncode;
|
|
650
|
+
}
|
|
651
|
+
encode(bytes) {
|
|
652
|
+
if (bytes instanceof Uint8Array) {
|
|
653
|
+
return `${this.prefix}${this.baseEncode(bytes)}`;
|
|
654
|
+
} else {
|
|
655
|
+
throw Error("Unknown type, must be binary type");
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
var Decoder = class {
|
|
660
|
+
constructor(name2, prefix, baseDecode) {
|
|
661
|
+
this.name = name2;
|
|
662
|
+
this.prefix = prefix;
|
|
663
|
+
if (prefix.codePointAt(0) === void 0) {
|
|
664
|
+
throw new Error("Invalid prefix character");
|
|
665
|
+
}
|
|
666
|
+
this.prefixCodePoint = prefix.codePointAt(0);
|
|
667
|
+
this.baseDecode = baseDecode;
|
|
668
|
+
}
|
|
669
|
+
decode(text) {
|
|
670
|
+
if (typeof text === "string") {
|
|
671
|
+
if (text.codePointAt(0) !== this.prefixCodePoint) {
|
|
672
|
+
throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);
|
|
673
|
+
}
|
|
674
|
+
return this.baseDecode(text.slice(this.prefix.length));
|
|
675
|
+
} else {
|
|
676
|
+
throw Error("Can only multibase decode strings");
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
or(decoder) {
|
|
680
|
+
return or(this, decoder);
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
var ComposedDecoder = class {
|
|
684
|
+
constructor(decoders) {
|
|
685
|
+
this.decoders = decoders;
|
|
686
|
+
}
|
|
687
|
+
or(decoder) {
|
|
688
|
+
return or(this, decoder);
|
|
689
|
+
}
|
|
690
|
+
decode(input) {
|
|
691
|
+
const prefix = input[0];
|
|
692
|
+
const decoder = this.decoders[prefix];
|
|
693
|
+
if (decoder) {
|
|
694
|
+
return decoder.decode(input);
|
|
695
|
+
} else {
|
|
696
|
+
throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
var or = (left, right) => new ComposedDecoder({
|
|
701
|
+
...left.decoders || { [left.prefix]: left },
|
|
702
|
+
...right.decoders || { [right.prefix]: right }
|
|
703
|
+
});
|
|
704
|
+
var Codec = class {
|
|
705
|
+
constructor(name2, prefix, baseEncode, baseDecode) {
|
|
706
|
+
this.name = name2;
|
|
707
|
+
this.prefix = prefix;
|
|
708
|
+
this.baseEncode = baseEncode;
|
|
709
|
+
this.baseDecode = baseDecode;
|
|
710
|
+
this.encoder = new Encoder(name2, prefix, baseEncode);
|
|
711
|
+
this.decoder = new Decoder(name2, prefix, baseDecode);
|
|
712
|
+
}
|
|
713
|
+
encode(input) {
|
|
714
|
+
return this.encoder.encode(input);
|
|
715
|
+
}
|
|
716
|
+
decode(input) {
|
|
717
|
+
return this.decoder.decode(input);
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
var from = ({ name: name2, prefix, encode: encode5, decode: decode6 }) => new Codec(name2, prefix, encode5, decode6);
|
|
721
|
+
var baseX = ({ prefix, name: name2, alphabet: alphabet2 }) => {
|
|
722
|
+
const { encode: encode5, decode: decode6 } = base_x_default(alphabet2, name2);
|
|
723
|
+
return from({
|
|
724
|
+
prefix,
|
|
725
|
+
name: name2,
|
|
726
|
+
encode: encode5,
|
|
727
|
+
decode: (text) => coerce(decode6(text))
|
|
728
|
+
});
|
|
729
|
+
};
|
|
730
|
+
var decode4 = (string2, alphabet2, bitsPerChar, name2) => {
|
|
731
|
+
const codes = {};
|
|
732
|
+
for (let i = 0; i < alphabet2.length; ++i) {
|
|
733
|
+
codes[alphabet2[i]] = i;
|
|
734
|
+
}
|
|
735
|
+
let end = string2.length;
|
|
736
|
+
while (string2[end - 1] === "=") {
|
|
737
|
+
--end;
|
|
738
|
+
}
|
|
739
|
+
const out = new Uint8Array(end * bitsPerChar / 8 | 0);
|
|
740
|
+
let bits = 0;
|
|
741
|
+
let buffer = 0;
|
|
742
|
+
let written = 0;
|
|
743
|
+
for (let i = 0; i < end; ++i) {
|
|
744
|
+
const value = codes[string2[i]];
|
|
745
|
+
if (value === void 0) {
|
|
746
|
+
throw new SyntaxError(`Non-${name2} character`);
|
|
747
|
+
}
|
|
748
|
+
buffer = buffer << bitsPerChar | value;
|
|
749
|
+
bits += bitsPerChar;
|
|
750
|
+
if (bits >= 8) {
|
|
751
|
+
bits -= 8;
|
|
752
|
+
out[written++] = 255 & buffer >> bits;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
if (bits >= bitsPerChar || 255 & buffer << 8 - bits) {
|
|
756
|
+
throw new SyntaxError("Unexpected end of data");
|
|
757
|
+
}
|
|
758
|
+
return out;
|
|
759
|
+
};
|
|
760
|
+
var encode2 = (data, alphabet2, bitsPerChar) => {
|
|
761
|
+
const pad = alphabet2[alphabet2.length - 1] === "=";
|
|
762
|
+
const mask = (1 << bitsPerChar) - 1;
|
|
763
|
+
let out = "";
|
|
764
|
+
let bits = 0;
|
|
765
|
+
let buffer = 0;
|
|
766
|
+
for (let i = 0; i < data.length; ++i) {
|
|
767
|
+
buffer = buffer << 8 | data[i];
|
|
768
|
+
bits += 8;
|
|
769
|
+
while (bits > bitsPerChar) {
|
|
770
|
+
bits -= bitsPerChar;
|
|
771
|
+
out += alphabet2[mask & buffer >> bits];
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
if (bits) {
|
|
775
|
+
out += alphabet2[mask & buffer << bitsPerChar - bits];
|
|
776
|
+
}
|
|
777
|
+
if (pad) {
|
|
778
|
+
while (out.length * bitsPerChar & 7) {
|
|
779
|
+
out += "=";
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
return out;
|
|
783
|
+
};
|
|
784
|
+
var rfc4648 = ({ name: name2, prefix, bitsPerChar, alphabet: alphabet2 }) => {
|
|
785
|
+
return from({
|
|
786
|
+
prefix,
|
|
787
|
+
name: name2,
|
|
788
|
+
encode(input) {
|
|
789
|
+
return encode2(input, alphabet2, bitsPerChar);
|
|
790
|
+
},
|
|
791
|
+
decode(input) {
|
|
792
|
+
return decode4(input, alphabet2, bitsPerChar, name2);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
// ../../node_modules/multiformats/esm/src/bases/base58.js
|
|
798
|
+
var base58btc = baseX({
|
|
799
|
+
name: "base58btc",
|
|
800
|
+
prefix: "z",
|
|
801
|
+
alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
802
|
+
});
|
|
803
|
+
var base58flickr = baseX({
|
|
804
|
+
name: "base58flickr",
|
|
805
|
+
prefix: "Z",
|
|
806
|
+
alphabet: "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
|
|
807
|
+
});
|
|
808
|
+
|
|
809
|
+
// ../../node_modules/multiformats/esm/src/bases/base32.js
|
|
810
|
+
var base32_exports = {};
|
|
811
|
+
__export(base32_exports, {
|
|
812
|
+
base32: () => base32,
|
|
813
|
+
base32hex: () => base32hex,
|
|
814
|
+
base32hexpad: () => base32hexpad,
|
|
815
|
+
base32hexpadupper: () => base32hexpadupper,
|
|
816
|
+
base32hexupper: () => base32hexupper,
|
|
817
|
+
base32pad: () => base32pad,
|
|
818
|
+
base32padupper: () => base32padupper,
|
|
819
|
+
base32upper: () => base32upper,
|
|
820
|
+
base32z: () => base32z
|
|
821
|
+
});
|
|
822
|
+
var base32 = rfc4648({
|
|
823
|
+
prefix: "b",
|
|
824
|
+
name: "base32",
|
|
825
|
+
alphabet: "abcdefghijklmnopqrstuvwxyz234567",
|
|
826
|
+
bitsPerChar: 5
|
|
827
|
+
});
|
|
828
|
+
var base32upper = rfc4648({
|
|
829
|
+
prefix: "B",
|
|
830
|
+
name: "base32upper",
|
|
831
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
|
|
832
|
+
bitsPerChar: 5
|
|
833
|
+
});
|
|
834
|
+
var base32pad = rfc4648({
|
|
835
|
+
prefix: "c",
|
|
836
|
+
name: "base32pad",
|
|
837
|
+
alphabet: "abcdefghijklmnopqrstuvwxyz234567=",
|
|
838
|
+
bitsPerChar: 5
|
|
839
|
+
});
|
|
840
|
+
var base32padupper = rfc4648({
|
|
841
|
+
prefix: "C",
|
|
842
|
+
name: "base32padupper",
|
|
843
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
|
|
844
|
+
bitsPerChar: 5
|
|
845
|
+
});
|
|
846
|
+
var base32hex = rfc4648({
|
|
847
|
+
prefix: "v",
|
|
848
|
+
name: "base32hex",
|
|
849
|
+
alphabet: "0123456789abcdefghijklmnopqrstuv",
|
|
850
|
+
bitsPerChar: 5
|
|
851
|
+
});
|
|
852
|
+
var base32hexupper = rfc4648({
|
|
853
|
+
prefix: "V",
|
|
854
|
+
name: "base32hexupper",
|
|
855
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV",
|
|
856
|
+
bitsPerChar: 5
|
|
857
|
+
});
|
|
858
|
+
var base32hexpad = rfc4648({
|
|
859
|
+
prefix: "t",
|
|
860
|
+
name: "base32hexpad",
|
|
861
|
+
alphabet: "0123456789abcdefghijklmnopqrstuv=",
|
|
862
|
+
bitsPerChar: 5
|
|
863
|
+
});
|
|
864
|
+
var base32hexpadupper = rfc4648({
|
|
865
|
+
prefix: "T",
|
|
866
|
+
name: "base32hexpadupper",
|
|
867
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUV=",
|
|
868
|
+
bitsPerChar: 5
|
|
869
|
+
});
|
|
870
|
+
var base32z = rfc4648({
|
|
871
|
+
prefix: "h",
|
|
872
|
+
name: "base32z",
|
|
873
|
+
alphabet: "ybndrfg8ejkmcpqxot1uwisza345h769",
|
|
874
|
+
bitsPerChar: 5
|
|
875
|
+
});
|
|
876
|
+
|
|
877
|
+
// ../../node_modules/multiformats/esm/src/cid.js
|
|
878
|
+
var CID = class {
|
|
879
|
+
constructor(version2, code2, multihash, bytes) {
|
|
880
|
+
this.code = code2;
|
|
881
|
+
this.version = version2;
|
|
882
|
+
this.multihash = multihash;
|
|
883
|
+
this.bytes = bytes;
|
|
884
|
+
this.byteOffset = bytes.byteOffset;
|
|
885
|
+
this.byteLength = bytes.byteLength;
|
|
886
|
+
this.asCID = this;
|
|
887
|
+
this._baseCache = /* @__PURE__ */ new Map();
|
|
888
|
+
Object.defineProperties(this, {
|
|
889
|
+
byteOffset: hidden,
|
|
890
|
+
byteLength: hidden,
|
|
891
|
+
code: readonly,
|
|
892
|
+
version: readonly,
|
|
893
|
+
multihash: readonly,
|
|
894
|
+
bytes: readonly,
|
|
895
|
+
_baseCache: hidden,
|
|
896
|
+
asCID: hidden
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
toV0() {
|
|
900
|
+
switch (this.version) {
|
|
901
|
+
case 0: {
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
default: {
|
|
905
|
+
const { code: code2, multihash } = this;
|
|
906
|
+
if (code2 !== DAG_PB_CODE) {
|
|
907
|
+
throw new Error("Cannot convert a non dag-pb CID to CIDv0");
|
|
908
|
+
}
|
|
909
|
+
if (multihash.code !== SHA_256_CODE) {
|
|
910
|
+
throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");
|
|
911
|
+
}
|
|
912
|
+
return CID.createV0(multihash);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
toV1() {
|
|
917
|
+
switch (this.version) {
|
|
918
|
+
case 0: {
|
|
919
|
+
const { code: code2, digest: digest2 } = this.multihash;
|
|
920
|
+
const multihash = create(code2, digest2);
|
|
921
|
+
return CID.createV1(this.code, multihash);
|
|
922
|
+
}
|
|
923
|
+
case 1: {
|
|
924
|
+
return this;
|
|
925
|
+
}
|
|
926
|
+
default: {
|
|
927
|
+
throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
equals(other) {
|
|
932
|
+
return other && this.code === other.code && this.version === other.version && equals2(this.multihash, other.multihash);
|
|
933
|
+
}
|
|
934
|
+
toString(base3) {
|
|
935
|
+
const { bytes, version: version2, _baseCache } = this;
|
|
936
|
+
switch (version2) {
|
|
937
|
+
case 0:
|
|
938
|
+
return toStringV0(bytes, _baseCache, base3 || base58btc.encoder);
|
|
939
|
+
default:
|
|
940
|
+
return toStringV1(bytes, _baseCache, base3 || base32.encoder);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
toJSON() {
|
|
944
|
+
return {
|
|
945
|
+
code: this.code,
|
|
946
|
+
version: this.version,
|
|
947
|
+
hash: this.multihash.bytes
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
get [Symbol.toStringTag]() {
|
|
951
|
+
return "CID";
|
|
952
|
+
}
|
|
953
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
954
|
+
return "CID(" + this.toString() + ")";
|
|
955
|
+
}
|
|
956
|
+
static isCID(value) {
|
|
957
|
+
deprecate(/^0\.0/, IS_CID_DEPRECATION);
|
|
958
|
+
return !!(value && (value[cidSymbol] || value.asCID === value));
|
|
959
|
+
}
|
|
960
|
+
get toBaseEncodedString() {
|
|
961
|
+
throw new Error("Deprecated, use .toString()");
|
|
962
|
+
}
|
|
963
|
+
get codec() {
|
|
964
|
+
throw new Error('"codec" property is deprecated, use integer "code" property instead');
|
|
965
|
+
}
|
|
966
|
+
get buffer() {
|
|
967
|
+
throw new Error("Deprecated .buffer property, use .bytes to get Uint8Array instead");
|
|
968
|
+
}
|
|
969
|
+
get multibaseName() {
|
|
970
|
+
throw new Error('"multibaseName" property is deprecated');
|
|
971
|
+
}
|
|
972
|
+
get prefix() {
|
|
973
|
+
throw new Error('"prefix" property is deprecated');
|
|
974
|
+
}
|
|
975
|
+
static asCID(value) {
|
|
976
|
+
if (value instanceof CID) {
|
|
977
|
+
return value;
|
|
978
|
+
} else if (value != null && value.asCID === value) {
|
|
979
|
+
const { version: version2, code: code2, multihash, bytes } = value;
|
|
980
|
+
return new CID(version2, code2, multihash, bytes || encodeCID(version2, code2, multihash.bytes));
|
|
981
|
+
} else if (value != null && value[cidSymbol] === true) {
|
|
982
|
+
const { version: version2, multihash, code: code2 } = value;
|
|
983
|
+
const digest2 = decode3(multihash);
|
|
984
|
+
return CID.create(version2, code2, digest2);
|
|
985
|
+
} else {
|
|
986
|
+
return null;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
static create(version2, code2, digest2) {
|
|
990
|
+
if (typeof code2 !== "number") {
|
|
991
|
+
throw new Error("String codecs are no longer supported");
|
|
992
|
+
}
|
|
993
|
+
switch (version2) {
|
|
994
|
+
case 0: {
|
|
995
|
+
if (code2 !== DAG_PB_CODE) {
|
|
996
|
+
throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`);
|
|
997
|
+
} else {
|
|
998
|
+
return new CID(version2, code2, digest2, digest2.bytes);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
case 1: {
|
|
1002
|
+
const bytes = encodeCID(version2, code2, digest2.bytes);
|
|
1003
|
+
return new CID(version2, code2, digest2, bytes);
|
|
1004
|
+
}
|
|
1005
|
+
default: {
|
|
1006
|
+
throw new Error("Invalid version");
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
static createV0(digest2) {
|
|
1011
|
+
return CID.create(0, DAG_PB_CODE, digest2);
|
|
1012
|
+
}
|
|
1013
|
+
static createV1(code2, digest2) {
|
|
1014
|
+
return CID.create(1, code2, digest2);
|
|
1015
|
+
}
|
|
1016
|
+
static decode(bytes) {
|
|
1017
|
+
const [cid, remainder] = CID.decodeFirst(bytes);
|
|
1018
|
+
if (remainder.length) {
|
|
1019
|
+
throw new Error("Incorrect length");
|
|
1020
|
+
}
|
|
1021
|
+
return cid;
|
|
1022
|
+
}
|
|
1023
|
+
static decodeFirst(bytes) {
|
|
1024
|
+
const specs = CID.inspectBytes(bytes);
|
|
1025
|
+
const prefixSize = specs.size - specs.multihashSize;
|
|
1026
|
+
const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
|
|
1027
|
+
if (multihashBytes.byteLength !== specs.multihashSize) {
|
|
1028
|
+
throw new Error("Incorrect length");
|
|
1029
|
+
}
|
|
1030
|
+
const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
|
|
1031
|
+
const digest2 = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
|
|
1032
|
+
const cid = specs.version === 0 ? CID.createV0(digest2) : CID.createV1(specs.codec, digest2);
|
|
1033
|
+
return [
|
|
1034
|
+
cid,
|
|
1035
|
+
bytes.subarray(specs.size)
|
|
1036
|
+
];
|
|
1037
|
+
}
|
|
1038
|
+
static inspectBytes(initialBytes) {
|
|
1039
|
+
let offset = 0;
|
|
1040
|
+
const next = () => {
|
|
1041
|
+
const [i, length2] = decode2(initialBytes.subarray(offset));
|
|
1042
|
+
offset += length2;
|
|
1043
|
+
return i;
|
|
1044
|
+
};
|
|
1045
|
+
let version2 = next();
|
|
1046
|
+
let codec = DAG_PB_CODE;
|
|
1047
|
+
if (version2 === 18) {
|
|
1048
|
+
version2 = 0;
|
|
1049
|
+
offset = 0;
|
|
1050
|
+
} else if (version2 === 1) {
|
|
1051
|
+
codec = next();
|
|
1052
|
+
}
|
|
1053
|
+
if (version2 !== 0 && version2 !== 1) {
|
|
1054
|
+
throw new RangeError(`Invalid CID version ${version2}`);
|
|
1055
|
+
}
|
|
1056
|
+
const prefixSize = offset;
|
|
1057
|
+
const multihashCode = next();
|
|
1058
|
+
const digestSize = next();
|
|
1059
|
+
const size = offset + digestSize;
|
|
1060
|
+
const multihashSize = size - prefixSize;
|
|
1061
|
+
return {
|
|
1062
|
+
version: version2,
|
|
1063
|
+
codec,
|
|
1064
|
+
multihashCode,
|
|
1065
|
+
digestSize,
|
|
1066
|
+
multihashSize,
|
|
1067
|
+
size
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
static parse(source, base3) {
|
|
1071
|
+
const [prefix, bytes] = parseCIDtoBytes(source, base3);
|
|
1072
|
+
const cid = CID.decode(bytes);
|
|
1073
|
+
cid._baseCache.set(prefix, source);
|
|
1074
|
+
return cid;
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
var parseCIDtoBytes = (source, base3) => {
|
|
1078
|
+
switch (source[0]) {
|
|
1079
|
+
case "Q": {
|
|
1080
|
+
const decoder = base3 || base58btc;
|
|
1081
|
+
return [
|
|
1082
|
+
base58btc.prefix,
|
|
1083
|
+
decoder.decode(`${base58btc.prefix}${source}`)
|
|
1084
|
+
];
|
|
1085
|
+
}
|
|
1086
|
+
case base58btc.prefix: {
|
|
1087
|
+
const decoder = base3 || base58btc;
|
|
1088
|
+
return [
|
|
1089
|
+
base58btc.prefix,
|
|
1090
|
+
decoder.decode(source)
|
|
1091
|
+
];
|
|
1092
|
+
}
|
|
1093
|
+
case base32.prefix: {
|
|
1094
|
+
const decoder = base3 || base32;
|
|
1095
|
+
return [
|
|
1096
|
+
base32.prefix,
|
|
1097
|
+
decoder.decode(source)
|
|
1098
|
+
];
|
|
1099
|
+
}
|
|
1100
|
+
default: {
|
|
1101
|
+
if (base3 == null) {
|
|
1102
|
+
throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");
|
|
1103
|
+
}
|
|
1104
|
+
return [
|
|
1105
|
+
source[0],
|
|
1106
|
+
base3.decode(source)
|
|
1107
|
+
];
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
};
|
|
1111
|
+
var toStringV0 = (bytes, cache, base3) => {
|
|
1112
|
+
const { prefix } = base3;
|
|
1113
|
+
if (prefix !== base58btc.prefix) {
|
|
1114
|
+
throw Error(`Cannot string encode V0 in ${base3.name} encoding`);
|
|
1115
|
+
}
|
|
1116
|
+
const cid = cache.get(prefix);
|
|
1117
|
+
if (cid == null) {
|
|
1118
|
+
const cid2 = base3.encode(bytes).slice(1);
|
|
1119
|
+
cache.set(prefix, cid2);
|
|
1120
|
+
return cid2;
|
|
1121
|
+
} else {
|
|
1122
|
+
return cid;
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
var toStringV1 = (bytes, cache, base3) => {
|
|
1126
|
+
const { prefix } = base3;
|
|
1127
|
+
const cid = cache.get(prefix);
|
|
1128
|
+
if (cid == null) {
|
|
1129
|
+
const cid2 = base3.encode(bytes);
|
|
1130
|
+
cache.set(prefix, cid2);
|
|
1131
|
+
return cid2;
|
|
1132
|
+
} else {
|
|
1133
|
+
return cid;
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
var DAG_PB_CODE = 112;
|
|
1137
|
+
var SHA_256_CODE = 18;
|
|
1138
|
+
var encodeCID = (version2, code2, multihash) => {
|
|
1139
|
+
const codeOffset = encodingLength(version2);
|
|
1140
|
+
const hashOffset = codeOffset + encodingLength(code2);
|
|
1141
|
+
const bytes = new Uint8Array(hashOffset + multihash.byteLength);
|
|
1142
|
+
encodeTo(version2, bytes, 0);
|
|
1143
|
+
encodeTo(code2, bytes, codeOffset);
|
|
1144
|
+
bytes.set(multihash, hashOffset);
|
|
1145
|
+
return bytes;
|
|
1146
|
+
};
|
|
1147
|
+
var cidSymbol = Symbol.for("@ipld/js-cid/CID");
|
|
1148
|
+
var readonly = {
|
|
1149
|
+
writable: false,
|
|
1150
|
+
configurable: false,
|
|
1151
|
+
enumerable: true
|
|
1152
|
+
};
|
|
1153
|
+
var hidden = {
|
|
1154
|
+
writable: false,
|
|
1155
|
+
enumerable: false,
|
|
1156
|
+
configurable: false
|
|
1157
|
+
};
|
|
1158
|
+
var version = "0.0.0-dev";
|
|
1159
|
+
var deprecate = (range2, message) => {
|
|
1160
|
+
if (range2.test(version)) {
|
|
1161
|
+
console.warn(message);
|
|
1162
|
+
} else {
|
|
1163
|
+
throw new Error(message);
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
var IS_CID_DEPRECATION = `CID.isCID(v) is deprecated and will be removed in the next major release.
|
|
1167
|
+
Following code pattern:
|
|
1168
|
+
|
|
1169
|
+
if (CID.isCID(value)) {
|
|
1170
|
+
doSomethingWithCID(value)
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
Is replaced with:
|
|
1174
|
+
|
|
1175
|
+
const cid = CID.asCID(value)
|
|
1176
|
+
if (cid) {
|
|
1177
|
+
// Make sure to use cid instead of value
|
|
1178
|
+
doSomethingWithCID(cid)
|
|
1179
|
+
}
|
|
1180
|
+
`;
|
|
1181
|
+
|
|
1182
|
+
// ../../node_modules/multiformats/esm/src/bases/identity.js
|
|
1183
|
+
var identity_exports = {};
|
|
1184
|
+
__export(identity_exports, {
|
|
1185
|
+
identity: () => identity
|
|
1186
|
+
});
|
|
1187
|
+
var identity = from({
|
|
1188
|
+
prefix: "\0",
|
|
1189
|
+
name: "identity",
|
|
1190
|
+
encode: (buf) => toString(buf),
|
|
1191
|
+
decode: (str) => fromString(str)
|
|
1192
|
+
});
|
|
1193
|
+
|
|
1194
|
+
// ../../node_modules/multiformats/esm/src/bases/base2.js
|
|
1195
|
+
var base2_exports = {};
|
|
1196
|
+
__export(base2_exports, {
|
|
1197
|
+
base2: () => base2
|
|
1198
|
+
});
|
|
1199
|
+
var base2 = rfc4648({
|
|
1200
|
+
prefix: "0",
|
|
1201
|
+
name: "base2",
|
|
1202
|
+
alphabet: "01",
|
|
1203
|
+
bitsPerChar: 1
|
|
1204
|
+
});
|
|
1205
|
+
|
|
1206
|
+
// ../../node_modules/multiformats/esm/src/bases/base8.js
|
|
1207
|
+
var base8_exports = {};
|
|
1208
|
+
__export(base8_exports, {
|
|
1209
|
+
base8: () => base8
|
|
1210
|
+
});
|
|
1211
|
+
var base8 = rfc4648({
|
|
1212
|
+
prefix: "7",
|
|
1213
|
+
name: "base8",
|
|
1214
|
+
alphabet: "01234567",
|
|
1215
|
+
bitsPerChar: 3
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
// ../../node_modules/multiformats/esm/src/bases/base10.js
|
|
1219
|
+
var base10_exports = {};
|
|
1220
|
+
__export(base10_exports, {
|
|
1221
|
+
base10: () => base10
|
|
1222
|
+
});
|
|
1223
|
+
var base10 = baseX({
|
|
1224
|
+
prefix: "9",
|
|
1225
|
+
name: "base10",
|
|
1226
|
+
alphabet: "0123456789"
|
|
1227
|
+
});
|
|
1228
|
+
|
|
1229
|
+
// ../../node_modules/multiformats/esm/src/bases/base16.js
|
|
1230
|
+
var base16_exports = {};
|
|
1231
|
+
__export(base16_exports, {
|
|
1232
|
+
base16: () => base16,
|
|
1233
|
+
base16upper: () => base16upper
|
|
1234
|
+
});
|
|
1235
|
+
var base16 = rfc4648({
|
|
1236
|
+
prefix: "f",
|
|
1237
|
+
name: "base16",
|
|
1238
|
+
alphabet: "0123456789abcdef",
|
|
1239
|
+
bitsPerChar: 4
|
|
1240
|
+
});
|
|
1241
|
+
var base16upper = rfc4648({
|
|
1242
|
+
prefix: "F",
|
|
1243
|
+
name: "base16upper",
|
|
1244
|
+
alphabet: "0123456789ABCDEF",
|
|
1245
|
+
bitsPerChar: 4
|
|
1246
|
+
});
|
|
1247
|
+
|
|
1248
|
+
// ../../node_modules/multiformats/esm/src/bases/base36.js
|
|
1249
|
+
var base36_exports = {};
|
|
1250
|
+
__export(base36_exports, {
|
|
1251
|
+
base36: () => base36,
|
|
1252
|
+
base36upper: () => base36upper
|
|
1253
|
+
});
|
|
1254
|
+
var base36 = baseX({
|
|
1255
|
+
prefix: "k",
|
|
1256
|
+
name: "base36",
|
|
1257
|
+
alphabet: "0123456789abcdefghijklmnopqrstuvwxyz"
|
|
1258
|
+
});
|
|
1259
|
+
var base36upper = baseX({
|
|
1260
|
+
prefix: "K",
|
|
1261
|
+
name: "base36upper",
|
|
1262
|
+
alphabet: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
1263
|
+
});
|
|
1264
|
+
|
|
1265
|
+
// ../../node_modules/multiformats/esm/src/bases/base64.js
|
|
1266
|
+
var base64_exports = {};
|
|
1267
|
+
__export(base64_exports, {
|
|
1268
|
+
base64: () => base64,
|
|
1269
|
+
base64pad: () => base64pad,
|
|
1270
|
+
base64url: () => base64url,
|
|
1271
|
+
base64urlpad: () => base64urlpad
|
|
1272
|
+
});
|
|
1273
|
+
var base64 = rfc4648({
|
|
1274
|
+
prefix: "m",
|
|
1275
|
+
name: "base64",
|
|
1276
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
|
|
1277
|
+
bitsPerChar: 6
|
|
1278
|
+
});
|
|
1279
|
+
var base64pad = rfc4648({
|
|
1280
|
+
prefix: "M",
|
|
1281
|
+
name: "base64pad",
|
|
1282
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
|
1283
|
+
bitsPerChar: 6
|
|
1284
|
+
});
|
|
1285
|
+
var base64url = rfc4648({
|
|
1286
|
+
prefix: "u",
|
|
1287
|
+
name: "base64url",
|
|
1288
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
|
|
1289
|
+
bitsPerChar: 6
|
|
1290
|
+
});
|
|
1291
|
+
var base64urlpad = rfc4648({
|
|
1292
|
+
prefix: "U",
|
|
1293
|
+
name: "base64urlpad",
|
|
1294
|
+
alphabet: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",
|
|
1295
|
+
bitsPerChar: 6
|
|
1296
|
+
});
|
|
1297
|
+
|
|
1298
|
+
// ../../node_modules/multiformats/esm/src/bases/base256emoji.js
|
|
1299
|
+
var base256emoji_exports = {};
|
|
1300
|
+
__export(base256emoji_exports, {
|
|
1301
|
+
base256emoji: () => base256emoji
|
|
1302
|
+
});
|
|
1303
|
+
var alphabet = Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}");
|
|
1304
|
+
var alphabetBytesToChars = alphabet.reduce((p, c, i) => {
|
|
1305
|
+
p[i] = c;
|
|
1306
|
+
return p;
|
|
1307
|
+
}, []);
|
|
1308
|
+
var alphabetCharsToBytes = alphabet.reduce((p, c, i) => {
|
|
1309
|
+
p[c.codePointAt(0)] = i;
|
|
1310
|
+
return p;
|
|
1311
|
+
}, []);
|
|
1312
|
+
function encode3(data) {
|
|
1313
|
+
return data.reduce((p, c) => {
|
|
1314
|
+
p += alphabetBytesToChars[c];
|
|
1315
|
+
return p;
|
|
1316
|
+
}, "");
|
|
1317
|
+
}
|
|
1318
|
+
function decode5(str) {
|
|
1319
|
+
const byts = [];
|
|
1320
|
+
for (const char of str) {
|
|
1321
|
+
const byt = alphabetCharsToBytes[char.codePointAt(0)];
|
|
1322
|
+
if (byt === void 0) {
|
|
1323
|
+
throw new Error(`Non-base256emoji character: ${char}`);
|
|
1324
|
+
}
|
|
1325
|
+
byts.push(byt);
|
|
1326
|
+
}
|
|
1327
|
+
return new Uint8Array(byts);
|
|
1328
|
+
}
|
|
1329
|
+
var base256emoji = from({
|
|
1330
|
+
prefix: "\u{1F680}",
|
|
1331
|
+
name: "base256emoji",
|
|
1332
|
+
encode: encode3,
|
|
1333
|
+
decode: decode5
|
|
1334
|
+
});
|
|
1335
|
+
|
|
1336
|
+
// ../../node_modules/multiformats/esm/src/hashes/sha2-browser.js
|
|
1337
|
+
var sha2_browser_exports = {};
|
|
1338
|
+
__export(sha2_browser_exports, {
|
|
1339
|
+
sha256: () => sha256,
|
|
1340
|
+
sha512: () => sha512
|
|
1341
|
+
});
|
|
1342
|
+
|
|
1343
|
+
// ../../node_modules/multiformats/esm/src/hashes/hasher.js
|
|
1344
|
+
var from2 = ({ name: name2, code: code2, encode: encode5 }) => new Hasher(name2, code2, encode5);
|
|
1345
|
+
var Hasher = class {
|
|
1346
|
+
constructor(name2, code2, encode5) {
|
|
1347
|
+
this.name = name2;
|
|
1348
|
+
this.code = code2;
|
|
1349
|
+
this.encode = encode5;
|
|
1350
|
+
}
|
|
1351
|
+
digest(input) {
|
|
1352
|
+
if (input instanceof Uint8Array) {
|
|
1353
|
+
const result = this.encode(input);
|
|
1354
|
+
return result instanceof Uint8Array ? create(this.code, result) : result.then((digest2) => create(this.code, digest2));
|
|
1355
|
+
} else {
|
|
1356
|
+
throw Error("Unknown type, must be binary type");
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
// ../../node_modules/multiformats/esm/src/hashes/sha2-browser.js
|
|
1362
|
+
var sha = (name2) => async (data) => new Uint8Array(await crypto.subtle.digest(name2, data));
|
|
1363
|
+
var sha256 = from2({
|
|
1364
|
+
name: "sha2-256",
|
|
1365
|
+
code: 18,
|
|
1366
|
+
encode: sha("SHA-256")
|
|
1367
|
+
});
|
|
1368
|
+
var sha512 = from2({
|
|
1369
|
+
name: "sha2-512",
|
|
1370
|
+
code: 19,
|
|
1371
|
+
encode: sha("SHA-512")
|
|
1372
|
+
});
|
|
1373
|
+
|
|
1374
|
+
// ../../node_modules/multiformats/esm/src/hashes/identity.js
|
|
1375
|
+
var identity_exports2 = {};
|
|
1376
|
+
__export(identity_exports2, {
|
|
1377
|
+
identity: () => identity2
|
|
1378
|
+
});
|
|
1379
|
+
var code = 0;
|
|
1380
|
+
var name = "identity";
|
|
1381
|
+
var encode4 = coerce;
|
|
1382
|
+
var digest = (input) => create(code, encode4(input));
|
|
1383
|
+
var identity2 = {
|
|
1384
|
+
code,
|
|
1385
|
+
name,
|
|
1386
|
+
encode: encode4,
|
|
1387
|
+
digest
|
|
1388
|
+
};
|
|
1389
|
+
|
|
1390
|
+
// ../../node_modules/multiformats/esm/src/codecs/json.js
|
|
1391
|
+
var textEncoder = new TextEncoder();
|
|
1392
|
+
var textDecoder = new TextDecoder();
|
|
1393
|
+
|
|
1394
|
+
// ../../node_modules/multiformats/esm/src/basics.js
|
|
1395
|
+
var bases = {
|
|
1396
|
+
...identity_exports,
|
|
1397
|
+
...base2_exports,
|
|
1398
|
+
...base8_exports,
|
|
1399
|
+
...base10_exports,
|
|
1400
|
+
...base16_exports,
|
|
1401
|
+
...base32_exports,
|
|
1402
|
+
...base36_exports,
|
|
1403
|
+
...base58_exports,
|
|
1404
|
+
...base64_exports,
|
|
1405
|
+
...base256emoji_exports
|
|
1406
|
+
};
|
|
1407
|
+
var hashes = {
|
|
1408
|
+
...sha2_browser_exports,
|
|
1409
|
+
...identity_exports2
|
|
1410
|
+
};
|
|
1411
|
+
|
|
1412
|
+
// ../../node_modules/uint8arrays/esm/src/util/bases.js
|
|
1413
|
+
function createCodec(name2, prefix, encode5, decode6) {
|
|
1414
|
+
return {
|
|
1415
|
+
name: name2,
|
|
1416
|
+
prefix,
|
|
1417
|
+
encoder: {
|
|
1418
|
+
name: name2,
|
|
1419
|
+
prefix,
|
|
1420
|
+
encode: encode5
|
|
1421
|
+
},
|
|
1422
|
+
decoder: { decode: decode6 }
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
var string = createCodec("utf8", "u", (buf) => {
|
|
1426
|
+
const decoder = new TextDecoder("utf8");
|
|
1427
|
+
return "u" + decoder.decode(buf);
|
|
1428
|
+
}, (str) => {
|
|
1429
|
+
const encoder = new TextEncoder();
|
|
1430
|
+
return encoder.encode(str.substring(1));
|
|
1431
|
+
});
|
|
1432
|
+
var ascii = createCodec("ascii", "a", (buf) => {
|
|
1433
|
+
let string2 = "a";
|
|
1434
|
+
for (let i = 0; i < buf.length; i++) {
|
|
1435
|
+
string2 += String.fromCharCode(buf[i]);
|
|
1436
|
+
}
|
|
1437
|
+
return string2;
|
|
1438
|
+
}, (str) => {
|
|
1439
|
+
str = str.substring(1);
|
|
1440
|
+
const buf = new Uint8Array(str.length);
|
|
1441
|
+
for (let i = 0; i < str.length; i++) {
|
|
1442
|
+
buf[i] = str.charCodeAt(i);
|
|
1443
|
+
}
|
|
1444
|
+
return buf;
|
|
1445
|
+
});
|
|
1446
|
+
var BASES = {
|
|
1447
|
+
utf8: string,
|
|
1448
|
+
"utf-8": string,
|
|
1449
|
+
hex: bases.base16,
|
|
1450
|
+
latin1: ascii,
|
|
1451
|
+
ascii,
|
|
1452
|
+
binary: ascii,
|
|
1453
|
+
...bases
|
|
1454
|
+
};
|
|
1455
|
+
var bases_default = BASES;
|
|
1456
|
+
|
|
1457
|
+
// ../../node_modules/uint8arrays/esm/src/from-string.js
|
|
1458
|
+
function fromString2(string2, encoding = "utf8") {
|
|
1459
|
+
const base3 = bases_default[encoding];
|
|
1460
|
+
if (!base3) {
|
|
1461
|
+
throw new Error(`Unsupported encoding "${encoding}"`);
|
|
1462
|
+
}
|
|
1463
|
+
return base3.decoder.decode(`${base3.prefix}${string2}`);
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
// ../../node_modules/uint8arrays/esm/src/to-string.js
|
|
1467
|
+
function toString2(array, encoding = "utf8") {
|
|
1468
|
+
const base3 = bases_default[encoding];
|
|
1469
|
+
if (!base3) {
|
|
1470
|
+
throw new Error(`Unsupported encoding "${encoding}"`);
|
|
1471
|
+
}
|
|
1472
|
+
return base3.encoder.encode(array).substring(1);
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
// src/ipld.ts
|
|
1476
|
+
var jsonToIpld = (val) => {
|
|
1477
|
+
if (Array.isArray(val)) {
|
|
1478
|
+
return val.map((item) => jsonToIpld(item));
|
|
1479
|
+
}
|
|
1480
|
+
if (val && typeof val === "object") {
|
|
1481
|
+
if (typeof val["$link"] === "string" && Object.keys(val).length === 1) {
|
|
1482
|
+
return CID.parse(val["$link"]);
|
|
1483
|
+
}
|
|
1484
|
+
if (typeof val["$bytes"] === "string" && Object.keys(val).length === 1) {
|
|
1485
|
+
return fromString2(val["$bytes"], "base64");
|
|
1486
|
+
}
|
|
1487
|
+
const toReturn = {};
|
|
1488
|
+
for (const key of Object.keys(val)) {
|
|
1489
|
+
toReturn[key] = jsonToIpld(val[key]);
|
|
1490
|
+
}
|
|
1491
|
+
return toReturn;
|
|
1492
|
+
}
|
|
1493
|
+
return val;
|
|
1494
|
+
};
|
|
1495
|
+
var ipldToJson = (val) => {
|
|
1496
|
+
if (Array.isArray(val)) {
|
|
1497
|
+
return val.map((item) => ipldToJson(item));
|
|
1498
|
+
}
|
|
1499
|
+
if (val && typeof val === "object") {
|
|
1500
|
+
if (val instanceof Uint8Array) {
|
|
1501
|
+
return {
|
|
1502
|
+
$bytes: toString2(val, "base64")
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
if (CID.asCID(val)) {
|
|
1506
|
+
return {
|
|
1507
|
+
$link: val.toString()
|
|
1508
|
+
};
|
|
1509
|
+
}
|
|
1510
|
+
const toReturn = {};
|
|
1511
|
+
for (const key of Object.keys(val)) {
|
|
1512
|
+
toReturn[key] = ipldToJson(val[key]);
|
|
1513
|
+
}
|
|
1514
|
+
return toReturn;
|
|
1515
|
+
}
|
|
1516
|
+
return val;
|
|
1517
|
+
};
|
|
1518
|
+
|
|
1519
|
+
// ../../node_modules/zod/lib/index.mjs
|
|
1520
|
+
var util;
|
|
1521
|
+
(function(util2) {
|
|
1522
|
+
util2.assertEqual = (val) => val;
|
|
1523
|
+
function assertIs(_arg) {
|
|
1524
|
+
}
|
|
1525
|
+
util2.assertIs = assertIs;
|
|
1526
|
+
function assertNever(_x) {
|
|
1527
|
+
throw new Error();
|
|
1528
|
+
}
|
|
1529
|
+
util2.assertNever = assertNever;
|
|
1530
|
+
util2.arrayToEnum = (items) => {
|
|
1531
|
+
const obj = {};
|
|
1532
|
+
for (const item of items) {
|
|
1533
|
+
obj[item] = item;
|
|
1534
|
+
}
|
|
1535
|
+
return obj;
|
|
1536
|
+
};
|
|
1537
|
+
util2.getValidEnumValues = (obj) => {
|
|
1538
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
1539
|
+
const filtered = {};
|
|
1540
|
+
for (const k of validKeys) {
|
|
1541
|
+
filtered[k] = obj[k];
|
|
1542
|
+
}
|
|
1543
|
+
return util2.objectValues(filtered);
|
|
1544
|
+
};
|
|
1545
|
+
util2.objectValues = (obj) => {
|
|
1546
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
1547
|
+
return obj[e];
|
|
1548
|
+
});
|
|
1549
|
+
};
|
|
1550
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
1551
|
+
const keys = [];
|
|
1552
|
+
for (const key in object) {
|
|
1553
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
1554
|
+
keys.push(key);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return keys;
|
|
1558
|
+
};
|
|
1559
|
+
util2.find = (arr, checker) => {
|
|
1560
|
+
for (const item of arr) {
|
|
1561
|
+
if (checker(item))
|
|
1562
|
+
return item;
|
|
1563
|
+
}
|
|
1564
|
+
return void 0;
|
|
1565
|
+
};
|
|
1566
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
1567
|
+
function joinValues(array, separator = " | ") {
|
|
1568
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
1569
|
+
}
|
|
1570
|
+
util2.joinValues = joinValues;
|
|
1571
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
1572
|
+
if (typeof value === "bigint") {
|
|
1573
|
+
return value.toString();
|
|
1574
|
+
}
|
|
1575
|
+
return value;
|
|
1576
|
+
};
|
|
1577
|
+
})(util || (util = {}));
|
|
1578
|
+
var ZodParsedType = util.arrayToEnum([
|
|
1579
|
+
"string",
|
|
1580
|
+
"nan",
|
|
1581
|
+
"number",
|
|
1582
|
+
"integer",
|
|
1583
|
+
"float",
|
|
1584
|
+
"boolean",
|
|
1585
|
+
"date",
|
|
1586
|
+
"bigint",
|
|
1587
|
+
"symbol",
|
|
1588
|
+
"function",
|
|
1589
|
+
"undefined",
|
|
1590
|
+
"null",
|
|
1591
|
+
"array",
|
|
1592
|
+
"object",
|
|
1593
|
+
"unknown",
|
|
1594
|
+
"promise",
|
|
1595
|
+
"void",
|
|
1596
|
+
"never",
|
|
1597
|
+
"map",
|
|
1598
|
+
"set"
|
|
1599
|
+
]);
|
|
1600
|
+
var getParsedType = (data) => {
|
|
1601
|
+
const t = typeof data;
|
|
1602
|
+
switch (t) {
|
|
1603
|
+
case "undefined":
|
|
1604
|
+
return ZodParsedType.undefined;
|
|
1605
|
+
case "string":
|
|
1606
|
+
return ZodParsedType.string;
|
|
1607
|
+
case "number":
|
|
1608
|
+
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
1609
|
+
case "boolean":
|
|
1610
|
+
return ZodParsedType.boolean;
|
|
1611
|
+
case "function":
|
|
1612
|
+
return ZodParsedType.function;
|
|
1613
|
+
case "bigint":
|
|
1614
|
+
return ZodParsedType.bigint;
|
|
1615
|
+
case "object":
|
|
1616
|
+
if (Array.isArray(data)) {
|
|
1617
|
+
return ZodParsedType.array;
|
|
1618
|
+
}
|
|
1619
|
+
if (data === null) {
|
|
1620
|
+
return ZodParsedType.null;
|
|
1621
|
+
}
|
|
1622
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
1623
|
+
return ZodParsedType.promise;
|
|
1624
|
+
}
|
|
1625
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
1626
|
+
return ZodParsedType.map;
|
|
1627
|
+
}
|
|
1628
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
1629
|
+
return ZodParsedType.set;
|
|
1630
|
+
}
|
|
1631
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
1632
|
+
return ZodParsedType.date;
|
|
1633
|
+
}
|
|
1634
|
+
return ZodParsedType.object;
|
|
1635
|
+
default:
|
|
1636
|
+
return ZodParsedType.unknown;
|
|
1637
|
+
}
|
|
1638
|
+
};
|
|
1639
|
+
var ZodIssueCode = util.arrayToEnum([
|
|
1640
|
+
"invalid_type",
|
|
1641
|
+
"invalid_literal",
|
|
1642
|
+
"custom",
|
|
1643
|
+
"invalid_union",
|
|
1644
|
+
"invalid_union_discriminator",
|
|
1645
|
+
"invalid_enum_value",
|
|
1646
|
+
"unrecognized_keys",
|
|
1647
|
+
"invalid_arguments",
|
|
1648
|
+
"invalid_return_type",
|
|
1649
|
+
"invalid_date",
|
|
1650
|
+
"invalid_string",
|
|
1651
|
+
"too_small",
|
|
1652
|
+
"too_big",
|
|
1653
|
+
"invalid_intersection_types",
|
|
1654
|
+
"not_multiple_of"
|
|
1655
|
+
]);
|
|
1656
|
+
var quotelessJson = (obj) => {
|
|
1657
|
+
const json = JSON.stringify(obj, null, 2);
|
|
1658
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
1659
|
+
};
|
|
1660
|
+
var ZodError = class extends Error {
|
|
1661
|
+
constructor(issues) {
|
|
1662
|
+
super();
|
|
1663
|
+
this.issues = [];
|
|
1664
|
+
this.addIssue = (sub) => {
|
|
1665
|
+
this.issues = [...this.issues, sub];
|
|
1666
|
+
};
|
|
1667
|
+
this.addIssues = (subs = []) => {
|
|
1668
|
+
this.issues = [...this.issues, ...subs];
|
|
1669
|
+
};
|
|
1670
|
+
const actualProto = new.target.prototype;
|
|
1671
|
+
if (Object.setPrototypeOf) {
|
|
1672
|
+
Object.setPrototypeOf(this, actualProto);
|
|
1673
|
+
} else {
|
|
1674
|
+
this.__proto__ = actualProto;
|
|
1675
|
+
}
|
|
1676
|
+
this.name = "ZodError";
|
|
1677
|
+
this.issues = issues;
|
|
1678
|
+
}
|
|
1679
|
+
get errors() {
|
|
1680
|
+
return this.issues;
|
|
1681
|
+
}
|
|
1682
|
+
format(_mapper) {
|
|
1683
|
+
const mapper = _mapper || function(issue) {
|
|
1684
|
+
return issue.message;
|
|
1685
|
+
};
|
|
1686
|
+
const fieldErrors = { _errors: [] };
|
|
1687
|
+
const processError = (error) => {
|
|
1688
|
+
for (const issue of error.issues) {
|
|
1689
|
+
if (issue.code === "invalid_union") {
|
|
1690
|
+
issue.unionErrors.map(processError);
|
|
1691
|
+
} else if (issue.code === "invalid_return_type") {
|
|
1692
|
+
processError(issue.returnTypeError);
|
|
1693
|
+
} else if (issue.code === "invalid_arguments") {
|
|
1694
|
+
processError(issue.argumentsError);
|
|
1695
|
+
} else if (issue.path.length === 0) {
|
|
1696
|
+
fieldErrors._errors.push(mapper(issue));
|
|
1697
|
+
} else {
|
|
1698
|
+
let curr = fieldErrors;
|
|
1699
|
+
let i = 0;
|
|
1700
|
+
while (i < issue.path.length) {
|
|
1701
|
+
const el = issue.path[i];
|
|
1702
|
+
const terminal = i === issue.path.length - 1;
|
|
1703
|
+
if (!terminal) {
|
|
1704
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1705
|
+
} else {
|
|
1706
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1707
|
+
curr[el]._errors.push(mapper(issue));
|
|
1708
|
+
}
|
|
1709
|
+
curr = curr[el];
|
|
1710
|
+
i++;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
};
|
|
1715
|
+
processError(this);
|
|
1716
|
+
return fieldErrors;
|
|
1717
|
+
}
|
|
1718
|
+
toString() {
|
|
1719
|
+
return this.message;
|
|
1720
|
+
}
|
|
1721
|
+
get message() {
|
|
1722
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
1723
|
+
}
|
|
1724
|
+
get isEmpty() {
|
|
1725
|
+
return this.issues.length === 0;
|
|
1726
|
+
}
|
|
1727
|
+
flatten(mapper = (issue) => issue.message) {
|
|
1728
|
+
const fieldErrors = {};
|
|
1729
|
+
const formErrors = [];
|
|
1730
|
+
for (const sub of this.issues) {
|
|
1731
|
+
if (sub.path.length > 0) {
|
|
1732
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
1733
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
1734
|
+
} else {
|
|
1735
|
+
formErrors.push(mapper(sub));
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
return { formErrors, fieldErrors };
|
|
1739
|
+
}
|
|
1740
|
+
get formErrors() {
|
|
1741
|
+
return this.flatten();
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
ZodError.create = (issues) => {
|
|
1745
|
+
const error = new ZodError(issues);
|
|
1746
|
+
return error;
|
|
1747
|
+
};
|
|
1748
|
+
var errorMap = (issue, _ctx) => {
|
|
1749
|
+
let message;
|
|
1750
|
+
switch (issue.code) {
|
|
1751
|
+
case ZodIssueCode.invalid_type:
|
|
1752
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
1753
|
+
message = "Required";
|
|
1754
|
+
} else {
|
|
1755
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
1756
|
+
}
|
|
1757
|
+
break;
|
|
1758
|
+
case ZodIssueCode.invalid_literal:
|
|
1759
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
1760
|
+
break;
|
|
1761
|
+
case ZodIssueCode.unrecognized_keys:
|
|
1762
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
1763
|
+
break;
|
|
1764
|
+
case ZodIssueCode.invalid_union:
|
|
1765
|
+
message = `Invalid input`;
|
|
1766
|
+
break;
|
|
1767
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
1768
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
1769
|
+
break;
|
|
1770
|
+
case ZodIssueCode.invalid_enum_value:
|
|
1771
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
1772
|
+
break;
|
|
1773
|
+
case ZodIssueCode.invalid_arguments:
|
|
1774
|
+
message = `Invalid function arguments`;
|
|
1775
|
+
break;
|
|
1776
|
+
case ZodIssueCode.invalid_return_type:
|
|
1777
|
+
message = `Invalid function return type`;
|
|
1778
|
+
break;
|
|
1779
|
+
case ZodIssueCode.invalid_date:
|
|
1780
|
+
message = `Invalid date`;
|
|
1781
|
+
break;
|
|
1782
|
+
case ZodIssueCode.invalid_string:
|
|
1783
|
+
if (typeof issue.validation === "object") {
|
|
1784
|
+
if ("startsWith" in issue.validation) {
|
|
1785
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
1786
|
+
} else if ("endsWith" in issue.validation) {
|
|
1787
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
1788
|
+
} else {
|
|
1789
|
+
util.assertNever(issue.validation);
|
|
1790
|
+
}
|
|
1791
|
+
} else if (issue.validation !== "regex") {
|
|
1792
|
+
message = `Invalid ${issue.validation}`;
|
|
1793
|
+
} else {
|
|
1794
|
+
message = "Invalid";
|
|
1795
|
+
}
|
|
1796
|
+
break;
|
|
1797
|
+
case ZodIssueCode.too_small:
|
|
1798
|
+
if (issue.type === "array")
|
|
1799
|
+
message = `Array must contain ${issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
1800
|
+
else if (issue.type === "string")
|
|
1801
|
+
message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
1802
|
+
else if (issue.type === "number")
|
|
1803
|
+
message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
|
|
1804
|
+
else if (issue.type === "date")
|
|
1805
|
+
message = `Date must be greater than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.minimum)}`;
|
|
1806
|
+
else
|
|
1807
|
+
message = "Invalid input";
|
|
1808
|
+
break;
|
|
1809
|
+
case ZodIssueCode.too_big:
|
|
1810
|
+
if (issue.type === "array")
|
|
1811
|
+
message = `Array must contain ${issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
1812
|
+
else if (issue.type === "string")
|
|
1813
|
+
message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
1814
|
+
else if (issue.type === "number")
|
|
1815
|
+
message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
|
|
1816
|
+
else if (issue.type === "date")
|
|
1817
|
+
message = `Date must be smaller than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.maximum)}`;
|
|
1818
|
+
else
|
|
1819
|
+
message = "Invalid input";
|
|
1820
|
+
break;
|
|
1821
|
+
case ZodIssueCode.custom:
|
|
1822
|
+
message = `Invalid input`;
|
|
1823
|
+
break;
|
|
1824
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
1825
|
+
message = `Intersection results could not be merged`;
|
|
1826
|
+
break;
|
|
1827
|
+
case ZodIssueCode.not_multiple_of:
|
|
1828
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
1829
|
+
break;
|
|
1830
|
+
default:
|
|
1831
|
+
message = _ctx.defaultError;
|
|
1832
|
+
util.assertNever(issue);
|
|
1833
|
+
}
|
|
1834
|
+
return { message };
|
|
1835
|
+
};
|
|
1836
|
+
var overrideErrorMap = errorMap;
|
|
1837
|
+
function setErrorMap(map) {
|
|
1838
|
+
overrideErrorMap = map;
|
|
1839
|
+
}
|
|
1840
|
+
function getErrorMap() {
|
|
1841
|
+
return overrideErrorMap;
|
|
1842
|
+
}
|
|
1843
|
+
var makeIssue = (params) => {
|
|
1844
|
+
const { data, path, errorMaps, issueData } = params;
|
|
1845
|
+
const fullPath = [...path, ...issueData.path || []];
|
|
1846
|
+
const fullIssue = {
|
|
1847
|
+
...issueData,
|
|
1848
|
+
path: fullPath
|
|
1849
|
+
};
|
|
1850
|
+
let errorMessage = "";
|
|
1851
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
1852
|
+
for (const map of maps) {
|
|
1853
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
1854
|
+
}
|
|
1855
|
+
return {
|
|
1856
|
+
...issueData,
|
|
1857
|
+
path: fullPath,
|
|
1858
|
+
message: issueData.message || errorMessage
|
|
1859
|
+
};
|
|
1860
|
+
};
|
|
1861
|
+
var EMPTY_PATH = [];
|
|
1862
|
+
function addIssueToContext(ctx, issueData) {
|
|
1863
|
+
const issue = makeIssue({
|
|
1864
|
+
issueData,
|
|
1865
|
+
data: ctx.data,
|
|
1866
|
+
path: ctx.path,
|
|
1867
|
+
errorMaps: [
|
|
1868
|
+
ctx.common.contextualErrorMap,
|
|
1869
|
+
ctx.schemaErrorMap,
|
|
1870
|
+
getErrorMap(),
|
|
1871
|
+
errorMap
|
|
1872
|
+
].filter((x) => !!x)
|
|
1873
|
+
});
|
|
1874
|
+
ctx.common.issues.push(issue);
|
|
1875
|
+
}
|
|
1876
|
+
var ParseStatus = class {
|
|
1877
|
+
constructor() {
|
|
1878
|
+
this.value = "valid";
|
|
1879
|
+
}
|
|
1880
|
+
dirty() {
|
|
1881
|
+
if (this.value === "valid")
|
|
1882
|
+
this.value = "dirty";
|
|
1883
|
+
}
|
|
1884
|
+
abort() {
|
|
1885
|
+
if (this.value !== "aborted")
|
|
1886
|
+
this.value = "aborted";
|
|
1887
|
+
}
|
|
1888
|
+
static mergeArray(status, results) {
|
|
1889
|
+
const arrayValue = [];
|
|
1890
|
+
for (const s of results) {
|
|
1891
|
+
if (s.status === "aborted")
|
|
1892
|
+
return INVALID;
|
|
1893
|
+
if (s.status === "dirty")
|
|
1894
|
+
status.dirty();
|
|
1895
|
+
arrayValue.push(s.value);
|
|
1896
|
+
}
|
|
1897
|
+
return { status: status.value, value: arrayValue };
|
|
1898
|
+
}
|
|
1899
|
+
static async mergeObjectAsync(status, pairs) {
|
|
1900
|
+
const syncPairs = [];
|
|
1901
|
+
for (const pair of pairs) {
|
|
1902
|
+
syncPairs.push({
|
|
1903
|
+
key: await pair.key,
|
|
1904
|
+
value: await pair.value
|
|
1905
|
+
});
|
|
1906
|
+
}
|
|
1907
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1908
|
+
}
|
|
1909
|
+
static mergeObjectSync(status, pairs) {
|
|
1910
|
+
const finalObject = {};
|
|
1911
|
+
for (const pair of pairs) {
|
|
1912
|
+
const { key, value } = pair;
|
|
1913
|
+
if (key.status === "aborted")
|
|
1914
|
+
return INVALID;
|
|
1915
|
+
if (value.status === "aborted")
|
|
1916
|
+
return INVALID;
|
|
1917
|
+
if (key.status === "dirty")
|
|
1918
|
+
status.dirty();
|
|
1919
|
+
if (value.status === "dirty")
|
|
1920
|
+
status.dirty();
|
|
1921
|
+
if (typeof value.value !== "undefined" || pair.alwaysSet) {
|
|
1922
|
+
finalObject[key.value] = value.value;
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
return { status: status.value, value: finalObject };
|
|
1926
|
+
}
|
|
1927
|
+
};
|
|
1928
|
+
var INVALID = Object.freeze({
|
|
1929
|
+
status: "aborted"
|
|
1930
|
+
});
|
|
1931
|
+
var DIRTY = (value) => ({ status: "dirty", value });
|
|
1932
|
+
var OK = (value) => ({ status: "valid", value });
|
|
1933
|
+
var isAborted = (x) => x.status === "aborted";
|
|
1934
|
+
var isDirty = (x) => x.status === "dirty";
|
|
1935
|
+
var isValid = (x) => x.status === "valid";
|
|
1936
|
+
var isAsync = (x) => typeof Promise !== void 0 && x instanceof Promise;
|
|
1937
|
+
var errorUtil;
|
|
1938
|
+
(function(errorUtil2) {
|
|
1939
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
1940
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
1941
|
+
})(errorUtil || (errorUtil = {}));
|
|
1942
|
+
var ParseInputLazyPath = class {
|
|
1943
|
+
constructor(parent, value, path, key) {
|
|
1944
|
+
this.parent = parent;
|
|
1945
|
+
this.data = value;
|
|
1946
|
+
this._path = path;
|
|
1947
|
+
this._key = key;
|
|
1948
|
+
}
|
|
1949
|
+
get path() {
|
|
1950
|
+
return this._path.concat(this._key);
|
|
1951
|
+
}
|
|
1952
|
+
};
|
|
1953
|
+
var handleResult = (ctx, result) => {
|
|
1954
|
+
if (isValid(result)) {
|
|
1955
|
+
return { success: true, data: result.value };
|
|
1956
|
+
} else {
|
|
1957
|
+
if (!ctx.common.issues.length) {
|
|
1958
|
+
throw new Error("Validation failed but no issues detected.");
|
|
1959
|
+
}
|
|
1960
|
+
const error = new ZodError(ctx.common.issues);
|
|
1961
|
+
return { success: false, error };
|
|
1962
|
+
}
|
|
1963
|
+
};
|
|
1964
|
+
function processCreateParams(params) {
|
|
1965
|
+
if (!params)
|
|
1966
|
+
return {};
|
|
1967
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
1968
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
1969
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
1970
|
+
}
|
|
1971
|
+
if (errorMap2)
|
|
1972
|
+
return { errorMap: errorMap2, description };
|
|
1973
|
+
const customMap = (iss, ctx) => {
|
|
1974
|
+
if (iss.code !== "invalid_type")
|
|
1975
|
+
return { message: ctx.defaultError };
|
|
1976
|
+
if (typeof ctx.data === "undefined") {
|
|
1977
|
+
return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
|
|
1978
|
+
}
|
|
1979
|
+
return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
|
|
1980
|
+
};
|
|
1981
|
+
return { errorMap: customMap, description };
|
|
1982
|
+
}
|
|
1983
|
+
var ZodType = class {
|
|
1984
|
+
constructor(def2) {
|
|
1985
|
+
this.spa = this.safeParseAsync;
|
|
1986
|
+
this.superRefine = this._refinement;
|
|
1987
|
+
this._def = def2;
|
|
1988
|
+
this.parse = this.parse.bind(this);
|
|
1989
|
+
this.safeParse = this.safeParse.bind(this);
|
|
1990
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
1991
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
1992
|
+
this.spa = this.spa.bind(this);
|
|
1993
|
+
this.refine = this.refine.bind(this);
|
|
1994
|
+
this.refinement = this.refinement.bind(this);
|
|
1995
|
+
this.superRefine = this.superRefine.bind(this);
|
|
1996
|
+
this.optional = this.optional.bind(this);
|
|
1997
|
+
this.nullable = this.nullable.bind(this);
|
|
1998
|
+
this.nullish = this.nullish.bind(this);
|
|
1999
|
+
this.array = this.array.bind(this);
|
|
2000
|
+
this.promise = this.promise.bind(this);
|
|
2001
|
+
this.or = this.or.bind(this);
|
|
2002
|
+
this.and = this.and.bind(this);
|
|
2003
|
+
this.transform = this.transform.bind(this);
|
|
2004
|
+
this.default = this.default.bind(this);
|
|
2005
|
+
this.describe = this.describe.bind(this);
|
|
2006
|
+
this.isNullable = this.isNullable.bind(this);
|
|
2007
|
+
this.isOptional = this.isOptional.bind(this);
|
|
2008
|
+
}
|
|
2009
|
+
get description() {
|
|
2010
|
+
return this._def.description;
|
|
2011
|
+
}
|
|
2012
|
+
_getType(input) {
|
|
2013
|
+
return getParsedType(input.data);
|
|
2014
|
+
}
|
|
2015
|
+
_getOrReturnCtx(input, ctx) {
|
|
2016
|
+
return ctx || {
|
|
2017
|
+
common: input.parent.common,
|
|
2018
|
+
data: input.data,
|
|
2019
|
+
parsedType: getParsedType(input.data),
|
|
2020
|
+
schemaErrorMap: this._def.errorMap,
|
|
2021
|
+
path: input.path,
|
|
2022
|
+
parent: input.parent
|
|
2023
|
+
};
|
|
2024
|
+
}
|
|
2025
|
+
_processInputParams(input) {
|
|
2026
|
+
return {
|
|
2027
|
+
status: new ParseStatus(),
|
|
2028
|
+
ctx: {
|
|
2029
|
+
common: input.parent.common,
|
|
2030
|
+
data: input.data,
|
|
2031
|
+
parsedType: getParsedType(input.data),
|
|
2032
|
+
schemaErrorMap: this._def.errorMap,
|
|
2033
|
+
path: input.path,
|
|
2034
|
+
parent: input.parent
|
|
2035
|
+
}
|
|
2036
|
+
};
|
|
2037
|
+
}
|
|
2038
|
+
_parseSync(input) {
|
|
2039
|
+
const result = this._parse(input);
|
|
2040
|
+
if (isAsync(result)) {
|
|
2041
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
2042
|
+
}
|
|
2043
|
+
return result;
|
|
2044
|
+
}
|
|
2045
|
+
_parseAsync(input) {
|
|
2046
|
+
const result = this._parse(input);
|
|
2047
|
+
return Promise.resolve(result);
|
|
2048
|
+
}
|
|
2049
|
+
parse(data, params) {
|
|
2050
|
+
const result = this.safeParse(data, params);
|
|
2051
|
+
if (result.success)
|
|
2052
|
+
return result.data;
|
|
2053
|
+
throw result.error;
|
|
2054
|
+
}
|
|
2055
|
+
safeParse(data, params) {
|
|
2056
|
+
var _a;
|
|
2057
|
+
const ctx = {
|
|
2058
|
+
common: {
|
|
2059
|
+
issues: [],
|
|
2060
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
2061
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap
|
|
2062
|
+
},
|
|
2063
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
2064
|
+
schemaErrorMap: this._def.errorMap,
|
|
2065
|
+
parent: null,
|
|
2066
|
+
data,
|
|
2067
|
+
parsedType: getParsedType(data)
|
|
2068
|
+
};
|
|
2069
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
2070
|
+
return handleResult(ctx, result);
|
|
2071
|
+
}
|
|
2072
|
+
async parseAsync(data, params) {
|
|
2073
|
+
const result = await this.safeParseAsync(data, params);
|
|
2074
|
+
if (result.success)
|
|
2075
|
+
return result.data;
|
|
2076
|
+
throw result.error;
|
|
2077
|
+
}
|
|
2078
|
+
async safeParseAsync(data, params) {
|
|
2079
|
+
const ctx = {
|
|
2080
|
+
common: {
|
|
2081
|
+
issues: [],
|
|
2082
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
2083
|
+
async: true
|
|
2084
|
+
},
|
|
2085
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
2086
|
+
schemaErrorMap: this._def.errorMap,
|
|
2087
|
+
parent: null,
|
|
2088
|
+
data,
|
|
2089
|
+
parsedType: getParsedType(data)
|
|
2090
|
+
};
|
|
2091
|
+
const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
|
|
2092
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
2093
|
+
return handleResult(ctx, result);
|
|
2094
|
+
}
|
|
2095
|
+
refine(check, message) {
|
|
2096
|
+
const getIssueProperties = (val) => {
|
|
2097
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
2098
|
+
return { message };
|
|
2099
|
+
} else if (typeof message === "function") {
|
|
2100
|
+
return message(val);
|
|
2101
|
+
} else {
|
|
2102
|
+
return message;
|
|
2103
|
+
}
|
|
2104
|
+
};
|
|
2105
|
+
return this._refinement((val, ctx) => {
|
|
2106
|
+
const result = check(val);
|
|
2107
|
+
const setError = () => ctx.addIssue({
|
|
2108
|
+
code: ZodIssueCode.custom,
|
|
2109
|
+
...getIssueProperties(val)
|
|
2110
|
+
});
|
|
2111
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
2112
|
+
return result.then((data) => {
|
|
2113
|
+
if (!data) {
|
|
2114
|
+
setError();
|
|
2115
|
+
return false;
|
|
2116
|
+
} else {
|
|
2117
|
+
return true;
|
|
2118
|
+
}
|
|
2119
|
+
});
|
|
2120
|
+
}
|
|
2121
|
+
if (!result) {
|
|
2122
|
+
setError();
|
|
2123
|
+
return false;
|
|
2124
|
+
} else {
|
|
2125
|
+
return true;
|
|
2126
|
+
}
|
|
2127
|
+
});
|
|
2128
|
+
}
|
|
2129
|
+
refinement(check, refinementData) {
|
|
2130
|
+
return this._refinement((val, ctx) => {
|
|
2131
|
+
if (!check(val)) {
|
|
2132
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
2133
|
+
return false;
|
|
2134
|
+
} else {
|
|
2135
|
+
return true;
|
|
2136
|
+
}
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
_refinement(refinement) {
|
|
2140
|
+
return new ZodEffects({
|
|
2141
|
+
schema: this,
|
|
2142
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2143
|
+
effect: { type: "refinement", refinement }
|
|
2144
|
+
});
|
|
2145
|
+
}
|
|
2146
|
+
optional() {
|
|
2147
|
+
return ZodOptional.create(this);
|
|
2148
|
+
}
|
|
2149
|
+
nullable() {
|
|
2150
|
+
return ZodNullable.create(this);
|
|
2151
|
+
}
|
|
2152
|
+
nullish() {
|
|
2153
|
+
return this.optional().nullable();
|
|
2154
|
+
}
|
|
2155
|
+
array() {
|
|
2156
|
+
return ZodArray.create(this);
|
|
2157
|
+
}
|
|
2158
|
+
promise() {
|
|
2159
|
+
return ZodPromise.create(this);
|
|
2160
|
+
}
|
|
2161
|
+
or(option) {
|
|
2162
|
+
return ZodUnion.create([this, option]);
|
|
2163
|
+
}
|
|
2164
|
+
and(incoming) {
|
|
2165
|
+
return ZodIntersection.create(this, incoming);
|
|
2166
|
+
}
|
|
2167
|
+
transform(transform) {
|
|
2168
|
+
return new ZodEffects({
|
|
2169
|
+
schema: this,
|
|
2170
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2171
|
+
effect: { type: "transform", transform }
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
default(def2) {
|
|
2175
|
+
const defaultValueFunc = typeof def2 === "function" ? def2 : () => def2;
|
|
2176
|
+
return new ZodDefault({
|
|
2177
|
+
innerType: this,
|
|
2178
|
+
defaultValue: defaultValueFunc,
|
|
2179
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
2180
|
+
});
|
|
2181
|
+
}
|
|
2182
|
+
brand() {
|
|
2183
|
+
return new ZodBranded({
|
|
2184
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
2185
|
+
type: this,
|
|
2186
|
+
...processCreateParams(void 0)
|
|
2187
|
+
});
|
|
2188
|
+
}
|
|
2189
|
+
describe(description) {
|
|
2190
|
+
const This = this.constructor;
|
|
2191
|
+
return new This({
|
|
2192
|
+
...this._def,
|
|
2193
|
+
description
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
isOptional() {
|
|
2197
|
+
return this.safeParse(void 0).success;
|
|
2198
|
+
}
|
|
2199
|
+
isNullable() {
|
|
2200
|
+
return this.safeParse(null).success;
|
|
2201
|
+
}
|
|
2202
|
+
};
|
|
2203
|
+
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
2204
|
+
var uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
2205
|
+
var emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
|
|
2206
|
+
var ZodString = class extends ZodType {
|
|
2207
|
+
constructor() {
|
|
2208
|
+
super(...arguments);
|
|
2209
|
+
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
2210
|
+
validation,
|
|
2211
|
+
code: ZodIssueCode.invalid_string,
|
|
2212
|
+
...errorUtil.errToObj(message)
|
|
2213
|
+
});
|
|
2214
|
+
this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
|
|
2215
|
+
this.trim = () => new ZodString({
|
|
2216
|
+
...this._def,
|
|
2217
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
_parse(input) {
|
|
2221
|
+
const parsedType = this._getType(input);
|
|
2222
|
+
if (parsedType !== ZodParsedType.string) {
|
|
2223
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2224
|
+
addIssueToContext(
|
|
2225
|
+
ctx2,
|
|
2226
|
+
{
|
|
2227
|
+
code: ZodIssueCode.invalid_type,
|
|
2228
|
+
expected: ZodParsedType.string,
|
|
2229
|
+
received: ctx2.parsedType
|
|
2230
|
+
}
|
|
2231
|
+
);
|
|
2232
|
+
return INVALID;
|
|
2233
|
+
}
|
|
2234
|
+
const status = new ParseStatus();
|
|
2235
|
+
let ctx = void 0;
|
|
2236
|
+
for (const check of this._def.checks) {
|
|
2237
|
+
if (check.kind === "min") {
|
|
2238
|
+
if (input.data.length < check.value) {
|
|
2239
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2240
|
+
addIssueToContext(ctx, {
|
|
2241
|
+
code: ZodIssueCode.too_small,
|
|
2242
|
+
minimum: check.value,
|
|
2243
|
+
type: "string",
|
|
2244
|
+
inclusive: true,
|
|
2245
|
+
message: check.message
|
|
2246
|
+
});
|
|
2247
|
+
status.dirty();
|
|
2248
|
+
}
|
|
2249
|
+
} else if (check.kind === "max") {
|
|
2250
|
+
if (input.data.length > check.value) {
|
|
2251
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2252
|
+
addIssueToContext(ctx, {
|
|
2253
|
+
code: ZodIssueCode.too_big,
|
|
2254
|
+
maximum: check.value,
|
|
2255
|
+
type: "string",
|
|
2256
|
+
inclusive: true,
|
|
2257
|
+
message: check.message
|
|
2258
|
+
});
|
|
2259
|
+
status.dirty();
|
|
2260
|
+
}
|
|
2261
|
+
} else if (check.kind === "email") {
|
|
2262
|
+
if (!emailRegex.test(input.data)) {
|
|
2263
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2264
|
+
addIssueToContext(ctx, {
|
|
2265
|
+
validation: "email",
|
|
2266
|
+
code: ZodIssueCode.invalid_string,
|
|
2267
|
+
message: check.message
|
|
2268
|
+
});
|
|
2269
|
+
status.dirty();
|
|
2270
|
+
}
|
|
2271
|
+
} else if (check.kind === "uuid") {
|
|
2272
|
+
if (!uuidRegex.test(input.data)) {
|
|
2273
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2274
|
+
addIssueToContext(ctx, {
|
|
2275
|
+
validation: "uuid",
|
|
2276
|
+
code: ZodIssueCode.invalid_string,
|
|
2277
|
+
message: check.message
|
|
2278
|
+
});
|
|
2279
|
+
status.dirty();
|
|
2280
|
+
}
|
|
2281
|
+
} else if (check.kind === "cuid") {
|
|
2282
|
+
if (!cuidRegex.test(input.data)) {
|
|
2283
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2284
|
+
addIssueToContext(ctx, {
|
|
2285
|
+
validation: "cuid",
|
|
2286
|
+
code: ZodIssueCode.invalid_string,
|
|
2287
|
+
message: check.message
|
|
2288
|
+
});
|
|
2289
|
+
status.dirty();
|
|
2290
|
+
}
|
|
2291
|
+
} else if (check.kind === "url") {
|
|
2292
|
+
try {
|
|
2293
|
+
new URL(input.data);
|
|
2294
|
+
} catch (_a) {
|
|
2295
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2296
|
+
addIssueToContext(ctx, {
|
|
2297
|
+
validation: "url",
|
|
2298
|
+
code: ZodIssueCode.invalid_string,
|
|
2299
|
+
message: check.message
|
|
2300
|
+
});
|
|
2301
|
+
status.dirty();
|
|
2302
|
+
}
|
|
2303
|
+
} else if (check.kind === "regex") {
|
|
2304
|
+
check.regex.lastIndex = 0;
|
|
2305
|
+
const testResult = check.regex.test(input.data);
|
|
2306
|
+
if (!testResult) {
|
|
2307
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2308
|
+
addIssueToContext(ctx, {
|
|
2309
|
+
validation: "regex",
|
|
2310
|
+
code: ZodIssueCode.invalid_string,
|
|
2311
|
+
message: check.message
|
|
2312
|
+
});
|
|
2313
|
+
status.dirty();
|
|
2314
|
+
}
|
|
2315
|
+
} else if (check.kind === "trim") {
|
|
2316
|
+
input.data = input.data.trim();
|
|
2317
|
+
} else if (check.kind === "startsWith") {
|
|
2318
|
+
if (!input.data.startsWith(check.value)) {
|
|
2319
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2320
|
+
addIssueToContext(ctx, {
|
|
2321
|
+
code: ZodIssueCode.invalid_string,
|
|
2322
|
+
validation: { startsWith: check.value },
|
|
2323
|
+
message: check.message
|
|
2324
|
+
});
|
|
2325
|
+
status.dirty();
|
|
2326
|
+
}
|
|
2327
|
+
} else if (check.kind === "endsWith") {
|
|
2328
|
+
if (!input.data.endsWith(check.value)) {
|
|
2329
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2330
|
+
addIssueToContext(ctx, {
|
|
2331
|
+
code: ZodIssueCode.invalid_string,
|
|
2332
|
+
validation: { endsWith: check.value },
|
|
2333
|
+
message: check.message
|
|
2334
|
+
});
|
|
2335
|
+
status.dirty();
|
|
2336
|
+
}
|
|
2337
|
+
} else {
|
|
2338
|
+
util.assertNever(check);
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
return { status: status.value, value: input.data };
|
|
2342
|
+
}
|
|
2343
|
+
_addCheck(check) {
|
|
2344
|
+
return new ZodString({
|
|
2345
|
+
...this._def,
|
|
2346
|
+
checks: [...this._def.checks, check]
|
|
2347
|
+
});
|
|
2348
|
+
}
|
|
2349
|
+
email(message) {
|
|
2350
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
2351
|
+
}
|
|
2352
|
+
url(message) {
|
|
2353
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
2354
|
+
}
|
|
2355
|
+
uuid(message) {
|
|
2356
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
2357
|
+
}
|
|
2358
|
+
cuid(message) {
|
|
2359
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
2360
|
+
}
|
|
2361
|
+
regex(regex, message) {
|
|
2362
|
+
return this._addCheck({
|
|
2363
|
+
kind: "regex",
|
|
2364
|
+
regex,
|
|
2365
|
+
...errorUtil.errToObj(message)
|
|
2366
|
+
});
|
|
2367
|
+
}
|
|
2368
|
+
startsWith(value, message) {
|
|
2369
|
+
return this._addCheck({
|
|
2370
|
+
kind: "startsWith",
|
|
2371
|
+
value,
|
|
2372
|
+
...errorUtil.errToObj(message)
|
|
2373
|
+
});
|
|
2374
|
+
}
|
|
2375
|
+
endsWith(value, message) {
|
|
2376
|
+
return this._addCheck({
|
|
2377
|
+
kind: "endsWith",
|
|
2378
|
+
value,
|
|
2379
|
+
...errorUtil.errToObj(message)
|
|
2380
|
+
});
|
|
2381
|
+
}
|
|
2382
|
+
min(minLength, message) {
|
|
2383
|
+
return this._addCheck({
|
|
2384
|
+
kind: "min",
|
|
2385
|
+
value: minLength,
|
|
2386
|
+
...errorUtil.errToObj(message)
|
|
2387
|
+
});
|
|
2388
|
+
}
|
|
2389
|
+
max(maxLength, message) {
|
|
2390
|
+
return this._addCheck({
|
|
2391
|
+
kind: "max",
|
|
2392
|
+
value: maxLength,
|
|
2393
|
+
...errorUtil.errToObj(message)
|
|
2394
|
+
});
|
|
2395
|
+
}
|
|
2396
|
+
length(len, message) {
|
|
2397
|
+
return this.min(len, message).max(len, message);
|
|
2398
|
+
}
|
|
2399
|
+
get isEmail() {
|
|
2400
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
2401
|
+
}
|
|
2402
|
+
get isURL() {
|
|
2403
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
2404
|
+
}
|
|
2405
|
+
get isUUID() {
|
|
2406
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
2407
|
+
}
|
|
2408
|
+
get isCUID() {
|
|
2409
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
2410
|
+
}
|
|
2411
|
+
get minLength() {
|
|
2412
|
+
let min = null;
|
|
2413
|
+
for (const ch of this._def.checks) {
|
|
2414
|
+
if (ch.kind === "min") {
|
|
2415
|
+
if (min === null || ch.value > min)
|
|
2416
|
+
min = ch.value;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
return min;
|
|
2420
|
+
}
|
|
2421
|
+
get maxLength() {
|
|
2422
|
+
let max = null;
|
|
2423
|
+
for (const ch of this._def.checks) {
|
|
2424
|
+
if (ch.kind === "max") {
|
|
2425
|
+
if (max === null || ch.value < max)
|
|
2426
|
+
max = ch.value;
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
return max;
|
|
2430
|
+
}
|
|
2431
|
+
};
|
|
2432
|
+
ZodString.create = (params) => {
|
|
2433
|
+
return new ZodString({
|
|
2434
|
+
checks: [],
|
|
2435
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
2436
|
+
...processCreateParams(params)
|
|
2437
|
+
});
|
|
2438
|
+
};
|
|
2439
|
+
function floatSafeRemainder(val, step) {
|
|
2440
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
2441
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
2442
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
2443
|
+
const valInt = parseInt(val.toFixed(decCount).replace(".", ""));
|
|
2444
|
+
const stepInt = parseInt(step.toFixed(decCount).replace(".", ""));
|
|
2445
|
+
return valInt % stepInt / Math.pow(10, decCount);
|
|
2446
|
+
}
|
|
2447
|
+
var ZodNumber = class extends ZodType {
|
|
2448
|
+
constructor() {
|
|
2449
|
+
super(...arguments);
|
|
2450
|
+
this.min = this.gte;
|
|
2451
|
+
this.max = this.lte;
|
|
2452
|
+
this.step = this.multipleOf;
|
|
2453
|
+
}
|
|
2454
|
+
_parse(input) {
|
|
2455
|
+
const parsedType = this._getType(input);
|
|
2456
|
+
if (parsedType !== ZodParsedType.number) {
|
|
2457
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2458
|
+
addIssueToContext(ctx2, {
|
|
2459
|
+
code: ZodIssueCode.invalid_type,
|
|
2460
|
+
expected: ZodParsedType.number,
|
|
2461
|
+
received: ctx2.parsedType
|
|
2462
|
+
});
|
|
2463
|
+
return INVALID;
|
|
2464
|
+
}
|
|
2465
|
+
let ctx = void 0;
|
|
2466
|
+
const status = new ParseStatus();
|
|
2467
|
+
for (const check of this._def.checks) {
|
|
2468
|
+
if (check.kind === "int") {
|
|
2469
|
+
if (!util.isInteger(input.data)) {
|
|
2470
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2471
|
+
addIssueToContext(ctx, {
|
|
2472
|
+
code: ZodIssueCode.invalid_type,
|
|
2473
|
+
expected: "integer",
|
|
2474
|
+
received: "float",
|
|
2475
|
+
message: check.message
|
|
2476
|
+
});
|
|
2477
|
+
status.dirty();
|
|
2478
|
+
}
|
|
2479
|
+
} else if (check.kind === "min") {
|
|
2480
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2481
|
+
if (tooSmall) {
|
|
2482
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2483
|
+
addIssueToContext(ctx, {
|
|
2484
|
+
code: ZodIssueCode.too_small,
|
|
2485
|
+
minimum: check.value,
|
|
2486
|
+
type: "number",
|
|
2487
|
+
inclusive: check.inclusive,
|
|
2488
|
+
message: check.message
|
|
2489
|
+
});
|
|
2490
|
+
status.dirty();
|
|
2491
|
+
}
|
|
2492
|
+
} else if (check.kind === "max") {
|
|
2493
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2494
|
+
if (tooBig) {
|
|
2495
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2496
|
+
addIssueToContext(ctx, {
|
|
2497
|
+
code: ZodIssueCode.too_big,
|
|
2498
|
+
maximum: check.value,
|
|
2499
|
+
type: "number",
|
|
2500
|
+
inclusive: check.inclusive,
|
|
2501
|
+
message: check.message
|
|
2502
|
+
});
|
|
2503
|
+
status.dirty();
|
|
2504
|
+
}
|
|
2505
|
+
} else if (check.kind === "multipleOf") {
|
|
2506
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
2507
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2508
|
+
addIssueToContext(ctx, {
|
|
2509
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2510
|
+
multipleOf: check.value,
|
|
2511
|
+
message: check.message
|
|
2512
|
+
});
|
|
2513
|
+
status.dirty();
|
|
2514
|
+
}
|
|
2515
|
+
} else {
|
|
2516
|
+
util.assertNever(check);
|
|
2517
|
+
}
|
|
2518
|
+
}
|
|
2519
|
+
return { status: status.value, value: input.data };
|
|
2520
|
+
}
|
|
2521
|
+
gte(value, message) {
|
|
2522
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2523
|
+
}
|
|
2524
|
+
gt(value, message) {
|
|
2525
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2526
|
+
}
|
|
2527
|
+
lte(value, message) {
|
|
2528
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2529
|
+
}
|
|
2530
|
+
lt(value, message) {
|
|
2531
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2532
|
+
}
|
|
2533
|
+
setLimit(kind, value, inclusive, message) {
|
|
2534
|
+
return new ZodNumber({
|
|
2535
|
+
...this._def,
|
|
2536
|
+
checks: [
|
|
2537
|
+
...this._def.checks,
|
|
2538
|
+
{
|
|
2539
|
+
kind,
|
|
2540
|
+
value,
|
|
2541
|
+
inclusive,
|
|
2542
|
+
message: errorUtil.toString(message)
|
|
2543
|
+
}
|
|
2544
|
+
]
|
|
2545
|
+
});
|
|
2546
|
+
}
|
|
2547
|
+
_addCheck(check) {
|
|
2548
|
+
return new ZodNumber({
|
|
2549
|
+
...this._def,
|
|
2550
|
+
checks: [...this._def.checks, check]
|
|
2551
|
+
});
|
|
2552
|
+
}
|
|
2553
|
+
int(message) {
|
|
2554
|
+
return this._addCheck({
|
|
2555
|
+
kind: "int",
|
|
2556
|
+
message: errorUtil.toString(message)
|
|
2557
|
+
});
|
|
2558
|
+
}
|
|
2559
|
+
positive(message) {
|
|
2560
|
+
return this._addCheck({
|
|
2561
|
+
kind: "min",
|
|
2562
|
+
value: 0,
|
|
2563
|
+
inclusive: false,
|
|
2564
|
+
message: errorUtil.toString(message)
|
|
2565
|
+
});
|
|
2566
|
+
}
|
|
2567
|
+
negative(message) {
|
|
2568
|
+
return this._addCheck({
|
|
2569
|
+
kind: "max",
|
|
2570
|
+
value: 0,
|
|
2571
|
+
inclusive: false,
|
|
2572
|
+
message: errorUtil.toString(message)
|
|
2573
|
+
});
|
|
2574
|
+
}
|
|
2575
|
+
nonpositive(message) {
|
|
2576
|
+
return this._addCheck({
|
|
2577
|
+
kind: "max",
|
|
2578
|
+
value: 0,
|
|
2579
|
+
inclusive: true,
|
|
2580
|
+
message: errorUtil.toString(message)
|
|
2581
|
+
});
|
|
2582
|
+
}
|
|
2583
|
+
nonnegative(message) {
|
|
2584
|
+
return this._addCheck({
|
|
2585
|
+
kind: "min",
|
|
2586
|
+
value: 0,
|
|
2587
|
+
inclusive: true,
|
|
2588
|
+
message: errorUtil.toString(message)
|
|
2589
|
+
});
|
|
2590
|
+
}
|
|
2591
|
+
multipleOf(value, message) {
|
|
2592
|
+
return this._addCheck({
|
|
2593
|
+
kind: "multipleOf",
|
|
2594
|
+
value,
|
|
2595
|
+
message: errorUtil.toString(message)
|
|
2596
|
+
});
|
|
2597
|
+
}
|
|
2598
|
+
get minValue() {
|
|
2599
|
+
let min = null;
|
|
2600
|
+
for (const ch of this._def.checks) {
|
|
2601
|
+
if (ch.kind === "min") {
|
|
2602
|
+
if (min === null || ch.value > min)
|
|
2603
|
+
min = ch.value;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
return min;
|
|
2607
|
+
}
|
|
2608
|
+
get maxValue() {
|
|
2609
|
+
let max = null;
|
|
2610
|
+
for (const ch of this._def.checks) {
|
|
2611
|
+
if (ch.kind === "max") {
|
|
2612
|
+
if (max === null || ch.value < max)
|
|
2613
|
+
max = ch.value;
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
return max;
|
|
2617
|
+
}
|
|
2618
|
+
get isInt() {
|
|
2619
|
+
return !!this._def.checks.find((ch) => ch.kind === "int");
|
|
2620
|
+
}
|
|
2621
|
+
};
|
|
2622
|
+
ZodNumber.create = (params) => {
|
|
2623
|
+
return new ZodNumber({
|
|
2624
|
+
checks: [],
|
|
2625
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
2626
|
+
...processCreateParams(params)
|
|
2627
|
+
});
|
|
2628
|
+
};
|
|
2629
|
+
var ZodBigInt = class extends ZodType {
|
|
2630
|
+
_parse(input) {
|
|
2631
|
+
const parsedType = this._getType(input);
|
|
2632
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
2633
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2634
|
+
addIssueToContext(ctx, {
|
|
2635
|
+
code: ZodIssueCode.invalid_type,
|
|
2636
|
+
expected: ZodParsedType.bigint,
|
|
2637
|
+
received: ctx.parsedType
|
|
2638
|
+
});
|
|
2639
|
+
return INVALID;
|
|
2640
|
+
}
|
|
2641
|
+
return OK(input.data);
|
|
2642
|
+
}
|
|
2643
|
+
};
|
|
2644
|
+
ZodBigInt.create = (params) => {
|
|
2645
|
+
return new ZodBigInt({
|
|
2646
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
2647
|
+
...processCreateParams(params)
|
|
2648
|
+
});
|
|
2649
|
+
};
|
|
2650
|
+
var ZodBoolean = class extends ZodType {
|
|
2651
|
+
_parse(input) {
|
|
2652
|
+
const parsedType = this._getType(input);
|
|
2653
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
2654
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2655
|
+
addIssueToContext(ctx, {
|
|
2656
|
+
code: ZodIssueCode.invalid_type,
|
|
2657
|
+
expected: ZodParsedType.boolean,
|
|
2658
|
+
received: ctx.parsedType
|
|
2659
|
+
});
|
|
2660
|
+
return INVALID;
|
|
2661
|
+
}
|
|
2662
|
+
return OK(input.data);
|
|
2663
|
+
}
|
|
2664
|
+
};
|
|
2665
|
+
ZodBoolean.create = (params) => {
|
|
2666
|
+
return new ZodBoolean({
|
|
2667
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2668
|
+
...processCreateParams(params)
|
|
2669
|
+
});
|
|
2670
|
+
};
|
|
2671
|
+
var ZodDate = class extends ZodType {
|
|
2672
|
+
_parse(input) {
|
|
2673
|
+
const parsedType = this._getType(input);
|
|
2674
|
+
if (parsedType !== ZodParsedType.date) {
|
|
2675
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2676
|
+
addIssueToContext(ctx2, {
|
|
2677
|
+
code: ZodIssueCode.invalid_type,
|
|
2678
|
+
expected: ZodParsedType.date,
|
|
2679
|
+
received: ctx2.parsedType
|
|
2680
|
+
});
|
|
2681
|
+
return INVALID;
|
|
2682
|
+
}
|
|
2683
|
+
if (isNaN(input.data.getTime())) {
|
|
2684
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2685
|
+
addIssueToContext(ctx2, {
|
|
2686
|
+
code: ZodIssueCode.invalid_date
|
|
2687
|
+
});
|
|
2688
|
+
return INVALID;
|
|
2689
|
+
}
|
|
2690
|
+
const status = new ParseStatus();
|
|
2691
|
+
let ctx = void 0;
|
|
2692
|
+
for (const check of this._def.checks) {
|
|
2693
|
+
if (check.kind === "min") {
|
|
2694
|
+
if (input.data.getTime() < check.value) {
|
|
2695
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2696
|
+
addIssueToContext(ctx, {
|
|
2697
|
+
code: ZodIssueCode.too_small,
|
|
2698
|
+
message: check.message,
|
|
2699
|
+
inclusive: true,
|
|
2700
|
+
minimum: check.value,
|
|
2701
|
+
type: "date"
|
|
2702
|
+
});
|
|
2703
|
+
status.dirty();
|
|
2704
|
+
}
|
|
2705
|
+
} else if (check.kind === "max") {
|
|
2706
|
+
if (input.data.getTime() > check.value) {
|
|
2707
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2708
|
+
addIssueToContext(ctx, {
|
|
2709
|
+
code: ZodIssueCode.too_big,
|
|
2710
|
+
message: check.message,
|
|
2711
|
+
inclusive: true,
|
|
2712
|
+
maximum: check.value,
|
|
2713
|
+
type: "date"
|
|
2714
|
+
});
|
|
2715
|
+
status.dirty();
|
|
2716
|
+
}
|
|
2717
|
+
} else {
|
|
2718
|
+
util.assertNever(check);
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
return {
|
|
2722
|
+
status: status.value,
|
|
2723
|
+
value: new Date(input.data.getTime())
|
|
2724
|
+
};
|
|
2725
|
+
}
|
|
2726
|
+
_addCheck(check) {
|
|
2727
|
+
return new ZodDate({
|
|
2728
|
+
...this._def,
|
|
2729
|
+
checks: [...this._def.checks, check]
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
min(minDate, message) {
|
|
2733
|
+
return this._addCheck({
|
|
2734
|
+
kind: "min",
|
|
2735
|
+
value: minDate.getTime(),
|
|
2736
|
+
message: errorUtil.toString(message)
|
|
2737
|
+
});
|
|
2738
|
+
}
|
|
2739
|
+
max(maxDate, message) {
|
|
2740
|
+
return this._addCheck({
|
|
2741
|
+
kind: "max",
|
|
2742
|
+
value: maxDate.getTime(),
|
|
2743
|
+
message: errorUtil.toString(message)
|
|
2744
|
+
});
|
|
2745
|
+
}
|
|
2746
|
+
get minDate() {
|
|
2747
|
+
let min = null;
|
|
2748
|
+
for (const ch of this._def.checks) {
|
|
2749
|
+
if (ch.kind === "min") {
|
|
2750
|
+
if (min === null || ch.value > min)
|
|
2751
|
+
min = ch.value;
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
return min != null ? new Date(min) : null;
|
|
2755
|
+
}
|
|
2756
|
+
get maxDate() {
|
|
2757
|
+
let max = null;
|
|
2758
|
+
for (const ch of this._def.checks) {
|
|
2759
|
+
if (ch.kind === "max") {
|
|
2760
|
+
if (max === null || ch.value < max)
|
|
2761
|
+
max = ch.value;
|
|
2762
|
+
}
|
|
2763
|
+
}
|
|
2764
|
+
return max != null ? new Date(max) : null;
|
|
2765
|
+
}
|
|
2766
|
+
};
|
|
2767
|
+
ZodDate.create = (params) => {
|
|
2768
|
+
return new ZodDate({
|
|
2769
|
+
checks: [],
|
|
2770
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2771
|
+
...processCreateParams(params)
|
|
2772
|
+
});
|
|
2773
|
+
};
|
|
2774
|
+
var ZodUndefined = class extends ZodType {
|
|
2775
|
+
_parse(input) {
|
|
2776
|
+
const parsedType = this._getType(input);
|
|
2777
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2778
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2779
|
+
addIssueToContext(ctx, {
|
|
2780
|
+
code: ZodIssueCode.invalid_type,
|
|
2781
|
+
expected: ZodParsedType.undefined,
|
|
2782
|
+
received: ctx.parsedType
|
|
2783
|
+
});
|
|
2784
|
+
return INVALID;
|
|
2785
|
+
}
|
|
2786
|
+
return OK(input.data);
|
|
2787
|
+
}
|
|
2788
|
+
};
|
|
2789
|
+
ZodUndefined.create = (params) => {
|
|
2790
|
+
return new ZodUndefined({
|
|
2791
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2792
|
+
...processCreateParams(params)
|
|
2793
|
+
});
|
|
2794
|
+
};
|
|
2795
|
+
var ZodNull = class extends ZodType {
|
|
2796
|
+
_parse(input) {
|
|
2797
|
+
const parsedType = this._getType(input);
|
|
2798
|
+
if (parsedType !== ZodParsedType.null) {
|
|
2799
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2800
|
+
addIssueToContext(ctx, {
|
|
2801
|
+
code: ZodIssueCode.invalid_type,
|
|
2802
|
+
expected: ZodParsedType.null,
|
|
2803
|
+
received: ctx.parsedType
|
|
2804
|
+
});
|
|
2805
|
+
return INVALID;
|
|
2806
|
+
}
|
|
2807
|
+
return OK(input.data);
|
|
2808
|
+
}
|
|
2809
|
+
};
|
|
2810
|
+
ZodNull.create = (params) => {
|
|
2811
|
+
return new ZodNull({
|
|
2812
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2813
|
+
...processCreateParams(params)
|
|
2814
|
+
});
|
|
2815
|
+
};
|
|
2816
|
+
var ZodAny = class extends ZodType {
|
|
2817
|
+
constructor() {
|
|
2818
|
+
super(...arguments);
|
|
2819
|
+
this._any = true;
|
|
2820
|
+
}
|
|
2821
|
+
_parse(input) {
|
|
2822
|
+
return OK(input.data);
|
|
2823
|
+
}
|
|
2824
|
+
};
|
|
2825
|
+
ZodAny.create = (params) => {
|
|
2826
|
+
return new ZodAny({
|
|
2827
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2828
|
+
...processCreateParams(params)
|
|
2829
|
+
});
|
|
2830
|
+
};
|
|
2831
|
+
var ZodUnknown = class extends ZodType {
|
|
2832
|
+
constructor() {
|
|
2833
|
+
super(...arguments);
|
|
2834
|
+
this._unknown = true;
|
|
2835
|
+
}
|
|
2836
|
+
_parse(input) {
|
|
2837
|
+
return OK(input.data);
|
|
2838
|
+
}
|
|
2839
|
+
};
|
|
2840
|
+
ZodUnknown.create = (params) => {
|
|
2841
|
+
return new ZodUnknown({
|
|
2842
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2843
|
+
...processCreateParams(params)
|
|
2844
|
+
});
|
|
2845
|
+
};
|
|
2846
|
+
var ZodNever = class extends ZodType {
|
|
2847
|
+
_parse(input) {
|
|
2848
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2849
|
+
addIssueToContext(ctx, {
|
|
2850
|
+
code: ZodIssueCode.invalid_type,
|
|
2851
|
+
expected: ZodParsedType.never,
|
|
2852
|
+
received: ctx.parsedType
|
|
2853
|
+
});
|
|
2854
|
+
return INVALID;
|
|
2855
|
+
}
|
|
2856
|
+
};
|
|
2857
|
+
ZodNever.create = (params) => {
|
|
2858
|
+
return new ZodNever({
|
|
2859
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2860
|
+
...processCreateParams(params)
|
|
2861
|
+
});
|
|
2862
|
+
};
|
|
2863
|
+
var ZodVoid = class extends ZodType {
|
|
2864
|
+
_parse(input) {
|
|
2865
|
+
const parsedType = this._getType(input);
|
|
2866
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2867
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2868
|
+
addIssueToContext(ctx, {
|
|
2869
|
+
code: ZodIssueCode.invalid_type,
|
|
2870
|
+
expected: ZodParsedType.void,
|
|
2871
|
+
received: ctx.parsedType
|
|
2872
|
+
});
|
|
2873
|
+
return INVALID;
|
|
2874
|
+
}
|
|
2875
|
+
return OK(input.data);
|
|
2876
|
+
}
|
|
2877
|
+
};
|
|
2878
|
+
ZodVoid.create = (params) => {
|
|
2879
|
+
return new ZodVoid({
|
|
2880
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2881
|
+
...processCreateParams(params)
|
|
2882
|
+
});
|
|
2883
|
+
};
|
|
2884
|
+
var ZodArray = class extends ZodType {
|
|
2885
|
+
_parse(input) {
|
|
2886
|
+
const { ctx, status } = this._processInputParams(input);
|
|
2887
|
+
const def2 = this._def;
|
|
2888
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2889
|
+
addIssueToContext(ctx, {
|
|
2890
|
+
code: ZodIssueCode.invalid_type,
|
|
2891
|
+
expected: ZodParsedType.array,
|
|
2892
|
+
received: ctx.parsedType
|
|
2893
|
+
});
|
|
2894
|
+
return INVALID;
|
|
2895
|
+
}
|
|
2896
|
+
if (def2.minLength !== null) {
|
|
2897
|
+
if (ctx.data.length < def2.minLength.value) {
|
|
2898
|
+
addIssueToContext(ctx, {
|
|
2899
|
+
code: ZodIssueCode.too_small,
|
|
2900
|
+
minimum: def2.minLength.value,
|
|
2901
|
+
type: "array",
|
|
2902
|
+
inclusive: true,
|
|
2903
|
+
message: def2.minLength.message
|
|
2904
|
+
});
|
|
2905
|
+
status.dirty();
|
|
2906
|
+
}
|
|
2907
|
+
}
|
|
2908
|
+
if (def2.maxLength !== null) {
|
|
2909
|
+
if (ctx.data.length > def2.maxLength.value) {
|
|
2910
|
+
addIssueToContext(ctx, {
|
|
2911
|
+
code: ZodIssueCode.too_big,
|
|
2912
|
+
maximum: def2.maxLength.value,
|
|
2913
|
+
type: "array",
|
|
2914
|
+
inclusive: true,
|
|
2915
|
+
message: def2.maxLength.message
|
|
2916
|
+
});
|
|
2917
|
+
status.dirty();
|
|
2918
|
+
}
|
|
2919
|
+
}
|
|
2920
|
+
if (ctx.common.async) {
|
|
2921
|
+
return Promise.all(ctx.data.map((item, i) => {
|
|
2922
|
+
return def2.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2923
|
+
})).then((result2) => {
|
|
2924
|
+
return ParseStatus.mergeArray(status, result2);
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
const result = ctx.data.map((item, i) => {
|
|
2928
|
+
return def2.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2929
|
+
});
|
|
2930
|
+
return ParseStatus.mergeArray(status, result);
|
|
2931
|
+
}
|
|
2932
|
+
get element() {
|
|
2933
|
+
return this._def.type;
|
|
2934
|
+
}
|
|
2935
|
+
min(minLength, message) {
|
|
2936
|
+
return new ZodArray({
|
|
2937
|
+
...this._def,
|
|
2938
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
2939
|
+
});
|
|
2940
|
+
}
|
|
2941
|
+
max(maxLength, message) {
|
|
2942
|
+
return new ZodArray({
|
|
2943
|
+
...this._def,
|
|
2944
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
2945
|
+
});
|
|
2946
|
+
}
|
|
2947
|
+
length(len, message) {
|
|
2948
|
+
return this.min(len, message).max(len, message);
|
|
2949
|
+
}
|
|
2950
|
+
nonempty(message) {
|
|
2951
|
+
return this.min(1, message);
|
|
2952
|
+
}
|
|
2953
|
+
};
|
|
2954
|
+
ZodArray.create = (schema2, params) => {
|
|
2955
|
+
return new ZodArray({
|
|
2956
|
+
type: schema2,
|
|
2957
|
+
minLength: null,
|
|
2958
|
+
maxLength: null,
|
|
2959
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
2960
|
+
...processCreateParams(params)
|
|
2961
|
+
});
|
|
2962
|
+
};
|
|
2963
|
+
var objectUtil;
|
|
2964
|
+
(function(objectUtil2) {
|
|
2965
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
2966
|
+
return {
|
|
2967
|
+
...first,
|
|
2968
|
+
...second
|
|
2969
|
+
};
|
|
2970
|
+
};
|
|
2971
|
+
})(objectUtil || (objectUtil = {}));
|
|
2972
|
+
var AugmentFactory = (def2) => (augmentation) => {
|
|
2973
|
+
return new ZodObject({
|
|
2974
|
+
...def2,
|
|
2975
|
+
shape: () => ({
|
|
2976
|
+
...def2.shape(),
|
|
2977
|
+
...augmentation
|
|
2978
|
+
})
|
|
2979
|
+
});
|
|
2980
|
+
};
|
|
2981
|
+
function deepPartialify(schema2) {
|
|
2982
|
+
if (schema2 instanceof ZodObject) {
|
|
2983
|
+
const newShape = {};
|
|
2984
|
+
for (const key in schema2.shape) {
|
|
2985
|
+
const fieldSchema = schema2.shape[key];
|
|
2986
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
2987
|
+
}
|
|
2988
|
+
return new ZodObject({
|
|
2989
|
+
...schema2._def,
|
|
2990
|
+
shape: () => newShape
|
|
2991
|
+
});
|
|
2992
|
+
} else if (schema2 instanceof ZodArray) {
|
|
2993
|
+
return ZodArray.create(deepPartialify(schema2.element));
|
|
2994
|
+
} else if (schema2 instanceof ZodOptional) {
|
|
2995
|
+
return ZodOptional.create(deepPartialify(schema2.unwrap()));
|
|
2996
|
+
} else if (schema2 instanceof ZodNullable) {
|
|
2997
|
+
return ZodNullable.create(deepPartialify(schema2.unwrap()));
|
|
2998
|
+
} else if (schema2 instanceof ZodTuple) {
|
|
2999
|
+
return ZodTuple.create(schema2.items.map((item) => deepPartialify(item)));
|
|
3000
|
+
} else {
|
|
3001
|
+
return schema2;
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
var ZodObject = class extends ZodType {
|
|
3005
|
+
constructor() {
|
|
3006
|
+
super(...arguments);
|
|
3007
|
+
this._cached = null;
|
|
3008
|
+
this.nonstrict = this.passthrough;
|
|
3009
|
+
this.augment = AugmentFactory(this._def);
|
|
3010
|
+
this.extend = AugmentFactory(this._def);
|
|
3011
|
+
}
|
|
3012
|
+
_getCached() {
|
|
3013
|
+
if (this._cached !== null)
|
|
3014
|
+
return this._cached;
|
|
3015
|
+
const shape = this._def.shape();
|
|
3016
|
+
const keys = util.objectKeys(shape);
|
|
3017
|
+
return this._cached = { shape, keys };
|
|
3018
|
+
}
|
|
3019
|
+
_parse(input) {
|
|
3020
|
+
const parsedType = this._getType(input);
|
|
3021
|
+
if (parsedType !== ZodParsedType.object) {
|
|
3022
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
3023
|
+
addIssueToContext(ctx2, {
|
|
3024
|
+
code: ZodIssueCode.invalid_type,
|
|
3025
|
+
expected: ZodParsedType.object,
|
|
3026
|
+
received: ctx2.parsedType
|
|
3027
|
+
});
|
|
3028
|
+
return INVALID;
|
|
3029
|
+
}
|
|
3030
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3031
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
3032
|
+
const extraKeys = [];
|
|
3033
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
3034
|
+
for (const key in ctx.data) {
|
|
3035
|
+
if (!shapeKeys.includes(key)) {
|
|
3036
|
+
extraKeys.push(key);
|
|
3037
|
+
}
|
|
3038
|
+
}
|
|
3039
|
+
}
|
|
3040
|
+
const pairs = [];
|
|
3041
|
+
for (const key of shapeKeys) {
|
|
3042
|
+
const keyValidator = shape[key];
|
|
3043
|
+
const value = ctx.data[key];
|
|
3044
|
+
pairs.push({
|
|
3045
|
+
key: { status: "valid", value: key },
|
|
3046
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
3047
|
+
alwaysSet: key in ctx.data
|
|
3048
|
+
});
|
|
3049
|
+
}
|
|
3050
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
3051
|
+
const unknownKeys = this._def.unknownKeys;
|
|
3052
|
+
if (unknownKeys === "passthrough") {
|
|
3053
|
+
for (const key of extraKeys) {
|
|
3054
|
+
pairs.push({
|
|
3055
|
+
key: { status: "valid", value: key },
|
|
3056
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
3057
|
+
});
|
|
3058
|
+
}
|
|
3059
|
+
} else if (unknownKeys === "strict") {
|
|
3060
|
+
if (extraKeys.length > 0) {
|
|
3061
|
+
addIssueToContext(ctx, {
|
|
3062
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
3063
|
+
keys: extraKeys
|
|
3064
|
+
});
|
|
3065
|
+
status.dirty();
|
|
3066
|
+
}
|
|
3067
|
+
} else if (unknownKeys === "strip")
|
|
3068
|
+
;
|
|
3069
|
+
else {
|
|
3070
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
3071
|
+
}
|
|
3072
|
+
} else {
|
|
3073
|
+
const catchall = this._def.catchall;
|
|
3074
|
+
for (const key of extraKeys) {
|
|
3075
|
+
const value = ctx.data[key];
|
|
3076
|
+
pairs.push({
|
|
3077
|
+
key: { status: "valid", value: key },
|
|
3078
|
+
value: catchall._parse(
|
|
3079
|
+
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
3080
|
+
),
|
|
3081
|
+
alwaysSet: key in ctx.data
|
|
3082
|
+
});
|
|
3083
|
+
}
|
|
3084
|
+
}
|
|
3085
|
+
if (ctx.common.async) {
|
|
3086
|
+
return Promise.resolve().then(async () => {
|
|
3087
|
+
const syncPairs = [];
|
|
3088
|
+
for (const pair of pairs) {
|
|
3089
|
+
const key = await pair.key;
|
|
3090
|
+
syncPairs.push({
|
|
3091
|
+
key,
|
|
3092
|
+
value: await pair.value,
|
|
3093
|
+
alwaysSet: pair.alwaysSet
|
|
3094
|
+
});
|
|
3095
|
+
}
|
|
3096
|
+
return syncPairs;
|
|
3097
|
+
}).then((syncPairs) => {
|
|
3098
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
3099
|
+
});
|
|
3100
|
+
} else {
|
|
3101
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
get shape() {
|
|
3105
|
+
return this._def.shape();
|
|
3106
|
+
}
|
|
3107
|
+
strict(message) {
|
|
3108
|
+
errorUtil.errToObj;
|
|
3109
|
+
return new ZodObject({
|
|
3110
|
+
...this._def,
|
|
3111
|
+
unknownKeys: "strict",
|
|
3112
|
+
...message !== void 0 ? {
|
|
3113
|
+
errorMap: (issue, ctx) => {
|
|
3114
|
+
var _a, _b, _c, _d;
|
|
3115
|
+
const defaultError = (_c = (_b = (_a = this._def).errorMap) === null || _b === void 0 ? void 0 : _b.call(_a, issue, ctx).message) !== null && _c !== void 0 ? _c : ctx.defaultError;
|
|
3116
|
+
if (issue.code === "unrecognized_keys")
|
|
3117
|
+
return {
|
|
3118
|
+
message: (_d = errorUtil.errToObj(message).message) !== null && _d !== void 0 ? _d : defaultError
|
|
3119
|
+
};
|
|
3120
|
+
return {
|
|
3121
|
+
message: defaultError
|
|
3122
|
+
};
|
|
3123
|
+
}
|
|
3124
|
+
} : {}
|
|
3125
|
+
});
|
|
3126
|
+
}
|
|
3127
|
+
strip() {
|
|
3128
|
+
return new ZodObject({
|
|
3129
|
+
...this._def,
|
|
3130
|
+
unknownKeys: "strip"
|
|
3131
|
+
});
|
|
3132
|
+
}
|
|
3133
|
+
passthrough() {
|
|
3134
|
+
return new ZodObject({
|
|
3135
|
+
...this._def,
|
|
3136
|
+
unknownKeys: "passthrough"
|
|
3137
|
+
});
|
|
3138
|
+
}
|
|
3139
|
+
setKey(key, schema2) {
|
|
3140
|
+
return this.augment({ [key]: schema2 });
|
|
3141
|
+
}
|
|
3142
|
+
merge(merging) {
|
|
3143
|
+
const merged = new ZodObject({
|
|
3144
|
+
unknownKeys: merging._def.unknownKeys,
|
|
3145
|
+
catchall: merging._def.catchall,
|
|
3146
|
+
shape: () => objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3147
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
3148
|
+
});
|
|
3149
|
+
return merged;
|
|
3150
|
+
}
|
|
3151
|
+
catchall(index) {
|
|
3152
|
+
return new ZodObject({
|
|
3153
|
+
...this._def,
|
|
3154
|
+
catchall: index
|
|
3155
|
+
});
|
|
3156
|
+
}
|
|
3157
|
+
pick(mask) {
|
|
3158
|
+
const shape = {};
|
|
3159
|
+
util.objectKeys(mask).map((key) => {
|
|
3160
|
+
if (this.shape[key])
|
|
3161
|
+
shape[key] = this.shape[key];
|
|
3162
|
+
});
|
|
3163
|
+
return new ZodObject({
|
|
3164
|
+
...this._def,
|
|
3165
|
+
shape: () => shape
|
|
3166
|
+
});
|
|
3167
|
+
}
|
|
3168
|
+
omit(mask) {
|
|
3169
|
+
const shape = {};
|
|
3170
|
+
util.objectKeys(this.shape).map((key) => {
|
|
3171
|
+
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
3172
|
+
shape[key] = this.shape[key];
|
|
3173
|
+
}
|
|
3174
|
+
});
|
|
3175
|
+
return new ZodObject({
|
|
3176
|
+
...this._def,
|
|
3177
|
+
shape: () => shape
|
|
3178
|
+
});
|
|
3179
|
+
}
|
|
3180
|
+
deepPartial() {
|
|
3181
|
+
return deepPartialify(this);
|
|
3182
|
+
}
|
|
3183
|
+
partial(mask) {
|
|
3184
|
+
const newShape = {};
|
|
3185
|
+
if (mask) {
|
|
3186
|
+
util.objectKeys(this.shape).map((key) => {
|
|
3187
|
+
if (util.objectKeys(mask).indexOf(key) === -1) {
|
|
3188
|
+
newShape[key] = this.shape[key];
|
|
3189
|
+
} else {
|
|
3190
|
+
newShape[key] = this.shape[key].optional();
|
|
3191
|
+
}
|
|
3192
|
+
});
|
|
3193
|
+
return new ZodObject({
|
|
3194
|
+
...this._def,
|
|
3195
|
+
shape: () => newShape
|
|
3196
|
+
});
|
|
3197
|
+
} else {
|
|
3198
|
+
for (const key in this.shape) {
|
|
3199
|
+
const fieldSchema = this.shape[key];
|
|
3200
|
+
newShape[key] = fieldSchema.optional();
|
|
3201
|
+
}
|
|
3202
|
+
}
|
|
3203
|
+
return new ZodObject({
|
|
3204
|
+
...this._def,
|
|
3205
|
+
shape: () => newShape
|
|
3206
|
+
});
|
|
3207
|
+
}
|
|
3208
|
+
required() {
|
|
3209
|
+
const newShape = {};
|
|
3210
|
+
for (const key in this.shape) {
|
|
3211
|
+
const fieldSchema = this.shape[key];
|
|
3212
|
+
let newField = fieldSchema;
|
|
3213
|
+
while (newField instanceof ZodOptional) {
|
|
3214
|
+
newField = newField._def.innerType;
|
|
3215
|
+
}
|
|
3216
|
+
newShape[key] = newField;
|
|
3217
|
+
}
|
|
3218
|
+
return new ZodObject({
|
|
3219
|
+
...this._def,
|
|
3220
|
+
shape: () => newShape
|
|
3221
|
+
});
|
|
3222
|
+
}
|
|
3223
|
+
keyof() {
|
|
3224
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
3225
|
+
}
|
|
3226
|
+
};
|
|
3227
|
+
ZodObject.create = (shape, params) => {
|
|
3228
|
+
return new ZodObject({
|
|
3229
|
+
shape: () => shape,
|
|
3230
|
+
unknownKeys: "strip",
|
|
3231
|
+
catchall: ZodNever.create(),
|
|
3232
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3233
|
+
...processCreateParams(params)
|
|
3234
|
+
});
|
|
3235
|
+
};
|
|
3236
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
3237
|
+
return new ZodObject({
|
|
3238
|
+
shape: () => shape,
|
|
3239
|
+
unknownKeys: "strict",
|
|
3240
|
+
catchall: ZodNever.create(),
|
|
3241
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3242
|
+
...processCreateParams(params)
|
|
3243
|
+
});
|
|
3244
|
+
};
|
|
3245
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
3246
|
+
return new ZodObject({
|
|
3247
|
+
shape,
|
|
3248
|
+
unknownKeys: "strip",
|
|
3249
|
+
catchall: ZodNever.create(),
|
|
3250
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3251
|
+
...processCreateParams(params)
|
|
3252
|
+
});
|
|
3253
|
+
};
|
|
3254
|
+
var ZodUnion = class extends ZodType {
|
|
3255
|
+
_parse(input) {
|
|
3256
|
+
const { ctx } = this._processInputParams(input);
|
|
3257
|
+
const options = this._def.options;
|
|
3258
|
+
function handleResults(results) {
|
|
3259
|
+
for (const result of results) {
|
|
3260
|
+
if (result.result.status === "valid") {
|
|
3261
|
+
return result.result;
|
|
3262
|
+
}
|
|
3263
|
+
}
|
|
3264
|
+
for (const result of results) {
|
|
3265
|
+
if (result.result.status === "dirty") {
|
|
3266
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
3267
|
+
return result.result;
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
3271
|
+
addIssueToContext(ctx, {
|
|
3272
|
+
code: ZodIssueCode.invalid_union,
|
|
3273
|
+
unionErrors
|
|
3274
|
+
});
|
|
3275
|
+
return INVALID;
|
|
3276
|
+
}
|
|
3277
|
+
if (ctx.common.async) {
|
|
3278
|
+
return Promise.all(options.map(async (option) => {
|
|
3279
|
+
const childCtx = {
|
|
3280
|
+
...ctx,
|
|
3281
|
+
common: {
|
|
3282
|
+
...ctx.common,
|
|
3283
|
+
issues: []
|
|
3284
|
+
},
|
|
3285
|
+
parent: null
|
|
3286
|
+
};
|
|
3287
|
+
return {
|
|
3288
|
+
result: await option._parseAsync({
|
|
3289
|
+
data: ctx.data,
|
|
3290
|
+
path: ctx.path,
|
|
3291
|
+
parent: childCtx
|
|
3292
|
+
}),
|
|
3293
|
+
ctx: childCtx
|
|
3294
|
+
};
|
|
3295
|
+
})).then(handleResults);
|
|
3296
|
+
} else {
|
|
3297
|
+
let dirty = void 0;
|
|
3298
|
+
const issues = [];
|
|
3299
|
+
for (const option of options) {
|
|
3300
|
+
const childCtx = {
|
|
3301
|
+
...ctx,
|
|
3302
|
+
common: {
|
|
3303
|
+
...ctx.common,
|
|
3304
|
+
issues: []
|
|
3305
|
+
},
|
|
3306
|
+
parent: null
|
|
3307
|
+
};
|
|
3308
|
+
const result = option._parseSync({
|
|
3309
|
+
data: ctx.data,
|
|
3310
|
+
path: ctx.path,
|
|
3311
|
+
parent: childCtx
|
|
3312
|
+
});
|
|
3313
|
+
if (result.status === "valid") {
|
|
3314
|
+
return result;
|
|
3315
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
3316
|
+
dirty = { result, ctx: childCtx };
|
|
3317
|
+
}
|
|
3318
|
+
if (childCtx.common.issues.length) {
|
|
3319
|
+
issues.push(childCtx.common.issues);
|
|
3320
|
+
}
|
|
3321
|
+
}
|
|
3322
|
+
if (dirty) {
|
|
3323
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
3324
|
+
return dirty.result;
|
|
3325
|
+
}
|
|
3326
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
3327
|
+
addIssueToContext(ctx, {
|
|
3328
|
+
code: ZodIssueCode.invalid_union,
|
|
3329
|
+
unionErrors
|
|
3330
|
+
});
|
|
3331
|
+
return INVALID;
|
|
3332
|
+
}
|
|
3333
|
+
}
|
|
3334
|
+
get options() {
|
|
3335
|
+
return this._def.options;
|
|
3336
|
+
}
|
|
3337
|
+
};
|
|
3338
|
+
ZodUnion.create = (types, params) => {
|
|
3339
|
+
return new ZodUnion({
|
|
3340
|
+
options: types,
|
|
3341
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
3342
|
+
...processCreateParams(params)
|
|
3343
|
+
});
|
|
3344
|
+
};
|
|
3345
|
+
var ZodDiscriminatedUnion = class extends ZodType {
|
|
3346
|
+
_parse(input) {
|
|
3347
|
+
const { ctx } = this._processInputParams(input);
|
|
3348
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3349
|
+
addIssueToContext(ctx, {
|
|
3350
|
+
code: ZodIssueCode.invalid_type,
|
|
3351
|
+
expected: ZodParsedType.object,
|
|
3352
|
+
received: ctx.parsedType
|
|
3353
|
+
});
|
|
3354
|
+
return INVALID;
|
|
3355
|
+
}
|
|
3356
|
+
const discriminator = this.discriminator;
|
|
3357
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
3358
|
+
const option = this.options.get(discriminatorValue);
|
|
3359
|
+
if (!option) {
|
|
3360
|
+
addIssueToContext(ctx, {
|
|
3361
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
3362
|
+
options: this.validDiscriminatorValues,
|
|
3363
|
+
path: [discriminator]
|
|
3364
|
+
});
|
|
3365
|
+
return INVALID;
|
|
3366
|
+
}
|
|
3367
|
+
if (ctx.common.async) {
|
|
3368
|
+
return option._parseAsync({
|
|
3369
|
+
data: ctx.data,
|
|
3370
|
+
path: ctx.path,
|
|
3371
|
+
parent: ctx
|
|
3372
|
+
});
|
|
3373
|
+
} else {
|
|
3374
|
+
return option._parseSync({
|
|
3375
|
+
data: ctx.data,
|
|
3376
|
+
path: ctx.path,
|
|
3377
|
+
parent: ctx
|
|
3378
|
+
});
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
get discriminator() {
|
|
3382
|
+
return this._def.discriminator;
|
|
3383
|
+
}
|
|
3384
|
+
get validDiscriminatorValues() {
|
|
3385
|
+
return Array.from(this.options.keys());
|
|
3386
|
+
}
|
|
3387
|
+
get options() {
|
|
3388
|
+
return this._def.options;
|
|
3389
|
+
}
|
|
3390
|
+
static create(discriminator, types, params) {
|
|
3391
|
+
const options = /* @__PURE__ */ new Map();
|
|
3392
|
+
try {
|
|
3393
|
+
types.forEach((type) => {
|
|
3394
|
+
const discriminatorValue = type.shape[discriminator].value;
|
|
3395
|
+
options.set(discriminatorValue, type);
|
|
3396
|
+
});
|
|
3397
|
+
} catch (e) {
|
|
3398
|
+
throw new Error("The discriminator value could not be extracted from all the provided schemas");
|
|
3399
|
+
}
|
|
3400
|
+
if (options.size !== types.length) {
|
|
3401
|
+
throw new Error("Some of the discriminator values are not unique");
|
|
3402
|
+
}
|
|
3403
|
+
return new ZodDiscriminatedUnion({
|
|
3404
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
3405
|
+
discriminator,
|
|
3406
|
+
options,
|
|
3407
|
+
...processCreateParams(params)
|
|
3408
|
+
});
|
|
3409
|
+
}
|
|
3410
|
+
};
|
|
3411
|
+
function mergeValues(a, b) {
|
|
3412
|
+
const aType = getParsedType(a);
|
|
3413
|
+
const bType = getParsedType(b);
|
|
3414
|
+
if (a === b) {
|
|
3415
|
+
return { valid: true, data: a };
|
|
3416
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
3417
|
+
const bKeys = util.objectKeys(b);
|
|
3418
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
3419
|
+
const newObj = { ...a, ...b };
|
|
3420
|
+
for (const key of sharedKeys) {
|
|
3421
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
3422
|
+
if (!sharedValue.valid) {
|
|
3423
|
+
return { valid: false };
|
|
3424
|
+
}
|
|
3425
|
+
newObj[key] = sharedValue.data;
|
|
3426
|
+
}
|
|
3427
|
+
return { valid: true, data: newObj };
|
|
3428
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
3429
|
+
if (a.length !== b.length) {
|
|
3430
|
+
return { valid: false };
|
|
3431
|
+
}
|
|
3432
|
+
const newArray = [];
|
|
3433
|
+
for (let index = 0; index < a.length; index++) {
|
|
3434
|
+
const itemA = a[index];
|
|
3435
|
+
const itemB = b[index];
|
|
3436
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
3437
|
+
if (!sharedValue.valid) {
|
|
3438
|
+
return { valid: false };
|
|
3439
|
+
}
|
|
3440
|
+
newArray.push(sharedValue.data);
|
|
3441
|
+
}
|
|
3442
|
+
return { valid: true, data: newArray };
|
|
3443
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
3444
|
+
return { valid: true, data: a };
|
|
3445
|
+
} else {
|
|
3446
|
+
return { valid: false };
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
var ZodIntersection = class extends ZodType {
|
|
3450
|
+
_parse(input) {
|
|
3451
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3452
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
3453
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
3454
|
+
return INVALID;
|
|
3455
|
+
}
|
|
3456
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
3457
|
+
if (!merged.valid) {
|
|
3458
|
+
addIssueToContext(ctx, {
|
|
3459
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
3460
|
+
});
|
|
3461
|
+
return INVALID;
|
|
3462
|
+
}
|
|
3463
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
3464
|
+
status.dirty();
|
|
3465
|
+
}
|
|
3466
|
+
return { status: status.value, value: merged.data };
|
|
3467
|
+
};
|
|
3468
|
+
if (ctx.common.async) {
|
|
3469
|
+
return Promise.all([
|
|
3470
|
+
this._def.left._parseAsync({
|
|
3471
|
+
data: ctx.data,
|
|
3472
|
+
path: ctx.path,
|
|
3473
|
+
parent: ctx
|
|
3474
|
+
}),
|
|
3475
|
+
this._def.right._parseAsync({
|
|
3476
|
+
data: ctx.data,
|
|
3477
|
+
path: ctx.path,
|
|
3478
|
+
parent: ctx
|
|
3479
|
+
})
|
|
3480
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
3481
|
+
} else {
|
|
3482
|
+
return handleParsed(this._def.left._parseSync({
|
|
3483
|
+
data: ctx.data,
|
|
3484
|
+
path: ctx.path,
|
|
3485
|
+
parent: ctx
|
|
3486
|
+
}), this._def.right._parseSync({
|
|
3487
|
+
data: ctx.data,
|
|
3488
|
+
path: ctx.path,
|
|
3489
|
+
parent: ctx
|
|
3490
|
+
}));
|
|
3491
|
+
}
|
|
3492
|
+
}
|
|
3493
|
+
};
|
|
3494
|
+
ZodIntersection.create = (left, right, params) => {
|
|
3495
|
+
return new ZodIntersection({
|
|
3496
|
+
left,
|
|
3497
|
+
right,
|
|
3498
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
3499
|
+
...processCreateParams(params)
|
|
3500
|
+
});
|
|
3501
|
+
};
|
|
3502
|
+
var ZodTuple = class extends ZodType {
|
|
3503
|
+
_parse(input) {
|
|
3504
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3505
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3506
|
+
addIssueToContext(ctx, {
|
|
3507
|
+
code: ZodIssueCode.invalid_type,
|
|
3508
|
+
expected: ZodParsedType.array,
|
|
3509
|
+
received: ctx.parsedType
|
|
3510
|
+
});
|
|
3511
|
+
return INVALID;
|
|
3512
|
+
}
|
|
3513
|
+
if (ctx.data.length < this._def.items.length) {
|
|
3514
|
+
addIssueToContext(ctx, {
|
|
3515
|
+
code: ZodIssueCode.too_small,
|
|
3516
|
+
minimum: this._def.items.length,
|
|
3517
|
+
inclusive: true,
|
|
3518
|
+
type: "array"
|
|
3519
|
+
});
|
|
3520
|
+
return INVALID;
|
|
3521
|
+
}
|
|
3522
|
+
const rest = this._def.rest;
|
|
3523
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
3524
|
+
addIssueToContext(ctx, {
|
|
3525
|
+
code: ZodIssueCode.too_big,
|
|
3526
|
+
maximum: this._def.items.length,
|
|
3527
|
+
inclusive: true,
|
|
3528
|
+
type: "array"
|
|
3529
|
+
});
|
|
3530
|
+
status.dirty();
|
|
3531
|
+
}
|
|
3532
|
+
const items = ctx.data.map((item, itemIndex) => {
|
|
3533
|
+
const schema2 = this._def.items[itemIndex] || this._def.rest;
|
|
3534
|
+
if (!schema2)
|
|
3535
|
+
return null;
|
|
3536
|
+
return schema2._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
3537
|
+
}).filter((x) => !!x);
|
|
3538
|
+
if (ctx.common.async) {
|
|
3539
|
+
return Promise.all(items).then((results) => {
|
|
3540
|
+
return ParseStatus.mergeArray(status, results);
|
|
3541
|
+
});
|
|
3542
|
+
} else {
|
|
3543
|
+
return ParseStatus.mergeArray(status, items);
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
get items() {
|
|
3547
|
+
return this._def.items;
|
|
3548
|
+
}
|
|
3549
|
+
rest(rest) {
|
|
3550
|
+
return new ZodTuple({
|
|
3551
|
+
...this._def,
|
|
3552
|
+
rest
|
|
3553
|
+
});
|
|
3554
|
+
}
|
|
3555
|
+
};
|
|
3556
|
+
ZodTuple.create = (schemas, params) => {
|
|
3557
|
+
if (!Array.isArray(schemas)) {
|
|
3558
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3559
|
+
}
|
|
3560
|
+
return new ZodTuple({
|
|
3561
|
+
items: schemas,
|
|
3562
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3563
|
+
rest: null,
|
|
3564
|
+
...processCreateParams(params)
|
|
3565
|
+
});
|
|
3566
|
+
};
|
|
3567
|
+
var ZodRecord = class extends ZodType {
|
|
3568
|
+
get keySchema() {
|
|
3569
|
+
return this._def.keyType;
|
|
3570
|
+
}
|
|
3571
|
+
get valueSchema() {
|
|
3572
|
+
return this._def.valueType;
|
|
3573
|
+
}
|
|
3574
|
+
_parse(input) {
|
|
3575
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3576
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3577
|
+
addIssueToContext(ctx, {
|
|
3578
|
+
code: ZodIssueCode.invalid_type,
|
|
3579
|
+
expected: ZodParsedType.object,
|
|
3580
|
+
received: ctx.parsedType
|
|
3581
|
+
});
|
|
3582
|
+
return INVALID;
|
|
3583
|
+
}
|
|
3584
|
+
const pairs = [];
|
|
3585
|
+
const keyType = this._def.keyType;
|
|
3586
|
+
const valueType = this._def.valueType;
|
|
3587
|
+
for (const key in ctx.data) {
|
|
3588
|
+
pairs.push({
|
|
3589
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
3590
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key))
|
|
3591
|
+
});
|
|
3592
|
+
}
|
|
3593
|
+
if (ctx.common.async) {
|
|
3594
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
3595
|
+
} else {
|
|
3596
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
get element() {
|
|
3600
|
+
return this._def.valueType;
|
|
3601
|
+
}
|
|
3602
|
+
static create(first, second, third) {
|
|
3603
|
+
if (second instanceof ZodType) {
|
|
3604
|
+
return new ZodRecord({
|
|
3605
|
+
keyType: first,
|
|
3606
|
+
valueType: second,
|
|
3607
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3608
|
+
...processCreateParams(third)
|
|
3609
|
+
});
|
|
3610
|
+
}
|
|
3611
|
+
return new ZodRecord({
|
|
3612
|
+
keyType: ZodString.create(),
|
|
3613
|
+
valueType: first,
|
|
3614
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3615
|
+
...processCreateParams(second)
|
|
3616
|
+
});
|
|
3617
|
+
}
|
|
3618
|
+
};
|
|
3619
|
+
var ZodMap = class extends ZodType {
|
|
3620
|
+
_parse(input) {
|
|
3621
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3622
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
3623
|
+
addIssueToContext(ctx, {
|
|
3624
|
+
code: ZodIssueCode.invalid_type,
|
|
3625
|
+
expected: ZodParsedType.map,
|
|
3626
|
+
received: ctx.parsedType
|
|
3627
|
+
});
|
|
3628
|
+
return INVALID;
|
|
3629
|
+
}
|
|
3630
|
+
const keyType = this._def.keyType;
|
|
3631
|
+
const valueType = this._def.valueType;
|
|
3632
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
3633
|
+
return {
|
|
3634
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
3635
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
3636
|
+
};
|
|
3637
|
+
});
|
|
3638
|
+
if (ctx.common.async) {
|
|
3639
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3640
|
+
return Promise.resolve().then(async () => {
|
|
3641
|
+
for (const pair of pairs) {
|
|
3642
|
+
const key = await pair.key;
|
|
3643
|
+
const value = await pair.value;
|
|
3644
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3645
|
+
return INVALID;
|
|
3646
|
+
}
|
|
3647
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3648
|
+
status.dirty();
|
|
3649
|
+
}
|
|
3650
|
+
finalMap.set(key.value, value.value);
|
|
3651
|
+
}
|
|
3652
|
+
return { status: status.value, value: finalMap };
|
|
3653
|
+
});
|
|
3654
|
+
} else {
|
|
3655
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3656
|
+
for (const pair of pairs) {
|
|
3657
|
+
const key = pair.key;
|
|
3658
|
+
const value = pair.value;
|
|
3659
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3660
|
+
return INVALID;
|
|
3661
|
+
}
|
|
3662
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3663
|
+
status.dirty();
|
|
3664
|
+
}
|
|
3665
|
+
finalMap.set(key.value, value.value);
|
|
3666
|
+
}
|
|
3667
|
+
return { status: status.value, value: finalMap };
|
|
3668
|
+
}
|
|
3669
|
+
}
|
|
3670
|
+
};
|
|
3671
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
3672
|
+
return new ZodMap({
|
|
3673
|
+
valueType,
|
|
3674
|
+
keyType,
|
|
3675
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3676
|
+
...processCreateParams(params)
|
|
3677
|
+
});
|
|
3678
|
+
};
|
|
3679
|
+
var ZodSet = class extends ZodType {
|
|
3680
|
+
_parse(input) {
|
|
3681
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3682
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
3683
|
+
addIssueToContext(ctx, {
|
|
3684
|
+
code: ZodIssueCode.invalid_type,
|
|
3685
|
+
expected: ZodParsedType.set,
|
|
3686
|
+
received: ctx.parsedType
|
|
3687
|
+
});
|
|
3688
|
+
return INVALID;
|
|
3689
|
+
}
|
|
3690
|
+
const def2 = this._def;
|
|
3691
|
+
if (def2.minSize !== null) {
|
|
3692
|
+
if (ctx.data.size < def2.minSize.value) {
|
|
3693
|
+
addIssueToContext(ctx, {
|
|
3694
|
+
code: ZodIssueCode.too_small,
|
|
3695
|
+
minimum: def2.minSize.value,
|
|
3696
|
+
type: "set",
|
|
3697
|
+
inclusive: true,
|
|
3698
|
+
message: def2.minSize.message
|
|
3699
|
+
});
|
|
3700
|
+
status.dirty();
|
|
3701
|
+
}
|
|
3702
|
+
}
|
|
3703
|
+
if (def2.maxSize !== null) {
|
|
3704
|
+
if (ctx.data.size > def2.maxSize.value) {
|
|
3705
|
+
addIssueToContext(ctx, {
|
|
3706
|
+
code: ZodIssueCode.too_big,
|
|
3707
|
+
maximum: def2.maxSize.value,
|
|
3708
|
+
type: "set",
|
|
3709
|
+
inclusive: true,
|
|
3710
|
+
message: def2.maxSize.message
|
|
3711
|
+
});
|
|
3712
|
+
status.dirty();
|
|
3713
|
+
}
|
|
3714
|
+
}
|
|
3715
|
+
const valueType = this._def.valueType;
|
|
3716
|
+
function finalizeSet(elements2) {
|
|
3717
|
+
const parsedSet = /* @__PURE__ */ new Set();
|
|
3718
|
+
for (const element of elements2) {
|
|
3719
|
+
if (element.status === "aborted")
|
|
3720
|
+
return INVALID;
|
|
3721
|
+
if (element.status === "dirty")
|
|
3722
|
+
status.dirty();
|
|
3723
|
+
parsedSet.add(element.value);
|
|
3724
|
+
}
|
|
3725
|
+
return { status: status.value, value: parsedSet };
|
|
3726
|
+
}
|
|
3727
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
3728
|
+
if (ctx.common.async) {
|
|
3729
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
3730
|
+
} else {
|
|
3731
|
+
return finalizeSet(elements);
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
min(minSize, message) {
|
|
3735
|
+
return new ZodSet({
|
|
3736
|
+
...this._def,
|
|
3737
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3738
|
+
});
|
|
3739
|
+
}
|
|
3740
|
+
max(maxSize, message) {
|
|
3741
|
+
return new ZodSet({
|
|
3742
|
+
...this._def,
|
|
3743
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3744
|
+
});
|
|
3745
|
+
}
|
|
3746
|
+
size(size, message) {
|
|
3747
|
+
return this.min(size, message).max(size, message);
|
|
3748
|
+
}
|
|
3749
|
+
nonempty(message) {
|
|
3750
|
+
return this.min(1, message);
|
|
3751
|
+
}
|
|
3752
|
+
};
|
|
3753
|
+
ZodSet.create = (valueType, params) => {
|
|
3754
|
+
return new ZodSet({
|
|
3755
|
+
valueType,
|
|
3756
|
+
minSize: null,
|
|
3757
|
+
maxSize: null,
|
|
3758
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3759
|
+
...processCreateParams(params)
|
|
3760
|
+
});
|
|
3761
|
+
};
|
|
3762
|
+
var ZodFunction = class extends ZodType {
|
|
3763
|
+
constructor() {
|
|
3764
|
+
super(...arguments);
|
|
3765
|
+
this.validate = this.implement;
|
|
3766
|
+
}
|
|
3767
|
+
_parse(input) {
|
|
3768
|
+
const { ctx } = this._processInputParams(input);
|
|
3769
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
3770
|
+
addIssueToContext(ctx, {
|
|
3771
|
+
code: ZodIssueCode.invalid_type,
|
|
3772
|
+
expected: ZodParsedType.function,
|
|
3773
|
+
received: ctx.parsedType
|
|
3774
|
+
});
|
|
3775
|
+
return INVALID;
|
|
3776
|
+
}
|
|
3777
|
+
function makeArgsIssue(args, error) {
|
|
3778
|
+
return makeIssue({
|
|
3779
|
+
data: args,
|
|
3780
|
+
path: ctx.path,
|
|
3781
|
+
errorMaps: [
|
|
3782
|
+
ctx.common.contextualErrorMap,
|
|
3783
|
+
ctx.schemaErrorMap,
|
|
3784
|
+
getErrorMap(),
|
|
3785
|
+
errorMap
|
|
3786
|
+
].filter((x) => !!x),
|
|
3787
|
+
issueData: {
|
|
3788
|
+
code: ZodIssueCode.invalid_arguments,
|
|
3789
|
+
argumentsError: error
|
|
3790
|
+
}
|
|
3791
|
+
});
|
|
3792
|
+
}
|
|
3793
|
+
function makeReturnsIssue(returns, error) {
|
|
3794
|
+
return makeIssue({
|
|
3795
|
+
data: returns,
|
|
3796
|
+
path: ctx.path,
|
|
3797
|
+
errorMaps: [
|
|
3798
|
+
ctx.common.contextualErrorMap,
|
|
3799
|
+
ctx.schemaErrorMap,
|
|
3800
|
+
getErrorMap(),
|
|
3801
|
+
errorMap
|
|
3802
|
+
].filter((x) => !!x),
|
|
3803
|
+
issueData: {
|
|
3804
|
+
code: ZodIssueCode.invalid_return_type,
|
|
3805
|
+
returnTypeError: error
|
|
3806
|
+
}
|
|
3807
|
+
});
|
|
3808
|
+
}
|
|
3809
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
3810
|
+
const fn = ctx.data;
|
|
3811
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
3812
|
+
return OK(async (...args) => {
|
|
3813
|
+
const error = new ZodError([]);
|
|
3814
|
+
const parsedArgs = await this._def.args.parseAsync(args, params).catch((e) => {
|
|
3815
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
3816
|
+
throw error;
|
|
3817
|
+
});
|
|
3818
|
+
const result = await fn(...parsedArgs);
|
|
3819
|
+
const parsedReturns = await this._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
3820
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
3821
|
+
throw error;
|
|
3822
|
+
});
|
|
3823
|
+
return parsedReturns;
|
|
3824
|
+
});
|
|
3825
|
+
} else {
|
|
3826
|
+
return OK((...args) => {
|
|
3827
|
+
const parsedArgs = this._def.args.safeParse(args, params);
|
|
3828
|
+
if (!parsedArgs.success) {
|
|
3829
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
3830
|
+
}
|
|
3831
|
+
const result = fn(...parsedArgs.data);
|
|
3832
|
+
const parsedReturns = this._def.returns.safeParse(result, params);
|
|
3833
|
+
if (!parsedReturns.success) {
|
|
3834
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
3835
|
+
}
|
|
3836
|
+
return parsedReturns.data;
|
|
3837
|
+
});
|
|
3838
|
+
}
|
|
3839
|
+
}
|
|
3840
|
+
parameters() {
|
|
3841
|
+
return this._def.args;
|
|
3842
|
+
}
|
|
3843
|
+
returnType() {
|
|
3844
|
+
return this._def.returns;
|
|
3845
|
+
}
|
|
3846
|
+
args(...items) {
|
|
3847
|
+
return new ZodFunction({
|
|
3848
|
+
...this._def,
|
|
3849
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
3850
|
+
});
|
|
3851
|
+
}
|
|
3852
|
+
returns(returnType) {
|
|
3853
|
+
return new ZodFunction({
|
|
3854
|
+
...this._def,
|
|
3855
|
+
returns: returnType
|
|
3856
|
+
});
|
|
3857
|
+
}
|
|
3858
|
+
implement(func) {
|
|
3859
|
+
const validatedFunc = this.parse(func);
|
|
3860
|
+
return validatedFunc;
|
|
3861
|
+
}
|
|
3862
|
+
strictImplement(func) {
|
|
3863
|
+
const validatedFunc = this.parse(func);
|
|
3864
|
+
return validatedFunc;
|
|
3865
|
+
}
|
|
3866
|
+
static create(args, returns, params) {
|
|
3867
|
+
return new ZodFunction({
|
|
3868
|
+
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
3869
|
+
returns: returns || ZodUnknown.create(),
|
|
3870
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
3871
|
+
...processCreateParams(params)
|
|
3872
|
+
});
|
|
3873
|
+
}
|
|
3874
|
+
};
|
|
3875
|
+
var ZodLazy = class extends ZodType {
|
|
3876
|
+
get schema() {
|
|
3877
|
+
return this._def.getter();
|
|
3878
|
+
}
|
|
3879
|
+
_parse(input) {
|
|
3880
|
+
const { ctx } = this._processInputParams(input);
|
|
3881
|
+
const lazySchema = this._def.getter();
|
|
3882
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
3883
|
+
}
|
|
3884
|
+
};
|
|
3885
|
+
ZodLazy.create = (getter, params) => {
|
|
3886
|
+
return new ZodLazy({
|
|
3887
|
+
getter,
|
|
3888
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
3889
|
+
...processCreateParams(params)
|
|
3890
|
+
});
|
|
3891
|
+
};
|
|
3892
|
+
var ZodLiteral = class extends ZodType {
|
|
3893
|
+
_parse(input) {
|
|
3894
|
+
if (input.data !== this._def.value) {
|
|
3895
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3896
|
+
addIssueToContext(ctx, {
|
|
3897
|
+
code: ZodIssueCode.invalid_literal,
|
|
3898
|
+
expected: this._def.value
|
|
3899
|
+
});
|
|
3900
|
+
return INVALID;
|
|
3901
|
+
}
|
|
3902
|
+
return { status: "valid", value: input.data };
|
|
3903
|
+
}
|
|
3904
|
+
get value() {
|
|
3905
|
+
return this._def.value;
|
|
3906
|
+
}
|
|
3907
|
+
};
|
|
3908
|
+
ZodLiteral.create = (value, params) => {
|
|
3909
|
+
return new ZodLiteral({
|
|
3910
|
+
value,
|
|
3911
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3912
|
+
...processCreateParams(params)
|
|
3913
|
+
});
|
|
3914
|
+
};
|
|
3915
|
+
function createZodEnum(values, params) {
|
|
3916
|
+
return new ZodEnum({
|
|
3917
|
+
values,
|
|
3918
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
3919
|
+
...processCreateParams(params)
|
|
3920
|
+
});
|
|
3921
|
+
}
|
|
3922
|
+
var ZodEnum = class extends ZodType {
|
|
3923
|
+
_parse(input) {
|
|
3924
|
+
if (typeof input.data !== "string") {
|
|
3925
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3926
|
+
const expectedValues = this._def.values;
|
|
3927
|
+
addIssueToContext(ctx, {
|
|
3928
|
+
expected: util.joinValues(expectedValues),
|
|
3929
|
+
received: ctx.parsedType,
|
|
3930
|
+
code: ZodIssueCode.invalid_type
|
|
3931
|
+
});
|
|
3932
|
+
return INVALID;
|
|
3933
|
+
}
|
|
3934
|
+
if (this._def.values.indexOf(input.data) === -1) {
|
|
3935
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3936
|
+
const expectedValues = this._def.values;
|
|
3937
|
+
addIssueToContext(ctx, {
|
|
3938
|
+
received: ctx.data,
|
|
3939
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3940
|
+
options: expectedValues
|
|
3941
|
+
});
|
|
3942
|
+
return INVALID;
|
|
3943
|
+
}
|
|
3944
|
+
return OK(input.data);
|
|
3945
|
+
}
|
|
3946
|
+
get options() {
|
|
3947
|
+
return this._def.values;
|
|
3948
|
+
}
|
|
3949
|
+
get enum() {
|
|
3950
|
+
const enumValues = {};
|
|
3951
|
+
for (const val of this._def.values) {
|
|
3952
|
+
enumValues[val] = val;
|
|
3953
|
+
}
|
|
3954
|
+
return enumValues;
|
|
3955
|
+
}
|
|
3956
|
+
get Values() {
|
|
3957
|
+
const enumValues = {};
|
|
3958
|
+
for (const val of this._def.values) {
|
|
3959
|
+
enumValues[val] = val;
|
|
3960
|
+
}
|
|
3961
|
+
return enumValues;
|
|
3962
|
+
}
|
|
3963
|
+
get Enum() {
|
|
3964
|
+
const enumValues = {};
|
|
3965
|
+
for (const val of this._def.values) {
|
|
3966
|
+
enumValues[val] = val;
|
|
3967
|
+
}
|
|
3968
|
+
return enumValues;
|
|
3969
|
+
}
|
|
3970
|
+
};
|
|
3971
|
+
ZodEnum.create = createZodEnum;
|
|
3972
|
+
var ZodNativeEnum = class extends ZodType {
|
|
3973
|
+
_parse(input) {
|
|
3974
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3975
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3976
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
3977
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3978
|
+
addIssueToContext(ctx, {
|
|
3979
|
+
expected: util.joinValues(expectedValues),
|
|
3980
|
+
received: ctx.parsedType,
|
|
3981
|
+
code: ZodIssueCode.invalid_type
|
|
3982
|
+
});
|
|
3983
|
+
return INVALID;
|
|
3984
|
+
}
|
|
3985
|
+
if (nativeEnumValues.indexOf(input.data) === -1) {
|
|
3986
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3987
|
+
addIssueToContext(ctx, {
|
|
3988
|
+
received: ctx.data,
|
|
3989
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3990
|
+
options: expectedValues
|
|
3991
|
+
});
|
|
3992
|
+
return INVALID;
|
|
3993
|
+
}
|
|
3994
|
+
return OK(input.data);
|
|
3995
|
+
}
|
|
3996
|
+
get enum() {
|
|
3997
|
+
return this._def.values;
|
|
3998
|
+
}
|
|
3999
|
+
};
|
|
4000
|
+
ZodNativeEnum.create = (values, params) => {
|
|
4001
|
+
return new ZodNativeEnum({
|
|
4002
|
+
values,
|
|
4003
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
4004
|
+
...processCreateParams(params)
|
|
4005
|
+
});
|
|
4006
|
+
};
|
|
4007
|
+
var ZodPromise = class extends ZodType {
|
|
4008
|
+
_parse(input) {
|
|
4009
|
+
const { ctx } = this._processInputParams(input);
|
|
4010
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
4011
|
+
addIssueToContext(ctx, {
|
|
4012
|
+
code: ZodIssueCode.invalid_type,
|
|
4013
|
+
expected: ZodParsedType.promise,
|
|
4014
|
+
received: ctx.parsedType
|
|
4015
|
+
});
|
|
4016
|
+
return INVALID;
|
|
4017
|
+
}
|
|
4018
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
4019
|
+
return OK(promisified.then((data) => {
|
|
4020
|
+
return this._def.type.parseAsync(data, {
|
|
4021
|
+
path: ctx.path,
|
|
4022
|
+
errorMap: ctx.common.contextualErrorMap
|
|
4023
|
+
});
|
|
4024
|
+
}));
|
|
4025
|
+
}
|
|
4026
|
+
};
|
|
4027
|
+
ZodPromise.create = (schema2, params) => {
|
|
4028
|
+
return new ZodPromise({
|
|
4029
|
+
type: schema2,
|
|
4030
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
4031
|
+
...processCreateParams(params)
|
|
4032
|
+
});
|
|
4033
|
+
};
|
|
4034
|
+
var ZodEffects = class extends ZodType {
|
|
4035
|
+
innerType() {
|
|
4036
|
+
return this._def.schema;
|
|
4037
|
+
}
|
|
4038
|
+
_parse(input) {
|
|
4039
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4040
|
+
const effect = this._def.effect || null;
|
|
4041
|
+
if (effect.type === "preprocess") {
|
|
4042
|
+
const processed = effect.transform(ctx.data);
|
|
4043
|
+
if (ctx.common.async) {
|
|
4044
|
+
return Promise.resolve(processed).then((processed2) => {
|
|
4045
|
+
return this._def.schema._parseAsync({
|
|
4046
|
+
data: processed2,
|
|
4047
|
+
path: ctx.path,
|
|
4048
|
+
parent: ctx
|
|
4049
|
+
});
|
|
4050
|
+
});
|
|
4051
|
+
} else {
|
|
4052
|
+
return this._def.schema._parseSync({
|
|
4053
|
+
data: processed,
|
|
4054
|
+
path: ctx.path,
|
|
4055
|
+
parent: ctx
|
|
4056
|
+
});
|
|
4057
|
+
}
|
|
4058
|
+
}
|
|
4059
|
+
const checkCtx = {
|
|
4060
|
+
addIssue: (arg) => {
|
|
4061
|
+
addIssueToContext(ctx, arg);
|
|
4062
|
+
if (arg.fatal) {
|
|
4063
|
+
status.abort();
|
|
4064
|
+
} else {
|
|
4065
|
+
status.dirty();
|
|
4066
|
+
}
|
|
4067
|
+
},
|
|
4068
|
+
get path() {
|
|
4069
|
+
return ctx.path;
|
|
4070
|
+
}
|
|
4071
|
+
};
|
|
4072
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
4073
|
+
if (effect.type === "refinement") {
|
|
4074
|
+
const executeRefinement = (acc) => {
|
|
4075
|
+
const result = effect.refinement(acc, checkCtx);
|
|
4076
|
+
if (ctx.common.async) {
|
|
4077
|
+
return Promise.resolve(result);
|
|
4078
|
+
}
|
|
4079
|
+
if (result instanceof Promise) {
|
|
4080
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
4081
|
+
}
|
|
4082
|
+
return acc;
|
|
4083
|
+
};
|
|
4084
|
+
if (ctx.common.async === false) {
|
|
4085
|
+
const inner = this._def.schema._parseSync({
|
|
4086
|
+
data: ctx.data,
|
|
4087
|
+
path: ctx.path,
|
|
4088
|
+
parent: ctx
|
|
4089
|
+
});
|
|
4090
|
+
if (inner.status === "aborted")
|
|
4091
|
+
return INVALID;
|
|
4092
|
+
if (inner.status === "dirty")
|
|
4093
|
+
status.dirty();
|
|
4094
|
+
executeRefinement(inner.value);
|
|
4095
|
+
return { status: status.value, value: inner.value };
|
|
4096
|
+
} else {
|
|
4097
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
4098
|
+
if (inner.status === "aborted")
|
|
4099
|
+
return INVALID;
|
|
4100
|
+
if (inner.status === "dirty")
|
|
4101
|
+
status.dirty();
|
|
4102
|
+
return executeRefinement(inner.value).then(() => {
|
|
4103
|
+
return { status: status.value, value: inner.value };
|
|
4104
|
+
});
|
|
4105
|
+
});
|
|
4106
|
+
}
|
|
4107
|
+
}
|
|
4108
|
+
if (effect.type === "transform") {
|
|
4109
|
+
if (ctx.common.async === false) {
|
|
4110
|
+
const base3 = this._def.schema._parseSync({
|
|
4111
|
+
data: ctx.data,
|
|
4112
|
+
path: ctx.path,
|
|
4113
|
+
parent: ctx
|
|
4114
|
+
});
|
|
4115
|
+
if (!isValid(base3))
|
|
4116
|
+
return base3;
|
|
4117
|
+
const result = effect.transform(base3.value, checkCtx);
|
|
4118
|
+
if (result instanceof Promise) {
|
|
4119
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
4120
|
+
}
|
|
4121
|
+
return { status: status.value, value: result };
|
|
4122
|
+
} else {
|
|
4123
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base3) => {
|
|
4124
|
+
if (!isValid(base3))
|
|
4125
|
+
return base3;
|
|
4126
|
+
return Promise.resolve(effect.transform(base3.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
4127
|
+
});
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4130
|
+
util.assertNever(effect);
|
|
4131
|
+
}
|
|
4132
|
+
};
|
|
4133
|
+
ZodEffects.create = (schema2, effect, params) => {
|
|
4134
|
+
return new ZodEffects({
|
|
4135
|
+
schema: schema2,
|
|
4136
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4137
|
+
effect,
|
|
4138
|
+
...processCreateParams(params)
|
|
4139
|
+
});
|
|
4140
|
+
};
|
|
4141
|
+
ZodEffects.createWithPreprocess = (preprocess, schema2, params) => {
|
|
4142
|
+
return new ZodEffects({
|
|
4143
|
+
schema: schema2,
|
|
4144
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
4145
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4146
|
+
...processCreateParams(params)
|
|
4147
|
+
});
|
|
4148
|
+
};
|
|
4149
|
+
var ZodOptional = class extends ZodType {
|
|
4150
|
+
_parse(input) {
|
|
4151
|
+
const parsedType = this._getType(input);
|
|
4152
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
4153
|
+
return OK(void 0);
|
|
4154
|
+
}
|
|
4155
|
+
return this._def.innerType._parse(input);
|
|
4156
|
+
}
|
|
4157
|
+
unwrap() {
|
|
4158
|
+
return this._def.innerType;
|
|
4159
|
+
}
|
|
4160
|
+
};
|
|
4161
|
+
ZodOptional.create = (type, params) => {
|
|
4162
|
+
return new ZodOptional({
|
|
4163
|
+
innerType: type,
|
|
4164
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
4165
|
+
...processCreateParams(params)
|
|
4166
|
+
});
|
|
4167
|
+
};
|
|
4168
|
+
var ZodNullable = class extends ZodType {
|
|
4169
|
+
_parse(input) {
|
|
4170
|
+
const parsedType = this._getType(input);
|
|
4171
|
+
if (parsedType === ZodParsedType.null) {
|
|
4172
|
+
return OK(null);
|
|
4173
|
+
}
|
|
4174
|
+
return this._def.innerType._parse(input);
|
|
4175
|
+
}
|
|
4176
|
+
unwrap() {
|
|
4177
|
+
return this._def.innerType;
|
|
4178
|
+
}
|
|
4179
|
+
};
|
|
4180
|
+
ZodNullable.create = (type, params) => {
|
|
4181
|
+
return new ZodNullable({
|
|
4182
|
+
innerType: type,
|
|
4183
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
4184
|
+
...processCreateParams(params)
|
|
4185
|
+
});
|
|
4186
|
+
};
|
|
4187
|
+
var ZodDefault = class extends ZodType {
|
|
4188
|
+
_parse(input) {
|
|
4189
|
+
const { ctx } = this._processInputParams(input);
|
|
4190
|
+
let data = ctx.data;
|
|
4191
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
4192
|
+
data = this._def.defaultValue();
|
|
4193
|
+
}
|
|
4194
|
+
return this._def.innerType._parse({
|
|
4195
|
+
data,
|
|
4196
|
+
path: ctx.path,
|
|
4197
|
+
parent: ctx
|
|
4198
|
+
});
|
|
4199
|
+
}
|
|
4200
|
+
removeDefault() {
|
|
4201
|
+
return this._def.innerType;
|
|
4202
|
+
}
|
|
4203
|
+
};
|
|
4204
|
+
ZodDefault.create = (type, params) => {
|
|
4205
|
+
return new ZodOptional({
|
|
4206
|
+
innerType: type,
|
|
4207
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
4208
|
+
...processCreateParams(params)
|
|
4209
|
+
});
|
|
4210
|
+
};
|
|
4211
|
+
var ZodNaN = class extends ZodType {
|
|
4212
|
+
_parse(input) {
|
|
4213
|
+
const parsedType = this._getType(input);
|
|
4214
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
4215
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4216
|
+
addIssueToContext(ctx, {
|
|
4217
|
+
code: ZodIssueCode.invalid_type,
|
|
4218
|
+
expected: ZodParsedType.nan,
|
|
4219
|
+
received: ctx.parsedType
|
|
4220
|
+
});
|
|
4221
|
+
return INVALID;
|
|
4222
|
+
}
|
|
4223
|
+
return { status: "valid", value: input.data };
|
|
4224
|
+
}
|
|
4225
|
+
};
|
|
4226
|
+
ZodNaN.create = (params) => {
|
|
4227
|
+
return new ZodNaN({
|
|
4228
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
4229
|
+
...processCreateParams(params)
|
|
4230
|
+
});
|
|
4231
|
+
};
|
|
4232
|
+
var BRAND = Symbol("zod_brand");
|
|
4233
|
+
var ZodBranded = class extends ZodType {
|
|
4234
|
+
_parse(input) {
|
|
4235
|
+
const { ctx } = this._processInputParams(input);
|
|
4236
|
+
const data = ctx.data;
|
|
4237
|
+
return this._def.type._parse({
|
|
4238
|
+
data,
|
|
4239
|
+
path: ctx.path,
|
|
4240
|
+
parent: ctx
|
|
4241
|
+
});
|
|
4242
|
+
}
|
|
4243
|
+
unwrap() {
|
|
4244
|
+
return this._def.type;
|
|
4245
|
+
}
|
|
4246
|
+
};
|
|
4247
|
+
var custom = (check, params = {}, fatal) => {
|
|
4248
|
+
if (check)
|
|
4249
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
4250
|
+
if (!check(data)) {
|
|
4251
|
+
const p = typeof params === "function" ? params(data) : params;
|
|
4252
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
4253
|
+
ctx.addIssue({ code: "custom", ...p2, fatal });
|
|
4254
|
+
}
|
|
4255
|
+
});
|
|
4256
|
+
return ZodAny.create();
|
|
4257
|
+
};
|
|
4258
|
+
var late = {
|
|
4259
|
+
object: ZodObject.lazycreate
|
|
4260
|
+
};
|
|
4261
|
+
var ZodFirstPartyTypeKind;
|
|
4262
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
4263
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
4264
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
4265
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
4266
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
4267
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
4268
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
4269
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
4270
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
4271
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
4272
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
4273
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
4274
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
4275
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
4276
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
4277
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
4278
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
4279
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
4280
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
4281
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
4282
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
4283
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
4284
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
4285
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
4286
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
4287
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
4288
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
4289
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
4290
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
4291
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
4292
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
4293
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
4294
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
4295
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
4296
|
+
var instanceOfType = (cls, params = {
|
|
4297
|
+
message: `Input not instance of ${cls.name}`
|
|
4298
|
+
}) => custom((data) => data instanceof cls, params, true);
|
|
4299
|
+
var stringType = ZodString.create;
|
|
4300
|
+
var numberType = ZodNumber.create;
|
|
4301
|
+
var nanType = ZodNaN.create;
|
|
4302
|
+
var bigIntType = ZodBigInt.create;
|
|
4303
|
+
var booleanType = ZodBoolean.create;
|
|
4304
|
+
var dateType = ZodDate.create;
|
|
4305
|
+
var undefinedType = ZodUndefined.create;
|
|
4306
|
+
var nullType = ZodNull.create;
|
|
4307
|
+
var anyType = ZodAny.create;
|
|
4308
|
+
var unknownType = ZodUnknown.create;
|
|
4309
|
+
var neverType = ZodNever.create;
|
|
4310
|
+
var voidType = ZodVoid.create;
|
|
4311
|
+
var arrayType = ZodArray.create;
|
|
4312
|
+
var objectType = ZodObject.create;
|
|
4313
|
+
var strictObjectType = ZodObject.strictCreate;
|
|
4314
|
+
var unionType = ZodUnion.create;
|
|
4315
|
+
var discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4316
|
+
var intersectionType = ZodIntersection.create;
|
|
4317
|
+
var tupleType = ZodTuple.create;
|
|
4318
|
+
var recordType = ZodRecord.create;
|
|
4319
|
+
var mapType = ZodMap.create;
|
|
4320
|
+
var setType = ZodSet.create;
|
|
4321
|
+
var functionType = ZodFunction.create;
|
|
4322
|
+
var lazyType = ZodLazy.create;
|
|
4323
|
+
var literalType = ZodLiteral.create;
|
|
4324
|
+
var enumType = ZodEnum.create;
|
|
4325
|
+
var nativeEnumType = ZodNativeEnum.create;
|
|
4326
|
+
var promiseType = ZodPromise.create;
|
|
4327
|
+
var effectsType = ZodEffects.create;
|
|
4328
|
+
var optionalType = ZodOptional.create;
|
|
4329
|
+
var nullableType = ZodNullable.create;
|
|
4330
|
+
var preprocessType = ZodEffects.createWithPreprocess;
|
|
4331
|
+
var ostring = () => stringType().optional();
|
|
4332
|
+
var onumber = () => numberType().optional();
|
|
4333
|
+
var oboolean = () => booleanType().optional();
|
|
4334
|
+
var NEVER = INVALID;
|
|
4335
|
+
var mod = /* @__PURE__ */ Object.freeze({
|
|
4336
|
+
__proto__: null,
|
|
4337
|
+
getParsedType,
|
|
4338
|
+
ZodParsedType,
|
|
4339
|
+
defaultErrorMap: errorMap,
|
|
4340
|
+
setErrorMap,
|
|
4341
|
+
getErrorMap,
|
|
4342
|
+
makeIssue,
|
|
4343
|
+
EMPTY_PATH,
|
|
4344
|
+
addIssueToContext,
|
|
4345
|
+
ParseStatus,
|
|
4346
|
+
INVALID,
|
|
4347
|
+
DIRTY,
|
|
4348
|
+
OK,
|
|
4349
|
+
isAborted,
|
|
4350
|
+
isDirty,
|
|
4351
|
+
isValid,
|
|
4352
|
+
isAsync,
|
|
4353
|
+
ZodType,
|
|
4354
|
+
ZodString,
|
|
4355
|
+
ZodNumber,
|
|
4356
|
+
ZodBigInt,
|
|
4357
|
+
ZodBoolean,
|
|
4358
|
+
ZodDate,
|
|
4359
|
+
ZodUndefined,
|
|
4360
|
+
ZodNull,
|
|
4361
|
+
ZodAny,
|
|
4362
|
+
ZodUnknown,
|
|
4363
|
+
ZodNever,
|
|
4364
|
+
ZodVoid,
|
|
4365
|
+
ZodArray,
|
|
4366
|
+
get objectUtil() {
|
|
4367
|
+
return objectUtil;
|
|
4368
|
+
},
|
|
4369
|
+
ZodObject,
|
|
4370
|
+
ZodUnion,
|
|
4371
|
+
ZodDiscriminatedUnion,
|
|
4372
|
+
ZodIntersection,
|
|
4373
|
+
ZodTuple,
|
|
4374
|
+
ZodRecord,
|
|
4375
|
+
ZodMap,
|
|
4376
|
+
ZodSet,
|
|
4377
|
+
ZodFunction,
|
|
4378
|
+
ZodLazy,
|
|
4379
|
+
ZodLiteral,
|
|
4380
|
+
ZodEnum,
|
|
4381
|
+
ZodNativeEnum,
|
|
4382
|
+
ZodPromise,
|
|
4383
|
+
ZodEffects,
|
|
4384
|
+
ZodTransformer: ZodEffects,
|
|
4385
|
+
ZodOptional,
|
|
4386
|
+
ZodNullable,
|
|
4387
|
+
ZodDefault,
|
|
4388
|
+
ZodNaN,
|
|
4389
|
+
BRAND,
|
|
4390
|
+
ZodBranded,
|
|
4391
|
+
custom,
|
|
4392
|
+
Schema: ZodType,
|
|
4393
|
+
ZodSchema: ZodType,
|
|
4394
|
+
late,
|
|
4395
|
+
get ZodFirstPartyTypeKind() {
|
|
4396
|
+
return ZodFirstPartyTypeKind;
|
|
4397
|
+
},
|
|
4398
|
+
any: anyType,
|
|
4399
|
+
array: arrayType,
|
|
4400
|
+
bigint: bigIntType,
|
|
4401
|
+
boolean: booleanType,
|
|
4402
|
+
date: dateType,
|
|
4403
|
+
discriminatedUnion: discriminatedUnionType,
|
|
4404
|
+
effect: effectsType,
|
|
4405
|
+
"enum": enumType,
|
|
4406
|
+
"function": functionType,
|
|
4407
|
+
"instanceof": instanceOfType,
|
|
4408
|
+
intersection: intersectionType,
|
|
4409
|
+
lazy: lazyType,
|
|
4410
|
+
literal: literalType,
|
|
4411
|
+
map: mapType,
|
|
4412
|
+
nan: nanType,
|
|
4413
|
+
nativeEnum: nativeEnumType,
|
|
4414
|
+
never: neverType,
|
|
4415
|
+
"null": nullType,
|
|
4416
|
+
nullable: nullableType,
|
|
4417
|
+
number: numberType,
|
|
4418
|
+
object: objectType,
|
|
4419
|
+
oboolean,
|
|
4420
|
+
onumber,
|
|
4421
|
+
optional: optionalType,
|
|
4422
|
+
ostring,
|
|
4423
|
+
preprocess: preprocessType,
|
|
4424
|
+
promise: promiseType,
|
|
4425
|
+
record: recordType,
|
|
4426
|
+
set: setType,
|
|
4427
|
+
strictObject: strictObjectType,
|
|
4428
|
+
string: stringType,
|
|
4429
|
+
transformer: effectsType,
|
|
4430
|
+
tuple: tupleType,
|
|
4431
|
+
"undefined": undefinedType,
|
|
4432
|
+
union: unionType,
|
|
4433
|
+
unknown: unknownType,
|
|
4434
|
+
"void": voidType,
|
|
4435
|
+
NEVER,
|
|
4436
|
+
ZodIssueCode,
|
|
4437
|
+
quotelessJson,
|
|
4438
|
+
ZodError
|
|
4439
|
+
});
|
|
4440
|
+
|
|
4441
|
+
// src/types.ts
|
|
4442
|
+
var cidSchema = mod.any().refine((obj) => CID.asCID(obj) !== null, {
|
|
4443
|
+
message: "Not a CID"
|
|
4444
|
+
}).transform((obj) => CID.asCID(obj));
|
|
4445
|
+
var schema = {
|
|
4446
|
+
cid: cidSchema,
|
|
4447
|
+
bytes: mod.instanceof(Uint8Array),
|
|
4448
|
+
string: mod.string(),
|
|
4449
|
+
array: mod.array(mod.unknown()),
|
|
4450
|
+
map: mod.record(mod.string(), mod.unknown()),
|
|
4451
|
+
unknown: mod.unknown()
|
|
4452
|
+
};
|
|
4453
|
+
var def = {
|
|
4454
|
+
cid: {
|
|
4455
|
+
name: "cid",
|
|
4456
|
+
schema: schema.cid
|
|
4457
|
+
},
|
|
4458
|
+
bytes: {
|
|
4459
|
+
name: "bytes",
|
|
4460
|
+
schema: schema.bytes
|
|
4461
|
+
},
|
|
4462
|
+
string: {
|
|
4463
|
+
name: "string",
|
|
4464
|
+
schema: schema.string
|
|
4465
|
+
},
|
|
4466
|
+
map: {
|
|
4467
|
+
name: "map",
|
|
4468
|
+
schema: schema.map
|
|
4469
|
+
},
|
|
4470
|
+
unknown: {
|
|
4471
|
+
name: "unknown",
|
|
4472
|
+
schema: schema.unknown
|
|
4473
|
+
}
|
|
4474
|
+
};
|
|
4475
|
+
|
|
4476
|
+
// src/times.ts
|
|
4477
|
+
var SECOND = 1e3;
|
|
4478
|
+
var MINUTE = SECOND * 60;
|
|
4479
|
+
var HOUR = MINUTE * 60;
|
|
4480
|
+
var DAY = HOUR * 24;
|
|
4481
|
+
|
|
4482
|
+
// src/strings.ts
|
|
4483
|
+
var utf8Len = (str) => {
|
|
4484
|
+
return new TextEncoder().encode(str).byteLength;
|
|
4485
|
+
};
|
|
4486
|
+
var graphemeLen = (str) => {
|
|
4487
|
+
return [...new Intl.Segmenter().segment(str)].length;
|
|
4488
|
+
};
|
|
4489
|
+
//# sourceMappingURL=index.js.map
|