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 =
4646
4747// optional token for authorization/authentication of webhook calls
4848export 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'
77const asyncExec = promisify ( cpExec )
88const asyncRemoveFile = promisify ( fs . unlink )
99const asyncReadFile = promisify ( fs . readFile )
10+ const asyncWriteFile = promisify ( fs . writeFile )
1011
1112interface ExecParams {
1213 command : string
@@ -27,5 +28,12 @@ export const removeFile = (...paths: string[]) => {
2728}
2829
2930export 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+ } )
3139}
Original file line number Diff line number Diff line change 11import * as vscode from '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 const value : string | undefined = await this . storage . get ( this . key )
2022 if ( value ) {
2123 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+ }
2236 }
2337 return this . 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 public reset = ( ) => {
3857 this . set ( this . defaultValue )
You can’t perform that action at this time.
0 commit comments