Skip to content

Commit

Permalink
💥 Convert package to ESM
Browse files Browse the repository at this point in the history
Migration Guide:

This relases changes the package from a Common JS module to an EcmaScript module, and drops support for older versions of Node.

- The minimum version of Node.js supported is now: `12.20.0`, `14.13.1`, and `16.0.0`
- The package must now be imported using the native `import` syntax instead of with `require`
- The exported functions have been renamed to have better stand-alone names:
  - `get` to `getAttribute`
  - `set` to `setAttribute`
  - `remove` to `removeAttribute`
  - `list` to `listAttributes`
  - `getSync` to `getAttributeSync`
  - `setSync` to `setAttributeSync`
  - `removeSync` to `removeAttributeSync`
  - `listSync` to `listAttributesSync`
  • Loading branch information
LinusU committed May 11, 2021
1 parent 6445f63 commit 4b926f1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
node: [8.6.0, 10.0.0, 12.0.0]
node: [12.20.0, 14.13.1, 16.0.0]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand Down
24 changes: 12 additions & 12 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,45 @@
*
* @returns a `Promise` that will resolve with the value of the attribute.
*/
export function get (path: string, attr: string): Promise<Buffer>
export function getAttribute (path: string, attr: string): Promise<Buffer>

/**
* Synchronous version of `get`.
* Synchronous version of `getAttribute`.
*/
export function getSync (path: string, attr: string): Buffer
export function getAttributeSync (path: string, attr: string): Buffer

/**
* Set extended attribute `attr` to `value` on file at `path`.
*
* @returns a `Promise` that will resolve when the value has been set.
*/
export function set (path: string, attr: string, value: Buffer | string): Promise<void>
export function setAttribute (path: string, attr: string, value: Buffer | string): Promise<void>

/**
* Synchronous version of `set`.
* Synchronous version of `setAttribute`.
*/
export function setSync (path: string, attr: string, value: Buffer | string): void
export function setAttributeSync (path: string, attr: string, value: Buffer | string): void

/**
* Remove extended attribute `attr` on file at `path`.
*
* @returns a `Promise` that will resolve when the value has been removed.
*/
export function remove (path: string, attr: string): Promise<void>
export function removeAttribute (path: string, attr: string): Promise<void>

/**
* Synchronous version of `remove`.
* Synchronous version of `removeAttribute`.
*/
export function removeSync (path: string, attr: string): void
export function removeAttributeSync (path: string, attr: string): void

/**
* List all attributes on file at `path`.
*
* @returns a `Promise` that will resolve with an array of strings, e.g. `['user.linusu.test', 'com.apple.FinderInfo']`.
*/
export function list (path: string): Promise<string[]>
export function listAttributes (path: string): Promise<string[]>

/**
* Synchronous version of `list`.
* Synchronous version of `listAttributes`.
*/
export function listSync (path: string): string[]
export function listAttributesSync (path: string): string[]
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
import { createRequire } from 'node:module'

const addon = require('./build/Release/xattr')
const addon = createRequire(import.meta.url)('./build/Release/xattr')

function validateArgument (key, val) {
switch (key) {
Expand All @@ -21,28 +21,28 @@ function validateArgument (key, val) {

/* Async methods */

exports.get = function get (path, attr) {
export function getAttribute (path, attr) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)

return addon.get(path, attr)
}

exports.set = function set (path, attr, value) {
export function setAttribute (path, attr, value) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)
value = validateArgument('value', value)

return addon.set(path, attr, value)
}

exports.list = function list (path) {
export function listAttributes (path) {
path = validateArgument('path', path)

return addon.list(path)
}

exports.remove = function remove (path, attr) {
export function removeAttribute (path, attr) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)

Expand All @@ -51,28 +51,28 @@ exports.remove = function remove (path, attr) {

/* Sync methods */

exports.getSync = function getSync (path, attr) {
export function getAttributeSync (path, attr) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)

return addon.getSync(path, attr)
}

exports.setSync = function setSync (path, attr, value) {
export function setAttributeSync (path, attr, value) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)
value = validateArgument('value', value)

return addon.setSync(path, attr, value)
}

exports.listSync = function listSync (path) {
export function listAttributesSync (path) {
path = validateArgument('path', path)

return addon.listSync(path)
}

exports.removeSync = function removeSync (path, attr) {
export function removeAttributeSync (path, attr) {
path = validateArgument('path', path)
attr = validateArgument('attr', attr)

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.3.1",
"license": "MIT",
"repository": "LinusU/fs-xattr",
"type": "module",
"exports": "./index.js",
"files": [
"binding.gyp",
"index.d.ts",
Expand All @@ -20,7 +22,7 @@
"ts-readme-generator": "^0.4.3"
},
"engines": {
"node": ">=8.6.0"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"os": [
"!win32"
Expand Down
30 changes: 15 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@ npm install --save fs-xattr
## Usage

```javascript
const xattr = require('fs-xattr')
import { getAttribute, setAttribute } from 'fs-xattr'

await xattr.set('index.js', 'user.linusu.test', 'Hello, World!')
await setAttribute('index.js', 'user.linusu.test', 'Hello, World!')

console.log(await xattr.get('index.js', 'user.linusu.test'))
console.log(await getAttribute('index.js', 'user.linusu.test'))
//=> Hello, World!
```

## API

### `get(path, attr)`
### `getAttribute(path, attr)`

- `path` (`string`, required)
- `attr` (`string`, required)
- returns `Promise<Buffer>` - a `Promise` that will resolve with the value of the attribute.

Get extended attribute `attr` from file at `path`.

### `getSync(path, attr)`
### `getAttributeSync(path, attr)`

- `path` (`string`, required)
- `attr` (`string`, required)
- returns `Buffer`

Synchronous version of `get`.
Synchronous version of `getAttribute`.

### `set(path, attr, value)`
### `setAttribute(path, attr, value)`

- `path` (`string`, required)
- `attr` (`string`, required)
Expand All @@ -53,42 +53,42 @@ Synchronous version of `get`.

Set extended attribute `attr` to `value` on file at `path`.

### `setSync(path, attr, value)`
### `setAttributeSync(path, attr, value)`

- `path` (`string`, required)
- `attr` (`string`, required)
- `value` (`Buffer` or `string`, required)

Synchronous version of `set`.
Synchronous version of `setAttribute`.

### `remove(path, attr)`
### `removeAttribute(path, attr)`

- `path` (`string`, required)
- `attr` (`string`, required)
- returns `Promise<void>` - a `Promise` that will resolve when the value has been removed.

Remove extended attribute `attr` on file at `path`.

### `removeSync(path, attr)`
### `removeAttributeSync(path, attr)`

- `path` (`string`, required)
- `attr` (`string`, required)

Synchronous version of `remove`.
Synchronous version of `removeAttribute`.

### `list(path)`
### `listAttributes(path)`

- `path` (`string`, required)
- returns `Promise<Array<string>>` - a `Promise` that will resolve with an array of strings, e.g. `['user.linusu.test', 'com.apple.FinderInfo']`.

List all attributes on file at `path`.

### `listSync(path)`
### `listAttributesSync(path)`

- `path` (`string`, required)
- returns `Array<string>`

Synchronous version of `list`.
Synchronous version of `listAttributes`.

## Namespaces

Expand Down
51 changes: 25 additions & 26 deletions test/xattr.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-env mocha */

'use strict'
import * as xattr from '../index.js'

const xattr = require('../')
import assert from 'node:assert'
import crypto from 'node:crypto'
import fs from 'node:fs'
import os from 'node:os'

const fs = require('fs')
const os = require('os')
const temp = require('fs-temp')
const assert = require('assert')
const crypto = require('crypto')
import temp from 'fs-temp'

const attribute0 = 'user.linusu.test'
const attribute1 = 'user.linusu.secondary'
Expand All @@ -23,30 +22,30 @@ describe('xattr#sync', function () {
})

it('should set an attribute', function () {
xattr.setSync(path, attribute0, payload0)
xattr.setSync(path, attribute1, payload1)
xattr.setAttributeSync(path, attribute0, payload0)
xattr.setAttributeSync(path, attribute1, payload1)
})

it('should get an attribute', function () {
const val = xattr.getSync(path, attribute0)
const val = xattr.getAttributeSync(path, attribute0)
assert(Buffer.isBuffer(val))
assert.strictEqual(val.toString(), payload0)
})

it('should list the attributes', function () {
const val = xattr.listSync(path)
const val = xattr.listAttributesSync(path)
assert.ok(val.includes(attribute0))
assert.ok(val.includes(attribute1))
})

it('should remove the attribute', function () {
xattr.removeSync(path, attribute0)
xattr.removeSync(path, attribute1)
xattr.removeAttributeSync(path, attribute0)
xattr.removeAttributeSync(path, attribute1)
})

it('should give useful errors', function () {
assert.throws(function () {
xattr.getSync(path, attribute0)
xattr.getAttributeSync(path, attribute0)
}, function (err) {
assert.strictEqual(err.errno, os.platform() === 'darwin' ? 93 : 61)
assert.strictEqual(err.code, os.platform() === 'darwin' ? 'ENOATTR' : 'ENODATA')
Expand All @@ -67,33 +66,33 @@ describe('xattr#async', function () {
})

it('should set an attribute', async function () {
await xattr.set(path, attribute0, payload0)
await xattr.set(path, attribute1, payload1)
await xattr.setAttribute(path, attribute0, payload0)
await xattr.setAttribute(path, attribute1, payload1)
})

it('should get an attribute', async function () {
const val = await xattr.get(path, attribute0)
const val = await xattr.getAttribute(path, attribute0)

assert(Buffer.isBuffer(val))
assert.strictEqual(val.toString(), payload0)
})

it('should list the attributes', async function () {
const list = await xattr.list(path)
const list = await xattr.listAttributes(path)

assert.ok(list.includes(attribute0))
assert.ok(list.includes(attribute1))
})

it('should remove the attribute', async function () {
await xattr.remove(path, attribute0)
await xattr.remove(path, attribute1)
await xattr.removeAttribute(path, attribute0)
await xattr.removeAttribute(path, attribute1)
})

it('should give useful errors', async function () {
let err
try {
await xattr.get(path, attribute0)
await xattr.getAttribute(path, attribute0)
} catch (_err) {
err = _err
}
Expand All @@ -116,30 +115,30 @@ describe('xattr#utf8', function () {
})

it('should set an attribute', async function () {
await xattr.set(path, attribute0, payload0)
await xattr.setAttribute(path, attribute0, payload0)
})

it('should get an attribute', async function () {
const val = await xattr.get(path, attribute0)
const val = await xattr.getAttribute(path, attribute0)

assert(Buffer.isBuffer(val))
assert.strictEqual(val.toString(), payload0)
})

it('should list the attributes', async function () {
const list = await xattr.list(path)
const list = await xattr.listAttributes(path)

assert.ok(list.includes(attribute0))
})

it('should remove the attribute', async function () {
await xattr.remove(path, attribute0)
await xattr.removeAttribute(path, attribute0)
})

it('should give useful errors', async function () {
let err
try {
await xattr.get(path, attribute0)
await xattr.getAttribute(path, attribute0)
} catch (_err) {
err = _err
}
Expand Down

0 comments on commit 4b926f1

Please sign in to comment.