🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@aws-sdk/core

Package Overview
Dependencies
Maintainers
2
Versions
227
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aws-sdk/core - npm Package Compare versions

Comparing version
3.977.2
to
3.977.3
+20
dist-es/submodules/protocols/json/codec-v1/JsonCodec.js
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./JsonShapeDeserializer";
import { JsonShapeSerializer } from "./JsonShapeSerializer";
export class JsonCodec extends SerdeContextConfig {
settings;
constructor(settings) {
super();
this.settings = settings;
}
createSerializer() {
const serializer = new JsonShapeSerializer(this.settings);
serializer.setSerdeContext(this.serdeContext);
return serializer;
}
createDeserializer() {
const deserializer = new JsonShapeDeserializer(this.settings);
deserializer.setSerdeContext(this.serdeContext);
return deserializer;
}
}
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonShapeDeserializer2 } from "./JsonShapeDeserializer2";
import { JsonShapeSerializer2 } from "./JsonShapeSerializer2";
export class JsonCodec2 extends SerdeContextConfig {
settings;
constructor(settings) {
super();
this.settings = settings;
}
createSerializer() {
const serializer = new JsonShapeSerializer2(this.settings);
serializer.setSerdeContext(this.serdeContext);
return serializer;
}
createDeserializer() {
const deserializer = new JsonShapeDeserializer2(this.settings);
deserializer.setSerdeContext(this.serdeContext);
return deserializer;
}
}
import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { LazyJsonString, NumericValue, parseEpochTimestamp, parseRfc3339DateTimeWithOffset, parseRfc7231DateTime, } from "@smithy/core/serde";
import { fromBase64 } from "@smithy/core/serde";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { UnionSerde } from "../../UnionSerde";
import { detectBufferParsing } from "../detectBufferParsing";
import { jsonReviver } from "../jsonReviver";
import { needsReviver } from "../needsReviver";
import { parseJsonBody } from "../parseJsonBody";
import { writeKey } from "../../writeKey";
export class JsonShapeDeserializer2 extends SerdeContextConfig {
settings;
constructor(settings) {
super();
this.settings = settings;
}
async read(schema, data) {
const reviver = needsReviver(schema) ? jsonReviver : undefined;
let parsed;
if (typeof data === "string") {
parsed = JSON.parse(data, reviver);
}
else if (data instanceof Uint8Array && detectBufferParsing()) {
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
parsed = JSON.parse(buf, reviver);
}
else {
parsed = await parseJsonBody(data, this.serdeContext);
}
return this._read(schema, parsed);
}
readObject(schema, data) {
return this._read(schema, data);
}
_read(schema, value) {
const isObject = value !== null && typeof value === "object";
const ns = NormalizedSchema.of(schema);
if (isObject) {
if (ns.isStructSchema()) {
return this._readStruct(ns, value);
}
if (Array.isArray(value) && ns.isListSchema()) {
const listMember = ns.getValueSchema();
for (let i = 0; i < value.length; ++i) {
value[i] = this._read(listMember, value[i]);
}
return value;
}
if (ns.isMapSchema()) {
const mapMember = ns.getValueSchema();
const map = value;
for (const k in map) {
if (k === "__proto__") {
writeKey(map);
}
map[k] = this._read(mapMember, map[k]);
}
return map;
}
}
if (ns.isBlobSchema() && typeof value === "string") {
return fromBase64(value);
}
const mediaType = ns.getMergedTraits().mediaType;
if (ns.isStringSchema() && typeof value === "string" && mediaType) {
const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
if (isJson) {
return LazyJsonString.from(value);
}
return value;
}
if (ns.isTimestampSchema() && value != null) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case 5:
return parseRfc3339DateTimeWithOffset(value);
case 6:
return parseRfc7231DateTime(value);
case 7:
return parseEpochTimestamp(value);
default:
console.warn("Missing timestamp format, parsing value with Date constructor:", value);
return new Date(value);
}
}
if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) {
return BigInt(value);
}
if (ns.isBigDecimalSchema() && value != undefined) {
if (value instanceof NumericValue) {
return value;
}
const untyped = value;
if (untyped.type === "bigDecimal" && "string" in untyped) {
return new NumericValue(untyped.string, untyped.type);
}
return new NumericValue(String(value), "bigDecimal");
}
if (ns.isNumericSchema() && typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
}
return value;
}
if (ns.isDocumentSchema()) {
if (isObject) {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; ++i) {
const v = value[i];
if (!(v instanceof NumericValue)) {
value[i] = this._read(ns, v);
}
}
}
else {
const doc = value;
for (const k in doc) {
if (k === "__proto__") {
writeKey(doc);
}
const v = doc[k];
if (!(v instanceof NumericValue)) {
doc[k] = this._read(ns, v);
}
}
}
return value;
}
else {
return value;
}
}
return value;
}
_readStruct(ns, record) {
const union = ns.isUnionSchema();
const out = {};
let nameMap = void 0;
const { jsonName } = this.settings;
if (jsonName) {
nameMap = {};
}
let unionSerde;
if (union) {
unionSerde = new UnionSerde(record, out);
}
for (const [memberName, memberSchema] of ns.structIterator()) {
let fromKey = memberName;
if (jsonName) {
fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
nameMap[fromKey] = memberName;
}
if (union) {
unionSerde.mark(fromKey);
}
if (record[fromKey] != null) {
out[memberName] = this._read(memberSchema, record[fromKey]);
}
}
if (union) {
unionSerde.writeUnknown();
}
else if (typeof record.__type === "string") {
for (const k in record) {
const v = record[k];
const t = jsonName ? (nameMap[k] ?? k) : k;
if (!(t in out)) {
out[t] = v;
}
}
}
return out;
}
}
import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { dateToUtcString, generateIdempotencyToken, LazyJsonString, NumericValue, toBase64 } from "@smithy/core/serde";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { writeKey } from "../../writeKey";
const encoder = new TextEncoder();
const OPEN_BRACE = 0x7b;
const CLOSE_BRACE = 0x7d;
const OPEN_BRACKET = 0x5b;
const CLOSE_BRACKET = 0x5d;
const QUOTE = 0x22;
const COLON = 0x3a;
const COMMA = 0x2c;
const BACKSLASH = 0x5c;
const TRUE = new Uint8Array([0x74, 0x72, 0x75, 0x65]);
const FALSE = new Uint8Array([0x66, 0x61, 0x6c, 0x73, 0x65]);
const NULL = new Uint8Array([0x6e, 0x75, 0x6c, 0x6c]);
const ESCAPE_TABLE = new Array(128).fill(null);
ESCAPE_TABLE[0x08] = "b";
ESCAPE_TABLE[0x09] = "t";
ESCAPE_TABLE[0x0a] = "n";
ESCAPE_TABLE[0x0c] = "f";
ESCAPE_TABLE[0x0d] = "r";
ESCAPE_TABLE[0x22] = '"';
ESCAPE_TABLE[0x5c] = "\\";
for (let i = 0; i < 0x20; i++) {
if (ESCAPE_TABLE[i] === null) {
ESCAPE_TABLE[i] = "u00" + i.toString(16).padStart(2, "0");
}
}
const INITIAL_BUFFER_SIZE = 2048;
function alloc(size) {
return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
}
export class JsonShapeSerializer2 extends SerdeContextConfig {
settings;
json;
i = 0;
rootSchema;
rawValue;
passthrough = false;
constructor(settings) {
super();
this.settings = settings;
this.json = alloc(INITIAL_BUFFER_SIZE);
}
write(schema, value) {
this.i = 0;
this.rawValue = value;
this.rootSchema = NormalizedSchema.of(schema);
this.passthrough =
!this.rootSchema.isStructSchema() &&
!this.rootSchema.isDocumentSchema() &&
(this.rootSchema.isBlobSchema() || this.rootSchema.isStringSchema());
if (!this.passthrough) {
this.writeValue(this.rootSchema, value, undefined);
}
}
writeDiscriminatedDocument(schema, value) {
this.i = 0;
this.rootSchema = NormalizedSchema.of(schema);
const ns = this.rootSchema;
if (ns.isStructSchema() && value != null && typeof value === "object") {
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
this.writeAsciiQuoted("__type");
this.json[this.i++] = COLON;
this.writeAsciiQuoted(ns.getName(true) ?? "Unknown");
let wroteAny = true;
const { jsonName } = this.settings;
for (const [memberName, memberSchema] of ns.structIterator()) {
const item = value[memberName];
if (item == null && !memberSchema.isIdempotencyToken()) {
continue;
}
if (wroteAny) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
const targetKey = jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
this.writeAsciiQuoted(targetKey);
this.json[this.i++] = COLON;
this.writeValue(memberSchema, item, ns);
wroteAny = true;
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
else {
this.writeValue(ns, value, undefined);
}
}
flush() {
this.rootSchema = undefined;
const finalPosition = this.i;
this.i = 0;
const raw = this.rawValue;
this.rawValue = undefined;
if (finalPosition === 0) {
return raw;
}
const result = this.json.subarray(0, finalPosition);
this.json = alloc(INITIAL_BUFFER_SIZE);
return result;
}
ensure(byteCount) {
const { i, json } = this;
if (i + byteCount > json.length) {
let newSize = json.length * 2;
while (newSize < i + byteCount) {
newSize *= 2;
}
const next = alloc(newSize);
next.set(this.json);
this.json = next;
}
}
writeAscii(s) {
const z = s.length;
this.ensure(z);
let { i, json } = this;
for (let j = 0; j < z; ++j) {
json[i] = s.charCodeAt(j);
i += 1;
}
this.i = i;
}
writeAsciiQuoted(s) {
const z = s.length;
this.ensure(z + 4);
let { json, i } = this;
json[i++] = QUOTE;
for (let j = 0; j < z; ++j) {
json[i++] = s.charCodeAt(j);
}
json[i++] = QUOTE;
this.i = i;
}
writeJsonString(s) {
this.ensure(s.length * 2 + 2);
this.json[this.i++] = QUOTE;
const z = s.length;
for (let j = 0; j < z; ++j) {
const c = s.charCodeAt(j);
if (c > 0x22 && c < 0x5c) {
this.json[this.i++] = c;
}
else if (c < 0x80) {
const esc = ESCAPE_TABLE[c];
if (esc !== null) {
this.ensure(esc.length + 1);
this.json[this.i++] = BACKSLASH;
for (let k = 0; k < esc.length; k++) {
this.json[this.i++] = esc.charCodeAt(k);
}
}
else {
this.json[this.i++] = c;
}
}
else if (c >= 0xd800 && c <= 0xdbff) {
const next = j + 1 < z ? s.charCodeAt(j + 1) : 0;
if (next >= 0xdc00 && next <= 0xdfff) {
this.ensure(4);
const { written } = encoder.encodeInto(s.substring(j, j + 2), this.json.subarray(this.i));
this.i += written;
j++;
}
else {
this.ensure(6);
this.writeUnicodeEscape(c);
}
}
else if (c >= 0xdc00 && c <= 0xdfff) {
this.ensure(6);
this.writeUnicodeEscape(c);
}
else {
let { i, json } = this;
if (c < 0x800) {
json[i++] = 0xc0 | (c >> 6);
json[i++] = 0x80 | (c & 0x3f);
}
else {
json[i++] = 0xe0 | (c >> 12);
json[i++] = 0x80 | ((c >> 6) & 0x3f);
json[i++] = 0x80 | (c & 0x3f);
}
this.i = i;
}
}
this.json[this.i++] = QUOTE;
}
writeUnicodeEscape(code) {
let { json, i } = this;
json[i++] = BACKSLASH;
json[i++] = 0x75;
const hex = code.toString(16).padStart(4, "0");
for (let j = 0; j < 4; ++j) {
json[i++] = hex.charCodeAt(j);
}
this.i = i;
}
static B64 = (() => {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const table = new Uint8Array(64);
for (let i = 0; i < 64; i++)
table[i] = chars.charCodeAt(i);
return table;
})();
writeBase64(data) {
const b64Len = Math.ceil(data.length / 3) * 4;
this.ensure(b64Len + 2);
const json = this.json;
const B64 = JsonShapeSerializer2.B64;
let i = this.i;
json[i++] = QUOTE;
const len = data.length;
const remainder = len % 3;
const mainLen = len - remainder;
for (let j = 0; j < mainLen; j += 3) {
const a = data[j];
const b = data[j + 1];
const c = data[j + 2];
json[i++] = B64[a >> 2];
json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
json[i++] = B64[((b & 0x0f) << 2) | (c >> 6)];
json[i++] = B64[c & 0x3f];
}
if (remainder === 2) {
const a = data[mainLen];
const b = data[mainLen + 1];
json[i++] = B64[a >> 2];
json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
json[i++] = B64[(b & 0x0f) << 2];
json[i++] = 0x3d;
}
else if (remainder === 1) {
const a = data[mainLen];
json[i++] = B64[a >> 2];
json[i++] = B64[(a & 0x03) << 4];
json[i++] = 0x3d;
json[i++] = 0x3d;
}
json[i++] = QUOTE;
this.i = i;
}
writeValue(schema, value, container) {
if (value == null) {
if (container?.isStructSchema()) {
if (value === undefined) {
const ns = NormalizedSchema.of(schema);
if (ns.isIdempotencyToken()) {
this.writeAsciiQuoted(generateIdempotencyToken());
return;
}
}
return;
}
this.ensure(4);
this.json.set(NULL, this.i);
this.i += 4;
return;
}
const ns = NormalizedSchema.of(schema);
const isObject = typeof value === "object";
if (ns.isStringSchema()) {
const mediaType = ns.getMergedTraits().mediaType;
if (mediaType) {
const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
if (isJson) {
this.writeJsonString(LazyJsonString.from(value).toString());
return;
}
}
}
if (isObject) {
if (ns.isStructSchema()) {
this.writeStruct(ns, value);
return;
}
if (Array.isArray(value) && (ns.isListSchema() || ns.isDocumentSchema())) {
this.writeList(ns, value, ns.isDocumentSchema());
return;
}
if (ns.isMapSchema()) {
this.writeMap(ns, value, false);
return;
}
if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
this.writeBase64(value);
return;
}
if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
this.writeTimestamp(ns, value);
return;
}
if (value instanceof NumericValue) {
this.writeAscii(value.string);
return;
}
if (ns.isDocumentSchema()) {
if (Array.isArray(value)) {
this.writeList(ns, value, true);
}
else {
this.writeMap(ns, value, true);
}
return;
}
const json = JSON.stringify(value);
this.writeAscii(json);
return;
}
if (typeof value === "string") {
if (ns.isBlobSchema()) {
const b64 = (this.serdeContext?.base64Encoder ?? toBase64)(value);
this.writeAsciiQuoted(b64);
return;
}
this.writeJsonString(value);
return;
}
if (typeof value === "number") {
if (ns.isNumericSchema() && (Math.abs(value) === Infinity || isNaN(value))) {
this.writeAsciiQuoted(String(value));
return;
}
const numStr = String(value);
this.writeAscii(numStr);
return;
}
if (typeof value === "boolean") {
this.ensure(5);
if (value) {
this.json.set(TRUE, this.i);
this.i += 4;
}
else {
this.json.set(FALSE, this.i);
this.i += 5;
}
return;
}
if (typeof value === "bigint") {
this.writeAscii(value.toString());
return;
}
this.writeAscii(String(value));
}
writeStruct(ns, value) {
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
let first = true;
let wroteAny = false;
const hasType = typeof value.__type === "string";
let writtenKeys;
if (hasType) {
writtenKeys = new Set();
}
for (const [memberName, memberSchema] of ns.structIterator()) {
const item = value[memberName];
if (item == null && !memberSchema.isIdempotencyToken())
continue;
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
wroteAny = true;
const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
if (writtenKeys) {
writtenKeys.add(memberName);
writtenKeys.add(targetKey);
}
this.writeAsciiQuoted(targetKey);
this.json[this.i++] = COLON;
this.writeValue(memberSchema, item, ns);
}
if (!wroteAny && ns.isUnionSchema()) {
const { $unknown } = value;
if (Array.isArray($unknown)) {
const [k, v] = $unknown;
this.writeAsciiQuoted(k);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(15, v, ns);
}
}
else if (hasType) {
for (const k in value) {
const targetKey = this.settings.jsonName ? (writtenKeys.has(k) ? k : k) : k;
if (writtenKeys.has(targetKey))
continue;
writtenKeys.add(targetKey);
const v = value[k];
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
this.writeAsciiQuoted(targetKey);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(15, v, undefined);
}
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
writeList(ns, value, isDocument) {
this.ensure(2);
this.json[this.i++] = OPEN_BRACKET;
const sparse = !!ns.getMergedTraits().sparse;
const valueSchema = ns.getValueSchema();
for (let i = 0; i < value.length; ++i) {
const item = value[i];
if (isDocument ? item === undefined : item == null && !sparse) {
continue;
}
if (i !== 0) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
this.writeValue(valueSchema, item, undefined);
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACKET;
}
writeMap(ns, value, isDocument) {
const sparse = !!ns.getMergedTraits().sparse;
const valueSchema = ns.getValueSchema();
if (!isDocument) {
if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
let input = value;
if (sparse) {
input = {};
for (const k in value) {
if (k === "__proto__") {
writeKey(input);
}
input[k] = value[k] ?? null;
}
}
const json = JSON.stringify(input);
this.ensure(json.length * 3);
const { written } = encoder.encodeInto(json, this.json.subarray(this.i));
this.i += written;
return;
}
}
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
let first = true;
for (const k in value) {
const v = value[k];
if (isDocument ? v === undefined : v == null && !sparse) {
continue;
}
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
this.writeJsonString(k);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(valueSchema, v, undefined);
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
writeTimestamp(ns, value) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case 5: {
const iso = value.toISOString().replace(".000Z", "Z");
this.writeAsciiQuoted(iso);
return;
}
case 6: {
this.writeAsciiQuoted(dateToUtcString(value));
return;
}
case 7: {
const epochSecs = String(value.getTime() / 1000);
this.writeAscii(epochSecs);
return;
}
default: {
const epochSecs = String(value.getTime() / 1000);
this.writeAscii(epochSecs);
return;
}
}
}
}
import type { Codec } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./JsonShapeDeserializer";
import { JsonShapeSerializer } from "./JsonShapeSerializer";
import type { JsonSettings } from "./JsonSettings";
/**
* @deprecated use JsonCodec2.
* @public
*/
export declare class JsonCodec extends SerdeContextConfig implements Codec<string, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): JsonShapeSerializer;
createDeserializer(): JsonShapeDeserializer;
}
import type { CodecSettings } from "@smithy/types";
/**
* @public
*/
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
import type { $ShapeSerializer, $Codec, $ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonSettings";
/**
* @public
*/
export declare class JsonCodec2 extends SerdeContextConfig implements $Codec<Uint8Array, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): $ShapeSerializer<Uint8Array>;
createDeserializer(): $ShapeDeserializer<string>;
}
import type { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonSettings";
/**
* Performance-optimized JSON deserializer.
*
* Skips UTF-8 decoding when the runtime supports JSON.parse(Buffer) (Node 22+).
*
* After JSON.parse, lists, maps, and document containers are mutated in place
* (element values are overwritten with their deserialized form) rather than
* copied into new arrays/objects. Structs allocate a fresh object because
* jsonName traits require key renaming, and building the output object
* incrementally lets V8 assign a stable hidden class rather than
* deoptimizing from repeated property deletion/addition on an existing shape.
*
* In-place mutation is safe here because the parsed tree is locally owned
* after JSON.parse with no external references, so rewriting values avoids
* redundant allocation and GC pressure.
*
* @public
*/
export declare class JsonShapeDeserializer2 extends SerdeContextConfig implements ShapeDeserializer<string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
read(schema: Schema, data: string | Uint8Array | unknown): Promise<any>;
readObject(schema: Schema, data: DocumentType): any;
protected _read(schema: Schema, value: unknown): any;
private _readStruct;
}
import { NormalizedSchema } from "@smithy/core/schema";
import type { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonSettings";
/**
* Single-pass JSON serializer that writes directly to a Uint8Array buffer.
* Fewer intermediate states as when compared to the initial multi-pass implementation.
*
* @public
*/
export declare class JsonShapeSerializer2 extends SerdeContextConfig implements ShapeSerializer<Uint8Array> {
readonly settings: JsonSettings;
private json;
private i;
private rootSchema;
private rawValue;
private passthrough;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
/**
* @internal
*/
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
/**
* Returns the serialized JSON as a Uint8Array (UTF-8 bytes).
* This is the primary output — pass directly to request.body.
*/
flush(): Uint8Array;
protected ensure(byteCount: number): void;
/**
* Write a raw ASCII string (no JSON escaping). Used for pre-validated content
* like numeric literals and pre-encoded base64.
*/
protected writeAscii(s: string): void;
/**
* Write a quoted ASCII string with no escape checking.
* Used for struct member keys (jsonName or model names) which are
* guaranteed to be safe ASCII identifiers. No control chars, quotes,
* backslashes, or non-ASCII.
* Ensures extra room for surrounding structural chars (comma, colon).
*/
protected writeAsciiQuoted(s: string): void;
/**
* Write a JSON-escaped string including the surrounding quotes.
* Fast-path for ASCII, falls back to TextEncoder for multi-byte.
*/
protected writeJsonString(s: string): void;
protected writeUnicodeEscape(code: number): void;
protected static readonly B64: Uint8Array;
/**
* Write a Uint8Array as a quoted base64 string directly into the buffer.
* No intermediate JS string, no escape checking (base64 alphabet is safe ASCII).
*/
protected writeBase64(data: Uint8Array): void;
protected writeValue(schema: Schema, value: unknown, container: NormalizedSchema | undefined): void;
protected writeStruct(ns: NormalizedSchema, value: Record<string, unknown>): void;
protected writeList(ns: NormalizedSchema, value: unknown[], isDocument?: boolean): void;
protected writeMap(ns: NormalizedSchema, value: Record<string, unknown>, isDocument?: boolean): void;
protected writeTimestamp(ns: NormalizedSchema, value: Date): void;
}
import type { CodecSettings } from "@smithy/types";
/**
* @public
*/
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
import { Codec } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./JsonShapeDeserializer";
import { JsonShapeSerializer } from "./JsonShapeSerializer";
import { JsonSettings } from "./JsonSettings";
export declare class JsonCodec extends SerdeContextConfig implements Codec<string, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): JsonShapeSerializer;
createDeserializer(): JsonShapeDeserializer;
}
import { CodecSettings } from "@smithy/types";
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
import { $ShapeSerializer, $Codec, $ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonSettings";
export declare class JsonCodec2 extends SerdeContextConfig implements $Codec<Uint8Array, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): $ShapeSerializer<Uint8Array>;
createDeserializer(): $ShapeDeserializer<string>;
}
import { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonSettings";
export declare class JsonShapeDeserializer2
extends SerdeContextConfig
implements ShapeDeserializer<string>
{
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
read(schema: Schema, data: string | Uint8Array | unknown): Promise<any>;
readObject(schema: Schema, data: DocumentType): any;
protected _read(schema: Schema, value: unknown): any;
private _readStruct;
}
import { NormalizedSchema } from "@smithy/core/schema";
import { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonSettings";
export declare class JsonShapeSerializer2
extends SerdeContextConfig
implements ShapeSerializer<Uint8Array>
{
readonly settings: JsonSettings;
private json;
private i;
private rootSchema;
private rawValue;
private passthrough;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
flush(): Uint8Array;
protected ensure(byteCount: number): void;
protected writeAscii(s: string): void;
protected writeAsciiQuoted(s: string): void;
protected writeJsonString(s: string): void;
protected writeUnicodeEscape(code: number): void;
protected static readonly B64: Uint8Array;
protected writeBase64(data: Uint8Array): void;
protected writeValue(
schema: Schema,
value: unknown,
container: NormalizedSchema | undefined,
): void;
protected writeStruct(ns: NormalizedSchema, value: Record<string, unknown>): void;
protected writeList(ns: NormalizedSchema, value: unknown[], isDocument?: boolean): void;
protected writeMap(
ns: NormalizedSchema,
value: Record<string, unknown>,
isDocument?: boolean,
): void;
protected writeTimestamp(ns: NormalizedSchema, value: Date): void;
}
import { CodecSettings } from "@smithy/types";
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
+6
-3
export { AwsSmithyRpcV2CborProtocol } from "./cbor/AwsSmithyRpcV2CborProtocol";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
export { AwsJson1_0Protocol } from "./json/AwsJson1_0Protocol";

@@ -7,6 +6,8 @@ export { AwsJson1_1Protocol } from "./json/AwsJson1_1Protocol";

export { AwsRestJsonProtocol } from "./json/AwsRestJsonProtocol";
export { JsonCodec } from "./json/JsonCodec";
export { JsonCodec } from "./json/codec-v1/JsonCodec";
export { JsonShapeDeserializer } from "./json/codec-v1/JsonShapeDeserializer";
export { JsonShapeSerializer } from "./json/codec-v1/JsonShapeSerializer";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { JsonCodec2 } from "./json/codec-v2/JsonCodec2";
export { JsonShapeDeserializer2 } from "./json/codec-v2/JsonShapeDeserializer2";
export { JsonShapeSerializer2 } from "./json/codec-v2/JsonShapeSerializer2";
export { parseJsonBody, parseJsonErrorBody, loadRestJsonErrorCode, loadJsonRpcErrorCode } from "./json/parseJsonBody";

@@ -21,1 +22,3 @@ export { AwsEc2QueryProtocol } from "./query/AwsEc2QueryProtocol";

export { parseXmlBody, parseXmlErrorBody, loadRestXmlErrorCode } from "./xml/parseXmlBody";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
import { RpcProtocol } from "@smithy/core/protocols";
import { deref, NormalizedSchema } from "@smithy/core/schema";
import { ProtocolLib } from "../ProtocolLib";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
import { loadJsonRpcErrorCode } from "./parseJsonBody";

@@ -6,0 +6,0 @@ export class AwsJsonRpcProtocol extends RpcProtocol {

import { HttpBindingProtocol, HttpInterceptingShapeDeserializer, HttpInterceptingShapeSerializer, } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { ProtocolLib } from "../ProtocolLib";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
import { loadRestJsonErrorCode } from "./parseJsonBody";

@@ -6,0 +6,0 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {

export { AwsSmithyRpcV2CborProtocol } from "./cbor/AwsSmithyRpcV2CborProtocol";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
export { AwsJson1_0Protocol } from "./json/AwsJson1_0Protocol";

@@ -7,7 +6,9 @@ export { AwsJson1_1Protocol } from "./json/AwsJson1_1Protocol";

export { AwsRestJsonProtocol } from "./json/AwsRestJsonProtocol";
export { JsonCodec } from "./json/JsonCodec";
export type { JsonSettings } from "./json/JsonCodec";
export { JsonCodec } from "./json/codec-v1/JsonCodec";
export type { JsonSettings } from "./json/JsonSettings";
export { JsonShapeDeserializer } from "./json/codec-v1/JsonShapeDeserializer";
export { JsonShapeSerializer } from "./json/codec-v1/JsonShapeSerializer";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { JsonCodec2 } from "./json/codec-v2/JsonCodec2";
export { JsonShapeDeserializer2 } from "./json/codec-v2/JsonShapeDeserializer2";
export { JsonShapeSerializer2 } from "./json/codec-v2/JsonShapeSerializer2";
export { parseJsonBody, parseJsonErrorBody, loadRestJsonErrorCode, loadJsonRpcErrorCode } from "./json/parseJsonBody";

@@ -24,1 +25,3 @@ export { AwsEc2QueryProtocol } from "./query/AwsEc2QueryProtocol";

export { parseXmlBody, parseXmlErrorBody, loadRestXmlErrorCode } from "./xml/parseXmlBody";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
import type { TypeRegistry } from "@smithy/core/schema";
import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol";
import type { JsonCodec } from "./JsonCodec";
import type { JsonCodec } from "./codec-v1/JsonCodec";
/**

@@ -5,0 +5,0 @@ * @public

import type { TypeRegistry } from "@smithy/core/schema";
import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol";
import type { JsonCodec } from "./JsonCodec";
import type { JsonCodec } from "./codec-v1/JsonCodec";
/**

@@ -5,0 +5,0 @@ * @public

import { RpcProtocol } from "@smithy/core/protocols";
import type { TypeRegistry } from "@smithy/core/schema";
import type { EndpointBearer, HandlerExecutionContext, HttpRequest, HttpResponse, OperationSchema, ResponseMetadata, SerdeFunctions, ShapeDeserializer, ShapeSerializer } from "@smithy/types";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
/**

@@ -6,0 +6,0 @@ * @public

import { HttpBindingProtocol } from "@smithy/core/protocols";
import type { TypeRegistry } from "@smithy/core/schema";
import type { EndpointBearer, HandlerExecutionContext, HttpRequest, HttpResponse, MetadataBearer, OperationSchema, ResponseMetadata, SerdeFunctions, ShapeDeserializer, ShapeSerializer } from "@smithy/types";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
/**

@@ -6,0 +6,0 @@ * @public

import type { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonCodec";
import type { JsonSettings } from "./JsonSettings";
/**
* @deprecated prefer JsonShapeDeserializer in codec-v2.
* @public

@@ -6,0 +7,0 @@ */

import { NormalizedSchema } from "@smithy/core/schema";
import type { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonCodec";
import type { JsonSettings } from "./JsonSettings";
/**
* @deprecated prefer byte-targeting JsonShapeSerializer or its string adapter StringJsonShapeSerializer in codec-v2.
* @public

@@ -7,0 +8,0 @@ */

export { AwsSmithyRpcV2CborProtocol } from "./cbor/AwsSmithyRpcV2CborProtocol";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
export { AwsJson1_0Protocol } from "./json/AwsJson1_0Protocol";

@@ -7,7 +6,9 @@ export { AwsJson1_1Protocol } from "./json/AwsJson1_1Protocol";

export { AwsRestJsonProtocol } from "./json/AwsRestJsonProtocol";
export { JsonCodec } from "./json/JsonCodec";
export { JsonSettings } from "./json/JsonCodec";
export { JsonCodec } from "./json/codec-v1/JsonCodec";
export { JsonSettings } from "./json/JsonSettings";
export { JsonShapeDeserializer } from "./json/codec-v1/JsonShapeDeserializer";
export { JsonShapeSerializer } from "./json/codec-v1/JsonShapeSerializer";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { JsonCodec2 } from "./json/codec-v2/JsonCodec2";
export { JsonShapeDeserializer2 } from "./json/codec-v2/JsonShapeDeserializer2";
export { JsonShapeSerializer2 } from "./json/codec-v2/JsonShapeSerializer2";
export {

@@ -29,1 +30,3 @@ parseJsonBody,

export { parseXmlBody, parseXmlErrorBody, loadRestXmlErrorCode } from "./xml/parseXmlBody";
export { awsExpectUnion } from "./json/awsExpectUnion";
export { _toStr, _toBool, _toNum } from "./coercing-serializers";
import { TypeRegistry } from "@smithy/core/schema";
import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
export declare class AwsJson1_0Protocol extends AwsJsonRpcProtocol {

@@ -5,0 +5,0 @@ constructor({

import { TypeRegistry } from "@smithy/core/schema";
import { AwsJsonRpcProtocol } from "./AwsJsonRpcProtocol";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
export declare class AwsJson1_1Protocol extends AwsJsonRpcProtocol {

@@ -5,0 +5,0 @@ constructor({

@@ -14,3 +14,3 @@ import { RpcProtocol } from "@smithy/core/protocols";

} from "@smithy/types";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
export declare abstract class AwsJsonRpcProtocol extends RpcProtocol {

@@ -17,0 +17,0 @@ protected serializer: ShapeSerializer<string | Uint8Array>;

@@ -15,3 +15,3 @@ import { HttpBindingProtocol } from "@smithy/core/protocols";

} from "@smithy/types";
import { JsonCodec } from "./JsonCodec";
import { JsonCodec } from "./codec-v1/JsonCodec";
export declare class AwsRestJsonProtocol extends HttpBindingProtocol {

@@ -18,0 +18,0 @@ protected serializer: ShapeSerializer<string | Uint8Array>;

import { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonCodec";
import { JsonSettings } from "./JsonSettings";
export declare class JsonShapeDeserializer

@@ -5,0 +5,0 @@ extends SerdeContextConfig

import { NormalizedSchema } from "@smithy/core/schema";
import { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonCodec";
import { JsonSettings } from "./JsonSettings";
export declare class JsonShapeSerializer

@@ -6,0 +6,0 @@ extends SerdeContextConfig

{
"name": "@aws-sdk/core",
"version": "3.977.2",
"version": "3.977.3",
"description": "Core functions & classes shared by multiple AWS SDK clients.",

@@ -130,4 +130,4 @@ "homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages-internal/core",

"@aws/lambda-invoke-store": "^0.3.0",
"@smithy/core": "^3.29.8",
"@smithy/signature-v4": "^5.6.9",
"@smithy/core": "^3.31.1",
"@smithy/signature-v4": "^5.6.12",
"@smithy/types": "^4.16.1",

@@ -134,0 +134,0 @@ "bowser": "^2.11.0",

import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { LazyJsonString, NumericValue, parseEpochTimestamp, parseRfc3339DateTimeWithOffset, parseRfc7231DateTime, } from "@smithy/core/serde";
import { fromBase64 } from "@smithy/core/serde";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { UnionSerde } from "../../UnionSerde";
import { detectBufferParsing } from "../detectBufferParsing";
import { jsonReviver } from "../jsonReviver";
import { needsReviver } from "../needsReviver";
import { parseJsonBody } from "../parseJsonBody";
import { writeKey } from "../../writeKey";
export class BufferJsonShapeDeserializer extends SerdeContextConfig {
settings;
constructor(settings) {
super();
this.settings = settings;
}
async read(schema, data) {
const reviver = needsReviver(schema) ? jsonReviver : undefined;
let parsed;
if (typeof data === "string") {
parsed = JSON.parse(data, reviver);
}
else if (data instanceof Uint8Array && detectBufferParsing()) {
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
parsed = JSON.parse(buf, reviver);
}
else {
parsed = await parseJsonBody(data, this.serdeContext);
}
return this._read(schema, parsed);
}
readObject(schema, data) {
return this._read(schema, data);
}
_read(schema, value) {
const isObject = value !== null && typeof value === "object";
const ns = NormalizedSchema.of(schema);
if (isObject) {
if (ns.isStructSchema()) {
return this._readStruct(ns, value);
}
if (Array.isArray(value) && ns.isListSchema()) {
const listMember = ns.getValueSchema();
for (let i = 0; i < value.length; ++i) {
value[i] = this._read(listMember, value[i]);
}
return value;
}
if (ns.isMapSchema()) {
const mapMember = ns.getValueSchema();
const map = value;
for (const k in map) {
if (k === "__proto__") {
writeKey(map);
}
map[k] = this._read(mapMember, map[k]);
}
return map;
}
}
if (ns.isBlobSchema() && typeof value === "string") {
return fromBase64(value);
}
const mediaType = ns.getMergedTraits().mediaType;
if (ns.isStringSchema() && typeof value === "string" && mediaType) {
const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
if (isJson) {
return LazyJsonString.from(value);
}
return value;
}
if (ns.isTimestampSchema() && value != null) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case 5:
return parseRfc3339DateTimeWithOffset(value);
case 6:
return parseRfc7231DateTime(value);
case 7:
return parseEpochTimestamp(value);
default:
console.warn("Missing timestamp format, parsing value with Date constructor:", value);
return new Date(value);
}
}
if (ns.isBigIntegerSchema() && (typeof value === "number" || typeof value === "string")) {
return BigInt(value);
}
if (ns.isBigDecimalSchema() && value != undefined) {
if (value instanceof NumericValue) {
return value;
}
const untyped = value;
if (untyped.type === "bigDecimal" && "string" in untyped) {
return new NumericValue(untyped.string, untyped.type);
}
return new NumericValue(String(value), "bigDecimal");
}
if (ns.isNumericSchema() && typeof value === "string") {
switch (value) {
case "Infinity":
return Infinity;
case "-Infinity":
return -Infinity;
case "NaN":
return NaN;
}
return value;
}
if (ns.isDocumentSchema()) {
if (isObject) {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; ++i) {
const v = value[i];
if (!(v instanceof NumericValue)) {
value[i] = this._read(ns, v);
}
}
}
else {
const doc = value;
for (const k in doc) {
if (k === "__proto__") {
writeKey(doc);
}
const v = doc[k];
if (!(v instanceof NumericValue)) {
doc[k] = this._read(ns, v);
}
}
}
return value;
}
else {
return value;
}
}
return value;
}
_readStruct(ns, record) {
const union = ns.isUnionSchema();
const out = {};
let nameMap = void 0;
const { jsonName } = this.settings;
if (jsonName) {
nameMap = {};
}
let unionSerde;
if (union) {
unionSerde = new UnionSerde(record, out);
}
for (const [memberName, memberSchema] of ns.structIterator()) {
let fromKey = memberName;
if (jsonName) {
fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey;
nameMap[fromKey] = memberName;
}
if (union) {
unionSerde.mark(fromKey);
}
if (record[fromKey] != null) {
out[memberName] = this._read(memberSchema, record[fromKey]);
}
}
if (union) {
unionSerde.writeUnknown();
}
else if (typeof record.__type === "string") {
for (const k in record) {
const v = record[k];
const t = jsonName ? (nameMap[k] ?? k) : k;
if (!(t in out)) {
out[t] = v;
}
}
}
return out;
}
}
import { determineTimestampFormat } from "@smithy/core/protocols";
import { NormalizedSchema } from "@smithy/core/schema";
import { dateToUtcString, generateIdempotencyToken, LazyJsonString, NumericValue, toBase64, toUtf8, } from "@smithy/core/serde";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { writeKey } from "../../writeKey";
const encoder = new TextEncoder();
const OPEN_BRACE = 0x7b;
const CLOSE_BRACE = 0x7d;
const OPEN_BRACKET = 0x5b;
const CLOSE_BRACKET = 0x5d;
const QUOTE = 0x22;
const COLON = 0x3a;
const COMMA = 0x2c;
const BACKSLASH = 0x5c;
const TRUE = new Uint8Array([0x74, 0x72, 0x75, 0x65]);
const FALSE = new Uint8Array([0x66, 0x61, 0x6c, 0x73, 0x65]);
const NULL = new Uint8Array([0x6e, 0x75, 0x6c, 0x6c]);
const ESCAPE_TABLE = new Array(128).fill(null);
ESCAPE_TABLE[0x08] = "b";
ESCAPE_TABLE[0x09] = "t";
ESCAPE_TABLE[0x0a] = "n";
ESCAPE_TABLE[0x0c] = "f";
ESCAPE_TABLE[0x0d] = "r";
ESCAPE_TABLE[0x22] = '"';
ESCAPE_TABLE[0x5c] = "\\";
for (let i = 0; i < 0x20; i++) {
if (ESCAPE_TABLE[i] === null) {
ESCAPE_TABLE[i] = "u00" + i.toString(16).padStart(2, "0");
}
}
const INITIAL_BUFFER_SIZE = 2048;
function alloc(size) {
return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size);
}
export class ByteJsonShapeSerializer extends SerdeContextConfig {
settings;
json;
i = 0;
rootSchema;
constructor(settings) {
super();
this.settings = settings;
this.json = alloc(INITIAL_BUFFER_SIZE);
}
write(schema, value) {
this.i = 0;
this.rootSchema = NormalizedSchema.of(schema);
this.writeValue(this.rootSchema, value, undefined);
}
writeDiscriminatedDocument(schema, value) {
this.i = 0;
this.rootSchema = NormalizedSchema.of(schema);
const ns = this.rootSchema;
if (ns.isStructSchema() && value != null && typeof value === "object") {
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
this.writeAsciiQuoted("__type");
this.json[this.i++] = COLON;
this.writeAsciiQuoted(ns.getName(true) ?? "Unknown");
let wroteAny = true;
const { jsonName } = this.settings;
for (const [memberName, memberSchema] of ns.structIterator()) {
const item = value[memberName];
if (item == null && !memberSchema.isIdempotencyToken()) {
continue;
}
if (wroteAny) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
const targetKey = jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
this.writeAsciiQuoted(targetKey);
this.json[this.i++] = COLON;
this.writeValue(memberSchema, item, ns);
wroteAny = true;
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
else {
this.writeValue(ns, value, undefined);
}
}
flush() {
this.rootSchema = undefined;
const finalPosition = this.i;
this.i = 0;
const result = this.json.subarray(0, finalPosition);
this.json = alloc(INITIAL_BUFFER_SIZE);
return result;
}
ensure(byteCount) {
const { i, json } = this;
if (i + byteCount > json.length) {
let newSize = json.length * 2;
while (newSize < i + byteCount) {
newSize *= 2;
}
const next = alloc(newSize);
next.set(this.json);
this.json = next;
}
}
writeAscii(s) {
const z = s.length;
this.ensure(z);
let { i, json } = this;
for (let j = 0; j < z; ++j) {
json[i] = s.charCodeAt(j);
i += 1;
}
this.i = i;
}
writeAsciiQuoted(s) {
const z = s.length;
this.ensure(z + 4);
let { json, i } = this;
json[i++] = QUOTE;
for (let j = 0; j < z; ++j) {
json[i++] = s.charCodeAt(j);
}
json[i++] = QUOTE;
this.i = i;
}
writeJsonString(s) {
this.ensure(s.length * 2 + 2);
this.json[this.i++] = QUOTE;
const z = s.length;
for (let j = 0; j < z; ++j) {
const c = s.charCodeAt(j);
if (c > 0x22 && c < 0x5c) {
this.json[this.i++] = c;
}
else if (c < 0x80) {
const esc = ESCAPE_TABLE[c];
if (esc !== null) {
this.ensure(esc.length + 1);
this.json[this.i++] = BACKSLASH;
for (let k = 0; k < esc.length; k++) {
this.json[this.i++] = esc.charCodeAt(k);
}
}
else {
this.json[this.i++] = c;
}
}
else if (c >= 0xd800 && c <= 0xdbff) {
const next = j + 1 < z ? s.charCodeAt(j + 1) : 0;
if (next >= 0xdc00 && next <= 0xdfff) {
this.ensure(4);
const { written } = encoder.encodeInto(s.substring(j, j + 2), this.json.subarray(this.i));
this.i += written;
j++;
}
else {
this.ensure(6);
this.writeUnicodeEscape(c);
}
}
else if (c >= 0xdc00 && c <= 0xdfff) {
this.ensure(6);
this.writeUnicodeEscape(c);
}
else {
let { i, json } = this;
if (c < 0x800) {
json[i++] = 0xc0 | (c >> 6);
json[i++] = 0x80 | (c & 0x3f);
}
else {
json[i++] = 0xe0 | (c >> 12);
json[i++] = 0x80 | ((c >> 6) & 0x3f);
json[i++] = 0x80 | (c & 0x3f);
}
this.i = i;
}
}
this.json[this.i++] = QUOTE;
}
writeUnicodeEscape(code) {
let { json, i } = this;
json[i++] = BACKSLASH;
json[i++] = 0x75;
const hex = code.toString(16).padStart(4, "0");
for (let j = 0; j < 4; ++j) {
json[i++] = hex.charCodeAt(j);
}
this.i = i;
}
static B64 = (() => {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const table = new Uint8Array(64);
for (let i = 0; i < 64; i++)
table[i] = chars.charCodeAt(i);
return table;
})();
writeBase64(data) {
const b64Len = Math.ceil(data.length / 3) * 4;
this.ensure(b64Len + 2);
const json = this.json;
const B64 = ByteJsonShapeSerializer.B64;
let i = this.i;
json[i++] = QUOTE;
const len = data.length;
const remainder = len % 3;
const mainLen = len - remainder;
for (let j = 0; j < mainLen; j += 3) {
const a = data[j];
const b = data[j + 1];
const c = data[j + 2];
json[i++] = B64[a >> 2];
json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
json[i++] = B64[((b & 0x0f) << 2) | (c >> 6)];
json[i++] = B64[c & 0x3f];
}
if (remainder === 2) {
const a = data[mainLen];
const b = data[mainLen + 1];
json[i++] = B64[a >> 2];
json[i++] = B64[((a & 0x03) << 4) | (b >> 4)];
json[i++] = B64[(b & 0x0f) << 2];
json[i++] = 0x3d;
}
else if (remainder === 1) {
const a = data[mainLen];
json[i++] = B64[a >> 2];
json[i++] = B64[(a & 0x03) << 4];
json[i++] = 0x3d;
json[i++] = 0x3d;
}
json[i++] = QUOTE;
this.i = i;
}
writeValue(schema, value, container) {
if (value == null) {
if (container?.isStructSchema()) {
if (value === undefined) {
const ns = NormalizedSchema.of(schema);
if (ns.isIdempotencyToken()) {
this.writeAsciiQuoted(generateIdempotencyToken());
return;
}
}
return;
}
this.ensure(4);
this.json.set(NULL, this.i);
this.i += 4;
return;
}
const ns = NormalizedSchema.of(schema);
const isObject = typeof value === "object";
if (isObject) {
if (ns.isStructSchema()) {
this.writeStruct(ns, value);
return;
}
if (Array.isArray(value) && (ns.isListSchema() || ns.isDocumentSchema())) {
this.writeList(ns, value, ns.isDocumentSchema());
return;
}
if (ns.isMapSchema()) {
this.writeMap(ns, value, false);
return;
}
if (value instanceof Uint8Array && (ns.isBlobSchema() || ns.isDocumentSchema())) {
this.writeBase64(value);
return;
}
if (value instanceof Date && (ns.isTimestampSchema() || ns.isDocumentSchema())) {
this.writeTimestamp(ns, value);
return;
}
if (value instanceof NumericValue) {
this.writeAscii(value.string);
return;
}
if (ns.isDocumentSchema()) {
if (Array.isArray(value)) {
this.writeList(ns, value, true);
}
else {
this.writeMap(ns, value, true);
}
return;
}
const json = JSON.stringify(value);
this.writeAscii(json);
return;
}
if (typeof value === "string") {
if (ns.isStringSchema()) {
const mediaType = ns.getMergedTraits().mediaType;
if (mediaType) {
const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
if (isJson) {
this.writeJsonString(LazyJsonString.from(value).toString());
return;
}
}
}
if (ns.isBlobSchema()) {
const b64 = (this.serdeContext?.base64Encoder ?? toBase64)(value);
this.writeAsciiQuoted(b64);
return;
}
this.writeJsonString(value);
return;
}
if (typeof value === "number") {
if (ns.isNumericSchema() && (Math.abs(value) === Infinity || isNaN(value))) {
this.writeAsciiQuoted(String(value));
return;
}
const numStr = String(value);
this.writeAscii(numStr);
return;
}
if (typeof value === "boolean") {
this.ensure(5);
if (value) {
this.json.set(TRUE, this.i);
this.i += 4;
}
else {
this.json.set(FALSE, this.i);
this.i += 5;
}
return;
}
if (typeof value === "bigint") {
this.writeAscii(value.toString());
return;
}
this.writeAscii(String(value));
}
writeStruct(ns, value) {
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
let first = true;
let wroteAny = false;
const hasType = typeof value.__type === "string";
let writtenKeys;
if (hasType) {
writtenKeys = new Set();
}
for (const [memberName, memberSchema] of ns.structIterator()) {
const item = value[memberName];
if (item == null && !memberSchema.isIdempotencyToken())
continue;
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
wroteAny = true;
const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName;
if (writtenKeys) {
writtenKeys.add(memberName);
writtenKeys.add(targetKey);
}
this.writeAsciiQuoted(targetKey);
this.json[this.i++] = COLON;
this.writeValue(memberSchema, item, ns);
}
if (!wroteAny && ns.isUnionSchema()) {
const { $unknown } = value;
if (Array.isArray($unknown)) {
const [k, v] = $unknown;
this.writeAsciiQuoted(k);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(15, v, ns);
}
}
else if (hasType) {
for (const k in value) {
const targetKey = this.settings.jsonName ? (writtenKeys.has(k) ? k : k) : k;
if (writtenKeys.has(targetKey))
continue;
writtenKeys.add(targetKey);
const v = value[k];
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
this.writeAsciiQuoted(targetKey);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(15, v, undefined);
}
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
writeList(ns, value, isDocument) {
this.ensure(2);
this.json[this.i++] = OPEN_BRACKET;
const sparse = !!ns.getMergedTraits().sparse;
const valueSchema = ns.getValueSchema();
for (let i = 0; i < value.length; ++i) {
const item = value[i];
if (isDocument ? item === undefined : item == null && !sparse) {
continue;
}
if (i !== 0) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
this.writeValue(valueSchema, item, undefined);
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACKET;
}
writeMap(ns, value, isDocument) {
const sparse = !!ns.getMergedTraits().sparse;
const valueSchema = ns.getValueSchema();
if (!isDocument) {
if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) {
let input = value;
if (sparse) {
input = {};
for (const k in value) {
if (k === "__proto__") {
writeKey(input);
}
input[k] = value[k] ?? null;
}
}
const json = JSON.stringify(input);
this.ensure(json.length * 3);
const { written } = encoder.encodeInto(json, this.json.subarray(this.i));
this.i += written;
return;
}
}
this.ensure(2);
this.json[this.i++] = OPEN_BRACE;
let first = true;
for (const k in value) {
const v = value[k];
if (isDocument ? v === undefined : v == null && !sparse) {
continue;
}
if (!first) {
this.ensure(1);
this.json[this.i++] = COMMA;
}
first = false;
this.writeJsonString(k);
this.ensure(1);
this.json[this.i++] = COLON;
this.writeValue(valueSchema, v, undefined);
}
this.ensure(1);
this.json[this.i++] = CLOSE_BRACE;
}
writeTimestamp(ns, value) {
const format = determineTimestampFormat(ns, this.settings);
switch (format) {
case 5: {
const iso = value.toISOString().replace(".000Z", "Z");
this.writeAsciiQuoted(iso);
return;
}
case 6: {
this.writeAsciiQuoted(dateToUtcString(value));
return;
}
case 7: {
const epochSecs = String(value.getTime() / 1000);
this.writeAscii(epochSecs);
return;
}
default: {
const epochSecs = String(value.getTime() / 1000);
this.writeAscii(epochSecs);
return;
}
}
}
}
export class StringJsonShapeSerializer extends SerdeContextConfig {
settings;
byteSerializer;
constructor(settings) {
super();
this.settings = settings;
this.byteSerializer = new ByteJsonShapeSerializer(settings);
}
write(schema, value) {
this.byteSerializer.write(schema, value);
}
writeDiscriminatedDocument(schema, value) {
this.byteSerializer.writeDiscriminatedDocument(schema, value);
}
flush() {
return (this.serdeContext?.utf8Encoder ?? toUtf8)(this.byteSerializer.flush());
}
}
import { SerdeContextConfig } from "../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./codec-v1/JsonShapeDeserializer";
import { JsonShapeSerializer } from "./codec-v1/JsonShapeSerializer";
export class JsonCodec extends SerdeContextConfig {
settings;
constructor(settings) {
super();
this.settings = settings;
}
createSerializer() {
const serializer = new JsonShapeSerializer(this.settings);
serializer.setSerdeContext(this.serdeContext);
return serializer;
}
createDeserializer() {
const deserializer = new JsonShapeDeserializer(this.settings);
deserializer.setSerdeContext(this.serdeContext);
return deserializer;
}
}
import type { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonCodec";
/**
* Performance-optimized JSON deserializer.
*
* Skips UTF-8 decoding when the runtime supports JSON.parse(Buffer) (Node 22+).
*
* After JSON.parse, lists, maps, and document containers are mutated in place
* (element values are overwritten with their deserialized form) rather than
* copied into new arrays/objects. Structs allocate a fresh object because
* jsonName traits require key renaming, and building the output object
* incrementally lets V8 assign a stable hidden class rather than
* deoptimizing from repeated property deletion/addition on an existing shape.
*
* In-place mutation is safe here because the parsed tree is locally owned
* after JSON.parse with no external references, so rewriting values avoids
* redundant allocation and GC pressure.
*
* @internal
*/
export declare class BufferJsonShapeDeserializer extends SerdeContextConfig implements ShapeDeserializer<string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
read(schema: Schema, data: string | Uint8Array | unknown): Promise<any>;
readObject(schema: Schema, data: DocumentType): any;
protected _read(schema: Schema, value: unknown): any;
private _readStruct;
}
import type { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import type { JsonSettings } from "../JsonCodec";
/**
* Experimental single-pass JSON serializer that writes directly to a Uint8Array buffer.
* Fewer intermediate states as when compared to the initial multi-pass implementation.
*
* @internal
*/
export declare class ByteJsonShapeSerializer extends SerdeContextConfig implements ShapeSerializer<Uint8Array> {
readonly settings: JsonSettings;
private json;
private i;
private rootSchema;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
/**
* @internal
*/
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
/**
* Returns the serialized JSON as a Uint8Array (UTF-8 bytes).
* This is the primary output — pass directly to request.body.
*/
flush(): Uint8Array;
private ensure;
/**
* Write a raw ASCII string (no JSON escaping). Used for pre-validated content
* like numeric literals and pre-encoded base64.
*/
private writeAscii;
/**
* Write a quoted ASCII string with no escape checking.
* Used for struct member keys (jsonName or model names) which are
* guaranteed to be safe ASCII identifiers. No control chars, quotes,
* backslashes, or non-ASCII.
* Ensures extra room for surrounding structural chars (comma, colon).
*/
private writeAsciiQuoted;
/**
* Write a JSON-escaped string including the surrounding quotes.
* Fast-path for ASCII, falls back to TextEncoder for multi-byte.
*/
private writeJsonString;
private writeUnicodeEscape;
private static readonly B64;
/**
* Write a Uint8Array as a quoted base64 string directly into the buffer.
* No intermediate JS string, no escape checking (base64 alphabet is safe ASCII).
*/
private writeBase64;
private writeValue;
private writeStruct;
private writeList;
private writeMap;
private writeTimestamp;
}
/**
* A string adapter for the byte serializer, for backwards compatibility.
* @public
*/
export declare class StringJsonShapeSerializer extends SerdeContextConfig implements ShapeSerializer<string> {
readonly settings: JsonSettings;
private byteSerializer;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
/**
* @internal
*/
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
flush(): string;
}
import type { Codec, CodecSettings } from "@smithy/types";
import { SerdeContextConfig } from "../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./codec-v1/JsonShapeDeserializer";
import { JsonShapeSerializer } from "./codec-v1/JsonShapeSerializer";
/**
* @public
*/
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
/**
* @public
*/
export declare class JsonCodec extends SerdeContextConfig implements Codec<string, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): JsonShapeSerializer;
createDeserializer(): JsonShapeDeserializer;
}
import { DocumentType, Schema, ShapeDeserializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonCodec";
export declare class BufferJsonShapeDeserializer
extends SerdeContextConfig
implements ShapeDeserializer<string>
{
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
read(schema: Schema, data: string | Uint8Array | unknown): Promise<any>;
readObject(schema: Schema, data: DocumentType): any;
protected _read(schema: Schema, value: unknown): any;
private _readStruct;
}
import { Schema, ShapeSerializer } from "@smithy/types";
import { SerdeContextConfig } from "../../ConfigurableSerdeContext";
import { JsonSettings } from "../JsonCodec";
export declare class ByteJsonShapeSerializer
extends SerdeContextConfig
implements ShapeSerializer<Uint8Array>
{
readonly settings: JsonSettings;
private json;
private i;
private rootSchema;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
flush(): Uint8Array;
private ensure;
private writeAscii;
private writeAsciiQuoted;
private writeJsonString;
private writeUnicodeEscape;
private static readonly B64;
private writeBase64;
private writeValue;
private writeStruct;
private writeList;
private writeMap;
private writeTimestamp;
}
export declare class StringJsonShapeSerializer
extends SerdeContextConfig
implements ShapeSerializer<string>
{
readonly settings: JsonSettings;
private byteSerializer;
constructor(settings: JsonSettings);
write(schema: Schema, value: unknown): void;
writeDiscriminatedDocument(schema: Schema, value: unknown): void;
flush(): string;
}
import { Codec, CodecSettings } from "@smithy/types";
import { SerdeContextConfig } from "../ConfigurableSerdeContext";
import { JsonShapeDeserializer } from "./codec-v1/JsonShapeDeserializer";
import { JsonShapeSerializer } from "./codec-v1/JsonShapeSerializer";
export type JsonSettings = CodecSettings & {
jsonName: boolean;
};
export declare class JsonCodec extends SerdeContextConfig implements Codec<string, string> {
readonly settings: JsonSettings;
constructor(settings: JsonSettings);
createSerializer(): JsonShapeSerializer;
createDeserializer(): JsonShapeDeserializer;
}

Sorry, the diff of this file is too big to display