mirror of
https://github.com/movie-web/providers-api.git
synced 2025-09-13 05:33:26 +00:00
Initial setup from simple-proxy
This commit is contained in:
7
.dockerignore
Normal file
7
.dockerignore
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
node_modules
|
||||||
|
*.log*
|
||||||
|
.nitro
|
||||||
|
.cache
|
||||||
|
.output
|
||||||
|
.env
|
||||||
|
dist
|
7
.editorconfig
Normal file
7
.editorconfig
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
indent_size = 2
|
||||||
|
indent_style = space
|
3
.eslintignore
Normal file
3
.eslintignore
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
dist
|
||||||
|
.output
|
||||||
|
node-modules
|
50
.eslintrc.js
Normal file
50
.eslintrc.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
module.exports = {
|
||||||
|
env: {
|
||||||
|
browser: true,
|
||||||
|
},
|
||||||
|
extends: [
|
||||||
|
"plugin:@typescript-eslint/recommended",
|
||||||
|
"plugin:prettier/recommended",
|
||||||
|
],
|
||||||
|
ignorePatterns: ["public/*", "dist/*", "/*.js", "/*.ts"],
|
||||||
|
parser: "@typescript-eslint/parser",
|
||||||
|
parserOptions: {
|
||||||
|
project: "./tsconfig.json",
|
||||||
|
tsconfigRootDir: "./",
|
||||||
|
},
|
||||||
|
settings: {
|
||||||
|
"import/resolver": {
|
||||||
|
typescript: {
|
||||||
|
project: "./tsconfig.json",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: ["@typescript-eslint", "import", "prettier"],
|
||||||
|
rules: {
|
||||||
|
"no-underscore-dangle": "off",
|
||||||
|
"@typescript-eslint/no-explicit-any": "off",
|
||||||
|
"no-console": "off",
|
||||||
|
"@typescript-eslint/no-this-alias": "off",
|
||||||
|
"import/prefer-default-export": "off",
|
||||||
|
"@typescript-eslint/no-empty-function": "off",
|
||||||
|
"no-shadow": "off",
|
||||||
|
"@typescript-eslint/no-shadow": ["error"],
|
||||||
|
"no-restricted-syntax": "off",
|
||||||
|
"import/no-unresolved": ["error", { ignore: ["^virtual:"] }],
|
||||||
|
"consistent-return": "off",
|
||||||
|
"no-continue": "off",
|
||||||
|
"no-eval": "off",
|
||||||
|
"no-await-in-loop": "off",
|
||||||
|
"no-nested-ternary": "off",
|
||||||
|
"prefer-destructuring": "off",
|
||||||
|
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
|
||||||
|
"import/extensions": [
|
||||||
|
"error",
|
||||||
|
"ignorePackages",
|
||||||
|
{
|
||||||
|
ts: "never",
|
||||||
|
tsx: "never",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
}
|
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
node_modules
|
||||||
|
*.log*
|
||||||
|
.nitro
|
||||||
|
.cache
|
||||||
|
.output
|
||||||
|
.env
|
||||||
|
dist
|
4
.prettierrc.js
Normal file
4
.prettierrc.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
module.exports = {
|
||||||
|
trailingComma: 'all',
|
||||||
|
singleQuote: true,
|
||||||
|
};
|
3
.vscode/extensions.json
vendored
Normal file
3
.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"recommendations": ["dbaeumer.vscode-eslint", "editorconfig.editorconfig"]
|
||||||
|
}
|
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
|
||||||
|
"eslint.format.enable": true
|
||||||
|
}
|
31
package.json
Normal file
31
package.json
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "providers-api",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"prepare": "nitropack prepare",
|
||||||
|
"dev": "nitropack dev",
|
||||||
|
"build": "nitropack build",
|
||||||
|
"build:cloudflare": "NITRO_PRESET=cloudflare npm run build",
|
||||||
|
"build:aws": "NITRO_PRESET=aws_lambda npm run build",
|
||||||
|
"build:node": "NITRO_PRESET=node-server npm run build",
|
||||||
|
"start": "node .output/server/index.mjs",
|
||||||
|
"lint": "eslint --ext .ts src/",
|
||||||
|
"lint:fix": "eslint --fix --ext .ts src/",
|
||||||
|
"preinstall": "npx only-allow pnpm"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@movie-web/providers": "^1.1.5",
|
||||||
|
"h3": "^1.9.0",
|
||||||
|
"nitropack": "latest"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
||||||
|
"@typescript-eslint/parser": "^6.14.0",
|
||||||
|
"eslint": "^8.56.0",
|
||||||
|
"eslint-config-airbnb-base": "^15.0.0",
|
||||||
|
"eslint-config-prettier": "^9.1.0",
|
||||||
|
"eslint-import-resolver-typescript": "^3.6.1",
|
||||||
|
"eslint-plugin-prettier": "^5.0.1"
|
||||||
|
}
|
||||||
|
}
|
4394
pnpm-lock.yaml
generated
Normal file
4394
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
39
src/routes/index.ts
Normal file
39
src/routes/index.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { getBodyBuffer } from '@/utils/body';
|
||||||
|
import {
|
||||||
|
getProxyHeaders,
|
||||||
|
getAfterResponseHeaders,
|
||||||
|
cleanupHeadersBeforeProxy,
|
||||||
|
} from '@/utils/headers';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
// handle cors, if applicable
|
||||||
|
if (isPreflightRequest(event)) return handleCors(event, {});
|
||||||
|
|
||||||
|
// parse destination URL
|
||||||
|
const destination = getQuery<{ destination?: string }>(event).destination;
|
||||||
|
if (!destination)
|
||||||
|
return await sendJson({
|
||||||
|
event,
|
||||||
|
status: 400,
|
||||||
|
data: {
|
||||||
|
error: 'destination query parameter invalid',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// read body
|
||||||
|
const body = await getBodyBuffer(event);
|
||||||
|
|
||||||
|
// proxy
|
||||||
|
cleanupHeadersBeforeProxy(event);
|
||||||
|
await proxyRequest(event, destination, {
|
||||||
|
fetchOptions: {
|
||||||
|
redirect: 'follow',
|
||||||
|
headers: getProxyHeaders(event.headers),
|
||||||
|
body,
|
||||||
|
},
|
||||||
|
onResponse(outputEvent, response) {
|
||||||
|
const headers = getAfterResponseHeaders(response.headers, response.url);
|
||||||
|
setResponseHeaders(outputEvent, headers);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
13
src/utils/body.ts
Normal file
13
src/utils/body.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { H3Event } from 'h3';
|
||||||
|
|
||||||
|
export function hasBody(event: H3Event) {
|
||||||
|
const method = event.method.toUpperCase();
|
||||||
|
return ['PUT', 'POST', 'PATCH', 'DELETE'].includes(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getBodyBuffer(
|
||||||
|
event: H3Event,
|
||||||
|
): Promise<Buffer | undefined> {
|
||||||
|
if (!hasBody(event)) return;
|
||||||
|
return await readRawBody(event, false);
|
||||||
|
}
|
73
src/utils/headers.ts
Normal file
73
src/utils/headers.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { H3Event } from 'h3';
|
||||||
|
|
||||||
|
const blacklistedHeaders = [
|
||||||
|
'cf-connecting-ip',
|
||||||
|
'cf-worker',
|
||||||
|
'cf-ray',
|
||||||
|
'cf-visitor',
|
||||||
|
'cf-ew-via',
|
||||||
|
'x-forwarded-for',
|
||||||
|
'x-forwarded-host',
|
||||||
|
'x-forwarded-proto',
|
||||||
|
'forwarded',
|
||||||
|
'x-real-ip',
|
||||||
|
];
|
||||||
|
|
||||||
|
function copyHeader(
|
||||||
|
headers: Headers,
|
||||||
|
outputHeaders: Headers,
|
||||||
|
inputKey: string,
|
||||||
|
outputKey: string,
|
||||||
|
) {
|
||||||
|
if (headers.has(inputKey))
|
||||||
|
outputHeaders.set(outputKey, headers.get(inputKey) ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getProxyHeaders(headers: Headers): Headers {
|
||||||
|
const output = new Headers();
|
||||||
|
|
||||||
|
const headerMap: Record<string, string> = {
|
||||||
|
'X-Cookie': 'Cookie',
|
||||||
|
'X-Referer': 'Referer',
|
||||||
|
'X-Origin': 'Origin',
|
||||||
|
};
|
||||||
|
Object.entries(headerMap).forEach((entry) => {
|
||||||
|
copyHeader(headers, output, entry[0], entry[1]);
|
||||||
|
});
|
||||||
|
|
||||||
|
output.set(
|
||||||
|
'User-Agent',
|
||||||
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
|
||||||
|
);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAfterResponseHeaders(
|
||||||
|
headers: Headers,
|
||||||
|
finalUrl: string,
|
||||||
|
): Record<string, string> {
|
||||||
|
const output: Record<string, string> = {};
|
||||||
|
|
||||||
|
if (headers.has('Set-Cookie'))
|
||||||
|
output['X-Set-Cookie'] = headers.get('Set-Cookie') ?? '';
|
||||||
|
|
||||||
|
return {
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Expose-Headers': '*',
|
||||||
|
Vary: 'Origin',
|
||||||
|
'X-Final-Destination': finalUrl,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeHeadersFromEvent(event: H3Event, key: string) {
|
||||||
|
const normalizedKey = key.toLowerCase();
|
||||||
|
if (event.node.req.headers[normalizedKey])
|
||||||
|
delete event.node.req.headers[normalizedKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cleanupHeadersBeforeProxy(event: H3Event) {
|
||||||
|
blacklistedHeaders.forEach((key) => {
|
||||||
|
removeHeadersFromEvent(event, key);
|
||||||
|
});
|
||||||
|
}
|
10
src/utils/sending.ts
Normal file
10
src/utils/sending.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { H3Event, EventHandlerRequest } from 'h3';
|
||||||
|
|
||||||
|
export async function sendJson(ops: {
|
||||||
|
event: H3Event<EventHandlerRequest>;
|
||||||
|
data: Record<string, any>;
|
||||||
|
status?: number;
|
||||||
|
}) {
|
||||||
|
setResponseStatus(ops.event, ops.status ?? 200);
|
||||||
|
await send(ops.event, JSON.stringify(ops.data, null, 2), 'application/json');
|
||||||
|
}
|
Reference in New Issue
Block a user