From d44843d18749f3a4397df568de183771d65a543a Mon Sep 17 00:00:00 2001 From: rrr523 Date: Fri, 31 May 2024 16:17:11 +0800 Subject: [PATCH] fix: Upload JSON file --- .changeset/violet-news-smell.md | 5 +++++ packages/js-sdk/package.json | 1 + packages/js-sdk/src/clients/spclient/spClient.ts | 13 +++++++------ pnpm-lock.yaml | 12 ++++++++++-- 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 .changeset/violet-news-smell.md diff --git a/.changeset/violet-news-smell.md b/.changeset/violet-news-smell.md new file mode 100644 index 00000000..112ca134 --- /dev/null +++ b/.changeset/violet-news-smell.md @@ -0,0 +1,5 @@ +--- +'@bnb-chain/greenfield-js-sdk': patch +--- + +fix: Upload file that `content-type` is `application/json`, cancel serialize. diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.json index 723c77fa..bd975bd8 100644 --- a/packages/js-sdk/package.json +++ b/packages/js-sdk/package.json @@ -70,6 +70,7 @@ "@ethersproject/units": "^5.7.0", "@metamask/eth-sig-util": "^5.0.2", "@noble/curves": "^1.3.0", + "browser-or-node": "^3.0.0", "cross-fetch": "^4.0.0", "dayjs": "^1.11.7", "ethereum-cryptography": "^2.0.0", diff --git a/packages/js-sdk/src/clients/spclient/spClient.ts b/packages/js-sdk/src/clients/spclient/spClient.ts index 92ce675a..df47ae58 100644 --- a/packages/js-sdk/src/clients/spclient/spClient.ts +++ b/packages/js-sdk/src/clients/spclient/spClient.ts @@ -20,6 +20,7 @@ import { getGetObjectMetaInfo } from './spApis/getObject'; import { getPutObjectMetaInfo } from './spApis/putObject'; import { assertFileType, assertHttpMethod } from '@/utils'; import { UploadFile } from '@/types/sp/Common'; +import { isNode } from 'browser-or-node'; export interface ISpClient { callApi( @@ -168,6 +169,7 @@ export class SpClient implements ISpClient { }, ) { const R = superagent.put(url); + R.buffer(true); R.timeout(timeout); R.ok((res) => res.status < 500); @@ -178,11 +180,6 @@ export class SpClient implements ISpClient { } try { - const R = superagent.put(url); - R.buffer(true); - R.timeout(timeout); - R.ok((res) => res.status < 500); - if (options.headers) { (options.headers as Headers).forEach((v: string, k: string) => { R.set(k, v); @@ -196,7 +193,11 @@ export class SpClient implements ISpClient { } const file = assertFileType(uploadFile) ? uploadFile.content : uploadFile; - const response = await R.send(file); + + // https://ladjs.github.io/superagent/docs/index.html#serializing-request-body + const serializeFile = + isNode && R.get('Content-Type') === 'application/json' ? file.toString() : file; + const response = await R.send(serializeFile); const { status } = response; if (status === SP_NOT_AVAILABLE_ERROR_CODE) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 392fd01b..67dfe239 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -429,6 +429,9 @@ importers: '@noble/curves': specifier: ^1.3.0 version: 1.4.0 + browser-or-node: + specifier: ^3.0.0 + version: 3.0.0 cross-fetch: specifier: ^4.0.0 version: 4.0.0 @@ -4369,6 +4372,9 @@ packages: brorand@1.1.0: resolution: {integrity: sha512-cKV8tMCEpQs4hK/ik71d6LrPOnpkpGBR0wzxqr68g2m/LB2GxVYQroAjMJZRVM1Y4BCjCKc3vAamxSzOY2RP+w==} + browser-or-node@3.0.0: + resolution: {integrity: sha512-iczIdVJzGEYhP5DqQxYM9Hh7Ztpqqi+CXZpSmX8ALFs9ecXkQIeqRyM6TfxEfMVpwhl3dSuDvxdzzo9sUOIVBQ==} + browser-stdout@1.3.1: resolution: {integrity: sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==} @@ -15035,7 +15041,7 @@ snapshots: dependencies: '@types/istanbul-lib-coverage': 2.0.6 '@types/istanbul-reports': 3.0.4 - '@types/node': 20.6.0 + '@types/node': 18.19.33 '@types/yargs': 15.0.19 chalk: 4.1.2 @@ -18273,6 +18279,8 @@ snapshots: brorand@1.1.0: {} + browser-or-node@3.0.0: {} + browser-stdout@1.3.1: {} browserify-aes@1.2.0: @@ -18519,7 +18527,7 @@ snapshots: chrome-launcher@0.15.2: dependencies: - '@types/node': 20.6.0 + '@types/node': 18.19.33 escape-string-regexp: 4.0.0 is-wsl: 2.2.0 lighthouse-logger: 1.4.2