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