mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-20 01:25:16 +00:00
Remove proxy package (#3124)
This commit is contained in:
@@ -196,13 +196,6 @@
|
||||
"typescript": "^5.5.3",
|
||||
},
|
||||
},
|
||||
"packages/proxy": {
|
||||
"name": "@gitbook/proxy",
|
||||
"version": "0.1.0",
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.3",
|
||||
},
|
||||
},
|
||||
"packages/react-contentkit": {
|
||||
"name": "@gitbook/react-contentkit",
|
||||
"version": "0.7.0",
|
||||
@@ -642,8 +635,6 @@
|
||||
|
||||
"@gitbook/openapi-parser": ["@gitbook/openapi-parser@workspace:packages/openapi-parser"],
|
||||
|
||||
"@gitbook/proxy": ["@gitbook/proxy@workspace:packages/proxy"],
|
||||
|
||||
"@gitbook/react-contentkit": ["@gitbook/react-contentkit@workspace:packages/react-contentkit"],
|
||||
|
||||
"@gitbook/react-math": ["@gitbook/react-math@workspace:packages/react-math"],
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
dist/
|
||||
@@ -1,7 +0,0 @@
|
||||
# @gitbook/proxy
|
||||
|
||||
## 0.1.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 53b9f10: First version
|
||||
@@ -1,28 +0,0 @@
|
||||
# `@gitbook/proxy`
|
||||
|
||||
Host a GitBook site on your own domain as a subpath.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { proxyToGitBook } from '@gitbook/proxy';
|
||||
|
||||
const site = proxyToGitBook(event.request, {
|
||||
site: 'mycompany.gitbook.io/site/',
|
||||
basePath: '/docs',
|
||||
});
|
||||
|
||||
export default {
|
||||
async fetch(request) {
|
||||
// If the requst matches the basePath /docs, we serve from GitBook
|
||||
if (site.match(request)) {
|
||||
return site.fetch(request);
|
||||
}
|
||||
|
||||
// Otherwise we do something else.
|
||||
return new Response('Not found', {
|
||||
statusCode: 404,
|
||||
});
|
||||
},
|
||||
};
|
||||
```
|
||||
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"name": "@gitbook/proxy",
|
||||
"description": "Host a GitBook site on your own domain as a subpath",
|
||||
"version": "0.1.0",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"development": "./src/index.ts",
|
||||
"default": "./dist/index.js"
|
||||
}
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"clean": "rm -rf ./dist",
|
||||
"unit": "bun test"
|
||||
},
|
||||
"files": ["dist", "src", "README.md", "CHANGELOG.md"]
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
import { describe, expect, it } from 'bun:test';
|
||||
import { proxyToGitBook } from '.';
|
||||
|
||||
describe('.match', () => {
|
||||
it('should return true if the request is below the base path', () => {
|
||||
const site = proxyToGitBook({ site: 'https://org.gitbook.io/example/', basePath: '/docs' });
|
||||
expect(site.match('/docs')).toBe(true);
|
||||
expect(site.match('/docs/')).toBe(true);
|
||||
expect(site.match('/docs/hello')).toBe(true);
|
||||
expect(site.match('/docs/hello/world')).toBe(true);
|
||||
|
||||
expect(site.match('/hello/world')).toBe(false);
|
||||
expect(site.match('/')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.request', () => {
|
||||
it('should compute a proper request for a sub-path', () => {
|
||||
const site = proxyToGitBook({ site: 'https://org.gitbook.io/example/', basePath: '/docs' });
|
||||
const request = new Request('https://example.com/docs/hello/world');
|
||||
|
||||
const proxiedRequest = site.request(request);
|
||||
expect(proxiedRequest.url).toBe('https://hosting.gitbook.io/docs/hello/world');
|
||||
expect(proxiedRequest.headers.get('Host')).toBe('hosting.gitbook.io');
|
||||
expect(proxiedRequest.headers.get('X-Forwarded-Host')).toBe('example.com');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-BasePath')).toBe('/docs');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-Site-URL')).toBe(
|
||||
'https://org.gitbook.io/example/'
|
||||
);
|
||||
});
|
||||
|
||||
it('should compute a proper request on the root', () => {
|
||||
const site = proxyToGitBook({ site: 'https://org.gitbook.io/example/', basePath: '/docs' });
|
||||
const request = new Request('https://example.com/docs');
|
||||
|
||||
const proxiedRequest = site.request(request);
|
||||
expect(proxiedRequest.url).toBe('https://hosting.gitbook.io/docs');
|
||||
expect(proxiedRequest.headers.get('Host')).toBe('hosting.gitbook.io');
|
||||
expect(proxiedRequest.headers.get('X-Forwarded-Host')).toBe('example.com');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-BasePath')).toBe('/docs');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-Site-URL')).toBe(
|
||||
'https://org.gitbook.io/example/'
|
||||
);
|
||||
});
|
||||
|
||||
it('should normalize the basepath', () => {
|
||||
const site = proxyToGitBook({ site: 'https://org.gitbook.io/example/', basePath: 'docs/' });
|
||||
const request = new Request('https://example.com/docs/hello/world');
|
||||
|
||||
const proxiedRequest = site.request(request);
|
||||
expect(proxiedRequest.url).toBe('https://hosting.gitbook.io/docs/hello/world');
|
||||
expect(proxiedRequest.headers.get('Host')).toBe('hosting.gitbook.io');
|
||||
expect(proxiedRequest.headers.get('X-Forwarded-Host')).toBe('example.com');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-BasePath')).toBe('/docs');
|
||||
expect(proxiedRequest.headers.get('X-GitBook-Site-URL')).toBe(
|
||||
'https://org.gitbook.io/example/'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,97 +0,0 @@
|
||||
export interface ProxyToGitBookOptions {
|
||||
/**
|
||||
* The URL of the published site.
|
||||
* @example "https://mycompany.gitbook.io/docs"
|
||||
*/
|
||||
site: string;
|
||||
|
||||
/**
|
||||
* Base path to serve the site on.
|
||||
* @example "/docs"
|
||||
*/
|
||||
basePath: string;
|
||||
|
||||
/**
|
||||
* Hostname used by GitBook to serve content.
|
||||
* Do not set this option unless you know what you are doing.
|
||||
*/
|
||||
gitbookHost?: string;
|
||||
}
|
||||
|
||||
export interface ProxySite {
|
||||
/**
|
||||
* Test if the request should be proxied to this site.
|
||||
*/
|
||||
match(request: Request | string): boolean;
|
||||
|
||||
/**
|
||||
* Get the proxied request for a given request.
|
||||
*/
|
||||
request(request: Request): Request;
|
||||
|
||||
/**
|
||||
* Fetch the request from the site.
|
||||
*/
|
||||
fetch(request: Request): Promise<Response>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxies requests to a GitBook site.
|
||||
*/
|
||||
export function proxyToGitBook(options: ProxyToGitBookOptions): ProxySite {
|
||||
const { gitbookHost = 'hosting.gitbook.io' } = options;
|
||||
|
||||
const siteUrl = new URL(options.site);
|
||||
const rawSiteUrl = siteUrl.toString();
|
||||
|
||||
const basePath = normalizeBasePath(options.basePath);
|
||||
|
||||
const site: ProxySite = {
|
||||
match: (request) => {
|
||||
const pathname = typeof request === 'string' ? request : new URL(request.url).pathname;
|
||||
return pathname === basePath || pathname.startsWith(`${basePath}/`);
|
||||
},
|
||||
|
||||
request: (originRequest) => {
|
||||
const originUrl = new URL(originRequest.url);
|
||||
|
||||
const url = new URL(originUrl);
|
||||
url.hostname = gitbookHost;
|
||||
|
||||
const proxyRequest = new Request(url, originRequest);
|
||||
proxyRequest.headers.set('Host', gitbookHost);
|
||||
|
||||
// Pass the original host and protocol
|
||||
proxyRequest.headers.set('X-Forwarded-Host', originUrl.hostname);
|
||||
proxyRequest.headers.set('X-Forwarded-Proto', 'https');
|
||||
|
||||
// Pass the basepath on the original URL
|
||||
proxyRequest.headers.set('X-GitBook-BasePath', basePath);
|
||||
|
||||
// Pass the site URL
|
||||
proxyRequest.headers.set('X-GitBook-Site-URL', rawSiteUrl);
|
||||
|
||||
return proxyRequest;
|
||||
},
|
||||
|
||||
fetch: async (originRequest) => {
|
||||
return fetch(site.request(originRequest));
|
||||
},
|
||||
};
|
||||
|
||||
return site;
|
||||
}
|
||||
|
||||
function normalizeBasePath(basePath: string): string {
|
||||
let result = withLeadingSlash(basePath);
|
||||
result = withoutTrailingSlash(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function withLeadingSlash(path: string): string {
|
||||
return path.startsWith('/') ? path : `/${path}`;
|
||||
}
|
||||
|
||||
function withoutTrailingSlash(path: string): string {
|
||||
return path.endsWith('/') ? path.slice(0, -1) : path;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "esnext",
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": false,
|
||||
"declaration": true,
|
||||
"outDir": "dist",
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"incremental": true,
|
||||
"types": [
|
||||
"bun-types" // add Bun global
|
||||
]
|
||||
},
|
||||
"include": ["src/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user