Skip to content

Commit

Permalink
⚙️ Cloudflare DurableObject iyileştir
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed May 21, 2024
1 parent 573c790 commit ae8db56
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 51 deletions.
111 changes: 60 additions & 51 deletions cloudflare/durableObject.d.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>} callback
* @return {!Promise<T>}
*/
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<string>} 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<string, *>} key
* @param {*=} val
* @return {!Promise<void>}
*/
cloudflare.DurableObject.Storage.prototype.put = function (key, value) { };
cloudflare.DurableObjectStorage.prototype.put = function (key, val) { };

/**
* @nosideeffects
* @param {string} key
* @return {!Promise<boolean>}
*/
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<!Response>}
* @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<!Response>}
*/
Expand All @@ -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 () { }
Expand All @@ -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<!Response>}
* @see https://fetch.spec.whatwg.org/#fetch-method
* @see https://developers.cloudflare.com/workers/runtime-apis/fetch/
*/
cloudflare.DurableObjectStub.prototype.fetch = function (input, init) { }
49 changes: 49 additions & 0 deletions cloudflare/mock/durableObject.js
Original file line number Diff line number Diff line change
@@ -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<string>} 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<T>} callback
* @return {!Promise<T>}
*/
blockConcurrencyWhile(callback) {
return callback();
}
}

export { DurableObjectState };
21 changes: 21 additions & 0 deletions util/hex.js
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit ae8db56

Please sign in to comment.