diff --git a/cloudflare/durableObject.d.js b/cloudflare/durableObject.d.js index e11927f..e73d89c 100644 --- a/cloudflare/durableObject.d.js +++ b/cloudflare/durableObject.d.js @@ -6,59 +6,91 @@ */ /** - * @interface + * A state of the DurableObject. * - * @param {!cloudflare.DurableObject.State} state - * @param {!cloudflare.Environment} env + * @interface */ -cloudflare.DurableObject = function (state, env) { } +cloudflare.DurableObjectState = function () { } /** - * A state of the DurableObject. - * - * @interface + * @template T + * @param {function():!Promise} callback + * @return {!Promise} */ -cloudflare.DurableObject.State = function () { } +cloudflare.DurableObjectState.prototype.blockConcurrencyWhile = function (callback) { } -/** @const {!cloudflare.DurableObject.Storage} */ -cloudflare.DurableObject.State.prototype.storage; +/** @const {!cloudflare.DurableObjectStorage} */ +cloudflare.DurableObjectState.prototype.storage; /** @const {!cloudflare.DurableObjectId} */ -cloudflare.DurableObject.State.prototype.id; +cloudflare.DurableObjectState.prototype.id; /** * Transactional storage of the durable object. * * @interface - * @see https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#transactional-storage-api */ -cloudflare.DurableObject.Storage = function () { } +cloudflare.DurableObjectStorage = function () { } /** * @nosideeffects * @param {string|!Array} key - * @return {!Promise<*>} + * @return {!Promise} */ -cloudflare.DurableObject.Storage.prototype.get = function (key) { }; +cloudflare.DurableObjectStorage.prototype.get = function (key) { }; /** * @nosideeffects - * @param {string} key - * @param {*} value + * @param {string|!Object} key + * @param {*=} val * @return {!Promise} */ -cloudflare.DurableObject.Storage.prototype.put = function (key, value) { }; +cloudflare.DurableObjectStorage.prototype.put = function (key, val) { }; /** * @nosideeffects * @param {string} key * @return {!Promise} */ -cloudflare.DurableObject.Storage.prototype.delete = function (key) { }; +cloudflare.DurableObjectStorage.prototype.delete = function (key) { }; + +/** + * @typedef {{ +* locationHint: string +* }} +*/ +cloudflare.DurableObjectStubOptions; + +/** +* @interface +*/ +cloudflare.DurableObjectStub = function () { } + +/** +* A durable object stub has the same fetch interface as the web api fetch. +* +* @param {!RequestInfo} input +* @param {!RequestInit=} init +* @return {!Promise} +* @see https://fetch.spec.whatwg.org/#fetch-method +* @see https://developers.cloudflare.com/workers/runtime-apis/fetch/ +*/ +cloudflare.DurableObjectStub.prototype.fetch = function (input, init) { } + +/** +* @nosideeffects +* @param {!cloudflare.DurableObjectId} durableObjectId +* @param {!cloudflare.DurableObjectStubOptions=} options +* @return {!cloudflare.DurableObjectStub} +*/ +cloudflare.DurableObjectBinding.prototype.get = function (durableObjectId, options) { } /** * @interface - * + */ +cloudflare.DurableObject = function (state, env) { } + +/** * @param {!Request} req * @return {!Promise} */ @@ -76,6 +108,14 @@ cloudflare.DurableObjectId = function () { } cloudflare.DurableObjectId.prototype.toString = function () { } /** + * @nosideeffects + * @return {boolean} + */ +cloudflare.DurableObjectId.prototype.equals = function () { } + +/** + * Called `DurableObjectNamespace` in `@cloudflare/workers-types`. + * * @interface */ cloudflare.DurableObjectBinding = function () { } @@ -99,34 +139,3 @@ cloudflare.DurableObjectBinding.prototype.idFromString = function (hexId) { } * @return {!cloudflare.DurableObjectId} */ cloudflare.DurableObjectBinding.prototype.newUniqueId = function () { } - -/** - * @typedef {{ - * locationHint: string - * }} - */ -cloudflare.StubOptions; - -/** - * @nosideeffects - * @param {!cloudflare.DurableObjectId} durableObjectId - * @param {!cloudflare.StubOptions=} options - * @return {!cloudflare.DurableObjectStub} - */ -cloudflare.DurableObjectBinding.prototype.get = function (durableObjectId, options) { } - -/** - * @interface - */ -cloudflare.DurableObjectStub = function () { } - -/** - * A durable object stub has the same fetch interface as the web api fetch. - * - * @param {!RequestInfo} input - * @param {!RequestInit=} init - * @return {!Promise} - * @see https://fetch.spec.whatwg.org/#fetch-method - * @see https://developers.cloudflare.com/workers/runtime-apis/fetch/ - */ -cloudflare.DurableObjectStub.prototype.fetch = function (input, init) { } diff --git a/cloudflare/mock/durableObject.js b/cloudflare/mock/durableObject.js new file mode 100644 index 0000000..a5b37bd --- /dev/null +++ b/cloudflare/mock/durableObject.js @@ -0,0 +1,49 @@ + +/** + * @implements {!cloudflare.DurableObjectState} + */ +class DurableObjectState { + constructor() { + /** @const {!cloudflare.DurableObjectId} */ + this.id = {}; + this.mem = {}; + /** @const {!cloudflare.DurableObjectStorage} */ + this.storage = { + /** + * @override + * + * @param {!Array} keys + */ + get: (keys) => { + const map = new Map(); + const mem = this.mem; + for (const key of keys) + if (key in mem) map.set(key, mem[key]); + return Promise.resolve(map); + }, + + put: (keys, val) => { + if (typeof keys === 'string') + this.mem[keys] = val; + else + Object.assign(this.mem, keys); + return Promise.resolve(); + }, + + delete: (key) => Promise.resolve(true) + } + } + + /** + * @override + * + * @template T + * @param {function():!Promise} callback + * @return {!Promise} + */ + blockConcurrencyWhile(callback) { + return callback(); + } +} + +export { DurableObjectState }; diff --git a/util/hex.js b/util/hex.js new file mode 100644 index 0000000..286449a --- /dev/null +++ b/util/hex.js @@ -0,0 +1,21 @@ + +const NibbleToBinary = {}; + +for (let i = 0; i < 16; ++i) + NibbleToBinary[i.toString(16).toUpperCase()] = NibbleToBinary[i.toString(16)] + = i.toString(2).padStart(4, "0"); + +/** + * @param {string} hexStr + * @return {string} + */ +const toBin = (hexStr) => { + const parts = Array(hexStr.length); + for (let i = 0; i < hexStr.length; ++i) + parts[i] = NibbleToBinary[hexStr[i]]; + return parts.join(""); +} + +export default { + toBin, +}