Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(cli): change local YAML files only through a locked update (#44375)

Xiyuan Chenb2455705bf88
diff --git a/cli/src/auth/pending-login.ts b/cli/src/auth/pending-login.ts
index cfcc5842d56..9b5864cc139 100644
--- a/cli/src/auth/pending-login.ts
+++ b/cli/src/auth/pending-login.ts
@@ -3,7 +3,7 @@ import { join } from 'node:path'
import { z } from 'zod'
import { env } from '@/plugins/env'
import { io } from '@/plugins/io'
-import { localFile } from '@/store/local-file'
+import { localFile, REMOVE } from '@/store/local-file'
export const PENDING_FILE_NAME = 'login-pending.yml'
@@ -33,8 +33,9 @@ export async function pendingLoginStore(ctx: CommandContext): Promise<PendingLog
)
return {
read: file.read,
- save: file.write,
- clear: file.remove,
- clearIf: (deviceCode) => file.removeIf((pending) => pending.device_code === deviceCode),
+ save: (pending) => file.update(() => pending),
+ clear: () => file.update(() => REMOVE),
+ clearIf: (deviceCode) =>
+ file.update((pending) => (pending?.device_code === deviceCode ? REMOVE : undefined)),
}
}
diff --git a/cli/src/plugins/config/index.test.ts b/cli/src/plugins/config/index.test.ts
index 31f84cfdc22..7f7117d803f 100644
--- a/cli/src/plugins/config/index.test.ts
+++ b/cli/src/plugins/config/index.test.ts
@@ -82,3 +82,30 @@ it.each([
await c.set('http.timeout', '5000')
expect(readFileSync(file, 'utf8')).toBe('http:\n timeout: 5000\n')
})
+
+it('reads the latest file, not the one it saw first', async () => {
+ const c = await load()
+ expect(await c.get('http.timeout')).toBe(30_000)
+ writeFileSync(file, 'http:\n timeout: 7000\n')
+ expect(await c.get('http.timeout')).toBe(7000)
+})
+
+it('lets separate processes set and unset in parallel', async () => {
+ const [a, b] = await Promise.all([load(), load()])
+ await Promise.all([
+ a.set('http.timeout', '5000'),
+ b.unset('http.timeout'),
+ b.set('http.timeout', '6000'),
+ ])
+ expect([5000, 6000, 30_000]).toContain(await a.get('http.timeout'))
+ expect(readFileSync(file, 'utf8')).toMatch(/^http:\n {2}timeout: \d+\n$/)
+})
+
+it('reports a bad value once across reads and a set', async () => {
+ writeFileSync(file, 'http:\n timeout: fast\n')
+ const c = await load()
+ await c.get('http.timeout')
+ await c.get()
+ await c.set('http.timeout', '5000')
+ expect(streams.errBuf()).toBe(`${file}: ignoring the invalid value of http.timeout\n`)
+})
diff --git a/cli/src/plugins/config/index.ts b/cli/src/plugins/config/index.ts
index 37ab1b22afa..0e10ce25084 100644
--- a/cli/src/plugins/config/index.ts
+++ b/cli/src/plugins/config/index.ts
@@ -100,19 +100,16 @@ export const config = definePlugin({
const path = join((await ctx.get(env)).configDir, CONFIG_FILE_NAME)
const file = localFile(path, CONFIG_SCHEMA, (await ctx.get(io)).notice)
- let cached: ConfigDoc | undefined
-
async function doc(): Promise<ConfigDoc> {
- if (cached === undefined) cached = (await file.read()) ?? CONFIG_SCHEMA.parse({})
- return cached
+ return (await file.read()) ?? CONFIG_SCHEMA.parse({})
}
async function persist(mutate: (target: Record<string, unknown>) => void): Promise<void> {
- const current = structuredClone(await doc()) as Record<string, unknown>
- mutate(current)
- const validated = validateDoc(current, path)
- await file.write(validated)
- cached = validated
+ await file.update((current) => {
+ const target: Record<string, unknown> = current ?? CONFIG_SCHEMA.parse({})
+ mutate(target)
+ return validateDoc(target, path)
+ })
}
return {
diff --git a/cli/src/plugins/session/index.ts b/cli/src/plugins/session/index.ts
index 0316c35ee03..757212775d3 100644
--- a/cli/src/plugins/session/index.ts
+++ b/cli/src/plugins/session/index.ts
@@ -6,7 +6,7 @@ import { ErrorCode } from '@/errors/codes'
import { definePlugin } from '@/kernel/plugin'
import { ENV, env } from '@/plugins/env'
import { io } from '@/plugins/io'
-import { localFile } from '@/store/local-file'
+import { localFile, REMOVE } from '@/store/local-file'
import { STORAGE_MODES } from '@/store/store'
export const LOGIN_FILE_NAME = 'login.yml'
@@ -90,13 +90,9 @@ export const session = definePlugin({
}
: null
- // Read once: a broken file warns on its first read only.
- let saved: Login | null | undefined
async function readLogin(): Promise<Login | null> {
- if (saved !== undefined) return saved
const doc = await file.read()
- saved = doc === null ? null : docToLogin(doc)
- return saved
+ return doc === null ? null : docToLogin(doc)
}
const current = async (): Promise<Login | null> => envLogin ?? readLogin()
@@ -116,13 +112,11 @@ export const session = definePlugin({
message: 'cannot save a login while DIFY_SERVER/DIFY_TOKEN are set',
})
}
- await file.write(loginToDoc(login))
- saved = login
+ await file.update(() => loginToDoc(login))
},
clear: async () => {
if (fromEnv) return
- await file.remove()
- saved = null
+ await file.update(() => REMOVE)
},
workspaceId: async () => {
if (envWorkspaceId !== undefined) return envWorkspaceId
diff --git a/cli/src/store/local-file.test.ts b/cli/src/store/local-file.test.ts
index efd3b0a85d8..2921ab2a1dc 100644
--- a/cli/src/store/local-file.test.ts
+++ b/cli/src/store/local-file.test.ts
@@ -1,9 +1,9 @@
-import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, expect, it } from 'vite-plus/test'
import { z } from 'zod'
-import { localFile } from './local-file'
+import { localFile, REMOVE } from './local-file'
const SCHEMA = z.object({ name: z.string(), size: z.number().default(1) })
@@ -17,16 +17,46 @@ afterEach(() => rmSync(dir, { recursive: true, force: true }))
it('writes the whole file in the schema shape, dropping keys the schema does not know', async () => {
const file = localFile(path, SCHEMA, () => {})
- await file.write({ name: 'a', size: 2, legacy: true } as z.output<typeof SCHEMA>)
+ await file.update(() => ({ name: 'a', size: 2, legacy: true }) as z.output<typeof SCHEMA>)
expect(readFileSync(path, 'utf8')).toBe('name: a\nsize: 2\n')
})
it('refuses to write a document the schema rejects', async () => {
const file = localFile(path, SCHEMA, () => {})
- await expect(file.write({ name: 1 } as unknown as z.output<typeof SCHEMA>)).rejects.toThrow()
+ await expect(
+ file.update(() => ({ name: 1 }) as unknown as z.output<typeof SCHEMA>),
+ ).rejects.toThrow()
expect(await file.read()).toBeNull()
})
+it('changes the latest document, not one read earlier', async () => {
+ const file = localFile(path, SCHEMA, () => {})
+ writeFileSync(path, 'name: a\n')
+ expect(await file.read()).toEqual({ name: 'a', size: 1 })
+ writeFileSync(path, 'name: b\nsize: 5\n')
+ await file.update((doc) => (doc === null ? undefined : { ...doc, size: doc.size + 1 }))
+ expect(readFileSync(path, 'utf8')).toBe('name: b\nsize: 6\n')
+})
+
+it('removes the file only when the change says REMOVE', async () => {
+ const file = localFile(path, SCHEMA, () => {})
+ writeFileSync(path, 'name: a\n')
+ await file.update((doc) => (doc?.name === 'b' ? REMOVE : undefined))
+ expect(existsSync(path)).toBe(true)
+ await file.update((doc) => (doc?.name === 'a' ? REMOVE : undefined))
+ expect(existsSync(path)).toBe(false)
+})
+
+it('reports each problem with a file once, however often it is read', async () => {
+ writeFileSync(path, 'name: a\nsize: big\n')
+ const notices: string[] = []
+ const file = localFile(path, SCHEMA, (text) => notices.push(text))
+ await file.read()
+ await file.read()
+ await file.update((doc) => doc ?? undefined)
+ expect(notices).toEqual([`${path}: ignoring the invalid value of size`])
+})
+
const NESTED = z.object({
host: z.string(),
account: z.object({ id: z.string(), name: z.string() }).optional(),
diff --git a/cli/src/store/local-file.ts b/cli/src/store/local-file.ts
index c39152f1d07..b7fa6e884cb 100644
--- a/cli/src/store/local-file.ts
+++ b/cli/src/store/local-file.ts
@@ -1,7 +1,10 @@
import type { z } from 'zod'
+import type { Change } from './store'
import { isRecord } from '@/util/is-record'
import { BadYamlFormatError } from './errors'
-import { loadYaml, YamlStore } from './store'
+import { loadYaml, REMOVE, YamlStore } from './store'
+
+export { REMOVE } from './store'
export type Notice = (text: string) => void
@@ -45,16 +48,6 @@ function readTolerant<S extends z.ZodType>(
notice(`${path} is not valid YAML; ignoring it`)
return null
}
- if (raw === null) return null
- return parseTolerant(raw, path, schema, notice)
-}
-
-function parseTolerant<S extends z.ZodType>(
- raw: unknown,
- path: string,
- schema: S,
- notice: Notice,
-): z.output<S> | null {
if (raw === null) return null
if (!isRecord(raw)) {
notice(`${path} is not a YAML mapping; ignoring it`)
@@ -79,35 +72,35 @@ function parseTolerant<S extends z.ZodType>(
export type LocalFile<T> = Readonly<{
path: string
read: () => Promise<T | null>
- write: (doc: T) => Promise<void>
- update: (transform: (doc: T | null) => T | undefined | Promise<T | undefined>) => Promise<void>
- remove: () => Promise<void>
- /** Remove the file only while it still holds a document `matches` accepts. */
- removeIf: (matches: (doc: T) => boolean) => Promise<void>
+ /** The only way to change the file: `change` sees the latest document, under the file's lock. */
+ update: (change: (doc: T | null) => Change<T>) => Promise<void>
}>
-/** Tolerant read (bad value → default + notice; unusable file → null); write replaces the whole file. */
+// A file is read again on every use, so the same problem would be reported each time.
+function onceEach(notice: Notice): Notice {
+ const said = new Set<string>()
+ return (text) => {
+ if (said.has(text)) return
+ said.add(text)
+ notice(text)
+ }
+}
+
+/** Tolerant read (bad value → default + notice; unusable file → null); update writes the whole file. */
export function localFile<S extends z.ZodType>(
path: string,
schema: S,
notice: Notice,
): LocalFile<z.output<S>> {
const store = new YamlStore(path)
+ const report = onceEach(notice)
return {
path,
- read: async () => readTolerant(await store.getRaw(), schema, path, notice),
- write: async (doc) => store.setTyped(schema.parse(doc)),
- update: async (transform) => {
- await store.update(async (content) => {
- const next = await transform(readTolerant(content, schema, path, notice))
- return next === undefined ? undefined : schema.parse(next)
- })
- },
- remove: () => store.rm(),
- removeIf: (matches) =>
- store.rmIf((doc) => {
- const parsed = schema.safeParse(doc)
- return parsed.success && matches(parsed.data)
+ read: async () => readTolerant(await store.read(), schema, path, report),
+ update: (change) =>
+ store.update((content) => {
+ const next = change(readTolerant(content, schema, path, report))
+ return next === undefined || next === REMOVE ? next : schema.parse(next)
}),
}
}
diff --git a/cli/src/store/store.test.ts b/cli/src/store/store.test.ts
index 2660d7f6cff..2d4d9185140 100644
--- a/cli/src/store/store.test.ts
+++ b/cli/src/store/store.test.ts
@@ -1,10 +1,10 @@
import { existsSync, readFileSync, statSync } from 'node:fs'
-import { mkdtemp, rm, writeFile } from 'node:fs/promises'
+import { mkdtemp, rm, unlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vite-plus/test'
import { BadYamlFormatError } from './errors'
-import { YamlStore } from './store'
+import { loadYaml, REMOVE, YamlStore } from './store'
let dir: string
let path: string
@@ -20,12 +20,12 @@ afterEach(async () => {
async function parsed<T = Record<string, unknown>>(content: string): Promise<T | null> {
await writeFile(path, content)
- return new YamlStore(path).getTyped<T>()
+ return loadYaml(await new YamlStore(path).read(), path) as T | null
}
-describe('YamlStore.getTyped', () => {
+describe('loadYaml', () => {
it('returns null when the file does not exist', async () => {
- expect(await new YamlStore(path).getTyped()).toBeNull()
+ expect(loadYaml(await new YamlStore(path).read(), path)).toBeNull()
})
it('reads an empty or comment-only file as an empty mapping', async () => {
@@ -82,57 +82,54 @@ describe('YamlStore.getTyped', () => {
})
})
-describe('YamlStore.withLock concurrency', () => {
+describe('YamlStore lock', () => {
const HOLD_MS = 200
- const releaseLater = (store: YamlStore) =>
- new Promise<void>((resolve) => setTimeout(() => void store.unlock().then(resolve), HOLD_MS))
+ // Holds the lock the way another difyctl process would, then lets it go.
+ async function holdLock(beforeRelease: () => Promise<void> = async () => {}): Promise<void> {
+ await writeFile(`${path}.lock`, '')
+ setTimeout(() => void beforeRelease().then(() => unlink(`${path}.lock`)), HOLD_MS)
+ }
- it('a second read waits while the first holds the lock, then reads', async () => {
+ it('a read waits while another process holds the lock', async () => {
await writeFile(path, 'key: value\n')
- const s1 = new YamlStore(path)
- const s2 = new YamlStore(path)
-
- await s1.lock()
- const [value] = await Promise.all([s2.getTyped(), releaseLater(s1)])
-
- expect(value).toEqual({ key: 'value' })
+ await holdLock()
+ const started = Date.now()
+ expect(await new YamlStore(path).read()).toBe('key: value\n')
+ expect(Date.now() - started).toBeGreaterThanOrEqual(HOLD_MS / 2)
})
- it('a second write waits while the first holds the lock, then writes', async () => {
+ it('an update waits while another process holds the lock, then sees its write', async () => {
await writeFile(path, 'key: original\n')
- const s1 = new YamlStore(path)
- const s2 = new YamlStore(path)
-
- await s1.lock()
- await Promise.all([s2.setTyped({ key: 'written' }), releaseLater(s1)])
-
- expect(await s2.getTyped()).toEqual({ key: 'written' })
- })
-})
-
-describe('YamlStore.rmIf', () => {
- it('removes the file when the predicate accepts what it holds', async () => {
- await writeFile(path, 'device_code: dc-1\n')
- await new YamlStore(path).rmIf((doc) => doc?.device_code === 'dc-1')
- expect(existsSync(path)).toBe(false)
- })
-
- it('keeps the file when it holds something else', async () => {
- await writeFile(path, 'device_code: dc-2\n')
- await new YamlStore(path).rmIf((doc) => doc?.device_code === 'dc-1')
- expect(readFileSync(path, 'utf8')).toBe('device_code: dc-2\n')
- })
-
- it('is a no-op when the file does not exist', async () => {
- await new YamlStore(path).rmIf(() => true)
- expect(existsSync(path)).toBe(false)
+ await holdLock(() => writeFile(path, 'key: theirs\n'))
+ let seen: string | undefined
+ await new YamlStore(path).update((content) => {
+ seen = content
+ return { key: 'mine' }
+ })
+ expect(seen).toBe('key: theirs\n')
+ expect(readFileSync(path, 'utf8')).toBe('key: mine\n')
+ })
+
+ it('parallel updates from separate stores all land', async () => {
+ const WRITERS = 8
+ await Promise.all(
+ Array.from({ length: WRITERS }, (_, n) =>
+ new YamlStore(path).update((content) => ({
+ ...loadYaml(content, path),
+ [`k${n}`]: n,
+ })),
+ ),
+ )
+ expect(Object.keys(loadYaml(await new YamlStore(path).read(), path) ?? {})).toHaveLength(
+ WRITERS,
+ )
})
})
-describe('YamlStore persistence', () => {
+describe('YamlStore.update', () => {
it('round-trips a nested document through disk', async () => {
- await new YamlStore(path).setTyped({ a: { b: { c: 'deep' } }, workspace: 'ws-123' })
- expect(await new YamlStore(path).getTyped()).toEqual({
+ await new YamlStore(path).update(() => ({ a: { b: { c: 'deep' } }, workspace: 'ws-123' }))
+ expect(loadYaml(await new YamlStore(path).read(), path)).toEqual({
a: { b: { c: 'deep' } },
workspace: 'ws-123',
})
@@ -141,36 +138,37 @@ describe('YamlStore persistence', () => {
it.skipIf(process.platform === 'win32')(
'writes the file with owner-only permissions',
async () => {
- await new YamlStore(path).setTyped({ token: 'abc-123' })
+ await new YamlStore(path).update(() => ({ token: 'abc-123' }))
expect(statSync(path).mode & 0o777).toBe(0o600)
},
)
- it('forgets its cached content once the file is deleted and reloaded', async () => {
+ it('leaves the file alone when the change returns undefined', async () => {
await writeFile(path, 'key: value\n')
-
- const store = new YamlStore(path)
- expect(await store.getTyped<{ key: string }>()).toEqual({ key: 'value' })
-
- await rm(path)
- expect(await store.getTyped<{ key: string }>()).toBeNull()
- })
-
- it('flush writes file when dirty (content changed from undefined)', async () => {
- const store = new YamlStore(path)
- store.setRawContent('key: value\n')
- await store.flush()
- expect(existsSync(path)).toBe(true)
+ await new YamlStore(path).update(() => undefined)
expect(readFileSync(path, 'utf8')).toBe('key: value\n')
})
- it('flush is a no-op when loaded content is set back unchanged', async () => {
+ it('skips the write when the document is unchanged', async () => {
await writeFile(path, 'key: value\n')
- const store = new YamlStore(path)
- await store.load()
const mtime = statSync(path).mtimeMs
- store.setRawContent('key: value\n')
- await store.flush()
+ await new YamlStore(path).update(() => ({ key: 'value' }))
expect(statSync(path).mtimeMs).toBe(mtime)
})
+
+ it('deletes the file on REMOVE, and is a no-op when there is no file', async () => {
+ await writeFile(path, 'key: value\n')
+ await new YamlStore(path).update(() => REMOVE)
+ expect(existsSync(path)).toBe(false)
+ await new YamlStore(path).update(() => REMOVE)
+ expect(existsSync(path)).toBe(false)
+ })
+
+ it('reads the file again on each call', async () => {
+ const store = new YamlStore(path)
+ await writeFile(path, 'key: value\n')
+ expect(await store.read()).toBe('key: value\n')
+ await rm(path)
+ expect(await store.read()).toBeUndefined()
+ })
})
diff --git a/cli/src/store/store.ts b/cli/src/store/store.ts
index 76da5315a39..6a2a928594e 100644
--- a/cli/src/store/store.ts
+++ b/cli/src/store/store.ts
@@ -46,157 +46,91 @@ function unlockAsync(path: string): Promise<void> {
export const STORAGE_MODES = ['keychain', 'file'] as const
export type StorageMode = (typeof STORAGE_MODES)[number]
+/** What an update returns to delete the file. */
+export const REMOVE = Symbol('remove')
+
+/** The next document, REMOVE to delete the file, or undefined to leave the file as it is. */
+export type Change<T> = T | typeof REMOVE | undefined
+
export class YamlStore {
- filePath: string
- private rawContent: string | undefined
+ private readonly filePath: string
private readonly platform: Platform
- private dirty: boolean = false
constructor(filePath: string) {
this.filePath = filePath
this.platform = resolvePlatform()
}
- private async ensureDir(): Promise<void> {
- await fsp.mkdir(dirname(this.filePath), { recursive: true, mode: DIR_PERM })
- }
-
- async unlock(): Promise<void> {
- await unlockAsync(`${this.filePath}.lock`)
+ async read(): Promise<string | undefined> {
+ return this.withLock(() => this.load())
}
/**
- * atomically write raw_content (if any)
+ * Read, change and write the file under one lock, so a change made by another
+ * process in between is never lost. `change` is sync: an async one could hold the
+ * lock past LOCK_STALE_MS, and another process would then take it.
*/
- async flush(): Promise<void> {
- // we don't handle A-B-A scenario,
- // which is not likely to happen in cli
- if (!this.dirty) {
- return
- }
+ async update<T>(change: (content: string | undefined) => Change<T>): Promise<void> {
+ await this.withLock(async () => {
+ const content = await this.load()
+ const next = change(content)
+ if (next === undefined) return
+ if (next === REMOVE) return this.rm()
+ const dumped = dump(next, { lineWidth: -1, noRefs: true })
+ if (dumped !== content) await this.replace(dumped)
+ })
+ }
- if (this.rawContent !== undefined) {
- await this.ensureDir()
- const tmp = `${this.filePath}.tmp.${pid()}.${Date.now()}`
- try {
- await fsp.writeFile(tmp, this.rawContent, { mode: FILE_PERM })
- this.platform.atomicReplace(tmp, this.filePath)
- } catch (err) {
- try {
- await fsp.unlink(tmp)
- } catch {
- /* tmp may not exist */
- }
- throw err
- }
+ private async withLock<R>(body: () => Promise<R>): Promise<R> {
+ await this.lock()
+ try {
+ return await body()
+ } finally {
+ await unlockAsync(`${this.filePath}.lock`)
}
-
- this.dirty = false
}
- async lock(): Promise<void> {
- await this.ensureDir()
+ private async lock(): Promise<void> {
+ await fsp.mkdir(dirname(this.filePath), { recursive: true, mode: DIR_PERM })
try {
await lockAsync(`${this.filePath}.lock`, {
stale: LOCK_STALE_MS,
wait: LOCK_WAIT_MS,
})
} catch (err) {
- const code = (err as NodeJS.ErrnoException).code
- if (code === 'EEXIST') {
+ if ((err as NodeJS.ErrnoException).code === 'EEXIST')
throw new ConcurrentAccessError(this.filePath)
- }
throw err
}
}
- async load(): Promise<void> {
+ private async load(): Promise<string | undefined> {
try {
- this.rawContent = await fsp.readFile(this.filePath, 'utf8')
- this.dirty = false
+ return await fsp.readFile(this.filePath, 'utf8')
} catch (err) {
- const code = (err as NodeJS.ErrnoException).code
- if (code !== 'ENOENT') {
- throw err
- }
- this.rawContent = undefined
+ if ((err as NodeJS.ErrnoException).code === 'ENOENT') return undefined
+ throw err
}
}
- public setRawContent(content: string): void {
- this.dirty = content !== this.getRawContent()
- this.rawContent = content
- }
-
- public getRawContent(): string | undefined {
- return this.rawContent
- }
-
- /** Read, transform, and replace a document under one lock for read-modify-write operations. */
- async update<T>(
- update: (rawContent: string | undefined) => T | undefined | Promise<T | undefined>,
- ): Promise<void> {
- await this.withLock(async () => {
- await this.load()
- const next = await update(this.rawContent)
- if (next === undefined) return
- this.setRawContent(dump(next, { lineWidth: -1, noRefs: true }))
- await this.flush()
- })
- }
-
- async getRaw(): Promise<string | undefined> {
- return this.withLock(async () => {
- await this.load()
- return this.rawContent
- })
- }
-
- private async withLock<R>(body: () => R | Promise<R>): Promise<R> {
- await this.lock()
+ private async replace(content: string): Promise<void> {
+ const tmp = `${this.filePath}.tmp.${pid()}.${Date.now()}`
try {
- return await body()
- } finally {
- await this.unlock()
+ await fsp.writeFile(tmp, content, { mode: FILE_PERM })
+ this.platform.atomicReplace(tmp, this.filePath)
+ } catch (err) {
+ await fsp.unlink(tmp).catch(() => undefined)
+ throw err
}
}
- /**
- * Remove the underlying file of the store. No-op if file doesn't exist.
- */
- async rm(): Promise<void> {
+ private async rm(): Promise<void> {
try {
await fsp.unlink(this.filePath)
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err
}
}
-
- /**
- * Remove the file only when `matches` accepts what it holds now. The read and the
- * removal share one lock, so a file another process rewrote in between survives.
- */
- async rmIf(matches: (doc: Record<string, unknown> | null) => boolean): Promise<void> {
- await this.withLock(async () => {
- await this.load()
- if (matches(loadYaml(this.getRawContent(), this.filePath))) await this.rm()
- })
- }
-
- async getTyped<T>(): Promise<T | null> {
- return this.withLock(async () => {
- await this.load()
- return loadYaml(this.getRawContent(), this.filePath) as T
- })
- }
-
- async setTyped<T>(data: T): Promise<void> {
- await this.withLock(async () => {
- await this.load()
- this.setRawContent(dump(data, { lineWidth: -1, noRefs: true }))
- await this.flush()
- })
- }
}
export function loadYaml(
diff --git a/cli/src/store/token-store.test.ts b/cli/src/store/token-store.test.ts
index ea3dbb9bee4..89c4374c44d 100644
--- a/cli/src/store/token-store.test.ts
+++ b/cli/src/store/token-store.test.ts
@@ -48,58 +48,11 @@ describe('FileTokenStore', () => {
expect(await s.read('https://self.example.com', '[email protected]')).toBe('C')
})
- it('preserves concurrent writes from separate stores', async () => {
- let updateCount = 0
- let releaseFirstUpdate!: () => void
- let signalFirstUpdate!: () => void
- let signalSecondRequest!: () => void
- const firstUpdateStarted = new Promise<void>((resolve) => {
- signalFirstUpdate = resolve
- })
- const secondUpdateRequested = new Promise<void>((resolve) => {
- signalSecondRequest = resolve
- })
- const firstUpdateGate = new Promise<void>((resolve) => {
- releaseFirstUpdate = resolve
- })
- let secondTransformEntered = false
-
- function openWithSynchronizedUpdate(): FileTokenStore {
- const source = localFile(file, TOKENS_SCHEMA, ioService(streams).notice)
- return new FileTokenStore({
- ...source,
- update: async (transform) => {
- updateCount += 1
- if (updateCount === 2) signalSecondRequest()
- await source.update(async (doc) => {
- if (updateCount === 1) {
- signalFirstUpdate()
- await firstUpdateGate
- } else {
- secondTransformEntered = true
- }
- return transform(doc)
- })
- },
- })
- }
-
- const first = openWithSynchronizedUpdate()
- const second = openWithSynchronizedUpdate()
- const firstWrite = first.write('https://one.example.com', '[email protected]', 'A')
- await firstUpdateStarted
- const secondWrite = second.write('https://two.example.com', '[email protected]', 'B')
- await secondUpdateRequested
- await new Promise<void>((resolve) => setImmediate(resolve))
- const secondWaitedForLock = !secondTransformEntered
- releaseFirstUpdate()
- await Promise.all([firstWrite, secondWrite])
-
- expect(secondWaitedForLock).toBe(true)
-
+ it('preserves parallel writes from separate stores', async () => {
+ const hosts = Array.from({ length: 8 }, (_, n) => `https://h${n}.example.com`)
+ await Promise.all(hosts.map((host) => open().write(host, '[email protected]', host)))
const result = open()
- expect(await result.read('https://one.example.com', '[email protected]')).toBe('A')
- expect(await result.read('https://two.example.com', '[email protected]')).toBe('B')
+ for (const host of hosts) expect(await result.read(host, '[email protected]')).toBe(host)
})
it('persists the nested shape on disk', async () => {