forked from shrimpia/great-ebichiri-wall
-
Notifications
You must be signed in to change notification settings - Fork 1
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
cc8de2e
commit 9009440
Showing
16 changed files
with
161 additions
and
6 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,9 @@ | ||
{ | ||
"scripts": { | ||
"deploy": "wrangler deploy --minify src/index.ts" | ||
}, | ||
"devDependencies": { | ||
"@cloudflare/workers-types": "^4.20240208.0", | ||
"wrangler": "^3.25.0" | ||
} | ||
} |
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,60 @@ | ||
export default { | ||
async fetch(request: Request) { | ||
const ccLimit = 4; | ||
const atLimit = 3; | ||
|
||
// If the request method is HEAD or GET, return it as it is. | ||
if (request.method === 'HEAD' || request.method === 'GET') { | ||
return await fetch(request.url, { | ||
method: request.method, | ||
headers: request.headers, | ||
}); | ||
} | ||
|
||
const body = await request.text(); | ||
|
||
try { | ||
const bodyJson = JSON.parse(body); | ||
const cc = bodyJson.cc?.length ?? 0; | ||
|
||
// Check if mentions exceed the limit | ||
const mentions = (bodyJson.text || '').match(/@.*?@.*?\..*?/g) || []; | ||
|
||
if (mentions.length > atLimit) { | ||
return new Response(JSON.stringify({ | ||
error: { | ||
message: 'Too many Ats.', | ||
code: 'TOO_MANY_ATS', | ||
id: 'c7e10ff1-042f-441a-b490-836956560650', | ||
} | ||
}), { | ||
// Note: Returning a 400 in ActivityPub may result in repeated retries from the remote or, in the worst case, delivery suspension. Therefore, return a 202 for 'inbox'. | ||
status: request.url.includes('inbox') ? 202 : 400, | ||
}); | ||
} | ||
|
||
if (cc > ccLimit) { | ||
return new Response(JSON.stringify({ | ||
error: { | ||
message: 'Too many mentions.', | ||
code: 'TOO_MANY_MENTIONS', | ||
id: 'c7e10ff1-042f-441a-b490-836956560650', | ||
} | ||
}), { | ||
// Note: Returning a 400 in ActivityPub may lead to repeated retries from the remote or, in the worst case, delivery suspension. Therefore, return a 202 in the case of 'inbox'. | ||
status: request.url.includes('inbox') ? 202 : 400, | ||
}); | ||
} | ||
} catch (e) { | ||
// do nothing | ||
} | ||
|
||
// No badWords filtering | ||
|
||
return await fetch(request.url, { | ||
method: 'POST', | ||
headers: request.headers, | ||
body, | ||
}); | ||
}, | ||
}; |
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,16 @@ | ||
import { regexpMatch } from "./regexpMatch"; | ||
import { simpleMatch } from "./simpleMatch"; | ||
|
||
/** | ||
* Determines whether [word] is included in [text] or matches as a regular expression. | ||
* @param text The text to be evaluated. | ||
* @param word The word or regular expression pattern to check for. If enclosed in /, it is treated as a regular expression. | ||
* @returns True if there is a match, false otherwise. | ||
*/ | ||
export const match = (text: string, word: string) => { | ||
if (word.startsWith('/') && word.endsWith('/')) { | ||
return regexpMatch(text, word); | ||
} else { | ||
return simpleMatch(text, word); | ||
} | ||
}; |
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,20 @@ | ||
/** | ||
* Determines whether [text] matches the regular expression [pattern]. | ||
* @param text | ||
* @param pattern | ||
* @returns True if there is a match, false otherwise. | ||
*/ | ||
|
||
export const regexpMatch = (text: string, pattern: string) => { | ||
try { | ||
const regexp = new RegExp(pattern.slice(1, -1)); | ||
return regexp.test(text); | ||
} catch (e) { | ||
if (e instanceof SyntaxError) { | ||
// If the regular expression is invalid, it will be treated as a non-match. | ||
return false; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}; |
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,9 @@ | ||
/** | ||
* Determines partial matching of a simple string. | ||
* @param text | ||
* @param word | ||
* @returns True if there is a match, false otherwise. | ||
*/ | ||
export const simpleMatch = (text: string, word: string) => { | ||
return text.includes(word); | ||
}; |
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,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"strict": true, | ||
"lib": [ | ||
"ESNext" | ||
], | ||
"types": [ | ||
"@cloudflare/workers-types" | ||
], | ||
"jsx": "react-jsx" | ||
}, | ||
} |
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,10 @@ | ||
name = "workers-block-ngword-in-post-body" | ||
compatibility_date = "2023-12-01" | ||
main = "./src/index.js" | ||
|
||
# [vars] | ||
# MY_VARIABLE = "production_value" | ||
|
||
[[kv_namespaces]] | ||
binding = "KV" | ||
id = "YOUR_KV_ID" |