初始化仓库
This commit is contained in:
13
node_modules/openai/src/internal/qs/LICENSE.md
generated
vendored
Normal file
13
node_modules/openai/src/internal/qs/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
BSD 3-Clause License
|
||||
|
||||
Copyright (c) 2014, Nathan LaFreniere and other [contributors](https://github.com/puruvj/neoqs/graphs/contributors) All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
3
node_modules/openai/src/internal/qs/README.md
generated
vendored
Normal file
3
node_modules/openai/src/internal/qs/README.md
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# qs
|
||||
|
||||
This is a vendored version of [neoqs](https://github.com/PuruVJ/neoqs) which is a TypeScript rewrite of [qs](https://github.com/ljharb/qs), a query string library.
|
||||
10
node_modules/openai/src/internal/qs/formats.ts
generated
vendored
Normal file
10
node_modules/openai/src/internal/qs/formats.ts
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Format } from './types';
|
||||
|
||||
export const default_format: Format = 'RFC3986';
|
||||
export const default_formatter = (v: PropertyKey) => String(v);
|
||||
export const formatters: Record<Format, (str: PropertyKey) => string> = {
|
||||
RFC1738: (v: PropertyKey) => String(v).replace(/%20/g, '+'),
|
||||
RFC3986: default_formatter,
|
||||
};
|
||||
export const RFC1738 = 'RFC1738';
|
||||
export const RFC3986 = 'RFC3986';
|
||||
13
node_modules/openai/src/internal/qs/index.ts
generated
vendored
Normal file
13
node_modules/openai/src/internal/qs/index.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import { default_format, formatters, RFC1738, RFC3986 } from './formats';
|
||||
|
||||
const formats = {
|
||||
formatters,
|
||||
RFC1738,
|
||||
RFC3986,
|
||||
default: default_format,
|
||||
};
|
||||
|
||||
export { stringify } from './stringify';
|
||||
export { formats };
|
||||
|
||||
export type { DefaultDecoder, DefaultEncoder, Format, ParseOptions, StringifyOptions } from './types';
|
||||
385
node_modules/openai/src/internal/qs/stringify.ts
generated
vendored
Normal file
385
node_modules/openai/src/internal/qs/stringify.ts
generated
vendored
Normal file
@@ -0,0 +1,385 @@
|
||||
import { encode, is_buffer, maybe_map, has } from './utils';
|
||||
import { default_format, default_formatter, formatters } from './formats';
|
||||
import type { NonNullableProperties, StringifyOptions } from './types';
|
||||
import { isArray } from '../utils/values';
|
||||
|
||||
const array_prefix_generators = {
|
||||
brackets(prefix: PropertyKey) {
|
||||
return String(prefix) + '[]';
|
||||
},
|
||||
comma: 'comma',
|
||||
indices(prefix: PropertyKey, key: string) {
|
||||
return String(prefix) + '[' + key + ']';
|
||||
},
|
||||
repeat(prefix: PropertyKey) {
|
||||
return String(prefix);
|
||||
},
|
||||
};
|
||||
|
||||
const push_to_array = function (arr: any[], value_or_array: any) {
|
||||
Array.prototype.push.apply(arr, isArray(value_or_array) ? value_or_array : [value_or_array]);
|
||||
};
|
||||
|
||||
let toISOString;
|
||||
|
||||
const defaults = {
|
||||
addQueryPrefix: false,
|
||||
allowDots: false,
|
||||
allowEmptyArrays: false,
|
||||
arrayFormat: 'indices',
|
||||
charset: 'utf-8',
|
||||
charsetSentinel: false,
|
||||
delimiter: '&',
|
||||
encode: true,
|
||||
encodeDotInKeys: false,
|
||||
encoder: encode,
|
||||
encodeValuesOnly: false,
|
||||
format: default_format,
|
||||
formatter: default_formatter,
|
||||
/** @deprecated */
|
||||
indices: false,
|
||||
serializeDate(date) {
|
||||
return (toISOString ??= Function.prototype.call.bind(Date.prototype.toISOString))(date);
|
||||
},
|
||||
skipNulls: false,
|
||||
strictNullHandling: false,
|
||||
} as NonNullableProperties<StringifyOptions & { formatter: (typeof formatters)['RFC1738'] }>;
|
||||
|
||||
function is_non_nullish_primitive(v: unknown): v is string | number | boolean | symbol | bigint {
|
||||
return (
|
||||
typeof v === 'string' ||
|
||||
typeof v === 'number' ||
|
||||
typeof v === 'boolean' ||
|
||||
typeof v === 'symbol' ||
|
||||
typeof v === 'bigint'
|
||||
);
|
||||
}
|
||||
|
||||
const sentinel = {};
|
||||
|
||||
function inner_stringify(
|
||||
object: any,
|
||||
prefix: PropertyKey,
|
||||
generateArrayPrefix: StringifyOptions['arrayFormat'] | ((prefix: string, key: string) => string),
|
||||
commaRoundTrip: boolean,
|
||||
allowEmptyArrays: boolean,
|
||||
strictNullHandling: boolean,
|
||||
skipNulls: boolean,
|
||||
encodeDotInKeys: boolean,
|
||||
encoder: StringifyOptions['encoder'],
|
||||
filter: StringifyOptions['filter'],
|
||||
sort: StringifyOptions['sort'],
|
||||
allowDots: StringifyOptions['allowDots'],
|
||||
serializeDate: StringifyOptions['serializeDate'],
|
||||
format: StringifyOptions['format'],
|
||||
formatter: StringifyOptions['formatter'],
|
||||
encodeValuesOnly: boolean,
|
||||
charset: StringifyOptions['charset'],
|
||||
sideChannel: WeakMap<any, any>,
|
||||
) {
|
||||
let obj = object;
|
||||
|
||||
let tmp_sc = sideChannel;
|
||||
let step = 0;
|
||||
let find_flag = false;
|
||||
while ((tmp_sc = tmp_sc.get(sentinel)) !== void undefined && !find_flag) {
|
||||
// Where object last appeared in the ref tree
|
||||
const pos = tmp_sc.get(object);
|
||||
step += 1;
|
||||
if (typeof pos !== 'undefined') {
|
||||
if (pos === step) {
|
||||
throw new RangeError('Cyclic object value');
|
||||
} else {
|
||||
find_flag = true; // Break while
|
||||
}
|
||||
}
|
||||
if (typeof tmp_sc.get(sentinel) === 'undefined') {
|
||||
step = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof filter === 'function') {
|
||||
obj = filter(prefix, obj);
|
||||
} else if (obj instanceof Date) {
|
||||
obj = serializeDate?.(obj);
|
||||
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
||||
obj = maybe_map(obj, function (value) {
|
||||
if (value instanceof Date) {
|
||||
return serializeDate?.(value);
|
||||
}
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
if (obj === null) {
|
||||
if (strictNullHandling) {
|
||||
return encoder && !encodeValuesOnly ?
|
||||
// @ts-expect-error
|
||||
encoder(prefix, defaults.encoder, charset, 'key', format)
|
||||
: prefix;
|
||||
}
|
||||
|
||||
obj = '';
|
||||
}
|
||||
|
||||
if (is_non_nullish_primitive(obj) || is_buffer(obj)) {
|
||||
if (encoder) {
|
||||
const key_value =
|
||||
encodeValuesOnly ? prefix
|
||||
// @ts-expect-error
|
||||
: encoder(prefix, defaults.encoder, charset, 'key', format);
|
||||
return [
|
||||
formatter?.(key_value) +
|
||||
'=' +
|
||||
// @ts-expect-error
|
||||
formatter?.(encoder(obj, defaults.encoder, charset, 'value', format)),
|
||||
];
|
||||
}
|
||||
return [formatter?.(prefix) + '=' + formatter?.(String(obj))];
|
||||
}
|
||||
|
||||
const values: string[] = [];
|
||||
|
||||
if (typeof obj === 'undefined') {
|
||||
return values;
|
||||
}
|
||||
|
||||
let obj_keys;
|
||||
if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
||||
// we need to join elements in
|
||||
if (encodeValuesOnly && encoder) {
|
||||
// @ts-expect-error values only
|
||||
obj = maybe_map(obj, encoder);
|
||||
}
|
||||
obj_keys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
|
||||
} else if (isArray(filter)) {
|
||||
obj_keys = filter;
|
||||
} else {
|
||||
const keys = Object.keys(obj);
|
||||
obj_keys = sort ? keys.sort(sort) : keys;
|
||||
}
|
||||
|
||||
const encoded_prefix = encodeDotInKeys ? String(prefix).replace(/\./g, '%2E') : String(prefix);
|
||||
|
||||
const adjusted_prefix =
|
||||
commaRoundTrip && isArray(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;
|
||||
|
||||
if (allowEmptyArrays && isArray(obj) && obj.length === 0) {
|
||||
return adjusted_prefix + '[]';
|
||||
}
|
||||
|
||||
for (let j = 0; j < obj_keys.length; ++j) {
|
||||
const key = obj_keys[j];
|
||||
const value =
|
||||
// @ts-ignore
|
||||
typeof key === 'object' && typeof key.value !== 'undefined' ? key.value : obj[key as any];
|
||||
|
||||
if (skipNulls && value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const encoded_key = allowDots && encodeDotInKeys ? (key as any).replace(/\./g, '%2E') : key;
|
||||
const key_prefix =
|
||||
isArray(obj) ?
|
||||
typeof generateArrayPrefix === 'function' ?
|
||||
generateArrayPrefix(adjusted_prefix, encoded_key)
|
||||
: adjusted_prefix
|
||||
: adjusted_prefix + (allowDots ? '.' + encoded_key : '[' + encoded_key + ']');
|
||||
|
||||
sideChannel.set(object, step);
|
||||
const valueSideChannel = new WeakMap();
|
||||
valueSideChannel.set(sentinel, sideChannel);
|
||||
push_to_array(
|
||||
values,
|
||||
inner_stringify(
|
||||
value,
|
||||
key_prefix,
|
||||
generateArrayPrefix,
|
||||
commaRoundTrip,
|
||||
allowEmptyArrays,
|
||||
strictNullHandling,
|
||||
skipNulls,
|
||||
encodeDotInKeys,
|
||||
// @ts-ignore
|
||||
generateArrayPrefix === 'comma' && encodeValuesOnly && isArray(obj) ? null : encoder,
|
||||
filter,
|
||||
sort,
|
||||
allowDots,
|
||||
serializeDate,
|
||||
format,
|
||||
formatter,
|
||||
encodeValuesOnly,
|
||||
charset,
|
||||
valueSideChannel,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
function normalize_stringify_options(
|
||||
opts: StringifyOptions = defaults,
|
||||
): NonNullableProperties<Omit<StringifyOptions, 'indices'>> & { indices?: boolean } {
|
||||
if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
|
||||
throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
|
||||
}
|
||||
|
||||
if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {
|
||||
throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');
|
||||
}
|
||||
|
||||
if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
|
||||
throw new TypeError('Encoder has to be a function.');
|
||||
}
|
||||
|
||||
const charset = opts.charset || defaults.charset;
|
||||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
||||
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
|
||||
}
|
||||
|
||||
let format = default_format;
|
||||
if (typeof opts.format !== 'undefined') {
|
||||
if (!has(formatters, opts.format)) {
|
||||
throw new TypeError('Unknown format option provided.');
|
||||
}
|
||||
format = opts.format;
|
||||
}
|
||||
const formatter = formatters[format];
|
||||
|
||||
let filter = defaults.filter;
|
||||
if (typeof opts.filter === 'function' || isArray(opts.filter)) {
|
||||
filter = opts.filter;
|
||||
}
|
||||
|
||||
let arrayFormat: StringifyOptions['arrayFormat'];
|
||||
if (opts.arrayFormat && opts.arrayFormat in array_prefix_generators) {
|
||||
arrayFormat = opts.arrayFormat;
|
||||
} else if ('indices' in opts) {
|
||||
arrayFormat = opts.indices ? 'indices' : 'repeat';
|
||||
} else {
|
||||
arrayFormat = defaults.arrayFormat;
|
||||
}
|
||||
|
||||
if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
|
||||
throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
|
||||
}
|
||||
|
||||
const allowDots =
|
||||
typeof opts.allowDots === 'undefined' ?
|
||||
!!opts.encodeDotInKeys === true ?
|
||||
true
|
||||
: defaults.allowDots
|
||||
: !!opts.allowDots;
|
||||
|
||||
return {
|
||||
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
|
||||
// @ts-ignore
|
||||
allowDots: allowDots,
|
||||
allowEmptyArrays:
|
||||
typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
|
||||
arrayFormat: arrayFormat,
|
||||
charset: charset,
|
||||
charsetSentinel:
|
||||
typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
||||
commaRoundTrip: !!opts.commaRoundTrip,
|
||||
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
|
||||
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
|
||||
encodeDotInKeys:
|
||||
typeof opts.encodeDotInKeys === 'boolean' ? opts.encodeDotInKeys : defaults.encodeDotInKeys,
|
||||
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
|
||||
encodeValuesOnly:
|
||||
typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
|
||||
filter: filter,
|
||||
format: format,
|
||||
formatter: formatter,
|
||||
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
|
||||
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
|
||||
// @ts-ignore
|
||||
sort: typeof opts.sort === 'function' ? opts.sort : null,
|
||||
strictNullHandling:
|
||||
typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
|
||||
};
|
||||
}
|
||||
|
||||
export function stringify(object: any, opts: StringifyOptions = {}) {
|
||||
let obj = object;
|
||||
const options = normalize_stringify_options(opts);
|
||||
|
||||
let obj_keys: PropertyKey[] | undefined;
|
||||
let filter;
|
||||
|
||||
if (typeof options.filter === 'function') {
|
||||
filter = options.filter;
|
||||
obj = filter('', obj);
|
||||
} else if (isArray(options.filter)) {
|
||||
filter = options.filter;
|
||||
obj_keys = filter;
|
||||
}
|
||||
|
||||
const keys: string[] = [];
|
||||
|
||||
if (typeof obj !== 'object' || obj === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const generateArrayPrefix = array_prefix_generators[options.arrayFormat];
|
||||
const commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;
|
||||
|
||||
if (!obj_keys) {
|
||||
obj_keys = Object.keys(obj);
|
||||
}
|
||||
|
||||
if (options.sort) {
|
||||
obj_keys.sort(options.sort);
|
||||
}
|
||||
|
||||
const sideChannel = new WeakMap();
|
||||
for (let i = 0; i < obj_keys.length; ++i) {
|
||||
const key = obj_keys[i]!;
|
||||
|
||||
if (options.skipNulls && obj[key] === null) {
|
||||
continue;
|
||||
}
|
||||
push_to_array(
|
||||
keys,
|
||||
inner_stringify(
|
||||
obj[key],
|
||||
key,
|
||||
// @ts-expect-error
|
||||
generateArrayPrefix,
|
||||
commaRoundTrip,
|
||||
options.allowEmptyArrays,
|
||||
options.strictNullHandling,
|
||||
options.skipNulls,
|
||||
options.encodeDotInKeys,
|
||||
options.encode ? options.encoder : null,
|
||||
options.filter,
|
||||
options.sort,
|
||||
options.allowDots,
|
||||
options.serializeDate,
|
||||
options.format,
|
||||
options.formatter,
|
||||
options.encodeValuesOnly,
|
||||
options.charset,
|
||||
sideChannel,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const joined = keys.join(options.delimiter);
|
||||
let prefix = options.addQueryPrefix === true ? '?' : '';
|
||||
|
||||
if (options.charsetSentinel) {
|
||||
if (options.charset === 'iso-8859-1') {
|
||||
// encodeURIComponent('✓'), the "numeric entity" representation of a checkmark
|
||||
prefix += 'utf8=%26%2310003%3B&';
|
||||
} else {
|
||||
// encodeURIComponent('✓')
|
||||
prefix += 'utf8=%E2%9C%93&';
|
||||
}
|
||||
}
|
||||
|
||||
return joined.length > 0 ? prefix + joined : '';
|
||||
}
|
||||
71
node_modules/openai/src/internal/qs/types.ts
generated
vendored
Normal file
71
node_modules/openai/src/internal/qs/types.ts
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
export type Format = 'RFC1738' | 'RFC3986';
|
||||
|
||||
export type DefaultEncoder = (str: any, defaultEncoder?: any, charset?: string) => string;
|
||||
export type DefaultDecoder = (str: string, decoder?: any, charset?: string) => string;
|
||||
|
||||
export type BooleanOptional = boolean | undefined;
|
||||
|
||||
export type StringifyBaseOptions = {
|
||||
delimiter?: string;
|
||||
allowDots?: boolean;
|
||||
encodeDotInKeys?: boolean;
|
||||
strictNullHandling?: boolean;
|
||||
skipNulls?: boolean;
|
||||
encode?: boolean;
|
||||
encoder?: (
|
||||
str: any,
|
||||
defaultEncoder: DefaultEncoder,
|
||||
charset: string,
|
||||
type: 'key' | 'value',
|
||||
format?: Format,
|
||||
) => string;
|
||||
filter?: Array<PropertyKey> | ((prefix: PropertyKey, value: any) => any);
|
||||
arrayFormat?: 'indices' | 'brackets' | 'repeat' | 'comma';
|
||||
indices?: boolean;
|
||||
sort?: ((a: PropertyKey, b: PropertyKey) => number) | null;
|
||||
serializeDate?: (d: Date) => string;
|
||||
format?: 'RFC1738' | 'RFC3986';
|
||||
formatter?: (str: PropertyKey) => string;
|
||||
encodeValuesOnly?: boolean;
|
||||
addQueryPrefix?: boolean;
|
||||
charset?: 'utf-8' | 'iso-8859-1';
|
||||
charsetSentinel?: boolean;
|
||||
allowEmptyArrays?: boolean;
|
||||
commaRoundTrip?: boolean;
|
||||
};
|
||||
|
||||
export type StringifyOptions = StringifyBaseOptions;
|
||||
|
||||
export type ParseBaseOptions = {
|
||||
comma?: boolean;
|
||||
delimiter?: string | RegExp;
|
||||
depth?: number | false;
|
||||
decoder?: (str: string, defaultDecoder: DefaultDecoder, charset: string, type: 'key' | 'value') => any;
|
||||
arrayLimit?: number;
|
||||
parseArrays?: boolean;
|
||||
plainObjects?: boolean;
|
||||
allowPrototypes?: boolean;
|
||||
allowSparse?: boolean;
|
||||
parameterLimit?: number;
|
||||
strictDepth?: boolean;
|
||||
strictNullHandling?: boolean;
|
||||
ignoreQueryPrefix?: boolean;
|
||||
charset?: 'utf-8' | 'iso-8859-1';
|
||||
charsetSentinel?: boolean;
|
||||
interpretNumericEntities?: boolean;
|
||||
allowEmptyArrays?: boolean;
|
||||
duplicates?: 'combine' | 'first' | 'last';
|
||||
allowDots?: boolean;
|
||||
decodeDotInKeys?: boolean;
|
||||
};
|
||||
|
||||
export type ParseOptions = ParseBaseOptions;
|
||||
|
||||
export type ParsedQs = {
|
||||
[key: string]: undefined | string | string[] | ParsedQs | ParsedQs[];
|
||||
};
|
||||
|
||||
// Type to remove null or undefined union from each property
|
||||
export type NonNullableProperties<T> = {
|
||||
[K in keyof T]-?: Exclude<T[K], undefined | null>;
|
||||
};
|
||||
265
node_modules/openai/src/internal/qs/utils.ts
generated
vendored
Normal file
265
node_modules/openai/src/internal/qs/utils.ts
generated
vendored
Normal file
@@ -0,0 +1,265 @@
|
||||
import { RFC1738 } from './formats';
|
||||
import type { DefaultEncoder, Format } from './types';
|
||||
import { isArray } from '../utils/values';
|
||||
|
||||
export let has = (obj: object, key: PropertyKey): boolean => (
|
||||
(has = (Object as any).hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty)),
|
||||
has(obj, key)
|
||||
);
|
||||
|
||||
const hex_table = /* @__PURE__ */ (() => {
|
||||
const array = [];
|
||||
for (let i = 0; i < 256; ++i) {
|
||||
array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
|
||||
}
|
||||
|
||||
return array;
|
||||
})();
|
||||
|
||||
function compact_queue<T extends Record<string, any>>(queue: Array<{ obj: T; prop: string }>) {
|
||||
while (queue.length > 1) {
|
||||
const item = queue.pop();
|
||||
if (!item) continue;
|
||||
|
||||
const obj = item.obj[item.prop];
|
||||
|
||||
if (isArray(obj)) {
|
||||
const compacted: unknown[] = [];
|
||||
|
||||
for (let j = 0; j < obj.length; ++j) {
|
||||
if (typeof obj[j] !== 'undefined') {
|
||||
compacted.push(obj[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
item.obj[item.prop] = compacted;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function array_to_object(source: any[], options: { plainObjects: boolean }) {
|
||||
const obj = options && options.plainObjects ? Object.create(null) : {};
|
||||
for (let i = 0; i < source.length; ++i) {
|
||||
if (typeof source[i] !== 'undefined') {
|
||||
obj[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
export function merge(
|
||||
target: any,
|
||||
source: any,
|
||||
options: { plainObjects?: boolean; allowPrototypes?: boolean } = {},
|
||||
) {
|
||||
if (!source) {
|
||||
return target;
|
||||
}
|
||||
|
||||
if (typeof source !== 'object') {
|
||||
if (isArray(target)) {
|
||||
target.push(source);
|
||||
} else if (target && typeof target === 'object') {
|
||||
if ((options && (options.plainObjects || options.allowPrototypes)) || !has(Object.prototype, source)) {
|
||||
target[source] = true;
|
||||
}
|
||||
} else {
|
||||
return [target, source];
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
if (!target || typeof target !== 'object') {
|
||||
return [target].concat(source);
|
||||
}
|
||||
|
||||
let mergeTarget = target;
|
||||
if (isArray(target) && !isArray(source)) {
|
||||
// @ts-ignore
|
||||
mergeTarget = array_to_object(target, options);
|
||||
}
|
||||
|
||||
if (isArray(target) && isArray(source)) {
|
||||
source.forEach(function (item, i) {
|
||||
if (has(target, i)) {
|
||||
const targetItem = target[i];
|
||||
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
|
||||
target[i] = merge(targetItem, item, options);
|
||||
} else {
|
||||
target.push(item);
|
||||
}
|
||||
} else {
|
||||
target[i] = item;
|
||||
}
|
||||
});
|
||||
return target;
|
||||
}
|
||||
|
||||
return Object.keys(source).reduce(function (acc, key) {
|
||||
const value = source[key];
|
||||
|
||||
if (has(acc, key)) {
|
||||
acc[key] = merge(acc[key], value, options);
|
||||
} else {
|
||||
acc[key] = value;
|
||||
}
|
||||
return acc;
|
||||
}, mergeTarget);
|
||||
}
|
||||
|
||||
export function assign_single_source(target: any, source: any) {
|
||||
return Object.keys(source).reduce(function (acc, key) {
|
||||
acc[key] = source[key];
|
||||
return acc;
|
||||
}, target);
|
||||
}
|
||||
|
||||
export function decode(str: string, _: any, charset: string) {
|
||||
const strWithoutPlus = str.replace(/\+/g, ' ');
|
||||
if (charset === 'iso-8859-1') {
|
||||
// unescape never throws, no try...catch needed:
|
||||
return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
|
||||
}
|
||||
// utf-8
|
||||
try {
|
||||
return decodeURIComponent(strWithoutPlus);
|
||||
} catch (e) {
|
||||
return strWithoutPlus;
|
||||
}
|
||||
}
|
||||
|
||||
const limit = 1024;
|
||||
|
||||
export const encode: (
|
||||
str: any,
|
||||
defaultEncoder: DefaultEncoder,
|
||||
charset: string,
|
||||
type: 'key' | 'value',
|
||||
format: Format,
|
||||
) => string = (str, _defaultEncoder, charset, _kind, format: Format) => {
|
||||
// This code was originally written by Brian White for the io.js core querystring library.
|
||||
// It has been adapted here for stricter adherence to RFC 3986
|
||||
if (str.length === 0) {
|
||||
return str;
|
||||
}
|
||||
|
||||
let string = str;
|
||||
if (typeof str === 'symbol') {
|
||||
string = Symbol.prototype.toString.call(str);
|
||||
} else if (typeof str !== 'string') {
|
||||
string = String(str);
|
||||
}
|
||||
|
||||
if (charset === 'iso-8859-1') {
|
||||
return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
|
||||
return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
|
||||
});
|
||||
}
|
||||
|
||||
let out = '';
|
||||
for (let j = 0; j < string.length; j += limit) {
|
||||
const segment = string.length >= limit ? string.slice(j, j + limit) : string;
|
||||
const arr = [];
|
||||
|
||||
for (let i = 0; i < segment.length; ++i) {
|
||||
let c = segment.charCodeAt(i);
|
||||
if (
|
||||
c === 0x2d || // -
|
||||
c === 0x2e || // .
|
||||
c === 0x5f || // _
|
||||
c === 0x7e || // ~
|
||||
(c >= 0x30 && c <= 0x39) || // 0-9
|
||||
(c >= 0x41 && c <= 0x5a) || // a-z
|
||||
(c >= 0x61 && c <= 0x7a) || // A-Z
|
||||
(format === RFC1738 && (c === 0x28 || c === 0x29)) // ( )
|
||||
) {
|
||||
arr[arr.length] = segment.charAt(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c < 0x80) {
|
||||
arr[arr.length] = hex_table[c];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c < 0x800) {
|
||||
arr[arr.length] = hex_table[0xc0 | (c >> 6)]! + hex_table[0x80 | (c & 0x3f)];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c < 0xd800 || c >= 0xe000) {
|
||||
arr[arr.length] =
|
||||
hex_table[0xe0 | (c >> 12)]! + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
|
||||
continue;
|
||||
}
|
||||
|
||||
i += 1;
|
||||
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
|
||||
|
||||
arr[arr.length] =
|
||||
hex_table[0xf0 | (c >> 18)]! +
|
||||
hex_table[0x80 | ((c >> 12) & 0x3f)] +
|
||||
hex_table[0x80 | ((c >> 6) & 0x3f)] +
|
||||
hex_table[0x80 | (c & 0x3f)];
|
||||
}
|
||||
|
||||
out += arr.join('');
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
export function compact(value: any) {
|
||||
const queue = [{ obj: { o: value }, prop: 'o' }];
|
||||
const refs = [];
|
||||
|
||||
for (let i = 0; i < queue.length; ++i) {
|
||||
const item = queue[i];
|
||||
// @ts-ignore
|
||||
const obj = item.obj[item.prop];
|
||||
|
||||
const keys = Object.keys(obj);
|
||||
for (let j = 0; j < keys.length; ++j) {
|
||||
const key = keys[j]!;
|
||||
const val = obj[key];
|
||||
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
|
||||
queue.push({ obj: obj, prop: key });
|
||||
refs.push(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
compact_queue(queue);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export function is_regexp(obj: any) {
|
||||
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
||||
}
|
||||
|
||||
export function is_buffer(obj: any) {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
|
||||
}
|
||||
|
||||
export function combine(a: any, b: any) {
|
||||
return [].concat(a, b);
|
||||
}
|
||||
|
||||
export function maybe_map<T>(val: T[], fn: (v: T) => T) {
|
||||
if (isArray(val)) {
|
||||
const mapped = [];
|
||||
for (let i = 0; i < val.length; i += 1) {
|
||||
mapped.push(fn(val[i]!));
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
return fn(val);
|
||||
}
|
||||
Reference in New Issue
Block a user