@stacks/storage 4.0.3-beta.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stacks/storage",
3
- "version": "4.0.3-beta.0",
3
+ "version": "4.1.0",
4
4
  "description": "Stacks storage library",
5
5
  "author": "yknl <yukanliao@gmail.com>",
6
6
  "homepage": "https://blockstack.org",
@@ -32,12 +32,13 @@
32
32
  "url": "https://github.com/blockstack/blockstack.js/issues"
33
33
  },
34
34
  "dependencies": {
35
- "@stacks/auth": "^4.0.3-beta.0",
36
- "@stacks/common": "^4.0.3-beta.0",
37
- "@stacks/encryption": "^4.0.3-beta.0",
35
+ "@stacks/auth": "^4.1.0",
36
+ "@stacks/common": "^4.1.0",
37
+ "@stacks/encryption": "^4.1.0",
38
38
  "jsontokens": "^3.0.0"
39
39
  },
40
40
  "devDependencies": {
41
+ "@stacks/network": "^4.1.0",
41
42
  "@types/jest": "^26.0.22",
42
43
  "@types/jsdom": "^16.2.10",
43
44
  "jest": "^26.6.3",
@@ -61,5 +62,5 @@
61
62
  "browser": "dist/polyfill/index.js",
62
63
  "umd:main": "dist/umd/index.js",
63
64
  "unpkg": "dist/umd/index.js",
64
- "gitHead": "e65defc1a2bef36884bdcc2010763935955e2856"
65
+ "gitHead": "d0ca86ec4547a21d7da7730a1074f410b3e85b2a"
65
66
  }
package/src/hub.ts CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  Buffer,
4
4
  ConflictError,
5
5
  DoesNotExist,
6
- fetchPrivate,
7
6
  GaiaHubErrorResponse,
8
7
  Logger,
9
8
  megabytesToBytes,
@@ -21,6 +20,7 @@ import {
21
20
  randomBytes,
22
21
  Signature,
23
22
  } from '@stacks/encryption';
23
+ import { createFetchFn, FetchFn } from '@stacks/network';
24
24
  import { TokenSigner } from 'jsontokens';
25
25
 
26
26
  /**
@@ -62,7 +62,8 @@ export async function uploadToGaiaHub(
62
62
  contentType = 'application/octet-stream',
63
63
  newFile = true,
64
64
  etag?: string,
65
- dangerouslyIgnoreEtag?: boolean
65
+ dangerouslyIgnoreEtag?: boolean,
66
+ fetchFn: FetchFn = createFetchFn()
66
67
  ): Promise<UploadResponse> {
67
68
  Logger.debug(`uploadToGaiaHub: uploading ${filename} to ${hubConfig.server}`);
68
69
 
@@ -79,14 +80,11 @@ export async function uploadToGaiaHub(
79
80
  }
80
81
  }
81
82
 
82
- const response = await fetchPrivate(
83
- `${hubConfig.server}/store/${hubConfig.address}/${filename}`,
84
- {
85
- method: 'POST',
86
- headers,
87
- body: contents,
88
- }
89
- );
83
+ const response = await fetchFn(`${hubConfig.server}/store/${hubConfig.address}/${filename}`, {
84
+ method: 'POST',
85
+ headers,
86
+ body: contents,
87
+ });
90
88
  if (!response.ok) {
91
89
  throw await getBlockstackErrorFromResponse(
92
90
  response,
@@ -102,17 +100,18 @@ export async function uploadToGaiaHub(
102
100
  * @param filename
103
101
  * @param hubConfig
104
102
  */
105
- export async function deleteFromGaiaHub(filename: string, hubConfig: GaiaHubConfig): Promise<void> {
103
+ export async function deleteFromGaiaHub(
104
+ filename: string,
105
+ hubConfig: GaiaHubConfig,
106
+ fetchFn: FetchFn = createFetchFn()
107
+ ): Promise<void> {
106
108
  Logger.debug(`deleteFromGaiaHub: deleting ${filename} from ${hubConfig.server}`);
107
- const response = await fetchPrivate(
108
- `${hubConfig.server}/delete/${hubConfig.address}/${filename}`,
109
- {
110
- method: 'DELETE',
111
- headers: {
112
- Authorization: `bearer ${hubConfig.token}`,
113
- },
114
- }
115
- );
109
+ const response = await fetchFn(`${hubConfig.server}/delete/${hubConfig.address}/${filename}`, {
110
+ method: 'DELETE',
111
+ headers: {
112
+ Authorization: `bearer ${hubConfig.token}`,
113
+ },
114
+ });
116
115
  if (!response.ok) {
117
116
  throw await getBlockstackErrorFromResponse(
118
117
  response,
@@ -208,11 +207,12 @@ function makeV1GaiaAuthToken(
208
207
  export async function connectToGaiaHub(
209
208
  gaiaHubUrl: string,
210
209
  challengeSignerHex: string,
211
- associationToken?: string
210
+ associationToken?: string,
211
+ fetchFn: FetchFn = createFetchFn()
212
212
  ): Promise<GaiaHubConfig> {
213
213
  Logger.debug(`connectToGaiaHub: ${gaiaHubUrl}/hub_info`);
214
214
 
215
- const response = await fetchPrivate(`${gaiaHubUrl}/hub_info`);
215
+ const response = await fetchFn(`${gaiaHubUrl}/hub_info`);
216
216
  const hubInfo = await response.json();
217
217
  const readURL = hubInfo.read_url_prefix;
218
218
  const token = makeV1GaiaAuthToken(hubInfo, challengeSignerHex, gaiaHubUrl, associationToken);
@@ -233,8 +233,12 @@ export async function connectToGaiaHub(
233
233
  *
234
234
  * @ignore
235
235
  */
236
- export async function getBucketUrl(gaiaHubUrl: string, appPrivateKey: string): Promise<string> {
237
- const response = await fetchPrivate(`${gaiaHubUrl}/hub_info`);
236
+ export async function getBucketUrl(
237
+ gaiaHubUrl: string,
238
+ appPrivateKey: string,
239
+ fetchFn: FetchFn = createFetchFn()
240
+ ): Promise<string> {
241
+ const response = await fetchFn(`${gaiaHubUrl}/hub_info`);
238
242
  const responseText = await response.text();
239
243
  const responseJSON = JSON.parse(responseText);
240
244
  const readURL = responseJSON.read_url_prefix;
package/src/storage.ts CHANGED
@@ -1,10 +1,7 @@
1
- // @ts-ignore
2
- import { Buffer } from '@stacks/common';
3
1
  import { lookupProfile, NAME_LOOKUP_PATH, UserSession } from '@stacks/auth';
4
2
  import {
5
3
  BLOCKSTACK_DEFAULT_GAIA_HUB_URL,
6
4
  DoesNotExist,
7
- fetchPrivate,
8
5
  GaiaHubError,
9
6
  getGlobalObject,
10
7
  InvalidStateError,
@@ -20,6 +17,7 @@ import {
20
17
  signECDSA,
21
18
  verifyECDSA,
22
19
  } from '@stacks/encryption';
20
+ import { createFetchFn, FetchFn } from '@stacks/network';
23
21
  import { FileContentLoader } from './fileContentLoader';
24
22
  import {
25
23
  connectToGaiaHub,
@@ -272,11 +270,12 @@ export class Storage {
272
270
  app: string,
273
271
  username: string | undefined,
274
272
  zoneFileLookupURL: string | undefined,
275
- forceText: boolean
273
+ forceText: boolean,
274
+ fetchFn: FetchFn = createFetchFn()
276
275
  ): Promise<string | ArrayBuffer | null> {
277
276
  const opts = { app, username, zoneFileLookupURL };
278
277
  const readUrl = await this.getFileUrl(path, opts);
279
- const response = await fetchPrivate(readUrl);
278
+ const response = await fetchFn(readUrl);
280
279
  if (!response.ok) {
281
280
  throw await getBlockstackErrorFromResponse(response, `getFile ${path} failed.`, null);
282
281
  }
@@ -394,6 +393,7 @@ export class Storage {
394
393
  privateKey?: string,
395
394
  username?: string,
396
395
  zoneFileLookupURL?: string
396
+ // eslint-disable-next-line node/prefer-global/buffer
397
397
  ): Promise<string | Buffer> {
398
398
  const appPrivateKey = privateKey || this.userSession.loadUserData().appPrivateKey;
399
399
 
@@ -460,6 +460,7 @@ export class Storage {
460
460
  */
461
461
  async putFile(
462
462
  path: string,
463
+ // eslint-disable-next-line node/prefer-global/buffer
463
464
  content: string | Buffer | ArrayBufferView | Blob,
464
465
  options?: PutFileOptions
465
466
  ): Promise<string> {
@@ -556,6 +557,7 @@ export class Storage {
556
557
  };
557
558
  } else {
558
559
  // In all other cases, we only need one upload.
560
+ // eslint-disable-next-line node/prefer-global/buffer
559
561
  let contentForUpload: string | Buffer | Blob;
560
562
  if (!opt.encrypt && !opt.sign) {
561
563
  // If content does not need encrypted or signed, it can be passed directly
@@ -693,7 +695,8 @@ export class Storage {
693
695
  page: string | null,
694
696
  callCount: number,
695
697
  fileCount: number,
696
- callback: (name: string) => boolean
698
+ callback: (name: string) => boolean,
699
+ fetchFn: FetchFn = createFetchFn()
697
700
  ): Promise<number> {
698
701
  if (callCount > 65536) {
699
702
  // this is ridiculously huge, and probably indicates
@@ -714,10 +717,7 @@ export class Storage {
714
717
  },
715
718
  body: pageRequest,
716
719
  };
717
- response = await fetchPrivate(
718
- `${hubConfig.server}/list-files/${hubConfig.address}`,
719
- fetchOptions
720
- );
720
+ response = await fetchFn(`${hubConfig.server}/list-files/${hubConfig.address}`, fetchOptions);
721
721
  if (!response.ok) {
722
722
  throw await getBlockstackErrorFromResponse(response, 'ListFiles failed.', hubConfig);
723
723
  }