@miniflare/durable-objects
Advanced tools
Comparing version 2.9.0-next.1 to 2.9.0
@@ -30,2 +30,3 @@ /// <reference types="node" /> | ||
deleteAlarm(key: string): Promise<void>; | ||
flushAlarms(keys?: string[]): Promise<void>; | ||
dispose(): void; | ||
@@ -135,2 +136,3 @@ } | ||
constructor(ctx: PluginContext, options?: DurableObjectsOptions); | ||
getStorage(storage: StorageFactory, id: DurableObjectId): DurableObjectStorage; | ||
getObject(storage: StorageFactory, id: DurableObjectId): Promise<DurableObjectState>; | ||
@@ -140,5 +142,5 @@ getNamespace(storage: StorageFactory, objectName: string): DurableObjectNamespace; | ||
flushAlarms(storageFactory: StorageFactory, ids?: DurableObjectId[]): Promise<void>; | ||
beforeReload(): void; | ||
beforeReload(): Promise<void>; | ||
reload(bindings: Context, moduleExports: Context, mounts: Map<string, Mount>): void; | ||
dispose(): void; | ||
dispose(): Promise<void>; | ||
} | ||
@@ -154,2 +156,3 @@ | ||
blockConcurrencyWhile<T>(closure: () => Promise<T>): Promise<T>; | ||
/* Excluded from this release type: [_kRunWithGates] */ | ||
[kFetch](request: Request): Promise<Response>; | ||
@@ -224,2 +227,4 @@ [kAlarm](): Promise<void>; | ||
/* Excluded from this release type: _kRunWithGates */ | ||
declare const kStartTxnCount: unique symbol; | ||
@@ -226,0 +231,0 @@ |
@@ -48,2 +48,3 @@ var __create = Object.create; | ||
ShadowStorage: () => ShadowStorage, | ||
_kRunWithGates: () => _kRunWithGates, | ||
kAlarmExists: () => kAlarmExists | ||
@@ -61,3 +62,3 @@ }); | ||
async setupStore(storage, persist) { | ||
this.#store = await storage.storage(ALARM_KEY, persist); | ||
this.#store = storage.storage(ALARM_KEY, persist); | ||
const { keys } = await this.#store.list(); | ||
@@ -72,3 +73,3 @@ for (const { name, metadata } of keys) { | ||
doAlarm.timeout = setTimeout(() => { | ||
this.#deleteAlarm(objectKey, doAlarm); | ||
void this.#deleteAlarm(objectKey, doAlarm); | ||
this.#callback?.(objectKey); | ||
@@ -134,2 +135,18 @@ }, Math.max(doAlarm.scheduledTime - now, 0)); | ||
} | ||
async flushAlarms(keys) { | ||
if (keys === void 0) { | ||
for (const [key, alarm] of this.#alarms) { | ||
await this.#deleteAlarm(key, alarm); | ||
await this.#callback?.(key); | ||
} | ||
} else { | ||
for (const key of keys) { | ||
const alarm = this.#alarms.get(key); | ||
if (alarm !== void 0) { | ||
await this.#deleteAlarm(key, alarm); | ||
await this.#callback?.(key); | ||
} | ||
} | ||
} | ||
} | ||
dispose() { | ||
@@ -181,2 +198,3 @@ if (this.#alarmTimeout) { | ||
var kInstance = Symbol("kInstance"); | ||
var _kRunWithGates = Symbol("kRunWithGates"); | ||
var kAlarm = Symbol("kAlarm"); | ||
@@ -196,5 +214,8 @@ var kFetch = Symbol("kFetch"); | ||
} | ||
[_kRunWithGates](closure) { | ||
const outputGate = new import_shared2.OutputGate(); | ||
return outputGate.runWith(() => this.#inputGate.runWith(closure)); | ||
} | ||
[kFetch](request) { | ||
const outputGate = new import_shared2.OutputGate(); | ||
return outputGate.runWith(() => this.#inputGate.runWith(() => { | ||
return this[_kRunWithGates](() => { | ||
const instance = this[kInstance]; | ||
@@ -205,8 +226,7 @@ if (!instance?.fetch) { | ||
return instance.fetch(request); | ||
})); | ||
}); | ||
} | ||
[kAlarm]() { | ||
const outputGate = new import_shared2.OutputGate(); | ||
return outputGate.runWith(() => this.#inputGate.runWith(() => { | ||
this.storage.deleteAlarm(); | ||
return this[_kRunWithGates](async () => { | ||
await this.storage.deleteAlarm(); | ||
const instance = this[kInstance]; | ||
@@ -217,3 +237,3 @@ if (!instance?.alarm) { | ||
return instance.alarm(); | ||
})); | ||
}); | ||
} | ||
@@ -965,2 +985,11 @@ }; | ||
// packages/durable-objects/src/plugin.ts | ||
function getObjectKeyFromId(id) { | ||
return `${id[kObjectName]}:${id.toString()}`; | ||
} | ||
function getObjectIdFromKey(key) { | ||
const colonIndex = key.lastIndexOf(":"); | ||
const objectName = key.substring(0, colonIndex); | ||
const hexId = key.substring(colonIndex + 1); | ||
return new DurableObjectId(objectName, hexId); | ||
} | ||
var DurableObjectsPlugin = class extends import_shared5.Plugin { | ||
@@ -977,4 +1006,7 @@ durableObjects; | ||
#bindings = {}; | ||
#objects = new Map(); | ||
#objectStorages = new Map(); | ||
#objectStates = new Map(); | ||
#alarmStore; | ||
#alarmStoreCallback; | ||
#alarmStoreCallbackAttached = false; | ||
constructor(ctx, options) { | ||
@@ -992,23 +1024,29 @@ super(ctx); | ||
} | ||
getStorage(storage, id) { | ||
const key = getObjectKeyFromId(id); | ||
let objectStorage = this.#objectStorages.get(key); | ||
if (objectStorage !== void 0) | ||
return objectStorage; | ||
objectStorage = new DurableObjectStorage(storage.storage(key, this.#persist), this.#alarmStore.buildBridge(key)); | ||
this.#objectStorages.set(key, objectStorage); | ||
return objectStorage; | ||
} | ||
async getObject(storage, id) { | ||
(0, import_assert4.default)(this.#contextPromise, "beforeReload() must be called before getObject()"); | ||
await this.#contextPromise; | ||
const key = getObjectKeyFromId(id); | ||
let state = this.#objectStates.get(key); | ||
if (state !== void 0) | ||
return state; | ||
const objectName = id[kObjectName]; | ||
const key = `${objectName}:${id.toString()}`; | ||
let statePromise = this.#objects.get(key); | ||
if (statePromise) | ||
return statePromise; | ||
statePromise = (async () => { | ||
const objectStorage = new DurableObjectStorage(await storage.storage(key, this.#persist), this.#alarmStore.buildBridge(key)); | ||
const unnamedId = new DurableObjectId(objectName, id.toString()); | ||
const state = new DurableObjectState(unnamedId, objectStorage); | ||
const constructor = this.#constructors.get(objectName); | ||
(0, import_assert4.default)(constructor); | ||
state[kInstance] = new constructor(state, this.#bindings); | ||
if (!state[kInstance]?.alarm) | ||
objectStorage[kAlarmExists] = false; | ||
return state; | ||
})(); | ||
this.#objects.set(key, statePromise); | ||
return statePromise; | ||
const unnamedId = new DurableObjectId(objectName, id.toString()); | ||
const objectStorage = this.getStorage(storage, id); | ||
state = new DurableObjectState(unnamedId, objectStorage); | ||
this.#objectStates.set(key, state); | ||
const constructor = this.#constructors.get(objectName); | ||
(0, import_assert4.default)(constructor); | ||
state[kInstance] = new constructor(state, this.#bindings); | ||
if (!state[kInstance]?.alarm) | ||
objectStorage[kAlarmExists] = false; | ||
return state; | ||
} | ||
@@ -1030,28 +1068,14 @@ getNamespace(storage, objectName) { | ||
} | ||
async #setupAlarms(storage) { | ||
async #setupAlarms(storageFactory) { | ||
if (this.durableObjectsAlarms === false) | ||
return; | ||
await this.#alarmStore.setupStore(storage, this.#persist); | ||
await this.#alarmStore.setupAlarms(async (objectKey) => { | ||
const index = objectKey.lastIndexOf(":"); | ||
const objectName = objectKey.substring(0, index); | ||
const hexId = objectKey.substring(index + 1); | ||
const id = new DurableObjectId(objectName, hexId); | ||
const state = await this.getObject(storage, id); | ||
await this.#alarmStore.setupStore(storageFactory, this.#persist); | ||
this.#alarmStoreCallback = async (objectKey) => { | ||
const id = getObjectIdFromKey(objectKey); | ||
const state = await this.getObject(storageFactory, id); | ||
await this.#executeAlarm(state); | ||
}); | ||
}; | ||
} | ||
async flushAlarms(storageFactory, ids) { | ||
if (ids !== void 0) { | ||
for (const id of ids) { | ||
const state = await this.getObject(storageFactory, id); | ||
await this.#executeAlarm(state); | ||
this.#alarmStore.deleteAlarm(`${id[kObjectName]}:${id.toString()}`); | ||
} | ||
} else { | ||
for (const [key, state] of this.#objects) { | ||
await this.#executeAlarm(await state); | ||
this.#alarmStore.deleteAlarm(key); | ||
} | ||
} | ||
flushAlarms(storageFactory, ids) { | ||
return this.#alarmStore.flushAlarms(ids?.map(getObjectKeyFromId)); | ||
} | ||
@@ -1066,5 +1090,10 @@ async #executeAlarm(state) { | ||
} | ||
beforeReload() { | ||
this.#objects.clear(); | ||
async beforeReload() { | ||
this.#objectStorages.clear(); | ||
this.#objectStates.clear(); | ||
this.#contextPromise = new Promise((resolve) => this.#contextResolve = resolve); | ||
if (!this.#alarmStoreCallbackAttached && this.#alarmStoreCallback !== void 0) { | ||
this.#alarmStoreCallbackAttached = true; | ||
await this.#alarmStore.setupAlarms(this.#alarmStoreCallback); | ||
} | ||
} | ||
@@ -1096,5 +1125,5 @@ reload(bindings, moduleExports, mounts) { | ||
} | ||
dispose() { | ||
async dispose() { | ||
await this.beforeReload(); | ||
this.#alarmStore.dispose(); | ||
return this.beforeReload(); | ||
} | ||
@@ -1158,4 +1187,5 @@ }; | ||
ShadowStorage, | ||
_kRunWithGates, | ||
kAlarmExists | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@miniflare/durable-objects", | ||
"version": "2.9.0-next.1", | ||
"version": "2.9.0", | ||
"description": "Durable Objects module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -38,12 +38,12 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/core": "2.9.0-next.1", | ||
"@miniflare/shared": "2.9.0-next.1", | ||
"@miniflare/storage-memory": "2.9.0-next.1", | ||
"@miniflare/core": "2.9.0", | ||
"@miniflare/shared": "2.9.0", | ||
"@miniflare/storage-memory": "2.9.0", | ||
"undici": "5.9.1" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/cache": "2.9.0-next.1", | ||
"@miniflare/runner-vm": "2.9.0-next.1", | ||
"@miniflare/shared-test": "2.9.0-next.1" | ||
"@miniflare/cache": "2.9.0", | ||
"@miniflare/runner-vm": "2.9.0", | ||
"@miniflare/shared-test": "2.9.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
75774
1392
0
+ Added@miniflare/core@2.9.0(transitive)
+ Added@miniflare/queues@2.9.0(transitive)
+ Added@miniflare/shared@2.9.0(transitive)
+ Added@miniflare/storage-memory@2.9.0(transitive)
+ Added@miniflare/watcher@2.9.0(transitive)
+ Added@types/better-sqlite3@7.6.11(transitive)
+ Added@types/node@22.9.0(transitive)
+ Addedbuiltins@5.1.0(transitive)
+ Addedcross-spawn@7.0.5(transitive)
+ Addedexeca@6.1.0(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedhuman-signals@3.0.1(transitive)
+ Addedis-stream@3.0.0(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-fn@4.0.0(transitive)
+ Addednpm-run-path@5.3.0(transitive)
+ Addednpx-import@1.1.4(transitive)
+ Addedonetime@6.0.0(transitive)
+ Addedparse-package-name@1.0.0(transitive)
+ Addedpath-key@3.1.14.0.0(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstrip-final-newline@3.0.0(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addedvalidate-npm-package-name@4.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
- Removed@miniflare/core@2.9.0-next.1(transitive)
- Removed@miniflare/queues@2.9.0-next.1(transitive)
- Removed@miniflare/shared@2.9.0-next.1(transitive)
- Removed@miniflare/storage-memory@2.9.0-next.1(transitive)
- Removed@miniflare/watcher@2.9.0-next.1(transitive)
Updated@miniflare/core@2.9.0
Updated@miniflare/shared@2.9.0