New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sqlite.org/sqlite-wasm

Package Overview
Dependencies
Maintainers
2
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sqlite.org/sqlite-wasm - npm Package Compare versions

Comparing version 3.45.3-build3 to 3.46.0-build1

sqlite-wasm/jswasm/sqlite3-worker1-promiser.mjs

2

package.json
{
"name": "@sqlite.org/sqlite-wasm",
"version": "3.45.3-build3",
"version": "3.46.0-build1",
"description": "SQLite Wasm conveniently wrapped as an ES Module.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -54,3 +54,3 @@ /*

const wPost = (type, ...args) => postMessage({ type, payload: args });
const installAsyncProxy = function (self) {
const installAsyncProxy = function () {
const toss = function (...args) {

@@ -68,16 +68,4 @@ throw new Error(args.join(' '));

/**
* Will hold state copied to this object from the syncronous side of
* this API.
*/
const state = Object.create(null);
/**
* verbose:
*
* 0 = no logging output
* 1 = only errors
* 2 = warnings and errors
* 3 = debug, warnings, and errors
*/
state.verbose = 1;

@@ -139,31 +127,6 @@

/**
* __openFiles is a map of sqlite3_file pointers (integers) to
* metadata related to a given OPFS file handles. The pointers are, in
* this side of the interface, opaque file handle IDs provided by the
* synchronous part of this constellation. Each value is an object
* with a structure demonstrated in the xOpen() impl.
*/
const __openFiles = Object.create(null);
/**
* __implicitLocks is a Set of sqlite3_file pointers (integers) which were
* "auto-locked". i.e. those for which we obtained a sync access
* handle without an explicit xLock() call. Such locks will be
* released during db connection idle time, whereas a sync access
* handle obtained via xLock(), or subsequently xLock()'d after
* auto-acquisition, will not be released until xUnlock() is called.
*
* Maintenance reminder: if we relinquish auto-locks at the end of the
* operation which acquires them, we pay a massive performance
* penalty: speedtest1 benchmarks take up to 4x as long. By delaying
* the lock release until idle time, the hit is negligible.
*/
const __implicitLocks = new Set();
/**
* Expects an OPFS file path. It gets resolved, such that ".."
* components are properly expanded, and returned. If the 2nd arg is
* true, the result is returned as an array of path elements, else an
* absolute path string is returned.
*/
const getResolvedPath = function (filename, splitIt) {

@@ -174,8 +137,2 @@ const p = new URL(filename, 'file://irrelevant').pathname;

/**
* Takes the absolute path to a filesystem element. Returns an array
* of [handleOfContainingDir, filename]. If the 2nd argument is truthy
* then each directory element leading to the file is created along
* the way. Throws if any creation or resolution fails.
*/
const getDirForFilename = async function f(absFilename, createDirs = false) {

@@ -193,12 +150,2 @@ const path = getResolvedPath(absFilename, true);

/**
* If the given file-holding object has a sync handle attached to it,
* that handle is remove and asynchronously closed. Though it may
* sound sensible to continue work as soon as the close() returns
* (noting that it's asynchronous), doing so can cause operations
* performed soon afterwards, e.g. a call to getSyncHandle() to fail
* because they may happen out of order from the close(). OPFS does
* not guaranty that the actual order of operations is retained in
* such cases. i.e. always "await" on the result of this function.
*/
const closeSyncHandle = async (fh) => {

@@ -215,13 +162,2 @@ if (fh.syncHandle) {

/**
* A proxy for closeSyncHandle() which is guaranteed to not throw.
*
* This function is part of a lock/unlock step in functions which
* require a sync access handle but may be called without xLock()
* having been called first. Such calls need to release that
* handle to avoid locking the file for all of time. This is an
* _attempt_ at reducing cross-tab contention but it may prove
* to be more of a problem than a solution and may need to be
* removed.
*/
const closeSyncHandleNoThrow = async (fh) => {

@@ -235,6 +171,4 @@ try {

/* Release all auto-locks. */
const releaseImplicitLocks = async () => {
if (__implicitLocks.size) {
/* Release all auto-locks. */
for (const fid of __implicitLocks) {

@@ -248,10 +182,2 @@ const fh = __openFiles[fid];

/**
* An experiment in improving concurrency by freeing up implicit locks
* sooner. This is known to impact performance dramatically but it has
* also shown to improve concurrency considerably.
*
* If fh.releaseImplicitLocks is truthy and fh is in __implicitLocks,
* this routine returns closeSyncHandleNoThrow(), else it is a no-op.
*/
const releaseImplicitLock = async (fh) => {

@@ -263,20 +189,2 @@ if (fh.releaseImplicitLocks && __implicitLocks.has(fh.fid)) {

/**
* An error class specifically for use with getSyncHandle(), the goal
* of which is to eventually be able to distinguish unambiguously
* between locking-related failures and other types, noting that we
* cannot currently do so because createSyncAccessHandle() does not
* define its exceptions in the required level of detail.
*
* 2022-11-29: according to:
*
* https://github.com/whatwg/fs/pull/21
*
* NoModificationAllowedError will be the standard exception thrown
* when acquisition of a sync access handle fails due to a locking
* error. As of this writing, that error type is not visible in the
* dev console in Chrome v109, nor is it documented in MDN, but an
* error with that "name" property is being thrown from the OPFS
* layer.
*/
class GetSyncHandleError extends Error {

@@ -297,8 +205,5 @@ constructor(errorObject, ...msg) {

(e.cause.name === 'NoModificationAllowedError' ||
/* Inconsistent exception.name from Chrome/ium with the
same exception.message text: */
(e.cause.name === 'DOMException' &&
0 === e.cause.message.indexOf('Access Handles cannot')))
? /*console.warn("SQLITE_BUSY",e),*/
state.sq3Codes.SQLITE_BUSY
? state.sq3Codes.SQLITE_BUSY
: rc;

@@ -309,14 +214,3 @@ } else {

};
/**
* Returns the sync access handle associated with the given file
* handle object (which must be a valid handle object, as created by
* xOpen()), lazily opening it if needed.
*
* In order to help alleviate cross-tab contention for a dabase, if
* an exception is thrown while acquiring the handle, this routine
* will wait briefly and try again, up to some fixed number of
* times. If acquisition still fails at that point it will give up
* and propagate the exception. Client-level code will see that as
* an I/O error.
*/
const getSyncHandle = async (fh, opName) => {

@@ -332,5 +226,2 @@ if (!fh.syncHandle) {

try {
//if(i<3) toss("Just testing getSyncHandle() wait-and-retry.");
//TODO? A config option which tells it to throw here
//randomly every now and then, for testing purposes.
fh.syncHandle = await fh.fileHandle.createSyncAccessHandle();

@@ -381,6 +272,2 @@ break;

/**
* Stores the given value at state.sabOPView[state.opIds.rc] and then
* Atomics.notify()'s it.
*/
const storeAndNotify = (opName, value) => {

@@ -392,3 +279,2 @@ log(opName + '() => notify(', value, ')');

/** Throws if fh is a file-holding object which is flagged as read-only. */
const affirmNotRO = function (opName, fh) {

@@ -398,9 +284,2 @@ if (fh.readOnly) toss(opName + '(): File is read-only: ' + fh.filenameAbs);

/**
* We track 2 different timers: the "metrics" timer records how much
* time we spend performing work. The "wait" timer records how much
* time we spend waiting on the underlying OPFS timer. See the calls
* to mTimeStart(), mTimeEnd(), wTimeStart(), and wTimeEnd()
* throughout this file to see how they're used.
*/
const __mTimer = Object.create(null);

@@ -412,3 +291,3 @@ __mTimer.op = undefined;

__mTimer.op = op;
//metrics[op] || toss("Maintenance required: missing metrics for",op);
++metrics[op].count;

@@ -424,3 +303,2 @@ };

__wTimer.op = op;
//metrics[op] || toss("Maintenance required: missing metrics for",op);
};

@@ -430,15 +308,4 @@ const wTimeEnd = () =>

/**
* Gets set to true by the 'opfs-async-shutdown' command to quit the
* wait loop. This is only intended for debugging purposes: we cannot
* inspect this file's state while the tight waitLoop() is running and
* need a way to stop that loop for introspection purposes.
*/
let flagAsyncShutdown = false;
/**
* Asynchronous wrappers for sqlite3_vfs and sqlite3_io_methods
* methods, as well as helpers like mkdir(). Maintenance reminder:
* members are in alphabetical order to simplify finding them.
*/
const vfsAsyncImpls = {

@@ -472,13 +339,3 @@ 'opfs-async-metrics': async () => {

mTimeStart('xAccess');
/* OPFS cannot support the full range of xAccess() queries
sqlite3 calls for. We can essentially just tell if the file
is accessible, but if it is then it's automatically writable
(unless it's locked, which we cannot(?) know without trying
to open it). OPFS does not have the notion of read-only.
The return semantics of this function differ from sqlite3's
xAccess semantics because we are limited in what we can
communicate back to our synchronous communication partner: 0 =
accessible, non-0 means not accessible.
*/
let rc = 0;

@@ -498,3 +355,3 @@ wTimeStart('xAccess');

},
xClose: async function (fid /*sqlite3_file pointer*/) {
xClose: async function (fid) {
const opName = 'xClose';

@@ -531,14 +388,2 @@ mTimeStart(opName);

xDeleteNoWait: async function (filename, syncDir = 0, recursive = false) {
/* The syncDir flag is, for purposes of the VFS API's semantics,
ignored here. However, if it has the value 0x1234 then: after
deleting the given file, recursively try to delete any empty
directories left behind in its wake (ignoring any errors and
stopping at the first failure).
That said: we don't know for sure that removeEntry() fails if
the dir is not empty because the API is not documented. It has,
however, a "recursive" flag which defaults to false, so
presumably it will fail if the dir is not empty and that flag
is false.
*/
let rc = 0;

@@ -564,3 +409,3 @@ wTimeStart('xDelete');

},
xFileSize: async function (fid /*sqlite3_file pointer*/) {
xFileSize: async function (fid) {
mTimeStart('xFileSize');

@@ -582,6 +427,3 @@ const fh = __openFiles[fid];

},
xLock: async function (
fid /*sqlite3_file pointer*/,
lockType /*SQLITE_LOCK_...*/,
) {
xLock: async function (fid, lockType) {
mTimeStart('xLock');

@@ -610,8 +452,3 @@ const fh = __openFiles[fid];

},
xOpen: async function (
fid /*sqlite3_file pointer*/,
filename,
flags /*SQLITE_OPEN_...*/,
opfsFlags /*OPFS_...*/,
) {
xOpen: async function (fid, filename, flags, opfsFlags) {
const opName = 'xOpen';

@@ -632,2 +469,7 @@ mTimeStart(opName);

}
if (state.opfsFlags.OPFS_UNLINK_BEFORE_OPEN & opfsFlags) {
try {
await hDir.removeEntry(filenamePart);
} catch (e) {}
}
const hFile = await hDir.getFileHandle(filenamePart, { create });

@@ -650,15 +492,4 @@ wTimeEnd();

state.opfsFlags.defaultUnlockAsap;
if (
0 /* this block is modelled after something wa-sqlite
does but it leads to immediate contention on journal files.
Update: this approach reportedly only works for DELETE journal
mode. */ &&
0 === (flags & state.sq3Codes.SQLITE_OPEN_MAIN_DB)
) {
/* sqlite does not lock these files, so go ahead and grab an OPFS
lock. */
fh.xLock = 'xOpen' /* Truthy value to keep entry from getting
flagged as auto-locked. String value so
that we can easily distinguish is later
if needed. */;
if (0 && 0 === (flags & state.sq3Codes.SQLITE_OPEN_MAIN_DB)) {
fh.xLock = 'xOpen';
await getSyncHandle(fh, 'xOpen');

@@ -676,3 +507,3 @@ }

},
xRead: async function (fid /*sqlite3_file pointer*/, n, offset64) {
xRead: async function (fid, n, offset64) {
mTimeStart('xRead');

@@ -690,3 +521,2 @@ let rc = 0,

if (nRead < n) {
/* Zero-fill remaining bytes */
fh.sabView.fill(0, nRead, n);

@@ -705,3 +535,3 @@ rc = state.sq3Codes.SQLITE_IOERR_SHORT_READ;

},
xSync: async function (fid /*sqlite3_file pointer*/, flags /*ignored*/) {
xSync: async function (fid, flags) {
mTimeStart('xSync');

@@ -723,3 +553,3 @@ const fh = __openFiles[fid];

},
xTruncate: async function (fid /*sqlite3_file pointer*/, size) {
xTruncate: async function (fid, size) {
mTimeStart('xTruncate');

@@ -745,6 +575,3 @@ let rc = 0;

},
xUnlock: async function (
fid /*sqlite3_file pointer*/,
lockType /*SQLITE_LOCK_...*/,
) {
xUnlock: async function (fid, lockType) {
mTimeStart('xUnlock');

@@ -766,3 +593,3 @@ let rc = 0;

},
xWrite: async function (fid /*sqlite3_file pointer*/, n, offset64) {
xWrite: async function (fid, n, offset64) {
mTimeStart('xWrite');

@@ -791,9 +618,5 @@ let rc;

},
}; /*vfsAsyncImpls*/
};
const initS11n = () => {
/**
* ACHTUNG: this code is 100% duplicated in the other half of this
* proxy! The documentation is maintained in the "synchronous half".
*/
if (state.s11n) return state.s11n;

@@ -870,3 +693,2 @@ const textDecoder = new TextDecoder(),

} else {
/*String*/
n = viewDV.getInt32(offset, state.littleEndian);

@@ -881,3 +703,3 @@ offset += 4;

if (clear) viewU8[0] = 0;
//log("deserialize:",argc, rc);
metrics.s11n.deserialize.time += performance.now() - t;

@@ -890,10 +712,7 @@ return rc;

if (args.length) {
//log("serialize():",args);
const typeIds = [];
let i = 0,
offset = 1;
viewU8[0] = args.length & 0xff /* header = # of args */;
viewU8[0] = args.length & 0xff;
for (; i < args.length; ++i, ++offset) {
/* Write the TypeIds.id value into the next args.length
bytes. */
typeIds.push(getTypeId(args[i]));

@@ -903,4 +722,2 @@ viewU8[offset] = typeIds[i].id;

for (i = 0; i < args.length; ++i) {
/* Deserialize the following bytes based on their
corresponding TypeIds.id from the header. */
const t = typeIds[i];

@@ -911,3 +728,2 @@ if (t.setter) {

} else {
/*String*/
const s = textEncoder.encode(args[i]);

@@ -920,3 +736,2 @@ viewDV.setInt32(offset, s.byteLength, state.littleEndian);

}
//log("serialize() result:",viewU8.slice(0,offset));
} else {

@@ -937,3 +752,3 @@ viewU8[0] = 0;

return state.s11n;
}; /*initS11n()*/
};

@@ -961,17 +776,2 @@ const waitLoop = async function f() {

) {
/* Maintenance note: we compare against 'not-equal' because
https://github.com/tomayac/sqlite-wasm/issues/12
is reporting that this occassionally, under high loads,
returns 'ok', which leads to the whichOp being 0 (which
isn't a valid operation ID and leads to an exception,
along with a corresponding ugly console log
message). Unfortunately, the conditions for that cannot
be reliably reproduced. The only place in our code which
writes a 0 to the state.opIds.whichOp SharedArrayBuffer
index is a few lines down from here, and that instance
is required in order for clear communication between
the sync half of this proxy and this half.
*/
await releaseImplicitLocks();

@@ -984,9 +784,4 @@ continue;

opHandlers[opId] ?? toss('No waitLoop handler for whichOp #', opId);
const args =
state.s11n.deserialize(
true /* clear s11n to keep the caller from confusing this with
an exception string written by the upcoming
operation */,
) || [];
//warn("waitLoop() whichOp =",opId, hnd, args);
const args = state.s11n.deserialize(true) || [];
if (hnd.f) await hnd.f(...args);

@@ -1007,3 +802,2 @@ else error('Missing callback for opId', opId);

case 'opfs-async-init': {
/* Receive shared state from synchronous partner */
const opt = data.args;

@@ -1052,3 +846,3 @@ for (const k in opt) state[k] = opt[k];

.catch((e) => error('error initializing OPFS asyncer:', e));
}; /*installAsyncProxy()*/
};
if (!globalThis.SharedArrayBuffer) {

@@ -1075,3 +869,3 @@ wPost(

} else {
installAsyncProxy(self);
installAsyncProxy();
}

@@ -54,2 +54,3 @@ /*

let dbId;
let promiserFunc;
config.worker.onmessage = function (ev) {

@@ -61,3 +62,3 @@ ev = ev.data;

if (ev && 'sqlite3-api' === ev.type && 'worker1-ready' === ev.result) {
if (config.onready) config.onready();
if (config.onready) config.onready(promiserFunc);
return;

@@ -94,3 +95,3 @@ }

};
return function () {
return (promiserFunc = function () {
let msg;

@@ -105,3 +106,3 @@ if (1 === arguments.length) {

} else {
toss('Invalid arugments for sqlite3Worker1Promiser()-created factory.');
toss('Invalid arguments for sqlite3Worker1Promiser()-created factory.');
}

@@ -141,4 +142,5 @@ if (!msg.dbId && msg.type !== 'open') msg.dbId = dbId;

return p;
};
});
};
globalThis.sqlite3Worker1Promiser.defaultConfig = {

@@ -163,1 +165,35 @@ worker: function () {

};
sqlite3Worker1Promiser.v2 = function (config) {
let oldFunc;
if ('function' == typeof config) {
oldFunc = config;
config = {};
} else if ('function' === typeof config?.onready) {
oldFunc = config.onready;
delete config.onready;
}
const promiseProxy = Object.create(null);
config = Object.assign(config || Object.create(null), {
onready: async function (func) {
try {
if (oldFunc) await oldFunc(func);
promiseProxy.resolve(func);
} catch (e) {
promiseProxy.reject(e);
}
},
});
const p = new Promise(function (resolve, reject) {
promiseProxy.resolve = resolve;
promiseProxy.reject = reject;
});
try {
this.original(config);
} catch (e) {
promiseProxy.reject(e);
}
return p;
}.bind({
original: sqlite3Worker1Promiser,
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc