Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
chore: convert to esm (#103)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: deep imports are no longer possible
  • Loading branch information
achingbrain authored Sep 8, 2021
1 parent cc096b0 commit 314d130
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 130 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run pretest
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- uses: codecov/codecov-action@v1
test-electron-main:
Expand All @@ -39,11 +40,13 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-main --bail
- run: npm run pretest
- run: npx xvfb-maybe aegir test -t electron-main --bail -f dist/cjs/node-test/*js
test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
- run: npm run pretest
- run: npx xvfb-maybe aegir test -t electron-renderer --bail -f dist/cjs/browser-test/*js
56 changes: 42 additions & 14 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,38 +1,66 @@
yarn.lock
package-lock.json
**/node_modules/
**/*.log
test/repo-tests*
dist/

# Logs
logs
*.log

coverage
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

build
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
# Optional npm cache directory
.npm

lib
dist
docs
# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# while testing npm5
package-lock.json
yarn.lock
types
docs
48 changes: 30 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@
"description": "Datastore implementation with file system backend",
"leadMaintainer": "Alex Potsides <[email protected]>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"types": "types/src/index.d.ts",
"type": "module",
"files": [
"src",
"dist"
"*",
"!**/*.tsbuildinfo"
],
"browser": {
"fs": false,
"path": false,
"util": false
},
"scripts": {
"prepare": "aegir build --no-bundle",
"test": "aegir test -t node",
"build": "aegir build",
"lint": "aegir lint",
"release": "aegir release -t node --build false",
"release-minor": "aegir release --type minor -t node --build false",
"release-major": "aegir release --type major -t node --build false",
"coverage": "nyc -s npm run test:node && nyc report --reporter=html",
"dep-check": "aegir dep-check"
"clean": "rimraf dist types",
"prepare": "aegir build --no-bundle && cp -R types dist",
"lint": "aegir ts -p check && aegir lint",
"build": "aegir build --no-bundle",
"release": "aegir release --target node",
"release-minor": "aegir release --type minor --target node",
"release-major": "aegir release --type major --target node",
"pretest": "aegir build --esm-tests",
"test": "aegir test",
"dep-check": "aegir dep-check -i rimraf"
},
"repository": {
"type": "git",
Expand All @@ -38,25 +45,30 @@
},
"homepage": "https://github.com/ipfs/js-datastore-fs#readme",
"dependencies": {
"datastore-core": "^5.0.0",
"datastore-core": "^6.0.5",
"fast-write-atomic": "^0.2.0",
"interface-datastore": "^5.1.1",
"it-glob": "0.0.13",
"interface-datastore": "^6.0.2",
"it-glob": "^1.0.1",
"it-map": "^1.0.5",
"it-parallel-batch": "^1.0.9",
"mkdirp": "^1.0.4"
},
"devDependencies": {
"aegir": "^34.0.2",
"@types/mkdirp": "^1.0.2",
"@types/rimraf": "^3.0.2",
"aegir": "^35.0.3",
"async-iterator-all": "^1.0.0",
"detect-node": "^2.0.4",
"interface-datastore-tests": "^1.0.0",
"interface-datastore-tests": "^2.0.3",
"ipfs-utils": "^8.1.3",
"memdown": "^6.0.0",
"rimraf": "^3.0.2"
},
"eslintConfig": {
"extends": "ipfs"
"extends": "ipfs",
"parserOptions": {
"sourceType": "module"
}
},
"contributors": [
"achingbrain <[email protected]>",
Expand Down
52 changes: 24 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
'use strict'
import fs from 'fs'
import glob from 'it-glob'
import mkdirp from 'mkdirp'
import path from 'path'
import { promisify } from 'util'
import {
Key
} from 'interface-datastore'
import {
BaseDatastore, Errors
} from 'datastore-core'
import map from 'it-map'
import parallel from 'it-parallel-batch'
// @ts-ignore no types
import fwa from 'fast-write-atomic'

const fs = require('fs')
const glob = require('it-glob')
// @ts-ignore
const mkdirp = require('mkdirp')
const promisify = require('util').promisify
// @ts-ignore
const writeAtomic = promisify(require('fast-write-atomic'))
const path = require('path')
const {
Adapter, Key, Errors
} = require('interface-datastore')
const map = require('it-map')
const parallel = require('it-parallel-batch')

const noop = () => {}
const fsAccess = promisify(fs.access || noop)
const fsReadFile = promisify(fs.readFile || noop)
const fsUnlink = promisify(fs.unlink || noop)
const writeAtomic = promisify(fwa)

/**
* @typedef {import('interface-datastore').Datastore} Datastore
Expand All @@ -40,13 +38,13 @@ const fsUnlink = promisify(fs.unlink || noop)
async function writeFile (path, contents) {
try {
await writeAtomic(path, contents)
} catch (err) {
} catch (/** @type {any} */ err) {
if (err.code === 'EPERM' && err.syscall === 'rename') {
// 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
// Make sure we can read & write to this file
await fsAccess(path, fs.constants.F_OK | fs.constants.W_OK)
await fs.promises.access(path, fs.constants.F_OK | fs.constants.W_OK)

// The file was created by another context - this means there were
// attempts to write the same block by two different function calls
Expand All @@ -65,7 +63,7 @@ async function writeFile (path, contents) {
*
* @implements {Datastore}
*/
class FsDatastore extends Adapter {
export class DatastoreFs extends BaseDatastore {
/**
* @param {string} location
* @param {{ createIfMissing?: boolean, errorIfExists?: boolean, extension?: string, putManyConcurrency?: number } | undefined} [opts]
Expand Down Expand Up @@ -217,7 +215,7 @@ class FsDatastore extends Adapter {

let data
try {
data = await fsReadFile(file)
data = await fs.promises.readFile(file)
} catch (err) {
throw Errors.notFoundError(err)
}
Expand All @@ -234,7 +232,7 @@ class FsDatastore extends Adapter {
const parts = this._encode(key)
let data
try {
data = await fsReadFile(parts.file)
data = await fs.promises.readFile(parts.file)
} catch (err) {
throw Errors.notFoundError(err)
}
Expand Down Expand Up @@ -283,7 +281,7 @@ class FsDatastore extends Adapter {
const parts = this._encode(key)

try {
await fsAccess(parts.file)
await fs.promises.access(parts.file)
} catch (err) {
return false
}
Expand All @@ -299,7 +297,7 @@ class FsDatastore extends Adapter {
async delete (key) {
const parts = this._encode(key)
try {
await fsUnlink(parts.file)
await fs.promises.unlink(parts.file)
} catch (err) {
if (err.code === 'ENOENT') {
return
Expand Down Expand Up @@ -327,7 +325,7 @@ class FsDatastore extends Adapter {

for await (const file of files) {
try {
const buf = await fsReadFile(file)
const buf = await fs.promises.readFile(file)

/** @type {Pair} */
const pair = {
Expand Down Expand Up @@ -365,5 +363,3 @@ class FsDatastore extends Adapter {
yield * map(files, f => this._decode(f))
}
}

module.exports = FsDatastore
Loading

0 comments on commit 314d130

Please sign in to comment.