Skip to content

Commit

Permalink
refactor: move to deno 2.0 and refactor whole project to new features
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Oct 24, 2024
1 parent 5eda80d commit 9e691aa
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 60 deletions.
12 changes: 12 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"workspace": [
"./packages/denodb",
"./packages/deta",
"./packages/file",
"./packages/free",
"./packages/mongodb",
"./packages/psql",
"./packages/redis",
"./packages/supabase"
]
}
135 changes: 135 additions & 0 deletions deno.lock

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

3 changes: 3 additions & 0 deletions packages/denodb/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "@grammyjs/storage-deno"
}
6 changes: 6 additions & 0 deletions packages/file/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@grammyjs/storage-file",
"imports": {
"grammy": "https://lib.deno.dev/x/[email protected]/mod.ts"
}
}
19 changes: 0 additions & 19 deletions packages/file/src/deps.deno.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/file/src/deps.node.ts

This file was deleted.

19 changes: 12 additions & 7 deletions packages/file/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { cwd, fs, path, StorageAdapter } from './deps.deno.ts';
import { StorageAdapter } from 'grammy';
import { cwd } from 'node:process';
import { mkdirSync } from 'node:fs';
import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
import path from 'node:path';

type Serializer<Session> = (input: Session) => string;
type Deserializer<Session> = (input: string) => Session;
Expand Down Expand Up @@ -32,7 +36,7 @@ export class FileAdapter<T> implements StorageAdapter<T> {
this.deserializer = opts.deserializer ??
((input) => JSON.parse(input));

fs.ensureDirSync(this.folderPath);
mkdirSync(this.folderPath, { recursive: true });
}

private resolveSessionPath(key: string) {
Expand All @@ -42,7 +46,7 @@ export class FileAdapter<T> implements StorageAdapter<T> {

private async findSessionFile(key: string) {
try {
return await fs.readFile(this.resolveSessionPath(key));
return await readFile(this.resolveSessionPath(key), { encoding: 'utf8' });
} catch {
return null;
}
Expand All @@ -63,13 +67,14 @@ export class FileAdapter<T> implements StorageAdapter<T> {
const fileName = `${key}.json`;
const folderPath = fullPath.substring(0, fullPath.length - fileName.length);

await fs.ensureDir(folderPath);
await fs.writeFile(fullPath, this.serializer(value));
await mkdir(folderPath, { recursive: true });
await writeFile(fullPath, this.serializer(value));
}

async delete(key: string) {
try {
await fs.remove(this.resolveSessionPath(key));
} catch {}
await unlink(this.resolveSessionPath(key));
// deno-lint-ignore no-empty
} catch { /* empty */ }
}
}

0 comments on commit 9e691aa

Please sign in to comment.