11function Env ( name ) {
22 this . name = name
3+ this . data = null
34 this . logs = [ ]
45 this . isSurge = ( ) => 'undefined' !== typeof $httpClient
56 this . isQuanX = ( ) => 'undefined' !== typeof $task
7+ this . isNode = ( ) => 'undefined' !== typeof module && ! ! module . exports
68 this . log = ( ...log ) => {
79 this . logs = [ ...this . logs , ...log ]
810 if ( log ) console . log ( log . join ( '\n' ) )
@@ -20,13 +22,35 @@ function Env(name) {
2022 this . getdata = ( key ) => {
2123 if ( this . isSurge ( ) ) return $persistentStore . read ( key )
2224 if ( this . isQuanX ( ) ) return $prefs . valueForKey ( key )
25+ if ( this . isNode ( ) ) {
26+ const datafile = 'box.dat'
27+ this . fs = this . fs ? this . fs : require ( 'fs' )
28+ if ( this . fs . existsSync ( datafile ) ) {
29+ this . data = JSON . parse ( this . fs . readFileSync ( datafile ) )
30+ return this . data [ key ]
31+ } else {
32+ return null
33+ }
34+ }
2335 }
2436 this . setdata = ( val , key ) => {
2537 if ( this . isSurge ( ) ) return $persistentStore . write ( val , key )
2638 if ( this . isQuanX ( ) ) return $prefs . setValueForKey ( val , key )
39+ if ( this . isNode ( ) ) {
40+ const datafile = 'box.dat'
41+ this . fs = this . fs ? this . fs : require ( 'fs' )
42+ if ( this . fs . existsSync ( datafile ) ) {
43+ this . data = JSON . parse ( this . fs . readFileSync ( datafile ) )
44+ this . data [ key ] = val
45+ this . fs . writeFileSync ( datafile , JSON . stringify ( this . data ) )
46+ return true
47+ } else {
48+ return false
49+ }
50+ }
2751 }
28- this . get = ( url , callback ) => this . send ( url , 'GET' , callback )
2952 this . wait = ( min , max = min ) => ( resove ) => setTimeout ( ( ) => resove ( ) , Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) )
53+ this . get = ( url , callback ) => this . send ( url , 'GET' , callback )
3054 this . post = ( url , callback ) => this . send ( url , 'POST' , callback )
3155 this . send = ( url , method , callback ) => {
3256 if ( this . isSurge ( ) ) {
@@ -49,6 +73,15 @@ function Env(name) {
4973 ( reason ) => callback ( reason . error , reason , reason )
5074 )
5175 }
76+ if ( this . isNode ( ) ) {
77+ this . request = this . request ? this . request : require ( 'request' )
78+ url . method = method
79+ url . gzip = true
80+ this . request ( url , ( err , response , body ) => {
81+ if ( response ) response . status = response . statusCode
82+ callback ( null , response , body )
83+ } )
84+ }
5285 }
53- this . done = ( value = { } ) => $done ( value )
86+ this . done = ( value = { } ) => ( ! this . isNode ( ) ? $done ( value ) : null )
5487}
0 commit comments