File tree Expand file tree Collapse file tree 8 files changed +59
-3
lines changed
packages/gitbook-v2/src/lib Expand file tree Collapse file tree 8 files changed +59
-3
lines changed Original file line number Diff line number Diff line change @@ -4,5 +4,4 @@ export * from './pages';
4
4
export * from './urls' ;
5
5
export * from './errors' ;
6
6
export * from './lookup' ;
7
- export * from './proxy' ;
8
7
export * from './visitor' ;
Original file line number Diff line number Diff line change 1
1
import { withLeadingSlash , withTrailingSlash } from '@/lib/paths' ;
2
2
import type { PublishedSiteContent } from '@gitbook/api' ;
3
- import { getProxyRequestIdentifier , isProxyRequest } from '. /proxy' ;
3
+ import { getProxyRequestIdentifier , isProxyRequest } from '@v2/lib /proxy' ;
4
4
5
5
/**
6
6
* Get the appropriate base path for the visitor authentication cookie.
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'bun:test' ;
2
+ import { getImageResizingContextId } from './getImageResizingContextId' ;
3
+
4
+ describe ( 'getImageResizingContextId' , ( ) => {
5
+ it ( 'should return proxy identifier for proxy requests' , ( ) => {
6
+ const proxyRequestURL = new URL ( 'https://proxy.gitbook.site/sites/site_foo/hello/world' ) ;
7
+ expect ( getImageResizingContextId ( proxyRequestURL ) ) . toBe ( 'sites/site_foo' ) ;
8
+ } ) ;
9
+
10
+ it ( 'should return preview identifier for preview requests' , ( ) => {
11
+ const previewRequestURL = new URL ( 'https://preview/site_foo/hello/world' ) ;
12
+ expect ( getImageResizingContextId ( previewRequestURL ) ) . toBe ( 'site_foo' ) ;
13
+ } ) ;
14
+
15
+ it ( 'should return host for regular requests' , ( ) => {
16
+ const regularRequestURL = new URL ( 'https://example.com/docs/foo/hello/world' ) ;
17
+ expect ( getImageResizingContextId ( regularRequestURL ) ) . toBe ( 'example.com' ) ;
18
+ } ) ;
19
+ } ) ;
Original file line number Diff line number Diff line change 1
- import { getProxyRequestIdentifier , isProxyRequest } from '../data' ;
1
+ import { getPreviewRequestIdentifier , isPreviewRequest } from '@v2/lib/preview' ;
2
+ import { getProxyRequestIdentifier , isProxyRequest } from '@v2/lib/proxy' ;
2
3
3
4
/**
4
5
* Get the site identifier to use for image resizing for an incoming request.
@@ -8,6 +9,9 @@ export function getImageResizingContextId(url: URL): string {
8
9
if ( isProxyRequest ( url ) ) {
9
10
return getProxyRequestIdentifier ( url ) ;
10
11
}
12
+ if ( isPreviewRequest ( url ) ) {
13
+ return getPreviewRequestIdentifier ( url ) ;
14
+ }
11
15
12
16
return url . host ;
13
17
}
Original file line number Diff line number Diff line change
1
+ import { describe , expect , it } from 'bun:test' ;
2
+ import { getPreviewRequestIdentifier , isPreviewRequest } from './preview' ;
3
+
4
+ describe ( 'isPreviewRequest' , ( ) => {
5
+ it ( 'should return true for preview requests' , ( ) => {
6
+ const previewRequestURL = new URL ( 'https://preview/site_foo/hello/world' ) ;
7
+ expect ( isPreviewRequest ( previewRequestURL ) ) . toBe ( true ) ;
8
+ } ) ;
9
+
10
+ it ( 'should return false for non-preview requests' , ( ) => {
11
+ const nonPreviewRequestURL = new URL ( 'https://example.com/docs/foo/hello/world' ) ;
12
+ expect ( isPreviewRequest ( nonPreviewRequestURL ) ) . toBe ( false ) ;
13
+ } ) ;
14
+ } ) ;
15
+
16
+ describe ( 'getPreviewRequestIdentifier' , ( ) => {
17
+ it ( 'should return the correct identifier for preview requests' , ( ) => {
18
+ const previewRequestURL = new URL ( 'https://preview/site_foo/hello/world' ) ;
19
+ expect ( getPreviewRequestIdentifier ( previewRequestURL ) ) . toBe ( 'site_foo' ) ;
20
+ } ) ;
21
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Check if the request to the site is a preview request.
3
+ */
4
+ export function isPreviewRequest ( requestURL : URL ) : boolean {
5
+ return requestURL . host === 'preview' ;
6
+ }
7
+
8
+ export function getPreviewRequestIdentifier ( requestURL : URL ) : string {
9
+ // For preview requests, we extract the site ID from the pathname
10
+ // e.g. https://preview/site_id/...
11
+ const pathname = requestURL . pathname . slice ( 1 ) . split ( '/' ) ;
12
+ return pathname [ 0 ] ;
13
+ }
File renamed without changes.
File renamed without changes.
You can’t perform that action at this time.
0 commit comments