@google-cloud/spanner 8.13.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +1 -16
  2. package/build/src/batch-transaction.js +15 -12
  3. package/build/src/codec.d.ts +18 -30
  4. package/build/src/codec.js +197 -79
  5. package/build/src/common.d.ts +14 -0
  6. package/build/src/common.js +28 -3
  7. package/build/src/database.d.ts +31 -41
  8. package/build/src/database.js +147 -315
  9. package/build/src/helper.d.ts +12 -0
  10. package/build/src/helper.js +44 -0
  11. package/build/src/index.d.ts +24 -8
  12. package/build/src/index.js +303 -147
  13. package/build/src/instance.d.ts +15 -2
  14. package/build/src/instance.js +11 -2
  15. package/build/src/instrument.d.ts +12 -1
  16. package/build/src/instrument.js +13 -0
  17. package/build/src/metrics/constants.d.ts +13 -6
  18. package/build/src/metrics/constants.js +24 -14
  19. package/build/src/metrics/external-types.d.ts +14 -0
  20. package/build/src/metrics/interceptor.js +39 -44
  21. package/build/src/metrics/metrics-tracer-factory.d.ts +25 -14
  22. package/build/src/metrics/metrics-tracer-factory.js +119 -78
  23. package/build/src/metrics/metrics-tracer.d.ts +19 -6
  24. package/build/src/metrics/metrics-tracer.js +147 -55
  25. package/build/src/metrics/spanner-metrics-exporter.js +4 -0
  26. package/build/src/metrics/transform.d.ts +3 -4
  27. package/build/src/metrics/transform.js +16 -10
  28. package/build/src/multiplexed-session.d.ts +26 -2
  29. package/build/src/multiplexed-session.js +39 -11
  30. package/build/src/partial-result-stream.d.ts +26 -33
  31. package/build/src/partial-result-stream.js +263 -283
  32. package/build/src/request-stream-coordinator.d.ts +88 -0
  33. package/build/src/request-stream-coordinator.js +229 -0
  34. package/build/src/request_id_header.d.ts +6 -4
  35. package/build/src/request_id_header.js +24 -24
  36. package/build/src/session-factory.d.ts +62 -60
  37. package/build/src/session-factory.js +46 -86
  38. package/build/src/session-pool.d.ts +97 -227
  39. package/build/src/session-pool.js +74 -547
  40. package/build/src/session.js +1 -1
  41. package/build/src/transaction-runner.js +1 -12
  42. package/build/src/transaction.d.ts +21 -5
  43. package/build/src/transaction.js +107 -136
  44. package/package.json +28 -37
package/README.md CHANGED
@@ -104,26 +104,11 @@ Refer to the Observability README to know more about tracing support in the Clou
104
104
 
105
105
  ## Multiplexed Sessions
106
106
 
107
- Spanner's Multiplexed Sessions is now default enabled session mode in node client. This feature helps reduce
107
+ Spanner's Multiplexed Sessions is the default and only session mode in the Node.js client. This feature helps reduce
108
108
  session management overhead and minimize session-related errors.
109
109
 
110
110
  For a detailed explanation on multiplexed sessions, please refer to the [official documentation](https://cloud.google.com/spanner/docs/sessions#multiplexed_sessions).
111
111
 
112
- ## Regular Sessions
113
-
114
- To use regular sessions, disable the multiplexed sessions and set the following environment variables to `false`:
115
-
116
- * **For Read-Only Transactions:**
117
- - `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS`
118
- * **For Partitioned Operations:**
119
- - `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS`
120
- - `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_PARTITIONED_OPS`
121
- * **For Read-Write Transactions:**
122
- - `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS`
123
- - `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS_FOR_RW`
124
-
125
- For a detailed explanation on session modes and env configurations, please refer to the [official documentation](https://cloud.google.com/spanner/docs/sessions).
126
-
127
112
 
128
113
  ## Samples
129
114
 
@@ -81,13 +81,14 @@ class BatchTransaction extends transaction_1.Snapshot {
81
81
  }
82
82
  createQueryPartitions(query, cb) {
83
83
  const request = typeof query === 'string' ? { sql: query } : query;
84
- const reqOpts = Object.assign({}, request, transaction_1.Snapshot.encodeParams(request));
85
- delete reqOpts.gaxOptions;
86
- delete reqOpts.types;
84
+ const { gaxOptions: _omittedGaxOptions, types: _omittedTypes, ...cleanRequest } = request;
85
+ void _omittedGaxOptions;
86
+ void _omittedTypes;
87
+ const reqOpts = Object.assign({}, cleanRequest, transaction_1.Snapshot.encodeParams(request));
87
88
  const traceConfig = {
88
- sql: request.sql,
89
89
  opts: this._observabilityOptions,
90
90
  dbName: this.getDBName(),
91
+ ...(0, instrument_1.getQueryTraceConfig)(query),
91
92
  };
92
93
  return (0, instrument_1.startTrace)('BatchTransaction.createQueryPartitions', traceConfig, span => {
93
94
  const headers = {};
@@ -128,17 +129,18 @@ class BatchTransaction extends transaction_1.Snapshot {
128
129
  dbName: this.getDBName(),
129
130
  };
130
131
  return (0, instrument_1.startTrace)('BatchTransaction.createPartitions_', traceConfig, span => {
131
- const query = Object.assign({}, config.reqOpts, {
132
+ const baseRequest = Object.assign({}, config.reqOpts, {
132
133
  session: this.session.formattedName_,
133
134
  transaction: { id: this.id },
134
135
  });
135
- config.reqOpts = Object.assign({}, query);
136
+ config.reqOpts = baseRequest;
136
137
  const headers = {
137
138
  [common_1.CLOUD_RESOURCE_HEADER]: this.session.parent
138
139
  .formattedName_,
139
140
  };
140
141
  config.headers = (0, request_id_header_1.injectRequestIDIntoHeaders)(headers, this.session);
141
- delete query.partitionOptions;
142
+ const { partitionOptions: _omittedPartitionOptions, ...baseRequestWithoutPartitionOptions } = baseRequest;
143
+ void _omittedPartitionOptions;
142
144
  this.session.request(config, (err, resp) => {
143
145
  if (err) {
144
146
  (0, instrument_1.setSpanError)(span, err);
@@ -147,7 +149,7 @@ class BatchTransaction extends transaction_1.Snapshot {
147
149
  return;
148
150
  }
149
151
  const partitions = resp.partitions.map(partition => {
150
- return Object.assign({}, query, partition);
152
+ return Object.assign({}, baseRequestWithoutPartitionOptions, partition);
151
153
  });
152
154
  if (resp.transaction) {
153
155
  const { id, readTimestamp } = resp.transaction;
@@ -168,12 +170,13 @@ class BatchTransaction extends transaction_1.Snapshot {
168
170
  dbName: this.getDBName(),
169
171
  };
170
172
  return (0, instrument_1.startTrace)('BatchTransaction.createReadPartitions', traceConfig, span => {
171
- const reqOpts = Object.assign({}, options, {
173
+ const { gaxOptions: _omittedGaxOptions, keys: _omittedKeys, ranges: _omittedRanges, ...cleanOptions } = options;
174
+ void _omittedGaxOptions;
175
+ void _omittedKeys;
176
+ void _omittedRanges;
177
+ const reqOpts = Object.assign({}, cleanOptions, {
172
178
  keySet: transaction_1.Snapshot.encodeKeySet(options),
173
179
  });
174
- delete reqOpts.gaxOptions;
175
- delete reqOpts.keys;
176
- delete reqOpts.ranges;
177
180
  const headers = {};
178
181
  if (this._getSpanner().routeToLeaderEnabled) {
179
182
  (0, common_1.addLeaderAwareRoutingHeader)(headers);
@@ -317,6 +317,12 @@ declare function decode(value: Value, type: spannerClient.spanner.v1.Type, colum
317
317
  /**
318
318
  * Encode a value in the format the API expects.
319
319
  *
320
+ * This emits the `google.protobuf.Value` shape in a single pass: values are
321
+ * normalized and wrapped in their protobuf kind at the same time, so neither an
322
+ * intermediate representation nor a per-value `ObjectToStructConverter` is
323
+ * allocated. The branches prioritize primitive and common types for fast
324
+ * dispatch in hot query execution paths.
325
+ *
320
326
  * @private
321
327
  *
322
328
  * @param {*} value The value to be encoded.
@@ -338,41 +344,21 @@ export interface Type {
338
344
  interface FieldType extends Type {
339
345
  name: string;
340
346
  }
341
- /**
342
- * @typedef {ParamType} StructField
343
- * @property {string} name The name of the field.
344
- */
345
- /**
346
- * @typedef {object} ParamType
347
- * @property {string} type The param type. Must be one of the following:
348
- * - uuid
349
- * - float32
350
- * - float64
351
- * - int64
352
- * - numeric
353
- * - bool
354
- * - string
355
- * - bytes
356
- * - json
357
- * - interval
358
- * - proto
359
- * - enum
360
- * - timestamp
361
- * - date
362
- * - struct
363
- * - array
364
- * @property {StructField[]} [fields] **For struct types only**. Type
365
- * definitions for the individual fields.
366
- * @property {string|ParamType} [child] **For array types only**. The array
367
- * element type.
368
- */
347
+ declare function _resetUuidUntypedFlagWarnedForTest(): void;
348
+ declare function isUuidUntypedEnv(): boolean;
369
349
  /**
370
350
  * Get the corresponding Spanner data type for the provided value.
371
351
  *
352
+ * NOTE: This function is internal and not exposed in the public API.
353
+ * Parameterless types return shared, frozen singletons to eliminate allocation
354
+ * and GC overhead. Callers must treat the returned Type descriptor as read-only.
355
+ *
372
356
  * @private
373
357
  *
374
358
  * @param {*} value - The value.
375
- * @returns {object}
359
+ * @param {boolean} [enableUuidAsUntyped] - Whether UUID strings should be
360
+ * typed as unspecified.
361
+ * @returns {Type}
376
362
  *
377
363
  * @example
378
364
  * ```
@@ -380,7 +366,7 @@ interface FieldType extends Type {
380
366
  * // {type: 'float64'}
381
367
  * ```
382
368
  */
383
- declare function getType(value: Value): Type;
369
+ declare function getType(value: Value, enableUuidAsUntyped?: boolean): Type;
384
370
  /**
385
371
  * Converts a value to google.protobuf.ListValue
386
372
  *
@@ -439,5 +425,7 @@ export declare const codec: {
439
425
  encode: typeof encode;
440
426
  getType: typeof getType;
441
427
  Struct: typeof Struct;
428
+ _resetUuidUntypedFlagWarnedForTest: typeof _resetUuidUntypedFlagWarnedForTest;
429
+ isUuidUntypedEnv: typeof isUuidUntypedEnv;
442
430
  };
443
431
  export {};
@@ -17,7 +17,6 @@ exports.getDecoder = getDecoder;
17
17
  * See the License for the specific language governing permissions and
18
18
  * limitations under the License.
19
19
  */
20
- const service_1 = require("./common-grpc/service");
21
20
  const precise_date_1 = require("@google-cloud/precise-date");
22
21
  const helper_1 = require("./helper");
23
22
  const big_js_1 = require("big.js");
@@ -55,7 +54,6 @@ function isValidTimestamp(year, month, day, hours, minutes, seconds, millisecond
55
54
  }
56
55
  return isValidDate(year, month, day);
57
56
  }
58
- let uuidUntypedFlagWarned = false;
59
57
  /**
60
58
  * Date-like object used to represent Cloud Spanner Dates. DATE types represent
61
59
  * a logical calendar date, independent of time zone. DATE values do not
@@ -1011,65 +1009,131 @@ function parsePreciseDate(isoString) {
1011
1009
  /**
1012
1010
  * Encode a value in the format the API expects.
1013
1011
  *
1012
+ * This emits the `google.protobuf.Value` shape in a single pass: values are
1013
+ * normalized and wrapped in their protobuf kind at the same time, so neither an
1014
+ * intermediate representation nor a per-value `ObjectToStructConverter` is
1015
+ * allocated. The branches prioritize primitive and common types for fast
1016
+ * dispatch in hot query execution paths.
1017
+ *
1014
1018
  * @private
1015
1019
  *
1016
1020
  * @param {*} value The value to be encoded.
1017
1021
  * @returns {object} google.protobuf.Value
1018
1022
  */
1019
1023
  function encode(value) {
1020
- return service_1.GrpcService.encodeValue_(encodeValue(value));
1024
+ switch (typeof value) {
1025
+ case 'string':
1026
+ return { stringValue: value };
1027
+ case 'number':
1028
+ // Only non-integer, finite numbers are sent as a protobuf number. Every
1029
+ // other numeric value — integers (INT64 is a string on the wire) as well
1030
+ // as NaN and ±Infinity — is sent as a string.
1031
+ return Number.isFinite(value) && !Number.isInteger(value)
1032
+ ? { numberValue: value }
1033
+ : { stringValue: value.toString() };
1034
+ case 'boolean':
1035
+ return { boolValue: value };
1036
+ case 'object':
1037
+ return value === null ? { nullValue: 0 } : encodeObject(value);
1038
+ default:
1039
+ throw new Error(`Value of type ${typeof value} not recognized.`);
1040
+ }
1021
1041
  }
1022
1042
  /**
1023
- * Formats values into expected format of google.protobuf.Value. The actual
1024
- * conversion to a google.protobuf.Value object happens via
1025
- * `Service.encodeValue_`
1043
+ * Encodes a non-null object into a google.protobuf.Value.
1026
1044
  *
1027
1045
  * @private
1028
1046
  *
1029
- * @param {*} value The value to be encoded.
1030
- * @returns {*}
1047
+ * @param {object} value The value to be encoded.
1048
+ * @returns {object} google.protobuf.Value
1031
1049
  */
1032
- function encodeValue(value) {
1033
- if ((0, helper_1.isNumber)(value) && !(0, helper_1.isDecimal)(value)) {
1034
- return value.toString();
1035
- }
1036
- if ((0, helper_1.isDate)(value)) {
1037
- return value.toJSON();
1050
+ function encodeObject(value) {
1051
+ if (value instanceof Date) {
1052
+ // `Date.prototype.toJSON` returns null for invalid dates.
1053
+ const json = value.toJSON();
1054
+ return json === null ? { nullValue: 0 } : { stringValue: json };
1038
1055
  }
1039
1056
  if (value instanceof WrappedNumber) {
1040
- return value.value;
1057
+ // `Float`/`Float32` hold a number, `Int`/`PGOid` hold a string.
1058
+ return encodeNumberOrString(value.value);
1041
1059
  }
1042
- if (value instanceof Numeric) {
1043
- return value.value;
1044
- }
1045
- if (value instanceof PGNumeric) {
1046
- return value.value;
1060
+ if (Array.isArray(value)) {
1061
+ return { listValue: { values: encodeArrayValues(value) } };
1047
1062
  }
1048
1063
  if (Buffer.isBuffer(value)) {
1049
- return value.toString('base64');
1064
+ return { stringValue: value.toString('base64') };
1050
1065
  }
1051
- if (value instanceof ProtoMessage) {
1052
- return value.value.toString('base64');
1066
+ if (value instanceof Numeric || value instanceof PGNumeric) {
1067
+ return { stringValue: value.value };
1053
1068
  }
1054
- if (value instanceof ProtoEnum) {
1055
- return value.value;
1069
+ if (value instanceof Interval) {
1070
+ return { stringValue: value.toISO8601() };
1056
1071
  }
1057
- if (value instanceof Struct) {
1058
- return Array.from(value).map(field => encodeValue(field.value));
1072
+ if (value instanceof ProtoMessage) {
1073
+ return { stringValue: value.value.toString('base64') };
1059
1074
  }
1060
- if ((0, helper_1.isArray)(value)) {
1061
- return value.map(encodeValue);
1075
+ if (value instanceof ProtoEnum) {
1076
+ // Holds either the numeric constant or its string representation.
1077
+ return encodeNumberOrString(value.value);
1062
1078
  }
1063
1079
  if (value instanceof PGJsonb) {
1064
- return value.toString();
1065
- }
1066
- if (value instanceof Interval) {
1067
- return value.toISO8601();
1080
+ return { stringValue: value.toString() };
1068
1081
  }
1069
1082
  if ((0, helper_1.isObject)(value)) {
1070
- return JSON.stringify(value);
1083
+ const json = JSON.stringify(value);
1084
+ // `JSON.stringify` returns undefined only when an object's `toJSON()`
1085
+ // method returns undefined. Other objects without a JSON representation
1086
+ // (such as Functions, Symbols, and Maps) fail `isObject` and throw below.
1087
+ // Note: `JSON.stringify` will throw a TypeError for circular structures
1088
+ // or BigInt property values.
1089
+ if (json !== undefined) {
1090
+ return { stringValue: json };
1091
+ }
1071
1092
  }
1072
- return value;
1093
+ if (value instanceof Number ||
1094
+ value instanceof String ||
1095
+ value instanceof Boolean) {
1096
+ // Boxed primitives are never produced by the public API, but were accepted
1097
+ // by the previous implementation.
1098
+ return encode(value.valueOf());
1099
+ }
1100
+ if ((0, helper_1.isDate)(value)) {
1101
+ // A `Date` from another realm (e.g. a `vm` context) fails `instanceof`.
1102
+ const json = value.toJSON();
1103
+ return json === null ? { nullValue: 0 } : { stringValue: json };
1104
+ }
1105
+ throw new Error('Value of type object not recognized.');
1106
+ }
1107
+ /**
1108
+ * Encodes the members of an array — or the field values of a {@link Struct},
1109
+ * which is itself an array — into google.protobuf.Value messages.
1110
+ *
1111
+ * @private
1112
+ *
1113
+ * @param {Array} value The array to be encoded.
1114
+ * @returns {object[]} google.protobuf.Value[]
1115
+ */
1116
+ function encodeArrayValues(value) {
1117
+ const { length } = value;
1118
+ const values = new Array(length);
1119
+ const isStruct = value instanceof Struct;
1120
+ for (let i = 0; i < length; i++) {
1121
+ values[i] = encode(isStruct ? value[i].value : value[i]);
1122
+ }
1123
+ return values;
1124
+ }
1125
+ /**
1126
+ * Wraps an already normalized value in the matching google.protobuf.Value kind.
1127
+ *
1128
+ * @private
1129
+ *
1130
+ * @param {string|number} value The value to be wrapped.
1131
+ * @returns {object} google.protobuf.Value
1132
+ */
1133
+ function encodeNumberOrString(value) {
1134
+ return typeof value === 'number'
1135
+ ? { numberValue: value }
1136
+ : { stringValue: value };
1073
1137
  }
1074
1138
  /**
1075
1139
  * Just a map with friendlier names for the types.
@@ -1093,12 +1157,30 @@ const TypeCode = {
1093
1157
  bytes: 'BYTES',
1094
1158
  json: 'JSON',
1095
1159
  jsonb: 'JSON',
1160
+ pgJsonb: 'JSON',
1096
1161
  interval: 'INTERVAL',
1097
1162
  proto: 'PROTO',
1098
1163
  enum: 'ENUM',
1099
1164
  array: 'ARRAY',
1100
1165
  struct: 'STRUCT',
1101
1166
  };
1167
+ // Parameterless type singletons. These are frozen and reused across `getType`
1168
+ // calls to eliminate heap allocations and GC pressure during parameter encoding.
1169
+ const TYPE_STRING = Object.freeze({ type: 'string' });
1170
+ const TYPE_BOOL = Object.freeze({ type: 'bool' });
1171
+ const TYPE_INT64 = Object.freeze({ type: 'int64' });
1172
+ const TYPE_FLOAT64 = Object.freeze({ type: 'float64' });
1173
+ const TYPE_FLOAT32 = Object.freeze({ type: 'float32' });
1174
+ const TYPE_NUMERIC = Object.freeze({ type: 'numeric' });
1175
+ const TYPE_PG_NUMERIC = Object.freeze({ type: 'pgNumeric' });
1176
+ const TYPE_PG_JSONB = Object.freeze({ type: 'pgJsonb' });
1177
+ const TYPE_PG_OID = Object.freeze({ type: 'pgOid' });
1178
+ const TYPE_INTERVAL = Object.freeze({ type: 'interval' });
1179
+ const TYPE_BYTES = Object.freeze({ type: 'bytes' });
1180
+ const TYPE_DATE = Object.freeze({ type: 'date' });
1181
+ const TYPE_TIMESTAMP = Object.freeze({ type: 'timestamp' });
1182
+ const TYPE_JSON = Object.freeze({ type: 'json' });
1183
+ const TYPE_UNSPECIFIED = Object.freeze({ type: 'unspecified' });
1102
1184
  /**
1103
1185
  * @typedef {ParamType} StructField
1104
1186
  * @property {string} name The name of the field.
@@ -1111,6 +1193,9 @@ const TypeCode = {
1111
1193
  * - float64
1112
1194
  * - int64
1113
1195
  * - numeric
1196
+ * - pgNumeric
1197
+ * - pgJsonb
1198
+ * - pgOid
1114
1199
  * - bool
1115
1200
  * - string
1116
1201
  * - bytes
@@ -1122,18 +1207,39 @@ const TypeCode = {
1122
1207
  * - date
1123
1208
  * - struct
1124
1209
  * - array
1210
+ * - unspecified
1125
1211
  * @property {StructField[]} [fields] **For struct types only**. Type
1126
1212
  * definitions for the individual fields.
1127
1213
  * @property {string|ParamType} [child] **For array types only**. The array
1128
1214
  * element type.
1129
1215
  */
1216
+ let uuidUntypedFlagWarned = false;
1217
+ function _resetUuidUntypedFlagWarnedForTest() {
1218
+ uuidUntypedFlagWarned = false;
1219
+ }
1220
+ function isUuidUntypedEnv() {
1221
+ if (process.env['SPANNER_ENABLE_UUID_AS_UNTYPED']?.toLowerCase() === 'true') {
1222
+ if (!uuidUntypedFlagWarned) {
1223
+ process.emitWarning('SPANNER_ENABLE_UUID_AS_UNTYPED environment variable is deprecated and will be removed in a future release.', 'DeprecationWarning');
1224
+ uuidUntypedFlagWarned = true;
1225
+ }
1226
+ return true;
1227
+ }
1228
+ return false;
1229
+ }
1130
1230
  /**
1131
1231
  * Get the corresponding Spanner data type for the provided value.
1132
1232
  *
1233
+ * NOTE: This function is internal and not exposed in the public API.
1234
+ * Parameterless types return shared, frozen singletons to eliminate allocation
1235
+ * and GC overhead. Callers must treat the returned Type descriptor as read-only.
1236
+ *
1133
1237
  * @private
1134
1238
  *
1135
1239
  * @param {*} value - The value.
1136
- * @returns {object}
1240
+ * @param {boolean} [enableUuidAsUntyped] - Whether UUID strings should be
1241
+ * typed as unspecified.
1242
+ * @returns {Type}
1137
1243
  *
1138
1244
  * @example
1139
1245
  * ```
@@ -1141,31 +1247,55 @@ const TypeCode = {
1141
1247
  * // {type: 'float64'}
1142
1248
  * ```
1143
1249
  */
1144
- function getType(value) {
1145
- const isSpecialNumber = (0, helper_1.isInfinite)(value) || ((0, helper_1.isNumber)(value) && isNaN(value));
1146
- if (value instanceof Float32) {
1147
- return { type: 'float32' };
1250
+ function getType(value, enableUuidAsUntyped) {
1251
+ if (value === null || value === undefined) {
1252
+ return TYPE_UNSPECIFIED;
1253
+ }
1254
+ if (typeof value === 'string') {
1255
+ const isUuidUntyped = enableUuidAsUntyped ?? isUuidUntypedEnv();
1256
+ if (isUuidUntyped && (0, helper_1.isUuid)(value)) {
1257
+ return TYPE_UNSPECIFIED;
1258
+ }
1259
+ return TYPE_STRING;
1260
+ }
1261
+ if (typeof value === 'number') {
1262
+ return Number.isInteger(value) ? TYPE_INT64 : TYPE_FLOAT64;
1263
+ }
1264
+ if (typeof value === 'boolean') {
1265
+ return TYPE_BOOL;
1266
+ }
1267
+ if (Buffer.isBuffer(value)) {
1268
+ return TYPE_BYTES;
1269
+ }
1270
+ if (value instanceof SpannerDate) {
1271
+ return TYPE_DATE;
1148
1272
  }
1149
- if ((0, helper_1.isDecimal)(value) || isSpecialNumber || value instanceof Float) {
1150
- return { type: 'float64' };
1273
+ if (value instanceof Date || (0, helper_1.isDate)(value)) {
1274
+ return TYPE_TIMESTAMP;
1151
1275
  }
1152
- if ((0, helper_1.isNumber)(value) || value instanceof Int) {
1153
- return { type: 'int64' };
1276
+ if (value instanceof Int) {
1277
+ return TYPE_INT64;
1278
+ }
1279
+ if (value instanceof Float) {
1280
+ return TYPE_FLOAT64;
1281
+ }
1282
+ if (value instanceof Float32) {
1283
+ return TYPE_FLOAT32;
1154
1284
  }
1155
1285
  if (value instanceof Numeric) {
1156
- return { type: 'numeric' };
1286
+ return TYPE_NUMERIC;
1157
1287
  }
1158
1288
  if (value instanceof PGNumeric) {
1159
- return { type: 'pgNumeric' };
1289
+ return TYPE_PG_NUMERIC;
1160
1290
  }
1161
1291
  if (value instanceof PGJsonb) {
1162
- return { type: 'pgJsonb' };
1292
+ return TYPE_PG_JSONB;
1163
1293
  }
1164
1294
  if (value instanceof PGOid) {
1165
- return { type: 'pgOid' };
1295
+ return TYPE_PG_OID;
1166
1296
  }
1167
1297
  if (value instanceof Interval) {
1168
- return { type: 'interval' };
1298
+ return TYPE_INTERVAL;
1169
1299
  }
1170
1300
  if (value instanceof ProtoMessage) {
1171
1301
  return { type: 'proto', fullName: value.fullName };
@@ -1173,39 +1303,16 @@ function getType(value) {
1173
1303
  if (value instanceof ProtoEnum) {
1174
1304
  return { type: 'enum', fullName: value.fullName };
1175
1305
  }
1176
- if ((0, helper_1.isBoolean)(value)) {
1177
- return { type: 'bool' };
1178
- }
1179
- if (process.env['SPANNER_ENABLE_UUID_AS_UNTYPED'] === 'true') {
1180
- if (!uuidUntypedFlagWarned) {
1181
- process.emitWarning('SPANNER_ENABLE_UUID_AS_UNTYPED environment variable is deprecated and will be removed in a future release.', 'DeprecationWarning');
1182
- uuidUntypedFlagWarned = true;
1183
- }
1184
- if ((0, helper_1.isUuid)(value)) {
1185
- return { type: 'unspecified' };
1186
- }
1187
- }
1188
- if ((0, helper_1.isString)(value)) {
1189
- return { type: 'string' };
1190
- }
1191
- if (Buffer.isBuffer(value)) {
1192
- return { type: 'bytes' };
1193
- }
1194
- if (value instanceof SpannerDate) {
1195
- return { type: 'date' };
1196
- }
1197
- if ((0, helper_1.isDate)(value)) {
1198
- return { type: 'timestamp' };
1199
- }
1200
1306
  if (value instanceof Struct) {
1307
+ const isUuidUntyped = enableUuidAsUntyped ?? isUuidUntypedEnv();
1201
1308
  return {
1202
1309
  type: 'struct',
1203
1310
  fields: Array.from(value).map(({ name, value }) => {
1204
- return Object.assign({ name }, getType(value));
1311
+ return Object.assign({ name }, getType(value, isUuidUntyped));
1205
1312
  }),
1206
1313
  };
1207
1314
  }
1208
- if ((0, helper_1.isArray)(value)) {
1315
+ if (Array.isArray(value)) {
1209
1316
  let child;
1210
1317
  for (let i = 0; i < value.length; i++) {
1211
1318
  child = value[i];
@@ -1215,13 +1322,22 @@ function getType(value) {
1215
1322
  }
1216
1323
  return {
1217
1324
  type: 'array',
1218
- child: getType(child),
1325
+ child: getType(child, enableUuidAsUntyped),
1219
1326
  };
1220
1327
  }
1328
+ if (value instanceof String) {
1329
+ return TYPE_STRING;
1330
+ }
1331
+ if (value instanceof Number) {
1332
+ return Number.isInteger(value.valueOf()) ? TYPE_INT64 : TYPE_FLOAT64;
1333
+ }
1334
+ if (value instanceof Boolean) {
1335
+ return TYPE_BOOL;
1336
+ }
1221
1337
  if ((0, helper_1.isObject)(value)) {
1222
- return { type: 'json' };
1338
+ return TYPE_JSON;
1223
1339
  }
1224
- return { type: 'unspecified' };
1340
+ return TYPE_UNSPECIFIED;
1225
1341
  }
1226
1342
  /**
1227
1343
  * Converts a value to google.protobuf.ListValue
@@ -1299,7 +1415,7 @@ function createTypeObject(friendlyType) {
1299
1415
  type.typeAnnotation =
1300
1416
  spannerClient.spanner.v1.TypeAnnotationCode.PG_NUMERIC;
1301
1417
  }
1302
- else if (friendlyType.type === 'jsonb') {
1418
+ else if (friendlyType.type === 'jsonb' || friendlyType.type === 'pgJsonb') {
1303
1419
  type.typeAnnotation = spannerClient.spanner.v1.TypeAnnotationCode.PG_JSONB;
1304
1420
  }
1305
1421
  else if (friendlyType.type === 'pgOid') {
@@ -1329,5 +1445,7 @@ exports.codec = {
1329
1445
  encode,
1330
1446
  getType,
1331
1447
  Struct,
1448
+ _resetUuidUntypedFlagWarnedForTest,
1449
+ isUuidUntypedEnv,
1332
1450
  };
1333
1451
  //# sourceMappingURL=codec.js.map
@@ -59,6 +59,20 @@ export declare const AFE_SERVER_TIMING_HEADER = "x-goog-spanner-enable-afe-serve
59
59
  export declare function addLeaderAwareRoutingHeader(headers: {
60
60
  [k: string]: string;
61
61
  }): void;
62
+ /**
63
+ * Returns whether AFE (Application Frontend Extension) server timing is enabled.
64
+ *
65
+ * This function checks the value of the environment variable
66
+ * `SPANNER_DISABLE_AFE_SERVER_TIMING`. If the variable is set to the
67
+ * string `'true'` (case-insensitive), then AFE server timing is considered disabled,
68
+ * and this function returns `false`. For all other values (including if the variable is unset),
69
+ * the function returns `true`.
70
+ *
71
+ * @returns {boolean} `true` if AFE server timing is enabled; otherwise, `false`.
72
+ */
73
+ export declare function isAFEServerTimingEnabled(): boolean;
74
+ /** Resets the cached value (use in tests if env changes). */
75
+ export declare function resetAFEServerTimingForTest(): void;
62
76
  /**
63
77
  * Returns common headers to add.
64
78
  * @param headers Common header list.
@@ -17,8 +17,9 @@
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.AFE_SERVER_TIMING_HEADER = exports.END_TO_END_TRACING_HEADER = exports.LEADER_AWARE_ROUTING_HEADER = exports.CLOUD_RESOURCE_HEADER = void 0;
19
19
  exports.addLeaderAwareRoutingHeader = addLeaderAwareRoutingHeader;
20
+ exports.isAFEServerTimingEnabled = isAFEServerTimingEnabled;
21
+ exports.resetAFEServerTimingForTest = resetAFEServerTimingForTest;
20
22
  exports.getCommonHeaders = getCommonHeaders;
21
- const _1 = require(".");
22
23
  /*!
23
24
  * HTTP header for the resource prefix to improve routing
24
25
  * by the backend.
@@ -43,17 +44,41 @@ exports.AFE_SERVER_TIMING_HEADER = 'x-goog-spanner-enable-afe-server-timing';
43
44
  function addLeaderAwareRoutingHeader(headers) {
44
45
  headers[exports.LEADER_AWARE_ROUTING_HEADER] = 'true';
45
46
  }
47
+ let isAFEServerTimingEnabledCached;
48
+ /**
49
+ * Returns whether AFE (Application Frontend Extension) server timing is enabled.
50
+ *
51
+ * This function checks the value of the environment variable
52
+ * `SPANNER_DISABLE_AFE_SERVER_TIMING`. If the variable is set to the
53
+ * string `'true'` (case-insensitive), then AFE server timing is considered disabled,
54
+ * and this function returns `false`. For all other values (including if the variable is unset),
55
+ * the function returns `true`.
56
+ *
57
+ * @returns {boolean} `true` if AFE server timing is enabled; otherwise, `false`.
58
+ */
59
+ function isAFEServerTimingEnabled() {
60
+ if (isAFEServerTimingEnabledCached === undefined) {
61
+ isAFEServerTimingEnabledCached =
62
+ process.env['SPANNER_DISABLE_AFE_SERVER_TIMING']?.toLowerCase() !==
63
+ 'true';
64
+ }
65
+ return isAFEServerTimingEnabledCached;
66
+ }
67
+ /** Resets the cached value (use in tests if env changes). */
68
+ function resetAFEServerTimingForTest() {
69
+ isAFEServerTimingEnabledCached = undefined;
70
+ }
46
71
  /**
47
72
  * Returns common headers to add.
48
73
  * @param headers Common header list.
49
74
  */
50
75
  function getCommonHeaders(resourceName, enableTracing) {
51
76
  const headers = {};
52
- if (process.env.SPANNER_ENABLE_END_TO_END_TRACING === 'true' ||
77
+ if (process.env.SPANNER_ENABLE_END_TO_END_TRACING?.toLowerCase() === 'true' ||
53
78
  enableTracing) {
54
79
  headers[exports.END_TO_END_TRACING_HEADER] = 'true';
55
80
  }
56
- if (_1.Spanner.isAFEServerTimingEnabled()) {
81
+ if (isAFEServerTimingEnabled()) {
57
82
  headers[exports.AFE_SERVER_TIMING_HEADER] = 'true';
58
83
  }
59
84
  headers[exports.CLOUD_RESOURCE_HEADER] = resourceName;