@google-cloud/spanner 8.7.0 → 8.8.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.
- package/build/protos/google/spanner/v1/commit_response.proto +16 -0
- package/build/protos/google/spanner/v1/spanner.proto +55 -2
- package/build/protos/google/spanner/v1/transaction.proto +38 -18
- package/build/protos/protos.d.ts +1639 -1473
- package/build/protos/protos.js +21738 -13062
- package/build/protos/protos.json +296 -213
- package/build/src/codec.d.ts +10 -16
- package/build/src/codec.js +337 -68
- package/build/src/common-grpc/service.js +26 -16
- package/build/src/database.js +4 -1
- package/build/src/helper.d.ts +16 -0
- package/build/src/helper.js +89 -0
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +34 -24
- package/build/src/instance.js +4 -1
- package/build/src/instrument.d.ts +10 -0
- package/build/src/instrument.js +74 -13
- package/build/src/metrics/metrics-tracer-factory.d.ts +1 -1
- package/build/src/metrics/metrics-tracer-factory.js +5 -5
- package/build/src/metrics/metrics-tracer.d.ts +3 -3
- package/build/src/metrics/metrics-tracer.js +6 -6
- package/build/src/multiplexed-session.js +22 -19
- package/build/src/partial-result-stream.d.ts +11 -0
- package/build/src/partial-result-stream.js +79 -12
- package/build/src/request_id_header.js +12 -25
- package/build/src/session-pool.js +37 -34
- package/build/src/session.js +1 -1
- package/build/src/transaction.js +1 -1
- package/build/src/v1/spanner_client.d.ts +47 -4
- package/build/src/v1/spanner_client.js +47 -2
- package/build/src/v1/spanner_client_config.json +4 -0
- package/package.json +3 -11
package/build/src/codec.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.codec = exports.Interval = exports.PGOid = exports.PGJsonb = exports.ProtoEnum = exports.ProtoMessage = exports.PGNumeric = exports.Numeric = exports.Struct = exports.Int = exports.Float = exports.Float32 = exports.SpannerDate = void 0;
|
|
4
|
+
exports.getDecoder = getDecoder;
|
|
4
5
|
/*!
|
|
5
6
|
* Copyright 2017 Google Inc. All Rights Reserved.
|
|
6
7
|
*
|
|
@@ -22,7 +23,37 @@ const helper_1 = require("./helper");
|
|
|
22
23
|
const big_js_1 = require("big.js");
|
|
23
24
|
const protos_1 = require("../protos/protos");
|
|
24
25
|
const google_gax_1 = require("google-gax");
|
|
25
|
-
const
|
|
26
|
+
const DATE_REGEX = /^\d{4}-\d{1,2}-\d{1,2}/;
|
|
27
|
+
const DIGITS_REGEX = /^\d+$/;
|
|
28
|
+
const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
29
|
+
function isValidDate(year, month, date) {
|
|
30
|
+
if (month < 0 || month > 11) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (date < 1) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
if (month === 1) {
|
|
37
|
+
const isLeap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
38
|
+
return date <= (isLeap ? 29 : 28);
|
|
39
|
+
}
|
|
40
|
+
return date <= DAYS_IN_MONTH[month];
|
|
41
|
+
}
|
|
42
|
+
function isValidTimestamp(year, month, day, hours, minutes, seconds, milliseconds) {
|
|
43
|
+
if (hours < 0 || hours > 23) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
if (minutes < 0 || minutes > 59) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (seconds < 0 || seconds > 59) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (milliseconds < 0 || milliseconds > 999) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return isValidDate(year, month, day);
|
|
56
|
+
}
|
|
26
57
|
let uuidUntypedFlagWarned = false;
|
|
27
58
|
/**
|
|
28
59
|
* Date-like object used to represent Cloud Spanner Dates. DATE types represent
|
|
@@ -54,17 +85,48 @@ let uuidUntypedFlagWarned = false;
|
|
|
54
85
|
class SpannerDate extends Date {
|
|
55
86
|
constructor(...dateFields) {
|
|
56
87
|
const yearOrDateString = dateFields[0];
|
|
88
|
+
let customYear;
|
|
89
|
+
// Fast-path parsing for standard YYYY-MM-DD date strings.
|
|
90
|
+
// This avoids RegExp matching, array split allocations, and local timezone conversions
|
|
91
|
+
// by directly parsing date components and invoking the Date constructor in local time.
|
|
92
|
+
if (typeof yearOrDateString === 'string' &&
|
|
93
|
+
yearOrDateString.length === 10 &&
|
|
94
|
+
yearOrDateString[4] === '-' &&
|
|
95
|
+
yearOrDateString[7] === '-') {
|
|
96
|
+
const year = Number(yearOrDateString.substring(0, 4));
|
|
97
|
+
const month = Number(yearOrDateString.substring(5, 7)) - 1;
|
|
98
|
+
const date = Number(yearOrDateString.substring(8, 10));
|
|
99
|
+
if (year >= 1970 &&
|
|
100
|
+
!Number.isNaN(year) &&
|
|
101
|
+
isValidDate(year, month, date)) {
|
|
102
|
+
super(year, month, date);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (typeof yearOrDateString === 'number' &&
|
|
107
|
+
yearOrDateString > 0 &&
|
|
108
|
+
yearOrDateString < 100) {
|
|
109
|
+
customYear = yearOrDateString;
|
|
110
|
+
}
|
|
57
111
|
// yearOrDateString could be 0 (number).
|
|
58
112
|
if (yearOrDateString === null || yearOrDateString === undefined) {
|
|
59
113
|
dateFields[0] = new Date().toDateString();
|
|
60
114
|
}
|
|
61
115
|
// JavaScript Date objects will interpret ISO date strings as Zulu time,
|
|
62
116
|
// but by formatting it, we can infer local time.
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
117
|
+
if (typeof yearOrDateString === 'string' &&
|
|
118
|
+
DATE_REGEX.test(yearOrDateString)) {
|
|
119
|
+
const [yearStr, monthStr, dateStr] = yearOrDateString.split(/-|T/);
|
|
120
|
+
const year = parseInt(yearStr, 10);
|
|
121
|
+
if (year < 100) {
|
|
122
|
+
customYear = year;
|
|
123
|
+
}
|
|
124
|
+
dateFields = [`${monthStr}-${dateStr}-${yearStr}`];
|
|
66
125
|
}
|
|
67
126
|
super(...dateFields.slice(0, 3));
|
|
127
|
+
if (customYear !== undefined) {
|
|
128
|
+
this.setFullYear(customYear);
|
|
129
|
+
}
|
|
68
130
|
}
|
|
69
131
|
/**
|
|
70
132
|
* Returns the date in ISO date format.
|
|
@@ -130,7 +192,7 @@ class Int extends WrappedNumber {
|
|
|
130
192
|
}
|
|
131
193
|
valueOf() {
|
|
132
194
|
const num = Number(this.value);
|
|
133
|
-
if (
|
|
195
|
+
if (!Number.isSafeInteger(num)) {
|
|
134
196
|
throw new google_gax_1.GoogleError(`Integer ${this.value} is out of bounds.`);
|
|
135
197
|
}
|
|
136
198
|
return num;
|
|
@@ -263,7 +325,7 @@ class ProtoEnum {
|
|
|
263
325
|
* @code{IProtoEnumParams} can accept either a number or a string as a value so
|
|
264
326
|
* converting to string and checking whether it's numeric using regex.
|
|
265
327
|
*/
|
|
266
|
-
if (
|
|
328
|
+
if (DIGITS_REGEX.test(protoEnumParams.value.toString())) {
|
|
267
329
|
this.value = protoEnumParams.value.toString();
|
|
268
330
|
}
|
|
269
331
|
else if (protoEnumParams.enumObject &&
|
|
@@ -314,7 +376,7 @@ class PGOid extends WrappedNumber {
|
|
|
314
376
|
}
|
|
315
377
|
valueOf() {
|
|
316
378
|
const num = Number(this.value);
|
|
317
|
-
if (
|
|
379
|
+
if (!Number.isSafeInteger(num)) {
|
|
318
380
|
throw new google_gax_1.GoogleError(`PG.OID ${this.value} is out of bounds.`);
|
|
319
381
|
}
|
|
320
382
|
return num;
|
|
@@ -574,22 +636,29 @@ exports.Interval = Interval;
|
|
|
574
636
|
* @param {JSONOptions} [options] JSON options.
|
|
575
637
|
* @returns {object}
|
|
576
638
|
*/
|
|
639
|
+
const DEFAULT_JSON_OPTIONS = {
|
|
640
|
+
wrapNumbers: false,
|
|
641
|
+
wrapStructs: false,
|
|
642
|
+
includeNameless: false,
|
|
643
|
+
};
|
|
577
644
|
function convertFieldsToJson(fields, options) {
|
|
578
645
|
const json = {};
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
for (
|
|
587
|
-
|
|
646
|
+
const resolvedOptions = options
|
|
647
|
+
? {
|
|
648
|
+
wrapNumbers: !!options.wrapNumbers,
|
|
649
|
+
wrapStructs: !!options.wrapStructs,
|
|
650
|
+
includeNameless: !!options.includeNameless,
|
|
651
|
+
}
|
|
652
|
+
: DEFAULT_JSON_OPTIONS;
|
|
653
|
+
for (let i = 0; i < fields.length; i++) {
|
|
654
|
+
const field = fields[i];
|
|
655
|
+
const name = field.name;
|
|
656
|
+
if (!name && !resolvedOptions.includeNameless) {
|
|
588
657
|
continue;
|
|
589
658
|
}
|
|
590
|
-
const fieldName = name ? name : `_${
|
|
659
|
+
const fieldName = name ? name : `_${i}`;
|
|
591
660
|
try {
|
|
592
|
-
json[fieldName] = convertValueToJson(value,
|
|
661
|
+
json[fieldName] = convertValueToJson(field.value, resolvedOptions);
|
|
593
662
|
}
|
|
594
663
|
catch (e) {
|
|
595
664
|
e.message = [
|
|
@@ -598,7 +667,6 @@ function convertFieldsToJson(fields, options) {
|
|
|
598
667
|
].join(' ');
|
|
599
668
|
throw e;
|
|
600
669
|
}
|
|
601
|
-
index++;
|
|
602
670
|
}
|
|
603
671
|
return json;
|
|
604
672
|
}
|
|
@@ -612,6 +680,9 @@ function convertFieldsToJson(fields, options) {
|
|
|
612
680
|
* @return {*}
|
|
613
681
|
*/
|
|
614
682
|
function convertValueToJson(value, options) {
|
|
683
|
+
if (value === null || typeof value !== 'object') {
|
|
684
|
+
return value;
|
|
685
|
+
}
|
|
615
686
|
if (!options.wrapNumbers && value instanceof WrappedNumber) {
|
|
616
687
|
return value.valueOf();
|
|
617
688
|
}
|
|
@@ -633,111 +704,308 @@ function convertValueToJson(value, options) {
|
|
|
633
704
|
return value;
|
|
634
705
|
}
|
|
635
706
|
/**
|
|
636
|
-
*
|
|
707
|
+
* Pre-compiles and returns a column-specific decoder function.
|
|
708
|
+
* This function resolves the type switch-case and metadata configuration
|
|
709
|
+
* once, returning a high-performance closure for cell decoding.
|
|
637
710
|
*
|
|
638
711
|
* @private
|
|
639
712
|
*
|
|
640
|
-
* @param {
|
|
641
|
-
* @param {object[]
|
|
642
|
-
* @param
|
|
643
|
-
* @returns {
|
|
713
|
+
* @param {google.spanner.v1.Type} type The Spanner column type metadata.
|
|
714
|
+
* @param {object} [columnMetadata] Optional column-specific metadata (e.g., custom schemas).
|
|
715
|
+
* @param {JSONOptions} [options] Optional configuration for JSON mode decoding.
|
|
716
|
+
* @returns {function} A high-performance decoding closure: `(value: Value) => Value`.
|
|
644
717
|
*/
|
|
645
|
-
function
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
718
|
+
function getDecoder(type, columnMetadata, options) {
|
|
719
|
+
const jsonMode = options !== undefined;
|
|
720
|
+
const wrapNumbers = !jsonMode || options?.wrapNumbers === true;
|
|
721
|
+
const wrapStructs = !jsonMode || options?.wrapStructs === true;
|
|
722
|
+
if (!type) {
|
|
723
|
+
return val => val;
|
|
724
|
+
}
|
|
725
|
+
let decoder;
|
|
651
726
|
switch (type.code) {
|
|
652
727
|
case protos_1.google.spanner.v1.TypeCode.BYTES:
|
|
653
728
|
case 'BYTES':
|
|
654
|
-
|
|
729
|
+
decoder = val => Buffer.from(val, 'base64');
|
|
655
730
|
break;
|
|
656
731
|
case protos_1.google.spanner.v1.TypeCode.PROTO:
|
|
657
732
|
case 'PROTO':
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
733
|
+
if (jsonMode) {
|
|
734
|
+
decoder = val => {
|
|
735
|
+
const decoded = Buffer.from(val, 'base64');
|
|
736
|
+
if (columnMetadata) {
|
|
737
|
+
return columnMetadata['toObject'](columnMetadata['decode'](decoded));
|
|
738
|
+
}
|
|
739
|
+
return decoded.toString();
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
else {
|
|
743
|
+
decoder = val => {
|
|
744
|
+
const decoded = Buffer.from(val, 'base64');
|
|
745
|
+
return new ProtoMessage({
|
|
746
|
+
value: decoded,
|
|
747
|
+
fullName: type.protoTypeFqn,
|
|
748
|
+
messageFunction: columnMetadata,
|
|
749
|
+
});
|
|
750
|
+
};
|
|
751
|
+
}
|
|
664
752
|
break;
|
|
665
753
|
case protos_1.google.spanner.v1.TypeCode.ENUM:
|
|
666
754
|
case 'ENUM':
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
755
|
+
if (jsonMode) {
|
|
756
|
+
decoder = val => {
|
|
757
|
+
let enumVal;
|
|
758
|
+
if (DIGITS_REGEX.test(val.toString())) {
|
|
759
|
+
enumVal = val.toString();
|
|
760
|
+
}
|
|
761
|
+
else if (columnMetadata &&
|
|
762
|
+
Object.prototype.hasOwnProperty.call(columnMetadata, val)) {
|
|
763
|
+
enumVal = columnMetadata[val];
|
|
764
|
+
}
|
|
765
|
+
else {
|
|
766
|
+
throw new google_gax_1.GoogleError('protoEnumParams cannot be used for constructing the ProtoEnum. Pass the number as the value or provide the enum string constant as the value along with the corresponding enumObject generated by protobufjs-cli.');
|
|
767
|
+
}
|
|
768
|
+
if (columnMetadata) {
|
|
769
|
+
const proto = Object.getPrototypeOf(columnMetadata);
|
|
770
|
+
if (proto &&
|
|
771
|
+
proto !== Object.prototype &&
|
|
772
|
+
proto[enumVal] !== undefined) {
|
|
773
|
+
return proto[enumVal];
|
|
774
|
+
}
|
|
775
|
+
if (Object.prototype.hasOwnProperty.call(columnMetadata, enumVal)) {
|
|
776
|
+
return columnMetadata[enumVal];
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return enumVal;
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
else {
|
|
783
|
+
decoder = val => new ProtoEnum({
|
|
784
|
+
value: val,
|
|
785
|
+
fullName: type.protoTypeFqn,
|
|
786
|
+
enumObject: columnMetadata,
|
|
787
|
+
});
|
|
788
|
+
}
|
|
672
789
|
break;
|
|
673
790
|
case protos_1.google.spanner.v1.TypeCode.FLOAT32:
|
|
674
791
|
case 'FLOAT32':
|
|
675
|
-
|
|
792
|
+
decoder = wrapNumbers ? val => new Float32(val) : val => Number(val);
|
|
676
793
|
break;
|
|
677
794
|
case protos_1.google.spanner.v1.TypeCode.FLOAT64:
|
|
678
795
|
case 'FLOAT64':
|
|
679
|
-
|
|
796
|
+
decoder = wrapNumbers ? val => new Float(val) : val => Number(val);
|
|
680
797
|
break;
|
|
681
798
|
case protos_1.google.spanner.v1.TypeCode.INT64:
|
|
682
799
|
case 'INT64':
|
|
683
800
|
if (type.typeAnnotation ===
|
|
684
801
|
protos_1.google.spanner.v1.TypeAnnotationCode.PG_OID ||
|
|
685
802
|
type.typeAnnotation === 'PG_OID') {
|
|
686
|
-
|
|
687
|
-
|
|
803
|
+
decoder = wrapNumbers
|
|
804
|
+
? val => new PGOid(val)
|
|
805
|
+
: val => {
|
|
806
|
+
const num = Number(val);
|
|
807
|
+
if (!Number.isSafeInteger(num)) {
|
|
808
|
+
throw new google_gax_1.GoogleError(`PG.OID ${val} is out of bounds.`);
|
|
809
|
+
}
|
|
810
|
+
return num;
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
decoder = wrapNumbers
|
|
815
|
+
? val => new Int(val)
|
|
816
|
+
: val => {
|
|
817
|
+
const num = Number(val);
|
|
818
|
+
if (!Number.isSafeInteger(num)) {
|
|
819
|
+
throw new google_gax_1.GoogleError(`Integer ${val} is out of bounds.`);
|
|
820
|
+
}
|
|
821
|
+
return num;
|
|
822
|
+
};
|
|
688
823
|
}
|
|
689
|
-
decoded = new Int(decoded);
|
|
690
824
|
break;
|
|
691
825
|
case protos_1.google.spanner.v1.TypeCode.NUMERIC:
|
|
692
826
|
case 'NUMERIC':
|
|
693
827
|
if (type.typeAnnotation ===
|
|
694
828
|
protos_1.google.spanner.v1.TypeAnnotationCode.PG_NUMERIC ||
|
|
695
829
|
type.typeAnnotation === 'PG_NUMERIC') {
|
|
696
|
-
|
|
697
|
-
|
|
830
|
+
decoder = val => new PGNumeric(val);
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
decoder = val => new Numeric(val);
|
|
698
834
|
}
|
|
699
|
-
decoded = new Numeric(decoded);
|
|
700
835
|
break;
|
|
701
836
|
case protos_1.google.spanner.v1.TypeCode.TIMESTAMP:
|
|
702
837
|
case 'TIMESTAMP':
|
|
703
|
-
|
|
838
|
+
decoder = val => parsePreciseDate(val);
|
|
704
839
|
break;
|
|
705
840
|
case protos_1.google.spanner.v1.TypeCode.DATE:
|
|
706
841
|
case 'DATE':
|
|
707
|
-
|
|
842
|
+
decoder = val => new SpannerDate(val);
|
|
708
843
|
break;
|
|
709
844
|
case protos_1.google.spanner.v1.TypeCode.JSON:
|
|
710
845
|
case 'JSON':
|
|
711
846
|
if (type.typeAnnotation ===
|
|
712
847
|
protos_1.google.spanner.v1.TypeAnnotationCode.PG_JSONB ||
|
|
713
848
|
type.typeAnnotation === 'PG_JSONB') {
|
|
714
|
-
|
|
715
|
-
|
|
849
|
+
decoder = val => new PGJsonb(val);
|
|
850
|
+
}
|
|
851
|
+
else {
|
|
852
|
+
decoder = val => JSON.parse(val);
|
|
716
853
|
}
|
|
717
|
-
decoded = JSON.parse(decoded);
|
|
718
854
|
break;
|
|
719
855
|
case protos_1.google.spanner.v1.TypeCode.INTERVAL:
|
|
720
856
|
case 'INTERVAL':
|
|
721
|
-
|
|
857
|
+
decoder = val => Interval.fromISO8601(val);
|
|
722
858
|
break;
|
|
723
859
|
case protos_1.google.spanner.v1.TypeCode.ARRAY:
|
|
724
|
-
case 'ARRAY':
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
});
|
|
860
|
+
case 'ARRAY': {
|
|
861
|
+
const elementDecoder = getDecoder(type.arrayElementType, columnMetadata, options);
|
|
862
|
+
decoder = val => val.map(elementDecoder);
|
|
728
863
|
break;
|
|
864
|
+
}
|
|
729
865
|
case protos_1.google.spanner.v1.TypeCode.STRUCT:
|
|
730
|
-
case 'STRUCT':
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
866
|
+
case 'STRUCT': {
|
|
867
|
+
const includeNameless = options?.includeNameless === true;
|
|
868
|
+
const fields = type.structType.fields;
|
|
869
|
+
const fieldDecoders = [];
|
|
870
|
+
for (let i = 0; i < fields.length; i++) {
|
|
871
|
+
const { name, type: fieldType } = fields[i];
|
|
872
|
+
if (!wrapStructs && !name && !includeNameless) {
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
const decodedName = wrapStructs ? name : name ? name : `_${i}`;
|
|
876
|
+
fieldDecoders.push({
|
|
877
|
+
name,
|
|
878
|
+
decodedName,
|
|
879
|
+
decoder: getDecoder(fieldType, columnMetadata &&
|
|
880
|
+
name !== null &&
|
|
881
|
+
name !== undefined &&
|
|
882
|
+
Object.prototype.hasOwnProperty.call(columnMetadata, name)
|
|
883
|
+
? columnMetadata[name]
|
|
884
|
+
: undefined, options),
|
|
885
|
+
index: i,
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
if (wrapStructs) {
|
|
889
|
+
decoder = val => {
|
|
890
|
+
const isArr = Array.isArray(val);
|
|
891
|
+
const structFields = fieldDecoders.map(({ name, decodedName, decoder, index }) => {
|
|
892
|
+
const value = decoder(isArr
|
|
893
|
+
? val[index]
|
|
894
|
+
: name !== null &&
|
|
895
|
+
name !== undefined &&
|
|
896
|
+
Object.prototype.hasOwnProperty.call(val, name)
|
|
897
|
+
? val[name]
|
|
898
|
+
: val[index]);
|
|
899
|
+
return { name: decodedName, value };
|
|
900
|
+
});
|
|
901
|
+
return Struct.fromArray(structFields);
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
else {
|
|
905
|
+
decoder = val => {
|
|
906
|
+
const isArr = Array.isArray(val);
|
|
907
|
+
const structObj = {};
|
|
908
|
+
const len = fieldDecoders.length;
|
|
909
|
+
for (let i = 0; i < len; i++) {
|
|
910
|
+
const { name, decodedName, decoder, index } = fieldDecoders[i];
|
|
911
|
+
structObj[decodedName] = decoder(isArr
|
|
912
|
+
? val[index]
|
|
913
|
+
: name !== null &&
|
|
914
|
+
name !== undefined &&
|
|
915
|
+
Object.prototype.hasOwnProperty.call(val, name)
|
|
916
|
+
? val[name]
|
|
917
|
+
: val[index]);
|
|
918
|
+
}
|
|
919
|
+
return structObj;
|
|
920
|
+
};
|
|
921
|
+
}
|
|
736
922
|
break;
|
|
923
|
+
}
|
|
737
924
|
default:
|
|
925
|
+
decoder = val => val;
|
|
738
926
|
break;
|
|
739
927
|
}
|
|
740
|
-
return
|
|
928
|
+
return val => (val === null || val === undefined ? null : decoder(val));
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* Re-decode after the generic gRPC decoding step.
|
|
932
|
+
*
|
|
933
|
+
* @private
|
|
934
|
+
*
|
|
935
|
+
* @param {*} value Value to decode
|
|
936
|
+
* @param {object[]} type Value type object.
|
|
937
|
+
* @param columnMetadata Optional parameter to deserialize data
|
|
938
|
+
* @returns {*}
|
|
939
|
+
*/
|
|
940
|
+
function decode(value, type, columnMetadata) {
|
|
941
|
+
return getDecoder(type, columnMetadata)(value);
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Fast-path parser for RFC 3339 formatted Spanner UTC timestamp strings.
|
|
945
|
+
*
|
|
946
|
+
* This completely avoids the overhead of the `@google-cloud/precise-date` string
|
|
947
|
+
* constructor which performs multiple RegExp executions and internally allocates a second
|
|
948
|
+
* helper Date instance.
|
|
949
|
+
*
|
|
950
|
+
* @private
|
|
951
|
+
* @param {string} isoString The Zulu timestamp string (e.g. "2021-05-11T16:46:04.872345678Z").
|
|
952
|
+
* @returns {PreciseDate} The parsed PreciseDate instance.
|
|
953
|
+
*/
|
|
954
|
+
function parsePreciseDate(isoString) {
|
|
955
|
+
if (typeof isoString === 'string' &&
|
|
956
|
+
isoString.length >= 20 &&
|
|
957
|
+
(isoString[isoString.length - 1] === 'Z' ||
|
|
958
|
+
isoString[isoString.length - 1] === 'z') &&
|
|
959
|
+
isoString[4] === '-' &&
|
|
960
|
+
isoString[7] === '-' &&
|
|
961
|
+
isoString[10] === 'T' &&
|
|
962
|
+
isoString[13] === ':' &&
|
|
963
|
+
isoString[16] === ':') {
|
|
964
|
+
const year = Number(isoString.substring(0, 4));
|
|
965
|
+
const month = Number(isoString.substring(5, 7)) - 1;
|
|
966
|
+
const day = Number(isoString.substring(8, 10));
|
|
967
|
+
const hours = Number(isoString.substring(11, 13));
|
|
968
|
+
const minutes = Number(isoString.substring(14, 16));
|
|
969
|
+
const seconds = Number(isoString.substring(17, 19));
|
|
970
|
+
if (Number.isNaN(year) ||
|
|
971
|
+
year < 1970 ||
|
|
972
|
+
Number.isNaN(month) ||
|
|
973
|
+
Number.isNaN(day) ||
|
|
974
|
+
Number.isNaN(hours) ||
|
|
975
|
+
Number.isNaN(minutes) ||
|
|
976
|
+
Number.isNaN(seconds)) {
|
|
977
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
978
|
+
}
|
|
979
|
+
let milliseconds = 0;
|
|
980
|
+
let microseconds = 0;
|
|
981
|
+
let nanoseconds = 0;
|
|
982
|
+
const dotIndex = isoString.indexOf('.', 19);
|
|
983
|
+
if (dotIndex !== -1) {
|
|
984
|
+
if (dotIndex !== 19) {
|
|
985
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
986
|
+
}
|
|
987
|
+
const subSecondsStr = isoString.substring(dotIndex + 1, isoString.length - 1);
|
|
988
|
+
if (!DIGITS_REGEX.test(subSecondsStr)) {
|
|
989
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
990
|
+
}
|
|
991
|
+
const padded = subSecondsStr.padEnd(9, '0');
|
|
992
|
+
milliseconds = Number(padded.substring(0, 3));
|
|
993
|
+
microseconds = Number(padded.substring(3, 6));
|
|
994
|
+
nanoseconds = Number(padded.substring(6, 9));
|
|
995
|
+
}
|
|
996
|
+
else if (isoString.length !== 20) {
|
|
997
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
998
|
+
}
|
|
999
|
+
const utcMillis = Date.UTC(year, month, day, hours, minutes, seconds, milliseconds);
|
|
1000
|
+
if (!isValidTimestamp(year, month, day, hours, minutes, seconds, milliseconds)) {
|
|
1001
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
1002
|
+
}
|
|
1003
|
+
const preciseDate = new precise_date_1.PreciseDate(utcMillis);
|
|
1004
|
+
preciseDate.setMicroseconds(microseconds);
|
|
1005
|
+
preciseDate.setNanoseconds(nanoseconds);
|
|
1006
|
+
return preciseDate;
|
|
1007
|
+
}
|
|
1008
|
+
return new precise_date_1.PreciseDate(isoString);
|
|
741
1009
|
}
|
|
742
1010
|
/**
|
|
743
1011
|
* Encode a value in the format the API expects.
|
|
@@ -912,7 +1180,7 @@ function getType(value) {
|
|
|
912
1180
|
process.emitWarning('SPANNER_ENABLE_UUID_AS_UNTYPED environment variable is deprecated and will be removed in a future release.', 'DeprecationWarning');
|
|
913
1181
|
uuidUntypedFlagWarned = true;
|
|
914
1182
|
}
|
|
915
|
-
if (
|
|
1183
|
+
if ((0, helper_1.isUuid)(value)) {
|
|
916
1184
|
return { type: 'unspecified' };
|
|
917
1185
|
}
|
|
918
1186
|
}
|
|
@@ -1055,6 +1323,7 @@ exports.codec = {
|
|
|
1055
1323
|
PGOid,
|
|
1056
1324
|
Interval,
|
|
1057
1325
|
convertFieldsToJson,
|
|
1326
|
+
getDecoder,
|
|
1058
1327
|
decode,
|
|
1059
1328
|
encode,
|
|
1060
1329
|
getType,
|
|
@@ -21,14 +21,14 @@ exports.GrpcService = exports.ObjectToStructConverter = void 0;
|
|
|
21
21
|
*/
|
|
22
22
|
const path = require("path");
|
|
23
23
|
const common_1 = require("@google-cloud/common");
|
|
24
|
-
const
|
|
24
|
+
const helper_1 = require("../helper");
|
|
25
25
|
const proto_loader_1 = require("@grpc/proto-loader");
|
|
26
26
|
const duplexify = require("duplexify");
|
|
27
27
|
const extend = require("extend");
|
|
28
28
|
const google_gax_1 = require("google-gax");
|
|
29
29
|
const retryRequest = require("retry-request");
|
|
30
30
|
const stream_1 = require("stream");
|
|
31
|
-
const
|
|
31
|
+
const helper_2 = require("../helper");
|
|
32
32
|
const gaxProtoPath = path.join(path.dirname(require.resolve('google-gax')), '..', 'protos');
|
|
33
33
|
// TODO: convert this object to an array
|
|
34
34
|
/**
|
|
@@ -173,7 +173,7 @@ class ObjectToStructConverter {
|
|
|
173
173
|
for (const prop in obj) {
|
|
174
174
|
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
|
|
175
175
|
const value = obj[prop];
|
|
176
|
-
if ((0,
|
|
176
|
+
if ((0, helper_2.isUndefined)(value)) {
|
|
177
177
|
continue;
|
|
178
178
|
}
|
|
179
179
|
convertedObject.fields[prop] = this.encodeValue_(value);
|
|
@@ -198,22 +198,22 @@ class ObjectToStructConverter {
|
|
|
198
198
|
*/
|
|
199
199
|
encodeValue_(value) {
|
|
200
200
|
let convertedValue;
|
|
201
|
-
if ((0,
|
|
201
|
+
if ((0, helper_2.isNull)(value)) {
|
|
202
202
|
convertedValue = {
|
|
203
203
|
nullValue: 0,
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
|
-
else if ((0,
|
|
206
|
+
else if ((0, helper_2.isNumber)(value)) {
|
|
207
207
|
convertedValue = {
|
|
208
208
|
numberValue: value,
|
|
209
209
|
};
|
|
210
210
|
}
|
|
211
|
-
else if ((0,
|
|
211
|
+
else if ((0, helper_2.isString)(value)) {
|
|
212
212
|
convertedValue = {
|
|
213
213
|
stringValue: value,
|
|
214
214
|
};
|
|
215
215
|
}
|
|
216
|
-
else if ((0,
|
|
216
|
+
else if ((0, helper_2.isBoolean)(value)) {
|
|
217
217
|
convertedValue = {
|
|
218
218
|
boolValue: value,
|
|
219
219
|
};
|
|
@@ -223,7 +223,7 @@ class ObjectToStructConverter {
|
|
|
223
223
|
blobValue: value,
|
|
224
224
|
};
|
|
225
225
|
}
|
|
226
|
-
else if ((0,
|
|
226
|
+
else if ((0, helper_2.isObject)(value)) {
|
|
227
227
|
if (this.seenObjects.has(value)) {
|
|
228
228
|
// Circular reference.
|
|
229
229
|
if (!this.removeCircular) {
|
|
@@ -242,7 +242,7 @@ class ObjectToStructConverter {
|
|
|
242
242
|
};
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
else if ((0,
|
|
245
|
+
else if ((0, helper_2.isArray)(value)) {
|
|
246
246
|
convertedValue = {
|
|
247
247
|
listValue: {
|
|
248
248
|
values: value.map(this.encodeValue_.bind(this)),
|
|
@@ -531,7 +531,7 @@ class GrpcService extends common_1.Service {
|
|
|
531
531
|
const service = this.getService_(protoOpts);
|
|
532
532
|
const grpcMetadata = this.grpcMetadata;
|
|
533
533
|
const grpcOpts = {};
|
|
534
|
-
if ((0,
|
|
534
|
+
if ((0, helper_2.isNumber)(protoOpts.timeout)) {
|
|
535
535
|
grpcOpts.deadline = GrpcService.createDeadline_(protoOpts.timeout);
|
|
536
536
|
}
|
|
537
537
|
try {
|
|
@@ -563,18 +563,28 @@ class GrpcService extends common_1.Service {
|
|
|
563
563
|
* @return {*} - The decoded value.
|
|
564
564
|
*/
|
|
565
565
|
static decodeValue_(value) {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
566
|
+
const kind = value.kind;
|
|
567
|
+
switch (kind) {
|
|
568
|
+
case 'stringValue': {
|
|
569
|
+
return value.stringValue;
|
|
570
|
+
}
|
|
571
|
+
case 'numberValue': {
|
|
572
|
+
return value.numberValue;
|
|
573
|
+
}
|
|
574
|
+
case 'boolValue': {
|
|
575
|
+
return value.boolValue;
|
|
569
576
|
}
|
|
570
577
|
case 'nullValue': {
|
|
571
578
|
return null;
|
|
572
579
|
}
|
|
580
|
+
case 'structValue': {
|
|
581
|
+
return GrpcService.structToObj_(value.structValue);
|
|
582
|
+
}
|
|
573
583
|
case 'listValue': {
|
|
574
584
|
return value.listValue.values.map(GrpcService.decodeValue_);
|
|
575
585
|
}
|
|
576
586
|
default: {
|
|
577
|
-
return value[
|
|
587
|
+
return value[kind];
|
|
578
588
|
}
|
|
579
589
|
}
|
|
580
590
|
}
|
|
@@ -615,7 +625,7 @@ class GrpcService extends common_1.Service {
|
|
|
615
625
|
* @return {error|null}
|
|
616
626
|
*/
|
|
617
627
|
static decorateError_(err) {
|
|
618
|
-
const errorObj = (0,
|
|
628
|
+
const errorObj = (0, helper_2.isError)(err) ? err : {};
|
|
619
629
|
return GrpcService.decorateGrpcResponse_(errorObj, err);
|
|
620
630
|
}
|
|
621
631
|
/**
|
|
@@ -765,7 +775,7 @@ class GrpcService extends common_1.Service {
|
|
|
765
775
|
delete reqOpts.autoPaginate;
|
|
766
776
|
delete reqOpts.autoPaginateVal;
|
|
767
777
|
delete reqOpts.objectMode;
|
|
768
|
-
return (0,
|
|
778
|
+
return (0, helper_1.replaceProjectIdToken)(reqOpts, this.projectId);
|
|
769
779
|
}
|
|
770
780
|
/**
|
|
771
781
|
* To authorize requests through gRPC, we must get the raw google-auth-library
|