@eighty4/dank 0.0.5-5 → 0.0.5-6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/dirs.ts CHANGED
@@ -60,29 +60,62 @@ export class Resolver {
60
60
  return join(this.#dirs.projectRootAbs, ...p)
61
61
  }
62
62
 
63
- // `p` is expected to be a relative path resolvable from the project dir
64
- isProjectSubpathInPagesDir(p: string): boolean {
65
- return resolve(join(this.#dirs.projectRootAbs, p)).startsWith(
63
+ isPagesSubpathResolvedToPagesDirSubpath(p: string): boolean {
64
+ return verifySubpathInRoot(this.#dirs.pagesAbs, this.#dirs.pagesAbs, p)
65
+ }
66
+
67
+ isPagesSubpathResolvedToProjectDirSubpath(p: string): boolean {
68
+ return verifySubpathInRoot(
69
+ this.#dirs.pagesAbs,
70
+ this.#dirs.projectRootAbs,
71
+ p,
72
+ )
73
+ }
74
+
75
+ isProjectSubpathResolvedToPagesDirSubpath(p: string): boolean {
76
+ return verifySubpathInRoot(
77
+ this.#dirs.projectRootAbs,
66
78
  this.#dirs.pagesAbs,
79
+ p,
67
80
  )
68
81
  }
69
82
 
70
- // `p` is expected to be a relative path resolvable from the pages dir
71
- isPagesSubpathInPagesDir(p: string): boolean {
72
- return this.isProjectSubpathInPagesDir(join(this.#dirs.pages, p))
83
+ isProjectSubpathResolvedToProjectDirSubpath(p: string): boolean {
84
+ return verifySubpathInRoot(
85
+ this.#dirs.projectRootAbs,
86
+ this.#dirs.projectRootAbs,
87
+ p,
88
+ )
73
89
  }
74
90
 
75
91
  projectPathFromAbsolute(p: string) {
76
92
  return p.replace(this.#dirs.projectRootAbs, '').substring(1)
77
93
  }
78
94
 
79
- // resolve a pages subpath from a resource within the pages directory by a relative href
80
95
  // `from` is expected to be a pages resource fs path starting with `pages/` and ending with filename
81
- // the result will be a pages subpath and will not have the pages dir prefix
82
- // returns 'outofbounds' if the relative path does not resolve to a file within the pages dir
83
- resolveHrefInPagesDir(from: string, href: string): string | ResolveError {
96
+ // `href` is a source relative path to another source used by a script src, link href or Worker ctor URL
97
+ // returns 'outofbounds' if the resolved path is not in the pages directory
98
+ resolvePagesRelativeHrefInPagesDir(
99
+ from: string,
100
+ href: string,
101
+ ): string | ResolveError {
84
102
  const p = join(dirname(from), href)
85
- if (this.isProjectSubpathInPagesDir(p)) {
103
+ if (this.isProjectSubpathResolvedToPagesDirSubpath(p)) {
104
+ return p
105
+ } else {
106
+ return 'outofbounds'
107
+ }
108
+ }
109
+
110
+ // `from` is expected to be a pages resource fs path starting with `pages/` and ending with filename
111
+ // `href` is a source relative path to another source used by a script src, link href or Worker ctor URL
112
+ // returns 'outofbounds' if the resolved path is not in the project directory
113
+ resolvePagesRelativeHrefInProjectDir(
114
+ from: string,
115
+ href: string,
116
+ ): string | ResolveError {
117
+ const p = join(dirname(from), href)
118
+ if (this.isProjectSubpathResolvedToProjectDirSubpath(p)) {
86
119
  return p
87
120
  } else {
88
121
  return 'outofbounds'
@@ -90,6 +123,14 @@ export class Resolver {
90
123
  }
91
124
  }
92
125
 
126
+ function verifySubpathInRoot(
127
+ resolveFrom: string,
128
+ expectWithin: string,
129
+ testSubpath: string,
130
+ ): boolean {
131
+ return resolve(join(resolveFrom, testSubpath)).startsWith(expectWithin)
132
+ }
133
+
93
134
  class WindowsResolver extends Resolver {
94
135
  constructor(dirs: DankDirectories) {
95
136
  super(dirs)
@@ -99,7 +140,21 @@ class WindowsResolver extends Resolver {
99
140
  return super.projectPathFromAbsolute(p).replaceAll('\\', '/')
100
141
  }
101
142
 
102
- resolveHrefInPagesDir(from: string, href: string): string | ResolveError {
103
- return super.resolveHrefInPagesDir(from, href).replaceAll('\\', '/')
143
+ resolvePagesRelativeHrefInPagesDir(
144
+ from: string,
145
+ href: string,
146
+ ): string | ResolveError {
147
+ return super
148
+ .resolvePagesRelativeHrefInPagesDir(from, href)
149
+ .replaceAll('\\', '/')
150
+ }
151
+
152
+ resolvePagesRelativeHrefInProjectDir(
153
+ from: string,
154
+ href: string,
155
+ ): string | ResolveError {
156
+ return super
157
+ .resolvePagesRelativeHrefInProjectDir(from, href)
158
+ .replaceAll('\\', '/')
104
159
  }
105
160
  }
package/lib/esbuild.ts CHANGED
@@ -156,10 +156,11 @@ export function workersPlugin(r: BuildRegistry): Plugin {
156
156
  1,
157
157
  workerCtorMatch.groups!.url.length - 1,
158
158
  )
159
- const workerEntryPoint = r.resolver.resolveHrefInPagesDir(
160
- clientScript,
161
- workerUrl,
162
- )
159
+ const workerEntryPoint =
160
+ r.resolver.resolvePagesRelativeHrefInProjectDir(
161
+ clientScript,
162
+ workerUrl,
163
+ )
163
164
  if (workerEntryPoint === 'outofbounds') {
164
165
  if (!errors) errors = []
165
166
  errors.push(
@@ -256,7 +257,7 @@ function outofboundsWorkerUrlCtorArg(
256
257
  ): PartialMessage {
257
258
  return {
258
259
  id: 'worker-url-outofbounds',
259
- text: `The ${workerCtorMatch.groups!.ctor} constructor URL arg \`${workerCtorMatch.groups!.url}\` cannot resolve to a path outside of the pages directory`,
260
+ text: `The ${workerCtorMatch.groups!.ctor} constructor URL arg \`${workerCtorMatch.groups!.url}\` cannot resolve to a path outside of the project directory`,
260
261
  location,
261
262
  }
262
263
  }
package/lib/html.ts CHANGED
@@ -237,7 +237,11 @@ export class HtmlEntrypoint extends EventEmitter<HtmlEntrypointEvents> {
237
237
  )
238
238
  return false
239
239
  }
240
- if (!this.#resolver.isPagesSubpathInPagesDir(p.fsPath)) {
240
+ if (
241
+ !this.#resolver.isPagesSubpathResolvedToPagesDirSubpath(
242
+ p.fsPath,
243
+ )
244
+ ) {
241
245
  this.#error(
242
246
  `partials cannot be referenced from outside the pages dir like \`${p.specifier}\` in webpage \`${join(this.#c.dirs.pages, this.#fsPath)}\``,
243
247
  )
@@ -395,7 +399,7 @@ export class HtmlEntrypoint extends EventEmitter<HtmlEntrypointEvents> {
395
399
  elem: Element,
396
400
  ): ImportedScript {
397
401
  const inPath = join(this.#c.dirs.pages, dirname(this.#fsPath), href)
398
- if (!this.#resolver.isProjectSubpathInPagesDir(inPath)) {
402
+ if (!this.#resolver.isPagesSubpathResolvedToPagesDirSubpath(inPath)) {
399
403
  throw new DankError(
400
404
  `href \`${href}\` in webpage \`${join(this.#c.dirs.pages, this.#fsPath)}\` cannot reference sources outside of the pages directory`,
401
405
  )
package/lib_js/dirs.js CHANGED
@@ -38,30 +38,47 @@ class Resolver {
38
38
  absProjectPath(...p) {
39
39
  return join(this.#dirs.projectRootAbs, ...p);
40
40
  }
41
- // `p` is expected to be a relative path resolvable from the project dir
42
- isProjectSubpathInPagesDir(p) {
43
- return resolve(join(this.#dirs.projectRootAbs, p)).startsWith(this.#dirs.pagesAbs);
41
+ isPagesSubpathResolvedToPagesDirSubpath(p) {
42
+ return verifySubpathInRoot(this.#dirs.pagesAbs, this.#dirs.pagesAbs, p);
44
43
  }
45
- // `p` is expected to be a relative path resolvable from the pages dir
46
- isPagesSubpathInPagesDir(p) {
47
- return this.isProjectSubpathInPagesDir(join(this.#dirs.pages, p));
44
+ isPagesSubpathResolvedToProjectDirSubpath(p) {
45
+ return verifySubpathInRoot(this.#dirs.pagesAbs, this.#dirs.projectRootAbs, p);
46
+ }
47
+ isProjectSubpathResolvedToPagesDirSubpath(p) {
48
+ return verifySubpathInRoot(this.#dirs.projectRootAbs, this.#dirs.pagesAbs, p);
49
+ }
50
+ isProjectSubpathResolvedToProjectDirSubpath(p) {
51
+ return verifySubpathInRoot(this.#dirs.projectRootAbs, this.#dirs.projectRootAbs, p);
48
52
  }
49
53
  projectPathFromAbsolute(p) {
50
54
  return p.replace(this.#dirs.projectRootAbs, "").substring(1);
51
55
  }
52
- // resolve a pages subpath from a resource within the pages directory by a relative href
53
56
  // `from` is expected to be a pages resource fs path starting with `pages/` and ending with filename
54
- // the result will be a pages subpath and will not have the pages dir prefix
55
- // returns 'outofbounds' if the relative path does not resolve to a file within the pages dir
56
- resolveHrefInPagesDir(from, href) {
57
+ // `href` is a source relative path to another source used by a script src, link href or Worker ctor URL
58
+ // returns 'outofbounds' if the resolved path is not in the pages directory
59
+ resolvePagesRelativeHrefInPagesDir(from, href) {
57
60
  const p = join(dirname(from), href);
58
- if (this.isProjectSubpathInPagesDir(p)) {
61
+ if (this.isProjectSubpathResolvedToPagesDirSubpath(p)) {
62
+ return p;
63
+ } else {
64
+ return "outofbounds";
65
+ }
66
+ }
67
+ // `from` is expected to be a pages resource fs path starting with `pages/` and ending with filename
68
+ // `href` is a source relative path to another source used by a script src, link href or Worker ctor URL
69
+ // returns 'outofbounds' if the resolved path is not in the project directory
70
+ resolvePagesRelativeHrefInProjectDir(from, href) {
71
+ const p = join(dirname(from), href);
72
+ if (this.isProjectSubpathResolvedToProjectDirSubpath(p)) {
59
73
  return p;
60
74
  } else {
61
75
  return "outofbounds";
62
76
  }
63
77
  }
64
78
  }
79
+ function verifySubpathInRoot(resolveFrom, expectWithin, testSubpath) {
80
+ return resolve(join(resolveFrom, testSubpath)).startsWith(expectWithin);
81
+ }
65
82
  class WindowsResolver extends Resolver {
66
83
  constructor(dirs) {
67
84
  super(dirs);
@@ -69,8 +86,11 @@ class WindowsResolver extends Resolver {
69
86
  projectPathFromAbsolute(p) {
70
87
  return super.projectPathFromAbsolute(p).replaceAll("\\", "/");
71
88
  }
72
- resolveHrefInPagesDir(from, href) {
73
- return super.resolveHrefInPagesDir(from, href).replaceAll("\\", "/");
89
+ resolvePagesRelativeHrefInPagesDir(from, href) {
90
+ return super.resolvePagesRelativeHrefInPagesDir(from, href).replaceAll("\\", "/");
91
+ }
92
+ resolvePagesRelativeHrefInProjectDir(from, href) {
93
+ return super.resolvePagesRelativeHrefInProjectDir(from, href).replaceAll("\\", "/");
74
94
  }
75
95
  }
76
96
  export {
package/lib_js/esbuild.js CHANGED
@@ -99,7 +99,7 @@ function workersPlugin(r) {
99
99
  clientScript = r.resolver.projectPathFromAbsolute(args.path);
100
100
  }
101
101
  const workerUrl = workerCtorMatch.groups.url.substring(1, workerCtorMatch.groups.url.length - 1);
102
- const workerEntryPoint = r.resolver.resolveHrefInPagesDir(clientScript, workerUrl);
102
+ const workerEntryPoint = r.resolver.resolvePagesRelativeHrefInProjectDir(clientScript, workerUrl);
103
103
  if (workerEntryPoint === "outofbounds") {
104
104
  if (!errors)
105
105
  errors = [];
@@ -159,7 +159,7 @@ function locationFromMatch(args, contents, match) {
159
159
  function outofboundsWorkerUrlCtorArg(location, workerCtorMatch) {
160
160
  return {
161
161
  id: "worker-url-outofbounds",
162
- text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` cannot resolve to a path outside of the pages directory`,
162
+ text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` cannot resolve to a path outside of the project directory`,
163
163
  location
164
164
  };
165
165
  }
package/lib_js/html.js CHANGED
@@ -115,7 +115,7 @@ class HtmlEntrypoint extends EventEmitter {
115
115
  this.#error(`partials cannot be referenced with an absolute path like \`${p.specifier}\` in webpage \`${join(this.#c.dirs.pages, this.#fsPath)}\``);
116
116
  return false;
117
117
  }
118
- if (!this.#resolver.isPagesSubpathInPagesDir(p.fsPath)) {
118
+ if (!this.#resolver.isPagesSubpathResolvedToPagesDirSubpath(p.fsPath)) {
119
119
  this.#error(`partials cannot be referenced from outside the pages dir like \`${p.specifier}\` in webpage \`${join(this.#c.dirs.pages, this.#fsPath)}\``);
120
120
  return false;
121
121
  }
@@ -222,7 +222,7 @@ class HtmlEntrypoint extends EventEmitter {
222
222
  }
223
223
  #parseImport(type, href, elem) {
224
224
  const inPath = join(this.#c.dirs.pages, dirname(this.#fsPath), href);
225
- if (!this.#resolver.isProjectSubpathInPagesDir(inPath)) {
225
+ if (!this.#resolver.isPagesSubpathResolvedToPagesDirSubpath(inPath)) {
226
226
  throw new DankError(`href \`${href}\` in webpage \`${join(this.#c.dirs.pages, this.#fsPath)}\` cannot reference sources outside of the pages directory`);
227
227
  }
228
228
  let outPath = join(dirname(this.#fsPath), href);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eighty4/dank",
3
- "version": "0.0.5-5",
3
+ "version": "0.0.5-6",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Adam McKee Bennett <adam.be.g84d@gmail.com>",