index.cjs 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var module$1 = require('module');
  4. var pathModule = require('path');
  5. var url = require('url');
  6. var loadNAPI = require('node-gyp-build-optional-packages');
  7. var events = require('events');
  8. var os$1 = require('os');
  9. var fs$1 = require('fs');
  10. var msgpackr = require('msgpackr');
  11. var weakLruCache = require('weak-lru-cache');
  12. var orderedBinary$1 = require('ordered-binary');
  13. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  14. function _interopNamespace(e) {
  15. if (e && e.__esModule) return e;
  16. var n = Object.create(null);
  17. if (e) {
  18. Object.keys(e).forEach(function (k) {
  19. if (k !== 'default') {
  20. var d = Object.getOwnPropertyDescriptor(e, k);
  21. Object.defineProperty(n, k, d.get ? d : {
  22. enumerable: true,
  23. get: function () { return e[k]; }
  24. });
  25. }
  26. });
  27. }
  28. n["default"] = e;
  29. return Object.freeze(n);
  30. }
  31. var pathModule__default = /*#__PURE__*/_interopDefaultLegacy(pathModule);
  32. var loadNAPI__default = /*#__PURE__*/_interopDefaultLegacy(loadNAPI);
  33. var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs$1);
  34. var orderedBinary__namespace = /*#__PURE__*/_interopNamespace(orderedBinary$1);
  35. let Env,
  36. Txn,
  37. Dbi,
  38. Compression,
  39. Cursor,
  40. getAddress,
  41. getBufferAddress;
  42. exports.clearKeptObjects = void 0;
  43. let globalBuffer,
  44. setGlobalBuffer,
  45. arch$1,
  46. fs,
  47. os,
  48. tmpdir,
  49. lmdbError,
  50. path,
  51. EventEmitter,
  52. orderedBinary,
  53. MsgpackrEncoder,
  54. WeakLRUCache,
  55. getByBinary,
  56. startRead,
  57. setReadCallback,
  58. write,
  59. position,
  60. iterate,
  61. prefetch,
  62. resetTxn,
  63. getCurrentValue,
  64. getStringByBinary,
  65. getSharedBuffer,
  66. compress,
  67. directWrite,
  68. getUserSharedBuffer,
  69. notifyUserCallbacks,
  70. attemptLock,
  71. unlock;
  72. exports.version = void 0;
  73. path = pathModule__default["default"];
  74. let dirName = pathModule.dirname(url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))).replace(/dist$/, '');
  75. let nativeAddon = loadNAPI__default["default"](dirName);
  76. if (process.isBun && false) {
  77. const { linkSymbols, FFIType } = require('bun:ffi');
  78. let lmdbLib = linkSymbols({
  79. getByBinary: {
  80. args: [FFIType.f64, FFIType.u32],
  81. returns: FFIType.u32,
  82. ptr: nativeAddon.getByBinaryPtr,
  83. },
  84. iterate: {
  85. args: [FFIType.f64],
  86. returns: FFIType.i32,
  87. ptr: nativeAddon.iteratePtr,
  88. },
  89. position: {
  90. args: [FFIType.f64, FFIType.u32, FFIType.u32, FFIType.u32, FFIType.f64],
  91. returns: FFIType.i32,
  92. ptr: nativeAddon.positionPtr,
  93. },
  94. write: {
  95. args: [FFIType.f64, FFIType.f64],
  96. returns: FFIType.i32,
  97. ptr: nativeAddon.writePtr,
  98. },
  99. resetTxn: {
  100. args: [FFIType.f64],
  101. returns: FFIType.void,
  102. ptr: nativeAddon.resetTxnPtr,
  103. },
  104. });
  105. for (let key in lmdbLib.symbols) {
  106. nativeAddon[key] = lmdbLib.symbols[key].native;
  107. }
  108. }
  109. setNativeFunctions(nativeAddon);
  110. function setNativeFunctions(externals) {
  111. Env = externals.Env;
  112. Txn = externals.Txn;
  113. Dbi = externals.Dbi;
  114. Compression = externals.Compression;
  115. getAddress = externals.getAddress;
  116. getBufferAddress = externals.getBufferAddress;
  117. externals.createBufferForAddress;
  118. exports.clearKeptObjects = externals.clearKeptObjects || function () {};
  119. getByBinary = externals.getByBinary;
  120. externals.detachBuffer;
  121. startRead = externals.startRead;
  122. setReadCallback = externals.setReadCallback;
  123. setGlobalBuffer = externals.setGlobalBuffer;
  124. globalBuffer = externals.globalBuffer;
  125. getSharedBuffer = externals.getSharedBuffer;
  126. prefetch = externals.prefetch;
  127. iterate = externals.iterate;
  128. position = externals.position;
  129. resetTxn = externals.resetTxn;
  130. directWrite = externals.directWrite;
  131. getUserSharedBuffer = externals.getUserSharedBuffer;
  132. notifyUserCallbacks = externals.notifyUserCallbacks;
  133. attemptLock = externals.attemptLock;
  134. unlock = externals.unlock;
  135. getCurrentValue = externals.getCurrentValue;
  136. externals.getCurrentShared;
  137. getStringByBinary = externals.getStringByBinary;
  138. externals.getSharedByBinary;
  139. write = externals.write;
  140. compress = externals.compress;
  141. Cursor = externals.Cursor;
  142. lmdbError = externals.lmdbError;
  143. exports.version = externals.version;
  144. if (externals.tmpdir) tmpdir = externals.tmpdir;
  145. }
  146. function setExternals(externals) {
  147. arch$1 = externals.arch;
  148. fs = externals.fs;
  149. EventEmitter = externals.EventEmitter;
  150. orderedBinary = externals.orderedBinary;
  151. MsgpackrEncoder = externals.MsgpackrEncoder;
  152. WeakLRUCache = externals.WeakLRUCache;
  153. tmpdir = externals.tmpdir;
  154. os = externals.os;
  155. externals.onExit;
  156. }
  157. function when(promise, callback, errback) {
  158. if (promise && promise.then) {
  159. return errback ?
  160. promise.then(callback, errback) :
  161. promise.then(callback);
  162. }
  163. return callback(promise);
  164. }
  165. var backpressureArray;
  166. const WAITING_OPERATION = 0x2000000;
  167. const BACKPRESSURE_THRESHOLD = 300000;
  168. const TXN_DELIMITER = 0x8000000;
  169. const TXN_COMMITTED = 0x10000000;
  170. const TXN_FLUSHED = 0x20000000;
  171. const TXN_FAILED = 0x40000000;
  172. const FAILED_CONDITION = 0x4000000;
  173. const REUSE_BUFFER_MODE$1 = 512;
  174. const RESET_BUFFER_MODE = 1024;
  175. const NO_RESOLVE = 16;
  176. const HAS_TXN = 8;
  177. const CONDITIONAL_VERSION_LESS_THAN = 0x800;
  178. const CONDITIONAL_ALLOW_NOTFOUND = 0x800;
  179. const SYNC_PROMISE_SUCCESS = Promise.resolve(true);
  180. const SYNC_PROMISE_FAIL = Promise.resolve(false);
  181. SYNC_PROMISE_SUCCESS.isSync = true;
  182. SYNC_PROMISE_SUCCESS.result = true;
  183. SYNC_PROMISE_FAIL.isSync = true;
  184. SYNC_PROMISE_FAIL.result = false;
  185. const PROMISE_SUCCESS = Promise.resolve(true);
  186. const arch = process.arch;
  187. const ABORT = 4.452694326329068e-106; // random/unguessable numbers, which work across module/versions and native
  188. const IF_EXISTS$1 = 3.542694326329068e-103;
  189. const CALLBACK_THREW = {};
  190. const LocalSharedArrayBuffer =
  191. typeof Deno != 'undefined' || // Deno can't handle SharedArrayBuffer as an FFI
  192. // argument due to https://github.com/denoland/deno/issues/12678
  193. typeof SharedArrayBuffer == 'undefined' // Sometimes electron doesn't have a SharedArrayBuffer
  194. ? ArrayBuffer
  195. : SharedArrayBuffer;
  196. const ByteArray =
  197. typeof Buffer != 'undefined'
  198. ? function (buffer) {
  199. return Buffer.from(buffer);
  200. }
  201. : Uint8Array;
  202. const queueTask =
  203. typeof setImmediate != 'undefined' ? setImmediate : setTimeout; // TODO: Or queueMicrotask?
  204. //let debugLog = []
  205. const WRITE_BUFFER_SIZE = 0x10000;
  206. function addWriteMethods(
  207. LMDBStore,
  208. {
  209. env,
  210. fixedBuffer,
  211. resetReadTxn,
  212. useWritemap,
  213. maxKeySize,
  214. eventTurnBatching,
  215. txnStartThreshold,
  216. batchStartThreshold,
  217. overlappingSync,
  218. commitDelay,
  219. separateFlushed,
  220. maxFlushDelay,
  221. },
  222. ) {
  223. // stands for write instructions
  224. var dynamicBytes;
  225. function allocateInstructionBuffer(lastPosition) {
  226. // Must use a shared buffer on older node in order to use Atomics, and it is also more correct since we are
  227. // indeed accessing and modifying it from another thread (in C). However, Deno can't handle it for
  228. // FFI so aliased above
  229. let buffer = new LocalSharedArrayBuffer(WRITE_BUFFER_SIZE);
  230. let lastBytes = dynamicBytes;
  231. dynamicBytes = new ByteArray(buffer);
  232. let uint32 = (dynamicBytes.uint32 = new Uint32Array(
  233. buffer,
  234. 0,
  235. WRITE_BUFFER_SIZE >> 2,
  236. ));
  237. uint32[2] = 0;
  238. dynamicBytes.float64 = new Float64Array(buffer, 0, WRITE_BUFFER_SIZE >> 3);
  239. buffer.address = getBufferAddress(dynamicBytes);
  240. uint32.address = buffer.address + uint32.byteOffset;
  241. dynamicBytes.position = 1; // we start at position 1 to save space for writing the txn id before the txn delimiter
  242. if (lastPosition) {
  243. lastBytes.float64[lastPosition + 1] =
  244. dynamicBytes.uint32.address + (dynamicBytes.position << 3);
  245. lastBytes.uint32[lastPosition << 1] = 3; // pointer instruction
  246. }
  247. return dynamicBytes;
  248. }
  249. var newBufferThreshold = (WRITE_BUFFER_SIZE - maxKeySize - 64) >> 3; // need to reserve more room if we do inline values
  250. var outstandingWriteCount = 0;
  251. var startAddress = 0;
  252. var writeTxn = null;
  253. var committed;
  254. var abortedNonChildTransactionWarn;
  255. var nextTxnCallbacks = [];
  256. var commitPromise,
  257. flushPromise,
  258. flushResolvers = [],
  259. batchFlushResolvers = [];
  260. commitDelay = commitDelay || 0;
  261. eventTurnBatching = eventTurnBatching === false ? false : true;
  262. var enqueuedCommit;
  263. var afterCommitCallbacks = [];
  264. var beforeCommitCallbacks = [];
  265. var enqueuedEventTurnBatch;
  266. var batchDepth = 0;
  267. var lastWritePromise;
  268. var writeBatchStart,
  269. outstandingBatchCount,
  270. lastSyncTxnFlush;
  271. var hasUnresolvedTxns;
  272. txnStartThreshold = txnStartThreshold || 5;
  273. batchStartThreshold = batchStartThreshold || 1000;
  274. maxFlushDelay = maxFlushDelay || 500;
  275. allocateInstructionBuffer();
  276. dynamicBytes.uint32[2] = TXN_DELIMITER | TXN_COMMITTED | TXN_FLUSHED;
  277. var txnResolution,
  278. nextResolution = {
  279. uint32: dynamicBytes.uint32,
  280. flagPosition: 2,
  281. flag: 0,
  282. valueBuffer: null,
  283. next: null,
  284. meta: null,
  285. };
  286. var uncommittedResolution = {
  287. uint32: null,
  288. flagPosition: 2,
  289. flag: 0,
  290. valueBuffer: null,
  291. next: nextResolution,
  292. meta: null,
  293. };
  294. var unwrittenResolution = nextResolution;
  295. var lastPromisedResolution = uncommittedResolution;
  296. var lastQueuedResolution = uncommittedResolution;
  297. function writeInstructions(flags, store, key, value, version, ifVersion) {
  298. let writeStatus;
  299. let targetBytes, position, encoder;
  300. let valueSize, valueBuffer, valueBufferStart;
  301. if (flags & 2) {
  302. // encode first in case we have to write a shared structure
  303. encoder = store.encoder;
  304. if (value && value['\x10binary-data\x02'])
  305. valueBuffer = value['\x10binary-data\x02'];
  306. else if (encoder) {
  307. if (encoder.copyBuffers)
  308. // use this as indicator for support buffer reuse for now
  309. valueBuffer = encoder.encode(
  310. value,
  311. REUSE_BUFFER_MODE$1 | (writeTxn ? RESET_BUFFER_MODE : 0),
  312. );
  313. // in addition, if we are writing sync, after using, we can immediately reset the encoder's position to reuse that space, which can improve performance
  314. else {
  315. // various other encoders, including JSON.stringify, that might serialize to a string
  316. valueBuffer = encoder.encode(value);
  317. if (typeof valueBuffer == 'string')
  318. valueBuffer = Buffer.from(valueBuffer); // TODO: Would be nice to write strings inline in the instructions
  319. }
  320. } else if (typeof value == 'string') {
  321. valueBuffer = Buffer.from(value); // TODO: Would be nice to write strings inline in the instructions
  322. } else if (value instanceof Uint8Array) valueBuffer = value;
  323. else
  324. throw new Error(
  325. 'Invalid value to put in database ' +
  326. value +
  327. ' (' +
  328. typeof value +
  329. '), consider using encoder',
  330. );
  331. valueBufferStart = valueBuffer.start;
  332. if (valueBufferStart > -1)
  333. // if we have buffers with start/end position
  334. valueSize = valueBuffer.end - valueBufferStart; // size
  335. else valueSize = valueBuffer.length;
  336. if (store.dupSort && valueSize > maxKeySize)
  337. throw new Error(
  338. 'The value is larger than the maximum size (' +
  339. maxKeySize +
  340. ') for a value in a dupSort database',
  341. );
  342. } else valueSize = 0;
  343. if (writeTxn) {
  344. targetBytes = fixedBuffer;
  345. position = 0;
  346. } else {
  347. if (eventTurnBatching && !enqueuedEventTurnBatch && batchDepth == 0) {
  348. enqueuedEventTurnBatch = queueTask(() => {
  349. try {
  350. for (let i = 0, l = beforeCommitCallbacks.length; i < l; i++) {
  351. try {
  352. beforeCommitCallbacks[i]();
  353. } catch (error) {
  354. console.error('In beforecommit callback', error);
  355. }
  356. }
  357. } catch (error) {
  358. console.error(error);
  359. }
  360. enqueuedEventTurnBatch = null;
  361. batchDepth--;
  362. finishBatch();
  363. if (writeBatchStart) writeBatchStart(); // TODO: When we support delay start of batch, optionally don't delay this
  364. });
  365. commitPromise = null; // reset the commit promise, can't know if it is really a new transaction prior to finishWrite being called
  366. flushPromise = null;
  367. writeBatchStart = writeInstructions(1, store);
  368. outstandingBatchCount = 0;
  369. batchDepth++;
  370. }
  371. targetBytes = dynamicBytes;
  372. position = targetBytes.position;
  373. }
  374. let uint32 = targetBytes.uint32,
  375. float64 = targetBytes.float64;
  376. let flagPosition = position << 1; // flagPosition is the 32-bit word starting position
  377. // don't increment position until we are sure we don't have any key writing errors
  378. if (!uint32) {
  379. throw new Error('Internal buffers have been corrupted');
  380. }
  381. uint32[flagPosition + 1] = store.db.dbi;
  382. if (flags & 4) {
  383. let keyStartPosition = (position << 3) + 12;
  384. let endPosition;
  385. try {
  386. endPosition = store.writeKey(key, targetBytes, keyStartPosition);
  387. if (!(keyStartPosition < endPosition) && (flags & 0xf) != 12)
  388. throw new Error(
  389. 'Invalid key or zero length key is not allowed in LMDB ' + key,
  390. );
  391. } catch (error) {
  392. targetBytes.fill(0, keyStartPosition);
  393. if (error.name == 'RangeError')
  394. error = new Error(
  395. 'Key size is larger than the maximum key size (' + maxKeySize + ')',
  396. );
  397. throw error;
  398. }
  399. let keySize = endPosition - keyStartPosition;
  400. if (keySize > maxKeySize) {
  401. targetBytes.fill(0, keyStartPosition); // restore zeros
  402. throw new Error(
  403. 'Key size is larger than the maximum key size (' + maxKeySize + ')',
  404. );
  405. }
  406. uint32[flagPosition + 2] = keySize;
  407. position = (endPosition + 16) >> 3;
  408. if (flags & 2) {
  409. let mustCompress;
  410. if (valueBufferStart > -1) {
  411. // if we have buffers with start/end position
  412. // record pointer to value buffer
  413. float64[position] =
  414. (valueBuffer.address ||
  415. (valueBuffer.address = getAddress(valueBuffer.buffer))) +
  416. valueBufferStart;
  417. if (store.compression) {
  418. let compressionFlagIndex =
  419. valueBufferStart + (store.compression.startingOffset || 0);
  420. // this is the compression indicator, so we must compress
  421. mustCompress =
  422. compressionFlagIndex < valueBuffer.end &&
  423. valueBuffer[compressionFlagIndex] >= 250;
  424. }
  425. } else {
  426. let valueArrayBuffer = valueBuffer.buffer;
  427. // record pointer to value buffer
  428. let address =
  429. (valueArrayBuffer.address ||
  430. (valueBuffer.length === 0
  431. ? 0 // externally allocated buffers of zero-length with the same non-null-pointer can crash node, #161
  432. : (valueArrayBuffer.address = getAddress(valueArrayBuffer)))) +
  433. valueBuffer.byteOffset;
  434. if (address <= 0 && valueBuffer.length > 0)
  435. console.error('Supplied buffer had an invalid address', address);
  436. float64[position] = address;
  437. if (store.compression) {
  438. let compressionFlagIndex = store.compression.startingOffset || 0;
  439. // this is the compression indicator, so we must compress
  440. mustCompress =
  441. compressionFlagIndex < valueBuffer.length &&
  442. valueBuffer[compressionFlagIndex] >= 250;
  443. }
  444. }
  445. uint32[(position++ << 1) - 1] = valueSize;
  446. if (
  447. store.compression &&
  448. (valueSize >= store.compression.threshold || mustCompress)
  449. ) {
  450. flags |= 0x100000;
  451. float64[position] = store.compression.address;
  452. if (!writeTxn)
  453. compress(env.address, uint32.address + (position << 3), () => {
  454. // this is never actually called in NodeJS, just use to pin the buffer in memory until it is finished
  455. // and is a no-op in Deno
  456. if (!float64) throw new Error('No float64 available');
  457. });
  458. position++;
  459. }
  460. }
  461. if (ifVersion !== undefined) {
  462. if (ifVersion === null)
  463. flags |= 0x10; // if it does not exist, MDB_NOOVERWRITE
  464. else {
  465. flags |= 0x100;
  466. float64[position++] = ifVersion;
  467. }
  468. }
  469. if (version !== undefined) {
  470. flags |= 0x200;
  471. float64[position++] = version || 0;
  472. }
  473. } else position++;
  474. targetBytes.position = position;
  475. if (writeTxn) {
  476. uint32[0] = flags;
  477. write(env.address, uint32.address);
  478. return () =>
  479. uint32[0] & FAILED_CONDITION ? SYNC_PROMISE_FAIL : SYNC_PROMISE_SUCCESS;
  480. }
  481. // if we ever use buffers that haven't been zero'ed, need to clear out the next slot like this:
  482. // uint32[position << 1] = 0 // clear out the next slot
  483. let nextUint32;
  484. if (position > newBufferThreshold) {
  485. targetBytes = allocateInstructionBuffer(position);
  486. position = targetBytes.position;
  487. nextUint32 = targetBytes.uint32;
  488. } else nextUint32 = uint32;
  489. let resolution = nextResolution;
  490. // create the placeholder next resolution
  491. nextResolution = resolution.next = {
  492. // we try keep resolutions exactly the same object type
  493. uint32: nextUint32,
  494. flagPosition: position << 1,
  495. flag: 0, // TODO: eventually eliminate this, as we can probably signify HAS_TXN/NO_RESOLVE/FAILED_CONDITION in upper bits
  496. valueBuffer: fixedBuffer, // these are all just placeholders so that we have the right hidden class initially allocated
  497. next: null,
  498. meta: null,
  499. };
  500. lastQueuedResolution = resolution;
  501. let writtenBatchDepth = batchDepth;
  502. return (callback) => {
  503. if (writtenBatchDepth) {
  504. // If we are in a batch, the transaction can't close, so we do the faster,
  505. // but non-deterministic updates, knowing that the write thread can
  506. // just poll for the status change if we miss a status update.
  507. // That is, if we are on x64 architecture...
  508. if (arch === 'x64') {
  509. writeStatus = uint32[flagPosition];
  510. uint32[flagPosition] = flags;
  511. } else {
  512. // However, on ARM processors, apparently more radical memory reordering can occur
  513. // so we need to use the slower atomic operation to ensure that a memory barrier is set
  514. // and that the value pointer is actually written before the flag is updated
  515. writeStatus = Atomics.or(uint32, flagPosition, flags);
  516. }
  517. if (writeBatchStart && !writeStatus) {
  518. outstandingBatchCount += 1 + (valueSize >> 12);
  519. if (outstandingBatchCount > batchStartThreshold) {
  520. outstandingBatchCount = 0;
  521. writeBatchStart();
  522. writeBatchStart = null;
  523. }
  524. }
  525. } // otherwise the transaction could end at any time and we need to know the
  526. // deterministically if it is ending, so we can reset the commit promise
  527. // so we use the slower atomic operation
  528. else writeStatus = Atomics.or(uint32, flagPosition, flags);
  529. outstandingWriteCount++;
  530. if (writeStatus & TXN_DELIMITER) {
  531. commitPromise = null; // TODO: Don't reset these if this comes from the batch start operation on an event turn batch
  532. flushPromise = null;
  533. flushResolvers = [];
  534. queueCommitResolution(resolution);
  535. if (!startAddress) {
  536. startAddress = uint32.address + (flagPosition << 2);
  537. }
  538. }
  539. if (!writtenBatchDepth && batchFlushResolvers.length > 0) {
  540. flushResolvers.push(...batchFlushResolvers);
  541. batchFlushResolvers = [];
  542. }
  543. if (!flushPromise && overlappingSync) {
  544. flushPromise = new Promise((resolve) => {
  545. if (writtenBatchDepth) {
  546. batchFlushResolvers.push(resolve);
  547. } else {
  548. flushResolvers.push(resolve);
  549. }
  550. });
  551. }
  552. if (writeStatus & WAITING_OPERATION) {
  553. // write thread is waiting
  554. write(env.address, 0);
  555. }
  556. if (outstandingWriteCount > BACKPRESSURE_THRESHOLD && !writeBatchStart) {
  557. if (!backpressureArray)
  558. backpressureArray = new Int32Array(new SharedArrayBuffer(4), 0, 1);
  559. Atomics.wait(
  560. backpressureArray,
  561. 0,
  562. 0,
  563. Math.round(outstandingWriteCount / BACKPRESSURE_THRESHOLD),
  564. );
  565. }
  566. if (startAddress) {
  567. if (eventTurnBatching)
  568. startWriting(); // start writing immediately because this has already been batched/queued
  569. else if (!enqueuedCommit && txnStartThreshold) {
  570. enqueuedCommit =
  571. commitDelay == 0 && typeof setImmediate != 'undefined'
  572. ? setImmediate(() => startWriting())
  573. : setTimeout(() => startWriting(), commitDelay);
  574. } else if (outstandingWriteCount > txnStartThreshold) startWriting();
  575. }
  576. if ((outstandingWriteCount & 7) === 0) resolveWrites();
  577. if (store.cache) {
  578. resolution.meta = {
  579. key,
  580. store,
  581. valueSize: valueBuffer ? valueBuffer.length : 0,
  582. };
  583. }
  584. resolution.valueBuffer = valueBuffer;
  585. if (callback) {
  586. if (callback === IF_EXISTS$1) ifVersion = IF_EXISTS$1;
  587. else {
  588. let meta = resolution.meta || (resolution.meta = {});
  589. meta.reject = callback;
  590. meta.resolve = (value) => callback(null, value);
  591. return;
  592. }
  593. }
  594. // if it is not conditional because of ifVersion or has any flags that can make the write conditional
  595. if (ifVersion === undefined && !(flags & 0x22030)) {
  596. if (writtenBatchDepth > 1) {
  597. if (!resolution.flag && !store.cache) resolution.flag = NO_RESOLVE;
  598. return PROMISE_SUCCESS; // or return undefined?
  599. }
  600. if (commitPromise) {
  601. if (!resolution.flag) resolution.flag = NO_RESOLVE;
  602. } else {
  603. commitPromise = new Promise((resolve, reject) => {
  604. let meta = resolution.meta || (resolution.meta = {});
  605. meta.resolve = resolve;
  606. resolve.unconditional = true;
  607. meta.reject = reject;
  608. });
  609. if (separateFlushed)
  610. commitPromise.flushed = overlappingSync
  611. ? flushPromise
  612. : commitPromise;
  613. }
  614. return commitPromise;
  615. }
  616. lastWritePromise = new Promise((resolve, reject) => {
  617. let meta = resolution.meta || (resolution.meta = {});
  618. meta.resolve = resolve;
  619. meta.reject = reject;
  620. });
  621. if (separateFlushed)
  622. lastWritePromise.flushed = overlappingSync
  623. ? flushPromise
  624. : lastWritePromise;
  625. return lastWritePromise;
  626. };
  627. }
  628. Promise.resolve();
  629. function startWriting() {
  630. if (enqueuedCommit) {
  631. clearImmediate(enqueuedCommit);
  632. enqueuedCommit = null;
  633. }
  634. let resolvers = flushResolvers;
  635. env.startWriting(startAddress, (status) => {
  636. if (dynamicBytes.uint32[dynamicBytes.position << 1] & TXN_DELIMITER)
  637. queueCommitResolution(nextResolution);
  638. resolveWrites(true);
  639. switch (status) {
  640. case 0:
  641. for (let resolver of resolvers) {
  642. resolver();
  643. }
  644. break;
  645. case 1:
  646. break;
  647. case 2:
  648. hasUnresolvedTxns = false;
  649. executeTxnCallbacks();
  650. return hasUnresolvedTxns;
  651. default:
  652. try {
  653. lmdbError(status);
  654. } catch (error) {
  655. console.error(error);
  656. if (commitRejectPromise) {
  657. commitRejectPromise.reject(error);
  658. commitRejectPromise = null;
  659. }
  660. }
  661. }
  662. });
  663. startAddress = 0;
  664. }
  665. function queueCommitResolution(resolution) {
  666. if (!(resolution.flag & HAS_TXN)) {
  667. resolution.flag = HAS_TXN;
  668. if (txnResolution) {
  669. txnResolution.nextTxn = resolution;
  670. //outstandingWriteCount = 0
  671. } else txnResolution = resolution;
  672. }
  673. }
  674. var TXN_DONE = TXN_COMMITTED | TXN_FAILED;
  675. function resolveWrites(async) {
  676. // clean up finished instructions
  677. let instructionStatus;
  678. while (
  679. (instructionStatus =
  680. unwrittenResolution.uint32[unwrittenResolution.flagPosition]) &
  681. 0x1000000
  682. ) {
  683. if (unwrittenResolution.callbacks) {
  684. nextTxnCallbacks.push(unwrittenResolution.callbacks);
  685. unwrittenResolution.callbacks = null;
  686. }
  687. outstandingWriteCount--;
  688. if (unwrittenResolution.flag !== HAS_TXN) {
  689. if (
  690. unwrittenResolution.flag === NO_RESOLVE &&
  691. !unwrittenResolution.meta
  692. ) {
  693. // in this case we can completely remove from the linked list, clearing more memory
  694. lastPromisedResolution.next = unwrittenResolution =
  695. unwrittenResolution.next;
  696. continue;
  697. }
  698. unwrittenResolution.uint32 = null;
  699. }
  700. unwrittenResolution.valueBuffer = null;
  701. unwrittenResolution.flag = instructionStatus;
  702. lastPromisedResolution = unwrittenResolution;
  703. unwrittenResolution = unwrittenResolution.next;
  704. }
  705. while (
  706. txnResolution &&
  707. (instructionStatus =
  708. txnResolution.uint32[txnResolution.flagPosition] & TXN_DONE)
  709. ) {
  710. if (instructionStatus & TXN_FAILED) rejectCommit();
  711. else resolveCommit(async);
  712. }
  713. }
  714. function resolveCommit(async) {
  715. afterCommit(txnResolution.uint32[txnResolution.flagPosition - 1]);
  716. if (async) resetReadTxn();
  717. else queueMicrotask(resetReadTxn); // TODO: only do this if there are actually committed writes?
  718. do {
  719. if (uncommittedResolution.meta && uncommittedResolution.meta.resolve) {
  720. let resolve = uncommittedResolution.meta.resolve;
  721. if (
  722. uncommittedResolution.flag & FAILED_CONDITION &&
  723. !resolve.unconditional
  724. )
  725. resolve(false);
  726. else resolve(true);
  727. }
  728. } while (
  729. (uncommittedResolution = uncommittedResolution.next) &&
  730. uncommittedResolution != txnResolution
  731. );
  732. txnResolution = txnResolution.nextTxn;
  733. }
  734. var commitRejectPromise;
  735. function rejectCommit() {
  736. afterCommit();
  737. if (!commitRejectPromise) {
  738. let rejectFunction;
  739. commitRejectPromise = new Promise(
  740. (resolve, reject) => (rejectFunction = reject),
  741. );
  742. commitRejectPromise.reject = rejectFunction;
  743. }
  744. do {
  745. if (uncommittedResolution.meta && uncommittedResolution.meta.reject) {
  746. uncommittedResolution.flag & 0xf;
  747. let error = new Error('Commit failed (see commitError for details)');
  748. error.commitError = commitRejectPromise;
  749. uncommittedResolution.meta.reject(error);
  750. }
  751. } while (
  752. (uncommittedResolution = uncommittedResolution.next) &&
  753. uncommittedResolution != txnResolution
  754. );
  755. txnResolution = txnResolution.nextTxn;
  756. }
  757. function atomicStatus(uint32, flagPosition, newStatus) {
  758. if (batchDepth) {
  759. // if we are in a batch, the transaction can't close, so we do the faster,
  760. // but non-deterministic updates, knowing that the write thread can
  761. // just poll for the status change if we miss a status update
  762. let writeStatus = uint32[flagPosition];
  763. uint32[flagPosition] = newStatus;
  764. return writeStatus;
  765. //return Atomics.or(uint32, flagPosition, newStatus)
  766. } // otherwise the transaction could end at any time and we need to know the
  767. // deterministically if it is ending, so we can reset the commit promise
  768. // so we use the slower atomic operation
  769. else
  770. try {
  771. return Atomics.or(uint32, flagPosition, newStatus);
  772. } catch (error) {
  773. console.error(error);
  774. return;
  775. }
  776. }
  777. function afterCommit(txnId) {
  778. for (let i = 0, l = afterCommitCallbacks.length; i < l; i++) {
  779. try {
  780. afterCommitCallbacks[i]({
  781. next: uncommittedResolution,
  782. last: txnResolution,
  783. txnId,
  784. });
  785. } catch (error) {
  786. console.error('In aftercommit callback', error);
  787. }
  788. }
  789. }
  790. async function executeTxnCallbacks() {
  791. env.writeTxn = writeTxn = { write: true };
  792. nextTxnCallbacks.isExecuting = true;
  793. for (let i = 0; i < nextTxnCallbacks.length; i++) {
  794. let txnCallbacks = nextTxnCallbacks[i];
  795. for (let j = 0, l = txnCallbacks.length; j < l; j++) {
  796. let userTxnCallback = txnCallbacks[j];
  797. let asChild = userTxnCallback.asChild;
  798. if (asChild) {
  799. env.beginTxn(1); // abortable
  800. let parentTxn = writeTxn;
  801. env.writeTxn = writeTxn = { write: true };
  802. try {
  803. let result = userTxnCallback.callback();
  804. if (result && result.then) {
  805. hasUnresolvedTxns = true;
  806. await result;
  807. }
  808. if (result === ABORT) env.abortTxn();
  809. else env.commitTxn();
  810. clearWriteTxn(parentTxn);
  811. txnCallbacks[j] = result;
  812. } catch (error) {
  813. clearWriteTxn(parentTxn);
  814. env.abortTxn();
  815. txnError(error, txnCallbacks, j);
  816. }
  817. } else {
  818. try {
  819. let result = userTxnCallback();
  820. txnCallbacks[j] = result;
  821. if (result && result.then) {
  822. hasUnresolvedTxns = true;
  823. await result;
  824. }
  825. } catch (error) {
  826. txnError(error, txnCallbacks, j);
  827. }
  828. }
  829. }
  830. }
  831. nextTxnCallbacks = [];
  832. clearWriteTxn(null);
  833. if (hasUnresolvedTxns) {
  834. env.resumeWriting();
  835. }
  836. function txnError(error, txnCallbacks, i) {
  837. (txnCallbacks.errors || (txnCallbacks.errors = []))[i] = error;
  838. txnCallbacks[i] = CALLBACK_THREW;
  839. }
  840. }
  841. function finishBatch() {
  842. let bytes = dynamicBytes;
  843. let uint32 = bytes.uint32;
  844. let nextPosition = bytes.position + 1;
  845. let writeStatus;
  846. if (nextPosition > newBufferThreshold) {
  847. allocateInstructionBuffer(nextPosition);
  848. nextResolution.flagPosition = dynamicBytes.position << 1;
  849. nextResolution.uint32 = dynamicBytes.uint32;
  850. writeStatus = atomicStatus(uint32, bytes.position << 1, 2); // atomically write the end block
  851. } else {
  852. uint32[nextPosition << 1] = 0; // clear out the next slot
  853. writeStatus = atomicStatus(uint32, bytes.position++ << 1, 2); // atomically write the end block
  854. nextResolution.flagPosition += 2;
  855. }
  856. if (writeStatus & WAITING_OPERATION) {
  857. write(env.address, 0);
  858. }
  859. }
  860. function clearWriteTxn(parentTxn) {
  861. // TODO: We might actually want to track cursors in a write txn and manually
  862. // close them.
  863. if (writeTxn && writeTxn.refCount > 0) writeTxn.isDone = true;
  864. env.writeTxn = writeTxn = parentTxn || null;
  865. }
  866. Object.assign(LMDBStore.prototype, {
  867. put(key, value, versionOrOptions, ifVersion) {
  868. let callback,
  869. flags = 15,
  870. type = typeof versionOrOptions;
  871. if (type == 'object' && versionOrOptions) {
  872. if (versionOrOptions.noOverwrite) flags |= 0x10;
  873. if (versionOrOptions.noDupData) flags |= 0x20;
  874. if (versionOrOptions.instructedWrite) flags |= 0x2000;
  875. if (versionOrOptions.append) flags |= 0x20000;
  876. if (versionOrOptions.ifVersion != undefined)
  877. ifVersion = versionOrOptions.ifVersion;
  878. versionOrOptions = versionOrOptions.version;
  879. if (typeof ifVersion == 'function') callback = ifVersion;
  880. } else if (type == 'function') {
  881. callback = versionOrOptions;
  882. }
  883. return writeInstructions(
  884. flags,
  885. this,
  886. key,
  887. value,
  888. this.useVersions ? versionOrOptions || 0 : undefined,
  889. ifVersion,
  890. )(callback);
  891. },
  892. remove(key, ifVersionOrValue, callback) {
  893. let flags = 13;
  894. let ifVersion, value;
  895. if (ifVersionOrValue !== undefined) {
  896. if (typeof ifVersionOrValue == 'function') callback = ifVersionOrValue;
  897. else if (ifVersionOrValue === IF_EXISTS$1 && !callback)
  898. // we have a handler for IF_EXISTS in the callback handler for remove
  899. callback = ifVersionOrValue;
  900. else if (this.useVersions) ifVersion = ifVersionOrValue;
  901. else {
  902. flags = 14;
  903. value = ifVersionOrValue;
  904. }
  905. }
  906. return writeInstructions(
  907. flags,
  908. this,
  909. key,
  910. value,
  911. undefined,
  912. ifVersion,
  913. )(callback);
  914. },
  915. del(key, options, callback) {
  916. return this.remove(key, options, callback);
  917. },
  918. ifNoExists(key, callback) {
  919. return this.ifVersion(key, null, callback);
  920. },
  921. ifVersion(key, version, callback, options) {
  922. if (!callback) {
  923. return new Batch((operations, callback) => {
  924. let promise = this.ifVersion(key, version, operations, options);
  925. if (callback) promise.then(callback);
  926. return promise;
  927. });
  928. }
  929. if (writeTxn) {
  930. if (version === undefined || this.doesExist(key, version)) {
  931. callback();
  932. return SYNC_PROMISE_SUCCESS;
  933. }
  934. return SYNC_PROMISE_FAIL;
  935. }
  936. let flags = key === undefined || version === undefined ? 1 : 4;
  937. if (options?.ifLessThan) flags |= CONDITIONAL_VERSION_LESS_THAN;
  938. if (options?.allowNotFound) flags |= CONDITIONAL_ALLOW_NOTFOUND;
  939. let finishStartWrite = writeInstructions(
  940. flags,
  941. this,
  942. key,
  943. undefined,
  944. undefined,
  945. version,
  946. );
  947. let promise;
  948. batchDepth += 2;
  949. if (batchDepth > 2) promise = finishStartWrite();
  950. else {
  951. writeBatchStart = () => {
  952. promise = finishStartWrite();
  953. };
  954. outstandingBatchCount = 0;
  955. }
  956. try {
  957. if (typeof callback === 'function') {
  958. callback();
  959. } else {
  960. for (let i = 0, l = callback.length; i < l; i++) {
  961. let operation = callback[i];
  962. this[operation.type](operation.key, operation.value);
  963. }
  964. }
  965. } finally {
  966. if (!promise) {
  967. finishBatch();
  968. batchDepth -= 2;
  969. promise = finishStartWrite(); // finish write once all the operations have been written (and it hasn't been written prematurely)
  970. writeBatchStart = null;
  971. } else {
  972. batchDepth -= 2;
  973. finishBatch();
  974. }
  975. }
  976. return promise;
  977. },
  978. batch(callbackOrOperations) {
  979. return this.ifVersion(undefined, undefined, callbackOrOperations);
  980. },
  981. drop(callback) {
  982. return writeInstructions(
  983. 1024 + 12,
  984. this,
  985. Buffer.from([]),
  986. undefined,
  987. undefined,
  988. undefined,
  989. )(callback);
  990. },
  991. clearAsync(callback) {
  992. if (this.encoder) {
  993. if (this.encoder.clearSharedData) this.encoder.clearSharedData();
  994. else if (this.encoder.structures) this.encoder.structures = [];
  995. }
  996. return writeInstructions(
  997. 12,
  998. this,
  999. Buffer.from([]),
  1000. undefined,
  1001. undefined,
  1002. undefined,
  1003. )(callback);
  1004. },
  1005. _triggerError() {
  1006. finishBatch();
  1007. },
  1008. putSync(key, value, versionOrOptions, ifVersion) {
  1009. if (writeTxn)
  1010. return (
  1011. this.put(key, value, versionOrOptions, ifVersion) ===
  1012. SYNC_PROMISE_SUCCESS
  1013. );
  1014. else
  1015. return this.transactionSync(
  1016. () =>
  1017. this.put(key, value, versionOrOptions, ifVersion) ===
  1018. SYNC_PROMISE_SUCCESS,
  1019. overlappingSync ? 0x10002 : 2,
  1020. ); // non-abortable, async flush
  1021. },
  1022. removeSync(key, ifVersionOrValue) {
  1023. if (writeTxn)
  1024. return this.remove(key, ifVersionOrValue) === SYNC_PROMISE_SUCCESS;
  1025. else
  1026. return this.transactionSync(
  1027. () => this.remove(key, ifVersionOrValue) === SYNC_PROMISE_SUCCESS,
  1028. overlappingSync ? 0x10002 : 2,
  1029. ); // non-abortable, async flush
  1030. },
  1031. transaction(callback) {
  1032. if (writeTxn && !nextTxnCallbacks.isExecuting) {
  1033. // already nested in a transaction, just execute and return
  1034. return callback();
  1035. }
  1036. return this.transactionAsync(callback);
  1037. },
  1038. childTransaction(callback) {
  1039. if (useWritemap)
  1040. throw new Error(
  1041. 'Child transactions are not supported in writemap mode',
  1042. );
  1043. if (writeTxn) {
  1044. let parentTxn = writeTxn;
  1045. let thisTxn = (env.writeTxn = writeTxn = { write: true });
  1046. env.beginTxn(1); // abortable
  1047. let callbackDone, finishTxn;
  1048. try {
  1049. return (writeTxn.childResults = when(
  1050. callback(),
  1051. (finishTxn = (result) => {
  1052. if (writeTxn !== thisTxn)
  1053. // need to wait for child txn to finish asynchronously
  1054. return writeTxn.childResults.then(() => finishTxn(result));
  1055. callbackDone = true;
  1056. if (result === ABORT) env.abortTxn();
  1057. else env.commitTxn();
  1058. clearWriteTxn(parentTxn);
  1059. return result;
  1060. }),
  1061. (error) => {
  1062. env.abortTxn();
  1063. clearWriteTxn(parentTxn);
  1064. throw error;
  1065. },
  1066. ));
  1067. } catch (error) {
  1068. if (!callbackDone) env.abortTxn();
  1069. clearWriteTxn(parentTxn);
  1070. throw error;
  1071. }
  1072. }
  1073. return this.transactionAsync(callback, true);
  1074. },
  1075. transactionAsync(callback, asChild) {
  1076. let txnIndex;
  1077. let txnCallbacks;
  1078. if (lastQueuedResolution.callbacks) {
  1079. txnCallbacks = lastQueuedResolution.callbacks;
  1080. txnIndex =
  1081. txnCallbacks.push(asChild ? { callback, asChild } : callback) - 1;
  1082. } else if (nextTxnCallbacks.isExecuting) {
  1083. txnCallbacks = [asChild ? { callback, asChild } : callback];
  1084. txnCallbacks.results = commitPromise;
  1085. nextTxnCallbacks.push(txnCallbacks);
  1086. txnIndex = 0;
  1087. } else {
  1088. if (writeTxn)
  1089. throw new Error('Can not enqueue transaction during write txn');
  1090. let finishWrite = writeInstructions(
  1091. 8 | (this.strictAsyncOrder ? 0x100000 : 0),
  1092. this,
  1093. );
  1094. txnCallbacks = [asChild ? { callback, asChild } : callback];
  1095. lastQueuedResolution.callbacks = txnCallbacks;
  1096. lastQueuedResolution.id = Math.random();
  1097. txnCallbacks.results = finishWrite();
  1098. txnIndex = 0;
  1099. }
  1100. return txnCallbacks.results.then((results) => {
  1101. let result = txnCallbacks[txnIndex];
  1102. if (result === CALLBACK_THREW) throw txnCallbacks.errors[txnIndex];
  1103. return result;
  1104. });
  1105. },
  1106. transactionSync(callback, flags) {
  1107. if (writeTxn) {
  1108. if (!useWritemap && (flags == undefined || flags & 1))
  1109. // can't use child transactions in write maps
  1110. // already nested in a transaction, execute as child transaction (if possible) and return
  1111. return this.childTransaction(callback);
  1112. let result = callback(); // else just run in current transaction
  1113. if (result == ABORT && !abortedNonChildTransactionWarn) {
  1114. console.warn(
  1115. 'Can not abort a transaction inside another transaction with ' +
  1116. (this.cache ? 'caching enabled' : 'useWritemap enabled'),
  1117. );
  1118. abortedNonChildTransactionWarn = true;
  1119. }
  1120. return result;
  1121. }
  1122. let callbackDone, finishTxn;
  1123. this.transactions++;
  1124. if (!env.address)
  1125. throw new Error(
  1126. 'The database has been closed and you can not transact on it',
  1127. );
  1128. env.beginTxn(flags == undefined ? 3 : flags);
  1129. let thisTxn = (writeTxn = env.writeTxn = { write: true });
  1130. try {
  1131. this.emit('begin-transaction');
  1132. return (writeTxn.childResults = when(
  1133. callback(),
  1134. (finishTxn = (result) => {
  1135. if (writeTxn !== thisTxn)
  1136. // need to wait for child txn to finish asynchronously
  1137. return writeTxn.childResults.then(() => finishTxn(result));
  1138. try {
  1139. callbackDone = true;
  1140. if (result === ABORT) env.abortTxn();
  1141. else {
  1142. env.commitTxn();
  1143. resetReadTxn();
  1144. }
  1145. return result;
  1146. } finally {
  1147. clearWriteTxn(null);
  1148. }
  1149. }),
  1150. (error) => {
  1151. try {
  1152. env.abortTxn();
  1153. } catch (e) {}
  1154. clearWriteTxn(null);
  1155. throw error;
  1156. },
  1157. ));
  1158. } catch (error) {
  1159. if (!callbackDone)
  1160. try {
  1161. env.abortTxn();
  1162. } catch (e) {}
  1163. clearWriteTxn(null);
  1164. throw error;
  1165. }
  1166. },
  1167. getWriteTxnId() {
  1168. return env.getWriteTxnId();
  1169. },
  1170. transactionSyncStart(callback) {
  1171. return this.transactionSync(callback, 0);
  1172. },
  1173. // make the db a thenable/promise-like for when the last commit is committed
  1174. committed: (committed = {
  1175. then(onfulfilled, onrejected) {
  1176. if (commitPromise) return commitPromise.then(onfulfilled, onrejected);
  1177. if (lastWritePromise)
  1178. // always resolve to true
  1179. return lastWritePromise.then(() => onfulfilled(true), onrejected);
  1180. return SYNC_PROMISE_SUCCESS.then(onfulfilled, onrejected);
  1181. },
  1182. }),
  1183. flushed: {
  1184. // make this a thenable for when the commit is flushed to disk
  1185. then(onfulfilled, onrejected) {
  1186. if (flushPromise) flushPromise.hasCallbacks = true;
  1187. return Promise.all([flushPromise || committed, lastSyncTxnFlush]).then(
  1188. onfulfilled,
  1189. onrejected,
  1190. );
  1191. },
  1192. },
  1193. _endWrites(resolvedPromise, resolvedSyncPromise) {
  1194. this.put =
  1195. this.remove =
  1196. this.del =
  1197. this.batch =
  1198. this.removeSync =
  1199. this.putSync =
  1200. this.transactionAsync =
  1201. this.drop =
  1202. this.clearAsync =
  1203. () => {
  1204. throw new Error('Database is closed');
  1205. };
  1206. // wait for all txns to finish, checking again after the current txn is done
  1207. let finalPromise = flushPromise || commitPromise || lastWritePromise;
  1208. if (flushPromise) flushPromise.hasCallbacks = true;
  1209. let finalSyncPromise = lastSyncTxnFlush;
  1210. if (
  1211. (finalPromise && resolvedPromise != finalPromise) ||
  1212. (finalSyncPromise )
  1213. ) {
  1214. return Promise.all([finalPromise, finalSyncPromise]).then(
  1215. () => this._endWrites(finalPromise, finalSyncPromise),
  1216. () => this._endWrites(finalPromise, finalSyncPromise),
  1217. );
  1218. }
  1219. Object.defineProperty(env, 'sync', { value: null });
  1220. },
  1221. on(event, callback) {
  1222. if (event == 'beforecommit') {
  1223. eventTurnBatching = true;
  1224. beforeCommitCallbacks.push(callback);
  1225. } else if (event == 'aftercommit') afterCommitCallbacks.push(callback);
  1226. else if (event == 'committed') {
  1227. this.getUserSharedBuffer('__committed__', new ArrayBuffer(0), {
  1228. envKey: true,
  1229. callback,
  1230. });
  1231. } else super.on(event, callback);
  1232. },
  1233. });
  1234. }
  1235. class Batch extends Array {
  1236. constructor(callback) {
  1237. super();
  1238. this.callback = callback;
  1239. }
  1240. put(key, value) {
  1241. this.push({ type: 'put', key, value });
  1242. }
  1243. del(key) {
  1244. this.push({ type: 'del', key });
  1245. }
  1246. clear() {
  1247. this.length = 0;
  1248. }
  1249. write(callback) {
  1250. return this.callback(this, callback);
  1251. }
  1252. }
  1253. function asBinary(buffer) {
  1254. return {
  1255. ['\x10binary-data\x02']: buffer,
  1256. };
  1257. }
  1258. const SKIP = {};
  1259. const DONE = {
  1260. value: null,
  1261. done: true,
  1262. };
  1263. const RETURN_DONE = {
  1264. // we allow this one to be mutated
  1265. value: null,
  1266. done: true,
  1267. };
  1268. if (!Symbol.asyncIterator) {
  1269. Symbol.asyncIterator = Symbol.for('Symbol.asyncIterator');
  1270. }
  1271. const NO_OPTIONS = {};
  1272. class RangeIterable {
  1273. constructor(sourceArray) {
  1274. if (sourceArray) {
  1275. this.iterate = sourceArray[Symbol.iterator].bind(sourceArray);
  1276. }
  1277. }
  1278. map(func) {
  1279. let source = this;
  1280. let iterable = new RangeIterable();
  1281. iterable.iterate = (options = NO_OPTIONS) => {
  1282. const { async } = options;
  1283. let iterator =
  1284. source[async ? Symbol.asyncIterator : Symbol.iterator](options);
  1285. if (!async) source.isSync = true;
  1286. let i = -1;
  1287. return {
  1288. next(resolvedResult) {
  1289. let result;
  1290. do {
  1291. let iteratorResult;
  1292. try {
  1293. if (resolvedResult) {
  1294. iteratorResult = resolvedResult;
  1295. resolvedResult = null; // don't go in this branch on next iteration
  1296. } else {
  1297. i++;
  1298. iteratorResult = iterator.next();
  1299. if (iteratorResult.then) {
  1300. if (!async) {
  1301. this.throw(
  1302. new Error(
  1303. 'Can not synchronously iterate with promises as iterator results',
  1304. ),
  1305. );
  1306. }
  1307. return iteratorResult.then(
  1308. (iteratorResult) => this.next(iteratorResult),
  1309. (error) => {
  1310. return this.throw(error);
  1311. },
  1312. );
  1313. }
  1314. }
  1315. if (iteratorResult.done === true) {
  1316. this.done = true;
  1317. if (iterable.onDone) iterable.onDone();
  1318. return iteratorResult;
  1319. }
  1320. try {
  1321. result = func.call(source, iteratorResult.value, i);
  1322. if (result && result.then && async) {
  1323. // if async, wait for promise to resolve before returning iterator result
  1324. return result.then(
  1325. (result) =>
  1326. result === SKIP
  1327. ? this.next()
  1328. : {
  1329. value: result,
  1330. },
  1331. (error) => {
  1332. if (options.continueOnRecoverableError)
  1333. error.continueIteration = true;
  1334. return this.throw(error);
  1335. },
  1336. );
  1337. }
  1338. } catch (error) {
  1339. // if the error came from the user function, we can potentially mark it for continuing iteration
  1340. if (options.continueOnRecoverableError)
  1341. error.continueIteration = true;
  1342. throw error; // throw to next catch to handle
  1343. }
  1344. } catch (error) {
  1345. if (iterable.handleError) {
  1346. // if we have handleError, we can use it to further handle errors
  1347. try {
  1348. result = iterable.handleError(error, i);
  1349. } catch (error2) {
  1350. return this.throw(error2);
  1351. }
  1352. } else return this.throw(error);
  1353. }
  1354. } while (result === SKIP);
  1355. if (result === DONE) {
  1356. return this.return();
  1357. }
  1358. return {
  1359. value: result,
  1360. };
  1361. },
  1362. return(value) {
  1363. if (!this.done) {
  1364. RETURN_DONE.value = value;
  1365. this.done = true;
  1366. if (iterable.onDone) iterable.onDone();
  1367. iterator.return();
  1368. }
  1369. return RETURN_DONE;
  1370. },
  1371. throw(error) {
  1372. if (error.continueIteration) {
  1373. // if it's a recoverable error, we can return or throw without closing the iterator
  1374. if (iterable.returnRecoverableErrors)
  1375. try {
  1376. return {
  1377. value: iterable.returnRecoverableErrors(error),
  1378. };
  1379. } catch (error) {
  1380. // if this throws, we need to go back to closing the iterator
  1381. this.return();
  1382. throw error;
  1383. }
  1384. if (options.continueOnRecoverableError) throw error; // throw without closing iterator
  1385. }
  1386. // else we are done with the iterator (and can throw)
  1387. this.return();
  1388. throw error;
  1389. },
  1390. };
  1391. };
  1392. return iterable;
  1393. }
  1394. [Symbol.asyncIterator](options) {
  1395. if (options) options = { ...options, async: true };
  1396. else options = { async: true };
  1397. return (this.iterator = this.iterate(options));
  1398. }
  1399. [Symbol.iterator](options) {
  1400. return (this.iterator = this.iterate(options));
  1401. }
  1402. filter(func) {
  1403. let iterable = this.map((element) => {
  1404. let result = func(element);
  1405. // handle promise
  1406. if (result?.then)
  1407. return result.then((result) => (result ? element : SKIP));
  1408. else return result ? element : SKIP;
  1409. });
  1410. let iterate = iterable.iterate;
  1411. iterable.iterate = (options = NO_OPTIONS) => {
  1412. // explicitly prevent continue on recoverable error with filter
  1413. if (options.continueOnRecoverableError)
  1414. options = { ...options, continueOnRecoverableError: false };
  1415. return iterate(options);
  1416. };
  1417. return iterable;
  1418. }
  1419. forEach(callback) {
  1420. let iterator = (this.iterator = this.iterate());
  1421. let result;
  1422. while ((result = iterator.next()).done !== true) {
  1423. callback(result.value);
  1424. }
  1425. }
  1426. concat(secondIterable) {
  1427. let concatIterable = new RangeIterable();
  1428. concatIterable.iterate = (options = NO_OPTIONS) => {
  1429. let iterator = (this.iterator = this.iterate(options));
  1430. let isFirst = true;
  1431. function iteratorDone(result) {
  1432. if (isFirst) {
  1433. try {
  1434. isFirst = false;
  1435. iterator =
  1436. secondIterable[
  1437. options.async ? Symbol.asyncIterator : Symbol.iterator
  1438. ]();
  1439. result = iterator.next();
  1440. if (concatIterable.onDone) {
  1441. if (result.then) {
  1442. if (!options.async)
  1443. throw new Error(
  1444. 'Can not synchronously iterate with promises as iterator results',
  1445. );
  1446. result.then(
  1447. (result) => {
  1448. if (result.done()) concatIterable.onDone();
  1449. },
  1450. (error) => {
  1451. this.return();
  1452. throw error;
  1453. },
  1454. );
  1455. } else if (result.done) concatIterable.onDone();
  1456. }
  1457. } catch (error) {
  1458. this.throw(error);
  1459. }
  1460. } else {
  1461. if (concatIterable.onDone) concatIterable.onDone();
  1462. }
  1463. return result;
  1464. }
  1465. return {
  1466. next() {
  1467. try {
  1468. let result = iterator.next();
  1469. if (result.then) {
  1470. if (!options.async)
  1471. throw new Error(
  1472. 'Can not synchronously iterate with promises as iterator results',
  1473. );
  1474. return result.then((result) => {
  1475. if (result.done) return iteratorDone(result);
  1476. return result;
  1477. });
  1478. }
  1479. if (result.done) return iteratorDone(result);
  1480. return result;
  1481. } catch (error) {
  1482. this.throw(error);
  1483. }
  1484. },
  1485. return(value) {
  1486. if (!this.done) {
  1487. RETURN_DONE.value = value;
  1488. this.done = true;
  1489. if (concatIterable.onDone) concatIterable.onDone();
  1490. iterator.return();
  1491. }
  1492. return RETURN_DONE;
  1493. },
  1494. throw(error) {
  1495. if (options.continueOnRecoverableError) throw error;
  1496. this.return();
  1497. throw error;
  1498. },
  1499. };
  1500. };
  1501. return concatIterable;
  1502. }
  1503. flatMap(callback) {
  1504. let mappedIterable = new RangeIterable();
  1505. mappedIterable.iterate = (options = NO_OPTIONS) => {
  1506. let iterator = (this.iterator = this.iterate(options));
  1507. let currentSubIterator;
  1508. return {
  1509. next(resolvedResult) {
  1510. try {
  1511. do {
  1512. if (currentSubIterator) {
  1513. let result;
  1514. if (resolvedResult) {
  1515. result = resolvedResult;
  1516. resolvedResult = undefined;
  1517. } else result = currentSubIterator.next();
  1518. if (result.then) {
  1519. if (!options.async)
  1520. throw new Error(
  1521. 'Can not synchronously iterate with promises as iterator results',
  1522. );
  1523. return result.then((result) => this.next(result));
  1524. }
  1525. if (!result.done) {
  1526. return result;
  1527. }
  1528. }
  1529. let result;
  1530. if (resolvedResult != undefined) {
  1531. result = resolvedResult;
  1532. resolvedResult = undefined;
  1533. } else result = iterator.next();
  1534. if (result.then) {
  1535. if (!options.async)
  1536. throw new Error(
  1537. 'Can not synchronously iterate with promises as iterator results',
  1538. );
  1539. currentSubIterator = undefined;
  1540. return result.then((result) => this.next(result));
  1541. }
  1542. if (result.done) {
  1543. if (mappedIterable.onDone) mappedIterable.onDone();
  1544. return result;
  1545. }
  1546. try {
  1547. let value = callback(result.value);
  1548. if (value?.then) {
  1549. if (!options.async)
  1550. throw new Error(
  1551. 'Can not synchronously iterate with promises as iterator results',
  1552. );
  1553. return value.then(
  1554. (value) => {
  1555. if (
  1556. Array.isArray(value) ||
  1557. value instanceof RangeIterable
  1558. ) {
  1559. currentSubIterator = value[Symbol.iterator]();
  1560. return this.next();
  1561. } else {
  1562. currentSubIterator = null;
  1563. return { value };
  1564. }
  1565. },
  1566. (error) => {
  1567. if (options.continueOnRecoverableError)
  1568. error.continueIteration = true;
  1569. this.throw(error);
  1570. },
  1571. );
  1572. }
  1573. if (Array.isArray(value) || value instanceof RangeIterable)
  1574. currentSubIterator = value[Symbol.iterator]();
  1575. else {
  1576. currentSubIterator = null;
  1577. return { value };
  1578. }
  1579. } catch (error) {
  1580. if (options.continueOnRecoverableError)
  1581. error.continueIteration = true;
  1582. throw error;
  1583. }
  1584. } while (true);
  1585. } catch (error) {
  1586. this.throw(error);
  1587. }
  1588. },
  1589. return() {
  1590. if (mappedIterable.onDone) mappedIterable.onDone();
  1591. if (currentSubIterator) currentSubIterator.return();
  1592. return iterator.return();
  1593. },
  1594. throw(error) {
  1595. if (options.continueOnRecoverableError) throw error;
  1596. if (mappedIterable.onDone) mappedIterable.onDone();
  1597. if (currentSubIterator) currentSubIterator.return();
  1598. this.return();
  1599. throw error;
  1600. },
  1601. };
  1602. };
  1603. return mappedIterable;
  1604. }
  1605. slice(start, end) {
  1606. let iterable = this.map((element, i) => {
  1607. if (i < start) return SKIP;
  1608. if (i >= end) {
  1609. DONE.value = element;
  1610. return DONE;
  1611. }
  1612. return element;
  1613. });
  1614. iterable.handleError = (error, i) => {
  1615. if (i < start) return SKIP;
  1616. if (i >= end) {
  1617. return DONE;
  1618. }
  1619. throw error;
  1620. };
  1621. return iterable;
  1622. }
  1623. mapError(catch_callback) {
  1624. let iterable = this.map((element) => {
  1625. return element;
  1626. });
  1627. let iterate = iterable.iterate;
  1628. iterable.iterate = (options = NO_OPTIONS) => {
  1629. // we need to ensure the whole stack
  1630. // of iterables is set up to handle recoverable errors and continue iteration
  1631. return iterate({ ...options, continueOnRecoverableError: true });
  1632. };
  1633. iterable.returnRecoverableErrors = catch_callback;
  1634. return iterable;
  1635. }
  1636. next() {
  1637. if (!this.iterator) this.iterator = this.iterate();
  1638. return this.iterator.next();
  1639. }
  1640. toJSON() {
  1641. if (this.asArray && this.asArray.forEach) {
  1642. return this.asArray;
  1643. }
  1644. const error = new Error(
  1645. 'Can not serialize async iterables without first calling resolving asArray',
  1646. );
  1647. error.resolution = this.asArray;
  1648. throw error;
  1649. //return Array.from(this)
  1650. }
  1651. get asArray() {
  1652. if (this._asArray) return this._asArray;
  1653. let promise = new Promise((resolve, reject) => {
  1654. let iterator = this.iterate(true);
  1655. let array = [];
  1656. let iterable = this;
  1657. Object.defineProperty(array, 'iterable', { value: iterable });
  1658. function next(result) {
  1659. while (result.done !== true) {
  1660. if (result.then) {
  1661. return result.then(next);
  1662. } else {
  1663. array.push(result.value);
  1664. }
  1665. result = iterator.next();
  1666. }
  1667. resolve((iterable._asArray = array));
  1668. }
  1669. next(iterator.next());
  1670. });
  1671. promise.iterable = this;
  1672. return this._asArray || (this._asArray = promise);
  1673. }
  1674. resolveData() {
  1675. return this.asArray;
  1676. }
  1677. at(index) {
  1678. for (let entry of this) {
  1679. if (index-- === 0) return entry;
  1680. }
  1681. }
  1682. }
  1683. RangeIterable.prototype.DONE = DONE;
  1684. const REUSE_BUFFER_MODE = 512;
  1685. const writeUint32Key = (key, target, start) => {
  1686. (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).setUint32(start, key, true);
  1687. return start + 4;
  1688. };
  1689. const readUint32Key = (target, start) => {
  1690. return (target.dataView || (target.dataView = new DataView(target.buffer, 0, target.length))).getUint32(start, true);
  1691. };
  1692. const writeBufferKey = (key, target, start) => {
  1693. target.set(key, start);
  1694. return key.length + start;
  1695. };
  1696. const Uint8ArraySlice$1 = Uint8Array.prototype.slice;
  1697. const readBufferKey = (target, start, end) => {
  1698. return Uint8ArraySlice$1.call(target, start, end);
  1699. };
  1700. let lastEncodedValue, bytes;
  1701. function applyKeyHandling(store) {
  1702. if (store.encoding == 'ordered-binary') {
  1703. store.encoder = store.decoder = {
  1704. writeKey: orderedBinary.writeKey,
  1705. readKey: orderedBinary.readKey,
  1706. };
  1707. }
  1708. if (store.encoder && store.encoder.writeKey && !store.encoder.encode) {
  1709. store.encoder.encode = function(value, mode) {
  1710. if (typeof value !== 'object' && value && value === lastEncodedValue) ; else {
  1711. lastEncodedValue = value;
  1712. bytes = saveKey(value, this.writeKey, false, store.maxKeySize);
  1713. }
  1714. if (bytes.end > 0 && !(REUSE_BUFFER_MODE & mode)) {
  1715. return bytes.subarray(bytes.start, bytes.end);
  1716. }
  1717. return bytes;
  1718. };
  1719. store.encoder.copyBuffers = true; // just an indicator for the buffer reuse in write.js
  1720. }
  1721. if (store.decoder && store.decoder.readKey && !store.decoder.decode) {
  1722. store.decoder.decode = function(buffer) { return this.readKey(buffer, 0, buffer.length); };
  1723. store.decoderCopies = true;
  1724. }
  1725. if (store.keyIsUint32 || store.keyEncoding == 'uint32') {
  1726. store.writeKey = writeUint32Key;
  1727. store.readKey = readUint32Key;
  1728. } else if (store.keyIsBuffer || store.keyEncoding == 'binary') {
  1729. store.writeKey = writeBufferKey;
  1730. store.readKey = readBufferKey;
  1731. } else if (store.keyEncoder) {
  1732. store.writeKey = store.keyEncoder.writeKey;
  1733. store.readKey = store.keyEncoder.readKey;
  1734. } else {
  1735. store.writeKey = orderedBinary.writeKey;
  1736. store.readKey = orderedBinary.readKey;
  1737. }
  1738. }
  1739. let saveBuffer, saveDataView = { setFloat64() {}, setUint32() {} }, saveDataAddress;
  1740. let savePosition$1 = 8000;
  1741. let DYNAMIC_KEY_BUFFER_SIZE$1 = 8192;
  1742. function allocateSaveBuffer() {
  1743. saveBuffer = typeof Buffer != 'undefined' ? Buffer.alloc(DYNAMIC_KEY_BUFFER_SIZE$1) : new Uint8Array(DYNAMIC_KEY_BUFFER_SIZE$1);
  1744. saveBuffer.buffer.address = getAddress(saveBuffer.buffer);
  1745. saveDataAddress = saveBuffer.buffer.address;
  1746. // TODO: Conditionally only do this for key sequences?
  1747. saveDataView.setUint32(savePosition$1, 0xffffffff);
  1748. saveDataView.setFloat64(savePosition$1 + 4, saveDataAddress, true); // save a pointer from the old buffer to the new address for the sake of the prefetch sequences
  1749. saveDataView = saveBuffer.dataView || (saveBuffer.dataView = new DataView(saveBuffer.buffer, saveBuffer.byteOffset, saveBuffer.byteLength));
  1750. savePosition$1 = 0;
  1751. }
  1752. function saveKey(key, writeKey, saveTo, maxKeySize, flags) {
  1753. if (savePosition$1 > 7800) {
  1754. allocateSaveBuffer();
  1755. }
  1756. let start = savePosition$1;
  1757. try {
  1758. savePosition$1 = key === undefined ? start + 4 :
  1759. writeKey(key, saveBuffer, start + 4);
  1760. } catch (error) {
  1761. saveBuffer.fill(0, start + 4); // restore zeros
  1762. if (error.name == 'RangeError') {
  1763. if (8180 - start < maxKeySize) {
  1764. allocateSaveBuffer(); // try again:
  1765. return saveKey(key, writeKey, saveTo, maxKeySize);
  1766. }
  1767. throw new Error('Key was too large, max key size is ' + maxKeySize);
  1768. } else
  1769. throw error;
  1770. }
  1771. let length = savePosition$1 - start - 4;
  1772. if (length > maxKeySize) {
  1773. throw new Error('Key of size ' + length + ' was too large, max key size is ' + maxKeySize);
  1774. }
  1775. if (savePosition$1 >= 8160) { // need to reserve enough room at the end for pointers
  1776. savePosition$1 = start; // reset position
  1777. allocateSaveBuffer(); // try again:
  1778. return saveKey(key, writeKey, saveTo, maxKeySize);
  1779. }
  1780. if (saveTo) {
  1781. saveDataView.setUint32(start, flags ? length | flags : length, true); // save the length
  1782. saveTo.saveBuffer = saveBuffer;
  1783. savePosition$1 = (savePosition$1 + 12) & 0xfffffc;
  1784. return start + saveDataAddress;
  1785. } else {
  1786. saveBuffer.start = start + 4;
  1787. saveBuffer.end = savePosition$1;
  1788. savePosition$1 = (savePosition$1 + 7) & 0xfffff8; // full 64-bit word alignment since these are usually copied
  1789. return saveBuffer;
  1790. }
  1791. }
  1792. const IF_EXISTS = 3.542694326329068e-103;
  1793. const DEFAULT_BEGINNING_KEY = Buffer.from([5]); // the default starting key for iteration, which excludes symbols/metadata
  1794. const ITERATOR_DONE = { done: true, value: undefined };
  1795. const Uint8ArraySlice = Uint8Array.prototype.slice;
  1796. let getValueBytes = globalBuffer;
  1797. if (!getValueBytes.maxLength) {
  1798. getValueBytes.maxLength = getValueBytes.length;
  1799. getValueBytes.isGlobal = true;
  1800. Object.defineProperty(getValueBytes, 'length', {
  1801. value: getValueBytes.length,
  1802. writable: true,
  1803. configurable: true,
  1804. });
  1805. }
  1806. const START_ADDRESS_POSITION = 4064;
  1807. const NEW_BUFFER_THRESHOLD = 0x8000;
  1808. const SOURCE_SYMBOL = Symbol.for('source');
  1809. const UNMODIFIED = {};
  1810. let mmaps = [];
  1811. function addReadMethods(
  1812. LMDBStore,
  1813. { maxKeySize, env, keyBytes, keyBytesView, getLastVersion, getLastTxnId },
  1814. ) {
  1815. let readTxn,
  1816. readTxnRenewed,
  1817. asSafeBuffer = false;
  1818. let renewId = 1;
  1819. let outstandingReads = 0;
  1820. Object.assign(LMDBStore.prototype, {
  1821. getString(id, options) {
  1822. let txn =
  1823. env.writeTxn ||
  1824. (options && options.transaction) ||
  1825. (readTxnRenewed ? readTxn : renewReadTxn(this));
  1826. let string = getStringByBinary(
  1827. this.dbAddress,
  1828. this.writeKey(id, keyBytes, 0),
  1829. txn.address || 0,
  1830. );
  1831. if (typeof string === 'number') {
  1832. // indicates the buffer wasn't large enough
  1833. this._allocateGetBuffer(string);
  1834. // and then try again
  1835. string = getStringByBinary(
  1836. this.dbAddress,
  1837. this.writeKey(id, keyBytes, 0),
  1838. txn.address || 0,
  1839. );
  1840. }
  1841. if (string) this.lastSize = string.length;
  1842. return string;
  1843. },
  1844. getBinaryFast(id, options) {
  1845. let rc;
  1846. let txn =
  1847. env.writeTxn ||
  1848. (options && options.transaction) ||
  1849. (readTxnRenewed ? readTxn : renewReadTxn(this));
  1850. rc = this.lastSize = getByBinary(
  1851. this.dbAddress,
  1852. this.writeKey(id, keyBytes, 0),
  1853. (options && options.ifNotTxnId) || 0,
  1854. txn.address || 0,
  1855. );
  1856. if (rc < 0) {
  1857. if (rc == -30798)
  1858. // MDB_NOTFOUND
  1859. return; // undefined
  1860. if (rc == -30004)
  1861. // txn id matched
  1862. return UNMODIFIED;
  1863. if (
  1864. rc == -30781 /*MDB_BAD_VALSIZE*/ &&
  1865. this.writeKey(id, keyBytes, 0) == 0
  1866. )
  1867. throw new Error(
  1868. id === undefined
  1869. ? 'A key is required for get, but is undefined'
  1870. : 'Zero length key is not allowed in LMDB',
  1871. );
  1872. if (rc == -30000)
  1873. // int32 overflow, read uint32
  1874. rc = this.lastSize = keyBytesView.getUint32(0, true);
  1875. else if (rc == -30001) {
  1876. // shared buffer
  1877. this.lastSize = keyBytesView.getUint32(0, true);
  1878. let bufferId = keyBytesView.getUint32(4, true);
  1879. let bytes = getMMapBuffer(bufferId, this.lastSize);
  1880. return asSafeBuffer ? Buffer.from(bytes) : bytes;
  1881. } else throw lmdbError(rc);
  1882. }
  1883. let compression = this.compression;
  1884. let bytes = compression ? compression.getValueBytes : getValueBytes;
  1885. if (rc > bytes.maxLength) {
  1886. // this means the target buffer wasn't big enough, so the get failed to copy all the data from the database, need to either grow or use special buffer
  1887. return this._returnLargeBuffer(() =>
  1888. getByBinary(
  1889. this.dbAddress,
  1890. this.writeKey(id, keyBytes, 0),
  1891. 0,
  1892. txn.address || 0,
  1893. ),
  1894. );
  1895. }
  1896. bytes.length = this.lastSize;
  1897. return bytes;
  1898. },
  1899. getBFAsync(id, options, callback) {
  1900. let txn =
  1901. env.writeTxn ||
  1902. (options && options.transaction) ||
  1903. (readTxnRenewed ? readTxn : renewReadTxn(this));
  1904. txn.refCount = (txn.refCount || 0) + 1;
  1905. outstandingReads++;
  1906. if (!txn.address) {
  1907. throw new Error('Invalid transaction, it has no address');
  1908. }
  1909. let address = recordReadInstruction(
  1910. txn.address,
  1911. this.db.dbi,
  1912. id,
  1913. this.writeKey,
  1914. maxKeySize,
  1915. (rc, bufferId, offset, size) => {
  1916. if (rc && rc !== 1) callback(lmdbError(rc));
  1917. outstandingReads--;
  1918. let buffer = mmaps[bufferId];
  1919. if (!buffer) {
  1920. buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);
  1921. }
  1922. //console.log({bufferId, offset, size})
  1923. if (buffer.isSharedMap) {
  1924. // using LMDB shared memory
  1925. // TODO: We may want explicit support for clearing aborting the transaction on the next event turn,
  1926. // but for now we are relying on the GC to cleanup transaction for larger blocks of memory
  1927. let bytes = new Uint8Array(buffer, offset, size);
  1928. bytes.txn = txn;
  1929. callback(bytes, 0, size);
  1930. } else {
  1931. // using copied memory
  1932. txn.done(); // decrement and possibly abort
  1933. callback(buffer, offset, size);
  1934. }
  1935. },
  1936. );
  1937. if (address) {
  1938. startRead(address, () => {
  1939. resolveReads();
  1940. });
  1941. }
  1942. },
  1943. getAsync(id, options, callback) {
  1944. let promise;
  1945. if (!callback) promise = new Promise((resolve) => (callback = resolve));
  1946. this.getBFAsync(id, options, (buffer, offset, size) => {
  1947. if (this.useVersions) {
  1948. // TODO: And get the version
  1949. offset += 8;
  1950. size -= 8;
  1951. }
  1952. let bytes = new Uint8Array(buffer, offset, size);
  1953. let value;
  1954. if (this.decoder) {
  1955. // the decoder potentially uses the data from the buffer in the future and needs a stable buffer
  1956. value = bytes && this.decoder.decode(bytes);
  1957. } else if (this.encoding == 'binary') {
  1958. value = bytes;
  1959. } else {
  1960. value = Buffer.prototype.utf8Slice.call(bytes, 0, size);
  1961. if (this.encoding == 'json' && value) value = JSON.parse(value);
  1962. }
  1963. callback(value);
  1964. });
  1965. return promise;
  1966. },
  1967. retain(data, options) {
  1968. if (!data) return;
  1969. let source = data[SOURCE_SYMBOL];
  1970. let buffer = source ? source.bytes : data;
  1971. if (!buffer.isGlobal && !env.writeTxn) {
  1972. let txn =
  1973. options?.transaction ||
  1974. (readTxnRenewed ? readTxn : renewReadTxn(this));
  1975. buffer.txn = txn;
  1976. txn.refCount = (txn.refCount || 0) + 1;
  1977. return data;
  1978. } else {
  1979. buffer = Uint8ArraySlice.call(buffer, 0, this.lastSize);
  1980. if (source) {
  1981. source.bytes = buffer;
  1982. return data;
  1983. } else return buffer;
  1984. }
  1985. },
  1986. _returnLargeBuffer(getFast) {
  1987. let bytes;
  1988. let compression = this.compression;
  1989. if (asSafeBuffer && this.lastSize > NEW_BUFFER_THRESHOLD) {
  1990. // used by getBinary to indicate it should create a dedicated buffer to receive this
  1991. let bytesToRestore;
  1992. try {
  1993. if (compression) {
  1994. bytesToRestore = compression.getValueBytes;
  1995. let dictionary = compression.dictionary || [];
  1996. let dictLength = (dictionary.length >> 3) << 3; // make sure it is word-aligned
  1997. bytes = makeReusableBuffer(this.lastSize);
  1998. compression.setBuffer(
  1999. bytes.buffer,
  2000. bytes.byteOffset,
  2001. this.lastSize,
  2002. dictionary,
  2003. dictLength,
  2004. );
  2005. compression.getValueBytes = bytes;
  2006. } else {
  2007. bytesToRestore = getValueBytes;
  2008. setGlobalBuffer(
  2009. (bytes = getValueBytes = makeReusableBuffer(this.lastSize)),
  2010. );
  2011. }
  2012. getFast();
  2013. } finally {
  2014. if (compression) {
  2015. let dictLength = (compression.dictionary.length >> 3) << 3;
  2016. compression.setBuffer(
  2017. bytesToRestore.buffer,
  2018. bytesToRestore.byteOffset,
  2019. bytesToRestore.maxLength,
  2020. compression.dictionary,
  2021. dictLength,
  2022. );
  2023. compression.getValueBytes = bytesToRestore;
  2024. } else {
  2025. setGlobalBuffer(bytesToRestore);
  2026. getValueBytes = bytesToRestore;
  2027. }
  2028. }
  2029. return bytes;
  2030. }
  2031. // grow our shared/static buffer to accomodate the size of the data
  2032. bytes = this._allocateGetBuffer(this.lastSize);
  2033. // and try again
  2034. getFast();
  2035. bytes.length = this.lastSize;
  2036. return bytes;
  2037. },
  2038. _allocateGetBuffer(lastSize) {
  2039. let newLength = Math.min(Math.max(lastSize * 2, 0x1000), 0xfffffff8);
  2040. let bytes;
  2041. if (this.compression) {
  2042. let dictionary =
  2043. this.compression.dictionary || Buffer.allocUnsafeSlow(0);
  2044. let dictLength = (dictionary.length >> 3) << 3; // make sure it is word-aligned
  2045. bytes = Buffer.allocUnsafeSlow(newLength + dictLength);
  2046. bytes.set(dictionary); // copy dictionary into start
  2047. // the section after the dictionary is the target area for get values
  2048. bytes = bytes.subarray(dictLength);
  2049. this.compression.setBuffer(
  2050. bytes.buffer,
  2051. bytes.byteOffset,
  2052. newLength,
  2053. dictionary,
  2054. dictLength,
  2055. );
  2056. bytes.maxLength = newLength;
  2057. Object.defineProperty(bytes, 'length', {
  2058. value: newLength,
  2059. writable: true,
  2060. configurable: true,
  2061. });
  2062. this.compression.getValueBytes = bytes;
  2063. } else {
  2064. bytes = makeReusableBuffer(newLength);
  2065. setGlobalBuffer((getValueBytes = bytes));
  2066. }
  2067. bytes.isGlobal = true;
  2068. return bytes;
  2069. },
  2070. getBinary(id, options) {
  2071. try {
  2072. asSafeBuffer = true;
  2073. let fastBuffer = this.getBinaryFast(id, options);
  2074. return (
  2075. fastBuffer &&
  2076. (fastBuffer.isGlobal
  2077. ? Uint8ArraySlice.call(fastBuffer, 0, this.lastSize)
  2078. : fastBuffer)
  2079. );
  2080. } finally {
  2081. asSafeBuffer = false;
  2082. }
  2083. },
  2084. getSharedBinary(id, options) {
  2085. let fastBuffer = this.getBinaryFast(id, options);
  2086. if (fastBuffer) {
  2087. if (fastBuffer.isGlobal || writeTxn)
  2088. return Uint8ArraySlice.call(fastBuffer, 0, this.lastSize);
  2089. fastBuffer.txn = options && options.transaction;
  2090. options.transaction.refCount = (options.transaction.refCount || 0) + 1;
  2091. return fastBuffer;
  2092. }
  2093. },
  2094. get(id, options) {
  2095. if (this.decoderCopies) {
  2096. // the decoder copies any data, so we can use the fast binary retrieval that overwrites the same buffer space
  2097. let bytes = this.getBinaryFast(id, options);
  2098. return (
  2099. bytes &&
  2100. (bytes == UNMODIFIED
  2101. ? UNMODIFIED
  2102. : this.decoder.decode(bytes, options))
  2103. );
  2104. }
  2105. if (this.encoding == 'binary') return this.getBinary(id, options);
  2106. if (this.decoder) {
  2107. // the decoder potentially uses the data from the buffer in the future and needs a stable buffer
  2108. let bytes = this.getBinary(id, options);
  2109. return (
  2110. bytes &&
  2111. (bytes == UNMODIFIED ? UNMODIFIED : this.decoder.decode(bytes))
  2112. );
  2113. }
  2114. let result = this.getString(id, options);
  2115. if (result) {
  2116. if (this.encoding == 'json') return JSON.parse(result);
  2117. }
  2118. return result;
  2119. },
  2120. getEntry(id, options) {
  2121. let value = this.get(id, options);
  2122. if (value !== undefined) {
  2123. if (this.useVersions)
  2124. return {
  2125. value,
  2126. version: getLastVersion(),
  2127. //size: this.lastSize
  2128. };
  2129. else
  2130. return {
  2131. value,
  2132. //size: this.lastSize
  2133. };
  2134. }
  2135. },
  2136. directWrite(id, options) {
  2137. let rc;
  2138. let txn =
  2139. env.writeTxn ||
  2140. (options && options.transaction) ||
  2141. (readTxnRenewed ? readTxn : renewReadTxn(this));
  2142. let keySize = this.writeKey(id, keyBytes, 0);
  2143. let dataOffset = ((keySize >> 3) + 1) << 3;
  2144. keyBytes.set(options.bytes, dataOffset);
  2145. rc = directWrite(
  2146. this.dbAddress,
  2147. keySize,
  2148. options.offset,
  2149. options.bytes.length,
  2150. txn.address || 0,
  2151. );
  2152. if (rc < 0) lmdbError(rc);
  2153. },
  2154. getUserSharedBuffer(id, defaultBuffer, options) {
  2155. let keySize;
  2156. const setKeyBytes = () => {
  2157. if (options?.envKey) keySize = this.writeKey(id, keyBytes, 0);
  2158. else {
  2159. keyBytes.dataView.setUint32(0, this.db.dbi);
  2160. keySize = this.writeKey(id, keyBytes, 4);
  2161. }
  2162. };
  2163. setKeyBytes();
  2164. let sharedBuffer = getUserSharedBuffer(
  2165. env.address,
  2166. keySize,
  2167. defaultBuffer,
  2168. options?.callback,
  2169. );
  2170. sharedBuffer.notify = () => {
  2171. setKeyBytes();
  2172. return notifyUserCallbacks(env.address, keySize);
  2173. };
  2174. return sharedBuffer;
  2175. },
  2176. attemptLock(id, version, callback) {
  2177. if (!env.address) throw new Error('Can not operate on a closed database');
  2178. keyBytes.dataView.setUint32(0, this.db.dbi);
  2179. keyBytes.dataView.setFloat64(4, version);
  2180. let keySize = this.writeKey(id, keyBytes, 12);
  2181. return attemptLock(env.address, keySize, callback);
  2182. },
  2183. unlock(id, version, onlyCheck) {
  2184. if (!env.address) throw new Error('Can not operate on a closed database');
  2185. keyBytes.dataView.setUint32(0, this.db.dbi);
  2186. keyBytes.dataView.setFloat64(4, version);
  2187. let keySize = this.writeKey(id, keyBytes, 12);
  2188. return unlock(env.address, keySize, onlyCheck);
  2189. },
  2190. hasLock(id, version) {
  2191. return this.unlock(id, version, true);
  2192. },
  2193. resetReadTxn() {
  2194. resetReadTxn();
  2195. },
  2196. _commitReadTxn() {
  2197. if (readTxn) {
  2198. readTxn.isCommitted = true;
  2199. readTxn.commit();
  2200. }
  2201. lastReadTxnRef = null;
  2202. readTxnRenewed = null;
  2203. readTxn = null;
  2204. },
  2205. ensureReadTxn() {
  2206. if (!env.writeTxn && !readTxnRenewed) renewReadTxn(this);
  2207. },
  2208. doesExist(key, versionOrValue, options) {
  2209. if (versionOrValue == null) {
  2210. // undefined means the entry exists, null is used specifically to check for the entry *not* existing
  2211. return (
  2212. (this.getBinaryFast(key, options) === undefined) ==
  2213. (versionOrValue === null)
  2214. );
  2215. } else if (this.useVersions) {
  2216. return (
  2217. this.getBinaryFast(key, options) !== undefined &&
  2218. (versionOrValue === IF_EXISTS || getLastVersion() === versionOrValue)
  2219. );
  2220. } else {
  2221. if (versionOrValue && versionOrValue['\x10binary-data\x02'])
  2222. versionOrValue = versionOrValue['\x10binary-data\x02'];
  2223. else if (this.encoder)
  2224. versionOrValue = this.encoder.encode(versionOrValue);
  2225. if (typeof versionOrValue == 'string')
  2226. versionOrValue = Buffer.from(versionOrValue);
  2227. let defaultOptions = { start: versionOrValue, exactMatch: true };
  2228. return (
  2229. this.getValuesCount(
  2230. key,
  2231. options ? Object.assign(defaultOptions, options) : defaultOptions,
  2232. ) > 0
  2233. );
  2234. }
  2235. },
  2236. getValues(key, options) {
  2237. let defaultOptions = {
  2238. key,
  2239. valuesForKey: true,
  2240. };
  2241. if (options && options.snapshot === false)
  2242. throw new Error('Can not disable snapshots for getValues');
  2243. return this.getRange(
  2244. options ? Object.assign(defaultOptions, options) : defaultOptions,
  2245. );
  2246. },
  2247. getKeys(options) {
  2248. if (!options) options = {};
  2249. options.values = false;
  2250. return this.getRange(options);
  2251. },
  2252. getCount(options) {
  2253. if (!options) options = {};
  2254. options.onlyCount = true;
  2255. return this.getRange(options).iterate();
  2256. },
  2257. getKeysCount(options) {
  2258. if (!options) options = {};
  2259. options.onlyCount = true;
  2260. options.values = false;
  2261. return this.getRange(options).iterate();
  2262. },
  2263. getValuesCount(key, options) {
  2264. if (!options) options = {};
  2265. options.key = key;
  2266. options.valuesForKey = true;
  2267. options.onlyCount = true;
  2268. return this.getRange(options).iterate();
  2269. },
  2270. getRange(options) {
  2271. let iterable = new RangeIterable();
  2272. let textDecoder = new TextDecoder();
  2273. if (!options) options = {};
  2274. let includeValues = options.values !== false;
  2275. let includeVersions = options.versions;
  2276. let valuesForKey = options.valuesForKey;
  2277. let limit = options.limit;
  2278. let db = this.db;
  2279. let snapshot = options.snapshot;
  2280. if (snapshot === false && this.dupSort && includeValues)
  2281. throw new Error(
  2282. 'Can not disable snapshot on a' + ' dupSort data store',
  2283. );
  2284. let compression = this.compression;
  2285. iterable.iterate = () => {
  2286. const reverse = options.reverse;
  2287. let currentKey = valuesForKey
  2288. ? options.key
  2289. : reverse || 'start' in options
  2290. ? options.start
  2291. : DEFAULT_BEGINNING_KEY;
  2292. let count = 0;
  2293. let cursor, cursorRenewId, cursorAddress;
  2294. let txn;
  2295. let flags =
  2296. (includeValues ? 0x100 : 0) |
  2297. (reverse ? 0x400 : 0) |
  2298. (valuesForKey ? 0x800 : 0) |
  2299. (options.exactMatch ? 0x4000 : 0) |
  2300. (options.inclusiveEnd ? 0x8000 : 0) |
  2301. (options.exclusiveStart ? 0x10000 : 0);
  2302. let store = this;
  2303. function resetCursor() {
  2304. try {
  2305. if (cursor) finishCursor();
  2306. let txnAddress;
  2307. txn = options.transaction;
  2308. if (txn) {
  2309. if (txn.isDone)
  2310. throw new Error(
  2311. 'Can not iterate on range with transaction that is already' +
  2312. ' done',
  2313. );
  2314. txnAddress = txn.address;
  2315. if (!txnAddress) {
  2316. throw new Error('Invalid transaction, it has no address');
  2317. }
  2318. cursor = null;
  2319. } else {
  2320. let writeTxn = env.writeTxn;
  2321. if (writeTxn) snapshot = false;
  2322. txn =
  2323. env.writeTxn ||
  2324. options.transaction ||
  2325. (readTxnRenewed ? readTxn : renewReadTxn(store));
  2326. cursor = !writeTxn && db.availableCursor;
  2327. }
  2328. if (cursor) {
  2329. db.availableCursor = null;
  2330. flags |= 0x2000;
  2331. } else {
  2332. cursor = new Cursor(db, txnAddress || 0);
  2333. }
  2334. cursorAddress = cursor.address;
  2335. if (txn.use)
  2336. txn.use(); // track transaction so we always use the same one
  2337. else txn.refCount = (txn.refCount || 0) + 1;
  2338. if (snapshot === false) {
  2339. cursorRenewId = renewId; // use shared read transaction
  2340. txn.renewingRefCount = (txn.renewingRefCount || 0) + 1; // need to know how many are renewing cursors
  2341. }
  2342. } catch (error) {
  2343. if (cursor) {
  2344. try {
  2345. cursor.close();
  2346. } catch (error) {}
  2347. }
  2348. throw error;
  2349. }
  2350. }
  2351. resetCursor();
  2352. if (options.onlyCount) {
  2353. flags |= 0x1000;
  2354. let count = position$1(options.offset);
  2355. if (count < 0) lmdbError(count);
  2356. finishCursor();
  2357. return count;
  2358. }
  2359. function position$1(offset) {
  2360. if (!env.address) {
  2361. throw new Error('Can not iterate on a closed database');
  2362. }
  2363. let keySize =
  2364. currentKey === undefined
  2365. ? 0
  2366. : store.writeKey(currentKey, keyBytes, 0);
  2367. let endAddress;
  2368. if (valuesForKey) {
  2369. if (options.start === undefined && options.end === undefined)
  2370. endAddress = 0;
  2371. else {
  2372. let startAddress;
  2373. if (store.encoder.writeKey) {
  2374. startAddress = saveKey(
  2375. options.start,
  2376. store.encoder.writeKey,
  2377. iterable,
  2378. maxKeySize,
  2379. );
  2380. keyBytesView.setFloat64(
  2381. START_ADDRESS_POSITION,
  2382. startAddress,
  2383. true,
  2384. );
  2385. endAddress = saveKey(
  2386. options.end,
  2387. store.encoder.writeKey,
  2388. iterable,
  2389. maxKeySize,
  2390. );
  2391. } else if (
  2392. (!options.start || options.start instanceof Uint8Array) &&
  2393. (!options.end || options.end instanceof Uint8Array)
  2394. ) {
  2395. startAddress = saveKey(
  2396. options.start,
  2397. orderedBinary.writeKey,
  2398. iterable,
  2399. maxKeySize,
  2400. );
  2401. keyBytesView.setFloat64(
  2402. START_ADDRESS_POSITION,
  2403. startAddress,
  2404. true,
  2405. );
  2406. endAddress = saveKey(
  2407. options.end,
  2408. orderedBinary.writeKey,
  2409. iterable,
  2410. maxKeySize,
  2411. );
  2412. } else {
  2413. throw new Error(
  2414. 'Only key-based encoding is supported for start/end values',
  2415. );
  2416. }
  2417. }
  2418. } else
  2419. endAddress = saveKey(
  2420. reverse && !('end' in options)
  2421. ? DEFAULT_BEGINNING_KEY
  2422. : options.end,
  2423. store.writeKey,
  2424. iterable,
  2425. maxKeySize,
  2426. );
  2427. return position(
  2428. cursorAddress,
  2429. flags,
  2430. offset || 0,
  2431. keySize,
  2432. endAddress,
  2433. );
  2434. }
  2435. function finishCursor() {
  2436. if (!cursor || txn.isDone) return;
  2437. if (iterable.onDone) iterable.onDone();
  2438. if (cursorRenewId) txn.renewingRefCount--;
  2439. if (txn.refCount <= 1 && txn.notCurrent) {
  2440. cursor.close(); // this must be closed before the transaction is aborted or it can cause a
  2441. // segmentation fault
  2442. }
  2443. if (txn.done) txn.done();
  2444. else if (--txn.refCount <= 0 && txn.notCurrent) {
  2445. txn.abort();
  2446. txn.isDone = true;
  2447. }
  2448. if (!txn.isDone) {
  2449. if (db.availableCursor || txn != readTxn) {
  2450. cursor.close();
  2451. } else {
  2452. // try to reuse it
  2453. db.availableCursor = cursor;
  2454. db.cursorTxn = txn;
  2455. }
  2456. }
  2457. cursor = null;
  2458. }
  2459. return {
  2460. next() {
  2461. let keySize, lastSize;
  2462. if (cursorRenewId && (cursorRenewId != renewId || txn.isDone)) {
  2463. if (flags & 0x10000) flags = flags & ~0x10000; // turn off exclusive start when repositioning
  2464. resetCursor();
  2465. keySize = position$1(0);
  2466. }
  2467. if (!cursor) {
  2468. return ITERATOR_DONE;
  2469. }
  2470. if (count === 0) {
  2471. // && includeValues) // on first entry, get current value if we need to
  2472. keySize = position$1(options.offset);
  2473. } else keySize = iterate(cursorAddress);
  2474. if (keySize <= 0 || count++ >= limit) {
  2475. if (keySize < -30700 && keySize !== -30798) lmdbError(keySize);
  2476. finishCursor();
  2477. return ITERATOR_DONE;
  2478. }
  2479. if (!valuesForKey || snapshot === false) {
  2480. if (keySize > 20000) {
  2481. if (keySize > 0x1000000) lmdbError(keySize - 0x100000000);
  2482. throw new Error('Invalid key size ' + keySize.toString(16));
  2483. }
  2484. currentKey = store.readKey(keyBytes, 32, keySize + 32);
  2485. }
  2486. if (includeValues) {
  2487. let value;
  2488. lastSize = keyBytesView.getUint32(0, true);
  2489. let bufferId = keyBytesView.getUint32(4, true);
  2490. let bytes;
  2491. if (bufferId) {
  2492. bytes = getMMapBuffer(bufferId, lastSize);
  2493. if (store.encoding === 'binary') bytes = Buffer.from(bytes);
  2494. } else {
  2495. bytes = compression ? compression.getValueBytes : getValueBytes;
  2496. if (lastSize > bytes.maxLength) {
  2497. store.lastSize = lastSize;
  2498. asSafeBuffer = store.encoding === 'binary';
  2499. try {
  2500. bytes = store._returnLargeBuffer(() =>
  2501. getCurrentValue(cursorAddress),
  2502. );
  2503. } finally {
  2504. asSafeBuffer = false;
  2505. }
  2506. } else bytes.length = lastSize;
  2507. }
  2508. if (store.decoder) {
  2509. value = store.decoder.decode(bytes, lastSize);
  2510. } else if (store.encoding == 'binary')
  2511. value = bytes.isGlobal
  2512. ? Uint8ArraySlice.call(bytes, 0, lastSize)
  2513. : bytes;
  2514. else {
  2515. // use the faster utf8Slice if available, otherwise fall back to TextDecoder (a little slower)
  2516. // note applying Buffer's utf8Slice to a Uint8Array works in Node, but not in Bun.
  2517. value = bytes.utf8Slice
  2518. ? bytes.utf8Slice(0, lastSize)
  2519. : textDecoder.decode(
  2520. Uint8ArraySlice.call(bytes, 0, lastSize),
  2521. );
  2522. if (store.encoding == 'json' && value)
  2523. value = JSON.parse(value);
  2524. }
  2525. if (includeVersions)
  2526. return {
  2527. value: {
  2528. key: currentKey,
  2529. value,
  2530. version: getLastVersion(),
  2531. },
  2532. };
  2533. else if (valuesForKey)
  2534. return {
  2535. value,
  2536. };
  2537. else
  2538. return {
  2539. value: {
  2540. key: currentKey,
  2541. value,
  2542. },
  2543. };
  2544. } else if (includeVersions) {
  2545. return {
  2546. value: {
  2547. key: currentKey,
  2548. version: getLastVersion(),
  2549. },
  2550. };
  2551. } else {
  2552. return {
  2553. value: currentKey,
  2554. };
  2555. }
  2556. },
  2557. return() {
  2558. finishCursor();
  2559. return ITERATOR_DONE;
  2560. },
  2561. throw() {
  2562. finishCursor();
  2563. return ITERATOR_DONE;
  2564. },
  2565. };
  2566. };
  2567. return iterable;
  2568. },
  2569. getMany(keys, callback) {
  2570. // this is an asynchronous get for multiple keys. It actually works by prefetching asynchronously,
  2571. // allowing a separate thread/task to absorb the potentially largest cost: hard page faults (and disk I/O).
  2572. // And then we just do standard sync gets (to deserialized data) to fulfil the callback/promise
  2573. // once the prefetch occurs
  2574. let promise = callback
  2575. ? undefined
  2576. : new Promise(
  2577. (resolve) => (callback = (error, results) => resolve(results)),
  2578. );
  2579. this.prefetch(keys, () => {
  2580. let results = new Array(keys.length);
  2581. for (let i = 0, l = keys.length; i < l; i++) {
  2582. results[i] = get.call(this, keys[i]);
  2583. }
  2584. callback(null, results);
  2585. });
  2586. return promise;
  2587. },
  2588. getSharedBufferForGet(id, options) {
  2589. let txn =
  2590. env.writeTxn ||
  2591. (options && options.transaction) ||
  2592. (readTxnRenewed ? readTxn : renewReadTxn(this));
  2593. this.lastSize = this.keyIsCompatibility
  2594. ? txn.getBinaryShared(id)
  2595. : this.db.get(this.writeKey(id, keyBytes, 0));
  2596. if (this.lastSize === -30798) {
  2597. // not found code
  2598. return; //undefined
  2599. }
  2600. return this.lastSize;
  2601. },
  2602. prefetch(keys, callback) {
  2603. if (!keys) throw new Error('An array of keys must be provided');
  2604. if (!keys.length) {
  2605. if (callback) {
  2606. callback(null);
  2607. return;
  2608. } else return Promise.resolve();
  2609. }
  2610. let buffers = [];
  2611. let startPosition;
  2612. let bufferHolder = {};
  2613. let lastBuffer;
  2614. for (let key of keys) {
  2615. let position;
  2616. if (key && key.key !== undefined && key.value !== undefined) {
  2617. position = saveKey(
  2618. key.value,
  2619. this.writeKey,
  2620. bufferHolder,
  2621. maxKeySize,
  2622. 0x80000000,
  2623. );
  2624. saveReferenceToBuffer();
  2625. saveKey(key.key, this.writeKey, bufferHolder, maxKeySize);
  2626. } else {
  2627. position = saveKey(key, this.writeKey, bufferHolder, maxKeySize);
  2628. }
  2629. if (!startPosition) startPosition = position;
  2630. saveReferenceToBuffer();
  2631. }
  2632. function saveReferenceToBuffer() {
  2633. if (bufferHolder.saveBuffer != lastBuffer) {
  2634. buffers.push(bufferHolder.saveBuffer);
  2635. lastBuffer = bufferHolder.saveBuffer;
  2636. }
  2637. }
  2638. saveKey(undefined, this.writeKey, bufferHolder, maxKeySize);
  2639. saveReferenceToBuffer();
  2640. outstandingReads++;
  2641. prefetch(this.dbAddress, startPosition, (error) => {
  2642. outstandingReads--;
  2643. if (error)
  2644. console.error('Error with prefetch', buffers); // partly exists to keep the buffers pinned in memory
  2645. else callback(null);
  2646. });
  2647. if (!callback) return new Promise((resolve) => (callback = resolve));
  2648. },
  2649. useReadTransaction() {
  2650. let txn = readTxnRenewed ? readTxn : renewReadTxn(this);
  2651. if (!txn.use) {
  2652. throw new Error('Can not use read transaction from a closed database');
  2653. }
  2654. // because the renew actually happens lazily in read operations, renew needs to be explicit
  2655. // here in order to actually secure a real read transaction. Try to only do it if necessary;
  2656. // once it has a refCount, it should be good to go
  2657. if (!(readTxn.refCount - (readTxn.renewingRefCount || 0) > 0))
  2658. txn.renew();
  2659. txn.use();
  2660. return txn;
  2661. },
  2662. close(callback) {
  2663. this.status = 'closing';
  2664. let txnPromise;
  2665. if (this.isRoot) {
  2666. // if it is root, we need to abort and/or wait for transactions to finish
  2667. if (readTxn) {
  2668. try {
  2669. readTxn.abort();
  2670. } catch (error) {}
  2671. } else readTxn = {};
  2672. readTxn.isDone = true;
  2673. Object.defineProperty(readTxn, 'renew', {
  2674. value: () => {
  2675. throw new Error('Can not read from a closed database');
  2676. },
  2677. configurable: true,
  2678. });
  2679. Object.defineProperty(readTxn, 'use', {
  2680. value: () => {
  2681. throw new Error('Can not read from a closed database');
  2682. },
  2683. configurable: true,
  2684. });
  2685. readTxnRenewed = null;
  2686. txnPromise = this._endWrites && this._endWrites();
  2687. }
  2688. const doClose = () => {
  2689. if (this.isRoot) {
  2690. if (outstandingReads > 0) {
  2691. return new Promise((resolve) =>
  2692. setTimeout(() => resolve(doClose()), 1),
  2693. );
  2694. }
  2695. env.address = 0;
  2696. try {
  2697. env.close();
  2698. } catch (error) {}
  2699. } else this.db.close();
  2700. this.status = 'closed';
  2701. if (callback) callback();
  2702. };
  2703. if (txnPromise) return txnPromise.then(doClose);
  2704. else {
  2705. doClose();
  2706. return Promise.resolve();
  2707. }
  2708. },
  2709. getStats() {
  2710. env.writeTxn || (readTxnRenewed ? readTxn : renewReadTxn(this));
  2711. let dbStats = this.db.stat();
  2712. dbStats.root = env.stat();
  2713. Object.assign(dbStats, env.info());
  2714. dbStats.free = env.freeStat();
  2715. return dbStats;
  2716. },
  2717. });
  2718. let get = LMDBStore.prototype.get;
  2719. let lastReadTxnRef;
  2720. function getMMapBuffer(bufferId, size) {
  2721. let buffer = mmaps[bufferId];
  2722. if (!buffer) {
  2723. buffer = mmaps[bufferId] = getSharedBuffer(bufferId, env.address);
  2724. }
  2725. let offset = keyBytesView.getUint32(8, true);
  2726. return new Uint8Array(buffer, offset, size);
  2727. }
  2728. function renewReadTxn(store) {
  2729. if (!env.address) {
  2730. throw new Error('Can not renew a transaction from a closed database');
  2731. }
  2732. if (!readTxn) {
  2733. let retries = 0;
  2734. let waitArray;
  2735. do {
  2736. try {
  2737. let lastReadTxn = lastReadTxnRef && lastReadTxnRef.deref();
  2738. readTxn = new Txn(
  2739. env,
  2740. 0x20000,
  2741. lastReadTxn && !lastReadTxn.isDone && lastReadTxn,
  2742. );
  2743. if (readTxn.address == 0) {
  2744. readTxn = lastReadTxn;
  2745. if (readTxn.notCurrent) readTxn.notCurrent = false;
  2746. }
  2747. break;
  2748. } catch (error) {
  2749. if (error.message.includes('temporarily')) {
  2750. if (!waitArray)
  2751. waitArray = new Int32Array(new SharedArrayBuffer(4), 0, 1);
  2752. Atomics.wait(waitArray, 0, 0, retries * 2);
  2753. } else throw error;
  2754. }
  2755. } while (retries++ < 100);
  2756. }
  2757. // we actually don't renew here, we let the renew take place in the next
  2758. // lmdb native read/call so as to avoid an extra native call
  2759. readTxnRenewed = setTimeout(resetReadTxn, 0);
  2760. store.emit('begin-transaction');
  2761. return readTxn;
  2762. }
  2763. function resetReadTxn() {
  2764. renewId++;
  2765. if (readTxnRenewed) {
  2766. readTxnRenewed = null;
  2767. if (readTxn.refCount - (readTxn.renewingRefCount || 0) > 0) {
  2768. readTxn.notCurrent = true;
  2769. lastReadTxnRef = new WeakRef(readTxn);
  2770. readTxn = null;
  2771. } else if (readTxn.address && !readTxn.isDone) {
  2772. resetTxn(readTxn.address);
  2773. } else {
  2774. console.warn('Attempt to reset an invalid read txn', readTxn);
  2775. throw new Error('Attempt to reset an invalid read txn');
  2776. }
  2777. }
  2778. }
  2779. }
  2780. function makeReusableBuffer(size) {
  2781. let bytes =
  2782. typeof Buffer != 'undefined' ? Buffer.alloc(size) : new Uint8Array(size);
  2783. bytes.maxLength = size;
  2784. Object.defineProperty(bytes, 'length', {
  2785. value: size,
  2786. writable: true,
  2787. configurable: true,
  2788. });
  2789. return bytes;
  2790. }
  2791. Txn.prototype.done = function () {
  2792. this.refCount--;
  2793. if (this.refCount === 0 && this.notCurrent) {
  2794. this.abort();
  2795. this.isDone = true;
  2796. } else if (this.refCount < 0)
  2797. throw new Error('Can not finish a transaction more times than it was used');
  2798. };
  2799. Txn.prototype.use = function () {
  2800. this.refCount = (this.refCount || 0) + 1;
  2801. };
  2802. let readInstructions,
  2803. readCallbacks = new Map(),
  2804. uint32Instructions,
  2805. instructionsDataView = { setFloat64() {}, setUint32() {} },
  2806. instructionsAddress;
  2807. let savePosition = 8000;
  2808. let DYNAMIC_KEY_BUFFER_SIZE = 8192;
  2809. function allocateInstructionsBuffer() {
  2810. readInstructions =
  2811. typeof Buffer != 'undefined'
  2812. ? Buffer.alloc(DYNAMIC_KEY_BUFFER_SIZE)
  2813. : new Uint8Array(DYNAMIC_KEY_BUFFER_SIZE);
  2814. uint32Instructions = new Int32Array(
  2815. readInstructions.buffer,
  2816. 0,
  2817. readInstructions.buffer.byteLength >> 2,
  2818. );
  2819. uint32Instructions[2] = 0xf0000000; // indicates a new read task must be started
  2820. instructionsAddress = readInstructions.buffer.address = getAddress(
  2821. readInstructions.buffer,
  2822. );
  2823. readInstructions.dataView = instructionsDataView = new DataView(
  2824. readInstructions.buffer,
  2825. readInstructions.byteOffset,
  2826. readInstructions.byteLength,
  2827. );
  2828. savePosition = 0;
  2829. }
  2830. function recordReadInstruction(
  2831. txnAddress,
  2832. dbi,
  2833. key,
  2834. writeKey,
  2835. maxKeySize,
  2836. callback,
  2837. ) {
  2838. if (savePosition > 7800) {
  2839. allocateInstructionsBuffer();
  2840. }
  2841. let start = savePosition;
  2842. let keyPosition = savePosition + 16;
  2843. try {
  2844. savePosition =
  2845. key === undefined
  2846. ? keyPosition
  2847. : writeKey(key, readInstructions, keyPosition);
  2848. } catch (error) {
  2849. if (error.name == 'RangeError') {
  2850. if (8180 - start < maxKeySize) {
  2851. allocateInstructionsBuffer(); // try again:
  2852. return recordReadInstruction(
  2853. txnAddress,
  2854. dbi,
  2855. key,
  2856. writeKey,
  2857. maxKeySize,
  2858. callback,
  2859. );
  2860. }
  2861. throw new Error('Key was too large, max key size is ' + maxKeySize);
  2862. } else throw error;
  2863. }
  2864. let length = savePosition - keyPosition;
  2865. if (length > maxKeySize) {
  2866. savePosition = start;
  2867. throw new Error(
  2868. 'Key of size ' + length + ' was too large, max key size is ' + maxKeySize,
  2869. );
  2870. }
  2871. uint32Instructions[(start >> 2) + 3] = length; // save the length
  2872. uint32Instructions[(start >> 2) + 2] = dbi;
  2873. savePosition = (savePosition + 12) & 0xfffffc;
  2874. instructionsDataView.setFloat64(start, txnAddress, true);
  2875. let callbackId = addReadCallback(() => {
  2876. let position = start >> 2;
  2877. let rc = thisInstructions[position];
  2878. callback(
  2879. rc,
  2880. thisInstructions[position + 1],
  2881. thisInstructions[position + 2],
  2882. thisInstructions[position + 3],
  2883. );
  2884. });
  2885. let thisInstructions = uint32Instructions;
  2886. //if (start === 0)
  2887. return startRead(instructionsAddress + start, callbackId, {}, 'read');
  2888. //else
  2889. //nextRead(start);
  2890. }
  2891. let nextCallbackId = 0;
  2892. let addReadCallback = globalThis.__lmdb_read_callback;
  2893. if (!addReadCallback) {
  2894. addReadCallback = globalThis.__lmdb_read_callback = function (callback) {
  2895. let callbackId = nextCallbackId++;
  2896. readCallbacks.set(callbackId, callback);
  2897. return callbackId;
  2898. };
  2899. setReadCallback(function (callbackId) {
  2900. readCallbacks.get(callbackId)();
  2901. readCallbacks.delete(callbackId);
  2902. });
  2903. }
  2904. let getLastVersion$1, getLastTxnId$1;
  2905. const mapGet = Map.prototype.get;
  2906. const CachingStore = (Store, env) => {
  2907. let childTxnChanges;
  2908. return class LMDBStore extends Store {
  2909. constructor(dbName, options) {
  2910. super(dbName, options);
  2911. if (!env.cacheCommitter) {
  2912. env.cacheCommitter = true;
  2913. this.on('aftercommit', ({ next, last, txnId }) => {
  2914. do {
  2915. let meta = next.meta;
  2916. let store = meta && meta.store;
  2917. if (store) {
  2918. if (next.flag & FAILED_CONDITION)
  2919. store.cache.delete(meta.key); // just delete it from the map
  2920. else {
  2921. let expirationPriority = meta.valueSize >> 10;
  2922. let cache = store.cache;
  2923. let entry = mapGet.call(cache, meta.key);
  2924. if (entry && !entry.txnId) {
  2925. entry.txnId = txnId;
  2926. cache.used(entry, expirationPriority + 4); // this will enter it into the LRFU (with a little lower priority than a read)
  2927. }
  2928. }
  2929. }
  2930. } while (next != last && (next = next.next));
  2931. });
  2932. }
  2933. this.db.cachingDb = this;
  2934. if (options.cache.clearKeptInterval)
  2935. options.cache.clearKeptObjects = exports.clearKeptObjects;
  2936. this.cache = new WeakLRUCache(options.cache);
  2937. if (options.cache.validated) this.cache.validated = true;
  2938. }
  2939. get isCaching() {
  2940. return true;
  2941. }
  2942. get(id, options) {
  2943. let value;
  2944. if (this.cache.validated) {
  2945. let entry = this.cache.get(id);
  2946. if (entry) {
  2947. let cachedValue = entry.value;
  2948. if (entry.txnId != null) {
  2949. value = super.get(id, {
  2950. ifNotTxnId: entry.txnId,
  2951. transaction: options && options.transaction,
  2952. });
  2953. if (value === UNMODIFIED) return cachedValue;
  2954. } // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
  2955. else return cachedValue;
  2956. } else value = super.get(id, options);
  2957. } else if (options && options.transaction) {
  2958. return super.get(id, options);
  2959. } else {
  2960. value = this.cache.getValue(id);
  2961. if (value !== undefined) {
  2962. return value;
  2963. }
  2964. value = super.get(id);
  2965. }
  2966. if (
  2967. value &&
  2968. typeof value === 'object' &&
  2969. !options &&
  2970. typeof id !== 'object'
  2971. ) {
  2972. let entry = this.cache.setValue(id, value, this.lastSize >> 10);
  2973. if (this.useVersions) {
  2974. entry.version = getLastVersion$1();
  2975. }
  2976. if (this.cache.validated) entry.txnId = getLastTxnId$1();
  2977. }
  2978. return value;
  2979. }
  2980. getEntry(id, options) {
  2981. let entry, value;
  2982. if (this.cache.validated) {
  2983. entry = this.cache.get(id);
  2984. if (entry) {
  2985. if (entry.txnId != null) {
  2986. value = super.get(id, {
  2987. ifNotTxnId: entry.txnId,
  2988. transaction: options && options.transaction,
  2989. });
  2990. if (value === UNMODIFIED) return entry;
  2991. } // with no txn id we do not validate; this is the state of a cached value after a write before it transacts
  2992. else return entry;
  2993. } else value = super.get(id, options);
  2994. } else if (options && options.transaction) {
  2995. return super.getEntry(id, options);
  2996. } else {
  2997. entry = this.cache.get(id);
  2998. if (entry !== undefined) {
  2999. return entry;
  3000. }
  3001. value = super.get(id);
  3002. }
  3003. if (value === undefined) return;
  3004. if (value && typeof value === 'object' && typeof id !== 'object') {
  3005. entry = this.cache.setValue(id, value, this.lastSize >> 10);
  3006. } else entry = { value };
  3007. if (this.useVersions) entry.version = getLastVersion$1();
  3008. if (this.cache.validated) entry.txnId = getLastTxnId$1();
  3009. return entry;
  3010. }
  3011. putEntry(id, entry, ifVersion) {
  3012. let result = super.put(id, entry.value, entry.version, ifVersion);
  3013. if (typeof id === 'object') return result;
  3014. if (result && result.then)
  3015. this.cache.setManually(id, entry); // set manually so we can keep it pinned in memory until it is committed
  3016. // sync operation, immediately add to cache
  3017. else this.cache.set(id, entry);
  3018. }
  3019. put(id, value, version, ifVersion) {
  3020. let result = super.put(id, value, version, ifVersion);
  3021. if (typeof id !== 'object') {
  3022. if (value && value['\x10binary-data\x02']) {
  3023. // don't cache binary data, since it will be decoded on get
  3024. this.cache.delete(id);
  3025. return result;
  3026. }
  3027. let entry;
  3028. if (this.cachePuts === false) {
  3029. // we are not caching puts, clear the entry at least
  3030. this.cache.delete(id);
  3031. } else {
  3032. if (result?.isSync) {
  3033. // sync operation, immediately add to cache
  3034. if (result.result)
  3035. // if it succeeds
  3036. entry = this.cache.setValue(id, value, 0);
  3037. else {
  3038. this.cache.delete(id);
  3039. return result;
  3040. } // sync failure
  3041. // otherwise keep it pinned in memory until it is committed
  3042. } else entry = this.cache.setValue(id, value, -1);
  3043. }
  3044. if (childTxnChanges) childTxnChanges.add(id);
  3045. if (version !== undefined && entry)
  3046. entry.version =
  3047. typeof version === 'object' ? version.version : version;
  3048. }
  3049. return result;
  3050. }
  3051. putSync(id, value, version, ifVersion) {
  3052. let result = super.putSync(id, value, version, ifVersion);
  3053. if (id !== 'object') {
  3054. // sync operation, immediately add to cache, otherwise keep it pinned in memory until it is committed
  3055. if (
  3056. value &&
  3057. this.cachePuts !== false &&
  3058. typeof value === 'object' &&
  3059. result
  3060. ) {
  3061. let entry = this.cache.setValue(id, value);
  3062. if (childTxnChanges) childTxnChanges.add(id);
  3063. if (version !== undefined) {
  3064. entry.version =
  3065. typeof version === 'object' ? version.version : version;
  3066. }
  3067. } // it is possible that a value used to exist here
  3068. else this.cache.delete(id);
  3069. }
  3070. return result;
  3071. }
  3072. remove(id, ifVersion) {
  3073. this.cache.delete(id);
  3074. return super.remove(id, ifVersion);
  3075. }
  3076. removeSync(id, ifVersion) {
  3077. this.cache.delete(id);
  3078. return super.removeSync(id, ifVersion);
  3079. }
  3080. clearAsync(callback) {
  3081. this.cache.clear();
  3082. return super.clearAsync(callback);
  3083. }
  3084. clearSync() {
  3085. this.cache.clear();
  3086. super.clearSync();
  3087. }
  3088. childTransaction(callback) {
  3089. return super.childTransaction(() => {
  3090. let cache = this.cache;
  3091. let previousChanges = childTxnChanges;
  3092. try {
  3093. childTxnChanges = new Set();
  3094. return when(
  3095. callback(),
  3096. (result) => {
  3097. if (result === ABORT) return abort();
  3098. childTxnChanges = previousChanges;
  3099. return result;
  3100. },
  3101. abort,
  3102. );
  3103. } catch (error) {
  3104. abort(error);
  3105. }
  3106. function abort(error) {
  3107. // if the transaction was aborted, remove all affected entries from cache
  3108. for (let id of childTxnChanges) cache.delete(id);
  3109. childTxnChanges = previousChanges;
  3110. if (error) throw error;
  3111. else return ABORT;
  3112. }
  3113. });
  3114. }
  3115. doesExist(key, versionOrValue) {
  3116. let entry = this.cache.get(key);
  3117. if (entry) {
  3118. if (versionOrValue == null) {
  3119. return versionOrValue !== null;
  3120. } else if (this.useVersions) {
  3121. return (
  3122. versionOrValue === IF_EXISTS$1 || entry.version === versionOrValue
  3123. );
  3124. }
  3125. }
  3126. return super.doesExist(key, versionOrValue);
  3127. }
  3128. };
  3129. };
  3130. function setGetLastVersion(get, getTxnId) {
  3131. getLastVersion$1 = get;
  3132. getLastTxnId$1 = getTxnId;
  3133. }
  3134. let moduleRequire = typeof require == 'function' && require;
  3135. function setRequire(require) {
  3136. moduleRequire = require;
  3137. }
  3138. setGetLastVersion(getLastVersion, getLastTxnId);
  3139. let keyBytes, keyBytesView;
  3140. const { onExit, getEnvsPointer, setEnvsPointer, getEnvFlags, setJSFlags } = nativeAddon;
  3141. if (globalThis.__lmdb_envs__)
  3142. setEnvsPointer(globalThis.__lmdb_envs__);
  3143. else
  3144. globalThis.__lmdb_envs__ = getEnvsPointer();
  3145. // this is hard coded as an upper limit because it is important assumption of the fixed buffers in writing instructions
  3146. // this corresponds to the max key size for 8KB pages
  3147. const MAX_KEY_SIZE = 4026;
  3148. // this is used as the key size by default because default page size is OS page size, which is usually
  3149. // 4KB (but is 16KB on M-series MacOS), and this keeps a consistent max key size when no page size specified.
  3150. const DEFAULT_MAX_KEY_SIZE = 1978;
  3151. const DEFAULT_COMMIT_DELAY = 0;
  3152. const allDbs = new Map();
  3153. let defaultCompression;
  3154. let hasRegisteredOnExit;
  3155. function open(path$1, options) {
  3156. if (nativeAddon.open) {
  3157. if (nativeAddon.open !== open) {
  3158. // this is the case when lmdb-js has been opened in both ESM and CJS mode, which means that there are two
  3159. // separate JS modules, but they are both using the same native module.
  3160. getLastVersion = nativeAddon.getLastVersion;
  3161. getLastTxnId = nativeAddon.getLastTxnId;
  3162. setGetLastVersion(getLastVersion, getLastTxnId);
  3163. return nativeAddon.open(path$1, options);
  3164. }
  3165. } else {
  3166. nativeAddon.open = open;
  3167. nativeAddon.getLastVersion = getLastVersion;
  3168. nativeAddon.getLastTxnId = getLastTxnId;
  3169. }
  3170. if (!keyBytes) // TODO: Consolidate get buffer and key buffer (don't think we need both)
  3171. allocateFixedBuffer();
  3172. if (typeof path$1 == 'object' && !options) {
  3173. options = path$1;
  3174. path$1 = options.path;
  3175. }
  3176. options = options || {};
  3177. let noFSAccess = options.noFSAccess; // this can only be configured on open, can't let users change it
  3178. let userOptions = options;
  3179. if (path$1 == null) {
  3180. options = Object.assign({
  3181. deleteOnClose: true,
  3182. noSync: true,
  3183. }, options);
  3184. path$1 = tmpdir() + '/' + Math.floor(Math.random() * 2821109907455).toString(36) + '.mdb';
  3185. } else if (!options)
  3186. options = {};
  3187. let extension = path.extname(path$1);
  3188. let name = path.basename(path$1, extension);
  3189. let is32Bit = arch$1().endsWith('32');
  3190. let isLegacyLMDB = exports.version.patch < 90;
  3191. let remapChunks = (options.remapChunks || options.encryptionKey || (options.mapSize ?
  3192. (is32Bit && options.mapSize > 0x100000000) : // larger than fits in address space, must use dynamic maps
  3193. is32Bit)) && !isLegacyLMDB; // without a known map size, we default to being able to handle large data correctly/well*/
  3194. let userMapSize = options.mapSize;
  3195. options = Object.assign({
  3196. noSubdir: Boolean(extension),
  3197. isRoot: true,
  3198. maxDbs: 12,
  3199. remapChunks,
  3200. keyBytes,
  3201. overlappingSync: (options.noSync || options.readOnly) ? false : (os != 'win32'),
  3202. // default map size limit of 4 exabytes when using remapChunks, since it is not preallocated and we can
  3203. // make it super huge.
  3204. mapSize: remapChunks ? 0x10000000000000 :
  3205. isLegacyLMDB ? is32Bit ? 0x1000000 : 0x100000000 : 0x20000, // Otherwise we start small with 128KB
  3206. safeRestore: process.env.LMDB_RESTORE == 'safe',
  3207. }, options);
  3208. options.path = path$1;
  3209. if (options.asyncTransactionOrder == 'strict') {
  3210. options.strictAsyncOrder = true;
  3211. }
  3212. if (nativeAddon.version.major + nativeAddon.version.minor / 100 + nativeAddon.version.patch / 10000 < 0.0980) {
  3213. options.overlappingSync = false; // not support on older versions
  3214. options.trackMetrics = false;
  3215. options.usePreviousSnapshot = false;
  3216. options.safeRestore = false;
  3217. options.remapChunks = false;
  3218. if (!userMapSize) options.mapSize = 0x40000000; // 1 GB
  3219. }
  3220. if (!exists(options.noSubdir ? path.dirname(path$1) : path$1))
  3221. fs.mkdirSync(options.noSubdir ? path.dirname(path$1) : path$1, { recursive: true }
  3222. );
  3223. function makeCompression(compressionOptions) {
  3224. if (compressionOptions instanceof Compression)
  3225. return compressionOptions;
  3226. let useDefault = typeof compressionOptions != 'object';
  3227. if (useDefault && defaultCompression)
  3228. return defaultCompression;
  3229. compressionOptions = Object.assign({
  3230. threshold: 1000,
  3231. dictionary: fs.readFileSync(new URL('./dict/dict.txt', (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)).replace(/dist[\\\/]index.cjs$/, ''))),
  3232. getValueBytes: makeReusableBuffer(0),
  3233. }, compressionOptions);
  3234. let compression = Object.assign(new Compression(compressionOptions), compressionOptions);
  3235. if (useDefault)
  3236. defaultCompression = compression;
  3237. return compression;
  3238. }
  3239. if (isLegacyLMDB) {
  3240. // legacy LMDB, turn off these options
  3241. Object.assign(options, { overlappingSync: false, remapChunks: false, safeRestore: false });
  3242. }
  3243. if (options.compression)
  3244. options.compression = makeCompression(options.compression);
  3245. let flags =
  3246. (options.overlappingSync ? 0x1000 : 0) |
  3247. (options.noSubdir ? 0x4000 : 0) |
  3248. (options.noSync ? 0x10000 : 0) |
  3249. (options.readOnly ? 0x20000 : 0) |
  3250. (options.noMetaSync ? 0x40000 : 0) |
  3251. (options.useWritemap ? 0x80000 : 0) |
  3252. (options.mapAsync ? 0x100000 : 0) |
  3253. (options.noReadAhead ? 0x800000 : 0) |
  3254. (options.noMemInit ? 0x1000000 : 0) |
  3255. (options.usePreviousSnapshot ? 0x2000000 : 0) |
  3256. (options.remapChunks ? 0x4000000 : 0) |
  3257. (options.safeRestore ? 0x800 : 0) |
  3258. (options.trackMetrics ? 0x400 : 0);
  3259. let env = new Env();
  3260. let jsFlags = (options.overlappingSync ? 0x1000 : 0) |
  3261. (options.separateFlushed ? 1 : 0) |
  3262. (options.deleteOnClose ? 2 : 0);
  3263. let rc = env.open(options, flags, jsFlags);
  3264. env.path = path$1;
  3265. if (rc)
  3266. lmdbError(rc);
  3267. delete options.keyBytes; // no longer needed, don't copy to stores
  3268. let maxKeySize = env.getMaxKeySize();
  3269. maxKeySize = Math.min(maxKeySize, options.pageSize ? MAX_KEY_SIZE : DEFAULT_MAX_KEY_SIZE);
  3270. flags = getEnvFlags(env.address); // re-retrieve them, they are not necessarily the same if we are connecting to an existing env
  3271. if (flags & 0x1000) {
  3272. if (userOptions.noSync) {
  3273. env.close();
  3274. throw new Error('Can not set noSync on a database that was opened with overlappingSync');
  3275. }
  3276. } else if (options.overlappingSync) {
  3277. if (userOptions.overlappingSync) {
  3278. env.close();
  3279. throw new Error('Can not enable overlappingSync on a database that was opened without this flag');
  3280. }
  3281. options.overlappingSync = false;
  3282. jsFlags = jsFlags & 0xff; // clear overlapping sync
  3283. setJSFlags(env.address, jsFlags);
  3284. }
  3285. env.readerCheck(); // clear out any stale entries
  3286. if ((options.overlappingSync || options.deleteOnClose) && !hasRegisteredOnExit && process.on) {
  3287. hasRegisteredOnExit = true;
  3288. process.on('exit', onExit);
  3289. }
  3290. class LMDBStore extends EventEmitter {
  3291. constructor(dbName, dbOptions) {
  3292. super();
  3293. if (dbName === undefined)
  3294. throw new Error('Database name must be supplied in name property (may be null for root database)');
  3295. if (options.compression && dbOptions.compression !== false && typeof dbOptions.compression != 'object')
  3296. dbOptions.compression = options.compression; // use the parent compression if available
  3297. else if (dbOptions.compression)
  3298. dbOptions.compression = makeCompression(dbOptions.compression);
  3299. if (dbOptions.dupSort && (dbOptions.useVersions || dbOptions.cache)) {
  3300. throw new Error('The dupSort flag can not be combined with versions or caching');
  3301. }
  3302. let keyIsBuffer = dbOptions.keyIsBuffer;
  3303. if (dbOptions.keyEncoding == 'uint32') {
  3304. dbOptions.keyIsUint32 = true;
  3305. } else if (dbOptions.keyEncoder) {
  3306. if (dbOptions.keyEncoder.enableNullTermination) {
  3307. dbOptions.keyEncoder.enableNullTermination();
  3308. } else
  3309. keyIsBuffer = true;
  3310. } else if (dbOptions.keyEncoding == 'binary') {
  3311. keyIsBuffer = true;
  3312. }
  3313. let flags = (dbOptions.reverseKey ? 0x02 : 0) |
  3314. (dbOptions.dupSort ? 0x04 : 0) |
  3315. (dbOptions.dupFixed ? 0x10 : 0) |
  3316. (dbOptions.integerDup ? 0x20 : 0) |
  3317. (dbOptions.reverseDup ? 0x40 : 0) |
  3318. (!options.readOnly && dbOptions.create !== false ? 0x40000 : 0) |
  3319. (dbOptions.useVersions ? 0x100 : 0);
  3320. let keyType = (dbOptions.keyIsUint32 || dbOptions.keyEncoding == 'uint32') ? 2 : keyIsBuffer ? 3 : 0;
  3321. if (keyType == 2)
  3322. flags |= 0x08; // integer key
  3323. if (options.readOnly) {
  3324. // in read-only mode we use a read-only txn to open the database
  3325. // TODO: LMDB is actually not entirely thread-safe when it comes to opening databases with
  3326. // read-only transactions since there is a race condition on setting the update dbis that
  3327. // occurs outside the lock
  3328. // make sure we are using a fresh read txn, so we don't want to share with a cursor txn
  3329. this.resetReadTxn();
  3330. this.ensureReadTxn();
  3331. this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
  3332. } else {
  3333. this.transactionSync(() => {
  3334. this.db = new Dbi(env, flags, dbName, keyType, dbOptions.compression);
  3335. }, options.overlappingSync ? 0x10002 : 2); // no flush-sync, but synchronously commit
  3336. }
  3337. this._commitReadTxn(); // current read transaction becomes invalid after opening another db
  3338. if (!this.db || this.db.dbi == 0xffffffff) {// not found
  3339. throw new Error('Database not found')
  3340. }
  3341. this.dbAddress = this.db.address;
  3342. this.db.name = dbName || null;
  3343. this.name = dbName;
  3344. this.status = 'open';
  3345. this.env = env;
  3346. this.reads = 0;
  3347. this.writes = 0;
  3348. this.transactions = 0;
  3349. this.averageTransactionTime = 5;
  3350. if (dbOptions.syncBatchThreshold)
  3351. console.warn('syncBatchThreshold is no longer supported');
  3352. if (dbOptions.immediateBatchThreshold)
  3353. console.warn('immediateBatchThreshold is no longer supported');
  3354. this.commitDelay = DEFAULT_COMMIT_DELAY;
  3355. Object.assign(this, { // these are the options that are inherited
  3356. path: options.path,
  3357. encoding: options.encoding,
  3358. strictAsyncOrder: options.strictAsyncOrder,
  3359. }, dbOptions);
  3360. let Encoder;
  3361. if (this.encoder && this.encoder.Encoder) {
  3362. Encoder = this.encoder.Encoder;
  3363. this.encoder = null; // don't copy everything from the module
  3364. }
  3365. if (!Encoder && !(this.encoder && this.encoder.encode) && (!this.encoding || this.encoding == 'msgpack' || this.encoding == 'cbor')) {
  3366. Encoder = (this.encoding == 'cbor' ? moduleRequire('cbor-x').Encoder : MsgpackrEncoder);
  3367. }
  3368. if (Encoder) {
  3369. this.encoder = new Encoder(Object.assign(
  3370. assignConstrainedProperties(['copyBuffers', 'getStructures', 'saveStructures', 'useFloat32', 'useRecords', 'structuredClone', 'variableMapSize', 'useTimestamp32', 'largeBigIntToFloat', 'encodeUndefinedAsNil', 'int64AsNumber', 'onInvalidDate', 'mapsAsObjects', 'useTag259ForMaps', 'pack', 'maxSharedStructures', 'shouldShareStructure', 'randomAccessStructure', 'freezeData'],
  3371. this.sharedStructuresKey !== undefined ? this.setupSharedStructures() : {
  3372. copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
  3373. }, options, dbOptions), this.encoder));
  3374. }
  3375. if (this.encoding == 'json') {
  3376. this.encoder = {
  3377. encode: JSON.stringify,
  3378. };
  3379. } else if (this.encoder) {
  3380. this.decoder = this.encoder;
  3381. this.decoderCopies = !this.encoder.needsStableBuffer;
  3382. }
  3383. this.maxKeySize = maxKeySize;
  3384. applyKeyHandling(this);
  3385. allDbs.set(dbName ? name + '-' + dbName : name, this);
  3386. }
  3387. openDB(dbName, dbOptions) {
  3388. if (this.dupSort && this.name == null)
  3389. throw new Error('Can not open named databases if the main database is dupSort')
  3390. if (typeof dbName == 'object' && !dbOptions) {
  3391. dbOptions = dbName;
  3392. dbName = dbOptions.name;
  3393. } else
  3394. dbOptions = dbOptions || {};
  3395. try {
  3396. return dbOptions.cache ?
  3397. new (CachingStore(LMDBStore, env))(dbName, dbOptions) :
  3398. new LMDBStore(dbName, dbOptions);
  3399. } catch(error) {
  3400. if (error.message == 'Database not found')
  3401. return; // return undefined to indicate db not found
  3402. if (error.message.indexOf('MDB_DBS_FULL') > -1) {
  3403. error.message += ' (increase your maxDbs option)';
  3404. }
  3405. throw error;
  3406. }
  3407. }
  3408. open(dbOptions, callback) {
  3409. let db = this.openDB(dbOptions);
  3410. if (callback)
  3411. callback(null, db);
  3412. return db;
  3413. }
  3414. backup(path$1, compact) {
  3415. if (noFSAccess)
  3416. return;
  3417. fs.mkdirSync(path.dirname(path$1), { recursive: true });
  3418. return new Promise((resolve, reject) => env.copy(path$1, compact, (error) => {
  3419. if (error) {
  3420. reject(error);
  3421. } else {
  3422. resolve();
  3423. }
  3424. }));
  3425. }
  3426. isOperational() {
  3427. return this.status == 'open';
  3428. }
  3429. sync(callback) {
  3430. return env.sync(callback || function(error) {
  3431. if (error) {
  3432. console.error(error);
  3433. }
  3434. });
  3435. }
  3436. deleteDB() {
  3437. console.warn('deleteDB() is deprecated, use drop or dropSync instead');
  3438. return this.dropSync();
  3439. }
  3440. dropSync() {
  3441. this.transactionSync(() =>
  3442. this.db.drop({
  3443. justFreePages: false
  3444. }), options.overlappingSync ? 0x10002 : 2);
  3445. }
  3446. clear(callback) {
  3447. if (typeof callback == 'function')
  3448. return this.clearAsync(callback);
  3449. console.warn('clear() is deprecated, use clearAsync or clearSync instead');
  3450. this.clearSync();
  3451. }
  3452. clearSync() {
  3453. if (this.encoder) {
  3454. if (this.encoder.clearSharedData)
  3455. this.encoder.clearSharedData();
  3456. else if (this.encoder.structures)
  3457. this.encoder.structures = [];
  3458. }
  3459. this.transactionSync(() =>
  3460. this.db.drop({
  3461. justFreePages: true
  3462. }), options.overlappingSync ? 0x10002 : 2);
  3463. }
  3464. readerCheck() {
  3465. return env.readerCheck();
  3466. }
  3467. readerList() {
  3468. return env.readerList().join('');
  3469. }
  3470. setupSharedStructures() {
  3471. const getStructures = () => {
  3472. let lastVersion; // because we are doing a read here, we may need to save and restore the lastVersion from the last read
  3473. if (this.useVersions)
  3474. lastVersion = getLastVersion();
  3475. let buffer = this.getBinary(this.sharedStructuresKey);
  3476. if (this.useVersions)
  3477. setLastVersion(lastVersion);
  3478. return buffer && this.decoder.decode(buffer);
  3479. };
  3480. return {
  3481. saveStructures: (structures, isCompatible) => {
  3482. return this.transactionSync(() => {
  3483. let existingStructuresBuffer = this.getBinary(this.sharedStructuresKey);
  3484. let existingStructures = existingStructuresBuffer && this.decoder.decode(existingStructuresBuffer);
  3485. if (typeof isCompatible == 'function' ?
  3486. !isCompatible(existingStructures) :
  3487. (existingStructures && existingStructures.length != isCompatible))
  3488. return false; // it changed, we need to indicate that we couldn't update
  3489. this.put(this.sharedStructuresKey, structures);
  3490. }, options.overlappingSync ? 0x10000 : 0);
  3491. },
  3492. getStructures,
  3493. copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
  3494. };
  3495. }
  3496. }
  3497. // if caching class overrides putSync, don't want to double call the caching code
  3498. LMDBStore.prototype.putSync;
  3499. LMDBStore.prototype.removeSync;
  3500. addReadMethods(LMDBStore, { env, maxKeySize, keyBytes, keyBytesView, getLastVersion });
  3501. if (!options.readOnly)
  3502. addWriteMethods(LMDBStore, { env, maxKeySize, fixedBuffer: keyBytes,
  3503. resetReadTxn: LMDBStore.prototype.resetReadTxn, ...options });
  3504. LMDBStore.prototype.supports = {
  3505. permanence: true,
  3506. bufferKeys: true,
  3507. promises: true,
  3508. snapshots: true,
  3509. clear: true,
  3510. status: true,
  3511. deferredOpen: true,
  3512. openCallback: true,
  3513. };
  3514. let Class = options.cache ? CachingStore(LMDBStore, env) : LMDBStore;
  3515. return options.asClass ? Class : new Class(options.name || null, options);
  3516. }
  3517. function openAsClass(path, options) {
  3518. if (typeof path == 'object' && !options) {
  3519. options = path;
  3520. path = options.path;
  3521. }
  3522. options = options || {};
  3523. options.asClass = true;
  3524. return open(path, options);
  3525. }
  3526. function getLastVersion() {
  3527. return keyBytesView.getFloat64(16, true);
  3528. }
  3529. function setLastVersion(version) {
  3530. return keyBytesView.setFloat64(16, version, true);
  3531. }
  3532. function getLastTxnId() {
  3533. return keyBytesView.getUint32(32, true);
  3534. }
  3535. const KEY_BUFFER_SIZE = 4096;
  3536. function allocateFixedBuffer() {
  3537. keyBytes = typeof Buffer != 'undefined' ? Buffer.allocUnsafeSlow(KEY_BUFFER_SIZE) : new Uint8Array(KEY_BUFFER_SIZE);
  3538. const keyBuffer = keyBytes.buffer;
  3539. keyBytesView = keyBytes.dataView || (keyBytes.dataView = new DataView(keyBytes.buffer, 0, KEY_BUFFER_SIZE)); // max key size is actually 4026
  3540. keyBytes.uint32 = new Uint32Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 2);
  3541. keyBytes.float64 = new Float64Array(keyBuffer, 0, KEY_BUFFER_SIZE >> 3);
  3542. keyBytes.uint32.address = keyBytes.address = keyBuffer.address = getAddress(keyBuffer);
  3543. }
  3544. function exists(path) {
  3545. if (fs.existsSync)
  3546. return fs.existsSync(path);
  3547. try {
  3548. return fs.statSync(path);
  3549. } catch (error) {
  3550. return false
  3551. }
  3552. }
  3553. function assignConstrainedProperties(allowedProperties, target) {
  3554. for (let i = 2; i < arguments.length; i++) {
  3555. let source = arguments[i];
  3556. for (let key in source) {
  3557. if (allowedProperties.includes(key))
  3558. target[key] = source[key];
  3559. }
  3560. }
  3561. return target;
  3562. }
  3563. function levelup(store) {
  3564. return Object.assign(Object.create(store), {
  3565. get(key, options, callback) {
  3566. let result = store.get(key);
  3567. if (typeof options == 'function')
  3568. callback = options;
  3569. if (callback) {
  3570. if (result === undefined)
  3571. callback(new NotFoundError());
  3572. else
  3573. callback(null, result);
  3574. } else {
  3575. if (result === undefined)
  3576. return Promise.reject(new NotFoundError());
  3577. else
  3578. return Promise.resolve(result);
  3579. }
  3580. },
  3581. });
  3582. }
  3583. class NotFoundError extends Error {
  3584. constructor(message) {
  3585. super(message);
  3586. this.name = 'NotFoundError';
  3587. this.notFound = true;
  3588. }
  3589. }
  3590. orderedBinary__namespace.enableNullTermination();
  3591. setExternals({
  3592. arch: os$1.arch, fs: fs__default["default"], tmpdir: os$1.tmpdir, MsgpackrEncoder: msgpackr.Encoder, WeakLRUCache: weakLruCache.WeakLRUCache, orderedBinary: orderedBinary__namespace,
  3593. EventEmitter: events.EventEmitter, os: os$1.platform(), onExit(callback) {
  3594. if (process.getMaxListeners() < process.listenerCount('exit') + 8)
  3595. process.setMaxListeners(process.listenerCount('exit') + 8);
  3596. process.on('exit', callback);
  3597. },
  3598. });
  3599. let { noop } = nativeAddon;
  3600. const TIMESTAMP_PLACEHOLDER = new Uint8Array([1,1,1,1,0,0,0,0]);
  3601. const DIRECT_WRITE_PLACEHOLDER = new Uint8Array([1,1,1,2,0,0,0,0]);
  3602. const TransactionFlags = {
  3603. ABORTABLE: 1,
  3604. SYNCHRONOUS_COMMIT: 2,
  3605. NO_SYNC_FLUSH: 0x10000,
  3606. };
  3607. var index = {
  3608. open, openAsClass, getLastVersion, compareKey: orderedBinary$1.compareKeys, keyValueToBuffer: orderedBinary$1.toBufferKey, bufferToKeyValue: orderedBinary$1.fromBufferKey, ABORT, IF_EXISTS: IF_EXISTS$1, asBinary, levelup, TransactionFlags, version: exports.version
  3609. };
  3610. let require$1 = module$1.createRequire((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)));
  3611. setRequire(require$1);
  3612. exports.v8AccelerationEnabled = false;
  3613. let versions = process.versions;
  3614. if (!versions.deno && !process.isBun) {
  3615. let [ majorVersion, minorVersion ] = versions.node.split('.');
  3616. if (versions.v8 && +majorVersion == nativeAddon.version.nodeCompiledVersion) {
  3617. let v8Funcs = {};
  3618. let fastApiCalls = (majorVersion == 17 || majorVersion == 18 || majorVersion == 16 && minorVersion > 8) && !process.env.DISABLE_TURBO_CALLS;
  3619. if (fastApiCalls) {
  3620. require$1('v8').setFlagsFromString('--turbo-fast-api-calls');
  3621. }
  3622. nativeAddon.enableDirectV8(v8Funcs, fastApiCalls);
  3623. Object.assign(nativeAddon, v8Funcs);
  3624. exports.v8AccelerationEnabled = true;
  3625. } else if (majorVersion == 14) {
  3626. // node v14 only has ABI compatibility with node v16 for zero-arg clearKeptObjects
  3627. let v8Funcs = {};
  3628. nativeAddon.enableDirectV8(v8Funcs, false);
  3629. nativeAddon.clearKeptObjects = v8Funcs.clearKeptObjects;
  3630. }
  3631. nativeAddon.enableThreadSafeCalls();
  3632. }
  3633. setNativeFunctions(nativeAddon);
  3634. Object.defineProperty(exports, 'bufferToKeyValue', {
  3635. enumerable: true,
  3636. get: function () { return orderedBinary$1.fromBufferKey; }
  3637. });
  3638. Object.defineProperty(exports, 'compareKey', {
  3639. enumerable: true,
  3640. get: function () { return orderedBinary$1.compareKeys; }
  3641. });
  3642. Object.defineProperty(exports, 'compareKeys', {
  3643. enumerable: true,
  3644. get: function () { return orderedBinary$1.compareKeys; }
  3645. });
  3646. Object.defineProperty(exports, 'keyValueToBuffer', {
  3647. enumerable: true,
  3648. get: function () { return orderedBinary$1.toBufferKey; }
  3649. });
  3650. exports.ABORT = ABORT;
  3651. exports.DIRECT_WRITE_PLACEHOLDER = DIRECT_WRITE_PLACEHOLDER;
  3652. exports.IF_EXISTS = IF_EXISTS$1;
  3653. exports.SKIP = SKIP;
  3654. exports.TIMESTAMP_PLACEHOLDER = TIMESTAMP_PLACEHOLDER;
  3655. exports.TransactionFlags = TransactionFlags;
  3656. exports.allDbs = allDbs;
  3657. exports.asBinary = asBinary;
  3658. exports["default"] = index;
  3659. exports.getLastTxnId = getLastTxnId;
  3660. exports.getLastVersion = getLastVersion;
  3661. exports.levelup = levelup;
  3662. exports.nativeAddon = nativeAddon;
  3663. exports.noop = noop;
  3664. exports.open = open;
  3665. exports.openAsClass = openAsClass;
  3666. //# sourceMappingURL=index.cjs.map