forked from jestjs/jest
/
index.ts
145 lines (120 loc) · 3.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs';
import v8 from 'v8';
type Path = string;
// JSON and V8 serializers are both stable when it comes to compatibility. The
// current JSON specification is well defined in RFC 8259, and V8 ensures that
// the versions are compatible by encoding the serialization version in the own
// generated buffer.
const JS_TYPE = '__$t__';
const JS_VALUE = '__$v__';
const JS_VF = '__$f__';
function replacer(_key: string, value: any): any {
// NaN cannot be in a switch statement, because NaN !== NaN.
if (Number.isNaN(value)) {
return {[JS_TYPE]: 'n'};
}
switch (value) {
case undefined:
return {[JS_TYPE]: 'u'};
case +Infinity:
return {[JS_TYPE]: '+'};
case -Infinity:
return {[JS_TYPE]: '-'};
}
switch (value && value.constructor) {
case Date:
return {[JS_TYPE]: 'd', [JS_VALUE]: value.getTime()};
case RegExp:
return {[JS_TYPE]: 'r', [JS_VALUE]: value.source, [JS_VF]: value.flags};
case Set:
return {[JS_TYPE]: 's', [JS_VALUE]: Array.from(value)};
case Map:
return {[JS_TYPE]: 'm', [JS_VALUE]: Array.from(value)};
case Buffer:
return {[JS_TYPE]: 'b', [JS_VALUE]: value.toString('latin1')};
}
return value;
}
function reviver(_key: string, value: any): any {
if (!value || (typeof value !== 'object' && !value.hasOwnProperty(JS_TYPE))) {
return value;
}
switch (value[JS_TYPE]) {
case 'u':
return undefined;
case 'n':
return NaN;
case '+':
return +Infinity;
case '-':
return -Infinity;
case 'd':
return new Date(value[JS_VALUE]);
case 'r':
return new RegExp(value[JS_VALUE], value[JS_VF]);
case 's':
return new Set(value[JS_VALUE]);
case 'm':
return new Map(value[JS_VALUE]);
case 'b':
return Buffer.from(value[JS_VALUE], 'latin1');
}
return value;
}
function jsonStringify(content: unknown) {
// Not pretty, but the ES JSON spec says that "toJSON" will be called before
// getting into your replacer, so we have to remove them beforehand. See
// https://www.ecma-international.org/ecma-262/#sec-serializejsonproperty
// section 2.b for more information.
const dateToJSON = Date.prototype.toJSON;
const bufferToJSON = Buffer.prototype.toJSON;
/* eslint-disable no-extend-native */
try {
// @ts-ignore intentional removal of "toJSON" property.
Date.prototype.toJSON = undefined;
// @ts-ignore intentional removal of "toJSON" property.
Buffer.prototype.toJSON = undefined;
return JSON.stringify(content, replacer);
} finally {
Date.prototype.toJSON = dateToJSON;
Buffer.prototype.toJSON = bufferToJSON;
}
/* eslint-enable no-extend-native */
}
function jsonParse(content: string) {
return JSON.parse(content, reviver);
}
// In memory functions.
export function deserialize(buffer: Buffer): any {
return v8.deserialize
? v8.deserialize(buffer)
: jsonParse(buffer.toString('utf8'));
}
export function serialize(content: unknown): Buffer {
return v8.serialize
? v8.serialize(content)
: Buffer.from(jsonStringify(content));
}
// Synchronous filesystem functions.
export function readFileSync(filePath: Path): any {
return v8.deserialize
? v8.deserialize(fs.readFileSync(filePath))
: jsonParse(fs.readFileSync(filePath, 'utf8'));
}
export function writeFileSync(filePath: Path, content: any) {
return v8.serialize
? fs.writeFileSync(filePath, v8.serialize(content))
: fs.writeFileSync(filePath, jsonStringify(content), 'utf8');
}
export default {
deserialize,
readFileSync,
serialize,
writeFileSync,
};