Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace JSON11-produced hex escape codes with unicode escape codes #879

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Deprecated
### Removed
### Fixed
- Replace JSON11-produced hex escape codes with unicode escape codes ([#879](https://github.com/opensearch-project/opensearch-js/pull/879))
### Security

## [3.0.0]
Expand Down Expand Up @@ -310,4 +311,4 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Bumps `tsd` from 0.22.0 to 0.24.1
- Bumps `semver` from 7.3.7 to 7.3.8
### Fixed
- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291))
- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291))
3 changes: 2 additions & 1 deletion lib/Serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const kJsonOptions = Symbol('secure json parse options');
const JSON11 = require('json11');

const isBigIntSupported = typeof BigInt !== 'undefined';
const hexEscapeRegex = /\\x([0-9A-Fa-f]{2})/g;

class Serializer {
constructor(opts = {}) {
Expand Down Expand Up @@ -81,7 +82,7 @@ class Serializer {
quote: '"',
quoteNames: true,
});
if (temp) json = temp;
if (temp) json = temp.replace(hexEscapeRegex, (_, c) => "\\u00".append(c));
} catch (ex) {
// Do nothing: JSON.stringify succeeded but JSON11.stringify failed; return the
// JSON.stringify result.
Expand Down
7 changes: 7 additions & 0 deletions test/unit/serializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,10 @@ test('disable prototype poisoning protection only for constructor', (t) => {
t.fail(err);
}
});

test('Long numerals and ANSI escape sequences', (t) => {
t.plan(1);
const s = new Serializer({ enableLongNumeralSupport: true });
const obj = {message: "hello \u001b[38;7;2mworld\u001b[0m", value: BigInt(Number.MAX_SAFE_INTEGER) * 2n};
t.same(s.deserialize(s.serialize(obj)), obj);
})
Loading