1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.PassThroughShim = exports.formatAsUTCISO = exports.convertObjKeysToSnakeCase = exports.unicodeJSONStringify = exports.objectKeyToLowercase = exports.qsStringify = exports.encodeURI = exports.fixedEncodeURIComponent = exports.objectEntries = exports.normalize = void 0;
|
17 | const querystring = require("querystring");
|
18 | const stream_1 = require("stream");
|
19 | function normalize(optionsOrCallback, cb) {
|
20 | const options = (typeof optionsOrCallback === 'object' ? optionsOrCallback : {});
|
21 | const callback = (typeof optionsOrCallback === 'function' ? optionsOrCallback : cb);
|
22 | return { options, callback };
|
23 | }
|
24 | exports.normalize = normalize;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function objectEntries(obj) {
|
31 | return Object.keys(obj).map(key => [key, obj[key]]);
|
32 | }
|
33 | exports.objectEntries = objectEntries;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function fixedEncodeURIComponent(str) {
|
44 | return encodeURIComponent(str).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase());
|
45 | }
|
46 | exports.fixedEncodeURIComponent = fixedEncodeURIComponent;
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function encodeURI(uri, encodeSlash) {
|
57 |
|
58 |
|
59 | return uri
|
60 | .split('/')
|
61 | .map(fixedEncodeURIComponent)
|
62 | .join(encodeSlash ? '%2F' : '/');
|
63 | }
|
64 | exports.encodeURI = encodeURI;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function qsStringify(qs) {
|
71 | return querystring.stringify(qs, '&', '=', {
|
72 | encodeURIComponent: (component) => encodeURI(component, true),
|
73 | });
|
74 | }
|
75 | exports.qsStringify = qsStringify;
|
76 | 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 | exports.objectKeyToLowercase = objectKeyToLowercase;
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | function unicodeJSONStringify(obj) {
|
92 | return JSON.stringify(obj).replace(/[\u0080-\uFFFF]/g, (char) => '\\u' + ('0000' + char.charCodeAt(0).toString(16)).slice(-4));
|
93 | }
|
94 | exports.unicodeJSONStringify = unicodeJSONStringify;
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | function convertObjKeysToSnakeCase(obj) {
|
101 | if (obj instanceof Date || obj instanceof RegExp) {
|
102 | return obj;
|
103 | }
|
104 | if (Array.isArray(obj)) {
|
105 | return obj.map(convertObjKeysToSnakeCase);
|
106 | }
|
107 | if (obj instanceof Object) {
|
108 | return Object.keys(obj).reduce((acc, cur) => {
|
109 | const s = cur[0].toLocaleLowerCase() +
|
110 | cur.slice(1).replace(/([A-Z]+)/g, (match, p1) => {
|
111 | return `_${p1.toLowerCase()}`;
|
112 | });
|
113 | acc[s] = convertObjKeysToSnakeCase(obj[cur]);
|
114 | return acc;
|
115 | }, Object());
|
116 | }
|
117 | return obj;
|
118 | }
|
119 | exports.convertObjKeysToSnakeCase = convertObjKeysToSnakeCase;
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function formatAsUTCISO(dateTimeToFormat, includeTime = false, dateDelimiter = '', timeDelimiter = '') {
|
129 | const year = dateTimeToFormat.getUTCFullYear();
|
130 | const month = dateTimeToFormat.getUTCMonth() + 1;
|
131 | const day = dateTimeToFormat.getUTCDate();
|
132 | const hour = dateTimeToFormat.getUTCHours();
|
133 | const minute = dateTimeToFormat.getUTCMinutes();
|
134 | const second = dateTimeToFormat.getUTCSeconds();
|
135 | let resultString = `${year.toString().padStart(4, '0')}${dateDelimiter}${month
|
136 | .toString()
|
137 | .padStart(2, '0')}${dateDelimiter}${day.toString().padStart(2, '0')}`;
|
138 | if (includeTime) {
|
139 | resultString = `${resultString}T${hour
|
140 | .toString()
|
141 | .padStart(2, '0')}${timeDelimiter}${minute
|
142 | .toString()
|
143 | .padStart(2, '0')}${timeDelimiter}${second.toString().padStart(2, '0')}Z`;
|
144 | }
|
145 | return resultString;
|
146 | }
|
147 | exports.formatAsUTCISO = formatAsUTCISO;
|
148 | class PassThroughShim extends stream_1.PassThrough {
|
149 | constructor() {
|
150 | super(...arguments);
|
151 | this.shouldEmitReading = true;
|
152 | this.shouldEmitWriting = true;
|
153 | }
|
154 | _read(size) {
|
155 | if (this.shouldEmitReading) {
|
156 | this.emit('reading');
|
157 | this.shouldEmitReading = false;
|
158 | }
|
159 | super._read(size);
|
160 | }
|
161 | _write(chunk, encoding, callback) {
|
162 | if (this.shouldEmitWriting) {
|
163 | this.emit('writing');
|
164 | this.shouldEmitWriting = false;
|
165 | }
|
166 |
|
167 | process.nextTick(() => {
|
168 | super._write(chunk, encoding, callback);
|
169 | });
|
170 | }
|
171 | _final(callback) {
|
172 |
|
173 |
|
174 | if (this.shouldEmitReading) {
|
175 | this.emit('reading');
|
176 | this.shouldEmitReading = false;
|
177 | }
|
178 | if (this.shouldEmitWriting) {
|
179 | this.emit('writing');
|
180 | this.shouldEmitWriting = false;
|
181 | }
|
182 | callback(null);
|
183 | }
|
184 | }
|
185 | exports.PassThroughShim = PassThroughShim;
|
186 |
|
\ | No newline at end of file |