migrate: move flock and hash utilities to shared package (#22640)
This commit is contained in:
parent
f1751401aa
commit
4ae7c77f8a
23 changed files with 929 additions and 41 deletions
2
bun.lock
2
bun.lock
|
|
@ -527,9 +527,11 @@
|
||||||
"mime-types": "3.0.2",
|
"mime-types": "3.0.2",
|
||||||
"minimatch": "10.2.5",
|
"minimatch": "10.2.5",
|
||||||
"semver": "catalog:",
|
"semver": "catalog:",
|
||||||
|
"xdg-basedir": "5.1.0",
|
||||||
"zod": "catalog:",
|
"zod": "catalog:",
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/bun": "catalog:",
|
||||||
"@types/semver": "catalog:",
|
"@types/semver": "catalog:",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import {
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
import { pathToFileURL } from "url"
|
import { pathToFileURL } from "url"
|
||||||
import { Filesystem } from "../util/filesystem"
|
import { Filesystem } from "../util/filesystem"
|
||||||
import { Hash } from "../util/hash"
|
import { Hash } from "@opencode-ai/shared/util/hash"
|
||||||
import { ACPSessionManager } from "./session"
|
import { ACPSessionManager } from "./session"
|
||||||
import type { ACPConfig } from "./types"
|
import type { ACPConfig } from "./types"
|
||||||
import { Provider } from "../provider/provider"
|
import { Provider } from "../provider/provider"
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import { hasTheme, upsertTheme } from "../context/theme"
|
||||||
import { Global } from "@/global"
|
import { Global } from "@/global"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { Process } from "@/util/process"
|
import { Process } from "@/util/process"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { Flag } from "@/flag/flag"
|
import { Flag } from "@/flag/flag"
|
||||||
import { INTERNAL_TUI_PLUGINS, type InternalTuiPlugin } from "./internal"
|
import { INTERNAL_TUI_PLUGINS, type InternalTuiPlugin } from "./internal"
|
||||||
import { setupSlots, Slot as View } from "./slots"
|
import { setupSlots, Slot as View } from "./slots"
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ import type { ConsoleState } from "./console-state"
|
||||||
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
||||||
import { InstanceState } from "@/effect/instance-state"
|
import { InstanceState } from "@/effect/instance-state"
|
||||||
import { Context, Duration, Effect, Exit, Fiber, Layer, Option } from "effect"
|
import { Context, Duration, Effect, Exit, Fiber, Layer, Option } from "effect"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { isPathPluginSpec, parsePluginSpecifier, resolvePathPluginTarget } from "@/plugin/shared"
|
import { isPathPluginSpec, parsePluginSpecifier, resolvePathPluginTarget } from "@/plugin/shared"
|
||||||
import { Npm } from "../npm"
|
import { Npm } from "../npm"
|
||||||
import { InstanceRef } from "@/effect/instance-ref"
|
import { InstanceRef } from "@/effect/instance-ref"
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import os from "os"
|
import os from "os"
|
||||||
import { Filesystem } from "../util/filesystem"
|
import { Filesystem } from "../util/filesystem"
|
||||||
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
|
|
||||||
const app = "opencode"
|
const app = "opencode"
|
||||||
|
|
||||||
|
|
@ -26,6 +27,9 @@ export namespace Global {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Initialize Flock with global state path
|
||||||
|
Flock.setGlobal({ state })
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fs.mkdir(Global.Path.data, { recursive: true }),
|
fs.mkdir(Global.Path.data, { recursive: true }),
|
||||||
fs.mkdir(Global.Path.config, { recursive: true }),
|
fs.mkdir(Global.Path.config, { recursive: true }),
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import { Log } from "../util/log"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { readdir, rm } from "fs/promises"
|
import { readdir, rm } from "fs/promises"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { Arborist } from "@npmcli/arborist"
|
import { Arborist } from "@npmcli/arborist"
|
||||||
|
|
||||||
export namespace Npm {
|
export namespace Npm {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import {
|
||||||
import { ConfigPaths } from "@/config/paths"
|
import { ConfigPaths } from "@/config/paths"
|
||||||
import { Global } from "@/global"
|
import { Global } from "@/global"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { isRecord } from "@/util/record"
|
import { isRecord } from "@/util/record"
|
||||||
|
|
||||||
import { parsePluginSpecifier, readPackageThemes, readPluginPackage, resolvePluginTarget } from "./shared"
|
import { parsePluginSpecifier, readPackageThemes, readPluginPackage, resolvePluginTarget } from "./shared"
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { fileURLToPath } from "url"
|
||||||
import { Flag } from "@/flag/flag"
|
import { Flag } from "@/flag/flag"
|
||||||
import { Global } from "@/global"
|
import { Global } from "@/global"
|
||||||
import { Filesystem } from "@/util/filesystem"
|
import { Filesystem } from "@/util/filesystem"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
|
|
||||||
import { parsePluginSpecifier, pluginSource } from "./shared"
|
import { parsePluginSpecifier, pluginSource } from "./shared"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import { Installation } from "../installation"
|
||||||
import { Flag } from "../flag/flag"
|
import { Flag } from "../flag/flag"
|
||||||
import { lazy } from "@/util/lazy"
|
import { lazy } from "@/util/lazy"
|
||||||
import { Filesystem } from "../util/filesystem"
|
import { Filesystem } from "../util/filesystem"
|
||||||
import { Flock } from "@/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { Hash } from "@/util/hash"
|
import { Hash } from "@opencode-ai/shared/util/hash"
|
||||||
|
|
||||||
// Try to import bundled snapshot (generated at build time)
|
// Try to import bundled snapshot (generated at build time)
|
||||||
// Falls back to undefined in dev mode when snapshot doesn't exist
|
// Falls back to undefined in dev mode when snapshot doesn't exist
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import { mapValues, mergeDeep, omit, pickBy, sortBy } from "remeda"
|
||||||
import { NoSuchModelError, type Provider as SDK } from "ai"
|
import { NoSuchModelError, type Provider as SDK } from "ai"
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
import { Npm } from "../npm"
|
import { Npm } from "../npm"
|
||||||
import { Hash } from "../util/hash"
|
import { Hash } from "@opencode-ai/shared/util/hash"
|
||||||
import { Plugin } from "../plugin"
|
import { Plugin } from "../plugin"
|
||||||
import { NamedError } from "@opencode-ai/shared/util/error"
|
import { NamedError } from "@opencode-ai/shared/util/error"
|
||||||
import { type LanguageModelV3 } from "@ai-sdk/provider"
|
import { type LanguageModelV3 } from "@ai-sdk/provider"
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import z from "zod"
|
||||||
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
|
import * as CrossSpawnSpawner from "@/effect/cross-spawn-spawner"
|
||||||
import { InstanceState } from "@/effect/instance-state"
|
import { InstanceState } from "@/effect/instance-state"
|
||||||
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
||||||
import { Hash } from "@/util/hash"
|
import { Hash } from "@opencode-ai/shared/util/hash"
|
||||||
import { Config } from "../config/config"
|
import { Config } from "../config/config"
|
||||||
import { Global } from "../global"
|
import { Global } from "../global"
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
import { Flock } from "../../src/util/flock"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
|
|
||||||
type Msg = {
|
type Msg = {
|
||||||
key: string
|
key: string
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {},
|
"scripts": {
|
||||||
|
"test": "bun test"
|
||||||
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"opencode": "./bin/opencode"
|
"opencode": "./bin/opencode"
|
||||||
},
|
},
|
||||||
|
|
@ -14,7 +16,8 @@
|
||||||
},
|
},
|
||||||
"imports": {},
|
"imports": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/semver": "catalog:"
|
"@types/semver": "catalog:",
|
||||||
|
"@types/bun": "catalog:"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@effect/platform-node": "catalog:",
|
"@effect/platform-node": "catalog:",
|
||||||
|
|
@ -23,6 +26,7 @@
|
||||||
"mime-types": "3.0.2",
|
"mime-types": "3.0.2",
|
||||||
"minimatch": "10.2.5",
|
"minimatch": "10.2.5",
|
||||||
"semver": "catalog:",
|
"semver": "catalog:",
|
||||||
|
"xdg-basedir": "5.1.0",
|
||||||
"zod": "catalog:"
|
"zod": "catalog:"
|
||||||
},
|
},
|
||||||
"overrides": {
|
"overrides": {
|
||||||
|
|
|
||||||
42
packages/shared/src/global.ts
Normal file
42
packages/shared/src/global.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import path from "path"
|
||||||
|
import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir"
|
||||||
|
import os from "os"
|
||||||
|
import { Context, Effect, Layer } from "effect"
|
||||||
|
|
||||||
|
export namespace Global {
|
||||||
|
export class Service extends Context.Service<Service, Interface>()("@opencode/Global") {}
|
||||||
|
|
||||||
|
export interface Interface {
|
||||||
|
readonly home: string
|
||||||
|
readonly data: string
|
||||||
|
readonly cache: string
|
||||||
|
readonly config: string
|
||||||
|
readonly state: string
|
||||||
|
readonly bin: string
|
||||||
|
readonly log: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export const layer = Layer.effect(
|
||||||
|
Service,
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const app = "opencode"
|
||||||
|
const home = process.env.OPENCODE_TEST_HOME ?? os.homedir()
|
||||||
|
const data = path.join(xdgData!, app)
|
||||||
|
const cache = path.join(xdgCache!, app)
|
||||||
|
const cfg = path.join(xdgConfig!, app)
|
||||||
|
const state = path.join(xdgState!, app)
|
||||||
|
const bin = path.join(cache, "bin")
|
||||||
|
const log = path.join(data, "log")
|
||||||
|
|
||||||
|
return Service.of({
|
||||||
|
home,
|
||||||
|
data,
|
||||||
|
cache,
|
||||||
|
config: cfg,
|
||||||
|
state,
|
||||||
|
bin,
|
||||||
|
log,
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
247
packages/shared/src/npm.ts
Normal file
247
packages/shared/src/npm.ts
Normal file
|
|
@ -0,0 +1,247 @@
|
||||||
|
import path from "path"
|
||||||
|
import semver from "semver"
|
||||||
|
import { Arborist } from "@npmcli/arborist"
|
||||||
|
import { Effect, Schema, Context, Layer, Option, FileSystem } from "effect"
|
||||||
|
import { NodeFileSystem } from "@effect/platform-node"
|
||||||
|
import { AppFileSystem } from "./filesystem"
|
||||||
|
import { Global } from "./global"
|
||||||
|
import { Flock } from "./util/flock"
|
||||||
|
|
||||||
|
export namespace Npm {
|
||||||
|
export class InstallFailedError extends Schema.TaggedErrorClass<InstallFailedError>()("NpmInstallFailedError", {
|
||||||
|
pkg: Schema.String,
|
||||||
|
cause: Schema.optional(Schema.Defect),
|
||||||
|
}) {}
|
||||||
|
|
||||||
|
export interface EntryPoint {
|
||||||
|
readonly directory: string
|
||||||
|
readonly entrypoint: Option.Option<string>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Interface {
|
||||||
|
readonly add: (pkg: string) => Effect.Effect<EntryPoint, InstallFailedError>
|
||||||
|
readonly install: (dir: string) => Effect.Effect<void>
|
||||||
|
readonly outdated: (pkg: string, cachedVersion: string) => Effect.Effect<boolean>
|
||||||
|
readonly which: (pkg: string) => Effect.Effect<Option.Option<string>>
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Service extends Context.Service<Service, Interface>()("@opencode/Npm") {}
|
||||||
|
|
||||||
|
const illegal = process.platform === "win32" ? new Set(["<", ">", ":", '"', "|", "?", "*"]) : undefined
|
||||||
|
|
||||||
|
export function sanitize(pkg: string) {
|
||||||
|
if (!illegal) return pkg
|
||||||
|
return Array.from(pkg, (char) => (illegal.has(char) || char.charCodeAt(0) < 32 ? "_" : char)).join("")
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolveEntryPoint = (name: string, dir: string): EntryPoint => {
|
||||||
|
let entrypoint: Option.Option<string>
|
||||||
|
try {
|
||||||
|
const resolved = typeof Bun !== "undefined" ? import.meta.resolve(name, dir) : import.meta.resolve(dir)
|
||||||
|
entrypoint = Option.some(resolved)
|
||||||
|
} catch {
|
||||||
|
entrypoint = Option.none()
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
directory: dir,
|
||||||
|
entrypoint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArboristNode {
|
||||||
|
name: string
|
||||||
|
path: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ArboristTree {
|
||||||
|
edgesOut: Map<string, { to?: ArboristNode }>
|
||||||
|
}
|
||||||
|
export const layer = Layer.effect(
|
||||||
|
Service,
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const afs = yield* AppFileSystem.Service
|
||||||
|
const global = yield* Global.Service
|
||||||
|
const fs = yield* FileSystem.FileSystem
|
||||||
|
const directory = (pkg: string) => path.join(global.cache, "packages", sanitize(pkg))
|
||||||
|
|
||||||
|
const outdated = Effect.fn("Npm.outdated")(function* (pkg: string, cachedVersion: string) {
|
||||||
|
const response = yield* Effect.tryPromise({
|
||||||
|
try: () => fetch(`https://registry.npmjs.org/${pkg}`),
|
||||||
|
catch: () => undefined,
|
||||||
|
}).pipe(Effect.orElseSucceed(() => undefined))
|
||||||
|
|
||||||
|
if (!response || !response.ok) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = yield* Effect.tryPromise({
|
||||||
|
try: () => response.json() as Promise<{ "dist-tags"?: { latest?: string } }>,
|
||||||
|
catch: () => undefined,
|
||||||
|
}).pipe(Effect.orElseSucceed(() => undefined))
|
||||||
|
|
||||||
|
const latestVersion = data?.["dist-tags"]?.latest
|
||||||
|
if (!latestVersion) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
const range = /[\s^~*xX<>|=]/.test(cachedVersion)
|
||||||
|
if (range) return !semver.satisfies(latestVersion, cachedVersion)
|
||||||
|
|
||||||
|
return semver.lt(cachedVersion, latestVersion)
|
||||||
|
})
|
||||||
|
|
||||||
|
const add = Effect.fn("Npm.add")(function* (pkg: string) {
|
||||||
|
const dir = directory(pkg)
|
||||||
|
yield* Flock.effect(`npm-install:${dir}`)
|
||||||
|
|
||||||
|
const arborist = new Arborist({
|
||||||
|
path: dir,
|
||||||
|
binLinks: true,
|
||||||
|
progress: false,
|
||||||
|
savePrefix: "",
|
||||||
|
ignoreScripts: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const tree = yield* Effect.tryPromise({
|
||||||
|
try: () => arborist.loadVirtual().catch(() => undefined),
|
||||||
|
catch: () => undefined,
|
||||||
|
}).pipe(Effect.orElseSucceed(() => undefined)) as Effect.Effect<ArboristTree | undefined>
|
||||||
|
|
||||||
|
if (tree) {
|
||||||
|
const first = tree.edgesOut.values().next().value?.to
|
||||||
|
if (first) {
|
||||||
|
return resolveEntryPoint(first.name, first.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = yield* Effect.tryPromise({
|
||||||
|
try: () =>
|
||||||
|
arborist.reify({
|
||||||
|
add: [pkg],
|
||||||
|
save: true,
|
||||||
|
saveType: "prod",
|
||||||
|
}),
|
||||||
|
catch: (cause) => new InstallFailedError({ pkg, cause }),
|
||||||
|
}) as Effect.Effect<ArboristTree, InstallFailedError>
|
||||||
|
|
||||||
|
const first = result.edgesOut.values().next().value?.to
|
||||||
|
if (!first) {
|
||||||
|
return yield* new InstallFailedError({ pkg })
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolveEntryPoint(first.name, first.path)
|
||||||
|
}, Effect.scoped)
|
||||||
|
|
||||||
|
const install = Effect.fn("Npm.install")(function* (dir: string) {
|
||||||
|
yield* Flock.effect(`npm-install:${dir}`)
|
||||||
|
|
||||||
|
const reify = Effect.fnUntraced(function* () {
|
||||||
|
const arb = new Arborist({
|
||||||
|
path: dir,
|
||||||
|
binLinks: true,
|
||||||
|
progress: false,
|
||||||
|
savePrefix: "",
|
||||||
|
ignoreScripts: true,
|
||||||
|
})
|
||||||
|
yield* Effect.tryPromise({
|
||||||
|
try: () => arb.reify().catch(() => {}),
|
||||||
|
catch: () => {},
|
||||||
|
}).pipe(Effect.orElseSucceed(() => {}))
|
||||||
|
})
|
||||||
|
|
||||||
|
const nodeModulesExists = yield* afs.existsSafe(path.join(dir, "node_modules"))
|
||||||
|
if (!nodeModulesExists) {
|
||||||
|
yield* reify()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const pkg = yield* afs.readJson(path.join(dir, "package.json")).pipe(Effect.orElseSucceed(() => ({})))
|
||||||
|
const lock = yield* afs.readJson(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => ({})))
|
||||||
|
|
||||||
|
const pkgAny = pkg as any
|
||||||
|
const lockAny = lock as any
|
||||||
|
|
||||||
|
const declared = new Set([
|
||||||
|
...Object.keys(pkgAny?.dependencies || {}),
|
||||||
|
...Object.keys(pkgAny?.devDependencies || {}),
|
||||||
|
...Object.keys(pkgAny?.peerDependencies || {}),
|
||||||
|
...Object.keys(pkgAny?.optionalDependencies || {}),
|
||||||
|
])
|
||||||
|
|
||||||
|
const root = lockAny?.packages?.[""] || {}
|
||||||
|
const locked = new Set([
|
||||||
|
...Object.keys(root?.dependencies || {}),
|
||||||
|
...Object.keys(root?.devDependencies || {}),
|
||||||
|
...Object.keys(root?.peerDependencies || {}),
|
||||||
|
...Object.keys(root?.optionalDependencies || {}),
|
||||||
|
])
|
||||||
|
|
||||||
|
for (const name of declared) {
|
||||||
|
if (!locked.has(name)) {
|
||||||
|
yield* reify()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, Effect.scoped)
|
||||||
|
|
||||||
|
const which = Effect.fn("Npm.which")(function* (pkg: string) {
|
||||||
|
const dir = directory(pkg)
|
||||||
|
const binDir = path.join(dir, "node_modules", ".bin")
|
||||||
|
|
||||||
|
const pick = Effect.fnUntraced(function* () {
|
||||||
|
const files = yield* fs.readDirectory(binDir).pipe(Effect.catch(() => Effect.succeed([] as string[])))
|
||||||
|
|
||||||
|
if (files.length === 0) return Option.none<string>()
|
||||||
|
if (files.length === 1) return Option.some(files[0])
|
||||||
|
|
||||||
|
const pkgJson = yield* afs.readJson(path.join(dir, "node_modules", pkg, "package.json")).pipe(Effect.option)
|
||||||
|
|
||||||
|
if (Option.isSome(pkgJson)) {
|
||||||
|
const parsed = pkgJson.value as { bin?: string | Record<string, string> }
|
||||||
|
if (parsed?.bin) {
|
||||||
|
const unscoped = pkg.startsWith("@") ? pkg.split("/")[1] : pkg
|
||||||
|
const bin = parsed.bin
|
||||||
|
if (typeof bin === "string") return Option.some(unscoped)
|
||||||
|
const keys = Object.keys(bin)
|
||||||
|
if (keys.length === 1) return Option.some(keys[0])
|
||||||
|
return bin[unscoped] ? Option.some(unscoped) : Option.some(keys[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Option.some(files[0])
|
||||||
|
})
|
||||||
|
|
||||||
|
return yield* Effect.gen(function* () {
|
||||||
|
const bin = yield* pick()
|
||||||
|
if (Option.isSome(bin)) {
|
||||||
|
return Option.some(path.join(binDir, bin.value))
|
||||||
|
}
|
||||||
|
|
||||||
|
yield* fs.remove(path.join(dir, "package-lock.json")).pipe(Effect.orElseSucceed(() => {}))
|
||||||
|
|
||||||
|
yield* add(pkg)
|
||||||
|
|
||||||
|
const resolved = yield* pick()
|
||||||
|
if (Option.isNone(resolved)) return Option.none<string>()
|
||||||
|
return Option.some(path.join(binDir, resolved.value))
|
||||||
|
}).pipe(
|
||||||
|
Effect.scoped,
|
||||||
|
Effect.orElseSucceed(() => Option.none<string>()),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
return Service.of({
|
||||||
|
add,
|
||||||
|
install,
|
||||||
|
outdated,
|
||||||
|
which,
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
export const defaultLayer = layer.pipe(
|
||||||
|
Layer.provide(AppFileSystem.layer),
|
||||||
|
Layer.provide(Global.layer),
|
||||||
|
Layer.provide(NodeFileSystem.layer),
|
||||||
|
)
|
||||||
|
}
|
||||||
44
packages/shared/src/types.d.ts
vendored
Normal file
44
packages/shared/src/types.d.ts
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
declare module "@npmcli/arborist" {
|
||||||
|
export interface ArboristOptions {
|
||||||
|
path: string
|
||||||
|
binLinks?: boolean
|
||||||
|
progress?: boolean
|
||||||
|
savePrefix?: string
|
||||||
|
ignoreScripts?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ArboristNode {
|
||||||
|
name: string
|
||||||
|
path: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ArboristEdge {
|
||||||
|
to?: ArboristNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ArboristTree {
|
||||||
|
edgesOut: Map<string, ArboristEdge>
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ReifyOptions {
|
||||||
|
add?: string[]
|
||||||
|
save?: boolean
|
||||||
|
saveType?: "prod" | "dev" | "optional" | "peer"
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Arborist {
|
||||||
|
constructor(options: ArboristOptions)
|
||||||
|
loadVirtual(): Promise<ArboristTree | undefined>
|
||||||
|
reify(options?: ReifyOptions): Promise<ArboristTree>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
declare var Bun:
|
||||||
|
| {
|
||||||
|
file(path: string): {
|
||||||
|
text(): Promise<string>
|
||||||
|
json(): Promise<unknown>
|
||||||
|
}
|
||||||
|
write(path: string, content: string | Uint8Array): Promise<void>
|
||||||
|
}
|
||||||
|
| undefined
|
||||||
|
|
@ -2,11 +2,25 @@ import path from "path"
|
||||||
import os from "os"
|
import os from "os"
|
||||||
import { randomBytes, randomUUID } from "crypto"
|
import { randomBytes, randomUUID } from "crypto"
|
||||||
import { mkdir, readFile, rm, stat, utimes, writeFile } from "fs/promises"
|
import { mkdir, readFile, rm, stat, utimes, writeFile } from "fs/promises"
|
||||||
import { Global } from "@/global"
|
import { Hash } from "./hash"
|
||||||
import { Hash } from "@/util/hash"
|
import { Effect } from "effect"
|
||||||
|
|
||||||
|
export type FlockGlobal = {
|
||||||
|
state: string
|
||||||
|
}
|
||||||
|
|
||||||
export namespace Flock {
|
export namespace Flock {
|
||||||
const root = path.join(Global.Path.state, "locks")
|
let global: FlockGlobal | undefined
|
||||||
|
|
||||||
|
export function setGlobal(g: FlockGlobal) {
|
||||||
|
global = g
|
||||||
|
}
|
||||||
|
|
||||||
|
const root = () => {
|
||||||
|
if (!global) throw new Error("Flock global not set")
|
||||||
|
return path.join(global.state, "locks")
|
||||||
|
}
|
||||||
|
|
||||||
// Defaults for callers that do not provide timing options.
|
// Defaults for callers that do not provide timing options.
|
||||||
const defaultOpts = {
|
const defaultOpts = {
|
||||||
staleMs: 60_000,
|
staleMs: 60_000,
|
||||||
|
|
@ -301,7 +315,7 @@ export namespace Flock {
|
||||||
baseDelayMs: input.baseDelayMs ?? defaultOpts.baseDelayMs,
|
baseDelayMs: input.baseDelayMs ?? defaultOpts.baseDelayMs,
|
||||||
maxDelayMs: input.maxDelayMs ?? defaultOpts.maxDelayMs,
|
maxDelayMs: input.maxDelayMs ?? defaultOpts.maxDelayMs,
|
||||||
}
|
}
|
||||||
const dir = input.dir ?? root
|
const dir = input.dir ?? root()
|
||||||
|
|
||||||
await mkdir(dir, { recursive: true })
|
await mkdir(dir, { recursive: true })
|
||||||
const lockfile = path.join(dir, Hash.fast(key) + ".lock")
|
const lockfile = path.join(dir, Hash.fast(key) + ".lock")
|
||||||
|
|
@ -330,4 +344,11 @@ export namespace Flock {
|
||||||
input.signal?.throwIfAborted()
|
input.signal?.throwIfAborted()
|
||||||
return await fn()
|
return await fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const effect = Effect.fn("Flock.effect")(function* (key: string) {
|
||||||
|
return yield* Effect.acquireRelease(
|
||||||
|
Effect.promise((signal) => Flock.acquire(key, { signal })),
|
||||||
|
(foo) => Effect.promise(() => foo.release()),
|
||||||
|
).pipe(Effect.asVoid)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
338
packages/shared/test/filesystem/filesystem.test.ts
Normal file
338
packages/shared/test/filesystem/filesystem.test.ts
Normal file
|
|
@ -0,0 +1,338 @@
|
||||||
|
import { describe, test, expect } from "bun:test"
|
||||||
|
import { Effect, Layer, FileSystem } from "effect"
|
||||||
|
import { NodeFileSystem } from "@effect/platform-node"
|
||||||
|
import { AppFileSystem } from "@opencode-ai/shared/filesystem"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
|
const live = AppFileSystem.layer.pipe(Layer.provideMerge(NodeFileSystem.layer))
|
||||||
|
const { effect: it } = testEffect(live)
|
||||||
|
|
||||||
|
describe("AppFileSystem", () => {
|
||||||
|
describe("isDir", () => {
|
||||||
|
it(
|
||||||
|
"returns true for directories",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
expect(yield* fs.isDir(tmp)).toBe(true)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"returns false for files",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "test.txt")
|
||||||
|
yield* filesys.writeFileString(file, "hello")
|
||||||
|
expect(yield* fs.isDir(file)).toBe(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"returns false for non-existent paths",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
expect(yield* fs.isDir("/tmp/nonexistent-" + Math.random())).toBe(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("isFile", () => {
|
||||||
|
it(
|
||||||
|
"returns true for files",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "test.txt")
|
||||||
|
yield* filesys.writeFileString(file, "hello")
|
||||||
|
expect(yield* fs.isFile(file)).toBe(true)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"returns false for directories",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
expect(yield* fs.isFile(tmp)).toBe(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("readJson / writeJson", () => {
|
||||||
|
it(
|
||||||
|
"round-trips JSON data",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "data.json")
|
||||||
|
const data = { name: "test", count: 42, nested: { ok: true } }
|
||||||
|
|
||||||
|
yield* fs.writeJson(file, data)
|
||||||
|
const result = yield* fs.readJson(file)
|
||||||
|
|
||||||
|
expect(result).toEqual(data)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("ensureDir", () => {
|
||||||
|
it(
|
||||||
|
"creates nested directories",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const nested = path.join(tmp, "a", "b", "c")
|
||||||
|
|
||||||
|
yield* fs.ensureDir(nested)
|
||||||
|
|
||||||
|
const info = yield* filesys.stat(nested)
|
||||||
|
expect(info.type).toBe("Directory")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"is idempotent",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const dir = path.join(tmp, "existing")
|
||||||
|
yield* filesys.makeDirectory(dir)
|
||||||
|
|
||||||
|
yield* fs.ensureDir(dir)
|
||||||
|
|
||||||
|
const info = yield* filesys.stat(dir)
|
||||||
|
expect(info.type).toBe("Directory")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("writeWithDirs", () => {
|
||||||
|
it(
|
||||||
|
"creates parent directories if missing",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "deep", "nested", "file.txt")
|
||||||
|
|
||||||
|
yield* fs.writeWithDirs(file, "hello")
|
||||||
|
|
||||||
|
expect(yield* filesys.readFileString(file)).toBe("hello")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"writes directly when parent exists",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "direct.txt")
|
||||||
|
|
||||||
|
yield* fs.writeWithDirs(file, "world")
|
||||||
|
|
||||||
|
expect(yield* filesys.readFileString(file)).toBe("world")
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"writes Uint8Array content",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "binary.bin")
|
||||||
|
const content = new Uint8Array([0x00, 0x01, 0x02, 0x03])
|
||||||
|
|
||||||
|
yield* fs.writeWithDirs(file, content)
|
||||||
|
|
||||||
|
const result = yield* filesys.readFile(file)
|
||||||
|
expect(new Uint8Array(result)).toEqual(content)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("findUp", () => {
|
||||||
|
it(
|
||||||
|
"finds target in start directory",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "target.txt"), "found")
|
||||||
|
|
||||||
|
const result = yield* fs.findUp("target.txt", tmp)
|
||||||
|
expect(result).toEqual([path.join(tmp, "target.txt")])
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"finds target in parent directories",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "marker"), "root")
|
||||||
|
const child = path.join(tmp, "a", "b")
|
||||||
|
yield* filesys.makeDirectory(child, { recursive: true })
|
||||||
|
|
||||||
|
const result = yield* fs.findUp("marker", child, tmp)
|
||||||
|
expect(result).toEqual([path.join(tmp, "marker")])
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"returns empty array when not found",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const result = yield* fs.findUp("nonexistent", tmp, tmp)
|
||||||
|
expect(result).toEqual([])
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("up", () => {
|
||||||
|
it(
|
||||||
|
"finds multiple targets walking up",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "a.txt"), "a")
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "b.txt"), "b")
|
||||||
|
const child = path.join(tmp, "sub")
|
||||||
|
yield* filesys.makeDirectory(child)
|
||||||
|
yield* filesys.writeFileString(path.join(child, "a.txt"), "a-child")
|
||||||
|
|
||||||
|
const result = yield* fs.up({ targets: ["a.txt", "b.txt"], start: child, stop: tmp })
|
||||||
|
|
||||||
|
expect(result).toContain(path.join(child, "a.txt"))
|
||||||
|
expect(result).toContain(path.join(tmp, "a.txt"))
|
||||||
|
expect(result).toContain(path.join(tmp, "b.txt"))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("glob", () => {
|
||||||
|
it(
|
||||||
|
"finds files matching pattern",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "a.ts"), "a")
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "b.ts"), "b")
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "c.json"), "c")
|
||||||
|
|
||||||
|
const result = yield* fs.glob("*.ts", { cwd: tmp })
|
||||||
|
expect(result.sort()).toEqual(["a.ts", "b.ts"])
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"supports absolute paths",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "file.txt"), "hello")
|
||||||
|
|
||||||
|
const result = yield* fs.glob("*.txt", { cwd: tmp, absolute: true })
|
||||||
|
expect(result).toEqual([path.join(tmp, "file.txt")])
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("globMatch", () => {
|
||||||
|
it(
|
||||||
|
"matches patterns",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
expect(fs.globMatch("*.ts", "foo.ts")).toBe(true)
|
||||||
|
expect(fs.globMatch("*.ts", "foo.json")).toBe(false)
|
||||||
|
expect(fs.globMatch("src/**", "src/a/b.ts")).toBe(true)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("globUp", () => {
|
||||||
|
it(
|
||||||
|
"finds files walking up directories",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
yield* filesys.writeFileString(path.join(tmp, "root.md"), "root")
|
||||||
|
const child = path.join(tmp, "a", "b")
|
||||||
|
yield* filesys.makeDirectory(child, { recursive: true })
|
||||||
|
yield* filesys.writeFileString(path.join(child, "leaf.md"), "leaf")
|
||||||
|
|
||||||
|
const result = yield* fs.globUp("*.md", child, tmp)
|
||||||
|
expect(result).toContain(path.join(child, "leaf.md"))
|
||||||
|
expect(result).toContain(path.join(tmp, "root.md"))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("built-in passthrough", () => {
|
||||||
|
it(
|
||||||
|
"exists works",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "exists.txt")
|
||||||
|
yield* filesys.writeFileString(file, "yes")
|
||||||
|
|
||||||
|
expect(yield* filesys.exists(file)).toBe(true)
|
||||||
|
expect(yield* filesys.exists(file + ".nope")).toBe(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it(
|
||||||
|
"remove works",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const fs = yield* AppFileSystem.Service
|
||||||
|
const filesys = yield* FileSystem.FileSystem
|
||||||
|
const tmp = yield* filesys.makeTempDirectoryScoped()
|
||||||
|
const file = path.join(tmp, "delete-me.txt")
|
||||||
|
yield* filesys.writeFileString(file, "bye")
|
||||||
|
|
||||||
|
yield* filesys.remove(file)
|
||||||
|
|
||||||
|
expect(yield* filesys.exists(file)).toBe(false)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("pure helpers", () => {
|
||||||
|
test("mimeType returns correct types", () => {
|
||||||
|
expect(AppFileSystem.mimeType("file.json")).toBe("application/json")
|
||||||
|
expect(AppFileSystem.mimeType("image.png")).toBe("image/png")
|
||||||
|
expect(AppFileSystem.mimeType("unknown.qzx")).toBe("application/octet-stream")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("contains checks path containment", () => {
|
||||||
|
expect(AppFileSystem.contains("/a/b", "/a/b/c")).toBe(true)
|
||||||
|
expect(AppFileSystem.contains("/a/b", "/a/c")).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("overlaps detects overlapping paths", () => {
|
||||||
|
expect(AppFileSystem.overlaps("/a/b", "/a/b/c")).toBe(true)
|
||||||
|
expect(AppFileSystem.overlaps("/a/b/c", "/a/b")).toBe(true)
|
||||||
|
expect(AppFileSystem.overlaps("/a", "/b")).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
72
packages/shared/test/fixture/flock-worker.ts
Normal file
72
packages/shared/test/fixture/flock-worker.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import fs from "fs/promises"
|
||||||
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
|
|
||||||
|
type Msg = {
|
||||||
|
key: string
|
||||||
|
dir: string
|
||||||
|
staleMs?: number
|
||||||
|
timeoutMs?: number
|
||||||
|
baseDelayMs?: number
|
||||||
|
maxDelayMs?: number
|
||||||
|
holdMs?: number
|
||||||
|
ready?: string
|
||||||
|
active?: string
|
||||||
|
done?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms: number) {
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
setTimeout(resolve, ms)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function input() {
|
||||||
|
const raw = process.argv[2]
|
||||||
|
if (!raw) {
|
||||||
|
throw new Error("Missing flock worker input")
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.parse(raw) as Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
async function job(input: Msg) {
|
||||||
|
if (input.ready) {
|
||||||
|
await fs.writeFile(input.ready, String(process.pid))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.active) {
|
||||||
|
await fs.writeFile(input.active, String(process.pid), { flag: "wx" })
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (input.holdMs && input.holdMs > 0) {
|
||||||
|
await sleep(input.holdMs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.done) {
|
||||||
|
await fs.appendFile(input.done, "1\n")
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (input.active) {
|
||||||
|
await fs.rm(input.active, { force: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const msg = input()
|
||||||
|
|
||||||
|
await Flock.withLock(msg.key, () => job(msg), {
|
||||||
|
dir: msg.dir,
|
||||||
|
staleMs: msg.staleMs,
|
||||||
|
timeoutMs: msg.timeoutMs,
|
||||||
|
baseDelayMs: msg.baseDelayMs,
|
||||||
|
maxDelayMs: msg.maxDelayMs,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await main().catch((err) => {
|
||||||
|
const text = err instanceof Error ? (err.stack ?? err.message) : String(err)
|
||||||
|
process.stderr.write(text)
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
53
packages/shared/test/lib/effect.ts
Normal file
53
packages/shared/test/lib/effect.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
import { test, type TestOptions } from "bun:test"
|
||||||
|
import { Cause, Effect, Exit, Layer } from "effect"
|
||||||
|
import type * as Scope from "effect/Scope"
|
||||||
|
import * as TestClock from "effect/testing/TestClock"
|
||||||
|
import * as TestConsole from "effect/testing/TestConsole"
|
||||||
|
|
||||||
|
type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)
|
||||||
|
|
||||||
|
const body = <A, E, R>(value: Body<A, E, R>) => Effect.suspend(() => (typeof value === "function" ? value() : value))
|
||||||
|
|
||||||
|
const run = <A, E, R, E2>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2>) =>
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(layer), Effect.exit)
|
||||||
|
if (Exit.isFailure(exit)) {
|
||||||
|
for (const err of Cause.prettyErrors(exit.cause)) {
|
||||||
|
yield* Effect.logError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return yield* exit
|
||||||
|
}).pipe(Effect.runPromise)
|
||||||
|
|
||||||
|
const make = <R, E>(testLayer: Layer.Layer<R, E>, liveLayer: Layer.Layer<R, E>) => {
|
||||||
|
const effect = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test(name, () => run(value, testLayer), opts)
|
||||||
|
|
||||||
|
effect.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test.only(name, () => run(value, testLayer), opts)
|
||||||
|
|
||||||
|
effect.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test.skip(name, () => run(value, testLayer), opts)
|
||||||
|
|
||||||
|
const live = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test(name, () => run(value, liveLayer), opts)
|
||||||
|
|
||||||
|
live.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test.only(name, () => run(value, liveLayer), opts)
|
||||||
|
|
||||||
|
live.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
|
||||||
|
test.skip(name, () => run(value, liveLayer), opts)
|
||||||
|
|
||||||
|
return { effect, live }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test environment with TestClock and TestConsole
|
||||||
|
const testEnv = Layer.mergeAll(TestConsole.layer, TestClock.layer())
|
||||||
|
|
||||||
|
// Live environment - uses real clock, but keeps TestConsole for output capture
|
||||||
|
const liveEnv = TestConsole.layer
|
||||||
|
|
||||||
|
export const it = make(testEnv, liveEnv)
|
||||||
|
|
||||||
|
export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
|
||||||
|
make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))
|
||||||
18
packages/shared/test/npm.test.ts
Normal file
18
packages/shared/test/npm.test.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
import { Npm } from "@opencode-ai/shared/npm"
|
||||||
|
|
||||||
|
const win = process.platform === "win32"
|
||||||
|
|
||||||
|
describe("Npm.sanitize", () => {
|
||||||
|
test("keeps normal scoped package specs unchanged", () => {
|
||||||
|
expect(Npm.sanitize("@opencode/acme")).toBe("@opencode/acme")
|
||||||
|
expect(Npm.sanitize("@opencode/acme@1.0.0")).toBe("@opencode/acme@1.0.0")
|
||||||
|
expect(Npm.sanitize("prettier")).toBe("prettier")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("handles git https specs", () => {
|
||||||
|
const spec = "acme@git+https://github.com/opencode/acme.git"
|
||||||
|
const expected = win ? "acme@git+https_//github.com/opencode/acme.git" : spec
|
||||||
|
expect(Npm.sanitize(spec)).toBe(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
@ -1,14 +1,10 @@
|
||||||
import { describe, expect, test } from "bun:test"
|
import { describe, expect, test } from "bun:test"
|
||||||
import fs from "fs/promises"
|
import fs from "fs/promises"
|
||||||
|
import { spawn } from "child_process"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { Flock } from "../../src/util/flock"
|
import os from "os"
|
||||||
import { Hash } from "../../src/util/hash"
|
import { Flock } from "@opencode-ai/shared/util/flock"
|
||||||
import { Process } from "../../src/util/process"
|
import { Hash } from "@opencode-ai/shared/util/hash"
|
||||||
import { Filesystem } from "../../src/util/filesystem"
|
|
||||||
import { tmpdir } from "../fixture/fixture"
|
|
||||||
|
|
||||||
const root = path.join(import.meta.dir, "../..")
|
|
||||||
const worker = path.join(import.meta.dir, "../fixture/flock-worker.ts")
|
|
||||||
|
|
||||||
type Msg = {
|
type Msg = {
|
||||||
key: string
|
key: string
|
||||||
|
|
@ -23,6 +19,19 @@ type Msg = {
|
||||||
done?: string
|
done?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const root = path.join(import.meta.dir, "../..")
|
||||||
|
const worker = path.join(import.meta.dir, "../fixture/flock-worker.ts")
|
||||||
|
|
||||||
|
async function tmpdir() {
|
||||||
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "flock-test-"))
|
||||||
|
return {
|
||||||
|
path: dir,
|
||||||
|
async [Symbol.asyncDispose]() {
|
||||||
|
await fs.rm(dir, { recursive: true, force: true })
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function lock(dir: string, key: string) {
|
function lock(dir: string, key: string) {
|
||||||
return path.join(dir, Hash.fast(key) + ".lock")
|
return path.join(dir, Hash.fast(key) + ".lock")
|
||||||
}
|
}
|
||||||
|
|
@ -51,21 +60,55 @@ async function wait(file: string, timeout = 3_000) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function run(msg: Msg) {
|
function run(msg: Msg) {
|
||||||
return Process.run([process.execPath, worker, JSON.stringify(msg)], {
|
return new Promise<{ code: number; stdout: Buffer; stderr: Buffer }>((resolve) => {
|
||||||
cwd: root,
|
const proc = spawn(process.execPath, [worker, JSON.stringify(msg)], {
|
||||||
nothrow: true,
|
cwd: root,
|
||||||
|
})
|
||||||
|
|
||||||
|
const stdout: Buffer[] = []
|
||||||
|
const stderr: Buffer[] = []
|
||||||
|
|
||||||
|
proc.stdout?.on("data", (data) => stdout.push(Buffer.from(data)))
|
||||||
|
proc.stderr?.on("data", (data) => stderr.push(Buffer.from(data)))
|
||||||
|
|
||||||
|
proc.on("close", (code) => {
|
||||||
|
resolve({
|
||||||
|
code: code ?? 1,
|
||||||
|
stdout: Buffer.concat(stdout),
|
||||||
|
stderr: Buffer.concat(stderr),
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function spawn(msg: Msg) {
|
function spawnWorker(msg: Msg) {
|
||||||
return Process.spawn([process.execPath, worker, JSON.stringify(msg)], {
|
return spawn(process.execPath, [worker, JSON.stringify(msg)], {
|
||||||
cwd: root,
|
cwd: root,
|
||||||
stdin: "ignore",
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
stdout: "pipe",
|
|
||||||
stderr: "pipe",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function stopWorker(proc: ReturnType<typeof spawnWorker>) {
|
||||||
|
if (proc.exitCode !== null || proc.signalCode !== null) return Promise.resolve()
|
||||||
|
|
||||||
|
if (process.platform !== "win32" || !proc.pid) {
|
||||||
|
proc.kill()
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise<void>((resolve) => {
|
||||||
|
const killProc = spawn("taskkill", ["/pid", String(proc.pid), "/T", "/F"])
|
||||||
|
killProc.on("close", () => {
|
||||||
|
proc.kill()
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readJson<T>(p: string): Promise<T> {
|
||||||
|
return JSON.parse(await fs.readFile(p, "utf8"))
|
||||||
|
}
|
||||||
|
|
||||||
describe("util.flock", () => {
|
describe("util.flock", () => {
|
||||||
test("enforces mutual exclusion under process contention", async () => {
|
test("enforces mutual exclusion under process contention", async () => {
|
||||||
await using tmp = await tmpdir()
|
await using tmp = await tmpdir()
|
||||||
|
|
@ -104,7 +147,7 @@ describe("util.flock", () => {
|
||||||
const dir = path.join(tmp.path, "locks")
|
const dir = path.join(tmp.path, "locks")
|
||||||
const key = "flock:timeout"
|
const key = "flock:timeout"
|
||||||
const ready = path.join(tmp.path, "ready")
|
const ready = path.join(tmp.path, "ready")
|
||||||
const proc = spawn({
|
const proc = spawnWorker({
|
||||||
key,
|
key,
|
||||||
dir,
|
dir,
|
||||||
ready,
|
ready,
|
||||||
|
|
@ -131,8 +174,8 @@ describe("util.flock", () => {
|
||||||
expect(seen.length).toBeGreaterThan(0)
|
expect(seen.length).toBeGreaterThan(0)
|
||||||
expect(seen.every((x) => x === key)).toBe(true)
|
expect(seen.every((x) => x === key)).toBe(true)
|
||||||
} finally {
|
} finally {
|
||||||
await Process.stop(proc).catch(() => undefined)
|
await stopWorker(proc).catch(() => undefined)
|
||||||
await proc.exited.catch(() => undefined)
|
await new Promise((resolve) => proc.on("close", resolve))
|
||||||
}
|
}
|
||||||
}, 15_000)
|
}, 15_000)
|
||||||
|
|
||||||
|
|
@ -141,7 +184,7 @@ describe("util.flock", () => {
|
||||||
const dir = path.join(tmp.path, "locks")
|
const dir = path.join(tmp.path, "locks")
|
||||||
const key = "flock:crash"
|
const key = "flock:crash"
|
||||||
const ready = path.join(tmp.path, "ready")
|
const ready = path.join(tmp.path, "ready")
|
||||||
const proc = spawn({
|
const proc = spawnWorker({
|
||||||
key,
|
key,
|
||||||
dir,
|
dir,
|
||||||
ready,
|
ready,
|
||||||
|
|
@ -151,8 +194,8 @@ describe("util.flock", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
await wait(ready, 5_000)
|
await wait(ready, 5_000)
|
||||||
await Process.stop(proc)
|
await stopWorker(proc)
|
||||||
await proc.exited.catch(() => undefined)
|
await new Promise((resolve) => proc.on("close", resolve))
|
||||||
|
|
||||||
let hit = false
|
let hit = false
|
||||||
await Flock.withLock(
|
await Flock.withLock(
|
||||||
|
|
@ -276,7 +319,7 @@ describe("util.flock", () => {
|
||||||
await Flock.withLock(
|
await Flock.withLock(
|
||||||
key,
|
key,
|
||||||
async () => {
|
async () => {
|
||||||
const json = await Filesystem.readJson<{
|
const json = await readJson<{
|
||||||
token?: unknown
|
token?: unknown
|
||||||
pid?: unknown
|
pid?: unknown
|
||||||
hostname?: unknown
|
hostname?: unknown
|
||||||
|
|
@ -324,7 +367,7 @@ describe("util.flock", () => {
|
||||||
const err = await Flock.withLock(
|
const err = await Flock.withLock(
|
||||||
key,
|
key,
|
||||||
async () => {
|
async () => {
|
||||||
const json = await Filesystem.readJson<{ token?: string }>(meta)
|
const json = await readJson<{ token?: string }>(meta)
|
||||||
json.token = "tampered"
|
json.token = "tampered"
|
||||||
await fs.writeFile(meta, JSON.stringify(json, null, 2))
|
await fs.writeFile(meta, JSON.stringify(json, null, 2))
|
||||||
},
|
},
|
||||||
Loading…
Add table
Add a link
Reference in a new issue