mirror of
https://github.com/uetchy/namae.git
synced 2025-07-02 14:20:03 +09:00
commit
a954db9fe9
39
api/services/jsorg/[query].ts
Normal file
39
api/services/jsorg/[query].ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { VercelRequest, VercelResponse } from '@vercel/node';
|
||||||
|
import { VM } from 'vm2';
|
||||||
|
import { fetch, send, sendError } from '../../../util/http';
|
||||||
|
|
||||||
|
export default async function handler(
|
||||||
|
req: VercelRequest,
|
||||||
|
res: VercelResponse
|
||||||
|
): Promise<void> {
|
||||||
|
const { query } = req.query;
|
||||||
|
|
||||||
|
if (!query || typeof query !== 'string') {
|
||||||
|
return sendError(res, new Error('No query given'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!/([a-zA-Z0-9_-])\.js\.org/.test(query)) {
|
||||||
|
return sendError(res, new Error('Invalid format'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const cname = query.replace(/\.js\.org$/, '');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get cnames from js.org repo
|
||||||
|
const source = await fetch(
|
||||||
|
`https://raw.githubusercontent.com/js-org/js.org/master/cnames_active.js`,
|
||||||
|
'GET'
|
||||||
|
).then((res) => res.text());
|
||||||
|
const vm = new VM();
|
||||||
|
vm.run(source);
|
||||||
|
const cnames = Object.keys(vm.sandbox.cnames_active);
|
||||||
|
|
||||||
|
if (cnames.includes(cname)) {
|
||||||
|
send(res, { availability: false });
|
||||||
|
} else {
|
||||||
|
send(res, { availability: true });
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
sendError(res, err as any);
|
||||||
|
}
|
||||||
|
}
|
@ -36,6 +36,7 @@
|
|||||||
"styled-components": "^5.3.1",
|
"styled-components": "^5.3.1",
|
||||||
"swr": "^1.0.1",
|
"swr": "^1.0.1",
|
||||||
"validator": "^13.6.0",
|
"validator": "^13.6.0",
|
||||||
|
"vm2": "^3.9.5",
|
||||||
"whois-json": "^2.0.4"
|
"whois-json": "^2.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -20,7 +20,7 @@ const JsOrgCard: React.FC<{ query: string }> = ({ query }) => {
|
|||||||
{(name) => (
|
{(name) => (
|
||||||
<DedicatedAvailability
|
<DedicatedAvailability
|
||||||
name={`${name}.js.org`}
|
name={`${name}.js.org`}
|
||||||
service="dns"
|
service="jsorg"
|
||||||
message="Go to js.org repository"
|
message="Go to js.org repository"
|
||||||
link="https://github.com/js-org/js.org"
|
link="https://github.com/js-org/js.org"
|
||||||
messageIfTaken={`Go to ${name}.js.org`}
|
messageIfTaken={`Go to ${name}.js.org`}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { NowResponse } from '@vercel/node';
|
import { VercelResponse } from '@vercel/node';
|
||||||
import nodeFetch from 'cross-fetch';
|
import nodeFetch from 'cross-fetch';
|
||||||
|
|
||||||
export type HttpMethod =
|
export type HttpMethod =
|
||||||
@ -16,15 +16,15 @@ export function fetch(
|
|||||||
method: HttpMethod = 'HEAD'
|
method: HttpMethod = 'HEAD'
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
return nodeFetch(url, {
|
return nodeFetch(url, {
|
||||||
method: method,
|
method,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function send(res: NowResponse, data: object): void {
|
export function send(res: VercelResponse, data: object): void {
|
||||||
res.setHeader('Cache-Control', 's-maxage=86400');
|
res.setHeader('Cache-Control', 's-maxage=86400');
|
||||||
res.json(data);
|
res.json(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sendError(res: NowResponse, error: Error): void {
|
export function sendError(res: VercelResponse, error: Error): void {
|
||||||
res.status(400).json({ error: error.message });
|
res.status(400).json({ error: error.message });
|
||||||
}
|
}
|
||||||
|
@ -12748,6 +12748,11 @@ vm-browserify@^1.0.1:
|
|||||||
resolved "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
|
resolved "https://registry.npmjs.org/vm-browserify/-/vm-browserify-1.1.2.tgz#78641c488b8e6ca91a75f511e7a3b32a86e5dda0"
|
||||||
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==
|
integrity sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==
|
||||||
|
|
||||||
|
vm2@^3.9.5:
|
||||||
|
version "3.9.5"
|
||||||
|
resolved "https://registry.npmjs.org/vm2/-/vm2-3.9.5.tgz#5288044860b4bbace443101fcd3bddb2a0aa2496"
|
||||||
|
integrity sha512-LuCAHZN75H9tdrAiLFf030oW7nJV5xwNMuk1ymOZwopmuK3d2H4L1Kv4+GFHgarKiLfXXLFU+7LDABHnwOkWng==
|
||||||
|
|
||||||
void-elements@3.1.0:
|
void-elements@3.1.0:
|
||||||
version "3.1.0"
|
version "3.1.0"
|
||||||
resolved "https://registry.npmjs.org/void-elements/-/void-elements-3.1.0.tgz#614f7fbf8d801f0bb5f0661f5b2f5785750e4f09"
|
resolved "https://registry.npmjs.org/void-elements/-/void-elements-3.1.0.tgz#614f7fbf8d801f0bb5f0661f5b2f5785750e4f09"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user