33 *
44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
6- *
7- * @flow
86 */
97
10- 'use strict' ;
11-
128import fs from 'fs' ;
139import v8 from 'v8' ;
1410
15- import type { Path } from 'types/Config' ;
11+ type Path = string ;
1612
1713// JSON and V8 serializers are both stable when it comes to compatibility. The
1814// current JSON specification is well defined in RFC 8259, and V8 ensures that
@@ -23,7 +19,7 @@ const JS_TYPE = '__$t__';
2319const JS_VALUE = '__$v__' ;
2420const JS_VF = '__$f__' ;
2521
26- function replacer ( key : string , value : any ) : any {
22+ function replacer ( _key : string , value : any ) : any {
2723 // NaN cannot be in a switch statement, because NaN !== NaN.
2824 if ( Number . isNaN ( value ) ) {
2925 return { [ JS_TYPE ] : 'n' } ;
@@ -32,72 +28,58 @@ function replacer(key: string, value: any): any {
3228 switch ( value ) {
3329 case undefined :
3430 return { [ JS_TYPE ] : 'u' } ;
35-
3631 case + Infinity :
3732 return { [ JS_TYPE ] : '+' } ;
38-
3933 case - Infinity :
4034 return { [ JS_TYPE ] : '-' } ;
4135 }
4236
4337 switch ( value && value . constructor ) {
4438 case Date :
4539 return { [ JS_TYPE ] : 'd' , [ JS_VALUE ] : value . getTime ( ) } ;
46-
4740 case RegExp :
4841 return { [ JS_TYPE ] : 'r' , [ JS_VALUE ] : value . source , [ JS_VF ] : value . flags } ;
49-
5042 case Set :
5143 return { [ JS_TYPE ] : 's' , [ JS_VALUE ] : Array . from ( value ) } ;
52-
5344 case Map :
5445 return { [ JS_TYPE ] : 'm' , [ JS_VALUE ] : Array . from ( value ) } ;
55-
5646 case Buffer :
5747 return { [ JS_TYPE ] : 'b' , [ JS_VALUE ] : value . toString ( 'latin1' ) } ;
5848 }
5949
6050 return value ;
6151}
6252
63- function reviver ( key : string , value : any ) : any {
53+ function reviver ( _key : string , value : any ) : any {
6454 if ( ! value || ( typeof value !== 'object' && ! value . hasOwnProperty ( JS_TYPE ) ) ) {
6555 return value ;
6656 }
6757
6858 switch ( value [ JS_TYPE ] ) {
6959 case 'u' :
7060 return undefined ;
71-
7261 case 'n' :
7362 return NaN ;
74-
7563 case '+' :
7664 return + Infinity ;
77-
7865 case '-' :
7966 return - Infinity ;
80-
8167 case 'd' :
8268 return new Date ( value [ JS_VALUE ] ) ;
83-
8469 case 'r' :
8570 return new RegExp ( value [ JS_VALUE ] , value [ JS_VF ] ) ;
86-
8771 case 's' :
8872 return new Set ( value [ JS_VALUE ] ) ;
89-
9073 case 'm' :
9174 return new Map ( value [ JS_VALUE ] ) ;
92-
9375 case 'b' :
9476 return Buffer . from ( value [ JS_VALUE ] , 'latin1' ) ;
9577 }
9678
9779 return value ;
9880}
9981
100- function jsonStringify ( content ) {
82+ function jsonStringify ( content : unknown ) {
10183 // Not pretty, but the ES JSON spec says that "toJSON" will be called before
10284 // getting into your replacer, so we have to remove them beforehand. See
10385 // https://www.ecma-international.org/ecma-262/#sec-serializejsonproperty
@@ -109,37 +91,33 @@ function jsonStringify(content) {
10991 /* eslint-disable no-extend-native */
11092
11193 try {
112- // $FlowFixMe: intentional removal of "toJSON" property.
94+ // @ts -ignore intentional removal of "toJSON" property.
11395 Date . prototype . toJSON = undefined ;
114- // $FlowFixMe: intentional removal of "toJSON" property.
96+ // @ts -ignore intentional removal of "toJSON" property.
11597 Buffer . prototype . toJSON = undefined ;
11698
11799 return JSON . stringify ( content , replacer ) ;
118100 } finally {
119- // $FlowFixMe: intentional assignment of "toJSON" property.
120101 Date . prototype . toJSON = dateToJSON ;
121- // $FlowFixMe: intentional assignment of "toJSON" property.
122102 Buffer . prototype . toJSON = bufferToJSON ;
123103 }
124104
125105 /* eslint-enable no-extend-native */
126106}
127107
128- function jsonParse ( content ) {
108+ function jsonParse ( content : string ) {
129109 return JSON . parse ( content , reviver ) ;
130110}
131111
132112// In memory functions.
133113
134114export function deserialize ( buffer : Buffer ) : any {
135- // $FlowFixMe - Node 8+ only
136115 return v8 . deserialize
137116 ? v8 . deserialize ( buffer )
138117 : jsonParse ( buffer . toString ( 'utf8' ) ) ;
139118}
140119
141- export function serialize ( content : any ) : Buffer {
142- // $FlowFixMe - Node 8+ only
120+ export function serialize ( content : unknown ) : Buffer {
143121 return v8 . serialize
144122 ? v8 . serialize ( content )
145123 : Buffer . from ( jsonStringify ( content ) ) ;
@@ -148,14 +126,12 @@ export function serialize(content: any): Buffer {
148126// Synchronous filesystem functions.
149127
150128export function readFileSync ( filePath : Path ) : any {
151- // $FlowFixMe - Node 8+ only
152129 return v8 . deserialize
153130 ? v8 . deserialize ( fs . readFileSync ( filePath ) )
154131 : jsonParse ( fs . readFileSync ( filePath , 'utf8' ) ) ;
155132}
156133
157134export function writeFileSync ( filePath : Path , content : any ) {
158- // $FlowFixMe - Node 8+ only
159135 return v8 . serialize
160136 ? fs . writeFileSync ( filePath , v8 . serialize ( content ) )
161137 : fs . writeFileSync ( filePath , jsonStringify ( content ) , 'utf8' ) ;
0 commit comments