|
| 1 | +import { deref, ListSchema, MapSchema, StructureSchema } from "@smithy/core/schema"; |
| 2 | +import { copyDocumentWithTransform, parseEpochTimestamp } from "@smithy/smithy-client"; |
| 3 | +import { |
| 4 | + Codec, |
| 5 | + MemberSchema, |
| 6 | + Schema, |
| 7 | + SchemaRef, |
| 8 | + ShapeDeserializer, |
| 9 | + ShapeSerializer, |
| 10 | + TraitsSchema, |
| 11 | +} from "@smithy/types"; |
| 12 | + |
| 13 | +import { cbor } from "./cbor"; |
| 14 | +import { dateToTag } from "./parseCborBody"; |
| 15 | + |
| 16 | +export class CborCodec implements Codec { |
| 17 | + public createSerializer(): CborShapeSerializer { |
| 18 | + return new CborShapeSerializer(); |
| 19 | + } |
| 20 | + public createDeserializer(): CborShapeDeserializer { |
| 21 | + return new CborShapeDeserializer(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +export class CborShapeSerializer implements ShapeSerializer { |
| 26 | + private value: unknown; |
| 27 | + |
| 28 | + public write(schema: Schema, value: unknown): void { |
| 29 | + this.value = copyDocumentWithTransform(value, schema, (_: any, schemaRef: SchemaRef) => { |
| 30 | + if (_ instanceof Date) { |
| 31 | + return dateToTag(_); |
| 32 | + } |
| 33 | + const schema = deref((schemaRef as MemberSchema)?.[0] ?? schemaRef); |
| 34 | + if (_ instanceof Uint8Array) { |
| 35 | + return _; |
| 36 | + } |
| 37 | + const sparse = (schema as TraitsSchema)?.traits?.sparse; |
| 38 | + if (Array.isArray(_)) { |
| 39 | + if (!sparse) { |
| 40 | + return _.filter((item) => item != null); |
| 41 | + } |
| 42 | + } else if (_ && typeof _ === "object") { |
| 43 | + if (!sparse) { |
| 44 | + for (const [k, v] of Object.entries(_)) { |
| 45 | + if (v == null) { |
| 46 | + delete _[k]; |
| 47 | + } |
| 48 | + } |
| 49 | + return _; |
| 50 | + } |
| 51 | + } |
| 52 | + return _; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + public async flush(): Promise<Uint8Array> { |
| 57 | + const buffer = cbor.serialize(this.value); |
| 58 | + this.value = undefined; |
| 59 | + return buffer as Uint8Array; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export class CborShapeDeserializer implements ShapeDeserializer { |
| 64 | + public read(schema: Schema, bytes: Uint8Array): any { |
| 65 | + const data: any = cbor.deserialize(bytes); |
| 66 | + return this.readValue(schema, data); |
| 67 | + } |
| 68 | + |
| 69 | + private readValue(schema: Schema, value: any): any { |
| 70 | + if (typeof schema === "string") { |
| 71 | + if (schema === "time" || schema === "epoch-seconds" || schema === "date-time") { |
| 72 | + return parseEpochTimestamp(value); |
| 73 | + } |
| 74 | + if (schema === "blob") { |
| 75 | + return value; |
| 76 | + } |
| 77 | + } |
| 78 | + switch (typeof value) { |
| 79 | + case "undefined": |
| 80 | + case "boolean": |
| 81 | + case "number": |
| 82 | + case "string": |
| 83 | + case "bigint": |
| 84 | + case "symbol": |
| 85 | + return value; |
| 86 | + case "function": |
| 87 | + case "object": |
| 88 | + if (value === null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + if ("byteLength" in (value as Uint8Array)) { |
| 92 | + return value; |
| 93 | + } |
| 94 | + if (value instanceof Date) { |
| 95 | + return value; |
| 96 | + } |
| 97 | + const traits = |
| 98 | + Array.isArray(schema) && schema.length >= 2 |
| 99 | + ? { |
| 100 | + ...(deref((schema as MemberSchema)[0]) as TraitsSchema)?.traits, |
| 101 | + ...(schema as MemberSchema)[1], |
| 102 | + } |
| 103 | + : (deref(schema) as TraitsSchema)?.traits; |
| 104 | + |
| 105 | + if (Array.isArray(value)) { |
| 106 | + const newArray = []; |
| 107 | + for (const item of value) { |
| 108 | + newArray.push(this.readValue(schema instanceof ListSchema ? deref(schema.valueSchema) : void 0, item)); |
| 109 | + if (!traits?.sparse) { |
| 110 | + if (newArray[newArray.length - 1] == null) { |
| 111 | + newArray.pop(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return newArray; |
| 116 | + } |
| 117 | + |
| 118 | + const newObject = {} as any; |
| 119 | + for (const key of Object.keys(value)) { |
| 120 | + const targetSchema = |
| 121 | + schema instanceof StructureSchema |
| 122 | + ? deref(schema.members[key]?.[0]) |
| 123 | + : schema instanceof MapSchema |
| 124 | + ? deref(schema.valueSchema) |
| 125 | + : void 0; |
| 126 | + newObject[key] = this.readValue(targetSchema, value[key]); |
| 127 | + if (!traits?.sparse && newObject[key] == null) { |
| 128 | + delete newObject[key]; |
| 129 | + } |
| 130 | + } |
| 131 | + return newObject; |
| 132 | + default: |
| 133 | + return value; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public getContainerSize(): number { |
| 138 | + throw new Error("Method not implemented."); |
| 139 | + } |
| 140 | +} |
0 commit comments