Skip to content

Commit

Permalink
fix: re-add ignore etag bool
Browse files Browse the repository at this point in the history
  • Loading branch information
aulneau authored and yknl committed Dec 7, 2020
1 parent 27f9cfb commit c29e68d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 56 deletions.
2 changes: 1 addition & 1 deletion packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
"module": "dist/storage.esm.js",
"typings": "dist/storage/src/index.d.ts",
"umd:main": "dist/storage.umd.production.js",
"unpkg": "dist/storage.cjs.production.min.js",
Expand Down
50 changes: 30 additions & 20 deletions packages/storage/src/hub.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Transaction, script, ECPair } from 'bitcoinjs-lib';
import { ECPair, script, Transaction } from 'bitcoinjs-lib';
import { TokenSigner } from 'jsontokens';
import {
randomBytes,
hashSha256Sync,
getPublicKeyFromPrivate,
ecPairToAddress,
getPublicKeyFromPrivate,
hashSha256Sync,
hexStringToECPair,
randomBytes,
} from '@stacks/encryption';

import {
Logger,
fetchPrivate,
megabytesToBytes,
BadPathError,
ConflictError,
DoesNotExist,
fetchPrivate,
GaiaHubErrorResponse,
Logger,
megabytesToBytes,
NotEnoughProofError,
PayloadTooLargeError,
ValidationError,
PreconditionFailedError,
ValidationError,
} from '@stacks/common';

/**
Expand Down Expand Up @@ -49,16 +49,19 @@ interface UploadResponse {
* @param contents
* @param hubConfig
* @param contentType
*
* @param newFile
* @param etag
* @param dangerouslyIgnoreEtag
* @ignore
*/
export async function uploadToGaiaHub(
filename: string,
contents: Blob | Buffer | ArrayBufferView | string,
hubConfig: GaiaHubConfig,
contentType: string = 'application/octet-stream',
newFile: boolean = true,
etag?: string
contentType = 'application/octet-stream',
newFile = true,
etag?: string,
dangerouslyIgnoreEtag?: boolean
): Promise<UploadResponse> {
Logger.debug(`uploadToGaiaHub: uploading ${filename} to ${hubConfig.server}`);

Expand All @@ -67,10 +70,12 @@ export async function uploadToGaiaHub(
Authorization: `bearer ${hubConfig.token}`,
};

if (newFile) {
headers['If-None-Match'] = '*';
} else if (etag) {
headers['If-Match'] = etag;
if (!dangerouslyIgnoreEtag) {
if (newFile) {
headers['If-None-Match'] = '*';
} else if (etag) {
headers['If-Match'] = etag;
}
}

const response = await fetchPrivate(
Expand All @@ -89,11 +94,13 @@ export async function uploadToGaiaHub(
);
}
const responseText = await response.text();
const responseJSON = JSON.parse(responseText);

return responseJSON;
return JSON.parse(responseText);
}

/**
* @param filename
* @param hubConfig
*/
export async function deleteFromGaiaHub(filename: string, hubConfig: GaiaHubConfig): Promise<void> {
Logger.debug(`deleteFromGaiaHub: deleting ${filename} from ${hubConfig.server}`);
const response = await fetchPrivate(
Expand Down Expand Up @@ -288,7 +295,10 @@ export async function getBlockstackErrorFromResponse(
} else if (gaiaResponse.status === 412) {
return new PreconditionFailedError(errorMsg, gaiaResponse);
} else if (gaiaResponse.status === 413) {
const maxBytes = hubConfig ? megabytesToBytes(hubConfig!.max_file_upload_size_megabytes!) : 0;
const maxBytes =
hubConfig && hubConfig.max_file_upload_size_megabytes
? megabytesToBytes(hubConfig.max_file_upload_size_megabytes)
: 0;
return new PayloadTooLargeError(errorMsg, gaiaResponse, maxBytes);
} else {
return new Error(errorMsg);
Expand Down
56 changes: 36 additions & 20 deletions packages/storage/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import {
getFullReadUrl,
uploadToGaiaHub,
connectToGaiaHub,
getBucketUrl,
GaiaHubConfig,
deleteFromGaiaHub,
GaiaHubConfig,
getBlockstackErrorFromResponse,
getBucketUrl,
getFullReadUrl,
uploadToGaiaHub,
} from './hub';

import {
signECDSA,
verifyECDSA,
eciesGetJsonStringLength,
EncryptionOptions,
getPublicKeyFromPrivate,
publicKeyToAddress,
EncryptionOptions,
signECDSA,
verifyECDSA,
} from '@stacks/encryption';

import {
InvalidStateError,
SignatureVerificationError,
BLOCKSTACK_DEFAULT_GAIA_HUB_URL,
DoesNotExist,
PayloadTooLargeError,
fetchPrivate,
GaiaHubError,
getGlobalObject,
InvalidStateError,
megabytesToBytes,
fetchPrivate,
BLOCKSTACK_DEFAULT_GAIA_HUB_URL,
PayloadTooLargeError,
SignatureVerificationError,
} from '@stacks/common';

import { FileContentLoader } from './fileContentLoader';

import { UserSession, NAME_LOOKUP_PATH, lookupProfile } from '@stacks/auth';
import { lookupProfile, NAME_LOOKUP_PATH, UserSession } from '@stacks/auth';

/**
* Specify a valid MIME type, encryption options, and whether to sign the [[UserSession.putFile]].
Expand All @@ -51,6 +51,10 @@ export interface PutFileOptions extends EncryptionOptions {
* @default true
*/
encrypt?: boolean | string;
/**
* Ignore etag for concurrency control and force file to be written.
*/
dangerouslyIgnoreEtag?: boolean;
}

const SIGNATURE_FILE_SUFFIX = '.sig';
Expand Down Expand Up @@ -465,6 +469,7 @@ export class Storage {
encrypt: true,
sign: false,
cipherTextEncoding: 'hex',
dangerouslyIgnoreEtag: false,
};
const opt = Object.assign({}, defaults, options);

Expand Down Expand Up @@ -502,11 +507,13 @@ export class Storage {

let etag: string;
let newFile = true;

const sessionData = this.userSession.store.getSessionData();
if (sessionData.etags![path]) {
newFile = false;
etag = sessionData.etags![path];

if (!opt.dangerouslyIgnoreEtag) {
if (sessionData.etags?.[path]) {
newFile = false;
etag = sessionData.etags?.[path];
}
}

let uploadFn: (hubConfig: GaiaHubConfig) => Promise<string>;
Expand All @@ -526,7 +533,15 @@ export class Storage {
uploadFn = async (hubConfig: GaiaHubConfig) => {
const writeResponse = (
await Promise.all([
uploadToGaiaHub(path, contentData, hubConfig, contentType, newFile, etag),
uploadToGaiaHub(
path,
contentData,
hubConfig,
contentType,
newFile,
etag,
opt.dangerouslyIgnoreEtag
),
uploadToGaiaHub(
`${path}${SIGNATURE_FILE_SUFFIX}`,
signatureContent,
Expand All @@ -535,7 +550,7 @@ export class Storage {
),
])
)[0];
if (writeResponse.etag) {
if (!opt.dangerouslyIgnoreEtag && writeResponse.etag) {
sessionData.etags![path] = writeResponse.etag;
this.userSession.store.setSessionData(sessionData);
}
Expand Down Expand Up @@ -576,7 +591,8 @@ export class Storage {
hubConfig,
contentType,
newFile,
etag
etag,
opt.dangerouslyIgnoreEtag
);
if (writeResponse.etag) {
sessionData.etags![path] = writeResponse.etag;
Expand Down
15 changes: 0 additions & 15 deletions packages/storage/tsdx.config.js

This file was deleted.

0 comments on commit c29e68d

Please sign in to comment.