@oramacloud/client
Advanced tools
Comparing version 1.0.0-beta.7 to 1.0.0-beta.8
@@ -1,280 +0,2 @@ | ||
// src/client.ts | ||
import { formatElapsedTime } from "@orama/orama/components"; | ||
import { createId } from "@paralleldrive/cuid2"; | ||
// src/cache.ts | ||
var Cache = class { | ||
cache; | ||
config; | ||
constructor(config) { | ||
this.cache = /* @__PURE__ */ new Map(); | ||
this.config = config; | ||
} | ||
set(key, value) { | ||
this.cache.set(key, value); | ||
} | ||
get(key) { | ||
return this.cache.get(key); | ||
} | ||
has(key) { | ||
return this.cache.has(key); | ||
} | ||
delete(key) { | ||
return this.cache.delete(key); | ||
} | ||
clear() { | ||
this.cache.clear(); | ||
} | ||
size() { | ||
return this.cache.size; | ||
} | ||
}; | ||
// src/constants.ts | ||
var MICROSECONDS_BASE = 1e6; | ||
var DEFAULT_TELEMETRY_FLUSH_INTERVAL = 5e3; | ||
var DEFAULT_TELEMETRY_FLUSH_SIZE = 25; | ||
// package.json | ||
var version = "1.0.0-beta.6"; | ||
var package_default = { | ||
name: "@oramacloud/client", | ||
version, | ||
description: "Orama SDK for Node.js, Deno, and Browsers", | ||
type: "module", | ||
sideEffects: false, | ||
main: "./dist/cjs/index.js", | ||
types: "./dist/src/index.d.ts", | ||
exports: { | ||
".": { | ||
types: "./dist/src/index.d.ts", | ||
import: "./dist/esm/index.js", | ||
require: "./dist/cjs/index.js" | ||
} | ||
}, | ||
scripts: { | ||
lint: "ts-standard --fix", | ||
build: "tsup src/index.ts --format cjs,esm && npm run build:ts", | ||
"build:ts": "tsc -p . --emitDeclarationOnly", | ||
test: 'echo "Error: no test specified" && exit 1', | ||
"serve:example": "esbuild src/index.ts --bundle --outfile=example/out.js --format=esm --watch --servedir=example" | ||
}, | ||
keywords: [ | ||
"orama", | ||
"search engine", | ||
"sdk" | ||
], | ||
files: [ | ||
"dist" | ||
], | ||
author: { | ||
name: "Michele Riva", | ||
email: "michele.riva@oramasearch.com", | ||
url: "https://github.com/MicheleRiva" | ||
}, | ||
license: "ISC", | ||
dependencies: { | ||
"@orama/orama": "^1.0.8", | ||
"@paralleldrive/cuid2": "^2.2.1" | ||
}, | ||
devDependencies: { | ||
"@types/node": "^20.3.1", | ||
concurrently: "^8.2.0", | ||
esbuild: "0.18.5", | ||
"ts-standard": "^12.0.2", | ||
tsup: "^7.1.0", | ||
typescript: "^5.1.3" | ||
} | ||
}; | ||
// src/collector.ts | ||
var Collector = class _Collector { | ||
data; | ||
params; | ||
config; | ||
constructor(config) { | ||
this.data = []; | ||
this.config = config; | ||
} | ||
setParams(params) { | ||
this.params = params; | ||
} | ||
static create(config) { | ||
const collector = new _Collector(config); | ||
collector.start(); | ||
return collector; | ||
} | ||
add(data) { | ||
this.data.push({ | ||
rawSearchString: data.rawSearchString, | ||
query: data.query, | ||
resultsCount: data.resultsCount, | ||
roundTripTime: data.roundTripTime, | ||
searchedAt: data.searchedAt, | ||
// The referer is different for every event: | ||
// the user can search in different pages of the website | ||
// and the referer will be different for each page | ||
referer: typeof location !== "undefined" ? location.toString() : void 0 | ||
}); | ||
if (this.params != null && this.data.length >= this.config.flushSize) { | ||
this.flush(); | ||
} | ||
} | ||
flush() { | ||
if (this.params == null || this.data.length === 0) { | ||
return; | ||
} | ||
const data = this.data; | ||
this.data = []; | ||
const body = { | ||
source: "fe", | ||
deploymentID: this.params.deploymentID, | ||
index: this.params.index, | ||
oramaId: this.config.id, | ||
oramaVersion: package_default.version, | ||
// The user agent is the same for every event | ||
// Because we use "application/x-www-form-urlencoded", | ||
// the browser doens't send the user agent automatically | ||
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : void 0, | ||
events: data | ||
}; | ||
navigator.sendBeacon?.( | ||
this.params.endpoint + `?api-key=${this.config.api_key}`, | ||
JSON.stringify(body) | ||
); | ||
} | ||
start() { | ||
setInterval(this.flush.bind(this), this.config.flushInterval); | ||
} | ||
}; | ||
// src/heartbeat.ts | ||
var HeartBeat = class { | ||
constructor(params) { | ||
this.params = params; | ||
} | ||
intervalId; | ||
start() { | ||
this.stop(); | ||
this.intervalId = setInterval(this.beat.bind(this), this.params.frequency); | ||
} | ||
stop() { | ||
if (this.intervalId !== void 0) { | ||
clearInterval(this.intervalId); | ||
} | ||
} | ||
beat() { | ||
navigator.sendBeacon?.(this.params.endpoint); | ||
} | ||
}; | ||
// src/client.ts | ||
var OramaClient = class { | ||
id = createId(); | ||
api_key; | ||
endpoint; | ||
collector; | ||
cache; | ||
heartbeat; | ||
initPromise; | ||
constructor(params) { | ||
this.api_key = params.api_key; | ||
this.endpoint = params.endpoint; | ||
if (params.telemetry !== false) { | ||
const telementryConfig = { | ||
id: this.id, | ||
api_key: this.api_key, | ||
flushInterval: params.telemetry?.flushInterval ?? DEFAULT_TELEMETRY_FLUSH_INTERVAL, | ||
flushSize: params.telemetry?.flushSize ?? DEFAULT_TELEMETRY_FLUSH_SIZE | ||
}; | ||
this.collector = Collector.create(telementryConfig); | ||
} | ||
if (params.cache !== false) { | ||
const cacheParams = {}; | ||
this.cache = new Cache(cacheParams); | ||
} | ||
this.init(); | ||
} | ||
async search(query, config) { | ||
await this.initPromise; | ||
const cacheKey = JSON.stringify(query); | ||
let roundTripTime; | ||
let searchResults; | ||
let cached = false; | ||
const shouldUseCache = config?.fresh !== true && this.cache?.has(cacheKey); | ||
if (shouldUseCache === true && this.cache != null) { | ||
roundTripTime = 0; | ||
searchResults = this.cache.get(cacheKey); | ||
cached = true; | ||
} else { | ||
const timeStart = Date.now(); | ||
searchResults = await this.fetch( | ||
"search", | ||
"POST", | ||
{ q: query }, | ||
config?.abortController | ||
); | ||
const timeEnd = Date.now(); | ||
searchResults.elapsed = await formatElapsedTime(BigInt(timeEnd * MICROSECONDS_BASE - timeStart * MICROSECONDS_BASE)); | ||
roundTripTime = timeEnd - timeStart; | ||
this.cache?.set(cacheKey, searchResults); | ||
} | ||
if (this.collector != null) { | ||
this.collector.add({ | ||
rawSearchString: query.term, | ||
resultsCount: searchResults.hits.length, | ||
roundTripTime, | ||
query, | ||
cached, | ||
searchedAt: /* @__PURE__ */ new Date() | ||
}); | ||
} | ||
return searchResults; | ||
} | ||
startHeartBeat(config) { | ||
this.heartbeat?.stop(); | ||
this.heartbeat = new HeartBeat({ | ||
...config, | ||
endpoint: this.endpoint + `/health?api-key=${this.api_key}` | ||
}); | ||
this.heartbeat.start(); | ||
} | ||
stopHeartBeat() { | ||
this.heartbeat?.stop(); | ||
} | ||
init() { | ||
this.initPromise = this.fetch("init", "GET").then((b) => { | ||
this.collector?.setParams({ | ||
endpoint: b.collectUrl, | ||
deploymentID: b.deploymentID, | ||
index: b.index | ||
}); | ||
}).catch((err) => console.log(err)); | ||
} | ||
async fetch(path, method, body, abortController) { | ||
if (abortController?.signal.aborted === true) { | ||
throw new Error("Request aborted"); | ||
} | ||
const requestOptions = { | ||
method, | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
// Unfortunatelly we can't send this headers otherwise we should pay CORS preflight request | ||
// 'x-orama-instance-id': this.id, | ||
// 'x-orama-version': version | ||
}, | ||
signal: abortController?.signal | ||
}; | ||
if (method === "POST" && body !== void 0) { | ||
const b = body; | ||
b.version = version; | ||
b.id = this.id; | ||
requestOptions.body = Object.entries(b).map(([key, value]) => `${key}=${encodeURIComponent(JSON.stringify(value))}`).join("&"); | ||
} | ||
const res = await fetch(`${this.endpoint}/${path}?api-key=${this.api_key}`, requestOptions); | ||
return await res.json(); | ||
} | ||
}; | ||
export { | ||
OramaClient | ||
}; | ||
"use strict";var u=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var x=(i,e)=>{for(var t in e)u(i,t,{get:e[t],enumerable:!0})},T=(i,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of S(e))!I.call(i,r)&&r!==t&&u(i,r,{get:()=>e[r],enumerable:!(s=b(e,r))||s.enumerable});return i};var w=i=>T(u({},"__esModule",{value:!0}),i);var O={};x(O,{OramaClient:()=>m});module.exports=w(O);var C=require("@orama/orama/components"),g=require("@paralleldrive/cuid2");var l=class{cache;config;constructor(e){this.cache=new Map,this.config=e}set(e,t){this.cache.set(e,t)}get(e){return this.cache.get(e)}has(e){return this.cache.has(e)}delete(e){return this.cache.delete(e)}clear(){this.cache.clear()}size(){return this.cache.size}};var v="1.0.0-beta.7";var y={name:"@oramacloud/client",version:v,description:"Orama SDK for Node.js, Deno, and Browsers",type:"module",sideEffects:!1,main:"./dist/index.js",types:"./dist/index.d.ts",".":{require:"./dist/index.js",import:"./dist/esm/index.js",types:"./dist/index.d.ts",browsers:"./dist/iife/index.js"},scripts:{lint:"ts-standard --fix",build:"tsup src/index.ts --format cjs,esm,iife --dts --minify --sourcemap --clean --legacy-output",test:'echo "Error: no test specified" && exit 1',"serve:example":"esbuild src/index.ts --bundle --outfile=example/out.js --format=esm --watch --servedir=example"},keywords:["orama","search engine","sdk"],files:["dist"],author:{name:"Michele Riva",email:"michele.riva@oramasearch.com",url:"https://github.com/MicheleRiva"},license:"ISC",dependencies:{"@orama/orama":"^1.0.8","@paralleldrive/cuid2":"^2.2.1"},devDependencies:{"@types/node":"^20.3.1",concurrently:"^8.2.0",esbuild:"0.18.5","ts-standard":"^12.0.2",tsup:"^7.1.0",typescript:"^5.1.3"}};var h=class i{data;params;config;constructor(e){this.data=[],this.config=e}setParams(e){this.params=e}static create(e){let t=new i(e);return t.start(),t}add(e){this.data.push({rawSearchString:e.rawSearchString,query:e.query,resultsCount:e.resultsCount,roundTripTime:e.roundTripTime,searchedAt:e.searchedAt,referer:typeof location<"u"?location.toString():void 0}),this.params!=null&&this.data.length>=this.config.flushSize&&this.flush()}flush(){if(this.params==null||this.data.length===0)return;let e=this.data;this.data=[];let t={source:"fe",deploymentID:this.params.deploymentID,index:this.params.index,oramaId:this.config.id,oramaVersion:y.version,userAgent:typeof navigator<"u"?navigator.userAgent:void 0,events:e};navigator.sendBeacon?.(this.params.endpoint+`?api-key=${this.config.api_key}`,JSON.stringify(t))}start(){setInterval(this.flush.bind(this),this.config.flushInterval)}};var p=class{constructor(e){this.params=e}intervalId;start(){this.stop(),this.intervalId=setInterval(this.beat.bind(this),this.params.frequency)}stop(){this.intervalId!==void 0&&clearInterval(this.intervalId)}beat(){navigator.sendBeacon?.(this.params.endpoint)}};var m=class{id=(0,g.createId)();api_key;endpoint;collector;cache;heartbeat;initPromise;constructor(e){if(this.api_key=e.api_key,this.endpoint=e.endpoint,e.telemetry!==!1){let t={id:this.id,api_key:this.api_key,flushInterval:e.telemetry?.flushInterval??5e3,flushSize:e.telemetry?.flushSize??25};this.collector=h.create(t)}if(e.cache!==!1){let t={};this.cache=new l(t)}this.init()}async search(e,t){await this.initPromise;let s=JSON.stringify(e),r,a,d=!1;if((t?.fresh!==!0&&this.cache?.has(s))===!0&&this.cache!=null)r=0,a=this.cache.get(s),d=!0;else{let n=Date.now();a=await this.fetch("search","POST",{q:e},t?.abortController);let c=Date.now();a.elapsed=await(0,C.formatElapsedTime)(BigInt(c*1e6-n*1e6)),r=c-n,this.cache?.set(s,a)}return this.collector!=null&&this.collector.add({rawSearchString:e.term,resultsCount:a.hits.length,roundTripTime:r,query:e,cached:d,searchedAt:new Date}),a}startHeartBeat(e){this.heartbeat?.stop(),this.heartbeat=new p({...e,endpoint:this.endpoint+`/health?api-key=${this.api_key}`}),this.heartbeat.start()}stopHeartBeat(){this.heartbeat?.stop()}init(){this.initPromise=this.fetch("init","GET").then(e=>{this.collector?.setParams({endpoint:e.collectUrl,deploymentID:e.deploymentID,index:e.index})}).catch(e=>console.log(e))}async fetch(e,t,s,r){if(r?.signal.aborted===!0)throw new Error("Request aborted");let a={method:t,headers:{"Content-Type":"application/x-www-form-urlencoded"},signal:r?.signal};if(t==="POST"&&s!==void 0){let o=s;o.version=v,o.id=this.id,a.body=Object.entries(o).map(([n,c])=>`${n}=${encodeURIComponent(JSON.stringify(c))}`).join("&")}return await(await fetch(`${this.endpoint}/${e}?api-key=${this.api_key}`,a)).json()}};0&&(module.exports={OramaClient}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@oramacloud/client", | ||
"version": "1.0.0-beta.7", | ||
"version": "1.0.0-beta.8", | ||
"description": "Orama SDK for Node.js, Deno, and Browsers", | ||
"type": "module", | ||
"sideEffects": false, | ||
"main": "./dist/index.cjs", | ||
"types": "./dist/src/index.d.ts", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs", | ||
"types": "./dist/src/index.d.ts" | ||
"require": "./dist/index.js", | ||
"import": "./dist/esm/index.js", | ||
"types": "./dist/index.d.ts", | ||
"browsers": "./dist/iife/index.js" | ||
}, | ||
@@ -42,4 +43,3 @@ "keywords": [ | ||
"lint": "ts-standard --fix", | ||
"build": "tsup src/index.ts --format cjs,esm && npm run build:ts", | ||
"build:ts": "tsc -p . --emitDeclarationOnly", | ||
"build": "tsup src/index.ts --format cjs,esm,iife --dts --minify --sourcemap --clean --legacy-output", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
@@ -46,0 +46,0 @@ "serve:example": "esbuild src/index.ts --bundle --outfile=example/out.js --format=esm --watch --servedir=example" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
128798
11
153
6
4
5