From c29e68dff37e7691c45b0f624108712690596a3d Mon Sep 17 00:00:00 2001 From: Thomas Osmonson Date: Sun, 6 Dec 2020 10:33:17 -0600 Subject: [PATCH] fix: re-add ignore etag bool --- packages/storage/package.json | 2 +- packages/storage/src/hub.ts | 50 +++++++++++++++++------------ packages/storage/src/storage.ts | 56 +++++++++++++++++++++------------ packages/storage/tsdx.config.js | 15 --------- 4 files changed, 67 insertions(+), 56 deletions(-) delete mode 100644 packages/storage/tsdx.config.js diff --git a/packages/storage/package.json b/packages/storage/package.json index c7147eb22..3443cf312 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -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", diff --git a/packages/storage/src/hub.ts b/packages/storage/src/hub.ts index 89482dd3d..f0a819d87 100644 --- a/packages/storage/src/hub.ts +++ b/packages/storage/src/hub.ts @@ -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'; /** @@ -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 { Logger.debug(`uploadToGaiaHub: uploading ${filename} to ${hubConfig.server}`); @@ -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( @@ -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 { Logger.debug(`deleteFromGaiaHub: deleting ${filename} from ${hubConfig.server}`); const response = await fetchPrivate( @@ -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); diff --git a/packages/storage/src/storage.ts b/packages/storage/src/storage.ts index 1131c3fe6..5c1d6b1d0 100644 --- a/packages/storage/src/storage.ts +++ b/packages/storage/src/storage.ts @@ -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]]. @@ -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'; @@ -465,6 +469,7 @@ export class Storage { encrypt: true, sign: false, cipherTextEncoding: 'hex', + dangerouslyIgnoreEtag: false, }; const opt = Object.assign({}, defaults, options); @@ -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; @@ -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, @@ -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); } @@ -576,7 +591,8 @@ export class Storage { hubConfig, contentType, newFile, - etag + etag, + opt.dangerouslyIgnoreEtag ); if (writeResponse.etag) { sessionData.etags![path] = writeResponse.etag; diff --git a/packages/storage/tsdx.config.js b/packages/storage/tsdx.config.js deleted file mode 100644 index d5839e6ad..000000000 --- a/packages/storage/tsdx.config.js +++ /dev/null @@ -1,15 +0,0 @@ -module.exports = { - rollup(config, options) { - if (options.format === 'esm') { - config.output = { - ...config.output, - dir: 'dist/', - entryFileNames: '[name].esm.js', - preserveModules: true, - preserveModulesRoot: 'src', - }; - delete config.output.file; - } - return config; - }, -};