Skip to content

Commit 6505d5d

Browse files
authored
fix: file path not resolving in windows fix: inRequest not parsing connection header when multiple values exists (#28)
1 parent 3e29cdf commit 6505d5d

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inspatial/cloud",
3-
"version": "0.5.2",
3+
"version": "0.5.3",
44
"license": "Apache-2.0",
55
"exports": {
66
".": "./mod.ts",

src/orm/entry/entry-type.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { raiseORMException } from "~/orm/orm-exception.ts";
1515
import convertString from "~/utils/convert-string.ts";
1616
import type { EntryPermission } from "~/orm/roles/entry-permissions.ts";
1717
import type { InField } from "@inspatial/cloud/types";
18+
import { getCallerPath } from "../../utils/path-utils.ts";
1819

1920
/**
2021
* This class is used to define an Entry Type in the ORM.
@@ -54,20 +55,7 @@ export class EntryType<
5455
super(name, config);
5556

5657
if (!rm) {
57-
try {
58-
const matchPattern = /^\s+at\s(.+)\/[\w-_\s\d]+.ts:\d+:\d+/;
59-
const callingFunction = new Error().stack?.split("\n")[2];
60-
const match = callingFunction?.match(matchPattern);
61-
if (match) {
62-
const dir = new URL(match[1]);
63-
if (dir.protocol === "file:") {
64-
this.dir = dir.pathname;
65-
}
66-
}
67-
} catch (_e) {
68-
console.log("Error while setting dir for EntryType:", _e);
69-
// silently ignore
70-
}
58+
this.dir = getCallerPath();
7159
}
7260
this.sourceConfig = {
7361
...config,

src/orm/settings/settings-type.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
SettingsPermission,
1313
SettingsRole,
1414
} from "../roles/settings-permissions.ts";
15+
import { getCallerPath, normalizePath } from "../../utils/path-utils.ts";
1516

1617
/**
1718
* Defines a settings type for the ORM.
@@ -44,19 +45,7 @@ export class SettingsType<
4445
) {
4546
super(name, config);
4647
if (!rm) {
47-
try {
48-
const matchPattern = /^\s+at\s(.+)\/[\w-_\s\d]+.ts:\d+:\d+/;
49-
const callingFunction = new Error().stack?.split("\n")[2];
50-
const match = callingFunction?.match(matchPattern);
51-
if (match) {
52-
const dir = new URL(match[1]);
53-
if (dir.protocol === "file:") {
54-
this.dir = dir.pathname;
55-
}
56-
}
57-
} catch (_e) {
58-
// silently ignore
59-
}
48+
this.dir = getCallerPath();
6049
}
6150
this.sourceConfig = {
6251
...config,

src/serve/in-request.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ export class InRequest<CTX extends Record<string, any> = Record<string, any>> {
163163
let upgrade = "";
164164
this.request.headers.forEach((value, key) => {
165165
if (key.toLowerCase() === "connection") {
166-
connection = value.toLowerCase();
166+
const connMatch = value.toLowerCase().match(/(upgrade)/);
167+
168+
connection = connMatch ? connMatch[1] : value.toLowerCase();
167169
}
168170
if (key.toLowerCase() === "upgrade") {
169171
upgrade = value.toLowerCase();

src/utils/path-utils.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,29 @@ export function normalizePath(path: string, options?: {
2222
*/
2323
toDirname?: boolean;
2424
}): string {
25-
path = path.replace("file:///", IS_UNIX ? "/" : "");
25+
path = path.replace("/^file:\/\//", "");
2626
path = decodeURIComponent(path);
27-
path = IS_WINDOWS ? path.replace(/\\/g, "/") : path;
27+
path = IS_WINDOWS
28+
? path.replace(/\\/g, "/").replace(/^\/([A-Z]:)/, "$1")
29+
: path;
2830
if (options?.toDirname) {
2931
path = path.replace(/\/[\w\s]*\.\w*$/, "");
3032
}
3133
return path;
3234
}
35+
36+
/** Returns the folder path of the calling function where this `getCallerPath()` function is called.
37+
* ONLY if the module is a local file url (`file:///...`)
38+
*/
39+
export function getCallerPath(): string | undefined {
40+
const matchPattern = /(file:\/\/.+)\/[\w-_\s\d]+.ts:\d+:\d+/;
41+
const callingFunction = new Error().stack?.split("\n")[3];
42+
const match = callingFunction?.match(matchPattern);
43+
if (match) {
44+
const dir = new URL(match[1]);
45+
if (dir.protocol === "file:") {
46+
return normalizePath(dir.pathname);
47+
}
48+
}
49+
return undefined;
50+
}

0 commit comments

Comments
 (0)