Skip to content

Commit

Permalink
Fix compatibility for @astrojs/db with cloudflare (#10677)
Browse files Browse the repository at this point in the history
* Fix compatibility for @astrojs/db with cloudflare

* Use a manual implementation

* Update lockfile
  • Loading branch information
matthewp authored Apr 4, 2024
1 parent 45414a2 commit 1662aa8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-eyes-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Fix compatibility of @astrojs/db with Cloudflare
2 changes: 1 addition & 1 deletion packages/db/src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pathToFileURL } from 'url';
import { pathToFileURL } from './utils.js';
import { type ColumnBuilderBaseConfig, type ColumnDataType, sql } from 'drizzle-orm';
import {
type IndexBuilder,
Expand Down
26 changes: 26 additions & 0 deletions packages/db/src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AstroError } from 'astro/errors';

const isWindows = process?.platform === "win32";

/**
* Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
*/
Expand All @@ -22,3 +24,27 @@ export async function safeFetch(
export class AstroDbError extends AstroError {
name = 'Astro DB Error';
}

export default function slash(path: string) {
const isExtendedLengthPath = path.startsWith('\\\\?\\');

if (isExtendedLengthPath) {
return path;
}

return path.replace(/\\/g, '/');
}

export function pathToFileURL(path: string): URL {
if(isWindows) {
let slashed = slash(path);
// Windows like C:/foo/bar
if(!slashed.startsWith('/')) {
slashed = '/' + slashed;
}
return new URL('file://' + slashed);
}

// Unix is easy
return new URL('file://' + path);
}
37 changes: 36 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1662aa8

Please sign in to comment.