Skip to content

Commit

Permalink
fix : fix constructor params & improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzulul committed Jan 26, 2024
1 parent 61f970c commit 9368a9c
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,5 @@ dist
#hook
test/cjs/node_modules/
test/mjs/node_modules/
public/
#public/
package-lock.json
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
* ✅ Plain output
* ✅ SSL

## Qiuck Start

### Local

```javascript
git clone https://github.com/nuzulul/opencors.git
npm install
npm start
```

### Deploy

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgit.luolix.top%2Fnuzulul%2Fopencors&project-name=opencors&repository-name=opencors)

## Demo

[https://codesandbox.io/p/devbox/nodejs-cors-proxy-xqsqwg](https://codesandbox.io/p/devbox/nodejs-cors-proxy-xqsqwg)
Expand Down
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
}
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"start": "npm run build && node public/test/test/server-test.js",
"build-win": "del /S /Q .\\dist\\* && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
"start-ts":"ts-node server.ts",
"dev":"nodemon -e js,ts --watch src --watch test --exec \"npm start\"",
"start": "tsc && node public/server.js",
"prepare-build-win-1": "if not exist .\\dist (mkdir dist) else (rmdir /S /Q .\\dist\\)",
"prepare-build-win-2": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)",
"build-win": "npm run prepare-build-win-1 && npm run prepare-build-win-2 && tsc && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json",
"dev":"nodemon -e js,ts --watch src --watch server.ts --exec \"npm start\"",
"clean-win":"del /S /Q .\\public && del /S /Q .\\dist && rmdir /S /Q .\\node_modules && rmdir /S /Q .\\test\\cjs\\node_modules && rmdir /S /Q .\\test\\mjs\\node_modules",
"test-win":"npm link && cd .\\test\\cjs && npm install && npm link opencors && cd .\\..\\mjs && npm install && npm link opencors",
"test-cjs":"cd .\\test\\cjs && npm start",
"test-mjs":"cd .\\test\\mjs && npm start",
"prepare-win":"npm install && npm run build-win && npm run test-win && npm run build",
"publish-win":"npm run clean-win && npm run build-win && npm publish"
"prepare-win":"npm install && npm run build-win && npm run dev"
},
"files": [
"dist/",
"LICENSE"
],
"repository": {
"type": "git",
"url": "git+https://github.com/nuzulul/opencors.git"
Expand Down
1 change: 1 addition & 0 deletions public/server.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
6 changes: 6 additions & 0 deletions public/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const opencors_1 = require("./src/opencors");
const server = new opencors_1.OpenCORS({
//port:4000
});
5 changes: 5 additions & 0 deletions public/src/opencors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export declare class OpenCORS {
constructor({ port }: {
port?: number;
});
}
82 changes: 82 additions & 0 deletions public/src/opencors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenCORS = void 0;
const http_1 = __importDefault(require("http"));
const https_1 = __importDefault(require("https"));
class OpenCORS {
constructor({ port }) {
const serverport = port || process.env.PORT || 8080;
const server = http_1.default.createServer((req, res) => __awaiter(this, void 0, void 0, function* () {
var _a;
function isvalidurl(url) {
try {
return !!(new URL(url));
}
catch (e) {
return false;
}
}
function get(targeturl, resolve) {
https_1.default.get(targeturl, res => {
if ((res.statusCode === 301 || res.statusCode === 302) && (res.headers.location != undefined)) {
return get(res.headers.location, resolve);
}
let data = [];
res.on('data', chunk => { data.push(chunk); });
res.on('end', () => {
try {
const raw = Buffer.concat(data).toString();
resolve(raw);
}
catch (err) {
let msg = JSON.stringify({ status: 'error', msg: err, targeturl });
resolve(msg);
}
});
});
}
function get_page(targeturl) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve) => {
get(targeturl, resolve);
});
});
}
let body = (_a = 'OpenCors\n' +
'OpenCORS is a simple NodeJS based CORS Proxy\n' +
'https://github.com/nuzulul/opencors\n\n' +
'Usage :\n' + req.headers.host + '/?url=\n\n' +
'Agent:\n' + req.headers['user-agent']) !== null && _a !== void 0 ? _a : "Unknown";
let requrl = req.url;
requrl = requrl.startsWith('/') ? 'http://' + req.headers.host + req.url : req.url;
const targeturl = (new URL(requrl)).searchParams.get("url");
if (isvalidurl(targeturl)) {
const rawdata = yield get_page(targeturl);
body = rawdata;
}
else if (targeturl != null) {
body = JSON.stringify({ status: 'error', msg: 'Invalid target url', targeturl });
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(body);
}));
server.listen(serverport, () => {
console.log(`Server running at ${serverport}`);
});
}
}
exports.OpenCORS = OpenCORS;
6 changes: 6 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {OpenCORS} from './src/opencors'

const server = new OpenCORS({
//port:4000
})

2 changes: 1 addition & 1 deletion src/opencors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import https from 'https'

export class OpenCORS{

public constructor({port}:{port:number}){
public constructor({port}:{port?:number}){

const serverport:string|number = port || process.env.PORT || 8080

Expand Down
6 changes: 0 additions & 6 deletions test/test/server-test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"target": "es6",
"rootDir": ".",
},
"include": ["test/test/server-test.js","src"],
"include": ["server.ts","src"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
23 changes: 23 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": 2,
"builds": [
{
"src": "public/server.js",
"use": "@vercel/node",
"config": { "includeFiles": ["public/**"] }
}
],
"routes": [
{
"src": "/(.*)",
"dest": "public/server.js",
"methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
"headers": {
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
}
}
]
}

0 comments on commit 9368a9c

Please sign in to comment.