Skip to content

fallback session state to file #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fallback to file
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Nov 21, 2021
commit bf9ed01adf414c4f0bf4ff7cc2be61bb80a85b11
3 changes: 3 additions & 0 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ export const CONTENT_SECURITY_POLICY_EXEMPTIONS: string | null =

// optional token for authorization/authentication of webhook calls
export const WEBHOOK_TOKEN = process.env.CODEROAD_WEBHOOK_TOKEN || null

// a path to write session state to a file. Useful for maintaining session across containers
export const SESSION_FILE_PATH = process.env.CODEROAD_SESSION_FILE_PATH || null
10 changes: 9 additions & 1 deletion src/services/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { WORKSPACE_ROOT } from '../../environment'
const asyncExec = promisify(cpExec)
const asyncRemoveFile = promisify(fs.unlink)
const asyncReadFile = promisify(fs.readFile)
const asyncWriteFile = promisify(fs.writeFile)

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

export const readFile = (...paths: string[]) => {
return asyncReadFile(join(...paths))
return asyncReadFile(join(...paths), 'utf8')
}

export const writeFile = (data: any, ...paths: string[]) => {
const filePath = join(...paths)
return asyncWriteFile(filePath, JSON.stringify(data)).catch((err) => {
console.error(`Failed to write to ${filePath}`)
})
}
21 changes: 20 additions & 1 deletion src/services/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as vscode from 'vscode'
import { readFile, writeFile } from '../node'
import { SESSION_FILE_PATH } from '../../environment'

// NOTE: localStorage is not available on client
// and must be stored in editor
Expand All @@ -19,6 +21,18 @@ class Storage<T> {
const value: string | undefined = await this.storage.get(this.key)
if (value) {
return JSON.parse(value)
} else if (SESSION_FILE_PATH) {
// optionally read from file as a fallback to localstorage
const sessionFile = await readFile(SESSION_FILE_PATH)
try {
const session = JSON.parse(sessionFile)
if (session && session[this.key]) {
// TODO: validate session
return session[this.key]
}
} catch (err) {
console.error(`Failed to parse session file: ${SESSION_FILE_PATH}`)
}
}
return this.defaultValue
}
Expand All @@ -32,7 +46,12 @@ class Storage<T> {
...current,
...value,
})
this.storage.update(this.key, next)
this.storage.update(this.key, next).then(() => {
// optionally write to file
if (SESSION_FILE_PATH) {
writeFile(this.storage, SESSION_FILE_PATH)
}
})
}
public reset = () => {
this.set(this.defaultValue)
Expand Down