From 59064165dba09f95d5270bec6eb6e76e73c6b453 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Mon, 26 Jun 2023 22:47:13 +0200 Subject: [PATCH] Started adding test suite. Started adding code for allowing users to update and delete existing files (#22) and started refactoring to allow for multiple range headers (#16) --- .gitignore | 17 +- .vscode/settings.json | 11 +- Dockerfile | 2 +- README.md | 34 +- chompfile.toml | 19 +- package-lock.json | 485 +++++++++++++++++++-- package.json | 2 + src/lib/range-fns.js | 19 + src/server/index.js | 14 +- src/server/routes/{404.js => 404/index.js} | 0 src/server/routes/delete/index.js | 7 + src/server/routes/get/file/index.js | 60 +-- src/server/routes/get/index.js | 2 +- src/server/routes/head/index.js | 30 +- src/server/routes/index.js | 7 + src/server/routes/post/index.js | 119 +++++ src/server/routes/put/index.js | 117 +---- {test => test-mnts}/README.md | 0 {test => test-mnts}/mnt1/README.md | 0 {test => test-mnts}/mnt2/README.md | 0 test/test.js | 9 + 21 files changed, 715 insertions(+), 239 deletions(-) create mode 100644 src/lib/range-fns.js rename src/server/routes/{404.js => 404/index.js} (100%) create mode 100644 src/server/routes/delete/index.js create mode 100644 src/server/routes/index.js create mode 100644 src/server/routes/post/index.js rename {test => test-mnts}/README.md (100%) rename {test => test-mnts}/mnt1/README.md (100%) rename {test => test-mnts}/mnt2/README.md (100%) create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore index f3d4a12..e286554 100644 --- a/.gitignore +++ b/.gitignore @@ -7,14 +7,15 @@ yarn-error.log* lerna-debug.log* # Local dev environment -/test/* -!/test/ -/test/mnt1/* -!/test/mnt1/ -!/test/mnt1/README.md -/test/mnt2/* -!/test/mnt2/ -!/test/mnt2/README.md +/test-mnts/* +!/test-mnts/ +!/test-mnts/README.md +/test-mnts/mnt1/* +!/test-mnts/mnt1/ +!/test-mnts/mnt1/README.md +/test-mnts/mnt2/* +!/test-mnts/mnt2/ +!/test-mnts/mnt2/README.md # Diagnostic reports (https://nodejs.org/api/report.html) report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json diff --git a/.vscode/settings.json b/.vscode/settings.json index a5d7ae8..fc846b4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,12 @@ { - "cSpell.words": ["Dockerized", "geotiff", "mbuffer", "mnemosyne", "noindex", "somisana", "Zarr"] + "cSpell.words": [ + "Dockerized", + "geotiff", + "keepalive", + "mbuffer", + "mnemosyne", + "noindex", + "somisana", + "Zarr" + ] } diff --git a/Dockerfile b/Dockerfile index 5706682..1b9ee8d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20.3.0-alpine +FROM node:20.3.1-alpine ARG TC=UTC ARG NODE_ENV=production diff --git a/README.md b/README.md index 36a9680..21f9447 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ Tools that understand cloud-optimized formats (i.e. COGs, Zarrs, etc.) should wo curl \ -X GET \ --keepalive-time 1200 \ - https:///filename.tif + http://localhost:3000/filename.tif ``` **_Download partial file via cURL_** @@ -67,7 +67,7 @@ curl \ curl \ -H "Range bytes=12-20" \ -X GET \ - https:///filename.tif + http://localhost:3000/filename.tif ``` ### Customize GET requests via URL params @@ -93,7 +93,7 @@ Any folder that includes an `index.html` file will be served as a website. One p ```js // Client side JavaScript -fetch('https:///directory?json') +fetch('http://localhost:3000/directory?json') .then(res => res.json()) .then(json => { document.getElementsByTagName('body')[0].append(JSON.stringify(json)) @@ -108,7 +108,7 @@ Any files/folder in the exposed volume will be served. To upload files to the se ### cURL examples -Here are some examples using `cURL` (and some notes): +Here are some examples using `cURL` (and some notes) using the default authentication token on localhost. (Look at server logs for authentication tokens): - For `PUT` requests I find it's necessary to pipe the output to `cat`, since `cURL` won't print output to stdout (who knows why) when using `POST` and `PUT` requests - The `-T` header means 'transfer file'. Use this instead of `--data-binary`, since the latter will try to load your entire file into memory before sending @@ -126,10 +126,10 @@ cat ./some/local/cog.tiff \ --progress-bar \ --keepalive-time 1200 \ -X PUT \ - -H "Authorization: Bearer " \ + -H "Authorization: Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822" \ -H "Content-Type: application/octet-stream" \ --data-binary @- \ - https:///some/deep/nested/directory/cog.tif \ + http://localhost:3000/some/deep/nested/directory/cog.tif \ | cat ``` @@ -142,14 +142,14 @@ curl \ --progress-bar \ --keepalive-time 1200 \ -X PUT \ - -H "Authorization: Bearer " \ + -H "Authorization: Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822" \ -T \ ./some/local/cog.tiff \ - https:///some/deep/nested/directory/cog.tif \ + http://localhost:3000/some/deep/nested/directory/cog.tif \ | cat ``` -And then that file can be retrieved at `https:///some/deep/nested/directory/cog.tif`. +And then that file can be retrieved at `http://localhost:3000/some/deep/nested/directory/cog.tif`. #### Chunked uploads @@ -163,11 +163,11 @@ cat ./some/local/cog.tiff \ --progress-bar \ --keepalive-time 1200 \ -X PUT \ - -H "Authorization: Bearer " \ + -H "Authorization: Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822" \ -H "Content-Type: application/octet-stream" \ -T \ - \ - https:///some/deep/nested/directory/cog.tif \ + http://localhost:3000/some/deep/nested/directory/cog.tif \ | cat ``` @@ -184,9 +184,9 @@ mbuffer \ --keepalive-time 1200 \ -X PUT \ -H "Content-Type: application/octet-stream" \ - -H "Authorization: Bearer " \ + -H "Authorization: Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822" \ --data-binary @- \ - https:///some/deep/nested/directory/cog.tif \ + http://localhost:3000/some/deep/nested/directory/cog.tif \ | cat ``` @@ -203,10 +203,10 @@ find \ --progress-bar \ --keepalive-time 1200 \ -X PUT \ - -H "Authorization: Bearer " \ + -H "Authorization: Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822" \ --create-dirs \ -T {} \ - https:///some/nested/directory/{} \; \ + http://localhost:3000/some/nested/directory/{} \; \ | cat ``` @@ -217,10 +217,10 @@ The equivalent to the `cURL` utility on Windows Platform is the `Invoke-RestMeth cd /to/the/directory/with/your/file $FILENAME = "some-file.tiff" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" -$headers.Add("Authorization", "Bearer ") +$headers.Add("Authorization", "Bearer f7efafd138da71cdcb0aa4767da9b6ee:1cc83802e7a4a831b17efa9ffecf4822") Invoke-RestMethod ` - -Uri "https:///some/nested/directory/$FILENAME" ` + -Uri "http://localhost:3000/some/nested/directory/$FILENAME" ` -Method Put ` -InFile "./$FILENAME" ` -Headers $headers ` diff --git a/chompfile.toml b/chompfile.toml index a0323c6..82b0bdd 100644 --- a/chompfile.toml +++ b/chompfile.toml @@ -5,25 +5,30 @@ extensions = ['chomp@0.1:prettier'] [[task]] name = 'start' -env = { TZ='UTC', NODE_ENV='development'} -deps = [ 'src/**/*.js' ] +env = { TZ = 'UTC', NODE_ENV = 'development' } +deps = ["test"] run = """ node \ --trace-warnings \ bin/mnemosyne.js \ --key TjWnZr4u7w!z%C*F-JaNdRgUkXp2s5v8 \ + --login local \ --login username \ --login username2 \ - --volume ./test \ - --volume ./test/mnt1 \ - --volume ./test/mnt2 + --volume ./test-mnts \ + --volume ./test-mnts/mnt1 \ + --volume ./test-mnts/mnt2 """ +[[task]] +name = 'test' +deps = ['src/**/*.js', "test/**/*.js"] +run = "mocha" + [[task]] name = 'lint' run = "eslint ." - [[task]] name = 'prettier' template = 'prettier' @@ -44,4 +49,4 @@ find . \ -name package-lock.json \ -type f \ -delete -""" \ No newline at end of file +""" diff --git a/package-lock.json b/package-lock.json index 5640696..54e17f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "he": "^1.2.0", "mime": "^3.0.0", "mkdirp": "^3.0.1", + "mocha": "^10.2.0", "yargs": "^17.7.2" }, "bin": { @@ -211,6 +212,14 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/ansi-colors": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", + "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", + "engines": { + "node": ">=6" + } + }, "node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", @@ -233,28 +242,61 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", - "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==", - "dev": true + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", - "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true + "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" + }, + "node_modules/binary-extensions": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", + "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", + "engines": { + "node": ">=8" + } }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "dependencies": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, + "node_modules/braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dependencies": { + "fill-range": "^7.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/browser-stdout": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==" + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -264,11 +306,21 @@ "node": ">=6" } }, + "node_modules/camelcase": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/chalk": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dev": true, "dependencies": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -280,6 +332,43 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/chokidar": { + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", + "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "funding": [ + { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + ], + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/chokidar/node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/cliui": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", @@ -312,8 +401,7 @@ "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", - "dev": true + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "node_modules/cross-spawn": { "version": "7.0.3", @@ -333,7 +421,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -346,12 +433,31 @@ } } }, + "node_modules/decamelize": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/deep-is": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, + "node_modules/diff": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz", + "integrity": "sha512-/VTCrvm5Z0JGty/BWHljh+BAiw3IK+2j87NGMu8Nwc/f48WoDAC395uomO9ZD117ZOBaHmkX1oyLvkVM/aIT3w==", + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -381,7 +487,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", - "dev": true, "engines": { "node": ">=10" }, @@ -610,11 +715,21 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/find-up": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", - "dev": true, "dependencies": { "locate-path": "^6.0.0", "path-exists": "^4.0.0" @@ -626,6 +741,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/flat": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/flat/-/flat-5.0.2.tgz", + "integrity": "sha512-b6suED+5/3rTpUBdG1gupIl8MPFCAMA0QXwmljLhvCUKcUvdE4gWky9zpuGCcXHOsz4J9wPGNWq6OKpmIzz3hQ==", + "bin": { + "flat": "cli.js" + } + }, "node_modules/flat-cache": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-3.0.4.tgz", @@ -648,8 +771,20 @@ "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", - "dev": true + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" + }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } }, "node_modules/get-caller-file": { "version": "2.0.5", @@ -716,7 +851,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true, "engines": { "node": ">=8" } @@ -767,7 +901,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", - "dev": true, "dependencies": { "once": "^1.3.0", "wrappy": "1" @@ -776,14 +909,23 @@ "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -800,7 +942,6 @@ "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dev": true, "dependencies": { "is-extglob": "^2.1.1" }, @@ -808,6 +949,14 @@ "node": ">=0.10.0" } }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "engines": { + "node": ">=0.12.0" + } + }, "node_modules/is-path-inside": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz", @@ -817,6 +966,25 @@ "node": ">=8" } }, + "node_modules/is-plain-obj": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", + "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", + "engines": { + "node": ">=8" + } + }, + "node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", @@ -827,7 +995,6 @@ "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", - "dev": true, "dependencies": { "argparse": "^2.0.1" }, @@ -864,7 +1031,6 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", - "dev": true, "dependencies": { "p-locate": "^5.0.0" }, @@ -881,6 +1047,21 @@ "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/mime": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/mime/-/mime-3.0.0.tgz", @@ -918,11 +1099,163 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/mocha": { + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-10.2.0.tgz", + "integrity": "sha512-IDY7fl/BecMwFHzoqF2sg/SHHANeBoMMXFlS9r0OXKDssYE1M5O43wUY/9BVPeIvfH2zmEbBfseqN9gBQZzXkg==", + "dependencies": { + "ansi-colors": "4.1.1", + "browser-stdout": "1.3.1", + "chokidar": "3.5.3", + "debug": "4.3.4", + "diff": "5.0.0", + "escape-string-regexp": "4.0.0", + "find-up": "5.0.0", + "glob": "7.2.0", + "he": "1.2.0", + "js-yaml": "4.1.0", + "log-symbols": "4.1.0", + "minimatch": "5.0.1", + "ms": "2.1.3", + "nanoid": "3.3.3", + "serialize-javascript": "6.0.0", + "strip-json-comments": "3.1.1", + "supports-color": "8.1.1", + "workerpool": "6.2.1", + "yargs": "16.2.0", + "yargs-parser": "20.2.4", + "yargs-unparser": "2.0.0" + }, + "bin": { + "_mocha": "bin/_mocha", + "mocha": "bin/mocha.js" + }, + "engines": { + "node": ">= 14.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/mochajs" + } + }, + "node_modules/mocha/node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/mocha/node_modules/glob": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/mocha/node_modules/glob/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, + "node_modules/mocha/node_modules/minimatch": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.0.1.tgz", + "integrity": "sha512-nLDxIFRyhDblz3qMuq+SoRZED4+miJ/G+tdDrjkkkRnjAsBexeGpgjLEQ0blJy7rHhR2b93rhQY4SvyWu9v03g==", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/mocha/node_modules/minimatch/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/mocha/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" + }, + "node_modules/mocha/node_modules/supports-color": { + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/mocha/node_modules/yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/mocha/node_modules/yargs-parser": { + "version": "20.2.4", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.4.tgz", + "integrity": "sha512-WOkpgNhPTlE73h4VFAFsOnomJVaovO8VqLDzy5saChRBFQFBoMYirowyW+Q9HB4HFF4Z7VZTiG3iSzJJA29yRA==", + "engines": { + "node": ">=10" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + }, + "node_modules/nanoid": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.3.tgz", + "integrity": "sha512-p1sjXuopFs0xg+fPASzQ28agW1oHD7xDsd9Xkf3T15H3c/cifrFHVwrh74PdoklAPi+i7MdRsE47vm2r6JoB+w==", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } }, "node_modules/natural-compare": { "version": "1.4.0", @@ -930,11 +1263,18 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "dev": true, "dependencies": { "wrappy": "1" } @@ -960,7 +1300,6 @@ "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", - "dev": true, "dependencies": { "yocto-queue": "^0.1.0" }, @@ -975,7 +1314,6 @@ "version": "5.0.0", "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", - "dev": true, "dependencies": { "p-limit": "^3.0.2" }, @@ -1002,7 +1340,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", - "dev": true, "engines": { "node": ">=8" } @@ -1011,7 +1348,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -1025,6 +1361,17 @@ "node": ">=8" } }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", @@ -1090,6 +1437,25 @@ } ] }, + "node_modules/randombytes": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", + "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", + "dependencies": { + "safe-buffer": "^5.1.0" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -1155,6 +1521,33 @@ "queue-microtask": "^1.2.2" } }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/serialize-javascript": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.0.tgz", + "integrity": "sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==", + "dependencies": { + "randombytes": "^2.1.0" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -1204,7 +1597,6 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", - "dev": true, "engines": { "node": ">=8" }, @@ -1216,7 +1608,6 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, "dependencies": { "has-flag": "^4.0.0" }, @@ -1230,6 +1621,17 @@ "integrity": "sha512-N+8UisAXDGk8PFXP4HAzVR9nbfmVJ3zYLAWiTIoqC5v5isinhr+r5uaO8+7r3BMfuNIufIsA7RdpVgacC2cSpw==", "dev": true }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", @@ -1287,6 +1689,11 @@ "node": ">=0.10.0" } }, + "node_modules/workerpool": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/workerpool/-/workerpool-6.2.1.tgz", + "integrity": "sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==" + }, "node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", @@ -1306,8 +1713,7 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "dev": true + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "node_modules/y18n": { "version": "5.0.8", @@ -1342,11 +1748,24 @@ "node": ">=12" } }, + "node_modules/yargs-unparser": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/yargs-unparser/-/yargs-unparser-2.0.0.tgz", + "integrity": "sha512-7pRTIA9Qc1caZ0bZ6RYRGbHJthJWuakf+WmHK0rVeLkNrrGhfoabBNdue6kdINI6r4if7ocq9aD/n7xwKOdzOA==", + "dependencies": { + "camelcase": "^6.0.0", + "decamelize": "^4.0.0", + "flat": "^5.0.2", + "is-plain-obj": "^2.1.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", - "dev": true, "engines": { "node": ">=10" }, diff --git a/package.json b/package.json index 0663ced..edef429 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "LICENSE" ], "scripts": { + "test": "mocha", "?": "Refer to ./chompfile.toml" }, "bin": { @@ -46,6 +47,7 @@ "he": "^1.2.0", "mime": "^3.0.0", "mkdirp": "^3.0.1", + "mocha": "^10.2.0", "yargs": "^17.7.2" }, "engines": { diff --git a/src/lib/range-fns.js b/src/lib/range-fns.js new file mode 100644 index 0000000..e5328b5 --- /dev/null +++ b/src/lib/range-fns.js @@ -0,0 +1,19 @@ +export const parseRangeHeader = function (rangeHeader, contentLength) { + const range = rangeHeader.replace(/bytes=/, '').split('-') + const start = parseInt(range[0], 10) + const end = range[1] ? parseInt(range[1], 10) : contentLength - 1 + + if (isNaN(start) && isNaN(end)) { + return { start: null, end: null } + } + + if (isNaN(start)) { + return { start: contentLength - end, end } + } + + if (isNaN(end)) { + return { start, end: contentLength - 1 } + } + + return [{ start, end }] +} diff --git a/src/server/index.js b/src/server/index.js index 3e1bd24..958c948 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -1,10 +1,6 @@ import { createServer } from 'http' import { info, error } from '../logger/index.js' -import options from './routes/options/index.js' -import head from './routes/head/index.js' -import get from './routes/get/index.js' -import put from './routes/put/index.js' -import _404 from './routes/404.js' +import { options, head, get, put, _404, post, httpDelete } from './routes/index.js' import parseResource from './middleware/parse-resource.js' import setResponseHeaders from './middleware/set-response-headers.js' import checkContinue from './middleware/check-continue.js' @@ -39,6 +35,14 @@ const server = createServer(async (req, res) => { await put.call(ctx, req, res) break + case 'POST': + await post.call(ctx, req, res) + break + + case 'DELETE': + await httpDelete.call(ctx, req, res) + break + default: await _404.call(ctx, req, res) break diff --git a/src/server/routes/404.js b/src/server/routes/404/index.js similarity index 100% rename from src/server/routes/404.js rename to src/server/routes/404/index.js diff --git a/src/server/routes/delete/index.js b/src/server/routes/delete/index.js new file mode 100644 index 0000000..827ac7b --- /dev/null +++ b/src/server/routes/delete/index.js @@ -0,0 +1,7 @@ +export default async function () { + const { req, res } = this + + res.statusCode = 501 + res.write('Not implemented yet') + res.end() +} diff --git a/src/server/routes/get/file/index.js b/src/server/routes/get/file/index.js index dfaa272..b5b2a6d 100644 --- a/src/server/routes/get/file/index.js +++ b/src/server/routes/get/file/index.js @@ -1,25 +1,6 @@ import { stat } from 'fs/promises' import streamFile from './_stream-file.js' - -export const parseRangeHeader = function (rangeHeader, contentLength) { - const range = rangeHeader.replace(/bytes=/, '').split('-') - const start = parseInt(range[0], 10) - const end = range[1] ? parseInt(range[1], 10) : contentLength - 1 - - if (isNaN(start) && isNaN(end)) { - return { start: null, end: null } - } - - if (isNaN(start)) { - return { start: contentLength - end, end } - } - - if (isNaN(end)) { - return { start, end: contentLength - 1 } - } - - return { start, end } -} +import { parseRangeHeader } from '../../../../lib/range-fns.js' export default async function serveFile() { const { @@ -34,25 +15,28 @@ export default async function serveFile() { const { range } = request.headers if (range) { - const { start, end } = parseRangeHeader(range, contentLength) - - if (start === null || end === null || start >= contentLength || end >= contentLength) { - response.writeHead(416, { 'Content-Range': `bytes */${contentLength}` }) - return response.end() + const ranges = parseRangeHeader(range, contentLength) + for (const { start, end } of ranges) { + // Invalid range + if (start === null || end === null || start >= contentLength || end >= contentLength) { + response.writeHead(416, { 'Content-Range': `bytes */${contentLength}` }) + return response.end() + } + + // Valid range + response.statusCode = 206 + response.setHeader('Content-Range', `bytes ${start}-${end}/${contentLength}`) + response.setHeader('Accept-Ranges', 'bytes') + await streamFile({ + size: contentLength, + contentLength: end - start + 1, + request, + response, + file, + start, + end, + }) } - - response.statusCode = 206 - response.setHeader('Content-Range', `bytes ${start}-${end}/${contentLength}`) - response.setHeader('Accept-Ranges', 'bytes') - await streamFile({ - size: contentLength, - contentLength: end - start + 1, - request, - response, - file, - start, - end, - }) } else { response.statusCode = 200 streamFile({ size: contentLength, contentLength, request, response, file }) diff --git a/src/server/routes/get/index.js b/src/server/routes/get/index.js index 444c260..87ab07c 100644 --- a/src/server/routes/get/index.js +++ b/src/server/routes/get/index.js @@ -1,5 +1,5 @@ import { warn } from '../../../logger/index.js' -import _404 from '../404.js' +import _404 from '../404/index.js' import serveFile from './file/index.js' import serveDir from './dir/index.js' diff --git a/src/server/routes/head/index.js b/src/server/routes/head/index.js index 3332922..765438c 100644 --- a/src/server/routes/head/index.js +++ b/src/server/routes/head/index.js @@ -1,6 +1,6 @@ import { stat } from 'fs/promises' import mime from 'mime' -import { parseRangeHeader } from '../get/file/index.js' +import { parseRangeHeader } from '../../../lib/range-fns.js' export default async function () { const { @@ -34,20 +34,22 @@ export default async function () { } if (range) { - const { start, end } = parseRangeHeader(range, contentLength) - - if (start === null || end === null || start >= contentLength || end >= contentLength) { - response.writeHead(416, { 'Content-Range': `bytes */${contentLength}` }) - return response.end() + const ranges = parseRangeHeader(range, contentLength) + for (const { start, end } of ranges) { + // Invalid range + if (start === null || end === null || start >= contentLength || end >= contentLength) { + response.writeHead(416, { 'Content-Range': `bytes */${contentLength}` }) + return response.end() + } + + // Valid range + response.writeHead(206, { + 'Content-Range': `bytes ${start}-${end}/${contentLength}`, + 'Accept-Ranges': 'bytes', + 'Content-Length': end - start + 1, + 'Content-Type': contentType, + }) } - - // Otherwise serve range-accepting response - response.writeHead(206, { - 'Content-Range': `bytes ${start}-${end}/${contentLength}`, - 'Accept-Ranges': 'bytes', - 'Content-Length': end - start + 1, - 'Content-Type': contentType, - }) } else { response.writeHead(200, { 'Content-Length': contentLength, diff --git a/src/server/routes/index.js b/src/server/routes/index.js new file mode 100644 index 0000000..e2ab9a6 --- /dev/null +++ b/src/server/routes/index.js @@ -0,0 +1,7 @@ +export { default as httpDelete } from './delete/index.js' +export { default as _404 } from './404/index.js' +export { default as get } from './get/index.js' +export { default as head } from './head/index.js' +export { default as options } from './options/index.js' +export { default as put } from './put/index.js' +export { default as post } from './post/index.js' diff --git a/src/server/routes/post/index.js b/src/server/routes/post/index.js new file mode 100644 index 0000000..41f5026 --- /dev/null +++ b/src/server/routes/post/index.js @@ -0,0 +1,119 @@ +import { KEY } from '../../../config/index.js' +import { createWriteStream } from 'fs' +import { access, unlink } from 'fs/promises' +import { error } from '../../../logger/index.js' +import { mkdirp } from 'mkdirp' +import { dirname } from 'path' +import authenticate from '../../../lib/authenticate.js' + +export async function createResource() { + const { + req, + res, + resource: { + _paths, + url: { href }, + }, + } = this + + if (_paths.length !== 1) { + const msg = + 'Conflict. Ambiguous upload path specified targeting multiple possible volumes. Please specify an existing root directory.' + res.writeHead(409, msg, { 'Content-Type': 'text/plain' }) + res.write(msg) + res.end() + return + } + + // Ensure that uploads are enabled for this server + if (!KEY) { + res.writeHead(405, { 'Content-Type': 'text/plain' }) + res.write('PUT has been disabled for this server') + res.end() + return + } + + // Ensure that a valid token is used + try { + authenticate(req) + } catch (e) { + error(e) + res.statusCode = 401 + res.write('Unauthorized') + res.end() + return + } + + const { path } = _paths[0] + + // Check if the resource exists + let exists + try { + await access(path) + exists = true + } catch { + exists = false + } + + // If it exists return 409 + if (exists) { + const msg = 'Conflict. Upload path already exists' + res.writeHead(409, msg, { 'Content-Type': 'text/plain' }) + res.write(msg) + res.end() + return + } + + // Get upload path + const dir = dirname(path) + + // Ensure dir exists + await mkdirp(dir) + + // Stream file contents to disk + const stream = createWriteStream(path) + + // Delete failed uploads + stream.on('error', async err => { + await unlink(path) + error(err) + res.writeHead(500, { 'Content-Type': 'text/plain' }) + res.write('Internal Server Error') + res.end() + }) + + // Keep track of how much is received + let received = 0 + req.on('data', chunk => { + received += chunk.length + }) + + req.pipe(stream) + + // Handle aborted requests + req.on('aborted', async () => { + error('Connection terminated by client') + await unlink(path) + res.writeHead(400, { 'Content-Type': 'text/plain' }) + res.write('Bad Request') + res.end() + }) + + await new Promise(resolve => { + stream.on('close', async () => { + // Respond with new resource info + const msg = `Received ${received} bytes` + res.writeHead(201, { + 'Content-Type': 'text/plain', + 'Content-Location': href, + }) + res.write(msg) + res.end() + resolve() + }) + }) +} + +export default async function () { + return createResource.call(this) +} diff --git a/src/server/routes/put/index.js b/src/server/routes/put/index.js index 972704a..afcb90f 100644 --- a/src/server/routes/put/index.js +++ b/src/server/routes/put/index.js @@ -1,116 +1,5 @@ -import { KEY } from '../../../config/index.js' -import { createWriteStream, promises as fsPromises } from 'fs' -import { error } from '../../../logger/index.js' -import { mkdirp } from 'mkdirp' -import { dirname } from 'path' -import authenticate from '../../../lib/authenticate.js' +import { createResource } from '../post/index.js' -const { access, unlink } = fsPromises - -export default async function handleUploadRequest() { - const { - req, - res, - resource: { - _paths, - url: { href }, - }, - } = this - - if (_paths.length !== 1) { - const msg = - 'Conflict. Ambiguous upload path specified targeting multiple possible volumes. Please specify an existing root directory.' - res.writeHead(409, msg, { 'Content-Type': 'text/plain' }) - res.write(msg) - res.end() - return - } - - // Ensure that uploads are enabled for this server - if (!KEY) { - res.writeHead(405, { 'Content-Type': 'text/plain' }) - res.write('PUT has been disabled for this server') - res.end() - return - } - - // Ensure that a valid token is used - try { - authenticate(req) - } catch (e) { - error(e) - res.statusCode = 401 - res.write('Unauthorized') - res.end() - return - } - - const { path } = _paths[0] - - // Check if the resource exists - let exists - try { - await access(path) - exists = true - } catch { - exists = false - } - - // If it exists return 409 - if (exists) { - const msg = 'Conflict. Upload path already exists' - res.writeHead(409, msg, { 'Content-Type': 'text/plain' }) - res.write(msg) - res.end() - return - } - - // Get upload path - const dir = dirname(path) - - // Ensure dir exists - await mkdirp(dir) - - // Stream file contents to disk - const stream = createWriteStream(path) - - // Delete failed uploads - stream.on('error', async err => { - await unlink(path) - error(err) - res.writeHead(500, { 'Content-Type': 'text/plain' }) - res.write('Internal Server Error') - res.end() - }) - - // Keep track of how much is received - let received = 0 - req.on('data', chunk => { - received += chunk.length - }) - - req.pipe(stream) - - // Handle aborted requests - req.on('aborted', async () => { - error('Connection terminated by client') - await unlink(path) - res.writeHead(400, { 'Content-Type': 'text/plain' }) - res.write('Bad Request') - res.end() - }) - - await new Promise(resolve => { - stream.on('close', async () => { - // Respond with new resource info - const msg = `Received ${received} bytes` - res.writeHead(201, { - 'Content-Type': 'text/plain', - 'Content-Location': href, - }) - res.write(msg) - res.end() - resolve() - }) - }) +export default async function () { + return createResource.call(this) } diff --git a/test/README.md b/test-mnts/README.md similarity index 100% rename from test/README.md rename to test-mnts/README.md diff --git a/test/mnt1/README.md b/test-mnts/mnt1/README.md similarity index 100% rename from test/mnt1/README.md rename to test-mnts/mnt1/README.md diff --git a/test/mnt2/README.md b/test-mnts/mnt2/README.md similarity index 100% rename from test/mnt2/README.md rename to test-mnts/mnt2/README.md diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..ce402e2 --- /dev/null +++ b/test/test.js @@ -0,0 +1,9 @@ +import assert from 'assert' + +describe('Array', function () { + describe('#indexOf()', function () { + it('should return -1 when the value is not present', function () { + assert.equal([1, 2, 3].indexOf(4), -1) + }) + }) +})