-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare @gitbook/proxy to support serving under sub-directory (#2641)
- Loading branch information
Showing
9 changed files
with
298 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@gitbook/proxy': minor | ||
--- | ||
|
||
First version |
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
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 @@ | ||
dist/ |
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,28 @@ | ||
# `@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, | ||
}); | ||
}, | ||
}; | ||
``` |
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,28 @@ | ||
{ | ||
"name": "@gitbook/proxy", | ||
"description": "Host a GitBook site on your own domain as a subpath", | ||
"version": "0.0.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" | ||
] | ||
} |
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,59 @@ | ||
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/', | ||
); | ||
}); | ||
}); |
Oops, something went wrong.