mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-11-10 01:15:35 -06:00
Compare commits
1 Commits
debug_time
...
proxy-api-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e65a139ab |
66
.eslintrc.js
66
.eslintrc.js
@@ -105,8 +105,6 @@ module.exports = {
|
||||
],
|
||||
},
|
||||
],
|
||||
'no-useless-constructor': 'off',
|
||||
'@typescript-eslint/no-useless-constructor': 'error',
|
||||
'no-void': ['error', { allowAsStatement: true }],
|
||||
'n/no-missing-import': 'off', // using 'import' plugin instead, to support TS aliases
|
||||
'no-redeclare': 'off', // automatically checked by the TypeScript compiler
|
||||
@@ -171,6 +169,7 @@ module.exports = {
|
||||
'vue/no-empty-component-block': 'error',
|
||||
'vue/no-multiple-objects-in-class': 'error',
|
||||
'vue/no-ref-object-reactivity-loss': 'error',
|
||||
'vue/no-required-prop-with-default': 'error',
|
||||
'vue/no-static-inline-styles': 'error',
|
||||
'vue/no-template-target-blank': 'error',
|
||||
'vue/no-undef-components': ['error', { ignorePatterns: ['RouterLink', 'RouterView', 'I18nT'] }],
|
||||
@@ -191,14 +190,18 @@ module.exports = {
|
||||
// Specific rules for XO i18n-related files
|
||||
{
|
||||
files: [
|
||||
'@xen-orchestra/lite/**/*.{vue,ts}',
|
||||
'@xen-orchestra/web/**/*.{vue,ts}',
|
||||
'@xen-orchestra/lite/**/*.{vue,ts,json}',
|
||||
'@xen-orchestra/web/**/*.{vue,ts,json}',
|
||||
'@xen-orchestra/web-core/**/*.{vue,ts,json}',
|
||||
],
|
||||
extends: ['plugin:@intlify/vue-i18n/base'],
|
||||
settings: {
|
||||
'vue-i18n': {
|
||||
localeDir: ['@xen-orchestra/web-core/lib/locales/*.json'],
|
||||
localeDir: [
|
||||
'@xen-orchestra/lite/src/locales/*.json',
|
||||
'@xen-orchestra/web/src/locales/*.json',
|
||||
'@xen-orchestra/web-core/lib/locales/*.json',
|
||||
],
|
||||
messageSyntaxVersion: '^9.9.0',
|
||||
},
|
||||
},
|
||||
@@ -216,47 +219,13 @@ module.exports = {
|
||||
'@intlify/vue-i18n/no-raw-text': [
|
||||
'error',
|
||||
{
|
||||
ignoreText: [
|
||||
'Xen Orchestra Lite',
|
||||
'Xen Orchestra',
|
||||
'Xen Orchestra Appliance',
|
||||
'XO Lite',
|
||||
'XCP-ng',
|
||||
'XO 5',
|
||||
'404',
|
||||
'⚠️',
|
||||
],
|
||||
ignoreText: ['Xen Orchestra Lite', 'Xen Orchestra', 'XO Lite', 'XCP-ng', 'XO 5', '404', '⚠️'],
|
||||
},
|
||||
],
|
||||
'@intlify/vue-i18n/no-v-html': 'error',
|
||||
'@intlify/vue-i18n/valid-message-syntax': 'error',
|
||||
'@intlify/vue-i18n/no-duplicate-keys-in-locale': 'error',
|
||||
'@intlify/vue-i18n/no-missing-keys-in-other-locales': [
|
||||
'error',
|
||||
{
|
||||
ignoreLocales: [
|
||||
'de',
|
||||
'fa',
|
||||
'cs',
|
||||
'es',
|
||||
'sv',
|
||||
'it',
|
||||
'ru',
|
||||
'uk',
|
||||
'nl',
|
||||
'pt-BR',
|
||||
'da',
|
||||
'ko',
|
||||
'nb-NO',
|
||||
'ja',
|
||||
'fi',
|
||||
'pl',
|
||||
'zh-Hans',
|
||||
'sk',
|
||||
'pt',
|
||||
],
|
||||
},
|
||||
],
|
||||
'@intlify/vue-i18n/no-missing-keys-in-other-locales': ['error', { ignoreLocales: ['de', 'fa'] }],
|
||||
},
|
||||
},
|
||||
// Specific rules for XO dev-related files
|
||||
@@ -281,7 +250,7 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ['@xen-orchestra/{web-core,lite,web}/src/route-map.d.ts'],
|
||||
files: ['@xen-orchestra/{web-core,lite,web}/typed-router.d.ts'],
|
||||
parserOptions: {
|
||||
sourceType: 'module',
|
||||
},
|
||||
@@ -290,21 +259,8 @@ module.exports = {
|
||||
'eslint-comments/no-unlimited-disable': 'off',
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ['*.d.ts'],
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
sourceType: 'module',
|
||||
},
|
||||
rules: {
|
||||
'n/no-unsupported-features/es-syntax': 'off',
|
||||
'no-unused-vars': 'off',
|
||||
},
|
||||
},
|
||||
],
|
||||
|
||||
ignorePatterns: ['@vates/fatfs/'],
|
||||
|
||||
parserOptions: {
|
||||
ecmaVersion: 13,
|
||||
sourceType: 'script',
|
||||
|
||||
4
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
4
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -40,8 +40,8 @@ body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
- Don't forget to [read this first](https://docs.xen-orchestra.com/community)
|
||||
- As well as follow [this guide](https://docs.xen-orchestra.com/community#report-a-bug)
|
||||
- Don't forget to [read this first](https://xen-orchestra.com/docs/community.html)
|
||||
- As well as follow [this guide](https://xen-orchestra.com/docs/community.html#report-a-bug)
|
||||
- type: input
|
||||
id: xo-sources-commit-number
|
||||
attributes:
|
||||
|
||||
30
.github/workflows/ci.yml
vendored
30
.github/workflows/ci.yml
vendored
@@ -1,50 +1,38 @@
|
||||
name: Continuous Integration
|
||||
name: Continous Integration
|
||||
on: push
|
||||
jobs:
|
||||
CI:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
# https://github.com/actions/checkout
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install packages
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y curl qemu-utils python3-vmdkstream git libxml2-utils libfuse2 nbdkit
|
||||
|
||||
# https://turbo.build/repo/docs/guides/ci-vendors/github-actions#caching-with-github-actionscache
|
||||
- name: Cache Turbo
|
||||
uses: actions/cache@v4
|
||||
# https://github.com/actions/cache
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: '.turbo'
|
||||
key: ${{ runner.os }}-turbo-${{ github.sha }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-turbo-
|
||||
|
||||
# https://github.com/actions/setup-node
|
||||
path: '**/node_modules/.cache/turbo'
|
||||
key: ${{ runner.os }}-turbo-cache
|
||||
- name: Setup Node environment
|
||||
uses: actions/setup-node@v4
|
||||
# https://github.com/actions/setup-node
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '>=22.12'
|
||||
node-version: '18'
|
||||
cache: 'yarn'
|
||||
|
||||
- name: Install project dependencies
|
||||
run: yarn
|
||||
|
||||
- name: Ensure packages are sorted in CHANGELOG.unreleased.md
|
||||
run: ./scripts/gen-deps-list.js --check-order > /dev/null
|
||||
|
||||
- name: Ensure yarn.lock is up-to-date
|
||||
run: git diff --exit-code yarn.lock
|
||||
|
||||
- name: Build the project
|
||||
run: yarn build
|
||||
|
||||
- name: Unit tests
|
||||
run: yarn test-unit
|
||||
|
||||
- name: Lint tests
|
||||
run: yarn test-lint
|
||||
|
||||
- name: Integration tests
|
||||
run: sudo yarn test-integration
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,8 +7,6 @@
|
||||
/@vates/*/node_modules/
|
||||
/@xen-orchestra/*/dist/
|
||||
/@xen-orchestra/*/node_modules/
|
||||
/@xen-orchestra/rest-api/open-api/spec/swagger.json
|
||||
/@xen-orchestra/rest-api/src/open-api/routes/routes.ts
|
||||
/packages/*/dist/
|
||||
/packages/*/node_modules/
|
||||
|
||||
|
||||
@@ -3,5 +3,4 @@
|
||||
**/*.hbs
|
||||
|
||||
# Generated files
|
||||
/@xen-orchestra/web/src/route-map.d.ts
|
||||
/@xen-orchestra/lite/src/route-map.d.ts
|
||||
/@xen-orchestra/web/typed-router.d.ts
|
||||
|
||||
@@ -16,7 +16,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
|
||||
|
||||
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `10`. The value `0` means no concurrency limit.
|
||||
- `signal`: an abort signal to stop the iteration
|
||||
- `stopOnError`: whether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
|
||||
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
|
||||
|
||||
```js
|
||||
import { asyncEach } from '@vates/async-each'
|
||||
|
||||
@@ -34,7 +34,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
|
||||
|
||||
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `10`. The value `0` means no concurrency limit.
|
||||
- `signal`: an abort signal to stop the iteration
|
||||
- `stopOnError`: whether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
|
||||
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`
|
||||
|
||||
```js
|
||||
import { asyncEach } from '@vates/async-each'
|
||||
|
||||
5
@vates/async-each/index.d.ts
vendored
5
@vates/async-each/index.d.ts
vendored
@@ -1,5 +0,0 @@
|
||||
export function asyncEach<Item>(
|
||||
iterable: Iterable<Item> | AsyncIterable<Item>,
|
||||
iteratee: (item: Item, index: number, iterable: Iterable<Item> | AsyncIterable<Item>) => Promise<void>,
|
||||
opts?: { concurrency?: number; signal?: AbortSignal; stopOnError?: boolean }
|
||||
): Promise<void>
|
||||
@@ -11,8 +11,8 @@ class AggregateError extends Error {
|
||||
|
||||
/**
|
||||
* @template Item
|
||||
* @param {Iterable<Item>|AsyncIterable<Item>} iterable
|
||||
* @param {(item: Item, index: number, iterable: Iterable<Item>|AsyncIterable<Item>) => Promise<void>} iteratee
|
||||
* @param {Iterable<Item>} iterable
|
||||
* @param {(item: Item, index: number, iterable: Iterable<Item>) => Promise<void>} iteratee
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 10, signal, stopOnError = true } = {}) {
|
||||
@@ -73,20 +73,20 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 10, s
|
||||
}
|
||||
nextIsRunning = true
|
||||
if (running < concurrency) {
|
||||
try {
|
||||
const cursor = await it.next()
|
||||
if (cursor.done) {
|
||||
next = () => {
|
||||
if (running === 0) {
|
||||
if (errors.length === 0) {
|
||||
resolve()
|
||||
} else {
|
||||
reject(new AggregateError(errors))
|
||||
}
|
||||
const cursor = await it.next()
|
||||
if (cursor.done) {
|
||||
next = () => {
|
||||
if (running === 0) {
|
||||
if (errors.length === 0) {
|
||||
resolve()
|
||||
} else {
|
||||
reject(new AggregateError(errors))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
++running
|
||||
}
|
||||
} else {
|
||||
++running
|
||||
try {
|
||||
const result = iteratee.call(this, cursor.value, index++, iterable)
|
||||
let then
|
||||
if (result != null && typeof result === 'object' && typeof (then = result.then) === 'function') {
|
||||
@@ -94,9 +94,9 @@ exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 10, s
|
||||
} else {
|
||||
onFulfilled(result)
|
||||
}
|
||||
} catch (error) {
|
||||
onRejected(error)
|
||||
}
|
||||
} catch (error) {
|
||||
onRejected(error)
|
||||
}
|
||||
nextIsRunning = false
|
||||
return next()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it, beforeEach } = require('node:test')
|
||||
const { describe, it, beforeEach } = require('test')
|
||||
const assert = require('assert').strict
|
||||
const { spy } = require('sinon')
|
||||
|
||||
@@ -67,17 +67,20 @@ describe('asyncEach', () => {
|
||||
})
|
||||
|
||||
it('stops on first error when stopOnError is true', async () => {
|
||||
const tracker = new assert.CallTracker()
|
||||
|
||||
const error = new Error()
|
||||
const iteratee = spy((_, i) => {
|
||||
const iteratee = tracker.calls((_, i) => {
|
||||
if (i === 1) {
|
||||
throw error
|
||||
}
|
||||
})
|
||||
}, 2)
|
||||
assert.deepStrictEqual(
|
||||
await rejectionOf(asyncEach(iterable, iteratee, { concurrency: 1, stopOnError: true })),
|
||||
error
|
||||
)
|
||||
assert.deepStrictEqual(iteratee.callCount, 2)
|
||||
|
||||
tracker.verify()
|
||||
})
|
||||
|
||||
it('rejects AggregateError when stopOnError is false', async () => {
|
||||
@@ -97,30 +100,19 @@ describe('asyncEach', () => {
|
||||
})
|
||||
|
||||
it('can be interrupted with an AbortSignal', async () => {
|
||||
const tracker = new assert.CallTracker()
|
||||
|
||||
const ac = new AbortController()
|
||||
const iteratee = spy((_, i) => {
|
||||
const iteratee = tracker.calls((_, i) => {
|
||||
if (i === 1) {
|
||||
ac.abort()
|
||||
}
|
||||
})
|
||||
}, 2)
|
||||
await assert.rejects(asyncEach(iterable, iteratee, { concurrency: 1, signal: ac.signal }), {
|
||||
message: 'asyncEach aborted',
|
||||
})
|
||||
assert.deepStrictEqual(iteratee.callCount, 2)
|
||||
})
|
||||
|
||||
it('handle error on iterable', async () => {
|
||||
const iteratee = spy(async () => {})
|
||||
const iterable = async function* () {
|
||||
for (const value of [1, 2, 3]) {
|
||||
if (value === 2) {
|
||||
throw new Error('iterator error')
|
||||
}
|
||||
await randomDelay()
|
||||
yield value
|
||||
}
|
||||
}
|
||||
await assert.rejects(() => asyncEach(iterable(), iteratee, { concurrency: 1 }), { message: 'iterator error' })
|
||||
tracker.verify()
|
||||
})
|
||||
})
|
||||
)
|
||||
|
||||
@@ -24,16 +24,16 @@
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"types": "index.d.ts",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.0",
|
||||
"engines": {
|
||||
"node": ">=8.10"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sinon": "^18.0.0"
|
||||
"sinon": "^18.0.0",
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const assert = require('assert')
|
||||
|
||||
const { coalesceCalls } = require('./')
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const assert = require('node:assert').strict
|
||||
|
||||
const { compose } = require('./')
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
|
||||
const { decorateClass, decorateWith, decorateMethodsWith, perInstance } = require('./')
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* @param {Array|Buffer|string} data1 - The first data array or buffer to compare.
|
||||
* @param {T} data2 - The second data array or buffer to compare.
|
||||
* @param {(index: number, diff: T) => void} [cb] - The callback function to invoke for each difference. If not provided, an array of differences will be returned.
|
||||
* @returns {Array<number|T>|undefined} - An array of differences if no callback is provided; otherwise, undefined.
|
||||
* @returns {Array<number|T>|undefined} - An array of differences if no callback is provided, otherwise undefined.
|
||||
*/
|
||||
module.exports = function diff(data1, data2, cb) {
|
||||
let result
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('node:assert/strict')
|
||||
const test = require('node:test')
|
||||
const test = require('test')
|
||||
|
||||
const diff = require('./index.js')
|
||||
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import { deduped } from '@vates/disposable/deduped'
|
||||
// the connection with the passed host will be established once at the first call, then, it will be shared with the next calls
|
||||
const getConnection = deduped(async function (host)) {
|
||||
const connection = new Connection(host)
|
||||
return new Disposable(connection, () => connection.close())
|
||||
return new Disposabe(connection, () => connection.close())
|
||||
}, host => [host])
|
||||
```
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ import { deduped } from '@vates/disposable/deduped'
|
||||
// the connection with the passed host will be established once at the first call, then, it will be shared with the next calls
|
||||
const getConnection = deduped(async function (host)) {
|
||||
const connection = new Connection(host)
|
||||
return new Disposable(connection, () => connection.close())
|
||||
return new Disposabe(connection, () => connection.close())
|
||||
}, host => [host])
|
||||
```
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const { useFakeTimers, spy, assert } = require('sinon')
|
||||
|
||||
const { createDebounceResource } = require('./debounceResource')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const { spy, assert } = require('sinon')
|
||||
|
||||
const { deduped } = require('./deduped')
|
||||
|
||||
@@ -14,21 +14,22 @@
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"version": "0.1.6",
|
||||
"version": "0.1.5",
|
||||
"engines": {
|
||||
"node": ">=8.10"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vates/multi-key-map": "^0.2.0",
|
||||
"@xen-orchestra/async-map": "^0.1.3",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"@xen-orchestra/async-map": "^0.1.2",
|
||||
"@xen-orchestra/log": "^0.6.0",
|
||||
"ensure-array": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"sinon": "^18.0.0"
|
||||
"sinon": "^18.0.0",
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
# @vates/fatfs
|
||||
|
||||
This is a fork of the original library [natevw/fatfs](https://github.com/natevw/fatfs).
|
||||
|
||||
## Why?
|
||||
|
||||
This fork was created to add a missing feature in the original library: the ability to create labels (createLabel).
|
||||
The original library did not support this functionality, which was required to make Cloudbase-Init work on windows VMs.
|
||||
See: https://github.com/natevw/fatfs/issues/30, https://github.com/natevw/fatfs/pull/31
|
||||
|
||||
A standalone FAT16/FAT32 implementation that takes in a block-access interface and exposes something quite similar to `require('fs')` (i.e. the node.js built-in [Filesystem API](http://nodejs.org/api/fs.html)).
|
||||
|
||||
## Installation
|
||||
|
||||
`npm install fatfs`
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
var fatfs = require('fatfs'),
|
||||
fs = fatfs.createFileSystem(exampleDriver) // see below
|
||||
fs.stat('autoexec.bat', function (e, stats) {
|
||||
if (e) console.error(e)
|
||||
else console.log(stats)
|
||||
})
|
||||
// TODO: open a file and write to it or something…
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
- `fs = fatfs.createFileSystem(vol, [opts], [cb])` — Simply pass in a block driver (see below) mapped to a FAT partition somewhere, and get back an instance of an [`fs`](http://nodejs.org/api/fs.html). An options dictionary can be provided, details are in the next section. You may also optionally provide a callback `cb(err)` which will be automatically registered for the on `'ready'` or `'error'` event.
|
||||
- `'ready'` event — fired on `fs` when initial volume information has been determined and the API is ready to use. It is safe to call other `fs` methods before this fires **only if** you are sure the first sector will be readable and represents a valid FAT volume.
|
||||
- `'error'` event — fired if initialization fails for whatever reason.
|
||||
|
||||
### Filesystem options
|
||||
|
||||
The `opts` dictionary you pass to `fatfs.createFileSystem` can contain any of the following options:
|
||||
|
||||
- `ro` — Enables readonly mode if `true`. It defaults to `false`, but if your volume driver does not provide a `writeSectors` method it will be overridden to `true`.
|
||||
- `noatime` — The FAT filesystem can track the last access time (just a date, actually) but this means every read operation would also incur some write overhead. Defaults to `true`, meaning by default access times will **not** be updated on reads. Set this to `false` to track access times.
|
||||
- `modmode` — chooses how `fs.chmod` (and the mode field from `fs.stat`–family calls) should map FAT attributes to POSIX permissions. Set to the number `0111` to map the readonly flag to the user's write bit being unset, and the archive/system/hidden flags to the user/group/other executable bits respectively. Set to the number `07000` to map the readonly flag to _all_ write bits being unset, and the archive/system/hidden flags to the sticky/setgid/setuid bits respectively. Set to `null` for readonly mapping. Defaults to `0111`.
|
||||
- `umask` — any bits _set_ in this octal number will be _unset_ in the 'mode' field from `fs.stat`–family calls. It does not affect anything else. Defaults to `process.umask()`, or `0022` if that is unavailable.
|
||||
- `uid` — This value will be returned as the 'uid' stat field. It does not affect anything else. Defaults to `process.getuid()`, or `0` if that is unavailable.
|
||||
- `gid` — This value will be returned as the 'gid' stat field. It does not affect anything else. Defaults to `process.getgid()`, or `0` if that is unavailable.
|
||||
|
||||
(Note that these are similar to the options you could use with a POSIX `mount` operation.)
|
||||
|
||||
And that's it! The [rest of the API](http://nodejs.org/api/fs.html) (`fs.readdir`, `fs.open`, `fs.createReadStream`, `fs.appendFile`, etc.) is as documented by the node.js project.
|
||||
|
||||
Well, sort of…
|
||||
|
||||
## Caveats
|
||||
|
||||
### Temporary
|
||||
|
||||
- **BETA** **BETA** **BETA**. Seriously, this is a _brand new_, _from scratch_, _completely unproven_ filesystem implementation. It does not have full automated test coverage, and it has not been manually tested very much either. Please, please, please, **make sure you have a backup** of any important drive/image/card you unleash this upon.
|
||||
- A few methods are not quite implemented, either: `fs.rename`, `fs.unlink` and `fs.rmdir`, as well as `fs.watchFile`/`fs.unwatchFile` and `fs.watch`. These are Coming Soon™.
|
||||
- There are several internal housekeeping items (redundant FAT tables, extra FAT32 information, etc.) that are not done. These do not seem to affect interop, but you may see warnings when repairing a filesystem written by this module.
|
||||
- Oh, and not to scare you, but if an IO error happens while writing, the library usually just bails — bubbling an error up to your callback as if it were a hot potato. Although some attempt has been made to do separate writes in the safest order (e.g. allocating an additional file cluster, then appending data into it, and then finally updating the file's size), but this behavior has not been thoroughly audited for all operations. There's certainly no attempt to retry/cleanup/rollback if a multi-step change runs into trouble partway through.
|
||||
|
||||
### As-planned
|
||||
|
||||
Some of the differences between `fatfs` and the node.js `fs` module are "by design" for architectural simplicity and/or due to underlying FAT limitations.
|
||||
|
||||
- There are no `fs.*Sync` methods. (The volume driver is async, not to mention that supporting a separate \*Sync codepath would be an enormous duplication of effort of dubious value.)
|
||||
- This module does [almost] no read/write caching. This should be done in your volume driver, but see notes below.
|
||||
- You'll need multiple `createFileSystem` instances for multiple volumes; paths are relative to each, and don't share a namespace.
|
||||
- The FAT filesystem has no concept of symlinks, and hardlinks are not really an intentional feature. You will get an ENOSYS-like error when trying to create either type of link.
|
||||
|
||||
## "Volume driver" API
|
||||
|
||||
To use 'fatfs', you must provide a driver object with the following properties/methods:
|
||||
|
||||
- `driver.sectorSize` — number of bytes per sector on this device
|
||||
- `driver.numSectors` — count of sectors available on this media
|
||||
- `driver.readSectors(i, dest, cb)` — Fill `dest` with data starting at the `i`th sector and notify `cb(e)` when finished. You may assume `dest.length` is a multiple of `driver.sectorSize`.
|
||||
- `driver.writeSectors(i, data, cb)` — (optional) Write `data` starting at the `i`th sector and notify `cb(e)` when finished. You may assume `data.length` is a multiple of `driver.sectorSize`.
|
||||
|
||||
If you do not provide a `writeSectors` method, then `fatfs` will work in readonly mode. Pretty simple, eh? And the 'fatfs' module makes a good effort to check the parameters passed to your driver methods!
|
||||
|
||||
**TBD:** to facilitate proper cache handling, this module might add an optional `driver.flush(cb)` method at some point in the future.
|
||||
|
||||
Here's an example taken from code used to run this module's own tests:
|
||||
|
||||
```js
|
||||
// NOTE: this assumes image at `path` has no partition table.
|
||||
// If it did, you'd need to translate positions, natch…
|
||||
var fs = require('fs')
|
||||
|
||||
exports.createDriverSync = function (path, opts) {
|
||||
opts || (opts = {})
|
||||
|
||||
var secSize = 512,
|
||||
ro = opts.readOnly || false,
|
||||
fd = fs.openSync(path, ro ? 'r' : 'r+'),
|
||||
s = fs.fstatSync(fd)
|
||||
|
||||
return {
|
||||
sectorSize: secSize,
|
||||
numSectors: s.size / secSize,
|
||||
readSectors: function (i, dest, cb) {
|
||||
if (dest.length % secSize) throw Error('Unexpected buffer length!')
|
||||
fs.read(fd, dest, 0, dest.length, i * secSize, function (e, n, d) {
|
||||
cb(e, d)
|
||||
})
|
||||
},
|
||||
writeSectors: ro
|
||||
? null
|
||||
: function (i, data, cb) {
|
||||
if (data.length % secSize) throw Error('Unexpected buffer length!')
|
||||
fs.write(fd, data, 0, data.length, i * secSize, function (e) {
|
||||
cb(e)
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
© 2014 Nathan Vander Wilt.
|
||||
Funding for this work was provided by Technical Machine, Inc.
|
||||
|
||||
Reuse under your choice of:
|
||||
|
||||
- [BSD-2-Clause](http://opensource.org/licenses/BSD-2-Clause)
|
||||
- [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0.html)
|
||||
@@ -1,60 +0,0 @@
|
||||
var _ = require("./helpers.js");
|
||||
|
||||
exports.wrapDriver = function (volume, opts) {
|
||||
opts = _.extend({
|
||||
maxSectors: 2048
|
||||
}, opts);
|
||||
|
||||
var cache = {},
|
||||
advice = 'NORMAL',
|
||||
secSize = volume.sectorSize;
|
||||
|
||||
function _freezeBuffer(b) {
|
||||
var f = _.allocBuffer(b.length);
|
||||
b.copy(f);
|
||||
return f;
|
||||
}
|
||||
|
||||
function addToCache(i, data) {
|
||||
if (advice === 'SEQUENTIAL' || advice === 'NOREUSE') return;
|
||||
data = _freezeBuffer(data);
|
||||
cache[i] = data;
|
||||
//if (data.length > secSize) addToCache(i+1, data.slice(secSize));
|
||||
while (data.length > secSize) {
|
||||
data = data.slice(secSize);
|
||||
cache[++i] = data;
|
||||
}
|
||||
// simple highest-sectors-lose eviction policy for now
|
||||
Object.keys(cache).sort().slice(opts.maxSectors).forEach(function (x) {
|
||||
delete cache[x];
|
||||
});
|
||||
_.log(_.log.DBG, "Cache now contains:", Object.keys(cache).join(','));
|
||||
}
|
||||
|
||||
return {
|
||||
sectorSize: volume.sectorSize,
|
||||
numSectors: volume.numSectors,
|
||||
advice: function (val) {
|
||||
if (!arguments.length) return advice;
|
||||
else advice = val;
|
||||
if (advice === 'SEQUENTIAL' || advice === 'NOREUSE') cache = {};
|
||||
return this;
|
||||
},
|
||||
readSectors: function (i, dest, cb) {
|
||||
// TODO: handle having partial parts of dest!
|
||||
if (i in cache && dest.length === secSize) {
|
||||
cache[i].copy(dest);
|
||||
setImmediate(cb);
|
||||
} else volume.readSectors(i, dest, function (e) {
|
||||
if (e) cb(e);
|
||||
else addToCache(i, dest), cb();
|
||||
});
|
||||
},
|
||||
writeSectors: (!volume.writeSectors) ? null : function (i, data, cb) {
|
||||
volume.writeSectors(i, data, function (e) {
|
||||
if (e) cb(e);
|
||||
else addToCache(i, data), cb();
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -1,287 +0,0 @@
|
||||
var S = require("./structs.js"),
|
||||
_ = require("./helpers.js");
|
||||
|
||||
function _baseChain(vol) {
|
||||
var chain = {};
|
||||
|
||||
chain.sectorSize = vol._sectorSize;
|
||||
|
||||
function posFromOffset(off) {
|
||||
var secSize = chain.sectorSize,
|
||||
offset = off % secSize,
|
||||
sector = (off - offset) / secSize;
|
||||
return {sector:sector, offset:offset};
|
||||
}
|
||||
|
||||
var sectorCache = vol._makeCache();
|
||||
Object.defineProperty(chain, 'cacheAdvice', {
|
||||
enumerable: true,
|
||||
get: function () { return sectorCache.advice(); },
|
||||
set: function (v) { sectorCache.advice(v); }
|
||||
});
|
||||
chain._vol_readSectors = vol._readSectors.bind(vol, sectorCache);
|
||||
chain._vol_writeSectors = vol._writeSectors.bind(vol, sectorCache);
|
||||
|
||||
// cb(error, bytesRead, buffer)
|
||||
chain.readFromPosition = function (targetPos, buffer, cb) {
|
||||
if (typeof targetPos === 'number') targetPos = posFromOffset(targetPos);
|
||||
if (typeof buffer === 'number') buffer = _.allocBuffer(buffer);
|
||||
/* NOTE: to keep our contract with the volume driver, we need to read on _full_ sector boundaries!
|
||||
So we divide the read into [up to] three parts: {preface, main, trailer}
|
||||
This is kind of unfortunate, but in practice should often still be reasonably efficient. */
|
||||
if (targetPos.offset) chain.readSectors(targetPos.sector, _.allocBuffer(chain.sectorSize), function (e,d) {
|
||||
if (e || !d) cb(e, 0, buffer);
|
||||
else { // copy preface into `buffer`
|
||||
var dBeg = targetPos.offset,
|
||||
dEnd = dBeg + buffer.length;
|
||||
d.copy(buffer, 0, dBeg, dEnd);
|
||||
if (dEnd > d.length) readMain();
|
||||
else cb(null, buffer.length, buffer);
|
||||
}
|
||||
}); else readMain();
|
||||
function readMain() {
|
||||
var prefaceLen = targetPos.offset && (chain.sectorSize - targetPos.offset),
|
||||
trailerLen = (buffer.length - prefaceLen) % chain.sectorSize,
|
||||
mainSector = (prefaceLen) ? targetPos.sector + 1 : targetPos.sector,
|
||||
mainBuffer = (trailerLen) ? buffer.slice(prefaceLen, -trailerLen) : buffer.slice(prefaceLen);
|
||||
if (mainBuffer.length) chain.readSectors(mainSector, mainBuffer, function (e,d) {
|
||||
if (e || !d) cb(e, prefaceLen, buffer);
|
||||
else if (!trailerLen) cb(null, buffer.length, buffer);
|
||||
else readTrailer();
|
||||
}); else readTrailer();
|
||||
function readTrailer() {
|
||||
var trailerSector = mainSector + (mainBuffer.length / chain.sectorSize);
|
||||
chain.readSectors(trailerSector, _.allocBuffer(chain.sectorSize), function (e,d) {
|
||||
if (e || !d) cb(e, buffer.length-trailerLen, buffer);
|
||||
else {
|
||||
d.copy(buffer, buffer.length-trailerLen, 0, trailerLen);
|
||||
cb(null, buffer.length, buffer);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// cb(error)
|
||||
chain.writeToPosition = function (targetPos, data, cb) {
|
||||
_.log(_.log.DBG, "WRITING", data.length, "bytes at", targetPos, "in", this.toJSON(), data);
|
||||
if (typeof targetPos === 'number') targetPos = posFromOffset(targetPos);
|
||||
|
||||
var prefaceBuffer = (targetPos.offset) ? data.slice(0, chain.sectorSize-targetPos.offset) : null;
|
||||
if (prefaceBuffer) _modifySector(targetPos.sector, targetPos.offset, prefaceBuffer, function (e) {
|
||||
if (e) cb(e);
|
||||
else if (prefaceBuffer.length < data.length) writeMain();
|
||||
else cb();
|
||||
}); else writeMain();
|
||||
function writeMain() {
|
||||
var prefaceLen = (prefaceBuffer) ? prefaceBuffer.length : 0,
|
||||
trailerLen = (data.length - prefaceLen) % chain.sectorSize,
|
||||
mainSector = (prefaceLen) ? targetPos.sector + 1 : targetPos.sector,
|
||||
mainBuffer = (trailerLen) ? data.slice(prefaceLen, -trailerLen) : data.slice(prefaceLen);
|
||||
if (mainBuffer.length) chain.writeSectors(mainSector, mainBuffer, function (e) {
|
||||
if (e) cb(e);
|
||||
else if (!trailerLen) cb();
|
||||
else writeTrailer();
|
||||
}); else writeTrailer();
|
||||
function writeTrailer() {
|
||||
var trailerSector = mainSector + (mainBuffer.length / chain.sectorSize),
|
||||
trailerBuffer = data.slice(data.length-trailerLen); // WORKAROUND: https://github.com/tessel/runtime/issues/721
|
||||
_modifySector(trailerSector, 0, trailerBuffer, cb);
|
||||
}
|
||||
}
|
||||
function _modifySector(sec, off, data, cb) {
|
||||
chain.readSectors(sec, _.allocBuffer(chain.sectorSize), function (e, orig) {
|
||||
if (e) return cb(e);
|
||||
orig || (orig = _.allocBuffer(chain.sectorSize, 0));
|
||||
data.copy(orig, off);
|
||||
chain.writeSectors(sec, orig, cb);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return chain;
|
||||
};
|
||||
|
||||
|
||||
|
||||
exports.clusterChain = function (vol, firstCluster, _parent) {
|
||||
var chain = _baseChain(vol),
|
||||
cache = [firstCluster];
|
||||
|
||||
chain.firstCluster = firstCluster;
|
||||
|
||||
function _cacheIsComplete() {
|
||||
return cache[cache.length-1] === 'eof';
|
||||
}
|
||||
|
||||
function extendCacheToInclude(i, cb) { // NOTE: may `cb()` before returning!
|
||||
if (i < cache.length) cb(null, cache[i]);
|
||||
else if (_cacheIsComplete()) cb(null, 'eof');
|
||||
else vol.fetchFromFAT(cache[cache.length-1], function (e,d) {
|
||||
if (e) cb(e);
|
||||
else if (typeof d === 'string' && d !== 'eof') cb(S.err.IO());
|
||||
else {
|
||||
cache.push(d);
|
||||
extendCacheToInclude(i, cb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function expandChainToLength(clusterCount, cb) {
|
||||
if (!_cacheIsComplete()) throw Error("Must be called only when cache is complete!");
|
||||
else cache.pop(); // remove 'eof' entry until finished
|
||||
|
||||
function addCluster(clustersNeeded, lastCluster) {
|
||||
if (!clustersNeeded) cache.push('eof'), cb();
|
||||
else vol.allocateInFAT(lastCluster, function (e, newCluster) {
|
||||
if (e) cb(e);
|
||||
else vol.storeToFAT(lastCluster, newCluster, function (e) {
|
||||
if (e) return cb(e);
|
||||
|
||||
cache.push(newCluster);
|
||||
addCluster(clustersNeeded-1, newCluster);
|
||||
});
|
||||
});
|
||||
}
|
||||
addCluster(clusterCount - cache.length, cache[cache.length - 1]);
|
||||
}
|
||||
|
||||
function shrinkChainToLength(clusterCount, cb) {
|
||||
if (!_cacheIsComplete()) throw Error("Must be called only when cache is complete!");
|
||||
else cache.pop(); // remove 'eof' entry until finished
|
||||
|
||||
function removeClusters(count, cb) {
|
||||
if (!count) cache.push('eof'), cb();
|
||||
else vol.storeToFAT(cache.pop(), 'free', function (e) {
|
||||
if (e) cb(e);
|
||||
else removeClusters(count - 1, cb);
|
||||
});
|
||||
}
|
||||
// NOTE: for now, we don't remove the firstCluster ourselves; we should though!
|
||||
if (clusterCount) removeClusters(cache.length - clusterCount, cb);
|
||||
else removeClusters(cache.length - 1, cb);
|
||||
}
|
||||
|
||||
// [{firstSector,numSectors},{firstSector,numSectors},…]
|
||||
function determineSectorGroups(sectorIdx, numSectors, alloc, cb) {
|
||||
var sectorOffset = sectorIdx % vol._sectorsPerCluster,
|
||||
clusterIdx = (sectorIdx - sectorOffset) / vol._sectorsPerCluster,
|
||||
numClusters = Math.ceil((numSectors + sectorOffset) / vol._sectorsPerCluster),
|
||||
chainLength = clusterIdx + numClusters;
|
||||
extendCacheToInclude(chainLength-1, function (e,c) {
|
||||
if (e) cb(e);
|
||||
else if (c === 'eof' && alloc) expandChainToLength(chainLength, function (e) {
|
||||
if (e) cb(e);
|
||||
else _determineSectorGroups();
|
||||
});
|
||||
else _determineSectorGroups();
|
||||
});
|
||||
function _determineSectorGroups() {
|
||||
// …now we have a complete cache
|
||||
var groups = [],
|
||||
_group = null;
|
||||
for (var i = clusterIdx; i < chainLength; ++i) {
|
||||
var c = (i < cache.length) ? cache[i] : 'eof';
|
||||
if (c === 'eof') break;
|
||||
else if (_group && c !== _group._nextCluster) {
|
||||
groups.push(_group);
|
||||
_group = null;
|
||||
}
|
||||
if (!_group) _group = {
|
||||
_nextCluster: c+1,
|
||||
firstSector: vol._firstSectorOfCluster(c) + sectorOffset,
|
||||
numSectors: vol._sectorsPerCluster - sectorOffset
|
||||
}; else {
|
||||
_group._nextCluster += 1;
|
||||
_group.numSectors += vol._sectorsPerCluster;
|
||||
}
|
||||
sectorOffset = 0; // only first group is offset
|
||||
}
|
||||
if (_group) groups.push(_group);
|
||||
cb(null, groups, i === chainLength);
|
||||
}
|
||||
}
|
||||
|
||||
chain.readSectors = function (i, dest, cb) {
|
||||
var groupOffset = 0, groupsPending;
|
||||
determineSectorGroups(i, dest.length / chain.sectorSize, false, function (e, groups, complete) {
|
||||
if (e) cb(e);
|
||||
else if (!complete) groupsPending = -1, _pastEOF(cb);
|
||||
else if ((groupsPending = groups.length)) groups.forEach(function (group) {
|
||||
var groupLength = group.numSectors * chain.sectorSize,
|
||||
groupBuffer = dest.slice(groupOffset, groupOffset += groupLength);
|
||||
chain._vol_readSectors(group.firstSector, groupBuffer, function (e,d) {
|
||||
if (e && groupsPending !== -1) groupsPending = -1, cb(e);
|
||||
else if (--groupsPending === 0) cb(null, dest);
|
||||
});
|
||||
});
|
||||
else cb(null, dest); // 0-length destination case
|
||||
});
|
||||
};
|
||||
|
||||
// TODO: does this handle NOSPC condition?
|
||||
chain.writeSectors = function (i, data, cb) {
|
||||
var groupOffset = 0, groupsPending;
|
||||
determineSectorGroups(i, data.length / chain.sectorSize, true, function (e, groups) {
|
||||
if (e) cb(e);
|
||||
else if ((groupsPending = groups.length)) groups.forEach(function (group) {
|
||||
var groupLength = group.numSectors * chain.sectorSize,
|
||||
groupBuffer = data.slice(groupOffset, groupOffset += groupLength);
|
||||
chain._vol_writeSectors(group.firstSector, groupBuffer, function (e) {
|
||||
if (e && groupsPending !== -1) groupsPending = -1, cb(e);
|
||||
else if (--groupsPending === 0) cb();
|
||||
});
|
||||
});
|
||||
else cb(); // 0-length data case
|
||||
});
|
||||
};
|
||||
|
||||
chain.truncate = function (numSectors, cb) {
|
||||
extendCacheToInclude(Infinity, function (e,c) {
|
||||
if (e) return cb(e);
|
||||
|
||||
var currentLength = cache.length-1,
|
||||
clustersNeeded = Math.ceil(numSectors / vol._sectorsPerCluster);
|
||||
if (clustersNeeded < currentLength) shrinkChainToLength(clustersNeeded, cb);
|
||||
else if (clustersNeeded > currentLength) expandChainToLength(clustersNeeded, cb);
|
||||
else cb();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
chain.toJSON = function () {
|
||||
return {firstCluster:firstCluster};
|
||||
};
|
||||
|
||||
return chain;
|
||||
};
|
||||
|
||||
exports.sectorChain = function (vol, firstSector, numSectors) {
|
||||
var chain = _baseChain(vol);
|
||||
|
||||
chain.firstSector = firstSector;
|
||||
chain.numSectors = numSectors;
|
||||
|
||||
chain.readSectors = function (i, dest, cb) {
|
||||
if (i < numSectors) chain._vol_readSectors(firstSector+i, dest, cb);
|
||||
else _pastEOF(cb);
|
||||
};
|
||||
|
||||
chain.writeSectors = function (i, data, cb) {
|
||||
if (i < numSectors) chain._vol_writeSectors(firstSector+i, data, cb);
|
||||
else _.delayedCall(cb, S.err.NOSPC());
|
||||
};
|
||||
|
||||
chain.truncate = function (i, cb) {
|
||||
_.delayedCall(cb, S.err.INVAL());
|
||||
};
|
||||
|
||||
chain.toJSON = function () {
|
||||
return {firstSector:firstSector, numSectors:numSectors};
|
||||
};
|
||||
|
||||
return chain;
|
||||
};
|
||||
|
||||
// NOTE: used with mixed feelings, broken out to mark uses
|
||||
function _pastEOF(cb) { _.delayedCall(cb, null, null); }
|
||||
@@ -1,372 +0,0 @@
|
||||
var S = require("./structs.js"),
|
||||
_ = require("./helpers.js");
|
||||
|
||||
var dir = exports;
|
||||
|
||||
dir.iterator = function (dirChain, opts) {
|
||||
opts || (opts = {});
|
||||
|
||||
var cache = {buffer:null, n: null};
|
||||
function getSectorBuffer(n, cb) {
|
||||
if (cache.n === n) cb(null, cache.buffer);
|
||||
else cache.n = cache.buffer = null, dirChain.readSectors(n, _.allocBuffer(dirChain.sectorSize), function (e,d) {
|
||||
if (e) cb(e);
|
||||
else if (!d) return cb(null, null);
|
||||
else {
|
||||
cache.n = n;
|
||||
cache.buffer = d;
|
||||
getSectorBuffer(n, cb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var secIdx = 0,
|
||||
off = {bytes:0},
|
||||
long = null;
|
||||
function getNextEntry(cb) {
|
||||
if (off.bytes >= dirChain.sectorSize) {
|
||||
secIdx += 1;
|
||||
off.bytes -= dirChain.sectorSize;
|
||||
}
|
||||
var entryPos = {chain:dirChain, sector:secIdx, offset:off.bytes};
|
||||
getSectorBuffer(secIdx, function (e, buf) {
|
||||
if (e) return cb(S.err.IO());
|
||||
else if (!buf) return cb(null, null, entryPos);
|
||||
|
||||
var entryIdx = off.bytes,
|
||||
signalByte = buf[entryIdx];
|
||||
if (signalByte === S.entryDoneFlag) return cb(null, null, entryPos);
|
||||
else if (signalByte === S.entryFreeFlag) {
|
||||
off.bytes += S.dirEntry.size;
|
||||
long = null;
|
||||
if (opts.includeFree) return cb(null, {_free:true,_pos:entryPos}, entryPos);
|
||||
else return getNextEntry(cb); // usually just skip these
|
||||
}
|
||||
|
||||
var attrByte = buf[entryIdx+S.dirEntry.fields.Attr.offset],
|
||||
entryType = (attrByte === S.longDirFlag) ? S.longDirEntry : S.dirEntry_simple;
|
||||
var entry = entryType.valueFromBytes(buf, off);
|
||||
entry._pos = entryPos;
|
||||
_.log(_.log.DBG, "entry:", entry, secIdx, entryIdx);
|
||||
if (entryType === S.longDirEntry) {
|
||||
var firstEntry;
|
||||
if (entry.Ord & S.lastLongFlag) {
|
||||
firstEntry = true;
|
||||
entry.Ord &= ~S.lastLongFlag;
|
||||
long = {
|
||||
name: -1,
|
||||
sum: entry.Chksum,
|
||||
_rem: entry.Ord-1,
|
||||
_arr: []
|
||||
}
|
||||
}
|
||||
if (firstEntry || long && entry.Chksum === long.sum && entry.Ord === long._rem--) {
|
||||
var S_lde_f = S.longDirEntry.fields,
|
||||
namepart = entry.Name1;
|
||||
if (entry.Name1.length === S_lde_f.Name1.size/2) {
|
||||
namepart += entry.Name2;
|
||||
if (entry.Name2.length === S_lde_f.Name2.size/2) {
|
||||
namepart += entry.Name3;
|
||||
}
|
||||
}
|
||||
long._arr.push(namepart);
|
||||
if (!long._rem) {
|
||||
long.name = long._arr.reverse().join('');
|
||||
delete long._arr;
|
||||
delete long._rem;
|
||||
}
|
||||
} else long = null;
|
||||
} else if ((attrByte & 0x08) === 0) { // NOTE: checks `!entry.Attr.volume_id`
|
||||
var bestName = null;
|
||||
if (long && long.name) {
|
||||
var pos = entryIdx + S.dirEntry.fields['Name'].offset,
|
||||
sum = _.checksumName(buf, pos);
|
||||
if (sum === long.sum) bestName = long.name;
|
||||
}
|
||||
if (!bestName) {
|
||||
if (signalByte === S.entryIsE5Flag) entry.Name.filename = '\u00E5'+entry.Name.filename.slice(1);
|
||||
|
||||
var nam = entry.Name.filename.replace(/ +$/, ''),
|
||||
ext = entry.Name.extension.replace(/ +$/, '');
|
||||
// TODO: lowercase bits http://en.wikipedia.org/wiki/8.3_filename#Compatibility
|
||||
// via NTRes, bits 0x08 and 0x10 http://www.fdos.org/kernel/fatplus.txt.1
|
||||
bestName = (ext) ? nam+'.'+ext : nam;
|
||||
}
|
||||
entry._name = bestName;
|
||||
|
||||
// OPTIMIZATION: avoid processing any fields for non-matching entries
|
||||
// TODO: we could make this automatic via getters, but…?
|
||||
var _entryBuffer = buf.slice(off.bytes-S.dirEntry.size, off.bytes);
|
||||
entry._full = function () {
|
||||
var _entry = S.dirEntry.valueFromBytes(_entryBuffer);
|
||||
_.extend(entry, _entry);
|
||||
entry._size = entry.FileSize;
|
||||
entry._firstCluster = (entry.FstClusHI << 16) + entry.FstClusLO;
|
||||
return entry;
|
||||
};
|
||||
|
||||
long = null;
|
||||
return cb(null, entry, entryPos);
|
||||
} else long = null;
|
||||
getNextEntry(cb);
|
||||
});
|
||||
}
|
||||
|
||||
function iter(cb) {
|
||||
getNextEntry(cb);
|
||||
return iter; // TODO: previous value can't be re-used, so why make caller re-assign?
|
||||
}
|
||||
return iter;
|
||||
};
|
||||
|
||||
function _updateEntry(vol, entry, newStats) {
|
||||
if ('size' in newStats) entry._size = entry.FileSize = newStats.size;
|
||||
|
||||
if ('_touch' in newStats) newStats.archive = newStats.atime = newStats.mtime = true;
|
||||
if ('archive' in newStats) entry.Attr.archive = true; // TODO: also via newStats.mode?
|
||||
|
||||
var _now;
|
||||
function applyDate(d, prefix, timeToo, tenthToo) {
|
||||
if (d === true) d = _now || (_now = new Date());
|
||||
entry[prefix+'Date'] = {year:d.getFullYear()-1980, month:d.getMonth()+1, day:d.getDate()};
|
||||
if (timeToo) {
|
||||
entry[prefix+'Time'] = {hours:d.getHours(), minutes:d.getMinutes(), seconds_2:d.getSeconds()>>>1};
|
||||
if (tenthToo) {
|
||||
var msec = (d.getSeconds() % 2)*1000 + d.getMilliseconds();
|
||||
entry[prefix+'TimeTenth'] = Math.floor(msec / 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ('ctime' in newStats) applyDate(newStats.ctime, 'Crt', true, true);
|
||||
if ('mtime' in newStats) applyDate(newStats.mtime, 'Wrt', true);
|
||||
if ('atime' in newStats) applyDate(newStats.atime, 'LstAcc');
|
||||
|
||||
if ('mode' in newStats) {
|
||||
entry.Attr.directory = (newStats.mode & S._I.FDIR) ? true : false;
|
||||
entry.Attr.volume_id = (newStats.mode & S._I.FREG) ? false : true;
|
||||
if (vol.opts.modmode === 0111) {
|
||||
entry.Attr.archive = (newStats.mode & S._I.XUSR) ? true : false;
|
||||
entry.Attr.system = (newStats.mode & S._I.XGRP) ? true : false;
|
||||
entry.Attr.hidden = (newStats.mode & S._I.XOTH) ? true : false;
|
||||
entry.Attr.readonly = (newStats.mode & S._I.WUSR) ? false : true;
|
||||
} else if (vol.opts.modmode === 07000) {
|
||||
entry.Attr.archive = (newStats.mode & S._I.SVTX) ? true : false;
|
||||
entry.Attr.system = (newStats.mode & S._I.SGID) ? true : false;
|
||||
entry.Attr.hidden = (newStats.mode & S._I.SUID) ? true : false;
|
||||
entry.Attr.readonly = (
|
||||
newStats.mode & S._I.WUSR ||
|
||||
newStats.mode & S._I.WGRP ||
|
||||
newStats.mode & S._I.WOTH
|
||||
) ? false : true;
|
||||
}
|
||||
}
|
||||
|
||||
if ('firstCluster' in newStats) {
|
||||
entry.FstClusLO = newStats.firstCluster & 0xFFFF;
|
||||
entry.FstClusHI = newStats.firstCluster >>> 16;
|
||||
entry._firstCluster = newStats.firstCluster;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
dir.makeStat = function (vol, entry) {
|
||||
var stats = {}; // TODO: return an actual `instanceof fs.Stat` somehow?
|
||||
|
||||
stats.isFile = function () {
|
||||
return (!entry.Attr.volume_id && !entry.Attr.directory);
|
||||
};
|
||||
stats.isDirectory = function () {
|
||||
return entry.Attr.directory;
|
||||
};
|
||||
stats.isBlockDevice = function () { return false; }
|
||||
stats.isCharacterDevice = function () { return false; }
|
||||
stats.isSymbolicLink = function () { return false; }
|
||||
stats.isFIFO = function () { return false; }
|
||||
stats.isSocket = function () { return false; }
|
||||
stats.size = entry.FileSize;
|
||||
stats.blksize = vol._sectorsPerCluster*vol._sectorSize;
|
||||
stats.blocks = Math.ceil(stats.size / stats.blksize) || 1;
|
||||
stats.nlink = 1;
|
||||
|
||||
stats.mode = S._I.RUSR | S._I.RGRP | S._I.ROTH;
|
||||
if (!entry.Attr.readonly) stats.mode |= S._I.WUSR | S._I.WGRP | S._I.WOTH;
|
||||
if (entry.Attr.directory) stats.mode |= S._I.FDIR;
|
||||
else if (!entry.Attr.volume_id) stats.mode |= S._I.FREG;
|
||||
// NOTE: discussion at https://github.com/natevw/fatfs/issues/7
|
||||
if (vol.opts.modmode === 0111) {
|
||||
// expose using executable bits, like Samba
|
||||
if (entry.Attr.archive) stats.mode |= S._I.XUSR;
|
||||
if (entry.Attr.system) stats.mode |= S._I.XGRP;
|
||||
if (entry.Attr.hidden) stats.mode |= S._I.XOTH;
|
||||
} else if (vol.opts.modmode === 07000) {
|
||||
// expose using setXid/sticky bits, like MKS
|
||||
if (entry.Attr.archive) stats.mode |= S._I.SVTX;
|
||||
if (entry.Attr.system) stats.mode |= S._I.SGID;
|
||||
if (entry.Attr.hidden) stats.mode |= S._I.SUID;
|
||||
}
|
||||
|
||||
stats.mode &= ~vol.opts.umask;
|
||||
stats.uid = vol.opts.uid;
|
||||
stats.gid = vol.opts.gid;
|
||||
|
||||
function extractDate(prefix) {
|
||||
var date = entry[prefix+'Date'],
|
||||
time = entry[prefix+'Time'] || {hours:0, minutes:0, seconds_2:0},
|
||||
secs = time.seconds_2 * 2,
|
||||
sect = entry[prefix+'TimeTenth'] || 0;
|
||||
if (sect > 100) {
|
||||
secs += 1;
|
||||
sect -= 100;
|
||||
}
|
||||
return new Date(date.year+1980, date.month-1, date.day, time.hours, time.minutes, secs, sect*100);
|
||||
}
|
||||
stats.atime = extractDate('LstAcc');
|
||||
stats.mtime = extractDate('Wrt');
|
||||
stats.ctime = extractDate('Crt');
|
||||
|
||||
entry = { // keep immutable copy (with only the fields we need)
|
||||
Attr: _.extend({},entry.Attr),
|
||||
};
|
||||
|
||||
return stats;
|
||||
};
|
||||
|
||||
dir.init = function (vol, dirInfo, cb) {
|
||||
var dirChain = dirInfo.chain,
|
||||
isRootDir = ('numSectors' in dirChain), // HACK: all others would be a clusterChain
|
||||
initialCluster = _.allocBuffer(dirChain.sectorSize*vol._sectorsPerCluster),
|
||||
entriesOffset = {bytes:0};
|
||||
initialCluster.fill(0);
|
||||
function writeEntry(name, clusterNum) {
|
||||
while (name.length < 8) name += " ";
|
||||
S.dirEntry.bytesFromValue(_updateEntry(vol, {
|
||||
Name: {filename:name, extension:" "},
|
||||
Attr: {directory:true}
|
||||
}, {firstCluster:clusterNum, _touch:true,ctime:true}), initialCluster, entriesOffset);
|
||||
}
|
||||
if (!isRootDir) {
|
||||
writeEntry(".", dirChain.firstCluster);
|
||||
writeEntry("..", dirInfo.parent.chain.firstCluster);
|
||||
};
|
||||
dirChain.writeToPosition(0, initialCluster, cb);
|
||||
};
|
||||
|
||||
dir.addFile = function (vol, dirChain, entryInfo, opts, cb) {
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts;
|
||||
opts = {};
|
||||
}
|
||||
var name = entryInfo.name,
|
||||
entries = [], mainEntry;
|
||||
entries.push(mainEntry = {
|
||||
Name: _.shortname(name),
|
||||
Attr: {directory:opts.dir||false},
|
||||
_name: name
|
||||
});
|
||||
if (1 || mainEntry.Name._lossy) { // HACK: always write long names until `._lossy` is more useful!
|
||||
var workaroundTessel427 = ('\uFFFF'.length !== 1);
|
||||
if (workaroundTessel427) throw Error("Your JS runtime does not have proper Unicode string support. (If Tessel, is your firmware up-to-date?)");
|
||||
|
||||
// name entries should be 0x0000-terminated and 0xFFFF-filled
|
||||
var S_lde_f = S.longDirEntry.fields,
|
||||
ENTRY_CHUNK_LEN = (S_lde_f.Name1.size + S_lde_f.Name2.size + S_lde_f.Name3.size)/2,
|
||||
partialLen = name.length % ENTRY_CHUNK_LEN,
|
||||
paddingNeeded = partialLen && (ENTRY_CHUNK_LEN - partialLen);
|
||||
if (paddingNeeded--) name += '\u0000';
|
||||
while (paddingNeeded-- > 0) name += '\uFFFF';
|
||||
// now fill in as many entries as it takes
|
||||
var off = 0,
|
||||
ord = 1;
|
||||
while (off < name.length) entries.push({
|
||||
Ord: ord++,
|
||||
Name1: name.slice(off, off+=S_lde_f.Name1.size/2),
|
||||
Attr_raw: S.longDirFlag,
|
||||
Chksum: null,
|
||||
Name2: name.slice(off, off+=S_lde_f.Name2.size/2),
|
||||
Name3: name.slice(off, off+=S_lde_f.Name3.size/2)
|
||||
});
|
||||
entries[entries.length - 1].Ord |= S.lastLongFlag;
|
||||
}
|
||||
|
||||
if (entryInfo.tail) {
|
||||
var name = mainEntry.Name.filename,
|
||||
suffix = '~'+entryInfo.tail,
|
||||
endIdx = name.indexOf(' '),
|
||||
sufIdx = (~endIdx) ? Math.min(endIdx, name.length-suffix.length) : name.length-suffix.length;
|
||||
if (sufIdx < 0) return cb(S.err.NAMETOOLONG()); // TODO: would EXIST be more correct?
|
||||
mainEntry.Name.filename = name.slice(0,sufIdx)+suffix+name.slice(sufIdx+suffix.length);
|
||||
_.log(_.log.DBG, "Shortname amended to:", mainEntry.Name);
|
||||
}
|
||||
|
||||
vol.allocateInFAT(dirChain.firstCluster || 2, function (e,fileCluster) {
|
||||
if (e) return cb(e);
|
||||
|
||||
var nameBuf = S.dirEntry.fields['Name'].bytesFromValue(mainEntry.Name),
|
||||
nameSum = _.checksumName(nameBuf);
|
||||
// TODO: finalize initial properties… (via `opts.mode` instead?)
|
||||
_updateEntry(vol, mainEntry, {firstCluster:fileCluster, size:0, ctime:true,_touch:true});
|
||||
mainEntry._pos = _.adjustedPos(vol, entryInfo.target, S.dirEntry.size*(entries.length-1));
|
||||
entries.slice(1).forEach(function (entry) {
|
||||
entry.Chksum = nameSum;
|
||||
});
|
||||
entries.reverse();
|
||||
if (entryInfo.lastEntry) entries.push({});
|
||||
|
||||
var entriesData = _.allocBuffer(S.dirEntry.size*entries.length),
|
||||
dataOffset = {bytes:0};
|
||||
entries.forEach(function (entry) {
|
||||
var entryType = ('Ord' in entry) ? S.longDirEntry : S.dirEntry;
|
||||
entryType.bytesFromValue(entry, entriesData, dataOffset);
|
||||
});
|
||||
|
||||
_.log(_.log.DBG, "Writing", entriesData.length, "byte directory entry", mainEntry, "into", dirChain.toJSON(), "at", entryInfo.target);
|
||||
dirChain.writeToPosition(entryInfo.target, entriesData, function (e) {
|
||||
// TODO: if we get error, what/should we clean up?
|
||||
if (e) cb(e);
|
||||
else cb(null, mainEntry, vol.chainForCluster(fileCluster, dirChain));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
dir.findInDirectory = function (vol, dirChain, name, opts, cb) {
|
||||
var matchName = name.toUpperCase(),
|
||||
tailName = (opts.prepareForCreate) ? _.shortname(name) : null,
|
||||
maxTail = 0;
|
||||
|
||||
function processNext(next) {
|
||||
next = next(function (e, d, entryPos) {
|
||||
if (e) cb(e);
|
||||
else if (!d) cb(S.err.NOENT(), {tail:maxTail, target:entryPos, lastEntry:true});
|
||||
else if (d._free) processNext(next); // TODO: look for long enough reusable run
|
||||
else if (d._name.toUpperCase() === matchName) return cb(null, d._full());
|
||||
else if (!opts.prepareForCreate) processNext(next);
|
||||
else {
|
||||
var dNum = 1,
|
||||
dName = d.Name.filename,
|
||||
dTail = dName.match(/(.*)~(\d+)/);
|
||||
if (dTail) {
|
||||
dNum = +dTail[2];
|
||||
dName = dTail[1];
|
||||
}
|
||||
if (tailName.extension === d.Name.extension &&
|
||||
tailName.filename.indexOf(dName) === 0)
|
||||
{
|
||||
maxTail = Math.max(dNum+1, maxTail);
|
||||
}
|
||||
processNext(next);
|
||||
}
|
||||
});
|
||||
}
|
||||
processNext(dir.iterator(dirChain, {includeFree:(0 && opts.prepareForCreate)}));
|
||||
};
|
||||
|
||||
dir.updateEntry = function (vol, entry, newStats, cb) {
|
||||
if (!entry._pos || !entry._pos.chain) throw Error("Entry source unknown!");
|
||||
|
||||
var entryPos = entry._pos,
|
||||
newEntry = _updateEntry(vol, entry, newStats),
|
||||
data = S.dirEntry.bytesFromValue(newEntry);
|
||||
_.log(_.log.DBG, "UPDATING ENTRY", newStats, newEntry, entryPos, data);
|
||||
// TODO: if write fails, then entry becomes corrupt!
|
||||
entryPos.chain.writeToPosition(entryPos, data, cb);
|
||||
};
|
||||
@@ -1,206 +0,0 @@
|
||||
var S = require("./structs.js"),
|
||||
_xok = require('xok');
|
||||
|
||||
// ponyfills for older node.js
|
||||
exports.allocBuffer = Buffer.alloc || function (len, val) {
|
||||
var b = Buffer(len);
|
||||
if (arguments.length > 1) b.fill(val);
|
||||
return b;
|
||||
};
|
||||
exports.bufferFrom = Buffer.from || function (arg0, arg1) {
|
||||
return (arguments.length > 1) ? Buffer(arg0, arg1) : Buffer(arg0);
|
||||
};
|
||||
|
||||
// flag for WORKAROUND: https://github.com/tessel/beta/issues/380
|
||||
exports.workaroundTessel380 = !Buffer.from && function () {
|
||||
var b = Buffer([0]),
|
||||
s = b.slice(0);
|
||||
return ((s[0] = 0xFF) !== b[0]);
|
||||
}();
|
||||
|
||||
|
||||
// WORKAROUND: https://github.com/tessel/beta/issues/433
|
||||
var oldslice;
|
||||
if (!Buffer.alloc && Buffer(5).slice(10).length < 0) oldslice = Buffer.prototype.slice, Buffer.prototype.slice = function (s, e) {
|
||||
if (s > this.length) s = this.length;
|
||||
// ~WORKAROUND: https://github.com/tessel/beta/issues/434
|
||||
return (arguments.length > 1) ? oldslice.call(this, s, e) : oldslice.call(this, s);
|
||||
}
|
||||
|
||||
exports.absoluteSteps = function (path) {
|
||||
var steps = [];
|
||||
path.split('/').forEach(function (str) {
|
||||
// NOTE: these should actually be fine, just wasteful…
|
||||
if (str === '..') steps.pop();
|
||||
else if (str && str !== '.') steps.push(str);
|
||||
});
|
||||
return steps.map(exports.longname);
|
||||
};
|
||||
|
||||
exports.absolutePath = function (path) {
|
||||
return '/'+exports.absoluteSteps(path).join('/');
|
||||
};
|
||||
|
||||
exports.parseFlags = function (flags) {
|
||||
// read, write, append, create, truncate, exclusive
|
||||
var info, _dir; // NOTE: there might be more clever ways to "parse", but…
|
||||
if (flags[0] === '\\') {
|
||||
// internal flag used internally to `fs.open` directories without `S.err.ISDIR()`
|
||||
flags = flags.slice(1);
|
||||
_dir = true;
|
||||
}
|
||||
switch (flags) {
|
||||
case 'r': info = {read:true, write:false, create:false}; break;
|
||||
case 'r+': info = {read:true, write:true, create:false}; break;
|
||||
case 'rs': info = {read:true, write:false, create:false, sync:true}; break;
|
||||
case 'rs+': info = {read:true, write:true, create:false, sync:true}; break;
|
||||
case 'w': info = {read:false, write:true, create:true, truncate:true}; break;
|
||||
case 'wx': info = {read:false, write:true, create:true, exclusive:true}; break;
|
||||
case 'w+': info = {read:true, write:true, create:true, truncate:true}; break;
|
||||
case 'wx+': info = {read:true, write:true, create:true, exclusive:true}; break;
|
||||
case 'a': info = {read:false, write:true, create:true, append:true}; break;
|
||||
case 'ax': info = {read:false, write:true, create:true, append:true, exclusive:true}; break;
|
||||
case 'a+': info = {read:true, write:true, create:true, append:true}; break;
|
||||
case 'ax+': info = {read:true, write:true, create:true, append:true, exclusive:true}; break;
|
||||
default: throw Error("Unknown mode: "+flags); // TODO: throw as `S.err.INVAL`
|
||||
}
|
||||
if (info.sync) throw Error("Mode not implemented."); // TODO: what would this require of us?
|
||||
if (_dir) info._openDir = true;
|
||||
return info;
|
||||
};
|
||||
|
||||
|
||||
// TODO: these are great candidates for special test coverage!
|
||||
var _snInvalid = /[^A-Z0-9$%'-_@~`!(){}^#&.]/g; // NOTE: '.' is not valid but we split it away
|
||||
exports.shortname = function (name) {
|
||||
var lossy = false;
|
||||
// TODO: support preservation of case for otherwise non-lossy name!
|
||||
name = name.toUpperCase().replace(/ /g, '').replace(/^\.+/, '');
|
||||
name = name.replace(_snInvalid, function () {
|
||||
lossy = true;
|
||||
return '_';
|
||||
});
|
||||
|
||||
var parts = name.split('.'),
|
||||
basis3 = parts.pop(),
|
||||
basis8 = parts.join('');
|
||||
if (!parts.length) {
|
||||
basis8 = basis3;
|
||||
basis3 = ' ';
|
||||
}
|
||||
if (basis8.length > 8) {
|
||||
basis8 = basis8.slice(0,8);
|
||||
// NOTE: technically, spec's "lossy conversion" flag is NOT set by excess length.
|
||||
// But since lossy conversion and truncated names both need a numeric tail…
|
||||
lossy = true;
|
||||
} else while (basis8.length < 8) basis8 += ' ';
|
||||
if (basis3.length > 3) {
|
||||
basis3 = basis3.slice(0,3);
|
||||
lossy = true;
|
||||
} else while (basis3.length < 3) basis3 += ' ';
|
||||
|
||||
return {filename:basis8, extension:basis3, _lossy:lossy};
|
||||
return {basis:[basis8,basis3], lossy:lossy};
|
||||
};
|
||||
//shortname("autoexec.bat") => {basis:['AUTOEXEC','BAT'],lossy:false}
|
||||
//shortname("autoexecutable.batch") => {basis:['AUTOEXEC','BAT'],lossy:true}
|
||||
// TODO: OS X stores `shortname("._.Trashes")` as ['~1', 'TRA'] — should we?
|
||||
|
||||
var _lnInvalid = /[^a-zA-Z0-9$%'-_@~`!(){}^#&.+,;=[\] ]/g;
|
||||
exports.longname = function (name) {
|
||||
name = name.trim().replace(/\.+$/, '').replace(_lnInvalid, function (c) {
|
||||
if (c.length > 1) throw Error("Internal problem: unexpected match length!");
|
||||
if (c.charCodeAt(0) > 127) return c;
|
||||
else throw Error("Invalid character "+JSON.stringify(c)+" in name.");
|
||||
lossy = true;
|
||||
return '_';
|
||||
});
|
||||
if (name.length > 255) throw Error("Name is too long.");
|
||||
return name;
|
||||
};
|
||||
|
||||
function nameChkSum(sum, c) {
|
||||
return ((sum & 1) ? 0x80 : 0) + (sum >>> 1) + c & 0xFF;
|
||||
}
|
||||
|
||||
// WORKAROUND: https://github.com/tessel/beta/issues/335
|
||||
function reduceBuffer(buf, start, end, fn, res) {
|
||||
// NOTE: does not handle missing `res` like Array.prototype.reduce would
|
||||
for (var i = start; i < end; ++i) {
|
||||
res = fn(res, buf[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
exports.checksumName = function (buf,off) {
|
||||
off || (off = 0);
|
||||
var len = S.dirEntry.fields['Name'].size;
|
||||
return reduceBuffer(buf, off, off+len, nameChkSum, 0);
|
||||
};
|
||||
|
||||
|
||||
/* comparing C rounding trick from FAT spec with Math.ceil
|
||||
function tryBoth(d) {
|
||||
var a = ((D.RootEntCnt * 32) + (D.BytsPerSec - 1)) / D.BytsPerSec >>> 0,
|
||||
b = Math.ceil((D.RootEntCnt * 32) / D.BytsPerSec);
|
||||
if (b !== a) console.log("try", b, a, (b === a) ? '' : '*');
|
||||
return (b === a);
|
||||
}
|
||||
// BytsPerSec — "may take on only the following values: 512, 1024, 2048 or 4096"
|
||||
[512, 1024, 2048, 4096].forEach(function (bps) {
|
||||
// RootEntCnt — "a count that when multiplied by 32 results in an even multiple of BPB_BytsPerSec"
|
||||
for (var evenMultiplier = 0; evenMultiplier < 1024*1024*16; evenMultiplier += 2) {
|
||||
var rec = (bps * evenMultiplier) / 32;
|
||||
tryBoth({RootEntCnt:rec, BytsPerSec:bps});
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
exports.fmtHex = function (n, ff) {
|
||||
return (1+ff+n).toString(16).slice(1);
|
||||
};
|
||||
|
||||
exports.delayedCall = function (fn) {
|
||||
if (!fn) throw Error("No function provided!"); // debug aid
|
||||
var ctx = this,
|
||||
args = Array.prototype.slice.call(arguments, 1);
|
||||
setImmediate(function () {
|
||||
fn.apply(ctx, args);
|
||||
});
|
||||
};
|
||||
|
||||
exports.adjustedPos = function (vol, pos, bytes) {
|
||||
var _pos = {
|
||||
chain: pos.chain,
|
||||
sector: pos.sector,
|
||||
offset: pos.offset + bytes
|
||||
}, secSize = vol._sectorSize;
|
||||
while (_pos.offset >= secSize) {
|
||||
_pos.sector += 1;
|
||||
_pos.offset -= secSize;
|
||||
}
|
||||
return _pos;
|
||||
};
|
||||
|
||||
exports.extend = _xok;
|
||||
|
||||
var _prevDbg = Date.now(),
|
||||
_thresh = 50;
|
||||
|
||||
function log(level) {
|
||||
if (level < log.level) return;
|
||||
|
||||
var now = Date.now(),
|
||||
diff = now - _prevDbg;
|
||||
arguments[0] = ((diff < _thresh) ? " " : '') + diff.toFixed(0) + "ms";
|
||||
console.log.apply(console, arguments);
|
||||
_prevDbg = now;
|
||||
}
|
||||
log.DBG = -4;
|
||||
log.INFO = -3;
|
||||
log.WARN = -2;
|
||||
log.ERR = -1;
|
||||
|
||||
log.level = log.WARN;
|
||||
|
||||
exports.log = log;
|
||||
@@ -1,25 +0,0 @@
|
||||
var fs = require('fs');
|
||||
|
||||
exports.createDriverSync = function (path, opts) {
|
||||
opts || (opts = {});
|
||||
|
||||
var secSize = 512,
|
||||
ro = opts.readOnly || false,
|
||||
fd = fs.openSync(path, (ro) ? 'r' : 'r+'),
|
||||
s = fs.fstatSync(fd);
|
||||
|
||||
return {
|
||||
sectorSize: secSize,
|
||||
numSectors: s.size / secSize,
|
||||
readSectors: function (i, dest, cb) {
|
||||
fs.read(fd, dest, 0, dest.length, i*secSize, function (e,n,d) {
|
||||
cb(e,d);
|
||||
});
|
||||
},
|
||||
writeSectors: (ro) ? null : function (i, data, cb) {
|
||||
fs.write(fd, data, 0, data.length, i*secSize, function (e) {
|
||||
cb(e);
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -1,771 +0,0 @@
|
||||
var events = require('events'),
|
||||
streams = require('stream'),
|
||||
fifolock = require('fifolock'),
|
||||
S = require('./structs.js'),
|
||||
_ = require('./helpers.js')
|
||||
|
||||
//_.log.level = _.log.DBG;
|
||||
|
||||
exports.createFileSystem = function (volume, opts, cb) {
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts
|
||||
opts = null
|
||||
}
|
||||
opts = _.extend(
|
||||
{
|
||||
// c.f. https://www.kernel.org/doc/Documentation/filesystems/vfat.txt
|
||||
ro: false,
|
||||
noatime: true,
|
||||
modmode: 0111, // or `07000`
|
||||
umask: 'umask' in process ? process.umask() : 0022,
|
||||
uid: 'getuid' in process ? process.getuid() : 0,
|
||||
gid: 'getgid' in process ? process.getgid() : 0,
|
||||
},
|
||||
opts
|
||||
)
|
||||
if (!volume.writeSectors) opts.ro = true
|
||||
if (opts.ro) opts.noatime = true // natch
|
||||
|
||||
var fs = new events.EventEmitter(),
|
||||
vol = null,
|
||||
dir = require('./dir.js'),
|
||||
c = require('./chains.js'),
|
||||
q = fifolock()
|
||||
|
||||
var GROUP =
|
||||
_.log.level < _.log.INFO
|
||||
? q.TRANSACTION_WRAPPER.bind({
|
||||
postAcquire: function (proceed) {
|
||||
_.log(_.log.DBG, '=== Starting GROUP ===')
|
||||
proceed()
|
||||
},
|
||||
preRelease: function (finish) {
|
||||
_.log(_.log.DBG, '=== Finishing GROUP ===')
|
||||
finish()
|
||||
},
|
||||
})
|
||||
: q.TRANSACTION_WRAPPER
|
||||
|
||||
q.acquire(function (unlock) {
|
||||
// because of this, callers can start before 'ready'
|
||||
var d = _.allocBuffer(volume.sectorSize)
|
||||
volume.readSectors(0, d, function (e) {
|
||||
if (e) fs.emit('error', e)
|
||||
else {
|
||||
try {
|
||||
init(d)
|
||||
} catch (e) {
|
||||
fs.emit('error', e)
|
||||
unlock()
|
||||
return
|
||||
}
|
||||
fs.emit('ready')
|
||||
}
|
||||
unlock()
|
||||
})
|
||||
})
|
||||
|
||||
if (cb) fs.on('error', cb).on('ready', cb.bind(null, null))
|
||||
|
||||
function init(bootSector) {
|
||||
vol = require('./vol.js').init(volume, opts, bootSector)
|
||||
fs._dirIterator = dir.iterator.bind(dir)
|
||||
|
||||
var entryInfoByPath = {},
|
||||
baseEntry = {
|
||||
_refs: 0,
|
||||
_record: function () {
|
||||
entryInfoByPath[this.path] = this
|
||||
if (this.parent) this.parent.retain()
|
||||
return this
|
||||
},
|
||||
retain: function () {
|
||||
if (!this._refs) this._record()
|
||||
this._refs += 1
|
||||
return this
|
||||
},
|
||||
release: function () {
|
||||
this._refs -= 1
|
||||
if (!this._refs) this._rescind()
|
||||
},
|
||||
_rescind: function () {
|
||||
if (this.parent) this.parent.release()
|
||||
delete entryInfoByPath[this.path]
|
||||
},
|
||||
}
|
||||
fs._createSharedEntry = function (path, entry, chain, parent) {
|
||||
return _.extend(Object.create(baseEntry), {
|
||||
_refs: 0, // WORKAROUND: https://github.com/tessel/beta/issues/455
|
||||
path: path,
|
||||
entry: entry,
|
||||
chain: chain,
|
||||
parent: parent,
|
||||
}).retain()
|
||||
}
|
||||
fs._createSharedEntry('/', { Attr: { directory: true } }, vol.rootDirectoryChain)
|
||||
fs._sharedEntryForSteps = function (steps, opts, cb) {
|
||||
// NOTE: may `cb` before returning!
|
||||
var path = steps.join('/') || '/',
|
||||
name = steps.pop(), // n.b.
|
||||
info = entryInfoByPath[path]
|
||||
if (info) cb(null, info.retain())
|
||||
else
|
||||
fs._sharedEntryForSteps(steps, {}, function (e, parentInfo) {
|
||||
// n.b. `steps` don't include `name`
|
||||
if (e) cb(e)
|
||||
else if (!parentInfo.entry.Attr.directory) cb(S.err.NOTDIR())
|
||||
else
|
||||
dir.findInDirectory(vol, parentInfo.chain, name, opts, function (e, entry) {
|
||||
if (e && !opts.prepareForCreate) cb(e)
|
||||
else if (e) cb(e, { missingChild: _.extend(entry, { name: name }), parent: parentInfo })
|
||||
else cb(null, fs._createSharedEntry(path, entry, vol.chainForCluster(entry._firstCluster), parentInfo))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fs._updateEntry = dir.updateEntry.bind(dir, vol)
|
||||
fs._makeStat = dir.makeStat.bind(dir, vol)
|
||||
fs._addFile = dir.addFile.bind(dir, vol)
|
||||
fs._initDir = dir.init.bind(dir, vol)
|
||||
}
|
||||
|
||||
/**** ---- CORE API ---- ****/
|
||||
|
||||
// NOTE: we really don't share namespace, but avoid first three anyway…
|
||||
var fileDescriptors = [null, null, null]
|
||||
|
||||
fs.open = function (path, flags, mode, cb, _n_) {
|
||||
if (typeof mode === 'function') {
|
||||
_n_ = cb
|
||||
cb = mode
|
||||
mode = 0666
|
||||
}
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = { flags: null, entry: null, chain: null, pos: 0 },
|
||||
f = _.parseFlags(flags)
|
||||
if (vol.opts.ro && (f.write || f.create || f.truncate)) return _.delayedCall(cb, S.err.ROFS())
|
||||
else _fd.flags = f
|
||||
|
||||
fs._sharedEntryForSteps(_.absoluteSteps(path), { prepareForCreate: f.create }, function (e, info) {
|
||||
if (e && !(e.code === 'NOENT' && f.create && info)) cb(e)
|
||||
else if (e)
|
||||
fs._addFile(info.parent.chain, info.missingChild, { dir: f._openDir }, function (e, newEntry, newChain) {
|
||||
if (e) cb(e)
|
||||
else finish(fs._createSharedEntry(_.absolutePath(path), newEntry, newChain, info.parent))
|
||||
})
|
||||
else if (info && f.exclusive) cb(S.err.EXIST())
|
||||
else if (info.entry.Attr.directory && !f._openDir) cb(S.err.ISDIR())
|
||||
else if (f.write && info.entry.Attr.readonly) cb(S.err.ACCES())
|
||||
else finish(info)
|
||||
function finish(fileInfo) {
|
||||
var fd = fileDescriptors.push(_fd) - 1
|
||||
_fd.info = fileInfo
|
||||
_fd.entry = fileInfo.entry
|
||||
_fd.chain = fileInfo.chain
|
||||
if (f.append) _fd.pos = _fd.entry._size
|
||||
if (f._openDir) _fd.chain.cacheAdvice = 'WILLNEED'
|
||||
if (f.truncate && _fd.entry._size)
|
||||
fs.ftruncate(
|
||||
fd,
|
||||
0,
|
||||
function (e) {
|
||||
cb(e, fd)
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
else _.delayedCall(cb, null, fd) // (delay in case fs._sharedEntryForSteps all cached!)
|
||||
}
|
||||
})
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.fstat = function (fd, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.read) _.delayedCall(cb, S.err.BADF())
|
||||
else _.delayedCall(cb, null, fs._makeStat(_fd.entry))
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.futimes = function (fd, atime, mtime, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) _.delayedCall(cb, S.err.BADF())
|
||||
// NOTE: ctime would get touched on POSIX; but we map that to create time!
|
||||
else fs._updateEntry(_fd.entry, { atime: atime || true, mtime: mtime || true }, cb)
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.fchmod = function (fd, mode, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) _.delayedCall(cb, S.err.BADF())
|
||||
else {
|
||||
mode &= S._I._chmoddable
|
||||
if (_fd.entry.Attr.directory) mode |= S._I.FDIR
|
||||
else if (!_fd.entry.Attr.volume_id) mode |= S._I.FREG
|
||||
fs._updateEntry(_fd.entry, { mode: mode }, cb)
|
||||
}
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.read = function (fd, buf, off, len, pos, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.read) return _.delayedCall(cb, S.err.BADF())
|
||||
|
||||
var _pos = pos === null ? _fd.pos : pos,
|
||||
_len = Math.min(len, _fd.entry._size - _pos),
|
||||
_buf = buf.slice(off, off + _len)
|
||||
_fd.chain.readFromPosition(_pos, _buf, function (e, bytes, slice) {
|
||||
if (_.workaroundTessel380) _buf.copy(buf, off) // WORKAROUND: https://github.com/tessel/beta/issues/380
|
||||
_fd.pos = _pos + bytes
|
||||
if (e || vol.opts.noatime) finish(e)
|
||||
else fs._updateEntry(_fd.entry, { atime: true }, finish)
|
||||
function finish(e) {
|
||||
cb(e, bytes, buf)
|
||||
}
|
||||
})
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs._readdir = function (fd, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.read) return _.delayedCall(cb, S.err.BADF())
|
||||
|
||||
var entryNames = [],
|
||||
getNextEntry = fs._dirIterator(_fd.chain)
|
||||
function processNext() {
|
||||
getNextEntry(function (e, d) {
|
||||
if (e) cb(e)
|
||||
else if (!d && !entryNames.length)
|
||||
cb(null, entryNames) // WORKAROUND: https://github.com/tessel/beta/issues/435
|
||||
else if (!d)
|
||||
cb(null, entryNames.sort()) // NOTE: sort not required, but… [simplifies tests for starters!]
|
||||
else {
|
||||
if (d._name !== '.' && d._name !== '..') entryNames.push(d._name)
|
||||
processNext()
|
||||
}
|
||||
})
|
||||
}
|
||||
processNext()
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs._mkdir = function (fd, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) _.delayedCall(cb, S.err.BADF())
|
||||
else fs._initDir(_fd.info, cb)
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.write = function (fd, buf, off, len, pos, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) return _.delayedCall(cb, S.err.BADF())
|
||||
|
||||
var _pos = pos === null || _fd.flags.append ? _fd.pos : pos,
|
||||
_buf = buf.slice(off, off + len)
|
||||
if (_pos > _fd.entry._size) {
|
||||
// TODO: handle huge jumps by zeroing clusters individually?
|
||||
var padLen = _pos - _fd.entry._size,
|
||||
padBuf = _.allocBuffer(padLen + _buf.length)
|
||||
padBuf.fill(0x00, 0, padLen)
|
||||
_buf.copy(padBuf, padLen)
|
||||
_pos = _fd.entry._size
|
||||
_buf = padBuf
|
||||
}
|
||||
_fd.chain.writeToPosition(_pos, _buf, function (e) {
|
||||
_fd.pos = _pos + len
|
||||
var newSize = Math.max(_fd.entry._size, _fd.pos),
|
||||
newInfo = { size: newSize, _touch: true }
|
||||
fs._updateEntry(_fd.entry, newInfo, function (ee) {
|
||||
cb(e || ee, len, buf)
|
||||
})
|
||||
})
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
fs.ftruncate = function (fd, len, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) return _.delayedCall(cb, S.err.BADF())
|
||||
|
||||
var newStats = { size: len, _touch: true }
|
||||
// NOTE: we order operations for best state in case of only partial success
|
||||
if (len === _fd.entry._size) _.delayedCall(cb)
|
||||
else if (len < _fd.entry._size)
|
||||
fs._updateEntry(_fd.entry, newStats, function (e) {
|
||||
if (e) cb(e)
|
||||
else _fd.chain.truncate(Math.ceil(len / _fd.chain.sectorSize), cb)
|
||||
})
|
||||
// TODO: handle huge file expansions without as much memory pressure
|
||||
else
|
||||
_fd.chain.writeToPosition(_fd.entry._size, _.allocBuffer(len - _fd.entry._size, 0x00), function (e) {
|
||||
if (e) cb(e)
|
||||
else fs._updateEntry(_fd.entry, newStats, cb)
|
||||
})
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
// 'NORMAL', 'SEQUENTIAL', 'RANDOM', 'WILLNEED', 'DONTNEED', 'NOREUSE'
|
||||
fs._fadviseSync = function (fd, off, len, advice) {
|
||||
if (off !== 0 || len !== 0) throw Error('Cache advise can currently be given only for whole file!')
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd) throw S.err.BADF()
|
||||
else _fd.chain.cacheAdvice = advice
|
||||
}
|
||||
|
||||
fs.fsync = function (fd, cb) {
|
||||
// NOTE: we'll need to flush write cache here once we have one…
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd) _.delayedCall(cb, S.err.BADF())
|
||||
else _.delayedCall(cb)
|
||||
}
|
||||
|
||||
fs.close = function (fd, cb) {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd) _.delayedCall(cb, S.err.BADF())
|
||||
else setTimeout(_fd.info.release.bind(_fd.info), 500), _.delayedCall(cb, (fileDescriptors[fd] = null))
|
||||
}
|
||||
|
||||
/* STREAM WRAPPERS */
|
||||
|
||||
var workaroundTessel436
|
||||
try {
|
||||
new require('stream').Readable({ encoding: 'utf8' })
|
||||
new streams.Readable({ encoding: 'utf8' })
|
||||
} catch (e) {
|
||||
workaroundTessel436 = true
|
||||
}
|
||||
|
||||
function _createStream(StreamType, path, opts) {
|
||||
// [NOT REALLY A] WORKAROUND: https://github.com/tessel/beta/issues/436
|
||||
if (workaroundTessel436 && 'encoding' in opts) {
|
||||
console.warn('Tessel does not currently support encoding option for Readable streams, discarding!')
|
||||
delete opts.encoding
|
||||
}
|
||||
|
||||
var fd = opts.fd !== null ? opts.fd : '_opening_',
|
||||
pos = opts.start,
|
||||
stream = new StreamType(opts)
|
||||
|
||||
if (fd === '_opening_')
|
||||
fs.open(path, opts.flags, opts.mode, function (e, _fd) {
|
||||
if (e) {
|
||||
fd = '_open_error_'
|
||||
stream.emit('error', e)
|
||||
} else {
|
||||
fd = _fd
|
||||
fs._fadviseSync(fd, 0, 0, 'SEQUENTIAL')
|
||||
stream.emit('open', fd)
|
||||
}
|
||||
})
|
||||
|
||||
function autoClose(tombstone) {
|
||||
// NOTE: assumes caller will clear `fd`
|
||||
if (opts.autoClose)
|
||||
fs.close(fd, function (e) {
|
||||
if (e) stream.emit('error', e)
|
||||
else stream.emit('close')
|
||||
})
|
||||
fd = tombstone
|
||||
}
|
||||
|
||||
if (StreamType === streams.Readable) {
|
||||
stream._read = function (n) {
|
||||
var buf
|
||||
// TODO: optimize to fetch at least a full sector regardless of `n`…
|
||||
n = Math.min(n, opts.end - pos)
|
||||
if (fd === '_opening_')
|
||||
stream.once('open', function () {
|
||||
stream._read(n)
|
||||
})
|
||||
else if (pos > opts.end) stream.push(null)
|
||||
else if (n > 0)
|
||||
(buf = _.allocBuffer(n)),
|
||||
fs.read(fd, buf, 0, n, pos, function (e, n, d) {
|
||||
if (e) {
|
||||
autoClose('_read_error_')
|
||||
stream.emit('error', e)
|
||||
} else stream.push(n ? d.slice(0, n) : null)
|
||||
}),
|
||||
(pos += n)
|
||||
else stream.push(null)
|
||||
}
|
||||
|
||||
stream.once('end', function () {
|
||||
autoClose('_ended_')
|
||||
})
|
||||
} else if (StreamType === streams.Writable) {
|
||||
stream.bytesWritten = 0
|
||||
|
||||
stream._write = function (data, _enc, cb) {
|
||||
if (fd === '_opening_')
|
||||
stream.once('open', function () {
|
||||
stream._write(data, null, cb)
|
||||
})
|
||||
else
|
||||
fs.write(fd, data, 0, data.length, pos, function (e, n) {
|
||||
if (e) {
|
||||
autoClose('_write_error_')
|
||||
cb(e)
|
||||
} else {
|
||||
stream.bytesWritten += n
|
||||
cb()
|
||||
}
|
||||
}),
|
||||
(pos += data.length)
|
||||
}
|
||||
|
||||
stream.once('finish', function () {
|
||||
autoClose('_finished_')
|
||||
})
|
||||
}
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
fs.createReadStream = function (path, opts) {
|
||||
return _createStream(
|
||||
streams.Readable,
|
||||
path,
|
||||
_.extend(
|
||||
{
|
||||
start: 0,
|
||||
end: Infinity,
|
||||
flags: 'r',
|
||||
mode: 0666,
|
||||
encoding: null,
|
||||
fd: null, // ??? see https://github.com/joyent/node/issues/7708
|
||||
autoClose: true,
|
||||
},
|
||||
opts
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fs.createWriteStream = function (path, opts) {
|
||||
return _createStream(
|
||||
streams.Writable,
|
||||
path,
|
||||
_.extend(
|
||||
{
|
||||
start: 0,
|
||||
flags: 'w',
|
||||
mode: 0666,
|
||||
//encoding: null, // see https://github.com/joyent/node/issues/7710
|
||||
fd: null, // ??? see https://github.com/joyent/node/issues/7708
|
||||
autoClose: true,
|
||||
},
|
||||
opts,
|
||||
{ decodeStrings: true, objectMode: false }
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/* PATH WRAPPERS (albeit the only public interface for some folder operations) */
|
||||
|
||||
function _fdOperation(path, opts, fn, cb) {
|
||||
cb = GROUP(cb, function () {
|
||||
opts.advice || (opts.advice = 'NORMAL')
|
||||
fs.open(
|
||||
path,
|
||||
opts.flag,
|
||||
function (e, fd) {
|
||||
if (e) cb(e)
|
||||
else
|
||||
fs._fadviseSync(fd, 0, 0, opts.advice),
|
||||
fn(fd, function () {
|
||||
var ctx = this,
|
||||
args = arguments
|
||||
fs.close(
|
||||
fd,
|
||||
function (closeErr) {
|
||||
cb.apply(ctx, args)
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
})
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fs.stat = fs.lstat = function (path, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: 'r' },
|
||||
function (fd, cb) {
|
||||
fs.fstat(fd, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.exists = function (path, cb) {
|
||||
fs.stat(path, function (err) {
|
||||
cb(err ? false : true)
|
||||
})
|
||||
}
|
||||
|
||||
fs.readFile = function (path, opts, cb) {
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts
|
||||
opts = {}
|
||||
}
|
||||
opts.flag || (opts.flag = 'r')
|
||||
opts.advice || (opts.advice = 'NOREUSE')
|
||||
_fdOperation(
|
||||
path,
|
||||
opts,
|
||||
function (fd, cb) {
|
||||
fs.fstat(
|
||||
fd,
|
||||
function (e, stat) {
|
||||
if (e) return cb(e)
|
||||
else {
|
||||
var buffer = _.allocBuffer(stat.size)
|
||||
fs.read(
|
||||
fd,
|
||||
buffer,
|
||||
0,
|
||||
buffer.length,
|
||||
null,
|
||||
function (e) {
|
||||
if (e) cb(e)
|
||||
else cb(null, opts.encoding ? buffer.toString(opts.encoding) : buffer)
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
}
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.writeFile = function (path, data, opts, cb) {
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts
|
||||
opts = {}
|
||||
}
|
||||
opts.flag || (opts.flag = 'w')
|
||||
opts.advice || (opts.advice = 'NOREUSE')
|
||||
_fdOperation(
|
||||
path,
|
||||
opts,
|
||||
function (fd, cb) {
|
||||
if (typeof data === 'string') data = _.bufferFrom(data, opts.encoding || 'utf8')
|
||||
fs.write(
|
||||
fd,
|
||||
data,
|
||||
0,
|
||||
data.length,
|
||||
null,
|
||||
function (e) {
|
||||
cb(e)
|
||||
},
|
||||
'_nested_'
|
||||
)
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.appendFile = function (path, data, opts, cb) {
|
||||
if (typeof opts === 'function') {
|
||||
cb = opts
|
||||
opts = {}
|
||||
}
|
||||
opts.flag || (opts.flag = 'a')
|
||||
fs.writeFile(path, data, opts, cb)
|
||||
}
|
||||
|
||||
fs.truncate = function (path, len, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: 'r+' },
|
||||
function (fd, cb) {
|
||||
fs.ftruncate(fd, len, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.readdir = function (path, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\r' },
|
||||
function (fd, cb) {
|
||||
fs._readdir(fd, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.mkdir = function (path, mode, cb) {
|
||||
if (typeof mode === 'function') {
|
||||
cb = mode
|
||||
mode = 0777
|
||||
}
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\wx' },
|
||||
function (fd, cb) {
|
||||
fs._mkdir(fd, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.utimes = function (path, atime, mtime, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: 'r+' },
|
||||
function (fd, cb) {
|
||||
fs.futimes(fd, atime, mtime, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.chmod = fs.lchmod = function (path, mode, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\r+' },
|
||||
function (fd, cb) {
|
||||
fs.fchmod(fd, mode, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.chown = fs.lchown = function (path, uid, gid, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\r+' },
|
||||
function (fd, cb) {
|
||||
fs.fchown(fd, uid, gid, cb, '_nested_')
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
/* STUBS */
|
||||
|
||||
fs.link = function (src, dst, cb) {
|
||||
// NOTE: theoretically we _could_ do hard links [with untracked `stat.nlink` count…]
|
||||
_.delayedCall(cb, S.err.NOSYS())
|
||||
}
|
||||
|
||||
fs.symlink = function (src, dst, type, cb) {
|
||||
if (typeof type === 'function') {
|
||||
cb = type
|
||||
type = null
|
||||
}
|
||||
_.delayedCall(cb, S.err.NOSYS())
|
||||
}
|
||||
|
||||
fs.readlink = function (path, cb) {
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\r' },
|
||||
function (fd, cb) {
|
||||
// the named file is *never* a symbolic link…
|
||||
// NOTE: we still use _fdOperation for catching e.g. NOENT/NOTDIR errors…
|
||||
cb(S.err.INVAL())
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.realpath = function (path, cache, cb) {
|
||||
if (typeof cache === 'function') {
|
||||
cb = cache
|
||||
cache = null
|
||||
}
|
||||
if (cache)
|
||||
_.delayedCall(cb, S.err.NOSYS()) // TODO: what would be involved here?
|
||||
else
|
||||
_fdOperation(
|
||||
path,
|
||||
{ flag: '\\r' },
|
||||
function (fd, cb) {
|
||||
cb(null, _.absolutePath(path))
|
||||
},
|
||||
cb
|
||||
)
|
||||
}
|
||||
|
||||
fs.fchown = function (fd, uid, gid, cb, _n_) {
|
||||
cb = GROUP(
|
||||
cb,
|
||||
function () {
|
||||
var _fd = fileDescriptors[fd]
|
||||
if (!_fd || !_fd.flags.write) _.delayedCall(cb, S.err.BADF())
|
||||
else _.delayedCall(cb, S.err.NOSYS())
|
||||
},
|
||||
_n_ === '_nested_'
|
||||
)
|
||||
}
|
||||
|
||||
// See https://github.com/natevw/fatfs/pull/31/files
|
||||
fs.createLabel = function (name, cb) {
|
||||
fs.open(name, 'a', (e, _fd) => {
|
||||
if (e) {
|
||||
cb(e)
|
||||
} else {
|
||||
let fd = fileDescriptors[_fd]
|
||||
fd.entry.Attr.volume_id = true
|
||||
fd.entry.FstClusLO = 0
|
||||
fd.entry.FstClusHI = 0
|
||||
|
||||
fs._updateEntry(fd.entry, {}, cb)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return fs
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#! /bin/sh
|
||||
|
||||
|
||||
# NOTE: this only works on OS X
|
||||
# TODO: Linux could use mkfs stuff without attach?
|
||||
|
||||
# ./make_sample.sh /tmp/fat16.img FAT16
|
||||
|
||||
FILE=$1
|
||||
: ${FILE:?must be provided as first argument}
|
||||
|
||||
TYPE=$2
|
||||
: ${TYPE:?must be provided as second argument}
|
||||
|
||||
SIZE=$3 # in MB
|
||||
case "$TYPE" in
|
||||
"FAT12") : ${SIZE:=1} # <128
|
||||
;;
|
||||
"FAT16") : ${SIZE:=5} # >4
|
||||
;;
|
||||
"FAT32") : ${SIZE:=33} # >32
|
||||
;;
|
||||
*) : ${SIZE:=1} # e.g. ExFAT
|
||||
esac
|
||||
|
||||
echo Making $TYPE of $SIZE MB at $FILE
|
||||
|
||||
if [[ "$TYPE" = FAT* ]]
|
||||
then
|
||||
TYPE="MS-DOS $TYPE"
|
||||
fi
|
||||
|
||||
dd if=/dev/zero of=$FILE bs=1048576 count=$SIZE
|
||||
DEV=`hdiutil attach $FILE -nomount`
|
||||
diskutil eraseVolume "$TYPE" "FATFS TEST" $DEV
|
||||
hdiutil detach $DEV
|
||||
@@ -1,34 +0,0 @@
|
||||
{
|
||||
"private": false,
|
||||
"name": "@vates/fatfs",
|
||||
"version": "0.11.1",
|
||||
"description": "fs implementation on top of raw FAT16/FAT32 block source",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public"
|
||||
},
|
||||
"repository": {
|
||||
"directory": "@vates/fatfs",
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"keywords": [
|
||||
"filesystem",
|
||||
"fs",
|
||||
"fat",
|
||||
"fat32",
|
||||
"fat16"
|
||||
],
|
||||
"author": "Vates SAS",
|
||||
"license": "Apache-2.0",
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/fatfs",
|
||||
"dependencies": {
|
||||
"fifolock": "^1.0.0",
|
||||
"struct-fu": "^1.2.1",
|
||||
"xok": "^1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
}
|
||||
@@ -1,255 +0,0 @@
|
||||
// see http://staff.washington.edu/dittrich/misc/fatgen103.pdf
|
||||
// and http://www.cse.scu.edu/~tschwarz/COEN252_09/Lectures/FAT.html
|
||||
|
||||
var _ = require('struct-fu'),
|
||||
__ = require("./helpers.js");
|
||||
|
||||
var bootBase = _.struct([
|
||||
_.byte('jmpBoot', 3),
|
||||
_.char('OEMName', 8),
|
||||
_.uint16le('BytsPerSec'),
|
||||
_.uint8('SecPerClus'),
|
||||
_.uint16le('ResvdSecCnt'), // Rsvd in table, but Resvd in calcs…
|
||||
_.uint8('NumFATs'),
|
||||
_.uint16le('RootEntCnt'),
|
||||
_.uint16le('TotSec16'),
|
||||
_.uint8('Media'),
|
||||
_.uint16le('FATSz16'),
|
||||
_.uint16le('SecPerTrk'),
|
||||
_.uint16le('NumHeads'),
|
||||
_.uint32le('HiddSec'),
|
||||
_.uint32le('TotSec32')
|
||||
]);
|
||||
|
||||
var bootInfo = _.struct([
|
||||
_.uint8('DrvNum'),
|
||||
_.uint8('Reserved1'),
|
||||
_.uint8('BootSig'),
|
||||
_.uint32le('VolID'),
|
||||
_.char('VolLab', 11),
|
||||
_.char('FilSysType', 8)
|
||||
]);
|
||||
|
||||
exports.boot16 = _.struct([
|
||||
bootBase,
|
||||
bootInfo
|
||||
]);
|
||||
|
||||
exports.boot32 = _.struct([
|
||||
bootBase,
|
||||
_.uint32le('FATSz32'),
|
||||
_.struct('ExtFlags', [
|
||||
_.ubit('NumActiveFAT', 4),
|
||||
_.ubit('_reserved1', 3),
|
||||
_.bool('MirroredFAT'),
|
||||
_.ubit('_reserved2', 8)
|
||||
].reverse()),
|
||||
_.struct('FSVer', [
|
||||
_.uint8('Major'),
|
||||
_.uint8('Minor')
|
||||
]),
|
||||
_.uint32le('RootClus'),
|
||||
_.uint16le('FSInfo'),
|
||||
_.uint16le('BkBootSec'),
|
||||
_.byte('Reserved', 12),
|
||||
bootInfo
|
||||
]);
|
||||
|
||||
|
||||
var _time = _.struct([
|
||||
_.ubit('hours',5),
|
||||
_.ubit('minutes',6),
|
||||
_.ubit('seconds_2',5)
|
||||
]), time = {
|
||||
valueFromBytes: function (buf, off) {
|
||||
off || (off = {bytes:0});
|
||||
|
||||
var _buf = __.bufferFrom([buf[off.bytes+1], buf[off.bytes+0]]),
|
||||
val = _time.valueFromBytes(_buf);
|
||||
off.bytes += this.size;
|
||||
return val;
|
||||
},
|
||||
bytesFromValue: function (val, buf, off) {
|
||||
val || (val = {hours:0, minutes:0, seconds_2:0});
|
||||
buf || (buf = __.allocBuffer(this.size));
|
||||
off || (off = {bytes:0});
|
||||
|
||||
var _buf = _time.bytesFromValue(val);
|
||||
buf[off.bytes+1] = _buf[0];
|
||||
buf[off.bytes+0] = _buf[1];
|
||||
off.bytes += this.size;
|
||||
return buf;
|
||||
},
|
||||
size: _time.size
|
||||
};
|
||||
|
||||
var _date = _.struct([
|
||||
_.ubit('year',7),
|
||||
_.ubit('month',4),
|
||||
_.ubit('day',5)
|
||||
]), date = {
|
||||
valueFromBytes: function (buf, off) {
|
||||
off || (off = {bytes:0});
|
||||
|
||||
var _buf = __.bufferFrom([buf[off.bytes+1], buf[off.bytes+0]]),
|
||||
val = _date.valueFromBytes(_buf);
|
||||
off.bytes += this.size;
|
||||
return val;
|
||||
},
|
||||
bytesFromValue: function (val, buf, off) {
|
||||
val || (val = {year:0, month:0, day:0});
|
||||
buf || (buf = __.allocBuffer(this.size));
|
||||
off || (off = {bytes:0});
|
||||
|
||||
var _buf = _date.bytesFromValue(val);
|
||||
buf[off.bytes+1] = _buf[0];
|
||||
buf[off.bytes+0] = _buf[1];
|
||||
off.bytes += this.size;
|
||||
return buf;
|
||||
},
|
||||
size: _date.size
|
||||
};
|
||||
|
||||
exports.dirEntry = _.struct([
|
||||
_.struct('Name', [
|
||||
_.char('filename',8),
|
||||
_.char('extension',3)
|
||||
]),
|
||||
_.struct('Attr', [
|
||||
_.bool('readonly'),
|
||||
_.bool('hidden'),
|
||||
_.bool('system'),
|
||||
_.bool('volume_id'),
|
||||
_.bool('directory'),
|
||||
_.bool('archive'),
|
||||
_.ubit('reserved', 2)
|
||||
].reverse()),
|
||||
_.byte('NTRes', 1),
|
||||
_.uint8('CrtTimeTenth'),
|
||||
_.struct('CrtTime', [time]),
|
||||
_.struct('CrtDate', [date]),
|
||||
_.struct('LstAccDate', [date]),
|
||||
_.uint16le('FstClusHI'),
|
||||
_.struct('WrtTime', [time]),
|
||||
_.struct('WrtDate', [date]),
|
||||
_.uint16le('FstClusLO'),
|
||||
_.uint32le('FileSize')
|
||||
]);
|
||||
exports.entryDoneFlag = 0x00;
|
||||
exports.entryFreeFlag = 0xE5;
|
||||
exports.entryIsE5Flag = 0x05;
|
||||
|
||||
exports.dirEntry_simple = _.struct([
|
||||
_.struct('Name', [
|
||||
_.char('filename',8),
|
||||
_.char('extension',3)
|
||||
]),
|
||||
_.padTo(exports.dirEntry.size)
|
||||
/*
|
||||
_.uint8('Attr_raw'),
|
||||
_.byte('NTRes', 1),
|
||||
_.byte('Crt_raw', 1+2+2),
|
||||
_.byte('Lst_raw', 2),
|
||||
_.uint16le('FstClusHI'),
|
||||
_.byte('Wrt_raw', 2+2),
|
||||
_.uint16le('FstClusLO'),
|
||||
_.uint32le('FileSize')
|
||||
*/
|
||||
]);
|
||||
|
||||
|
||||
exports.lastLongFlag = 0x40;
|
||||
exports.longDirFlag = 0x0F;
|
||||
exports.longDirEntry = _.struct([
|
||||
_.uint8('Ord'),
|
||||
_.char16le('Name1', 10),
|
||||
_.uint8('Attr_raw'),
|
||||
_.uint8('Type'),
|
||||
_.uint8('Chksum'),
|
||||
_.char16le('Name2', 12),
|
||||
_.uint16le('FstClusLO'),
|
||||
_.char16le('Name3', 4)
|
||||
]);
|
||||
|
||||
if (exports.longDirEntry.size !== exports.dirEntry.size) throw Error("Structs ain't right!");
|
||||
|
||||
exports.fatField = {
|
||||
'fat12': _.struct('Status', [
|
||||
_.ubit('field0bc', 8),
|
||||
_.ubit('field1c', 4),
|
||||
_.ubit('field0a', 4),
|
||||
_.ubit('field1ab', 8),
|
||||
]),
|
||||
'fat16': _.uint16le('Status'),
|
||||
'fat32': _.uint32le('Status') // more properly this 4 bits reserved + uint28le
|
||||
};
|
||||
|
||||
exports.fatPrefix = {
|
||||
'fat12': 0xF00,
|
||||
'fat16': 0xFF00,
|
||||
'fat32': 0x0FFFFF00
|
||||
};
|
||||
|
||||
exports.fatStat = {
|
||||
free: 0x00,
|
||||
_undef: 0x01,
|
||||
rsvMin: 0xF0,
|
||||
bad: 0xF7,
|
||||
eofMin: 0xF8,
|
||||
eof: 0xFF
|
||||
};
|
||||
|
||||
exports._I = {
|
||||
RUSR: 0400,
|
||||
WUSR: 0200,
|
||||
XUSR: 0100,
|
||||
|
||||
RGRP: 0040,
|
||||
WGRP: 0020,
|
||||
XGRP: 0010,
|
||||
|
||||
ROTH: 0004,
|
||||
WOTH: 0002,
|
||||
XOTH: 0001,
|
||||
|
||||
SUID: 04000,
|
||||
SGID: 02000,
|
||||
SVTX: 01000,
|
||||
|
||||
FDIR: 040000,
|
||||
FREG: 0100000,
|
||||
};
|
||||
|
||||
exports._I.RWXU = exports._I.RUSR | exports._I.WUSR | exports._I.XUSR;
|
||||
exports._I.RWXG = exports._I.RGRP | exports._I.WGRP | exports._I.XGRP;
|
||||
exports._I.RWXO = exports._I.ROTH | exports._I.WOTH | exports._I.XOTH;
|
||||
exports._I._sss = exports._I.SUID | exports._I.SGID | exports._I.SVTX;
|
||||
exports._I._chmoddable = exports._I.RWXU | exports._I.RWXG | exports._I.RWXO | exports._I._sss;
|
||||
|
||||
var _errors = {
|
||||
IO: "Input/output error",
|
||||
NOENT: "No such file or directory",
|
||||
INVAL: "Invalid argument",
|
||||
EXIST: "File exists",
|
||||
NAMETOOLONG: "Filename too long",
|
||||
NOSPC: "No space left on device",
|
||||
NOSYS: "Function not supported",
|
||||
ROFS: "ROFLCopter file system",
|
||||
NOTDIR: "Not a directory",
|
||||
BADF: "Bad file descriptor",
|
||||
EXIST: "File exists",
|
||||
ISDIR: "Is a directory",
|
||||
ACCES: "Permission denied",
|
||||
NOSYS: "Function not implemented",
|
||||
_TODO: "Not implemented yet!"
|
||||
};
|
||||
|
||||
exports.err = {};
|
||||
Object.keys(_errors).forEach(function (sym) {
|
||||
var msg = _errors[sym];
|
||||
exports.err[sym] = function () {
|
||||
var e = new Error(msg);
|
||||
e.code = sym;
|
||||
return e;
|
||||
};
|
||||
});
|
||||
@@ -1,338 +0,0 @@
|
||||
// can be called from CLI with image type, absolute path to an image, or, required as a module
|
||||
|
||||
var _ = require("./helpers.js");
|
||||
|
||||
var type = process.argv[2];
|
||||
if (module.parent) exports.startTests = startTests;
|
||||
else if (!type) throw "Usage: node test [FAT12|FAT16|FAT32|ExFAT|…|/path/to/image]";
|
||||
else if (type[0] === '/') testWithImage(type);
|
||||
else {
|
||||
var uniq = Math.random().toString(36).slice(2),
|
||||
IMG = require('os').tmpdir()+"fatfs-test-"+uniq+".img";
|
||||
require('child_process').exec("./make_sample.sh "+JSON.stringify(IMG)+" "+JSON.stringify(type), function (e,out,err) {
|
||||
if (e) throw e;
|
||||
console.warn(err.toString());
|
||||
//console.log(out.toString());
|
||||
testWithImage(IMG);
|
||||
//console.log("open", IMG);
|
||||
//return;
|
||||
require('fs').unlink(IMG, function (e) {
|
||||
if (e) console.warn("Error cleaning up test image", e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function testWithImage(imagePath) {
|
||||
var vol = require("./img_volume.js").createDriverSync(imagePath);
|
||||
startTests(vol);
|
||||
}
|
||||
|
||||
|
||||
function startTests(vol, waitTime) {
|
||||
var fatfs = require("./"),
|
||||
fs = fatfs.createFileSystem(vol, {umask:0020, uid:99, gid:42});
|
||||
|
||||
waitTime || (waitTime = 0.5e3);
|
||||
|
||||
[
|
||||
'mkdir','readdir',
|
||||
//'rename','unlink','rmdir',
|
||||
'close','open','fsync',
|
||||
'ftruncate','truncate',
|
||||
'write','read','readFile','writeFile', 'appendFile',
|
||||
|
||||
'chown','lchown','fchown',
|
||||
'chmod','lchmod', 'fchmod',
|
||||
'utimes','futimes',
|
||||
'stat','lstat','fstat','exists',
|
||||
'link','symlink','readlink','realpath',
|
||||
|
||||
//'watchFile','unwatchFile','watch'
|
||||
].forEach(function (method) { assert(method in fs, "fs."+method+" has implementation."); });
|
||||
|
||||
var BASE_DIR = "/fat_test-"+Math.random().toFixed(20).slice(2),
|
||||
FILENAME = "Simple File.txt",
|
||||
TEXTDATA = "Hello world!";
|
||||
|
||||
var isReady = false;
|
||||
fs.on('ready', function () {
|
||||
assert(isReady = true, "Driver is ready.");
|
||||
}).on('error', function (e) {
|
||||
assert(e, "If fs driver fires 'error' event, it should include error object.");
|
||||
assert(false, "…but driver should not error when initializing in our case.");
|
||||
});
|
||||
setTimeout(function () {
|
||||
assert(isReady, "Driver fired ready event in timely fashion.");
|
||||
}, waitTime);
|
||||
|
||||
fs.readdir("/", function (e,files) {
|
||||
assert(isReady, "Method completed after 'ready' event.");
|
||||
assert(!e, "No error reading root directory.");
|
||||
assert(Array.isArray(files), "Got a list of files: "+files);
|
||||
});
|
||||
|
||||
fs.mkdir(BASE_DIR, function (e) {
|
||||
assert(!e, "No error from fs.mkdir");
|
||||
fs.readdir(BASE_DIR, function (e,arr) {
|
||||
assert(!e, "No error from fs.readdir");
|
||||
assert(arr.length === 0 , "No files in BASE_DIR yet.");
|
||||
});
|
||||
var file = [BASE_DIR,FILENAME].join('/');
|
||||
fs.writeFile(file, TEXTDATA, function (e) {
|
||||
assert(!e, "No error from fs.writeFile");
|
||||
startStreamTests();
|
||||
fs.realpath(file, function (e,path) {
|
||||
assert(!e, "No error from basic fs.realpath call.");
|
||||
assert(path === file, "We already had the real path.");
|
||||
});
|
||||
fs.realpath([BASE_DIR,".","garbage",".","..",FILENAME].join('/'), function (e,path) {
|
||||
assert(!e, "No error from fluffy fs.realpath call.");
|
||||
assert(path === file, "Fixed fluffy path matches normal one.");
|
||||
});
|
||||
fs.realpath([BASE_DIR,"non","existent","path"].join('/'), function (e) {
|
||||
assert(e, "Expected error calling fs.realpath on nonexistent file.");
|
||||
});
|
||||
|
||||
fs.readdir(BASE_DIR, function (e, arr) {
|
||||
assert(!e, "Still no error from fs.readdir");
|
||||
assert(arr.length === 2, "Test directory contains two files."); // (ours + startStreamTests's)
|
||||
assert(arr[0] === FILENAME, "Filename is correct.");
|
||||
|
||||
fs.stat(file, function (e,d) {
|
||||
assert(!e, "No error from fs.stat");
|
||||
assert(d.isFile() === true, "Result is a file…");
|
||||
assert(d.isDirectory() === false, "…and not a directory.");
|
||||
assert(d.size === Buffer.byteLength(TEXTDATA), "Size matches length of content written.");
|
||||
});
|
||||
fs.exists(file, function (bool) {
|
||||
assert(bool === true, "File exists.");
|
||||
});
|
||||
fs.readFile(file, {encoding:'utf8'}, function (e, d) {
|
||||
assert(!e, "No error from fs.readFile");
|
||||
assert(d === TEXTDATA, "Data matches what was written.");
|
||||
});
|
||||
// now, overwrite the same file and make sure that goes well too
|
||||
fs.writeFile(file, _.bufferFrom([0x42]), function (e) {
|
||||
assert(!e, "Still no error from fs.writeFile");
|
||||
fs.readdir(BASE_DIR, function (e, arr) {
|
||||
assert(!e, "No error from fs.readdir");
|
||||
assert(arr.length === 2, "Test directory still contains two files.");
|
||||
assert(arr[0] === FILENAME, "Filename still correct.");
|
||||
fs.stat(file, function (e,d) {
|
||||
assert(!e, "Still no error from fs.stat");
|
||||
assert(d.isFile() === true, "Result is still a file…");
|
||||
assert(d.isDirectory() === false, "…and not a directory.");
|
||||
assert(d.size === 1, "Size matches length of now-truncated content.");
|
||||
});
|
||||
fs.readFile(file, function (e, d) {
|
||||
assert(!e, "Still no error from fs.readFile");
|
||||
assert(Buffer.isBuffer(d), "Result without encoding is a buffer.");
|
||||
assert(d.length === 1, "Buffer is correct size.");
|
||||
assert(d[0] === 0x42, "Buffer content is correct.");
|
||||
|
||||
fs.truncate(file, 1025, function (e) {
|
||||
assert(!e, "No error from fs.truncate (extending)");
|
||||
fs.readFile(file, function (e, d) {
|
||||
assert(!e, "Still no error from fs.readFile after extension");
|
||||
assert(d.length === 1025, "Read after extension is correct size.");
|
||||
assert(d[0] === 0x42, "First byte is still correct.");
|
||||
var allZeroes = true;
|
||||
for (var i = 1, len = d.length; i < len; ++i) if (d[i] !== 0) allZeroes = false;
|
||||
assert(allZeroes, "Extended portion of file is zero-filled.");
|
||||
|
||||
fs.truncate(file, 3, function (e) {
|
||||
assert(!e, "No error from fs.truncate (shortening)");
|
||||
fs.readFile(file, function (e, d) {
|
||||
assert(!e, "Still no error from fs.readFile after shortening.");
|
||||
assert(d.length === 3, "Read after shortening is correct size.");
|
||||
assert(d[0] === 0x42, "First byte is still correct.");
|
||||
assert(d[1] === 0x00, "Second byte is still correct.");
|
||||
assert(d[2] === 0x00, "Third byte is still correct.");
|
||||
proceedWithMoreTests();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function proceedWithMoreTests() {
|
||||
var fd;
|
||||
fs.open(file, 'r', function (e, _fd) {
|
||||
assert(!e, "No error from fs.open.");
|
||||
fd = _fd;
|
||||
});
|
||||
var was = "\u0042\u0000\u0000",
|
||||
str = "abc";
|
||||
fs.appendFile(file, str, function (e) {
|
||||
assert(!e, "No error from fs.appendFile.");
|
||||
assert(fd, "File descriptor opened before appendFile called.");
|
||||
var buf = _.allocBuffer(str.length);
|
||||
fs.read(fd, buf, 0, buf.length, was.length, function (e,n,d) {
|
||||
assert(!e, "No error from fs.read after append.");
|
||||
assert(n === str.length, "All appended data was readable.");
|
||||
assert(d === buf, "Buffer returned from fs.read matched what was passed in.");
|
||||
assert(d.toString() === str, "Correct data found where expected.");
|
||||
});
|
||||
});
|
||||
fs.readFile(file, {encoding:'ascii'}, function (e,d) {
|
||||
assert(!e, "No error from fs.appendFile.");
|
||||
assert(d.length === 6, "Read is correct size after append.");
|
||||
assert(d === was+str, "Read string matches what was written and then appended.");
|
||||
|
||||
fs.open(file, 'a', function (e, fd2) {
|
||||
assert(!e, "No error from second open of file.");
|
||||
var str2 = "zyx",
|
||||
buf2 = _.allocBuffer(str2.length+2);
|
||||
buf2.write(str2, 1);
|
||||
fs.write(fd2, buf2, 1, buf2.length-2, was.length, function (e,n,d) {
|
||||
assert(!e, "No error from appending fs.write.");
|
||||
assert(n === buf2.length-2, "Wrote proper amount from buffer.");
|
||||
assert(d === buf2, "Returned original buffer.");
|
||||
|
||||
buf2.fill(0);
|
||||
buf2[0] = 0xFF;
|
||||
fs.read(fd, buf2, 1, buf2.length-1, was.length, function (e,n,d) {
|
||||
assert(!e, "No error from twice-appended read.");
|
||||
assert(n === buf2.length-1, "Read proper amount into buffer.");
|
||||
assert(buf2[0] === 0xFF, "Read left first byte in buffer properly alone.");
|
||||
assert(d.slice(1).toString() === (str+str2).slice(0, buf2.length-1), "Data was appended, not written at position.");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
var F = [BASE_DIR,"Manually inspect from time to time, please!.txt"].join('/'),
|
||||
S = 512,
|
||||
N = 16,
|
||||
b = _.allocBuffer(S*N);
|
||||
for (var i = 0; i < N; ++i) b.slice(S*i, S*i+S).fill(i.toString(16).charCodeAt(0));
|
||||
fs.writeFile(F, b, function (e) {
|
||||
assert(!e, "No error from fs.writeFile with counting blocks.");
|
||||
fs.readFile(F, function (e,d) {
|
||||
assert(!e, "No error from fs.readFile with counting blocks.");
|
||||
assert(d.length === b.length, "Readback is correct size");
|
||||
var matched = true;
|
||||
for (var i = 0; i < S*N; ++i) if (b[i] !== d[i]) matched = false;
|
||||
assert(matched, "Readback matches write byte-for-byte");
|
||||
});
|
||||
});
|
||||
|
||||
fs.stat(F, function (e,d) {
|
||||
assert(!e, "No error from fs.stat on counting blocks file.");
|
||||
assert(d.atime instanceof Date && !isNaN(d.atime.getTime()), "Access time is a valid date.");
|
||||
assert(d.mtime instanceof Date && !isNaN(d.mtime.getTime()), "Modify time is a valid date.");
|
||||
assert(d.ctime instanceof Date && !isNaN(d.ctime.getTime()), "Change^WCreate time is a valid date.");
|
||||
var tf = d.ctime.getTime(),
|
||||
ct = Date.now();
|
||||
assert(tf - 2*waitTime < ct && ct < tf + 2*waitTime, "Create time is within ± two `waitTime`s of now.");
|
||||
fs.utimes(F, new Date(2009, 7-1, 2), null, function (e) {
|
||||
assert(!e, "No error from fs.utimes.");
|
||||
fs.stat(F, function (e,d2) {
|
||||
assert(!e, "No fs.stat error after touching timestamps.");
|
||||
assert(+d.ctime === +d2.ctime, "Create time not changed by fs.utimes");
|
||||
assert(d2.atime.toString().indexOf("Jul 02 2009 00:00:00") === 4, "Access time set correctly");
|
||||
var tf = d2.mtime.getTime(),
|
||||
ct = Date.now();
|
||||
assert(tf-2e3 < ct && ct < tf+2e3+waitTime, "Modify time is within ± a few seconds of now.");
|
||||
|
||||
// NOTE: due to serialization, this can check results of the `fs.chmod` below, too!
|
||||
assert(!(d2.mode & 0100), "Archive bit is now unset.");
|
||||
assert(!(d2.mode & 0222), "Writable perms are unset.");
|
||||
assert(d2.mode & 0100000, "Regular file bit is set.");
|
||||
});
|
||||
});
|
||||
|
||||
assert(d.uid === 99, "Desired UID applied.");
|
||||
assert(d.gid === 42, "Desired GID applied.");
|
||||
assert(d.mode & 0100, "Archive bit is set.");
|
||||
assert(d.mode & 0200, "Writable perm is set for user.");
|
||||
assert((d.mode & 0022) === 0002, "Writable perm is only masked out for group.");
|
||||
fs.chmod(F, 0422, function (e) {
|
||||
assert(!e, "No error from fs.chmod.");
|
||||
});
|
||||
});
|
||||
|
||||
fs.chown(F, 99, 256, function (e) {
|
||||
assert(e && e.code === 'NOSYS', "Expected error from fs.fchown.");
|
||||
});
|
||||
}
|
||||
|
||||
function startStreamTests() {
|
||||
var file2 = [BASE_DIR,FILENAME+"2"].join('/'),
|
||||
outStream = fs.createWriteStream(file2);
|
||||
var outStreamOpened = false;
|
||||
outStream.on('open', function (fd) {
|
||||
outStreamOpened = true;
|
||||
assert(typeof fd === 'number', "Got file descriptor on fs.createWriteStream open.");
|
||||
}).on('error', function (e) {
|
||||
assert(e, "If fs.createWriteStream fires 'error' event, it should include error object.");
|
||||
assert(false, "But, fs.createWriteStream should not error during these tests.");
|
||||
});
|
||||
setTimeout(function () {
|
||||
assert(outStreamOpened, "outStream fired 'open' event in a timely fashion.");
|
||||
}, waitTime);
|
||||
var TEXT_MOD = TEXTDATA.toLowerCase()+"\n",
|
||||
NUM_REPS = (waitTime <= 1e3) ? 1024 : 16;
|
||||
outStream.write(TEXT_MOD, 'utf16le');
|
||||
outStream.write("Ο καλύτερος χρόνος να φυτευτεί ένα \ud83c\udf31 είναι δέκα έτη πριν.", 'utf16le');
|
||||
outStream.write("La vez del segundo mejor ahora está.\n", 'utf16le');
|
||||
for (var i = 0; i < NUM_REPS; ++i) outStream.write("123456789\n", 'ascii');
|
||||
outStream.write("JavaScript how do they work\n", 'utf16le');
|
||||
outStream.write("The end, almost.\n", 'utf16le');
|
||||
outStream.end(TEXTDATA, 'utf16le');
|
||||
var outStreamFinished = false;
|
||||
outStream.on('finish', function () {
|
||||
outStreamFinished = true;
|
||||
|
||||
var inStream = fs.createReadStream(file2, {start:NUM_REPS*10, encoding:'utf16le', autoClose:false}),
|
||||
gotData = false, gotEOF = false, inStreamFD = null;
|
||||
inStream.on('open', function (fd) {
|
||||
assert(typeof fd === 'number', "Got file descriptor on fs.createReadStream open.");
|
||||
inStreamFD = fd;
|
||||
});
|
||||
inStream.on('data', function (d) {
|
||||
gotData = true;
|
||||
assert(typeof d === 'string', "Data returned as string.");
|
||||
assert(d.slice(d.length-TEXTDATA.length) === TEXTDATA, "End of file matches what was written.");
|
||||
});
|
||||
inStream.on('end', function () {
|
||||
gotEOF = true;
|
||||
|
||||
var len = Buffer.byteLength(TEXT_MOD, 'utf16le'),
|
||||
buf = _.allocBuffer(len);
|
||||
fs.fsync(inStreamFD, function (e) {
|
||||
assert(!e, "No error from proper fsync.");
|
||||
});
|
||||
fs.fsync('garbage', function (e) {
|
||||
assert(e, "Expected error from garbage fsync.");
|
||||
});
|
||||
fs.read(inStreamFD, buf, 0, len, 0, function (e,n,d) {
|
||||
assert(!e, "No error reading from beginning of inStream's file descriptor.");
|
||||
assert(n === len, "Read complete buffer at beginning of inStream's fd.");
|
||||
assert(d.toString('utf16le') === TEXT_MOD, "Data matches at beginning of inStream's fd.");
|
||||
fs.close(inStreamFD, function (e) {
|
||||
assert(!e, "No error closing inStream's fd.");
|
||||
});
|
||||
});
|
||||
});
|
||||
setTimeout(function () {
|
||||
assert(gotData, "inStream fired 'data' event in a timely fashion.");
|
||||
setTimeout(function () {
|
||||
assert(gotEOF, "inStream fired 'eof' event in a timely fashion.");
|
||||
}, waitTime);
|
||||
}, waitTime);
|
||||
});
|
||||
setTimeout(function () {
|
||||
assert(outStreamFinished, "outStream fired 'finish' event in a timely fashion.");
|
||||
}, 2*waitTime);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function assert(b,msg) { if (!msg) console.warn("no msg", Error().stack); if (!b) throw Error("Assertion failure. "+msg); else console.log(msg); }
|
||||
@@ -1,165 +0,0 @@
|
||||
var S = require("./structs.js"),
|
||||
c = require("./chains.js"),
|
||||
$ = require("./cache.js"),
|
||||
_ = require("./helpers.js");
|
||||
|
||||
exports.init = function (volume, opts, bootSector) {
|
||||
if (bootSector[510] !== 0x55 || bootSector[511] !== 0xAA) throw Error("Invalid volume signature!");
|
||||
|
||||
var isFAT16 = bootSector.readUInt16LE(S.boot16.fields['FATSz16'].offset),
|
||||
bootStruct = (isFAT16) ? S.boot16 : S.boot32,
|
||||
BS = bootStruct.valueFromBytes(bootSector);
|
||||
_.log(_.log.DBG, "Boot sector info:", BS);
|
||||
bootSector = null; // allow GC
|
||||
if (!BS.BytsPerSec) throw Error("This looks like an ExFAT volume! (unsupported)");
|
||||
else if (BS.BytsPerSec !== volume.sectorSize) throw Error("Sector size mismatch with FAT table.");
|
||||
|
||||
var FATSz = (isFAT16) ? BS.FATSz16 : BS.FATSz32,
|
||||
rootDirSectors = Math.ceil((BS.RootEntCnt * 32) / BS.BytsPerSec),
|
||||
firstDataSector = BS.ResvdSecCnt + (BS.NumFATs * FATSz) + rootDirSectors,
|
||||
totSec = (BS.TotSec16) ? BS.TotSec16 : BS.TotSec32,
|
||||
dataSec = totSec - firstDataSector,
|
||||
countofClusters = Math.floor(dataSec / BS.SecPerClus);
|
||||
// avoid corrupting sectors from other partitions or whatnot
|
||||
if (totSec > volume.numSectors) throw Error("Volume size mismatch!");
|
||||
|
||||
var fatType;
|
||||
if (countofClusters < 4085) {
|
||||
fatType = 'fat12';
|
||||
} else if (countofClusters < 65525) {
|
||||
fatType = 'fat16';
|
||||
} else {
|
||||
fatType = 'fat32';
|
||||
}
|
||||
|
||||
_.log(_.log.DBG, "rootDirSectors", rootDirSectors, "firstDataSector", firstDataSector, "countofClusters", countofClusters, "=>", fatType);
|
||||
|
||||
var vol = {};
|
||||
|
||||
vol.opts = opts;
|
||||
|
||||
vol._sectorSize = BS.BytsPerSec;
|
||||
vol._sectorsPerCluster = BS.SecPerClus;
|
||||
vol._firstSectorOfCluster = function (n) {
|
||||
return firstDataSector + (n-2)*vol._sectorsPerCluster;
|
||||
};
|
||||
|
||||
vol._makeCache = function () {
|
||||
return $.wrapDriver(volume);
|
||||
};
|
||||
|
||||
vol._readSectors = function (cache, secNum, dest, cb) {
|
||||
if (typeof dest === 'function') {
|
||||
cb = dest;
|
||||
dest = _.allocBuffer(vol._sectorSize);
|
||||
}
|
||||
_.log(_.log.DBG, "vol._readSectors", secNum, dest.length);
|
||||
if (secNum < volume.numSectors) cache.readSectors(secNum, dest, function (e) { cb(e, dest); });
|
||||
else throw Error("Invalid sector number!");
|
||||
};
|
||||
|
||||
vol._writeSectors = function (cache, secNum, data, cb) {
|
||||
_.log(_.log.DBG, "vol._writeSectors", secNum, data.length);
|
||||
// NOTE: these are internal assertions, public API will get proper `S.err`s
|
||||
if (data.length % volume.sectorSize) throw Error("Buffer length not a multiple of sector size");
|
||||
else if (opts.ro) throw Error("Read-only filesystem");
|
||||
else if (secNum < volume.numSectors) cache.writeSectors(secNum, data, cb);
|
||||
else throw Error("Invalid sector number!");
|
||||
};
|
||||
|
||||
function fatInfoForCluster(n) {
|
||||
var entryStruct = S.fatField[fatType],
|
||||
FATOffset = (fatType === 'fat12') ? Math.floor(n/2) * entryStruct.size : n * entryStruct.size,
|
||||
SecNum = BS.ResvdSecCnt + Math.floor(FATOffset / BS.BytsPerSec);
|
||||
EntOffset = FATOffset % BS.BytsPerSec;
|
||||
return {sector:SecNum-BS.ResvdSecCnt, offset:EntOffset, struct:entryStruct};
|
||||
}
|
||||
|
||||
// TODO: all this FAT manipulation is crazy inefficient! needs read caching *and* write caching
|
||||
// …the best place for cache might be in `volume` handler, though. add a `flush` method to that spec?
|
||||
// TODO: how should we handle redundant FATs? mirror every write? just ignore completely? copy-on-eject?
|
||||
|
||||
var fatChain = c.sectorChain(vol, BS.ResvdSecCnt, FATSz);
|
||||
fatChain.cacheAdvice = 'RANDOM';
|
||||
|
||||
vol.fetchFromFAT = function (clusterNum, cb) {
|
||||
var info = fatInfoForCluster(clusterNum);
|
||||
fatChain.readFromPosition(info, info.struct.size, function (e,n,d) {
|
||||
if (e) return cb(e);
|
||||
var status = info.struct.valueFromBytes(d), prefix;
|
||||
if (fatType === 'fat12') {
|
||||
if (clusterNum % 2) {
|
||||
status = (status.field1ab << 4) + status.field1c;
|
||||
} else {
|
||||
status = (status.field0a << 8) + status.field0bc;
|
||||
}
|
||||
}
|
||||
else if (fatType === 'fat32') {
|
||||
status &= 0x0FFFFFFF;
|
||||
}
|
||||
|
||||
var prefix = S.fatPrefix[fatType];
|
||||
if (status === S.fatStat.free) cb(null, 'free');
|
||||
else if (status === S.fatStat._undef) cb(null, '-invalid-');
|
||||
else if (status > prefix+S.fatStat.eofMin) cb(null, 'eof');
|
||||
else if (status === prefix+S.fatStat.bad) cb(null, 'bad');
|
||||
else if (status > prefix+S.fatStat.rsvMin) cb(null, 'reserved');
|
||||
else cb(null, status);
|
||||
});
|
||||
};
|
||||
|
||||
vol.storeToFAT = function (clusterNum, status, cb) {
|
||||
if (typeof status === 'string') {
|
||||
status = S.fatStat[status];
|
||||
status += S.fatPrefix[fatType];
|
||||
}
|
||||
var info = fatInfoForCluster(clusterNum);
|
||||
// TODO: technically fat32 needs to *preserve* the high 4 bits
|
||||
if (fatType === 'fat12') fatChain.readFromPosition(info, info.struct.size, function (e,n,d) {
|
||||
var value = info.struct.valueFromBytes(d);
|
||||
if (clusterNum % 2) {
|
||||
value.field1ab = status >>> 4;
|
||||
value.field1c = status & 0x0F;
|
||||
} else {
|
||||
value.field0a = status >>> 8;
|
||||
value.field0bc = status & 0xFF;
|
||||
}
|
||||
var entry = info.struct.bytesFromValue(value);
|
||||
fatChain.writeToPosition(info, entry, cb);
|
||||
}); else {
|
||||
var entry = info.struct.bytesFromValue(status);
|
||||
fatChain.writeToPosition(info, entry, cb);
|
||||
}
|
||||
};
|
||||
|
||||
vol.allocateInFAT = function (hint, cb) {
|
||||
if (typeof hint === 'function') {
|
||||
cb = hint;
|
||||
hint = 2; // TODO: cache a better starting point?
|
||||
}
|
||||
function searchForFreeCluster(num, cb) {
|
||||
if (num < countofClusters) vol.fetchFromFAT(num, function (e, status) {
|
||||
if (e) cb(e);
|
||||
else if (status === 'free') cb(null, num);
|
||||
else searchForFreeCluster(num+1, cb);
|
||||
}); else cb(S.err.NOSPC()); // TODO: try searching backwards from hint…
|
||||
}
|
||||
searchForFreeCluster(hint, function (e, clusterNum) {
|
||||
if (e) cb(e);
|
||||
else vol.storeToFAT(clusterNum, 'eof', cb.bind(null,null,clusterNum));
|
||||
});
|
||||
};
|
||||
|
||||
vol.rootDirectoryChain = (isFAT16) ?
|
||||
c.sectorChain(vol, firstDataSector - rootDirSectors, rootDirSectors) :
|
||||
c.clusterChain(vol, BS.RootClus);
|
||||
vol.rootDirectoryChain.cacheAdvice = 'WILLNEED';
|
||||
vol.chainForCluster = c.clusterChain.bind(c, vol);
|
||||
vol.chainFromJSON = function (d) {
|
||||
return ('numSectors' in d) ?
|
||||
c.sectorChain(vol, d.firstSector, d.numSectors) :
|
||||
c.clusterChain(vol, d.firstCluster);
|
||||
};
|
||||
|
||||
return vol;
|
||||
}
|
||||
@@ -13,7 +13,7 @@ await mount(handler, diskId, mountPoint)
|
||||
|
||||
### cli
|
||||
|
||||
From the install folder:
|
||||
From the install folder :
|
||||
|
||||
```
|
||||
cli.mjs <remoteUrl> <vhdPathInRemote> <mountPoint>
|
||||
@@ -25,25 +25,4 @@ After installing the package
|
||||
xo-fuse-vhd <remoteUrl> <vhdPathInRemote> <mountPoint>
|
||||
```
|
||||
|
||||
The `remoteUrl` can be found using the following command from the XOA CLI, and then picking the right `url` property:
|
||||
|
||||
`xo-server-db ls remote`
|
||||
|
||||
The `vhdPathInRemote` follows a file structure described in [VM backups](https://github.com/vatesfr/xen-orchestra/blob/master/%40xen-orchestra/backups/docs/VM%20backups/README.md).
|
||||
|
||||
## Restore a file from a VHD using `fuse-vhd` CLI
|
||||
|
||||
1. Mount a VHD to the Filesystem (see **Usage**).
|
||||
2. Verify the VHD is Correctly Mounted:
|
||||
|
||||
`mount | grep fuse`
|
||||
|
||||
3. Get the `START` and `SIZE` of the disk whose file you want to restore. Multiply `START` by 512 (block size) to get the offset value. (`mountedVhdPath` is the `mountPoint` value from earlier, followed by `/vhd0`, for example). Depending on the partition type, you may need to do some additional setup to discover the partitions.
|
||||
|
||||
`partx --bytes --output=NR,START,SIZE,NAME,UUID,TYPE --pairs <mountedVhdPath>`
|
||||
|
||||
4. Mount the disk. Depending on the partition type, you may need to add some additional options. The `norecovery` option is used for ext3/ext4/xfs file systems; otherwise, remove this option.
|
||||
|
||||
`mount --options=loop,ro,norecovery,sizelimit=<SIZE>,offset=<START*512> --source=<mountedVhdPath> --target=<diskMountPoint>`
|
||||
|
||||
5. Copy your files from the mounted partition, then unmount the partition.
|
||||
remoteUrl can be found by using cli in `@xen-orchestra/fs` , for example a local remote will have a url like `file:///path/to/remote/root`
|
||||
|
||||
@@ -29,7 +29,7 @@ await mount(handler, diskId, mountPoint)
|
||||
|
||||
### cli
|
||||
|
||||
From the install folder:
|
||||
From the install folder :
|
||||
|
||||
```
|
||||
cli.mjs <remoteUrl> <vhdPathInRemote> <mountPoint>
|
||||
@@ -41,28 +41,7 @@ After installing the package
|
||||
xo-fuse-vhd <remoteUrl> <vhdPathInRemote> <mountPoint>
|
||||
```
|
||||
|
||||
The `remoteUrl` can be found using the following command from the XOA CLI, and then picking the right `url` property:
|
||||
|
||||
`xo-server-db ls remote`
|
||||
|
||||
The `vhdPathInRemote` follows a file structure described in [VM backups](https://github.com/vatesfr/xen-orchestra/blob/master/%40xen-orchestra/backups/docs/VM%20backups/README.md).
|
||||
|
||||
## Restore a file from a VHD using `fuse-vhd` CLI
|
||||
|
||||
1. Mount a VHD to the Filesystem (see **Usage**).
|
||||
2. Verify the VHD is Correctly Mounted:
|
||||
|
||||
`mount | grep fuse`
|
||||
|
||||
3. Get the `START` and `SIZE` of the disk whose file you want to restore. Multiply `START` by 512 (block size) to get the offset value. (`mountedVhdPath` is the `mountPoint` value from earlier, followed by `/vhd0`, for example). Depending on the partition type, you may need to do some additional setup to discover the partitions.
|
||||
|
||||
`partx --bytes --output=NR,START,SIZE,NAME,UUID,TYPE --pairs <mountedVhdPath>`
|
||||
|
||||
4. Mount the disk. Depending on the partition type, you may need to add some additional options. The `norecovery` option is used for ext3/ext4/xfs file systems; otherwise, remove this option.
|
||||
|
||||
`mount --options=loop,ro,norecovery,sizelimit=<SIZE>,offset=<START*512> --source=<mountedVhdPath> --target=<diskMountPoint>`
|
||||
|
||||
5. Copy your files from the mounted partition, then unmount the partition.
|
||||
remoteUrl can be found by using cli in `@xen-orchestra/fs` , for example a local remote will have a url like `file:///path/to/remote/root`
|
||||
|
||||
## Contributions
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import Fuse from 'fuse-native'
|
||||
import LRU from 'lru-cache'
|
||||
import Fuse from 'fuse-native'
|
||||
import { createLogger } from '@xen-orchestra/log'
|
||||
import { VhdSynthetic } from 'vhd-lib'
|
||||
import { Disposable, fromCallback } from 'promise-toolbox'
|
||||
|
||||
const { warn } = createLogger('vates:fuse-vhd')
|
||||
|
||||
// build a stat object from https://github.com/fuse-friends/fuse-native/blob/master/test/fixtures/stat.js
|
||||
// build a s stat object from https://github.com/fuse-friends/fuse-native/blob/master/test/fixtures/stat.js
|
||||
const stat = st => ({
|
||||
mtime: st.mtime || new Date(),
|
||||
atime: st.atime || new Date(),
|
||||
@@ -20,109 +20,42 @@ const stat = st => ({
|
||||
export const mount = Disposable.factory(async function* mount(handler, diskPath, mountDir) {
|
||||
const vhd = yield VhdSynthetic.fromVhdChain(handler, diskPath)
|
||||
|
||||
const cache = new LRU({
|
||||
max: 16, // each cached block is 2MB in size
|
||||
})
|
||||
await vhd.readBlockAllocationTable()
|
||||
const { blockSize } = vhd.header
|
||||
|
||||
// A zeroed buffer returned for VHD blocks that are not present in the chain.
|
||||
// Never mutated — safe to share across all reads.
|
||||
const EMPTY_BLOCK = Buffer.alloc(blockSize, 0)
|
||||
|
||||
// --- coalescing block read queue ---
|
||||
//
|
||||
// FUSE read requests from NTFS-3g arrive in small chunks (typically 4 KiB)
|
||||
// while VHD blocks are 2 MiB. Reading blocks one at a time from the backing
|
||||
// store (CIFS/NFS/…) bounds libuv threadpool usage to 1 concurrent I/O call.
|
||||
//
|
||||
// Coalescing: all pending requests for the same blockId share a single
|
||||
// readBlock() call. When the block resolves, every waiter is fulfilled at once.
|
||||
//
|
||||
// Together these two properties prevent the FUSE-on-FUSE threadpool deadlock:
|
||||
// concurrent NTFS-3g reads no longer starve the VHD block reads they depend on.
|
||||
// In-memory block cache. Each VHD block is 2 MiB; 16 blocks = 32 MiB.
|
||||
// This is the primary tool against the 300× CIFS amplification: NTFS-3g
|
||||
// reads a 2 MiB block in ~512 sequential 4 KiB FUSE requests. Without a
|
||||
// cache, each request causes a full 2 MiB CIFS read. With the cache, only
|
||||
// the first request reads from CIFS; the rest are served from RAM.
|
||||
const blockCache = new LRU({ max: 64 })
|
||||
|
||||
const pendingBlocks = new Map() // blockId → Promise<Buffer>
|
||||
const readQueue = [] // { blockId, resolve, reject }[]
|
||||
let queueRunning = false
|
||||
|
||||
// log stats every 10 s while there is activity
|
||||
|
||||
function enqueueBlock(blockId) {
|
||||
// 1. serve from cache if available
|
||||
if (blockCache.has(blockId)) {
|
||||
return Promise.resolve(blockCache.get(blockId))
|
||||
}
|
||||
// 2. coalesce: join an existing in-flight read for this block
|
||||
if (pendingBlocks.has(blockId)) {
|
||||
return pendingBlocks.get(blockId)
|
||||
}
|
||||
const p = new Promise((resolve, reject) => {
|
||||
readQueue.push({ blockId, resolve, reject })
|
||||
})
|
||||
pendingBlocks.set(blockId, p)
|
||||
p.then(
|
||||
data => {
|
||||
pendingBlocks.delete(blockId)
|
||||
blockCache.set(blockId, data)
|
||||
},
|
||||
() => pendingBlocks.delete(blockId)
|
||||
)
|
||||
drainQueue()
|
||||
return p
|
||||
}
|
||||
|
||||
function drainQueue() {
|
||||
if (queueRunning) return
|
||||
queueRunning = true
|
||||
// async IIFE — errors are forwarded to individual reject() callbacks,
|
||||
// so the outer floating promise is always fulfilled
|
||||
;(async () => {
|
||||
while (readQueue.length > 0) {
|
||||
const { blockId, resolve, reject } = readQueue.shift()
|
||||
try {
|
||||
resolve((await vhd.readBlock(blockId)).data)
|
||||
} catch (err) {
|
||||
reject(err)
|
||||
}
|
||||
}
|
||||
queueRunning = false
|
||||
})()
|
||||
}
|
||||
|
||||
async function readData(buf, len, pos) {
|
||||
if (len === 0) return 0
|
||||
const startBlockId = Math.floor(pos / blockSize)
|
||||
// use (pos + len - 1) so an exact block boundary doesn't include the next block
|
||||
const endBlockId = Math.floor((pos + len - 1) / blockSize)
|
||||
|
||||
let copied = 0
|
||||
for (let blockId = startBlockId; blockId <= endBlockId; blockId++) {
|
||||
const data = vhd.containsBlock(blockId) ? await enqueueBlock(blockId) : EMPTY_BLOCK
|
||||
const offsetStart = blockId === startBlockId ? pos % blockSize : 0
|
||||
const offsetEnd = blockId === endBlockId ? ((pos + len - 1) % blockSize) + 1 : blockSize
|
||||
data.copy(buf, copied, offsetStart, offsetEnd)
|
||||
copied += offsetEnd - offsetStart
|
||||
}
|
||||
return copied
|
||||
}
|
||||
|
||||
const fuse = new Fuse(mountDir, {
|
||||
async readdir(path, cb) {
|
||||
if (path === '/') return cb(null, ['vhd0'])
|
||||
if (path === '/') {
|
||||
return cb(null, ['vhd0'])
|
||||
}
|
||||
cb(Fuse.ENOENT)
|
||||
},
|
||||
async getattr(path, cb) {
|
||||
if (path === '/') return cb(null, stat({ mode: 'dir', size: 4096 }))
|
||||
if (path === '/vhd0') return cb(null, stat({ mode: 'file', size: vhd.footer.currentSize }))
|
||||
if (path === '/') {
|
||||
return cb(
|
||||
null,
|
||||
stat({
|
||||
mode: 'dir',
|
||||
size: 4096,
|
||||
})
|
||||
)
|
||||
}
|
||||
if (path === '/vhd0') {
|
||||
return cb(
|
||||
null,
|
||||
stat({
|
||||
mode: 'file',
|
||||
size: vhd.footer.currentSize,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
cb(Fuse.ENOENT)
|
||||
},
|
||||
read(path, fd, buf, len, pos, cb) {
|
||||
if (path === '/vhd0') {
|
||||
return readData(buf, len, pos).then(cb, error => {
|
||||
return vhd.readRawData(pos, len, cache, buf).then(cb, error => {
|
||||
warn('read error', { path, len, pos, error })
|
||||
cb(Fuse.EIO)
|
||||
})
|
||||
@@ -131,9 +64,7 @@ export const mount = Disposable.factory(async function* mount(handler, diskPath,
|
||||
},
|
||||
})
|
||||
return new Disposable(
|
||||
() => {
|
||||
return fromCallback(cb => fuse.unmount(cb))
|
||||
},
|
||||
() => fromCallback(cb => fuse.unmount(cb)),
|
||||
fromCallback(cb => fuse.mount(cb))
|
||||
)
|
||||
})
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@vates/fuse-vhd",
|
||||
"version": "2.1.3",
|
||||
"version": "2.1.1",
|
||||
"license": "ISC",
|
||||
"private": false,
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/fuse-vhd",
|
||||
@@ -19,12 +19,12 @@
|
||||
},
|
||||
"main": "./index.mjs",
|
||||
"dependencies": {
|
||||
"@xen-orchestra/fs": "^4.9.3",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"@xen-orchestra/fs": "^4.1.7",
|
||||
"@xen-orchestra/log": "^0.6.0",
|
||||
"fuse-native": "^2.2.6",
|
||||
"lru-cache": "^7.14.0",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"vhd-lib": "^4.16.1"
|
||||
"vhd-lib": "^4.11.0"
|
||||
},
|
||||
"bin": {
|
||||
"xo-fuse-vhd": "./cli.mjs"
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
A toolbox to ease the use of generator
|
||||
|
||||
### Timeout
|
||||
|
||||
wrap a source async generator to have it throw an error when timeout is reached
|
||||
timeout is a positive number in milliseconds
|
||||
|
||||
```js
|
||||
import { Timeout } from '@vates/generator-toolbox'
|
||||
|
||||
const wrappedGenerator = new Timeout(sourceGenerator, timeout)
|
||||
```
|
||||
|
||||
### Throttle
|
||||
|
||||
wrap a source async generator to have it respect a max speed ( in bytes per seconds).
|
||||
speed is either a strictly positive number or a function returning a strictly positive number. A speed change will be used for the next emitted packet.
|
||||
|
||||
The source generator must yield object with a length property.
|
||||
|
||||
Optimized for small yields regarding to the speed, since it won't split incoming packet.
|
||||
|
||||
If the generator reached the max speed it will be paused, limiting memory consumption.
|
||||
|
||||
```js
|
||||
import { Throttle } from '@vates/generator-toolbox'
|
||||
|
||||
const wrappedGenerator = new Throttle(sourceGenerator, speed)
|
||||
```
|
||||
|
||||
### Synchronized
|
||||
|
||||
Fork a generator. The rules are:
|
||||
|
||||
- if the source returns, all the forks return
|
||||
- if the forks error, all the forks error with the same error
|
||||
- if a fork returns, it is stopped, but the generator continue with the other
|
||||
- if a fork errors, it is stopped, but the generator continue with the other
|
||||
- if all the forks return, the source is stopped
|
||||
- if all the forks error, the source is errored with the last error
|
||||
- the source start producing a packet when the fastest forked ask for it
|
||||
- the source forks get the packet only when all the forks asked for it, no buffer stores in memory
|
||||
|
||||
```ts
|
||||
import { Synchronized } from '@vates/generator-toolbox'
|
||||
|
||||
async function consume(generator: AsyncGenerator) {
|
||||
for await (const val of generator) {
|
||||
console.log({ val })
|
||||
}
|
||||
}
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await Promise.all([consume(first), consume(second)])
|
||||
```
|
||||
|
||||
Note: you can stop early a generator by calling `generator.return()`, and you can stop in in error by calling `generator.throw(error)`
|
||||
@@ -1 +0,0 @@
|
||||
../../scripts/npmignore
|
||||
@@ -1,89 +0,0 @@
|
||||
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||
|
||||
# @vates/generator-toolbox
|
||||
|
||||
[](https://npmjs.org/package/@vates/generator-toolbox)  [](https://bundlephobia.com/result?p=@vates/generator-toolbox) [](https://npmjs.org/package/@vates/generator-toolbox)
|
||||
|
||||
## Install
|
||||
|
||||
Installation of the [npm package](https://npmjs.org/package/@vates/generator-toolbox):
|
||||
|
||||
```sh
|
||||
npm install --save @vates/generator-toolbox
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
A toolbox to ease the use of generator
|
||||
|
||||
### Timeout
|
||||
|
||||
wrap a source async generator to have it throw an error when timeout is reached
|
||||
timeout is a positive number in milliseconds
|
||||
|
||||
```js
|
||||
import { Timeout } from '@vates/generator-toolbox'
|
||||
|
||||
const wrappedGenerator = new Timeout(sourceGenerator, timeout)
|
||||
```
|
||||
|
||||
### Throttle
|
||||
|
||||
wrap a source async generator to have it respect a max speed ( in bytes per seconds).
|
||||
speed is either a strictly positive number or a function returning a strictly positive number. A speed change will be used for the next emitted packet.
|
||||
|
||||
The source generator must yield object with a length property.
|
||||
|
||||
Optimized for small yields regarding to the speed, since it won't split incoming packet.
|
||||
|
||||
If the generator reached the max speed it will be paused, limiting memory consumption.
|
||||
|
||||
```js
|
||||
import { Throttle } from '@vates/generator-toolbox'
|
||||
|
||||
const wrappedGenerator = new Throttle(sourceGenerator, speed)
|
||||
```
|
||||
|
||||
### Synchronized
|
||||
|
||||
Fork a generator. The rules are:
|
||||
|
||||
- if the source returns, all the forks return
|
||||
- if the forks error, all the forks error with the same error
|
||||
- if a fork returns, it is stopped, but the generator continue with the other
|
||||
- if a fork errors, it is stopped, but the generator continue with the other
|
||||
- if all the forks return, the source is stopped
|
||||
- if all the forks error, the source is errored with the last error
|
||||
- the source start producing a packet when the fastest forked ask for it
|
||||
- the source forks get the packet only when all the forks asked for it, no buffer stores in memory
|
||||
|
||||
```ts
|
||||
import { Synchronized } from '@vates/generator-toolbox'
|
||||
|
||||
async function consume(generator: AsyncGenerator) {
|
||||
for await (const val of generator) {
|
||||
console.log({ val })
|
||||
}
|
||||
}
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await Promise.all([consume(first), consume(second)])
|
||||
```
|
||||
|
||||
Note: you can stop early a generator by calling `generator.return()`, and you can stop in in error by calling `generator.throw(error)`
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are _very_ welcomed, either on the documentation or on
|
||||
the code.
|
||||
|
||||
You may:
|
||||
|
||||
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
|
||||
you've encountered;
|
||||
- fork and create a pull request.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](https://spdx.org/licenses/MIT) © [Vates SAS](https://vates.fr)
|
||||
@@ -1,6 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
import eslint from '@eslint/js'
|
||||
import tseslint from 'typescript-eslint'
|
||||
|
||||
export default tseslint.config(eslint.configs.recommended, tseslint.configs.recommended)
|
||||
@@ -1,38 +0,0 @@
|
||||
{
|
||||
"name": "@vates/generator-toolbox",
|
||||
"version": "1.1.3",
|
||||
"main": "dist/index.mjs",
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.19.0",
|
||||
"rimraf": "^6.0.1",
|
||||
"@types/node": "^20.6",
|
||||
"typescript": "~5.6",
|
||||
"typescript-eslint": "^8.61.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"dev": "tsc --watch",
|
||||
"postversion": "npm publish --access public",
|
||||
"prebuild": "rimraf dist/",
|
||||
"predev": "yarn run prebuild",
|
||||
"test": "tsc && node --test **/*.test.mjs",
|
||||
"clean": "rimraf dist/"
|
||||
},
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/generator-toolbox",
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"repository": {
|
||||
"directory": "@vates/generator-toolbox",
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.18"
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export { Synchronized } from './synchronized.mjs'
|
||||
export { Throttle } from './throttle.mjs'
|
||||
export { Timeout } from './timeout.mjs'
|
||||
@@ -1,150 +0,0 @@
|
||||
import assert from 'node:assert'
|
||||
|
||||
export class Synchronized<T, TReturn, TNext> {
|
||||
#source: AsyncGenerator<T, TReturn, TNext>
|
||||
#forks = new Map<string, Forked<T, TReturn, TNext>>()
|
||||
#removedForks = new Set<string>()
|
||||
#waitingForks = new Set<string>()
|
||||
#started = false
|
||||
|
||||
#nextValueForksReady?: {
|
||||
promise: Promise<IteratorResult<T>>
|
||||
forksWaitingReject: (error: Error) => void
|
||||
forksWaitingResolve: () => void
|
||||
}
|
||||
|
||||
constructor(source: AsyncGenerator<T, TReturn, TNext>) {
|
||||
this.#source = source
|
||||
}
|
||||
|
||||
fork(uid: string): AsyncGenerator<T, TReturn, TNext> {
|
||||
assert.strictEqual(this.#started, false, `can't create a fork after consuming the data`)
|
||||
assert.strictEqual(this.#forks.has(uid), false, `a fork with id ${uid} already exists`)
|
||||
|
||||
const fork = new Forked<T, TReturn, TNext>(this, uid)
|
||||
this.#forks.set(uid, fork)
|
||||
return fork
|
||||
}
|
||||
|
||||
async #resolveWhenAllForksReady(): Promise<IteratorResult<T>> {
|
||||
if (!this.#nextValueForksReady) {
|
||||
throw new Error('Can t wait forks if there are noone waiting')
|
||||
}
|
||||
const { promise, forksWaitingResolve } = this.#nextValueForksReady
|
||||
if (this.#waitingForks.size === this.#forks.size) {
|
||||
// reset value
|
||||
this.#waitingForks.clear()
|
||||
this.#nextValueForksReady = undefined
|
||||
forksWaitingResolve() // for the other forks waiting
|
||||
}
|
||||
return promise
|
||||
}
|
||||
|
||||
async next(uid: string): Promise<IteratorResult<T>> {
|
||||
if (this.#removedForks.has(uid)) {
|
||||
return { done: true, value: undefined }
|
||||
}
|
||||
if (!this.#forks.has(uid)) {
|
||||
throw new Error(`trying to advance fork ${uid} that is not a fork of this one`)
|
||||
}
|
||||
|
||||
if (this.#waitingForks.has(uid)) {
|
||||
throw new Error(`Fork ${uid} is already waiting`)
|
||||
}
|
||||
|
||||
this.#started = true
|
||||
if (this.#nextValueForksReady === undefined) {
|
||||
let forksWaitingResolve = () => {}
|
||||
let forksWaitingReject: (reason?: Error) => void = () => {}
|
||||
const next = this.#source.next().catch(async error => {
|
||||
const e = new Error(`Error in the source generator ${error.message}`, { cause: error })
|
||||
forksWaitingReject(e)
|
||||
// source has failed, kill everything, and stop the forks
|
||||
for (const uid of [...this.#forks.keys()]) {
|
||||
await this.remove(uid, error)
|
||||
}
|
||||
})
|
||||
const promise = Promise.all([
|
||||
next,
|
||||
new Promise((_resolve, _reject) => {
|
||||
forksWaitingResolve = () => _resolve(undefined)
|
||||
forksWaitingReject = _reject
|
||||
}),
|
||||
]).then(([_]) => _ as IteratorResult<T>)
|
||||
|
||||
this.#nextValueForksReady = { promise, forksWaitingResolve, forksWaitingReject }
|
||||
}
|
||||
this.#waitingForks.add(uid)
|
||||
return this.#resolveWhenAllForksReady()
|
||||
}
|
||||
|
||||
async remove(uid: string, error?: Error): Promise<IteratorResult<T>> {
|
||||
const fork = this.#forks.get(uid)
|
||||
if (fork === undefined) {
|
||||
if (this.#removedForks.has(uid)) {
|
||||
// already removed
|
||||
return { done: true, value: undefined }
|
||||
}
|
||||
throw new Error(`trying to remove fork with uid ${uid} that is not a fork of this one`)
|
||||
}
|
||||
this.#forks.delete(uid)
|
||||
this.#waitingForks.delete(uid)
|
||||
this.#removedForks.add(uid)
|
||||
try {
|
||||
if (error === undefined) {
|
||||
await fork.return()
|
||||
} else {
|
||||
await fork.throw(error)
|
||||
}
|
||||
} catch (cleaningError) {
|
||||
console.error('Error while cleaning the forked', {
|
||||
cleaningError,
|
||||
sourceError: error,
|
||||
})
|
||||
}
|
||||
|
||||
if (this.#forks.size === 0) {
|
||||
if (error === undefined) {
|
||||
await this.#source.return(undefined as TReturn)
|
||||
} else {
|
||||
await this.#source.throw(error)
|
||||
}
|
||||
// Reject any pending forks waiting for the next value
|
||||
if (this.#nextValueForksReady) {
|
||||
this.#nextValueForksReady.forksWaitingReject(new Error('Source generator terminated.', { cause: error }))
|
||||
this.#nextValueForksReady = undefined
|
||||
}
|
||||
// clear state
|
||||
this.#removedForks.clear()
|
||||
this.#waitingForks.clear()
|
||||
} else {
|
||||
// this fork was maybe blocking the others
|
||||
if (this.#nextValueForksReady) {
|
||||
await this.#resolveWhenAllForksReady()
|
||||
}
|
||||
}
|
||||
return { done: true, value: undefined }
|
||||
}
|
||||
}
|
||||
|
||||
class Forked<T, TReturn, TNext> implements AsyncGenerator<T, TReturn, TNext> {
|
||||
#parent: Synchronized<T, TReturn, TNext>
|
||||
#uid: string
|
||||
constructor(parent: Synchronized<T, TReturn, TNext>, uid: string) {
|
||||
this.#parent = parent
|
||||
this.#uid = uid
|
||||
}
|
||||
next(): Promise<IteratorResult<T>> {
|
||||
return this.#parent.next(this.#uid)
|
||||
}
|
||||
async return(): Promise<IteratorResult<T>> {
|
||||
return this.#parent.remove(this.#uid)
|
||||
}
|
||||
async throw(e: Error): Promise<IteratorResult<T>> {
|
||||
return this.#parent.remove(this.#uid, e)
|
||||
}
|
||||
|
||||
[Symbol.asyncIterator](): AsyncGenerator<T, TReturn, TNext> {
|
||||
return this
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
import assert from 'node:assert'
|
||||
import { suite, test } from 'node:test'
|
||||
import { Synchronized } from './synchronized.mjs'
|
||||
|
||||
async function* makeRangeGenerator(end = Infinity, progress = { yielded: 0 }, onYielded = (_val: unknown) => {}) {
|
||||
for (let i = 0; i < end; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
yield i
|
||||
onYielded(i)
|
||||
progress.yielded = i
|
||||
}
|
||||
return progress
|
||||
}
|
||||
|
||||
async function consume(
|
||||
iterable: AsyncGenerator,
|
||||
delay = 2500,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
onConsumed = (_val: unknown, _iterable: AsyncGenerator) => Promise.resolve(false)
|
||||
) {
|
||||
for await (const val of iterable) {
|
||||
await new Promise(resolve => setTimeout(resolve, delay))
|
||||
if (await onConsumed(val, iterable)) {
|
||||
iterable.return(undefined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suite('success', () => {
|
||||
test('for-await-of break stops the source generator', async () => {
|
||||
let sourceClosed = false
|
||||
const source = (async function* () {
|
||||
try {
|
||||
for (let i = 0; i < 10; i++) {
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
yield i
|
||||
}
|
||||
} finally {
|
||||
sourceClosed = true
|
||||
}
|
||||
})()
|
||||
|
||||
const forker = new Synchronized(source)
|
||||
const fork = forker.fork('first')
|
||||
|
||||
// for-await-of must call fork.return() on break, which propagates to the source
|
||||
for await (const val of fork) {
|
||||
if (val === 2) break
|
||||
}
|
||||
|
||||
assert.strictEqual(sourceClosed, true)
|
||||
})
|
||||
|
||||
test('if works with multiple consumer', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(3, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await Promise.all([consume(first, 50), consume(second, 500)])
|
||||
assert.strictEqual(progress.yielded, 2)
|
||||
})
|
||||
|
||||
test('if works with one consumer returning', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(10, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await new Promise(resolve => setTimeout(resolve, 500))
|
||||
await Promise.all([
|
||||
consume(first, 1),
|
||||
consume(second, 1, async (val: unknown) => {
|
||||
return Promise.resolve(val === 2)
|
||||
}),
|
||||
])
|
||||
assert.strictEqual(progress.yielded, 9)
|
||||
})
|
||||
test('if works with all consumers returning', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(10, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await new Promise(resolve => setTimeout(resolve, 500))
|
||||
await Promise.all([
|
||||
consume(first, 1, async (val: unknown) => {
|
||||
return Promise.resolve(val === 5)
|
||||
}),
|
||||
consume(second, 1, async (val: unknown) => {
|
||||
return Promise.resolve(val === 2)
|
||||
}),
|
||||
])
|
||||
assert.strictEqual(progress.yielded, 4)
|
||||
})
|
||||
})
|
||||
|
||||
suite('error', () => {
|
||||
test('it fails all if error on source', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(5, progress, (val: unknown) => {
|
||||
if (val === 2) {
|
||||
throw new Error('source stop')
|
||||
}
|
||||
})
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
await assert.rejects(Promise.all([consume(first, 1), consume(second, 1)]))
|
||||
|
||||
assert.strictEqual(progress.yielded, 1)
|
||||
})
|
||||
test('it flows until the end if one consumer throws', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(10, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
|
||||
const firstConsume = consume(first, 1)
|
||||
|
||||
const secondConsume = assert.rejects(() =>
|
||||
consume(second, 1, async (val: unknown, iterable) => {
|
||||
if (val === 1) {
|
||||
await iterable.throw(new Error('error on second'))
|
||||
}
|
||||
return false
|
||||
})
|
||||
)
|
||||
await Promise.allSettled([firstConsume, secondConsume])
|
||||
assert.strictEqual(progress.yielded, 9)
|
||||
})
|
||||
test('it throw if ALL consumer throws', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(5, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
const second = forker.fork('second')
|
||||
|
||||
const firstConsume = consume(first, 1, async (val: unknown, iterable) => {
|
||||
if (val === 3) {
|
||||
await iterable.throw(new Error('error on first'))
|
||||
}
|
||||
return false
|
||||
})
|
||||
const secondConsume = consume(second, 1, async (val: unknown, iterable) => {
|
||||
if (val === 1) {
|
||||
await iterable.throw(new Error('error on second'))
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
await assert.rejects(Promise.all([firstConsume, secondConsume]))
|
||||
assert.strictEqual(progress.yielded, 2)
|
||||
})
|
||||
|
||||
test('It should not allow a new fork after the start', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(5, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
const first = forker.fork('first')
|
||||
await first.next()
|
||||
assert.throws(() => forker.fork('second'))
|
||||
})
|
||||
test('It should not allow fork with the same uid', async () => {
|
||||
const progress = { yielded: 0 }
|
||||
const generator = makeRangeGenerator(5, progress)
|
||||
const forker = new Synchronized(generator)
|
||||
forker.fork('first')
|
||||
forker.fork('second')
|
||||
assert.throws(() => forker.fork('first'))
|
||||
})
|
||||
})
|
||||
@@ -1,68 +0,0 @@
|
||||
import assert from 'node:assert'
|
||||
|
||||
/**
|
||||
*
|
||||
* This class will throttle the production of a group of generators
|
||||
*
|
||||
* limits: depends on the event loop, so don't expect it to be very precise
|
||||
* works better with more small blocks
|
||||
* The source generator must yield object with a length property
|
||||
* Changing the speed will only be takin into account fot the next packets asked
|
||||
*/
|
||||
export class Throttle {
|
||||
#previousSlot = 0
|
||||
#bytesPerSecond: number | (() => number)
|
||||
get speed(): number {
|
||||
let speed: number
|
||||
if (typeof this.#bytesPerSecond === 'function') {
|
||||
speed = this.#bytesPerSecond()
|
||||
} else {
|
||||
speed = this.#bytesPerSecond
|
||||
}
|
||||
assert.ok(speed >= 0, `speed must be positive, ${speed} computed`)
|
||||
return speed
|
||||
}
|
||||
constructor(speed: number | (() => number)) {
|
||||
this.#bytesPerSecond = speed
|
||||
}
|
||||
|
||||
getNextSlot(length: number): { timeout?: ReturnType<typeof setTimeout>; promise?: Promise<unknown> } {
|
||||
assert.notStrictEqual(length, undefined, `throttled stream need to expose a length property }`)
|
||||
assert.ok(length > 0, `throttled stream must expose a positive length property , ${length} given }`)
|
||||
if (this.speed === 0) {
|
||||
return {}
|
||||
}
|
||||
const previous = this.#previousSlot
|
||||
const nextSlot = Math.round(previous + (length * 1000) / this.speed)
|
||||
if (nextSlot < Date.now()) {
|
||||
// we're above the limit, go now
|
||||
this.#previousSlot = Date.now()
|
||||
return {}
|
||||
}
|
||||
this.#previousSlot = nextSlot
|
||||
// wait till the next slot
|
||||
// it won't be extremely precise since the event loop is not
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined = undefined
|
||||
const promise = new Promise(function (resolve) {
|
||||
timeout = setTimeout(resolve, nextSlot - Date.now())
|
||||
})
|
||||
return { promise, timeout }
|
||||
}
|
||||
|
||||
async *createThrottledGenerator(source: AsyncGenerator<{ length: number }>): AsyncGenerator<{ length: number }> {
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined
|
||||
try {
|
||||
for await (const value of source) {
|
||||
const res = this.getNextSlot(value.length)
|
||||
timeout = res.timeout
|
||||
// wait for the time slot before yielding the data
|
||||
if (res.promise !== undefined) {
|
||||
await res.promise
|
||||
}
|
||||
yield value
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
import { Throttle } from './throttle.mjs'
|
||||
import { suite, test } from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
|
||||
async function* makeGenerator(chunkSize = 1024 * 1024, nbChunks = 100) {
|
||||
for (let i = 0; i < nbChunks; i++) {
|
||||
yield Buffer.allocUnsafe(chunkSize)
|
||||
}
|
||||
}
|
||||
|
||||
async function consumes(generator: AsyncGenerator) {
|
||||
// eslint-disable-next-line no-empty,@typescript-eslint/no-unused-vars
|
||||
for await (const data of generator) {
|
||||
}
|
||||
}
|
||||
|
||||
suite('it throttle one', () => {
|
||||
test('base test', async () => {
|
||||
// 10MB/s
|
||||
const throttler = new Throttle(10 * 1024 * 1024)
|
||||
const throttled = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 20))
|
||||
|
||||
const start = Date.now()
|
||||
await consumes(throttled)
|
||||
const end = Date.now()
|
||||
assert.strictEqual(Math.round((end - start) / 1000), 2)
|
||||
})
|
||||
test('variable speed test', async () => {
|
||||
// 10MB/s
|
||||
const throttler = new Throttle(() => 10 * 1024 * 1024)
|
||||
const throttled = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 20))
|
||||
|
||||
const start = Date.now()
|
||||
await consumes(throttled)
|
||||
const end = Date.now()
|
||||
assert.strictEqual(Math.round((end - start) / 1000), 2)
|
||||
})
|
||||
test('multiple generator test', async () => {
|
||||
// 10MB/s
|
||||
const throttler = new Throttle(10 * 1024 * 1024)
|
||||
const first = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 10))
|
||||
const second = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 10))
|
||||
const start = Date.now()
|
||||
|
||||
await Promise.all([consumes(first), consumes(second)])
|
||||
const end = Date.now()
|
||||
assert.strictEqual(Math.round((end - start) / 1000), 2)
|
||||
})
|
||||
test(`it doesn't accept negative value`, async () => {
|
||||
// 10MB/s
|
||||
let throttler = new Throttle(-10 * 1024 * 1024)
|
||||
let generator = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 10))
|
||||
await assert.rejects(() => consumes(generator))
|
||||
|
||||
let index = 5
|
||||
throttler = new Throttle(() => {
|
||||
index--
|
||||
return index > 0 ? 10 * 1024 * 1024 : -10 * 1024 * 1024
|
||||
})
|
||||
generator = throttler.createThrottledGenerator(makeGenerator(1024 * 1024, 10))
|
||||
await assert.rejects(() => consumes(generator))
|
||||
})
|
||||
})
|
||||
@@ -1,49 +0,0 @@
|
||||
import assert from 'node:assert'
|
||||
|
||||
const TIMEOUT = Symbol('timeout')
|
||||
|
||||
export class Timeout<T> implements AsyncGenerator {
|
||||
#source: AsyncGenerator<T>
|
||||
#timeout: number
|
||||
constructor(source: AsyncGenerator<T>, timeout: number) {
|
||||
assert.ok(timeout > 0, `Timeout value must be positive, ${timeout} received`)
|
||||
this.#source = source
|
||||
this.#timeout = timeout
|
||||
}
|
||||
async next(): Promise<IteratorResult<T>> {
|
||||
let timeoutHandle: ReturnType<typeof setTimeout>
|
||||
|
||||
const sourceNext = this.#source.next()
|
||||
const result = await Promise.race([
|
||||
sourceNext.then(res => {
|
||||
clearTimeout(timeoutHandle)
|
||||
return res
|
||||
}),
|
||||
new Promise<typeof TIMEOUT>(resolve => {
|
||||
timeoutHandle = setTimeout(() => resolve(TIMEOUT), this.#timeout)
|
||||
}),
|
||||
])
|
||||
|
||||
if (result === TIMEOUT) {
|
||||
// stop the source once the in-flight next settles to avoid data loss on the next call
|
||||
// we don't want to await for the end of timeouted code
|
||||
sourceNext.then(
|
||||
() => this.#source.return(undefined).catch(() => {}),
|
||||
() => {} // source already errored, nothing to clean up
|
||||
)
|
||||
throw new Error('Timeout reached')
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
return(): Promise<IteratorResult<T>> {
|
||||
return this.#source.return(undefined)
|
||||
}
|
||||
throw(e: Error): Promise<IteratorResult<T>> {
|
||||
return this.#source.throw(e)
|
||||
}
|
||||
|
||||
[Symbol.asyncIterator](): AsyncGenerator<T> {
|
||||
return this
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
import { Timeout } from './timeout.mjs'
|
||||
|
||||
describe('Timeout class', () => {
|
||||
it('should reject a timeout with a negative or zero value ', async () => {
|
||||
// Create a mock async generator that never resolves
|
||||
const mockGenerator = (async function* () {
|
||||
yield 1
|
||||
})()
|
||||
|
||||
assert.throws(() => new Timeout(mockGenerator, 0))
|
||||
assert.throws(() => new Timeout(mockGenerator, -10))
|
||||
})
|
||||
it('should resolve with the value from the source generator if it completes before the timeout', async () => {
|
||||
// Create a mock async generator that resolves immediately
|
||||
const mockGenerator = (async function* () {
|
||||
yield 1
|
||||
})()
|
||||
|
||||
const timeout = new Timeout(mockGenerator, 100) // 100ms timeout
|
||||
|
||||
const result = await timeout.next()
|
||||
assert.strictEqual(result.value, 1)
|
||||
assert.strictEqual(result.done, false)
|
||||
})
|
||||
|
||||
it('should reject with a timeout error if the source generator takes too long', async () => {
|
||||
// Create a mock async generator that never resolves
|
||||
// eslint-disable-next-line require-yield
|
||||
const mockGenerator = (async function* () {
|
||||
await new Promise(() => {}) // Never resolves
|
||||
})()
|
||||
|
||||
const timeout = new Timeout(mockGenerator, 10) // 10ms timeout
|
||||
|
||||
await assert.rejects(timeout.next(), 'Expected timeout error was not thrown')
|
||||
})
|
||||
it('should allow iteration using for-await-of', async () => {
|
||||
// Create a mock async generator that yields values
|
||||
const mockGenerator: AsyncGenerator<number> = (async function* () {
|
||||
yield 1
|
||||
yield 2
|
||||
yield 3
|
||||
})()
|
||||
|
||||
const timeout = new Timeout(mockGenerator, 100) // 100ms timeout
|
||||
|
||||
const results: Array<number> = []
|
||||
for await (const value of timeout) {
|
||||
results.push(value as number)
|
||||
}
|
||||
|
||||
assert.deepStrictEqual(results, [1, 2, 3])
|
||||
})
|
||||
|
||||
it('should handle the throw method correctly', async () => {
|
||||
// Create a mock async generator with a throw method
|
||||
const mockGenerator = (async function* () {
|
||||
yield 1
|
||||
})()
|
||||
|
||||
const timeout = new Timeout(mockGenerator, 100) // 100ms timeout
|
||||
|
||||
await assert.rejects(timeout.throw(new Error('Test error')), 'Expected error was not thrown')
|
||||
})
|
||||
|
||||
it('should close the source generator after a timeout', async () => {
|
||||
let sourceClosed = false
|
||||
const slowSource = (async function* () {
|
||||
try {
|
||||
// resolves after 100ms so the in-flight next() eventually settles
|
||||
await new Promise(resolve => setTimeout(resolve, 100))
|
||||
yield 1
|
||||
} finally {
|
||||
sourceClosed = true
|
||||
}
|
||||
})()
|
||||
|
||||
const timeout = new Timeout(slowSource, 10) // fires before source yields
|
||||
await assert.rejects(() => timeout.next(), /Timeout reached/)
|
||||
|
||||
// wait for the in-flight next() to settle so source.return() can be called
|
||||
await new Promise(resolve => setTimeout(resolve, 200))
|
||||
assert.strictEqual(sourceClosed, true)
|
||||
})
|
||||
})
|
||||
@@ -1,13 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2023",
|
||||
"module": "ESNext",
|
||||
"types": ["node"], // Only use types from Node.js and packages that are explicitly imported
|
||||
"moduleResolution": "bundler",
|
||||
"strict": true,
|
||||
"declaration": true, // Generates `.d.ts` files for type safety
|
||||
"outDir": "dist" // Outputs compiled code to the `dist` directory
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
### `create([opts], [requestListener])`
|
||||
|
||||
Creates a new server instance. `opts` may contain `createServer` and `createSecureServer` factories (defaults to `http.createServer` and `https.createServer`). `requestListener` is registered as a `request` event listener.
|
||||
|
||||
```js
|
||||
import { create } from '@vates/http-server-plus'
|
||||
|
||||
const server = create(function onRequest(req, res) {
|
||||
res.end('Hello')
|
||||
})
|
||||
```
|
||||
|
||||
### `server.listen(opts)`
|
||||
|
||||
Starts listening. Returns a promise that resolves to a human-readable address string, or rejects with `error.niceAddress` set.
|
||||
|
||||
```js
|
||||
// HTTP on a specific port
|
||||
await server.listen({ port: 8080 })
|
||||
|
||||
// HTTPS — detected automatically when cert+key are provided
|
||||
await server.listen({
|
||||
port: 443,
|
||||
cert: fs.readFileSync('certificate.pem'),
|
||||
key: fs.readFileSync('private_key.pem'),
|
||||
})
|
||||
|
||||
// Unix socket
|
||||
await server.listen({ socket: '/var/run/http.sock' })
|
||||
|
||||
// File descriptor
|
||||
await server.listen({ fd: 3 })
|
||||
|
||||
// Systemd socket activation
|
||||
await server.listen({ systemdSocket: 0 })
|
||||
```
|
||||
|
||||
> As a convenience, if `hostname` is `'localhost'`, it will listen on both `127.0.0.1` and `::1`, similar to what Node does when no hostname is provided.
|
||||
|
||||
Multiple endpoints can be started on the same instance:
|
||||
|
||||
```js
|
||||
const [httpAddr, httpsAddr] = await Promise.all([server.listen({ port: 80 }), server.listen({ port: 443, cert, key })])
|
||||
console.log('listening on', httpAddr, httpsAddr)
|
||||
```
|
||||
|
||||
### HTTP/2
|
||||
|
||||
Pass a custom `createSecureServer` factory to enable HTTP/2:
|
||||
|
||||
```js
|
||||
import { createSecureServer } from 'node:http2'
|
||||
import { create } from '@vates/http-server-plus'
|
||||
|
||||
const server = create({ createSecureServer }, app)
|
||||
await server.listen({ port: 443, cert, key })
|
||||
```
|
||||
|
||||
### `server.close()`
|
||||
|
||||
Closes all underlying servers. Returns a promise that resolves once the `close` event is emitted.
|
||||
|
||||
```js
|
||||
await server.close()
|
||||
```
|
||||
@@ -1 +0,0 @@
|
||||
../../scripts/npmignore
|
||||
@@ -1,98 +0,0 @@
|
||||
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||
|
||||
# @vates/http-server-plus
|
||||
|
||||
[](https://npmjs.org/package/@vates/http-server-plus)  [](https://bundlephobia.com/result?p=@vates/http-server-plus) [](https://npmjs.org/package/@vates/http-server-plus)
|
||||
|
||||
> Augmented `http.Server`, HTTP/HTTPS/HTTP2 and multiple ports on the same instance
|
||||
|
||||
## Install
|
||||
|
||||
Installation of the [npm package](https://npmjs.org/package/@vates/http-server-plus):
|
||||
|
||||
```sh
|
||||
npm install --save @vates/http-server-plus
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### `create([opts], [requestListener])`
|
||||
|
||||
Creates a new server instance. `opts` may contain `createServer` and `createSecureServer` factories (defaults to `http.createServer` and `https.createServer`). `requestListener` is registered as a `request` event listener.
|
||||
|
||||
```js
|
||||
import { create } from '@vates/http-server-plus'
|
||||
|
||||
const server = create(function onRequest(req, res) {
|
||||
res.end('Hello')
|
||||
})
|
||||
```
|
||||
|
||||
### `server.listen(opts)`
|
||||
|
||||
Starts listening. Returns a promise that resolves to a human-readable address string, or rejects with `error.niceAddress` set.
|
||||
|
||||
```js
|
||||
// HTTP on a specific port
|
||||
await server.listen({ port: 8080 })
|
||||
|
||||
// HTTPS — detected automatically when cert+key are provided
|
||||
await server.listen({
|
||||
port: 443,
|
||||
cert: fs.readFileSync('certificate.pem'),
|
||||
key: fs.readFileSync('private_key.pem'),
|
||||
})
|
||||
|
||||
// Unix socket
|
||||
await server.listen({ socket: '/var/run/http.sock' })
|
||||
|
||||
// File descriptor
|
||||
await server.listen({ fd: 3 })
|
||||
|
||||
// Systemd socket activation
|
||||
await server.listen({ systemdSocket: 0 })
|
||||
```
|
||||
|
||||
> As a convenience, if `hostname` is `'localhost'`, it will listen on both `127.0.0.1` and `::1`, similar to what Node does when no hostname is provided.
|
||||
|
||||
Multiple endpoints can be started on the same instance:
|
||||
|
||||
```js
|
||||
const [httpAddr, httpsAddr] = await Promise.all([server.listen({ port: 80 }), server.listen({ port: 443, cert, key })])
|
||||
console.log('listening on', httpAddr, httpsAddr)
|
||||
```
|
||||
|
||||
### HTTP/2
|
||||
|
||||
Pass a custom `createSecureServer` factory to enable HTTP/2:
|
||||
|
||||
```js
|
||||
import { createSecureServer } from 'node:http2'
|
||||
import { create } from '@vates/http-server-plus'
|
||||
|
||||
const server = create({ createSecureServer }, app)
|
||||
await server.listen({ port: 443, cert, key })
|
||||
```
|
||||
|
||||
### `server.close()`
|
||||
|
||||
Closes all underlying servers. Returns a promise that resolves once the `close` event is emitted.
|
||||
|
||||
```js
|
||||
await server.close()
|
||||
```
|
||||
|
||||
## Contributions
|
||||
|
||||
Contributions are _very_ welcomed, either on the documentation or on
|
||||
the code.
|
||||
|
||||
You may:
|
||||
|
||||
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
|
||||
you've encountered;
|
||||
- fork and create a pull request.
|
||||
|
||||
## License
|
||||
|
||||
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)
|
||||
@@ -1,268 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const EventEmitter = require('events').EventEmitter
|
||||
const formatUrl = require('url').format
|
||||
const fromEvent = require('promise-toolbox/fromEvent')
|
||||
const inherits = require('util').inherits
|
||||
const resolvePath = require('path').resolve
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const forwardedEvents = [
|
||||
'checkContinue',
|
||||
'checkExpectation',
|
||||
'clientError',
|
||||
'connect',
|
||||
'connection',
|
||||
'listening',
|
||||
'request',
|
||||
'secureConnection',
|
||||
'upgrade',
|
||||
]
|
||||
|
||||
function extractProperty(obj, prop) {
|
||||
const value = obj[prop]
|
||||
if (value !== undefined) {
|
||||
delete obj[prop]
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
function forOwn(obj, cb) {
|
||||
const keys = Object.keys(obj)
|
||||
for (let i = 0, n = keys.length; i < n; ++i) {
|
||||
cb(obj[keys[i]])
|
||||
}
|
||||
}
|
||||
|
||||
function getSystemdFd(index) {
|
||||
if (process.pid !== +process.env.LISTEN_PID) {
|
||||
throw new Error('systemd sockets are not meant for us (LISTEN_PID)')
|
||||
}
|
||||
|
||||
const listenFds = process.env.LISTEN_FDS
|
||||
if (listenFds == null) {
|
||||
throw new Error('no systemd sockets found (LISTEN_FDS)')
|
||||
}
|
||||
const max = listenFds - 1
|
||||
|
||||
if (index > max) {
|
||||
throw new Error('no such systemd socket found')
|
||||
}
|
||||
|
||||
return index + 3 // 3 = SD_LISTEN_FDS_START
|
||||
}
|
||||
|
||||
const isEmpty = obj => Object.keys(obj).length === 0
|
||||
|
||||
// ===================================================================
|
||||
|
||||
function onNewListener(event, listener) {
|
||||
if (forwardedEvents.includes(event)) {
|
||||
forOwn(this._servers, server => {
|
||||
server.on(event, listener)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function onRemoveListener(event, listener) {
|
||||
if (forwardedEvents.includes(event)) {
|
||||
forOwn(this._servers, server => {
|
||||
server.off(event, listener)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function Server(opts) {
|
||||
EventEmitter.call(this)
|
||||
|
||||
this._createServer = (opts != null && opts.createServer) || require('http').createServer
|
||||
this._createSecureServer = (opts != null && opts.createSecureServer) || require('https').createServer
|
||||
this._servers = Object.create(null)
|
||||
this._niceAddresses = Object.create(null)
|
||||
|
||||
this.on('removeListener', onRemoveListener).on('newListener', onNewListener)
|
||||
}
|
||||
inherits(Server, EventEmitter)
|
||||
|
||||
const proto = Server.prototype
|
||||
|
||||
function getAddress(serverId) {
|
||||
return this[serverId].address()
|
||||
}
|
||||
proto.addresses = function Server$addresses() {
|
||||
const servers = this._servers
|
||||
return Object.keys(servers).map(getAddress, servers)
|
||||
}
|
||||
|
||||
function getNiceAddress(serverId) {
|
||||
return this[serverId]
|
||||
}
|
||||
proto.niceAddresses = function Server$niceAddresses() {
|
||||
const niceAddresses = this._niceAddresses
|
||||
return Object.keys(niceAddresses).map(getNiceAddress, niceAddresses)
|
||||
}
|
||||
|
||||
function close(server) {
|
||||
server.close()
|
||||
}
|
||||
|
||||
proto.close = function Server$close(callback) {
|
||||
if (callback) {
|
||||
this.once('close', callback)
|
||||
}
|
||||
|
||||
// Emit the close event even if there are no registered servers.
|
||||
if (isEmpty(this._servers)) {
|
||||
const self = this
|
||||
setImmediate(function () {
|
||||
self.emit('close')
|
||||
})
|
||||
return Promise.resolve()
|
||||
}
|
||||
// Closes each servers.
|
||||
forOwn(this._servers, close)
|
||||
|
||||
return fromEvent(this, 'close')
|
||||
}
|
||||
|
||||
let nextId = 0
|
||||
proto.listen = function Server$listen(opts) {
|
||||
opts = Object.assign({}, opts)
|
||||
|
||||
if (opts.hostname === 'localhost') {
|
||||
return Promise.all([
|
||||
this.listen(Object.assign(opts, { hostname: '::1' })),
|
||||
this.listen(Object.assign(opts, { hostname: '127.0.0.1' })),
|
||||
]).then(addresses => addresses[0])
|
||||
}
|
||||
|
||||
const hostname = extractProperty(opts, 'hostname')
|
||||
let port = extractProperty(opts, 'port')
|
||||
const systemdSocket = extractProperty(opts, 'systemdSocket')
|
||||
let fd = extractProperty(opts, 'fd')
|
||||
let socket = extractProperty(opts, 'socket')
|
||||
|
||||
const servers = this._servers
|
||||
|
||||
const niceAddresses = this._niceAddresses
|
||||
|
||||
let server, protocol
|
||||
if (opts.pfx || opts.SNICallback || (opts.cert && opts.key)) {
|
||||
server = this._createSecureServer(opts)
|
||||
protocol = 'https'
|
||||
} else {
|
||||
server = this._createServer(opts)
|
||||
protocol = 'http'
|
||||
}
|
||||
|
||||
const id = nextId++
|
||||
servers[id] = server
|
||||
|
||||
if (systemdSocket != null) {
|
||||
fd = getSystemdFd(systemdSocket)
|
||||
}
|
||||
|
||||
// Compute a temporary nice address to display in case of error.
|
||||
let niceAddress
|
||||
if (fd != null) {
|
||||
server.listen({ fd })
|
||||
niceAddress = protocol + '://<fd:' + fd + '>'
|
||||
} else if (socket != null) {
|
||||
socket = resolvePath(socket)
|
||||
server.listen(socket)
|
||||
niceAddress = protocol + '://' + socket
|
||||
} else {
|
||||
if (port == null) {
|
||||
port = protocol === 'https' ? 443 : 80
|
||||
}
|
||||
server.listen(port, hostname)
|
||||
niceAddress = formatUrl({
|
||||
protocol,
|
||||
|
||||
// Hostname default to localhost.
|
||||
hostname: hostname || 'localhost',
|
||||
|
||||
// No port means random, unknown for now.
|
||||
port: port || '<unknown>',
|
||||
})
|
||||
}
|
||||
|
||||
const emit = this.emit.bind(this)
|
||||
server.once('close', function onClose() {
|
||||
delete servers[id]
|
||||
delete niceAddresses[id]
|
||||
|
||||
if (isEmpty(servers)) {
|
||||
emit('close')
|
||||
}
|
||||
})
|
||||
|
||||
server.on('error', function onError() {
|
||||
delete servers[id]
|
||||
|
||||
// FIXME: Should it be forwarded and be fatal if there are no
|
||||
// listeners?
|
||||
})
|
||||
|
||||
forwardedEvents.forEach(event => {
|
||||
this.listeners(event).forEach(listener => {
|
||||
server.on(event, listener)
|
||||
})
|
||||
})
|
||||
|
||||
return fromEvent(server, 'listening').then(
|
||||
function () {
|
||||
const address = server.address()
|
||||
const niceAddress =
|
||||
typeof address === 'string'
|
||||
? protocol + '://' + address
|
||||
: formatUrl({
|
||||
protocol,
|
||||
hostname: address.address,
|
||||
port: address.port,
|
||||
})
|
||||
niceAddresses[id] = niceAddress
|
||||
return niceAddress
|
||||
},
|
||||
function (error) {
|
||||
error.niceAddress = niceAddress
|
||||
throw error
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
function ref(server) {
|
||||
server.ref()
|
||||
}
|
||||
|
||||
proto.ref = function Server$ref() {
|
||||
forOwn(this._servers, ref)
|
||||
}
|
||||
|
||||
function unref(server) {
|
||||
server.unref()
|
||||
}
|
||||
|
||||
proto.unref = function Server$unref() {
|
||||
forOwn(this._servers, unref)
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
module.exports = exports = Server
|
||||
|
||||
exports.create = function create(opts, requestListener) {
|
||||
if (typeof opts === 'function') {
|
||||
requestListener = opts
|
||||
opts = undefined
|
||||
}
|
||||
|
||||
const server = new Server(opts)
|
||||
if (requestListener !== undefined) {
|
||||
server.on('request', requestListener)
|
||||
}
|
||||
return server
|
||||
}
|
||||
@@ -1,303 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const assert = require('assert/strict')
|
||||
const { EventEmitter } = require('events')
|
||||
const resolvePath = require('path').resolve
|
||||
const { describe, it } = require('node:test')
|
||||
|
||||
const HttpServerPlus = require('./')
|
||||
|
||||
// ===================================================================
|
||||
|
||||
// Creates a minimal mock server that can be used as a drop-in for http/https.Server.
|
||||
// `address` is the value returned by mock.address() — use an object for TCP, a string for sockets.
|
||||
function createMock(address) {
|
||||
const mock = new EventEmitter()
|
||||
mock.listen = function () {
|
||||
setImmediate(() => this.emit('listening'))
|
||||
}
|
||||
mock.address = () => address
|
||||
mock.close = function () {
|
||||
setImmediate(() => this.emit('close'))
|
||||
}
|
||||
return mock
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
describe('HttpServerPlus', function () {
|
||||
describe('#create()', function () {
|
||||
it('creates a new instance', function () {
|
||||
assert(HttpServerPlus.create() instanceof HttpServerPlus)
|
||||
})
|
||||
|
||||
it('can register a `request` listener', function () {
|
||||
const listener = function () {}
|
||||
const server = HttpServerPlus.create(listener)
|
||||
|
||||
assert.deepEqual(server.listeners('request'), [listener])
|
||||
})
|
||||
})
|
||||
|
||||
describe('.addresses()', function () {
|
||||
it('returns the list of addresses the server is listening at', function () {
|
||||
const addr = { address: '127.0.0.1', port: 8080, family: 'IPv4' }
|
||||
const mock = createMock(addr)
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ port: 8080 }).then(function () {
|
||||
assert.deepEqual(server.addresses(), [addr])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.niceAddresses()', function () {
|
||||
it('returns the list of human readable addresses the server is listening at', function () {
|
||||
const mock = createMock({ address: '127.0.0.1', port: 8080 })
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ hostname: '127.0.0.1', port: 8080 }).then(function (address) {
|
||||
assert.deepEqual(server.niceAddresses(), [address])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('secureConnection event forwarding', function () {
|
||||
function createMockHttpsServer() {
|
||||
const server = new EventEmitter()
|
||||
server.listen = function () {
|
||||
setImmediate(() => this.emit('listening'))
|
||||
}
|
||||
server.address = () => ({ address: '127.0.0.1', port: 8443 })
|
||||
server.close = function () {
|
||||
setImmediate(() => this.emit('close'))
|
||||
}
|
||||
return server
|
||||
}
|
||||
|
||||
function createServerWithMock() {
|
||||
let mockServer
|
||||
const server = HttpServerPlus.create({
|
||||
createSecureServer: () => {
|
||||
mockServer = createMockHttpsServer()
|
||||
return mockServer
|
||||
},
|
||||
})
|
||||
return {
|
||||
server,
|
||||
getMock: () => mockServer,
|
||||
}
|
||||
}
|
||||
|
||||
it('forwards secureConnection to listeners registered before listen()', function () {
|
||||
const { server, getMock } = createServerWithMock()
|
||||
|
||||
const received = []
|
||||
server.on('secureConnection', socket => received.push(socket))
|
||||
|
||||
const fakeSocket = {}
|
||||
return server.listen({ cert: 'fake-cert', key: 'fake-key' }).then(function () {
|
||||
getMock().emit('secureConnection', fakeSocket)
|
||||
assert.deepEqual(received, [fakeSocket])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('forwards secureConnection to listeners registered after listen()', function () {
|
||||
const { server, getMock } = createServerWithMock()
|
||||
|
||||
return server.listen({ cert: 'fake-cert', key: 'fake-key' }).then(function () {
|
||||
const received = []
|
||||
server.on('secureConnection', socket => received.push(socket))
|
||||
|
||||
const fakeSocket = {}
|
||||
getMock().emit('secureConnection', fakeSocket)
|
||||
assert.deepEqual(received, [fakeSocket])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('stops forwarding secureConnection after listener is removed', function () {
|
||||
const { server, getMock } = createServerWithMock()
|
||||
|
||||
const received = []
|
||||
const listener = socket => received.push(socket)
|
||||
server.on('secureConnection', listener)
|
||||
|
||||
return server.listen({ cert: 'fake-cert', key: 'fake-key' }).then(function () {
|
||||
server.off('secureConnection', listener)
|
||||
|
||||
getMock().emit('secureConnection', {})
|
||||
assert.deepEqual(received, [])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.listen()', function () {
|
||||
it('can use a host:port', function () {
|
||||
let listenArgs
|
||||
const mock = createMock({ address: '127.0.0.1', port: 8080 })
|
||||
mock.listen = function (...args) {
|
||||
listenArgs = args
|
||||
setImmediate(() => this.emit('listening'))
|
||||
}
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ hostname: '127.0.0.1', port: 8080 }).then(function () {
|
||||
assert.deepEqual(listenArgs, [8080, '127.0.0.1'])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('can use a systemd socket', function () {
|
||||
const SD_LISTEN_FDS_START = 3
|
||||
const SOCKET_INDEX = 0
|
||||
|
||||
let listenArgs
|
||||
const mock = createMock('http://<fd:3>')
|
||||
mock.listen = function (...args) {
|
||||
listenArgs = args
|
||||
setImmediate(() => this.emit('listening'))
|
||||
}
|
||||
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
const env = process.env
|
||||
env.LISTEN_FDS = String(SOCKET_INDEX + 1)
|
||||
env.LISTEN_PID = String(process.pid)
|
||||
|
||||
return server.listen({ systemdSocket: SOCKET_INDEX }).then(function () {
|
||||
delete env.LISTEN_FDS
|
||||
delete env.LISTEN_PID
|
||||
|
||||
assert.deepEqual(listenArgs, [{ fd: SOCKET_INDEX + SD_LISTEN_FDS_START }])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('can use a socket', function () {
|
||||
const socketPath = '/tmp/http-server-plus-test.sock'
|
||||
let listenArgs
|
||||
const mock = createMock(socketPath)
|
||||
mock.listen = function (...args) {
|
||||
listenArgs = args
|
||||
setImmediate(() => this.emit('listening'))
|
||||
}
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ socket: socketPath }).then(function () {
|
||||
assert.deepEqual(listenArgs, [resolvePath(socketPath)])
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('can use a HTTPS certificate', function () {
|
||||
let capturedOpts
|
||||
const mock = createMock({ address: '127.0.0.1', port: 8443 })
|
||||
const server = HttpServerPlus.create({
|
||||
createSecureServer: opts => {
|
||||
capturedOpts = opts
|
||||
return mock
|
||||
},
|
||||
})
|
||||
|
||||
return server.listen({ cert: 'my-cert', key: 'my-key', port: 8443 }).then(function (address) {
|
||||
assert.equal(capturedOpts.cert, 'my-cert')
|
||||
assert.equal(capturedOpts.key, 'my-key')
|
||||
assert.ok(address.startsWith('https://'), `expected https URL, got: ${address}`)
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('returns a promise which will resolve once listening', function () {
|
||||
const mock = createMock({ address: '127.0.0.1', port: 8080 })
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ port: 8080 }).then(function (address) {
|
||||
assert.equal(typeof address, 'string')
|
||||
assert.ok(address.startsWith('http://'), `expected http URL, got: ${address}`)
|
||||
return server.close()
|
||||
})
|
||||
})
|
||||
|
||||
it('returns a promise which will reject if failure', function () {
|
||||
const listenError = new Error('EADDRINUSE')
|
||||
const mock = createMock(null)
|
||||
mock.listen = function () {
|
||||
setImmediate(() => this.emit('error', listenError))
|
||||
}
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
return server.listen({ port: 8080 }).then(
|
||||
function () {
|
||||
assert.fail('should have rejected')
|
||||
},
|
||||
function (err) {
|
||||
assert.equal(err, listenError)
|
||||
assert.equal(typeof err.niceAddress, 'string')
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('.close()', function () {
|
||||
it('closes all servers', function () {
|
||||
const closed = []
|
||||
let callCount = 0
|
||||
|
||||
const server = HttpServerPlus.create({
|
||||
createServer: () => {
|
||||
const id = callCount++
|
||||
const mock = createMock({ address: '127.0.0.1', port: id })
|
||||
mock.close = function () {
|
||||
closed.push(id)
|
||||
setImmediate(() => this.emit('close'))
|
||||
}
|
||||
return mock
|
||||
},
|
||||
})
|
||||
|
||||
return Promise.all([server.listen({ port: 0 }), server.listen({ port: 0 })])
|
||||
.then(function () {
|
||||
return server.close()
|
||||
})
|
||||
.then(function () {
|
||||
assert.equal(closed.length, 2)
|
||||
})
|
||||
})
|
||||
|
||||
it('emit the `close` event when all servers are closed', function () {
|
||||
const mock = createMock({ address: '127.0.0.1', port: 8080 })
|
||||
const server = HttpServerPlus.create({ createServer: () => mock })
|
||||
|
||||
let closeEmitted = false
|
||||
server.on('close', () => {
|
||||
closeEmitted = true
|
||||
})
|
||||
|
||||
return server
|
||||
.listen({ port: 8080 })
|
||||
.then(function () {
|
||||
return server.close()
|
||||
})
|
||||
.then(function () {
|
||||
assert.ok(closeEmitted)
|
||||
})
|
||||
})
|
||||
|
||||
it('emit the `close` event even if there are no servers', function () {
|
||||
const server = HttpServerPlus.create()
|
||||
|
||||
// close() with no servers returns Promise.resolve() immediately (before emitting
|
||||
// 'close' via setImmediate), so we must wait for the event itself, not the promise.
|
||||
return new Promise(function (resolve) {
|
||||
server.close(resolve)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,38 +0,0 @@
|
||||
{
|
||||
"name": "@vates/http-server-plus",
|
||||
"version": "2.0.2",
|
||||
"license": "ISC",
|
||||
"description": "Augmented `http.Server`, HTTP/HTTPS/HTTP2 and multiple ports on the same instance",
|
||||
"keywords": [
|
||||
"http",
|
||||
"https",
|
||||
"server",
|
||||
"ports"
|
||||
],
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/http-server-plus",
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"repository": {
|
||||
"directory": "@vates/http-server-plus",
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"dependencies": {
|
||||
"promise-toolbox": "^0.21.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint-plugin-n": "^15.1.0"
|
||||
},
|
||||
"scripts": {
|
||||
"pretest": "eslint .",
|
||||
"test": "node --test index.spec.js",
|
||||
"postversion": "npm publish --access public"
|
||||
},
|
||||
"private": false,
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
"url": "https://vates.fr"
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
The use for this library is to create a composite key from a JSON value, ignoring objects identity and properties order.
|
||||
The use for this librariy is to create a composite key from a JSON value, ignoring objects identity and properties order.
|
||||
|
||||
The hash algorithm used is intended to be fast and with low collisions and is not guaranteed to be be secure.
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ npm install --save @vates/json-hash
|
||||
|
||||
## Usage
|
||||
|
||||
The use for this library is to create a composite key from a JSON value, ignoring objects identity and properties order.
|
||||
The use for this librariy is to create a composite key from a JSON value, ignoring objects identity and properties order.
|
||||
|
||||
The hash algorithm used is intended to be fast and with low collisions and is not guaranteed to be be secure.
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
const { jsonHash } = require('@vates/json-hash')
|
||||
const assert = require('node:assert/strict')
|
||||
const test = require('node:test')
|
||||
const test = require('test')
|
||||
|
||||
const cases = [
|
||||
[null, 'dCNOmK_nSY-12vHzasLXiswzlGT5UHA7jAGYkvmCuQs'],
|
||||
|
||||
@@ -23,8 +23,11 @@
|
||||
"engines": {
|
||||
"node": ">=14.18.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { MultiKeyMap } = require('./')
|
||||
|
||||
@@ -24,6 +24,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.2.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,217 +0,0 @@
|
||||
import { RandomAccessDisk } from '@xen-orchestra/disk-transform'
|
||||
import NbdClient from '@vates/nbd-client'
|
||||
|
||||
/**
|
||||
* @typedef {{ offset: number, length: number }} DataRange
|
||||
*/
|
||||
|
||||
/**
|
||||
* @extends {RandomAccessDisk}
|
||||
*/
|
||||
export class NbdDisk extends RandomAccessDisk {
|
||||
#nbdInfos
|
||||
/** @type {NbdClient|undefined} */
|
||||
#nbdClient
|
||||
|
||||
/** @type {Array<DataRange> | undefined} */
|
||||
#dataMap
|
||||
|
||||
/** @type {number} */
|
||||
#blockSize
|
||||
|
||||
/** @type {number | undefined} */
|
||||
#hasBlockCursor
|
||||
|
||||
/** @type {number | undefined} */
|
||||
#hasBlockPreviousIndex
|
||||
|
||||
constructor(nbdInfos, blockSize, { dataMap } = {}) {
|
||||
super()
|
||||
this.#blockSize = blockSize
|
||||
this.#nbdInfos = nbdInfos
|
||||
this.#dataMap = dataMap && this.#processDatamap(dataMap)
|
||||
}
|
||||
|
||||
#processDatamap(rawDataMap) {
|
||||
const ranges = rawDataMap
|
||||
.filter(({ type, length }) => type === 0 && length > 0)
|
||||
.map(({ offset, length }) => ({ offset, length }))
|
||||
.sort(({ offset: offset1 }, { offset: offset2 }) => offset1 - offset2)
|
||||
|
||||
// hasBlock()'s forward-only cursor is only correct if the extents are
|
||||
// sorted AND disjoint: an earlier extent must never reach into a block
|
||||
// after a later one, otherwise the cursor could skip it and wrongly report
|
||||
// a block as empty, dropping data and corrupting the output.
|
||||
// Touching extents (cur.offset === prevEnd) are merged
|
||||
const merged = []
|
||||
for (const range of ranges) {
|
||||
const last = merged[merged.length - 1]
|
||||
if (last !== undefined) {
|
||||
const lastEnd = last.offset + last.length
|
||||
if (range.offset < lastEnd) {
|
||||
throw new Error(
|
||||
`overlapping ranges in data map: [${last.offset}, ${lastEnd}) and [${range.offset}, ${range.offset + range.length})`
|
||||
)
|
||||
}
|
||||
if (range.offset === lastEnd) {
|
||||
// touching: extend the previous extent
|
||||
last.length += range.length
|
||||
continue
|
||||
}
|
||||
}
|
||||
// gap (or first range): new extent
|
||||
merged.push(range)
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
* @returns {Promise<DiskBlock>}
|
||||
*/
|
||||
async readBlock(index) {
|
||||
if (!this.#nbdClient) {
|
||||
throw new Error("can't readBlock before init")
|
||||
}
|
||||
let data = await this.#nbdClient.readBlock(index, this.getBlockSize())
|
||||
if (data.length !== this.getBlockSize()) {
|
||||
// this condition only work on non aligned disks
|
||||
// we won't accept truncated block from unaligned disks
|
||||
if (index === Math.ceil(this.getVirtualSize() / this.getBlockSize()) - 1) {
|
||||
// last block add zeros at the end
|
||||
data = Buffer.concat([data], this.getBlockSize())
|
||||
} else {
|
||||
throw new Error(`Block ${index} is not at the right size, expecting ${this.getBlockSize()}, got ${data.length}`)
|
||||
}
|
||||
}
|
||||
return {
|
||||
index,
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
getVirtualSize() {
|
||||
if (!this.#nbdClient) {
|
||||
throw new Error("can't get size before init")
|
||||
}
|
||||
return Number(this.#nbdClient.exportSize)
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {number}
|
||||
*/
|
||||
getBlockSize() {
|
||||
return this.#blockSize
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async init() {
|
||||
this.#nbdClient = new NbdClient(this.#nbdInfos)
|
||||
await this.#nbdClient.connect()
|
||||
if (this.#dataMap === undefined) {
|
||||
this.#dataMap = this.#processDatamap(await this.#nbdClient.getMap())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async close() {
|
||||
await this.#nbdClient?.disconnect()
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isDifferencing() {
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Array<number>}
|
||||
*/
|
||||
getBlockIndexes() {
|
||||
const indexes = new Set()
|
||||
this.#dataMap?.forEach(({ offset, length }) => {
|
||||
if (length === 0) return
|
||||
|
||||
const firstBlockIndex = Math.floor(offset / this.getBlockSize())
|
||||
const lastBlockIndex = Math.floor((offset + length - 1) / this.getBlockSize())
|
||||
|
||||
for (let blockIndex = firstBlockIndex; blockIndex <= lastBlockIndex; blockIndex++) {
|
||||
indexes.add(blockIndex)
|
||||
}
|
||||
})
|
||||
return [...indexes].sort((a, b) => a - b)
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the allocated blocks without materializing the index list.
|
||||
*
|
||||
* @returns {number}
|
||||
*/
|
||||
getBlockIndexesCount() {
|
||||
if (!this.#dataMap) {
|
||||
throw new Error("can't getBlockIndexesCount before init")
|
||||
}
|
||||
|
||||
const blockSize = this.getBlockSize()
|
||||
|
||||
let count = 0
|
||||
let lastCountedBlock = -1
|
||||
for (const { offset, length } of this.#dataMap) {
|
||||
const firstBlockIndex = Math.floor(offset / blockSize)
|
||||
const lastBlockIndex = Math.floor((offset + length - 1) / blockSize)
|
||||
const from = Math.max(firstBlockIndex, lastCountedBlock + 1)
|
||||
|
||||
if (lastBlockIndex >= from) {
|
||||
count += lastBlockIndex - from + 1
|
||||
lastCountedBlock = lastBlockIndex
|
||||
}
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} index
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasBlock(index) {
|
||||
if (!this.#dataMap) {
|
||||
throw new Error("can't hasBlock before init")
|
||||
}
|
||||
|
||||
const blockSize = this.getBlockSize()
|
||||
const blockStart = index * blockSize
|
||||
const blockEnd = blockStart + blockSize
|
||||
|
||||
let startExtentIndex = 0
|
||||
if (this.#hasBlockCursor !== undefined && index >= this.#hasBlockPreviousIndex) {
|
||||
startExtentIndex = this.#hasBlockCursor
|
||||
} else {
|
||||
this.#hasBlockCursor = undefined
|
||||
}
|
||||
this.#hasBlockPreviousIndex = index
|
||||
|
||||
const dataMap = this.#dataMap
|
||||
const l = dataMap.length
|
||||
for (let i = startExtentIndex; i < l; i++) {
|
||||
const { offset, length } = dataMap[i]
|
||||
if (offset >= blockEnd) {
|
||||
// extents are sorted: nothing from here on can overlap this block
|
||||
break
|
||||
}
|
||||
if (offset + length > blockStart) {
|
||||
this.#hasBlockCursor = i
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
import { describe, it } from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
|
||||
import { NbdDisk } from './NbdDisk.mjs'
|
||||
|
||||
const KiB = 1024
|
||||
const MiB = 1024 * KiB
|
||||
const TiB = 1024 * 1024 * MiB
|
||||
|
||||
const SOURCE_BLOCK_SIZE = 2 * MiB // VHD_BLOCK_SIZE, the block size used when importing from VMware
|
||||
const CLUSTER_SIZE = 64 * KiB // qcow2 cluster size
|
||||
|
||||
// hasBlock() only needs the dataMap (passed through the constructor) and the
|
||||
// block size, so we can exercise it without a live NBD connection.
|
||||
describe('NbdDisk.hasBlock', () => {
|
||||
it('reports allocated and unallocated blocks correctly', () => {
|
||||
const dataMap = [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE }, // block 0
|
||||
{ type: 0, offset: 5 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // block 5
|
||||
]
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
|
||||
assert.strictEqual(disk.hasBlock(0), true)
|
||||
assert.strictEqual(disk.hasBlock(1), false)
|
||||
assert.strictEqual(disk.hasBlock(4), false)
|
||||
assert.strictEqual(disk.hasBlock(5), true)
|
||||
assert.strictEqual(disk.hasBlock(6), false)
|
||||
})
|
||||
|
||||
it('handles repeated and out-of-order queries with the resume cursor', () => {
|
||||
const dataMap = [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE }, // block 0
|
||||
{ type: 0, offset: 5 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // block 5
|
||||
]
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
|
||||
// repeated indexes (the qcow2 path queries each 2MB block ~32 times as it
|
||||
// walks 64KB clusters) must return a stable answer
|
||||
assert.strictEqual(disk.hasBlock(5), true)
|
||||
assert.strictEqual(disk.hasBlock(5), true)
|
||||
assert.strictEqual(disk.hasBlock(6), false)
|
||||
assert.strictEqual(disk.hasBlock(6), false)
|
||||
|
||||
// querying backwards must still be correct (falls back to a full scan)
|
||||
assert.strictEqual(disk.hasBlock(0), true)
|
||||
assert.strictEqual(disk.hasBlock(5), true)
|
||||
})
|
||||
|
||||
it('invalidates the cursor on a backward miss (no stale skip)', () => {
|
||||
const dataMap = [
|
||||
{ type: 0, offset: 3 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // block 3
|
||||
{ type: 0, offset: 10 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // block 10
|
||||
]
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
|
||||
assert.strictEqual(disk.hasBlock(10), true) // forward hit: cursor parks on the high extent
|
||||
assert.strictEqual(disk.hasBlock(1), false) // backward miss: must invalidate the cursor
|
||||
assert.strictEqual(disk.hasBlock(3), true) // lower extent must not be skipped
|
||||
})
|
||||
|
||||
it('throws on overlapping ranges (would break the resume cursor)', () => {
|
||||
const dataMap = [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE },
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE / 2, length: SOURCE_BLOCK_SIZE }, // overlaps the previous range
|
||||
]
|
||||
assert.throws(() => new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap }), /overlapping ranges/)
|
||||
})
|
||||
|
||||
it('merges touching ranges and drops zero-length / non type-0 entries', () => {
|
||||
const dataMap = [
|
||||
{ type: 0, offset: 2 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // [2, 3)
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE / 2 }, // [0, .5)
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // [1, 2)
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE / 2, length: SOURCE_BLOCK_SIZE / 2 }, // [.5, 1), touches [0, .5)
|
||||
{ type: 1, offset: 10 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // wrong type: ignored
|
||||
{ type: 0, offset: 7 * SOURCE_BLOCK_SIZE, length: 0 }, // zero length: ignored
|
||||
]
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
|
||||
assert.strictEqual(disk.hasBlock(0), true)
|
||||
assert.strictEqual(disk.hasBlock(1), true)
|
||||
assert.strictEqual(disk.hasBlock(2), true)
|
||||
assert.strictEqual(disk.hasBlock(3), false)
|
||||
assert.strictEqual(disk.hasBlock(7), false) // zero-length range dropped
|
||||
assert.strictEqual(disk.hasBlock(10), false) // non type-0 range dropped
|
||||
})
|
||||
})
|
||||
|
||||
describe('NbdDisk.getBlockIndexesCount', () => {
|
||||
const cases = {
|
||||
'aligned, disjoint blocks': [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE }, // block 0
|
||||
{ type: 0, offset: 5 * SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE }, // block 5
|
||||
],
|
||||
'a range spanning several blocks': [
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE / 2, length: 3 * SOURCE_BLOCK_SIZE }, // blocks 0,1,2,3
|
||||
],
|
||||
'two disjoint extents sharing block 0 (needs dedup)': [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE / 2 }, // [0, .5) => block 0
|
||||
{ type: 0, offset: 3 * (SOURCE_BLOCK_SIZE / 4), length: SOURCE_BLOCK_SIZE }, // [.75, 1.75) => blocks 0,1
|
||||
],
|
||||
'touching sub-block fragments': [
|
||||
{ type: 0, offset: 0, length: SOURCE_BLOCK_SIZE / 2 },
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE / 2, length: SOURCE_BLOCK_SIZE / 2 },
|
||||
{ type: 0, offset: SOURCE_BLOCK_SIZE, length: SOURCE_BLOCK_SIZE },
|
||||
],
|
||||
empty: [],
|
||||
}
|
||||
|
||||
for (const [name, dataMap] of Object.entries(cases)) {
|
||||
it(`matches getBlockIndexes().length: ${name}`, () => {
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
assert.strictEqual(disk.getBlockIndexesCount(), disk.getBlockIndexes().length)
|
||||
})
|
||||
}
|
||||
|
||||
it('matches getBlockIndexes().length on the large sparse map', () => {
|
||||
const virtualSize = Math.floor(3.5 * TiB)
|
||||
const NB_RANGES = 113528
|
||||
const dataMap = []
|
||||
for (let i = 0; i < NB_RANGES; i++) {
|
||||
dataMap.push({ type: 0, offset: Math.floor((i / NB_RANGES) * virtualSize), length: CLUSTER_SIZE })
|
||||
}
|
||||
const disk = new NbdDisk({}, SOURCE_BLOCK_SIZE, { dataMap })
|
||||
assert.strictEqual(disk.getBlockIndexesCount(), disk.getBlockIndexes().length)
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
export const INIT_PASSWD = Buffer.from('NBDMAGIC') // "NBDMAGIC" ensure we're connected to a nbd server
|
||||
export const OPTS_MAGIC = Buffer.from('IHAVEOPT') // "IHAVEOPT" start an option block
|
||||
export const NBD_OPT_REPLY_MAGIC = 1100100111001001n // magic received during negotiation
|
||||
export const NBD_OPT_REPLY_MAGIC = 1100100111001001n // magic received during negociation
|
||||
export const NBD_OPT_EXPORT_NAME = 1
|
||||
export const NBD_OPT_ABORT = 2
|
||||
export const NBD_OPT_LIST = 3
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import assert from 'node:assert'
|
||||
import { Socket } from 'node:net'
|
||||
import { connect } from 'node:tls'
|
||||
import { pRetry, pDelay, pTimeout, pFromCallback } from 'promise-toolbox'
|
||||
import { fromCallback, pRetry, pDelay, pTimeout, pFromCallback } from 'promise-toolbox'
|
||||
import { readChunkStrict } from '@vates/read-chunk'
|
||||
import { createLogger } from '@xen-orchestra/log'
|
||||
import {
|
||||
@@ -20,8 +20,6 @@ import {
|
||||
OPTS_MAGIC,
|
||||
NBD_CMD_DISC,
|
||||
} from './constants.mjs'
|
||||
import { spawn } from 'node:child_process'
|
||||
|
||||
const { warn } = createLogger('vates:nbd-client')
|
||||
|
||||
// documentation is here : https://github.com/NetworkBlockDevice/nbd/blob/master/doc/proto.md
|
||||
@@ -44,9 +42,9 @@ export default class NbdClient {
|
||||
// AFAIK, there is no guaranty the server answers in the same order as the queries
|
||||
// so we handle a backlog of command waiting for response and handle concurrency manually
|
||||
|
||||
#waitingForResponse // there is already a listener waiting for a response
|
||||
#waitingForResponse // there is already a listenner waiting for a response
|
||||
#nextCommandQueryId = BigInt(0)
|
||||
#commandQueryBacklog // map of command waiting for a response queryId => { size/*in byte*/, resolve, reject}
|
||||
#commandQueryBacklog // map of command waiting for an response queryId => { size/*in byte*/, resolve, reject}
|
||||
#connected = false
|
||||
|
||||
#reconnectingPromise
|
||||
@@ -197,7 +195,7 @@ export default class NbdClient {
|
||||
}
|
||||
|
||||
// send export name we want to access.
|
||||
// it's implicitly closing the negotiation phase.
|
||||
// it's implictly closing the negociation phase.
|
||||
await this.#write(OPTS_MAGIC)
|
||||
await this.#writeInt32(NBD_OPT_EXPORT_NAME)
|
||||
const exportNameBuffer = Buffer.from(this.#exportName)
|
||||
@@ -220,22 +218,8 @@ export default class NbdClient {
|
||||
}
|
||||
|
||||
#write(buffer) {
|
||||
let timeout
|
||||
const messageTimeout = this.#messageTimeout
|
||||
const socket = this.#serverSocket
|
||||
return Promise.race([
|
||||
new Promise((resolve, reject) => {
|
||||
timeout = setTimeout(() => {
|
||||
reject(new Error('timeout'))
|
||||
}, messageTimeout)
|
||||
}),
|
||||
new Promise(resolve =>
|
||||
socket.write(buffer, () => {
|
||||
clearTimeout(timeout)
|
||||
resolve()
|
||||
})
|
||||
),
|
||||
])
|
||||
const promise = fromCallback.call(this.#serverSocket, 'write', buffer)
|
||||
return pTimeout.call(promise, this.#messageTimeout)
|
||||
}
|
||||
|
||||
async #readInt32() {
|
||||
@@ -353,56 +337,10 @@ export default class NbdClient {
|
||||
async readBlock(index, size = NBD_DEFAULT_BLOCK_SIZE) {
|
||||
return pRetry(() => this.#readBlock(index, size), {
|
||||
tries: this.#readBlockRetries,
|
||||
when: error => error.code !== 'ERR_ABORTED',
|
||||
onRetry: async err => {
|
||||
warn('will retry reading block ', index, err)
|
||||
await this.reconnect()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the map of the file with holes, zeros and data, useful to handle efficiently sparse source
|
||||
* to implement this internally: use structure response if the server supports it, and then ask for BLOCK_STATUS *
|
||||
*
|
||||
* @returns {Promise<{ offset: number, length: number, type: number }[]>}
|
||||
* A promise that resolves to an array where each object represents a segment:
|
||||
* - `offset` — The byte offset from the start.
|
||||
* - `length` — The size of the segment in bytes.
|
||||
* - `type` — A numeric code indicating the segment type ( 0 means data).
|
||||
*/
|
||||
/* async */ getMap(signal) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const process = spawn('nbdinfo', [
|
||||
'--json',
|
||||
'--map',
|
||||
`nbd://${this.#serverAddress}:${this.#serverPort}/${encodeURIComponent(this.#exportName)}`,
|
||||
])
|
||||
let text = ''
|
||||
let errText = ''
|
||||
process.stdout.on('data', data => (text += data))
|
||||
process.stderr.on('data', data => (errText += data))
|
||||
const onAbort = () => {
|
||||
process.kill()
|
||||
reject(signal.reason)
|
||||
}
|
||||
signal?.addEventListener('abort', onAbort, { once: true })
|
||||
process.on('error', err => {
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
reject(err)
|
||||
})
|
||||
process.on('close', code => {
|
||||
signal?.removeEventListener('abort', onAbort)
|
||||
if (code !== 0) {
|
||||
return reject(new Error(`Error during getMap (code: ${code}): ${errText}`))
|
||||
}
|
||||
try {
|
||||
const json = JSON.parse(text)
|
||||
resolve(json)
|
||||
} catch (error) {
|
||||
reject(new Error(`${error} ${text}`))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,13 @@ export default class MultiNbdClient {
|
||||
* open nbdConcurrency connections to NBD servers
|
||||
* it must obtain at least one connection to succeed
|
||||
* it tries to spread connections on multiple host
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async connect() {
|
||||
const candidates = [...this.#settings]
|
||||
|
||||
const promises = []
|
||||
const baseOptions = this.#options
|
||||
const _connect = async () => {
|
||||
const _connect = async () => {
|
||||
if (candidates.length === 0) {
|
||||
return
|
||||
}
|
||||
@@ -64,15 +63,13 @@ export default class MultiNbdClient {
|
||||
return _connect()
|
||||
}
|
||||
}
|
||||
// don't connect in parallel since this can lead to race condition
|
||||
// on distributed systems ( like the NBD server of the XAPI)
|
||||
for (let i = 0; i < this.#nbdConcurrency; i++) {
|
||||
await _connect()
|
||||
promises.push(_connect())
|
||||
}
|
||||
await Promise.all(promises)
|
||||
|
||||
if (this.#clients.length === 0) {
|
||||
const error = new Error(`Fail to connect to any Nbd client`, { nbdInfos: this.#settings })
|
||||
error.code = 'NO_NBD_AVAILABLE'
|
||||
throw error
|
||||
throw new Error(`Fail to connect to any Nbd client`)
|
||||
}
|
||||
if (this.#clients.length < this.#nbdConcurrency) {
|
||||
warn(
|
||||
@@ -81,30 +78,17 @@ export default class MultiNbdClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async disconnect() {
|
||||
await asyncEach(this.#clients, client => client.disconnect(), {
|
||||
stopOnError: false,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} index
|
||||
* @param {number} size
|
||||
* @returns {Promise<Buffer>}
|
||||
*/
|
||||
async readBlock(index, size = NBD_DEFAULT_BLOCK_SIZE) {
|
||||
const clientId = index % this.#clients.length
|
||||
return this.#clients[clientId].readBlock(index, size)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {AsyncGenerator<Buffer>} indexGenerator
|
||||
*/
|
||||
async *readBlocks(indexGenerator) {
|
||||
// default : read all blocks
|
||||
const readAhead = []
|
||||
@@ -129,17 +113,4 @@ export default class MultiNbdClient {
|
||||
yield readAhead.shift()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* returns the map of the file with holes, zeros and data, useful to handle efficiently sparse source *
|
||||
*
|
||||
* @returns {Promise<{ offset: number, length: number, type: number }[]>}
|
||||
* A promise that resolves to an array where each object represents a segment:
|
||||
* - `offset` — The byte offset from the start.
|
||||
* - `length` — The size of the segment in bytes.
|
||||
* - `type` — A numeric code indicating the segment type (0 means no data).
|
||||
*/
|
||||
async getMap(signal) {
|
||||
// ask the map from one of the connected client
|
||||
return this.#clients[Math.floor(this.#clients.length * Math.random())].getMap(signal)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,19 +13,18 @@
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"version": "3.5.0",
|
||||
"version": "3.1.0",
|
||||
"engines": {
|
||||
"node": ">=14.0"
|
||||
},
|
||||
"main": "./index.mjs",
|
||||
"dependencies": {
|
||||
"@vates/async-each": "^1.0.3",
|
||||
"@vates/read-chunk": "^1.2.1",
|
||||
"@xen-orchestra/async-map": "^0.1.3",
|
||||
"@xen-orchestra/disk-transform": "^1.3.3",
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"@vates/async-each": "^1.0.0",
|
||||
"@vates/read-chunk": "^1.2.0",
|
||||
"@xen-orchestra/async-map": "^0.1.2",
|
||||
"@xen-orchestra/log": "^0.6.0",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"xen-api": "^4.8.0"
|
||||
"xen-api": "^4.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tap": "^18.7.0",
|
||||
|
||||
@@ -24,7 +24,7 @@ $ npm install node-vsphere-soap --save
|
||||
|
||||
## Sample Code
|
||||
|
||||
### To connect to a vCenter server
|
||||
### To connect to a vCenter server:
|
||||
|
||||
var nvs = require('node-vsphere-soap');
|
||||
var vc = new nvs.Client(host, user, password, sslVerify);
|
||||
@@ -54,7 +54,7 @@ $ npm install node-vsphere-soap --save
|
||||
- userName - username of authenticated user
|
||||
- fullName - full name of authenticated user
|
||||
|
||||
### To run a command
|
||||
### To run a command:
|
||||
|
||||
var vcCmd = vc.runCommand( commandToRun, arguments );
|
||||
vcCmd.once('result', function( result, raw, soapHeader) {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
This file creates the Client class
|
||||
|
||||
- when the class is instantiated, a connection will be made to the ESXi/vCenter server to verify that the creds are good
|
||||
- upon a bad login, the connection will be terminated
|
||||
- upon a bad login, the connnection will be terminated
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@vates/node-vsphere-soap",
|
||||
"version": "2.1.3",
|
||||
"version": "2.1.0",
|
||||
"description": "interface to vSphere SOAP/WSDL from node for interfacing with vCenter or ESXi, forked from node-vsphere-soap",
|
||||
"main": "lib/client.mjs",
|
||||
"author": "reedog117",
|
||||
@@ -11,11 +11,14 @@
|
||||
},
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"dependencies": {
|
||||
"@xen-orchestra/log": "^0.7.2",
|
||||
"axios": "^1.15.0",
|
||||
"@xen-orchestra/log": "^0.6.0",
|
||||
"axios": "^1.4.0",
|
||||
"soap": "^1.0.0",
|
||||
"soap-cookie": "^0.10.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
},
|
||||
"keywords": [
|
||||
"vsphere",
|
||||
"vcenter",
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import assert from 'assert'
|
||||
import { describe, it } from 'node:test'
|
||||
import { describe, it } from 'test'
|
||||
|
||||
import * as vc from '../lib/client.mjs'
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
### Usual workflow
|
||||
|
||||
> This section presents how this library should be used to implement a classic two factor authentication.
|
||||
> This section presents how this library should be used to implement a classic two factor authentification.
|
||||
|
||||
#### Setup
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ npm install --save @vates/otp
|
||||
|
||||
### Usual workflow
|
||||
|
||||
> This section presents how this library should be used to implement a classic two factor authentication.
|
||||
> This section presents how this library should be used to implement a classic two factor authentification.
|
||||
|
||||
#### Setup
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { strict as assert } from 'node:assert'
|
||||
import test from 'node:test'
|
||||
import test from 'test'
|
||||
|
||||
import {
|
||||
generateHotp,
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert/strict')
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
|
||||
const { every, not, some } = require('./')
|
||||
|
||||
|
||||
@@ -32,6 +32,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,17 +71,13 @@ exports.readChunk = readChunk
|
||||
* If `size` bytes are not available to be read, the returned promise is rejected.
|
||||
*
|
||||
* @param {Readable} stream - A readable stream to read from.
|
||||
* @param {number?} [size] - The number of bytes to read for binary streams (ignored for object streams).
|
||||
* @param {number} [size] - The number of bytes to read for binary streams (ignored for object streams).
|
||||
* @returns {Promise<Buffer|string|unknown>} - A Promise that resolves to the read chunk.
|
||||
*/
|
||||
exports.readChunkStrict = async function readChunkStrict(stream, size) {
|
||||
const chunk = await readChunk(stream, size)
|
||||
if (chunk === null) {
|
||||
if (size === undefined) {
|
||||
throw new Error(`stream has ended without data`)
|
||||
} else {
|
||||
throw new Error(`stream has ended without data, was looking for ${size} bytes`)
|
||||
}
|
||||
throw new Error('stream has ended without data')
|
||||
}
|
||||
|
||||
if (size !== undefined && chunk.length !== size) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const assert = require('node:assert').strict
|
||||
|
||||
const { Readable } = require('stream')
|
||||
|
||||
@@ -19,18 +19,21 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"version": "1.2.1",
|
||||
"version": "1.2.0",
|
||||
"engines": {
|
||||
"node": ">=8.10"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.2.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"isutf8": "^4.0.0"
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
const assert = require('node:assert').strict
|
||||
|
||||
const { Readable } = require('stream')
|
||||
|
||||
@@ -31,6 +31,9 @@
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,30 +49,21 @@ const result = await task.runInside(fn)
|
||||
// if fn rejects, the task will be marked as failed
|
||||
// if fn resolves, the task will be marked as succeeded
|
||||
const result = await task.run(fn)
|
||||
|
||||
// manually starts the task
|
||||
task.start()
|
||||
|
||||
// manually finishes the task in success state
|
||||
task.success(result)
|
||||
|
||||
// manually finishes the task in failure state
|
||||
task.failure(error)
|
||||
```
|
||||
|
||||
Inside a task:
|
||||
|
||||
```js
|
||||
// the abort signal of the current task if any; otherwise, is `undefined`
|
||||
// the abort signal of the current task if any, otherwise is `undefined`
|
||||
Task.abortSignal
|
||||
|
||||
// sends an info on the current task if any; otherwise, does nothing
|
||||
// sends an info on the current task if any, otherwise does nothing
|
||||
Task.info(message, data)
|
||||
|
||||
// sends an info on the current task if any; otherwise, does nothing
|
||||
// sends an info on the current task if any, otherwise does nothing
|
||||
Task.warning(message, data)
|
||||
|
||||
// attaches a property to the current task if any; otherwise, does nothing
|
||||
// attaches a property to the current task if any, otherwise does nothing
|
||||
//
|
||||
// the latest value takes precedence
|
||||
//
|
||||
@@ -149,7 +140,7 @@ const onProgress = makeOnProgress({
|
||||
|
||||
// This function is called each time a root task or a subtask is updated.
|
||||
//
|
||||
// `taskLog.$root` can be used to unconditionally access the root task.
|
||||
// `taskLog.$root` can be used to uncondionally access the root task.
|
||||
onTaskUpdate(taskLog) {},
|
||||
})
|
||||
|
||||
|
||||
@@ -65,30 +65,21 @@ const result = await task.runInside(fn)
|
||||
// if fn rejects, the task will be marked as failed
|
||||
// if fn resolves, the task will be marked as succeeded
|
||||
const result = await task.run(fn)
|
||||
|
||||
// manually starts the task
|
||||
task.start()
|
||||
|
||||
// manually finishes the task in success state
|
||||
task.success(result)
|
||||
|
||||
// manually finishes the task in failure state
|
||||
task.failure(error)
|
||||
```
|
||||
|
||||
Inside a task:
|
||||
|
||||
```js
|
||||
// the abort signal of the current task if any; otherwise, is `undefined`
|
||||
// the abort signal of the current task if any, otherwise is `undefined`
|
||||
Task.abortSignal
|
||||
|
||||
// sends an info on the current task if any; otherwise, does nothing
|
||||
// sends an info on the current task if any, otherwise does nothing
|
||||
Task.info(message, data)
|
||||
|
||||
// sends an info on the current task if any; otherwise, does nothing
|
||||
// sends an info on the current task if any, otherwise does nothing
|
||||
Task.warning(message, data)
|
||||
|
||||
// attaches a property to the current task if any; otherwise, does nothing
|
||||
// attaches a property to the current task if any, otherwise does nothing
|
||||
//
|
||||
// the latest value takes precedence
|
||||
//
|
||||
@@ -165,7 +156,7 @@ const onProgress = makeOnProgress({
|
||||
|
||||
// This function is called each time a root task or a subtask is updated.
|
||||
//
|
||||
// `taskLog.$root` can be used to unconditionally access the root task.
|
||||
// `taskLog.$root` can be used to uncondionally access the root task.
|
||||
onTaskUpdate(taskLog) {},
|
||||
})
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ exports.makeOnProgress = function ({ onRootTaskEnd = noop, onRootTaskStart = noo
|
||||
Object.defineProperty(taskLog, '$root', { value: taskLog })
|
||||
|
||||
// start of a root task
|
||||
onRootTaskStart(taskLog, event)
|
||||
onRootTaskStart(taskLog)
|
||||
} else {
|
||||
// start of a subtask
|
||||
const parent = taskLogs.get(parentId)
|
||||
@@ -54,10 +54,10 @@ exports.makeOnProgress = function ({ onRootTaskEnd = noop, onRootTaskStart = noo
|
||||
}
|
||||
|
||||
if (type === 'end' && taskLog.$root === taskLog) {
|
||||
onRootTaskEnd(taskLog, event)
|
||||
onRootTaskEnd(taskLog)
|
||||
}
|
||||
}
|
||||
|
||||
onTaskUpdate(taskLog, event)
|
||||
onTaskUpdate(taskLog)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('node:assert').strict
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
|
||||
const { makeOnProgress } = require('./combineEvents.js')
|
||||
const { Task } = require('./index.js')
|
||||
|
||||
@@ -7,27 +7,6 @@ function alreadyEnded() {
|
||||
throw new Error('task has already ended')
|
||||
}
|
||||
|
||||
const serializeErrors = errors => (Array.isArray(errors) ? errors.map(serializeError) : errors)
|
||||
|
||||
// Create a serializable object from an error.
|
||||
//
|
||||
// Otherwise some fields might be non-enumerable and missing from logs.
|
||||
const serializeError = error => {
|
||||
return error instanceof Error
|
||||
? {
|
||||
...error, // Copy enumerable properties.
|
||||
cause: error.cause,
|
||||
code: error.code,
|
||||
errors: serializeErrors(error.errors), // supports AggregateError
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
stack: error.stack,
|
||||
}
|
||||
: error
|
||||
}
|
||||
|
||||
exports.serializeError = serializeError
|
||||
|
||||
// define a read-only, non-enumerable, non-configurable property
|
||||
function define(object, property, value) {
|
||||
Object.defineProperty(object, property, { value })
|
||||
@@ -50,7 +29,7 @@ class Task {
|
||||
static get abortSignal() {
|
||||
const task = getTask()
|
||||
if (task !== undefined) {
|
||||
return task._abortController.signal
|
||||
return task.#abortController.signal
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,18 +65,11 @@ class Task {
|
||||
}
|
||||
}
|
||||
|
||||
// These two properties should not be used outside the class
|
||||
//
|
||||
// To ensure compatibility between multiple versions of this lib, they are kept public for now
|
||||
_abortController = new AbortController()
|
||||
_onProgress
|
||||
#abortController = new AbortController()
|
||||
#onProgress
|
||||
|
||||
get id() {
|
||||
// Use a compact, sortable, string representation of the creation date,
|
||||
// and add a random part to differentiate tasks created at the same time
|
||||
//
|
||||
// Due to the padding, dates are sortable up to 5188-04-22T11:04:28.415Z
|
||||
return (this.id = Date.now().toString(36).padStart(9, '0') + '-' + Math.random().toString(36).slice(2))
|
||||
return (this.id = Math.random().toString(36).slice(2))
|
||||
}
|
||||
set id(value) {
|
||||
define(this, 'id', value)
|
||||
@@ -114,23 +86,23 @@ class Task {
|
||||
this.#startData = { properties }
|
||||
|
||||
if (onProgress !== undefined) {
|
||||
this._onProgress = onProgress
|
||||
this.#onProgress = onProgress
|
||||
} else {
|
||||
const parent = getTask()
|
||||
if (parent !== undefined) {
|
||||
const { signal } = parent._abortController
|
||||
const { signal } = parent.#abortController
|
||||
signal.addEventListener('abort', () => {
|
||||
this._abortController.abort(signal.reason)
|
||||
this.#abortController.abort(signal.reason)
|
||||
})
|
||||
|
||||
this._onProgress = parent._onProgress
|
||||
this.#onProgress = parent.#onProgress
|
||||
this.#startData.parentId = parent.id
|
||||
} else {
|
||||
this._onProgress = noop
|
||||
this.#onProgress = noop
|
||||
}
|
||||
}
|
||||
|
||||
const { signal } = this._abortController
|
||||
const { signal } = this.#abortController
|
||||
signal.addEventListener('abort', () => {
|
||||
if (this.status === PENDING) {
|
||||
this.#maybeStart()
|
||||
@@ -145,7 +117,7 @@ class Task {
|
||||
}
|
||||
|
||||
abort(reason) {
|
||||
this._abortController.abort(reason)
|
||||
this.#abortController.abort(reason)
|
||||
}
|
||||
|
||||
#emit(type, data) {
|
||||
@@ -154,24 +126,15 @@ class Task {
|
||||
data.id = this.id
|
||||
data.timestamp = Date.now()
|
||||
data.type = type
|
||||
this._onProgress(data)
|
||||
this.#onProgress(data)
|
||||
}
|
||||
|
||||
#end(status, result) {
|
||||
assert.equal(this.#status, PENDING)
|
||||
assert.equal(this.#running, false)
|
||||
|
||||
if (result instanceof Error && result.toJSON === undefined) {
|
||||
result = serializeError(result)
|
||||
}
|
||||
|
||||
this.#emit('end', { status, result })
|
||||
this._onProgress = alreadyEnded
|
||||
this.#status = status
|
||||
}
|
||||
|
||||
failure(error) {
|
||||
this.#end(FAILURE, error)
|
||||
this.#emit('end', { status, result })
|
||||
this.#onProgress = alreadyEnded
|
||||
}
|
||||
|
||||
info(message, data) {
|
||||
@@ -179,8 +142,10 @@ class Task {
|
||||
}
|
||||
|
||||
#maybeStart() {
|
||||
if (this.#startData !== undefined) {
|
||||
this.start()
|
||||
const startData = this.#startData
|
||||
if (startData !== undefined) {
|
||||
this.#startData = undefined
|
||||
this.#emit('start', startData)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +170,6 @@ class Task {
|
||||
this.#running = false
|
||||
return result
|
||||
} catch (result) {
|
||||
this.#running = false
|
||||
this.#end(FAILURE, result)
|
||||
throw result
|
||||
}
|
||||
@@ -217,18 +181,6 @@ class Task {
|
||||
this.#emit('property', { name, value })
|
||||
}
|
||||
|
||||
start() {
|
||||
const startData = this.#startData
|
||||
assert.notEqual(startData, undefined, 'task has already started')
|
||||
|
||||
this.#startData = undefined
|
||||
this.#emit('start', startData)
|
||||
}
|
||||
|
||||
success(result) {
|
||||
this.#end(SUCCESS, result)
|
||||
}
|
||||
|
||||
warning(message, data) {
|
||||
assert.equal(this.status, PENDING)
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('node:assert').strict
|
||||
const { describe, it } = require('node:test')
|
||||
const { describe, it } = require('test')
|
||||
|
||||
const { makeOnProgress } = require('./combineEvents.js')
|
||||
const { Task, serializeError } = require('./index.js')
|
||||
const { Task } = require('./index.js')
|
||||
|
||||
const noop = Function.prototype
|
||||
|
||||
@@ -28,38 +28,8 @@ function createTask(opts) {
|
||||
return task
|
||||
}
|
||||
|
||||
describe('serializeError', function () {
|
||||
it('serializes simple error', function () {
|
||||
const err = new Error('test Error')
|
||||
err.code = 404
|
||||
const serialized = serializeError(err)
|
||||
assert.equal(serialized.message, 'test Error')
|
||||
assert.equal(serialized.name, 'Error')
|
||||
assert.equal(serialized.code, 404)
|
||||
assert.notEqual(serialized.stack, undefined)
|
||||
})
|
||||
|
||||
it('serializes aggregate error', function () {
|
||||
const err = new AggregateError([new Error('test error 1'), new Error('test error 2')], 'Error message', {
|
||||
cause: 'broken',
|
||||
})
|
||||
const serialized = serializeError(err)
|
||||
assert.equal(serialized.message, 'Error message')
|
||||
assert.equal(serialized.name, 'AggregateError')
|
||||
assert.equal(serialized.cause, 'broken')
|
||||
assert.notEqual(serialized.stack, undefined)
|
||||
assert.equal(serialized.errors.length, 2)
|
||||
assert.equal(serialized.errors[0].name, 'Error')
|
||||
assert.equal(serialized.errors[1].name, 'Error')
|
||||
assert.equal(serialized.errors[0].message, 'test error 1')
|
||||
assert.equal(serialized.errors[1].message, 'test error 2')
|
||||
assert.notEqual(serialized.errors[0].stack, undefined)
|
||||
assert.notEqual(serialized.errors[1].stack, undefined)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Task', function () {
|
||||
describe('constructor', function () {
|
||||
describe('contructor', function () {
|
||||
it('data properties are passed to the start event', async function () {
|
||||
const properties = { foo: 0, bar: 1 }
|
||||
const task = createTask({ properties })
|
||||
@@ -136,7 +106,7 @@ describe('Task', function () {
|
||||
assert.equal(task.$events.length, 3)
|
||||
assertEvent(task, { type: 'start' }, 0)
|
||||
assertEvent(task, { type: 'abortionRequested', reason }, 1)
|
||||
assertEvent(task, { type: 'end', status: 'failure', result: serializeError(result) }, 2)
|
||||
assertEvent(task, { type: 'end', status: 'failure', result }, 2)
|
||||
})
|
||||
|
||||
it('does not abort if the task succeed', async function () {
|
||||
@@ -175,43 +145,6 @@ describe('Task', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#failure()', function () {
|
||||
const error = new Error()
|
||||
|
||||
it('throws if the task has not started yet', function () {
|
||||
const task = createTask()
|
||||
|
||||
assert.throws(() => task.failure(error), { message: /^task has not started yet\b/ })
|
||||
})
|
||||
|
||||
it('throws if the task is running', function () {
|
||||
const task = createTask()
|
||||
task.runInside(noop)
|
||||
|
||||
assert.throws(() => task.failure(error), { code: 'ERR_ASSERTION' })
|
||||
})
|
||||
|
||||
it('throws if the task has already finished', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
task.success()
|
||||
|
||||
assert.throws(() => task.failure(error), { code: 'ERR_ASSERTION' })
|
||||
})
|
||||
|
||||
it('finishes the task and mark it as failed', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
task.failure(error)
|
||||
|
||||
assertEvent(task, {
|
||||
status: 'failure',
|
||||
result: serializeError(error),
|
||||
type: 'end',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.info()', function () {
|
||||
it('does nothing when run outside a task', function () {
|
||||
Task.info('foo')
|
||||
@@ -292,10 +225,10 @@ describe('Task', function () {
|
||||
|
||||
it('throws when the task is not started', function () {
|
||||
const task = createTask()
|
||||
assert.throws(() => task.set(name, value), { message: /^task has not started yet\b/ })
|
||||
assert.throws(() => task.set(name, value), { message: 'task has not started yet' })
|
||||
})
|
||||
|
||||
it(`emits a property message`, async function () {
|
||||
it(`emits an property message`, async function () {
|
||||
const task = createTask()
|
||||
await task.run(async () => {
|
||||
await Task.run(() => {
|
||||
@@ -311,65 +244,12 @@ describe('Task', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#start', function () {
|
||||
it('starts the task', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
|
||||
assertEvent(task, { type: 'start' })
|
||||
})
|
||||
|
||||
it('throws when the task has already started', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
|
||||
assert.throws(() => task.start(), { message: 'task has already started' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('#success()', function () {
|
||||
const result = {}
|
||||
|
||||
it('throws if the task has not started yet', function () {
|
||||
const task = createTask()
|
||||
|
||||
assert.throws(() => task.success(result), { message: /^task has not started yet\b/ })
|
||||
})
|
||||
|
||||
it('throws if the task is running', function () {
|
||||
const task = createTask()
|
||||
task.runInside(noop)
|
||||
|
||||
assert.throws(() => task.success(result), { code: 'ERR_ASSERTION' })
|
||||
})
|
||||
|
||||
it('throws if the task has already finished', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
task.success()
|
||||
|
||||
assert.throws(() => task.success(result), { code: 'ERR_ASSERTION' })
|
||||
})
|
||||
|
||||
it('finishes the task and mark it as successful', function () {
|
||||
const task = createTask()
|
||||
task.start()
|
||||
task.success(result)
|
||||
|
||||
assertEvent(task, {
|
||||
status: 'success',
|
||||
result,
|
||||
type: 'end',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('.warning()', function () {
|
||||
it('does nothing when run outside a task', function () {
|
||||
Task.warning('foo')
|
||||
})
|
||||
|
||||
it('emits a warning message when run inside a task', async function () {
|
||||
it('emits an warning message when run inside a task', async function () {
|
||||
const task = createTask()
|
||||
await task.run(() => {
|
||||
Task.warning('foo')
|
||||
@@ -386,7 +266,7 @@ describe('Task', function () {
|
||||
describe(`#${type}()`, function () {
|
||||
it('throws when the task is not started', function () {
|
||||
const task = createTask()
|
||||
assert.throws(() => task[type]('foo'), { message: /^task has not started yet\b/ })
|
||||
assert.throws(() => task[type]('foo'), { message: 'task has not started yet' })
|
||||
})
|
||||
|
||||
it(`emits an ${type} message`, async function () {
|
||||
@@ -505,7 +385,7 @@ describe('Task', function () {
|
||||
assert.equal(task.status, 'failure')
|
||||
assertEvent(task, {
|
||||
status: 'failure',
|
||||
result: serializeError(e),
|
||||
result: e,
|
||||
type: 'end',
|
||||
})
|
||||
})
|
||||
@@ -541,7 +421,7 @@ describe('Task', function () {
|
||||
assert.equal(task.status, 'failure')
|
||||
assertEvent(task, {
|
||||
status: 'failure',
|
||||
result: serializeError(e),
|
||||
result: e,
|
||||
type: 'end',
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,13 +13,16 @@
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"license": "ISC",
|
||||
"version": "0.7.0",
|
||||
"version": "0.4.1",
|
||||
"engines": {
|
||||
"node": ">=14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"test": "^3.3.0"
|
||||
},
|
||||
"scripts": {
|
||||
"postversion": "npm publish --access public",
|
||||
"test": "node --test"
|
||||
"test": "node--test"
|
||||
},
|
||||
"exports": {
|
||||
".": "./index.js",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../../scripts/npmignore
|
||||
@@ -1,21 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
import eslint from '@eslint/js'
|
||||
import tseslint from 'typescript-eslint'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import path from 'node:path'
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||
|
||||
export default tseslint.config(eslint.configs.recommended, tseslint.configs.recommended, {
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
},
|
||||
},
|
||||
ignores: ['dist'],
|
||||
rules: {
|
||||
// Allow empty interface for recursive generic typing (E.g. interface XenApiNetworkWrapped)
|
||||
'@typescript-eslint/no-empty-object-type': 'off',
|
||||
},
|
||||
})
|
||||
@@ -1,58 +0,0 @@
|
||||
{
|
||||
"name": "@vates/types",
|
||||
"private": false,
|
||||
"version": "1.30.1",
|
||||
"main": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"default": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.mts"
|
||||
},
|
||||
"./xo": "./dist/xo.d.mts",
|
||||
"./xo-app": "./dist/xo-app.d.mts",
|
||||
"./xen-api": "./dist/xen-api.d.mts",
|
||||
"./common": {
|
||||
"default": "./dist/common.mjs",
|
||||
"types": "./dist/common.d.mts"
|
||||
},
|
||||
"./lib/vates/*": {
|
||||
"default": "./dist/lib/vates-*.mjs",
|
||||
"type": "./dist/lib/vates-*.d.mts"
|
||||
},
|
||||
"./lib/xen-orchestra/*": {
|
||||
"default": "./dist/lib/xen-orchestra-*.mjs",
|
||||
"type": "./dist/lib/xen-orchestra-*.d.mts"
|
||||
},
|
||||
"./lib/complex-matcher": {
|
||||
"default": "./dist/lib/complex-matcher.mjs",
|
||||
"type": "./dist/lib/complex-matcher.d.mts"
|
||||
}
|
||||
},
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.19.0",
|
||||
"typescript": "~5.6.3",
|
||||
"typescript-eslint": "^8.61.0",
|
||||
"@types/express": "^5.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"prepublishOnly": "yarn run build",
|
||||
"postversion": "npm publish --access public"
|
||||
},
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/types",
|
||||
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||
"repository": {
|
||||
"directory": "@vates/types",
|
||||
"type": "git",
|
||||
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Vates SAS",
|
||||
"url": "https://vates.fr"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.18"
|
||||
}
|
||||
}
|
||||
@@ -1,771 +0,0 @@
|
||||
import type { XoHost, XoNetwork, XoVif } from './xo.mjs'
|
||||
|
||||
declare const __brand: unique symbol
|
||||
|
||||
export type Branded<TBrand extends string, TType = string> = TType & { [__brand]: TBrand }
|
||||
|
||||
export const HOST_POWER_STATE = {
|
||||
RUNNING: 'Running',
|
||||
HALTED: 'Halted',
|
||||
UNKNOWN: 'Unknown',
|
||||
} as const
|
||||
|
||||
export type HOST_POWER_STATE = (typeof HOST_POWER_STATE)[keyof typeof HOST_POWER_STATE]
|
||||
|
||||
export const TASK_ALLOWED_OPERATIONS = {
|
||||
CANCEL: 'cancel',
|
||||
DESTROY: 'destroy',
|
||||
} as const
|
||||
|
||||
export type TASK_ALLOWED_OPERATIONS = (typeof TASK_ALLOWED_OPERATIONS)[keyof typeof TASK_ALLOWED_OPERATIONS]
|
||||
|
||||
export const TASK_STATUS_TYPE = {
|
||||
CANCELLED: 'cancelled',
|
||||
CANCELLING: 'cancelling',
|
||||
FAILURE: 'failure',
|
||||
PENDING: 'pending',
|
||||
SUCCESS: 'success',
|
||||
} as const
|
||||
|
||||
export type TASK_STATUS_TYPE = (typeof TASK_STATUS_TYPE)[keyof typeof TASK_STATUS_TYPE]
|
||||
|
||||
export const EVENT_OPERATION = {
|
||||
ADD: 'add',
|
||||
DEL: 'del',
|
||||
MOD: 'mod',
|
||||
} as const
|
||||
|
||||
export type EVENT_OPERATION = (typeof EVENT_OPERATION)[keyof typeof EVENT_OPERATION]
|
||||
|
||||
export const POOL_ALLOWED_OPERATIONS = {
|
||||
APPLY_UPDATES: 'apply_updates',
|
||||
CERT_REFRESH: 'cert_refresh',
|
||||
CLUSTER_CREATE: 'cluster_create',
|
||||
CONFIGURE_REPOSITORIES: 'configure_repositories',
|
||||
COPY_PRIMARY_HOST_CERTS: 'copy_primary_host_certs',
|
||||
DESIGNATE_NEW_MASTER: 'designate_new_master',
|
||||
EJECT: 'eject',
|
||||
EXCHANGE_CA_CERTIFICATES_ON_JOIN: 'exchange_ca_certificates_on_join',
|
||||
EXCHANGE_CERTIFICATES_ON_JOIN: 'exchange_certificates_on_join',
|
||||
GET_UPDATES: 'get_updates',
|
||||
HA_DISABLE: 'ha_disable',
|
||||
HA_ENABLE: 'ha_enable',
|
||||
SYNC_UPDATES: 'sync_updates',
|
||||
TLS_VERIFICATION_ENABLE: 'tls_verification_enable',
|
||||
} as const
|
||||
|
||||
export type POOL_ALLOWED_OPERATIONS = (typeof POOL_ALLOWED_OPERATIONS)[keyof typeof POOL_ALLOWED_OPERATIONS]
|
||||
|
||||
export const TELEMETRY_FREQUENCY = {
|
||||
DAILY: 'daily',
|
||||
MONTHLY: 'monthly',
|
||||
WEEKLY: 'weekly',
|
||||
} as const
|
||||
|
||||
export type TELEMETRY_FREQUENCY = (typeof TELEMETRY_FREQUENCY)[keyof typeof TELEMETRY_FREQUENCY]
|
||||
|
||||
export const UPDATE_SYNC_FREQUENCY = {
|
||||
DAILY: 'daily',
|
||||
WEEKLY: 'weekly',
|
||||
} as const
|
||||
|
||||
export type UPDATE_SYNC_FREQUENCY = (typeof UPDATE_SYNC_FREQUENCY)[keyof typeof UPDATE_SYNC_FREQUENCY]
|
||||
|
||||
export const AFTER_APPLY_GUIDANCE = {
|
||||
RESTART_HOST: 'restartHost',
|
||||
RESTART_HVM: 'restartHVM',
|
||||
RESTART_PV: 'restartPV',
|
||||
RESTART_XAPI: 'restartXAPI',
|
||||
} as const
|
||||
|
||||
export type AFTER_APPLY_GUIDANCE = (typeof AFTER_APPLY_GUIDANCE)[keyof typeof AFTER_APPLY_GUIDANCE]
|
||||
|
||||
export const UPDATE_AFTER_APPLY_GUIDANCE = {
|
||||
RESTART_HOST: 'restartHost',
|
||||
RESTART_HVM: 'restartHVM',
|
||||
RESTART_PV: 'restartPV',
|
||||
RESTART_XAPI: 'restartXAPI',
|
||||
} as const
|
||||
|
||||
export type UPDATE_AFTER_APPLY_GUIDANCE = (typeof UPDATE_AFTER_APPLY_GUIDANCE)[keyof typeof UPDATE_AFTER_APPLY_GUIDANCE]
|
||||
|
||||
export const LIVEPATCH_STATUS = {
|
||||
OK: 'ok',
|
||||
OK_LIVEPATCH_COMPLETE: 'ok_livepatch_complete',
|
||||
OK_LIVEPATCH_INCOMPLETE: 'ok_livepatch_incomplete',
|
||||
} as const
|
||||
|
||||
export type LIVEPATCH_STATUS = (typeof LIVEPATCH_STATUS)[keyof typeof LIVEPATCH_STATUS]
|
||||
|
||||
export const VM_POWER_STATE = {
|
||||
HALTED: 'Halted',
|
||||
PAUSED: 'Paused',
|
||||
RUNNING: 'Running',
|
||||
SUSPENDED: 'Suspended',
|
||||
} as const
|
||||
|
||||
export type VM_POWER_STATE = (typeof VM_POWER_STATE)[keyof typeof VM_POWER_STATE]
|
||||
|
||||
export const UPDATE_GUIDANCES = {
|
||||
REBOOT_HOST: 'reboot_host',
|
||||
REBOOT_HOST_ON_KERNEL_LIVEPATCH_FAILURE: 'reboot_host_on_kernel_livepatch_failure',
|
||||
REBOOT_HOST_ON_LIVEPATCH_FAILURE: 'reboot_host_on_livepatch_failure',
|
||||
REBOOT_HOST_ON_XEN_LIVEPATCH_FAILURE: 'reboot_host_on_xen_livepatch_failure',
|
||||
RESTART_DEVICE_MODEL: 'restart_device_model',
|
||||
RESTART_TOOLSTACK: 'restart_toolstack',
|
||||
RESTART_VM: 'restart_vm',
|
||||
} as const
|
||||
|
||||
export type UPDATE_GUIDANCES = (typeof UPDATE_GUIDANCES)[keyof typeof UPDATE_GUIDANCES]
|
||||
|
||||
export const ON_SOFTREBOOT_BEHAVIOR = {
|
||||
DESTROY: 'destroy',
|
||||
PRESERVE: 'preserve',
|
||||
RESTART: 'restart',
|
||||
SOFT_REBOOT: 'soft_reboot',
|
||||
} as const
|
||||
|
||||
export type ON_SOFTREBOOT_BEHAVIOR = (typeof ON_SOFTREBOOT_BEHAVIOR)[keyof typeof ON_SOFTREBOOT_BEHAVIOR]
|
||||
|
||||
export const ON_NORMAL_EXIT = {
|
||||
DESTROY: 'destroy',
|
||||
RESTART: 'restart',
|
||||
} as const
|
||||
|
||||
export type ON_NORMAL_EXIT = (typeof ON_NORMAL_EXIT)[keyof typeof ON_NORMAL_EXIT]
|
||||
|
||||
export const VM_OPERATIONS = {
|
||||
ASSERT_OPERATION_VALID: 'assert_operation_valid',
|
||||
AWAITING_MEMORY_LIVE: 'awaiting_memory_live',
|
||||
CALL_PLUGIN: 'call_plugin',
|
||||
CHANGING_DYNAMIC_RANGE: 'changing_dynamic_range',
|
||||
CHANGING_MEMORY_LIMITS: 'changing_memory_limits',
|
||||
CHANGING_MEMORY_LIVE: 'changing_memory_live',
|
||||
CHANGING_NVRAM: 'changing_NVRAM',
|
||||
CHANGING_SHADOW_MEMORY: 'changing_shadow_memory',
|
||||
CHANGING_SHADOW_MEMORY_LIVE: 'changing_shadow_memory_live',
|
||||
CHANGING_STATIC_RANGE: 'changing_static_range',
|
||||
CHANGING_VCPUS: 'changing_VCPUs',
|
||||
CHANGING_VCPUS_LIVE: 'changing_VCPUs_live',
|
||||
CHECKPOINT: 'checkpoint',
|
||||
CLEAN_REBOOT: 'clean_reboot',
|
||||
CLEAN_SHUTDOWN: 'clean_shutdown',
|
||||
CLONE: 'clone',
|
||||
COPY: 'copy',
|
||||
CREATE_TEMPLATE: 'create_template',
|
||||
CREATE_VTPM: 'create_vtpm',
|
||||
CSVM: 'csvm',
|
||||
DATA_SOURCE_OP: 'data_source_op',
|
||||
DESTROY: 'destroy',
|
||||
EXPORT: 'export',
|
||||
GET_BOOT_RECORD: 'get_boot_record',
|
||||
HARD_REBOOT: 'hard_reboot',
|
||||
HARD_SHUTDOWN: 'hard_shutdown',
|
||||
IMPORT: 'import',
|
||||
MAKE_INTO_TEMPLATE: 'make_into_template',
|
||||
METADATA_EXPORT: 'metadata_export',
|
||||
MIGRATE_SEND: 'migrate_send',
|
||||
PAUSE: 'pause',
|
||||
POOL_MIGRATE: 'pool_migrate',
|
||||
POWER_STATE_RESET: 'power_state_reset',
|
||||
PROVISION: 'provision',
|
||||
QUERY_SERVICES: 'query_services',
|
||||
RESUME: 'resume',
|
||||
RESUME_ON: 'resume_on',
|
||||
REVERT: 'revert',
|
||||
REVERTING: 'reverting',
|
||||
SEND_SYSRQ: 'send_sysrq',
|
||||
SEND_TRIGGER: 'send_trigger',
|
||||
SHUTDOWN: 'shutdown',
|
||||
SNAPSHOT: 'snapshot',
|
||||
SNAPSHOT_WITH_QUIESCE: 'snapshot_with_quiesce',
|
||||
START: 'start',
|
||||
START_ON: 'start_on',
|
||||
SUSPEND: 'suspend',
|
||||
UNPAUSE: 'unpause',
|
||||
UPDATE_ALLOWED_OPERATIONS: 'update_allowed_operations',
|
||||
} as const
|
||||
|
||||
export type VM_OPERATIONS = (typeof VM_OPERATIONS)[keyof typeof VM_OPERATIONS]
|
||||
|
||||
export const ON_CRASH_BEHAVIOUR = {
|
||||
COREDUMP_AND_DESTROY: 'coredump_and_destroy',
|
||||
COREDUMP_AND_RESTART: 'coredump_and_restart',
|
||||
DESTROY: 'destroy',
|
||||
PRESERVE: 'preserve',
|
||||
RENAME_RESTART: 'rename_restart',
|
||||
RESTART: 'restart',
|
||||
} as const
|
||||
|
||||
export type ON_CRASH_BEHAVIOUR = (typeof ON_CRASH_BEHAVIOUR)[keyof typeof ON_CRASH_BEHAVIOUR]
|
||||
|
||||
export const DOMAIN_TYPE = {
|
||||
HVM: 'hvm',
|
||||
PV: 'pv',
|
||||
PVH: 'pvh',
|
||||
PV_IN_PVH: 'pv_in_pvh',
|
||||
UNSPECIFIED: 'unspecified',
|
||||
} as const
|
||||
|
||||
export type DOMAIN_TYPE = (typeof DOMAIN_TYPE)[keyof typeof DOMAIN_TYPE]
|
||||
|
||||
export const TRISTATE_TYPE = {
|
||||
NO: 'no',
|
||||
UNSPECIFIED: 'unspecified',
|
||||
YES: 'yes',
|
||||
} as const
|
||||
|
||||
export type TRISTATE_TYPE = (typeof TRISTATE_TYPE)[keyof typeof TRISTATE_TYPE]
|
||||
|
||||
export const VMPP_BACKUP_TYPE = {
|
||||
CHECKPOINT: 'checkpoint',
|
||||
SNAPSHOT: 'snapshot',
|
||||
} as const
|
||||
|
||||
export type VMPP_BACKUP_TYPE = (typeof VMPP_BACKUP_TYPE)[keyof typeof VMPP_BACKUP_TYPE]
|
||||
|
||||
export const VMPP_BACKUP_FREQUENCY = {
|
||||
DAILY: 'daily',
|
||||
HOURLY: 'hourly',
|
||||
WEEKLY: 'weekly',
|
||||
} as const
|
||||
|
||||
export type VMPP_BACKUP_FREQUENCY = (typeof VMPP_BACKUP_FREQUENCY)[keyof typeof VMPP_BACKUP_FREQUENCY]
|
||||
|
||||
export const VMPP_ARCHIVE_FREQUENCY = {
|
||||
ALWAYS_AFTER_BACKUP: 'always_after_backup',
|
||||
DAILY: 'daily',
|
||||
NEVER: 'never',
|
||||
WEEKLY: 'weekly',
|
||||
} as const
|
||||
|
||||
export type VMPP_ARCHIVE_FREQUENCY = (typeof VMPP_ARCHIVE_FREQUENCY)[keyof typeof VMPP_ARCHIVE_FREQUENCY]
|
||||
|
||||
export const VMPP_ARCHIVE_TARGET_TYPE = {
|
||||
CIFS: 'cifs',
|
||||
NFS: 'nfs',
|
||||
NONE: 'none',
|
||||
} as const
|
||||
|
||||
export type VMPP_ARCHIVE_TARGET_TYPE = (typeof VMPP_ARCHIVE_TARGET_TYPE)[keyof typeof VMPP_ARCHIVE_TARGET_TYPE]
|
||||
|
||||
export const VMSS_FREQUENCY = {
|
||||
DAILY: 'daily',
|
||||
HOURLY: 'hourly',
|
||||
WEEKLY: 'weekly',
|
||||
} as const
|
||||
|
||||
export type VMSS_FREQUENCY = (typeof VMSS_FREQUENCY)[keyof typeof VMSS_FREQUENCY]
|
||||
|
||||
export const VMSS_TYPE = {
|
||||
CHECKPOINT: 'checkpoint',
|
||||
SNAPSHOT: 'snapshot',
|
||||
SNAPSHOT_WITH_QUIESCE: 'snapshot_with_quiesce',
|
||||
} as const
|
||||
|
||||
export type VMSS_TYPE = (typeof VMSS_TYPE)[keyof typeof VMSS_TYPE]
|
||||
|
||||
export const VM_APPLIANCE_OPERATION = {
|
||||
CLEAN_SHUTDOWN: 'clean_shutdown',
|
||||
HARD_SHUTDOWN: 'hard_shutdown',
|
||||
SHUTDOWN: 'shutdown',
|
||||
START: 'start',
|
||||
} as const
|
||||
|
||||
export type VM_APPLIANCE_OPERATION = (typeof VM_APPLIANCE_OPERATION)[keyof typeof VM_APPLIANCE_OPERATION]
|
||||
|
||||
export const HOST_ALLOWED_OPERATIONS = {
|
||||
APPLY_UPDATES: 'apply_updates',
|
||||
ENABLE: 'enable',
|
||||
EVACUATE: 'evacuate',
|
||||
POWER_ON: 'power_on',
|
||||
PROVISION: 'provision',
|
||||
REBOOT: 'reboot',
|
||||
SHUTDOWN: 'shutdown',
|
||||
VM_MIGRATE: 'vm_migrate',
|
||||
VM_RESUME: 'vm_resume',
|
||||
VM_START: 'vm_start',
|
||||
} as const
|
||||
|
||||
export type HOST_ALLOWED_OPERATIONS = (typeof HOST_ALLOWED_OPERATIONS)[keyof typeof HOST_ALLOWED_OPERATIONS]
|
||||
|
||||
export const LATEST_SYNCED_UPDATES_APPLIED_STATE = {
|
||||
NO: 'no',
|
||||
UNKNOWN: 'unknown',
|
||||
YES: 'yes',
|
||||
} as const
|
||||
|
||||
export type LATEST_SYNCED_UPDATES_APPLIED_STATE =
|
||||
(typeof LATEST_SYNCED_UPDATES_APPLIED_STATE)[keyof typeof LATEST_SYNCED_UPDATES_APPLIED_STATE]
|
||||
|
||||
export const HOST_DISPLAY = {
|
||||
DISABLED: 'disabled',
|
||||
DISABLE_ON_REBOOT: 'disable_on_reboot',
|
||||
ENABLED: 'enabled',
|
||||
ENABLE_ON_REBOOT: 'enable_on_reboot',
|
||||
} as const
|
||||
|
||||
export type HOST_DISPLAY = (typeof HOST_DISPLAY)[keyof typeof HOST_DISPLAY]
|
||||
|
||||
export const HOST_SCHED_GRAN = {
|
||||
CORE: 'core',
|
||||
CPU: 'cpu',
|
||||
SOCKET: 'socket',
|
||||
} as const
|
||||
|
||||
export type HOST_SCHED_GRAN = (typeof HOST_SCHED_GRAN)[keyof typeof HOST_SCHED_GRAN]
|
||||
|
||||
export const HOST_NUMA_AFFINITY_POLICY = {
|
||||
ANY: 'any',
|
||||
BEST_EFFORT: 'best_effort',
|
||||
DEFAULT_POLICY: 'default_policy',
|
||||
} as const
|
||||
|
||||
export type HOST_NUMA_AFFINITY_POLICY = (typeof HOST_NUMA_AFFINITY_POLICY)[keyof typeof HOST_NUMA_AFFINITY_POLICY]
|
||||
|
||||
export const NETWORK_OPERATIONS = {
|
||||
ATTACHING: 'attaching',
|
||||
} as const
|
||||
|
||||
export type NETWORK_OPERATIONS = (typeof NETWORK_OPERATIONS)[keyof typeof NETWORK_OPERATIONS]
|
||||
|
||||
export const NETWORK_DEFAULT_LOCKING_MODE = {
|
||||
DISABLED: 'disabled',
|
||||
UNLOCKED: 'unlocked',
|
||||
} as const
|
||||
|
||||
export type NETWORK_DEFAULT_LOCKING_MODE =
|
||||
(typeof NETWORK_DEFAULT_LOCKING_MODE)[keyof typeof NETWORK_DEFAULT_LOCKING_MODE]
|
||||
|
||||
export const NETWORK_PURPOSE = {
|
||||
INSECURE_NBD: 'insecure_nbd',
|
||||
NBD: 'nbd',
|
||||
} as const
|
||||
|
||||
export type NETWORK_PURPOSE = (typeof NETWORK_PURPOSE)[keyof typeof NETWORK_PURPOSE]
|
||||
|
||||
export const VIF_OPERATIONS = {
|
||||
ATTACH: 'attach',
|
||||
PLUG: 'plug',
|
||||
UNPLUG: 'unplug',
|
||||
} as const
|
||||
|
||||
export type VIF_OPERATIONS = (typeof VIF_OPERATIONS)[keyof typeof VIF_OPERATIONS]
|
||||
|
||||
export const VIF_LOCKING_MODE = {
|
||||
DISABLED: 'disabled',
|
||||
LOCKED: 'locked',
|
||||
NETWORK_DEFAULT: 'network_default',
|
||||
UNLOCKED: 'unlocked',
|
||||
} as const
|
||||
|
||||
export type VIF_LOCKING_MODE = (typeof VIF_LOCKING_MODE)[keyof typeof VIF_LOCKING_MODE]
|
||||
|
||||
export const VIF_IPV4_CONFIGURATION_MODE = {
|
||||
NONE: 'None',
|
||||
STATIC: 'Static',
|
||||
} as const
|
||||
|
||||
export type VIF_IPV4_CONFIGURATION_MODE = (typeof VIF_IPV4_CONFIGURATION_MODE)[keyof typeof VIF_IPV4_CONFIGURATION_MODE]
|
||||
|
||||
export const VIF_IPV6_CONFIGURATION_MODE = {
|
||||
NONE: 'None',
|
||||
STATIC: 'Static',
|
||||
} as const
|
||||
|
||||
export type VIF_IPV6_CONFIGURATION_MODE = (typeof VIF_IPV6_CONFIGURATION_MODE)[keyof typeof VIF_IPV6_CONFIGURATION_MODE]
|
||||
|
||||
export const PIF_IGMP_STATUS = {
|
||||
DISABLED: 'disabled',
|
||||
ENABLED: 'enabled',
|
||||
UNKNOWN: 'unknown',
|
||||
} as const
|
||||
|
||||
export type PIF_IGMP_STATUS = (typeof PIF_IGMP_STATUS)[keyof typeof PIF_IGMP_STATUS]
|
||||
|
||||
export const IP_CONFIGURATION_MODE = {
|
||||
DHCP: 'DHCP',
|
||||
NONE: 'None',
|
||||
STATIC: 'Static',
|
||||
} as const
|
||||
|
||||
export type IP_CONFIGURATION_MODE = (typeof IP_CONFIGURATION_MODE)[keyof typeof IP_CONFIGURATION_MODE]
|
||||
|
||||
export const IPV6_CONFIGURATION_MODE = {
|
||||
AUTOCONF: 'Autoconf',
|
||||
DHCP: 'DHCP',
|
||||
NONE: 'None',
|
||||
STATIC: 'Static',
|
||||
} as const
|
||||
|
||||
export type IPV6_CONFIGURATION_MODE = (typeof IPV6_CONFIGURATION_MODE)[keyof typeof IPV6_CONFIGURATION_MODE]
|
||||
|
||||
export const PRIMARY_ADDRESS_TYPE = {
|
||||
IPV4: 'IPv4',
|
||||
IPV6: 'IPv6',
|
||||
} as const
|
||||
|
||||
export type PRIMARY_ADDRESS_TYPE = (typeof PRIMARY_ADDRESS_TYPE)[keyof typeof PRIMARY_ADDRESS_TYPE]
|
||||
|
||||
export const BOND_MODE = {
|
||||
ACTIVE_BACKUP: 'active-backup',
|
||||
BALANCE_SLB: 'balance-slb',
|
||||
LACP: 'lacp',
|
||||
} as const
|
||||
|
||||
export type BOND_MODE = (typeof BOND_MODE)[keyof typeof BOND_MODE]
|
||||
|
||||
export const STORAGE_OPERATIONS = {
|
||||
DESTROY: 'destroy',
|
||||
FORGET: 'forget',
|
||||
PBD_CREATE: 'pbd_create',
|
||||
PBD_DESTROY: 'pbd_destroy',
|
||||
PLUG: 'plug',
|
||||
SCAN: 'scan',
|
||||
UNPLUG: 'unplug',
|
||||
UPDATE: 'update',
|
||||
VDI_CLONE: 'vdi_clone',
|
||||
VDI_CREATE: 'vdi_create',
|
||||
VDI_DATA_DESTROY: 'vdi_data_destroy',
|
||||
VDI_DESTROY: 'vdi_destroy',
|
||||
VDI_DISABLE_CBT: 'vdi_disable_cbt',
|
||||
VDI_ENABLE_CBT: 'vdi_enable_cbt',
|
||||
VDI_INTRODUCE: 'vdi_introduce',
|
||||
VDI_LIST_CHANGED_BLOCKS: 'vdi_list_changed_blocks',
|
||||
VDI_MIRROR: 'vdi_mirror',
|
||||
VDI_RESIZE: 'vdi_resize',
|
||||
VDI_SET_ON_BOOT: 'vdi_set_on_boot',
|
||||
VDI_SNAPSHOT: 'vdi_snapshot',
|
||||
} as const
|
||||
|
||||
export type STORAGE_OPERATIONS = (typeof STORAGE_OPERATIONS)[keyof typeof STORAGE_OPERATIONS]
|
||||
|
||||
export const SR_HEALTH = {
|
||||
HEALTHY: 'healthy',
|
||||
RECOVERING: 'recovering',
|
||||
} as const
|
||||
|
||||
export type SR_HEALTH = (typeof SR_HEALTH)[keyof typeof SR_HEALTH]
|
||||
|
||||
export const VDI_OPERATIONS = {
|
||||
BLOCKED: 'blocked',
|
||||
CLONE: 'clone',
|
||||
COPY: 'copy',
|
||||
DATA_DESTROY: 'data_destroy',
|
||||
DESTROY: 'destroy',
|
||||
DISABLE_CBT: 'disable_cbt',
|
||||
ENABLE_CBT: 'enable_cbt',
|
||||
FORCE_UNLOCK: 'force_unlock',
|
||||
FORGET: 'forget',
|
||||
GENERATE_CONFIG: 'generate_config',
|
||||
LIST_CHANGED_BLOCKS: 'list_changed_blocks',
|
||||
MIRROR: 'mirror',
|
||||
RESIZE: 'resize',
|
||||
RESIZE_ONLINE: 'resize_online',
|
||||
SET_ON_BOOT: 'set_on_boot',
|
||||
SNAPSHOT: 'snapshot',
|
||||
UPDATE: 'update',
|
||||
} as const
|
||||
|
||||
export type VDI_OPERATIONS = (typeof VDI_OPERATIONS)[keyof typeof VDI_OPERATIONS]
|
||||
|
||||
export const VDI_TYPE = {
|
||||
CBT_METADATA: 'cbt_metadata',
|
||||
CRASHDUMP: 'crashdump',
|
||||
EPHEMERAL: 'ephemeral',
|
||||
HA_STATEFILE: 'ha_statefile',
|
||||
METADATA: 'metadata',
|
||||
PVS_CACHE: 'pvs_cache',
|
||||
REDO_LOG: 'redo_log',
|
||||
RRD: 'rrd',
|
||||
SUSPEND: 'suspend',
|
||||
SYSTEM: 'system',
|
||||
USER: 'user',
|
||||
} as const
|
||||
|
||||
export type VDI_TYPE = (typeof VDI_TYPE)[keyof typeof VDI_TYPE]
|
||||
|
||||
export const ON_BOOT = {
|
||||
PERSIST: 'persist',
|
||||
RESET: 'reset',
|
||||
} as const
|
||||
|
||||
export type ON_BOOT = (typeof ON_BOOT)[keyof typeof ON_BOOT]
|
||||
|
||||
export const VBD_OPERATIONS = {
|
||||
ATTACH: 'attach',
|
||||
EJECT: 'eject',
|
||||
INSERT: 'insert',
|
||||
PAUSE: 'pause',
|
||||
PLUG: 'plug',
|
||||
UNPAUSE: 'unpause',
|
||||
UNPLUG: 'unplug',
|
||||
UNPLUG_FORCE: 'unplug_force',
|
||||
} as const
|
||||
|
||||
export type VBD_OPERATIONS = (typeof VBD_OPERATIONS)[keyof typeof VBD_OPERATIONS]
|
||||
|
||||
export const VBD_TYPE = {
|
||||
CD: 'CD',
|
||||
DISK: 'Disk',
|
||||
FLOPPY: 'Floppy',
|
||||
} as const
|
||||
|
||||
export type VBD_TYPE = (typeof VBD_TYPE)[keyof typeof VBD_TYPE]
|
||||
|
||||
export const VBD_MODE = {
|
||||
RO: 'RO',
|
||||
RW: 'RW',
|
||||
} as const
|
||||
|
||||
export type VBD_MODE = (typeof VBD_MODE)[keyof typeof VBD_MODE]
|
||||
|
||||
export const VTPM_OPERATIONS = {
|
||||
DESTROY: 'destroy',
|
||||
} as const
|
||||
|
||||
export type VTPM_OPERATIONS = (typeof VTPM_OPERATIONS)[keyof typeof VTPM_OPERATIONS]
|
||||
|
||||
export const PERSISTENCE_BACKEND = {
|
||||
XAPI: 'xapi',
|
||||
} as const
|
||||
|
||||
export type PERSISTENCE_BACKEND = (typeof PERSISTENCE_BACKEND)[keyof typeof PERSISTENCE_BACKEND]
|
||||
|
||||
export const CONSOLE_PROTOCOL = {
|
||||
RDP: 'rdp',
|
||||
RFB: 'rfb',
|
||||
VT100: 'vt100',
|
||||
} as const
|
||||
|
||||
export type CONSOLE_PROTOCOL = (typeof CONSOLE_PROTOCOL)[keyof typeof CONSOLE_PROTOCOL]
|
||||
|
||||
export const CLS = {
|
||||
CERTIFICATE: 'Certificate',
|
||||
HOST: 'Host',
|
||||
POOL: 'Pool',
|
||||
PVS_PROXY: 'PVS_proxy',
|
||||
SR: 'SR',
|
||||
VDI: 'VDI',
|
||||
VM: 'VM',
|
||||
VMPP: 'VMPP',
|
||||
VMSS: 'VMSS',
|
||||
} as const
|
||||
|
||||
export type CLS = (typeof CLS)[keyof typeof CLS]
|
||||
|
||||
export const TUNNEL_PROTOCOL = {
|
||||
GRE: 'gre',
|
||||
VXLAN: 'vxlan',
|
||||
} as const
|
||||
|
||||
export type TUNNEL_PROTOCOL = (typeof TUNNEL_PROTOCOL)[keyof typeof TUNNEL_PROTOCOL]
|
||||
|
||||
export const SRIOV_CONFIGURATION_MODE = {
|
||||
MANUAL: 'manual',
|
||||
MODPROBE: 'modprobe',
|
||||
SYSFS: 'sysfs',
|
||||
UNKNOWN: 'unknown',
|
||||
} as const
|
||||
|
||||
export type SRIOV_CONFIGURATION_MODE = (typeof SRIOV_CONFIGURATION_MODE)[keyof typeof SRIOV_CONFIGURATION_MODE]
|
||||
|
||||
export const PGPU_DOM0_ACCESS = {
|
||||
DISABLED: 'disabled',
|
||||
DISABLE_ON_REBOOT: 'disable_on_reboot',
|
||||
ENABLED: 'enabled',
|
||||
ENABLE_ON_REBOOT: 'enable_on_reboot',
|
||||
} as const
|
||||
|
||||
export type PGPU_DOM0_ACCESS = (typeof PGPU_DOM0_ACCESS)[keyof typeof PGPU_DOM0_ACCESS]
|
||||
|
||||
export const ALLOCATION_ALGORITHM = {
|
||||
BREADTH_FIRST: 'breadth_first',
|
||||
DEPTH_FIRST: 'depth_first',
|
||||
} as const
|
||||
|
||||
export type ALLOCATION_ALGORITHM = (typeof ALLOCATION_ALGORITHM)[keyof typeof ALLOCATION_ALGORITHM]
|
||||
|
||||
export const VGPU_TYPE_IMPLEMENTATION = {
|
||||
GVT_G: 'gvt_g',
|
||||
MXGPU: 'mxgpu',
|
||||
NVIDIA: 'nvidia',
|
||||
NVIDIA_SRIOV: 'nvidia_sriov',
|
||||
PASSTHROUGH: 'passthrough',
|
||||
} as const
|
||||
|
||||
export type VGPU_TYPE_IMPLEMENTATION = (typeof VGPU_TYPE_IMPLEMENTATION)[keyof typeof VGPU_TYPE_IMPLEMENTATION]
|
||||
|
||||
export const PVS_PROXY_STATUS = {
|
||||
CACHING: 'caching',
|
||||
INCOMPATIBLE_PROTOCOL_VERSION: 'incompatible_protocol_version',
|
||||
INCOMPATIBLE_WRITE_CACHE_MODE: 'incompatible_write_cache_mode',
|
||||
INITIALISED: 'initialised',
|
||||
STOPPED: 'stopped',
|
||||
} as const
|
||||
|
||||
export type PVS_PROXY_STATUS = (typeof PVS_PROXY_STATUS)[keyof typeof PVS_PROXY_STATUS]
|
||||
|
||||
export const SDN_CONTROLLER_PROTOCOL = {
|
||||
PSSL: 'pssl',
|
||||
SSL: 'ssl',
|
||||
} as const
|
||||
|
||||
export type SDN_CONTROLLER_PROTOCOL = (typeof SDN_CONTROLLER_PROTOCOL)[keyof typeof SDN_CONTROLLER_PROTOCOL]
|
||||
|
||||
export const VUSB_OPERATIONS = {
|
||||
ATTACH: 'attach',
|
||||
PLUG: 'plug',
|
||||
UNPLUG: 'unplug',
|
||||
} as const
|
||||
|
||||
export type VUSB_OPERATIONS = (typeof VUSB_OPERATIONS)[keyof typeof VUSB_OPERATIONS]
|
||||
|
||||
export const CLUSTER_OPERATION = {
|
||||
ADD: 'add',
|
||||
DESTROY: 'destroy',
|
||||
DISABLE: 'disable',
|
||||
ENABLE: 'enable',
|
||||
REMOVE: 'remove',
|
||||
} as const
|
||||
|
||||
export type CLUSTER_OPERATION = (typeof CLUSTER_OPERATION)[keyof typeof CLUSTER_OPERATION]
|
||||
|
||||
export const CLUSTER_HOST_OPERATION = {
|
||||
DESTROY: 'destroy',
|
||||
DISABLE: 'disable',
|
||||
ENABLE: 'enable',
|
||||
} as const
|
||||
|
||||
export type CLUSTER_HOST_OPERATION = (typeof CLUSTER_HOST_OPERATION)[keyof typeof CLUSTER_HOST_OPERATION]
|
||||
|
||||
export const CERTIFICATE_TYPE = {
|
||||
CA: 'ca',
|
||||
HOST: 'host',
|
||||
HOST_INTERNAL: 'host_internal',
|
||||
} as const
|
||||
|
||||
export type CERTIFICATE_TYPE = (typeof CERTIFICATE_TYPE)[keyof typeof CERTIFICATE_TYPE]
|
||||
|
||||
export const OPAQUE_REF = { EMPTY: 'OpaqueRef:NULL' } as const
|
||||
|
||||
export type OPAQUE_REF_NULL = (typeof OPAQUE_REF)['EMPTY']
|
||||
|
||||
export const BACKUP_TYPE = { backup: 'backup', metadata: 'metadataBackup', mirror: 'mirrorBackup' } as const
|
||||
|
||||
export type BACKUP_TYPE = (typeof BACKUP_TYPE)[keyof typeof BACKUP_TYPE]
|
||||
|
||||
export const SUPPORTED_VDI_FORMAT = {
|
||||
raw: 'raw',
|
||||
vhd: 'vhd',
|
||||
qcow2: 'qcow2',
|
||||
} as const
|
||||
|
||||
export type SUPPORTED_VDI_FORMAT = (typeof SUPPORTED_VDI_FORMAT)[keyof typeof SUPPORTED_VDI_FORMAT]
|
||||
|
||||
// ----- XAPI Stats
|
||||
|
||||
type XapiStatsResponse<T> = {
|
||||
endTimestamp: number
|
||||
interval: number
|
||||
stats: T
|
||||
}
|
||||
type StatValues = (number | null)[]
|
||||
type RecordStatValues = Record<string, StatValues>
|
||||
|
||||
export type XapiStatsGranularity = 'seconds' | 'minutes' | 'hours' | 'days'
|
||||
|
||||
export type XapiHostStatsRaw = {
|
||||
cpus?: RecordStatValues
|
||||
ioThroughput?: {
|
||||
r: RecordStatValues
|
||||
w: RecordStatValues
|
||||
}
|
||||
iops?: {
|
||||
r: RecordStatValues
|
||||
w: RecordStatValues
|
||||
}
|
||||
iowait?: RecordStatValues
|
||||
latency?: {
|
||||
r: RecordStatValues
|
||||
w: RecordStatValues
|
||||
}
|
||||
load?: StatValues
|
||||
memory?: StatValues
|
||||
memoryFree?: StatValues
|
||||
pifs?: {
|
||||
rx: RecordStatValues
|
||||
tx: RecordStatValues
|
||||
}
|
||||
}
|
||||
export type XapiHostStats = XapiStatsResponse<XapiHostStatsRaw>
|
||||
|
||||
export type XapiVmStatsRaw = {
|
||||
cpus?: RecordStatValues
|
||||
cpuUsage?: StatValues
|
||||
runstateFullrun?: StatValues
|
||||
runstateFullContention?: StatValues
|
||||
runstatePartialRun?: StatValues
|
||||
runstatePartialContention?: StatValues
|
||||
runstateConcurrencyHazard?: StatValues
|
||||
runstateBlocked?: StatValues
|
||||
iops?: {
|
||||
r: RecordStatValues
|
||||
w: RecordStatValues
|
||||
}
|
||||
memory?: StatValues
|
||||
memoryFree?: StatValues
|
||||
memoryTarget?: StatValues
|
||||
vifs?: {
|
||||
rx: RecordStatValues
|
||||
tx: RecordStatValues
|
||||
}
|
||||
vifErrors?: {
|
||||
rx: RecordStatValues
|
||||
tx: RecordStatValues
|
||||
}
|
||||
xvds?: {
|
||||
w?: RecordStatValues
|
||||
r?: RecordStatValues
|
||||
total?: RecordStatValues
|
||||
}
|
||||
vbdLatency?: {
|
||||
w: RecordStatValues
|
||||
r: RecordStatValues
|
||||
}
|
||||
vbdIowait?: RecordStatValues
|
||||
vbdInflight?: RecordStatValues
|
||||
vbdAvgquSz?: RecordStatValues
|
||||
}
|
||||
export type XapiVmStats = XapiStatsResponse<XapiVmStatsRaw>
|
||||
|
||||
export type XapiPoolStats = Record<XoHost['id'], XapiHostStats | { error: Record<string, unknown> }>
|
||||
|
||||
export type TrafficRuleProtocol = (typeof TRAFFIC_RULE_PROTOCOLS)[number]
|
||||
|
||||
export const SDN_CONTROLLER_OF_RULES_KEY = 'xo:sdn-controller:of-rules'
|
||||
export const SDN_CONTROLLER_OF_METHOD_KEY = 'xo:sdn-controller:of-method'
|
||||
export const SDN_CONTROLLER_OF_FORMAT_KEY = 'xo:sdn-controller:of-format'
|
||||
export const TRAFFIC_RULE_PROTOCOLS = ['ARP', 'ICMP', 'IP', 'TCP', 'UDP'] as const
|
||||
export const TRAFFIC_RULE_PROTOCOLS_WITH_PORT: readonly TrafficRuleProtocol[] = ['TCP', 'UDP']
|
||||
|
||||
export type TrafficRuleTargetType = XoNetwork['type'] | XoVif['type']
|
||||
|
||||
export type TrafficRuleDirection = 'from' | 'to' | 'from/to'
|
||||
|
||||
export type RawTrafficRule = {
|
||||
allow: boolean
|
||||
protocol: string
|
||||
ipRange: string
|
||||
direction: string
|
||||
port?: string
|
||||
}
|
||||
|
||||
type BaseTrafficRule = RawTrafficRule & {
|
||||
id: string
|
||||
networkId: XoNetwork['id']
|
||||
}
|
||||
|
||||
export type TrafficRule =
|
||||
| (BaseTrafficRule & { type: XoVif['type']; sourceId: XoVif['id'] })
|
||||
| (BaseTrafficRule & { type: XoNetwork['type']; sourceId: XoNetwork['id'] })
|
||||
@@ -1,8 +0,0 @@
|
||||
export * from './common.mjs'
|
||||
export * from './xen-api.mjs'
|
||||
export * from './xo.mjs'
|
||||
export * from './xo-app.mjs'
|
||||
export * from './lib/xen-orchestra-xapi.mjs'
|
||||
export * from './lib/xen-orchestra-acl.mjs'
|
||||
export * from './lib/complex-matcher.mjs'
|
||||
export * from './lib/rest-api.mjs'
|
||||
@@ -1,5 +0,0 @@
|
||||
export type Or<T> = Id<{ __or: T[] }>
|
||||
|
||||
export type Id<T> = { id: T }
|
||||
|
||||
export type IdOr<T> = Id<T> | Or<T>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user