@orama/plugin-secure-proxy 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +13 -0
- package/README.md +36 -0
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.global.js +1 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2023 OramaSearch Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Orama Secure Proxy Plugin
|
|
2
|
+
|
|
3
|
+
[](https://github.com/oramasearch/orama/actions/workflows/turbo.yml)
|
|
4
|
+
|
|
5
|
+
Orama plugin for generating embeddings securely on the front-end.
|
|
6
|
+
|
|
7
|
+
# Usage
|
|
8
|
+
|
|
9
|
+
For the complete usage guide, please refer to the [official plugin documentation](https://docs.oramasearch.com/open-source/plugins/plugin-secure-proxy).
|
|
10
|
+
|
|
11
|
+
To use the Orama Secure Proxy Plugin, you will need to sign up for a free account at [https://cloud.oramasearch.com](https://cloud.oramasearch.com)
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { create, insert, search } from '@orama/orama'
|
|
15
|
+
import { pluginSecureProxy} from '@orama/plugin-secure-proxy'
|
|
16
|
+
|
|
17
|
+
const db = await create({
|
|
18
|
+
schema: {
|
|
19
|
+
title: 'string',
|
|
20
|
+
description: 'string',
|
|
21
|
+
embeddings: 'vector[1536]'
|
|
22
|
+
},
|
|
23
|
+
plugins: [
|
|
24
|
+
pluginSecureProxy({
|
|
25
|
+
apiKey: '<API-KEY>',
|
|
26
|
+
defaultProperty: 'embeddings'
|
|
27
|
+
})
|
|
28
|
+
]
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For the full configuration guide of this plugin, please follow the [official plugin documentation](https://docs.oramasearch.com/open-source/plugins/plugin-secure-proxy).
|
|
33
|
+
|
|
34
|
+
# License
|
|
35
|
+
|
|
36
|
+
[Apache-2.0](/LICENSE.md)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e,r,t,n,o,u,i){try{var a=e[u](i);var c=a.value}catch(e){t(e);return}if(a.done){r(c)}else{Promise.resolve(c).then(n,o)}}function r(r){return function(){var t=this,n=arguments;return new Promise(function(o,u){var i=r.apply(t,n);function a(r){e(i,o,u,a,c,"next",r)}function c(r){e(i,o,u,a,c,"throw",r)}a(undefined)})}}var t=function(e){"@swc/helpers - typeof";return e&&typeof Symbol!=="undefined"&&e.constructor===Symbol?"symbol":typeof e};var n=this&&this.__generator||function(e,r){var t,n,o,u,i={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]};return u={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(u[Symbol.iterator]=function(){return this}),u;function a(e){return function(r){return c([e,r])}}function c(u){if(t)throw new TypeError("Generator is already executing.");while(i)try{if(t=1,n&&(o=u[0]&2?n["return"]:u[0]?n["throw"]||((o=n["return"])&&o.call(n),0):n.next)&&!(o=o.call(n,u[1])).done)return o;if(n=0,o)u=[u[0]&2,o.value];switch(u[0]){case 0:case 1:o=u;break;case 4:i.label++;return{value:u[1],done:false};case 5:i.label++;n=u[1];u=[0];continue;case 7:u=i.ops.pop();i.trys.pop();continue;default:if(!(o=i.trys,o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){i=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(u[0]===6&&i.label<o[1]){i.label=o[1];o=u;break}if(o&&i.label<o[2]){i.label=o[2];i.ops.push(u);break}if(o[2])i.ops.pop();i.trys.pop();continue}u=r.call(e,i)}catch(e){u=[6,e];n=0}finally{t=o=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:true}}};var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var a=Object.prototype.hasOwnProperty;var c=function(e,r){for(var t in r)o(e,t,{get:r[t],enumerable:!0})},l=function(e,r,t,n){var c=true,l=false,f=undefined;if(r&&typeof r=="object"||typeof r=="function")try{var s=function(){var i=y.value;!a.call(e,i)&&i!==t&&o(e,i,{get:function(){return r[i]},enumerable:!(n=u(r,i))||n.enumerable})};for(var p=i(r)[Symbol.iterator](),y;!(c=(y=p.next()).done);c=true)s()}catch(e){l=true;f=e}finally{try{if(!c&&p.return!=null){p.return()}}finally{if(l){throw f}}}return e};var f=function(e){return l(o({},"__esModule",{value:!0}),e)};var s={};c(s,{pluginSecureProxy:function(){return P}});module.exports=f(s);var p="https://secure-proxy.orama.run",y="".concat(p,"/init"),h="".concat(p,"/query");function v(){return(typeof window==="undefined"?"undefined":t(window))>"u"}function d(){return v()?"http://localhost":window.location.href}function w(e){return b.apply(this,arguments)}function b(){b=r(function(e){return n(this,function(r){switch(r.label){case 0:return[4,fetch("".concat(y,"?apiKey=").concat(e),{headers:{Referer:d()}})];case 1:return[2,r.sent().json()]}})});return b.apply(this,arguments)}function m(e,r,t){return g.apply(this,arguments)}function g(){g=r(function(e,r,t){var o;return n(this,function(n){switch(n.label){case 0:o=new URLSearchParams({query:r,csrf:t}).toString();return[4,fetch("".concat(h,"?apiKey=").concat(e),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",Referer:d()},body:o})];case 1:return[2,n.sent().json()]}})});return g.apply(this,arguments)}function P(e){return x.apply(this,arguments)}function x(){x=r(function(e){var t,o;return n(this,function(u){switch(u.label){case 0:if(!e.apiKey)throw new Error('Missing "apiKey" parameter for plugin-telemetry');if(!e.defaultProperty)throw new Error('Missing "defaultProperty" parameter for plugin-telemetry');return[4,w(e.apiKey)];case 1:t=u.sent(),o=t.csrfToken;return[2,{name:"secure-proxy",beforeSearch:function t(t,u){return r(function(){var r,t,i,a,c;return n(this,function(n){switch(n.label){case 0:if(u.mode!=="vector"&&u.mode!=="hybrid"||(u===null||u===void 0?void 0:(r=u.vector)===null||r===void 0?void 0:r.value))return[2];if(!u.term)throw new Error('Neither "term" nor "vector" parameters were provided');i=u.term;return[4,m(e.apiKey,i,o)];case 1:a=n.sent();u.vector||(u.vector={property:(c=u===null||u===void 0?void 0:(t=u.vector)===null||t===void 0?void 0:t.property)!==null&&c!==void 0?c:e.defaultProperty,value:a}),u.vector.value=a;return[2]}})})()}}]}})});return x.apply(this,arguments)}0&&(module.exports={pluginSecureProxy:pluginSecureProxy});//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["src_exports","__export","pluginSecureProxy","__toCommonJS","SECURE_PROXY_ENDPOINT","INIT_URL","SEARCH_URL","isServer","getReferer","getCSRFToken","apiKey","getEmbeddings","query","csrfToken","body","pluginParams","_db","params","term","embeddings"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,IAAA,eAAAC,EAAAH,GAeA,IAAMI,EAAwB,iCACxBC,EAAW,GAAGD,CAAqB,QACnCE,EAAa,GAAGF,CAAqB,SAE3C,SAASG,GAAW,CAClB,OAAO,OAAO,OAAW,GAC3B,CAEA,SAASC,GAAa,CACpB,OAAOD,EAAS,EAAI,mBAAqB,OAAO,SAAS,IAC3D,CAEA,eAAeE,EAAaC,EAAuC,CAOjE,OANiB,MAAM,MAAM,GAAGL,CAAQ,WAAWK,CAAM,GAAI,CAC3D,QAAS,CACP,QAASF,EAAW,CACtB,CACF,CAAC,GAEe,KAAK,CACvB,CAEA,eAAeG,EAAcD,EAAgBE,EAAeC,EAAgD,CAC1G,IAAMC,EAAO,IAAI,gBAAgB,CAAE,MAAAF,EAAO,KAAMC,CAAU,CAAC,EAAE,SAAS,EAWtE,OATiB,MAAM,MAAM,GAAGP,CAAU,WAAWI,CAAM,GAAI,CAC7D,OAAQ,OACR,QAAS,CACP,eAAgB,mDAChB,QAASF,EAAW,CACtB,EACA,KAAAM,CACF,CAAC,GAEe,KAAK,CACvB,CAEA,eAAsBZ,EAAkBa,EAA0D,CAChG,GAAI,CAACA,EAAa,OAAQ,MAAM,IAAI,MAAM,iDAAiD,EAC3F,GAAI,CAACA,EAAa,gBAAiB,MAAM,IAAI,MAAM,0DAA0D,EAE7G,GAAM,CAAE,UAAAF,CAAU,EAAI,MAAMJ,EAAaM,EAAa,MAAM,EAE5D,MAAO,CACL,KAAM,eAEN,MAAM,aAAiCC,EAAeC,EAA6C,CAKjG,GAJIA,EAAO,OAAS,UAAYA,EAAO,OAAS,UAI5CA,GAAQ,QAAQ,MAClB,OAGF,GAAI,CAACA,EAAO,KACV,MAAM,IAAI,MAAM,sDAAsD,EAGxE,IAAMC,EAAOD,EAAO,KACdE,EAAa,MAAMR,EAAcI,EAAa,OAAQG,EAAML,CAAS,EAEtEI,EAAO,SACVA,EAAO,OAAS,CAEd,SAAUA,GAAQ,QAAQ,UAAYF,EAAa,gBACnD,MAAOI,CACT,GAGFF,EAAO,OAAO,MAAQE,CACxB,CACF,CACF","sourcesContent":["import type { AnyOrama, SearchParams, TypedDocument, OramaPluginAsync } from '@orama/orama'\n\ntype InitResponse = {\n clientID: string\n ttl: number\n csrfToken: string\n}\n\ntype EmbeddingsResponse = number[]\n\nexport type SecureProxyPluginOptions = {\n apiKey: string\n defaultProperty: string\n}\n\nconst SECURE_PROXY_ENDPOINT = 'https://secure-proxy.orama.run'\nconst INIT_URL = `${SECURE_PROXY_ENDPOINT}/init`\nconst SEARCH_URL = `${SECURE_PROXY_ENDPOINT}/query`\n\nfunction isServer() {\n return typeof window === 'undefined'\n}\n\nfunction getReferer() {\n return isServer() ? 'http://localhost' : window.location.href\n}\n\nasync function getCSRFToken(apiKey: string): Promise<InitResponse> {\n const response = await fetch(`${INIT_URL}?apiKey=${apiKey}`, {\n headers: {\n Referer: getReferer()\n }\n })\n\n return response.json()\n}\n\nasync function getEmbeddings(apiKey: string, query: string, csrfToken: string): Promise<EmbeddingsResponse> {\n const body = new URLSearchParams({ query, csrf: csrfToken }).toString()\n\n const response = await fetch(`${SEARCH_URL}?apiKey=${apiKey}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',\n Referer: getReferer()\n },\n body\n })\n\n return response.json()\n}\n\nexport async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync {\n if (!pluginParams.apiKey) throw new Error('Missing \"apiKey\" parameter for plugin-telemetry')\n if (!pluginParams.defaultProperty) throw new Error('Missing \"defaultProperty\" parameter for plugin-telemetry')\n\n const { csrfToken } = await getCSRFToken(pluginParams.apiKey)\n\n return {\n name: 'secure-proxy',\n\n async beforeSearch<T extends AnyOrama>(_db: AnyOrama, params: SearchParams<T, TypedDocument<any>>) {\n if (params.mode !== 'vector' && params.mode !== 'hybrid') {\n return\n }\n\n if (params?.vector?.value) {\n return\n }\n\n if (!params.term) {\n throw new Error('Neither \"term\" nor \"vector\" parameters were provided')\n }\n\n const term = params.term\n const embeddings = await getEmbeddings(pluginParams.apiKey, term, csrfToken)\n\n if (!params.vector) {\n params.vector = {\n // @ts-expect-error - vector param is not present in full-text search\n property: params?.vector?.property ?? pluginParams.defaultProperty,\n value: embeddings\n }\n }\n\n params.vector.value = embeddings\n }\n }\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OramaPluginAsync } from '@orama/orama';
|
|
2
|
+
|
|
3
|
+
type SecureProxyPluginOptions = {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
defaultProperty: string;
|
|
6
|
+
};
|
|
7
|
+
declare function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync;
|
|
8
|
+
|
|
9
|
+
export { SecureProxyPluginOptions, pluginSecureProxy };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OramaPluginAsync } from '@orama/orama';
|
|
2
|
+
|
|
3
|
+
type SecureProxyPluginOptions = {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
defaultProperty: string;
|
|
6
|
+
};
|
|
7
|
+
declare function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync;
|
|
8
|
+
|
|
9
|
+
export { SecureProxyPluginOptions, pluginSecureProxy };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function e(e,r,t,n,o,i,u){try{var a=e[i](u);var c=a.value}catch(e){t(e);return}if(a.done){r(c)}else{Promise.resolve(c).then(n,o)}}function r(r){return function(){var t=this,n=arguments;return new Promise(function(o,i){var u=r.apply(t,n);function a(r){e(u,o,i,a,c,"next",r)}function c(r){e(u,o,i,a,c,"throw",r)}a(undefined)})}}var t=function(e){"@swc/helpers - typeof";return e&&typeof Symbol!=="undefined"&&e.constructor===Symbol?"symbol":typeof e};var n=this&&this.__generator||function(e,r){var t,n,o,i,u={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(i[Symbol.iterator]=function(){return this}),i;function a(e){return function(r){return c([e,r])}}function c(i){if(t)throw new TypeError("Generator is already executing.");while(u)try{if(t=1,n&&(o=i[0]&2?n["return"]:i[0]?n["throw"]||((o=n["return"])&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;if(n=0,o)i=[i[0]&2,o.value];switch(i[0]){case 0:case 1:o=i;break;case 4:u.label++;return{value:i[1],done:false};case 5:u.label++;n=i[1];i=[0];continue;case 7:i=u.ops.pop();u.trys.pop();continue;default:if(!(o=u.trys,o=o.length>0&&o[o.length-1])&&(i[0]===6||i[0]===2)){u=0;continue}if(i[0]===3&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(i[0]===6&&u.label<o[1]){u.label=o[1];o=i;break}if(o&&u.label<o[2]){u.label=o[2];u.ops.push(i);break}if(o[2])u.ops.pop();u.trys.pop();continue}i=r.call(e,u)}catch(e){i=[6,e];n=0}finally{t=o=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};var pluginTelemetry=function(){var e=function e(){return(typeof window==="undefined"?"undefined":t(window))>"u"};var o=function r(){return e()?"http://localhost":window.location.href};var i=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var l=function(e,r){for(var t in r)i(e,t,{get:r[t],enumerable:!0})},f=function(e,r,t,n){var o=true,l=false,f=undefined;if(r&&typeof r=="object"||typeof r=="function")try{var s=function(){var o=y.value;!c.call(e,o)&&o!==t&&i(e,o,{get:function(){return r[o]},enumerable:!(n=u(r,o))||n.enumerable})};for(var p=a(r)[Symbol.iterator](),y;!(o=(y=p.next()).done);o=true)s()}catch(e){l=true;f=e}finally{try{if(!o&&p.return!=null){p.return()}}finally{if(l){throw f}}}return e};var s=function(e){return f(i({},"__esModule",{value:!0}),e)};var p={};l(p,{pluginSecureProxy:function(){return g}});var y="https://secure-proxy.orama.run",v="".concat(y,"/init"),h="".concat(y,"/query");function d(e){return w.apply(this,arguments)}function w(){w=r(function(e){return n(this,function(r){switch(r.label){case 0:return[4,fetch("".concat(v,"?apiKey=").concat(e),{headers:{Referer:o()}})];case 1:return[2,r.sent().json()]}})});return w.apply(this,arguments)}function b(e,r,t){return m.apply(this,arguments)}function m(){m=r(function(e,r,t){var i;return n(this,function(n){switch(n.label){case 0:i=new URLSearchParams({query:r,csrf:t}).toString();return[4,fetch("".concat(h,"?apiKey=").concat(e),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",Referer:o()},body:i})];case 1:return[2,n.sent().json()]}})});return m.apply(this,arguments)}function g(e){return P.apply(this,arguments)}function P(){P=r(function(e){var t,o;return n(this,function(i){switch(i.label){case 0:if(!e.apiKey)throw new Error('Missing "apiKey" parameter for plugin-telemetry');if(!e.defaultProperty)throw new Error('Missing "defaultProperty" parameter for plugin-telemetry');return[4,d(e.apiKey)];case 1:t=i.sent(),o=t.csrfToken;return[2,{name:"secure-proxy",beforeSearch:function t(t,i){return r(function(){var r,t,u,a,c;return n(this,function(n){switch(n.label){case 0:if(i.mode!=="vector"&&i.mode!=="hybrid"||(i===null||i===void 0?void 0:(r=i.vector)===null||r===void 0?void 0:r.value))return[2];if(!i.term)throw new Error('Neither "term" nor "vector" parameters were provided');u=i.term;return[4,b(e.apiKey,u,o)];case 1:a=n.sent();i.vector||(i.vector={property:(c=i===null||i===void 0?void 0:(t=i.vector)===null||t===void 0?void 0:t.property)!==null&&c!==void 0?c:e.defaultProperty,value:a}),i.vector.value=a;return[2]}})})()}}]}})});return P.apply(this,arguments)}return s(p)}();//# sourceMappingURL=index.global.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["src_exports","__export","pluginSecureProxy","SECURE_PROXY_ENDPOINT","INIT_URL","SEARCH_URL","isServer","getReferer","getCSRFToken","apiKey","getEmbeddings","query","csrfToken","body","pluginParams","_db","params","term","embeddings"],"mappings":"mcAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,IAeA,IAAMC,EAAwB,iCACxBC,EAAW,GAAGD,CAAqB,QACnCE,EAAa,GAAGF,CAAqB,SAE3C,SAASG,GAAW,CA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= {\n clientID: string\n ttl: number\n csrfToken: string\n}\n\ntype EmbeddingsResponse = number[]\n\nexport type SecureProxyPluginOptions = {\n apiKey: string\n defaultProperty: string\n}\n\nconst SECURE_PROXY_ENDPOINT = 'https://secure-proxy.orama.run'\nconst INIT_URL = `${SECURE_PROXY_ENDPOINT}/init`\nconst SEARCH_URL = `${SECURE_PROXY_ENDPOINT}/query`\n\nfunction isServer() {\n return typeof window === 'undefined'\n}\n\nfunction getReferer() {\n return isServer() ? 'http://localhost' : window.location.href\n}\n\nasync function getCSRFToken(apiKey: string): Promise<InitResponse> {\n const response = await fetch(`${INIT_URL}?apiKey=${apiKey}`, {\n headers: {\n Referer: getReferer()\n }\n })\n\n return response.json()\n}\n\nasync function getEmbeddings(apiKey: string, query: string, csrfToken: string): Promise<EmbeddingsResponse> {\n const body = new URLSearchParams({ query, csrf: csrfToken }).toString()\n\n const response = await fetch(`${SEARCH_URL}?apiKey=${apiKey}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',\n Referer: getReferer()\n },\n body\n })\n\n return response.json()\n}\n\nexport async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync {\n if (!pluginParams.apiKey) throw new Error('Missing \"apiKey\" parameter for plugin-telemetry')\n if (!pluginParams.defaultProperty) throw new Error('Missing \"defaultProperty\" parameter for plugin-telemetry')\n\n const { csrfToken } = await getCSRFToken(pluginParams.apiKey)\n\n return {\n name: 'secure-proxy',\n\n async beforeSearch<T extends AnyOrama>(_db: AnyOrama, params: SearchParams<T, TypedDocument<any>>) {\n if (params.mode !== 'vector' && params.mode !== 'hybrid') {\n return\n }\n\n if (params?.vector?.value) {\n return\n }\n\n if (!params.term) {\n throw new Error('Neither \"term\" nor \"vector\" parameters were provided')\n }\n\n const term = params.term\n const embeddings = await getEmbeddings(pluginParams.apiKey, term, csrfToken)\n\n if (!params.vector) {\n params.vector = {\n // @ts-expect-error - vector param is not present in full-text search\n property: params?.vector?.property ?? pluginParams.defaultProperty,\n value: embeddings\n }\n }\n\n params.vector.value = embeddings\n }\n }\n}\n"]}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,r,t,n,o,i,u){try{var a=e[i](u);var c=a.value}catch(e){t(e);return}if(a.done){r(c)}else{Promise.resolve(c).then(n,o)}}function r(r){return function(){var t=this,n=arguments;return new Promise(function(o,i){var u=r.apply(t,n);function a(r){e(u,o,i,a,c,"next",r)}function c(r){e(u,o,i,a,c,"throw",r)}a(undefined)})}}var t=function(e){"@swc/helpers - typeof";return e&&typeof Symbol!=="undefined"&&e.constructor===Symbol?"symbol":typeof e};var n=this&&this.__generator||function(e,r){var t,n,o,i,u={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]};return i={next:a(0),"throw":a(1),"return":a(2)},typeof Symbol==="function"&&(i[Symbol.iterator]=function(){return this}),i;function a(e){return function(r){return c([e,r])}}function c(i){if(t)throw new TypeError("Generator is already executing.");while(u)try{if(t=1,n&&(o=i[0]&2?n["return"]:i[0]?n["throw"]||((o=n["return"])&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;if(n=0,o)i=[i[0]&2,o.value];switch(i[0]){case 0:case 1:o=i;break;case 4:u.label++;return{value:i[1],done:false};case 5:u.label++;n=i[1];i=[0];continue;case 7:i=u.ops.pop();u.trys.pop();continue;default:if(!(o=u.trys,o=o.length>0&&o[o.length-1])&&(i[0]===6||i[0]===2)){u=0;continue}if(i[0]===3&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(i[0]===6&&u.label<o[1]){u.label=o[1];o=i;break}if(o&&u.label<o[2]){u.label=o[2];u.ops.push(i);break}if(o[2])u.ops.pop();u.trys.pop();continue}i=r.call(e,u)}catch(e){i=[6,e];n=0}finally{t=o=0}if(i[0]&5)throw i[1];return{value:i[0]?i[1]:void 0,done:true}}};var o="https://secure-proxy.orama.run",i="".concat(o,"/init"),u="".concat(o,"/query");function a(){return(typeof window==="undefined"?"undefined":t(window))>"u"}function c(){return a()?"http://localhost":window.location.href}function l(e){return s.apply(this,arguments)}function s(){s=r(function(e){return n(this,function(r){switch(r.label){case 0:return[4,fetch("".concat(i,"?apiKey=").concat(e),{headers:{Referer:c()}})];case 1:return[2,r.sent().json()]}})});return s.apply(this,arguments)}function f(e,r,t){return p.apply(this,arguments)}function p(){p=r(function(e,r,t){var o;return n(this,function(n){switch(n.label){case 0:o=new URLSearchParams({query:r,csrf:t}).toString();return[4,fetch("".concat(u,"?apiKey=").concat(e),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",Referer:c()},body:o})];case 1:return[2,n.sent().json()]}})});return p.apply(this,arguments)}function h(e){return y.apply(this,arguments)}function y(){y=r(function(e){var t,o;return n(this,function(i){switch(i.label){case 0:if(!e.apiKey)throw new Error('Missing "apiKey" parameter for plugin-telemetry');if(!e.defaultProperty)throw new Error('Missing "defaultProperty" parameter for plugin-telemetry');return[4,l(e.apiKey)];case 1:t=i.sent(),o=t.csrfToken;return[2,{name:"secure-proxy",beforeSearch:function t(t,i){return r(function(){var r,t,u,a,c;return n(this,function(n){switch(n.label){case 0:if(i.mode!=="vector"&&i.mode!=="hybrid"||(i===null||i===void 0?void 0:(r=i.vector)===null||r===void 0?void 0:r.value))return[2];if(!i.term)throw new Error('Neither "term" nor "vector" parameters were provided');u=i.term;return[4,f(e.apiKey,u,o)];case 1:a=n.sent();i.vector||(i.vector={property:(c=i===null||i===void 0?void 0:(t=i.vector)===null||t===void 0?void 0:t.property)!==null&&c!==void 0?c:e.defaultProperty,value:a}),i.vector.value=a;return[2]}})})()}}]}})});return y.apply(this,arguments)}export{h as pluginSecureProxy};//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["SECURE_PROXY_ENDPOINT","INIT_URL","SEARCH_URL","isServer","getReferer","getCSRFToken","apiKey","getEmbeddings","query","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Q,UAAYF,EAAa,gBACnD,MAAOI,CACT,GAGFF,EAAO,OAAO,MAAQE,CACxB,CACF,CACF","sourcesContent":["import type { AnyOrama, SearchParams, TypedDocument, OramaPluginAsync } from '@orama/orama'\n\ntype InitResponse = {\n clientID: string\n ttl: number\n csrfToken: string\n}\n\ntype EmbeddingsResponse = number[]\n\nexport type SecureProxyPluginOptions = {\n apiKey: string\n defaultProperty: string\n}\n\nconst SECURE_PROXY_ENDPOINT = 'https://secure-proxy.orama.run'\nconst INIT_URL = `${SECURE_PROXY_ENDPOINT}/init`\nconst SEARCH_URL = `${SECURE_PROXY_ENDPOINT}/query`\n\nfunction isServer() {\n return typeof window === 'undefined'\n}\n\nfunction getReferer() {\n return isServer() ? 'http://localhost' : window.location.href\n}\n\nasync function getCSRFToken(apiKey: string): Promise<InitResponse> {\n const response = await fetch(`${INIT_URL}?apiKey=${apiKey}`, {\n headers: {\n Referer: getReferer()\n }\n })\n\n return response.json()\n}\n\nasync function getEmbeddings(apiKey: string, query: string, csrfToken: string): Promise<EmbeddingsResponse> {\n const body = new URLSearchParams({ query, csrf: csrfToken }).toString()\n\n const response = await fetch(`${SEARCH_URL}?apiKey=${apiKey}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',\n Referer: getReferer()\n },\n body\n })\n\n return response.json()\n}\n\nexport async function pluginSecureProxy(pluginParams: SecureProxyPluginOptions): OramaPluginAsync {\n if (!pluginParams.apiKey) throw new Error('Missing \"apiKey\" parameter for plugin-telemetry')\n if (!pluginParams.defaultProperty) throw new Error('Missing \"defaultProperty\" parameter for plugin-telemetry')\n\n const { csrfToken } = await getCSRFToken(pluginParams.apiKey)\n\n return {\n name: 'secure-proxy',\n\n async beforeSearch<T extends AnyOrama>(_db: AnyOrama, params: SearchParams<T, TypedDocument<any>>) {\n if (params.mode !== 'vector' && params.mode !== 'hybrid') {\n return\n }\n\n if (params?.vector?.value) {\n return\n }\n\n if (!params.term) {\n throw new Error('Neither \"term\" nor \"vector\" parameters were provided')\n }\n\n const term = params.term\n const embeddings = await getEmbeddings(pluginParams.apiKey, term, csrfToken)\n\n if (!params.vector) {\n params.vector = {\n // @ts-expect-error - vector param is not present in full-text search\n property: params?.vector?.property ?? pluginParams.defaultProperty,\n value: embeddings\n }\n }\n\n params.vector.value = embeddings\n }\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@orama/plugin-secure-proxy",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Orama plugin for generating embeddings securely on the front-end",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"orama",
|
|
7
|
+
"embeddings",
|
|
8
|
+
"secure proxy",
|
|
9
|
+
"vector search"
|
|
10
|
+
],
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"require": "./dist/index.cjs",
|
|
17
|
+
"import": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"browser": "./dist/index.global.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/oramasearch/orama/issues"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/oramasearch/orama#readme",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/oramasearch/orama.git"
|
|
29
|
+
},
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^20.9.0",
|
|
40
|
+
"c8": "^7.12.0",
|
|
41
|
+
"sinon": "^17.0.1",
|
|
42
|
+
"tap": "^18.6.1",
|
|
43
|
+
"tap-mocha-reporter": "^5.0.3",
|
|
44
|
+
"tape": "^5.6.1",
|
|
45
|
+
"ts-standard": "^12.0.2",
|
|
46
|
+
"tsup": "^7.2.0",
|
|
47
|
+
"typescript": "^5.0.0",
|
|
48
|
+
"@orama/orama": "2.0.0"
|
|
49
|
+
},
|
|
50
|
+
"tap": {
|
|
51
|
+
"extends": "src/tests/config/tap.yml"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup --config tsup.lib.js",
|
|
55
|
+
"lint": "exit 0",
|
|
56
|
+
"test": "exit 0"
|
|
57
|
+
}
|
|
58
|
+
}
|