Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace fast-atomic-write with steno #285

4 changes: 2 additions & 2 deletions packages/blockstore-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@
},
"dependencies": {
"blockstore-core": "^4.0.0",
"fast-write-atomic": "^0.2.1",
"interface-blockstore": "^5.0.0",
"interface-store": "^5.0.0",
"it-glob": "^2.0.6",
"it-map": "^3.0.5",
"it-parallel-batch": "^3.0.4",
"multiformats": "^13.0.1"
"multiformats": "^13.0.1",
"steno": "^4.0.2"
},
"devDependencies": {
"aegir": "^42.2.3",
Expand Down
14 changes: 5 additions & 9 deletions packages/blockstore-fs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,29 @@

import fs from 'node:fs/promises'
import path from 'node:path'
import { promisify } from 'node:util'
import {
Errors
} from 'blockstore-core'
// @ts-expect-error no types
import fwa from 'fast-write-atomic'
import glob from 'it-glob'
import map from 'it-map'
import parallelBatch from 'it-parallel-batch'
import { Writer } from 'steno'
import { NextToLast, type ShardingStrategy } from './sharding.js'
import type { Blockstore, Pair } from 'interface-blockstore'
import type { AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'

const writeAtomic = promisify(fwa)

/**
* Write a file atomically
*/
async function writeFile (file: string, contents: Uint8Array): Promise<void> {
try {
await writeAtomic(file, contents)
const writer = new Writer(file)
await writer.write(contents)
} catch (err: any) {
if (err.syscall === 'rename' && ['ENOENT', 'EPERM'].includes(err.code)) {
// fast-write-atomic writes a file to a temp location before renaming it.
// On Windows, if the final file already exists this error is thrown.
// No such error is thrown on Linux/Mac
// steno writes a file to a temp location before renaming it.
// If the final file already exists this error is thrown.
// Make sure we can read & write to this file
await fs.access(file, fs.constants.F_OK | fs.constants.W_OK)

Expand Down
4 changes: 2 additions & 2 deletions packages/datastore-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
},
"dependencies": {
"datastore-core": "^9.0.0",
"fast-write-atomic": "^0.2.1",
"interface-datastore": "^8.0.0",
"interface-store": "^5.0.0",
"it-glob": "^2.0.6",
"it-map": "^3.0.5",
"it-parallel-batch": "^3.0.4"
"it-parallel-batch": "^3.0.4",
"steno": "^4.0.2"
},
"devDependencies": {
"@types/mkdirp": "^2.0.0",
Expand Down
14 changes: 5 additions & 9 deletions packages/datastore-fs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,29 @@

import fs from 'node:fs/promises'
import path from 'node:path'
import { promisify } from 'util'
import {
BaseDatastore, Errors
} from 'datastore-core'
// @ts-expect-error no types
import fwa from 'fast-write-atomic'
import {
Key, type KeyQuery, type Pair, type Query
} from 'interface-datastore'
import glob from 'it-glob'
import map from 'it-map'
import parallel from 'it-parallel-batch'
import { Writer } from 'steno'
import type { AwaitIterable } from 'interface-store'

const writeAtomic = promisify(fwa)

/**
* Write a file atomically
*/
async function writeFile (path: string, contents: Uint8Array): Promise<void> {
try {
await writeAtomic(path, contents)
const writer = new Writer(path)
await writer.write(contents)
} catch (err: any) {
if (err.syscall === 'rename' && ['ENOENT', 'EPERM'].includes(err.code)) {
// fast-write-atomic writes a file to a temp location before renaming it.
// On Windows, if the final file already exists this error is thrown.
// No such error is thrown on Linux/Mac
// steno writes a file to a temp location before renaming it.
// If the final file already exists this error is thrown.
// Make sure we can read & write to this file
await fs.access(path, fs.constants.F_OK | fs.constants.W_OK)

Expand Down
Loading