1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import * as path from 'path';
|
15 | import * as querystring from 'querystring';
|
16 | import { PassThrough } from 'stream';
|
17 | import * as url from 'url';
|
18 |
|
19 |
|
20 | import { getPackageJSON } from './package-json-helper.cjs';
|
21 |
|
22 | const fileURLToPath = url.fileURLToPath;
|
23 | const isEsm = true;
|
24 | export function normalize(optionsOrCallback, cb) {
|
25 | const options = (typeof optionsOrCallback === 'object' ? optionsOrCallback : {});
|
26 | const callback = (typeof optionsOrCallback === 'function' ? optionsOrCallback : cb);
|
27 | return { options, callback };
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function objectEntries(obj) {
|
35 | return Object.keys(obj).map(key => [key, obj[key]]);
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export function fixedEncodeURIComponent(str) {
|
47 | return encodeURIComponent(str).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase());
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export function encodeURI(uri, encodeSlash) {
|
59 |
|
60 |
|
61 | return uri
|
62 | .split('/')
|
63 | .map(fixedEncodeURIComponent)
|
64 | .join(encodeSlash ? '%2F' : '/');
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | export function qsStringify(qs) {
|
72 | return querystring.stringify(qs, '&', '=', {
|
73 | encodeURIComponent: (component) => encodeURI(component, true),
|
74 | });
|
75 | }
|
76 | export function objectKeyToLowercase(object) {
|
77 | const newObj = {};
|
78 | for (let key of Object.keys(object)) {
|
79 | const value = object[key];
|
80 | key = key.toLowerCase();
|
81 | newObj[key] = value;
|
82 | }
|
83 | return newObj;
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export function unicodeJSONStringify(obj) {
|
91 | return JSON.stringify(obj).replace(/[\u0080-\uFFFF]/g, (char) => '\\u' + ('0000' + char.charCodeAt(0).toString(16)).slice(-4));
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | export function convertObjKeysToSnakeCase(obj) {
|
99 | if (obj instanceof Date || obj instanceof RegExp) {
|
100 | return obj;
|
101 | }
|
102 | if (Array.isArray(obj)) {
|
103 | return obj.map(convertObjKeysToSnakeCase);
|
104 | }
|
105 | if (obj instanceof Object) {
|
106 | return Object.keys(obj).reduce((acc, cur) => {
|
107 | const s = cur[0].toLocaleLowerCase() +
|
108 | cur.slice(1).replace(/([A-Z]+)/g, (match, p1) => {
|
109 | return `_${p1.toLowerCase()}`;
|
110 | });
|
111 | acc[s] = convertObjKeysToSnakeCase(obj[cur]);
|
112 | return acc;
|
113 | }, Object());
|
114 | }
|
115 | return obj;
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | export function formatAsUTCISO(dateTimeToFormat, includeTime = false, dateDelimiter = '', timeDelimiter = '') {
|
126 | const year = dateTimeToFormat.getUTCFullYear();
|
127 | const month = dateTimeToFormat.getUTCMonth() + 1;
|
128 | const day = dateTimeToFormat.getUTCDate();
|
129 | const hour = dateTimeToFormat.getUTCHours();
|
130 | const minute = dateTimeToFormat.getUTCMinutes();
|
131 | const second = dateTimeToFormat.getUTCSeconds();
|
132 | let resultString = `${year.toString().padStart(4, '0')}${dateDelimiter}${month
|
133 | .toString()
|
134 | .padStart(2, '0')}${dateDelimiter}${day.toString().padStart(2, '0')}`;
|
135 | if (includeTime) {
|
136 | resultString = `${resultString}T${hour
|
137 | .toString()
|
138 | .padStart(2, '0')}${timeDelimiter}${minute
|
139 | .toString()
|
140 | .padStart(2, '0')}${timeDelimiter}${second.toString().padStart(2, '0')}Z`;
|
141 | }
|
142 | return resultString;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | export function getRuntimeTrackingString() {
|
149 | if (
|
150 |
|
151 |
|
152 | globalThis.Deno &&
|
153 |
|
154 |
|
155 | globalThis.Deno.version &&
|
156 |
|
157 |
|
158 | globalThis.Deno.version.deno) {
|
159 |
|
160 |
|
161 | return `gl-deno/${globalThis.Deno.version.deno}`;
|
162 | }
|
163 | else {
|
164 | return `gl-node/${process.versions.node}`;
|
165 | }
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | export function getUserAgentString() {
|
172 | const pkg = getPackageJSON();
|
173 | const hyphenatedPackageName = pkg.name
|
174 | .replace('@google-cloud', 'gcloud-node')
|
175 | .replace('/', '-');
|
176 | return hyphenatedPackageName + '/' + pkg.version;
|
177 | }
|
178 | export function getDirName() {
|
179 | let dirToUse = '';
|
180 | try {
|
181 | dirToUse = __dirname;
|
182 | }
|
183 | catch (e) {
|
184 |
|
185 |
|
186 | dirToUse = path.dirname(fileURLToPath(import.meta.url));
|
187 | }
|
188 | return dirToUse;
|
189 | }
|
190 | export function getModuleFormat() {
|
191 | return isEsm ? 'ESM' : 'CJS';
|
192 | }
|
193 | export class PassThroughShim extends PassThrough {
|
194 | constructor() {
|
195 | super(...arguments);
|
196 | this.shouldEmitReading = true;
|
197 | this.shouldEmitWriting = true;
|
198 | }
|
199 | _read(size) {
|
200 | if (this.shouldEmitReading) {
|
201 | this.emit('reading');
|
202 | this.shouldEmitReading = false;
|
203 | }
|
204 | super._read(size);
|
205 | }
|
206 | _write(chunk, encoding, callback) {
|
207 | if (this.shouldEmitWriting) {
|
208 | this.emit('writing');
|
209 | this.shouldEmitWriting = false;
|
210 | }
|
211 |
|
212 | process.nextTick(() => {
|
213 | super._write(chunk, encoding, callback);
|
214 | });
|
215 | }
|
216 | _final(callback) {
|
217 |
|
218 |
|
219 | if (this.shouldEmitReading) {
|
220 | this.emit('reading');
|
221 | this.shouldEmitReading = false;
|
222 | }
|
223 | if (this.shouldEmitWriting) {
|
224 | this.emit('writing');
|
225 | this.shouldEmitWriting = false;
|
226 | }
|
227 | callback(null);
|
228 | }
|
229 | }
|