-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cloudflare): refactor structure, optimize patterns
Refactors server directory structure by creating `entrypoints` directory and moving `server.advanced` and `server.directory` into it. Changes all respective imports to reflect this movement. The server's code imports from `./util.js` are refactored to `../util.js` due to the server files' move into the `entrypoints` directory. In `index.ts`, cleans up unused imports, and refactors code by moving utility functions that were previously inline to the new `./utils` directory. The code refactoring and restructuring allows for easier navigation and enhancement. The server and utility functions separation follows the single responsibility principle, favoring modular design. The pattern optimization aids in a smoother deployment process by eliminating redundant patterns. --------- Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: 100gle <[email protected]>
- Loading branch information
1 parent
0ab19ba
commit 8453a25
Showing
13 changed files
with
313 additions
and
327 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 @@ | ||
--- | ||
'@astrojs/cloudflare': patch | ||
--- | ||
|
||
Refactor codebase to enhance code readability and structure, to prioritize maintainability for long-term. |
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 1 addition & 1 deletion
2
...rations/cloudflare/src/server.advanced.ts → ...dflare/src/entrypoints/server.advanced.ts
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
2 changes: 1 addition & 1 deletion
2
...ations/cloudflare/src/server.directory.ts → ...flare/src/entrypoints/server.directory.ts
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,40 @@ | ||
import type { AstroAdapter, AstroFeatureMap } from 'astro'; | ||
|
||
export function getAdapter({ | ||
isModeDirectory, | ||
functionPerRoute, | ||
}: { | ||
isModeDirectory: boolean; | ||
functionPerRoute: boolean; | ||
}): AstroAdapter { | ||
const astroFeatures = { | ||
hybridOutput: 'stable', | ||
staticOutput: 'unsupported', | ||
serverOutput: 'stable', | ||
assets: { | ||
supportKind: 'stable', | ||
isSharpCompatible: false, | ||
isSquooshCompatible: false, | ||
}, | ||
} satisfies AstroFeatureMap; | ||
|
||
if (isModeDirectory) { | ||
return { | ||
name: '@astrojs/cloudflare', | ||
serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.directory.js', | ||
exports: ['onRequest', 'manifest'], | ||
adapterFeatures: { | ||
functionPerRoute, | ||
edgeMiddleware: false, | ||
}, | ||
supportedAstroFeatures: astroFeatures, | ||
}; | ||
} | ||
|
||
return { | ||
name: '@astrojs/cloudflare', | ||
serverEntrypoint: '@astrojs/cloudflare/entrypoints/server.advanced.js', | ||
exports: ['default'], | ||
supportedAstroFeatures: astroFeatures, | ||
}; | ||
} |
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
26 changes: 26 additions & 0 deletions
26
packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts
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,26 @@ | ||
/** | ||
* Remove duplicates and redundant patterns from an `include` or `exclude` list. | ||
* Otherwise Cloudflare will throw an error on deployment. Plus, it saves more entries. | ||
* E.g. `['/foo/*', '/foo/*', '/foo/bar'] => ['/foo/*']` | ||
* @param patterns a list of `include` or `exclude` patterns | ||
* @returns a deduplicated list of patterns | ||
*/ | ||
export function deduplicatePatterns(patterns: string[]) { | ||
const openPatterns: RegExp[] = []; | ||
|
||
// A value in the set may only occur once; it is unique in the set's collection. | ||
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set | ||
return [...new Set(patterns)] | ||
.sort((a, b) => a.length - b.length) | ||
.filter((pattern) => { | ||
if (openPatterns.some((p) => p.test(pattern))) { | ||
return false; | ||
} | ||
|
||
if (pattern.endsWith('*')) { | ||
openPatterns.push(new RegExp(`^${pattern.replace(/(\*\/)*\*$/g, '.*')}`)); | ||
} | ||
|
||
return true; | ||
}); | ||
} |
Oops, something went wrong.