Skip to content

Commit

Permalink
chore: edit formating
Browse files Browse the repository at this point in the history
  • Loading branch information
maraisr committed Jun 28, 2023
1 parent 411e664 commit 6d64a90
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 117 deletions.
12 changes: 8 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = tab
indent_size = 4
insert_final_newline = true
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,yml,yaml,md}]
indent_style = space
92 changes: 46 additions & 46 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
{
"name": "swrr",
"version": "0.0.6",
"license": "MIT",
"author": "Marais Rossow <me@marais.dev> (https://marais.io)",
"sideEffects": false,
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js"
},
"./package.json": "./package.json"
},
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js",
"index.mjs"
],
"scripts": {
"build": "bundt --minify",
"format": "prettier --write src .github *.md *.json",
"test": "uvu src \".test.ts$\" -r tsm",
"typecheck": "tsc --noEmit"
},
"prettier": "@marais/prettier",
"dependencies": {
"object-identity": "^0.1.1",
"worktop": "0.8.0-next.15"
},
"devDependencies": {
"@cloudflare/workers-types": "4.20230518.0",
"@marais/prettier": "0.0.2",
"@marais/tsconfig": "0.0.4",
"@types/node": "20.3.2",
"bundt": "2.0.0-next.5",
"nanospy": "1.0.0",
"prettier": "2.8.8",
"tsm": "2.3.0",
"typescript": "5.1.3",
"uvu": "0.5.4"
},
"volta": {
"node": "18.16.1"
}
"name": "swrr",
"version": "0.0.6",
"license": "MIT",
"author": "Marais Rossow <me@marais.dev> (https://marais.io)",
"sideEffects": false,
"exports": {
".": {
"import": "./index.mjs",
"require": "./index.js"
},
"./package.json": "./package.json"
},
"main": "index.js",
"module": "index.mjs",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js",
"index.mjs"
],
"scripts": {
"build": "bundt --minify",
"format": "prettier --write src .github *.md *.json",
"test": "uvu src \".test.ts$\" -r tsm",
"typecheck": "tsc --noEmit"
},
"prettier": "@marais/prettier",
"dependencies": {
"object-identity": "^0.1.1",
"worktop": "0.8.0-next.15"
},
"devDependencies": {
"@cloudflare/workers-types": "4.20230518.0",
"@marais/prettier": "0.0.4",
"@marais/tsconfig": "0.0.4",
"@types/node": "20.3.2",
"bundt": "2.0.0-next.5",
"nanospy": "1.0.0",
"prettier": "2.8.8",
"tsm": "2.3.0",
"typescript": "5.1.3",
"uvu": "0.5.4"
},
"volta": {
"node": "18.16.1"
}
}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ test('case', async () => {
const handler = Spy.spy((slug: string) => slug);

// ~> request bound
const broker = swr.make(binding as any as KVNamespace, context as any as ExecutionContext);
const broker = swr.make(
binding as any as KVNamespace,
context as any as ExecutionContext,
);

const getPosts = broker('posts', handler);

Expand All @@ -66,7 +69,10 @@ test('shouldnt call handler if time hasnt elapsed yet', async () => {
const binding = mock_kv();
const handler = Spy.spy((slug: string) => slug);

const broker = swr.make(binding as any as KVNamespace, context as any as ExecutionContext);
const broker = swr.make(
binding as any as KVNamespace,
context as any as ExecutionContext,
);

const getPosts = broker('posts', handler, {
ttl: 5,
Expand All @@ -87,7 +93,11 @@ test('shouldnt call handler if time hasnt elapsed yet', async () => {

assert.equal(await getPosts('my-slug'), 'my-slug');
assert.equal(date_spy.results, [0, 1000, 5000]);
assert.equal(handler.callCount, 2, 'time elapsed, handler should have been called again');
assert.equal(
handler.callCount,
2,
'time elapsed, handler should have been called again',
);

time = 7;

Expand Down
105 changes: 55 additions & 50 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,62 @@ import { SHA1 } from 'worktop/crypto';

import type { Lifetimes, make as _make } from 'swrr';

export const make: typeof _make = (binding, context) => (name, handler, options) => {
type Value = ReturnType<typeof handler>;
type Metadata = { expireAt: number };

const type = options?.type || 'json';
const lifetimes: Required<Lifetimes> = {
ttl: options?.ttl ?? 600,
maxTtl: options?.maxTtl ?? 1000,
};
export const make: typeof _make =
(binding, context) => (name, handler, options) => {
type Value = ReturnType<typeof handler>;
type Metadata = { expireAt: number };

const type = options?.type || 'json';
const lifetimes: Required<Lifetimes> = {
ttl: options?.ttl ?? 600,
maxTtl: options?.maxTtl ?? 1000,
};

return new Proxy(handler, {
async apply(target, this_arg, args_array) {
const key = args_array.length
? name + '::' + (await SHA1(identify(args_array)))
: name;

const result = await read<Value, Metadata>(binding, key, {
metadata: true,
cacheTtl: lifetimes.ttl,
// @ts-expect-error TS2769
type,
});

async function call(date = Date.now()) {
const raw_result = await Reflect.apply(
target,
this_arg,
args_array,
);

return new Proxy(handler, {
async apply(target, this_arg, args_array) {
const key = args_array.length
? name + '::' + (await SHA1(identify(args_array)))
: name;

const result = await read<Value, Metadata>(binding, key, {
metadata: true,
cacheTtl: lifetimes.ttl,
// @ts-expect-error TS2769
type,
});

async function call(date = Date.now()) {
const raw_result = await Reflect.apply(target, this_arg, args_array);

context.waitUntil(
write<Value, Metadata>(binding, key, raw_result, {
metadata: {
expireAt: date + lifetimes.ttl * 1000,
},
expirationTtl: lifetimes.maxTtl,
}),
);

return raw_result;
}

if (result.value != null) {
const called_at = Date.now();
if (
result.metadata!.expireAt == null ||
called_at >= result.metadata!.expireAt
) {
context.waitUntil(call(called_at));
context.waitUntil(
write<Value, Metadata>(binding, key, raw_result, {
metadata: {
expireAt: date + lifetimes.ttl * 1000,
},
expirationTtl: lifetimes.maxTtl,
}),
);

return raw_result;
}

return result.value;
}
if (result.value != null) {
const called_at = Date.now();
if (
result.metadata!.expireAt == null ||
called_at >= result.metadata!.expireAt
) {
context.waitUntil(call(called_at));
}

return result.value;
}

return await call();
},
});
};
return await call();
},
});
};
20 changes: 10 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"extends": "@marais/tsconfig",
"compilerOptions": {
"baseUrl": ".",
"types": ["@types/node", "@cloudflare/workers-types"],
"paths": {
"swrr": ["src/index.d.ts"]
}
},
"include": ["src", "test"],
"exclude": ["node_modules"]
"extends": "@marais/tsconfig",
"compilerOptions": {
"baseUrl": ".",
"types": ["@types/node", "@cloudflare/workers-types"],
"paths": {
"swrr": ["src/index.d.ts"]
}
},
"include": ["src", "test"],
"exclude": ["node_modules"]
}

0 comments on commit 6d64a90

Please sign in to comment.