Comparing version 0.4.0-alpha.15 to 0.4.0-alpha.16
@@ -5,18 +5,3 @@ /** | ||
* - MDN | ||
* - ESBuild | ||
*/ | ||
export type Version = [major: number, minor: number, patch: number]; | ||
export interface PlatformVersion { | ||
es: Version; | ||
chrome?: Version; | ||
edge?: Version; | ||
safari?: Version; | ||
firefox?: Version; | ||
opera?: Version; | ||
ios?: Version; | ||
samsung?: Version; | ||
deno?: Version; | ||
node?: Version; | ||
} | ||
export type Platform = keyof PlatformVersion; | ||
export declare const enum Feature { | ||
@@ -27,12 +12,12 @@ AggregateError = 1, | ||
BigInt = 8, | ||
Map = 16, | ||
MethodShorthand = 32, | ||
ObjectAssign = 64, | ||
Promise = 128, | ||
Set = 256, | ||
SymbolIterator = 512, | ||
TypedArray = 1024, | ||
BigIntTypedArray = 2048 | ||
ErrorPrototypeStack = 16, | ||
Map = 32, | ||
MethodShorthand = 64, | ||
ObjectAssign = 128, | ||
Promise = 256, | ||
Set = 512, | ||
SymbolIterator = 1024, | ||
TypedArray = 2048, | ||
BigIntTypedArray = 4096 | ||
} | ||
export type Target = [platform: Platform, version: Version]; | ||
export declare function parseTargets(targets: string | string[]): number; | ||
export declare const ALL_ENABLED = 8191; |
@@ -35,3 +35,3 @@ interface IndexAssignment { | ||
export interface Options { | ||
target: string | string[]; | ||
disabledFeatures: number; | ||
} | ||
@@ -38,0 +38,0 @@ export declare function createParserContext(options?: Partial<Options>): ParserContext; |
@@ -0,4 +1,5 @@ | ||
import { Feature } from './compat'; | ||
import { Options } from './context'; | ||
import { AsyncServerValue, PrimitiveValue, ServerValue, CommonServerValue, SemiPrimitiveValue, ErrorValue } from './types'; | ||
export { AsyncServerValue, ServerValue, PrimitiveValue, CommonServerValue, SemiPrimitiveValue, ErrorValue, }; | ||
export { AsyncServerValue, ServerValue, PrimitiveValue, CommonServerValue, SemiPrimitiveValue, ErrorValue, Feature, }; | ||
export declare function serialize<T extends ServerValue>(source: T, options?: Partial<Options>): string; | ||
@@ -5,0 +6,0 @@ export declare function serializeAsync<T extends AsyncServerValue>(source: T, options?: Partial<Options>): Promise<string>; |
@@ -0,5 +1,6 @@ | ||
import { ParserContext } from '../context'; | ||
import { AsyncServerValue, ErrorValue } from '../types'; | ||
export declare function getErrorConstructor(error: ErrorValue): "EvalError" | "RangeError" | "ReferenceError" | "SyntaxError" | "TypeError" | "URIError" | "Error"; | ||
export declare function getErrorOptions(error: Error): Record<string, any> | undefined; | ||
export declare function getErrorOptions(ctx: ParserContext, error: Error): Record<string, any> | undefined; | ||
export declare function getIterableOptions(obj: Iterable<any>): Record<string, unknown> | undefined; | ||
export declare function isIterable(value: unknown): value is Iterable<AsyncServerValue>; |
{ | ||
"name": "seroval", | ||
"type": "module", | ||
"version": "0.4.0-alpha.15", | ||
"version": "0.4.0-alpha.16", | ||
"files": [ | ||
@@ -67,3 +67,3 @@ "dist", | ||
}, | ||
"gitHead": "a9751ea8b416dc40622219d6d0fc4fba508fec7d" | ||
"gitHead": "ebed12239c5985c00e66f984da6b4a48d7519706" | ||
} |
@@ -6,35 +6,4 @@ /* eslint-disable guard-for-in */ | ||
* - MDN | ||
* - ESBuild | ||
*/ | ||
export type Version = [major: number, minor: number, patch: number]; | ||
function compareVersion(left: Version, right: Version) { | ||
let diff = left[0] - right[0]; | ||
if (diff === 0) { | ||
diff = left[1] - right[1]; | ||
} | ||
if (diff === 0) { | ||
diff = left[2] - right[2]; | ||
} | ||
return diff; | ||
} | ||
export interface PlatformVersion { | ||
es: Version; | ||
// desktop | ||
chrome?: Version; | ||
edge?: Version; | ||
safari?: Version; | ||
firefox?: Version; | ||
opera?: Version; | ||
// mobile | ||
ios?: Version; | ||
samsung?: Version; | ||
// js runtimes | ||
deno?: Version; | ||
node?: Version; | ||
} | ||
export type Platform = keyof PlatformVersion; | ||
export const enum Feature { | ||
@@ -45,239 +14,13 @@ AggregateError = 0x01, | ||
BigInt = 0x08, | ||
Map = 0x10, | ||
MethodShorthand = 0x20, | ||
ObjectAssign = 0x40, | ||
Promise = 0x80, | ||
Set = 0x100, | ||
SymbolIterator = 0x200, | ||
TypedArray = 0x400, | ||
BigIntTypedArray = 0x800, | ||
ErrorPrototypeStack = 0x10, | ||
Map = 0x20, | ||
MethodShorthand = 0x40, | ||
ObjectAssign = 0x80, | ||
Promise = 0x100, | ||
Set = 0x200, | ||
SymbolIterator = 0x400, | ||
TypedArray = 0x800, | ||
BigIntTypedArray = 0x1000, | ||
} | ||
type VersionTable = { | ||
[key in Feature]: PlatformVersion; | ||
} | ||
const VERSION_TABLE: VersionTable = { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility | ||
[Feature.AggregateError]: { | ||
es: [2021, 0, 0], | ||
chrome: [85, 0, 0], | ||
edge: [85, 0, 0], | ||
firefox: [79, 0, 0], | ||
opera: [71, 0, 0], | ||
safari: [14, 0, 0], | ||
ios: [14, 0, 0], | ||
samsung: [14, 0, 0], | ||
deno: [1, 2, 0], | ||
node: [15, 0, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility | ||
[Feature.ArrayPrototypeValues]: { | ||
es: [6, 0, 0], | ||
chrome: [66, 0, 0], | ||
edge: [14, 0, 0], | ||
firefox: [60, 0, 0], | ||
opera: [53, 0, 0], | ||
safari: [9, 0, 0], | ||
ios: [9, 0, 0], | ||
samsung: [9, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [10, 9, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility | ||
[Feature.ArrowFunction]: { | ||
es: [6, 0, 0], | ||
chrome: [45, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [24, 0, 0], | ||
opera: [32, 0, 0], | ||
safari: [10, 0, 0], | ||
ios: [10, 0, 0], | ||
samsung: [5, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [4, 0, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility | ||
[Feature.BigInt]: { | ||
es: [2020, 0, 0], | ||
chrome: [67, 0, 0], | ||
edge: [79, 0, 0], | ||
firefox: [68, 0, 0], | ||
opera: [54, 0, 0], | ||
safari: [14, 0, 0], | ||
ios: [14, 0, 0], | ||
samsung: [9, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [10, 4, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility | ||
[Feature.Map]: { | ||
es: [6, 0, 0], | ||
chrome: [38, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [13, 0, 0], | ||
opera: [25, 0, 0], | ||
safari: [8, 0, 0], | ||
ios: [8, 0, 0], | ||
samsung: [3, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [0, 12, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility | ||
[Feature.MethodShorthand]: { | ||
es: [6, 0, 0], | ||
chrome: [39, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [34, 0, 0], | ||
opera: [26, 0, 0], | ||
safari: [9, 0, 0], | ||
ios: [9, 0, 0], | ||
samsung: [4, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [4, 0, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility | ||
[Feature.ObjectAssign]: { | ||
es: [6, 0, 0], | ||
chrome: [45, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [34, 0, 0], | ||
opera: [32, 0, 0], | ||
safari: [9, 0, 0], | ||
ios: [9, 0, 0], | ||
samsung: [5, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [4, 0, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility | ||
[Feature.Promise]: { | ||
es: [6, 0, 0], | ||
chrome: [32, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [29, 0, 0], | ||
opera: [19, 0, 0], | ||
safari: [8, 0, 0], | ||
ios: [8, 0, 0], | ||
samsung: [2, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [0, 12, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility | ||
[Feature.Set]: { | ||
es: [6, 0, 0], | ||
chrome: [38, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [13, 0, 0], | ||
opera: [25, 0, 0], | ||
safari: [8, 0, 0], | ||
ios: [8, 0, 0], | ||
samsung: [3, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [0, 12, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility | ||
[Feature.SymbolIterator]: { | ||
es: [6, 0, 0], | ||
chrome: [43, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [36, 0, 0], | ||
opera: [30, 0, 0], | ||
safari: [10, 0, 0], | ||
ios: [10, 0, 0], | ||
samsung: [4, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [0, 12, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility | ||
[Feature.TypedArray]: { | ||
es: [6, 0, 0], | ||
chrome: [7, 0, 0], | ||
edge: [12, 0, 0], | ||
firefox: [4, 0, 0], | ||
opera: [11, 6, 0], | ||
safari: [5, 1, 0], | ||
ios: [4, 2, 0], | ||
samsung: [1, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [0, 10, 0], | ||
}, | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility | ||
[Feature.BigIntTypedArray]: { | ||
es: [2020, 0, 0], | ||
chrome: [67, 0, 0], | ||
edge: [79, 0, 0], | ||
firefox: [68, 0, 0], | ||
opera: [54, 6, 0], | ||
safari: [15, 1, 0], | ||
ios: [15, 2, 0], | ||
samsung: [9, 0, 0], | ||
deno: [1, 0, 0], | ||
node: [10, 4, 0], | ||
}, | ||
}; | ||
const platforms: Platform[] = [ | ||
'chrome', | ||
'deno', | ||
'edge', | ||
'es', | ||
'firefox', | ||
'ios', | ||
'node', | ||
'opera', | ||
'safari', | ||
'samsung', | ||
]; | ||
export type Target = [platform: Platform, version: Version]; | ||
function getPlatform(target: string): Platform { | ||
let platform: string; | ||
for (let i = 0, len = platforms.length; i < len; i++) { | ||
platform = target.substring(0, platforms[i].length); | ||
if (platform === platforms[i]) { | ||
return platform; | ||
} | ||
} | ||
throw new Error(`Invalid target "${target}"`); | ||
} | ||
function parseTarget(target: string): Target { | ||
const platform = getPlatform(target); | ||
const version = target.substring(platform.length); | ||
const [major, minor = '0', patch = '0'] = version.split('.'); | ||
return [platform, [Number(major), Number(minor), Number(patch)]]; | ||
} | ||
function getTargetVersions(targets: string | string[]): Target[] { | ||
if (Array.isArray(targets)) { | ||
const versions: Target[] = []; | ||
for (let i = 0, len = targets.length; i < len; i++) { | ||
versions.push(parseTarget(targets[i])); | ||
} | ||
return versions; | ||
} | ||
return [parseTarget(targets)]; | ||
} | ||
export function parseTargets(targets: string | string[]): number { | ||
const parsed = getTargetVersions(targets); | ||
let flags = 0; | ||
let base: PlatformVersion; | ||
let flag: boolean; | ||
let baseVersion: Version | undefined; | ||
for (const key of Object.keys(VERSION_TABLE)) { | ||
base = VERSION_TABLE[key as unknown as Feature]; | ||
flag = true; | ||
for (const [platform, version] of parsed) { | ||
baseVersion = base[platform]; | ||
flag = flag && !!baseVersion && (compareVersion(baseVersion, version) <= 0); | ||
} | ||
if (flag) { | ||
flags |= Number(key); | ||
} | ||
} | ||
return flags; | ||
} | ||
export const ALL_ENABLED = 0x1FFF; |
@@ -1,2 +0,2 @@ | ||
import { parseTargets } from './compat'; | ||
import { ALL_ENABLED } from './compat'; | ||
import getIdentifier from './get-identifier'; | ||
@@ -53,7 +53,7 @@ | ||
export interface Options { | ||
target: string | string[]; | ||
disabledFeatures: number; | ||
} | ||
const DEFAULT_OPTIONS: Options = { | ||
target: 'es2023', | ||
disabledFeatures: 0, | ||
}; | ||
@@ -67,3 +67,3 @@ | ||
refs: new Map(), | ||
features: parseTargets(result.target), | ||
features: ALL_ENABLED ^ result.disabledFeatures, | ||
}; | ||
@@ -70,0 +70,0 @@ } |
@@ -30,2 +30,3 @@ /* eslint-disable no-await-in-loop */ | ||
ErrorValue, | ||
Feature, | ||
}; | ||
@@ -32,0 +33,0 @@ |
@@ -290,3 +290,3 @@ /* eslint-disable no-await-in-loop */ | ||
): Promise<SerovalAggregateErrorNode> { | ||
const options = getErrorOptions(current); | ||
const options = getErrorOptions(ctx, current); | ||
const optionsNode = options | ||
@@ -313,3 +313,3 @@ ? await this.generateProperties(ctx, options) | ||
): Promise<SerovalErrorNode> { | ||
const options = getErrorOptions(current); | ||
const options = getErrorOptions(ctx, current); | ||
const optionsNode = options | ||
@@ -316,0 +316,0 @@ ? await this.generateProperties(ctx, options) |
@@ -0,1 +1,3 @@ | ||
import { Feature } from '../compat'; | ||
import { ParserContext } from '../context'; | ||
import { | ||
@@ -29,2 +31,3 @@ AsyncServerValue, | ||
export function getErrorOptions( | ||
ctx: ParserContext, | ||
error: Error, | ||
@@ -45,4 +48,11 @@ ) { | ||
if (name !== 'name' && name !== 'message') { | ||
options = options || {}; | ||
options[name] = error[name as keyof Error]; | ||
if (name === 'stack') { | ||
if (ctx.features & Feature.ErrorPrototypeStack) { | ||
options = options || {}; | ||
options[name] = error[name as keyof Error]; | ||
} | ||
} else { | ||
options = options || {}; | ||
options[name] = error[name as keyof Error]; | ||
} | ||
} | ||
@@ -49,0 +59,0 @@ } |
@@ -249,3 +249,3 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ | ||
): SerovalAggregateErrorNode { | ||
const options = getErrorOptions(current); | ||
const options = getErrorOptions(ctx, current); | ||
const optionsNode = options | ||
@@ -272,3 +272,3 @@ ? this.generateProperties(ctx, options) | ||
): SerovalErrorNode { | ||
const options = getErrorOptions(current); | ||
const options = getErrorOptions(ctx, current); | ||
const optionsNode = options | ||
@@ -275,0 +275,0 @@ ? this.generateProperties(ctx, options) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
399459
5602