Skip to content

Commit

Permalink
feat: persist etags in UserSession
Browse files Browse the repository at this point in the history
  • Loading branch information
reedrosenbluth committed Jul 8, 2020
1 parent ea01cbf commit 66ab8f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -25,7 +25,7 @@ To securely use the latest distribution of blockstack.js from a CDN, use the fol

<!-- cdn -->
```html
<script src="https://unpkg.com/blockstack@21.1.0/dist/blockstack.js" integrity="sha384-Q1+BMI0BUKW3cBc3ChRpJslh5pRxWutZjdd9MM/QKyPd0hevS2k4Sh+FstA7A6Zz" crossorigin="anonymous"></script>
<script src="https://unpkg.com/blockstack@21.1.0/dist/blockstack.js" integrity="sha384-qv8/td2FU/FhJ588BGhJ18iXM7ByyDyjnisQpXgqMQEFlvPgN11wR231w1M9fr+1" crossorigin="anonymous"></script>
```
<!-- cdnstop -->

Expand Down
9 changes: 8 additions & 1 deletion src/auth/sessionData.ts
Expand Up @@ -5,10 +5,13 @@ import { UserData } from './authApp'

const SESSION_VERSION = '1.0.0'

type EtagMap = { [key: string]: string; };

export interface SessionOptions {
coreNode?: string,
userData?: UserData,
transitKey?: string,
etags?: EtagMap,
localStorageKey?: string,
storeOptions?: {
localStorageKey?: string
Expand All @@ -27,10 +30,13 @@ export class SessionData {
// window.localStorage.setItem(BLOCKSTACK_STORAGE_LABEL, JSON.stringify(userData))
userData?: UserData

etags?: EtagMap

constructor(options: SessionOptions) {
this.version = SESSION_VERSION
this.userData = options.userData
this.transitKey = options.transitKey
this.etags = options.etags ? options.etags : {}
}

getGaiaHubConfig(): GaiaHubConfig {
Expand All @@ -48,7 +54,8 @@ export class SessionData {
const options: SessionOptions = {
coreNode: json.coreNode,
userData: json.userData,
transitKey: json.transitKey
transitKey: json.transitKey,
etags: json.etags
}
return new SessionData(options)
}
Expand Down
31 changes: 19 additions & 12 deletions src/storage/index.ts
Expand Up @@ -27,9 +27,6 @@ import { NAME_LOOKUP_PATH } from '../auth/authConstants'
import { getGlobalObject, getBlockstackErrorFromResponse, megabytesToBytes } from '../utils'
import { fetchPrivate } from '../fetchUtil'

const etags: { [key: string]: string; } = {}


export interface EncryptionOptions {
/**
* If set to `true` the data is signed using ECDSA on SHA256 hashes with the user's
Expand Down Expand Up @@ -311,7 +308,9 @@ async function getFileContents(caller: UserSession, path: string, app: string,

const etag = response.headers.get('ETag')
if (etag) {
etags[path] = etag
const sessionData = caller.store.getSessionData()
sessionData.etags[path] = etag
caller.store.setSessionData(sessionData)
}
if (forceText || contentType === null
|| contentType.startsWith('text')
Expand Down Expand Up @@ -755,9 +754,10 @@ export async function putFile(
let etag: string
let newFile = true

if (etags[path]) {
const sessionData = caller.store.getSessionData();
if (sessionData.etags[path]) {
newFile = false
etag = etags[path]
etag = sessionData.etags[path]
}

let uploadFn: (hubConfig: GaiaHubConfig) => Promise<string>
Expand All @@ -781,7 +781,8 @@ export async function putFile(
signatureContent, hubConfig, 'application/json')
]))[0]
if (writeResponse.etag) {
etags[path] = writeResponse.etag
sessionData.etags[path] = writeResponse.etag;
caller.store.setSessionData(sessionData);
}
return writeResponse.publicURL
}
Expand Down Expand Up @@ -818,7 +819,8 @@ export async function putFile(
path, contentForUpload, hubConfig, contentType, newFile, etag
)
if (writeResponse.etag) {
etags[path] = writeResponse.etag
sessionData.etags[path] = writeResponse.etag;
caller.store.setSessionData(sessionData);
}
return writeResponse.publicURL
}
Expand Down Expand Up @@ -857,26 +859,31 @@ export async function deleteFile(
) {
const gaiaHubConfig = await caller.getOrSetLocalGaiaHubConnection()
const opts = Object.assign({}, options)
const sessionData = caller.store.getSessionData();
if (opts.wasSigned) {
// If signed, delete both the content file and the .sig file
try {
await deleteFromGaiaHub(path, gaiaHubConfig)
await deleteFromGaiaHub(`${path}${SIGNATURE_FILE_SUFFIX}`, gaiaHubConfig)
delete etags[path];
delete sessionData.etags[path];
caller.store.setSessionData(sessionData);
} catch (error) {
const freshHubConfig = await caller.setLocalGaiaHubConnection()
await deleteFromGaiaHub(path, freshHubConfig)
await deleteFromGaiaHub(`${path}${SIGNATURE_FILE_SUFFIX}`, gaiaHubConfig)
delete etags[path];
delete sessionData.etags[path];
caller.store.setSessionData(sessionData);
}
} else {
try {
await deleteFromGaiaHub(path, gaiaHubConfig)
delete etags[path];
delete sessionData.etags[path];
caller.store.setSessionData(sessionData);
} catch (error) {
const freshHubConfig = await caller.setLocalGaiaHubConnection()
await deleteFromGaiaHub(path, freshHubConfig)
delete etags[path];
delete sessionData.etags[path];
caller.store.setSessionData(sessionData);
}
}
}
Expand Down

0 comments on commit 66ab8f1

Please sign in to comment.