diff options
-rw-r--r-- | node_modules/nyc/node_modules/yargs/package.json | 138 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/memidb-test.ts | 114 | ||||
-rw-r--r-- | src/memidb.ts | 352 | ||||
-rw-r--r-- | tsconfig.json | 1 | ||||
-rw-r--r-- | yarn.lock | 4 |
6 files changed, 545 insertions, 65 deletions
diff --git a/node_modules/nyc/node_modules/yargs/package.json b/node_modules/nyc/node_modules/yargs/package.json index a396ea7b4..42fa3bd3a 100644 --- a/node_modules/nyc/node_modules/yargs/package.json +++ b/node_modules/nyc/node_modules/yargs/package.json @@ -1,16 +1,57 @@ { - "name": "yargs", - "version": "7.1.0", - "description": "yargs the modern, pirate-themed, successor to optimist.", - "main": "./index.js", - "files": [ - "index.js", - "yargs.js", - "lib", - "locales", - "completion.sh.hbs", - "LICENSE" + "_args": [ + [ + { + "raw": "yargs@^7.1.0", + "scope": null, + "escapedName": "yargs", + "name": "yargs", + "rawSpec": "^7.1.0", + "spec": ">=7.1.0 <8.0.0", + "type": "range" + }, + "/Users/benjamincoe/oss/nyc" + ] + ], + "_from": "yargs@>=7.1.0 <8.0.0", + "_id": "yargs@7.1.0", + "_inCache": true, + "_location": "/yargs", + "_nodeVersion": "6.9.5", + "_npmOperationalInternal": { + "host": "packages-12-west.internal.npmjs.com", + "tmp": "tmp/yargs-7.1.0.tgz_1492119927787_0.18849953636527061" + }, + "_npmUser": { + "name": "bcoe", + "email": "ben@npmjs.com" + }, + "_npmVersion": "4.5.0", + "_phantomChildren": { + "string-width": "1.0.2", + "strip-ansi": "3.0.1", + "wrap-ansi": "2.1.0" + }, + "_requested": { + "raw": "yargs@^7.1.0", + "scope": null, + "escapedName": "yargs", + "name": "yargs", + "rawSpec": "^7.1.0", + "spec": ">=7.1.0 <8.0.0", + "type": "range" + }, + "_requiredBy": [ + "/" ], + "_resolved": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz", + "_shasum": "6ba318eb16961727f5d284f8ea003e8d6154d0c8", + "_shrinkwrap": null, + "_spec": "yargs@^7.1.0", + "_where": "/Users/benjamincoe/oss/nyc", + "bugs": { + "url": "https://github.com/yargs/yargs/issues" + }, "dependencies": { "camelcase": "^3.0.0", "cliui": "^3.2.0", @@ -26,6 +67,7 @@ "y18n": "^3.2.1", "yargs-parser": "^5.0.0" }, + "description": "yargs the modern, pirate-themed, successor to optimist.", "devDependencies": { "chai": "^3.4.1", "chalk": "^1.1.3", @@ -41,22 +83,31 @@ "standard-version": "^3.0.0", "which": "^1.2.9" }, - "scripts": { - "pretest": "standard", - "test": "nyc --cache mocha --require ./test/before.js --timeout=8000 --check-leaks", - "coverage": "nyc report --reporter=text-lcov | coveralls", - "release": "standard-version" + "directories": {}, + "dist": { + "shasum": "6ba318eb16961727f5d284f8ea003e8d6154d0c8", + "tarball": "https://registry.npmjs.org/yargs/-/yargs-7.1.0.tgz" }, - "repository": { - "type": "git", - "url": "http://github.com/yargs/yargs.git" + "engine": { + "node": ">=0.10" }, - "homepage": "http://yargs.js.org/", - "standard": { + "files": [ + "index.js", + "yargs.js", + "lib", + "locales", + "completion.sh.hbs", + "LICENSE" + ], + "gitHead": "e7359d632595c3a5fcfd691994859b66e8943c85", + "greenkeeper": { "ignore": [ - "**/example/**" + "string-width", + "read-pkg-up", + "camelcase" ] }, + "homepage": "http://yargs.js.org/", "keywords": [ "argument", "args", @@ -67,14 +118,43 @@ "command" ], "license": "MIT", - "engine": { - "node": ">=0.10" + "main": "./index.js", + "maintainers": [ + { + "name": "bcoe", + "email": "ben@npmjs.com" + }, + { + "name": "chevex", + "email": "alex.ford@codetunnel.com" + }, + { + "name": "nexdrew", + "email": "andrew@npmjs.com" + }, + { + "name": "nylen", + "email": "jnylen@gmail.com" + } + ], + "name": "yargs", + "optionalDependencies": {}, + "readme": " yargs\n========\n\nYargs be a node.js library fer hearties tryin' ter parse optstrings.\n\nWith yargs, ye be havin' a map that leads straight to yer treasure! Treasure of course, being a simple option hash.\n\n[![Build Status][travis-image]][travis-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![NPM version][npm-image]][npm-url]\n[![Windows Tests][windows-image]][windows-url]\n[![js-standard-style][standard-image]][standard-url]\n[![Conventional Commits][conventional-commits-image]][conventional-commits-url]\n[![Gitter][gitter-image]][gitter-url]\n\n> Yargs is the official successor to optimist. Please feel free to submit issues and pull requests. If you'd like to contribute and don't know where to start, have a look at [the issue list](https://github.com/yargs/yargs/issues) :)\n\nexamples\n========\n\nWith yargs, the options be just a hash!\n-------------------------------------------------------------------\n\nplunder.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs').argv;\n\nif (argv.ships > 3 && argv.distance < 53.5) {\n console.log('Plunder more riffiwobbles!');\n} else {\n console.log('Retreat from the xupptumblers!');\n}\n````\n\n***\n\n $ ./plunder.js --ships=4 --distance=22\n Plunder more riffiwobbles!\n\n $ ./plunder.js --ships 12 --distance 98.7\n Retreat from the xupptumblers!\n\n![Joe was one optimistic pirate.](https://i.imgur.com/4WFGVJ9.png)\n\nBut don't walk the plank just yet! There be more! You can do short options:\n-------------------------------------------------\n\nshort.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs').argv;\nconsole.log('(%d,%d)', argv.x, argv.y);\n````\n\n***\n\n $ ./short.js -x 10 -y 21\n (10,21)\n\nAnd booleans, both long, short, and even grouped:\n----------------------------------\n\nbool.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs').argv;\n\nif (argv.s) {\n process.stdout.write(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: ');\n}\nconsole.log(\n (argv.fr ? 'couac' : 'squawk') + (argv.p ? '!' : '')\n);\n````\n\n***\n\n $ ./bool.js -s\n The parrot says: squawk\n\n $ ./bool.js -sp\n The parrot says: squawk!\n\n $ ./bool.js -sp --fr\n Le perroquet dit: couac!\n\nAnd non-hyphenated options too! Just use `argv._`!\n-------------------------------------------------\n\nnonopt.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs').argv;\nconsole.log('(%d,%d)', argv.x, argv.y);\nconsole.log(argv._);\n````\n\n***\n\n $ ./nonopt.js -x 6.82 -y 3.35 rum\n (6.82,3.35)\n [ 'rum' ]\n\n $ ./nonopt.js \"me hearties\" -x 0.54 yo -y 1.12 ho\n (0.54,1.12)\n [ 'me hearties', 'yo', 'ho' ]\n\nYargs even counts your booleans!\n----------------------------------------------------------------------\n\ncount.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .count('verbose')\n .alias('v', 'verbose')\n .argv;\n\nVERBOSE_LEVEL = argv.verbose;\n\nfunction WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }\nfunction INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }\nfunction DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }\n\nWARN(\"Showing only important stuff\");\nINFO(\"Showing semi-important stuff too\");\nDEBUG(\"Extra chatty mode\");\n````\n\n***\n $ node count.js\n Showing only important stuff\n\n $ node count.js -v\n Showing only important stuff\n Showing semi-important stuff too\n\n $ node count.js -vv\n Showing only important stuff\n Showing semi-important stuff too\n Extra chatty mode\n\n $ node count.js -v --verbose\n Showing only important stuff\n Showing semi-important stuff too\n Extra chatty mode\n\nTell users how to use yer options and make demands.\n-------------------------------------------------\n\narea.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .usage('Usage: $0 -w [num] -h [num]')\n .demandOption(['w','h'])\n .argv;\n\nconsole.log(\"The area is:\", argv.w * argv.h);\n````\n\n***\n\n $ ./area.js -w 55 -h 11\n The area is: 605\n\n $ node ./area.js -w 4.91 -w 2.51\n Usage: area.js -w [num] -h [num]\n\n Options:\n -w [required]\n -h [required]\n\n Missing required arguments: h\n\nAfter yer demands have been met, demand more! Ask for non-hyphenated arguments!\n-----------------------------------------\n\ndemand_count.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .demandCommand(2)\n .argv;\nconsole.dir(argv);\n````\n\n***\n\n\t$ ./demand_count.js a\n\n\tNot enough non-option arguments: got 1, need at least 2\n\n\t$ ./demand_count.js a b\n\t{ _: [ 'a', 'b' ], '$0': 'demand_count.js' }\n\n\t$ ./demand_count.js a b c\n\t{ _: [ 'a', 'b', 'c' ], '$0': 'demand_count.js' }\n\nEVEN MORE SHIVER ME TIMBERS!\n------------------\n\ndefault_singles.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .default('x', 10)\n .default('y', 10)\n .argv\n;\nconsole.log(argv.x + argv.y);\n````\n\n***\n\n $ ./default_singles.js -x 5\n 15\n\ndefault_hash.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .default({ x : 10, y : 10 })\n .argv\n;\nconsole.log(argv.x + argv.y);\n````\n\n***\n\n $ ./default_hash.js -y 7\n 17\n\nAnd if you really want to get all descriptive about it...\n---------------------------------------------------------\n\nboolean_single.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .boolean('v')\n .argv\n;\nconsole.dir(argv.v);\nconsole.dir(argv._);\n````\n\n***\n\n $ ./boolean_single.js -v \"me hearties\" yo ho\n true\n [ 'me hearties', 'yo', 'ho' ]\n\n\nboolean_double.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .boolean(['x','y','z'])\n .argv\n;\nconsole.dir([ argv.x, argv.y, argv.z ]);\nconsole.dir(argv._);\n````\n\n***\n\n $ ./boolean_double.js -x -z one two three\n [ true, false, true ]\n [ 'one', 'two', 'three' ]\n\nYargs is here to help you...\n---------------------------\n\nYe can describe parameters fer help messages and set aliases. Yargs figures\nout how ter format a handy help string automatically.\n\nline_count.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .usage('Usage: $0 <command> [options]')\n .command('count', 'Count the lines in a file')\n .example('$0 count -f foo.js', 'count the lines in the given file')\n .alias('f', 'file')\n .nargs('f', 1)\n .describe('f', 'Load a file')\n .demandOption(['f'])\n .help('h')\n .alias('h', 'help')\n .epilog('copyright 2015')\n .argv;\n\nvar fs = require('fs');\nvar s = fs.createReadStream(argv.file);\n\nvar lines = 0;\ns.on('data', function (buf) {\n lines += buf.toString().match(/\\n/g).length;\n});\n\ns.on('end', function () {\n console.log(lines);\n});\n````\n\n***\n $ node line_count.js count\n Usage: line_count.js <command> [options]\n\n Commands:\n count Count the lines in a file\n\n Options:\n -f, --file Load a file [required]\n -h, --help Show help [boolean]\n\n Examples:\n line_count.js count -f foo.js count the lines in the given file\n\n copyright 2015\n\n Missing required arguments: f\n\n $ node line_count.js count --file line_count.js\n 26\n\n $ node line_count.js count -f line_count.js\n 26\n\nmethods\n=======\n\nBy itself,\n\n````javascript\nrequire('yargs').argv\n````\n\nwill use the `process.argv` array to construct the `argv` object.\n\nYou can pass in the `process.argv` yourself:\n\n````javascript\nrequire('yargs')([ '-x', '1', '-y', '2' ]).argv\n````\n\nor use `.parse()` to do the same thing:\n\n````javascript\nrequire('yargs').parse([ '-x', '1', '-y', '2' ])\n````\n\nThe rest of these methods below come in just before the terminating `.argv`.\n\n<a name=\"alias\"></a>.alias(key, alias)\n------------------\n\nSet key names as equivalent such that updates to a key will propagate to aliases\nand vice-versa.\n\nOptionally `.alias()` can take an object that maps keys to aliases.\nEach key of this object should be the canonical version of the option, and each\nvalue should be a string or an array of strings.\n\n.argv\n-----\n\nGet the arguments as a plain old object.\n\nArguments without a corresponding flag show up in the `argv._` array.\n\nThe script name or node command is available at `argv.$0` similarly to how `$0`\nworks in bash or perl.\n\nIf `yargs` is executed in an environment that embeds node and there's no script name (e.g.\n[Electron](http://electron.atom.io/) or [nw.js](http://nwjs.io/)), it will ignore the first parameter since it\nexpects it to be the script name. In order to override this behavior, use `.parse(process.argv.slice(1))`\ninstead of `.argv` and the first parameter won't be ignored.\n\n<a name=\"array\"></a>.array(key)\n----------\n\nTell the parser to interpret `key` as an array. If `.array('foo')` is set,\n`--foo foo bar` will be parsed as `['foo', 'bar']` rather than as `'foo'`.\n\n<a name=\"boolean\"></a>.boolean(key)\n-------------\n\nInterpret `key` as a boolean. If a non-flag option follows `key` in\n`process.argv`, that string won't get set as the value of `key`.\n\n`key` will default to `false`, unless a `default(key, undefined)` is\nexplicitly set.\n\nIf `key` is an array, interpret all the elements as booleans.\n\n.check(fn, [global=true])\n----------\n\nCheck that certain conditions are met in the provided arguments.\n\n`fn` is called with two arguments, the parsed `argv` hash and an array of options and their aliases.\n\nIf `fn` throws or returns a non-truthy value, show the thrown error, usage information, and\nexit.\n\n`global` indicates whether `check()` should be enabled both\nat the top-level and for each sub-command.\n\n<a name=\"choices\"></a>.choices(key, choices)\n----------------------\n\nLimit valid values for `key` to a predefined set of `choices`, given as an array\nor as an individual value.\n\n```js\nvar argv = require('yargs')\n .alias('i', 'ingredient')\n .describe('i', 'choose your sandwich ingredients')\n .choices('i', ['peanut-butter', 'jelly', 'banana', 'pickles'])\n .help('help')\n .argv\n```\n\nIf this method is called multiple times, all enumerated values will be merged\ntogether. Choices are generally strings or numbers, and value matching is\ncase-sensitive.\n\nOptionally `.choices()` can take an object that maps multiple keys to their\nchoices.\n\nChoices can also be specified as `choices` in the object given to `option()`.\n\n```js\nvar argv = require('yargs')\n .option('size', {\n alias: 's',\n describe: 'choose a size',\n choices: ['xs', 's', 'm', 'l', 'xl']\n })\n .argv\n```\n\n<a name=\"coerce\"></a>.coerce(key, fn)\n----------------\n\nProvide a synchronous function to coerce or transform the value(s) given on the\ncommand line for `key`.\n\nThe coercion function should accept one argument, representing the parsed value\nfrom the command line, and should return a new value or throw an error. The\nreturned value will be used as the value for `key` (or one of its aliases) in\n`argv`.\n\nIf the function throws, the error will be treated as a validation\nfailure, delegating to either a custom [`.fail()`](#fail) handler or printing\nthe error message in the console.\n\nCoercion will be applied to a value after\nall other modifications, such as [`.normalize()`](#normalize).\n\n_Examples:_\n\n```js\nvar argv = require('yargs')\n .coerce('file', function (arg) {\n return require('fs').readFileSync(arg, 'utf8')\n })\n .argv\n```\n\nOptionally `.coerce()` can take an object that maps several keys to their\nrespective coercion function.\n\n```js\nvar argv = require('yargs')\n .coerce({\n date: Date.parse,\n json: JSON.parse\n })\n .argv\n```\n\nYou can also map the same function to several keys at one time. Just pass an\narray of keys as the first argument to `.coerce()`:\n\n```js\nvar path = require('path')\nvar argv = require('yargs')\n .coerce(['src', 'dest'], path.resolve)\n .argv\n```\n\nIf you are using dot-notion or arrays, .e.g., `user.email` and `user.password`,\ncoercion will be applied to the final object that has been parsed:\n\n```js\n// --user.name Batman --user.password 123\n// gives us: {name: 'batman', password: '[SECRET]'}\nvar argv = require('yargs')\n .option('user')\n .coerce('user', opt => {\n opt.name = opt.name.toLowerCase()\n opt.password = '[SECRET]'\n return opt\n })\n .argv\n```\n\n.command(cmd, desc, [builder], [handler])\n-----------------------------------------\n.command(cmd, desc, [module])\n-----------------------------\n.command(module)\n----------------\n\nDefine the commands exposed by your application.\n\n`cmd` should be a string representing the command or an array of strings\nrepresenting the command and its aliases. Read more about command aliases in the\nsubsection below.\n\nUse `desc` to provide a description for each command your application accepts (the\nvalues stored in `argv._`). Set `desc` to `false` to create a hidden command.\nHidden commands don't show up in the help output and aren't available for\ncompletion.\n\nOptionally, you can provide a `builder` object to give hints about the\noptions that your command accepts:\n\n```js\nyargs\n .command('get', 'make a get HTTP request', {\n url: {\n alias: 'u',\n default: 'http://yargs.js.org/'\n }\n })\n .help()\n .argv\n```\n\n`builder` can also be a function. This function is executed\nwith a `yargs` instance, and can be used to provide _advanced_ command specific help:\n\n```js\nyargs\n .command('get', 'make a get HTTP request', function (yargs) {\n return yargs.option('url', {\n alias: 'u',\n default: 'http://yargs.js.org/'\n })\n })\n .help()\n .argv\n```\n\nYou can also provide a handler function, which will be executed with the\nparsed `argv` object:\n\n```js\nyargs\n .command(\n 'get',\n 'make a get HTTP request',\n function (yargs) {\n return yargs.option('u', {\n alias: 'url',\n describe: 'the URL to make an HTTP request to'\n })\n },\n function (argv) {\n console.log(argv.url)\n }\n )\n .help()\n .argv\n```\n\n### Default Commands\n\nTo specify a default command use the character `*`. A default command\nwill be run if the positional arguments provided match no known\ncommands:\n\n```js\nconst argv = require('yargs')\n .command('*', 'the default command', () => {}, (argv) => {\n console.log('this command will be run by default')\n })\n```\n\nThe command defined above will be executed if the program\nis run with `./my-cli.js --x=22`.\n\nDefault commands can also be used as a command alias, like so:\n\n```js\nconst argv = require('yargs')\n .command(['serve', '*'], 'the serve command', () => {}, (argv) => {\n console.log('this command will be run by default')\n })\n```\n\nThe command defined above will be executed if the program\nis run with `./my-cli.js --x=22`, or with `./my-cli.js serve --x=22`.\n\n### Positional Arguments\n\nCommands can accept _optional_ and _required_ positional arguments. Required\npositional arguments take the form `<foo>`, and optional arguments\ntake the form `[bar]`. The parsed positional arguments will be populated in\n`argv`:\n\n```js\nyargs.command('get <source> [proxy]', 'make a get HTTP request')\n .help()\n .argv\n```\n\n#### Positional Argument Aliases\n\nAliases can be provided for positional arguments using the `|` character.\nAs an example, suppose our application allows either a username _or_\nan email as the first argument:\n\n```js\nyargs.command('get <username|email> [password]', 'fetch a user by username or email.')\n .help()\n .argv\n```\n\nIn this way, both `argv.username` and `argv.email` would be populated with the\nsame value when the command is executed.\n\n#### Variadic Positional Arguments\n\nThe last positional argument can optionally accept an array of\nvalues, by using the `..` operator:\n\n```js\nyargs.command('download <url> [files..]', 'download several files')\n .help()\n .argv\n```\n\n### Command Execution\n\nWhen a command is given on the command line, yargs will execute the following:\n\n1. push the command into the current context\n2. reset non-global configuration\n3. apply command configuration via the `builder`, if given\n4. parse and validate args from the command line, including positional args\n5. if validation succeeds, run the `handler` function, if given\n6. pop the command from the current context\n\n### Command Aliases\n\nYou can define aliases for a command by putting the command and all of its\naliases into an array.\n\nAlternatively, a command module may specify an `aliases` property, which may be\na string or an array of strings. All aliases defined via the `command` property\nand the `aliases` property will be concatenated together.\n\nThe first element in the array is considered the canonical command, which may\ndefine positional arguments, and the remaining elements in the array are\nconsidered aliases. Aliases inherit positional args from the canonical command,\nand thus any positional args defined in the aliases themselves are ignored.\n\nIf either the canonical command or any of its aliases are given on the command\nline, the command will be executed.\n\n```js\n#!/usr/bin/env node\nrequire('yargs')\n .command(['start [app]', 'run', 'up'], 'Start up an app', {}, (argv) => {\n console.log('starting up the', argv.app || 'default', 'app')\n })\n .command({\n command: 'configure <key> [value]',\n aliases: ['config', 'cfg'],\n desc: 'Set a config variable',\n builder: (yargs) => yargs.default('value', 'true'),\n handler: (argv) => {\n console.log(`setting ${argv.key} to ${argv.value}`)\n }\n })\n .demandCommand()\n .help()\n .wrap(72)\n .argv\n```\n\n```\n$ ./svc.js help\nCommands:\n start [app] Start up an app [aliases: run, up]\n configure <key> [value] Set a config variable [aliases: config, cfg]\n\nOptions:\n --help Show help [boolean]\n\n$ ./svc.js cfg concurrency 4\nsetting concurrency to 4\n\n$ ./svc.js run web\nstarting up the web app\n```\n\n### Providing a Command Module\n\nFor complicated commands you can pull the logic into a module. A module\nsimply needs to export:\n\n* `exports.command`: string (or array of strings) that executes this command when given on the command line, first string may contain positional args\n* `exports.aliases`: array of strings (or a single string) representing aliases of `exports.command`, positional args defined in an alias are ignored\n* `exports.describe`: string used as the description for the command in help text, use `false` for a hidden command\n* `exports.builder`: object declaring the options the command accepts, or a function accepting and returning a yargs instance\n* `exports.handler`: a function which will be passed the parsed argv.\n\n```js\n// my-module.js\nexports.command = 'get <source> [proxy]'\n\nexports.describe = 'make a get HTTP request'\n\nexports.builder = {\n banana: {\n default: 'cool'\n },\n batman: {\n default: 'sad'\n }\n}\n\nexports.handler = function (argv) {\n // do something with argv.\n}\n```\n\nYou then register the module like so:\n\n```js\nyargs.command(require('my-module'))\n .help()\n .argv\n```\n\nOr if the module does not export `command` and `describe` (or if you just want to override them):\n\n```js\nyargs.command('get <source> [proxy]', 'make a get HTTP request', require('my-module'))\n .help()\n .argv\n```\n\n.commandDir(directory, [opts])\n------------------------------\n\nApply command modules from a directory relative to the module calling this method.\n\nThis allows you to organize multiple commands into their own modules under a\nsingle directory and apply all of them at once instead of calling\n`.command(require('./dir/module'))` multiple times.\n\nBy default, it ignores subdirectories. This is so you can use a directory\nstructure to represent your command hierarchy, where each command applies its\nsubcommands using this method in its builder function. See the example below.\n\nNote that yargs assumes all modules in the given directory are command modules\nand will error if non-command modules are encountered. In this scenario, you\ncan either move your module to a different directory or use the `exclude` or\n`visit` option to manually filter it out. More on that below.\n\n`directory` is a relative directory path as a string (required).\n\n`opts` is an options object (optional). The following options are valid:\n\n- `recurse`: boolean, default `false`\n\n Look for command modules in all subdirectories and apply them as a flattened\n (non-hierarchical) list.\n\n- `extensions`: array of strings, default `['js']`\n\n The types of files to look for when requiring command modules.\n\n- `visit`: function\n\n A synchronous function called for each command module encountered. Accepts\n `commandObject`, `pathToFile`, and `filename` as arguments. Returns\n `commandObject` to include the command; any falsy value to exclude/skip it.\n\n- `include`: RegExp or function\n\n Whitelist certain modules. See [`require-directory` whitelisting](https://www.npmjs.com/package/require-directory#whitelisting) for details.\n\n- `exclude`: RegExp or function\n\n Blacklist certain modules. See [`require-directory` blacklisting](https://www.npmjs.com/package/require-directory#blacklisting) for details.\n\n### Example command hierarchy using `.commandDir()`\n\nDesired CLI:\n\n```sh\n$ myapp --help\n$ myapp init\n$ myapp remote --help\n$ myapp remote add base http://yargs.js.org\n$ myapp remote prune base\n$ myapp remote prune base fork whatever\n```\n\nDirectory structure:\n\n```\nmyapp/\n├─ cli.js\n└─ cmds/\n ├─ init.js\n ├─ remote.js\n └─ remote_cmds/\n ├─ add.js\n └─ prune.js\n```\n\ncli.js:\n\n```js\n#!/usr/bin/env node\nrequire('yargs')\n .commandDir('cmds')\n .demandCommand()\n .help()\n .argv\n```\n\ncmds/init.js:\n\n```js\nexports.command = 'init [dir]'\nexports.desc = 'Create an empty repo'\nexports.builder = {\n dir: {\n default: '.'\n }\n}\nexports.handler = function (argv) {\n console.log('init called for dir', argv.dir)\n}\n```\n\ncmds/remote.js:\n\n```js\nexports.command = 'remote <command>'\nexports.desc = 'Manage set of tracked repos'\nexports.builder = function (yargs) {\n return yargs.commandDir('remote_cmds')\n}\nexports.handler = function (argv) {}\n```\n\ncmds/remote_cmds/add.js:\n\n```js\nexports.command = 'add <name> <url>'\nexports.desc = 'Add remote named <name> for repo at url <url>'\nexports.builder = {}\nexports.handler = function (argv) {\n console.log('adding remote %s at url %s', argv.name, argv.url)\n}\n```\n\ncmds/remote_cmds/prune.js:\n\n```js\nexports.command = 'prune <name> [names..]'\nexports.desc = 'Delete tracked branches gone stale for remotes'\nexports.builder = {}\nexports.handler = function (argv) {\n console.log('pruning remotes %s', [].concat(argv.name).concat(argv.names).join(', '))\n}\n```\n\n.completion([cmd], [description], [fn])\n---------------------------------------\n\nEnable bash-completion shortcuts for commands and options.\n\n`cmd`: When present in `argv._`, will result in the `.bashrc` completion script\nbeing outputted. To enable bash completions, concat the generated script to your\n`.bashrc` or `.bash_profile`.\n\n`description`: Provide a description in your usage instructions for the command\nthat generates bash completion scripts.\n\n`fn`: Rather than relying on yargs' default completion functionality, which\nshiver me timbers is pretty awesome, you can provide your own completion\nmethod.\n\nIf invoked without parameters, `.completion()` will make `completion` the command to output\nthe completion script.\n\n```js\nvar argv = require('yargs')\n .completion('completion', function(current, argv) {\n // 'current' is the current command being completed.\n // 'argv' is the parsed arguments so far.\n // simply return an array of completions.\n return [\n 'foo',\n 'bar'\n ];\n })\n .argv;\n```\n\nYou can also provide asynchronous completions.\n\n```js\nvar argv = require('yargs')\n .completion('completion', function(current, argv, done) {\n setTimeout(function() {\n done([\n 'apple',\n 'banana'\n ]);\n }, 500);\n })\n .argv;\n```\n\nBut wait, there's more! You can return an asynchronous promise.\n\n```js\nvar argv = require('yargs')\n .completion('completion', function(current, argv, done) {\n return new Promise(function (resolve, reject) {\n setTimeout(function () {\n resolve(['apple', 'banana'])\n }, 10)\n })\n })\n .argv;\n```\n\n<a name=\"config\"></a>.config([key], [description], [parseFn])\n-------------------------------------------------------------\n.config(object)\n---------------\n\nTells the parser that if the option specified by `key` is passed in, it\nshould be interpreted as a path to a JSON config file. The file is loaded\nand parsed, and its properties are set as arguments. Because the file is\nloaded using Node's require(), the filename MUST end in `.json` to be\ninterpreted correctly.\n\nIf invoked without parameters, `.config()` will make `--config` the option to pass the JSON config file.\n\nAn optional `description` can be provided to customize the config (`key`) option\nin the usage string.\n\nAn optional `parseFn` can be used to provide a custom parser. The parsing\nfunction must be synchronous, and should return an object containing\nkey value pairs or an error.\n\n```js\nvar argv = require('yargs')\n .config('settings', function (configPath) {\n return JSON.parse(fs.readFileSync(configPath, 'utf-8'))\n })\n .argv\n```\n\nYou can also pass an explicit configuration `object`, it will be parsed\nand its properties will be set as arguments.\n\n```js\nvar argv = require('yargs')\n .config({foo: 1, bar: 2})\n .argv\nconsole.log(argv)\n```\n\n```\n$ node test.js\n{ _: [],\n foo: 1,\n bar: 2,\n '$0': 'test.js' }\n```\n\nNote that a configuration object may extend from a JSON file using the `\"extends\"` property. When doing so, the `\"extends\"` value should be a path (relative or absolute) to the extended JSON file.\n\n<a name=\"conflicts\"></a>.conflicts(x, y)\n----------------------------------------------\n\nGiven the key `x` is set, the key `y` must not be set.\n\nOptionally `.conflicts()` can accept an object specifying multiple conflicting keys.\n\n<a name=\"count\"></a>.count(key)\n------------\n\nInterpret `key` as a boolean flag, but set its parsed value to the number of\nflag occurrences rather than `true` or `false`. Default value is thus `0`.\n\n<a name=\"default\"></a>.default(key, value, [description])\n---------------------------------------------------------\n.defaults(key, value, [description])\n------------------------------------\n\n**Note:** The `.defaults()` alias is deprecated. It will be\nremoved in the next major version.\n\nSet `argv[key]` to `value` if no option was specified in `process.argv`.\n\nOptionally `.default()` can take an object that maps keys to default values.\n\nBut wait, there's more! The default value can be a `function` which returns\na value. The name of the function will be used in the usage string:\n\n```js\nvar argv = require('yargs')\n .default('random', function randomValue() {\n return Math.random() * 256;\n }).argv;\n```\n\nOptionally, `description` can also be provided and will take precedence over\ndisplaying the value in the usage instructions:\n\n```js\n.default('timeout', 60000, '(one-minute)')\n```\n\n<a name=\"demand\"></a>.demand(count, [max], [msg]) [DEPRECATED]\n--------------------\n\n`demand()` has been deprecated, please instead see [`demandOption()`](#demandOption) and\n[`demandCommand()`](#demandCommand).\n\n<a name=\"demandOption\"></a>.demandOption(key, [msg | boolean])\n------------------------------\n.demandOption(key, msg)\n------------------------------\n\nIf `key` is a string, show the usage information and exit if `key` wasn't\nspecified in `process.argv`.\n\nIf `key` is an array, demand each element.\n\nIf a `msg` string is given, it will be printed when the argument is missing, instead of the standard error message.\n\n```javascript\n// demand an array of keys to be provided\nrequire('yargs')\n .option('run', {\n alias: 'r',\n describe: 'run your program'\n })\n .option('path', {\n alias: 'p',\n describe: 'provide a path to file'\n })\n .option('spec', {\n alias: 's',\n describe: 'program specifications'\n })\n .demandOption(['run', 'path'], 'Please provide both run and path arguments to work with this tool')\n .help()\n .argv\n```\nwhich will provide the following output:\n```bash\nOptions:\n --run, -r run your program [required]\n --path, -p provide a path to file [required]\n --spec, -s program specifications\n --help Show help [boolean]\n\n Missing required arguments: run, path\n Please provide both run and path arguments to work with this tool\n```\n\nIf a `boolean` value is given, it controls whether the option is demanded;\nthis is useful when using `.options()` to specify command line parameters.\n\n```javascript\n// demand individual options within the option constructor\nrequire('yargs')\n .options({\n 'run': {\n alias: 'r',\n describe: 'run your program',\n demandOption: true\n },\n 'path': {\n alias: 'p',\n describe: 'provide a path to file',\n demandOption: true\n },\n 'spec': {\n alias: 's',\n describe: 'program specifications'\n }\n })\n .help()\n .argv\n```\nwhich will provide the following output:\n```bash\nOptions:\n --run, -r run your program [required]\n --path, -p provide a path to file [required]\n --spec, -s program specifications\n --help Show help [boolean]\n\nMissing required arguments: run, path\n```\n\n<a name=\"demandCommand\"></a>.demandCommand([min=1], [minMsg])\n------------------------------\n.demandCommand([min=1], [max], [minMsg], [maxMsg])\n------------------------------\n\nDemand in context of commands. You can demand a minimum and a maximum number a user can have within your program, as well as provide corresponding error messages if either of the demands is not met.\n```javascript\nrequire('yargs')\n .command({\n command: 'configure <key> [value]',\n aliases: ['config', 'cfg'],\n desc: 'Set a config variable',\n builder: (yargs) => yargs.default('value', 'true'),\n handler: (argv) => {\n console.log(`setting ${argv.key} to ${argv.value}`)\n }\n })\n // provide a minimum demand and a minimum demand message\n .demandCommand(1, 'You need at least one command before moving on')\n .help()\n .argv\n```\n\nwhich will provide the following output:\n\n```bash\nCommands:\n configure <key> [value] Set a config variable [aliases: config, cfg]\n\nOptions:\n --help Show help [boolean]\n\nYou need at least one command before moving on\n```\n\n_Note: in `minMsg` and `maxMsg`, every occurrence of `$0` will be replaced\nwith the observed value, and every instance of `$1` will be replaced with the\nexpected value._\n\n<a name=\"describe\"></a>.describe(key, desc)\n--------------------\n\nDescribe a `key` for the generated usage information.\n\nOptionally `.describe()` can take an object that maps keys to descriptions.\n\n.detectLocale(boolean)\n-----------\n\nShould yargs attempt to detect the os' locale? Defaults to `true`.\n\n.env([prefix])\n--------------\n\nTell yargs to parse environment variables matching the given prefix and apply\nthem to argv as though they were command line arguments.\n\nUse the \"__\" separator in the environment variable to indicate nested options.\n(e.g. prefix_nested__foo => nested.foo)\n\nIf this method is called with no argument or with an empty string or with `true`,\nthen all env vars will be applied to argv.\n\nProgram arguments are defined in this order of precedence:\n\n1. Command line args\n2. Env vars\n3. Config file/objects\n4. Configured defaults\n\n```js\nvar argv = require('yargs')\n .env('MY_PROGRAM')\n .option('f', {\n alias: 'fruit-thing',\n default: 'apple'\n })\n .argv\nconsole.log(argv)\n```\n\n```\n$ node fruity.js\n{ _: [],\n f: 'apple',\n 'fruit-thing': 'apple',\n fruitThing: 'apple',\n '$0': 'fruity.js' }\n```\n\n```\n$ MY_PROGRAM_FRUIT_THING=banana node fruity.js\n{ _: [],\n fruitThing: 'banana',\n f: 'banana',\n 'fruit-thing': 'banana',\n '$0': 'fruity.js' }\n```\n\n```\n$ MY_PROGRAM_FRUIT_THING=banana node fruity.js -f cat\n{ _: [],\n f: 'cat',\n 'fruit-thing': 'cat',\n fruitThing: 'cat',\n '$0': 'fruity.js' }\n```\n\nEnv var parsing is disabled by default, but you can also explicitly disable it\nby calling `.env(false)`, e.g. if you need to undo previous configuration.\n\n.epilog(str)\n------------\n.epilogue(str)\n--------------\n\nA message to print at the end of the usage instructions, e.g.\n\n```js\nvar argv = require('yargs')\n .epilogue('for more information, find our manual at http://example.com');\n```\n\n.example(cmd, desc)\n-------------------\n\nGive some example invocations of your program. Inside `cmd`, the string\n`$0` will get interpolated to the current script name or node command for the\npresent script similar to how `$0` works in bash or perl.\nExamples will be printed out as part of the help message.\n\n<a name=\"exitprocess\"></a>.exitProcess(enable)\n----------------------------------\n\nBy default, yargs exits the process when the user passes a help flag, uses the\n`.version` functionality, or when validation fails. Calling\n`.exitProcess(false)` disables this behavior, enabling further actions after\nyargs have been validated.\n\n<a name=\"fail\"></a>.fail(fn)\n---------\n\nMethod to execute when a failure occurs, rather than printing the failure message.\n\n`fn` is called with the failure message that would have been printed, the\n`Error` instance originally thrown and yargs state when the failure\noccured.\n\n```js\nvar argv = require('yargs')\n .fail(function (msg, err, yargs) {\n if (err) throw err // preserve stack\n console.error('You broke it!')\n console.error(msg)\n console.error('You should be doing', yargs.help())\n process.exit(1)\n })\n .argv\n```\n\n.getCompletion(args, done);\n---------------------------\n\nAllows to programmatically get completion choices for any line.\n\n`args`: An array of the words in the command line to complete.\n\n`done`: The callback to be called with the resulting completions.\n\nFor example:\n\n```js\nrequire('yargs')\n .option('foobar')\n .option('foobaz')\n .completion()\n .getCompletion(['./test.js', '--foo'], function (completions) {\n console.log(completions)\n })\n```\n\nOutputs the same completion choices as `./test.js --foo`<kbd>TAB</kbd>: `--foobar` and `--foobaz`\n\n<a name=\"global\"></a>.global(globals, [global=true])\n------------\n\nIndicate that an option (or group of options) should not be reset when a command\nis executed, as an example:\n\n```js\nvar argv = require('yargs')\n .option('a', {\n alias: 'all',\n default: true,\n global: false\n })\n .option('n', {\n alias: 'none',\n default: true,\n global: false\n })\n .command('foo', 'foo command', function (yargs) {\n return yargs.option('b', {\n alias: 'bar'\n })\n })\n .help('help')\n .global('a')\n .argv\n```\n\nIf the `foo` command is executed the `all` option will remain, but the `none`\noption will have been eliminated.\n\nOptions default to being global.\n\n<a name=\"group\"></a>.group(key(s), groupName)\n--------------------\n\nGiven a key, or an array of keys, places options under an alternative heading\nwhen displaying usage instructions, e.g.,\n\n```js\nvar yargs = require('yargs')(['--help'])\n .help()\n .group('batman', 'Heroes:')\n .describe('batman', \"world's greatest detective\")\n .wrap(null)\n .argv\n```\n***\n Heroes:\n --batman world's greatest detective\n\n Options:\n --help Show help [boolean]\n\n<a name=\"help\"></a>.help()\n-----------------------------------------\n.help([option | boolean])\n-----------------------------------------\n.help([option, [description | boolean]])\n-----------------------------------------\n.help([option, [description, [boolean]]])\n-----------------------------------------\n\nAdd an option (e.g. `--help`) and implicit command that displays the usage\nstring and exits the process.\n\nIf present, the `description` parameter customizes the description of\nthe help option in the usage string.\n\nIf a boolean argument is provided, it will enable or disable the use of an\nimplicit command. The implicit command is enabled by default, but it can be\ndisabled by passing `false`.\n\nNote that any multi-char aliases (e.g. `help`) used for the help option will\nalso be used for the implicit command. If there are no multi-char aliases (e.g.\n`h`), then all single-char aliases will be used for the command.\n\nIf invoked without parameters, `.help()` will use `--help` as the option and\n`help` as the implicit command to trigger help output.\n\nExample:\n\n```js\nvar yargs = require(\"yargs\")(['--help'])\n .usage(\"$0 -operand1 number -operand2 number -operation [add|subtract]\")\n .help()\n .argv\n```\n\nLater on, `argv` can be retrieved with `yargs.argv`.\n\n<a name=\"implies\"></a>.implies(x, y)\n--------------\n\nGiven the key `x` is set, it is required that the key `y` is set.\n\nOptionally `.implies()` can accept an object specifying multiple implications.\n\n.locale()\n---------\n\nReturn the locale that yargs is currently using.\n\nBy default, yargs will auto-detect the operating system's locale so that\nyargs-generated help content will display in the user's language.\n\nTo override this behavior with a static locale, pass the desired locale as a\nstring to this method (see below).\n\n.locale(locale)\n---------------\n\nOverride the auto-detected locale from the user's operating system with a static\nlocale. Note that the OS locale can be modified by setting/exporting the `LC_ALL`\nenvironment variable.\n\n```js\nvar argv = require('yargs')\n .usage('./$0 - follow ye instructions true')\n .option('option', {\n alias: 'o',\n describe: \"'tis a mighty fine option\",\n demandOption: true\n })\n .command('run', \"Arrr, ya best be knowin' what yer doin'\")\n .example('$0 run foo', \"shiver me timbers, here's an example for ye\")\n .help('help')\n .wrap(70)\n .locale('pirate')\n .argv\n```\n\n***\n\n```shell\n./test.js - follow ye instructions true\n\nChoose yer command:\n run Arrr, ya best be knowin' what yer doin'\n\nOptions for me hearties!\n --option, -o 'tis a mighty fine option [requi-yar-ed]\n --help Parlay this here code of conduct [boolean]\n\nEx. marks the spot:\n test.js run foo shiver me timbers, here's an example for ye\n\nYe be havin' to set the followin' argument land lubber: option\n```\n\nLocales currently supported:\n\n* **de:** German.\n* **en:** American English.\n* **es:** Spanish.\n* **fr:** French.\n* **hi:** Hindi.\n* **hu:** Hungarian.\n* **id:** Indonesian.\n* **it:** Italian.\n* **ja:** Japanese.\n* **ko:** Korean.\n* **nb:** Norwegian Bokmål.\n* **pirate:** American Pirate.\n* **pl:** Polish.\n* **pt:** Portuguese.\n* **pt_BR:** Brazilian Portuguese.\n* **ru:** Russian.\n* **th:** Thai.\n* **tr:** Turkish.\n* **zh_CN:** Chinese.\n\nTo submit a new translation for yargs:\n\n1. use `./locales/en.json` as a starting point.\n2. submit a pull request with the new locale file.\n\n*The [Microsoft Terminology Search](http://www.microsoft.com/Language/en-US/Search.aspx) can be useful for finding the correct terminology in your locale.*\n\n<a name=\"nargs\"></a>.nargs(key, count)\n-----------\n\nThe number of arguments that should be consumed after a key. This can be a\nuseful hint to prevent parsing ambiguity. For example:\n\n```js\nvar argv = require('yargs')\n .nargs('token', 1)\n .parse(['--token', '-my-token']);\n```\n\nparses as:\n\n`{ _: [], token: '-my-token', '$0': 'node test' }`\n\nOptionally `.nargs()` can take an object of `key`/`narg` pairs.\n\n<a name=\"normalize\"></a>.normalize(key)\n---------------\n\nThe key provided represents a path and should have `path.normalize()` applied.\n\n<a name=\"number\"></a>.number(key)\n------------\n\nTell the parser to always interpret `key` as a number.\n\nIf `key` is an array, all elements will be parsed as numbers.\n\nIf the option is given on the command line without a value, `argv` will be\npopulated with `undefined`.\n\nIf the value given on the command line cannot be parsed as a number, `argv` will\nbe populated with `NaN`.\n\nNote that decimals, hexadecimals, and scientific notation are all accepted.\n\n```js\nvar argv = require('yargs')\n .number('n')\n .number(['width', 'height'])\n .argv\n```\n\n.option(key, [opt])\n-----------------\n.options(key, [opt])\n------------------\n\nThis method can be used to make yargs aware of options that _could_\nexist. You can also pass an `opt` object which can hold further\ncustomization, like `.alias()`, `.demandOption()` etc. for that option.\n\nFor example:\n\n````javascript\nvar argv = require('yargs')\n .option('f', {\n alias: 'file',\n demandOption: true,\n default: '/etc/passwd',\n describe: 'x marks the spot',\n type: 'string'\n })\n .argv\n;\n````\n\nis the same as\n\n````javascript\nvar argv = require('yargs')\n .alias('f', 'file')\n .demandOption('f')\n .default('f', '/etc/passwd')\n .describe('f', 'x marks the spot')\n .string('f')\n .argv\n;\n````\n\nOptionally `.options()` can take an object that maps keys to `opt` parameters.\n\n````javascript\nvar argv = require('yargs')\n .options({\n 'f': {\n alias: 'file',\n demandOption: true,\n default: '/etc/passwd',\n describe: 'x marks the spot',\n type: 'string'\n }\n })\n .argv\n;\n````\n\nValid `opt` keys include:\n\n- `alias`: string or array of strings, alias(es) for the canonical option key, see [`alias()`](#alias)\n- `array`: boolean, interpret option as an array, see [`array()`](#array)\n- `boolean`: boolean, interpret option as a boolean flag, see [`boolean()`](#boolean)\n- `choices`: value or array of values, limit valid option arguments to a predefined set, see [`choices()`](#choices)\n- `coerce`: function, coerce or transform parsed command line values into another value, see [`coerce()`](#coerce)\n- `config`: boolean, interpret option as a path to a JSON config file, see [`config()`](#config)\n- `configParser`: function, provide a custom config parsing function, see [`config()`](#config)\n- `conflicts`: string or object, require certain keys not to be set, see [`conflicts()`](#conflicts)\n- `count`: boolean, interpret option as a count of boolean flags, see [`count()`](#count)\n- `default`: value, set a default value for the option, see [`default()`](#default)\n- `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)\n- `demandOption`: boolean or string, demand the option be given, with optional error message, see [`demandOption()`](#demandOption)\n- `desc`/`describe`/`description`: string, the option description for help content, see [`describe()`](#describe)\n- `global`: boolean, indicate that this key should not be [reset](#reset) when a command is invoked, see [`global()`](#global)\n- `group`: string, when displaying usage instructions place the option under an alternative group heading, see [`group()`](#group)\n- `implies`: string or object, require certain keys to be set, see [`implies()`](#implies)\n- `nargs`: number, specify how many arguments should be consumed for the option, see [`nargs()`](#nargs)\n- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)\n- `number`: boolean, interpret option as a number, [`number()`](#number)\n- `requiresArg`: boolean, require the option be specified with a value, see [`requiresArg()`](#requiresArg)\n- `skipValidation`: boolean, skips validation if the option is present, see [`skipValidation()`](#skipValidation)\n- `string`: boolean, interpret option as a string, see [`string()`](#string)\n- `type`: one of the following strings\n - `'array'`: synonymous for `array: true`, see [`array()`](#array)\n - `'boolean'`: synonymous for `boolean: true`, see [`boolean()`](#boolean)\n - `'count'`: synonymous for `count: true`, see [`count()`](#count)\n - `'number'`: synonymous for `number: true`, see [`number()`](#number)\n - `'string'`: synonymous for `string: true`, see [`string()`](#string)\n\n.parse(args, [context], [parseCallback])\n------------\n\nParse `args` instead of `process.argv`. Returns the `argv` object.\n`args` may either be a pre-processed argv array, or a raw argument string.\n\nA `context` object can optionally be given as the second argument to `parse()`, providing a\nuseful mechanism for passing state information to commands:\n\n```js\nconst parser = yargs\n .command('lunch-train <restaurant>', 'start lunch train', function () {}, function (argv) {\n console.log(argv.restaurant, argv.time)\n })\n .parse(\"lunch-train rudy's\", {time: '12:15'})\n```\n\nA `parseCallback` can also be provided to `.parse()`. If a callback is given, it will be invoked with three arguments:\n\n1. `err`: populated if any validation errors raised while parsing.\n2. `argv`: the parsed argv object.\n3. `output`: any text that would have been output to the terminal, had a\n callback not been provided.\n\n```js\n// providing the `fn` argument to `parse()` runs yargs in headless mode, this\n// makes it easy to use yargs in contexts other than the CLI, e.g., writing\n// a chat-bot.\nconst parser = yargs\n .command('lunch-train <restaurant> <time>', 'start lunch train', function () {}, function (argv) {\n api.scheduleLunch(argv.restaurant, moment(argv.time))\n })\n .help()\n\nparser.parse(bot.userText, function (err, argv, output) {\n if (output) bot.respond(output)\n})\n```\n\n***Note:*** Providing a callback to `parse()` disables the [`exitProcess` setting](#exitprocess) until after the callback is invoked.\n\n.pkgConf(key, [cwd])\n------------\n\nSimilar to [`config()`](#config), indicates that yargs should interpret the object from the specified key in package.json\nas a configuration object.\n\n`cwd` can optionally be provided, the package.json will be read\nfrom this location.\n\nNote that a configuration stanza in package.json may extend from an identically keyed stanza in another package.json file using the `\"extends\"` property. When doing so, the `\"extends\"` value should be a path (relative or absolute) to the extended package.json file.\n\n.recommendCommands()\n---------------------------\n\nShould yargs provide suggestions regarding similar commands if no matching\ncommand is found?\n\n.require(key, [msg | boolean])\n------------------------------\n.required(key, [msg | boolean])\n------------------------------\n\nAn alias for [`demand()`](#demand). See docs there.\n\n<a name=\"requiresArg\"></a>.requiresArg(key)\n-----------------\n\nSpecifies either a single option key (string), or an array of options that\nmust be followed by option values. If any option value is missing, show the\nusage information and exit.\n\nThe default behavior is to set the value of any key not followed by an\noption value to `true`.\n\n<a name=\"reset\"></a>.reset()\n--------\n\nReset the argument object built up so far. This is useful for\ncreating nested command line interfaces. Use [global](#global)\nto specify keys that should not be reset.\n\n```js\nvar yargs = require('yargs')\n .usage('$0 command')\n .command('hello', 'hello command')\n .command('world', 'world command')\n .demandCommand(1, 'must provide a valid command'),\n argv = yargs.argv,\n command = argv._[0];\n\nif (command === 'hello') {\n yargs.reset()\n .usage('$0 hello')\n .help('h')\n .example('$0 hello', 'print the hello message!')\n .argv\n\n console.log('hello!');\n} else if (command === 'world'){\n yargs.reset()\n .usage('$0 world')\n .help('h')\n .example('$0 world', 'print the world message!')\n .argv\n\n console.log('world!');\n} else {\n yargs.showHelp();\n}\n```\n\n.showCompletionScript()\n----------------------\n\nGenerate a bash completion script. Users of your application can install this\nscript in their `.bashrc`, and yargs will provide completion shortcuts for\ncommands and options.\n\n.showHelp(consoleLevel='error')\n---------------------------\n\nPrint the usage data using the [`console`](https://nodejs.org/api/console.html) function `consoleLevel` for printing.\n\nExample:\n\n```js\nvar yargs = require(\"yargs\")\n .usage(\"$0 -operand1 number -operand2 number -operation [add|subtract]\");\nyargs.showHelp(); //prints to stderr using console.error()\n```\n\nOr, to print the usage data to `stdout` instead, you can specify the use of `console.log`:\n\n```js\nyargs.showHelp(\"log\"); //prints to stdout using console.log()\n```\n\nLater on, `argv` can be retrieved with `yargs.argv`.\n\n.showHelpOnFail(enable, [message])\n----------------------------------\n\nBy default, yargs outputs a usage string if any error is detected. Use the\n`.showHelpOnFail()` method to customize this behavior. If `enable` is `false`,\nthe usage string is not output. If the `message` parameter is present, this\nmessage is output after the error message.\n\nline_count.js:\n\n````javascript\n#!/usr/bin/env node\nvar argv = require('yargs')\n .usage('Count the lines in a file.\\nUsage: $0 -f <file>')\n .demandOption('f')\n .alias('f', 'file')\n .describe('f', 'Load a file')\n .string('f')\n .showHelpOnFail(false, 'Specify --help for available options')\n .help('help')\n .argv;\n\n// etc.\n````\n\n***\n\n```\n$ node line_count.js\nMissing argument value: f\n\nSpecify --help for available options\n```\n\n<a name=\"skipValidation\"></a>.skipValidation(key)\n-----------------\n\nSpecifies either a single option key (string), or an array of options.\nIf any of the options is present, yargs validation is skipped.\n\n.strict([global=true])\n---------\n\nAny command-line argument given that is not demanded, or does not have a\ncorresponding description, will be reported as an error.\n\n`global` indicates whether `strict()` should be enabled both\nat the top-level and for each sub-command.\n\n<a name=\"string\"></a>.string(key)\n------------\n\nTell the parser logic not to interpret `key` as a number or boolean.\nThis can be useful if you need to preserve leading zeros in an input.\n\nIf `key` is an array, interpret all the elements as strings.\n\n`.string('_')` will result in non-hyphenated arguments being interpreted as strings,\nregardless of whether they resemble numbers.\n\n.updateLocale(obj)\n------------------\n.updateStrings(obj)\n------------------\n\nOverride the default strings used by yargs with the key/value\npairs provided in `obj`:\n\n```js\nvar argv = require('yargs')\n .command('run', 'the run command')\n .help('help')\n .updateStrings({\n 'Commands:': 'My Commands -->\\n'\n })\n .wrap(null)\n .argv\n```\n\n***\n\n```shell\nMy Commands -->\n\n run the run command\n\nOptions:\n --help Show help [boolean]\n```\n\nIf you explicitly specify a `locale()`, you should do so *before* calling\n`updateStrings()`.\n\n.usage(message, [opts])\n---------------------\n\nSet a usage message to show which commands to use. Inside `message`, the string\n`$0` will get interpolated to the current script name or node command for the\npresent script similar to how `$0` works in bash or perl.\n\n`opts` is optional and acts like calling `.options(opts)`.\n\n<a name=\"version\"></a>.version([option], [description], [version])\n----------------------------------------\n\nAdd an option (e.g. `--version`) that displays the version number (given by the\n`version` parameter) and exits the process.\n\nIf no arguments are passed to `version` (`.version()`), yargs will parse the `package.json`\nof your module and use its `version` value. The default value of `option` is `--version`.\n\nYou can provide a `function` for version, rather than a string.\nThis is useful if you want to use a version stored in a location other than package.json:\n\n```js\nvar argv = require('yargs')\n .version(function() {\n return require('../lib/version').version;\n })\n .argv;\n```\n\n<a name=\"wrap\"></a>.wrap(columns)\n--------------\n\nFormat usage output to wrap at `columns` many columns.\n\nBy default wrap will be set to `Math.min(80, windowWidth)`. Use `.wrap(null)` to\nspecify no column limit (no right-align). Use `.wrap(yargs.terminalWidth())` to\nmaximize the width of yargs' usage instructions.\n\nparsing tricks\n==============\n\nstop parsing\n------------\n\nUse `--` to stop parsing flags and stuff the remainder into `argv._`.\n\n $ node examples/reflect.js -a 1 -b 2 -- -c 3 -d 4\n { _: [ '-c', '3', '-d', '4' ],\n a: 1,\n b: 2,\n '$0': 'examples/reflect.js' }\n\nnegate fields\n-------------\n\nIf you want to explicitly set a field to false instead of just leaving it\nundefined or to override a default you can do `--no-key`.\n\n $ node examples/reflect.js -a --no-b\n { _: [], a: true, b: false, '$0': 'examples/reflect.js' }\n\nnumbers\n-------\n\nEvery argument that looks like a number (`!isNaN(Number(arg))`) is converted to\none. This way you can just `net.createConnection(argv.port)` and you can add\nnumbers out of `argv` with `+` without having that mean concatenation,\nwhich is super frustrating.\n\nduplicates\n----------\n\nIf you specify a flag multiple times it will get turned into an array containing\nall the values in order.\n\n $ node examples/reflect.js -x 5 -x 8 -x 0\n { _: [], x: [ 5, 8, 0 ], '$0': 'examples/reflect.js' }\n\ndot notation\n------------\n\nWhen you use dots (`.`s) in argument names, an implicit object path is assumed.\nThis lets you organize arguments into nested objects.\n\n $ node examples/reflect.js --foo.bar.baz=33 --foo.quux=5\n { _: [],\n foo: { bar: { baz: 33 }, quux: 5 },\n '$0': 'examples/reflect.js' }\n\nshort numbers\n-------------\n\nShort numeric `-n5` style arguments work too:\n\n $ node examples/reflect.js -n123 -m456\n { _: [], n: 123, m: 456, '$0': 'examples/reflect.js' }\n\ninstallation\n============\n\nWith [npm](https://github.com/npm/npm), just do:\n\n npm install yargs\n\nor clone this project on github:\n\n git clone http://github.com/yargs/yargs.git\n\nTo run the tests with npm, just do:\n\n npm test\n\nconfiguration\n=============\n\nUsing the `yargs` stanza in your `package.json` you can turn on and off\nsome of yargs' parsing features:\n\n```json\n{\n \"yargs\": {\n \"short-option-groups\": true,\n \"camel-case-expansion\": true,\n \"dot-notation\": true,\n \"parse-numbers\": true,\n \"boolean-negation\": true\n }\n}\n```\n\nSee the [yargs-parser](https://github.com/yargs/yargs-parser#configuration) module\nfor detailed documentation of this feature.\n\ninspired by\n===========\n\nThis module is loosely inspired by Perl's\n[Getopt::Casual](http://search.cpan.org/~photo/Getopt-Casual-0.13.1/Casual.pm).\n\n[travis-url]: https://travis-ci.org/yargs/yargs\n[travis-image]: https://img.shields.io/travis/yargs/yargs/master.svg\n[coveralls-url]: https://coveralls.io/github/yargs/yargs\n[coveralls-image]: https://img.shields.io/coveralls/yargs/yargs.svg\n[npm-url]: https://www.npmjs.com/package/yargs\n[npm-image]: https://img.shields.io/npm/v/yargs.svg\n[windows-url]: https://ci.appveyor.com/project/bcoe/yargs-ljwvf\n[windows-image]: https://img.shields.io/appveyor/ci/bcoe/yargs-ljwvf/master.svg?label=Windows%20Tests\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard-url]: http://standardjs.com/\n[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg\n[conventional-commits-url]: https://conventionalcommits.org/\n[gitter-image]: https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000\n[gitter-url]: https://gitter.im/yargs/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link\n", + "readmeFilename": "README.md", + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/yargs/yargs.git" }, - "greenkeeper": { + "scripts": { + "coverage": "nyc report --reporter=text-lcov | coveralls", + "pretest": "standard", + "release": "standard-version", + "test": "nyc --cache mocha --require ./test/before.js --timeout=8000 --check-leaks" + }, + "standard": { "ignore": [ - "string-width", - "read-pkg-up", - "camelcase" + "**/example/**" ] - } + }, + "version": "7.1.0" } diff --git a/package.json b/package.json index 0bf0ae3c2..b4b79b6a8 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "pogen": "file:tooling/pogen/", "react": "^15.5.4", "react-dom": "^15.5.4", + "structured-clone": "^0.2.2", "through2": "^2.0.1", "ts-loader": "^2.0.3", "tslint": "^5.3.2", diff --git a/src/memidb-test.ts b/src/memidb-test.ts new file mode 100644 index 000000000..8f8498a6e --- /dev/null +++ b/src/memidb-test.ts @@ -0,0 +1,114 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +import {test} from "ava"; +import * as memidb from "./memidb"; + +test.cb("db open", (t) => { + let ncb = 0; + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + let called = false; + req.onupgradeneeded = (evt) => { + ncb += 1; + called = true; + t.is(req.result, evt.target); + t.is(evt.oldVersion, 0); + t.is(evt.newVersion, 1); + t.truthy(req.result); + t.pass(); + } + req.onsuccess = (evt) => { + t.is(ncb, 1); + t.is(req.result, evt.target); + t.truthy(req.result); + t.end(); + } +}); + +test.cb("store creation", (t) => { + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + req.onupgradeneeded = (evt) => { + const db: IDBDatabase = req.result; + + const store1 = db.createObjectStore("b-store"); + t.is(store1.name, "b-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["b-store"]); + + const store2 = db.createObjectStore("a-store"); + t.is(store2.name, "a-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["a-store", "b-store"]); + + const store3 = db.createObjectStore("c-store"); + t.is(store3.name, "c-store"); + t.deepEqual(Array.from(db.objectStoreNames), ["a-store", "b-store", "c-store"]); + t.pass(); + } + req.onsuccess = (evt) => { + t.end(); + } +}); + + +test.cb("put and get", (t) => { + const idb = new memidb.MemoryIDBFactory(); + const req = idb.open("testdb"); + req.onupgradeneeded = (evt) => { + const db: IDBDatabase = req.result; + const store1 = db.createObjectStore("mystore"); + store1.put({answer: 42}, "a"); + } + req.onsuccess = (evt) => { + t.end() + } +}); + + +test("key path evaluation", (t) => { + const obj = { + a: { + b: { + c: 42, + }, + }, + b: "hello", + "": "spam", + arr: ["foo", "bar"], + } + t.deepEqual(memidb.evaluateKeyPath(obj, ""), obj); + t.deepEqual(memidb.evaluateKeyPath(obj, "a.b.c"), 42); + t.deepEqual(memidb.evaluateKeyPath(obj, "a.b"), {c: 42}); + t.deepEqual(memidb.evaluateKeyPath(obj, "foo"), undefined); + t.deepEqual(memidb.evaluateKeyPath(obj, ["a.b.c", "foo"]), undefined); + t.deepEqual(memidb.evaluateKeyPath(obj, ["a.b.c", "b"]), [42, "hello"]); + t.deepEqual(memidb.evaluateKeyPath(obj, "arr.0"), "foo"); + t.deepEqual(memidb.evaluateKeyPath(obj, "."), "spam"); +}); + +test("key path evaluation with replacement", (t) => { + const obj: any = { + a: { + b: { + c: 42, + }, + }, + } + memidb.evaluateKeyPath(obj, "a.b.c", 24); + t.is(obj.a.b.c, 24); + memidb.evaluateKeyPath(obj, "a.b", 24); + t.is(obj.a.b, 24); +}); diff --git a/src/memidb.ts b/src/memidb.ts index 36607d71f..14a1efbe8 100644 --- a/src/memidb.ts +++ b/src/memidb.ts @@ -29,20 +29,25 @@ const structuredClone = require("structured-clone"); -const structuredSerialize = require("structured-clone").serialize; -interface StoredObject { - key: any; - object: string; -} - interface Store { name: string; - keyPath: string | string[]; + keyPath?: string | string[]; keyGenerator: number; autoIncrement: boolean; - objects: { [strKey: string]: StoredObject }; + objects: { [primaryKey: string]: any }; + indices: { [indexName: string]: Index }; +} + +interface Index { + multiEntry: boolean; + unique: boolean; + + /** + * Map the index's key to the primary key. + */ + map: { [indexKey: string]: string[] }; } @@ -80,11 +85,80 @@ class MyDomStringList extends Array<string> implements DOMStringList { } -function callEventHandler(h: EventListenerOrEventListenerObject, evt: Event, target: any) { - if ("handleEvent" in h) { - (h as EventListenerObject).handleEvent(evt); - } else { - (h as EventListener).call(target, evt); +class MyKeyRange implements IDBKeyRange { + static only(value: any): IDBKeyRange { + return new MyKeyRange(value, value, false, false); + } + + static bound(lower: any, upper: any, lowerOpen: boolean = false, upperOpen: boolean = false) { + return new MyKeyRange(lower, upper, lowerOpen, upperOpen); + } + + static lowerBound(lower: any, lowerOpen: boolean = false) { + return new MyKeyRange(lower, undefined, lowerOpen, true); + } + + static upperBound(upper: any, upperOpen: boolean = false) { + return new MyKeyRange(undefined, upper, true, upperOpen); + } + + constructor(public lower: any, public upper: any, public lowerOpen: boolean, public upperOpen: boolean) { + } +} + + +/** + * Type guard for an IDBKeyRange. + */ +export function isKeyRange(obj: any): obj is IDBKeyRange { + return (typeof obj === "object" && + "lower" in obj && "upper" in obj && + "lowerOpen" in obj && "upperOpen" in obj); +} + + +class IndexHandle implements IDBIndex { + + _unique: boolean; + _multiEntry: boolean; + + get keyPath(): string | string[] { + throw Error("not implemented"); + } + + get name () { + return this.indexName; + } + + get unique() { + return this._unique; + } + + get multiEntry() { + return this._multiEntry; + } + + constructor(public objectStore: MyObjectStore, public indexName: string) { + } + + count(key?: IDBKeyRange | IDBValidKey): IDBRequest { + throw Error("not implemented"); + } + + get(key: IDBKeyRange | IDBValidKey): IDBRequest { + throw Error("not implemented"); + } + + getKey(key: IDBKeyRange | IDBValidKey): IDBRequest { + throw Error("not implemented"); + } + + openCursor(range?: IDBKeyRange | IDBValidKey, direction?: IDBCursorDirection): IDBRequest { + throw Error("not implemented"); + } + + openKeyCursor(range?: IDBKeyRange | IDBValidKey, direction?: IDBCursorDirection): IDBRequest { + throw Error("not implemented"); } } @@ -92,9 +166,10 @@ class MyRequest implements IDBRequest { onerror: (this: IDBRequest, ev: Event) => any; onsuccess: (this: IDBRequest, ev: Event) => any; - successHandlers: Array<(this: IDBRequest, ev: Event) => any>; + successHandlers: Array<(this: IDBRequest, ev: Event) => any> = []; done: boolean = false; + _result: any; constructor(public _transaction: Transaction, public runner: () => void) { } @@ -113,7 +188,7 @@ class MyRequest implements IDBRequest { } get result(): any { - return null; + return this._result; } get source() { @@ -188,17 +263,78 @@ class OpenDBRequest extends MyRequest implements IDBOpenDBRequest { } } +function follow(x: any, s: string, replacement?: any): any { + if (s === "") { + return x; + } + const ptIdx = s.indexOf("."); + if (ptIdx < 0) { + const v = x[s]; + if (replacement !== undefined) { + x[s] = replacement; + } + return v; + } else { + const identifier = s.substring(0, ptIdx); + const rest = s.substring(ptIdx + 1); + return follow(x[identifier], rest, replacement); + } +} + +export function evaluateKeyPath(x: any, path: string | string[], replacement?: any): any { + if (typeof path === "string") { + return follow(x, path, replacement); + } else if (Array.isArray(path)) { + const res: any[] = []; + for (let s of path) { + let c = follow(x, s, replacement); + if (c === undefined) { + return undefined; + } + res.push(c); + } + return res; + } else { + throw Error("invalid key path, must be string or array of strings"); + } +} + +function stringifyKey(key: any) { + return JSON.stringify(key); +} + +export function isValidKey(key: any, memo: any[] = []) { + if (typeof key === "string" || typeof key === "number" || key instanceof Date) { + return true; + } + if (Array.isArray(key)) { + for (const element of key) { + if (!isValidKey(element, memo.concat([key]))) { + return false; + } + } + return true; + } + return false; +} class MyObjectStore implements IDBObjectStore { + + _keyPath: string | string[] | undefined; + _autoIncrement: boolean; + get indexNames() { return new DOMStringList(); } - constructor(public transaction: Transaction, public dbName: string, public storeName: string) { + constructor(public transaction: Transaction, public storeName: string) { + this._keyPath = this.transaction.transactionDbData.stores[this.storeName].keyPath as (string | string[]); + this._autoIncrement = this.transaction.transactionDbData.stores[this.storeName].autoIncrement; } - get keyPath() { - return this.transaction.db.dbData.stores[this.storeName].keyPath; + get keyPath(): string | string[] { + // TypeScript definitions are wrong here and don't permit a null keyPath + return this._keyPath as (string | string[]); } get name() { @@ -206,15 +342,77 @@ class MyObjectStore implements IDBObjectStore { } get autoIncrement() { - return this.transaction.db.dbData.stores[this.storeName].autoIncrement; + return this._autoIncrement; } - add(value: any, key?: any): IDBRequest { - throw Error("not implemented"); + storeImpl(originalValue: any, key: any|undefined, allowExisting: boolean) { + if (this.transaction.mode === "readonly") { + throw Error(); + } + if (!this.transaction.active) { + throw Error(); + } + if (!this.transaction.transactionDbData.stores.hasOwnProperty(this.storeName)) { + throw Error("object store was deleted"); + } + + const store = this.transaction.transactionDbData.stores[this.storeName]; + + const value = structuredClone(originalValue); + + if (this.keyPath) { + // we're dealine with in-line keys + if (key) { + throw Error("keys not allowed with in-line keys"); + } + key = evaluateKeyPath(value, this.keyPath); + if (!key && !this.autoIncrement) { + throw Error("key path must evaluate to key for in-line stores without autoIncrement"); + } + if (this.autoIncrement) { + if (key && typeof key === "number") { + store.keyGenerator = key + 1; + } else { + key = store.keyGenerator; + store.keyGenerator += 1; + evaluateKeyPath(value, this.keyPath, key); + } + } + } else { + // we're dealing with out-of-line keys + if (!key && !this.autoIncrement) { + throw Error("key must be provided for out-of-line stores without autoIncrement"); + } + key = this.transaction.transactionDbData.stores + if (this.autoIncrement) { + if (key && typeof key === "number") { + store.keyGenerator = key + 1; + } else { + key = store.keyGenerator; + store.keyGenerator += 1; + } + } + } + + const stringKey = stringifyKey(key); + + if (store.objects.hasOwnProperty(stringKey) && !allowExisting) { + throw Error("key already exists"); + } + + store.objects[stringKey] = value; + + const req = new MyRequest(this.transaction, () => { + }); + return req; } put(value: any, key?: any): IDBRequest { - throw Error("not implemented"); + return this.storeImpl(value, key, true); + } + + add(value: any, key?: any): IDBRequest { + return this.storeImpl(value, key, false); } delete(key: any): IDBRequest { @@ -242,7 +440,7 @@ class MyObjectStore implements IDBObjectStore { } index(indexName: string): IDBIndex { - throw Error("not implemented"); + return new IndexHandle(this, indexName); } openCursor(range?: IDBKeyRange | IDBValidKey, direction?: IDBCursorDirection): IDBRequest { @@ -257,19 +455,31 @@ class Db implements IDBDatabase { onerror: (this: IDBDatabase, ev: Event) => any; onversionchange: (ev: IDBVersionChangeEvent) => any; + _storeNames: string[] = []; + constructor(private _name: string, private _version: number, private factory: MemoryIDBFactory) { + for (let storeName in this.dbData.stores) { + if (this.dbData.stores.hasOwnProperty(storeName)) { + this._storeNames.push(storeName); + } + } + this._storeNames.sort(); } - get dbData() { + get dbData(): Database { return this.factory.data[this._name]; } + set dbData(data) { + this.factory.data[this._name] = data; + } + get name() { return this._name; } get objectStoreNames() { - return new MyDomStringList(); + return new MyDomStringList(...this._storeNames); } get version() { @@ -284,11 +494,44 @@ class Db implements IDBDatabase { if (tx.mode !== "versionchange") { throw Error("invalid mode"); } - throw Error("not implemented"); + + const td = tx.transactionDbData; + if (td.stores[name]) { + throw Error("object store already exists"); + } + + td.stores[name] = { + autoIncrement: !!(optionalParameters && optionalParameters.autoIncrement), + indices: {}, + keyGenerator: 1, + name, + objects: [], + }; + + this._storeNames.push(name); + this._storeNames.sort(); + + return new MyObjectStore(tx, name); } deleteObjectStore(name: string): void { - throw Error("not implemented"); + let tx = this.factory.getTransaction(); + if (tx.mode !== "versionchange") { + throw Error("invalid mode"); + } + + const td = tx.transactionDbData; + if (td.stores[name]) { + throw Error("object store does not exists"); + } + + const idx = this._storeNames.indexOf(name); + if (idx < 0) { + throw Error(); + } + this._storeNames.splice(idx, 1); + + delete td.stores[name]; } transaction(storeNames: string | string[], mode: IDBTransactionMode = "readonly"): IDBTransaction { @@ -342,11 +585,30 @@ class Transaction implements IDBTransaction { return this._mode; } + get active(): boolean { + return this.state === TransactionState.Running || this.state === TransactionState.Created; + } + start() { if (this.state != TransactionState.Created) { throw Error(); } + this.state = TransactionState.Running; this._transactionDbData = structuredClone(this.dbHandle.dbData); + if (!this._transactionDbData) { + throw Error(); + } + } + + commit() { + if (this.state != TransactionState.Running) { + throw Error(); + } + if (!this._transactionDbData) { + throw Error(); + } + this.state = TransactionState.Commited; + this.dbHandle.dbData = this._transactionDbData; } get error(): DOMException { @@ -373,7 +635,7 @@ class Transaction implements IDBTransaction { } objectStore(storeName: string): IDBObjectStore { - return new MyObjectStore(this, this.dbName, storeName); + return new MyObjectStore(this, storeName); } dispatchEvent(evt: Event): boolean { @@ -425,8 +687,9 @@ class MyEvent implements Event { _timeStamp: number = 0; _type: string; - constructor(typeArg: string) { + constructor(typeArg: string, target: any) { this._type = typeArg; + this._target = target; } get eventPhase() { @@ -519,10 +782,10 @@ class MyEvent implements Event { class VersionChangeEvent extends MyEvent { _newVersion: number|null; _oldVersion: number; - constructor(oldVersion: number, newVersion?: number) { - super("VersionChange"); + constructor(oldVersion: number, newVersion: number|null, target: any) { + super("VersionChange", target); this._oldVersion = oldVersion; - this._newVersion = newVersion || null; + this._newVersion = newVersion; } get newVersion() { @@ -535,7 +798,7 @@ class VersionChangeEvent extends MyEvent { } -class MemoryIDBFactory implements IDBFactory { +export class MemoryIDBFactory implements IDBFactory { data: Databases = {}; currentRequest: MyRequest|undefined; @@ -570,7 +833,7 @@ class MemoryIDBFactory implements IDBFactory { // auto-commit the transaction that the // previous request worked on. let lastTx = prevRequest._transaction; - this.data[lastTx.dbName] = lastTx.transactionDbData; + lastTx.commit(); } }; alreadyResolved.then(() => { @@ -604,13 +867,19 @@ class MemoryIDBFactory implements IDBFactory { } let upgradeNeeded = false; + let oldVersion: number; let mydb: Database; if (dbName in this.data) { mydb = this.data[dbName]; + if (!mydb) { + throw Error(); + } + oldVersion = mydb.version; if (version === undefined || version == mydb.version) { // we can open without upgrading } else if (version > mydb.version) { upgradeNeeded = true; + mydb.version = version; } else { throw Error("version error"); } @@ -621,17 +890,21 @@ class MemoryIDBFactory implements IDBFactory { version: (version || 1), }; upgradeNeeded = true; + oldVersion = 0; } + this.data[dbName] = mydb; + const db = new Db(dbName, mydb.version, this); const tx = new Transaction(dbName, db, "versionchange"); const req = new OpenDBRequest(tx, () => { + req._result = db; if (upgradeNeeded) { - let versionChangeEvt = new VersionChangeEvent(mydb.version, version); + let versionChangeEvt = new VersionChangeEvent(oldVersion, mydb.version, db); req.callOnupgradeneeded(versionChangeEvt); } - let successEvent = new MyEvent("success"); + let successEvent = new MyEvent("success", db); req.callSuccess(successEvent); }); @@ -640,3 +913,10 @@ class MemoryIDBFactory implements IDBFactory { return req; } } + +/** + * Inject our IndexedDb implementation in the global namespace, + * potentially replacing an existing implementation. + */ +export function injectGlobals() { +} diff --git a/tsconfig.json b/tsconfig.json index 1f649ef19..dee558cf1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -40,6 +40,7 @@ "src/i18n.tsx", "src/i18n/strings.ts", "src/logging.ts", + "src/memidb-test.ts", "src/memidb.ts", "src/query.ts", "src/timer.ts", @@ -4610,6 +4610,10 @@ strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" +structured-clone@^0.2.2: + version "0.2.2" + resolved "https://registry.yarnpkg.com/structured-clone/-/structured-clone-0.2.2.tgz#ac92b6be31958a643db30f1335abc6a1b02dfdc2" + supports-color@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-0.2.0.tgz#d92de2694eb3f67323973d7ae3d8b55b4c22190a" |