-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8af35d
commit bc2e130
Showing
15 changed files
with
2,120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# http://editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_style = tab | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.yml] | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
# Logs | ||
|
||
logs | ||
_.log | ||
npm-debug.log_ | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
.pnpm-debug.log* | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
|
||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
||
# Runtime data | ||
|
||
pids | ||
_.pid | ||
_.seed | ||
\*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
|
||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
|
||
coverage | ||
\*.lcov | ||
|
||
# nyc test coverage | ||
|
||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
|
||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
|
||
bower_components | ||
|
||
# node-waf configuration | ||
|
||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
|
||
build/Release | ||
|
||
# Dependency directories | ||
|
||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
|
||
web_modules/ | ||
|
||
# TypeScript cache | ||
|
||
\*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
|
||
.npm | ||
|
||
# Optional eslint cache | ||
|
||
.eslintcache | ||
|
||
# Optional stylelint cache | ||
|
||
.stylelintcache | ||
|
||
# Microbundle cache | ||
|
||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
|
||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
|
||
\*.tgz | ||
|
||
# Yarn Integrity file | ||
|
||
.yarn-integrity | ||
|
||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
|
||
.cache | ||
.parcel-cache | ||
|
||
# Next.js build output | ||
|
||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
|
||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
|
||
.cache/ | ||
|
||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
|
||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
|
||
# public | ||
|
||
# vuepress build output | ||
|
||
.vuepress/dist | ||
|
||
# vuepress v2.x temp and cache directory | ||
|
||
.temp | ||
.cache | ||
|
||
# Docusaurus cache and generated files | ||
|
||
.docusaurus | ||
|
||
# Serverless directories | ||
|
||
.serverless/ | ||
|
||
# FuseBox cache | ||
|
||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
|
||
.dynamodb/ | ||
|
||
# TernJS port file | ||
|
||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
|
||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.\* | ||
|
||
# wrangler project | ||
|
||
.dev.vars | ||
.wrangler/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"printWidth": 140, | ||
"singleQuote": true, | ||
"semi": true, | ||
"useTabs": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "cloudflare-workers", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"deploy": "wrangler deploy", | ||
"dev": "wrangler dev", | ||
"start": "wrangler dev", | ||
"test": "vitest", | ||
"cf-typegen": "wrangler types", | ||
"cf-initdb": "wrangler d1 execute comments --remote --file=schema.sql" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/vitest-pool-workers": "^0.4.5", | ||
"@cloudflare/workers-types": "^4.20240712.0", | ||
"prettier": "3.3.3", | ||
"typescript": "^5.5.2", | ||
"vitest": "1.5.0", | ||
"wrangler": "^3.60.3" | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e", | ||
"dependencies": { | ||
"itty-router": "^5.0.17" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
DROP TABLE IF EXISTS `comments`; | ||
DROP TABLE IF EXISTS `commenters`; | ||
DROP TABLE IF EXISTS `offsets`; | ||
DROP TABLE IF EXISTS `pages`; | ||
|
||
CREATE TABLE IF NOT EXISTS `pages` ( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`path` TEXT UNIQUE NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS `offsets` ( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`page_id` INTEGER NOT NULL, | ||
`start` INTEGER NOT NULL, | ||
`end` INTEGER NOT NULL, | ||
FOREIGN KEY(`page_id`) REFERENCES `pages`(`id`) | ||
); | ||
CREATE INDEX idx_offsets ON `offsets`(`start`, `end`); | ||
|
||
CREATE TABLE IF NOT EXISTS `commenters`( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`name` TEXT NOT NULL, | ||
`user_agent` TEXT NOT NULL, | ||
`ip_address` TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS `comments` ( | ||
`id` INTEGER PRIMARY KEY AUTOINCREMENT, | ||
`offset_id` INTEGER NOT NULL, | ||
`commenter_id` INTEGER NOT NULL, | ||
`comment` TEXT NOT NULL, | ||
`created_time` TEXT NOT NULL, -- SQLite currently not support TIMESTAMP, use ISO 8601 DateTime | ||
FOREIGN KEY(`offset_id`) REFERENCES `offsets`(`id`), | ||
FOREIGN KEY(`commenter_id`) REFERENCES `commenters`(`id`) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { GetComment, GetCommentRespBody, PostComment } from './types'; | ||
|
||
export async function postComment(env: Env, req: PostComment) { | ||
const db = env.DB; | ||
|
||
let page = await db.prepare('SELECT * FROM pages WHERE path = ?').bind(req.path).first(); | ||
|
||
if (!page) { | ||
await db.prepare('INSERT INTO pages (path) VALUES (?)').bind(req.path).run(); | ||
page = (await db.prepare('SELECT last_insert_rowid() AS id').first())!; | ||
} | ||
|
||
let offset = await db | ||
.prepare('SELECT * FROM offsets WHERE page_id = ? AND start = ? AND end = ?') | ||
.bind(page.id, req.offset.start, req.offset.end) | ||
.first(); | ||
|
||
if (!offset) { | ||
await db.prepare('INSERT INTO offsets (page_id, start, end) VALUES (?, ?, ?)').bind(page.id, req.offset.start, req.offset.end).run(); | ||
offset = (await db.prepare('SELECT last_insert_rowid() AS id').first())!; | ||
} | ||
|
||
await db | ||
.prepare('INSERT INTO commenters (name, user_agent, ip_address) VALUES (?, ?, ?)') | ||
.bind(req.commenter?.name, req.commenter?.user_agent, req.commenter?.ip_address) | ||
.run(); | ||
const commiterId = (await db.prepare('SELECT last_insert_rowid() AS id').first())!.id; | ||
|
||
await db | ||
.prepare('INSERT INTO comments (offset_id, commenter_id, comment, created_time) VALUES (?, ?, ?, ?)') | ||
.bind(offset.id, commiterId, req.comment, new Date().toISOString()) | ||
.run(); | ||
} | ||
|
||
export async function getComment(env: Env, req: GetComment): Promise<GetCommentRespBody> { | ||
const db = env.DB; | ||
|
||
const page = await db.prepare('SELECT * FROM pages WHERE path = ?').bind(req.path).first(); | ||
|
||
if (!page) { | ||
return []; | ||
} | ||
|
||
const comments = ( | ||
await db | ||
.prepare( | ||
'SELECT * FROM comments JOIN commenters ON comments.commenter_id = commenters.id JOIN offsets ON comments.offset_id = offsets.id WHERE offsets.page_id = ?', | ||
) | ||
.bind(page.id) | ||
.all() | ||
).results; | ||
|
||
return comments.map((comment) => { | ||
return { | ||
offset: { | ||
start: comment.start as number, | ||
end: comment.end as number, | ||
}, | ||
commenter: { | ||
name: comment.name as string | null, | ||
}, | ||
comment: comment.comment as string, | ||
created_time: comment.created_time as string, | ||
}; | ||
}); | ||
} |
Oops, something went wrong.