FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
allproxy/client/src/store/APFileSystem.ts at master · allproxy/allproxy · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
allproxy
/
allproxy
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
92
Code
Issues
2
Pull requests
6
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
allproxy
/
client
/
src
/
store
/
APFileSystem.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
267 lines (240 loc) · 9.68 KB
Breadcrumbs
allproxy
/
client
/
src
/
store
/
APFileSystem.ts
Copy path
File metadata and controls
267 lines (240 loc) · 9.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import
{
Socket
}
from
"socket.io-client"
;
import
FS
from
'@isomorphic-git/lightning-fs'
;
import
{
urlPathStore
}
from
"./UrlPathStore"
;
import
{
defaultScript
,
jsonLogStore
,
setDefaultScript
}
from
"./JSONLogStore"
;
import
{
sessionStore
}
from
"./SessionStore"
;
const
CHUNKSIZE
=
500000
;
const
defaultFsType
:
'browserFs'
|
'serverFs'
=
!
urlPathStore
.
isLocalhost
(
)
||
urlPathStore
.
isGitHubPages
(
)
||
process
.
env
.
NODE_ENV
!==
"production"
?
'browserFs'
:
'serverFs'
;
const
fs
=
new
FS
(
urlPathStore
.
isLocalhost
(
)
?
'allproxy'
:
document
.
location
.
hostname
)
.
promises
;
export
async
function
initApFileSystem
(
)
{
log
(
defaultFsType
,
'initApFileSystem'
)
;
await
mkdirIfRequired
(
'/intercept'
)
;
await
mkdirIfRequired
(
'/proto'
)
;
await
mkdirIfRequired
(
'/bin'
)
;
await
mkdirIfRequired
(
'/sessions'
)
;
await
mkdirIfRequired
(
'/jsonFields'
)
;
await
mkdirIfRequired
(
'/scripts'
)
;
await
mkdirIfRequired
(
'/queries'
)
;
if
(
urlPathStore
.
isGitHubPages
(
)
&&
!
urlPathStore
.
isLocalhost
(
)
)
{
await
fetchApFileSystem
(
)
;
}
else
{
updateScript
(
)
;
}
}
async
function
mkdirIfRequired
(
dir
:
string
)
{
try
{
log
(
defaultFsType
,
'mkdirIfRequired'
,
dir
)
;
await
fs
.
mkdir
(
dir
)
;
}
catch
(
e
)
{
}
}
function
log
(
...
args
:
any
[
]
)
{
if
(
process
.
env
.
NODE_ENV
===
'development'
)
console
.
log
(
...
args
)
;
}
export
default
class
APFileSystem
{
private
socket
?:
Socket
=
undefined
;
public
setSocket
(
socket
:
Socket
)
{
this
.
socket
=
socket
;
}
public
isConnected
(
)
{
return
this
.
socket
?.
connected
;
}
// mkdir
public
async
mkdir
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
{
log
(
fsType
,
'mkdir'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
fs
.
mkdir
(
'/'
+
path
)
;
}
else
{
this
.
socket
?.
emit
(
'mkdir'
,
path
)
;
}
}
// rmdir
public
async
rmdir
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
{
log
(
fsType
,
'rmdir'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
fs
.
rmdir
(
'/'
+
path
)
;
}
else
{
this
.
socket
?.
emit
(
'rmdir'
,
path
)
;
}
}
// writeFile
public
async
writeFile
(
path
:
string
,
data
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
void
>
{
log
(
fsType
,
'writeFile'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
return
fs
.
writeFile
(
'/'
+
path
,
data
)
;
}
return
new
Promise
<
void
>
(
async
(
resolve1
)
=>
{
for
(
let
offset
=
0
;
offset
<
data
.
length
;
offset
+=
CHUNKSIZE
)
{
await
new
Promise
(
(
resolve2
)
=>
{
const
chunk
=
data
.
substring
(
offset
,
Math
.
min
(
offset
+
CHUNKSIZE
,
data
.
length
)
)
;
const
operation
=
offset
===
0
?
'writeFile'
:
'appendFile'
;
this
.
socket
?.
emit
(
operation
,
path
,
chunk
,
(
)
=>
resolve2
(
0
)
)
;
}
)
;
}
resolve1
(
)
;
}
)
;
}
// deleteFile
public
async
deleteFile
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
void
>
{
log
(
fsType
,
'deleteFile'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
return
fs
.
unlink
(
'/'
+
path
)
;
}
return
new
Promise
<
void
>
(
(
resolve
)
=>
{
this
.
socket
?.
emit
(
'deleteFile'
,
path
,
(
)
=>
{
resolve
(
)
;
}
)
;
}
)
;
}
// renameFile
public
async
renameFile
(
oldPath
:
string
,
newPath
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
void
>
{
log
(
fsType
,
'renameFile'
,
oldPath
,
newPath
)
;
if
(
fsType
===
'browserFs'
)
{
return
fs
.
rename
(
'/'
+
oldPath
,
'/'
+
newPath
)
;
}
return
new
Promise
<
void
>
(
(
resolve
)
=>
{
this
.
socket
?.
emit
(
'renameFile'
,
oldPath
,
newPath
,
(
)
=>
{
resolve
(
)
;
}
)
;
}
)
;
}
// exists
public
async
exists
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
boolean
>
{
log
(
fsType
,
'exists?'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
try
{
await
fs
.
stat
(
'/'
+
path
)
;
log
(
fsType
,
'exists - true'
,
path
)
;
return
true
;
}
catch
(
e
)
{
log
(
fsType
,
'exists - false'
,
path
)
;
return
false
;
}
}
return
new
Promise
<
boolean
>
(
(
resolve
)
=>
{
if
(
!
this
.
isConnected
(
)
)
{
resolve
(
false
)
;
}
else
{
setTimeout
(
(
)
=>
resolve
(
false
)
,
5000
)
;
this
.
socket
?.
emit
(
'exists'
,
path
,
(
exists
:
boolean
)
=>
{
log
(
fsType
,
'exists - '
+
exists
,
path
)
;
resolve
(
exists
)
;
}
)
;
}
}
)
;
}
// readdir
public
async
readDir
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
string
[
]
>
{
log
(
fsType
,
'readDir...'
,
path
)
;
if
(
fsType
===
'browserFs'
)
{
const
files
=
fs
.
readdir
(
'/'
+
path
)
;
log
(
fsType
,
'readDir'
,
path
,
files
)
;
return
files
;
}
return
new
Promise
<
string
[
]
>
(
(
resolve
)
=>
{
this
.
socket
?.
emit
(
'readDir'
,
path
,
(
files
:
string
[
]
)
=>
{
log
(
fsType
,
'readDir'
,
path
,
files
)
;
resolve
(
files
)
;
}
)
;
}
)
;
}
public
async
grepDir
(
path
:
string
,
match
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
{
file
:
string
,
match
:
string
}
[
]
>
{
const
result
:
{
file
:
string
,
match
:
string
}
[
]
=
[
]
;
if
(
fsType
===
'browserFs'
)
{
for
(
const
sessionName
of
await
this
.
readDir
(
path
)
)
{
const
matchString
=
await
sessionStore
.
searchSession
(
sessionName
,
match
)
;
if
(
matchString
!==
''
)
{
result
.
push
(
{
file
:
path
+
'/'
+
sessionName
,
match
:
matchString
}
)
;
}
}
return
result
;
}
else
{
return
new
Promise
<
{
file
:
string
,
match
:
string
}
[
]
>
(
(
resolve
)
=>
{
this
.
socket
?.
emit
(
'grepDir'
,
path
,
match
,
(
files
:
string
[
]
)
=>
{
for
(
const
file
of
files
)
{
result
.
push
(
{
file
,
match
:
''
}
)
;
}
resolve
(
result
)
;
}
)
;
}
)
;
}
}
// readFile
public
async
readFile
(
path
:
string
,
fsType
:
'browserFs'
|
'serverFs'
=
defaultFsType
)
:
Promise
<
string
>
{
if
(
fsType
===
'browserFs'
)
{
const
data
=
(
await
fs
.
readFile
(
'/'
+
path
)
)
.
toString
(
)
;
log
(
fsType
,
'readFile'
,
path
,
data
)
;
return
data
;
}
const
chunks
:
string
[
]
=
[
]
;
return
new
Promise
<
string
>
(
async
(
resolve1
)
=>
{
let
done
=
false
;
for
(
let
offset
=
0
;
!
done
;
offset
+=
CHUNKSIZE
)
{
await
new
Promise
(
(
resolve2
)
=>
{
this
.
socket
?.
emit
(
'readFile'
,
path
,
offset
,
CHUNKSIZE
,
(
chunk
:
string
,
eof
:
boolean
)
=>
{
chunks
.
push
(
chunk
)
;
done
=
eof
;
//log(fsType,'readFile', offset, chunk.length, chunks.length, eof);
resolve2
(
0
)
;
}
)
;
}
)
;
}
const
data
=
chunks
.
join
(
''
)
;
log
(
fsType
,
'readFile'
,
path
,
data
)
;
resolve1
(
data
)
;
}
)
;
}
}
async
function
updateScript
(
)
{
await
apFileSystem
.
writeFile
(
'/scripts/jsonLogScriptDefault'
,
defaultScript
)
;
if
(
!
await
apFileSystem
.
exists
(
'/scripts/jsonLogScriptVersion'
)
)
{
await
apFileSystem
.
writeFile
(
'/scripts/jsonLogScriptVersion'
,
'0'
)
;
}
const
version
=
await
apFileSystem
.
readFile
(
'/scripts/jsonLogScriptVersion'
)
;
if
(
!
await
apFileSystem
.
exists
(
'/scripts/jsonLogScript'
)
||
version
===
'0'
)
{
await
apFileSystem
.
writeFile
(
'/scripts/jsonLogScript'
,
defaultScript
)
;
}
}
async
function
fetchApFileSystem
(
)
{
const
url
=
document
.
location
.
href
.
split
(
'#'
)
[
0
]
+
'apFileSystem.json'
;
const
response
=
await
fetch
(
url
)
;
if
(
response
.
status
===
200
)
{
const
json
=
await
response
.
json
(
)
;
// jsonFields
const
fields
=
await
apFileSystem
.
readDir
(
'/jsonFields'
)
;
if
(
fields
.
length
===
0
)
{
for
(
const
field
of
json
.
jsonFields
)
{
await
apFileSystem
.
writeFile
(
'/jsonFields/'
+
field
,
field
)
;
}
await
apFileSystem
.
writeFile
(
'/briefJsonFields.json'
,
json
.
briefJsonFields
)
;
}
// scripts
if
(
!
await
apFileSystem
.
exists
(
'/scripts/method'
)
)
await
apFileSystem
.
writeFile
(
'/scripts/method'
,
json
.
method
)
;
setDefaultScript
(
json
.
jsonLogScript
)
;
updateScript
(
)
;
// Queries
const
queries
=
await
apFileSystem
.
readDir
(
'/queries'
)
;
if
(
queries
.
length
===
0
)
{
for
(
const
dir
in
json
.
queries
)
{
await
apFileSystem
.
mkdir
(
'/queries/'
+
dir
)
;
await
apFileSystem
.
writeFile
(
'/queries/'
+
dir
+
'/query.txt'
,
json
.
queries
[
dir
]
.
query
)
;
}
}
let
jsonQueries
=
[
]
;
if
(
await
apFileSystem
.
exists
(
'/jsonQueries.json'
)
)
{
jsonQueries
=
JSON
.
parse
(
await
apFileSystem
.
readFile
(
'/jsonQueries.json'
)
)
;
}
if
(
jsonQueries
.
length
===
0
)
await
apFileSystem
.
writeFile
(
'/jsonQueries.json'
,
json
.
jsonQueries
)
;
let
jsonSubQueries
=
[
]
;
if
(
await
apFileSystem
.
exists
(
'/jsonSubQueries.json'
)
)
{
jsonSubQueries
=
JSON
.
parse
(
await
apFileSystem
.
readFile
(
'/jsonSubQueries.json'
)
)
;
}
if
(
jsonSubQueries
.
length
===
0
)
await
apFileSystem
.
writeFile
(
'/jsonSubQueries.json'
,
json
.
jsonSubQueries
)
;
// Update
await
jsonLogStore
.
init
(
)
;
}
}
export
const
apFileSystem
=
new
APFileSystem
(
)
;
Back
|
FazBrowse Home
|
New Git URL