Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 5x 20x 5x 47x 47x 118x 437x 437x 437x 118x 2x 7x 6x 110x 110x 110x 110x 110x 977x 977x 977x 326x 326x 651x 426x 426x 391x 110x 7x 7x 426x 245x 245x 181x 437x 47x 390x 54x 336x 36x 300x 95x 205x | import { jsonProperty } from './json'
import { decodeBase64ToBytes, encodeBytesToBase64 } from './storage-encoding'
/** A property with an explicit EDM type; Binary is base64, DateTime ISO and Int64 decimal text. */
export type TableEdmProperty =
| { value: string; type: 'Binary' | 'DateTime' | 'Guid' | 'Int64' | 'String' }
| { value: boolean; type: 'Boolean' }
| { value: number; type: 'Double' | 'Int32' }
/** The service infers primitive types; Date, bigint and bytes become DateTime, Int64 and Binary. */
export type TableEntityProperty =
| string
| number
| boolean
| null
| Date
| bigint
| Uint8Array
| TableEdmProperty
export interface TableEntity {
partitionKey: string
rowKey: string
[property: string]: TableEntityProperty
}
/** A property as read: DateTime, Int64 and Binary decode to Date, bigint and Uint8Array. */
export type TableEntityValue =
| string
| number
| boolean
| Date
| bigint
| Uint8Array
| { value: string; type: 'Guid' }
/** An entity as read; a projection may omit the keys and timestamp. */
export interface TableEntityResult {
partitionKey?: string
rowKey?: string
etag: string
timestamp?: string
[property: string]: TableEntityValue | undefined
}
/** Service names of the system properties, which reads rename and writes map back. */
const serviceNames = new Map([
['partitionKey', 'PartitionKey'],
['rowKey', 'RowKey'],
['etag', 'odata.etag'],
['timestamp', 'Timestamp'],
])
const publicNames = new Map(Array.from(serviceNames, ([name, serviceName]) => [serviceName, name]))
/** Annotated types without a JSON equivalent; other values stay as parsed. */
const decoders: Partial<Record<string, (value: string) => TableEntityValue>> = {
'Edm.Binary': decodeBase64ToBytes,
'Edm.DateTime': (value) => new Date(value),
'Edm.Guid': (value) => ({ value, type: 'Guid' }),
'Edm.Int64': BigInt,
}
// Entries become own properties, so a property such as `__proto__` keeps its name.
export function serializeEntity({ partitionKey, rowKey, ...properties }: TableEntity): string {
const body = Object.entries(properties).flatMap(([name, property]) => {
const serviceName = serviceNames.get(name) ?? name
const [value, type] = encodeProperty(property)
return type === undefined
? [[serviceName, value]]
: [
[serviceName, value],
[`${serviceName}@odata.type`, type],
]
})
return JSON.stringify({
...Object.fromEntries(body),
PartitionKey: partitionKey,
RowKey: rowKey,
})
}
/** `$select` of public property names; the etag is always returned and is not selectable. */
export function serializeSelect(names: string[]): string {
return names
.filter((name) => name !== 'etag' && name !== 'odata.etag')
.map((name) => serviceNames.get(name) ?? name)
.join(',')
}
export function parseEntity(value: unknown): TableEntityResult {
const etag = jsonProperty(value, 'odata.etag')
Iif (typeof value !== 'object' || value === null || typeof etag !== 'string') {
throw new TypeError('Invalid table entity response')
}
const properties: [string, TableEntityValue][] = []
const system: [string, string][] = []
for (const name of Object.keys(value)) {
const property = jsonProperty(value, name)
const publicName = publicNames.get(name)
if (publicName !== undefined) {
Eif (typeof property === 'string') {
system.push([publicName, property])
}
} else if (!name.startsWith('odata.') && !name.endsWith('@odata.type')) {
const decoded = decodeProperty(property, jsonProperty(value, `${name}@odata.type`))
if (decoded !== undefined) {
properties.push([name, decoded])
}
}
}
// System properties win over user properties with the same name.
return { ...Object.fromEntries(properties), ...Object.fromEntries(system), etag }
}
export function parseEntityList(value: unknown): TableEntityResult[] {
const entities = jsonProperty(value, 'value')
return Array.isArray(entities) ? entities.map((entity) => parseEntity(entity)) : []
}
function decodeProperty(property: unknown, type: unknown): TableEntityValue | undefined {
if (typeof property === 'string') {
const decode = typeof type === 'string' ? decoders[type] : undefined
return decode === undefined ? property : decode(property)
}
return typeof property === 'number' || typeof property === 'boolean' ? property : undefined
}
function encodeProperty(
property: TableEntityProperty,
): [value: string | number | boolean | null, type?: string] {
if (property instanceof Date) {
return [property.toISOString(), 'Edm.DateTime']
}
if (typeof property === 'bigint') {
return [property.toString(), 'Edm.Int64']
}
if (property instanceof Uint8Array) {
return [encodeBytesToBase64(property), 'Edm.Binary']
}
if (typeof property === 'object' && property !== null) {
return [property.value, `Edm.${property.type}`]
}
return [property]
}
|