-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switches to limited number of node modules that Workers support. Adds workarounds to mismatches between Deno and Node. - [deps]: uses simpler check for `config_dir` to avoid `node:os`. - [deps]: switches to `@sntran/html-rewriter` for cross runtime. - [mod]: fallback for undefined `import.meta.url` in Workers. - [backend/drive/auth]: move `reveal` out of global scope. - [backend/local]: switch to dynamic import for `node:fs/promises`. - [cmd/config]: switch to dynamic import for `node:fs/promises`. - [cmd/lsf]: creates new `Headers` instead of spreading. - [cmd/serve/http]: switches to `@sntran/serve` for cross runtime. Added a `_worker.js` script to run as example on CF Workers.
- Loading branch information
Showing
20 changed files
with
2,132 additions
and
169 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
node_modules | ||
.DS_Store | ||
.wrangler |
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,76 @@ | ||
import * as backends from "./backend/main.js"; | ||
|
||
/** | ||
* A handler | ||
* | ||
* @callback Handler | ||
* @param {Request} request | ||
* @param {Object} env Environment variables. | ||
* @returns {Response|Promise<Response>} | ||
*/ | ||
|
||
/** | ||
* Serves a bare remote, i.e., `/:memory:/path/to/file` | ||
* | ||
* All configuration must be passed as query parameters. | ||
* | ||
* @param {Request} request | ||
* @param {Object} env | ||
* @param {Object} params | ||
* @param {string} params.remote | ||
* @param {string} [params.path] | ||
* @returns {Promise<Response} | ||
*/ | ||
function remote(request, _env, params) { | ||
let { remote, path = "/" } = params; | ||
|
||
const backend = backends[remote]; | ||
if (!backend) { | ||
return new Response("Remote not found.", { status: 404 }); | ||
} | ||
|
||
if (!path) { | ||
path = "/"; | ||
} | ||
|
||
const url = new URL(request.url); | ||
url.pathname = path; | ||
|
||
request = new Request(url, request); | ||
|
||
return backend.fetch(request); | ||
} | ||
|
||
const routes = { | ||
"/\\::remote\\:/": remote, | ||
"/\\::remote\\:/:path*": remote, | ||
}; | ||
|
||
/** | ||
* Routes request to the appropriate handler. | ||
* @param {Object} routes | ||
* @returns {Handler} | ||
*/ | ||
function router(routes = {}) { | ||
return function (request, env) { | ||
const url = new URL(request.url); | ||
|
||
for (const [route, handler] of Object.entries(routes)) { | ||
const [pathname, method] = route.split("@").reverse(); | ||
|
||
if (method && request.method !== method) continue; | ||
|
||
const pattern = new URLPattern({ pathname }); | ||
if (!pattern.test(url)) continue; | ||
|
||
const params = pattern.exec(url)?.pathname?.groups || {}; | ||
return handler(request, env, params); | ||
} | ||
|
||
return env.ASSETS.fetch(request); | ||
}; | ||
} | ||
|
||
export default { | ||
fetch: router(routes), | ||
}; |
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
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
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
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
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
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
Oops, something went wrong.