sqlite-worker
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -14,2 +14,3 @@ 'use strict'; | ||
const base = options.dist || dist; | ||
const {credentials} = options; | ||
const query = how => (template, ...values) => post(how, {template, values}); | ||
@@ -23,3 +24,4 @@ const post = (action, options) => new Promise((resolve, reject) => { | ||
options.worker || | ||
(base + '/worker.js') | ||
(base + '/worker.js'), | ||
{credentials} | ||
), { | ||
@@ -26,0 +28,0 @@ onmessage({data: {id, result, error}}) { |
@@ -1,1 +0,1 @@ | ||
const{assign:e}=Object,t="function"==typeof importScripts,r=t?".":import.meta.url.replace(/\/[^/]*$/,""),o=r=>new Promise(((o,n)=>{const s=()=>{const e=self.module.exports;delete self.exports,self.module=void 0,o(e)};if(self.exports={},self.module={exports:exports},t)importScripts(r),s();else{const{head:t}=document;e(t.appendChild(document.createElement("script")),{onload(){t.removeChild(this),s()},onerror:n,src:r})}}));const n=new WeakMap,s=(e,...t)=>{const{t:r,v:o}=((e,t)=>{const r=[e[0]],o=[];for(let n=0,s=0,c=0,{length:a}=t;s<a;s++)t[s]instanceof l?r[n]+=t[s].v+e[s+1]:(o[c++]=s,r[++n]=e[s+1]);return{t:r,v:o}})(e,t),s=n.get(e)||n.set(e,{}).get(e);return(s[r]||(s[r]=[r])).concat(o.map((e=>t[e])))};function l(e){this.v=e}const c=(e,t)=>(r,...o)=>new Promise(((n,l)=>{r.some(u)&&l(a(new Error("SQLITE_ERROR: SQL injection hazard")));const[c,...i]=s(r,...o);e[t](c.join("?"),i,((e,t)=>{e?l(e):n(t)}))})),a=e=>(e.code="SQLITE_ERROR",e),i=(e,...t)=>new l(function(e){for(var t=e[0],r=1,o=arguments.length;r<o;r++)t+=arguments[r]+e[r];return t}(e,...t)),u=e=>e.includes("?");function d(e){return{all:c(e,"all"),get:c(e,"get"),query:c(e,"run"),raw:i}}const p=(t,r=1)=>new Promise(((o,n)=>{e(indexedDB.open(t,r),{onupgradeneeded({target:{result:t,transaction:r}}){t.objectStoreNames.contains("sqlite")||t.createObjectStore("sqlite").createIndex("buffer","buffer",{unique:!0}),e(r,{oncomplete(){o(t)}})},onsuccess({target:{result:e}}){o(e)},onerror:n})})),m=(t={})=>new Promise(((n,s)=>{const l=t.dist||r;o(l+"/sql-wasm.js").then((({default:r})=>{Promise.all([p(t.name||"sqlite-worker"),r({locateFile:e=>l+"/"+e})]).then((([r,{Database:o}])=>{const l=e=>r.transaction(["sqlite"],e).objectStore("sqlite");e(l("readonly").get("buffer"),{onsuccess(){let r=Promise.resolve();const{result:s}=this,c=new o(s||t.database||new Uint8Array(0)),a=()=>{r=r.then((()=>new Promise(((r,o)=>{const n=c.export();e(l("readwrite").put(n,"buffer").transaction,{oncomplete(){r(),t.update&&t.update(n)},onabort:o,onerror:o})}))))};s||a();const{all:i,get:u,query:p,raw:m}=d({all(e,t,r){try{const o=c.exec(e,t),n=[];o.forEach(f,n),r(null,n)}catch(e){r(e)}},get(e,t,r){try{const o=c.exec(e+" LIMIT 1",t),n=[];o.forEach(f,n),r(null,n.shift()||null)}catch(e){r(e)}},run(e,t,r){try{r(null,c.run(e,t))}catch(e){r(e)}}});let h=0;n({all:i,get:u,raw:m,query(e){return/\b(?:INSERT|DELETE|UPDATE)\b/i.test(e[0])&&(clearTimeout(h),h=setTimeout(a,t.timeout||250)),p.apply(this,arguments)}})},onerror:s})}),s)}))}));function f({columns:e,values:t}){for(let{length:r}=t,o=0;o<r;o++){const r=t[o],n={};for(let{length:t}=e,o=0;o<t;o++)n[e[o]]=r[o];this.push(n)}}const h=new Map;let g=0;function w(t){const o=t.dist||r,n=e=>(t,...r)=>s(e,{template:t,values:r}),s=(e,t)=>new Promise(((r,o)=>{const n=g++;h.set(n,{resolve:r,reject:o}),l.postMessage({id:n,action:e,options:t})})),l=e(new Worker(t.worker||o+"/worker.js"),{onmessage({data:{id:e,result:t,error:r}}){const{resolve:o,reject:n}=h.get(e);h.delete(e),r?n(r):o(t)}});return s("init",e({library:o+"/init.js"},t)).then((()=>({all:n("all"),get:n("get"),query:n("query")})))}export{w as SQLiteWorker,m as init}; | ||
const{assign:e}=Object,t="function"==typeof importScripts,r=t?".":import.meta.url.replace(/\/[^/]*$/,""),n=r=>new Promise(((n,o)=>{const s=()=>{const e=self.module.exports;delete self.exports,self.module=void 0,n(e)};if(self.exports={},self.module={exports:exports},t)importScripts(r),s();else{const{head:t}=document;e(t.appendChild(document.createElement("script")),{onload(){t.removeChild(this),s()},onerror:o,src:r})}}));const o=new WeakMap,s=(e,...t)=>{const{t:r,v:n}=((e,t)=>{const r=[e[0]],n=[];for(let o=0,s=0,c=0,{length:a}=t;s<a;s++)t[s]instanceof l?r[o]+=t[s].v+e[s+1]:(n[c++]=s,r[++o]=e[s+1]);return{t:r,v:n}})(e,t),s=o.get(e)||o.set(e,{}).get(e);return(s[r]||(s[r]=[r])).concat(n.map((e=>t[e])))};function l(e){this.v=e}const c=(e,t)=>(r,...n)=>new Promise(((o,l)=>{r.some(u)&&l(a(new Error("SQLITE_ERROR: SQL injection hazard")));const[c,...i]=s(r,...n);e[t](c.join("?"),i,((e,t)=>{e?l(e):o(t)}))})),a=e=>(e.code="SQLITE_ERROR",e),i=(e,...t)=>new l(function(e){for(var t=e[0],r=1,n=arguments.length;r<n;r++)t+=arguments[r]+e[r];return t}(e,...t)),u=e=>e.includes("?");function d(e){return{all:c(e,"all"),get:c(e,"get"),query:c(e,"run"),raw:i}}const p=(t,r=1)=>new Promise(((n,o)=>{e(indexedDB.open(t,r),{onupgradeneeded({target:{result:t,transaction:r}}){t.objectStoreNames.contains("sqlite")||t.createObjectStore("sqlite").createIndex("buffer","buffer",{unique:!0}),e(r,{oncomplete(){n(t)}})},onsuccess({target:{result:e}}){n(e)},onerror:o})})),m=(t={})=>new Promise(((o,s)=>{const l=t.dist||r;n(l+"/sql-wasm.js").then((({default:r})=>{Promise.all([p(t.name||"sqlite-worker"),r({locateFile:e=>l+"/"+e})]).then((([r,{Database:n}])=>{const l=e=>r.transaction(["sqlite"],e).objectStore("sqlite");e(l("readonly").get("buffer"),{onsuccess(){let r=Promise.resolve();const{result:s}=this,c=new n(s||t.database||new Uint8Array(0)),a=()=>{r=r.then((()=>new Promise(((r,n)=>{const o=c.export();e(l("readwrite").put(o,"buffer").transaction,{oncomplete(){r(),t.update&&t.update(o)},onabort:n,onerror:n})}))))};s||a();const{all:i,get:u,query:p,raw:m}=d({all(e,t,r){try{const n=c.exec(e,t),o=[];n.forEach(f,o),r(null,o)}catch(e){r(e)}},get(e,t,r){try{const n=c.exec(e+" LIMIT 1",t),o=[];n.forEach(f,o),r(null,o.shift()||null)}catch(e){r(e)}},run(e,t,r){try{r(null,c.run(e,t))}catch(e){r(e)}}});let h=0;o({all:i,get:u,raw:m,query(e){return/\b(?:INSERT|DELETE|UPDATE)\b/i.test(e[0])&&(clearTimeout(h),h=setTimeout(a,t.timeout||250)),p.apply(this,arguments)}})},onerror:s})}),s)}))}));function f({columns:e,values:t}){for(let{length:r}=t,n=0;n<r;n++){const r=t[n],o={};for(let{length:t}=e,n=0;n<t;n++)o[e[n]]=r[n];this.push(o)}}const h=new Map;let g=0;function w(t){const n=t.dist||r,{credentials:o}=t,s=e=>(t,...r)=>l(e,{template:t,values:r}),l=(e,t)=>new Promise(((r,n)=>{const o=g++;h.set(o,{resolve:r,reject:n}),c.postMessage({id:o,action:e,options:t})})),c=e(new Worker(t.worker||n+"/worker.js",{credentials:o}),{onmessage({data:{id:e,result:t,error:r}}){const{resolve:n,reject:o}=h.get(e);h.delete(e),r?o(r):n(t)}});return l("init",e({library:n+"/init.js"},t)).then((()=>({all:s("all"),get:s("get"),query:s("query")})))}export{w as SQLiteWorker,m as init}; |
@@ -11,2 +11,3 @@ import {assign, dist} from './utils.js'; | ||
const base = options.dist || dist; | ||
const {credentials} = options; | ||
const query = how => (template, ...values) => post(how, {template, values}); | ||
@@ -20,3 +21,4 @@ const post = (action, options) => new Promise((resolve, reject) => { | ||
options.worker || | ||
(base + '/worker.js') | ||
(base + '/worker.js'), | ||
{credentials} | ||
), { | ||
@@ -23,0 +25,0 @@ onmessage({data: {id, result, error}}) { |
{ | ||
"name": "sqlite-worker", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "A simple, and persistent, SQLite database for Web and Workers", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/index.js", |
@@ -133,2 +133,3 @@ # sqlite-worker | ||
* **worker**: the *string* path where the *JS* worker to use is located. By default, this is the [dist/worker.js](./dist/worker.js) file, which is a pre-optimized version of [this source](./esm/worker.js). | ||
* **credentials**: the optional credentials *string* between `omit`, `same-origin`, or `include`, defaulting to `omit`, or better, undefined credentials. | ||
@@ -158,1 +159,3 @@ | ||
**[Live Demo](https://webreflection.github.io/sqlite-worker/test/)** - please note if you read two *OK* after the list of expected errors (due code coverage) it means everything is fine and your browser works as expected. | ||
**[CodePen](https://codepen.io/WebReflection/pen/NWROrom?editors=0010)** - will show the table result as JSON in the body. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1225009
747
160