Skip to content

Commit

Permalink
fix: use shim instead of node:path in runtime code (#117) (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Romuald Brillout <[email protected]>
  • Loading branch information
samuba and brillout authored Dec 3, 2024
1 parent 8b2c202 commit 6a47ec2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test:e2e": "test-e2e",
"test:types": "test-types",
"// Run `$ pnpm test:setup` before running `$ pnpm test`": "",
"test:setup": "pnpm run clean && pnpm install && pnpm run build",
"test:setup": "pnpm install && pnpm run build",
"========= Dev": "",
"// Develop telefunc": "",
"dev": "cd telefunc/ && pnpm run dev",
Expand Down
10 changes: 5 additions & 5 deletions telefunc/node/server/runTelefunc/loadTelefuncFilesFromConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { loadTelefuncFilesFromConfig }

import { assert, assertPosixPath, assertUsage, isTelefuncFilePath } from '../../utils'
import { posix } from 'node:path'
import type { TelefuncFiles } from '../types'
import { import_ } from '@brillout/import'
import pc from '@brillout/picocolors'
Expand Down Expand Up @@ -39,15 +38,16 @@ async function loadTelefuncFilesFromConfig(runContext: {

function resolveTelefuncFilePath(telefuncFilePathAbsolute: string, appRootDir: string): string {
assertPosixPath(telefuncFilePathAbsolute)
const path = posix.relative(appRootDir, telefuncFilePathAbsolute)
assertPosixPath(path)
assertUsage(
!path.startsWith('../'),
`The telefunc file ${telefuncFilePathAbsolute} doesn't live inside the root directory ${appRootDir} of your project. Either move your telefunc file inside the root, or change ${pc.cyan(
telefuncFilePathAbsolute.startsWith(appRootDir),
`The telefunc file ${telefuncFilePathAbsolute} doesn't live inside the root directory ${appRootDir} of your project. Either move the telefunc file inside the root directory, or change ${pc.cyan(
'config.root',
)} (https://telefunc.com/root).`,
)
let path = telefuncFilePathAbsolute.slice(appRootDir.length)
if (path.startsWith('/')) path = path.slice(1)
assert(!path.startsWith('/') && !path.startsWith('.'))
assertPosixPath(path)
const telefuncFilePath = '/' + path
assert(isTelefuncFilePath(telefuncFilePathAbsolute))
return telefuncFilePath
Expand Down
7 changes: 3 additions & 4 deletions telefunc/node/server/serverConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ export { configUser as config }
export { getServerConfig }
export type { ConfigUser }

import { isAbsolute } from 'node:path'
import { assertUsage, hasProp, toPosixPath, isTelefuncFilePath } from '../utils'
import { assertUsage, hasProp, toPosixPath, isTelefuncFilePath, pathIsAbsolute } from '../utils'

/** Telefunc Server Configuration */
type ConfigUser = {
Expand Down Expand Up @@ -64,7 +63,7 @@ function getServerConfig(): ConfigResolved {
function validateUserConfig(configUserUnwrapped: ConfigUser, prop: string, val: unknown) {
if (prop === 'root') {
assertUsage(typeof val === 'string', 'config.root should be a string')
assertUsage(isAbsolute(val), 'config.root should be an absolute path')
assertUsage(pathIsAbsolute(val), 'config.root should be an absolute path')
configUserUnwrapped[prop] = val
} else if (prop === 'telefuncUrl') {
assertUsage(typeof val === 'string', 'config.telefuncUrl should be a string')
Expand All @@ -78,7 +77,7 @@ function validateUserConfig(configUserUnwrapped: ConfigUser, prop: string, val:
assertUsage(Array.isArray(val), wrongType)
val.forEach((val: unknown) => {
assertUsage(typeof val === 'string', wrongType)
assertUsage(isAbsolute(val), `[config.telefuncFiles] ${val} should be an absolute path`)
assertUsage(pathIsAbsolute(val), `[config.telefuncFiles] ${val} should be an absolute path`)
assertUsage(isTelefuncFilePath(toPosixPath(val)), `[config.telefuncFiles] ${val} doesn't contain \`.telefunc.\``)
})
configUserUnwrapped[prop] = val
Expand Down
1 change: 1 addition & 0 deletions telefunc/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from '../utils/assertModuleScope'
export * from '../utils/isTelefuncFilePath'
export * from '../utils/isWebpack'
export * from '../utils/isVikeApp'
export * from '../utils/path-shim'
8 changes: 4 additions & 4 deletions telefunc/utils/getOutDirs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export { getOutDirAbsolute }

import type { UserConfig, ResolvedConfig } from 'vite'
import { assert } from './assert'
import { pathJoin } from './path-shim.js'
import { assertPosixPath, toPosixPath } from './filesystemPathHandling'
import path from 'node:path'

/** Appends `client/` or `server/` to `config.build.outDir` */
function determineOutDir(config: ResolvedConfig): string | null {
Expand Down Expand Up @@ -34,8 +34,8 @@ function determineOutDir(config: ResolvedConfig): string | null {
function declineOutDirs(outDirRoot: string) {
assertIsOutDirRoot(outDirRoot)
assertPosixPath(outDirRoot)
const outDirClient = path.posix.join(outDirRoot, 'client')
const outDirServer = path.posix.join(outDirRoot, 'server')
const outDirClient = pathJoin(outDirRoot, 'client')
const outDirServer = pathJoin(outDirRoot, 'server')
assertIsNotOutDirRoot(outDirClient)
assertIsNotOutDirRoot(outDirServer)
return { outDirClient, outDirServer }
Expand Down Expand Up @@ -71,7 +71,7 @@ function getOutDirAbsolute(config: ResolvedConfig): string {
if (!outDirIsAbsolutePath(outDir)) {
const { root } = config
assertPosixPath(root)
outDir = path.posix.join(root, outDir)
outDir = pathJoin(root, outDir)
}
return outDir
}
Expand Down
1 change: 0 additions & 1 deletion telefunc/utils/isTelefuncFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { isTelefuncFilePath }

import { assertPosixPath } from './filesystemPathHandling'
import { assert } from './assert'

function isTelefuncFilePath(filePath: string): boolean {
assertPosixPath(filePath)
Expand Down
21 changes: 21 additions & 0 deletions telefunc/utils/path-shim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export { pathJoin }
export { pathIsAbsolute }

// Simple shim for `import * from "node:path"` used by the server runtime.
// Robust alternative: https://github.com/unjs/pathe

import { assert } from './assert.js'

function pathJoin(path1: string, path2: string): string {
assert(!path1.includes('\\'))
assert(!path2.includes('\\'))
let joined = [...path1.split('/'), ...path2.split('/')].filter(Boolean).join('/')
if (path1.startsWith('/')) joined = '/' + joined
return joined
}

// https://github.com/unjs/pathe/blob/1eadc66c0fb3b2916cbcc1c73370bf4b824985ff/src/path.ts#L14
const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/
function pathIsAbsolute(filePath: string) {
return IS_ABSOLUTE_RE.test(filePath)
}

0 comments on commit 6a47ec2

Please sign in to comment.