@aws-sdk/core
Advanced tools
| import { toUtf8 } from "@smithy/core/serde"; | ||
| export class JsonBytesStringAdapter extends Uint8Array { | ||
| string = null; | ||
| static allocUnsafe(bytes) { | ||
| if (typeof Buffer === "function") { | ||
| const buffer = Buffer.allocUnsafe(bytes); | ||
| return new JsonBytesStringAdapter(buffer.buffer, buffer.byteOffset, buffer.byteLength); | ||
| } | ||
| return new JsonBytesStringAdapter(bytes); | ||
| } | ||
| toString() { | ||
| return this.s(); | ||
| } | ||
| valueOf() { | ||
| return this.s(); | ||
| } | ||
| includes(searchString, position) { | ||
| if (typeof searchString === "string") { | ||
| return this.s().includes(searchString, position); | ||
| } | ||
| return Uint8Array.prototype.includes.call(this, searchString, position); | ||
| } | ||
| indexOf(searchString, position) { | ||
| if (typeof searchString === "string") { | ||
| return this.s().indexOf(searchString, position); | ||
| } | ||
| return Uint8Array.prototype.indexOf.call(this, searchString, position); | ||
| } | ||
| lastIndexOf(searchString, position) { | ||
| if (typeof searchString === "string") { | ||
| return this.s().lastIndexOf(searchString, position); | ||
| } | ||
| const fn = Uint8Array.prototype.lastIndexOf; | ||
| if (position !== undefined) { | ||
| return fn.call(this, searchString, position); | ||
| } | ||
| return fn.call(this, searchString); | ||
| } | ||
| startsWith(searchString, position) { | ||
| return this.s().startsWith(searchString, position); | ||
| } | ||
| endsWith(searchString, endPosition) { | ||
| return this.s().endsWith(searchString, endPosition); | ||
| } | ||
| match(regexp) { | ||
| return this.s().match(regexp); | ||
| } | ||
| replace(searchValue, replaceValue) { | ||
| return this.s().replace(searchValue, replaceValue); | ||
| } | ||
| search(regexp) { | ||
| return this.s().search(regexp); | ||
| } | ||
| split(separator, limit) { | ||
| return this.s().split(separator, limit); | ||
| } | ||
| substring(start, end) { | ||
| return this.s().substring(start, end); | ||
| } | ||
| trim() { | ||
| return this.s().trim(); | ||
| } | ||
| trimStart() { | ||
| return this.s().trimStart(); | ||
| } | ||
| trimEnd() { | ||
| return this.s().trimEnd(); | ||
| } | ||
| charAt(pos) { | ||
| return this.s().charAt(pos); | ||
| } | ||
| charCodeAt(index) { | ||
| return this.s().charCodeAt(index); | ||
| } | ||
| padStart(maxLength, fillString) { | ||
| return this.s().padStart(maxLength, fillString); | ||
| } | ||
| padEnd(maxLength, fillString) { | ||
| return this.s().padEnd(maxLength, fillString); | ||
| } | ||
| repeat(count) { | ||
| return this.s().repeat(count); | ||
| } | ||
| toUpperCase() { | ||
| return this.s().toUpperCase(); | ||
| } | ||
| toLowerCase() { | ||
| return this.s().toLowerCase(); | ||
| } | ||
| s() { | ||
| if (this.string == null) { | ||
| const n = Date.now(); | ||
| if (n > warned + 60_000) { | ||
| console.warn("@aws-sdk/core/protocols - WARN - JsonCodec2: you have called a string method on a Uint8Array request body. " + | ||
| "It has been automatically converted to string. In a future version this will throw an error."); | ||
| warned = n; | ||
| } | ||
| this.string = toUtf8(this); | ||
| } | ||
| return this.string; | ||
| } | ||
| } | ||
| var warned = 0; |
| /** | ||
| * A Uint8Array subclass holding serialized JSON bytes that provides | ||
| * string method compatibility for customer middleware. | ||
| * | ||
| * Middleware historically observed `request.body` as a string for JSON | ||
| * protocol services. This class preserves that contract: string methods | ||
| * and coercion (template literals, `+` operator) lazily decode the bytes | ||
| * to a cached string. The HTTP transport layer still sees a Uint8Array | ||
| * and sends raw bytes without conversion. | ||
| * | ||
| * @internal | ||
| * @deprecated this adapter will be removed in a future version. | ||
| */ | ||
| export declare class JsonBytesStringAdapter extends Uint8Array { | ||
| private string; | ||
| static allocUnsafe(bytes: number): JsonBytesStringAdapter; | ||
| /** | ||
| * Enables string coercion via template literals and `+` operator. | ||
| */ | ||
| toString(): string; | ||
| /** | ||
| * Enables string coercion in comparison and arithmetic contexts. | ||
| * @returns a string. The signature is set to avoid incompatibility with extending Uint8Array. | ||
| */ | ||
| valueOf(): any; | ||
| includes(searchString: string | number, position?: number): boolean; | ||
| indexOf(searchString: string | number, position?: number): number; | ||
| lastIndexOf(searchString: string | number, position?: number): number; | ||
| startsWith(searchString: string, position?: number): boolean; | ||
| endsWith(searchString: string, endPosition?: number): boolean; | ||
| match(regexp: string | RegExp): RegExpMatchArray | null; | ||
| replace(searchValue: string | RegExp, replaceValue: string): string; | ||
| search(regexp: string | RegExp): number; | ||
| split(separator: string | RegExp, limit?: number): string[]; | ||
| substring(start: number, end?: number): string; | ||
| trim(): string; | ||
| trimStart(): string; | ||
| trimEnd(): string; | ||
| charAt(pos: number): string; | ||
| charCodeAt(index: number): number; | ||
| padStart(maxLength: number, fillString?: string): string; | ||
| padEnd(maxLength: number, fillString?: string): string; | ||
| repeat(count: number): string; | ||
| toUpperCase(): string; | ||
| toLowerCase(): string; | ||
| /** | ||
| * Decodes the bytes to a UTF-8 string on first access, then caches. | ||
| * Subsequent calls return the cached string with zero cost. | ||
| */ | ||
| private s; | ||
| } |
| export declare class JsonBytesStringAdapter extends Uint8Array { | ||
| private string; | ||
| static allocUnsafe(bytes: number): JsonBytesStringAdapter; | ||
| toString(): string; | ||
| valueOf(): any; | ||
| includes(searchString: string | number, position?: number): boolean; | ||
| indexOf(searchString: string | number, position?: number): number; | ||
| lastIndexOf(searchString: string | number, position?: number): number; | ||
| startsWith(searchString: string, position?: number): boolean; | ||
| endsWith(searchString: string, endPosition?: number): boolean; | ||
| match(regexp: string | RegExp): RegExpMatchArray | null; | ||
| replace(searchValue: string | RegExp, replaceValue: string): string; | ||
| search(regexp: string | RegExp): number; | ||
| split(separator: string | RegExp, limit?: number): string[]; | ||
| substring(start: number, end?: number): string; | ||
| trim(): string; | ||
| trimStart(): string; | ||
| trimEnd(): string; | ||
| charAt(pos: number): string; | ||
| charCodeAt(index: number): number; | ||
| padStart(maxLength: number, fillString?: string): string; | ||
| padEnd(maxLength: number, fillString?: string): string; | ||
| repeat(count: number): string; | ||
| toUpperCase(): string; | ||
| toLowerCase(): string; | ||
| private s; | ||
| } |
| 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 { fromBase64, LazyJsonString, NumericValue, parseEpochTimestamp, parseRfc3339DateTimeWithOffset, parseRfc7231DateTime, } from "@smithy/core/serde"; | ||
| import { SerdeContextConfig } from "../../ConfigurableSerdeContext"; | ||
@@ -22,5 +21,11 @@ import { UnionSerde } from "../../UnionSerde"; | ||
| if (typeof data === "string") { | ||
| if (data.length === 0) { | ||
| return {}; | ||
| } | ||
| parsed = JSON.parse(data, reviver); | ||
| } | ||
| else if (data instanceof Uint8Array && detectBufferParsing()) { | ||
| if (data.byteLength === 0) { | ||
| return {}; | ||
| } | ||
| const buf = Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -30,3 +35,3 @@ parsed = JSON.parse(buf, reviver); | ||
| else { | ||
| parsed = await parseJsonBody(data, this.serdeContext); | ||
| parsed = await parseJsonBody(data, this.serdeContext, schema); | ||
| } | ||
@@ -47,4 +52,6 @@ return this._read(schema, parsed); | ||
| const listMember = ns.getValueSchema(); | ||
| for (let i = 0; i < value.length; ++i) { | ||
| value[i] = this._read(listMember, value[i]); | ||
| if (this.needsTransform(listMember)) { | ||
| for (let i = 0; i < value.length; ++i) { | ||
| value[i] = this._read(listMember, value[i]); | ||
| } | ||
| } | ||
@@ -56,7 +63,9 @@ return value; | ||
| const map = value; | ||
| for (const k in map) { | ||
| if (k === "__proto__") { | ||
| writeKey(map); | ||
| if (this.needsTransform(mapMember)) { | ||
| for (const k in map) { | ||
| if (k === "__proto__") { | ||
| writeKey(map); | ||
| } | ||
| map[k] = this._read(mapMember, map[k]); | ||
| } | ||
| map[k] = this._read(mapMember, map[k]); | ||
| } | ||
@@ -137,7 +146,3 @@ return map; | ||
| } | ||
| return value; | ||
| } | ||
| else { | ||
| return value; | ||
| } | ||
| } | ||
@@ -149,5 +154,6 @@ return value; | ||
| const out = {}; | ||
| let nameMap = void 0; | ||
| let nameMap; | ||
| const hasType = typeof record.__type === "string"; | ||
| const { jsonName } = this.settings; | ||
| if (jsonName) { | ||
| if (jsonName && hasType) { | ||
| nameMap = {}; | ||
@@ -163,3 +169,5 @@ } | ||
| fromKey = memberSchema.getMergedTraits().jsonName ?? fromKey; | ||
| nameMap[fromKey] = memberName; | ||
| if (hasType) { | ||
| nameMap[fromKey] = memberName; | ||
| } | ||
| } | ||
@@ -176,3 +184,3 @@ if (union) { | ||
| } | ||
| else if (typeof record.__type === "string") { | ||
| else if (hasType) { | ||
| for (const k in record) { | ||
@@ -188,2 +196,14 @@ const v = record[k]; | ||
| } | ||
| needsTransform(ns) { | ||
| if (ns.isBlobSchema() || ns.isTimestampSchema() || ns.isBigIntegerSchema() || ns.isBigDecimalSchema()) { | ||
| return true; | ||
| } | ||
| if (ns.isDocumentSchema() || ns.isStructSchema() || ns.isListSchema() || ns.isMapSchema()) { | ||
| return true; | ||
| } | ||
| if (ns.isStringSchema() && ns.getMergedTraits().mediaType) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
@@ -6,2 +6,3 @@ import { determineTimestampFormat } from "@smithy/core/protocols"; | ||
| import { writeKey } from "../../writeKey"; | ||
| import { JsonBytesStringAdapter } from "./JsonBytesStringAdapter"; | ||
| const encoder = new TextEncoder(); | ||
@@ -34,3 +35,3 @@ const OPEN_BRACE = 0x7b; | ||
| function alloc(size) { | ||
| return typeof Buffer !== "undefined" ? Buffer.allocUnsafe(size) : new Uint8Array(size); | ||
| return JsonBytesStringAdapter.allocUnsafe(size); | ||
| } | ||
@@ -53,6 +54,3 @@ export class JsonShapeSerializer2 extends SerdeContextConfig { | ||
| this.rootSchema = NormalizedSchema.of(schema); | ||
| this.passthrough = | ||
| !this.rootSchema.isStructSchema() && | ||
| !this.rootSchema.isDocumentSchema() && | ||
| (this.rootSchema.isBlobSchema() || this.rootSchema.isStringSchema()); | ||
| this.passthrough = this.rootSchema.isBlobSchema() || this.rootSchema.isStringSchema(); | ||
| if (!this.passthrough) { | ||
@@ -67,26 +65,9 @@ this.writeValue(this.rootSchema, value, undefined); | ||
| 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; | ||
| this.writeValue(ns, value, undefined); | ||
| const prefix = `"__type":"${ns.getName(true) ?? "Unknown"}",`; | ||
| const z = prefix.length; | ||
| this.ensure(z); | ||
| this.json.copyWithin(1 + z, 1, this.i); | ||
| encoder.encodeInto(prefix, this.json.subarray(1)); | ||
| this.i += z; | ||
| } | ||
@@ -144,3 +125,3 @@ else { | ||
| writeJsonString(s) { | ||
| this.ensure(s.length * 2 + 2); | ||
| this.ensure(s.length * 3 + 2); | ||
| this.json[this.i++] = QUOTE; | ||
@@ -172,3 +153,3 @@ const z = s.length; | ||
| this.i += written; | ||
| j++; | ||
| ++j; | ||
| } | ||
@@ -213,4 +194,5 @@ else { | ||
| const table = new Uint8Array(64); | ||
| for (let i = 0; i < 64; i++) | ||
| for (let i = 0; i < 64; ++i) { | ||
| table[i] = chars.charCodeAt(i); | ||
| } | ||
| return table; | ||
@@ -342,10 +324,12 @@ })(); | ||
| this.ensure(5); | ||
| let { i, json } = this; | ||
| if (value) { | ||
| this.json.set(TRUE, this.i); | ||
| this.i += 4; | ||
| json.set(TRUE, i); | ||
| i += 4; | ||
| } | ||
| else { | ||
| this.json.set(FALSE, this.i); | ||
| this.i += 5; | ||
| json.set(FALSE, i); | ||
| i += 5; | ||
| } | ||
| this.i = i; | ||
| return; | ||
@@ -362,3 +346,2 @@ } | ||
| this.json[this.i++] = OPEN_BRACE; | ||
| let first = true; | ||
| let wroteAny = false; | ||
@@ -372,9 +355,9 @@ const hasType = typeof value.__type === "string"; | ||
| const item = value[memberName]; | ||
| if (item == null && !memberSchema.isIdempotencyToken()) | ||
| if (item == null && !memberSchema.isIdempotencyToken()) { | ||
| continue; | ||
| if (!first) { | ||
| } | ||
| if (wroteAny) { | ||
| this.ensure(1); | ||
| this.json[this.i++] = COMMA; | ||
| } | ||
| first = false; | ||
| wroteAny = true; | ||
@@ -402,13 +385,13 @@ const targetKey = this.settings.jsonName ? (memberSchema.getMergedTraits().jsonName ?? memberName) : memberName; | ||
| for (const k in value) { | ||
| const targetKey = this.settings.jsonName ? (writtenKeys.has(k) ? k : k) : k; | ||
| if (writtenKeys.has(targetKey)) | ||
| if (writtenKeys.has(k)) { | ||
| continue; | ||
| writtenKeys.add(targetKey); | ||
| } | ||
| writtenKeys.add(k); | ||
| const v = value[k]; | ||
| if (!first) { | ||
| if (wroteAny) { | ||
| this.ensure(1); | ||
| this.json[this.i++] = COMMA; | ||
| } | ||
| first = false; | ||
| this.writeAsciiQuoted(targetKey); | ||
| wroteAny = true; | ||
| this.writeAsciiQuoted(k); | ||
| this.ensure(1); | ||
@@ -423,6 +406,15 @@ this.json[this.i++] = COLON; | ||
| writeList(ns, value, isDocument) { | ||
| const sparse = !!ns.getMergedTraits().sparse; | ||
| const valueSchema = ns.getValueSchema(); | ||
| if (!isDocument) { | ||
| if (valueSchema.isStringSchema() || valueSchema.isNumericSchema() || valueSchema.isBooleanSchema()) { | ||
| const json = sparse ? JSON.stringify(value) : JSON.stringify(value.filter((_) => _ != null)); | ||
| this.ensure(json.length * 3); | ||
| this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written; | ||
| return; | ||
| } | ||
| } | ||
| this.ensure(2); | ||
| this.json[this.i++] = OPEN_BRACKET; | ||
| const sparse = !!ns.getMergedTraits().sparse; | ||
| const valueSchema = ns.getValueSchema(); | ||
| let wroteFirstItem = false; | ||
| for (let i = 0; i < value.length; ++i) { | ||
@@ -433,3 +425,3 @@ const item = value[i]; | ||
| } | ||
| if (i !== 0) { | ||
| if (wroteFirstItem) { | ||
| this.ensure(1); | ||
@@ -439,2 +431,3 @@ this.json[this.i++] = COMMA; | ||
| this.writeValue(valueSchema, item, undefined); | ||
| wroteFirstItem = true; | ||
| } | ||
@@ -461,4 +454,3 @@ this.ensure(1); | ||
| this.ensure(json.length * 3); | ||
| const { written } = encoder.encodeInto(json, this.json.subarray(this.i)); | ||
| this.i += written; | ||
| this.i += encoder.encodeInto(json, this.json.subarray(this.i)).written; | ||
| return; | ||
@@ -465,0 +457,0 @@ } |
@@ -5,2 +5,3 @@ import type { $ShapeSerializer, $Codec, $ShapeDeserializer } from "@smithy/types"; | ||
| /** | ||
| * Codec grouping the v2 serializer and deserializer. | ||
| * @public | ||
@@ -7,0 +8,0 @@ */ |
@@ -29,2 +29,3 @@ import type { DocumentType, Schema, ShapeDeserializer } from "@smithy/types"; | ||
| private _readStruct; | ||
| private needsTransform; | ||
| } |
@@ -14,2 +14,3 @@ import { DocumentType, Schema, ShapeDeserializer } from "@smithy/types"; | ||
| private _readStruct; | ||
| private needsTransform; | ||
| } |
+1
-1
| { | ||
| "name": "@aws-sdk/core", | ||
| "version": "3.977.3", | ||
| "version": "3.977.4", | ||
| "description": "Core functions & classes shared by multiple AWS SDK clients.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages-internal/core", |
Sorry, the diff of this file is too big to display
562941
1.88%360
0.84%13661
2.32%