Skip to content

Commit b472b54

Browse files
committed
fallback to file
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent 060ad10 commit b472b54

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
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

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
const asyncExec = promisify(cpExec)
88
const asyncRemoveFile = promisify(fs.unlink)
99
const asyncReadFile = promisify(fs.readFile)
10+
const asyncWriteFile = promisify(fs.writeFile)
1011

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

2930
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+
})
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 * 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)

0 commit comments

Comments
 (0)