seroval 0.14.0 → 0.15.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.
Files changed (47) hide show
  1. package/dist/cjs/development/index.cjs +157 -117
  2. package/dist/cjs/development/index.cjs.map +2 -2
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +157 -117
  5. package/dist/esm/development/index.mjs.map +2 -2
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/core/base-primitives.d.ts +3 -3
  8. package/dist/types/core/base-primitives.d.ts.map +1 -1
  9. package/dist/types/core/constants.d.ts +2 -3
  10. package/dist/types/core/constants.d.ts.map +1 -1
  11. package/dist/types/core/context/deserializer.d.ts.map +1 -1
  12. package/dist/types/core/context/parser/stream.d.ts.map +1 -1
  13. package/dist/types/core/context/parser.d.ts +3 -3
  14. package/dist/types/core/context/parser.d.ts.map +1 -1
  15. package/dist/types/core/context/serializer.d.ts +11 -10
  16. package/dist/types/core/context/serializer.d.ts.map +1 -1
  17. package/dist/types/core/cross/index.d.ts.map +1 -1
  18. package/dist/types/core/cross/serializer.d.ts +1 -8
  19. package/dist/types/core/cross/serializer.d.ts.map +1 -1
  20. package/dist/types/core/keys.d.ts +0 -12
  21. package/dist/types/core/keys.d.ts.map +1 -1
  22. package/dist/types/core/plugin.d.ts.map +1 -1
  23. package/dist/types/core/special-reference.d.ts +13 -3
  24. package/dist/types/core/special-reference.d.ts.map +1 -1
  25. package/dist/types/core/types.d.ts +33 -20
  26. package/dist/types/core/types.d.ts.map +1 -1
  27. package/dist/types/core/web-api.d.ts +2 -2
  28. package/dist/types/core/web-api.d.ts.map +1 -1
  29. package/dist/types/index.d.ts +0 -1
  30. package/dist/types/index.d.ts.map +1 -1
  31. package/package.json +2 -2
  32. package/src/core/base-primitives.ts +3 -2
  33. package/src/core/constants.ts +1 -2
  34. package/src/core/context/deserializer.ts +6 -7
  35. package/src/core/context/parser/async.ts +2 -2
  36. package/src/core/context/parser/stream.ts +29 -13
  37. package/src/core/context/parser.ts +12 -36
  38. package/src/core/context/serializer.ts +105 -44
  39. package/src/core/cross/index.ts +11 -2
  40. package/src/core/cross/serializer.ts +0 -57
  41. package/src/core/keys.ts +2 -38
  42. package/src/core/plugin.ts +0 -2
  43. package/src/core/special-reference.ts +23 -3
  44. package/src/core/types.ts +33 -24
  45. package/src/core/web-api.ts +2 -1
  46. package/src/index.ts +0 -2
  47. package/src/core/is-serializable.new.ts +0 -208
@@ -1,208 +0,0 @@
1
- import { ALL_ENABLED, BIGINT_FLAG, Feature } from './compat';
2
- import { INV_SYMBOL_REF, UNIVERSAL_SENTINEL } from './constants';
3
- import type { Plugin, PluginAccessOptions } from './plugin';
4
- import { hasReferenceID } from './reference';
5
- import { getErrorOptions } from './utils/error';
6
-
7
- export type SerializationMode = 'sync' | 'async' | 'stream';
8
-
9
- export interface IsSerializableOptions extends PluginAccessOptions {
10
- mode: SerializationMode;
11
- disabledFeatures?: number;
12
- }
13
-
14
- class IsSerializableContext implements PluginAccessOptions {
15
- plugins?: Plugin<any, any>[] | undefined;
16
-
17
- mode: SerializationMode;
18
-
19
- features: number;
20
-
21
- constructor(options: IsSerializableOptions) {
22
- this.mode = options.mode;
23
- this.plugins = options.plugins;
24
- this.features = ALL_ENABLED ^ (options.disabledFeatures || 0);
25
- }
26
-
27
- checkArray(values: unknown[]): boolean {
28
- if (!this.check(values[0])) {
29
- return false;
30
- }
31
- for (let i = 1, len = values.length; i < len; i++) {
32
- if (!this.check(values[i])) {
33
- return false;
34
- }
35
- }
36
- return true;
37
- }
38
-
39
- checkPlainObject(value: object): boolean {
40
- return this.checkArray(Object.values(value));
41
- }
42
-
43
- checkError(value: Error): boolean {
44
- const options = getErrorOptions(value, this.features);
45
- if (options) {
46
- return this.checkPlainObject(options);
47
- }
48
- return true;
49
- }
50
-
51
- checkPlugin(value: unknown): boolean {
52
- const current = this.plugins;
53
- if (current) {
54
- for (let i = 0, len = current.length; i < len; i++) {
55
- const plugin = current[i];
56
- if (plugin.test(value) && plugin.isSerializable && plugin.isSerializable(value)) {
57
- return true;
58
- }
59
- }
60
- }
61
- return true;
62
- }
63
-
64
- checkObject(value: object | null): boolean {
65
- if (!value) {
66
- return true;
67
- }
68
- if (Array.isArray(value)) {
69
- return this.checkArray(value);
70
- }
71
- const currentClass = value.constructor;
72
- switch (currentClass) {
73
- case Date:
74
- case RegExp:
75
- case Number:
76
- case Boolean:
77
- case String:
78
- case BigInt:
79
- return true;
80
- case Object:
81
- case undefined:
82
- return this.checkPlainObject(value);
83
- case Error:
84
- case EvalError:
85
- case RangeError:
86
- case ReferenceError:
87
- case SyntaxError:
88
- case TypeError:
89
- case URIError:
90
- return this.checkError(value as unknown as Error);
91
- default:
92
- break;
93
- }
94
- const currentFeatures = this.features;
95
- // Promises
96
- if (
97
- (currentFeatures & Feature.Promise)
98
- && (currentClass === Promise || value instanceof Promise)
99
- ) {
100
- return this.mode === 'async' || this.mode === 'stream';
101
- }
102
- // Typed Arrays
103
- if (currentFeatures & Feature.TypedArray) {
104
- switch (currentClass) {
105
- case ArrayBuffer:
106
- case Int8Array:
107
- case Int16Array:
108
- case Int32Array:
109
- case Uint8Array:
110
- case Uint16Array:
111
- case Uint32Array:
112
- case Uint8ClampedArray:
113
- case Float32Array:
114
- case Float64Array:
115
- case DataView:
116
- return true;
117
- default:
118
- break;
119
- }
120
- }
121
- // BigInt Typed Arrays
122
- if ((currentFeatures & BIGINT_FLAG) === BIGINT_FLAG) {
123
- switch (currentClass) {
124
- case BigInt64Array:
125
- case BigUint64Array:
126
- return true;
127
- default:
128
- break;
129
- }
130
- }
131
- // ES Collection
132
- if (currentFeatures & Feature.Map && currentClass === Map) {
133
- return true;
134
- }
135
- if (currentFeatures & Feature.Set && currentClass === Set) {
136
- return true;
137
- }
138
- // Web APIs
139
- if (currentFeatures & Feature.WebAPI) {
140
- switch (currentClass) {
141
- case (typeof URL !== 'undefined' ? URL : UNIVERSAL_SENTINEL):
142
- case (typeof URLSearchParams !== 'undefined' ? URLSearchParams : UNIVERSAL_SENTINEL):
143
- case (typeof Headers !== 'undefined' ? Headers : UNIVERSAL_SENTINEL):
144
- case (typeof FormData !== 'undefined' ? FormData : UNIVERSAL_SENTINEL):
145
- case (typeof Event !== 'undefined' ? Event : UNIVERSAL_SENTINEL):
146
- case (typeof CustomEvent !== 'undefined' ? CustomEvent : UNIVERSAL_SENTINEL):
147
- case (typeof DOMException !== 'undefined' ? DOMException : UNIVERSAL_SENTINEL):
148
- return true;
149
- case (typeof Request !== 'undefined' ? Request : UNIVERSAL_SENTINEL):
150
- case (typeof Response !== 'undefined' ? Response : UNIVERSAL_SENTINEL):
151
- return this.mode === 'async' || this.mode === 'stream';
152
- case (typeof Blob !== 'undefined' ? Blob : UNIVERSAL_SENTINEL):
153
- case (typeof File !== 'undefined' ? File : UNIVERSAL_SENTINEL):
154
- return this.mode === 'async';
155
- case (typeof ReadableStream !== 'undefined' ? ReadableStream : UNIVERSAL_SENTINEL):
156
- return this.mode === 'stream';
157
- default:
158
- break;
159
- }
160
- }
161
- const parsed = this.checkPlugin(value);
162
- if (parsed) {
163
- return parsed;
164
- }
165
- if (
166
- (currentFeatures & Feature.AggregateError)
167
- && typeof AggregateError !== 'undefined'
168
- && (currentClass === AggregateError || value instanceof AggregateError)
169
- ) {
170
- return true;
171
- }
172
- // Slow path. We only need to handle Errors and Iterators
173
- // since they have very broad implementations.
174
- if (value instanceof Error) {
175
- return true;
176
- }
177
- // Generator functions don't have a global constructor
178
- // despite existing
179
- if (currentFeatures & Feature.Symbol && Symbol.iterator in value) {
180
- return true;
181
- }
182
- return false;
183
- }
184
-
185
- check(value: unknown): boolean {
186
- switch (typeof value) {
187
- case 'string':
188
- case 'number':
189
- case 'boolean':
190
- case 'undefined':
191
- return true;
192
- case 'bigint':
193
- return !!(this.features & Feature.BigInt);
194
- case 'object':
195
- return this.checkObject(value);
196
- case 'symbol':
197
- return !!(this.features & Feature.Symbol) && value in INV_SYMBOL_REF;
198
- case 'function':
199
- return hasReferenceID(value);
200
- default:
201
- return false;
202
- }
203
- }
204
- }
205
-
206
- export function isSerializable<T>(value: T, options: IsSerializableOptions): boolean {
207
- return new IsSerializableContext(options).check(value);
208
- }