-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix][ST-29177] Issues with Special Characters ByteLength in SDK (#835)
* fix(ts-client): issues with special characters encoding in sdk - install and use browser friendly buffer module to properly read body bytelength * fix(ts-client): issues with special characters encoding in sdk - install and use browser friendly buffer module to properly read body bytelength * chore(release-changeset): changeset - add release changeset * chore(ts-client): buffer type - add checks for Buffer body type * chore(ts-client): add test - add tests to validate functionality * chore(ts-client): add test - add more tests to validate functionality
- Loading branch information
Showing
5 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools/ts-client': patch | ||
--- | ||
|
||
Fix issues with counting special characters' length |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
var Buffer = require('buffer/').Buffer | ||
|
||
export default function byteLength<T>(body: T): string { | ||
if (typeof body === 'string') return body.length.toString() | ||
if (body && typeof body === 'object') | ||
if (body && (typeof body === 'string' || body instanceof Uint8Array)) { | ||
return Buffer.byteLength(body).toString() | ||
} | ||
|
||
if (body && typeof body === 'object') { | ||
return new TextEncoder().encode(JSON.stringify(body)).length.toString() | ||
if (body instanceof Uint8Array) return body.length.toString() | ||
} | ||
|
||
return '0' | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/sdk-client-v3/tests/utils.test/byteLength.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
var Buffer = require('buffer/').Buffer | ||
import { byteLength } from '../../src/utils' | ||
|
||
describe('byteLength test', () => { | ||
test('should properly decode a normal character string.', () => { | ||
const str = 'this is a regular/normal characters string' | ||
expect(byteLength(str)).toEqual(str.length.toString()) | ||
}) | ||
|
||
test('should properly decode a special character string.', () => { | ||
const str = 'äëöü' | ||
expect(byteLength(str)).toEqual('8') | ||
expect(str.length.toString()).toEqual('4') | ||
|
||
expect(byteLength(str)).not.toEqual(str.length.toString()) | ||
expect(Number(byteLength(str)) - str.length).not.toEqual(0) | ||
expect(byteLength(str)).toEqual('8') | ||
}) | ||
|
||
test('should show the difference between `String.length` and `Buffer.byteLength`', () => { | ||
const str = 'äëöü' | ||
expect(byteLength(str)).toEqual('8') | ||
expect(str.length.toString()).toEqual('4') | ||
}) | ||
|
||
test('should show there is no difference between `new TextEncoder().encode`', () => { | ||
const str = 'äëöü' | ||
expect(byteLength(str)).toEqual('8') | ||
expect(new TextEncoder().encode(str).length.toString()).toEqual('8') | ||
|
||
const str2 = JSON.stringify({ n: 'äëöü' }) | ||
expect(byteLength(str2)).toEqual('16') | ||
expect(new TextEncoder().encode(str2).length.toString()).toEqual('16') | ||
}) | ||
|
||
test('should return an accurate result of byte length of a string', () => { | ||
const str = 'the string length is 23' | ||
expect(byteLength(str)).toEqual('23') | ||
}) | ||
|
||
test('should return an accurate result of byte length of a Buffer', () => { | ||
const buffer = Buffer.from('the string length is 23') | ||
expect(byteLength(buffer)).toEqual('23') | ||
}) | ||
|
||
test('should return an accurate result of byte length of an object', () => { | ||
const object = { 'byte-length': 18 } | ||
expect(byteLength(object)).toEqual('18') | ||
}) | ||
|
||
test('should return `0` is body is null', () => { | ||
expect(byteLength(null)).toEqual('0') | ||
}) | ||
|
||
test('should return `0` is body is undefined', () => { | ||
expect(byteLength(undefined)).toEqual(`0`) | ||
}) | ||
test('should return `0` is body is not provided', () => { | ||
// @ts-ignore | ||
expect(byteLength()).toEqual('0') | ||
}) | ||
}) |