opusdev/vector-similarity-api
1
1'use strict';2 3var undefined;4 5var $Object = require('es-object-atoms');6 7var $Error = require('es-errors');8var $EvalError = require('es-errors/eval');9var $RangeError = require('es-errors/range');10var $ReferenceError = require('es-errors/ref');11var $SyntaxError = require('es-errors/syntax');12var $TypeError = require('es-errors/type');13var $URIError = require('es-errors/uri');14 15var abs = require('math-intrinsics/abs');16var floor = require('math-intrinsics/floor');17var max = require('math-intrinsics/max');18var min = require('math-intrinsics/min');19var pow = require('math-intrinsics/pow');20var round = require('math-intrinsics/round');21var sign = require('math-intrinsics/sign');22 23var $Function = Function;24 25// eslint-disable-next-line consistent-return26var getEvalledConstructor = function (expressionSyntax) {27 try {28 return $Function('"use strict"; return (' + expressionSyntax + ').constructor;')();29 } catch (e) {}30};31 32var $gOPD = require('gopd');33var $defineProperty = require('es-define-property');34 35var throwTypeError = function () {36 throw new $TypeError();37};38var ThrowTypeError = $gOPD39 ? (function () {40 try {41 // eslint-disable-next-line no-unused-expressions, no-caller, no-restricted-properties42 arguments.callee; // IE 8 does not throw here43 return throwTypeError;44 } catch (calleeThrows) {45 try {46 // IE 8 throws on Object.getOwnPropertyDescriptor(arguments, '')47 return $gOPD(arguments, 'callee').get;48 } catch (gOPDthrows) {49 return throwTypeError;50 }51 }52 }())53 : throwTypeError;54 55var hasSymbols = require('has-symbols')();56 57var getProto = require('get-proto');58var $ObjectGPO = require('get-proto/Object.getPrototypeOf');59var $ReflectGPO = require('get-proto/Reflect.getPrototypeOf');60 61var $apply = require('call-bind-apply-helpers/functionApply');62var $call = require('call-bind-apply-helpers/functionCall');63 64var needsEval = {};65 66var TypedArray = typeof Uint8Array === 'undefined' || !getProto ? undefined : getProto(Uint8Array);67 68var INTRINSICS = {69 __proto__: null,70 '%AggregateError%': typeof AggregateError === 'undefined' ? undefined : AggregateError,71 '%Array%': Array,72 '%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,73 '%ArrayIteratorPrototype%': hasSymbols && getProto ? getProto([][Symbol.iterator]()) : undefined,74 '%AsyncFromSyncIteratorPrototype%': undefined,75 '%AsyncFunction%': needsEval,76 '%AsyncGenerator%': needsEval,77 '%AsyncGeneratorFunction%': needsEval,78 '%AsyncIteratorPrototype%': needsEval,79 '%Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,80 '%BigInt%': typeof BigInt === 'undefined' ? undefined : BigInt,81 '%BigInt64Array%': typeof BigInt64Array === 'undefined' ? undefined : BigInt64Array,82 '%BigUint64Array%': typeof BigUint64Array === 'undefined' ? undefined : BigUint64Array,83 '%Boolean%': Boolean,84 '%DataView%': typeof DataView === 'undefined' ? undefined : DataView,85 '%Date%': Date,86 '%decodeURI%': decodeURI,87 '%decodeURIComponent%': decodeURIComponent,88 '%encodeURI%': encodeURI,89 '%encodeURIComponent%': encodeURIComponent,90 '%Error%': $Error,91 '%eval%': eval, // eslint-disable-line no-eval92 '%EvalError%': $EvalError,93 '%Float16Array%': typeof Float16Array === 'undefined' ? undefined : Float16Array,94 '%Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,95 '%Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,96 '%FinalizationRegistry%': typeof FinalizationRegistry === 'undefined' ? undefined : FinalizationRegistry,97 '%Function%': $Function,98 '%GeneratorFunction%': needsEval,99 '%Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,100 '%Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,101 '%Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,102 '%isFinite%': isFinite,103 '%isNaN%': isNaN,104 '%IteratorPrototype%': hasSymbols && getProto ? getProto(getProto([][Symbol.iterator]())) : undefined,105 '%JSON%': typeof JSON === 'object' ? JSON : undefined,106 '%Map%': typeof Map === 'undefined' ? undefined : Map,107 '%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols || !getProto ? undefined : getProto(new Map()[Symbol.iterator]()),108 '%Math%': Math,109 '%Number%': Number,110 '%Object%': $Object,111 '%Object.getOwnPropertyDescriptor%': $gOPD,112 '%parseFloat%': parseFloat,113 '%parseInt%': parseInt,114 '%Promise%': typeof Promise === 'undefined' ? undefined : Promise,115 '%Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,116 '%RangeError%': $RangeError,117 '%ReferenceError%': $ReferenceError,118 '%Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,119 '%RegExp%': RegExp,120 '%Set%': typeof Set === 'undefined' ? undefined : Set,121 '%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols || !getProto ? undefined : getProto(new Set()[Symbol.iterator]()),122 '%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,123 '%String%': String,124 '%StringIteratorPrototype%': hasSymbols && getProto ? getProto(''[Symbol.iterator]()) : undefined,125 '%Symbol%': hasSymbols ? Symbol : undefined,126 '%SyntaxError%': $SyntaxError,127 '%ThrowTypeError%': ThrowTypeError,128 '%TypedArray%': TypedArray,129 '%TypeError%': $TypeError,130 '%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,131 '%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,132 '%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,133 '%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,134 '%URIError%': $URIError,135 '%WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,136 '%WeakRef%': typeof WeakRef === 'undefined' ? undefined : WeakRef,137 '%WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet,138 139 '%Function.prototype.call%': $call,140 '%Function.prototype.apply%': $apply,141 '%Object.defineProperty%': $defineProperty,142 '%Object.getPrototypeOf%': $ObjectGPO,143 '%Math.abs%': abs,144 '%Math.floor%': floor,145 '%Math.max%': max,146 '%Math.min%': min,147 '%Math.pow%': pow,148 '%Math.round%': round,149 '%Math.sign%': sign,150 '%Reflect.getPrototypeOf%': $ReflectGPO151};152 153if (getProto) {154 try {155 null.error; // eslint-disable-line no-unused-expressions156 } catch (e) {157 // https://github.com/tc39/proposal-shadowrealm/pull/384#issuecomment-1364264229158 var errorProto = getProto(getProto(e));159 INTRINSICS['%Error.prototype%'] = errorProto;160 }161}162 163var doEval = function doEval(name) {164 var value;165 if (name === '%AsyncFunction%') {166 value = getEvalledConstructor('async function () {}');167 } else if (name === '%GeneratorFunction%') {168 value = getEvalledConstructor('function* () {}');169 } else if (name === '%AsyncGeneratorFunction%') {170 value = getEvalledConstructor('async function* () {}');171 } else if (name === '%AsyncGenerator%') {172 var fn = doEval('%AsyncGeneratorFunction%');173 if (fn) {174 value = fn.prototype;175 }176 } else if (name === '%AsyncIteratorPrototype%') {177 var gen = doEval('%AsyncGenerator%');178 if (gen && getProto) {179 value = getProto(gen.prototype);180 }181 }182 183 INTRINSICS[name] = value;184 185 return value;186};187 188var LEGACY_ALIASES = {189 __proto__: null,190 '%ArrayBufferPrototype%': ['ArrayBuffer', 'prototype'],191 '%ArrayPrototype%': ['Array', 'prototype'],192 '%ArrayProto_entries%': ['Array', 'prototype', 'entries'],193 '%ArrayProto_forEach%': ['Array', 'prototype', 'forEach'],194 '%ArrayProto_keys%': ['Array', 'prototype', 'keys'],195 '%ArrayProto_values%': ['Array', 'prototype', 'values'],196 '%AsyncFunctionPrototype%': ['AsyncFunction', 'prototype'],197 '%AsyncGenerator%': ['AsyncGeneratorFunction', 'prototype'],198 '%AsyncGeneratorPrototype%': ['AsyncGeneratorFunction', 'prototype', 'prototype'],199 '%BooleanPrototype%': ['Boolean', 'prototype'],200 '%DataViewPrototype%': ['DataView', 'prototype'],201 '%DatePrototype%': ['Date', 'prototype'],202 '%ErrorPrototype%': ['Error', 'prototype'],203 '%EvalErrorPrototype%': ['EvalError', 'prototype'],204 '%Float32ArrayPrototype%': ['Float32Array', 'prototype'],205 '%Float64ArrayPrototype%': ['Float64Array', 'prototype'],206 '%FunctionPrototype%': ['Function', 'prototype'],207 '%Generator%': ['GeneratorFunction', 'prototype'],208 '%GeneratorPrototype%': ['GeneratorFunction', 'prototype', 'prototype'],209 '%Int8ArrayPrototype%': ['Int8Array', 'prototype'],210 '%Int16ArrayPrototype%': ['Int16Array', 'prototype'],211 '%Int32ArrayPrototype%': ['Int32Array', 'prototype'],212 '%JSONParse%': ['JSON', 'parse'],213 '%JSONStringify%': ['JSON', 'stringify'],214 '%MapPrototype%': ['Map', 'prototype'],215 '%NumberPrototype%': ['Number', 'prototype'],216 '%ObjectPrototype%': ['Object', 'prototype'],217 '%ObjProto_toString%': ['Object', 'prototype', 'toString'],218 '%ObjProto_valueOf%': ['Object', 'prototype', 'valueOf'],219 '%PromisePrototype%': ['Promise', 'prototype'],220 '%PromiseProto_then%': ['Promise', 'prototype', 'then'],221 '%Promise_all%': ['Promise', 'all'],222 '%Promise_reject%': ['Promise', 'reject'],223 '%Promise_resolve%': ['Promise', 'resolve'],224 '%RangeErrorPrototype%': ['RangeError', 'prototype'],225 '%ReferenceErrorPrototype%': ['ReferenceError', 'prototype'],226 '%RegExpPrototype%': ['RegExp', 'prototype'],227 '%SetPrototype%': ['Set', 'prototype'],228 '%SharedArrayBufferPrototype%': ['SharedArrayBuffer', 'prototype'],229 '%StringPrototype%': ['String', 'prototype'],230 '%SymbolPrototype%': ['Symbol', 'prototype'],231 '%SyntaxErrorPrototype%': ['SyntaxError', 'prototype'],232 '%TypedArrayPrototype%': ['TypedArray', 'prototype'],233 '%TypeErrorPrototype%': ['TypeError', 'prototype'],234 '%Uint8ArrayPrototype%': ['Uint8Array', 'prototype'],235 '%Uint8ClampedArrayPrototype%': ['Uint8ClampedArray', 'prototype'],236 '%Uint16ArrayPrototype%': ['Uint16Array', 'prototype'],237 '%Uint32ArrayPrototype%': ['Uint32Array', 'prototype'],238 '%URIErrorPrototype%': ['URIError', 'prototype'],239 '%WeakMapPrototype%': ['WeakMap', 'prototype'],240 '%WeakSetPrototype%': ['WeakSet', 'prototype']241};242 243var bind = require('function-bind');244var hasOwn = require('hasown');245var $concat = bind.call($call, Array.prototype.concat);246var $spliceApply = bind.call($apply, Array.prototype.splice);247var $replace = bind.call($call, String.prototype.replace);248var $strSlice = bind.call($call, String.prototype.slice);249var $exec = bind.call($call, RegExp.prototype.exec);250 251/* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */252var rePropName = /[^%.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|%$))/g;253var reEscapeChar = /\\(\\)?/g; /** Used to match backslashes in property paths. */254var stringToPath = function stringToPath(string) {255 var first = $strSlice(string, 0, 1);256 var last = $strSlice(string, -1);257 if (first === '%' && last !== '%') {258 throw new $SyntaxError('invalid intrinsic syntax, expected closing `%`');259 } else if (last === '%' && first !== '%') {260 throw new $SyntaxError('invalid intrinsic syntax, expected opening `%`');261 }262 var result = [];263 $replace(string, rePropName, function (match, number, quote, subString) {264 result[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match;265 });266 return result;267};268/* end adaptation */269 270var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {271 var intrinsicName = name;272 var alias;273 if (hasOwn(LEGACY_ALIASES, intrinsicName)) {274 alias = LEGACY_ALIASES[intrinsicName];275 intrinsicName = '%' + alias[0] + '%';276 }277 278 if (hasOwn(INTRINSICS, intrinsicName)) {279 var value = INTRINSICS[intrinsicName];280 if (value === needsEval) {281 value = doEval(intrinsicName);282 }283 if (typeof value === 'undefined' && !allowMissing) {284 throw new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');285 }286 287 return {288 alias: alias,289 name: intrinsicName,290 value: value291 };292 }293 294 throw new $SyntaxError('intrinsic ' + name + ' does not exist!');295};296 297module.exports = function GetIntrinsic(name, allowMissing) {298 if (typeof name !== 'string' || name.length === 0) {299 throw new $TypeError('intrinsic name must be a non-empty string');300 }301 if (arguments.length > 1 && typeof allowMissing !== 'boolean') {302 throw new $TypeError('"allowMissing" argument must be a boolean');303 }304 305 if ($exec(/^%?[^%]*%?$/, name) === null) {306 throw new $SyntaxError('`%` may not be present anywhere but at the beginning and end of the intrinsic name');307 }308 var parts = stringToPath(name);309 var intrinsicBaseName = parts.length > 0 ? parts[0] : '';310 311 var intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing);312 var intrinsicRealName = intrinsic.name;313 var value = intrinsic.value;314 var skipFurtherCaching = false;315 316 var alias = intrinsic.alias;317 if (alias) {318 intrinsicBaseName = alias[0];319 $spliceApply(parts, $concat([0, 1], alias));320 }321 322 for (var i = 1, isOwn = true; i < parts.length; i += 1) {323 var part = parts[i];324 var first = $strSlice(part, 0, 1);325 var last = $strSlice(part, -1);326 if (327 (328 (first === '"' || first === "'" || first === '`')329 || (last === '"' || last === "'" || last === '`')330 )331 && first !== last332 ) {333 throw new $SyntaxError('property names with quotes must have matching quotes');334 }335 if (part === 'constructor' || !isOwn) {336 skipFurtherCaching = true;337 }338 339 intrinsicBaseName += '.' + part;340 intrinsicRealName = '%' + intrinsicBaseName + '%';341 342 if (hasOwn(INTRINSICS, intrinsicRealName)) {343 value = INTRINSICS[intrinsicRealName];344 } else if (value != null) {345 if (!(part in value)) {346 if (!allowMissing) {347 throw new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.');348 }349 return void undefined;350 }351 if ($gOPD && (i + 1) >= parts.length) {352 var desc = $gOPD(value, part);353 isOwn = !!desc;354 355 // By convention, when a data property is converted to an accessor356 // property to emulate a data property that does not suffer from357 // the override mistake, that accessor's getter is marked with358 // an `originalValue` property. Here, when we detect this, we359 // uphold the illusion by pretending to see that original data360 // property, i.e., returning the value rather than the getter361 // itself.362 if (isOwn && 'get' in desc && !('originalValue' in desc.get)) {363 value = desc.get;364 } else {365 value = value[part];366 }367 } else {368 isOwn = hasOwn(value, part);369 value = value[part];370 }371 372 if (isOwn && !skipFurtherCaching) {373 INTRINSICS[intrinsicRealName] = value;374 }375 }376 }377 return value;378};379 