File tree Expand file tree Collapse file tree 3 files changed +32
-2
lines changed Expand file tree Collapse file tree 3 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -46,3 +46,6 @@ export const CONTENT_SECURITY_POLICY_EXEMPTIONS: string | null =
46
46
47
47
// optional token for authorization/authentication of webhook calls
48
48
export const WEBHOOK_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
+ export const SESSION_FILE_PATH = process . env . CODEROAD_SESSION_FILE_PATH || null
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { WORKSPACE_ROOT } from '../../environment'
7
7
const asyncExec = promisify ( cpExec )
8
8
const asyncRemoveFile = promisify ( fs . unlink )
9
9
const asyncReadFile = promisify ( fs . readFile )
10
+ const asyncWriteFile = promisify ( fs . writeFile )
10
11
11
12
interface ExecParams {
12
13
command : string
@@ -27,5 +28,12 @@ export const removeFile = (...paths: string[]) => {
27
28
}
28
29
29
30
export const readFile = ( ...paths : string [ ] ) => {
30
- return asyncReadFile ( join ( ...paths ) )
31
+ return asyncReadFile ( join ( ...paths ) , 'utf8' )
32
+ }
33
+
34
+ export const writeFile = ( data : any , ...paths : string [ ] ) => {
35
+ const filePath = join ( ...paths )
36
+ return asyncWriteFile ( filePath , JSON . stringify ( data ) ) . catch ( ( err ) => {
37
+ console . error ( `Failed to write to ${ filePath } ` )
38
+ } )
31
39
}
Original file line number Diff line number Diff line change 1
1
import * as vscode from 'vscode'
2
+ import { readFile , writeFile } from '../node'
3
+ import { SESSION_FILE_PATH } from '../../environment'
2
4
3
5
// NOTE: localStorage is not available on client
4
6
// and must be stored in editor
@@ -19,6 +21,18 @@ class Storage<T> {
19
21
const value : string | undefined = await this . storage . get ( this . key )
20
22
if ( value ) {
21
23
return JSON . parse ( value )
24
+ } else if ( SESSION_FILE_PATH ) {
25
+ // optionally read from file as a fallback to localstorage
26
+ const sessionFile = await readFile ( SESSION_FILE_PATH )
27
+ try {
28
+ const session = JSON . parse ( sessionFile )
29
+ if ( session && session [ this . key ] ) {
30
+ // TODO: validate session
31
+ return session [ this . key ]
32
+ }
33
+ } catch ( err ) {
34
+ console . error ( `Failed to parse session file: ${ SESSION_FILE_PATH } ` )
35
+ }
22
36
}
23
37
return this . defaultValue
24
38
}
@@ -32,7 +46,12 @@ class Storage<T> {
32
46
...current ,
33
47
...value ,
34
48
} )
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
+ } )
36
55
}
37
56
public reset = ( ) => {
38
57
this . set ( this . defaultValue )
You can’t perform that action at this time.
0 commit comments