Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf9ed01

Browse files
committed
fallback to file
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parentd01b0d2 commitbf9ed01

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

‎src/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ export const CONTENT_SECURITY_POLICY_EXEMPTIONS: string | null =
4646

4747
// optional token for authorization/authentication of webhook calls
4848
exportconstWEBHOOK_TOKEN=process.env.CODEROAD_WEBHOOK_TOKEN||null
49+
50+
// a path to write session state to a file. Useful for maintaining session across containers
51+
exportconstSESSION_FILE_PATH=process.env.CODEROAD_SESSION_FILE_PATH||null

‎src/services/node/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { WORKSPACE_ROOT } from '../../environment'
77
constasyncExec=promisify(cpExec)
88
constasyncRemoveFile=promisify(fs.unlink)
99
constasyncReadFile=promisify(fs.readFile)
10+
constasyncWriteFile=promisify(fs.writeFile)
1011

1112
interfaceExecParams{
1213
command:string
@@ -27,5 +28,12 @@ export const removeFile = (...paths: string[]) => {
2728
}
2829

2930
exportconstreadFile=(...paths:string[])=>{
30-
returnasyncReadFile(join(...paths))
31+
returnasyncReadFile(join(...paths),'utf8')
32+
}
33+
34+
exportconstwriteFile=(data:any, ...paths:string[])=>{
35+
constfilePath=join(...paths)
36+
returnasyncWriteFile(filePath,JSON.stringify(data)).catch((err)=>{
37+
console.error(`Failed to write to${filePath}`)
38+
})
3139
}

‎src/services/storage/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import*asvscodefrom'vscode'
2+
import{readFile,writeFile}from'../node'
3+
import{SESSION_FILE_PATH}from'../../environment'
24

35
// NOTE: localStorage is not available on client
46
// and must be stored in editor
@@ -19,6 +21,18 @@ class Storage<T> {
1921
constvalue:string|undefined=awaitthis.storage.get(this.key)
2022
if(value){
2123
returnJSON.parse(value)
24+
}elseif(SESSION_FILE_PATH){
25+
// optionally read from file as a fallback to localstorage
26+
constsessionFile=awaitreadFile(SESSION_FILE_PATH)
27+
try{
28+
constsession=JSON.parse(sessionFile)
29+
if(session&&session[this.key]){
30+
// TODO: validate session
31+
returnsession[this.key]
32+
}
33+
}catch(err){
34+
console.error(`Failed to parse session file:${SESSION_FILE_PATH}`)
35+
}
2236
}
2337
returnthis.defaultValue
2438
}
@@ -32,7 +46,12 @@ class Storage<T> {
3246
...current,
3347
...value,
3448
})
35-
this.storage.update(this.key,next)
49+
this.storage.update(this.key,next).then(()=>{
50+
// optionally write to file
51+
if(SESSION_FILE_PATH){
52+
writeFile(this.storage,SESSION_FILE_PATH)
53+
}
54+
})
3655
}
3756
publicreset=()=>{
3857
this.set(this.defaultValue)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp