untrack unnecessary files
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
.DS_Store
|
||||||
|
node_modules/
|
||||||
|
data/
|
||||||
|
.vscode
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
timestamp,grade,case_1_likert,case_2_likert,case_3_likert,case_4_likert,case_5_likert,case_6_likert,case_7_likert,case_8_likert,case_9_likert,case_10_likert,case_11_likert,case_12_likert,case_13_likert,case_14_likert,case_15_likert,case_1_order,case_2_order,case_3_order,case_4_order,case_5_order,case_6_order,case_7_order,case_8_order,case_9_order,case_10_order,case_11_order,case_12_order,case_13_order,case_14_order,case_15_order,case_1_ms,case_2_ms,case_3_ms,case_4_ms,case_5_ms,case_6_ms,case_7_ms,case_8_ms,case_9_ms,case_10_ms,case_11_ms,case_12_ms,case_13_ms,case_14_ms,case_15_ms,ahp_w_stability,ahp_w_revenue,ahp_w_capital,ahp_w_plan,ahp_w_social,ahp_lambda_max,ahp_CI,ahp_CR,ahp_cr_warning,match_case_1,match_case_2,match_case_3,match_case_4,match_case_5,match_case_6,match_case_7,match_case_8,match_case_9,match_case_10,match_case_11,match_case_12,match_case_13,match_case_14,match_case_15,post_q1,post_q2,post_q3,post_q4,open_response
|
|
||||||
1778506748375,3학년,5,5,5,5,5,5,4,5,2,5,5,5,5,5,5,4,12,14,8,3,10,2,11,1,15,9,5,6,13,7,781,699,717,1448,1449,767,1533,832,2814,1315,848,818,681,665,733,0.2770,0.1510,0.1906,0.1906,0.1906,5.2453,0.0613,0.0495,0,1,0,1,0,1,1,1,1,0,1,0,1,0,1,0,5,5,5,5,
|
|
||||||
|
@@ -1,285 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"meta": {
|
|
||||||
"timestamp": 1778506748375,
|
|
||||||
"grade": "3학년"
|
|
||||||
},
|
|
||||||
"intuitive_judgments": [
|
|
||||||
{
|
|
||||||
"case_id": 9,
|
|
||||||
"presented_order": 1,
|
|
||||||
"judgment": 2,
|
|
||||||
"response_time_ms": 2814
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 7,
|
|
||||||
"presented_order": 2,
|
|
||||||
"judgment": 4,
|
|
||||||
"response_time_ms": 1533
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 5,
|
|
||||||
"presented_order": 3,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 1449
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 1,
|
|
||||||
"presented_order": 4,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 781
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 12,
|
|
||||||
"presented_order": 5,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 818
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 13,
|
|
||||||
"presented_order": 6,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 681
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 15,
|
|
||||||
"presented_order": 7,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 733
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 4,
|
|
||||||
"presented_order": 8,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 1448
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 11,
|
|
||||||
"presented_order": 9,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 848
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 6,
|
|
||||||
"presented_order": 10,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 767
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 8,
|
|
||||||
"presented_order": 11,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 832
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 2,
|
|
||||||
"presented_order": 12,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 699
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 14,
|
|
||||||
"presented_order": 13,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 665
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 3,
|
|
||||||
"presented_order": 14,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 717
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 10,
|
|
||||||
"presented_order": 15,
|
|
||||||
"judgment": 5,
|
|
||||||
"response_time_ms": 1315
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"ahp": {
|
|
||||||
"pairwise_raw": [
|
|
||||||
4,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
1
|
|
||||||
],
|
|
||||||
"weights": [
|
|
||||||
0.27702821819618745,
|
|
||||||
0.15103495945577752,
|
|
||||||
0.190645607449345,
|
|
||||||
0.190645607449345,
|
|
||||||
0.190645607449345
|
|
||||||
],
|
|
||||||
"lambda_max": 5.245333922288061,
|
|
||||||
"CI": 0.06133348057201515,
|
|
||||||
"CR": 0.04946248433227029,
|
|
||||||
"cr_warning_shown": false
|
|
||||||
},
|
|
||||||
"comparison": [
|
|
||||||
{
|
|
||||||
"case_id": 9,
|
|
||||||
"team": "A",
|
|
||||||
"type": "spike",
|
|
||||||
"model_score": 57.1,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 2,
|
|
||||||
"intuitive_category": "reject",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 7,
|
|
||||||
"team": "B",
|
|
||||||
"type": "spike",
|
|
||||||
"model_score": 56,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 4,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 5,
|
|
||||||
"team": "C",
|
|
||||||
"type": "spike",
|
|
||||||
"model_score": 51.1,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 1,
|
|
||||||
"team": "D",
|
|
||||||
"type": "baseline",
|
|
||||||
"model_score": 84,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 12,
|
|
||||||
"team": "E",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 74.2,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 13,
|
|
||||||
"team": "F",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 42.1,
|
|
||||||
"model_judgment": "reject",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 15,
|
|
||||||
"team": "G",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 49.7,
|
|
||||||
"model_judgment": "reject",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 4,
|
|
||||||
"team": "H",
|
|
||||||
"type": "baseline",
|
|
||||||
"model_score": 35.8,
|
|
||||||
"model_judgment": "reject",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 11,
|
|
||||||
"team": "I",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 35.3,
|
|
||||||
"model_judgment": "reject",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 6,
|
|
||||||
"team": "J",
|
|
||||||
"type": "spike",
|
|
||||||
"model_score": 73.1,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 8,
|
|
||||||
"team": "K",
|
|
||||||
"type": "spike",
|
|
||||||
"model_score": 72.9,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 2,
|
|
||||||
"team": "L",
|
|
||||||
"type": "baseline",
|
|
||||||
"model_score": 19.7,
|
|
||||||
"model_judgment": "reject",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 14,
|
|
||||||
"team": "M",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 76.9,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 3,
|
|
||||||
"team": "N",
|
|
||||||
"type": "baseline",
|
|
||||||
"model_score": 68,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"case_id": 10,
|
|
||||||
"team": "O",
|
|
||||||
"type": "conflict",
|
|
||||||
"model_score": 52.1,
|
|
||||||
"model_judgment": "select",
|
|
||||||
"intuitive_likert": 5,
|
|
||||||
"intuitive_category": "select",
|
|
||||||
"match": true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"post_survey": {
|
|
||||||
"q1": 5,
|
|
||||||
"q2": 5,
|
|
||||||
"q3": 5,
|
|
||||||
"q4": 5,
|
|
||||||
"open_response": ""
|
|
||||||
},
|
|
||||||
"server_timestamp": 1778506789232
|
|
||||||
}
|
|
||||||
]
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
../mime/cli.js
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
../sass/sass.js
|
|
||||||
-1007
File diff suppressed because it is too large
Load Diff
-21
@@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2017-present Devon Govett
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
This is the darwin-arm64 build of @parcel/watcher. See https://github.com/parcel-bundler/watcher for details.
|
|
||||||
-30
@@ -1,30 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@parcel/watcher-darwin-arm64",
|
|
||||||
"version": "2.5.6",
|
|
||||||
"main": "watcher.node",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/parcel-bundler/watcher.git"
|
|
||||||
},
|
|
||||||
"description": "A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.",
|
|
||||||
"license": "MIT",
|
|
||||||
"publishConfig": {
|
|
||||||
"access": "public"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/parcel"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"watcher.node"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 10.0.0"
|
|
||||||
},
|
|
||||||
"os": [
|
|
||||||
"darwin"
|
|
||||||
],
|
|
||||||
"cpu": [
|
|
||||||
"arm64"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
BIN
Binary file not shown.
-21
@@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2017-present Devon Govett
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
-136
@@ -1,136 +0,0 @@
|
|||||||
# @parcel/watcher
|
|
||||||
|
|
||||||
A native C++ Node module for querying and subscribing to filesystem events. Used by [Parcel 2](https://github.com/parcel-bundler/parcel).
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- **Watch** - subscribe to realtime recursive directory change notifications when files or directories are created, updated, or deleted.
|
|
||||||
- **Query** - performantly query for historical change events in a directory, even when your program is not running.
|
|
||||||
- **Native** - implemented in C++ for performance and low-level integration with the operating system.
|
|
||||||
- **Cross platform** - includes backends for macOS, Linux, Windows, FreeBSD, and Watchman.
|
|
||||||
- **Performant** - events are throttled in C++ so the JavaScript thread is not overwhelmed during large filesystem changes (e.g. `git checkout` or `npm install`).
|
|
||||||
- **Scalable** - tens of thousands of files can be watched or queried at once with good performance.
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
const watcher = require('@parcel/watcher');
|
|
||||||
const path = require('path');
|
|
||||||
|
|
||||||
// Subscribe to events
|
|
||||||
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
|
|
||||||
console.log(events);
|
|
||||||
});
|
|
||||||
|
|
||||||
// later on...
|
|
||||||
await subscription.unsubscribe();
|
|
||||||
|
|
||||||
// Get events since some saved snapshot in the past
|
|
||||||
let snapshotPath = path.join(process.cwd(), 'snapshot.txt');
|
|
||||||
let events = await watcher.getEventsSince(process.cwd(), snapshotPath);
|
|
||||||
|
|
||||||
// Save a snapshot for later
|
|
||||||
await watcher.writeSnapshot(process.cwd(), snapshotPath);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Watching
|
|
||||||
|
|
||||||
`@parcel/watcher` supports subscribing to realtime notifications of changes in a directory. It works recursively, so changes in sub-directories will also be emitted.
|
|
||||||
|
|
||||||
Events are throttled and coalesced for performance during large changes like `git checkout` or `npm install`, and a single notification will be emitted with all of the events at the end.
|
|
||||||
|
|
||||||
Only one notification will be emitted per file. For example, if a file was both created and updated since the last event, you'll get only a `create` event. If a file is both created and deleted, you will not be notifed of that file. Renames cause two events: a `delete` for the old name, and a `create` for the new name.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
let subscription = await watcher.subscribe(process.cwd(), (err, events) => {
|
|
||||||
console.log(events);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
Events have two properties:
|
|
||||||
|
|
||||||
- `type` - the event type: `create`, `update`, or `delete`.
|
|
||||||
- `path` - the absolute path to the file or directory.
|
|
||||||
|
|
||||||
To unsubscribe from change notifications, call the `unsubscribe` method on the returned subscription object.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
await subscription.unsubscribe();
|
|
||||||
```
|
|
||||||
|
|
||||||
`@parcel/watcher` has the following watcher backends, listed in priority order:
|
|
||||||
|
|
||||||
- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
|
|
||||||
- [Watchman](https://facebook.github.io/watchman/) if installed
|
|
||||||
- [inotify](http://man7.org/linux/man-pages/man7/inotify.7.html) on Linux
|
|
||||||
- [ReadDirectoryChangesW](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v%3Dvs.85%29.aspx) on Windows
|
|
||||||
- [kqueue](https://man.freebsd.org/cgi/man.cgi?kqueue) on FreeBSD, or as an alternative to FSEvents on macOS
|
|
||||||
|
|
||||||
You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
|
|
||||||
|
|
||||||
## Querying
|
|
||||||
|
|
||||||
`@parcel/watcher` also supports querying for historical changes made in a directory, even when your program is not running. This makes it easy to invalidate a cache and re-build only the files that have changed, for example. It can be **significantly** faster than traversing the entire filesystem to determine what files changed, depending on the platform.
|
|
||||||
|
|
||||||
In order to query for historical changes, you first need a previous snapshot to compare to. This can be saved to a file with the `writeSnapshot` function, e.g. just before your program exits.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
await watcher.writeSnapshot(dirPath, snapshotPath);
|
|
||||||
```
|
|
||||||
|
|
||||||
When your program starts up, you can query for changes that have occurred since that snapshot using the `getEventsSince` function.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
let events = await watcher.getEventsSince(dirPath, snapshotPath);
|
|
||||||
```
|
|
||||||
|
|
||||||
The events returned are exactly the same as the events that would be passed to the `subscribe` callback (see above).
|
|
||||||
|
|
||||||
`@parcel/watcher` has the following watcher backends, listed in priority order:
|
|
||||||
|
|
||||||
- [FSEvents](https://developer.apple.com/documentation/coreservices/file_system_events) on macOS
|
|
||||||
- [Watchman](https://facebook.github.io/watchman/) if installed
|
|
||||||
- [fts](http://man7.org/linux/man-pages/man3/fts.3.html) (brute force) on Linux and FreeBSD
|
|
||||||
- [FindFirstFile](https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea) (brute force) on Windows
|
|
||||||
|
|
||||||
The FSEvents (macOS) and Watchman backends are significantly more performant than the brute force backends used by default on Linux and Windows, for example returning results in miliseconds instead of seconds for large directory trees. This is because a background daemon monitoring filesystem changes on those platforms allows us to query cached data rather than traversing the filesystem manually (brute force).
|
|
||||||
|
|
||||||
macOS has good performance with FSEvents by default. For the best performance on other platforms, install [Watchman](https://facebook.github.io/watchman/) and it will be used by `@parcel/watcher` automatically.
|
|
||||||
|
|
||||||
You can specify the exact backend you wish to use by passing the `backend` option. If that backend is not available on the current platform, the default backend will be used instead. See below for the list of backend names that can be passed to the options.
|
|
||||||
|
|
||||||
## Options
|
|
||||||
|
|
||||||
All of the APIs in `@parcel/watcher` support the following options, which are passed as an object as the last function argument.
|
|
||||||
|
|
||||||
- `ignore` - an array of paths or glob patterns to ignore. uses [`is-glob`](https://github.com/micromatch/is-glob) to distinguish paths from globs. glob patterns are parsed with [`picomatch`](https://github.com/micromatch/picomatch) (see [features](https://github.com/micromatch/picomatch#globbing-features)).
|
|
||||||
- paths can be relative or absolute and can either be files or directories. No events will be emitted about these files or directories or their children.
|
|
||||||
- glob patterns match on relative paths from the root that is watched. No events will be emitted for matching paths.
|
|
||||||
- `backend` - the name of an explicitly chosen backend to use. Allowed options are `"fs-events"`, `"watchman"`, `"inotify"`, `"kqueue"`, `"windows"`, or `"brute-force"` (only for querying). If the specified backend is not available on the current platform, the default backend will be used instead.
|
|
||||||
|
|
||||||
## WASM
|
|
||||||
|
|
||||||
The `@parcel/watcher-wasm` package can be used in place of `@parcel/watcher` on unsupported platforms. It relies on the Node `fs` module, so in non-Node environments such as browsers, an `fs` polyfill will be needed.
|
|
||||||
|
|
||||||
**Note**: the WASM implementation is significantly less efficient than the native implementations because it must crawl the file system to watch each directory individually. Use the native `@parcel/watcher` package wherever possible.
|
|
||||||
|
|
||||||
```js
|
|
||||||
import {subscribe} from '@parcel/watcher-wasm';
|
|
||||||
|
|
||||||
// Use the module as documented above.
|
|
||||||
subscribe(/* ... */);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Who is using this?
|
|
||||||
|
|
||||||
- [Parcel 2](https://parceljs.org/)
|
|
||||||
- [VSCode](https://code.visualstudio.com/updates/v1_62#_file-watching-changes)
|
|
||||||
- [Tailwind CSS Intellisense](https://github.com/tailwindlabs/tailwindcss-intellisense)
|
|
||||||
- [Gatsby Cloud](https://twitter.com/chatsidhartha/status/1435647412828196867)
|
|
||||||
- [Nx](https://nx.dev)
|
|
||||||
- [Nuxt](https://nuxt.com)
|
|
||||||
- [Meteor](https://github.com/meteor/meteor)
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
MIT
|
|
||||||
-109
@@ -1,109 +0,0 @@
|
|||||||
{
|
|
||||||
"targets": [
|
|
||||||
{
|
|
||||||
"target_name": "watcher",
|
|
||||||
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
|
|
||||||
"sources": [ "src/binding.cc", "src/Watcher.cc", "src/Backend.cc", "src/DirTree.cc", "src/Glob.cc", "src/Debounce.cc" ],
|
|
||||||
"include_dirs" : ["<!(node -p \"require('node-addon-api').include_dir\")"],
|
|
||||||
'cflags!': [ '-fno-exceptions', '-std=c++17' ],
|
|
||||||
'cflags_cc!': [ '-fno-exceptions', '-std=c++17' ],
|
|
||||||
'cflags': [ '-fstack-protector-strong' ],
|
|
||||||
"conditions": [
|
|
||||||
['OS=="mac"', {
|
|
||||||
"sources": [
|
|
||||||
"src/watchman/BSER.cc",
|
|
||||||
"src/watchman/WatchmanBackend.cc",
|
|
||||||
"src/shared/BruteForceBackend.cc",
|
|
||||||
"src/unix/fts.cc",
|
|
||||||
"src/macos/FSEventsBackend.cc",
|
|
||||||
"src/kqueue/KqueueBackend.cc"
|
|
||||||
],
|
|
||||||
"link_settings": {
|
|
||||||
"libraries": ["CoreServices.framework"]
|
|
||||||
},
|
|
||||||
"defines": [
|
|
||||||
"WATCHMAN",
|
|
||||||
"BRUTE_FORCE",
|
|
||||||
"FS_EVENTS",
|
|
||||||
"KQUEUE"
|
|
||||||
],
|
|
||||||
"xcode_settings": {
|
|
||||||
"GCC_ENABLE_CPP_EXCEPTIONS": "YES"
|
|
||||||
}
|
|
||||||
}],
|
|
||||||
['OS=="mac" and target_arch=="arm64"', {
|
|
||||||
"xcode_settings": {
|
|
||||||
"ARCHS": ["arm64"]
|
|
||||||
}
|
|
||||||
}],
|
|
||||||
['OS=="linux" or OS=="android"', {
|
|
||||||
"sources": [
|
|
||||||
"src/watchman/BSER.cc",
|
|
||||||
"src/watchman/WatchmanBackend.cc",
|
|
||||||
"src/shared/BruteForceBackend.cc",
|
|
||||||
"src/linux/InotifyBackend.cc",
|
|
||||||
"src/unix/legacy.cc"
|
|
||||||
],
|
|
||||||
"defines": [
|
|
||||||
"WATCHMAN",
|
|
||||||
"INOTIFY",
|
|
||||||
"BRUTE_FORCE"
|
|
||||||
]
|
|
||||||
}],
|
|
||||||
['OS=="win"', {
|
|
||||||
"sources": [
|
|
||||||
"src/watchman/BSER.cc",
|
|
||||||
"src/watchman/WatchmanBackend.cc",
|
|
||||||
"src/shared/BruteForceBackend.cc",
|
|
||||||
"src/windows/WindowsBackend.cc",
|
|
||||||
"src/windows/win_utils.cc"
|
|
||||||
],
|
|
||||||
"defines": [
|
|
||||||
"WATCHMAN",
|
|
||||||
"WINDOWS",
|
|
||||||
"BRUTE_FORCE"
|
|
||||||
],
|
|
||||||
"msvs_settings": {
|
|
||||||
"VCCLCompilerTool": {
|
|
||||||
"ExceptionHandling": 1, # /EHsc
|
|
||||||
"AdditionalOptions": [
|
|
||||||
"-std:c++17",
|
|
||||||
"/guard:cf",
|
|
||||||
"/W3",
|
|
||||||
"/we4146",
|
|
||||||
"/w34244",
|
|
||||||
"/we4267",
|
|
||||||
"/sdl",
|
|
||||||
"/ZH:SHA_256"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"VCLinkerTool": {
|
|
||||||
"AdditionalOptions": [
|
|
||||||
"/DYNAMICBASE",
|
|
||||||
"/guard:cf"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}],
|
|
||||||
['OS=="freebsd"', {
|
|
||||||
"sources": [
|
|
||||||
"src/watchman/BSER.cc",
|
|
||||||
"src/watchman/WatchmanBackend.cc",
|
|
||||||
"src/shared/BruteForceBackend.cc",
|
|
||||||
"src/unix/fts.cc",
|
|
||||||
"src/kqueue/KqueueBackend.cc"
|
|
||||||
],
|
|
||||||
"defines": [
|
|
||||||
"WATCHMAN",
|
|
||||||
"BRUTE_FORCE",
|
|
||||||
"KQUEUE"
|
|
||||||
]
|
|
||||||
}]
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"variables": {
|
|
||||||
"openssl_fips": "",
|
|
||||||
"node_use_dtrace": "false"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-49
@@ -1,49 +0,0 @@
|
|||||||
declare type FilePath = string;
|
|
||||||
declare type GlobPattern = string;
|
|
||||||
|
|
||||||
declare namespace ParcelWatcher {
|
|
||||||
export type BackendType =
|
|
||||||
| 'fs-events'
|
|
||||||
| 'watchman'
|
|
||||||
| 'inotify'
|
|
||||||
| 'windows'
|
|
||||||
| 'brute-force';
|
|
||||||
export type EventType = 'create' | 'update' | 'delete';
|
|
||||||
export interface Options {
|
|
||||||
ignore?: (FilePath|GlobPattern)[];
|
|
||||||
backend?: BackendType;
|
|
||||||
}
|
|
||||||
export type SubscribeCallback = (
|
|
||||||
err: Error | null,
|
|
||||||
events: Event[]
|
|
||||||
) => unknown;
|
|
||||||
export interface AsyncSubscription {
|
|
||||||
unsubscribe(): Promise<void>;
|
|
||||||
}
|
|
||||||
export interface Event {
|
|
||||||
path: FilePath;
|
|
||||||
type: EventType;
|
|
||||||
}
|
|
||||||
export function getEventsSince(
|
|
||||||
dir: FilePath,
|
|
||||||
snapshot: FilePath,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<Event[]>;
|
|
||||||
export function subscribe(
|
|
||||||
dir: FilePath,
|
|
||||||
fn: SubscribeCallback,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<AsyncSubscription>;
|
|
||||||
export function unsubscribe(
|
|
||||||
dir: FilePath,
|
|
||||||
fn: SubscribeCallback,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<void>;
|
|
||||||
export function writeSnapshot(
|
|
||||||
dir: FilePath,
|
|
||||||
snapshot: FilePath,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<FilePath>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export = ParcelWatcher;
|
|
||||||
-42
@@ -1,42 +0,0 @@
|
|||||||
const {createWrapper} = require('./wrapper');
|
|
||||||
|
|
||||||
let name = `@parcel/watcher-${process.platform}-${process.arch}`;
|
|
||||||
if (process.platform === 'linux') {
|
|
||||||
const { MUSL, familySync } = require('detect-libc');
|
|
||||||
const family = familySync();
|
|
||||||
if (family === MUSL) {
|
|
||||||
name += '-musl';
|
|
||||||
} else {
|
|
||||||
name += '-glibc';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let binding;
|
|
||||||
try {
|
|
||||||
binding = require(name);
|
|
||||||
} catch (err) {
|
|
||||||
handleError(err);
|
|
||||||
try {
|
|
||||||
binding = require('./build/Release/watcher.node');
|
|
||||||
} catch (err) {
|
|
||||||
handleError(err);
|
|
||||||
try {
|
|
||||||
binding = require('./build/Debug/watcher.node');
|
|
||||||
} catch (err) {
|
|
||||||
handleError(err);
|
|
||||||
throw new Error(`No prebuild or local build of @parcel/watcher found. Tried ${name}. Please ensure it is installed (don't use --no-optional when installing with npm). Otherwise it is possible we don't support your platform yet. If this is the case, please report an issue to https://github.com/parcel-bundler/watcher.`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleError(err) {
|
|
||||||
if (err?.code !== 'MODULE_NOT_FOUND') {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const wrapper = createWrapper(binding);
|
|
||||||
exports.writeSnapshot = wrapper.writeSnapshot;
|
|
||||||
exports.getEventsSince = wrapper.getEventsSince;
|
|
||||||
exports.subscribe = wrapper.subscribe;
|
|
||||||
exports.unsubscribe = wrapper.unsubscribe;
|
|
||||||
-48
@@ -1,48 +0,0 @@
|
|||||||
// @flow
|
|
||||||
declare type FilePath = string;
|
|
||||||
declare type GlobPattern = string;
|
|
||||||
|
|
||||||
export type BackendType =
|
|
||||||
| 'fs-events'
|
|
||||||
| 'watchman'
|
|
||||||
| 'inotify'
|
|
||||||
| 'windows'
|
|
||||||
| 'brute-force';
|
|
||||||
export type EventType = 'create' | 'update' | 'delete';
|
|
||||||
export interface Options {
|
|
||||||
ignore?: Array<FilePath | GlobPattern>,
|
|
||||||
backend?: BackendType
|
|
||||||
}
|
|
||||||
export type SubscribeCallback = (
|
|
||||||
err: ?Error,
|
|
||||||
events: Array<Event>
|
|
||||||
) => mixed;
|
|
||||||
export interface AsyncSubscription {
|
|
||||||
unsubscribe(): Promise<void>
|
|
||||||
}
|
|
||||||
export interface Event {
|
|
||||||
path: FilePath,
|
|
||||||
type: EventType
|
|
||||||
}
|
|
||||||
declare module.exports: {
|
|
||||||
getEventsSince(
|
|
||||||
dir: FilePath,
|
|
||||||
snapshot: FilePath,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<Array<Event>>,
|
|
||||||
subscribe(
|
|
||||||
dir: FilePath,
|
|
||||||
fn: SubscribeCallback,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<AsyncSubscription>,
|
|
||||||
unsubscribe(
|
|
||||||
dir: FilePath,
|
|
||||||
fn: SubscribeCallback,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<void>,
|
|
||||||
writeSnapshot(
|
|
||||||
dir: FilePath,
|
|
||||||
snapshot: FilePath,
|
|
||||||
opts?: Options
|
|
||||||
): Promise<FilePath>
|
|
||||||
}
|
|
||||||
-88
@@ -1,88 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "@parcel/watcher",
|
|
||||||
"version": "2.5.6",
|
|
||||||
"main": "index.js",
|
|
||||||
"types": "index.d.ts",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/parcel-bundler/watcher.git"
|
|
||||||
},
|
|
||||||
"description": "A native C++ Node module for querying and subscribing to filesystem events. Used by Parcel 2.",
|
|
||||||
"license": "MIT",
|
|
||||||
"publishConfig": {
|
|
||||||
"access": "public"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"type": "opencollective",
|
|
||||||
"url": "https://opencollective.com/parcel"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"index.js",
|
|
||||||
"index.js.flow",
|
|
||||||
"index.d.ts",
|
|
||||||
"wrapper.js",
|
|
||||||
"package.json",
|
|
||||||
"README.md",
|
|
||||||
"LICENSE",
|
|
||||||
"src",
|
|
||||||
"scripts/build-from-source.js",
|
|
||||||
"binding.gyp"
|
|
||||||
],
|
|
||||||
"scripts": {
|
|
||||||
"prebuild": "prebuildify --napi --strip --tag-libc",
|
|
||||||
"format": "prettier --write \"./**/*.{js,json,md}\"",
|
|
||||||
"build": "node-gyp rebuild",
|
|
||||||
"install": "node scripts/build-from-source.js",
|
|
||||||
"test": "mocha"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 10.0.0"
|
|
||||||
},
|
|
||||||
"husky": {
|
|
||||||
"hooks": {
|
|
||||||
"pre-commit": "lint-staged"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"lint-staged": {
|
|
||||||
"*.{js,json,md}": [
|
|
||||||
"prettier --write",
|
|
||||||
"git add"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"detect-libc": "^2.0.3",
|
|
||||||
"is-glob": "^4.0.3",
|
|
||||||
"node-addon-api": "^7.0.0",
|
|
||||||
"picomatch": "^4.0.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"esbuild": "^0.19.8",
|
|
||||||
"fs-extra": "^10.0.0",
|
|
||||||
"husky": "^7.0.2",
|
|
||||||
"lint-staged": "^11.1.2",
|
|
||||||
"mocha": "^9.1.1",
|
|
||||||
"napi-wasm": "^1.1.0",
|
|
||||||
"prebuildify": "^6.0.1",
|
|
||||||
"prettier": "^2.3.2"
|
|
||||||
},
|
|
||||||
"binary": {
|
|
||||||
"napi_versions": [
|
|
||||||
3
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"@parcel/watcher-darwin-x64": "2.5.6",
|
|
||||||
"@parcel/watcher-darwin-arm64": "2.5.6",
|
|
||||||
"@parcel/watcher-win32-x64": "2.5.6",
|
|
||||||
"@parcel/watcher-win32-arm64": "2.5.6",
|
|
||||||
"@parcel/watcher-win32-ia32": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-x64-glibc": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-x64-musl": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-arm64-glibc": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-arm64-musl": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-arm-glibc": "2.5.6",
|
|
||||||
"@parcel/watcher-linux-arm-musl": "2.5.6",
|
|
||||||
"@parcel/watcher-android-arm64": "2.5.6",
|
|
||||||
"@parcel/watcher-freebsd-x64": "2.5.6"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
|
|
||||||
const {spawn} = require('child_process');
|
|
||||||
|
|
||||||
if (process.env.npm_config_build_from_source === 'true') {
|
|
||||||
build();
|
|
||||||
}
|
|
||||||
|
|
||||||
function build() {
|
|
||||||
spawn('node-gyp', ['rebuild'], { stdio: 'inherit', shell: true }).on('exit', function (code) {
|
|
||||||
process.exit(code);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
-186
@@ -1,186 +0,0 @@
|
|||||||
#ifdef FS_EVENTS
|
|
||||||
#include "macos/FSEventsBackend.hh"
|
|
||||||
#endif
|
|
||||||
#ifdef WATCHMAN
|
|
||||||
#include "watchman/WatchmanBackend.hh"
|
|
||||||
#endif
|
|
||||||
#ifdef WINDOWS
|
|
||||||
#include "windows/WindowsBackend.hh"
|
|
||||||
#endif
|
|
||||||
#ifdef INOTIFY
|
|
||||||
#include "linux/InotifyBackend.hh"
|
|
||||||
#endif
|
|
||||||
#ifdef KQUEUE
|
|
||||||
#include "kqueue/KqueueBackend.hh"
|
|
||||||
#endif
|
|
||||||
#ifdef __wasm32__
|
|
||||||
#include "wasm/WasmBackend.hh"
|
|
||||||
#endif
|
|
||||||
#include "shared/BruteForceBackend.hh"
|
|
||||||
|
|
||||||
#include "Backend.hh"
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
static std::unordered_map<std::string, std::shared_ptr<Backend>>& getSharedBackends() {
|
|
||||||
static std::unordered_map<std::string, std::shared_ptr<Backend>>* sharedBackends =
|
|
||||||
new std::unordered_map<std::string, std::shared_ptr<Backend>>();
|
|
||||||
return *sharedBackends;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Backend> getBackend(std::string backend) {
|
|
||||||
// Use FSEvents on macOS by default.
|
|
||||||
// Use watchman by default if available on other platforms.
|
|
||||||
// Fall back to brute force.
|
|
||||||
#ifdef FS_EVENTS
|
|
||||||
if (backend == "fs-events" || backend == "default") {
|
|
||||||
return std::make_shared<FSEventsBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef WATCHMAN
|
|
||||||
if ((backend == "watchman" || backend == "default") && WatchmanBackend::checkAvailable()) {
|
|
||||||
return std::make_shared<WatchmanBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef WINDOWS
|
|
||||||
if (backend == "windows" || backend == "default") {
|
|
||||||
return std::make_shared<WindowsBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef INOTIFY
|
|
||||||
if (backend == "inotify" || backend == "default") {
|
|
||||||
return std::make_shared<InotifyBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef KQUEUE
|
|
||||||
if (backend == "kqueue" || backend == "default") {
|
|
||||||
return std::make_shared<KqueueBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef __wasm32__
|
|
||||||
if (backend == "wasm" || backend == "default") {
|
|
||||||
return std::make_shared<WasmBackend>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (backend == "brute-force" || backend == "default") {
|
|
||||||
return std::make_shared<BruteForceBackend>();
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Backend> Backend::getShared(std::string backend) {
|
|
||||||
auto found = getSharedBackends().find(backend);
|
|
||||||
if (found != getSharedBackends().end()) {
|
|
||||||
return found->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto result = getBackend(backend);
|
|
||||||
if (!result) {
|
|
||||||
return getShared("default");
|
|
||||||
}
|
|
||||||
|
|
||||||
result->run();
|
|
||||||
getSharedBackends().emplace(backend, result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeShared(Backend *backend) {
|
|
||||||
for (auto it = getSharedBackends().begin(); it != getSharedBackends().end(); it++) {
|
|
||||||
if (it->second.get() == backend) {
|
|
||||||
getSharedBackends().erase(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Free up memory.
|
|
||||||
if (getSharedBackends().size() == 0) {
|
|
||||||
getSharedBackends().rehash(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::run() {
|
|
||||||
#ifndef __wasm32__
|
|
||||||
mThread = std::thread([this] () {
|
|
||||||
try {
|
|
||||||
start();
|
|
||||||
} catch (std::exception &err) {
|
|
||||||
handleError(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (mThread.joinable()) {
|
|
||||||
mStartedSignal.wait();
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
try {
|
|
||||||
start();
|
|
||||||
} catch (std::exception &err) {
|
|
||||||
handleError(err);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::notifyStarted() {
|
|
||||||
mStartedSignal.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::start() {
|
|
||||||
notifyStarted();
|
|
||||||
}
|
|
||||||
|
|
||||||
Backend::~Backend() {
|
|
||||||
#ifndef __wasm32__
|
|
||||||
// Wait for thread to stop
|
|
||||||
if (mThread.joinable()) {
|
|
||||||
// If the backend is being destroyed from the thread itself, detach, otherwise join.
|
|
||||||
if (mThread.get_id() == std::this_thread::get_id()) {
|
|
||||||
mThread.detach();
|
|
||||||
} else {
|
|
||||||
mThread.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::watch(WatcherRef watcher) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
auto res = mSubscriptions.find(watcher);
|
|
||||||
if (res == mSubscriptions.end()) {
|
|
||||||
try {
|
|
||||||
this->subscribe(watcher);
|
|
||||||
mSubscriptions.insert(watcher);
|
|
||||||
} catch (std::exception&) {
|
|
||||||
unref();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::unwatch(WatcherRef watcher) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
size_t deleted = mSubscriptions.erase(watcher);
|
|
||||||
if (deleted > 0) {
|
|
||||||
this->unsubscribe(watcher);
|
|
||||||
unref();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::unref() {
|
|
||||||
if (mSubscriptions.size() == 0) {
|
|
||||||
removeShared(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::handleWatcherError(WatcherError &err) {
|
|
||||||
unwatch(err.mWatcher);
|
|
||||||
err.mWatcher->notifyError(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Backend::handleError(std::exception &err) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); it++) {
|
|
||||||
(*it)->notifyError(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
removeShared(this);
|
|
||||||
}
|
|
||||||
-37
@@ -1,37 +0,0 @@
|
|||||||
#ifndef BACKEND_H
|
|
||||||
#define BACKEND_H
|
|
||||||
|
|
||||||
#include "Event.hh"
|
|
||||||
#include "Watcher.hh"
|
|
||||||
#include "Signal.hh"
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
class Backend {
|
|
||||||
public:
|
|
||||||
virtual ~Backend();
|
|
||||||
void run();
|
|
||||||
void notifyStarted();
|
|
||||||
|
|
||||||
virtual void start();
|
|
||||||
virtual void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) = 0;
|
|
||||||
virtual void getEventsSince(WatcherRef watcher, std::string *snapshotPath) = 0;
|
|
||||||
virtual void subscribe(WatcherRef watcher) = 0;
|
|
||||||
virtual void unsubscribe(WatcherRef watcher) = 0;
|
|
||||||
|
|
||||||
static std::shared_ptr<Backend> getShared(std::string backend);
|
|
||||||
|
|
||||||
void watch(WatcherRef watcher);
|
|
||||||
void unwatch(WatcherRef watcher);
|
|
||||||
void unref();
|
|
||||||
void handleWatcherError(WatcherError &err);
|
|
||||||
|
|
||||||
std::mutex mMutex;
|
|
||||||
std::thread mThread;
|
|
||||||
private:
|
|
||||||
std::unordered_set<WatcherRef> mSubscriptions;
|
|
||||||
Signal mStartedSignal;
|
|
||||||
|
|
||||||
void handleError(std::exception &err);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-113
@@ -1,113 +0,0 @@
|
|||||||
#include "Debounce.hh"
|
|
||||||
|
|
||||||
#ifdef __wasm32__
|
|
||||||
extern "C" void on_timeout(void *ctx) {
|
|
||||||
Debounce *debounce = (Debounce *)ctx;
|
|
||||||
debounce->notify();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::shared_ptr<Debounce> Debounce::getShared() {
|
|
||||||
static std::weak_ptr<Debounce> sharedInstance;
|
|
||||||
std::shared_ptr<Debounce> shared = sharedInstance.lock();
|
|
||||||
if (!shared) {
|
|
||||||
shared = std::make_shared<Debounce>();
|
|
||||||
sharedInstance = shared;
|
|
||||||
}
|
|
||||||
|
|
||||||
return shared;
|
|
||||||
}
|
|
||||||
|
|
||||||
Debounce::Debounce() {
|
|
||||||
mRunning = true;
|
|
||||||
#ifndef __wasm32__
|
|
||||||
mThread = std::thread([this] () {
|
|
||||||
loop();
|
|
||||||
});
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
Debounce::~Debounce() {
|
|
||||||
mRunning = false;
|
|
||||||
#ifndef __wasm32__
|
|
||||||
mWaitSignal.notify();
|
|
||||||
mThread.join();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debounce::add(void *key, std::function<void()> cb) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
mCallbacks.emplace(key, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debounce::remove(void *key) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
mCallbacks.erase(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debounce::trigger() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
#ifdef __wasm32__
|
|
||||||
notifyIfReady();
|
|
||||||
#else
|
|
||||||
mWaitSignal.notify();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef __wasm32__
|
|
||||||
void Debounce::loop() {
|
|
||||||
while (mRunning) {
|
|
||||||
mWaitSignal.wait();
|
|
||||||
if (!mRunning) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyIfReady();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void Debounce::notifyIfReady() {
|
|
||||||
if (!mRunning) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we haven't seen an event in more than the maximum wait time, notify callbacks immediately
|
|
||||||
// to ensure that we don't wait forever. Otherwise, wait for the minimum wait time and batch
|
|
||||||
// subsequent fast changes. This also means the first file change in a batch is notified immediately,
|
|
||||||
// separately from the rest of the batch. This seems like an acceptable tradeoff if the common case
|
|
||||||
// is that only a single file was updated at a time.
|
|
||||||
auto time = std::chrono::steady_clock::now();
|
|
||||||
if ((time - mLastTime) > std::chrono::milliseconds(MAX_WAIT_TIME)) {
|
|
||||||
mLastTime = time;
|
|
||||||
notify();
|
|
||||||
} else {
|
|
||||||
wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debounce::wait() {
|
|
||||||
#ifdef __wasm32__
|
|
||||||
clear_timeout(mTimeout);
|
|
||||||
mTimeout = set_timeout(MIN_WAIT_TIME, this);
|
|
||||||
#else
|
|
||||||
auto status = mWaitSignal.waitFor(std::chrono::milliseconds(MIN_WAIT_TIME));
|
|
||||||
if (mRunning && (status == std::cv_status::timeout)) {
|
|
||||||
notify();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Debounce::notify() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
|
|
||||||
mLastTime = std::chrono::steady_clock::now();
|
|
||||||
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
||||||
auto cb = it->second;
|
|
||||||
cb();
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef __wasm32__
|
|
||||||
mWaitSignal.reset();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
-49
@@ -1,49 +0,0 @@
|
|||||||
#ifndef DEBOUNCE_H
|
|
||||||
#define DEBOUNCE_H
|
|
||||||
|
|
||||||
#include <thread>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <functional>
|
|
||||||
#include "Signal.hh"
|
|
||||||
|
|
||||||
#define MIN_WAIT_TIME 50
|
|
||||||
#define MAX_WAIT_TIME 500
|
|
||||||
|
|
||||||
#ifdef __wasm32__
|
|
||||||
extern "C" {
|
|
||||||
int set_timeout(int ms, void *ctx);
|
|
||||||
void clear_timeout(int timeout);
|
|
||||||
void on_timeout(void *ctx);
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class Debounce {
|
|
||||||
public:
|
|
||||||
static std::shared_ptr<Debounce> getShared();
|
|
||||||
|
|
||||||
Debounce();
|
|
||||||
~Debounce();
|
|
||||||
|
|
||||||
void add(void *key, std::function<void()> cb);
|
|
||||||
void remove(void *key);
|
|
||||||
void trigger();
|
|
||||||
void notify();
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool mRunning;
|
|
||||||
std::mutex mMutex;
|
|
||||||
#ifdef __wasm32__
|
|
||||||
int mTimeout;
|
|
||||||
#else
|
|
||||||
Signal mWaitSignal;
|
|
||||||
std::thread mThread;
|
|
||||||
#endif
|
|
||||||
std::unordered_map<void *, std::function<void()>> mCallbacks;
|
|
||||||
std::chrono::time_point<std::chrono::steady_clock> mLastTime;
|
|
||||||
|
|
||||||
void loop();
|
|
||||||
void notifyIfReady();
|
|
||||||
void wait();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-164
@@ -1,164 +0,0 @@
|
|||||||
#include "DirTree.hh"
|
|
||||||
#include <inttypes.h>
|
|
||||||
|
|
||||||
// "Meyer's singleton", construction is ordered by use, likewise (reverse) for destruction.
|
|
||||||
// https://stackoverflow.com/a/17713799
|
|
||||||
// https://laristra.github.io/flecsi/src/developer-guide/patterns/meyers_singleton.html
|
|
||||||
static std::mutex& mDirCacheMutex() {
|
|
||||||
static std::mutex mutex;
|
|
||||||
return mutex;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::unordered_map<std::string, std::weak_ptr<DirTree>>& dirTreeCache() {
|
|
||||||
static std::unordered_map<std::string, std::weak_ptr<DirTree>> cache;
|
|
||||||
return cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DirTreeDeleter {
|
|
||||||
void operator()(DirTree *tree) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
|
|
||||||
cache.erase(tree->root);
|
|
||||||
delete tree;
|
|
||||||
|
|
||||||
// Free up memory.
|
|
||||||
if (cache.size() == 0) {
|
|
||||||
cache.rehash(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::shared_ptr<DirTree> DirTree::getCached(std::string root) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
std::unordered_map<std::string, std::weak_ptr<DirTree>> &cache = dirTreeCache();
|
|
||||||
|
|
||||||
auto found = cache.find(root);
|
|
||||||
std::shared_ptr<DirTree> tree;
|
|
||||||
|
|
||||||
// Use cached tree, or create an empty one.
|
|
||||||
if (found != cache.end()) {
|
|
||||||
tree = found->second.lock();
|
|
||||||
} else {
|
|
||||||
tree = std::shared_ptr<DirTree>(new DirTree(root), DirTreeDeleter());
|
|
||||||
cache.emplace(root, tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
return tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirTree::DirTree(std::string root, FILE *f) : root(root), isComplete(true) {
|
|
||||||
size_t size;
|
|
||||||
if (fscanf(f, "%zu", &size)) {
|
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
DirEntry entry(f);
|
|
||||||
entries.emplace(entry.path, entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Internal find method that has no lock
|
|
||||||
DirEntry *DirTree::_find(std::string path) {
|
|
||||||
auto found = entries.find(path);
|
|
||||||
if (found == entries.end()) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &found->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry *DirTree::add(std::string path, uint64_t mtime, bool isDir) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
|
|
||||||
DirEntry entry(path, mtime, isDir);
|
|
||||||
auto it = entries.emplace(entry.path, entry);
|
|
||||||
return &it.first->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry *DirTree::find(std::string path) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
return _find(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry *DirTree::update(std::string path, uint64_t mtime) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
|
|
||||||
DirEntry *found = _find(path);
|
|
||||||
if (found) {
|
|
||||||
found->mtime = mtime;
|
|
||||||
}
|
|
||||||
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
void DirTree::remove(std::string path) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
|
|
||||||
DirEntry *found = _find(path);
|
|
||||||
|
|
||||||
// Remove all sub-entries if this is a directory
|
|
||||||
if (found && found->isDir) {
|
|
||||||
std::string pathStart = path + DIR_SEP;
|
|
||||||
for (auto it = entries.begin(); it != entries.end();) {
|
|
||||||
if (it->first.rfind(pathStart, 0) == 0) {
|
|
||||||
it = entries.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
entries.erase(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DirTree::write(FILE *f) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
|
|
||||||
fprintf(f, "%zu\n", entries.size());
|
|
||||||
for (auto it = entries.begin(); it != entries.end(); it++) {
|
|
||||||
it->second.write(f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DirTree::getChanges(DirTree *snapshot, EventList &events) {
|
|
||||||
std::lock_guard<std::mutex> lock(mDirCacheMutex());
|
|
||||||
std::lock_guard<std::mutex> snapshotLock(snapshot->mMutex);
|
|
||||||
|
|
||||||
for (auto it = entries.begin(); it != entries.end(); it++) {
|
|
||||||
auto found = snapshot->entries.find(it->first);
|
|
||||||
if (found == snapshot->entries.end()) {
|
|
||||||
events.create(it->second.path);
|
|
||||||
} else if (found->second.mtime != it->second.mtime && !found->second.isDir && !it->second.isDir) {
|
|
||||||
events.update(it->second.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = snapshot->entries.begin(); it != snapshot->entries.end(); it++) {
|
|
||||||
size_t count = entries.count(it->first);
|
|
||||||
if (count == 0) {
|
|
||||||
events.remove(it->second.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry::DirEntry(std::string p, uint64_t t, bool d) {
|
|
||||||
path = p;
|
|
||||||
mtime = t;
|
|
||||||
isDir = d;
|
|
||||||
state = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry::DirEntry(FILE *f) {
|
|
||||||
size_t size;
|
|
||||||
if (fscanf(f, "%zu", &size)) {
|
|
||||||
path.resize(size);
|
|
||||||
if (fread(&path[0], sizeof(char), size, f)) {
|
|
||||||
int d = 0;
|
|
||||||
fscanf(f, "%" PRIu64 " %d\n", &mtime, &d);
|
|
||||||
isDir = d == 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DirEntry::write(FILE *f) const {
|
|
||||||
fprintf(f, "%zu%s%" PRIu64 " %d\n", path.size(), path.c_str(), mtime, isDir);
|
|
||||||
}
|
|
||||||
-50
@@ -1,50 +0,0 @@
|
|||||||
#ifndef DIR_TREE_H
|
|
||||||
#define DIR_TREE_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <memory>
|
|
||||||
#include "Event.hh"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define DIR_SEP "\\"
|
|
||||||
#else
|
|
||||||
#define DIR_SEP "/"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct DirEntry {
|
|
||||||
std::string path;
|
|
||||||
uint64_t mtime;
|
|
||||||
bool isDir;
|
|
||||||
mutable void *state;
|
|
||||||
|
|
||||||
DirEntry(std::string p, uint64_t t, bool d);
|
|
||||||
DirEntry(FILE *f);
|
|
||||||
void write(FILE *f) const;
|
|
||||||
bool operator==(const DirEntry &other) const {
|
|
||||||
return path == other.path;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class DirTree {
|
|
||||||
public:
|
|
||||||
static std::shared_ptr<DirTree> getCached(std::string root);
|
|
||||||
DirTree(std::string root) : root(root), isComplete(false) {}
|
|
||||||
DirTree(std::string root, FILE *f);
|
|
||||||
DirEntry *add(std::string path, uint64_t mtime, bool isDir);
|
|
||||||
DirEntry *find(std::string path);
|
|
||||||
DirEntry *update(std::string path, uint64_t mtime);
|
|
||||||
void remove(std::string path);
|
|
||||||
void write(FILE *f);
|
|
||||||
void getChanges(DirTree *snapshot, EventList &events);
|
|
||||||
|
|
||||||
std::mutex mMutex;
|
|
||||||
std::string root;
|
|
||||||
bool isComplete;
|
|
||||||
std::unordered_map<std::string, DirEntry> entries;
|
|
||||||
|
|
||||||
private:
|
|
||||||
DirEntry *_find(std::string path);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-109
@@ -1,109 +0,0 @@
|
|||||||
#ifndef EVENT_H
|
|
||||||
#define EVENT_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <node_api.h>
|
|
||||||
#include "wasm/include.h"
|
|
||||||
#include <napi.h>
|
|
||||||
#include <mutex>
|
|
||||||
#include <map>
|
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
using namespace Napi;
|
|
||||||
|
|
||||||
struct Event {
|
|
||||||
std::string path;
|
|
||||||
bool isCreated;
|
|
||||||
bool isDeleted;
|
|
||||||
Event(std::string path) : path(path), isCreated(false), isDeleted(false) {}
|
|
||||||
|
|
||||||
Value toJS(const Env& env) {
|
|
||||||
EscapableHandleScope scope(env);
|
|
||||||
Object res = Object::New(env);
|
|
||||||
std::string type = isCreated ? "create" : isDeleted ? "delete" : "update";
|
|
||||||
res.Set(String::New(env, "path"), String::New(env, path.c_str()));
|
|
||||||
res.Set(String::New(env, "type"), String::New(env, type.c_str()));
|
|
||||||
return scope.Escape(res);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class EventList {
|
|
||||||
public:
|
|
||||||
void create(std::string path) {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
Event *event = internalUpdate(path);
|
|
||||||
if (event->isDeleted) {
|
|
||||||
// Assume update event when rapidly removed and created
|
|
||||||
// https://github.com/parcel-bundler/watcher/issues/72
|
|
||||||
event->isDeleted = false;
|
|
||||||
} else {
|
|
||||||
event->isCreated = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Event *update(std::string path) {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
return internalUpdate(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove(std::string path) {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
Event *event = internalUpdate(path);
|
|
||||||
event->isDeleted = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
return mEvents.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Event> getEvents() {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
std::vector<Event> eventsCloneVector;
|
|
||||||
for(auto it = mEvents.begin(); it != mEvents.end(); ++it) {
|
|
||||||
if (!(it->second.isCreated && it->second.isDeleted)) {
|
|
||||||
eventsCloneVector.push_back(it->second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return eventsCloneVector;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear() {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
mEvents.clear();
|
|
||||||
mError.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void error(std::string err) {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
if (!mError.has_value()) {
|
|
||||||
mError.emplace(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool hasError() {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
return mError.has_value();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getError() {
|
|
||||||
std::lock_guard<std::mutex> l(mMutex);
|
|
||||||
return mError.value_or("");
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
mutable std::mutex mMutex;
|
|
||||||
std::map<std::string, Event> mEvents;
|
|
||||||
std::optional<std::string> mError;
|
|
||||||
Event *internalUpdate(std::string path) {
|
|
||||||
auto found = mEvents.find(path);
|
|
||||||
if (found == mEvents.end()) {
|
|
||||||
auto it = mEvents.emplace(path, Event(path));
|
|
||||||
return &it.first->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &found->second;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-22
@@ -1,22 +0,0 @@
|
|||||||
#include "Glob.hh"
|
|
||||||
|
|
||||||
#ifdef __wasm32__
|
|
||||||
extern "C" bool wasm_regex_match(const char *s, const char *regex);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Glob::Glob(std::string raw) {
|
|
||||||
mRaw = raw;
|
|
||||||
mHash = std::hash<std::string>()(raw);
|
|
||||||
#ifndef __wasm32__
|
|
||||||
mRegex = std::regex(raw);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Glob::isIgnored(std::string relative_path) const {
|
|
||||||
// Use native JS regex engine for wasm to reduce binary size.
|
|
||||||
#ifdef __wasm32__
|
|
||||||
return wasm_regex_match(relative_path.c_str(), mRaw.c_str());
|
|
||||||
#else
|
|
||||||
return std::regex_match(relative_path, mRegex);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
#ifndef GLOB_H
|
|
||||||
#define GLOB_H
|
|
||||||
|
|
||||||
#include <unordered_set>
|
|
||||||
#include <regex>
|
|
||||||
|
|
||||||
struct Glob {
|
|
||||||
std::size_t mHash;
|
|
||||||
std::string mRaw;
|
|
||||||
#ifndef __wasm32__
|
|
||||||
std::regex mRegex;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
Glob(std::string raw);
|
|
||||||
|
|
||||||
bool operator==(const Glob &other) const {
|
|
||||||
return mHash == other.mHash && mRaw == other.mRaw;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isIgnored(std::string relative_path) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace std
|
|
||||||
{
|
|
||||||
template <>
|
|
||||||
struct hash<Glob>
|
|
||||||
{
|
|
||||||
size_t operator()(const Glob& g) const {
|
|
||||||
return g.mHash;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
#ifndef PROMISE_RUNNER_H
|
|
||||||
#define PROMISE_RUNNER_H
|
|
||||||
|
|
||||||
#include <node_api.h>
|
|
||||||
#include "wasm/include.h"
|
|
||||||
#include <napi.h>
|
|
||||||
|
|
||||||
using namespace Napi;
|
|
||||||
|
|
||||||
class PromiseRunner {
|
|
||||||
public:
|
|
||||||
const Env env;
|
|
||||||
Promise::Deferred deferred;
|
|
||||||
|
|
||||||
PromiseRunner(Env env) : env(env), deferred(Promise::Deferred::New(env)) {
|
|
||||||
napi_status status = napi_create_async_work(env, nullptr, env.Undefined(),
|
|
||||||
onExecute, onWorkComplete, this, &work);
|
|
||||||
if (status != napi_ok) {
|
|
||||||
work = nullptr;
|
|
||||||
const napi_extended_error_info *error_info = 0;
|
|
||||||
napi_get_last_error_info(env, &error_info);
|
|
||||||
if (error_info->error_message) {
|
|
||||||
Error::New(env, error_info->error_message).ThrowAsJavaScriptException();
|
|
||||||
} else {
|
|
||||||
Error::New(env).ThrowAsJavaScriptException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~PromiseRunner() {}
|
|
||||||
|
|
||||||
Value queue() {
|
|
||||||
if (work) {
|
|
||||||
napi_status status = napi_queue_async_work(env, work);
|
|
||||||
if (status != napi_ok) {
|
|
||||||
onError(Error::New(env));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return deferred.Promise();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
napi_async_work work;
|
|
||||||
std::string error;
|
|
||||||
|
|
||||||
static void onExecute(napi_env env, void *this_pointer) {
|
|
||||||
PromiseRunner* self = (PromiseRunner*) this_pointer;
|
|
||||||
try {
|
|
||||||
self->execute();
|
|
||||||
} catch (std::exception &err) {
|
|
||||||
self->error = err.what();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void onWorkComplete(napi_env env, napi_status status, void *this_pointer) {
|
|
||||||
PromiseRunner* self = (PromiseRunner*) this_pointer;
|
|
||||||
if (status != napi_cancelled) {
|
|
||||||
HandleScope scope(self->env);
|
|
||||||
if (status == napi_ok) {
|
|
||||||
status = napi_delete_async_work(self->env, self->work);
|
|
||||||
if (status == napi_ok) {
|
|
||||||
if (self->error.size() == 0) {
|
|
||||||
self->onOK();
|
|
||||||
} else {
|
|
||||||
self->onError(Error::New(self->env, self->error));
|
|
||||||
}
|
|
||||||
delete self;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fallthrough for error handling
|
|
||||||
const napi_extended_error_info *error_info = 0;
|
|
||||||
napi_get_last_error_info(env, &error_info);
|
|
||||||
if (error_info->error_message){
|
|
||||||
self->onError(Error::New(env, error_info->error_message));
|
|
||||||
} else {
|
|
||||||
self->onError(Error::New(env));
|
|
||||||
}
|
|
||||||
delete self;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void execute() {}
|
|
||||||
virtual Value getResult() {
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
void onOK() {
|
|
||||||
HandleScope scope(env);
|
|
||||||
Value result = getResult();
|
|
||||||
deferred.Resolve(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
void onError(const Error &e) {
|
|
||||||
deferred.Reject(e.Value());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-46
@@ -1,46 +0,0 @@
|
|||||||
#ifndef SIGNAL_H
|
|
||||||
#define SIGNAL_H
|
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <condition_variable>
|
|
||||||
|
|
||||||
class Signal {
|
|
||||||
public:
|
|
||||||
Signal() : mFlag(false), mWaiting(false) {}
|
|
||||||
void wait() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
while (!mFlag) {
|
|
||||||
mWaiting = true;
|
|
||||||
mCond.wait(lock);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cv_status waitFor(std::chrono::milliseconds ms) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
return mCond.wait_for(lock, ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
void notify() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
mFlag = true;
|
|
||||||
mCond.notify_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
mFlag = false;
|
|
||||||
mWaiting = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isWaiting() {
|
|
||||||
return mWaiting;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool mFlag;
|
|
||||||
bool mWaiting;
|
|
||||||
std::mutex mMutex;
|
|
||||||
std::condition_variable mCond;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-241
@@ -1,241 +0,0 @@
|
|||||||
#include "Watcher.hh"
|
|
||||||
#include <unordered_set>
|
|
||||||
|
|
||||||
using namespace Napi;
|
|
||||||
|
|
||||||
struct WatcherHash {
|
|
||||||
std::size_t operator() (WatcherRef const &k) const {
|
|
||||||
return std::hash<std::string>()(k->mDir);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct WatcherCompare {
|
|
||||||
size_t operator() (WatcherRef const &a, WatcherRef const &b) const {
|
|
||||||
return *a == *b;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>& getSharedWatchers() {
|
|
||||||
static std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>* sharedWatchers =
|
|
||||||
new std::unordered_set<WatcherRef , WatcherHash, WatcherCompare>();
|
|
||||||
return *sharedWatchers;
|
|
||||||
}
|
|
||||||
|
|
||||||
WatcherRef Watcher::getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs) {
|
|
||||||
WatcherRef watcher = std::make_shared<Watcher>(dir, ignorePaths, ignoreGlobs);
|
|
||||||
auto found = getSharedWatchers().find(watcher);
|
|
||||||
if (found != getSharedWatchers().end()) {
|
|
||||||
return *found;
|
|
||||||
}
|
|
||||||
|
|
||||||
getSharedWatchers().insert(watcher);
|
|
||||||
return watcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeShared(Watcher *watcher) {
|
|
||||||
for (auto it = getSharedWatchers().begin(); it != getSharedWatchers().end(); it++) {
|
|
||||||
if (it->get() == watcher) {
|
|
||||||
getSharedWatchers().erase(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Free up memory.
|
|
||||||
if (getSharedWatchers().size() == 0) {
|
|
||||||
getSharedWatchers().rehash(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Watcher::Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs)
|
|
||||||
: mDir(dir),
|
|
||||||
mIgnorePaths(ignorePaths),
|
|
||||||
mIgnoreGlobs(ignoreGlobs) {
|
|
||||||
mDebounce = Debounce::getShared();
|
|
||||||
mDebounce->add(this, [this] () {
|
|
||||||
triggerCallbacks();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Watcher::~Watcher() {
|
|
||||||
mDebounce->remove(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Watcher::wait() {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
mCond.wait(lk);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Watcher::notify() {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
mCond.notify_all();
|
|
||||||
|
|
||||||
if (mCallbacks.size() > 0 && mEvents.size() > 0) {
|
|
||||||
// We must release our lock before calling into the debouncer
|
|
||||||
// to avoid a deadlock: the debouncer thread itself will require
|
|
||||||
// our lock from its thread when calling into `triggerCallbacks`
|
|
||||||
// while holding its own debouncer lock.
|
|
||||||
lk.unlock();
|
|
||||||
mDebounce->trigger();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CallbackData {
|
|
||||||
std::string error;
|
|
||||||
std::vector<Event> events;
|
|
||||||
CallbackData(std::string error, std::vector<Event> events) : error(error), events(events) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
Value callbackEventsToJS(const Env &env, std::vector<Event> &events) {
|
|
||||||
EscapableHandleScope scope(env);
|
|
||||||
Array arr = Array::New(env, events.size());
|
|
||||||
uint32_t currentEventIndex = 0;
|
|
||||||
for (auto eventIterator = events.begin(); eventIterator != events.end(); eventIterator++) {
|
|
||||||
arr.Set(currentEventIndex++, eventIterator->toJS(env));
|
|
||||||
}
|
|
||||||
return scope.Escape(arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void callJSFunction(Napi::Env env, Function jsCallback, CallbackData *data) {
|
|
||||||
HandleScope scope(env);
|
|
||||||
auto err = data->error.size() > 0 ? Error::New(env, data->error).Value() : env.Null();
|
|
||||||
auto events = callbackEventsToJS(env, data->events);
|
|
||||||
jsCallback.Call({err, events});
|
|
||||||
delete data;
|
|
||||||
|
|
||||||
// Throw errors from the callback as fatal exceptions
|
|
||||||
// If we don't handle these node segfaults...
|
|
||||||
if (env.IsExceptionPending()) {
|
|
||||||
Napi::Error err = env.GetAndClearPendingException();
|
|
||||||
napi_fatal_exception(env, err.Value());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Watcher::notifyError(std::exception &err) {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
||||||
CallbackData *data = new CallbackData(err.what(), {});
|
|
||||||
it->tsfn.BlockingCall(data, callJSFunction);
|
|
||||||
}
|
|
||||||
|
|
||||||
clearCallbacks();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called from the debounce thread.
|
|
||||||
void Watcher::triggerCallbacks() {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
if (mCallbacks.size() > 0 && (mEvents.size() > 0 || mEvents.hasError())) {
|
|
||||||
auto error = mEvents.getError();
|
|
||||||
auto events = mEvents.getEvents();
|
|
||||||
mEvents.clear();
|
|
||||||
|
|
||||||
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
||||||
it->tsfn.BlockingCall(new CallbackData(error, events), callJSFunction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This should be called from the JavaScript thread.
|
|
||||||
bool Watcher::watch(Function callback) {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
|
|
||||||
auto it = findCallback(callback);
|
|
||||||
if (it != mCallbacks.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto tsfn = ThreadSafeFunction::New(
|
|
||||||
callback.Env(),
|
|
||||||
callback,
|
|
||||||
"Watcher callback",
|
|
||||||
0, // Unlimited queue
|
|
||||||
1 // Initial thread count
|
|
||||||
);
|
|
||||||
|
|
||||||
mCallbacks.push_back(Callback {
|
|
||||||
tsfn,
|
|
||||||
Napi::Persistent(callback),
|
|
||||||
std::this_thread::get_id()
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This should be called from the JavaScript thread.
|
|
||||||
std::vector<Callback>::iterator Watcher::findCallback(Function callback) {
|
|
||||||
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
||||||
// Only consider callbacks created by the same thread, or V8 will panic.
|
|
||||||
if (it->threadId == std::this_thread::get_id() && it->ref.Value() == callback) {
|
|
||||||
return it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return mCallbacks.end();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This should be called from the JavaScript thread.
|
|
||||||
bool Watcher::unwatch(Function callback) {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
|
|
||||||
bool removed = false;
|
|
||||||
auto it = findCallback(callback);
|
|
||||||
if (it != mCallbacks.end()) {
|
|
||||||
it->tsfn.Release();
|
|
||||||
it->ref.Unref();
|
|
||||||
mCallbacks.erase(it);
|
|
||||||
removed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (removed && mCallbacks.size() == 0) {
|
|
||||||
unref();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Watcher::unref() {
|
|
||||||
if (mCallbacks.size() == 0) {
|
|
||||||
removeShared(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Watcher::destroy() {
|
|
||||||
std::unique_lock<std::mutex> lk(mMutex);
|
|
||||||
clearCallbacks();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Private because it doesn't lock.
|
|
||||||
void Watcher::clearCallbacks() {
|
|
||||||
for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++) {
|
|
||||||
it->tsfn.Release();
|
|
||||||
it->ref.Unref();
|
|
||||||
}
|
|
||||||
|
|
||||||
mCallbacks.clear();
|
|
||||||
unref();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Watcher::isIgnored(std::string path) {
|
|
||||||
for (auto it = mIgnorePaths.begin(); it != mIgnorePaths.end(); it++) {
|
|
||||||
auto dir = *it + DIR_SEP;
|
|
||||||
if (*it == path || path.compare(0, dir.size(), dir) == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto basePath = mDir + DIR_SEP;
|
|
||||||
|
|
||||||
if (path.rfind(basePath, 0) != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto relativePath = path.substr(basePath.size());
|
|
||||||
|
|
||||||
for (auto it = mIgnoreGlobs.begin(); it != mIgnoreGlobs.end(); it++) {
|
|
||||||
if (it->isIgnored(relativePath)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
-73
@@ -1,73 +0,0 @@
|
|||||||
#ifndef WATCHER_H
|
|
||||||
#define WATCHER_H
|
|
||||||
|
|
||||||
#include <condition_variable>
|
|
||||||
#include <unordered_set>
|
|
||||||
#include <set>
|
|
||||||
#include <node_api.h>
|
|
||||||
#include "Glob.hh"
|
|
||||||
#include "Event.hh"
|
|
||||||
#include "Debounce.hh"
|
|
||||||
#include "DirTree.hh"
|
|
||||||
#include "Signal.hh"
|
|
||||||
|
|
||||||
using namespace Napi;
|
|
||||||
|
|
||||||
struct Watcher;
|
|
||||||
using WatcherRef = std::shared_ptr<Watcher>;
|
|
||||||
|
|
||||||
struct Callback {
|
|
||||||
Napi::ThreadSafeFunction tsfn;
|
|
||||||
Napi::FunctionReference ref;
|
|
||||||
std::thread::id threadId;
|
|
||||||
};
|
|
||||||
|
|
||||||
class WatcherState {
|
|
||||||
public:
|
|
||||||
virtual ~WatcherState() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Watcher {
|
|
||||||
std::string mDir;
|
|
||||||
std::unordered_set<std::string> mIgnorePaths;
|
|
||||||
std::unordered_set<Glob> mIgnoreGlobs;
|
|
||||||
EventList mEvents;
|
|
||||||
std::shared_ptr<WatcherState> state;
|
|
||||||
|
|
||||||
Watcher(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
|
|
||||||
~Watcher();
|
|
||||||
|
|
||||||
bool operator==(const Watcher &other) const {
|
|
||||||
return mDir == other.mDir && mIgnorePaths == other.mIgnorePaths && mIgnoreGlobs == other.mIgnoreGlobs;
|
|
||||||
}
|
|
||||||
|
|
||||||
void wait();
|
|
||||||
void notify();
|
|
||||||
void notifyError(std::exception &err);
|
|
||||||
bool watch(Function callback);
|
|
||||||
bool unwatch(Function callback);
|
|
||||||
void unref();
|
|
||||||
bool isIgnored(std::string path);
|
|
||||||
void destroy();
|
|
||||||
|
|
||||||
static WatcherRef getShared(std::string dir, std::unordered_set<std::string> ignorePaths, std::unordered_set<Glob> ignoreGlobs);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::mutex mMutex;
|
|
||||||
std::condition_variable mCond;
|
|
||||||
std::vector<Callback> mCallbacks;
|
|
||||||
std::shared_ptr<Debounce> mDebounce;
|
|
||||||
|
|
||||||
std::vector<Callback>::iterator findCallback(Function callback);
|
|
||||||
void clearCallbacks();
|
|
||||||
void triggerCallbacks();
|
|
||||||
};
|
|
||||||
|
|
||||||
class WatcherError : public std::runtime_error {
|
|
||||||
public:
|
|
||||||
WatcherRef mWatcher;
|
|
||||||
WatcherError(std::string msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
|
|
||||||
WatcherError(const char *msg, WatcherRef watcher) : std::runtime_error(msg), mWatcher(watcher) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-268
@@ -1,268 +0,0 @@
|
|||||||
#include <unordered_set>
|
|
||||||
#include <node_api.h>
|
|
||||||
#include "wasm/include.h"
|
|
||||||
#include <napi.h>
|
|
||||||
#include "Glob.hh"
|
|
||||||
#include "Event.hh"
|
|
||||||
#include "Backend.hh"
|
|
||||||
#include "Watcher.hh"
|
|
||||||
#include "PromiseRunner.hh"
|
|
||||||
|
|
||||||
using namespace Napi;
|
|
||||||
|
|
||||||
std::unordered_set<std::string> getIgnorePaths(Env env, Value opts) {
|
|
||||||
std::unordered_set<std::string> result;
|
|
||||||
|
|
||||||
if (opts.IsObject()) {
|
|
||||||
Value v = opts.As<Object>().Get(String::New(env, "ignorePaths"));
|
|
||||||
if (v.IsArray()) {
|
|
||||||
Array items = v.As<Array>();
|
|
||||||
for (size_t i = 0; i < items.Length(); i++) {
|
|
||||||
Value item = items.Get(Number::New(env, static_cast<double>(i)));
|
|
||||||
if (item.IsString()) {
|
|
||||||
result.insert(std::string(item.As<String>().Utf8Value().c_str()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unordered_set<Glob> getIgnoreGlobs(Env env, Value opts) {
|
|
||||||
std::unordered_set<Glob> result;
|
|
||||||
|
|
||||||
if (opts.IsObject()) {
|
|
||||||
Value v = opts.As<Object>().Get(String::New(env, "ignoreGlobs"));
|
|
||||||
if (v.IsArray()) {
|
|
||||||
Array items = v.As<Array>();
|
|
||||||
for (size_t i = 0; i < items.Length(); i++) {
|
|
||||||
Value item = items.Get(Number::New(env, static_cast<double>(i)));
|
|
||||||
if (item.IsString()) {
|
|
||||||
auto key = item.As<String>().Utf8Value();
|
|
||||||
try {
|
|
||||||
result.emplace(key);
|
|
||||||
} catch (const std::regex_error& e) {
|
|
||||||
Error::New(env, e.what()).ThrowAsJavaScriptException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Backend> getBackend(Env env, Value opts) {
|
|
||||||
Value b = opts.As<Object>().Get(String::New(env, "backend"));
|
|
||||||
std::string backendName;
|
|
||||||
if (b.IsString()) {
|
|
||||||
backendName = std::string(b.As<String>().Utf8Value().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Backend::getShared(backendName);
|
|
||||||
}
|
|
||||||
|
|
||||||
class WriteSnapshotRunner : public PromiseRunner {
|
|
||||||
public:
|
|
||||||
WriteSnapshotRunner(Env env, Value dir, Value snap, Value opts)
|
|
||||||
: PromiseRunner(env),
|
|
||||||
snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
|
|
||||||
watcher = Watcher::getShared(
|
|
||||||
std::string(dir.As<String>().Utf8Value().c_str()),
|
|
||||||
getIgnorePaths(env, opts),
|
|
||||||
getIgnoreGlobs(env, opts)
|
|
||||||
);
|
|
||||||
|
|
||||||
backend = getBackend(env, opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
~WriteSnapshotRunner() {
|
|
||||||
watcher->unref();
|
|
||||||
backend->unref();
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
std::shared_ptr<Backend> backend;
|
|
||||||
WatcherRef watcher;
|
|
||||||
std::string snapshotPath;
|
|
||||||
|
|
||||||
void execute() override {
|
|
||||||
backend->writeSnapshot(watcher, &snapshotPath);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class GetEventsSinceRunner : public PromiseRunner {
|
|
||||||
public:
|
|
||||||
GetEventsSinceRunner(Env env, Value dir, Value snap, Value opts)
|
|
||||||
: PromiseRunner(env),
|
|
||||||
snapshotPath(std::string(snap.As<String>().Utf8Value().c_str())) {
|
|
||||||
watcher = std::make_shared<Watcher>(
|
|
||||||
std::string(dir.As<String>().Utf8Value().c_str()),
|
|
||||||
getIgnorePaths(env, opts),
|
|
||||||
getIgnoreGlobs(env, opts)
|
|
||||||
);
|
|
||||||
|
|
||||||
backend = getBackend(env, opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
~GetEventsSinceRunner() {
|
|
||||||
watcher->unref();
|
|
||||||
backend->unref();
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
std::shared_ptr<Backend> backend;
|
|
||||||
WatcherRef watcher;
|
|
||||||
std::string snapshotPath;
|
|
||||||
|
|
||||||
void execute() override {
|
|
||||||
backend->getEventsSince(watcher, &snapshotPath);
|
|
||||||
if (watcher->mEvents.hasError()) {
|
|
||||||
throw std::runtime_error(watcher->mEvents.getError());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Value getResult() override {
|
|
||||||
std::vector<Event> events = watcher->mEvents.getEvents();
|
|
||||||
Array eventsArray = Array::New(env, events.size());
|
|
||||||
uint32_t i = 0;
|
|
||||||
for (auto it = events.begin(); it != events.end(); it++) {
|
|
||||||
eventsArray.Set(i++, it->toJS(env));
|
|
||||||
}
|
|
||||||
return eventsArray;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class Runner>
|
|
||||||
Value queueSnapshotWork(const CallbackInfo& info) {
|
|
||||||
Env env = info.Env();
|
|
||||||
if (info.Length() < 1 || !info[0].IsString()) {
|
|
||||||
TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.Length() < 2 || !info[1].IsString()) {
|
|
||||||
TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.Length() >= 3 && !info[2].IsObject()) {
|
|
||||||
TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
|
|
||||||
return runner->queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
Value writeSnapshot(const CallbackInfo& info) {
|
|
||||||
return queueSnapshotWork<WriteSnapshotRunner>(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
Value getEventsSince(const CallbackInfo& info) {
|
|
||||||
return queueSnapshotWork<GetEventsSinceRunner>(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
class SubscribeRunner : public PromiseRunner {
|
|
||||||
public:
|
|
||||||
SubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
|
|
||||||
watcher = Watcher::getShared(
|
|
||||||
std::string(dir.As<String>().Utf8Value().c_str()),
|
|
||||||
getIgnorePaths(env, opts),
|
|
||||||
getIgnoreGlobs(env, opts)
|
|
||||||
);
|
|
||||||
|
|
||||||
backend = getBackend(env, opts);
|
|
||||||
watcher->watch(fn.As<Function>());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
WatcherRef watcher;
|
|
||||||
std::shared_ptr<Backend> backend;
|
|
||||||
FunctionReference callback;
|
|
||||||
|
|
||||||
void execute() override {
|
|
||||||
try {
|
|
||||||
backend->watch(watcher);
|
|
||||||
} catch (std::exception&) {
|
|
||||||
watcher->destroy();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class UnsubscribeRunner : public PromiseRunner {
|
|
||||||
public:
|
|
||||||
UnsubscribeRunner(Env env, Value dir, Value fn, Value opts) : PromiseRunner(env) {
|
|
||||||
watcher = Watcher::getShared(
|
|
||||||
std::string(dir.As<String>().Utf8Value().c_str()),
|
|
||||||
getIgnorePaths(env, opts),
|
|
||||||
getIgnoreGlobs(env, opts)
|
|
||||||
);
|
|
||||||
|
|
||||||
backend = getBackend(env, opts);
|
|
||||||
shouldUnwatch = watcher->unwatch(fn.As<Function>());
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
WatcherRef watcher;
|
|
||||||
std::shared_ptr<Backend> backend;
|
|
||||||
bool shouldUnwatch;
|
|
||||||
|
|
||||||
void execute() override {
|
|
||||||
if (shouldUnwatch) {
|
|
||||||
backend->unwatch(watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<class Runner>
|
|
||||||
Value queueSubscriptionWork(const CallbackInfo& info) {
|
|
||||||
Env env = info.Env();
|
|
||||||
if (info.Length() < 1 || !info[0].IsString()) {
|
|
||||||
TypeError::New(env, "Expected a string").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.Length() < 2 || !info[1].IsFunction()) {
|
|
||||||
TypeError::New(env, "Expected a function").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (info.Length() >= 3 && !info[2].IsObject()) {
|
|
||||||
TypeError::New(env, "Expected an object").ThrowAsJavaScriptException();
|
|
||||||
return env.Null();
|
|
||||||
}
|
|
||||||
|
|
||||||
Runner *runner = new Runner(info.Env(), info[0], info[1], info[2]);
|
|
||||||
return runner->queue();
|
|
||||||
}
|
|
||||||
|
|
||||||
Value subscribe(const CallbackInfo& info) {
|
|
||||||
return queueSubscriptionWork<SubscribeRunner>(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
Value unsubscribe(const CallbackInfo& info) {
|
|
||||||
return queueSubscriptionWork<UnsubscribeRunner>(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object Init(Env env, Object exports) {
|
|
||||||
exports.Set(
|
|
||||||
String::New(env, "writeSnapshot"),
|
|
||||||
Function::New(env, writeSnapshot)
|
|
||||||
);
|
|
||||||
exports.Set(
|
|
||||||
String::New(env, "getEventsSince"),
|
|
||||||
Function::New(env, getEventsSince)
|
|
||||||
);
|
|
||||||
exports.Set(
|
|
||||||
String::New(env, "subscribe"),
|
|
||||||
Function::New(env, subscribe)
|
|
||||||
);
|
|
||||||
exports.Set(
|
|
||||||
String::New(env, "unsubscribe"),
|
|
||||||
Function::New(env, unsubscribe)
|
|
||||||
);
|
|
||||||
return exports;
|
|
||||||
}
|
|
||||||
|
|
||||||
NODE_API_MODULE(watcher, Init)
|
|
||||||
-306
@@ -1,306 +0,0 @@
|
|||||||
#include <memory>
|
|
||||||
#include <poll.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <libgen.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "KqueueBackend.hh"
|
|
||||||
|
|
||||||
#if __APPLE__
|
|
||||||
#define st_mtim st_mtimespec
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(O_EVTONLY)
|
|
||||||
#define O_EVTONLY O_RDONLY
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
|
|
||||||
void KqueueBackend::start() {
|
|
||||||
if ((mKqueue = kqueue()) < 0) {
|
|
||||||
throw std::runtime_error(std::string("Unable to open kqueue: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a pipe that we will write to when we want to end the thread.
|
|
||||||
int err = pipe(mPipe);
|
|
||||||
if (err == -1) {
|
|
||||||
throw std::runtime_error(std::string("Unable to open pipe: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe kqueue to this pipe.
|
|
||||||
struct kevent ev;
|
|
||||||
EV_SET(
|
|
||||||
&ev,
|
|
||||||
mPipe[0],
|
|
||||||
EVFILT_READ,
|
|
||||||
EV_ADD | EV_CLEAR,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
if (kevent(mKqueue, &ev, 1, NULL, 0, 0)) {
|
|
||||||
close(mPipe[0]);
|
|
||||||
close(mPipe[1]);
|
|
||||||
throw std::runtime_error(std::string("Unable to watch pipe: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
notifyStarted();
|
|
||||||
|
|
||||||
struct kevent events[128];
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
int event_count = kevent(mKqueue, NULL, 0, events, 128, 0);
|
|
||||||
if (event_count < 0 || events[0].flags == EV_ERROR) {
|
|
||||||
throw std::runtime_error(std::string("kevent error: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Track all of the watchers that are touched so we can notify them at the end of the events.
|
|
||||||
std::unordered_set<WatcherRef> watchers;
|
|
||||||
|
|
||||||
for (int i = 0; i < event_count; i++) {
|
|
||||||
int flags = events[i].fflags;
|
|
||||||
int fd = events[i].ident;
|
|
||||||
if (fd == mPipe[0]) {
|
|
||||||
// pipe was written to. break out of the loop.
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto it = mFdToEntry.find(fd);
|
|
||||||
if (it == mFdToEntry.end()) {
|
|
||||||
// If fd wasn't in our map, we may have already stopped watching it. Ignore the event.
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry *entry = it->second;
|
|
||||||
|
|
||||||
if (flags & NOTE_WRITE && entry && entry->isDir) {
|
|
||||||
// If a write occurred on a directory, we have to diff the contents of that
|
|
||||||
// directory to determine what file was added/deleted.
|
|
||||||
compareDir(fd, entry->path, watchers);
|
|
||||||
} else {
|
|
||||||
std::vector<KqueueSubscription *> subs = findSubscriptions(entry->path);
|
|
||||||
for (auto it = subs.begin(); it != subs.end(); it++) {
|
|
||||||
KqueueSubscription *sub = *it;
|
|
||||||
watchers.insert(sub->watcher);
|
|
||||||
if (flags & (NOTE_DELETE | NOTE_RENAME | NOTE_REVOKE)) {
|
|
||||||
sub->watcher->mEvents.remove(sub->path);
|
|
||||||
sub->tree->remove(sub->path);
|
|
||||||
mFdToEntry.erase((int)(size_t)entry->state);
|
|
||||||
mSubscriptions.erase(sub->path);
|
|
||||||
} else if (flags & (NOTE_WRITE | NOTE_ATTRIB | NOTE_EXTEND)) {
|
|
||||||
struct stat st;
|
|
||||||
lstat(sub->path.c_str(), &st);
|
|
||||||
if (entry->mtime != CONVERT_TIME(st.st_mtim)) {
|
|
||||||
entry->mtime = CONVERT_TIME(st.st_mtim);
|
|
||||||
sub->watcher->mEvents.update(sub->path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = watchers.begin(); it != watchers.end(); it++) {
|
|
||||||
(*it)->notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
done:
|
|
||||||
close(mPipe[0]);
|
|
||||||
close(mPipe[1]);
|
|
||||||
mEndedSignal.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
KqueueBackend::~KqueueBackend() {
|
|
||||||
write(mPipe[1], "X", 1);
|
|
||||||
mEndedSignal.wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
void KqueueBackend::subscribe(WatcherRef watcher) {
|
|
||||||
// Build a full directory tree recursively, and watch each directory.
|
|
||||||
std::shared_ptr<DirTree> tree = getTree(watcher);
|
|
||||||
|
|
||||||
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
|
|
||||||
bool success = watchDir(watcher, it->second.path, tree);
|
|
||||||
if (!success) {
|
|
||||||
throw WatcherError(std::string("error watching " + watcher->mDir + ": " + strerror(errno)), watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool KqueueBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
|
|
||||||
if (watcher->isIgnored(path)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
DirEntry *entry = tree->find(path);
|
|
||||||
if (!entry) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
KqueueSubscription sub = {
|
|
||||||
.watcher = watcher,
|
|
||||||
.path = path,
|
|
||||||
.tree = tree
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!entry->state) {
|
|
||||||
int fd = open(path.c_str(), O_EVTONLY);
|
|
||||||
if (fd <= 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct kevent event;
|
|
||||||
EV_SET(
|
|
||||||
&event,
|
|
||||||
fd,
|
|
||||||
EVFILT_VNODE,
|
|
||||||
EV_ADD | EV_CLEAR | EV_ENABLE,
|
|
||||||
NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | NOTE_RENAME | NOTE_REVOKE,
|
|
||||||
0,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
if (kevent(mKqueue, &event, 1, NULL, 0, 0)) {
|
|
||||||
close(fd);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
entry->state = (void *)(size_t)fd;
|
|
||||||
mFdToEntry.emplace(fd, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
sub.fd = (int)(size_t)entry->state;
|
|
||||||
mSubscriptions.emplace(path, sub);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<KqueueSubscription *> KqueueBackend::findSubscriptions(std::string &path) {
|
|
||||||
// Find the subscriptions affected by this path.
|
|
||||||
// Copy pointers to them into a vector so that modifying mSubscriptions doesn't invalidate the iterator.
|
|
||||||
auto range = mSubscriptions.equal_range(path);
|
|
||||||
std::vector<KqueueSubscription *> subs;
|
|
||||||
for (auto it = range.first; it != range.second; it++) {
|
|
||||||
subs.push_back(&it->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
return subs;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool KqueueBackend::compareDir(int fd, std::string &path, std::unordered_set<WatcherRef> &watchers) {
|
|
||||||
// macOS doesn't support fdclosedir, so we have to duplicate the file descriptor
|
|
||||||
// to ensure the closedir doesn't also stop watching.
|
|
||||||
#if __APPLE__
|
|
||||||
fd = dup(fd);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
DIR *dir = fdopendir(fd);
|
|
||||||
if (dir == NULL) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// fdopendir doesn't rewind to the beginning.
|
|
||||||
rewinddir(dir);
|
|
||||||
|
|
||||||
std::vector<KqueueSubscription *> subs = findSubscriptions(path);
|
|
||||||
std::string dirStart = path + DIR_SEP;
|
|
||||||
|
|
||||||
std::unordered_set<std::shared_ptr<DirTree>> trees;
|
|
||||||
for (auto it = subs.begin(); it != subs.end(); it++) {
|
|
||||||
trees.emplace((*it)->tree);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unordered_set<std::string> entries;
|
|
||||||
struct dirent *entry;
|
|
||||||
while ((entry = readdir(dir))) {
|
|
||||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string fullpath = dirStart + entry->d_name;
|
|
||||||
entries.emplace(fullpath);
|
|
||||||
|
|
||||||
for (auto it = trees.begin(); it != trees.end(); it++) {
|
|
||||||
std::shared_ptr<DirTree> tree = *it;
|
|
||||||
if (!tree->find(fullpath)) {
|
|
||||||
struct stat st;
|
|
||||||
fstatat(fd, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);
|
|
||||||
tree->add(fullpath, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
|
|
||||||
|
|
||||||
// Notify all watchers with the same tree.
|
|
||||||
for (auto i = subs.begin(); i != subs.end(); i++) {
|
|
||||||
KqueueSubscription *sub = *i;
|
|
||||||
if (sub->tree == tree) {
|
|
||||||
if (sub->watcher->isIgnored(fullpath)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
sub->watcher->mEvents.create(fullpath);
|
|
||||||
watchers.emplace(sub->watcher);
|
|
||||||
|
|
||||||
bool success = watchDir(sub->watcher, fullpath, sub->tree);
|
|
||||||
if (!success) {
|
|
||||||
sub->tree->remove(fullpath);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = trees.begin(); it != trees.end(); it++) {
|
|
||||||
std::shared_ptr<DirTree> tree = *it;
|
|
||||||
for (auto entry = tree->entries.begin(); entry != tree->entries.end();) {
|
|
||||||
|
|
||||||
if (
|
|
||||||
entry->first.rfind(dirStart, 0) == 0 &&
|
|
||||||
entry->first.find(DIR_SEP, dirStart.length()) == std::string::npos &&
|
|
||||||
entries.count(entry->first) == 0
|
|
||||||
) {
|
|
||||||
// Notify all watchers with the same tree.
|
|
||||||
for (auto i = subs.begin(); i != subs.end(); i++) {
|
|
||||||
if ((*i)->tree == tree) {
|
|
||||||
KqueueSubscription *sub = *i;
|
|
||||||
if (!sub->watcher->isIgnored(entry->first)) {
|
|
||||||
sub->watcher->mEvents.remove(entry->first);
|
|
||||||
watchers.emplace(sub->watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mFdToEntry.erase((int)(size_t)entry->second.state);
|
|
||||||
mSubscriptions.erase(entry->first);
|
|
||||||
entry = tree->entries.erase(entry);
|
|
||||||
} else {
|
|
||||||
entry++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if __APPLE__
|
|
||||||
closedir(dir);
|
|
||||||
#else
|
|
||||||
fdclosedir(dir);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void KqueueBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
// Find any subscriptions pointing to this watcher, and remove them.
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
||||||
if (it->second.watcher.get() == watcher.get()) {
|
|
||||||
if (mSubscriptions.count(it->first) == 1) {
|
|
||||||
// Closing the file descriptor automatically unwatches it in the kqueue.
|
|
||||||
close(it->second.fd);
|
|
||||||
mFdToEntry.erase(it->second.fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
it = mSubscriptions.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
#ifndef KQUEUE_H
|
|
||||||
#define KQUEUE_H
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <sys/event.h>
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../Signal.hh"
|
|
||||||
|
|
||||||
struct KqueueSubscription {
|
|
||||||
WatcherRef watcher;
|
|
||||||
std::string path;
|
|
||||||
std::shared_ptr<DirTree> tree;
|
|
||||||
int fd;
|
|
||||||
};
|
|
||||||
|
|
||||||
class KqueueBackend : public BruteForceBackend {
|
|
||||||
public:
|
|
||||||
void start() override;
|
|
||||||
~KqueueBackend();
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
private:
|
|
||||||
int mKqueue;
|
|
||||||
int mPipe[2];
|
|
||||||
std::unordered_multimap<std::string, KqueueSubscription> mSubscriptions;
|
|
||||||
std::unordered_map<int, DirEntry *> mFdToEntry;
|
|
||||||
Signal mEndedSignal;
|
|
||||||
|
|
||||||
bool watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
|
|
||||||
bool compareDir(int fd, std::string &dir, std::unordered_set<WatcherRef> &watchers);
|
|
||||||
std::vector<KqueueSubscription *> findSubscriptions(std::string &path);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-236
@@ -1,236 +0,0 @@
|
|||||||
#include <memory>
|
|
||||||
#include <poll.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "InotifyBackend.hh"
|
|
||||||
|
|
||||||
#define INOTIFY_MASK \
|
|
||||||
IN_ATTRIB | IN_CREATE | IN_DELETE | \
|
|
||||||
IN_DELETE_SELF | IN_MODIFY | IN_MOVE_SELF | IN_MOVED_FROM | \
|
|
||||||
IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR | IN_EXCL_UNLINK
|
|
||||||
#define BUFFER_SIZE 8192
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
|
|
||||||
void InotifyBackend::start() {
|
|
||||||
// Create a pipe that we will write to when we want to end the thread.
|
|
||||||
int err = pipe2(mPipe, O_CLOEXEC | O_NONBLOCK);
|
|
||||||
if (err == -1) {
|
|
||||||
throw std::runtime_error(std::string("Unable to open pipe: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init inotify file descriptor.
|
|
||||||
mInotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
|
||||||
if (mInotify == -1) {
|
|
||||||
throw std::runtime_error(std::string("Unable to initialize inotify: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
pollfd pollfds[2];
|
|
||||||
pollfds[0].fd = mPipe[0];
|
|
||||||
pollfds[0].events = POLLIN;
|
|
||||||
pollfds[0].revents = 0;
|
|
||||||
pollfds[1].fd = mInotify;
|
|
||||||
pollfds[1].events = POLLIN;
|
|
||||||
pollfds[1].revents = 0;
|
|
||||||
|
|
||||||
notifyStarted();
|
|
||||||
|
|
||||||
// Loop until we get an event from the pipe.
|
|
||||||
while (true) {
|
|
||||||
int result = poll(pollfds, 2, 500);
|
|
||||||
if (result < 0) {
|
|
||||||
throw std::runtime_error(std::string("Unable to poll: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pollfds[0].revents) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pollfds[1].revents) {
|
|
||||||
handleEvents();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close(mPipe[0]);
|
|
||||||
close(mPipe[1]);
|
|
||||||
close(mInotify);
|
|
||||||
|
|
||||||
mEndedSignal.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
InotifyBackend::~InotifyBackend() {
|
|
||||||
write(mPipe[1], "X", 1);
|
|
||||||
mEndedSignal.wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::watch which takes a lock on mMutex
|
|
||||||
void InotifyBackend::subscribe(WatcherRef watcher) {
|
|
||||||
// Build a full directory tree recursively, and watch each directory.
|
|
||||||
std::shared_ptr<DirTree> tree = getTree(watcher);
|
|
||||||
|
|
||||||
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
|
|
||||||
if (it->second.isDir) {
|
|
||||||
bool success = watchDir(watcher, it->second.path, tree);
|
|
||||||
if (!success) {
|
|
||||||
throw WatcherError(std::string("inotify_add_watch on '") + it->second.path + std::string("' failed: ") + strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool InotifyBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
|
|
||||||
int wd = inotify_add_watch(mInotify, path.c_str(), INOTIFY_MASK);
|
|
||||||
if (wd == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<InotifySubscription> sub = std::make_shared<InotifySubscription>();
|
|
||||||
sub->tree = tree;
|
|
||||||
sub->path = path;
|
|
||||||
sub->watcher = watcher;
|
|
||||||
mSubscriptions.emplace(wd, sub);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void InotifyBackend::handleEvents() {
|
|
||||||
char buf[BUFFER_SIZE] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
|
|
||||||
struct inotify_event *event;
|
|
||||||
|
|
||||||
// Track all of the watchers that are touched so we can notify them at the end of the events.
|
|
||||||
std::unordered_set<WatcherRef> watchers;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
int n = read(mInotify, &buf, BUFFER_SIZE);
|
|
||||||
if (n < 0) {
|
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error(std::string("Error reading from inotify: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (char *ptr = buf; ptr < buf + n; ptr += sizeof(*event) + event->len) {
|
|
||||||
event = (struct inotify_event *)ptr;
|
|
||||||
|
|
||||||
if ((event->mask & IN_Q_OVERFLOW) == IN_Q_OVERFLOW) {
|
|
||||||
// overflow
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
handleEvent(event, watchers);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = watchers.begin(); it != watchers.end(); it++) {
|
|
||||||
(*it)->notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void InotifyBackend::handleEvent(struct inotify_event *event, std::unordered_set<WatcherRef> &watchers) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
|
|
||||||
// Find the subscriptions for this watch descriptor
|
|
||||||
auto range = mSubscriptions.equal_range(event->wd);
|
|
||||||
std::unordered_set<std::shared_ptr<InotifySubscription>> set;
|
|
||||||
for (auto it = range.first; it != range.second; it++) {
|
|
||||||
set.insert(it->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = set.begin(); it != set.end(); it++) {
|
|
||||||
if (handleSubscription(event, *it)) {
|
|
||||||
watchers.insert((*it)->watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool InotifyBackend::handleSubscription(struct inotify_event *event, std::shared_ptr<InotifySubscription> sub) {
|
|
||||||
// Build full path and check if its in our ignore list.
|
|
||||||
std::shared_ptr<Watcher> watcher = sub->watcher;
|
|
||||||
std::string path = std::string(sub->path);
|
|
||||||
bool isDir = event->mask & IN_ISDIR;
|
|
||||||
|
|
||||||
if (event->len > 0) {
|
|
||||||
path += "/" + std::string(event->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (watcher->isIgnored(path)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this is a create, check if it's a directory and start watching if it is.
|
|
||||||
// In any case, keep the directory tree up to date.
|
|
||||||
if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
|
|
||||||
watcher->mEvents.create(path);
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
// Use lstat to avoid resolving symbolic links that we cannot watch anyway
|
|
||||||
// https://github.com/parcel-bundler/watcher/issues/76
|
|
||||||
if (lstat(path.c_str(), &st) != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
|
|
||||||
|
|
||||||
if (entry->isDir) {
|
|
||||||
bool success = watchDir(watcher, path, sub->tree);
|
|
||||||
if (!success) {
|
|
||||||
sub->tree->remove(path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (event->mask & (IN_MODIFY | IN_ATTRIB)) {
|
|
||||||
watcher->mEvents.update(path);
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if (stat(path.c_str(), &st) != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
|
||||||
} else if (event->mask & (IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM | IN_MOVE_SELF)) {
|
|
||||||
bool isSelfEvent = (event->mask & (IN_DELETE_SELF | IN_MOVE_SELF));
|
|
||||||
// Ignore delete/move self events unless this is the recursive watch root
|
|
||||||
if (isSelfEvent && path != watcher->mDir) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the entry being deleted/moved is a directory, remove it from the list of subscriptions
|
|
||||||
// XXX: self events don't have the IN_ISDIR mask
|
|
||||||
if (isSelfEvent || isDir) {
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
||||||
if (it->second->path == path) {
|
|
||||||
it = mSubscriptions.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher->mEvents.remove(path);
|
|
||||||
sub->tree->remove(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::unwatch which takes a lock on mMutex
|
|
||||||
void InotifyBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
// Find any subscriptions pointing to this watcher, and remove them.
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
||||||
if (it->second->watcher.get() == watcher.get()) {
|
|
||||||
if (mSubscriptions.count(it->first) == 1) {
|
|
||||||
int err = inotify_rm_watch(mInotify, it->first);
|
|
||||||
if (err == -1) {
|
|
||||||
throw WatcherError(std::string("Unable to remove watcher: ") + strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
it = mSubscriptions.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
#ifndef INOTIFY_H
|
|
||||||
#define INOTIFY_H
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <sys/inotify.h>
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../Signal.hh"
|
|
||||||
|
|
||||||
struct InotifySubscription {
|
|
||||||
std::shared_ptr<DirTree> tree;
|
|
||||||
std::string path;
|
|
||||||
WatcherRef watcher;
|
|
||||||
};
|
|
||||||
|
|
||||||
class InotifyBackend : public BruteForceBackend {
|
|
||||||
public:
|
|
||||||
void start() override;
|
|
||||||
~InotifyBackend();
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
private:
|
|
||||||
int mPipe[2];
|
|
||||||
int mInotify;
|
|
||||||
std::unordered_multimap<int, std::shared_ptr<InotifySubscription>> mSubscriptions;
|
|
||||||
Signal mEndedSignal;
|
|
||||||
|
|
||||||
bool watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
|
|
||||||
void handleEvents();
|
|
||||||
void handleEvent(struct inotify_event *event, std::unordered_set<WatcherRef> &watchers);
|
|
||||||
bool handleSubscription(struct inotify_event *event, std::shared_ptr<InotifySubscription> sub);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-338
@@ -1,338 +0,0 @@
|
|||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
#include <unordered_set>
|
|
||||||
#include "../Event.hh"
|
|
||||||
#include "../Backend.hh"
|
|
||||||
#include "./FSEventsBackend.hh"
|
|
||||||
#include "../Watcher.hh"
|
|
||||||
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
#define IGNORED_FLAGS (kFSEventStreamEventFlagItemIsHardlink | kFSEventStreamEventFlagItemIsLastHardlink | kFSEventStreamEventFlagItemIsSymlink | kFSEventStreamEventFlagItemIsDir | kFSEventStreamEventFlagItemIsFile)
|
|
||||||
|
|
||||||
void stopStream(FSEventStreamRef stream, CFRunLoopRef runLoop) {
|
|
||||||
FSEventStreamStop(stream);
|
|
||||||
FSEventStreamUnscheduleFromRunLoop(stream, runLoop, kCFRunLoopDefaultMode);
|
|
||||||
FSEventStreamInvalidate(stream);
|
|
||||||
FSEventStreamRelease(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
// macOS has a case insensitive file system by default. In order to detect
|
|
||||||
// file renames that only affect case, we need to get the canonical path
|
|
||||||
// and compare it with the input path to determine if a file was created or deleted.
|
|
||||||
bool pathExists(char *path) {
|
|
||||||
int fd = open(path, O_RDONLY | O_SYMLINK);
|
|
||||||
if (fd == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[PATH_MAX];
|
|
||||||
if (fcntl(fd, F_GETPATH, buf) == -1) {
|
|
||||||
close(fd);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool res = strncmp(path, buf, PATH_MAX) == 0;
|
|
||||||
close(fd);
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
class State: public WatcherState {
|
|
||||||
public:
|
|
||||||
FSEventStreamRef stream;
|
|
||||||
std::shared_ptr<DirTree> tree;
|
|
||||||
uint64_t since;
|
|
||||||
};
|
|
||||||
|
|
||||||
void FSEventsCallback(
|
|
||||||
ConstFSEventStreamRef streamRef,
|
|
||||||
void *clientCallBackInfo,
|
|
||||||
size_t numEvents,
|
|
||||||
void *eventPaths,
|
|
||||||
const FSEventStreamEventFlags eventFlags[],
|
|
||||||
const FSEventStreamEventId eventIds[]
|
|
||||||
) {
|
|
||||||
char **paths = (char **)eventPaths;
|
|
||||||
std::shared_ptr<Watcher>& watcher = *static_cast<std::shared_ptr<Watcher> *>(clientCallBackInfo);
|
|
||||||
|
|
||||||
EventList& list = watcher->mEvents;
|
|
||||||
if (watcher->state == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto stateGuard = watcher->state;
|
|
||||||
auto* state = static_cast<State*>(stateGuard.get());
|
|
||||||
uint64_t since = state->since;
|
|
||||||
bool deletedRoot = false;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < numEvents; ++i) {
|
|
||||||
bool isCreated = (eventFlags[i] & kFSEventStreamEventFlagItemCreated) == kFSEventStreamEventFlagItemCreated;
|
|
||||||
bool isRemoved = (eventFlags[i] & kFSEventStreamEventFlagItemRemoved) == kFSEventStreamEventFlagItemRemoved;
|
|
||||||
bool isModified = (eventFlags[i] & kFSEventStreamEventFlagItemModified) == kFSEventStreamEventFlagItemModified ||
|
|
||||||
(eventFlags[i] & kFSEventStreamEventFlagItemInodeMetaMod) == kFSEventStreamEventFlagItemInodeMetaMod ||
|
|
||||||
(eventFlags[i] & kFSEventStreamEventFlagItemFinderInfoMod) == kFSEventStreamEventFlagItemFinderInfoMod ||
|
|
||||||
(eventFlags[i] & kFSEventStreamEventFlagItemChangeOwner) == kFSEventStreamEventFlagItemChangeOwner ||
|
|
||||||
(eventFlags[i] & kFSEventStreamEventFlagItemXattrMod) == kFSEventStreamEventFlagItemXattrMod;
|
|
||||||
bool isRenamed = (eventFlags[i] & kFSEventStreamEventFlagItemRenamed) == kFSEventStreamEventFlagItemRenamed;
|
|
||||||
bool isDone = (eventFlags[i] & kFSEventStreamEventFlagHistoryDone) == kFSEventStreamEventFlagHistoryDone;
|
|
||||||
bool isDir = (eventFlags[i] & kFSEventStreamEventFlagItemIsDir) == kFSEventStreamEventFlagItemIsDir;
|
|
||||||
|
|
||||||
|
|
||||||
if (eventFlags[i] & kFSEventStreamEventFlagMustScanSubDirs) {
|
|
||||||
if (eventFlags[i] & kFSEventStreamEventFlagUserDropped) {
|
|
||||||
list.error("Events were dropped by the FSEvents client. File system must be re-scanned.");
|
|
||||||
} else if (eventFlags[i] & kFSEventStreamEventFlagKernelDropped) {
|
|
||||||
list.error("Events were dropped by the kernel. File system must be re-scanned.");
|
|
||||||
} else {
|
|
||||||
list.error("Too many events. File system must be re-scanned.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDone) {
|
|
||||||
watcher->notify();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto ignoredFlags = IGNORED_FLAGS;
|
|
||||||
if (__builtin_available(macOS 10.13, *)) {
|
|
||||||
ignoredFlags |= kFSEventStreamEventFlagItemCloned;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we don't care about any of the flags that are set, ignore this event.
|
|
||||||
if ((eventFlags[i] & ~ignoredFlags) == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FSEvents exclusion paths only apply to files, not directories.
|
|
||||||
if (watcher->isIgnored(paths[i])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle unambiguous events first
|
|
||||||
if (isCreated && !(isRemoved || isModified || isRenamed)) {
|
|
||||||
state->tree->add(paths[i], 0, isDir);
|
|
||||||
list.create(paths[i]);
|
|
||||||
} else if (isRemoved && !(isCreated || isModified || isRenamed)) {
|
|
||||||
state->tree->remove(paths[i]);
|
|
||||||
list.remove(paths[i]);
|
|
||||||
if (paths[i] == watcher->mDir) {
|
|
||||||
deletedRoot = true;
|
|
||||||
}
|
|
||||||
} else if (isModified && !(isCreated || isRemoved || isRenamed)) {
|
|
||||||
struct stat file;
|
|
||||||
if (stat(paths[i], &file)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore if mtime is the same as the last event.
|
|
||||||
// This prevents duplicate events from being emitted.
|
|
||||||
// If tv_nsec is zero, the file system probably only has second-level
|
|
||||||
// granularity so allow the even through in that case.
|
|
||||||
uint64_t mtime = CONVERT_TIME(file.st_mtimespec);
|
|
||||||
DirEntry *entry = state->tree->find(paths[i]);
|
|
||||||
if (entry && mtime == entry->mtime && file.st_mtimespec.tv_nsec != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry) {
|
|
||||||
// Update mtime.
|
|
||||||
entry->mtime = mtime;
|
|
||||||
} else {
|
|
||||||
// Add to tree if this path has not been discovered yet.
|
|
||||||
state->tree->add(paths[i], mtime, S_ISDIR(file.st_mode));
|
|
||||||
}
|
|
||||||
|
|
||||||
list.update(paths[i]);
|
|
||||||
} else {
|
|
||||||
// If multiple flags were set, then we need to call `stat` to determine if the file really exists.
|
|
||||||
// This helps disambiguate creates, updates, and deletes.
|
|
||||||
struct stat file;
|
|
||||||
if (stat(paths[i], &file) || !pathExists(paths[i])) {
|
|
||||||
// File does not exist, so we have to assume it was removed. This is not exact since the
|
|
||||||
// flags set by fsevents get coalesced together (e.g. created & deleted), so there is no way to
|
|
||||||
// know whether the create and delete both happened since our snapshot (in which case
|
|
||||||
// we'd rather ignore this event completely). This will result in some extra delete events
|
|
||||||
// being emitted for files we don't know about, but that is the best we can do.
|
|
||||||
state->tree->remove(paths[i]);
|
|
||||||
list.remove(paths[i]);
|
|
||||||
if (paths[i] == watcher->mDir) {
|
|
||||||
deletedRoot = true;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the file was modified, and existed before, then this is an update, otherwise a create.
|
|
||||||
uint64_t ctime = CONVERT_TIME(file.st_birthtimespec);
|
|
||||||
uint64_t mtime = CONVERT_TIME(file.st_mtimespec);
|
|
||||||
DirEntry *entry = !since ? state->tree->find(paths[i]) : NULL;
|
|
||||||
if (entry && entry->mtime == mtime && file.st_mtimespec.tv_nsec != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Some mounted file systems report a creation time of 0/unix epoch which we special case.
|
|
||||||
if (isModified && (entry || (ctime <= since && ctime != 0))) {
|
|
||||||
state->tree->update(paths[i], mtime);
|
|
||||||
list.update(paths[i]);
|
|
||||||
} else {
|
|
||||||
state->tree->add(paths[i], mtime, S_ISDIR(file.st_mode));
|
|
||||||
list.create(paths[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!since) {
|
|
||||||
watcher->notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop watching if the root directory was deleted.
|
|
||||||
if (deletedRoot) {
|
|
||||||
stopStream((FSEventStreamRef)streamRef, CFRunLoopGetCurrent());
|
|
||||||
watcher->state = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void checkWatcher(WatcherRef watcher) {
|
|
||||||
struct stat file;
|
|
||||||
if (stat(watcher->mDir.c_str(), &file)) {
|
|
||||||
throw WatcherError(strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!S_ISDIR(file.st_mode)) {
|
|
||||||
throw WatcherError(strerror(ENOTDIR), watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FSEventsBackend::startStream(WatcherRef watcher, FSEventStreamEventId id) {
|
|
||||||
checkWatcher(watcher);
|
|
||||||
|
|
||||||
CFAbsoluteTime latency = 0.001;
|
|
||||||
CFStringRef fileWatchPath = CFStringCreateWithCString(
|
|
||||||
NULL,
|
|
||||||
watcher->mDir.c_str(),
|
|
||||||
kCFStringEncodingUTF8
|
|
||||||
);
|
|
||||||
|
|
||||||
CFArrayRef pathsToWatch = CFArrayCreate(
|
|
||||||
NULL,
|
|
||||||
(const void **)&fileWatchPath,
|
|
||||||
1,
|
|
||||||
NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
// Make a watcher reference we can pass into the callback. This ensures bumped ref-count.
|
|
||||||
std::shared_ptr<Watcher>* callbackWatcher = new std::shared_ptr<Watcher> (watcher);
|
|
||||||
FSEventStreamContext callbackInfo {0, static_cast<void*> (callbackWatcher), nullptr, nullptr, nullptr};
|
|
||||||
FSEventStreamRef stream = FSEventStreamCreate(
|
|
||||||
NULL,
|
|
||||||
&FSEventsCallback,
|
|
||||||
&callbackInfo,
|
|
||||||
pathsToWatch,
|
|
||||||
id,
|
|
||||||
latency,
|
|
||||||
kFSEventStreamCreateFlagFileEvents
|
|
||||||
);
|
|
||||||
|
|
||||||
CFMutableArrayRef exclusions = CFArrayCreateMutable(NULL, watcher->mIgnorePaths.size(), NULL);
|
|
||||||
for (auto it = watcher->mIgnorePaths.begin(); it != watcher->mIgnorePaths.end(); it++) {
|
|
||||||
CFStringRef path = CFStringCreateWithCString(
|
|
||||||
NULL,
|
|
||||||
it->c_str(),
|
|
||||||
kCFStringEncodingUTF8
|
|
||||||
);
|
|
||||||
|
|
||||||
CFArrayAppendValue(exclusions, (const void *)path);
|
|
||||||
}
|
|
||||||
|
|
||||||
FSEventStreamSetExclusionPaths(stream, exclusions);
|
|
||||||
|
|
||||||
FSEventStreamScheduleWithRunLoop(stream, mRunLoop, kCFRunLoopDefaultMode);
|
|
||||||
bool started = FSEventStreamStart(stream);
|
|
||||||
|
|
||||||
CFRelease(pathsToWatch);
|
|
||||||
CFRelease(fileWatchPath);
|
|
||||||
|
|
||||||
if (!started) {
|
|
||||||
FSEventStreamRelease(stream);
|
|
||||||
throw WatcherError("Error starting FSEvents stream", watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto stateGuard = watcher->state;
|
|
||||||
State* s = static_cast<State*>(stateGuard.get());
|
|
||||||
s->tree = std::make_shared<DirTree>(watcher->mDir);
|
|
||||||
s->stream = stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FSEventsBackend::start() {
|
|
||||||
mRunLoop = CFRunLoopGetCurrent();
|
|
||||||
CFRetain(mRunLoop);
|
|
||||||
|
|
||||||
// Unlock once run loop has started.
|
|
||||||
CFRunLoopPerformBlock(mRunLoop, kCFRunLoopDefaultMode, ^ {
|
|
||||||
notifyStarted();
|
|
||||||
});
|
|
||||||
|
|
||||||
CFRunLoopWakeUp(mRunLoop);
|
|
||||||
CFRunLoopRun();
|
|
||||||
}
|
|
||||||
|
|
||||||
FSEventsBackend::~FSEventsBackend() {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
CFRunLoopStop(mRunLoop);
|
|
||||||
CFRelease(mRunLoop);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FSEventsBackend::writeSnapshot(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
checkWatcher(watcher);
|
|
||||||
|
|
||||||
FSEventStreamEventId id = FSEventsGetCurrentEventId();
|
|
||||||
std::ofstream ofs(*snapshotPath);
|
|
||||||
ofs << id;
|
|
||||||
ofs << "\n";
|
|
||||||
|
|
||||||
struct timespec now;
|
|
||||||
clock_gettime(CLOCK_REALTIME, &now);
|
|
||||||
ofs << CONVERT_TIME(now);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FSEventsBackend::getEventsSince(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
std::ifstream ifs(*snapshotPath);
|
|
||||||
if (ifs.fail()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
FSEventStreamEventId id;
|
|
||||||
uint64_t since;
|
|
||||||
ifs >> id;
|
|
||||||
ifs >> since;
|
|
||||||
|
|
||||||
auto s = std::make_shared<State>();
|
|
||||||
s->since = since;
|
|
||||||
watcher->state = s;
|
|
||||||
|
|
||||||
startStream(watcher, id);
|
|
||||||
watcher->wait();
|
|
||||||
stopStream(s->stream, mRunLoop);
|
|
||||||
|
|
||||||
watcher->state = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::watch which takes a lock on mMutex
|
|
||||||
void FSEventsBackend::subscribe(WatcherRef watcher) {
|
|
||||||
auto s = std::make_shared<State>();
|
|
||||||
s->since = 0;
|
|
||||||
watcher->state = s;
|
|
||||||
startStream(watcher, kFSEventStreamEventIdSinceNow);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::unwatch which takes a lock on mMutex
|
|
||||||
void FSEventsBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
auto stateGuard = watcher->state;
|
|
||||||
State* s = static_cast<State*>(stateGuard.get());
|
|
||||||
if (s != nullptr) {
|
|
||||||
stopStream(s->stream, mRunLoop);
|
|
||||||
watcher->state = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-20
@@ -1,20 +0,0 @@
|
|||||||
#ifndef FS_EVENTS_H
|
|
||||||
#define FS_EVENTS_H
|
|
||||||
|
|
||||||
#include <CoreServices/CoreServices.h>
|
|
||||||
#include "../Backend.hh"
|
|
||||||
|
|
||||||
class FSEventsBackend : public Backend {
|
|
||||||
public:
|
|
||||||
void start() override;
|
|
||||||
~FSEventsBackend();
|
|
||||||
void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void getEventsSince(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
private:
|
|
||||||
void startStream(WatcherRef watcher, FSEventStreamEventId id);
|
|
||||||
CFRunLoopRef mRunLoop;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-41
@@ -1,41 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../Event.hh"
|
|
||||||
#include "./BruteForceBackend.hh"
|
|
||||||
|
|
||||||
std::shared_ptr<DirTree> BruteForceBackend::getTree(WatcherRef watcher, bool shouldRead) {
|
|
||||||
auto tree = DirTree::getCached(watcher->mDir);
|
|
||||||
|
|
||||||
// If the tree is not complete, read it if needed.
|
|
||||||
if (!tree->isComplete && shouldRead) {
|
|
||||||
readTree(watcher, tree);
|
|
||||||
tree->isComplete = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return tree;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BruteForceBackend::writeSnapshot(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
auto tree = getTree(watcher);
|
|
||||||
FILE *f = fopen(snapshotPath->c_str(), "w");
|
|
||||||
if (!f) {
|
|
||||||
throw std::runtime_error(std::string("Unable to open snapshot file: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
tree->write(f);
|
|
||||||
fclose(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BruteForceBackend::getEventsSince(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
FILE *f = fopen(snapshotPath->c_str(), "r");
|
|
||||||
if (!f) {
|
|
||||||
throw std::runtime_error(std::string("Unable to open snapshot file: ") + strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
DirTree snapshot{watcher->mDir, f};
|
|
||||||
auto now = getTree(watcher);
|
|
||||||
now->getChanges(&snapshot, watcher->mEvents);
|
|
||||||
fclose(f);
|
|
||||||
}
|
|
||||||
-25
@@ -1,25 +0,0 @@
|
|||||||
#ifndef BRUTE_FORCE_H
|
|
||||||
#define BRUTE_FORCE_H
|
|
||||||
|
|
||||||
#include "../Backend.hh"
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../Watcher.hh"
|
|
||||||
|
|
||||||
class BruteForceBackend : public Backend {
|
|
||||||
public:
|
|
||||||
void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void getEventsSince(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void subscribe(WatcherRef watcher) override {
|
|
||||||
throw "Brute force backend doesn't support subscriptions.";
|
|
||||||
}
|
|
||||||
|
|
||||||
void unsubscribe(WatcherRef watcher) override {
|
|
||||||
throw "Brute force backend doesn't support subscriptions.";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<DirTree> getTree(WatcherRef watcher, bool shouldRead = true);
|
|
||||||
private:
|
|
||||||
void readTree(WatcherRef watcher, std::shared_ptr<DirTree> tree);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-50
@@ -1,50 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
|
|
||||||
// weird error on linux
|
|
||||||
#ifdef __THROW
|
|
||||||
#undef __THROW
|
|
||||||
#endif
|
|
||||||
#define __THROW
|
|
||||||
|
|
||||||
#include <fts.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
#if __APPLE__
|
|
||||||
#define st_mtim st_mtimespec
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr<DirTree> tree) {
|
|
||||||
char *paths[2] {(char *)watcher->mDir.c_str(), NULL};
|
|
||||||
FTS *fts = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
|
|
||||||
if (!fts) {
|
|
||||||
throw WatcherError(strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
FTSENT *node;
|
|
||||||
bool isRoot = true;
|
|
||||||
|
|
||||||
while ((node = fts_read(fts)) != NULL) {
|
|
||||||
if (node->fts_errno) {
|
|
||||||
fts_close(fts);
|
|
||||||
throw WatcherError(strerror(node->fts_errno), watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isRoot && !(node->fts_info & FTS_D)) {
|
|
||||||
fts_close(fts);
|
|
||||||
throw WatcherError(strerror(ENOTDIR), watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (watcher->isIgnored(std::string(node->fts_path))) {
|
|
||||||
fts_set(fts, node, FTS_SKIP);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tree->add(node->fts_path, CONVERT_TIME(node->fts_statp->st_mtim), (node->fts_info & FTS_D) == FTS_D);
|
|
||||||
isRoot = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fts_close(fts);
|
|
||||||
}
|
|
||||||
-77
@@ -1,77 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
|
|
||||||
// weird error on linux
|
|
||||||
#ifdef __THROW
|
|
||||||
#undef __THROW
|
|
||||||
#endif
|
|
||||||
#define __THROW
|
|
||||||
|
|
||||||
#ifdef _LIBC
|
|
||||||
# include <include/sys/stat.h>
|
|
||||||
#else
|
|
||||||
# include <sys/stat.h>
|
|
||||||
#endif
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
#if __APPLE__
|
|
||||||
#define st_mtim st_mtimespec
|
|
||||||
#endif
|
|
||||||
#define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
|
|
||||||
|
|
||||||
void iterateDir(WatcherRef watcher, const std::shared_ptr <DirTree> tree, const char *relative, int parent_fd, const std::string &dirname) {
|
|
||||||
int open_flags = (O_RDONLY | O_CLOEXEC | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
|
|
||||||
int new_fd = openat(parent_fd, relative, open_flags);
|
|
||||||
if (new_fd == -1) {
|
|
||||||
if (errno == EACCES) {
|
|
||||||
return; // ignore insufficient permissions
|
|
||||||
}
|
|
||||||
|
|
||||||
throw WatcherError(strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct stat rootAttributes;
|
|
||||||
fstatat(new_fd, ".", &rootAttributes, AT_SYMLINK_NOFOLLOW);
|
|
||||||
tree->add(dirname, CONVERT_TIME(rootAttributes.st_mtim), true);
|
|
||||||
|
|
||||||
if (DIR *dir = fdopendir(new_fd)) {
|
|
||||||
while (struct dirent *ent = (errno = 0, readdir(dir))) {
|
|
||||||
if (ISDOT(ent->d_name)) continue;
|
|
||||||
|
|
||||||
std::string fullPath = dirname + "/" + ent->d_name;
|
|
||||||
|
|
||||||
if (!watcher->isIgnored(fullPath)) {
|
|
||||||
struct stat attrib;
|
|
||||||
fstatat(new_fd, ent->d_name, &attrib, AT_SYMLINK_NOFOLLOW);
|
|
||||||
bool isDir = ent->d_type == DT_DIR;
|
|
||||||
|
|
||||||
if (isDir) {
|
|
||||||
iterateDir(watcher, tree, ent->d_name, new_fd, fullPath);
|
|
||||||
} else {
|
|
||||||
tree->add(fullPath, CONVERT_TIME(attrib.st_mtim), isDir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
closedir(dir);
|
|
||||||
} else {
|
|
||||||
close(new_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errno) {
|
|
||||||
throw WatcherError(strerror(errno), watcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr <DirTree> tree) {
|
|
||||||
int fd = open(watcher->mDir.c_str(), O_RDONLY);
|
|
||||||
if (fd) {
|
|
||||||
iterateDir(watcher, tree, ".", fd, watcher->mDir);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-132
@@ -1,132 +0,0 @@
|
|||||||
#include <sys/stat.h>
|
|
||||||
#include "WasmBackend.hh"
|
|
||||||
|
|
||||||
#define CONVERT_TIME(ts) ((uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec)
|
|
||||||
|
|
||||||
void WasmBackend::start() {
|
|
||||||
notifyStarted();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WasmBackend::subscribe(WatcherRef watcher) {
|
|
||||||
// Build a full directory tree recursively, and watch each directory.
|
|
||||||
std::shared_ptr<DirTree> tree = getTree(watcher);
|
|
||||||
|
|
||||||
for (auto it = tree->entries.begin(); it != tree->entries.end(); it++) {
|
|
||||||
if (it->second.isDir) {
|
|
||||||
watchDir(watcher, it->second.path, tree);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WasmBackend::watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree) {
|
|
||||||
int wd = wasm_backend_add_watch(path.c_str(), (void *)this);
|
|
||||||
std::shared_ptr<WasmSubscription> sub = std::make_shared<WasmSubscription>();
|
|
||||||
sub->tree = tree;
|
|
||||||
sub->path = path;
|
|
||||||
sub->watcher = watcher;
|
|
||||||
mSubscriptions.emplace(wd, sub);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" void wasm_backend_event_handler(void *backend, int wd, int type, char *filename) {
|
|
||||||
WasmBackend *b = (WasmBackend *)(backend);
|
|
||||||
b->handleEvent(wd, type, filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WasmBackend::handleEvent(int wd, int type, char *filename) {
|
|
||||||
// Find the subscriptions for this watch descriptor
|
|
||||||
auto range = mSubscriptions.equal_range(wd);
|
|
||||||
std::unordered_set<std::shared_ptr<WasmSubscription>> set;
|
|
||||||
for (auto it = range.first; it != range.second; it++) {
|
|
||||||
set.insert(it->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = set.begin(); it != set.end(); it++) {
|
|
||||||
if (handleSubscription(type, filename, *it)) {
|
|
||||||
(*it)->watcher->notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WasmBackend::handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub) {
|
|
||||||
// Build full path and check if its in our ignore list.
|
|
||||||
WatcherRef watcher = sub->watcher;
|
|
||||||
std::string path = std::string(sub->path);
|
|
||||||
|
|
||||||
if (filename[0] != '\0') {
|
|
||||||
path += "/" + std::string(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (watcher->isIgnored(path)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == 1) {
|
|
||||||
struct stat st;
|
|
||||||
stat(path.c_str(), &st);
|
|
||||||
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
|
||||||
watcher->mEvents.update(path);
|
|
||||||
} else if (type == 2) {
|
|
||||||
// Determine if this is a create or delete depending on if the file exists or not.
|
|
||||||
struct stat st;
|
|
||||||
if (lstat(path.c_str(), &st)) {
|
|
||||||
// If the entry being deleted/moved is a directory, remove it from the list of subscriptions
|
|
||||||
DirEntry *entry = sub->tree->find(path);
|
|
||||||
if (!entry) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry->isDir) {
|
|
||||||
std::string pathStart = path + DIR_SEP;
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
||||||
if (it->second->path == path || it->second->path.rfind(pathStart, 0) == 0) {
|
|
||||||
wasm_backend_remove_watch(it->first);
|
|
||||||
it = mSubscriptions.erase(it);
|
|
||||||
} else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove all sub-entries
|
|
||||||
for (auto it = sub->tree->entries.begin(); it != sub->tree->entries.end();) {
|
|
||||||
if (it->first.rfind(pathStart, 0) == 0) {
|
|
||||||
watcher->mEvents.remove(it->first);
|
|
||||||
it = sub->tree->entries.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watcher->mEvents.remove(path);
|
|
||||||
sub->tree->remove(path);
|
|
||||||
} else if (sub->tree->find(path)) {
|
|
||||||
sub->tree->update(path, CONVERT_TIME(st.st_mtim));
|
|
||||||
watcher->mEvents.update(path);
|
|
||||||
} else {
|
|
||||||
watcher->mEvents.create(path);
|
|
||||||
|
|
||||||
// If this is a create, check if it's a directory and start watching if it is.
|
|
||||||
DirEntry *entry = sub->tree->add(path, CONVERT_TIME(st.st_mtim), S_ISDIR(st.st_mode));
|
|
||||||
if (entry->isDir) {
|
|
||||||
watchDir(watcher, path, sub->tree);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WasmBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
// Find any subscriptions pointing to this watcher, and remove them.
|
|
||||||
for (auto it = mSubscriptions.begin(); it != mSubscriptions.end();) {
|
|
||||||
if (it->second->watcher.get() == watcher.get()) {
|
|
||||||
if (mSubscriptions.count(it->first) == 1) {
|
|
||||||
wasm_backend_remove_watch(it->first);
|
|
||||||
}
|
|
||||||
|
|
||||||
it = mSubscriptions.erase(it);
|
|
||||||
} else {
|
|
||||||
it++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-34
@@ -1,34 +0,0 @@
|
|||||||
#ifndef WASM_H
|
|
||||||
#define WASM_H
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
int wasm_backend_add_watch(const char *filename, void *backend);
|
|
||||||
void wasm_backend_remove_watch(int wd);
|
|
||||||
void wasm_backend_event_handler(void *backend, int wd, int type, char *filename);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct WasmSubscription {
|
|
||||||
std::shared_ptr<DirTree> tree;
|
|
||||||
std::string path;
|
|
||||||
WatcherRef watcher;
|
|
||||||
};
|
|
||||||
|
|
||||||
class WasmBackend : public BruteForceBackend {
|
|
||||||
public:
|
|
||||||
void start() override;
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
void handleEvent(int wd, int type, char *filename);
|
|
||||||
private:
|
|
||||||
int mWasm;
|
|
||||||
std::unordered_multimap<int, std::shared_ptr<WasmSubscription>> mSubscriptions;
|
|
||||||
|
|
||||||
void watchDir(WatcherRef watcher, std::string path, std::shared_ptr<DirTree> tree);
|
|
||||||
bool handleSubscription(int type, char *filename, std::shared_ptr<WasmSubscription> sub);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-74
@@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright Node.js contributors. All rights reserved.
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to
|
|
||||||
deal in the Software without restriction, including without limitation the
|
|
||||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
||||||
sell copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
||||||
IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Node does not include the headers for these functions when compiling for WASM, so add them here.
|
|
||||||
#ifdef __wasm32__
|
|
||||||
extern "C" {
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_create_threadsafe_function(napi_env env,
|
|
||||||
napi_value func,
|
|
||||||
napi_value async_resource,
|
|
||||||
napi_value async_resource_name,
|
|
||||||
size_t max_queue_size,
|
|
||||||
size_t initial_thread_count,
|
|
||||||
void* thread_finalize_data,
|
|
||||||
napi_finalize thread_finalize_cb,
|
|
||||||
void* context,
|
|
||||||
napi_threadsafe_function_call_js call_js_cb,
|
|
||||||
napi_threadsafe_function* result);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL napi_get_threadsafe_function_context(
|
|
||||||
napi_threadsafe_function func, void** result);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_call_threadsafe_function(napi_threadsafe_function func,
|
|
||||||
void* data,
|
|
||||||
napi_threadsafe_function_call_mode is_blocking);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_acquire_threadsafe_function(napi_threadsafe_function func);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL napi_release_threadsafe_function(
|
|
||||||
napi_threadsafe_function func, napi_threadsafe_function_release_mode mode);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
||||||
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL
|
|
||||||
napi_create_async_work(napi_env env,
|
|
||||||
napi_value async_resource,
|
|
||||||
napi_value async_resource_name,
|
|
||||||
napi_async_execute_callback execute,
|
|
||||||
napi_async_complete_callback complete,
|
|
||||||
void* data,
|
|
||||||
napi_async_work* result);
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL napi_delete_async_work(napi_env env,
|
|
||||||
napi_async_work work);
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(napi_env env,
|
|
||||||
napi_async_work work);
|
|
||||||
NAPI_EXTERN napi_status NAPI_CDECL napi_cancel_async_work(napi_env env,
|
|
||||||
napi_async_work work);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
-302
@@ -1,302 +0,0 @@
|
|||||||
#include <stdint.h>
|
|
||||||
#include "./BSER.hh"
|
|
||||||
|
|
||||||
BSERType decodeType(std::istream &iss) {
|
|
||||||
int8_t type;
|
|
||||||
iss.read(reinterpret_cast<char*>(&type), sizeof(type));
|
|
||||||
return (BSERType) type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void expectType(std::istream &iss, BSERType expected) {
|
|
||||||
BSERType got = decodeType(iss);
|
|
||||||
if (got != expected) {
|
|
||||||
throw std::runtime_error("Unexpected BSER type");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void encodeType(std::ostream &oss, BSERType type) {
|
|
||||||
int8_t t = (int8_t)type;
|
|
||||||
oss.write(reinterpret_cast<char*>(&t), sizeof(t));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class Value : public BSERValue {
|
|
||||||
public:
|
|
||||||
T value;
|
|
||||||
Value(T val) {
|
|
||||||
value = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
Value() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERInteger : public Value<int64_t> {
|
|
||||||
public:
|
|
||||||
BSERInteger(int64_t value) : Value(value) {}
|
|
||||||
BSERInteger(std::istream &iss) {
|
|
||||||
int8_t int8;
|
|
||||||
int16_t int16;
|
|
||||||
int32_t int32;
|
|
||||||
int64_t int64;
|
|
||||||
|
|
||||||
BSERType type = decodeType(iss);
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case BSER_INT8:
|
|
||||||
iss.read(reinterpret_cast<char*>(&int8), sizeof(int8));
|
|
||||||
value = int8;
|
|
||||||
break;
|
|
||||||
case BSER_INT16:
|
|
||||||
iss.read(reinterpret_cast<char*>(&int16), sizeof(int16));
|
|
||||||
value = int16;
|
|
||||||
break;
|
|
||||||
case BSER_INT32:
|
|
||||||
iss.read(reinterpret_cast<char*>(&int32), sizeof(int32));
|
|
||||||
value = int32;
|
|
||||||
break;
|
|
||||||
case BSER_INT64:
|
|
||||||
iss.read(reinterpret_cast<char*>(&int64), sizeof(int64));
|
|
||||||
value = int64;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw std::runtime_error("Invalid BSER int type");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t intValue() override {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
if (value <= INT8_MAX) {
|
|
||||||
encodeType(oss, BSER_INT8);
|
|
||||||
int8_t v = (int8_t)value;
|
|
||||||
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
||||||
} else if (value <= INT16_MAX) {
|
|
||||||
encodeType(oss, BSER_INT16);
|
|
||||||
int16_t v = (int16_t)value;
|
|
||||||
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
||||||
} else if (value <= INT32_MAX) {
|
|
||||||
encodeType(oss, BSER_INT32);
|
|
||||||
int32_t v = (int32_t)value;
|
|
||||||
oss.write(reinterpret_cast<char*>(&v), sizeof(v));
|
|
||||||
} else {
|
|
||||||
encodeType(oss, BSER_INT64);
|
|
||||||
oss.write(reinterpret_cast<char*>(&value), sizeof(value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERArray : public Value<BSER::Array> {
|
|
||||||
public:
|
|
||||||
BSERArray() : Value() {}
|
|
||||||
BSERArray(BSER::Array value) : Value(value) {}
|
|
||||||
BSERArray(std::istream &iss) {
|
|
||||||
expectType(iss, BSER_ARRAY);
|
|
||||||
int64_t len = BSERInteger(iss).intValue();
|
|
||||||
for (int64_t i = 0; i < len; i++) {
|
|
||||||
value.push_back(BSER(iss));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER::Array arrayValue() override {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
encodeType(oss, BSER_ARRAY);
|
|
||||||
BSERInteger(value.size()).encode(oss);
|
|
||||||
for (auto it = value.begin(); it != value.end(); it++) {
|
|
||||||
it->encode(oss);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERString : public Value<std::string> {
|
|
||||||
public:
|
|
||||||
BSERString(std::string value) : Value(value) {}
|
|
||||||
BSERString(std::istream &iss) {
|
|
||||||
expectType(iss, BSER_STRING);
|
|
||||||
int64_t len = BSERInteger(iss).intValue();
|
|
||||||
value.resize(len);
|
|
||||||
iss.read(&value[0], len);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string stringValue() override {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
encodeType(oss, BSER_STRING);
|
|
||||||
BSERInteger(value.size()).encode(oss);
|
|
||||||
oss << value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERObject : public Value<BSER::Object> {
|
|
||||||
public:
|
|
||||||
BSERObject() : Value() {}
|
|
||||||
BSERObject(BSER::Object value) : Value(value) {}
|
|
||||||
BSERObject(std::istream &iss) {
|
|
||||||
expectType(iss, BSER_OBJECT);
|
|
||||||
int64_t len = BSERInteger(iss).intValue();
|
|
||||||
for (int64_t i = 0; i < len; i++) {
|
|
||||||
auto key = BSERString(iss).stringValue();
|
|
||||||
auto val = BSER(iss);
|
|
||||||
value.emplace(key, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER::Object objectValue() override {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
encodeType(oss, BSER_OBJECT);
|
|
||||||
BSERInteger(value.size()).encode(oss);
|
|
||||||
for (auto it = value.begin(); it != value.end(); it++) {
|
|
||||||
BSERString(it->first).encode(oss);
|
|
||||||
it->second.encode(oss);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERDouble : public Value<double> {
|
|
||||||
public:
|
|
||||||
BSERDouble(double value) : Value(value) {}
|
|
||||||
BSERDouble(std::istream &iss) {
|
|
||||||
expectType(iss, BSER_REAL);
|
|
||||||
iss.read(reinterpret_cast<char*>(&value), sizeof(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
double doubleValue() override {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
encodeType(oss, BSER_REAL);
|
|
||||||
oss.write(reinterpret_cast<char*>(&value), sizeof(value));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERBoolean : public Value<bool> {
|
|
||||||
public:
|
|
||||||
BSERBoolean(bool value) : Value(value) {}
|
|
||||||
bool boolValue() override { return value; }
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
int8_t t = value == true ? static_cast<int8_t>(BSER_BOOL_TRUE) : static_cast<int8_t>(BSER_BOOL_FALSE);
|
|
||||||
oss.write(reinterpret_cast<char*>(&t), sizeof(t));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERNull : public Value<bool> {
|
|
||||||
public:
|
|
||||||
BSERNull() : Value(false) {}
|
|
||||||
void encode(std::ostream &oss) override {
|
|
||||||
encodeType(oss, BSER_NULL);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::shared_ptr<BSERArray> decodeTemplate(std::istream &iss) {
|
|
||||||
expectType(iss, BSER_TEMPLATE);
|
|
||||||
auto keys = BSERArray(iss).arrayValue();
|
|
||||||
auto len = BSERInteger(iss).intValue();
|
|
||||||
std::shared_ptr<BSERArray> arr = std::make_shared<BSERArray>();
|
|
||||||
for (int64_t i = 0; i < len; i++) {
|
|
||||||
BSER::Object obj;
|
|
||||||
for (auto it = keys.begin(); it != keys.end(); it++) {
|
|
||||||
if (iss.peek() == 0x0c) {
|
|
||||||
iss.ignore(1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto val = BSER(iss);
|
|
||||||
obj.emplace(it->stringValue(), val);
|
|
||||||
}
|
|
||||||
arr->value.push_back(obj);
|
|
||||||
}
|
|
||||||
return arr;
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER::BSER(std::istream &iss) {
|
|
||||||
BSERType type = decodeType(iss);
|
|
||||||
iss.unget();
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case BSER_ARRAY:
|
|
||||||
m_ptr = std::make_shared<BSERArray>(iss);
|
|
||||||
break;
|
|
||||||
case BSER_OBJECT:
|
|
||||||
m_ptr = std::make_shared<BSERObject>(iss);
|
|
||||||
break;
|
|
||||||
case BSER_STRING:
|
|
||||||
m_ptr = std::make_shared<BSERString>(iss);
|
|
||||||
break;
|
|
||||||
case BSER_INT8:
|
|
||||||
case BSER_INT16:
|
|
||||||
case BSER_INT32:
|
|
||||||
case BSER_INT64:
|
|
||||||
m_ptr = std::make_shared<BSERInteger>(iss);
|
|
||||||
break;
|
|
||||||
case BSER_REAL:
|
|
||||||
m_ptr = std::make_shared<BSERDouble>(iss);
|
|
||||||
break;
|
|
||||||
case BSER_BOOL_TRUE:
|
|
||||||
iss.ignore(1);
|
|
||||||
m_ptr = std::make_shared<BSERBoolean>(true);
|
|
||||||
break;
|
|
||||||
case BSER_BOOL_FALSE:
|
|
||||||
iss.ignore(1);
|
|
||||||
m_ptr = std::make_shared<BSERBoolean>(false);
|
|
||||||
break;
|
|
||||||
case BSER_NULL:
|
|
||||||
iss.ignore(1);
|
|
||||||
m_ptr = std::make_shared<BSERNull>();
|
|
||||||
break;
|
|
||||||
case BSER_TEMPLATE:
|
|
||||||
m_ptr = decodeTemplate(iss);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw std::runtime_error("unknown BSER type");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER::BSER() : m_ptr(std::make_shared<BSERNull>()) {}
|
|
||||||
BSER::BSER(BSER::Array value) : m_ptr(std::make_shared<BSERArray>(value)) {}
|
|
||||||
BSER::BSER(BSER::Object value) : m_ptr(std::make_shared<BSERObject>(value)) {}
|
|
||||||
BSER::BSER(const char *value) : m_ptr(std::make_shared<BSERString>(value)) {}
|
|
||||||
BSER::BSER(std::string value) : m_ptr(std::make_shared<BSERString>(value)) {}
|
|
||||||
BSER::BSER(int64_t value) : m_ptr(std::make_shared<BSERInteger>(value)) {}
|
|
||||||
BSER::BSER(double value) : m_ptr(std::make_shared<BSERDouble>(value)) {}
|
|
||||||
BSER::BSER(bool value) : m_ptr(std::make_shared<BSERBoolean>(value)) {}
|
|
||||||
|
|
||||||
BSER::Array BSER::arrayValue() { return m_ptr->arrayValue(); }
|
|
||||||
BSER::Object BSER::objectValue() { return m_ptr->objectValue(); }
|
|
||||||
std::string BSER::stringValue() { return m_ptr->stringValue(); }
|
|
||||||
int64_t BSER::intValue() { return m_ptr->intValue(); }
|
|
||||||
double BSER::doubleValue() { return m_ptr->doubleValue(); }
|
|
||||||
bool BSER::boolValue() { return m_ptr->boolValue(); }
|
|
||||||
void BSER::encode(std::ostream &oss) {
|
|
||||||
m_ptr->encode(oss);
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t BSER::decodeLength(std::istream &iss) {
|
|
||||||
char pdu[2];
|
|
||||||
if (!iss.read(pdu, 2) || pdu[0] != 0 || pdu[1] != 1) {
|
|
||||||
throw std::runtime_error("Invalid BSER");
|
|
||||||
}
|
|
||||||
|
|
||||||
return BSERInteger(iss).intValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string BSER::encode() {
|
|
||||||
std::ostringstream oss(std::ios_base::binary);
|
|
||||||
encode(oss);
|
|
||||||
|
|
||||||
std::ostringstream res(std::ios_base::binary);
|
|
||||||
res.write("\x00\x01", 2);
|
|
||||||
|
|
||||||
BSERInteger(oss.str().size()).encode(res);
|
|
||||||
res << oss.str();
|
|
||||||
return res.str();
|
|
||||||
}
|
|
||||||
-69
@@ -1,69 +0,0 @@
|
|||||||
#ifndef BSER_H
|
|
||||||
#define BSER_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
enum BSERType {
|
|
||||||
BSER_ARRAY = 0x00,
|
|
||||||
BSER_OBJECT = 0x01,
|
|
||||||
BSER_STRING = 0x02,
|
|
||||||
BSER_INT8 = 0x03,
|
|
||||||
BSER_INT16 = 0x04,
|
|
||||||
BSER_INT32 = 0x05,
|
|
||||||
BSER_INT64 = 0x06,
|
|
||||||
BSER_REAL = 0x07,
|
|
||||||
BSER_BOOL_TRUE = 0x08,
|
|
||||||
BSER_BOOL_FALSE = 0x09,
|
|
||||||
BSER_NULL = 0x0a,
|
|
||||||
BSER_TEMPLATE = 0x0b
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERValue;
|
|
||||||
|
|
||||||
class BSER {
|
|
||||||
public:
|
|
||||||
typedef std::vector<BSER> Array;
|
|
||||||
typedef std::unordered_map<std::string, BSER> Object;
|
|
||||||
|
|
||||||
BSER();
|
|
||||||
BSER(BSER::Array value);
|
|
||||||
BSER(BSER::Object value);
|
|
||||||
BSER(std::string value);
|
|
||||||
BSER(const char *value);
|
|
||||||
BSER(int64_t value);
|
|
||||||
BSER(double value);
|
|
||||||
BSER(bool value);
|
|
||||||
BSER(std::istream &iss);
|
|
||||||
|
|
||||||
BSER::Array arrayValue();
|
|
||||||
BSER::Object objectValue();
|
|
||||||
std::string stringValue();
|
|
||||||
int64_t intValue();
|
|
||||||
double doubleValue();
|
|
||||||
bool boolValue();
|
|
||||||
void encode(std::ostream &oss);
|
|
||||||
|
|
||||||
static int64_t decodeLength(std::istream &iss);
|
|
||||||
std::string encode();
|
|
||||||
private:
|
|
||||||
std::shared_ptr<BSERValue> m_ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BSERValue {
|
|
||||||
protected:
|
|
||||||
friend class BSER;
|
|
||||||
virtual BSER::Array arrayValue() { return BSER::Array(); }
|
|
||||||
virtual BSER::Object objectValue() { return BSER::Object(); }
|
|
||||||
virtual std::string stringValue() { return std::string(); }
|
|
||||||
virtual int64_t intValue() { return 0; }
|
|
||||||
virtual double doubleValue() { return 0; }
|
|
||||||
virtual bool boolValue() { return false; }
|
|
||||||
virtual void encode(std::ostream &oss) {}
|
|
||||||
virtual ~BSERValue() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-175
@@ -1,175 +0,0 @@
|
|||||||
#ifndef IPC_H
|
|
||||||
#define IPC_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/un.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
class IPC {
|
|
||||||
public:
|
|
||||||
IPC(std::string path) {
|
|
||||||
mStopped = false;
|
|
||||||
#ifdef _WIN32
|
|
||||||
while (true) {
|
|
||||||
mPipe = CreateFile(
|
|
||||||
path.data(), // pipe name
|
|
||||||
GENERIC_READ | GENERIC_WRITE, // read and write access
|
|
||||||
0, // no sharing
|
|
||||||
NULL, // default security attributes
|
|
||||||
OPEN_EXISTING, // opens existing pipe
|
|
||||||
FILE_FLAG_OVERLAPPED, // attributes
|
|
||||||
NULL // no template file
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mPipe != INVALID_HANDLE_VALUE) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GetLastError() != ERROR_PIPE_BUSY) {
|
|
||||||
throw std::runtime_error("Could not open pipe");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for pipe to become available if it is busy
|
|
||||||
if (!WaitNamedPipe(path.data(), 30000)) {
|
|
||||||
throw std::runtime_error("Error waiting for pipe");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mReader = CreateEvent(NULL, true, false, NULL);
|
|
||||||
mWriter = CreateEvent(NULL, true, false, NULL);
|
|
||||||
#else
|
|
||||||
struct sockaddr_un addr;
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.sun_family = AF_UNIX;
|
|
||||||
strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);
|
|
||||||
|
|
||||||
mSock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
||||||
if (connect(mSock, (struct sockaddr *) &addr, sizeof(struct sockaddr_un))) {
|
|
||||||
throw std::runtime_error("Error connecting to socket");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
~IPC() {
|
|
||||||
mStopped = true;
|
|
||||||
#ifdef _WIN32
|
|
||||||
CancelIo(mPipe);
|
|
||||||
CloseHandle(mPipe);
|
|
||||||
CloseHandle(mReader);
|
|
||||||
CloseHandle(mWriter);
|
|
||||||
#else
|
|
||||||
shutdown(mSock, SHUT_RDWR);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void write(std::string buf) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
OVERLAPPED overlapped;
|
|
||||||
overlapped.hEvent = mWriter;
|
|
||||||
bool success = WriteFile(
|
|
||||||
mPipe, // pipe handle
|
|
||||||
buf.data(), // message
|
|
||||||
static_cast<DWORD>(buf.size()), // message length
|
|
||||||
NULL, // bytes written
|
|
||||||
&overlapped // overlapped
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mStopped) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
if (GetLastError() != ERROR_IO_PENDING) {
|
|
||||||
throw std::runtime_error("Write error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD written;
|
|
||||||
success = GetOverlappedResult(mPipe, &overlapped, &written, true);
|
|
||||||
if (!success) {
|
|
||||||
throw std::runtime_error("GetOverlappedResult failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (written != buf.size()) {
|
|
||||||
throw std::runtime_error("Wrong number of bytes written");
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
int r = 0;
|
|
||||||
for (unsigned int i = 0; i != buf.size(); i += r) {
|
|
||||||
r = ::write(mSock, &buf[i], buf.size() - i);
|
|
||||||
if (r == -1) {
|
|
||||||
if (errno == EAGAIN) {
|
|
||||||
r = 0;
|
|
||||||
} else if (mStopped) {
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
throw std::runtime_error("Write error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int read(char *buf, size_t len) {
|
|
||||||
#ifdef _WIN32
|
|
||||||
OVERLAPPED overlapped;
|
|
||||||
overlapped.hEvent = mReader;
|
|
||||||
bool success = ReadFile(
|
|
||||||
mPipe, // pipe handle
|
|
||||||
buf, // buffer to receive reply
|
|
||||||
static_cast<DWORD>(len), // size of buffer
|
|
||||||
NULL, // number of bytes read
|
|
||||||
&overlapped // overlapped
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!success && !mStopped) {
|
|
||||||
if (GetLastError() != ERROR_IO_PENDING) {
|
|
||||||
throw std::runtime_error("Read error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DWORD read = 0;
|
|
||||||
success = GetOverlappedResult(mPipe, &overlapped, &read, true);
|
|
||||||
if (!success && !mStopped) {
|
|
||||||
throw std::runtime_error("GetOverlappedResult failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
return read;
|
|
||||||
#else
|
|
||||||
int r = ::read(mSock, buf, len);
|
|
||||||
if (r == 0 && !mStopped) {
|
|
||||||
throw std::runtime_error("Socket ended unexpectedly");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r < 0) {
|
|
||||||
if (mStopped) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw std::runtime_error(strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool mStopped;
|
|
||||||
#ifdef _WIN32
|
|
||||||
HANDLE mPipe;
|
|
||||||
HANDLE mReader;
|
|
||||||
HANDLE mWriter;
|
|
||||||
#else
|
|
||||||
int mSock;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-342
@@ -1,342 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <algorithm>
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../Event.hh"
|
|
||||||
#include "./BSER.hh"
|
|
||||||
#include "./WatchmanBackend.hh"
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include "../windows/win_utils.hh"
|
|
||||||
#define S_ISDIR(mode) ((mode & _S_IFDIR) == _S_IFDIR)
|
|
||||||
#define popen _popen
|
|
||||||
#define pclose _pclose
|
|
||||||
#else
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#define normalizePath(dir) dir
|
|
||||||
#endif
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
BSER readBSER(T &&do_read) {
|
|
||||||
std::stringstream oss;
|
|
||||||
char buffer[256];
|
|
||||||
size_t r;
|
|
||||||
int64_t len = -1;
|
|
||||||
do {
|
|
||||||
// Start by reading a minimal amount of data in order to decode the length.
|
|
||||||
// After that, attempt to read the remaining length, up to the buffer size.
|
|
||||||
r = do_read(buffer, len == -1 ? 20 : (len < 256 ? len : 256));
|
|
||||||
oss << std::string(buffer, r);
|
|
||||||
|
|
||||||
if (len == -1) {
|
|
||||||
uint64_t l = BSER::decodeLength(oss);
|
|
||||||
len = l + oss.tellg();
|
|
||||||
}
|
|
||||||
|
|
||||||
len -= r;
|
|
||||||
} while (len > 0);
|
|
||||||
|
|
||||||
return BSER(oss);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getSockPath() {
|
|
||||||
auto var = getenv("WATCHMAN_SOCK");
|
|
||||||
if (var && *var) {
|
|
||||||
return std::string(var);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
FILE *fp = popen("watchman --output-encoding=bser get-sockname", "r");
|
|
||||||
#else
|
|
||||||
FILE *fp = popen("watchman --output-encoding=bser get-sockname 2>/dev/null", "r");
|
|
||||||
#endif
|
|
||||||
if (fp == NULL || errno == ECHILD) {
|
|
||||||
throw std::runtime_error("Failed to execute watchman");
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER b = readBSER([fp] (char *buf, size_t len) {
|
|
||||||
return fread(buf, sizeof(char), len, fp);
|
|
||||||
});
|
|
||||||
|
|
||||||
pclose(fp);
|
|
||||||
|
|
||||||
auto objValue = b.objectValue();
|
|
||||||
auto foundSockname = objValue.find("sockname");
|
|
||||||
if (foundSockname == objValue.end()) {
|
|
||||||
throw std::runtime_error("sockname not found");
|
|
||||||
}
|
|
||||||
return foundSockname->second.stringValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<IPC> watchmanConnect() {
|
|
||||||
std::string path = getSockPath();
|
|
||||||
return std::unique_ptr<IPC>(new IPC(path));
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER watchmanRead(IPC *ipc) {
|
|
||||||
return readBSER([ipc] (char *buf, size_t len) {
|
|
||||||
return ipc->read(buf, len);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
BSER::Object WatchmanBackend::watchmanRequest(BSER b) {
|
|
||||||
std::string cmd = b.encode();
|
|
||||||
mIPC->write(cmd);
|
|
||||||
mRequestSignal.notify();
|
|
||||||
|
|
||||||
mResponseSignal.wait();
|
|
||||||
mResponseSignal.reset();
|
|
||||||
|
|
||||||
if (!mError.empty()) {
|
|
||||||
std::runtime_error err = std::runtime_error(mError);
|
|
||||||
mError = std::string();
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
return mResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WatchmanBackend::watchmanWatch(std::string dir) {
|
|
||||||
std::vector<BSER> cmd;
|
|
||||||
cmd.push_back("watch");
|
|
||||||
cmd.push_back(normalizePath(dir));
|
|
||||||
watchmanRequest(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WatchmanBackend::checkAvailable() {
|
|
||||||
try {
|
|
||||||
watchmanConnect();
|
|
||||||
return true;
|
|
||||||
} catch (std::exception&) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleFiles(WatcherRef watcher, BSER::Object obj) {
|
|
||||||
auto found = obj.find("files");
|
|
||||||
if (found == obj.end()) {
|
|
||||||
throw WatcherError("Error reading changes from watchman", watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto files = found->second.arrayValue();
|
|
||||||
for (auto it = files.begin(); it != files.end(); it++) {
|
|
||||||
auto file = it->objectValue();
|
|
||||||
auto name = file.find("name")->second.stringValue();
|
|
||||||
#ifdef _WIN32
|
|
||||||
std::replace(name.begin(), name.end(), '/', '\\');
|
|
||||||
#endif
|
|
||||||
auto mode = file.find("mode")->second.intValue();
|
|
||||||
auto isNew = file.find("new")->second.boolValue();
|
|
||||||
auto exists = file.find("exists")->second.boolValue();
|
|
||||||
auto path = watcher->mDir + DIR_SEP + name;
|
|
||||||
if (watcher->isIgnored(path)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNew && exists) {
|
|
||||||
watcher->mEvents.create(path);
|
|
||||||
} else if (exists && !S_ISDIR(mode)) {
|
|
||||||
watcher->mEvents.update(path);
|
|
||||||
} else if (!isNew && !exists) {
|
|
||||||
watcher->mEvents.remove(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WatchmanBackend::handleSubscription(BSER::Object obj) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
auto subscription = obj.find("subscription")->second.stringValue();
|
|
||||||
auto it = mSubscriptions.find(subscription);
|
|
||||||
if (it == mSubscriptions.end()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto watcher = it->second;
|
|
||||||
try {
|
|
||||||
handleFiles(watcher, obj);
|
|
||||||
watcher->notify();
|
|
||||||
} catch (WatcherError &err) {
|
|
||||||
handleWatcherError(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WatchmanBackend::start() {
|
|
||||||
mIPC = watchmanConnect();
|
|
||||||
notifyStarted();
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
// If there are no subscriptions we are reading, wait for a request.
|
|
||||||
if (mSubscriptions.size() == 0) {
|
|
||||||
mRequestSignal.wait();
|
|
||||||
mRequestSignal.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Break out of loop if we are stopped.
|
|
||||||
if (mStopped) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempt to read from the socket.
|
|
||||||
// If there is an error and we are stopped, break.
|
|
||||||
BSER b;
|
|
||||||
try {
|
|
||||||
b = watchmanRead(&*mIPC);
|
|
||||||
} catch (std::exception &err) {
|
|
||||||
if (mStopped) {
|
|
||||||
break;
|
|
||||||
} else if (mResponseSignal.isWaiting()) {
|
|
||||||
mError = err.what();
|
|
||||||
mResponseSignal.notify();
|
|
||||||
} else {
|
|
||||||
// Throwing causes the backend to be destroyed, but we never reach the code below to notify the signal
|
|
||||||
mEndedSignal.notify();
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto obj = b.objectValue();
|
|
||||||
auto error = obj.find("error");
|
|
||||||
if (error != obj.end()) {
|
|
||||||
mError = error->second.stringValue();
|
|
||||||
mResponseSignal.notify();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this message is for a subscription, handle it, otherwise notify the request.
|
|
||||||
auto subscription = obj.find("subscription");
|
|
||||||
if (subscription != obj.end()) {
|
|
||||||
handleSubscription(obj);
|
|
||||||
} else {
|
|
||||||
mResponse = obj;
|
|
||||||
mResponseSignal.notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mEndedSignal.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
WatchmanBackend::~WatchmanBackend() {
|
|
||||||
// Mark the watcher as stopped, close the socket, and trigger the lock.
|
|
||||||
// This will cause the read loop to be broken and the thread to exit.
|
|
||||||
mStopped = true;
|
|
||||||
mIPC.reset();
|
|
||||||
mRequestSignal.notify();
|
|
||||||
|
|
||||||
// If not ended yet, wait.
|
|
||||||
mEndedSignal.wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WatchmanBackend::clock(WatcherRef watcher) {
|
|
||||||
BSER::Array cmd;
|
|
||||||
cmd.push_back("clock");
|
|
||||||
cmd.push_back(normalizePath(watcher->mDir));
|
|
||||||
|
|
||||||
BSER::Object obj = watchmanRequest(cmd);
|
|
||||||
auto found = obj.find("clock");
|
|
||||||
if (found == obj.end()) {
|
|
||||||
throw WatcherError("Error reading clock from watchman", watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
return found->second.stringValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WatchmanBackend::writeSnapshot(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
watchmanWatch(watcher->mDir);
|
|
||||||
|
|
||||||
std::ofstream ofs(*snapshotPath);
|
|
||||||
ofs << clock(watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WatchmanBackend::getEventsSince(WatcherRef watcher, std::string *snapshotPath) {
|
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
|
||||||
std::ifstream ifs(*snapshotPath);
|
|
||||||
if (ifs.fail()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
watchmanWatch(watcher->mDir);
|
|
||||||
|
|
||||||
std::string clock;
|
|
||||||
ifs >> clock;
|
|
||||||
|
|
||||||
BSER::Array cmd;
|
|
||||||
cmd.push_back("since");
|
|
||||||
cmd.push_back(normalizePath(watcher->mDir));
|
|
||||||
cmd.push_back(clock);
|
|
||||||
|
|
||||||
BSER::Object obj = watchmanRequest(cmd);
|
|
||||||
handleFiles(watcher, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string getId(WatcherRef watcher) {
|
|
||||||
std::ostringstream id;
|
|
||||||
id << "parcel-";
|
|
||||||
id << static_cast<void*>(watcher.get());
|
|
||||||
return id.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::watch which takes a lock on mMutex
|
|
||||||
void WatchmanBackend::subscribe(WatcherRef watcher) {
|
|
||||||
watchmanWatch(watcher->mDir);
|
|
||||||
|
|
||||||
std::string id = getId(watcher);
|
|
||||||
BSER::Array cmd;
|
|
||||||
cmd.push_back("subscribe");
|
|
||||||
cmd.push_back(normalizePath(watcher->mDir));
|
|
||||||
cmd.push_back(id);
|
|
||||||
|
|
||||||
BSER::Array fields;
|
|
||||||
fields.push_back("name");
|
|
||||||
fields.push_back("mode");
|
|
||||||
fields.push_back("exists");
|
|
||||||
fields.push_back("new");
|
|
||||||
|
|
||||||
BSER::Object opts;
|
|
||||||
opts.emplace("fields", fields);
|
|
||||||
opts.emplace("since", clock(watcher));
|
|
||||||
|
|
||||||
if (watcher->mIgnorePaths.size() > 0) {
|
|
||||||
BSER::Array ignore;
|
|
||||||
BSER::Array anyOf;
|
|
||||||
anyOf.push_back("anyof");
|
|
||||||
|
|
||||||
for (auto it = watcher->mIgnorePaths.begin(); it != watcher->mIgnorePaths.end(); it++) {
|
|
||||||
std::string pathStart = watcher->mDir + DIR_SEP;
|
|
||||||
if (it->rfind(pathStart, 0) == 0) {
|
|
||||||
auto relative = it->substr(pathStart.size());
|
|
||||||
BSER::Array dirname;
|
|
||||||
dirname.push_back("dirname");
|
|
||||||
dirname.push_back(relative);
|
|
||||||
anyOf.push_back(dirname);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ignore.push_back("not");
|
|
||||||
ignore.push_back(anyOf);
|
|
||||||
|
|
||||||
opts.emplace("expression", ignore);
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.push_back(opts);
|
|
||||||
watchmanRequest(cmd);
|
|
||||||
|
|
||||||
mSubscriptions.emplace(id, watcher);
|
|
||||||
mRequestSignal.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::unwatch which takes a lock on mMutex
|
|
||||||
void WatchmanBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
std::string id = getId(watcher);
|
|
||||||
auto erased = mSubscriptions.erase(id);
|
|
||||||
|
|
||||||
if (erased) {
|
|
||||||
BSER::Array cmd;
|
|
||||||
cmd.push_back("unsubscribe");
|
|
||||||
cmd.push_back(normalizePath(watcher->mDir));
|
|
||||||
cmd.push_back(id);
|
|
||||||
|
|
||||||
watchmanRequest(cmd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-35
@@ -1,35 +0,0 @@
|
|||||||
#ifndef WATCHMAN_H
|
|
||||||
#define WATCHMAN_H
|
|
||||||
|
|
||||||
#include "../Backend.hh"
|
|
||||||
#include "./BSER.hh"
|
|
||||||
#include "../Signal.hh"
|
|
||||||
#include "./IPC.hh"
|
|
||||||
|
|
||||||
class WatchmanBackend : public Backend {
|
|
||||||
public:
|
|
||||||
static bool checkAvailable();
|
|
||||||
void start() override;
|
|
||||||
WatchmanBackend() : mStopped(false) {};
|
|
||||||
~WatchmanBackend();
|
|
||||||
void writeSnapshot(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void getEventsSince(WatcherRef watcher, std::string *snapshotPath) override;
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
private:
|
|
||||||
std::unique_ptr<IPC> mIPC;
|
|
||||||
Signal mRequestSignal;
|
|
||||||
Signal mResponseSignal;
|
|
||||||
BSER::Object mResponse;
|
|
||||||
std::string mError;
|
|
||||||
std::unordered_map<std::string, WatcherRef> mSubscriptions;
|
|
||||||
bool mStopped;
|
|
||||||
Signal mEndedSignal;
|
|
||||||
|
|
||||||
std::string clock(WatcherRef watcher);
|
|
||||||
void watchmanWatch(std::string dir);
|
|
||||||
BSER::Object watchmanRequest(BSER cmd);
|
|
||||||
void handleSubscription(BSER::Object obj);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-282
@@ -1,282 +0,0 @@
|
|||||||
#include <string>
|
|
||||||
#include <stack>
|
|
||||||
#include "../DirTree.hh"
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
#include "./WindowsBackend.hh"
|
|
||||||
#include "./win_utils.hh"
|
|
||||||
|
|
||||||
#define DEFAULT_BUF_SIZE 1024 * 1024
|
|
||||||
#define NETWORK_BUF_SIZE 64 * 1024
|
|
||||||
#define CONVERT_TIME(ft) ULARGE_INTEGER{ft.dwLowDateTime, ft.dwHighDateTime}.QuadPart
|
|
||||||
|
|
||||||
void BruteForceBackend::readTree(WatcherRef watcher, std::shared_ptr<DirTree> tree) {
|
|
||||||
std::stack<std::string> directories;
|
|
||||||
|
|
||||||
directories.push(watcher->mDir);
|
|
||||||
|
|
||||||
while (!directories.empty()) {
|
|
||||||
HANDLE hFind = INVALID_HANDLE_VALUE;
|
|
||||||
|
|
||||||
std::string path = directories.top();
|
|
||||||
std::string spec = path + "\\*";
|
|
||||||
directories.pop();
|
|
||||||
|
|
||||||
WIN32_FIND_DATA ffd;
|
|
||||||
hFind = FindFirstFile(spec.c_str(), &ffd);
|
|
||||||
|
|
||||||
if (hFind == INVALID_HANDLE_VALUE) {
|
|
||||||
if (path == watcher->mDir) {
|
|
||||||
FindClose(hFind);
|
|
||||||
throw WatcherError("Error opening directory", watcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
tree->remove(path);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
if (strcmp(ffd.cFileName, ".") != 0 && strcmp(ffd.cFileName, "..") != 0) {
|
|
||||||
std::string fullPath = path + "\\" + ffd.cFileName;
|
|
||||||
if (watcher->isIgnored(fullPath)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tree->add(fullPath, CONVERT_TIME(ffd.ftLastWriteTime), ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
||||||
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
||||||
directories.push(fullPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while (FindNextFile(hFind, &ffd) != 0);
|
|
||||||
|
|
||||||
FindClose(hFind);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WindowsBackend::start() {
|
|
||||||
mRunning = true;
|
|
||||||
notifyStarted();
|
|
||||||
|
|
||||||
while (mRunning) {
|
|
||||||
SleepEx(INFINITE, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowsBackend::~WindowsBackend() {
|
|
||||||
// Mark as stopped, and queue a noop function in the thread to break the loop
|
|
||||||
mRunning = false;
|
|
||||||
QueueUserAPC([](__in ULONG_PTR) {}, mThread.native_handle(), (ULONG_PTR)this);
|
|
||||||
}
|
|
||||||
|
|
||||||
class Subscription: public WatcherState {
|
|
||||||
public:
|
|
||||||
Subscription(WindowsBackend *backend, WatcherRef watcher, std::shared_ptr<DirTree> tree) {
|
|
||||||
mRunning = true;
|
|
||||||
mBackend = backend;
|
|
||||||
mWatcher = watcher;
|
|
||||||
mTree = tree;
|
|
||||||
ZeroMemory(&mOverlapped, sizeof(OVERLAPPED));
|
|
||||||
mOverlapped.hEvent = this;
|
|
||||||
mReadBuffer.resize(DEFAULT_BUF_SIZE);
|
|
||||||
mWriteBuffer.resize(DEFAULT_BUF_SIZE);
|
|
||||||
|
|
||||||
mDirectoryHandle = CreateFileW(
|
|
||||||
utf8ToUtf16(watcher->mDir).data(),
|
|
||||||
FILE_LIST_DIRECTORY,
|
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
||||||
NULL,
|
|
||||||
OPEN_EXISTING,
|
|
||||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
|
||||||
NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
if (mDirectoryHandle == INVALID_HANDLE_VALUE) {
|
|
||||||
throw WatcherError("Invalid handle", mWatcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that the path is a directory
|
|
||||||
BY_HANDLE_FILE_INFORMATION info;
|
|
||||||
bool success = GetFileInformationByHandle(
|
|
||||||
mDirectoryHandle,
|
|
||||||
&info
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
throw WatcherError("Could not get file information", mWatcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
||||||
throw WatcherError("Not a directory", mWatcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Subscription() {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void run() {
|
|
||||||
try {
|
|
||||||
poll();
|
|
||||||
} catch (WatcherError &err) {
|
|
||||||
mBackend->handleWatcherError(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
if (mRunning) {
|
|
||||||
mRunning = false;
|
|
||||||
CancelIo(mDirectoryHandle);
|
|
||||||
CloseHandle(mDirectoryHandle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void poll() {
|
|
||||||
if (!mRunning) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asynchronously wait for changes.
|
|
||||||
int success = ReadDirectoryChangesW(
|
|
||||||
mDirectoryHandle,
|
|
||||||
mWriteBuffer.data(),
|
|
||||||
static_cast<DWORD>(mWriteBuffer.size()),
|
|
||||||
TRUE, // recursive
|
|
||||||
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES
|
|
||||||
| FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE,
|
|
||||||
NULL,
|
|
||||||
&mOverlapped,
|
|
||||||
[](DWORD errorCode, DWORD numBytes, LPOVERLAPPED overlapped) {
|
|
||||||
auto subscription = reinterpret_cast<Subscription *>(overlapped->hEvent);
|
|
||||||
try {
|
|
||||||
subscription->processEvents(errorCode);
|
|
||||||
} catch (WatcherError &err) {
|
|
||||||
subscription->mBackend->handleWatcherError(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
throw WatcherError("Failed to read changes", mWatcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void processEvents(DWORD errorCode) {
|
|
||||||
if (!mRunning) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (errorCode) {
|
|
||||||
case ERROR_OPERATION_ABORTED:
|
|
||||||
return;
|
|
||||||
case ERROR_INVALID_PARAMETER:
|
|
||||||
// resize buffers to network size (64kb), and try again
|
|
||||||
mReadBuffer.resize(NETWORK_BUF_SIZE);
|
|
||||||
mWriteBuffer.resize(NETWORK_BUF_SIZE);
|
|
||||||
poll();
|
|
||||||
return;
|
|
||||||
case ERROR_NOTIFY_ENUM_DIR:
|
|
||||||
throw WatcherError("Buffer overflow. Some events may have been lost.", mWatcher);
|
|
||||||
case ERROR_ACCESS_DENIED: {
|
|
||||||
// This can happen if the watched directory is deleted. Check if that is the case,
|
|
||||||
// and if so emit a delete event. Otherwise, fall through to default error case.
|
|
||||||
DWORD attrs = GetFileAttributesW(utf8ToUtf16(mWatcher->mDir).data());
|
|
||||||
bool isDir = attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY);
|
|
||||||
if (!isDir) {
|
|
||||||
mWatcher->mEvents.remove(mWatcher->mDir);
|
|
||||||
mTree->remove(mWatcher->mDir);
|
|
||||||
mWatcher->notify();
|
|
||||||
stop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if (errorCode != ERROR_SUCCESS) {
|
|
||||||
throw WatcherError("Unknown error", mWatcher);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap read and write buffers, and poll again
|
|
||||||
std::swap(mWriteBuffer, mReadBuffer);
|
|
||||||
poll();
|
|
||||||
|
|
||||||
// Read change events
|
|
||||||
BYTE *base = mReadBuffer.data();
|
|
||||||
while (true) {
|
|
||||||
PFILE_NOTIFY_INFORMATION info = (PFILE_NOTIFY_INFORMATION)base;
|
|
||||||
processEvent(info);
|
|
||||||
|
|
||||||
if (info->NextEntryOffset == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
base += info->NextEntryOffset;
|
|
||||||
}
|
|
||||||
|
|
||||||
mWatcher->notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
void processEvent(PFILE_NOTIFY_INFORMATION info) {
|
|
||||||
std::string path = mWatcher->mDir + "\\" + utf16ToUtf8(info->FileName, info->FileNameLength / sizeof(WCHAR));
|
|
||||||
if (mWatcher->isIgnored(path)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (info->Action) {
|
|
||||||
case FILE_ACTION_ADDED:
|
|
||||||
case FILE_ACTION_RENAMED_NEW_NAME: {
|
|
||||||
WIN32_FILE_ATTRIBUTE_DATA data;
|
|
||||||
if (GetFileAttributesExW(utf8ToUtf16(path).data(), GetFileExInfoStandard, &data)) {
|
|
||||||
mWatcher->mEvents.create(path);
|
|
||||||
mTree->add(path, CONVERT_TIME(data.ftLastWriteTime), data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case FILE_ACTION_MODIFIED: {
|
|
||||||
WIN32_FILE_ATTRIBUTE_DATA data;
|
|
||||||
if (GetFileAttributesExW(utf8ToUtf16(path).data(), GetFileExInfoStandard, &data)) {
|
|
||||||
mTree->update(path, CONVERT_TIME(data.ftLastWriteTime));
|
|
||||||
if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
|
||||||
mWatcher->mEvents.update(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case FILE_ACTION_REMOVED:
|
|
||||||
case FILE_ACTION_RENAMED_OLD_NAME:
|
|
||||||
mWatcher->mEvents.remove(path);
|
|
||||||
mTree->remove(path);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
WindowsBackend *mBackend;
|
|
||||||
std::shared_ptr<Watcher> mWatcher;
|
|
||||||
std::shared_ptr<DirTree> mTree;
|
|
||||||
bool mRunning;
|
|
||||||
HANDLE mDirectoryHandle;
|
|
||||||
std::vector<BYTE> mReadBuffer;
|
|
||||||
std::vector<BYTE> mWriteBuffer;
|
|
||||||
OVERLAPPED mOverlapped;
|
|
||||||
};
|
|
||||||
|
|
||||||
// This function is called by Backend::watch which takes a lock on mMutex
|
|
||||||
void WindowsBackend::subscribe(WatcherRef watcher) {
|
|
||||||
// Create a subscription for this watcher
|
|
||||||
auto sub = std::make_shared<Subscription>(this, watcher, getTree(watcher, false));
|
|
||||||
watcher->state = sub;
|
|
||||||
|
|
||||||
// Queue polling for this subscription in the correct thread.
|
|
||||||
bool success = QueueUserAPC([](__in ULONG_PTR ptr) {
|
|
||||||
Subscription *sub = (Subscription *)ptr;
|
|
||||||
sub->run();
|
|
||||||
}, mThread.native_handle(), (ULONG_PTR)sub.get());
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
throw std::runtime_error("Unable to queue APC");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function is called by Backend::unwatch which takes a lock on mMutex
|
|
||||||
void WindowsBackend::unsubscribe(WatcherRef watcher) {
|
|
||||||
watcher->state = nullptr;
|
|
||||||
}
|
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
#ifndef WINDOWS_H
|
|
||||||
#define WINDOWS_H
|
|
||||||
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#include "../shared/BruteForceBackend.hh"
|
|
||||||
|
|
||||||
class WindowsBackend : public BruteForceBackend {
|
|
||||||
public:
|
|
||||||
void start() override;
|
|
||||||
~WindowsBackend();
|
|
||||||
void subscribe(WatcherRef watcher) override;
|
|
||||||
void unsubscribe(WatcherRef watcher) override;
|
|
||||||
private:
|
|
||||||
bool mRunning;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-44
@@ -1,44 +0,0 @@
|
|||||||
#include "./win_utils.hh"
|
|
||||||
|
|
||||||
std::wstring utf8ToUtf16(std::string input) {
|
|
||||||
unsigned int len = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, NULL, 0);
|
|
||||||
WCHAR *output = new WCHAR[len];
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, output, len);
|
|
||||||
std::wstring res(output);
|
|
||||||
delete[] output;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string utf16ToUtf8(const WCHAR *input, DWORD length) {
|
|
||||||
unsigned int len = WideCharToMultiByte(CP_UTF8, 0, input, length, NULL, 0, NULL, NULL);
|
|
||||||
char *output = new char[len + 1];
|
|
||||||
WideCharToMultiByte(CP_UTF8, 0, input, length, output, len, NULL, NULL);
|
|
||||||
output[len] = '\0';
|
|
||||||
std::string res(output);
|
|
||||||
delete[] output;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string normalizePath(std::string path) {
|
|
||||||
// Prevent truncation to MAX_PATH characters by adding the \\?\ prefix
|
|
||||||
std::wstring p = utf8ToUtf16("\\\\?\\" + path);
|
|
||||||
|
|
||||||
// Get the required length for the output
|
|
||||||
DWORD len = GetLongPathNameW(p.data(), NULL, 0);
|
|
||||||
if (!len) {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allocate output array and get long path
|
|
||||||
WCHAR *output = new WCHAR[len];
|
|
||||||
len = GetLongPathNameW(p.data(), output, len);
|
|
||||||
if (!len) {
|
|
||||||
delete[] output;
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert back to utf8
|
|
||||||
std::string res = utf16ToUtf8(output + 4, len - 4);
|
|
||||||
delete[] output;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
#ifndef WIN_UTILS_H
|
|
||||||
#define WIN_UTILS_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
std::wstring utf8ToUtf16(std::string input);
|
|
||||||
std::string utf16ToUtf8(const WCHAR *input, DWORD length);
|
|
||||||
std::string normalizePath(std::string path);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
-74
@@ -1,74 +0,0 @@
|
|||||||
const path = require('path');
|
|
||||||
const picomatch = require('picomatch');
|
|
||||||
const isGlob = require('is-glob');
|
|
||||||
|
|
||||||
function normalizeOptions(dir, opts = {}) {
|
|
||||||
const { ignore, ...rest } = opts;
|
|
||||||
|
|
||||||
if (Array.isArray(ignore)) {
|
|
||||||
opts = { ...rest };
|
|
||||||
|
|
||||||
for (const value of ignore) {
|
|
||||||
if (isGlob(value)) {
|
|
||||||
if (!opts.ignoreGlobs) {
|
|
||||||
opts.ignoreGlobs = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const regex = picomatch.makeRe(value, {
|
|
||||||
// We set `dot: true` to workaround an issue with the
|
|
||||||
// regular expression on Linux where the resulting
|
|
||||||
// negative lookahead `(?!(\\/|^)` was never matching
|
|
||||||
// in some cases. See also https://bit.ly/3UZlQDm
|
|
||||||
dot: true,
|
|
||||||
windows: process.platform === 'win32',
|
|
||||||
});
|
|
||||||
opts.ignoreGlobs.push(regex.source);
|
|
||||||
} else {
|
|
||||||
if (!opts.ignorePaths) {
|
|
||||||
opts.ignorePaths = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
opts.ignorePaths.push(path.resolve(dir, value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.createWrapper = (binding) => {
|
|
||||||
return {
|
|
||||||
writeSnapshot(dir, snapshot, opts) {
|
|
||||||
return binding.writeSnapshot(
|
|
||||||
path.resolve(dir),
|
|
||||||
path.resolve(snapshot),
|
|
||||||
normalizeOptions(dir, opts),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
getEventsSince(dir, snapshot, opts) {
|
|
||||||
return binding.getEventsSince(
|
|
||||||
path.resolve(dir),
|
|
||||||
path.resolve(snapshot),
|
|
||||||
normalizeOptions(dir, opts),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
async subscribe(dir, fn, opts) {
|
|
||||||
dir = path.resolve(dir);
|
|
||||||
opts = normalizeOptions(dir, opts);
|
|
||||||
await binding.subscribe(dir, fn, opts);
|
|
||||||
|
|
||||||
return {
|
|
||||||
unsubscribe() {
|
|
||||||
return binding.unsubscribe(dir, fn, opts);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
unsubscribe(dir, fn, opts) {
|
|
||||||
return binding.unsubscribe(
|
|
||||||
path.resolve(dir),
|
|
||||||
fn,
|
|
||||||
normalizeOptions(dir, opts),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
-243
@@ -1,243 +0,0 @@
|
|||||||
1.3.8 / 2022-02-02
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.34
|
|
||||||
- deps: mime-db@~1.51.0
|
|
||||||
* deps: negotiator@0.6.3
|
|
||||||
|
|
||||||
1.3.7 / 2019-04-29
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.6.2
|
|
||||||
- Fix sorting charset, encoding, and language with extra parameters
|
|
||||||
|
|
||||||
1.3.6 / 2019-04-28
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.24
|
|
||||||
- deps: mime-db@~1.40.0
|
|
||||||
|
|
||||||
1.3.5 / 2018-02-28
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.18
|
|
||||||
- deps: mime-db@~1.33.0
|
|
||||||
|
|
||||||
1.3.4 / 2017-08-22
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.16
|
|
||||||
- deps: mime-db@~1.29.0
|
|
||||||
|
|
||||||
1.3.3 / 2016-05-02
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.11
|
|
||||||
- deps: mime-db@~1.23.0
|
|
||||||
* deps: negotiator@0.6.1
|
|
||||||
- perf: improve `Accept` parsing speed
|
|
||||||
- perf: improve `Accept-Charset` parsing speed
|
|
||||||
- perf: improve `Accept-Encoding` parsing speed
|
|
||||||
- perf: improve `Accept-Language` parsing speed
|
|
||||||
|
|
||||||
1.3.2 / 2016-03-08
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.10
|
|
||||||
- Fix extension of `application/dash+xml`
|
|
||||||
- Update primary extension for `audio/mp4`
|
|
||||||
- deps: mime-db@~1.22.0
|
|
||||||
|
|
||||||
1.3.1 / 2016-01-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.9
|
|
||||||
- deps: mime-db@~1.21.0
|
|
||||||
|
|
||||||
1.3.0 / 2015-09-29
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.7
|
|
||||||
- deps: mime-db@~1.19.0
|
|
||||||
* deps: negotiator@0.6.0
|
|
||||||
- Fix including type extensions in parameters in `Accept` parsing
|
|
||||||
- Fix parsing `Accept` parameters with quoted equals
|
|
||||||
- Fix parsing `Accept` parameters with quoted semicolons
|
|
||||||
- Lazy-load modules from main entry point
|
|
||||||
- perf: delay type concatenation until needed
|
|
||||||
- perf: enable strict mode
|
|
||||||
- perf: hoist regular expressions
|
|
||||||
- perf: remove closures getting spec properties
|
|
||||||
- perf: remove a closure from media type parsing
|
|
||||||
- perf: remove property delete from media type parsing
|
|
||||||
|
|
||||||
1.2.13 / 2015-09-06
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.6
|
|
||||||
- deps: mime-db@~1.18.0
|
|
||||||
|
|
||||||
1.2.12 / 2015-07-30
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.4
|
|
||||||
- deps: mime-db@~1.16.0
|
|
||||||
|
|
||||||
1.2.11 / 2015-07-16
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.3
|
|
||||||
- deps: mime-db@~1.15.0
|
|
||||||
|
|
||||||
1.2.10 / 2015-07-01
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.2
|
|
||||||
- deps: mime-db@~1.14.0
|
|
||||||
|
|
||||||
1.2.9 / 2015-06-08
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.1
|
|
||||||
- perf: fix deopt during mapping
|
|
||||||
|
|
||||||
1.2.8 / 2015-06-07
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.1.0
|
|
||||||
- deps: mime-db@~1.13.0
|
|
||||||
* perf: avoid argument reassignment & argument slice
|
|
||||||
* perf: avoid negotiator recursive construction
|
|
||||||
* perf: enable strict mode
|
|
||||||
* perf: remove unnecessary bitwise operator
|
|
||||||
|
|
||||||
1.2.7 / 2015-05-10
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.5.3
|
|
||||||
- Fix media type parameter matching to be case-insensitive
|
|
||||||
|
|
||||||
1.2.6 / 2015-05-07
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.11
|
|
||||||
- deps: mime-db@~1.9.1
|
|
||||||
* deps: negotiator@0.5.2
|
|
||||||
- Fix comparing media types with quoted values
|
|
||||||
- Fix splitting media types with quoted commas
|
|
||||||
|
|
||||||
1.2.5 / 2015-03-13
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.10
|
|
||||||
- deps: mime-db@~1.8.0
|
|
||||||
|
|
||||||
1.2.4 / 2015-02-14
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Support Node.js 0.6
|
|
||||||
* deps: mime-types@~2.0.9
|
|
||||||
- deps: mime-db@~1.7.0
|
|
||||||
* deps: negotiator@0.5.1
|
|
||||||
- Fix preference sorting to be stable for long acceptable lists
|
|
||||||
|
|
||||||
1.2.3 / 2015-01-31
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.8
|
|
||||||
- deps: mime-db@~1.6.0
|
|
||||||
|
|
||||||
1.2.2 / 2014-12-30
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.7
|
|
||||||
- deps: mime-db@~1.5.0
|
|
||||||
|
|
||||||
1.2.1 / 2014-12-30
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.5
|
|
||||||
- deps: mime-db@~1.3.1
|
|
||||||
|
|
||||||
1.2.0 / 2014-12-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.5.0
|
|
||||||
- Fix list return order when large accepted list
|
|
||||||
- Fix missing identity encoding when q=0 exists
|
|
||||||
- Remove dynamic building of Negotiator class
|
|
||||||
|
|
||||||
1.1.4 / 2014-12-10
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.4
|
|
||||||
- deps: mime-db@~1.3.0
|
|
||||||
|
|
||||||
1.1.3 / 2014-11-09
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.3
|
|
||||||
- deps: mime-db@~1.2.0
|
|
||||||
|
|
||||||
1.1.2 / 2014-10-14
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.4.9
|
|
||||||
- Fix error when media type has invalid parameter
|
|
||||||
|
|
||||||
1.1.1 / 2014-09-28
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: mime-types@~2.0.2
|
|
||||||
- deps: mime-db@~1.1.0
|
|
||||||
* deps: negotiator@0.4.8
|
|
||||||
- Fix all negotiations to be case-insensitive
|
|
||||||
- Stable sort preferences of same quality according to client order
|
|
||||||
|
|
||||||
1.1.0 / 2014-09-02
|
|
||||||
==================
|
|
||||||
|
|
||||||
* update `mime-types`
|
|
||||||
|
|
||||||
1.0.7 / 2014-07-04
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Fix wrong type returned from `type` when match after unknown extension
|
|
||||||
|
|
||||||
1.0.6 / 2014-06-24
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.4.7
|
|
||||||
|
|
||||||
1.0.5 / 2014-06-20
|
|
||||||
==================
|
|
||||||
|
|
||||||
* fix crash when unknown extension given
|
|
||||||
|
|
||||||
1.0.4 / 2014-06-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* use `mime-types`
|
|
||||||
|
|
||||||
1.0.3 / 2014-06-11
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: negotiator@0.4.6
|
|
||||||
- Order by specificity when quality is the same
|
|
||||||
|
|
||||||
1.0.2 / 2014-05-29
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Fix interpretation when header not in request
|
|
||||||
* deps: pin negotiator@0.4.5
|
|
||||||
|
|
||||||
1.0.1 / 2014-01-18
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Identity encoding isn't always acceptable
|
|
||||||
* deps: negotiator@~0.4.0
|
|
||||||
|
|
||||||
1.0.0 / 2013-12-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Genesis
|
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
(The MIT License)
|
|
||||||
|
|
||||||
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
|
|
||||||
Copyright (c) 2015 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
'Software'), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
-140
@@ -1,140 +0,0 @@
|
|||||||
# accepts
|
|
||||||
|
|
||||||
[![NPM Version][npm-version-image]][npm-url]
|
|
||||||
[![NPM Downloads][npm-downloads-image]][npm-url]
|
|
||||||
[![Node.js Version][node-version-image]][node-version-url]
|
|
||||||
[![Build Status][github-actions-ci-image]][github-actions-ci-url]
|
|
||||||
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
||||||
|
|
||||||
Higher level content negotiation based on [negotiator](https://www.npmjs.com/package/negotiator).
|
|
||||||
Extracted from [koa](https://www.npmjs.com/package/koa) for general use.
|
|
||||||
|
|
||||||
In addition to negotiator, it allows:
|
|
||||||
|
|
||||||
- Allows types as an array or arguments list, ie `(['text/html', 'application/json'])`
|
|
||||||
as well as `('text/html', 'application/json')`.
|
|
||||||
- Allows type shorthands such as `json`.
|
|
||||||
- Returns `false` when no types match
|
|
||||||
- Treats non-existent headers as `*`
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
||||||
[npm registry](https://www.npmjs.com/). Installation is done using the
|
|
||||||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ npm install accepts
|
|
||||||
```
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
```js
|
|
||||||
var accepts = require('accepts')
|
|
||||||
```
|
|
||||||
|
|
||||||
### accepts(req)
|
|
||||||
|
|
||||||
Create a new `Accepts` object for the given `req`.
|
|
||||||
|
|
||||||
#### .charset(charsets)
|
|
||||||
|
|
||||||
Return the first accepted charset. If nothing in `charsets` is accepted,
|
|
||||||
then `false` is returned.
|
|
||||||
|
|
||||||
#### .charsets()
|
|
||||||
|
|
||||||
Return the charsets that the request accepts, in the order of the client's
|
|
||||||
preference (most preferred first).
|
|
||||||
|
|
||||||
#### .encoding(encodings)
|
|
||||||
|
|
||||||
Return the first accepted encoding. If nothing in `encodings` is accepted,
|
|
||||||
then `false` is returned.
|
|
||||||
|
|
||||||
#### .encodings()
|
|
||||||
|
|
||||||
Return the encodings that the request accepts, in the order of the client's
|
|
||||||
preference (most preferred first).
|
|
||||||
|
|
||||||
#### .language(languages)
|
|
||||||
|
|
||||||
Return the first accepted language. If nothing in `languages` is accepted,
|
|
||||||
then `false` is returned.
|
|
||||||
|
|
||||||
#### .languages()
|
|
||||||
|
|
||||||
Return the languages that the request accepts, in the order of the client's
|
|
||||||
preference (most preferred first).
|
|
||||||
|
|
||||||
#### .type(types)
|
|
||||||
|
|
||||||
Return the first accepted type (and it is returned as the same text as what
|
|
||||||
appears in the `types` array). If nothing in `types` is accepted, then `false`
|
|
||||||
is returned.
|
|
||||||
|
|
||||||
The `types` array can contain full MIME types or file extensions. Any value
|
|
||||||
that is not a full MIME types is passed to `require('mime-types').lookup`.
|
|
||||||
|
|
||||||
#### .types()
|
|
||||||
|
|
||||||
Return the types that the request accepts, in the order of the client's
|
|
||||||
preference (most preferred first).
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
### Simple type negotiation
|
|
||||||
|
|
||||||
This simple example shows how to use `accepts` to return a different typed
|
|
||||||
respond body based on what the client wants to accept. The server lists it's
|
|
||||||
preferences in order and will get back the best match between the client and
|
|
||||||
server.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var accepts = require('accepts')
|
|
||||||
var http = require('http')
|
|
||||||
|
|
||||||
function app (req, res) {
|
|
||||||
var accept = accepts(req)
|
|
||||||
|
|
||||||
// the order of this list is significant; should be server preferred order
|
|
||||||
switch (accept.type(['json', 'html'])) {
|
|
||||||
case 'json':
|
|
||||||
res.setHeader('Content-Type', 'application/json')
|
|
||||||
res.write('{"hello":"world!"}')
|
|
||||||
break
|
|
||||||
case 'html':
|
|
||||||
res.setHeader('Content-Type', 'text/html')
|
|
||||||
res.write('<b>hello, world!</b>')
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
// the fallback is text/plain, so no need to specify it above
|
|
||||||
res.setHeader('Content-Type', 'text/plain')
|
|
||||||
res.write('hello, world!')
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
res.end()
|
|
||||||
}
|
|
||||||
|
|
||||||
http.createServer(app).listen(3000)
|
|
||||||
```
|
|
||||||
|
|
||||||
You can test this out with the cURL program:
|
|
||||||
```sh
|
|
||||||
curl -I -H'Accept: text/html' http://localhost:3000/
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
[MIT](LICENSE)
|
|
||||||
|
|
||||||
[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/accepts/master
|
|
||||||
[coveralls-url]: https://coveralls.io/r/jshttp/accepts?branch=master
|
|
||||||
[github-actions-ci-image]: https://badgen.net/github/checks/jshttp/accepts/master?label=ci
|
|
||||||
[github-actions-ci-url]: https://github.com/jshttp/accepts/actions/workflows/ci.yml
|
|
||||||
[node-version-image]: https://badgen.net/npm/node/accepts
|
|
||||||
[node-version-url]: https://nodejs.org/en/download
|
|
||||||
[npm-downloads-image]: https://badgen.net/npm/dm/accepts
|
|
||||||
[npm-url]: https://npmjs.org/package/accepts
|
|
||||||
[npm-version-image]: https://badgen.net/npm/v/accepts
|
|
||||||
-238
@@ -1,238 +0,0 @@
|
|||||||
/*!
|
|
||||||
* accepts
|
|
||||||
* Copyright(c) 2014 Jonathan Ong
|
|
||||||
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var Negotiator = require('negotiator')
|
|
||||||
var mime = require('mime-types')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = Accepts
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new Accepts object for the given req.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function Accepts (req) {
|
|
||||||
if (!(this instanceof Accepts)) {
|
|
||||||
return new Accepts(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.headers = req.headers
|
|
||||||
this.negotiator = new Negotiator(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the given `type(s)` is acceptable, returning
|
|
||||||
* the best match when true, otherwise `undefined`, in which
|
|
||||||
* case you should respond with 406 "Not Acceptable".
|
|
||||||
*
|
|
||||||
* The `type` value may be a single mime type string
|
|
||||||
* such as "application/json", the extension name
|
|
||||||
* such as "json" or an array `["json", "html", "text/plain"]`. When a list
|
|
||||||
* or array is given the _best_ match, if any is returned.
|
|
||||||
*
|
|
||||||
* Examples:
|
|
||||||
*
|
|
||||||
* // Accept: text/html
|
|
||||||
* this.types('html');
|
|
||||||
* // => "html"
|
|
||||||
*
|
|
||||||
* // Accept: text/*, application/json
|
|
||||||
* this.types('html');
|
|
||||||
* // => "html"
|
|
||||||
* this.types('text/html');
|
|
||||||
* // => "text/html"
|
|
||||||
* this.types('json', 'text');
|
|
||||||
* // => "json"
|
|
||||||
* this.types('application/json');
|
|
||||||
* // => "application/json"
|
|
||||||
*
|
|
||||||
* // Accept: text/*, application/json
|
|
||||||
* this.types('image/png');
|
|
||||||
* this.types('png');
|
|
||||||
* // => undefined
|
|
||||||
*
|
|
||||||
* // Accept: text/*;q=.5, application/json
|
|
||||||
* this.types(['html', 'json']);
|
|
||||||
* this.types('html', 'json');
|
|
||||||
* // => "json"
|
|
||||||
*
|
|
||||||
* @param {String|Array} types...
|
|
||||||
* @return {String|Array|Boolean}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Accepts.prototype.type =
|
|
||||||
Accepts.prototype.types = function (types_) {
|
|
||||||
var types = types_
|
|
||||||
|
|
||||||
// support flattened arguments
|
|
||||||
if (types && !Array.isArray(types)) {
|
|
||||||
types = new Array(arguments.length)
|
|
||||||
for (var i = 0; i < types.length; i++) {
|
|
||||||
types[i] = arguments[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no types, return all requested types
|
|
||||||
if (!types || types.length === 0) {
|
|
||||||
return this.negotiator.mediaTypes()
|
|
||||||
}
|
|
||||||
|
|
||||||
// no accept header, return first given type
|
|
||||||
if (!this.headers.accept) {
|
|
||||||
return types[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
var mimes = types.map(extToMime)
|
|
||||||
var accepts = this.negotiator.mediaTypes(mimes.filter(validMime))
|
|
||||||
var first = accepts[0]
|
|
||||||
|
|
||||||
return first
|
|
||||||
? types[mimes.indexOf(first)]
|
|
||||||
: false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return accepted encodings or best fit based on `encodings`.
|
|
||||||
*
|
|
||||||
* Given `Accept-Encoding: gzip, deflate`
|
|
||||||
* an array sorted by quality is returned:
|
|
||||||
*
|
|
||||||
* ['gzip', 'deflate']
|
|
||||||
*
|
|
||||||
* @param {String|Array} encodings...
|
|
||||||
* @return {String|Array}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Accepts.prototype.encoding =
|
|
||||||
Accepts.prototype.encodings = function (encodings_) {
|
|
||||||
var encodings = encodings_
|
|
||||||
|
|
||||||
// support flattened arguments
|
|
||||||
if (encodings && !Array.isArray(encodings)) {
|
|
||||||
encodings = new Array(arguments.length)
|
|
||||||
for (var i = 0; i < encodings.length; i++) {
|
|
||||||
encodings[i] = arguments[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no encodings, return all requested encodings
|
|
||||||
if (!encodings || encodings.length === 0) {
|
|
||||||
return this.negotiator.encodings()
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.negotiator.encodings(encodings)[0] || false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return accepted charsets or best fit based on `charsets`.
|
|
||||||
*
|
|
||||||
* Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
|
|
||||||
* an array sorted by quality is returned:
|
|
||||||
*
|
|
||||||
* ['utf-8', 'utf-7', 'iso-8859-1']
|
|
||||||
*
|
|
||||||
* @param {String|Array} charsets...
|
|
||||||
* @return {String|Array}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Accepts.prototype.charset =
|
|
||||||
Accepts.prototype.charsets = function (charsets_) {
|
|
||||||
var charsets = charsets_
|
|
||||||
|
|
||||||
// support flattened arguments
|
|
||||||
if (charsets && !Array.isArray(charsets)) {
|
|
||||||
charsets = new Array(arguments.length)
|
|
||||||
for (var i = 0; i < charsets.length; i++) {
|
|
||||||
charsets[i] = arguments[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no charsets, return all requested charsets
|
|
||||||
if (!charsets || charsets.length === 0) {
|
|
||||||
return this.negotiator.charsets()
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.negotiator.charsets(charsets)[0] || false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return accepted languages or best fit based on `langs`.
|
|
||||||
*
|
|
||||||
* Given `Accept-Language: en;q=0.8, es, pt`
|
|
||||||
* an array sorted by quality is returned:
|
|
||||||
*
|
|
||||||
* ['es', 'pt', 'en']
|
|
||||||
*
|
|
||||||
* @param {String|Array} langs...
|
|
||||||
* @return {Array|String}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Accepts.prototype.lang =
|
|
||||||
Accepts.prototype.langs =
|
|
||||||
Accepts.prototype.language =
|
|
||||||
Accepts.prototype.languages = function (languages_) {
|
|
||||||
var languages = languages_
|
|
||||||
|
|
||||||
// support flattened arguments
|
|
||||||
if (languages && !Array.isArray(languages)) {
|
|
||||||
languages = new Array(arguments.length)
|
|
||||||
for (var i = 0; i < languages.length; i++) {
|
|
||||||
languages[i] = arguments[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// no languages, return all requested languages
|
|
||||||
if (!languages || languages.length === 0) {
|
|
||||||
return this.negotiator.languages()
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.negotiator.languages(languages)[0] || false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert extnames to mime.
|
|
||||||
*
|
|
||||||
* @param {String} type
|
|
||||||
* @return {String}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function extToMime (type) {
|
|
||||||
return type.indexOf('/') === -1
|
|
||||||
? mime.lookup(type)
|
|
||||||
: type
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if mime is valid.
|
|
||||||
*
|
|
||||||
* @param {String} type
|
|
||||||
* @return {String}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function validMime (type) {
|
|
||||||
return typeof type === 'string'
|
|
||||||
}
|
|
||||||
-47
@@ -1,47 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "accepts",
|
|
||||||
"description": "Higher-level content negotiation",
|
|
||||||
"version": "1.3.8",
|
|
||||||
"contributors": [
|
|
||||||
"Douglas Christopher Wilson <doug@somethingdoug.com>",
|
|
||||||
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
|
||||||
"repository": "jshttp/accepts",
|
|
||||||
"dependencies": {
|
|
||||||
"mime-types": "~2.1.34",
|
|
||||||
"negotiator": "0.6.3"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"deep-equal": "1.0.1",
|
|
||||||
"eslint": "7.32.0",
|
|
||||||
"eslint-config-standard": "14.1.1",
|
|
||||||
"eslint-plugin-import": "2.25.4",
|
|
||||||
"eslint-plugin-markdown": "2.2.1",
|
|
||||||
"eslint-plugin-node": "11.1.0",
|
|
||||||
"eslint-plugin-promise": "4.3.1",
|
|
||||||
"eslint-plugin-standard": "4.1.0",
|
|
||||||
"mocha": "9.2.0",
|
|
||||||
"nyc": "15.1.0"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"LICENSE",
|
|
||||||
"HISTORY.md",
|
|
||||||
"index.js"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 0.6"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"lint": "eslint .",
|
|
||||||
"test": "mocha --reporter spec --check-leaks --bail test/",
|
|
||||||
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
|
|
||||||
"test-cov": "nyc --reporter=html --reporter=text npm test"
|
|
||||||
},
|
|
||||||
"keywords": [
|
|
||||||
"content",
|
|
||||||
"negotiation",
|
|
||||||
"accept",
|
|
||||||
"accepts"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com)
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
-43
@@ -1,43 +0,0 @@
|
|||||||
# Array Flatten
|
|
||||||
|
|
||||||
[![NPM version][npm-image]][npm-url]
|
|
||||||
[![NPM downloads][downloads-image]][downloads-url]
|
|
||||||
[![Build status][travis-image]][travis-url]
|
|
||||||
[![Test coverage][coveralls-image]][coveralls-url]
|
|
||||||
|
|
||||||
> Flatten an array of nested arrays into a single flat array. Accepts an optional depth.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```
|
|
||||||
npm install array-flatten --save
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var flatten = require('array-flatten')
|
|
||||||
|
|
||||||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9])
|
|
||||||
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
||||||
|
|
||||||
flatten([1, [2, [3, [4, [5], 6], 7], 8], 9], 2)
|
|
||||||
//=> [1, 2, 3, [4, [5], 6], 7, 8, 9]
|
|
||||||
|
|
||||||
(function () {
|
|
||||||
flatten(arguments) //=> [1, 2, 3]
|
|
||||||
})(1, [2, 3])
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
MIT
|
|
||||||
|
|
||||||
[npm-image]: https://img.shields.io/npm/v/array-flatten.svg?style=flat
|
|
||||||
[npm-url]: https://npmjs.org/package/array-flatten
|
|
||||||
[downloads-image]: https://img.shields.io/npm/dm/array-flatten.svg?style=flat
|
|
||||||
[downloads-url]: https://npmjs.org/package/array-flatten
|
|
||||||
[travis-image]: https://img.shields.io/travis/blakeembrey/array-flatten.svg?style=flat
|
|
||||||
[travis-url]: https://travis-ci.org/blakeembrey/array-flatten
|
|
||||||
[coveralls-image]: https://img.shields.io/coveralls/blakeembrey/array-flatten.svg?style=flat
|
|
||||||
[coveralls-url]: https://coveralls.io/r/blakeembrey/array-flatten?branch=master
|
|
||||||
-64
@@ -1,64 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Expose `arrayFlatten`.
|
|
||||||
*/
|
|
||||||
module.exports = arrayFlatten
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursive flatten function with depth.
|
|
||||||
*
|
|
||||||
* @param {Array} array
|
|
||||||
* @param {Array} result
|
|
||||||
* @param {Number} depth
|
|
||||||
* @return {Array}
|
|
||||||
*/
|
|
||||||
function flattenWithDepth (array, result, depth) {
|
|
||||||
for (var i = 0; i < array.length; i++) {
|
|
||||||
var value = array[i]
|
|
||||||
|
|
||||||
if (depth > 0 && Array.isArray(value)) {
|
|
||||||
flattenWithDepth(value, result, depth - 1)
|
|
||||||
} else {
|
|
||||||
result.push(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Recursive flatten function. Omitting depth is slightly faster.
|
|
||||||
*
|
|
||||||
* @param {Array} array
|
|
||||||
* @param {Array} result
|
|
||||||
* @return {Array}
|
|
||||||
*/
|
|
||||||
function flattenForever (array, result) {
|
|
||||||
for (var i = 0; i < array.length; i++) {
|
|
||||||
var value = array[i]
|
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
flattenForever(value, result)
|
|
||||||
} else {
|
|
||||||
result.push(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flatten an array, with the ability to define a depth.
|
|
||||||
*
|
|
||||||
* @param {Array} array
|
|
||||||
* @param {Number} depth
|
|
||||||
* @return {Array}
|
|
||||||
*/
|
|
||||||
function arrayFlatten (array, depth) {
|
|
||||||
if (depth == null) {
|
|
||||||
return flattenForever(array, [])
|
|
||||||
}
|
|
||||||
|
|
||||||
return flattenWithDepth(array, [], depth)
|
|
||||||
}
|
|
||||||
-39
@@ -1,39 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "array-flatten",
|
|
||||||
"version": "1.1.1",
|
|
||||||
"description": "Flatten an array of nested arrays into a single flat array",
|
|
||||||
"main": "array-flatten.js",
|
|
||||||
"files": [
|
|
||||||
"array-flatten.js",
|
|
||||||
"LICENSE"
|
|
||||||
],
|
|
||||||
"scripts": {
|
|
||||||
"test": "istanbul cover _mocha -- -R spec"
|
|
||||||
},
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "git://github.com/blakeembrey/array-flatten.git"
|
|
||||||
},
|
|
||||||
"keywords": [
|
|
||||||
"array",
|
|
||||||
"flatten",
|
|
||||||
"arguments",
|
|
||||||
"depth"
|
|
||||||
],
|
|
||||||
"author": {
|
|
||||||
"name": "Blake Embrey",
|
|
||||||
"email": "hello@blakeembrey.com",
|
|
||||||
"url": "http://blakeembrey.me"
|
|
||||||
},
|
|
||||||
"license": "MIT",
|
|
||||||
"bugs": {
|
|
||||||
"url": "https://github.com/blakeembrey/array-flatten/issues"
|
|
||||||
},
|
|
||||||
"homepage": "https://github.com/blakeembrey/array-flatten",
|
|
||||||
"devDependencies": {
|
|
||||||
"istanbul": "^0.3.13",
|
|
||||||
"mocha": "^2.2.4",
|
|
||||||
"pre-commit": "^1.0.7",
|
|
||||||
"standard": "^3.7.3"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-686
@@ -1,686 +0,0 @@
|
|||||||
1.20.5 / 2026-04-24
|
|
||||||
===================
|
|
||||||
* refactor(json): simplify strict mode error string construction
|
|
||||||
* fix: extended urlencoded parsing of arrays with >100 elements (#716)
|
|
||||||
* deps: qs@~6.15.1
|
|
||||||
|
|
||||||
1.20.4 / 2025-12-01
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@~6.14.0
|
|
||||||
* deps: use tilde notation for dependencies
|
|
||||||
* deps: http-errors@~2.0.1
|
|
||||||
* deps: raw-body@~2.5.3
|
|
||||||
|
|
||||||
1.20.3 / 2024-09-10
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@6.13.0
|
|
||||||
* add `depth` option to customize the depth level in the parser
|
|
||||||
* IMPORTANT: The default `depth` level for parsing URL-encoded data is now `32` (previously was `Infinity`)
|
|
||||||
|
|
||||||
1.20.2 / 2023-02-21
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix strict json error message on Node.js 19+
|
|
||||||
* deps: content-type@~1.0.5
|
|
||||||
- perf: skip value escaping when unnecessary
|
|
||||||
* deps: raw-body@2.5.2
|
|
||||||
|
|
||||||
1.20.1 / 2022-10-06
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@6.11.0
|
|
||||||
* perf: remove unnecessary object clone
|
|
||||||
|
|
||||||
1.20.0 / 2022-04-02
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix error message for json parse whitespace in `strict`
|
|
||||||
* Fix internal error when inflated body exceeds limit
|
|
||||||
* Prevent loss of async hooks context
|
|
||||||
* Prevent hanging when request already read
|
|
||||||
* deps: depd@2.0.0
|
|
||||||
- Replace internal `eval` usage with `Function` constructor
|
|
||||||
- Use instance methods on `process` to check for listeners
|
|
||||||
* deps: http-errors@2.0.0
|
|
||||||
- deps: depd@2.0.0
|
|
||||||
- deps: statuses@2.0.1
|
|
||||||
* deps: on-finished@2.4.1
|
|
||||||
* deps: qs@6.10.3
|
|
||||||
* deps: raw-body@2.5.1
|
|
||||||
- deps: http-errors@2.0.0
|
|
||||||
|
|
||||||
1.19.2 / 2022-02-15
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@3.1.2
|
|
||||||
* deps: qs@6.9.7
|
|
||||||
* Fix handling of `__proto__` keys
|
|
||||||
* deps: raw-body@2.4.3
|
|
||||||
- deps: bytes@3.1.2
|
|
||||||
|
|
||||||
1.19.1 / 2021-12-10
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@3.1.1
|
|
||||||
* deps: http-errors@1.8.1
|
|
||||||
- deps: inherits@2.0.4
|
|
||||||
- deps: toidentifier@1.0.1
|
|
||||||
- deps: setprototypeof@1.2.0
|
|
||||||
* deps: qs@6.9.6
|
|
||||||
* deps: raw-body@2.4.2
|
|
||||||
- deps: bytes@3.1.1
|
|
||||||
- deps: http-errors@1.8.1
|
|
||||||
* deps: safe-buffer@5.2.1
|
|
||||||
* deps: type-is@~1.6.18
|
|
||||||
|
|
||||||
1.19.0 / 2019-04-25
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@3.1.0
|
|
||||||
- Add petabyte (`pb`) support
|
|
||||||
* deps: http-errors@1.7.2
|
|
||||||
- Set constructor name when possible
|
|
||||||
- deps: setprototypeof@1.1.1
|
|
||||||
- deps: statuses@'>= 1.5.0 < 2'
|
|
||||||
* deps: iconv-lite@0.4.24
|
|
||||||
- Added encoding MIK
|
|
||||||
* deps: qs@6.7.0
|
|
||||||
- Fix parsing array brackets after index
|
|
||||||
* deps: raw-body@2.4.0
|
|
||||||
- deps: bytes@3.1.0
|
|
||||||
- deps: http-errors@1.7.2
|
|
||||||
- deps: iconv-lite@0.4.24
|
|
||||||
* deps: type-is@~1.6.17
|
|
||||||
- deps: mime-types@~2.1.24
|
|
||||||
- perf: prevent internal `throw` on invalid type
|
|
||||||
|
|
||||||
1.18.3 / 2018-05-14
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix stack trace for strict json parse error
|
|
||||||
* deps: depd@~1.1.2
|
|
||||||
- perf: remove argument reassignment
|
|
||||||
* deps: http-errors@~1.6.3
|
|
||||||
- deps: depd@~1.1.2
|
|
||||||
- deps: setprototypeof@1.1.0
|
|
||||||
- deps: statuses@'>= 1.3.1 < 2'
|
|
||||||
* deps: iconv-lite@0.4.23
|
|
||||||
- Fix loading encoding with year appended
|
|
||||||
- Fix deprecation warnings on Node.js 10+
|
|
||||||
* deps: qs@6.5.2
|
|
||||||
* deps: raw-body@2.3.3
|
|
||||||
- deps: http-errors@1.6.3
|
|
||||||
- deps: iconv-lite@0.4.23
|
|
||||||
* deps: type-is@~1.6.16
|
|
||||||
- deps: mime-types@~2.1.18
|
|
||||||
|
|
||||||
1.18.2 / 2017-09-22
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@2.6.9
|
|
||||||
* perf: remove argument reassignment
|
|
||||||
|
|
||||||
1.18.1 / 2017-09-12
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: content-type@~1.0.4
|
|
||||||
- perf: remove argument reassignment
|
|
||||||
- perf: skip parameter parsing when no parameters
|
|
||||||
* deps: iconv-lite@0.4.19
|
|
||||||
- Fix ISO-8859-1 regression
|
|
||||||
- Update Windows-1255
|
|
||||||
* deps: qs@6.5.1
|
|
||||||
- Fix parsing & compacting very deep objects
|
|
||||||
* deps: raw-body@2.3.2
|
|
||||||
- deps: iconv-lite@0.4.19
|
|
||||||
|
|
||||||
1.18.0 / 2017-09-08
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix JSON strict violation error to match native parse error
|
|
||||||
* Include the `body` property on verify errors
|
|
||||||
* Include the `type` property on all generated errors
|
|
||||||
* Use `http-errors` to set status code on errors
|
|
||||||
* deps: bytes@3.0.0
|
|
||||||
* deps: debug@2.6.8
|
|
||||||
* deps: depd@~1.1.1
|
|
||||||
- Remove unnecessary `Buffer` loading
|
|
||||||
* deps: http-errors@~1.6.2
|
|
||||||
- deps: depd@1.1.1
|
|
||||||
* deps: iconv-lite@0.4.18
|
|
||||||
- Add support for React Native
|
|
||||||
- Add a warning if not loaded as utf-8
|
|
||||||
- Fix CESU-8 decoding in Node.js 8
|
|
||||||
- Improve speed of ISO-8859-1 encoding
|
|
||||||
* deps: qs@6.5.0
|
|
||||||
* deps: raw-body@2.3.1
|
|
||||||
- Use `http-errors` for standard emitted errors
|
|
||||||
- deps: bytes@3.0.0
|
|
||||||
- deps: iconv-lite@0.4.18
|
|
||||||
- perf: skip buffer decoding on overage chunk
|
|
||||||
* perf: prevent internal `throw` when missing charset
|
|
||||||
|
|
||||||
1.17.2 / 2017-05-17
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@2.6.7
|
|
||||||
- Fix `DEBUG_MAX_ARRAY_LENGTH`
|
|
||||||
- deps: ms@2.0.0
|
|
||||||
* deps: type-is@~1.6.15
|
|
||||||
- deps: mime-types@~2.1.15
|
|
||||||
|
|
||||||
1.17.1 / 2017-03-06
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@6.4.0
|
|
||||||
- Fix regression parsing keys starting with `[`
|
|
||||||
|
|
||||||
1.17.0 / 2017-03-01
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: http-errors@~1.6.1
|
|
||||||
- Make `message` property enumerable for `HttpError`s
|
|
||||||
- deps: setprototypeof@1.0.3
|
|
||||||
* deps: qs@6.3.1
|
|
||||||
- Fix compacting nested arrays
|
|
||||||
|
|
||||||
1.16.1 / 2017-02-10
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@2.6.1
|
|
||||||
- Fix deprecation messages in WebStorm and other editors
|
|
||||||
- Undeprecate `DEBUG_FD` set to `1` or `2`
|
|
||||||
|
|
||||||
1.16.0 / 2017-01-17
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@2.6.0
|
|
||||||
- Allow colors in workers
|
|
||||||
- Deprecated `DEBUG_FD` environment variable
|
|
||||||
- Fix error when running under React Native
|
|
||||||
- Use same color for same namespace
|
|
||||||
- deps: ms@0.7.2
|
|
||||||
* deps: http-errors@~1.5.1
|
|
||||||
- deps: inherits@2.0.3
|
|
||||||
- deps: setprototypeof@1.0.2
|
|
||||||
- deps: statuses@'>= 1.3.1 < 2'
|
|
||||||
* deps: iconv-lite@0.4.15
|
|
||||||
- Added encoding MS-31J
|
|
||||||
- Added encoding MS-932
|
|
||||||
- Added encoding MS-936
|
|
||||||
- Added encoding MS-949
|
|
||||||
- Added encoding MS-950
|
|
||||||
- Fix GBK/GB18030 handling of Euro character
|
|
||||||
* deps: qs@6.2.1
|
|
||||||
- Fix array parsing from skipping empty values
|
|
||||||
* deps: raw-body@~2.2.0
|
|
||||||
- deps: iconv-lite@0.4.15
|
|
||||||
* deps: type-is@~1.6.14
|
|
||||||
- deps: mime-types@~2.1.13
|
|
||||||
|
|
||||||
1.15.2 / 2016-06-19
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@2.4.0
|
|
||||||
* deps: content-type@~1.0.2
|
|
||||||
- perf: enable strict mode
|
|
||||||
* deps: http-errors@~1.5.0
|
|
||||||
- Use `setprototypeof` module to replace `__proto__` setting
|
|
||||||
- deps: statuses@'>= 1.3.0 < 2'
|
|
||||||
- perf: enable strict mode
|
|
||||||
* deps: qs@6.2.0
|
|
||||||
* deps: raw-body@~2.1.7
|
|
||||||
- deps: bytes@2.4.0
|
|
||||||
- perf: remove double-cleanup on happy path
|
|
||||||
* deps: type-is@~1.6.13
|
|
||||||
- deps: mime-types@~2.1.11
|
|
||||||
|
|
||||||
1.15.1 / 2016-05-05
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@2.3.0
|
|
||||||
- Drop partial bytes on all parsed units
|
|
||||||
- Fix parsing byte string that looks like hex
|
|
||||||
* deps: raw-body@~2.1.6
|
|
||||||
- deps: bytes@2.3.0
|
|
||||||
* deps: type-is@~1.6.12
|
|
||||||
- deps: mime-types@~2.1.10
|
|
||||||
|
|
||||||
1.15.0 / 2016-02-10
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: http-errors@~1.4.0
|
|
||||||
- Add `HttpError` export, for `err instanceof createError.HttpError`
|
|
||||||
- deps: inherits@2.0.1
|
|
||||||
- deps: statuses@'>= 1.2.1 < 2'
|
|
||||||
* deps: qs@6.1.0
|
|
||||||
* deps: type-is@~1.6.11
|
|
||||||
- deps: mime-types@~2.1.9
|
|
||||||
|
|
||||||
1.14.2 / 2015-12-16
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: bytes@2.2.0
|
|
||||||
* deps: iconv-lite@0.4.13
|
|
||||||
* deps: qs@5.2.0
|
|
||||||
* deps: raw-body@~2.1.5
|
|
||||||
- deps: bytes@2.2.0
|
|
||||||
- deps: iconv-lite@0.4.13
|
|
||||||
* deps: type-is@~1.6.10
|
|
||||||
- deps: mime-types@~2.1.8
|
|
||||||
|
|
||||||
1.14.1 / 2015-09-27
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix issue where invalid charset results in 400 when `verify` used
|
|
||||||
* deps: iconv-lite@0.4.12
|
|
||||||
- Fix CESU-8 decoding in Node.js 4.x
|
|
||||||
* deps: raw-body@~2.1.4
|
|
||||||
- Fix masking critical errors from `iconv-lite`
|
|
||||||
- deps: iconv-lite@0.4.12
|
|
||||||
* deps: type-is@~1.6.9
|
|
||||||
- deps: mime-types@~2.1.7
|
|
||||||
|
|
||||||
1.14.0 / 2015-09-16
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Fix JSON strict parse error to match syntax errors
|
|
||||||
* Provide static `require` analysis in `urlencoded` parser
|
|
||||||
* deps: depd@~1.1.0
|
|
||||||
- Support web browser loading
|
|
||||||
* deps: qs@5.1.0
|
|
||||||
* deps: raw-body@~2.1.3
|
|
||||||
- Fix sync callback when attaching data listener causes sync read
|
|
||||||
* deps: type-is@~1.6.8
|
|
||||||
- Fix type error when given invalid type to match against
|
|
||||||
- deps: mime-types@~2.1.6
|
|
||||||
|
|
||||||
1.13.3 / 2015-07-31
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: type-is@~1.6.6
|
|
||||||
- deps: mime-types@~2.1.4
|
|
||||||
|
|
||||||
1.13.2 / 2015-07-05
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: iconv-lite@0.4.11
|
|
||||||
* deps: qs@4.0.0
|
|
||||||
- Fix dropping parameters like `hasOwnProperty`
|
|
||||||
- Fix user-visible incompatibilities from 3.1.0
|
|
||||||
- Fix various parsing edge cases
|
|
||||||
* deps: raw-body@~2.1.2
|
|
||||||
- Fix error stack traces to skip `makeError`
|
|
||||||
- deps: iconv-lite@0.4.11
|
|
||||||
* deps: type-is@~1.6.4
|
|
||||||
- deps: mime-types@~2.1.2
|
|
||||||
- perf: enable strict mode
|
|
||||||
- perf: remove argument reassignment
|
|
||||||
|
|
||||||
1.13.1 / 2015-06-16
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@2.4.2
|
|
||||||
- Downgraded from 3.1.0 because of user-visible incompatibilities
|
|
||||||
|
|
||||||
1.13.0 / 2015-06-14
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Add `statusCode` property on `Error`s, in addition to `status`
|
|
||||||
* Change `type` default to `application/json` for JSON parser
|
|
||||||
* Change `type` default to `application/x-www-form-urlencoded` for urlencoded parser
|
|
||||||
* Provide static `require` analysis
|
|
||||||
* Use the `http-errors` module to generate errors
|
|
||||||
* deps: bytes@2.1.0
|
|
||||||
- Slight optimizations
|
|
||||||
* deps: iconv-lite@0.4.10
|
|
||||||
- The encoding UTF-16 without BOM now defaults to UTF-16LE when detection fails
|
|
||||||
- Leading BOM is now removed when decoding
|
|
||||||
* deps: on-finished@~2.3.0
|
|
||||||
- Add defined behavior for HTTP `CONNECT` requests
|
|
||||||
- Add defined behavior for HTTP `Upgrade` requests
|
|
||||||
- deps: ee-first@1.1.1
|
|
||||||
* deps: qs@3.1.0
|
|
||||||
- Fix dropping parameters like `hasOwnProperty`
|
|
||||||
- Fix various parsing edge cases
|
|
||||||
- Parsed object now has `null` prototype
|
|
||||||
* deps: raw-body@~2.1.1
|
|
||||||
- Use `unpipe` module for unpiping requests
|
|
||||||
- deps: iconv-lite@0.4.10
|
|
||||||
* deps: type-is@~1.6.3
|
|
||||||
- deps: mime-types@~2.1.1
|
|
||||||
- perf: reduce try block size
|
|
||||||
- perf: remove bitwise operations
|
|
||||||
* perf: enable strict mode
|
|
||||||
* perf: remove argument reassignment
|
|
||||||
* perf: remove delete call
|
|
||||||
|
|
||||||
1.12.4 / 2015-05-10
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@~2.2.0
|
|
||||||
* deps: qs@2.4.2
|
|
||||||
- Fix allowing parameters like `constructor`
|
|
||||||
* deps: on-finished@~2.2.1
|
|
||||||
* deps: raw-body@~2.0.1
|
|
||||||
- Fix a false-positive when unpiping in Node.js 0.8
|
|
||||||
- deps: bytes@2.0.1
|
|
||||||
* deps: type-is@~1.6.2
|
|
||||||
- deps: mime-types@~2.0.11
|
|
||||||
|
|
||||||
1.12.3 / 2015-04-15
|
|
||||||
===================
|
|
||||||
|
|
||||||
* Slight efficiency improvement when not debugging
|
|
||||||
* deps: depd@~1.0.1
|
|
||||||
* deps: iconv-lite@0.4.8
|
|
||||||
- Add encoding alias UNICODE-1-1-UTF-7
|
|
||||||
* deps: raw-body@1.3.4
|
|
||||||
- Fix hanging callback if request aborts during read
|
|
||||||
- deps: iconv-lite@0.4.8
|
|
||||||
|
|
||||||
1.12.2 / 2015-03-16
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: qs@2.4.1
|
|
||||||
- Fix error when parameter `hasOwnProperty` is present
|
|
||||||
|
|
||||||
1.12.1 / 2015-03-15
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: debug@~2.1.3
|
|
||||||
- Fix high intensity foreground color for bold
|
|
||||||
- deps: ms@0.7.0
|
|
||||||
* deps: type-is@~1.6.1
|
|
||||||
- deps: mime-types@~2.0.10
|
|
||||||
|
|
||||||
1.12.0 / 2015-02-13
|
|
||||||
===================
|
|
||||||
|
|
||||||
* add `debug` messages
|
|
||||||
* accept a function for the `type` option
|
|
||||||
* use `content-type` to parse `Content-Type` headers
|
|
||||||
* deps: iconv-lite@0.4.7
|
|
||||||
- Gracefully support enumerables on `Object.prototype`
|
|
||||||
* deps: raw-body@1.3.3
|
|
||||||
- deps: iconv-lite@0.4.7
|
|
||||||
* deps: type-is@~1.6.0
|
|
||||||
- fix argument reassignment
|
|
||||||
- fix false-positives in `hasBody` `Transfer-Encoding` check
|
|
||||||
- support wildcard for both type and subtype (`*/*`)
|
|
||||||
- deps: mime-types@~2.0.9
|
|
||||||
|
|
||||||
1.11.0 / 2015-01-30
|
|
||||||
===================
|
|
||||||
|
|
||||||
* make internal `extended: true` depth limit infinity
|
|
||||||
* deps: type-is@~1.5.6
|
|
||||||
- deps: mime-types@~2.0.8
|
|
||||||
|
|
||||||
1.10.2 / 2015-01-20
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: iconv-lite@0.4.6
|
|
||||||
- Fix rare aliases of single-byte encodings
|
|
||||||
* deps: raw-body@1.3.2
|
|
||||||
- deps: iconv-lite@0.4.6
|
|
||||||
|
|
||||||
1.10.1 / 2015-01-01
|
|
||||||
===================
|
|
||||||
|
|
||||||
* deps: on-finished@~2.2.0
|
|
||||||
* deps: type-is@~1.5.5
|
|
||||||
- deps: mime-types@~2.0.7
|
|
||||||
|
|
||||||
1.10.0 / 2014-12-02
|
|
||||||
===================
|
|
||||||
|
|
||||||
* make internal `extended: true` array limit dynamic
|
|
||||||
|
|
||||||
1.9.3 / 2014-11-21
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: iconv-lite@0.4.5
|
|
||||||
- Fix Windows-31J and X-SJIS encoding support
|
|
||||||
* deps: qs@2.3.3
|
|
||||||
- Fix `arrayLimit` behavior
|
|
||||||
* deps: raw-body@1.3.1
|
|
||||||
- deps: iconv-lite@0.4.5
|
|
||||||
* deps: type-is@~1.5.3
|
|
||||||
- deps: mime-types@~2.0.3
|
|
||||||
|
|
||||||
1.9.2 / 2014-10-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@2.3.2
|
|
||||||
- Fix parsing of mixed objects and values
|
|
||||||
|
|
||||||
1.9.1 / 2014-10-22
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: on-finished@~2.1.1
|
|
||||||
- Fix handling of pipelined requests
|
|
||||||
* deps: qs@2.3.0
|
|
||||||
- Fix parsing of mixed implicit and explicit arrays
|
|
||||||
* deps: type-is@~1.5.2
|
|
||||||
- deps: mime-types@~2.0.2
|
|
||||||
|
|
||||||
1.9.0 / 2014-09-24
|
|
||||||
==================
|
|
||||||
|
|
||||||
* include the charset in "unsupported charset" error message
|
|
||||||
* include the encoding in "unsupported content encoding" error message
|
|
||||||
* deps: depd@~1.0.0
|
|
||||||
|
|
||||||
1.8.4 / 2014-09-23
|
|
||||||
==================
|
|
||||||
|
|
||||||
* fix content encoding to be case-insensitive
|
|
||||||
|
|
||||||
1.8.3 / 2014-09-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@2.2.4
|
|
||||||
- Fix issue with object keys starting with numbers truncated
|
|
||||||
|
|
||||||
1.8.2 / 2014-09-15
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: depd@0.4.5
|
|
||||||
|
|
||||||
1.8.1 / 2014-09-07
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: media-typer@0.3.0
|
|
||||||
* deps: type-is@~1.5.1
|
|
||||||
|
|
||||||
1.8.0 / 2014-09-05
|
|
||||||
==================
|
|
||||||
|
|
||||||
* make empty-body-handling consistent between chunked requests
|
|
||||||
- empty `json` produces `{}`
|
|
||||||
- empty `raw` produces `new Buffer(0)`
|
|
||||||
- empty `text` produces `''`
|
|
||||||
- empty `urlencoded` produces `{}`
|
|
||||||
* deps: qs@2.2.3
|
|
||||||
- Fix issue where first empty value in array is discarded
|
|
||||||
* deps: type-is@~1.5.0
|
|
||||||
- fix `hasbody` to be true for `content-length: 0`
|
|
||||||
|
|
||||||
1.7.0 / 2014-09-01
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `parameterLimit` option to `urlencoded` parser
|
|
||||||
* change `urlencoded` extended array limit to 100
|
|
||||||
* respond with 413 when over `parameterLimit` in `urlencoded`
|
|
||||||
|
|
||||||
1.6.7 / 2014-08-29
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@2.2.2
|
|
||||||
- Remove unnecessary cloning
|
|
||||||
|
|
||||||
1.6.6 / 2014-08-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@2.2.0
|
|
||||||
- Array parsing fix
|
|
||||||
- Performance improvements
|
|
||||||
|
|
||||||
1.6.5 / 2014-08-16
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: on-finished@2.1.0
|
|
||||||
|
|
||||||
1.6.4 / 2014-08-14
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@1.2.2
|
|
||||||
|
|
||||||
1.6.3 / 2014-08-10
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@1.2.1
|
|
||||||
|
|
||||||
1.6.2 / 2014-08-07
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@1.2.0
|
|
||||||
- Fix parsing array of objects
|
|
||||||
|
|
||||||
1.6.1 / 2014-08-06
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@1.1.0
|
|
||||||
- Accept urlencoded square brackets
|
|
||||||
- Accept empty values in implicit array notation
|
|
||||||
|
|
||||||
1.6.0 / 2014-08-05
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: qs@1.0.2
|
|
||||||
- Complete rewrite
|
|
||||||
- Limits array length to 20
|
|
||||||
- Limits object depth to 5
|
|
||||||
- Limits parameters to 1,000
|
|
||||||
|
|
||||||
1.5.2 / 2014-07-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: depd@0.4.4
|
|
||||||
- Work-around v8 generating empty stack traces
|
|
||||||
|
|
||||||
1.5.1 / 2014-07-26
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: depd@0.4.3
|
|
||||||
- Fix exception when global `Error.stackTraceLimit` is too low
|
|
||||||
|
|
||||||
1.5.0 / 2014-07-20
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: depd@0.4.2
|
|
||||||
- Add `TRACE_DEPRECATION` environment variable
|
|
||||||
- Remove non-standard grey color from color output
|
|
||||||
- Support `--no-deprecation` argument
|
|
||||||
- Support `--trace-deprecation` argument
|
|
||||||
* deps: iconv-lite@0.4.4
|
|
||||||
- Added encoding UTF-7
|
|
||||||
* deps: raw-body@1.3.0
|
|
||||||
- deps: iconv-lite@0.4.4
|
|
||||||
- Added encoding UTF-7
|
|
||||||
- Fix `Cannot switch to old mode now` error on Node.js 0.10+
|
|
||||||
* deps: type-is@~1.3.2
|
|
||||||
|
|
||||||
1.4.3 / 2014-06-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: type-is@1.3.1
|
|
||||||
- fix global variable leak
|
|
||||||
|
|
||||||
1.4.2 / 2014-06-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: type-is@1.3.0
|
|
||||||
- improve type parsing
|
|
||||||
|
|
||||||
1.4.1 / 2014-06-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* fix urlencoded extended deprecation message
|
|
||||||
|
|
||||||
1.4.0 / 2014-06-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `text` parser
|
|
||||||
* add `raw` parser
|
|
||||||
* check accepted charset in content-type (accepts utf-8)
|
|
||||||
* check accepted encoding in content-encoding (accepts identity)
|
|
||||||
* deprecate `bodyParser()` middleware; use `.json()` and `.urlencoded()` as needed
|
|
||||||
* deprecate `urlencoded()` without provided `extended` option
|
|
||||||
* lazy-load urlencoded parsers
|
|
||||||
* parsers split into files for reduced mem usage
|
|
||||||
* support gzip and deflate bodies
|
|
||||||
- set `inflate: false` to turn off
|
|
||||||
* deps: raw-body@1.2.2
|
|
||||||
- Support all encodings from `iconv-lite`
|
|
||||||
|
|
||||||
1.3.1 / 2014-06-11
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: type-is@1.2.1
|
|
||||||
- Switch dependency from mime to mime-types@1.0.0
|
|
||||||
|
|
||||||
1.3.0 / 2014-05-31
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `extended` option to urlencoded parser
|
|
||||||
|
|
||||||
1.2.2 / 2014-05-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* deps: raw-body@1.1.6
|
|
||||||
- assert stream encoding on node.js 0.8
|
|
||||||
- assert stream encoding on node.js < 0.10.6
|
|
||||||
- deps: bytes@1
|
|
||||||
|
|
||||||
1.2.1 / 2014-05-26
|
|
||||||
==================
|
|
||||||
|
|
||||||
* invoke `next(err)` after request fully read
|
|
||||||
- prevents hung responses and socket hang ups
|
|
||||||
|
|
||||||
1.2.0 / 2014-05-11
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `verify` option
|
|
||||||
* deps: type-is@1.2.0
|
|
||||||
- support suffix matching
|
|
||||||
|
|
||||||
1.1.2 / 2014-05-11
|
|
||||||
==================
|
|
||||||
|
|
||||||
* improve json parser speed
|
|
||||||
|
|
||||||
1.1.1 / 2014-05-11
|
|
||||||
==================
|
|
||||||
|
|
||||||
* fix repeated limit parsing with every request
|
|
||||||
|
|
||||||
1.1.0 / 2014-05-10
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `type` option
|
|
||||||
* deps: pin for safety and consistency
|
|
||||||
|
|
||||||
1.0.2 / 2014-04-14
|
|
||||||
==================
|
|
||||||
|
|
||||||
* use `type-is` module
|
|
||||||
|
|
||||||
1.0.1 / 2014-03-20
|
|
||||||
==================
|
|
||||||
|
|
||||||
* lower default limits to 100kb
|
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
(The MIT License)
|
|
||||||
|
|
||||||
Copyright (c) 2014 Jonathan Ong <me@jongleberry.com>
|
|
||||||
Copyright (c) 2014-2015 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
'Software'), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
-476
@@ -1,476 +0,0 @@
|
|||||||
# body-parser
|
|
||||||
|
|
||||||
[![NPM Version][npm-version-image]][npm-url]
|
|
||||||
[![NPM Downloads][npm-downloads-image]][npm-url]
|
|
||||||
[![Build Status][ci-image]][ci-url]
|
|
||||||
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
||||||
[![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]
|
|
||||||
|
|
||||||
Node.js body parsing middleware.
|
|
||||||
|
|
||||||
Parse incoming request bodies in a middleware before your handlers, available
|
|
||||||
under the `req.body` property.
|
|
||||||
|
|
||||||
**Note** As `req.body`'s shape is based on user-controlled input, all
|
|
||||||
properties and values in this object are untrusted and should be validated
|
|
||||||
before trusting. For example, `req.body.foo.toString()` may fail in multiple
|
|
||||||
ways, for example the `foo` property may not be there or may not be a string,
|
|
||||||
and `toString` may not be a function and instead a string or other user input.
|
|
||||||
|
|
||||||
[Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/).
|
|
||||||
|
|
||||||
_This does not handle multipart bodies_, due to their complex and typically
|
|
||||||
large nature. For multipart bodies, you may be interested in the following
|
|
||||||
modules:
|
|
||||||
|
|
||||||
* [busboy](https://www.npmjs.org/package/busboy#readme) and
|
|
||||||
[connect-busboy](https://www.npmjs.org/package/connect-busboy#readme)
|
|
||||||
* [multiparty](https://www.npmjs.org/package/multiparty#readme) and
|
|
||||||
[connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme)
|
|
||||||
* [formidable](https://www.npmjs.org/package/formidable#readme)
|
|
||||||
* [multer](https://www.npmjs.org/package/multer#readme)
|
|
||||||
|
|
||||||
This module provides the following parsers:
|
|
||||||
|
|
||||||
* [JSON body parser](#bodyparserjsonoptions)
|
|
||||||
* [Raw body parser](#bodyparserrawoptions)
|
|
||||||
* [Text body parser](#bodyparsertextoptions)
|
|
||||||
* [URL-encoded form body parser](#bodyparserurlencodedoptions)
|
|
||||||
|
|
||||||
Other body parsers you might be interested in:
|
|
||||||
|
|
||||||
- [body](https://www.npmjs.org/package/body#readme)
|
|
||||||
- [co-body](https://www.npmjs.org/package/co-body#readme)
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
```sh
|
|
||||||
$ npm install body-parser
|
|
||||||
```
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
```js
|
|
||||||
var bodyParser = require('body-parser')
|
|
||||||
```
|
|
||||||
|
|
||||||
The `bodyParser` object exposes various factories to create middlewares. All
|
|
||||||
middlewares will populate the `req.body` property with the parsed body when
|
|
||||||
the `Content-Type` request header matches the `type` option, or an empty
|
|
||||||
object (`{}`) if there was no body to parse, the `Content-Type` was not matched,
|
|
||||||
or an error occurred.
|
|
||||||
|
|
||||||
The various errors returned by this module are described in the
|
|
||||||
[errors section](#errors).
|
|
||||||
|
|
||||||
### bodyParser.json([options])
|
|
||||||
|
|
||||||
Returns middleware that only parses `json` and only looks at requests where
|
|
||||||
the `Content-Type` header matches the `type` option. This parser accepts any
|
|
||||||
Unicode encoding of the body and supports automatic inflation of `gzip` and
|
|
||||||
`deflate` encodings.
|
|
||||||
|
|
||||||
A new `body` object containing the parsed data is populated on the `request`
|
|
||||||
object after the middleware (i.e. `req.body`).
|
|
||||||
|
|
||||||
#### Options
|
|
||||||
|
|
||||||
The `json` function takes an optional `options` object that may contain any of
|
|
||||||
the following keys:
|
|
||||||
|
|
||||||
##### inflate
|
|
||||||
|
|
||||||
When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|
||||||
`false`, deflated bodies are rejected. Defaults to `true`.
|
|
||||||
|
|
||||||
##### limit
|
|
||||||
|
|
||||||
Controls the maximum request body size. If this is a number, then the value
|
|
||||||
specifies the number of bytes; if it is a string, the value is passed to the
|
|
||||||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|
||||||
to `'100kb'`.
|
|
||||||
|
|
||||||
##### reviver
|
|
||||||
|
|
||||||
The `reviver` option is passed directly to `JSON.parse` as the second
|
|
||||||
argument. You can find more information on this argument
|
|
||||||
[in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter).
|
|
||||||
|
|
||||||
##### strict
|
|
||||||
|
|
||||||
When set to `true`, will only accept arrays and objects; when `false` will
|
|
||||||
accept anything `JSON.parse` accepts. Defaults to `true`.
|
|
||||||
|
|
||||||
##### type
|
|
||||||
|
|
||||||
The `type` option is used to determine what media type the middleware will
|
|
||||||
parse. This option can be a string, array of strings, or a function. If not a
|
|
||||||
function, `type` option is passed directly to the
|
|
||||||
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|
||||||
be an extension name (like `json`), a mime type (like `application/json`), or
|
|
||||||
a mime type with a wildcard (like `*/*` or `*/json`). If a function, the `type`
|
|
||||||
option is called as `fn(req)` and the request is parsed if it returns a truthy
|
|
||||||
value. Defaults to `application/json`.
|
|
||||||
|
|
||||||
##### verify
|
|
||||||
|
|
||||||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|
||||||
where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|
||||||
encoding of the request. The parsing can be aborted by throwing an error.
|
|
||||||
|
|
||||||
### bodyParser.raw([options])
|
|
||||||
|
|
||||||
Returns middleware that parses all bodies as a `Buffer` and only looks at
|
|
||||||
requests where the `Content-Type` header matches the `type` option. This
|
|
||||||
parser supports automatic inflation of `gzip` and `deflate` encodings.
|
|
||||||
|
|
||||||
A new `body` object containing the parsed data is populated on the `request`
|
|
||||||
object after the middleware (i.e. `req.body`). This will be a `Buffer` object
|
|
||||||
of the body.
|
|
||||||
|
|
||||||
#### Options
|
|
||||||
|
|
||||||
The `raw` function takes an optional `options` object that may contain any of
|
|
||||||
the following keys:
|
|
||||||
|
|
||||||
##### inflate
|
|
||||||
|
|
||||||
When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|
||||||
`false`, deflated bodies are rejected. Defaults to `true`.
|
|
||||||
|
|
||||||
##### limit
|
|
||||||
|
|
||||||
Controls the maximum request body size. If this is a number, then the value
|
|
||||||
specifies the number of bytes; if it is a string, the value is passed to the
|
|
||||||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|
||||||
to `'100kb'`.
|
|
||||||
|
|
||||||
##### type
|
|
||||||
|
|
||||||
The `type` option is used to determine what media type the middleware will
|
|
||||||
parse. This option can be a string, array of strings, or a function.
|
|
||||||
If not a function, `type` option is passed directly to the
|
|
||||||
[type-is](https://www.npmjs.org/package/type-is#readme) library and this
|
|
||||||
can be an extension name (like `bin`), a mime type (like
|
|
||||||
`application/octet-stream`), or a mime type with a wildcard (like `*/*` or
|
|
||||||
`application/*`). If a function, the `type` option is called as `fn(req)`
|
|
||||||
and the request is parsed if it returns a truthy value. Defaults to
|
|
||||||
`application/octet-stream`.
|
|
||||||
|
|
||||||
##### verify
|
|
||||||
|
|
||||||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|
||||||
where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|
||||||
encoding of the request. The parsing can be aborted by throwing an error.
|
|
||||||
|
|
||||||
### bodyParser.text([options])
|
|
||||||
|
|
||||||
Returns middleware that parses all bodies as a string and only looks at
|
|
||||||
requests where the `Content-Type` header matches the `type` option. This
|
|
||||||
parser supports automatic inflation of `gzip` and `deflate` encodings.
|
|
||||||
|
|
||||||
A new `body` string containing the parsed data is populated on the `request`
|
|
||||||
object after the middleware (i.e. `req.body`). This will be a string of the
|
|
||||||
body.
|
|
||||||
|
|
||||||
#### Options
|
|
||||||
|
|
||||||
The `text` function takes an optional `options` object that may contain any of
|
|
||||||
the following keys:
|
|
||||||
|
|
||||||
##### defaultCharset
|
|
||||||
|
|
||||||
Specify the default character set for the text content if the charset is not
|
|
||||||
specified in the `Content-Type` header of the request. Defaults to `utf-8`.
|
|
||||||
|
|
||||||
##### inflate
|
|
||||||
|
|
||||||
When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|
||||||
`false`, deflated bodies are rejected. Defaults to `true`.
|
|
||||||
|
|
||||||
##### limit
|
|
||||||
|
|
||||||
Controls the maximum request body size. If this is a number, then the value
|
|
||||||
specifies the number of bytes; if it is a string, the value is passed to the
|
|
||||||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|
||||||
to `'100kb'`.
|
|
||||||
|
|
||||||
##### type
|
|
||||||
|
|
||||||
The `type` option is used to determine what media type the middleware will
|
|
||||||
parse. This option can be a string, array of strings, or a function. If not
|
|
||||||
a function, `type` option is passed directly to the
|
|
||||||
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|
||||||
be an extension name (like `txt`), a mime type (like `text/plain`), or a mime
|
|
||||||
type with a wildcard (like `*/*` or `text/*`). If a function, the `type`
|
|
||||||
option is called as `fn(req)` and the request is parsed if it returns a
|
|
||||||
truthy value. Defaults to `text/plain`.
|
|
||||||
|
|
||||||
##### verify
|
|
||||||
|
|
||||||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|
||||||
where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|
||||||
encoding of the request. The parsing can be aborted by throwing an error.
|
|
||||||
|
|
||||||
### bodyParser.urlencoded([options])
|
|
||||||
|
|
||||||
Returns middleware that only parses `urlencoded` bodies and only looks at
|
|
||||||
requests where the `Content-Type` header matches the `type` option. This
|
|
||||||
parser accepts only UTF-8 encoding of the body and supports automatic
|
|
||||||
inflation of `gzip` and `deflate` encodings.
|
|
||||||
|
|
||||||
A new `body` object containing the parsed data is populated on the `request`
|
|
||||||
object after the middleware (i.e. `req.body`). This object will contain
|
|
||||||
key-value pairs, where the value can be a string or array (when `extended` is
|
|
||||||
`false`), or any type (when `extended` is `true`).
|
|
||||||
|
|
||||||
#### Options
|
|
||||||
|
|
||||||
The `urlencoded` function takes an optional `options` object that may contain
|
|
||||||
any of the following keys:
|
|
||||||
|
|
||||||
##### extended
|
|
||||||
|
|
||||||
The `extended` option allows to choose between parsing the URL-encoded data
|
|
||||||
with the `querystring` library (when `false`) or the `qs` library (when
|
|
||||||
`true`). The "extended" syntax allows for rich objects and arrays to be
|
|
||||||
encoded into the URL-encoded format, allowing for a JSON-like experience
|
|
||||||
with URL-encoded. For more information, please
|
|
||||||
[see the qs library](https://www.npmjs.org/package/qs#readme).
|
|
||||||
|
|
||||||
Defaults to `true`, but using the default has been deprecated. Please
|
|
||||||
research into the difference between `qs` and `querystring` and choose the
|
|
||||||
appropriate setting.
|
|
||||||
|
|
||||||
##### inflate
|
|
||||||
|
|
||||||
When set to `true`, then deflated (compressed) bodies will be inflated; when
|
|
||||||
`false`, deflated bodies are rejected. Defaults to `true`.
|
|
||||||
|
|
||||||
##### limit
|
|
||||||
|
|
||||||
Controls the maximum request body size. If this is a number, then the value
|
|
||||||
specifies the number of bytes; if it is a string, the value is passed to the
|
|
||||||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
|
||||||
to `'100kb'`.
|
|
||||||
|
|
||||||
##### parameterLimit
|
|
||||||
|
|
||||||
The `parameterLimit` option controls the maximum number of parameters that
|
|
||||||
are allowed in the URL-encoded data. If a request contains more parameters
|
|
||||||
than this value, a 413 will be returned to the client. Defaults to `1000`.
|
|
||||||
|
|
||||||
##### type
|
|
||||||
|
|
||||||
The `type` option is used to determine what media type the middleware will
|
|
||||||
parse. This option can be a string, array of strings, or a function. If not
|
|
||||||
a function, `type` option is passed directly to the
|
|
||||||
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
|
||||||
be an extension name (like `urlencoded`), a mime type (like
|
|
||||||
`application/x-www-form-urlencoded`), or a mime type with a wildcard (like
|
|
||||||
`*/x-www-form-urlencoded`). If a function, the `type` option is called as
|
|
||||||
`fn(req)` and the request is parsed if it returns a truthy value. Defaults
|
|
||||||
to `application/x-www-form-urlencoded`.
|
|
||||||
|
|
||||||
##### verify
|
|
||||||
|
|
||||||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
|
||||||
where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
|
||||||
encoding of the request. The parsing can be aborted by throwing an error.
|
|
||||||
|
|
||||||
#### depth
|
|
||||||
|
|
||||||
The `depth` option is used to configure the maximum depth of the `qs` library when `extended` is `true`. This allows you to limit the amount of keys that are parsed and can be useful to prevent certain types of abuse. Defaults to `32`. It is recommended to keep this value as low as possible.
|
|
||||||
|
|
||||||
## Errors
|
|
||||||
|
|
||||||
The middlewares provided by this module create errors using the
|
|
||||||
[`http-errors` module](https://www.npmjs.com/package/http-errors). The errors
|
|
||||||
will typically have a `status`/`statusCode` property that contains the suggested
|
|
||||||
HTTP response code, an `expose` property to determine if the `message` property
|
|
||||||
should be displayed to the client, a `type` property to determine the type of
|
|
||||||
error without matching against the `message`, and a `body` property containing
|
|
||||||
the read body, if available.
|
|
||||||
|
|
||||||
The following are the common errors created, though any error can come through
|
|
||||||
for various reasons.
|
|
||||||
|
|
||||||
### content encoding unsupported
|
|
||||||
|
|
||||||
This error will occur when the request had a `Content-Encoding` header that
|
|
||||||
contained an encoding but the "inflation" option was set to `false`. The
|
|
||||||
`status` property is set to `415`, the `type` property is set to
|
|
||||||
`'encoding.unsupported'`, and the `charset` property will be set to the
|
|
||||||
encoding that is unsupported.
|
|
||||||
|
|
||||||
### entity parse failed
|
|
||||||
|
|
||||||
This error will occur when the request contained an entity that could not be
|
|
||||||
parsed by the middleware. The `status` property is set to `400`, the `type`
|
|
||||||
property is set to `'entity.parse.failed'`, and the `body` property is set to
|
|
||||||
the entity value that failed parsing.
|
|
||||||
|
|
||||||
### entity verify failed
|
|
||||||
|
|
||||||
This error will occur when the request contained an entity that could not be
|
|
||||||
failed verification by the defined `verify` option. The `status` property is
|
|
||||||
set to `403`, the `type` property is set to `'entity.verify.failed'`, and the
|
|
||||||
`body` property is set to the entity value that failed verification.
|
|
||||||
|
|
||||||
### request aborted
|
|
||||||
|
|
||||||
This error will occur when the request is aborted by the client before reading
|
|
||||||
the body has finished. The `received` property will be set to the number of
|
|
||||||
bytes received before the request was aborted and the `expected` property is
|
|
||||||
set to the number of expected bytes. The `status` property is set to `400`
|
|
||||||
and `type` property is set to `'request.aborted'`.
|
|
||||||
|
|
||||||
### request entity too large
|
|
||||||
|
|
||||||
This error will occur when the request body's size is larger than the "limit"
|
|
||||||
option. The `limit` property will be set to the byte limit and the `length`
|
|
||||||
property will be set to the request body's length. The `status` property is
|
|
||||||
set to `413` and the `type` property is set to `'entity.too.large'`.
|
|
||||||
|
|
||||||
### request size did not match content length
|
|
||||||
|
|
||||||
This error will occur when the request's length did not match the length from
|
|
||||||
the `Content-Length` header. This typically occurs when the request is malformed,
|
|
||||||
typically when the `Content-Length` header was calculated based on characters
|
|
||||||
instead of bytes. The `status` property is set to `400` and the `type` property
|
|
||||||
is set to `'request.size.invalid'`.
|
|
||||||
|
|
||||||
### stream encoding should not be set
|
|
||||||
|
|
||||||
This error will occur when something called the `req.setEncoding` method prior
|
|
||||||
to this middleware. This module operates directly on bytes only and you cannot
|
|
||||||
call `req.setEncoding` when using this module. The `status` property is set to
|
|
||||||
`500` and the `type` property is set to `'stream.encoding.set'`.
|
|
||||||
|
|
||||||
### stream is not readable
|
|
||||||
|
|
||||||
This error will occur when the request is no longer readable when this middleware
|
|
||||||
attempts to read it. This typically means something other than a middleware from
|
|
||||||
this module read the request body already and the middleware was also configured to
|
|
||||||
read the same request. The `status` property is set to `500` and the `type`
|
|
||||||
property is set to `'stream.not.readable'`.
|
|
||||||
|
|
||||||
### too many parameters
|
|
||||||
|
|
||||||
This error will occur when the content of the request exceeds the configured
|
|
||||||
`parameterLimit` for the `urlencoded` parser. The `status` property is set to
|
|
||||||
`413` and the `type` property is set to `'parameters.too.many'`.
|
|
||||||
|
|
||||||
### unsupported charset "BOGUS"
|
|
||||||
|
|
||||||
This error will occur when the request had a charset parameter in the
|
|
||||||
`Content-Type` header, but the `iconv-lite` module does not support it OR the
|
|
||||||
parser does not support it. The charset is contained in the message as well
|
|
||||||
as in the `charset` property. The `status` property is set to `415`, the
|
|
||||||
`type` property is set to `'charset.unsupported'`, and the `charset` property
|
|
||||||
is set to the charset that is unsupported.
|
|
||||||
|
|
||||||
### unsupported content encoding "bogus"
|
|
||||||
|
|
||||||
This error will occur when the request had a `Content-Encoding` header that
|
|
||||||
contained an unsupported encoding. The encoding is contained in the message
|
|
||||||
as well as in the `encoding` property. The `status` property is set to `415`,
|
|
||||||
the `type` property is set to `'encoding.unsupported'`, and the `encoding`
|
|
||||||
property is set to the encoding that is unsupported.
|
|
||||||
|
|
||||||
### The input exceeded the depth
|
|
||||||
|
|
||||||
This error occurs when using `bodyParser.urlencoded` with the `extended` property set to `true` and the input exceeds the configured `depth` option. The `status` property is set to `400`. It is recommended to review the `depth` option and evaluate if it requires a higher value. When the `depth` option is set to `32` (default value), the error will not be thrown.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
### Express/Connect top-level generic
|
|
||||||
|
|
||||||
This example demonstrates adding a generic JSON and URL-encoded parser as a
|
|
||||||
top-level middleware, which will parse the bodies of all incoming requests.
|
|
||||||
This is the simplest setup.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var express = require('express')
|
|
||||||
var bodyParser = require('body-parser')
|
|
||||||
|
|
||||||
var app = express()
|
|
||||||
|
|
||||||
// parse application/x-www-form-urlencoded
|
|
||||||
app.use(bodyParser.urlencoded({ extended: false }))
|
|
||||||
|
|
||||||
// parse application/json
|
|
||||||
app.use(bodyParser.json())
|
|
||||||
|
|
||||||
app.use(function (req, res) {
|
|
||||||
res.setHeader('Content-Type', 'text/plain')
|
|
||||||
res.write('you posted:\n')
|
|
||||||
res.end(JSON.stringify(req.body, null, 2))
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Express route-specific
|
|
||||||
|
|
||||||
This example demonstrates adding body parsers specifically to the routes that
|
|
||||||
need them. In general, this is the most recommended way to use body-parser with
|
|
||||||
Express.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var express = require('express')
|
|
||||||
var bodyParser = require('body-parser')
|
|
||||||
|
|
||||||
var app = express()
|
|
||||||
|
|
||||||
// create application/json parser
|
|
||||||
var jsonParser = bodyParser.json()
|
|
||||||
|
|
||||||
// create application/x-www-form-urlencoded parser
|
|
||||||
var urlencodedParser = bodyParser.urlencoded({ extended: false })
|
|
||||||
|
|
||||||
// POST /login gets urlencoded bodies
|
|
||||||
app.post('/login', urlencodedParser, function (req, res) {
|
|
||||||
res.send('welcome, ' + req.body.username)
|
|
||||||
})
|
|
||||||
|
|
||||||
// POST /api/users gets JSON bodies
|
|
||||||
app.post('/api/users', jsonParser, function (req, res) {
|
|
||||||
// create user in req.body
|
|
||||||
})
|
|
||||||
```
|
|
||||||
|
|
||||||
### Change accepted type for parsers
|
|
||||||
|
|
||||||
All the parsers accept a `type` option which allows you to change the
|
|
||||||
`Content-Type` that the middleware will parse.
|
|
||||||
|
|
||||||
```js
|
|
||||||
var express = require('express')
|
|
||||||
var bodyParser = require('body-parser')
|
|
||||||
|
|
||||||
var app = express()
|
|
||||||
|
|
||||||
// parse various different custom JSON types as JSON
|
|
||||||
app.use(bodyParser.json({ type: 'application/*+json' }))
|
|
||||||
|
|
||||||
// parse some custom thing into a Buffer
|
|
||||||
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
|
|
||||||
|
|
||||||
// parse an HTML body into a string
|
|
||||||
app.use(bodyParser.text({ type: 'text/html' }))
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
[MIT](LICENSE)
|
|
||||||
|
|
||||||
[ci-image]: https://badgen.net/github/checks/expressjs/body-parser/master?label=ci
|
|
||||||
[ci-url]: https://github.com/expressjs/body-parser/actions/workflows/ci.yml
|
|
||||||
[coveralls-image]: https://badgen.net/coveralls/c/github/expressjs/body-parser/master
|
|
||||||
[coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master
|
|
||||||
[node-version-image]: https://badgen.net/npm/node/body-parser
|
|
||||||
[node-version-url]: https://nodejs.org/en/download
|
|
||||||
[npm-downloads-image]: https://badgen.net/npm/dm/body-parser
|
|
||||||
[npm-url]: https://npmjs.org/package/body-parser
|
|
||||||
[npm-version-image]: https://badgen.net/npm/v/body-parser
|
|
||||||
[ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/expressjs/body-parser/badge
|
|
||||||
[ossf-scorecard-visualizer]: https://ossf.github.io/scorecard-visualizer/#/projects/github.com/expressjs/body-parser
|
|
||||||
-156
@@ -1,156 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var deprecate = require('depd')('body-parser')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cache of loaded parsers.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var parsers = Object.create(null)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef Parsers
|
|
||||||
* @type {function}
|
|
||||||
* @property {function} json
|
|
||||||
* @property {function} raw
|
|
||||||
* @property {function} text
|
|
||||||
* @property {function} urlencoded
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
* @type {Parsers}
|
|
||||||
*/
|
|
||||||
|
|
||||||
exports = module.exports = deprecate.function(bodyParser,
|
|
||||||
'bodyParser: use individual json/urlencoded middlewares')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JSON parser.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.defineProperty(exports, 'json', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: createParserGetter('json')
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Raw parser.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.defineProperty(exports, 'raw', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: createParserGetter('raw')
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Text parser.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.defineProperty(exports, 'text', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: createParserGetter('text')
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* URL-encoded parser.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
Object.defineProperty(exports, 'urlencoded', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: createParserGetter('urlencoded')
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a middleware to parse json and urlencoded bodies.
|
|
||||||
*
|
|
||||||
* @param {object} [options]
|
|
||||||
* @return {function}
|
|
||||||
* @deprecated
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function bodyParser (options) {
|
|
||||||
// use default type for parsers
|
|
||||||
var opts = Object.create(options || null, {
|
|
||||||
type: {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
value: undefined,
|
|
||||||
writable: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
var _urlencoded = exports.urlencoded(opts)
|
|
||||||
var _json = exports.json(opts)
|
|
||||||
|
|
||||||
return function bodyParser (req, res, next) {
|
|
||||||
_json(req, res, function (err) {
|
|
||||||
if (err) return next(err)
|
|
||||||
_urlencoded(req, res, next)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a getter for loading a parser.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function createParserGetter (name) {
|
|
||||||
return function get () {
|
|
||||||
return loadParser(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load a parser module.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function loadParser (parserName) {
|
|
||||||
var parser = parsers[parserName]
|
|
||||||
|
|
||||||
if (parser !== undefined) {
|
|
||||||
return parser
|
|
||||||
}
|
|
||||||
|
|
||||||
// this uses a switch for static require analysis
|
|
||||||
switch (parserName) {
|
|
||||||
case 'json':
|
|
||||||
parser = require('./lib/types/json')
|
|
||||||
break
|
|
||||||
case 'raw':
|
|
||||||
parser = require('./lib/types/raw')
|
|
||||||
break
|
|
||||||
case 'text':
|
|
||||||
parser = require('./lib/types/text')
|
|
||||||
break
|
|
||||||
case 'urlencoded':
|
|
||||||
parser = require('./lib/types/urlencoded')
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// store to prevent invoking require()
|
|
||||||
return (parsers[parserName] = parser)
|
|
||||||
}
|
|
||||||
-205
@@ -1,205 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var createError = require('http-errors')
|
|
||||||
var destroy = require('destroy')
|
|
||||||
var getBody = require('raw-body')
|
|
||||||
var iconv = require('iconv-lite')
|
|
||||||
var onFinished = require('on-finished')
|
|
||||||
var unpipe = require('unpipe')
|
|
||||||
var zlib = require('zlib')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = read
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read a request into a buffer and parse.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @param {object} res
|
|
||||||
* @param {function} next
|
|
||||||
* @param {function} parse
|
|
||||||
* @param {function} debug
|
|
||||||
* @param {object} options
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function read (req, res, next, parse, debug, options) {
|
|
||||||
var length
|
|
||||||
var opts = options
|
|
||||||
var stream
|
|
||||||
|
|
||||||
// flag as parsed
|
|
||||||
req._body = true
|
|
||||||
|
|
||||||
// read options
|
|
||||||
var encoding = opts.encoding !== null
|
|
||||||
? opts.encoding
|
|
||||||
: null
|
|
||||||
var verify = opts.verify
|
|
||||||
|
|
||||||
try {
|
|
||||||
// get the content stream
|
|
||||||
stream = contentstream(req, debug, opts.inflate)
|
|
||||||
length = stream.length
|
|
||||||
stream.length = undefined
|
|
||||||
} catch (err) {
|
|
||||||
return next(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// set raw-body options
|
|
||||||
opts.length = length
|
|
||||||
opts.encoding = verify
|
|
||||||
? null
|
|
||||||
: encoding
|
|
||||||
|
|
||||||
// assert charset is supported
|
|
||||||
if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
|
|
||||||
return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
|
|
||||||
charset: encoding.toLowerCase(),
|
|
||||||
type: 'charset.unsupported'
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
// read body
|
|
||||||
debug('read body')
|
|
||||||
getBody(stream, opts, function (error, body) {
|
|
||||||
if (error) {
|
|
||||||
var _error
|
|
||||||
|
|
||||||
if (error.type === 'encoding.unsupported') {
|
|
||||||
// echo back charset
|
|
||||||
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
|
|
||||||
charset: encoding.toLowerCase(),
|
|
||||||
type: 'charset.unsupported'
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// set status code on error
|
|
||||||
_error = createError(400, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// unpipe from stream and destroy
|
|
||||||
if (stream !== req) {
|
|
||||||
unpipe(req)
|
|
||||||
destroy(stream, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
// read off entire request
|
|
||||||
dump(req, function onfinished () {
|
|
||||||
next(createError(400, _error))
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// verify
|
|
||||||
if (verify) {
|
|
||||||
try {
|
|
||||||
debug('verify body')
|
|
||||||
verify(req, res, body, encoding)
|
|
||||||
} catch (err) {
|
|
||||||
next(createError(403, err, {
|
|
||||||
body: body,
|
|
||||||
type: err.type || 'entity.verify.failed'
|
|
||||||
}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse
|
|
||||||
var str = body
|
|
||||||
try {
|
|
||||||
debug('parse body')
|
|
||||||
str = typeof body !== 'string' && encoding !== null
|
|
||||||
? iconv.decode(body, encoding)
|
|
||||||
: body
|
|
||||||
req.body = parse(str)
|
|
||||||
} catch (err) {
|
|
||||||
next(createError(400, err, {
|
|
||||||
body: str,
|
|
||||||
type: err.type || 'entity.parse.failed'
|
|
||||||
}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
next()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the content stream of the request.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @param {function} debug
|
|
||||||
* @param {boolean} [inflate=true]
|
|
||||||
* @return {object}
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function contentstream (req, debug, inflate) {
|
|
||||||
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
|
|
||||||
var length = req.headers['content-length']
|
|
||||||
var stream
|
|
||||||
|
|
||||||
debug('content-encoding "%s"', encoding)
|
|
||||||
|
|
||||||
if (inflate === false && encoding !== 'identity') {
|
|
||||||
throw createError(415, 'content encoding unsupported', {
|
|
||||||
encoding: encoding,
|
|
||||||
type: 'encoding.unsupported'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (encoding) {
|
|
||||||
case 'deflate':
|
|
||||||
stream = zlib.createInflate()
|
|
||||||
debug('inflate body')
|
|
||||||
req.pipe(stream)
|
|
||||||
break
|
|
||||||
case 'gzip':
|
|
||||||
stream = zlib.createGunzip()
|
|
||||||
debug('gunzip body')
|
|
||||||
req.pipe(stream)
|
|
||||||
break
|
|
||||||
case 'identity':
|
|
||||||
stream = req
|
|
||||||
stream.length = length
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
throw createError(415, 'unsupported content encoding "' + encoding + '"', {
|
|
||||||
encoding: encoding,
|
|
||||||
type: 'encoding.unsupported'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dump the contents of a request.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @param {function} callback
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function dump (req, callback) {
|
|
||||||
if (onFinished.isFinished(req)) {
|
|
||||||
callback(null)
|
|
||||||
} else {
|
|
||||||
onFinished(req, callback)
|
|
||||||
req.resume()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-243
@@ -1,243 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014 Jonathan Ong
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var bytes = require('bytes')
|
|
||||||
var contentType = require('content-type')
|
|
||||||
var createError = require('http-errors')
|
|
||||||
var debug = require('debug')('body-parser:json')
|
|
||||||
var read = require('../read')
|
|
||||||
var typeis = require('type-is')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = json
|
|
||||||
|
|
||||||
/**
|
|
||||||
* RegExp to match the first non-space in a string.
|
|
||||||
*
|
|
||||||
* Allowed whitespace is defined in RFC 7159:
|
|
||||||
*
|
|
||||||
* ws = *(
|
|
||||||
* %x20 / ; Space
|
|
||||||
* %x09 / ; Horizontal tab
|
|
||||||
* %x0A / ; Line feed or New line
|
|
||||||
* %x0D ) ; Carriage return
|
|
||||||
*/
|
|
||||||
|
|
||||||
var FIRST_CHAR_REGEXP = /^[\x20\x09\x0a\x0d]*([^\x20\x09\x0a\x0d])/ // eslint-disable-line no-control-regex
|
|
||||||
|
|
||||||
var JSON_SYNTAX_CHAR = '#'
|
|
||||||
var JSON_SYNTAX_REGEXP = /#+/g
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a middleware to parse JSON bodies.
|
|
||||||
*
|
|
||||||
* @param {object} [options]
|
|
||||||
* @return {function}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function json (options) {
|
|
||||||
var opts = options || {}
|
|
||||||
|
|
||||||
var limit = typeof opts.limit !== 'number'
|
|
||||||
? bytes.parse(opts.limit || '100kb')
|
|
||||||
: opts.limit
|
|
||||||
var inflate = opts.inflate !== false
|
|
||||||
var reviver = opts.reviver
|
|
||||||
var strict = opts.strict !== false
|
|
||||||
var type = opts.type || 'application/json'
|
|
||||||
var verify = opts.verify || false
|
|
||||||
|
|
||||||
if (verify !== false && typeof verify !== 'function') {
|
|
||||||
throw new TypeError('option verify must be function')
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the appropriate type checking function
|
|
||||||
var shouldParse = typeof type !== 'function'
|
|
||||||
? typeChecker(type)
|
|
||||||
: type
|
|
||||||
|
|
||||||
function parse (body) {
|
|
||||||
if (body.length === 0) {
|
|
||||||
// special-case empty json body, as it's a common client-side mistake
|
|
||||||
// TODO: maybe make this configurable or part of "strict" option
|
|
||||||
return {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strict) {
|
|
||||||
var first = firstchar(body)
|
|
||||||
|
|
||||||
if (first !== '{' && first !== '[') {
|
|
||||||
debug('strict violation')
|
|
||||||
throw createStrictSyntaxError(body, first)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
debug('parse json')
|
|
||||||
return JSON.parse(body, reviver)
|
|
||||||
} catch (e) {
|
|
||||||
throw normalizeJsonSyntaxError(e, {
|
|
||||||
message: e.message,
|
|
||||||
stack: e.stack
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return function jsonParser (req, res, next) {
|
|
||||||
if (req._body) {
|
|
||||||
debug('body already parsed')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body = req.body || {}
|
|
||||||
|
|
||||||
// skip requests without bodies
|
|
||||||
if (!typeis.hasBody(req)) {
|
|
||||||
debug('skip empty body')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('content-type %j', req.headers['content-type'])
|
|
||||||
|
|
||||||
// determine if request should be parsed
|
|
||||||
if (!shouldParse(req)) {
|
|
||||||
debug('skip parsing')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// assert charset per RFC 7159 sec 8.1
|
|
||||||
var charset = getCharset(req) || 'utf-8'
|
|
||||||
if (charset.slice(0, 4) !== 'utf-') {
|
|
||||||
debug('invalid charset')
|
|
||||||
next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
|
|
||||||
charset: charset,
|
|
||||||
type: 'charset.unsupported'
|
|
||||||
}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// read
|
|
||||||
read(req, res, next, parse, debug, {
|
|
||||||
encoding: charset,
|
|
||||||
inflate: inflate,
|
|
||||||
limit: limit,
|
|
||||||
verify: verify
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create strict violation syntax error matching native error.
|
|
||||||
*
|
|
||||||
* @param {string} str
|
|
||||||
* @param {string} char
|
|
||||||
* @return {Error}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function createStrictSyntaxError (str, char) {
|
|
||||||
var index = str.indexOf(char)
|
|
||||||
var partial = ''
|
|
||||||
|
|
||||||
if (index !== -1) {
|
|
||||||
partial = str.substring(0, index) + new Array(str.length - index + 1).join(JSON_SYNTAX_CHAR)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation')
|
|
||||||
} catch (e) {
|
|
||||||
return normalizeJsonSyntaxError(e, {
|
|
||||||
message: e.message.replace(JSON_SYNTAX_REGEXP, function (placeholder) {
|
|
||||||
return str.substring(index, index + placeholder.length)
|
|
||||||
}),
|
|
||||||
stack: e.stack
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the first non-whitespace character in a string.
|
|
||||||
*
|
|
||||||
* @param {string} str
|
|
||||||
* @return {function}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function firstchar (str) {
|
|
||||||
var match = FIRST_CHAR_REGEXP.exec(str)
|
|
||||||
|
|
||||||
return match
|
|
||||||
? match[1]
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the charset of a request.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function getCharset (req) {
|
|
||||||
try {
|
|
||||||
return (contentType.parse(req).parameters.charset || '').toLowerCase()
|
|
||||||
} catch (e) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Normalize a SyntaxError for JSON.parse.
|
|
||||||
*
|
|
||||||
* @param {SyntaxError} error
|
|
||||||
* @param {object} obj
|
|
||||||
* @return {SyntaxError}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function normalizeJsonSyntaxError (error, obj) {
|
|
||||||
var keys = Object.getOwnPropertyNames(error)
|
|
||||||
|
|
||||||
for (var i = 0; i < keys.length; i++) {
|
|
||||||
var key = keys[i]
|
|
||||||
if (key !== 'stack' && key !== 'message') {
|
|
||||||
delete error[key]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// replace stack before message for Node.js 0.10 and below
|
|
||||||
error.stack = obj.stack.replace(error.message, obj.message)
|
|
||||||
error.message = obj.message
|
|
||||||
|
|
||||||
return error
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the simple type checker.
|
|
||||||
*
|
|
||||||
* @param {string} type
|
|
||||||
* @return {function}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function typeChecker (type) {
|
|
||||||
return function checkType (req) {
|
|
||||||
return Boolean(typeis(req, type))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var bytes = require('bytes')
|
|
||||||
var debug = require('debug')('body-parser:raw')
|
|
||||||
var read = require('../read')
|
|
||||||
var typeis = require('type-is')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = raw
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a middleware to parse raw bodies.
|
|
||||||
*
|
|
||||||
* @param {object} [options]
|
|
||||||
* @return {function}
|
|
||||||
* @api public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function raw (options) {
|
|
||||||
var opts = options || {}
|
|
||||||
|
|
||||||
var inflate = opts.inflate !== false
|
|
||||||
var limit = typeof opts.limit !== 'number'
|
|
||||||
? bytes.parse(opts.limit || '100kb')
|
|
||||||
: opts.limit
|
|
||||||
var type = opts.type || 'application/octet-stream'
|
|
||||||
var verify = opts.verify || false
|
|
||||||
|
|
||||||
if (verify !== false && typeof verify !== 'function') {
|
|
||||||
throw new TypeError('option verify must be function')
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the appropriate type checking function
|
|
||||||
var shouldParse = typeof type !== 'function'
|
|
||||||
? typeChecker(type)
|
|
||||||
: type
|
|
||||||
|
|
||||||
function parse (buf) {
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
return function rawParser (req, res, next) {
|
|
||||||
if (req._body) {
|
|
||||||
debug('body already parsed')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body = req.body || {}
|
|
||||||
|
|
||||||
// skip requests without bodies
|
|
||||||
if (!typeis.hasBody(req)) {
|
|
||||||
debug('skip empty body')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('content-type %j', req.headers['content-type'])
|
|
||||||
|
|
||||||
// determine if request should be parsed
|
|
||||||
if (!shouldParse(req)) {
|
|
||||||
debug('skip parsing')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// read
|
|
||||||
read(req, res, next, parse, debug, {
|
|
||||||
encoding: null,
|
|
||||||
inflate: inflate,
|
|
||||||
limit: limit,
|
|
||||||
verify: verify
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the simple type checker.
|
|
||||||
*
|
|
||||||
* @param {string} type
|
|
||||||
* @return {function}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function typeChecker (type) {
|
|
||||||
return function checkType (req) {
|
|
||||||
return Boolean(typeis(req, type))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-121
@@ -1,121 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var bytes = require('bytes')
|
|
||||||
var contentType = require('content-type')
|
|
||||||
var debug = require('debug')('body-parser:text')
|
|
||||||
var read = require('../read')
|
|
||||||
var typeis = require('type-is')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = text
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a middleware to parse text bodies.
|
|
||||||
*
|
|
||||||
* @param {object} [options]
|
|
||||||
* @return {function}
|
|
||||||
* @api public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function text (options) {
|
|
||||||
var opts = options || {}
|
|
||||||
|
|
||||||
var defaultCharset = opts.defaultCharset || 'utf-8'
|
|
||||||
var inflate = opts.inflate !== false
|
|
||||||
var limit = typeof opts.limit !== 'number'
|
|
||||||
? bytes.parse(opts.limit || '100kb')
|
|
||||||
: opts.limit
|
|
||||||
var type = opts.type || 'text/plain'
|
|
||||||
var verify = opts.verify || false
|
|
||||||
|
|
||||||
if (verify !== false && typeof verify !== 'function') {
|
|
||||||
throw new TypeError('option verify must be function')
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the appropriate type checking function
|
|
||||||
var shouldParse = typeof type !== 'function'
|
|
||||||
? typeChecker(type)
|
|
||||||
: type
|
|
||||||
|
|
||||||
function parse (buf) {
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
return function textParser (req, res, next) {
|
|
||||||
if (req._body) {
|
|
||||||
debug('body already parsed')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body = req.body || {}
|
|
||||||
|
|
||||||
// skip requests without bodies
|
|
||||||
if (!typeis.hasBody(req)) {
|
|
||||||
debug('skip empty body')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('content-type %j', req.headers['content-type'])
|
|
||||||
|
|
||||||
// determine if request should be parsed
|
|
||||||
if (!shouldParse(req)) {
|
|
||||||
debug('skip parsing')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// get charset
|
|
||||||
var charset = getCharset(req) || defaultCharset
|
|
||||||
|
|
||||||
// read
|
|
||||||
read(req, res, next, parse, debug, {
|
|
||||||
encoding: charset,
|
|
||||||
inflate: inflate,
|
|
||||||
limit: limit,
|
|
||||||
verify: verify
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the charset of a request.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function getCharset (req) {
|
|
||||||
try {
|
|
||||||
return (contentType.parse(req).parameters.charset || '').toLowerCase()
|
|
||||||
} catch (e) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the simple type checker.
|
|
||||||
*
|
|
||||||
* @param {string} type
|
|
||||||
* @return {function}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function typeChecker (type) {
|
|
||||||
return function checkType (req) {
|
|
||||||
return Boolean(typeis(req, type))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-299
@@ -1,299 +0,0 @@
|
|||||||
/*!
|
|
||||||
* body-parser
|
|
||||||
* Copyright(c) 2014 Jonathan Ong
|
|
||||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module dependencies.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var bytes = require('bytes')
|
|
||||||
var contentType = require('content-type')
|
|
||||||
var createError = require('http-errors')
|
|
||||||
var debug = require('debug')('body-parser:urlencoded')
|
|
||||||
var deprecate = require('depd')('body-parser')
|
|
||||||
var read = require('../read')
|
|
||||||
var typeis = require('type-is')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = urlencoded
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cache of parser modules.
|
|
||||||
*/
|
|
||||||
|
|
||||||
var parsers = Object.create(null)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a middleware to parse urlencoded bodies.
|
|
||||||
*
|
|
||||||
* @param {object} [options]
|
|
||||||
* @return {function}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function urlencoded (options) {
|
|
||||||
var opts = options || {}
|
|
||||||
|
|
||||||
// notice because option default will flip in next major
|
|
||||||
if (opts.extended === undefined) {
|
|
||||||
deprecate('undefined extended: provide extended option')
|
|
||||||
}
|
|
||||||
|
|
||||||
var extended = opts.extended !== false
|
|
||||||
var inflate = opts.inflate !== false
|
|
||||||
var limit = typeof opts.limit !== 'number'
|
|
||||||
? bytes.parse(opts.limit || '100kb')
|
|
||||||
: opts.limit
|
|
||||||
var type = opts.type || 'application/x-www-form-urlencoded'
|
|
||||||
var verify = opts.verify || false
|
|
||||||
|
|
||||||
if (verify !== false && typeof verify !== 'function') {
|
|
||||||
throw new TypeError('option verify must be function')
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the appropriate query parser
|
|
||||||
var queryparse = extended
|
|
||||||
? extendedparser(opts)
|
|
||||||
: simpleparser(opts)
|
|
||||||
|
|
||||||
// create the appropriate type checking function
|
|
||||||
var shouldParse = typeof type !== 'function'
|
|
||||||
? typeChecker(type)
|
|
||||||
: type
|
|
||||||
|
|
||||||
function parse (body) {
|
|
||||||
return body.length
|
|
||||||
? queryparse(body)
|
|
||||||
: {}
|
|
||||||
}
|
|
||||||
|
|
||||||
return function urlencodedParser (req, res, next) {
|
|
||||||
if (req._body) {
|
|
||||||
debug('body already parsed')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.body = req.body || {}
|
|
||||||
|
|
||||||
// skip requests without bodies
|
|
||||||
if (!typeis.hasBody(req)) {
|
|
||||||
debug('skip empty body')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('content-type %j', req.headers['content-type'])
|
|
||||||
|
|
||||||
// determine if request should be parsed
|
|
||||||
if (!shouldParse(req)) {
|
|
||||||
debug('skip parsing')
|
|
||||||
next()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// assert charset
|
|
||||||
var charset = getCharset(req) || 'utf-8'
|
|
||||||
if (charset !== 'utf-8') {
|
|
||||||
debug('invalid charset')
|
|
||||||
next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
|
|
||||||
charset: charset,
|
|
||||||
type: 'charset.unsupported'
|
|
||||||
}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// read
|
|
||||||
read(req, res, next, parse, debug, {
|
|
||||||
debug: debug,
|
|
||||||
encoding: charset,
|
|
||||||
inflate: inflate,
|
|
||||||
limit: limit,
|
|
||||||
verify: verify
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the extended query parser.
|
|
||||||
*
|
|
||||||
* @param {object} options
|
|
||||||
*/
|
|
||||||
|
|
||||||
function extendedparser (options) {
|
|
||||||
var parameterLimit = options.parameterLimit !== undefined
|
|
||||||
? options.parameterLimit
|
|
||||||
: 1000
|
|
||||||
var depth = options.depth !== undefined ? options.depth : 32
|
|
||||||
var parse = parser('qs')
|
|
||||||
|
|
||||||
if (isNaN(parameterLimit) || parameterLimit < 1) {
|
|
||||||
throw new TypeError('option parameterLimit must be a positive number')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNaN(depth) || depth < 0) {
|
|
||||||
throw new TypeError('option depth must be a zero or a positive number')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFinite(parameterLimit)) {
|
|
||||||
parameterLimit = parameterLimit | 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return function queryparse (body) {
|
|
||||||
var paramCount = parameterCount(body, parameterLimit)
|
|
||||||
|
|
||||||
if (paramCount === undefined) {
|
|
||||||
debug('too many parameters')
|
|
||||||
throw createError(413, 'too many parameters', {
|
|
||||||
type: 'parameters.too.many'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayLimit = Math.max(100, paramCount)
|
|
||||||
|
|
||||||
debug('parse extended urlencoding')
|
|
||||||
try {
|
|
||||||
return parse(body, {
|
|
||||||
allowPrototypes: true,
|
|
||||||
arrayLimit: arrayLimit,
|
|
||||||
depth: depth,
|
|
||||||
strictDepth: true,
|
|
||||||
parameterLimit: parameterLimit
|
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof RangeError) {
|
|
||||||
throw createError(400, 'The input exceeded the depth', {
|
|
||||||
type: 'querystring.parse.rangeError'
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the charset of a request.
|
|
||||||
*
|
|
||||||
* @param {object} req
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function getCharset (req) {
|
|
||||||
try {
|
|
||||||
return (contentType.parse(req).parameters.charset || '').toLowerCase()
|
|
||||||
} catch (e) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Count the number of parameters, stopping once limit reached
|
|
||||||
*
|
|
||||||
* @param {string} body
|
|
||||||
* @param {number} limit
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function parameterCount (body, limit) {
|
|
||||||
var count = 0
|
|
||||||
var index = -1
|
|
||||||
|
|
||||||
do {
|
|
||||||
count++
|
|
||||||
if (count > limit) {
|
|
||||||
return undefined
|
|
||||||
}
|
|
||||||
index = body.indexOf('&', index + 1)
|
|
||||||
} while (index !== -1)
|
|
||||||
|
|
||||||
return count
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get parser for module name dynamically.
|
|
||||||
*
|
|
||||||
* @param {string} name
|
|
||||||
* @return {function}
|
|
||||||
* @api private
|
|
||||||
*/
|
|
||||||
|
|
||||||
function parser (name) {
|
|
||||||
var mod = parsers[name]
|
|
||||||
|
|
||||||
if (mod !== undefined) {
|
|
||||||
return mod.parse
|
|
||||||
}
|
|
||||||
|
|
||||||
// this uses a switch for static require analysis
|
|
||||||
switch (name) {
|
|
||||||
case 'qs':
|
|
||||||
mod = require('qs')
|
|
||||||
break
|
|
||||||
case 'querystring':
|
|
||||||
mod = require('querystring')
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// store to prevent invoking require()
|
|
||||||
parsers[name] = mod
|
|
||||||
|
|
||||||
return mod.parse
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the simple query parser.
|
|
||||||
*
|
|
||||||
* @param {object} options
|
|
||||||
*/
|
|
||||||
|
|
||||||
function simpleparser (options) {
|
|
||||||
var parameterLimit = options.parameterLimit !== undefined
|
|
||||||
? options.parameterLimit
|
|
||||||
: 1000
|
|
||||||
var parse = parser('querystring')
|
|
||||||
|
|
||||||
if (isNaN(parameterLimit) || parameterLimit < 1) {
|
|
||||||
throw new TypeError('option parameterLimit must be a positive number')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isFinite(parameterLimit)) {
|
|
||||||
parameterLimit = parameterLimit | 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return function queryparse (body) {
|
|
||||||
var paramCount = parameterCount(body, parameterLimit)
|
|
||||||
|
|
||||||
if (paramCount === undefined) {
|
|
||||||
debug('too many parameters')
|
|
||||||
throw createError(413, 'too many parameters', {
|
|
||||||
type: 'parameters.too.many'
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
debug('parse urlencoding')
|
|
||||||
return parse(body, undefined, undefined, { maxKeys: parameterLimit })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the simple type checker.
|
|
||||||
*
|
|
||||||
* @param {string} type
|
|
||||||
* @return {function}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function typeChecker (type) {
|
|
||||||
return function checkType (req) {
|
|
||||||
return Boolean(typeis(req, type))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-46
@@ -1,46 +0,0 @@
|
|||||||
root = true
|
|
||||||
|
|
||||||
[*]
|
|
||||||
indent_style = space
|
|
||||||
indent_size = 4
|
|
||||||
end_of_line = lf
|
|
||||||
charset = utf-8
|
|
||||||
trim_trailing_whitespace = true
|
|
||||||
insert_final_newline = true
|
|
||||||
max_line_length = 180
|
|
||||||
quote_type = single
|
|
||||||
|
|
||||||
[test/*]
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[LICENSE.md]
|
|
||||||
indent_size = off
|
|
||||||
|
|
||||||
[*.md]
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[*.json]
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[Makefile]
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[CHANGELOG.md]
|
|
||||||
indent_style = space
|
|
||||||
indent_size = 2
|
|
||||||
|
|
||||||
[LICENSE]
|
|
||||||
indent_size = 2
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[coverage/**/*]
|
|
||||||
indent_size = off
|
|
||||||
indent_style = off
|
|
||||||
indent = off
|
|
||||||
max_line_length = off
|
|
||||||
|
|
||||||
[.nycrc]
|
|
||||||
indent_style = tab
|
|
||||||
|
|
||||||
[tea.yaml]
|
|
||||||
indent_size = 2
|
|
||||||
-12
@@ -1,12 +0,0 @@
|
|||||||
# These are supported funding model platforms
|
|
||||||
|
|
||||||
github: [ljharb]
|
|
||||||
patreon: # Replace with a single Patreon username
|
|
||||||
open_collective: # Replace with a single Open Collective username
|
|
||||||
ko_fi: # Replace with a single Ko-fi username
|
|
||||||
tidelift: npm/qs
|
|
||||||
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
||||||
liberapay: # Replace with a single Liberapay username
|
|
||||||
issuehunt: # Replace with a single IssueHunt username
|
|
||||||
otechie: # Replace with a single Otechie username
|
|
||||||
custom: # Replace with a single custom sponsorship URL
|
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
# Security
|
|
||||||
|
|
||||||
Please file a private vulnerability report via GitHub, email [@ljharb](https://github.com/ljharb), or see https://tidelift.com/security if you have a potential security vulnerability to report.
|
|
||||||
|
|
||||||
## Incident Response Plan
|
|
||||||
|
|
||||||
Please see our [Incident Response Plan](https://github.com/ljharb/.github/blob/main/INCIDENT_RESPONSE_PLAN.md).
|
|
||||||
|
|
||||||
## Threat Model
|
|
||||||
|
|
||||||
Please see [THREAT_MODEL.md](./THREAT_MODEL.md).
|
|
||||||
-78
@@ -1,78 +0,0 @@
|
|||||||
## Threat Model for qs (querystring parsing library)
|
|
||||||
|
|
||||||
### 1. Library Overview
|
|
||||||
|
|
||||||
- **Library Name:** qs
|
|
||||||
- **Brief Description:** A JavaScript library for parsing and stringifying URL query strings, supporting nested objects and arrays. It is widely used in Node.js and web applications for processing query parameters[2][6][8].
|
|
||||||
- **Key Public APIs/Functions:** `qs.parse()`, `qs.stringify()`
|
|
||||||
|
|
||||||
### 2. Define Scope
|
|
||||||
|
|
||||||
This threat model focuses on the core parsing and stringifying functionality, specifically the handling of nested objects and arrays, option validation, and cycle management in stringification.
|
|
||||||
|
|
||||||
### 3. Conceptual System Diagram
|
|
||||||
|
|
||||||
```
|
|
||||||
Caller Application → qs.parse(input, options) → Parsing Engine → Output Object
|
|
||||||
│
|
|
||||||
└→ Options Handling
|
|
||||||
|
|
||||||
Caller Application → qs.stringify(obj, options) → Stringifying Engine → Output String
|
|
||||||
│
|
|
||||||
└→ Options Handling
|
|
||||||
└→ Cycle Tracking
|
|
||||||
```
|
|
||||||
|
|
||||||
**Trust Boundaries:**
|
|
||||||
- **Input string (parse):** May come from untrusted sources (e.g., user input, network requests)
|
|
||||||
- **Input object (stringify):** May contain cycles, which can lead to infinite loops during stringification
|
|
||||||
- **Options:** Provided by the caller
|
|
||||||
- **Cycle Tracking:** Used only during stringification to detect and handle circular references
|
|
||||||
|
|
||||||
### 4. Identify Assets
|
|
||||||
|
|
||||||
- **Integrity of parsed output:** Prevent malicious manipulation of the output object structure, especially ensuring builtins/globals are not modified as a result of parse[3][4][8].
|
|
||||||
- **Confidentiality of processed data:** Avoid leaking sensitive information through errors or output.
|
|
||||||
- **Availability/performance for host application:** Prevent crashes or resource exhaustion in the consuming application.
|
|
||||||
- **Security of host application:** Prevent the library from being a vector for attacks (e.g., prototype pollution, DoS).
|
|
||||||
- **Reputation of library:** Maintain trust by avoiding supply chain attacks and vulnerabilities[1].
|
|
||||||
|
|
||||||
### 5. Identify Threats
|
|
||||||
|
|
||||||
| Component / API / Interaction | S | T | R | I | D | E |
|
|
||||||
|---------------------------------------|----|----|----|----|----|----|
|
|
||||||
| Public API Call (`parse`) | – | ✓ | – | ✓ | ✓ | ✓ |
|
|
||||||
| Public API Call (`stringify`) | – | ✓ | – | ✓ | ✓ | – |
|
|
||||||
| Options Handling | ✓ | ✓ | – | ✓ | – | ✓ |
|
|
||||||
| Dependency Interaction | – | – | – | – | ✓ | – |
|
|
||||||
|
|
||||||
**Key Threats:**
|
|
||||||
- **Tampering:** Malicious input can, if not prevented, alter parsed output (e.g., prototype pollution via `__proto__`, modification of builtins/globals)[3][4][8].
|
|
||||||
- **Information Disclosure:** Error messages may expose internal details or sensitive data.
|
|
||||||
- **Denial of Service:** Large or malformed input can exhaust memory or CPU.
|
|
||||||
- **Elevation of Privilege:** Prototype pollution can lead to unintended privilege escalation in the host application[3][4][8].
|
|
||||||
|
|
||||||
### 6. Mitigation/Countermeasures
|
|
||||||
|
|
||||||
| Threat Identified | Proposed Mitigation |
|
|
||||||
|---------------------------------------------------|---------------------|
|
|
||||||
| Tampering (malicious input, prototype pollution) | Strict input validation; keep `allowPrototypes: false` by default; use `plainObjects` for output; ensure builtins/globals are never modified by parse[4][8]. |
|
|
||||||
| Information Disclosure (error messages) | Generic error messages without stack traces or internal paths. |
|
|
||||||
| Denial of Service (memory/CPU exhaustion) | Enforce `arrayLimit` and `parameterLimit` with safe defaults; enable `throwOnLimitExceeded`; limit nesting depth[7]. |
|
|
||||||
| Elevation of Privilege (prototype pollution) | Keep `allowPrototypes: false`; validate options against allowlist; use `plainObjects` to avoid prototype pollution[4][8]. |
|
|
||||||
|
|
||||||
### 7. Risk Ranking
|
|
||||||
|
|
||||||
- **High:** Denial of Service via array parsing or malformed input (historical vulnerability)
|
|
||||||
- **Medium:** Prototype pollution via options or input (if `allowPrototypes` enabled)
|
|
||||||
- **Low:** Information disclosure in errors
|
|
||||||
|
|
||||||
### 8. Next Steps & Review
|
|
||||||
|
|
||||||
1. **Audit option validation logic.**
|
|
||||||
2. **Add depth limiting to nested parsing and stringification.**
|
|
||||||
3. **Implement fuzz testing for parser and stringifier edge cases.**
|
|
||||||
4. **Regularly review dependencies for vulnerabilities.**
|
|
||||||
5. **Keep documentation and threat model up to date.**
|
|
||||||
6. **Ensure builtins/globals are never modified as a result of parse.**
|
|
||||||
7. **Support round-trip consistency between parse and stringify as a non-security goal, with the right options[5][9].**
|
|
||||||
-13
@@ -1,13 +0,0 @@
|
|||||||
{
|
|
||||||
"all": true,
|
|
||||||
"check-coverage": false,
|
|
||||||
"reporter": ["text-summary", "text", "html", "json"],
|
|
||||||
"lines": 86,
|
|
||||||
"statements": 85.93,
|
|
||||||
"functions": 82.43,
|
|
||||||
"branches": 76.06,
|
|
||||||
"exclude": [
|
|
||||||
"coverage",
|
|
||||||
"dist"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
-812
@@ -1,812 +0,0 @@
|
|||||||
## **6.15.1**
|
|
||||||
- [Fix] `parse`: `parameterLimit: Infinity` with `throwOnLimitExceeded: true` silently drops all parameters
|
|
||||||
- [Deps] update `@ljharb/eslint-config`
|
|
||||||
- [Dev Deps] update `@ljharb/eslint-config`, `iconv-lite`
|
|
||||||
- [Tests] increase coverage
|
|
||||||
|
|
||||||
## **6.15.0**
|
|
||||||
- [New] `parse`: add `strictMerge` option to wrap object/primitive conflicts in an array (#425, #122)
|
|
||||||
- [Fix] `duplicates` option should not apply to bracket notation keys (#514)
|
|
||||||
|
|
||||||
## **6.14.2**
|
|
||||||
- [Fix] `parse`: mark overflow objects for indexed notation exceeding `arrayLimit` (#546)
|
|
||||||
- [Fix] `arrayLimit` means max count, not max index, in `combine`/`merge`/`parseArrayValue`
|
|
||||||
- [Fix] `parse`: throw on `arrayLimit` exceeded with indexed notation when `throwOnLimitExceeded` is true (#529)
|
|
||||||
- [Fix] `parse`: enforce `arrayLimit` on `comma`-parsed values
|
|
||||||
- [Fix] `parse`: fix error message to reflect arrayLimit as max index; remove extraneous comments (#545)
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [meta] fix changelog typo (`arrayLength` → `arrayLimit`)
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.14.1**
|
|
||||||
- [Fix] ensure `arrayLimit` applies to `[]` notation as well
|
|
||||||
- [Fix] `parse`: when a custom decoder returns `null` for a key, ignore that key
|
|
||||||
- [Refactor] `parse`: extract key segment splitting helper
|
|
||||||
- [meta] add threat model
|
|
||||||
- [actions] add workflow permissions
|
|
||||||
- [Tests] `stringify`: increase coverage
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `npmignore`, `es-value-fixtures`, `for-each`, `object-inspect`
|
|
||||||
|
|
||||||
## **6.14.0**
|
|
||||||
- [New] `parse`: add `throwOnParameterLimitExceeded` option (#517)
|
|
||||||
- [Refactor] `parse`: use `utils.combine` more
|
|
||||||
- [patch] `parse`: add explicit `throwOnLimitExceeded` default
|
|
||||||
- [actions] use shared action; re-add finishers
|
|
||||||
- [meta] Fix changelog formatting bug
|
|
||||||
- [Deps] update `side-channel`
|
|
||||||
- [Dev Deps] update `es-value-fixtures`, `has-bigints`, `has-proto`, `has-symbols`
|
|
||||||
- [Tests] increase coverage
|
|
||||||
|
|
||||||
## **6.13.3**
|
|
||||||
[Fix] fix regressions from robustness refactor
|
|
||||||
[actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.13.2**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.13.1**
|
|
||||||
- [Fix] `stringify`: avoid a crash when a `filter` key is `null`
|
|
||||||
- [Fix] `utils.merge`: functions should not be stringified into keys
|
|
||||||
- [Fix] `parse`: avoid a crash with interpretNumericEntities: true, comma: true, and iso charset
|
|
||||||
- [Fix] `stringify`: ensure a non-string `filter` does not crash
|
|
||||||
- [Refactor] use `__proto__` syntax instead of `Object.create` for null objects
|
|
||||||
- [Refactor] misc cleanup
|
|
||||||
- [Tests] `utils.merge`: add some coverage
|
|
||||||
- [Tests] fix a test case
|
|
||||||
- [actions] split out node 10-20, and 20+
|
|
||||||
- [Dev Deps] update `es-value-fixtures`, `mock-property`, `object-inspect`, `tape`
|
|
||||||
|
|
||||||
## **6.13.0**
|
|
||||||
- [New] `parse`: add `strictDepth` option (#511)
|
|
||||||
- [Tests] use `npm audit` instead of `aud`
|
|
||||||
|
|
||||||
## **6.12.5**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.12.4**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.12.3**
|
|
||||||
- [Fix] `parse`: properly account for `strictNullHandling` when `allowEmptyArrays`
|
|
||||||
- [meta] fix changelog indentation
|
|
||||||
|
|
||||||
## **6.12.2**
|
|
||||||
- [Fix] `parse`: parse encoded square brackets (#506)
|
|
||||||
- [readme] add CII best practices badge
|
|
||||||
|
|
||||||
## **6.12.1**
|
|
||||||
- [Fix] `parse`: Disable `decodeDotInKeys` by default to restore previous behavior (#501)
|
|
||||||
- [Performance] `utils`: Optimize performance under large data volumes, reduce memory usage, and speed up processing (#502)
|
|
||||||
- [Refactor] `utils`: use `+=`
|
|
||||||
- [Tests] increase coverage
|
|
||||||
|
|
||||||
## **6.12.0**
|
|
||||||
|
|
||||||
- [New] `parse`/`stringify`: add `decodeDotInKeys`/`encodeDotKeys` options (#488)
|
|
||||||
- [New] `parse`: add `duplicates` option
|
|
||||||
- [New] `parse`/`stringify`: add `allowEmptyArrays` option to allow [] in object values (#487)
|
|
||||||
- [Refactor] `parse`/`stringify`: move allowDots config logic to its own variable
|
|
||||||
- [Refactor] `stringify`: move option-handling code into `normalizeStringifyOptions`
|
|
||||||
- [readme] update readme, add logos (#484)
|
|
||||||
- [readme] `stringify`: clarify default `arrayFormat` behavior
|
|
||||||
- [readme] fix line wrapping
|
|
||||||
- [readme] remove dead badges
|
|
||||||
- [Deps] update `side-channel`
|
|
||||||
- [meta] make the dist build 50% smaller
|
|
||||||
- [meta] add `sideEffects` flag
|
|
||||||
- [meta] run build in prepack, not prepublish
|
|
||||||
- [Tests] `parse`: remove useless tests; add coverage
|
|
||||||
- [Tests] `stringify`: increase coverage
|
|
||||||
- [Tests] use `mock-property`
|
|
||||||
- [Tests] `stringify`: improve coverage
|
|
||||||
- [Dev Deps] update `@ljharb/eslint-config `, `aud`, `has-override-mistake`, `has-property-descriptors`, `mock-property`, `npmignore`, `object-inspect`, `tape`
|
|
||||||
- [Dev Deps] pin `glob`, since v10.3.8+ requires a broken `jackspeak`
|
|
||||||
- [Dev Deps] pin `jackspeak` since 2.1.2+ depends on npm aliases, which kill the install process in npm < 6
|
|
||||||
|
|
||||||
## **6.11.4**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.11.3**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.11.2**
|
|
||||||
- [Fix] `parse`: Fix parsing when the global Object prototype is frozen (#473)
|
|
||||||
- [Tests] add passing test cases with empty keys (#473)
|
|
||||||
|
|
||||||
## **6.11.1**
|
|
||||||
- [Fix] `stringify`: encode comma values more consistently (#463)
|
|
||||||
- [readme] add usage of `filter` option for injecting custom serialization, i.e. of custom types (#447)
|
|
||||||
- [meta] remove extraneous code backticks (#457)
|
|
||||||
- [meta] fix changelog markdown
|
|
||||||
- [actions] update checkout action
|
|
||||||
- [actions] restrict action permissions
|
|
||||||
- [Dev Deps] update `@ljharb/eslint-config`, `aud`, `object-inspect`, `tape`
|
|
||||||
|
|
||||||
## **6.11.0**
|
|
||||||
- [New] [Fix] `stringify`: revert 0e903c0; add `commaRoundTrip` option (#442)
|
|
||||||
- [readme] fix version badge
|
|
||||||
|
|
||||||
## **6.10.7**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.10.6**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.10.5**
|
|
||||||
- [Fix] `stringify`: with `arrayFormat: comma`, properly include an explicit `[]` on a single-item array (#434)
|
|
||||||
|
|
||||||
## **6.10.4**
|
|
||||||
- [Fix] `stringify`: with `arrayFormat: comma`, include an explicit `[]` on a single-item array (#441)
|
|
||||||
- [meta] use `npmignore` to autogenerate an npmignore file
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `has-symbol`, `object-inspect`, `tape`
|
|
||||||
|
|
||||||
## **6.10.3**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [actions] reuse common workflows
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-inspect`, `tape`
|
|
||||||
|
|
||||||
## **6.10.2**
|
|
||||||
- [Fix] `stringify`: actually fix cyclic references (#426)
|
|
||||||
- [Fix] `stringify`: avoid encoding arrayformat comma when `encodeValuesOnly = true` (#424)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] add note and links for coercing primitive values (#408)
|
|
||||||
- [actions] update codecov uploader
|
|
||||||
- [actions] update workflows
|
|
||||||
- [Tests] clean up stringify tests slightly
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `object-inspect`, `safe-publish-latest`, `tape`
|
|
||||||
|
|
||||||
## **6.10.1**
|
|
||||||
- [Fix] `stringify`: avoid exception on repeated object values (#402)
|
|
||||||
|
|
||||||
## **6.10.0**
|
|
||||||
- [New] `stringify`: throw on cycles, instead of an infinite loop (#395, #394, #393)
|
|
||||||
- [New] `parse`: add `allowSparse` option for collapsing arrays with missing indices (#312)
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] only run `npm run dist` in publish, not install
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `aud`, `has-symbols`, `tape`
|
|
||||||
- [Tests] fix tests on node v0.6
|
|
||||||
- [Tests] use `ljharb/actions/node/install` instead of `ljharb/actions/node/run`
|
|
||||||
- [Tests] Revert "[meta] ignore eclint transitive audit warning"
|
|
||||||
|
|
||||||
## **6.9.9**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.9.8**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.9.7**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] `stringify`: avoid encoding arrayformat comma when `encodeValuesOnly = true` (#424)
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] add note and links for coercing primitive values (#408)
|
|
||||||
- [Tests] clean up stringify tests slightly
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- Revert "[meta] ignore eclint transitive audit warning"
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Dev Deps] backport updates from main
|
|
||||||
|
|
||||||
## **6.9.6**
|
|
||||||
- [Fix] restore `dist` dir; mistakenly removed in d4f6c32
|
|
||||||
|
|
||||||
## **6.9.5**
|
|
||||||
- [Fix] `stringify`: do not encode parens for RFC1738
|
|
||||||
- [Fix] `stringify`: fix arrayFormat comma with empty array/objects (#350)
|
|
||||||
- [Refactor] `format`: remove `util.assign` call
|
|
||||||
- [meta] add "Allow Edits" workflow; update rebase workflow
|
|
||||||
- [actions] switch Automatic Rebase workflow to `pull_request_target` event
|
|
||||||
- [Tests] `stringify`: add tests for #378
|
|
||||||
- [Tests] migrate tests to Github Actions
|
|
||||||
- [Tests] run `nyc` on all tests; use `tape` runner
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `browserify`, `mkdirp`, `object-inspect`, `tape`; add `aud`
|
|
||||||
|
|
||||||
## **6.9.4**
|
|
||||||
- [Fix] `stringify`: when `arrayFormat` is `comma`, respect `serializeDate` (#364)
|
|
||||||
- [Refactor] `stringify`: reduce branching (part of #350)
|
|
||||||
- [Refactor] move `maybeMap` to `utils`
|
|
||||||
- [Dev Deps] update `browserify`, `tape`
|
|
||||||
|
|
||||||
## **6.9.3**
|
|
||||||
- [Fix] proper comma parsing of URL-encoded commas (#361)
|
|
||||||
- [Fix] parses comma delimited array while having percent-encoded comma treated as normal text (#336)
|
|
||||||
|
|
||||||
## **6.9.2**
|
|
||||||
- [Fix] `parse`: Fix parsing array from object with `comma` true (#359)
|
|
||||||
- [Fix] `parse`: throw a TypeError instead of an Error for bad charset (#349)
|
|
||||||
- [meta] ignore eclint transitive audit warning
|
|
||||||
- [meta] fix indentation in package.json
|
|
||||||
- [meta] add tidelift marketing copy
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `object-inspect`, `has-symbols`, `tape`, `mkdirp`, `iconv-lite`
|
|
||||||
- [actions] add automatic rebasing / merge commit blocking
|
|
||||||
|
|
||||||
## **6.9.1**
|
|
||||||
- [Fix] `parse`: with comma true, handle field that holds an array of arrays (#335)
|
|
||||||
- [Fix] `parse`: with comma true, do not split non-string values (#334)
|
|
||||||
- [meta] add `funding` field
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`
|
|
||||||
- [Tests] use shared travis-ci config
|
|
||||||
|
|
||||||
## **6.9.0**
|
|
||||||
- [New] `parse`/`stringify`: Pass extra key/value argument to `decoder` (#333)
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `evalmd`
|
|
||||||
- [Tests] `parse`: add passing `arrayFormat` tests
|
|
||||||
- [Tests] add `posttest` using `npx aud` to run `npm audit` without a lockfile
|
|
||||||
- [Tests] up to `node` `v12.10`, `v11.15`, `v10.16`, `v8.16`
|
|
||||||
- [Tests] `Buffer.from` in node v5.0-v5.9 and v4.0-v4.4 requires a TypedArray
|
|
||||||
|
|
||||||
## **6.8.5**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.8.4**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.8.3**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [Fix] `stringify`: avoid encoding arrayformat comma when `encodeValuesOnly = true` (#424)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Tests] clean up stringify tests slightly
|
|
||||||
- [Docs] add note and links for coercing primitive values (#408)
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Dev Deps] backport updates from main
|
|
||||||
- [Refactor] `stringify`: reduce branching
|
|
||||||
- [meta] do not publish workflow files
|
|
||||||
|
|
||||||
## **6.8.2**
|
|
||||||
- [Fix] proper comma parsing of URL-encoded commas (#361)
|
|
||||||
- [Fix] parses comma delimited array while having percent-encoded comma treated as normal text (#336)
|
|
||||||
|
|
||||||
## **6.8.1**
|
|
||||||
- [Fix] `parse`: Fix parsing array from object with `comma` true (#359)
|
|
||||||
- [Fix] `parse`: throw a TypeError instead of an Error for bad charset (#349)
|
|
||||||
- [Fix] `parse`: with comma true, handle field that holds an array of arrays (#335)
|
|
||||||
- [fix] `parse`: with comma true, do not split non-string values (#334)
|
|
||||||
- [meta] add tidelift marketing copy
|
|
||||||
- [meta] add `funding` field
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`, `safe-publish-latest`, `evalmd`, `has-symbols`, `iconv-lite`, `mkdirp`, `object-inspect`
|
|
||||||
- [Tests] `parse`: add passing `arrayFormat` tests
|
|
||||||
- [Tests] use shared travis-ci configs
|
|
||||||
- [Tests] `Buffer.from` in node v5.0-v5.9 and v4.0-v4.4 requires a TypedArray
|
|
||||||
- [actions] add automatic rebasing / merge commit blocking
|
|
||||||
|
|
||||||
## **6.8.0**
|
|
||||||
- [New] add `depth=false` to preserve the original key; [Fix] `depth=0` should preserve the original key (#326)
|
|
||||||
- [New] [Fix] stringify symbols and bigints
|
|
||||||
- [Fix] ensure node 0.12 can stringify Symbols
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Refactor] `formats`: tiny bit of cleanup.
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `browserify`, `safe-publish-latest`, `iconv-lite`, `tape`
|
|
||||||
- [Tests] add tests for `depth=0` and `depth=false` behavior, both current and intuitive/intended (#326)
|
|
||||||
- [Tests] use `eclint` instead of `editorconfig-tools`
|
|
||||||
- [docs] readme: add security note
|
|
||||||
- [meta] add github sponsorship
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
|
|
||||||
## **6.7.5**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.7.4**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.7.3**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] `stringify`: avoid encoding arrayformat comma when `encodeValuesOnly = true` (#424)
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] add note and links for coercing primitive values (#408)
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] do not publish workflow files
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Dev Deps] backport updates from main
|
|
||||||
- [Tests] use `nyc` for coverage
|
|
||||||
- [Tests] clean up stringify tests slightly
|
|
||||||
|
|
||||||
## **6.7.2**
|
|
||||||
- [Fix] proper comma parsing of URL-encoded commas (#361)
|
|
||||||
- [Fix] parses comma delimited array while having percent-encoded comma treated as normal text (#336)
|
|
||||||
|
|
||||||
## **6.7.1**
|
|
||||||
- [Fix] `parse`: Fix parsing array from object with `comma` true (#359)
|
|
||||||
- [Fix] `parse`: with comma true, handle field that holds an array of arrays (#335)
|
|
||||||
- [fix] `parse`: with comma true, do not split non-string values (#334)
|
|
||||||
- [Fix] `parse`: throw a TypeError instead of an Error for bad charset (#349)
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Refactor] `formats`: tiny bit of cleanup.
|
|
||||||
- readme: add security note
|
|
||||||
- [meta] add tidelift marketing copy
|
|
||||||
- [meta] add `funding` field
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`, `safe-publish-latest`, `evalmd`, `iconv-lite`, `mkdirp`, `object-inspect`, `browserify`
|
|
||||||
- [Tests] `parse`: add passing `arrayFormat` tests
|
|
||||||
- [Tests] use shared travis-ci configs
|
|
||||||
- [Tests] `Buffer.from` in node v5.0-v5.9 and v4.0-v4.4 requires a TypedArray
|
|
||||||
- [Tests] add tests for `depth=0` and `depth=false` behavior, both current and intuitive/intended
|
|
||||||
- [Tests] use `eclint` instead of `editorconfig-tools`
|
|
||||||
- [actions] add automatic rebasing / merge commit blocking
|
|
||||||
|
|
||||||
## **6.7.0**
|
|
||||||
- [New] `stringify`/`parse`: add `comma` as an `arrayFormat` option (#276, #219)
|
|
||||||
- [Fix] correctly parse nested arrays (#212)
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source, also with an array source
|
|
||||||
- [Robustness] `stringify`: cache `Object.prototype.hasOwnProperty`
|
|
||||||
- [Refactor] `utils`: `isBuffer`: small tweak; add tests
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Refactor] `parse`/`stringify`: make a function to normalize the options
|
|
||||||
- [Refactor] `utils`: reduce observable [[Get]]s
|
|
||||||
- [Refactor] `stringify`/`utils`: cache `Array.isArray`
|
|
||||||
- [Tests] always use `String(x)` over `x.toString()`
|
|
||||||
- [Tests] fix Buffer tests to work in node < 4.5 and node < 5.10
|
|
||||||
- [Tests] temporarily allow coverage to fail
|
|
||||||
|
|
||||||
## **6.6.3**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.6.2**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.6.1**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
||||||
- [Fix] correctly parse nested arrays
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [Robustness] `stringify`: cache `Object.prototype.hasOwnProperty`
|
|
||||||
- [Refactor] `formats`: tiny bit of cleanup.
|
|
||||||
- [Refactor] `utils`: `isBuffer`: small tweak; add tests
|
|
||||||
- [Refactor]: `stringify`/`utils`: cache `Array.isArray`
|
|
||||||
- [Refactor] `utils`: reduce observable [[Get]]s
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Refactor] `parse`/`stringify`: make a function to normalize the options
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] Clarify the need for "arrayLimit" option
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] do not publish workflow files
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [meta] Fixes typo in CHANGELOG.md
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Tests] fix Buffer tests to work in node < 4.5 and node < 5.10
|
|
||||||
- [Tests] always use `String(x)` over `x.toString()`
|
|
||||||
- [Dev Deps] backport from main
|
|
||||||
|
|
||||||
## **6.6.0**
|
|
||||||
- [New] Add support for iso-8859-1, utf8 "sentinel" and numeric entities (#268)
|
|
||||||
- [New] move two-value combine to a `utils` function (#189)
|
|
||||||
- [Fix] `stringify`: fix a crash with `strictNullHandling` and a custom `filter`/`serializeDate` (#279)
|
|
||||||
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]` (#260)
|
|
||||||
- [Fix] `stringify`: do not crash in an obscure combo of `interpretNumericEntities`, a bad custom `decoder`, & `iso-8859-1`
|
|
||||||
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
||||||
- [refactor] `stringify`: Avoid arr = arr.concat(...), push to the existing instance (#269)
|
|
||||||
- [Refactor] `parse`: only need to reassign the var once
|
|
||||||
- [Refactor] `parse`/`stringify`: clean up `charset` options checking; fix defaults
|
|
||||||
- [Refactor] add missing defaults
|
|
||||||
- [Refactor] `parse`: one less `concat` call
|
|
||||||
- [Refactor] `utils`: `compactQueue`: make it explicitly side-effecting
|
|
||||||
- [Dev Deps] update `browserify`, `eslint`, `@ljharb/eslint-config`, `iconv-lite`, `safe-publish-latest`, `tape`
|
|
||||||
- [Tests] up to `node` `v10.10`, `v9.11`, `v8.12`, `v6.14`, `v4.9`; pin included builds to LTS
|
|
||||||
|
|
||||||
## **6.5.5**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.5.4**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] document that `addQueryPrefix` does not add `?` to empty output (#418)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.5.3**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
||||||
- [Fix] correctly parse nested arrays
|
|
||||||
- [Fix] `stringify`: fix a crash with `strictNullHandling` and a custom `filter`/`serializeDate` (#279)
|
|
||||||
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
||||||
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]`
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
||||||
- [Refactor] `utils`: reduce observable [[Get]]s
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Refactor] `stringify`: Avoid arr = arr.concat(...), push to the existing instance (#269)
|
|
||||||
- [Refactor] `parse`: only need to reassign the var once
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [Docs] Clarify the need for "arrayLimit" option
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Tests] always use `String(x)` over `x.toString()`
|
|
||||||
- [Tests] remove nonexistent tape option
|
|
||||||
- [Dev Deps] backport from main
|
|
||||||
|
|
||||||
## **6.5.2**
|
|
||||||
- [Fix] use `safer-buffer` instead of `Buffer` constructor
|
|
||||||
- [Refactor] utils: `module.exports` one thing, instead of mutating `exports` (#230)
|
|
||||||
- [Dev Deps] update `browserify`, `eslint`, `iconv-lite`, `safer-buffer`, `tape`, `browserify`
|
|
||||||
|
|
||||||
## **6.5.1**
|
|
||||||
- [Fix] Fix parsing & compacting very deep objects (#224)
|
|
||||||
- [Refactor] name utils functions
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`
|
|
||||||
- [Tests] up to `node` `v8.4`; use `nvm install-latest-npm` so newer npm doesn’t break older node
|
|
||||||
- [Tests] Use precise dist for Node.js 0.6 runtime (#225)
|
|
||||||
- [Tests] make 0.6 required, now that it’s passing
|
|
||||||
- [Tests] on `node` `v8.2`; fix npm on node 0.6
|
|
||||||
|
|
||||||
## **6.5.0**
|
|
||||||
- [New] add `utils.assign`
|
|
||||||
- [New] pass default encoder/decoder to custom encoder/decoder functions (#206)
|
|
||||||
- [New] `parse`/`stringify`: add `ignoreQueryPrefix`/`addQueryPrefix` options, respectively (#213)
|
|
||||||
- [Fix] Handle stringifying empty objects with addQueryPrefix (#217)
|
|
||||||
- [Fix] do not mutate `options` argument (#207)
|
|
||||||
- [Refactor] `parse`: cache index to reuse in else statement (#182)
|
|
||||||
- [Docs] add various badges to readme (#208)
|
|
||||||
- [Dev Deps] update `eslint`, `browserify`, `iconv-lite`, `tape`
|
|
||||||
- [Tests] up to `node` `v8.1`, `v7.10`, `v6.11`; npm v4.6 breaks on node < v1; npm v5+ breaks on node < v4
|
|
||||||
- [Tests] add `editorconfig-tools`
|
|
||||||
|
|
||||||
## **6.4.3**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.4.2**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace runkit CI badge with shields.io check-runs badge
|
|
||||||
- [readme] replace travis CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.4.1**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Fix] use `safer-buffer` instead of `Buffer` constructor
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
||||||
- [Fix] `stringify`: fix a crash with `strictNullHandling` and a custom `filter`/`serializeDate` (#279)
|
|
||||||
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
||||||
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]`
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Refactor] `stringify`: Avoid arr = arr.concat(...), push to the existing instance (#269)
|
|
||||||
- [readme] remove travis badge; add github actions/codecov badges; update URLs
|
|
||||||
- [Docs] Clarify the need for "arrayLimit" option
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Tests] remove nonexistent tape option
|
|
||||||
- [Dev Deps] backport from main
|
|
||||||
|
|
||||||
## **6.4.0**
|
|
||||||
- [New] `qs.stringify`: add `encodeValuesOnly` option
|
|
||||||
- [Fix] follow `allowPrototypes` option during merge (#201, #201)
|
|
||||||
- [Fix] support keys starting with brackets (#202, #200)
|
|
||||||
- [Fix] chmod a-x
|
|
||||||
- [Dev Deps] update `eslint`
|
|
||||||
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
||||||
- [eslint] reduce warnings
|
|
||||||
|
|
||||||
## **6.3.5**
|
|
||||||
- [Fix] fix regressions from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.3.4**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace travis CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.3.3**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] fix for an impossible situation: when the formatter is called with a non-string value
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
||||||
- [Fix] `stringify`: fix a crash with `strictNullHandling` and a custom `filter`/`serializeDate` (#279)
|
|
||||||
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
||||||
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]`
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Refactor] `stringify`: Avoid arr = arr.concat(...), push to the existing instance (#269)
|
|
||||||
- [Docs] Clarify the need for "arrayLimit" option
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Tests] use `safer-buffer` instead of `Buffer` constructor
|
|
||||||
- [Tests] remove nonexistent tape option
|
|
||||||
- [Dev Deps] backport from main
|
|
||||||
|
|
||||||
## **6.3.2**
|
|
||||||
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
||||||
- [Dev Deps] update `eslint`
|
|
||||||
- [Fix] chmod a-x
|
|
||||||
- [Fix] support keys starting with brackets (#202, #200)
|
|
||||||
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
||||||
|
|
||||||
## **6.3.1**
|
|
||||||
- [Fix] ensure that `allowPrototypes: false` does not ever shadow Object.prototype properties (thanks, @snyk!)
|
|
||||||
- [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `browserify`, `iconv-lite`, `qs-iconv`, `tape`
|
|
||||||
- [Tests] on all node minors; improve test matrix
|
|
||||||
- [Docs] document stringify option `allowDots` (#195)
|
|
||||||
- [Docs] add empty object and array values example (#195)
|
|
||||||
- [Docs] Fix minor inconsistency/typo (#192)
|
|
||||||
- [Docs] document stringify option `sort` (#191)
|
|
||||||
- [Refactor] `stringify`: throw faster with an invalid encoder
|
|
||||||
- [Refactor] remove unnecessary escapes (#184)
|
|
||||||
- Remove contributing.md, since `qs` is no longer part of `hapi` (#183)
|
|
||||||
|
|
||||||
## **6.3.0**
|
|
||||||
- [New] Add support for RFC 1738 (#174, #173)
|
|
||||||
- [New] `stringify`: Add `serializeDate` option to customize Date serialization (#159)
|
|
||||||
- [Fix] ensure `utils.merge` handles merging two arrays
|
|
||||||
- [Refactor] only constructors should be capitalized
|
|
||||||
- [Refactor] capitalized var names are for constructors only
|
|
||||||
- [Refactor] avoid using a sparse array
|
|
||||||
- [Robustness] `formats`: cache `String#replace`
|
|
||||||
- [Dev Deps] update `browserify`, `eslint`, `@ljharb/eslint-config`; add `safe-publish-latest`
|
|
||||||
- [Tests] up to `node` `v6.8`, `v4.6`; improve test matrix
|
|
||||||
- [Tests] flesh out arrayLimit/arrayFormat tests (#107)
|
|
||||||
- [Tests] skip Object.create tests when null objects are not available
|
|
||||||
- [Tests] Turn on eslint for test files (#175)
|
|
||||||
|
|
||||||
## **6.2.6**
|
|
||||||
- [Fix] fix regression from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.2.5**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace travis CI badge with shields.io check-runs badge
|
|
||||||
- [actions] fix rebase workflow permissions
|
|
||||||
|
|
||||||
## **6.2.4**
|
|
||||||
- [Fix] `parse`: ignore `__proto__` keys (#428)
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and an array source
|
|
||||||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source
|
|
||||||
- [Fix] `utils`: `merge`: fix crash when `source` is a truthy primitive & no options are provided
|
|
||||||
- [Fix] when `parseArrays` is false, properly handle keys ending in `[]`
|
|
||||||
- [Robustness] `stringify`: avoid relying on a global `undefined` (#427)
|
|
||||||
- [Refactor] use cached `Array.isArray`
|
|
||||||
- [Docs] Clarify the need for "arrayLimit" option
|
|
||||||
- [meta] fix README.md (#399)
|
|
||||||
- [meta] Clean up license text so it’s properly detected as BSD-3-Clause
|
|
||||||
- [meta] add FUNDING.yml
|
|
||||||
- [actions] backport actions from main
|
|
||||||
- [Tests] use `safer-buffer` instead of `Buffer` constructor
|
|
||||||
- [Tests] remove nonexistent tape option
|
|
||||||
- [Dev Deps] backport from main
|
|
||||||
|
|
||||||
## **6.2.3**
|
|
||||||
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
||||||
- [Fix] chmod a-x
|
|
||||||
- [Fix] support keys starting with brackets (#202, #200)
|
|
||||||
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
||||||
|
|
||||||
## **6.2.2**
|
|
||||||
- [Fix] ensure that `allowPrototypes: false` does not ever shadow Object.prototype properties
|
|
||||||
|
|
||||||
## **6.2.1**
|
|
||||||
- [Fix] ensure `key[]=x&key[]&key[]=y` results in 3, not 2, values
|
|
||||||
- [Refactor] Be explicit and use `Object.prototype.hasOwnProperty.call`
|
|
||||||
- [Tests] remove `parallelshell` since it does not reliably report failures
|
|
||||||
- [Tests] up to `node` `v6.3`, `v5.12`
|
|
||||||
- [Dev Deps] update `tape`, `eslint`, `@ljharb/eslint-config`, `qs-iconv`
|
|
||||||
|
|
||||||
## [**6.2.0**](https://github.com/ljharb/qs/issues?milestone=36&state=closed)
|
|
||||||
- [New] pass Buffers to the encoder/decoder directly (#161)
|
|
||||||
- [New] add "encoder" and "decoder" options, for custom param encoding/decoding (#160)
|
|
||||||
- [Fix] fix compacting of nested sparse arrays (#150)
|
|
||||||
|
|
||||||
## **6.1.4**
|
|
||||||
- [Fix] fix regression from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.1.3**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace travis CI badge with shields.io check-runs badge
|
|
||||||
|
|
||||||
## **6.1.2**
|
|
||||||
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
||||||
- [Fix] chmod a-x
|
|
||||||
- [Fix] support keys starting with brackets (#202, #200)
|
|
||||||
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
||||||
|
|
||||||
## **6.1.1**
|
|
||||||
- [Fix] ensure that `allowPrototypes: false` does not ever shadow Object.prototype properties
|
|
||||||
|
|
||||||
## [**6.1.0**](https://github.com/ljharb/qs/issues?milestone=35&state=closed)
|
|
||||||
- [New] allowDots option for `stringify` (#151)
|
|
||||||
- [Fix] "sort" option should work at a depth of 3 or more (#151)
|
|
||||||
- [Fix] Restore `dist` directory; will be removed in v7 (#148)
|
|
||||||
|
|
||||||
## **6.0.6**
|
|
||||||
- [Fix] fix regression from robustness refactor
|
|
||||||
- [meta] add `npmignore` to autogenerate an npmignore file
|
|
||||||
- [actions] update reusable workflows
|
|
||||||
|
|
||||||
## **6.0.5**
|
|
||||||
- [Robustness] avoid `.push`, use `void`
|
|
||||||
- [readme] clarify `parseArrays` and `arrayLimit` documentation (#543)
|
|
||||||
- [readme] replace travis CI badge with shields.io check-runs badge
|
|
||||||
|
|
||||||
## **6.0.4**
|
|
||||||
- [Fix] follow `allowPrototypes` option during merge (#201, #200)
|
|
||||||
- [Fix] chmod a-x
|
|
||||||
- [Fix] support keys starting with brackets (#202, #200)
|
|
||||||
- [Tests] up to `node` `v7.7`, `v6.10`,` v4.8`; disable osx builds since they block linux builds
|
|
||||||
|
|
||||||
## **6.0.3**
|
|
||||||
- [Fix] ensure that `allowPrototypes: false` does not ever shadow Object.prototype properties
|
|
||||||
- [Fix] Restore `dist` directory; will be removed in v7 (#148)
|
|
||||||
|
|
||||||
## [**6.0.2**](https://github.com/ljharb/qs/issues?milestone=33&state=closed)
|
|
||||||
- Revert ES6 requirement and restore support for node down to v0.8.
|
|
||||||
|
|
||||||
## [**6.0.1**](https://github.com/ljharb/qs/issues?milestone=32&state=closed)
|
|
||||||
- [**#127**](https://github.com/ljharb/qs/pull/127) Fix engines definition in package.json
|
|
||||||
|
|
||||||
## [**6.0.0**](https://github.com/ljharb/qs/issues?milestone=31&state=closed)
|
|
||||||
- [**#124**](https://github.com/ljharb/qs/issues/124) Use ES6 and drop support for node < v4
|
|
||||||
|
|
||||||
## **5.2.1**
|
|
||||||
- [Fix] ensure `key[]=x&key[]&key[]=y` results in 3, not 2, values
|
|
||||||
|
|
||||||
## [**5.2.0**](https://github.com/ljharb/qs/issues?milestone=30&state=closed)
|
|
||||||
- [**#64**](https://github.com/ljharb/qs/issues/64) Add option to sort object keys in the query string
|
|
||||||
|
|
||||||
## [**5.1.0**](https://github.com/ljharb/qs/issues?milestone=29&state=closed)
|
|
||||||
- [**#117**](https://github.com/ljharb/qs/issues/117) make URI encoding stringified results optional
|
|
||||||
- [**#106**](https://github.com/ljharb/qs/issues/106) Add flag `skipNulls` to optionally skip null values in stringify
|
|
||||||
|
|
||||||
## [**5.0.0**](https://github.com/ljharb/qs/issues?milestone=28&state=closed)
|
|
||||||
- [**#114**](https://github.com/ljharb/qs/issues/114) default allowDots to false
|
|
||||||
- [**#100**](https://github.com/ljharb/qs/issues/100) include dist to npm
|
|
||||||
|
|
||||||
## [**4.0.0**](https://github.com/ljharb/qs/issues?milestone=26&state=closed)
|
|
||||||
- [**#98**](https://github.com/ljharb/qs/issues/98) make returning plain objects and allowing prototype overwriting properties optional
|
|
||||||
|
|
||||||
## [**3.1.0**](https://github.com/ljharb/qs/issues?milestone=24&state=closed)
|
|
||||||
- [**#89**](https://github.com/ljharb/qs/issues/89) Add option to disable "Transform dot notation to bracket notation"
|
|
||||||
|
|
||||||
## [**3.0.0**](https://github.com/ljharb/qs/issues?milestone=23&state=closed)
|
|
||||||
- [**#80**](https://github.com/ljharb/qs/issues/80) qs.parse silently drops properties
|
|
||||||
- [**#77**](https://github.com/ljharb/qs/issues/77) Perf boost
|
|
||||||
- [**#60**](https://github.com/ljharb/qs/issues/60) Add explicit option to disable array parsing
|
|
||||||
- [**#74**](https://github.com/ljharb/qs/issues/74) Bad parse when turning array into object
|
|
||||||
- [**#81**](https://github.com/ljharb/qs/issues/81) Add a `filter` option
|
|
||||||
- [**#68**](https://github.com/ljharb/qs/issues/68) Fixed issue with recursion and passing strings into objects.
|
|
||||||
- [**#66**](https://github.com/ljharb/qs/issues/66) Add mixed array and object dot notation support Closes: #47
|
|
||||||
- [**#76**](https://github.com/ljharb/qs/issues/76) RFC 3986
|
|
||||||
- [**#85**](https://github.com/ljharb/qs/issues/85) No equal sign
|
|
||||||
- [**#84**](https://github.com/ljharb/qs/issues/84) update license attribute
|
|
||||||
|
|
||||||
## [**2.4.1**](https://github.com/ljharb/qs/issues?milestone=20&state=closed)
|
|
||||||
- [**#73**](https://github.com/ljharb/qs/issues/73) Property 'hasOwnProperty' of object #<Object> is not a function
|
|
||||||
|
|
||||||
## [**2.4.0**](https://github.com/ljharb/qs/issues?milestone=19&state=closed)
|
|
||||||
- [**#70**](https://github.com/ljharb/qs/issues/70) Add arrayFormat option
|
|
||||||
|
|
||||||
## [**2.3.3**](https://github.com/ljharb/qs/issues?milestone=18&state=closed)
|
|
||||||
- [**#59**](https://github.com/ljharb/qs/issues/59) make sure array indexes are >= 0, closes #57
|
|
||||||
- [**#58**](https://github.com/ljharb/qs/issues/58) make qs usable for browser loader
|
|
||||||
|
|
||||||
## [**2.3.2**](https://github.com/ljharb/qs/issues?milestone=17&state=closed)
|
|
||||||
- [**#55**](https://github.com/ljharb/qs/issues/55) allow merging a string into an object
|
|
||||||
|
|
||||||
## [**2.3.1**](https://github.com/ljharb/qs/issues?milestone=16&state=closed)
|
|
||||||
- [**#52**](https://github.com/ljharb/qs/issues/52) Return "undefined" and "false" instead of throwing "TypeError".
|
|
||||||
|
|
||||||
## [**2.3.0**](https://github.com/ljharb/qs/issues?milestone=15&state=closed)
|
|
||||||
- [**#50**](https://github.com/ljharb/qs/issues/50) add option to omit array indices, closes #46
|
|
||||||
|
|
||||||
## [**2.2.5**](https://github.com/ljharb/qs/issues?milestone=14&state=closed)
|
|
||||||
- [**#39**](https://github.com/ljharb/qs/issues/39) Is there an alternative to Buffer.isBuffer?
|
|
||||||
- [**#49**](https://github.com/ljharb/qs/issues/49) refactor utils.merge, fixes #45
|
|
||||||
- [**#41**](https://github.com/ljharb/qs/issues/41) avoid browserifying Buffer, for #39
|
|
||||||
|
|
||||||
## [**2.2.4**](https://github.com/ljharb/qs/issues?milestone=13&state=closed)
|
|
||||||
- [**#38**](https://github.com/ljharb/qs/issues/38) how to handle object keys beginning with a number
|
|
||||||
|
|
||||||
## [**2.2.3**](https://github.com/ljharb/qs/issues?milestone=12&state=closed)
|
|
||||||
- [**#37**](https://github.com/ljharb/qs/issues/37) parser discards first empty value in array
|
|
||||||
- [**#36**](https://github.com/ljharb/qs/issues/36) Update to lab 4.x
|
|
||||||
|
|
||||||
## [**2.2.2**](https://github.com/ljharb/qs/issues?milestone=11&state=closed)
|
|
||||||
- [**#33**](https://github.com/ljharb/qs/issues/33) Error when plain object in a value
|
|
||||||
- [**#34**](https://github.com/ljharb/qs/issues/34) use Object.prototype.hasOwnProperty.call instead of obj.hasOwnProperty
|
|
||||||
- [**#24**](https://github.com/ljharb/qs/issues/24) Changelog? Semver?
|
|
||||||
|
|
||||||
## [**2.2.1**](https://github.com/ljharb/qs/issues?milestone=10&state=closed)
|
|
||||||
- [**#32**](https://github.com/ljharb/qs/issues/32) account for circular references properly, closes #31
|
|
||||||
- [**#31**](https://github.com/ljharb/qs/issues/31) qs.parse stackoverflow on circular objects
|
|
||||||
|
|
||||||
## [**2.2.0**](https://github.com/ljharb/qs/issues?milestone=9&state=closed)
|
|
||||||
- [**#26**](https://github.com/ljharb/qs/issues/26) Don't use Buffer global if it's not present
|
|
||||||
- [**#30**](https://github.com/ljharb/qs/issues/30) Bug when merging non-object values into arrays
|
|
||||||
- [**#29**](https://github.com/ljharb/qs/issues/29) Don't call Utils.clone at the top of Utils.merge
|
|
||||||
- [**#23**](https://github.com/ljharb/qs/issues/23) Ability to not limit parameters?
|
|
||||||
|
|
||||||
## [**2.1.0**](https://github.com/ljharb/qs/issues?milestone=8&state=closed)
|
|
||||||
- [**#22**](https://github.com/ljharb/qs/issues/22) Enable using a RegExp as delimiter
|
|
||||||
|
|
||||||
## [**2.0.0**](https://github.com/ljharb/qs/issues?milestone=7&state=closed)
|
|
||||||
- [**#18**](https://github.com/ljharb/qs/issues/18) Why is there arrayLimit?
|
|
||||||
- [**#20**](https://github.com/ljharb/qs/issues/20) Configurable parametersLimit
|
|
||||||
- [**#21**](https://github.com/ljharb/qs/issues/21) make all limits optional, for #18, for #20
|
|
||||||
|
|
||||||
## [**1.2.2**](https://github.com/ljharb/qs/issues?milestone=6&state=closed)
|
|
||||||
- [**#19**](https://github.com/ljharb/qs/issues/19) Don't overwrite null values
|
|
||||||
|
|
||||||
## [**1.2.1**](https://github.com/ljharb/qs/issues?milestone=5&state=closed)
|
|
||||||
- [**#16**](https://github.com/ljharb/qs/issues/16) ignore non-string delimiters
|
|
||||||
- [**#15**](https://github.com/ljharb/qs/issues/15) Close code block
|
|
||||||
|
|
||||||
## [**1.2.0**](https://github.com/ljharb/qs/issues?milestone=4&state=closed)
|
|
||||||
- [**#12**](https://github.com/ljharb/qs/issues/12) Add optional delim argument
|
|
||||||
- [**#13**](https://github.com/ljharb/qs/issues/13) fix #11: flattened keys in array are now correctly parsed
|
|
||||||
|
|
||||||
## [**1.1.0**](https://github.com/ljharb/qs/issues?milestone=3&state=closed)
|
|
||||||
- [**#7**](https://github.com/ljharb/qs/issues/7) Empty values of a POST array disappear after being submitted
|
|
||||||
- [**#9**](https://github.com/ljharb/qs/issues/9) Should not omit equals signs (=) when value is null
|
|
||||||
- [**#6**](https://github.com/ljharb/qs/issues/6) Minor grammar fix in README
|
|
||||||
|
|
||||||
## [**1.0.2**](https://github.com/ljharb/qs/issues?milestone=2&state=closed)
|
|
||||||
- [**#5**](https://github.com/ljharb/qs/issues/5) array holes incorrectly copied into object on large index
|
|
||||||
-29
@@ -1,29 +0,0 @@
|
|||||||
BSD 3-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2014, Nathan LaFreniere and other [contributors](https://github.com/ljharb/qs/graphs/contributors)
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
-758
@@ -1,758 +0,0 @@
|
|||||||
<p align="center">
|
|
||||||
<img alt="qs" src="./logos/banner_default.png" width="800" />
|
|
||||||
</p>
|
|
||||||
|
|
||||||
# qs <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
|
||||||
|
|
||||||
[![github actions][actions-image]][actions-url]
|
|
||||||
[![coverage][codecov-image]][codecov-url]
|
|
||||||
[![License][license-image]][license-url]
|
|
||||||
[![Downloads][downloads-image]][downloads-url]
|
|
||||||
[](https://bestpractices.coreinfrastructure.org/projects/9058)
|
|
||||||
|
|
||||||
[![npm badge][npm-badge-png]][package-url]
|
|
||||||
|
|
||||||
A querystring parsing and stringifying library with some added security.
|
|
||||||
|
|
||||||
Lead Maintainer: [Jordan Harband](https://github.com/ljharb)
|
|
||||||
|
|
||||||
The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring).
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var qs = require('qs');
|
|
||||||
var assert = require('assert');
|
|
||||||
|
|
||||||
var obj = qs.parse('a=c');
|
|
||||||
assert.deepEqual(obj, { a: 'c' });
|
|
||||||
|
|
||||||
var str = qs.stringify(obj);
|
|
||||||
assert.equal(str, 'a=c');
|
|
||||||
```
|
|
||||||
|
|
||||||
### Parsing Objects
|
|
||||||
|
|
||||||
[](#preventEval)
|
|
||||||
```javascript
|
|
||||||
qs.parse(string, [options]);
|
|
||||||
```
|
|
||||||
|
|
||||||
**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`.
|
|
||||||
For example, the string `'foo[bar]=baz'` converts to:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('foo[bar]=baz'), {
|
|
||||||
foo: {
|
|
||||||
bar: 'baz'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
When using the `plainObjects` option the parsed value is returned as a null object, created via `{ __proto__: null }` and as such you should be aware that prototype methods will not exist on it and a user may set those names to whatever value they like:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var nullObject = qs.parse('a[hasOwnProperty]=b', { plainObjects: true });
|
|
||||||
assert.deepEqual(nullObject, { a: { hasOwnProperty: 'b' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
By default parameters that would overwrite properties on the object prototype are ignored, if you wish to keep the data from those fields either use `plainObjects` as mentioned above, or set `allowPrototypes` to `true` which will allow user input to overwrite those properties.
|
|
||||||
*WARNING* It is generally a bad idea to enable this option as it can cause problems when attempting to use the properties that have been overwritten.
|
|
||||||
Always be careful with this option.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var protoObject = qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true });
|
|
||||||
assert.deepEqual(protoObject, { a: { hasOwnProperty: 'b' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
URI encoded strings work too:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('a%5Bb%5D=c'), {
|
|
||||||
a: { b: 'c' }
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('foo[bar][baz]=foobarbaz'), {
|
|
||||||
foo: {
|
|
||||||
bar: {
|
|
||||||
baz: 'foobarbaz'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
By default, when nesting objects **qs** will only parse up to 5 children deep.
|
|
||||||
This means if you attempt to parse a string like `'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var expected = {
|
|
||||||
a: {
|
|
||||||
b: {
|
|
||||||
c: {
|
|
||||||
d: {
|
|
||||||
e: {
|
|
||||||
f: {
|
|
||||||
'[g][h][i]': 'j'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
var string = 'a[b][c][d][e][f][g][h][i]=j';
|
|
||||||
assert.deepEqual(qs.parse(string), expected);
|
|
||||||
```
|
|
||||||
|
|
||||||
This depth can be overridden by passing a `depth` option to `qs.parse(string, [options])`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var deep = qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1 });
|
|
||||||
assert.deepEqual(deep, { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } });
|
|
||||||
```
|
|
||||||
|
|
||||||
You can configure **qs** to throw an error when parsing nested input beyond this depth using the `strictDepth` option (defaulted to false):
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
try {
|
|
||||||
qs.parse('a[b][c][d][e][f][g][h][i]=j', { depth: 1, strictDepth: true });
|
|
||||||
} catch (err) {
|
|
||||||
assert(err instanceof RangeError);
|
|
||||||
assert.strictEqual(err.message, 'Input depth exceeded depth option of 1 and strictDepth is true');
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The depth limit helps mitigate abuse when **qs** is used to parse user input, and it is recommended to keep it a reasonably small number. The strictDepth option adds a layer of protection by throwing an error when the limit is exceeded, allowing you to catch and handle such cases.
|
|
||||||
|
|
||||||
For similar reasons, by default **qs** will only parse up to 1000 parameters. This can be overridden by passing a `parameterLimit` option:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var limited = qs.parse('a=b&c=d', { parameterLimit: 1 });
|
|
||||||
assert.deepEqual(limited, { a: 'b' });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want an error to be thrown whenever the a limit is exceeded (eg, `parameterLimit`, `arrayLimit`), set the `throwOnLimitExceeded` option to `true`. This option will generate a descriptive error if the query string exceeds a configured limit.
|
|
||||||
```javascript
|
|
||||||
try {
|
|
||||||
qs.parse('a=1&b=2&c=3&d=4', { parameterLimit: 3, throwOnLimitExceeded: true });
|
|
||||||
} catch (err) {
|
|
||||||
assert(err instanceof Error);
|
|
||||||
assert.strictEqual(err.message, 'Parameter limit exceeded. Only 3 parameters allowed.');
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
When `throwOnLimitExceeded` is set to `false` (default), **qs** will parse up to the specified `parameterLimit` and ignore the rest without throwing an error.
|
|
||||||
|
|
||||||
To bypass the leading question mark, use `ignoreQueryPrefix`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var prefixed = qs.parse('?a=b&c=d', { ignoreQueryPrefix: true });
|
|
||||||
assert.deepEqual(prefixed, { a: 'b', c: 'd' });
|
|
||||||
```
|
|
||||||
|
|
||||||
An optional delimiter can also be passed:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var delimited = qs.parse('a=b;c=d', { delimiter: ';' });
|
|
||||||
assert.deepEqual(delimited, { a: 'b', c: 'd' });
|
|
||||||
```
|
|
||||||
|
|
||||||
Delimiters can be a regular expression too:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var regexed = qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ });
|
|
||||||
assert.deepEqual(regexed, { a: 'b', c: 'd', e: 'f' });
|
|
||||||
```
|
|
||||||
|
|
||||||
Option `allowDots` can be used to enable dot notation:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withDots = qs.parse('a.b=c', { allowDots: true });
|
|
||||||
assert.deepEqual(withDots, { a: { b: 'c' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
Option `decodeDotInKeys` can be used to decode dots in keys
|
|
||||||
Note: it implies `allowDots`, so `parse` will error if you set `decodeDotInKeys` to `true`, and `allowDots` to `false`.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withDots = qs.parse('name%252Eobj.first=John&name%252Eobj.last=Doe', { decodeDotInKeys: true });
|
|
||||||
assert.deepEqual(withDots, { 'name.obj': { first: 'John', last: 'Doe' }});
|
|
||||||
```
|
|
||||||
|
|
||||||
Option `allowEmptyArrays` can be used to allowing empty array values in object
|
|
||||||
```javascript
|
|
||||||
var withEmptyArrays = qs.parse('foo[]&bar=baz', { allowEmptyArrays: true });
|
|
||||||
assert.deepEqual(withEmptyArrays, { foo: [], bar: 'baz' });
|
|
||||||
```
|
|
||||||
|
|
||||||
Option `duplicates` can be used to change the behavior when duplicate keys are encountered
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('foo=bar&foo=baz'), { foo: ['bar', 'baz'] });
|
|
||||||
assert.deepEqual(qs.parse('foo=bar&foo=baz', { duplicates: 'combine' }), { foo: ['bar', 'baz'] });
|
|
||||||
assert.deepEqual(qs.parse('foo=bar&foo=baz', { duplicates: 'first' }), { foo: 'bar' });
|
|
||||||
assert.deepEqual(qs.parse('foo=bar&foo=baz', { duplicates: 'last' }), { foo: 'baz' });
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that keys with bracket notation (`[]`) always combine into arrays, regardless of the `duplicates` setting:
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('a=1&a=2&b[]=1&b[]=2', { duplicates: 'last' }), { a: '2', b: ['1', '2'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you have to deal with legacy browsers or services, there's also support for decoding percent-encoded octets as iso-8859-1:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var oldCharset = qs.parse('a=%A7', { charset: 'iso-8859-1' });
|
|
||||||
assert.deepEqual(oldCharset, { a: '§' });
|
|
||||||
```
|
|
||||||
|
|
||||||
Some services add an initial `utf8=✓` value to forms so that old Internet Explorer versions are more likely to submit the form as utf-8.
|
|
||||||
Additionally, the server can check the value against wrong encodings of the checkmark character and detect that a query string or `application/x-www-form-urlencoded` body was *not* sent as utf-8, eg. if the form had an `accept-charset` parameter or the containing page had a different character set.
|
|
||||||
|
|
||||||
**qs** supports this mechanism via the `charsetSentinel` option.
|
|
||||||
If specified, the `utf8` parameter will be omitted from the returned object.
|
|
||||||
It will be used to switch to `iso-8859-1`/`utf-8` mode depending on how the checkmark is encoded.
|
|
||||||
|
|
||||||
**Important**: When you specify both the `charset` option and the `charsetSentinel` option, the `charset` will be overridden when the request contains a `utf8` parameter from which the actual charset can be deduced.
|
|
||||||
In that sense the `charset` will behave as the default charset rather than the authoritative charset.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var detectedAsUtf8 = qs.parse('utf8=%E2%9C%93&a=%C3%B8', {
|
|
||||||
charset: 'iso-8859-1',
|
|
||||||
charsetSentinel: true
|
|
||||||
});
|
|
||||||
assert.deepEqual(detectedAsUtf8, { a: 'ø' });
|
|
||||||
|
|
||||||
// Browsers encode the checkmark as ✓ when submitting as iso-8859-1:
|
|
||||||
var detectedAsIso8859_1 = qs.parse('utf8=%26%2310003%3B&a=%F8', {
|
|
||||||
charset: 'utf-8',
|
|
||||||
charsetSentinel: true
|
|
||||||
});
|
|
||||||
assert.deepEqual(detectedAsIso8859_1, { a: 'ø' });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want to decode the `&#...;` syntax to the actual character, you can specify the `interpretNumericEntities` option as well:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var detectedAsIso8859_1 = qs.parse('a=%26%239786%3B', {
|
|
||||||
charset: 'iso-8859-1',
|
|
||||||
interpretNumericEntities: true
|
|
||||||
});
|
|
||||||
assert.deepEqual(detectedAsIso8859_1, { a: '☺' });
|
|
||||||
```
|
|
||||||
|
|
||||||
It also works when the charset has been detected in `charsetSentinel` mode.
|
|
||||||
|
|
||||||
### Parsing Arrays
|
|
||||||
|
|
||||||
**qs** can also parse arrays using a similar `[]` notation:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withArray = qs.parse('a[]=b&a[]=c');
|
|
||||||
assert.deepEqual(withArray, { a: ['b', 'c'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
You may specify an index as well:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withIndexes = qs.parse('a[1]=c&a[0]=b');
|
|
||||||
assert.deepEqual(withIndexes, { a: ['b', 'c'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number to create an array.
|
|
||||||
When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving their order:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var noSparse = qs.parse('a[1]=b&a[15]=c');
|
|
||||||
assert.deepEqual(noSparse, { a: ['b', 'c'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
You may also use `allowSparse` option to parse sparse arrays:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var sparseArray = qs.parse('a[1]=2&a[3]=5', { allowSparse: true });
|
|
||||||
assert.deepEqual(sparseArray, { a: [, '2', , '5'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that an empty string is also a value, and will be preserved:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withEmptyString = qs.parse('a[]=&a[]=b');
|
|
||||||
assert.deepEqual(withEmptyString, { a: ['', 'b'] });
|
|
||||||
|
|
||||||
var withIndexedEmptyString = qs.parse('a[0]=b&a[1]=&a[2]=c');
|
|
||||||
assert.deepEqual(withIndexedEmptyString, { a: ['b', '', 'c'] });
|
|
||||||
```
|
|
||||||
|
|
||||||
**qs** will also limit arrays to a maximum of `20` elements.
|
|
||||||
Any array members with an index of `20` or greater will instead be converted to an object with the index as the key.
|
|
||||||
This is needed to handle cases when someone sent, for example, `a[999999999]` and it will take significant time to iterate over this huge array.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withMaxIndex = qs.parse('a[100]=b');
|
|
||||||
assert.deepEqual(withMaxIndex, { a: { '100': 'b' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
This limit can be overridden by passing an `arrayLimit` option:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withArrayLimit = qs.parse('a[1]=b', { arrayLimit: 0 });
|
|
||||||
assert.deepEqual(withArrayLimit, { a: { '1': 'b' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you want to throw an error whenever the array limit is exceeded, set the `throwOnLimitExceeded` option to `true`. This option will generate a descriptive error if the query string exceeds a configured limit.
|
|
||||||
```javascript
|
|
||||||
try {
|
|
||||||
qs.parse('a[1]=b', { arrayLimit: 0, throwOnLimitExceeded: true });
|
|
||||||
} catch (err) {
|
|
||||||
assert(err instanceof Error);
|
|
||||||
assert.strictEqual(err.message, 'Array limit exceeded. Only 0 elements allowed in an array.');
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
When `throwOnLimitExceeded` is set to `false` (default), **qs** will parse up to the specified `arrayLimit` and if the limit is exceeded, the array will instead be converted to an object with the index as the key
|
|
||||||
|
|
||||||
To prevent array syntax (`a[]`, `a[0]`) from being parsed as arrays, set `parseArrays` to `false`.
|
|
||||||
Note that duplicate keys (e.g. `a=b&a=c`) may still produce arrays when `duplicates` is `'combine'` (the default).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var noParsingArrays = qs.parse('a[]=b', { parseArrays: false });
|
|
||||||
assert.deepEqual(noParsingArrays, { a: { '0': 'b' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you mix notations, **qs** will merge the two items into an object:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var mixedNotation = qs.parse('a[0]=b&a[b]=c');
|
|
||||||
assert.deepEqual(mixedNotation, { a: { '0': 'b', b: 'c' } });
|
|
||||||
```
|
|
||||||
|
|
||||||
When a key appears as both a plain value and an object, **qs** will by default wrap the conflicting values in an array (`strictMerge` defaults to `true`):
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('a[b]=c&a=d'), { a: [{ b: 'c' }, 'd'] });
|
|
||||||
assert.deepEqual(qs.parse('a=d&a[b]=c'), { a: ['d', { b: 'c' }] });
|
|
||||||
```
|
|
||||||
|
|
||||||
To restore the legacy behavior (where the primitive is used as a key with value `true`), set `strictMerge` to `false`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.deepEqual(qs.parse('a[b]=c&a=d', { strictMerge: false }), { a: { b: 'c', d: true } });
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also create arrays of objects:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var arraysOfObjects = qs.parse('a[][b]=c');
|
|
||||||
assert.deepEqual(arraysOfObjects, { a: [{ b: 'c' }] });
|
|
||||||
```
|
|
||||||
|
|
||||||
Some people use comma to join array, **qs** can parse it:
|
|
||||||
```javascript
|
|
||||||
var arraysOfObjects = qs.parse('a=b,c', { comma: true })
|
|
||||||
assert.deepEqual(arraysOfObjects, { a: ['b', 'c'] })
|
|
||||||
```
|
|
||||||
(_this cannot convert nested objects, such as `a={b:1},{c:d}`_)
|
|
||||||
|
|
||||||
### Parsing primitive/scalar values (numbers, booleans, null, etc)
|
|
||||||
|
|
||||||
By default, all values are parsed as strings.
|
|
||||||
This behavior will not change and is explained in [issue #91](https://github.com/ljharb/qs/issues/91).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var primitiveValues = qs.parse('a=15&b=true&c=null');
|
|
||||||
assert.deepEqual(primitiveValues, { a: '15', b: 'true', c: 'null' });
|
|
||||||
```
|
|
||||||
|
|
||||||
If you wish to auto-convert values which look like numbers, booleans, and other values into their primitive counterparts, you can use the [query-types Express JS middleware](https://github.com/xpepermint/query-types) which will auto-convert all request query parameters.
|
|
||||||
|
|
||||||
### Stringifying
|
|
||||||
|
|
||||||
[](#preventEval)
|
|
||||||
```javascript
|
|
||||||
qs.stringify(object, [options]);
|
|
||||||
```
|
|
||||||
|
|
||||||
When stringifying, **qs** by default URI encodes output. Objects are stringified as you would expect:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: 'b' }), 'a=b');
|
|
||||||
assert.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
|
|
||||||
```
|
|
||||||
|
|
||||||
This encoding can be disabled by setting the `encode` option to `false`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var unencoded = qs.stringify({ a: { b: 'c' } }, { encode: false });
|
|
||||||
assert.equal(unencoded, 'a[b]=c');
|
|
||||||
```
|
|
||||||
|
|
||||||
Encoding can be disabled for keys by setting the `encodeValuesOnly` option to `true`:
|
|
||||||
```javascript
|
|
||||||
var encodedValues = qs.stringify(
|
|
||||||
{ a: 'b', c: ['d', 'e=f'], f: [['g'], ['h']] },
|
|
||||||
{ encodeValuesOnly: true }
|
|
||||||
);
|
|
||||||
assert.equal(encodedValues,'a=b&c[0]=d&c[1]=e%3Df&f[0][0]=g&f[1][0]=h');
|
|
||||||
```
|
|
||||||
|
|
||||||
This encoding can also be replaced by a custom encoding method set as `encoder` option:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var encoded = qs.stringify({ a: { b: 'c' } }, { encoder: function (str) {
|
|
||||||
// Passed in values `a`, `b`, `c`
|
|
||||||
return // Return encoded string
|
|
||||||
}})
|
|
||||||
```
|
|
||||||
|
|
||||||
_(Note: the `encoder` option does not apply if `encode` is `false`)_
|
|
||||||
|
|
||||||
Analogue to the `encoder` there is a `decoder` option for `parse` to override decoding of properties and values:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var decoded = qs.parse('x=z', { decoder: function (str) {
|
|
||||||
// Passed in values `x`, `z`
|
|
||||||
return // Return decoded string
|
|
||||||
}})
|
|
||||||
```
|
|
||||||
|
|
||||||
You can encode keys and values using different logic by using the type argument provided to the encoder:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var encoded = qs.stringify({ a: { b: 'c' } }, { encoder: function (str, defaultEncoder, charset, type) {
|
|
||||||
if (type === 'key') {
|
|
||||||
return // Encoded key
|
|
||||||
} else if (type === 'value') {
|
|
||||||
return // Encoded value
|
|
||||||
}
|
|
||||||
}})
|
|
||||||
```
|
|
||||||
|
|
||||||
The type argument is also provided to the decoder:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var decoded = qs.parse('x=z', { decoder: function (str, defaultDecoder, charset, type) {
|
|
||||||
if (type === 'key') {
|
|
||||||
return // Decoded key
|
|
||||||
} else if (type === 'value') {
|
|
||||||
return // Decoded value
|
|
||||||
}
|
|
||||||
}})
|
|
||||||
```
|
|
||||||
|
|
||||||
Examples beyond this point will be shown as though the output is not URI encoded for clarity.
|
|
||||||
Please note that the return values in these cases *will* be URI encoded during real usage.
|
|
||||||
|
|
||||||
When arrays are stringified, they follow the `arrayFormat` option, which defaults to `indices`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ a: ['b', 'c', 'd'] });
|
|
||||||
// 'a[0]=b&a[1]=c&a[2]=d'
|
|
||||||
```
|
|
||||||
|
|
||||||
You may override this by setting the `indices` option to `false`, or to be more explicit, the `arrayFormat` option to `repeat`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false });
|
|
||||||
// 'a=b&a=c&a=d'
|
|
||||||
```
|
|
||||||
|
|
||||||
You may use the `arrayFormat` option to specify the format of the output array:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
|
|
||||||
// 'a[0]=b&a[1]=c'
|
|
||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
|
|
||||||
// 'a[]=b&a[]=c'
|
|
||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
|
|
||||||
// 'a=b&a=c'
|
|
||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
|
|
||||||
// 'a=b,c'
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: when using `arrayFormat` set to `'comma'`, you can also pass the `commaRoundTrip` option set to `true` or `false`, to append `[]` on single-item arrays, so that they can round trip through a parse.
|
|
||||||
|
|
||||||
When objects are stringified, by default they use bracket notation:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } });
|
|
||||||
// 'a[b][c]=d&a[b][e]=f'
|
|
||||||
```
|
|
||||||
|
|
||||||
You may override this to use dot notation by setting the `allowDots` option to `true`:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { allowDots: true });
|
|
||||||
// 'a.b.c=d&a.b.e=f'
|
|
||||||
```
|
|
||||||
|
|
||||||
You may encode the dot notation in the keys of object with option `encodeDotInKeys` by setting it to `true`:
|
|
||||||
Note: it implies `allowDots`, so `stringify` will error if you set `decodeDotInKeys` to `true`, and `allowDots` to `false`.
|
|
||||||
Caveat: when `encodeValuesOnly` is `true` as well as `encodeDotInKeys`, only dots in keys and nothing else will be encoded.
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ "name.obj": { "first": "John", "last": "Doe" } }, { allowDots: true, encodeDotInKeys: true })
|
|
||||||
// 'name%252Eobj.first=John&name%252Eobj.last=Doe'
|
|
||||||
```
|
|
||||||
|
|
||||||
You may allow empty array values by setting the `allowEmptyArrays` option to `true`:
|
|
||||||
```javascript
|
|
||||||
qs.stringify({ foo: [], bar: 'baz' }, { allowEmptyArrays: true });
|
|
||||||
// 'foo[]&bar=baz'
|
|
||||||
```
|
|
||||||
|
|
||||||
Empty strings and null values will omit the value, but the equals sign (=) remains in place:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: '' }), 'a=');
|
|
||||||
```
|
|
||||||
|
|
||||||
Key with no values (such as an empty object or array) will return nothing:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: [] }), '');
|
|
||||||
assert.equal(qs.stringify({ a: {} }), '');
|
|
||||||
assert.equal(qs.stringify({ a: [{}] }), '');
|
|
||||||
assert.equal(qs.stringify({ a: { b: []} }), '');
|
|
||||||
assert.equal(qs.stringify({ a: { b: {}} }), '');
|
|
||||||
```
|
|
||||||
|
|
||||||
Properties that are set to `undefined` will be omitted entirely:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: null, b: undefined }), 'a=');
|
|
||||||
```
|
|
||||||
|
|
||||||
The query string may optionally be prepended with a question mark:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: 'b', c: 'd' }, { addQueryPrefix: true }), '?a=b&c=d');
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that when the output is an empty string, the prefix will not be added:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({}, { addQueryPrefix: true }), '');
|
|
||||||
```
|
|
||||||
|
|
||||||
The delimiter may be overridden with stringify as well:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
assert.equal(qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' }), 'a=b;c=d');
|
|
||||||
```
|
|
||||||
|
|
||||||
If you only want to override the serialization of `Date` objects, you can provide a `serializeDate` option:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var date = new Date(7);
|
|
||||||
assert.equal(qs.stringify({ a: date }), 'a=1970-01-01T00:00:00.007Z'.replace(/:/g, '%3A'));
|
|
||||||
assert.equal(
|
|
||||||
qs.stringify({ a: date }, { serializeDate: function (d) { return d.getTime(); } }),
|
|
||||||
'a=7'
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
You may use the `sort` option to affect the order of parameter keys:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
function alphabeticalSort(a, b) {
|
|
||||||
return a.localeCompare(b);
|
|
||||||
}
|
|
||||||
assert.equal(qs.stringify({ a: 'c', z: 'y', b : 'f' }, { sort: alphabeticalSort }), 'a=c&b=f&z=y');
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, you can use the `filter` option to restrict which keys will be included in the stringified output.
|
|
||||||
If you pass a function, it will be called for each key to obtain the replacement value.
|
|
||||||
Otherwise, if you pass an array, it will be used to select properties and array indices for stringification:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
function filterFunc(prefix, value) {
|
|
||||||
if (prefix == 'b') {
|
|
||||||
// Return an `undefined` value to omit a property.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (prefix == 'e[f]') {
|
|
||||||
return value.getTime();
|
|
||||||
}
|
|
||||||
if (prefix == 'e[g][0]') {
|
|
||||||
return value * 2;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
qs.stringify({ a: 'b', c: 'd', e: { f: new Date(123), g: [2] } }, { filter: filterFunc });
|
|
||||||
// 'a=b&c=d&e[f]=123&e[g][0]=4'
|
|
||||||
qs.stringify({ a: 'b', c: 'd', e: 'f' }, { filter: ['a', 'e'] });
|
|
||||||
// 'a=b&e=f'
|
|
||||||
qs.stringify({ a: ['b', 'c', 'd'], e: 'f' }, { filter: ['a', 0, 2] });
|
|
||||||
// 'a[0]=b&a[2]=d'
|
|
||||||
```
|
|
||||||
|
|
||||||
You could also use `filter` to inject custom serialization for user defined types.
|
|
||||||
Consider you're working with some api that expects query strings of the format for ranges:
|
|
||||||
|
|
||||||
```
|
|
||||||
https://domain.com/endpoint?range=30...70
|
|
||||||
```
|
|
||||||
|
|
||||||
For which you model as:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
class Range {
|
|
||||||
constructor(from, to) {
|
|
||||||
this.from = from;
|
|
||||||
this.to = to;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
You could _inject_ a custom serializer to handle values of this type:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
qs.stringify(
|
|
||||||
{
|
|
||||||
range: new Range(30, 70),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
filter: (prefix, value) => {
|
|
||||||
if (value instanceof Range) {
|
|
||||||
return `${value.from}...${value.to}`;
|
|
||||||
}
|
|
||||||
// serialize the usual way
|
|
||||||
return value;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
// range=30...70
|
|
||||||
```
|
|
||||||
|
|
||||||
### Handling of `null` values
|
|
||||||
|
|
||||||
By default, `null` values are treated like empty strings:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var withNull = qs.stringify({ a: null, b: '' });
|
|
||||||
assert.equal(withNull, 'a=&b=');
|
|
||||||
```
|
|
||||||
|
|
||||||
Parsing does not distinguish between parameters with and without equal signs.
|
|
||||||
Both are converted to empty strings.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var equalsInsensitive = qs.parse('a&b=');
|
|
||||||
assert.deepEqual(equalsInsensitive, { a: '', b: '' });
|
|
||||||
```
|
|
||||||
|
|
||||||
To distinguish between `null` values and empty strings use the `strictNullHandling` flag. In the result string the `null`
|
|
||||||
values have no `=` sign:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var strictNull = qs.stringify({ a: null, b: '' }, { strictNullHandling: true });
|
|
||||||
assert.equal(strictNull, 'a&b=');
|
|
||||||
```
|
|
||||||
|
|
||||||
To parse values without `=` back to `null` use the `strictNullHandling` flag:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var parsedStrictNull = qs.parse('a&b=', { strictNullHandling: true });
|
|
||||||
assert.deepEqual(parsedStrictNull, { a: null, b: '' });
|
|
||||||
```
|
|
||||||
|
|
||||||
To completely skip rendering keys with `null` values, use the `skipNulls` flag:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var nullsSkipped = qs.stringify({ a: 'b', c: null}, { skipNulls: true });
|
|
||||||
assert.equal(nullsSkipped, 'a=b');
|
|
||||||
```
|
|
||||||
|
|
||||||
If you're communicating with legacy systems, you can switch to `iso-8859-1` using the `charset` option:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var iso = qs.stringify({ æ: 'æ' }, { charset: 'iso-8859-1' });
|
|
||||||
assert.equal(iso, '%E6=%E6');
|
|
||||||
```
|
|
||||||
|
|
||||||
Characters that don't exist in `iso-8859-1` will be converted to numeric entities, similar to what browsers do:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var numeric = qs.stringify({ a: '☺' }, { charset: 'iso-8859-1' });
|
|
||||||
assert.equal(numeric, 'a=%26%239786%3B');
|
|
||||||
```
|
|
||||||
|
|
||||||
You can use the `charsetSentinel` option to announce the character by including an `utf8=✓` parameter with the proper encoding if the checkmark, similar to what Ruby on Rails and others do when submitting forms.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var sentinel = qs.stringify({ a: '☺' }, { charsetSentinel: true });
|
|
||||||
assert.equal(sentinel, 'utf8=%E2%9C%93&a=%E2%98%BA');
|
|
||||||
|
|
||||||
var isoSentinel = qs.stringify({ a: 'æ' }, { charsetSentinel: true, charset: 'iso-8859-1' });
|
|
||||||
assert.equal(isoSentinel, 'utf8=%26%2310003%3B&a=%E6');
|
|
||||||
```
|
|
||||||
|
|
||||||
### Dealing with special character sets
|
|
||||||
|
|
||||||
By default the encoding and decoding of characters is done in `utf-8`, and `iso-8859-1` support is also built in via the `charset` parameter.
|
|
||||||
|
|
||||||
If you wish to encode querystrings to a different character set (i.e.
|
|
||||||
[Shift JIS](https://en.wikipedia.org/wiki/Shift_JIS)) you can use the
|
|
||||||
[`qs-iconv`](https://github.com/martinheidegger/qs-iconv) library:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var encoder = require('qs-iconv/encoder')('shift_jis');
|
|
||||||
var shiftJISEncoded = qs.stringify({ a: 'こんにちは!' }, { encoder: encoder });
|
|
||||||
assert.equal(shiftJISEncoded, 'a=%82%B1%82%F1%82%C9%82%BF%82%CD%81I');
|
|
||||||
```
|
|
||||||
|
|
||||||
This also works for decoding of query strings:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
var decoder = require('qs-iconv/decoder')('shift_jis');
|
|
||||||
var obj = qs.parse('a=%82%B1%82%F1%82%C9%82%BF%82%CD%81I', { decoder: decoder });
|
|
||||||
assert.deepEqual(obj, { a: 'こんにちは!' });
|
|
||||||
```
|
|
||||||
|
|
||||||
### RFC 3986 and RFC 1738 space encoding
|
|
||||||
|
|
||||||
RFC3986 used as default option and encodes ' ' to *%20* which is backward compatible.
|
|
||||||
In the same time, output can be stringified as per RFC1738 with ' ' equal to '+'.
|
|
||||||
|
|
||||||
```
|
|
||||||
assert.equal(qs.stringify({ a: 'b c' }), 'a=b%20c');
|
|
||||||
assert.equal(qs.stringify({ a: 'b c' }, { format : 'RFC3986' }), 'a=b%20c');
|
|
||||||
assert.equal(qs.stringify({ a: 'b c' }, { format : 'RFC1738' }), 'a=b+c');
|
|
||||||
```
|
|
||||||
|
|
||||||
## Security
|
|
||||||
|
|
||||||
Please email [@ljharb](https://github.com/ljharb) or see https://tidelift.com/security if you have a potential security vulnerability to report.
|
|
||||||
|
|
||||||
## qs for enterprise
|
|
||||||
|
|
||||||
Available as part of the Tidelift Subscription
|
|
||||||
|
|
||||||
The maintainers of qs and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications.
|
|
||||||
Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use.
|
|
||||||
[Learn more.](https://tidelift.com/subscription/pkg/npm-qs?utm_source=npm-qs&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
|
||||||
|
|
||||||
[package-url]: https://npmjs.org/package/qs
|
|
||||||
[npm-version-svg]: https://versionbadg.es/ljharb/qs.svg
|
|
||||||
[deps-svg]: https://david-dm.org/ljharb/qs.svg
|
|
||||||
[deps-url]: https://david-dm.org/ljharb/qs
|
|
||||||
[dev-deps-svg]: https://david-dm.org/ljharb/qs/dev-status.svg
|
|
||||||
[dev-deps-url]: https://david-dm.org/ljharb/qs#info=devDependencies
|
|
||||||
[npm-badge-png]: https://nodei.co/npm/qs.png?downloads=true&stars=true
|
|
||||||
[license-image]: https://img.shields.io/npm/l/qs.svg
|
|
||||||
[license-url]: LICENSE
|
|
||||||
[downloads-image]: https://img.shields.io/npm/dm/qs.svg
|
|
||||||
[downloads-url]: https://npm-stat.com/charts.html?package=qs
|
|
||||||
[codecov-image]: https://codecov.io/gh/ljharb/qs/branch/main/graphs/badge.svg
|
|
||||||
[codecov-url]: https://app.codecov.io/gh/ljharb/qs/
|
|
||||||
[actions-image]: https://img.shields.io/github/check-runs/ljharb/qs/main
|
|
||||||
[actions-url]: https://github.com/ljharb/qs/actions
|
|
||||||
|
|
||||||
## Acknowledgements
|
|
||||||
|
|
||||||
qs logo by [NUMI](https://github.com/numi-hq/open-design):
|
|
||||||
|
|
||||||
[<img src="https://raw.githubusercontent.com/numi-hq/open-design/main/assets/numi-lockup.png" alt="NUMI Logo" style="width: 200px;"/>](https://numi.tech/?ref=qs)
|
|
||||||
-141
File diff suppressed because one or more lines are too long
-56
@@ -1,56 +0,0 @@
|
|||||||
import ljharbConfig from '@ljharb/eslint-config/flat';
|
|
||||||
|
|
||||||
export default [
|
|
||||||
{
|
|
||||||
ignores: ['dist/'],
|
|
||||||
},
|
|
||||||
|
|
||||||
...ljharbConfig,
|
|
||||||
|
|
||||||
{
|
|
||||||
rules: {
|
|
||||||
complexity: 'off',
|
|
||||||
'consistent-return': 'warn',
|
|
||||||
'func-name-matching': 'off',
|
|
||||||
'id-length': [
|
|
||||||
'error',
|
|
||||||
{
|
|
||||||
max: 25,
|
|
||||||
min: 1,
|
|
||||||
properties: 'never',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
indent: ['error', 4],
|
|
||||||
'max-lines': 'off',
|
|
||||||
'max-lines-per-function': [
|
|
||||||
'error',
|
|
||||||
{ max: 150 },
|
|
||||||
],
|
|
||||||
'max-params': ['error', 18],
|
|
||||||
'max-statements': ['error', 100],
|
|
||||||
'multiline-comment-style': 'off',
|
|
||||||
'no-continue': 'warn',
|
|
||||||
'no-magic-numbers': 'off',
|
|
||||||
'no-restricted-syntax': [
|
|
||||||
'error',
|
|
||||||
'BreakStatement',
|
|
||||||
'DebuggerStatement',
|
|
||||||
'ForInStatement',
|
|
||||||
'LabeledStatement',
|
|
||||||
'WithStatement',
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
files: ['test/**'],
|
|
||||||
rules: {
|
|
||||||
'function-paren-newline': 'off',
|
|
||||||
'max-lines-per-function': 'off',
|
|
||||||
'max-statements': 'off',
|
|
||||||
'no-buffer-constructor': 'off',
|
|
||||||
'no-extend-native': 'off',
|
|
||||||
'no-throw-literal': 'off',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var replace = String.prototype.replace;
|
|
||||||
var percentTwenties = /%20/g;
|
|
||||||
|
|
||||||
var Format = {
|
|
||||||
RFC1738: 'RFC1738',
|
|
||||||
RFC3986: 'RFC3986'
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
'default': Format.RFC3986,
|
|
||||||
formatters: {
|
|
||||||
RFC1738: function (value) {
|
|
||||||
return replace.call(value, percentTwenties, '+');
|
|
||||||
},
|
|
||||||
RFC3986: function (value) {
|
|
||||||
return String(value);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
RFC1738: Format.RFC1738,
|
|
||||||
RFC3986: Format.RFC3986
|
|
||||||
};
|
|
||||||
-11
@@ -1,11 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var stringify = require('./stringify');
|
|
||||||
var parse = require('./parse');
|
|
||||||
var formats = require('./formats');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
formats: formats,
|
|
||||||
parse: parse,
|
|
||||||
stringify: stringify
|
|
||||||
};
|
|
||||||
-373
@@ -1,373 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var utils = require('./utils');
|
|
||||||
|
|
||||||
var has = Object.prototype.hasOwnProperty;
|
|
||||||
var isArray = Array.isArray;
|
|
||||||
|
|
||||||
var defaults = {
|
|
||||||
allowDots: false,
|
|
||||||
allowEmptyArrays: false,
|
|
||||||
allowPrototypes: false,
|
|
||||||
allowSparse: false,
|
|
||||||
arrayLimit: 20,
|
|
||||||
charset: 'utf-8',
|
|
||||||
charsetSentinel: false,
|
|
||||||
comma: false,
|
|
||||||
decodeDotInKeys: false,
|
|
||||||
decoder: utils.decode,
|
|
||||||
delimiter: '&',
|
|
||||||
depth: 5,
|
|
||||||
duplicates: 'combine',
|
|
||||||
ignoreQueryPrefix: false,
|
|
||||||
interpretNumericEntities: false,
|
|
||||||
parameterLimit: 1000,
|
|
||||||
parseArrays: true,
|
|
||||||
plainObjects: false,
|
|
||||||
strictDepth: false,
|
|
||||||
strictMerge: true,
|
|
||||||
strictNullHandling: false,
|
|
||||||
throwOnLimitExceeded: false
|
|
||||||
};
|
|
||||||
|
|
||||||
var interpretNumericEntities = function (str) {
|
|
||||||
return str.replace(/&#(\d+);/g, function ($0, numberStr) {
|
|
||||||
return String.fromCharCode(parseInt(numberStr, 10));
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var parseArrayValue = function (val, options, currentArrayLength) {
|
|
||||||
if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) {
|
|
||||||
return val.split(',');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.throwOnLimitExceeded && currentArrayLength >= options.arrayLimit) {
|
|
||||||
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return val;
|
|
||||||
};
|
|
||||||
|
|
||||||
// This is what browsers will submit when the ✓ character occurs in an
|
|
||||||
// application/x-www-form-urlencoded body and the encoding of the page containing
|
|
||||||
// the form is iso-8859-1, or when the submitted form has an accept-charset
|
|
||||||
// attribute of iso-8859-1. Presumably also with other charsets that do not contain
|
|
||||||
// the ✓ character, such as us-ascii.
|
|
||||||
var isoSentinel = 'utf8=%26%2310003%3B'; // encodeURIComponent('✓')
|
|
||||||
|
|
||||||
// These are the percent-encoded utf-8 octets representing a checkmark, indicating that the request actually is utf-8 encoded.
|
|
||||||
var charsetSentinel = 'utf8=%E2%9C%93'; // encodeURIComponent('✓')
|
|
||||||
|
|
||||||
var parseValues = function parseQueryStringValues(str, options) {
|
|
||||||
var obj = { __proto__: null };
|
|
||||||
|
|
||||||
var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
|
|
||||||
cleanStr = cleanStr.replace(/%5B/gi, '[').replace(/%5D/gi, ']');
|
|
||||||
|
|
||||||
var limit = options.parameterLimit === Infinity ? void undefined : options.parameterLimit;
|
|
||||||
var parts = cleanStr.split(
|
|
||||||
options.delimiter,
|
|
||||||
options.throwOnLimitExceeded && typeof limit !== 'undefined' ? limit + 1 : limit
|
|
||||||
);
|
|
||||||
|
|
||||||
if (options.throwOnLimitExceeded && typeof limit !== 'undefined' && parts.length > limit) {
|
|
||||||
throw new RangeError('Parameter limit exceeded. Only ' + limit + ' parameter' + (limit === 1 ? '' : 's') + ' allowed.');
|
|
||||||
}
|
|
||||||
|
|
||||||
var skipIndex = -1; // Keep track of where the utf8 sentinel was found
|
|
||||||
var i;
|
|
||||||
|
|
||||||
var charset = options.charset;
|
|
||||||
if (options.charsetSentinel) {
|
|
||||||
for (i = 0; i < parts.length; ++i) {
|
|
||||||
if (parts[i].indexOf('utf8=') === 0) {
|
|
||||||
if (parts[i] === charsetSentinel) {
|
|
||||||
charset = 'utf-8';
|
|
||||||
} else if (parts[i] === isoSentinel) {
|
|
||||||
charset = 'iso-8859-1';
|
|
||||||
}
|
|
||||||
skipIndex = i;
|
|
||||||
i = parts.length; // The eslint settings do not allow break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < parts.length; ++i) {
|
|
||||||
if (i === skipIndex) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var part = parts[i];
|
|
||||||
|
|
||||||
var bracketEqualsPos = part.indexOf(']=');
|
|
||||||
var pos = bracketEqualsPos === -1 ? part.indexOf('=') : bracketEqualsPos + 1;
|
|
||||||
|
|
||||||
var key;
|
|
||||||
var val;
|
|
||||||
if (pos === -1) {
|
|
||||||
key = options.decoder(part, defaults.decoder, charset, 'key');
|
|
||||||
val = options.strictNullHandling ? null : '';
|
|
||||||
} else {
|
|
||||||
key = options.decoder(part.slice(0, pos), defaults.decoder, charset, 'key');
|
|
||||||
|
|
||||||
if (key !== null) {
|
|
||||||
val = utils.maybeMap(
|
|
||||||
parseArrayValue(
|
|
||||||
part.slice(pos + 1),
|
|
||||||
options,
|
|
||||||
isArray(obj[key]) ? obj[key].length : 0
|
|
||||||
),
|
|
||||||
function (encodedVal) {
|
|
||||||
return options.decoder(encodedVal, defaults.decoder, charset, 'value');
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (val && options.interpretNumericEntities && charset === 'iso-8859-1') {
|
|
||||||
val = interpretNumericEntities(String(val));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (part.indexOf('[]=') > -1) {
|
|
||||||
val = isArray(val) ? [val] : val;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.comma && isArray(val) && val.length > options.arrayLimit) {
|
|
||||||
if (options.throwOnLimitExceeded) {
|
|
||||||
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
|
|
||||||
}
|
|
||||||
val = utils.combine([], val, options.arrayLimit, options.plainObjects);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key !== null) {
|
|
||||||
var existing = has.call(obj, key);
|
|
||||||
if (existing && (options.duplicates === 'combine' || part.indexOf('[]=') > -1)) {
|
|
||||||
obj[key] = utils.combine(
|
|
||||||
obj[key],
|
|
||||||
val,
|
|
||||||
options.arrayLimit,
|
|
||||||
options.plainObjects
|
|
||||||
);
|
|
||||||
} else if (!existing || options.duplicates === 'last') {
|
|
||||||
obj[key] = val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
var parseObject = function (chain, val, options, valuesParsed) {
|
|
||||||
var currentArrayLength = 0;
|
|
||||||
if (chain.length > 0 && chain[chain.length - 1] === '[]') {
|
|
||||||
var parentKey = chain.slice(0, -1).join('');
|
|
||||||
currentArrayLength = Array.isArray(val) && val[parentKey] ? val[parentKey].length : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var leaf = valuesParsed ? val : parseArrayValue(val, options, currentArrayLength);
|
|
||||||
|
|
||||||
for (var i = chain.length - 1; i >= 0; --i) {
|
|
||||||
var obj;
|
|
||||||
var root = chain[i];
|
|
||||||
|
|
||||||
if (root === '[]' && options.parseArrays) {
|
|
||||||
if (utils.isOverflow(leaf)) {
|
|
||||||
// leaf is already an overflow object, preserve it
|
|
||||||
obj = leaf;
|
|
||||||
} else {
|
|
||||||
obj = options.allowEmptyArrays && (leaf === '' || (options.strictNullHandling && leaf === null))
|
|
||||||
? []
|
|
||||||
: utils.combine(
|
|
||||||
[],
|
|
||||||
leaf,
|
|
||||||
options.arrayLimit,
|
|
||||||
options.plainObjects
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
obj = options.plainObjects ? { __proto__: null } : {};
|
|
||||||
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
|
|
||||||
var decodedRoot = options.decodeDotInKeys ? cleanRoot.replace(/%2E/g, '.') : cleanRoot;
|
|
||||||
var index = parseInt(decodedRoot, 10);
|
|
||||||
var isValidArrayIndex = !isNaN(index)
|
|
||||||
&& root !== decodedRoot
|
|
||||||
&& String(index) === decodedRoot
|
|
||||||
&& index >= 0
|
|
||||||
&& options.parseArrays;
|
|
||||||
if (!options.parseArrays && decodedRoot === '') {
|
|
||||||
obj = { 0: leaf };
|
|
||||||
} else if (isValidArrayIndex && index < options.arrayLimit) {
|
|
||||||
obj = [];
|
|
||||||
obj[index] = leaf;
|
|
||||||
} else if (isValidArrayIndex && options.throwOnLimitExceeded) {
|
|
||||||
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
|
|
||||||
} else if (isValidArrayIndex) {
|
|
||||||
obj[index] = leaf;
|
|
||||||
utils.markOverflow(obj, index);
|
|
||||||
} else if (decodedRoot !== '__proto__') {
|
|
||||||
obj[decodedRoot] = leaf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
leaf = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
return leaf;
|
|
||||||
};
|
|
||||||
|
|
||||||
var splitKeyIntoSegments = function splitKeyIntoSegments(givenKey, options) {
|
|
||||||
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey;
|
|
||||||
|
|
||||||
if (options.depth <= 0) {
|
|
||||||
if (!options.plainObjects && has.call(Object.prototype, key)) {
|
|
||||||
if (!options.allowPrototypes) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return [key];
|
|
||||||
}
|
|
||||||
|
|
||||||
var brackets = /(\[[^[\]]*])/;
|
|
||||||
var child = /(\[[^[\]]*])/g;
|
|
||||||
|
|
||||||
var segment = brackets.exec(key);
|
|
||||||
var parent = segment ? key.slice(0, segment.index) : key;
|
|
||||||
|
|
||||||
var keys = [];
|
|
||||||
|
|
||||||
if (parent) {
|
|
||||||
if (!options.plainObjects && has.call(Object.prototype, parent)) {
|
|
||||||
if (!options.allowPrototypes) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keys[keys.length] = parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
var i = 0;
|
|
||||||
while ((segment = child.exec(key)) !== null && i < options.depth) {
|
|
||||||
i += 1;
|
|
||||||
|
|
||||||
var segmentContent = segment[1].slice(1, -1);
|
|
||||||
if (!options.plainObjects && has.call(Object.prototype, segmentContent)) {
|
|
||||||
if (!options.allowPrototypes) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
keys[keys.length] = segment[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (segment) {
|
|
||||||
if (options.strictDepth === true) {
|
|
||||||
throw new RangeError('Input depth exceeded depth option of ' + options.depth + ' and strictDepth is true');
|
|
||||||
}
|
|
||||||
|
|
||||||
keys[keys.length] = '[' + key.slice(segment.index) + ']';
|
|
||||||
}
|
|
||||||
|
|
||||||
return keys;
|
|
||||||
};
|
|
||||||
|
|
||||||
var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
|
|
||||||
if (!givenKey) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = splitKeyIntoSegments(givenKey, options);
|
|
||||||
|
|
||||||
if (!keys) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parseObject(keys, val, options, valuesParsed);
|
|
||||||
};
|
|
||||||
|
|
||||||
var normalizeParseOptions = function normalizeParseOptions(opts) {
|
|
||||||
if (!opts) {
|
|
||||||
return defaults;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
|
|
||||||
throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.decodeDotInKeys !== 'undefined' && typeof opts.decodeDotInKeys !== 'boolean') {
|
|
||||||
throw new TypeError('`decodeDotInKeys` option can only be `true` or `false`, when provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.decoder !== null && typeof opts.decoder !== 'undefined' && typeof opts.decoder !== 'function') {
|
|
||||||
throw new TypeError('Decoder has to be a function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
|
||||||
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.throwOnLimitExceeded !== 'undefined' && typeof opts.throwOnLimitExceeded !== 'boolean') {
|
|
||||||
throw new TypeError('`throwOnLimitExceeded` option must be a boolean');
|
|
||||||
}
|
|
||||||
|
|
||||||
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset;
|
|
||||||
|
|
||||||
var duplicates = typeof opts.duplicates === 'undefined' ? defaults.duplicates : opts.duplicates;
|
|
||||||
|
|
||||||
if (duplicates !== 'combine' && duplicates !== 'first' && duplicates !== 'last') {
|
|
||||||
throw new TypeError('The duplicates option must be either combine, first, or last');
|
|
||||||
}
|
|
||||||
|
|
||||||
var allowDots = typeof opts.allowDots === 'undefined' ? opts.decodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
|
|
||||||
|
|
||||||
return {
|
|
||||||
allowDots: allowDots,
|
|
||||||
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
|
|
||||||
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes,
|
|
||||||
allowSparse: typeof opts.allowSparse === 'boolean' ? opts.allowSparse : defaults.allowSparse,
|
|
||||||
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit,
|
|
||||||
charset: charset,
|
|
||||||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
|
||||||
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma,
|
|
||||||
decodeDotInKeys: typeof opts.decodeDotInKeys === 'boolean' ? opts.decodeDotInKeys : defaults.decodeDotInKeys,
|
|
||||||
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder,
|
|
||||||
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter,
|
|
||||||
// eslint-disable-next-line no-implicit-coercion, no-extra-parens
|
|
||||||
depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth,
|
|
||||||
duplicates: duplicates,
|
|
||||||
ignoreQueryPrefix: opts.ignoreQueryPrefix === true,
|
|
||||||
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities,
|
|
||||||
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit,
|
|
||||||
parseArrays: opts.parseArrays !== false,
|
|
||||||
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects,
|
|
||||||
strictDepth: typeof opts.strictDepth === 'boolean' ? !!opts.strictDepth : defaults.strictDepth,
|
|
||||||
strictMerge: typeof opts.strictMerge === 'boolean' ? !!opts.strictMerge : defaults.strictMerge,
|
|
||||||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
|
|
||||||
throwOnLimitExceeded: typeof opts.throwOnLimitExceeded === 'boolean' ? opts.throwOnLimitExceeded : false
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = function (str, opts) {
|
|
||||||
var options = normalizeParseOptions(opts);
|
|
||||||
|
|
||||||
if (str === '' || str === null || typeof str === 'undefined') {
|
|
||||||
return options.plainObjects ? { __proto__: null } : {};
|
|
||||||
}
|
|
||||||
|
|
||||||
var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
|
|
||||||
var obj = options.plainObjects ? { __proto__: null } : {};
|
|
||||||
|
|
||||||
// Iterate over the keys and setup the new object
|
|
||||||
|
|
||||||
var keys = Object.keys(tempObj);
|
|
||||||
for (var i = 0; i < keys.length; ++i) {
|
|
||||||
var key = keys[i];
|
|
||||||
var newObj = parseKeys(key, tempObj[key], options, typeof str === 'string');
|
|
||||||
obj = utils.merge(obj, newObj, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.allowSparse === true) {
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.compact(obj);
|
|
||||||
};
|
|
||||||
-356
@@ -1,356 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var getSideChannel = require('side-channel');
|
|
||||||
var utils = require('./utils');
|
|
||||||
var formats = require('./formats');
|
|
||||||
var has = Object.prototype.hasOwnProperty;
|
|
||||||
|
|
||||||
var arrayPrefixGenerators = {
|
|
||||||
brackets: function brackets(prefix) {
|
|
||||||
return prefix + '[]';
|
|
||||||
},
|
|
||||||
comma: 'comma',
|
|
||||||
indices: function indices(prefix, key) {
|
|
||||||
return prefix + '[' + key + ']';
|
|
||||||
},
|
|
||||||
repeat: function repeat(prefix) {
|
|
||||||
return prefix;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var isArray = Array.isArray;
|
|
||||||
var push = Array.prototype.push;
|
|
||||||
var pushToArray = function (arr, valueOrArray) {
|
|
||||||
push.apply(arr, isArray(valueOrArray) ? valueOrArray : [valueOrArray]);
|
|
||||||
};
|
|
||||||
|
|
||||||
var toISO = Date.prototype.toISOString;
|
|
||||||
|
|
||||||
var defaultFormat = formats['default'];
|
|
||||||
var defaults = {
|
|
||||||
addQueryPrefix: false,
|
|
||||||
allowDots: false,
|
|
||||||
allowEmptyArrays: false,
|
|
||||||
arrayFormat: 'indices',
|
|
||||||
charset: 'utf-8',
|
|
||||||
charsetSentinel: false,
|
|
||||||
commaRoundTrip: false,
|
|
||||||
delimiter: '&',
|
|
||||||
encode: true,
|
|
||||||
encodeDotInKeys: false,
|
|
||||||
encoder: utils.encode,
|
|
||||||
encodeValuesOnly: false,
|
|
||||||
filter: void undefined,
|
|
||||||
format: defaultFormat,
|
|
||||||
formatter: formats.formatters[defaultFormat],
|
|
||||||
// deprecated
|
|
||||||
indices: false,
|
|
||||||
serializeDate: function serializeDate(date) {
|
|
||||||
return toISO.call(date);
|
|
||||||
},
|
|
||||||
skipNulls: false,
|
|
||||||
strictNullHandling: false
|
|
||||||
};
|
|
||||||
|
|
||||||
var isNonNullishPrimitive = function isNonNullishPrimitive(v) {
|
|
||||||
return typeof v === 'string'
|
|
||||||
|| typeof v === 'number'
|
|
||||||
|| typeof v === 'boolean'
|
|
||||||
|| typeof v === 'symbol'
|
|
||||||
|| typeof v === 'bigint';
|
|
||||||
};
|
|
||||||
|
|
||||||
var sentinel = {};
|
|
||||||
|
|
||||||
var stringify = function stringify(
|
|
||||||
object,
|
|
||||||
prefix,
|
|
||||||
generateArrayPrefix,
|
|
||||||
commaRoundTrip,
|
|
||||||
allowEmptyArrays,
|
|
||||||
strictNullHandling,
|
|
||||||
skipNulls,
|
|
||||||
encodeDotInKeys,
|
|
||||||
encoder,
|
|
||||||
filter,
|
|
||||||
sort,
|
|
||||||
allowDots,
|
|
||||||
serializeDate,
|
|
||||||
format,
|
|
||||||
formatter,
|
|
||||||
encodeValuesOnly,
|
|
||||||
charset,
|
|
||||||
sideChannel
|
|
||||||
) {
|
|
||||||
var obj = object;
|
|
||||||
|
|
||||||
var tmpSc = sideChannel;
|
|
||||||
var step = 0;
|
|
||||||
var findFlag = false;
|
|
||||||
while ((tmpSc = tmpSc.get(sentinel)) !== void undefined && !findFlag) {
|
|
||||||
// Where object last appeared in the ref tree
|
|
||||||
var pos = tmpSc.get(object);
|
|
||||||
step += 1;
|
|
||||||
if (typeof pos !== 'undefined') {
|
|
||||||
if (pos === step) {
|
|
||||||
throw new RangeError('Cyclic object value');
|
|
||||||
} else {
|
|
||||||
findFlag = true; // Break while
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (typeof tmpSc.get(sentinel) === 'undefined') {
|
|
||||||
step = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof filter === 'function') {
|
|
||||||
obj = filter(prefix, obj);
|
|
||||||
} else if (obj instanceof Date) {
|
|
||||||
obj = serializeDate(obj);
|
|
||||||
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
|
||||||
obj = utils.maybeMap(obj, function (value) {
|
|
||||||
if (value instanceof Date) {
|
|
||||||
return serializeDate(value);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (obj === null) {
|
|
||||||
if (strictNullHandling) {
|
|
||||||
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder, charset, 'key', format) : prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
obj = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNonNullishPrimitive(obj) || utils.isBuffer(obj)) {
|
|
||||||
if (encoder) {
|
|
||||||
var keyValue = encodeValuesOnly ? prefix : encoder(prefix, defaults.encoder, charset, 'key', format);
|
|
||||||
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
|
|
||||||
}
|
|
||||||
return [formatter(prefix) + '=' + formatter(String(obj))];
|
|
||||||
}
|
|
||||||
|
|
||||||
var values = [];
|
|
||||||
|
|
||||||
if (typeof obj === 'undefined') {
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
|
|
||||||
var objKeys;
|
|
||||||
if (generateArrayPrefix === 'comma' && isArray(obj)) {
|
|
||||||
// we need to join elements in
|
|
||||||
if (encodeValuesOnly && encoder) {
|
|
||||||
obj = utils.maybeMap(obj, encoder);
|
|
||||||
}
|
|
||||||
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
|
|
||||||
} else if (isArray(filter)) {
|
|
||||||
objKeys = filter;
|
|
||||||
} else {
|
|
||||||
var keys = Object.keys(obj);
|
|
||||||
objKeys = sort ? keys.sort(sort) : keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
var encodedPrefix = encodeDotInKeys ? String(prefix).replace(/\./g, '%2E') : String(prefix);
|
|
||||||
|
|
||||||
var adjustedPrefix = commaRoundTrip && isArray(obj) && obj.length === 1 ? encodedPrefix + '[]' : encodedPrefix;
|
|
||||||
|
|
||||||
if (allowEmptyArrays && isArray(obj) && obj.length === 0) {
|
|
||||||
return adjustedPrefix + '[]';
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var j = 0; j < objKeys.length; ++j) {
|
|
||||||
var key = objKeys[j];
|
|
||||||
var value = typeof key === 'object' && key && typeof key.value !== 'undefined'
|
|
||||||
? key.value
|
|
||||||
: obj[key];
|
|
||||||
|
|
||||||
if (skipNulls && value === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var encodedKey = allowDots && encodeDotInKeys ? String(key).replace(/\./g, '%2E') : String(key);
|
|
||||||
var keyPrefix = isArray(obj)
|
|
||||||
? typeof generateArrayPrefix === 'function' ? generateArrayPrefix(adjustedPrefix, encodedKey) : adjustedPrefix
|
|
||||||
: adjustedPrefix + (allowDots ? '.' + encodedKey : '[' + encodedKey + ']');
|
|
||||||
|
|
||||||
sideChannel.set(object, step);
|
|
||||||
var valueSideChannel = getSideChannel();
|
|
||||||
valueSideChannel.set(sentinel, sideChannel);
|
|
||||||
pushToArray(values, stringify(
|
|
||||||
value,
|
|
||||||
keyPrefix,
|
|
||||||
generateArrayPrefix,
|
|
||||||
commaRoundTrip,
|
|
||||||
allowEmptyArrays,
|
|
||||||
strictNullHandling,
|
|
||||||
skipNulls,
|
|
||||||
encodeDotInKeys,
|
|
||||||
generateArrayPrefix === 'comma' && encodeValuesOnly && isArray(obj) ? null : encoder,
|
|
||||||
filter,
|
|
||||||
sort,
|
|
||||||
allowDots,
|
|
||||||
serializeDate,
|
|
||||||
format,
|
|
||||||
formatter,
|
|
||||||
encodeValuesOnly,
|
|
||||||
charset,
|
|
||||||
valueSideChannel
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
return values;
|
|
||||||
};
|
|
||||||
|
|
||||||
var normalizeStringifyOptions = function normalizeStringifyOptions(opts) {
|
|
||||||
if (!opts) {
|
|
||||||
return defaults;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
|
|
||||||
throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {
|
|
||||||
throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
|
|
||||||
throw new TypeError('Encoder has to be a function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
var charset = opts.charset || defaults.charset;
|
|
||||||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
|
|
||||||
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
|
|
||||||
}
|
|
||||||
|
|
||||||
var format = formats['default'];
|
|
||||||
if (typeof opts.format !== 'undefined') {
|
|
||||||
if (!has.call(formats.formatters, opts.format)) {
|
|
||||||
throw new TypeError('Unknown format option provided.');
|
|
||||||
}
|
|
||||||
format = opts.format;
|
|
||||||
}
|
|
||||||
var formatter = formats.formatters[format];
|
|
||||||
|
|
||||||
var filter = defaults.filter;
|
|
||||||
if (typeof opts.filter === 'function' || isArray(opts.filter)) {
|
|
||||||
filter = opts.filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
var arrayFormat;
|
|
||||||
if (opts.arrayFormat in arrayPrefixGenerators) {
|
|
||||||
arrayFormat = opts.arrayFormat;
|
|
||||||
} else if ('indices' in opts) {
|
|
||||||
arrayFormat = opts.indices ? 'indices' : 'repeat';
|
|
||||||
} else {
|
|
||||||
arrayFormat = defaults.arrayFormat;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
|
|
||||||
throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
|
|
||||||
}
|
|
||||||
|
|
||||||
var allowDots = typeof opts.allowDots === 'undefined' ? opts.encodeDotInKeys === true ? true : defaults.allowDots : !!opts.allowDots;
|
|
||||||
|
|
||||||
return {
|
|
||||||
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
|
|
||||||
allowDots: allowDots,
|
|
||||||
allowEmptyArrays: typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
|
|
||||||
arrayFormat: arrayFormat,
|
|
||||||
charset: charset,
|
|
||||||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
|
|
||||||
commaRoundTrip: !!opts.commaRoundTrip,
|
|
||||||
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
|
|
||||||
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
|
|
||||||
encodeDotInKeys: typeof opts.encodeDotInKeys === 'boolean' ? opts.encodeDotInKeys : defaults.encodeDotInKeys,
|
|
||||||
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
|
|
||||||
encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
|
|
||||||
filter: filter,
|
|
||||||
format: format,
|
|
||||||
formatter: formatter,
|
|
||||||
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
|
|
||||||
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
|
|
||||||
sort: typeof opts.sort === 'function' ? opts.sort : null,
|
|
||||||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = function (object, opts) {
|
|
||||||
var obj = object;
|
|
||||||
var options = normalizeStringifyOptions(opts);
|
|
||||||
|
|
||||||
var objKeys;
|
|
||||||
var filter;
|
|
||||||
|
|
||||||
if (typeof options.filter === 'function') {
|
|
||||||
filter = options.filter;
|
|
||||||
obj = filter('', obj);
|
|
||||||
} else if (isArray(options.filter)) {
|
|
||||||
filter = options.filter;
|
|
||||||
objKeys = filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys = [];
|
|
||||||
|
|
||||||
if (typeof obj !== 'object' || obj === null) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
var generateArrayPrefix = arrayPrefixGenerators[options.arrayFormat];
|
|
||||||
var commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;
|
|
||||||
|
|
||||||
if (!objKeys) {
|
|
||||||
objKeys = Object.keys(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.sort) {
|
|
||||||
objKeys.sort(options.sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
var sideChannel = getSideChannel();
|
|
||||||
for (var i = 0; i < objKeys.length; ++i) {
|
|
||||||
var key = objKeys[i];
|
|
||||||
var value = obj[key];
|
|
||||||
|
|
||||||
if (options.skipNulls && value === null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
pushToArray(keys, stringify(
|
|
||||||
value,
|
|
||||||
key,
|
|
||||||
generateArrayPrefix,
|
|
||||||
commaRoundTrip,
|
|
||||||
options.allowEmptyArrays,
|
|
||||||
options.strictNullHandling,
|
|
||||||
options.skipNulls,
|
|
||||||
options.encodeDotInKeys,
|
|
||||||
options.encode ? options.encoder : null,
|
|
||||||
options.filter,
|
|
||||||
options.sort,
|
|
||||||
options.allowDots,
|
|
||||||
options.serializeDate,
|
|
||||||
options.format,
|
|
||||||
options.formatter,
|
|
||||||
options.encodeValuesOnly,
|
|
||||||
options.charset,
|
|
||||||
sideChannel
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
var joined = keys.join(options.delimiter);
|
|
||||||
var prefix = options.addQueryPrefix === true ? '?' : '';
|
|
||||||
|
|
||||||
if (options.charsetSentinel) {
|
|
||||||
if (options.charset === 'iso-8859-1') {
|
|
||||||
// encodeURIComponent('✓'), the "numeric entity" representation of a checkmark
|
|
||||||
prefix += 'utf8=%26%2310003%3B&';
|
|
||||||
} else {
|
|
||||||
// encodeURIComponent('✓')
|
|
||||||
prefix += 'utf8=%E2%9C%93&';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return joined.length > 0 ? prefix + joined : '';
|
|
||||||
};
|
|
||||||
-342
@@ -1,342 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var formats = require('./formats');
|
|
||||||
var getSideChannel = require('side-channel');
|
|
||||||
|
|
||||||
var has = Object.prototype.hasOwnProperty;
|
|
||||||
var isArray = Array.isArray;
|
|
||||||
|
|
||||||
// Track objects created from arrayLimit overflow using side-channel
|
|
||||||
// Stores the current max numeric index for O(1) lookup
|
|
||||||
var overflowChannel = getSideChannel();
|
|
||||||
|
|
||||||
var markOverflow = function markOverflow(obj, maxIndex) {
|
|
||||||
overflowChannel.set(obj, maxIndex);
|
|
||||||
return obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
var isOverflow = function isOverflow(obj) {
|
|
||||||
return overflowChannel.has(obj);
|
|
||||||
};
|
|
||||||
|
|
||||||
var getMaxIndex = function getMaxIndex(obj) {
|
|
||||||
return overflowChannel.get(obj);
|
|
||||||
};
|
|
||||||
|
|
||||||
var setMaxIndex = function setMaxIndex(obj, maxIndex) {
|
|
||||||
overflowChannel.set(obj, maxIndex);
|
|
||||||
};
|
|
||||||
|
|
||||||
var hexTable = (function () {
|
|
||||||
var array = [];
|
|
||||||
for (var i = 0; i < 256; ++i) {
|
|
||||||
array[array.length] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
return array;
|
|
||||||
}());
|
|
||||||
|
|
||||||
var compactQueue = function compactQueue(queue) {
|
|
||||||
while (queue.length > 1) {
|
|
||||||
var item = queue.pop();
|
|
||||||
var obj = item.obj[item.prop];
|
|
||||||
|
|
||||||
if (isArray(obj)) {
|
|
||||||
var compacted = [];
|
|
||||||
|
|
||||||
for (var j = 0; j < obj.length; ++j) {
|
|
||||||
if (typeof obj[j] !== 'undefined') {
|
|
||||||
compacted[compacted.length] = obj[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
item.obj[item.prop] = compacted;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var arrayToObject = function arrayToObject(source, options) {
|
|
||||||
var obj = options && options.plainObjects ? { __proto__: null } : {};
|
|
||||||
for (var i = 0; i < source.length; ++i) {
|
|
||||||
if (typeof source[i] !== 'undefined') {
|
|
||||||
obj[i] = source[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj;
|
|
||||||
};
|
|
||||||
|
|
||||||
var merge = function merge(target, source, options) {
|
|
||||||
/* eslint no-param-reassign: 0 */
|
|
||||||
if (!source) {
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof source !== 'object' && typeof source !== 'function') {
|
|
||||||
if (isArray(target)) {
|
|
||||||
var nextIndex = target.length;
|
|
||||||
if (options && typeof options.arrayLimit === 'number' && nextIndex > options.arrayLimit) {
|
|
||||||
return markOverflow(arrayToObject(target.concat(source), options), nextIndex);
|
|
||||||
}
|
|
||||||
target[nextIndex] = source;
|
|
||||||
} else if (target && typeof target === 'object') {
|
|
||||||
if (isOverflow(target)) {
|
|
||||||
// Add at next numeric index for overflow objects
|
|
||||||
var newIndex = getMaxIndex(target) + 1;
|
|
||||||
target[newIndex] = source;
|
|
||||||
setMaxIndex(target, newIndex);
|
|
||||||
} else if (options && options.strictMerge) {
|
|
||||||
return [target, source];
|
|
||||||
} else if (
|
|
||||||
(options && (options.plainObjects || options.allowPrototypes))
|
|
||||||
|| !has.call(Object.prototype, source)
|
|
||||||
) {
|
|
||||||
target[source] = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return [target, source];
|
|
||||||
}
|
|
||||||
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!target || typeof target !== 'object') {
|
|
||||||
if (isOverflow(source)) {
|
|
||||||
// Create new object with target at 0, source values shifted by 1
|
|
||||||
var sourceKeys = Object.keys(source);
|
|
||||||
var result = options && options.plainObjects
|
|
||||||
? { __proto__: null, 0: target }
|
|
||||||
: { 0: target };
|
|
||||||
for (var m = 0; m < sourceKeys.length; m++) {
|
|
||||||
var oldKey = parseInt(sourceKeys[m], 10);
|
|
||||||
result[oldKey + 1] = source[sourceKeys[m]];
|
|
||||||
}
|
|
||||||
return markOverflow(result, getMaxIndex(source) + 1);
|
|
||||||
}
|
|
||||||
var combined = [target].concat(source);
|
|
||||||
if (options && typeof options.arrayLimit === 'number' && combined.length > options.arrayLimit) {
|
|
||||||
return markOverflow(arrayToObject(combined, options), combined.length - 1);
|
|
||||||
}
|
|
||||||
return combined;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mergeTarget = target;
|
|
||||||
if (isArray(target) && !isArray(source)) {
|
|
||||||
mergeTarget = arrayToObject(target, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isArray(target) && isArray(source)) {
|
|
||||||
source.forEach(function (item, i) {
|
|
||||||
if (has.call(target, i)) {
|
|
||||||
var targetItem = target[i];
|
|
||||||
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
|
|
||||||
target[i] = merge(targetItem, item, options);
|
|
||||||
} else {
|
|
||||||
target[target.length] = item;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
target[i] = item;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object.keys(source).reduce(function (acc, key) {
|
|
||||||
var value = source[key];
|
|
||||||
|
|
||||||
if (has.call(acc, key)) {
|
|
||||||
acc[key] = merge(acc[key], value, options);
|
|
||||||
} else {
|
|
||||||
acc[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isOverflow(source) && !isOverflow(acc)) {
|
|
||||||
markOverflow(acc, getMaxIndex(source));
|
|
||||||
}
|
|
||||||
if (isOverflow(acc)) {
|
|
||||||
var keyNum = parseInt(key, 10);
|
|
||||||
if (String(keyNum) === key && keyNum >= 0 && keyNum > getMaxIndex(acc)) {
|
|
||||||
setMaxIndex(acc, keyNum);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
|
||||||
}, mergeTarget);
|
|
||||||
};
|
|
||||||
|
|
||||||
var assign = function assignSingleSource(target, source) {
|
|
||||||
return Object.keys(source).reduce(function (acc, key) {
|
|
||||||
acc[key] = source[key];
|
|
||||||
return acc;
|
|
||||||
}, target);
|
|
||||||
};
|
|
||||||
|
|
||||||
var decode = function (str, defaultDecoder, charset) {
|
|
||||||
var strWithoutPlus = str.replace(/\+/g, ' ');
|
|
||||||
if (charset === 'iso-8859-1') {
|
|
||||||
// unescape never throws, no try...catch needed:
|
|
||||||
return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
|
|
||||||
}
|
|
||||||
// utf-8
|
|
||||||
try {
|
|
||||||
return decodeURIComponent(strWithoutPlus);
|
|
||||||
} catch (e) {
|
|
||||||
return strWithoutPlus;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var limit = 1024;
|
|
||||||
|
|
||||||
/* eslint operator-linebreak: [2, "before"] */
|
|
||||||
|
|
||||||
var encode = function encode(str, defaultEncoder, charset, kind, format) {
|
|
||||||
// This code was originally written by Brian White (mscdex) for the io.js core querystring library.
|
|
||||||
// It has been adapted here for stricter adherence to RFC 3986
|
|
||||||
if (str.length === 0) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
var string = str;
|
|
||||||
if (typeof str === 'symbol') {
|
|
||||||
string = Symbol.prototype.toString.call(str);
|
|
||||||
} else if (typeof str !== 'string') {
|
|
||||||
string = String(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (charset === 'iso-8859-1') {
|
|
||||||
return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
|
|
||||||
return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
var out = '';
|
|
||||||
for (var j = 0; j < string.length; j += limit) {
|
|
||||||
var segment = string.length >= limit ? string.slice(j, j + limit) : string;
|
|
||||||
var arr = [];
|
|
||||||
|
|
||||||
for (var i = 0; i < segment.length; ++i) {
|
|
||||||
var c = segment.charCodeAt(i);
|
|
||||||
if (
|
|
||||||
c === 0x2D // -
|
|
||||||
|| c === 0x2E // .
|
|
||||||
|| c === 0x5F // _
|
|
||||||
|| c === 0x7E // ~
|
|
||||||
|| (c >= 0x30 && c <= 0x39) // 0-9
|
|
||||||
|| (c >= 0x41 && c <= 0x5A) // a-z
|
|
||||||
|| (c >= 0x61 && c <= 0x7A) // A-Z
|
|
||||||
|| (format === formats.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
|
|
||||||
) {
|
|
||||||
arr[arr.length] = segment.charAt(i);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c < 0x80) {
|
|
||||||
arr[arr.length] = hexTable[c];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c < 0x800) {
|
|
||||||
arr[arr.length] = hexTable[0xC0 | (c >> 6)]
|
|
||||||
+ hexTable[0x80 | (c & 0x3F)];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c < 0xD800 || c >= 0xE000) {
|
|
||||||
arr[arr.length] = hexTable[0xE0 | (c >> 12)]
|
|
||||||
+ hexTable[0x80 | ((c >> 6) & 0x3F)]
|
|
||||||
+ hexTable[0x80 | (c & 0x3F)];
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
i += 1;
|
|
||||||
c = 0x10000 + (((c & 0x3FF) << 10) | (segment.charCodeAt(i) & 0x3FF));
|
|
||||||
|
|
||||||
arr[arr.length] = hexTable[0xF0 | (c >> 18)]
|
|
||||||
+ hexTable[0x80 | ((c >> 12) & 0x3F)]
|
|
||||||
+ hexTable[0x80 | ((c >> 6) & 0x3F)]
|
|
||||||
+ hexTable[0x80 | (c & 0x3F)];
|
|
||||||
}
|
|
||||||
|
|
||||||
out += arr.join('');
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
};
|
|
||||||
|
|
||||||
var compact = function compact(value) {
|
|
||||||
var queue = [{ obj: { o: value }, prop: 'o' }];
|
|
||||||
var refs = [];
|
|
||||||
|
|
||||||
for (var i = 0; i < queue.length; ++i) {
|
|
||||||
var item = queue[i];
|
|
||||||
var obj = item.obj[item.prop];
|
|
||||||
|
|
||||||
var keys = Object.keys(obj);
|
|
||||||
for (var j = 0; j < keys.length; ++j) {
|
|
||||||
var key = keys[j];
|
|
||||||
var val = obj[key];
|
|
||||||
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
|
|
||||||
queue[queue.length] = { obj: obj, prop: key };
|
|
||||||
refs[refs.length] = val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compactQueue(queue);
|
|
||||||
|
|
||||||
return value;
|
|
||||||
};
|
|
||||||
|
|
||||||
var isRegExp = function isRegExp(obj) {
|
|
||||||
return Object.prototype.toString.call(obj) === '[object RegExp]';
|
|
||||||
};
|
|
||||||
|
|
||||||
var isBuffer = function isBuffer(obj) {
|
|
||||||
if (!obj || typeof obj !== 'object') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
|
|
||||||
};
|
|
||||||
|
|
||||||
var combine = function combine(a, b, arrayLimit, plainObjects) {
|
|
||||||
// If 'a' is already an overflow object, add to it
|
|
||||||
if (isOverflow(a)) {
|
|
||||||
var newIndex = getMaxIndex(a) + 1;
|
|
||||||
a[newIndex] = b;
|
|
||||||
setMaxIndex(a, newIndex);
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = [].concat(a, b);
|
|
||||||
if (result.length > arrayLimit) {
|
|
||||||
return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
var maybeMap = function maybeMap(val, fn) {
|
|
||||||
if (isArray(val)) {
|
|
||||||
var mapped = [];
|
|
||||||
for (var i = 0; i < val.length; i += 1) {
|
|
||||||
mapped[mapped.length] = fn(val[i]);
|
|
||||||
}
|
|
||||||
return mapped;
|
|
||||||
}
|
|
||||||
return fn(val);
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
arrayToObject: arrayToObject,
|
|
||||||
assign: assign,
|
|
||||||
combine: combine,
|
|
||||||
compact: compact,
|
|
||||||
decode: decode,
|
|
||||||
encode: encode,
|
|
||||||
isBuffer: isBuffer,
|
|
||||||
isOverflow: isOverflow,
|
|
||||||
isRegExp: isRegExp,
|
|
||||||
markOverflow: markOverflow,
|
|
||||||
maybeMap: maybeMap,
|
|
||||||
merge: merge
|
|
||||||
};
|
|
||||||
-94
@@ -1,94 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "qs",
|
|
||||||
"description": "A querystring parser that supports nesting and arrays, with a depth limit",
|
|
||||||
"homepage": "https://github.com/ljharb/qs",
|
|
||||||
"version": "6.15.1",
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "https://github.com/ljharb/qs.git"
|
|
||||||
},
|
|
||||||
"funding": {
|
|
||||||
"url": "https://github.com/sponsors/ljharb"
|
|
||||||
},
|
|
||||||
"main": "lib/index.js",
|
|
||||||
"sideEffects": false,
|
|
||||||
"contributors": [
|
|
||||||
{
|
|
||||||
"name": "Jordan Harband",
|
|
||||||
"email": "ljharb@gmail.com",
|
|
||||||
"url": "http://ljharb.codes"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"keywords": [
|
|
||||||
"querystring",
|
|
||||||
"qs",
|
|
||||||
"query",
|
|
||||||
"url",
|
|
||||||
"parse",
|
|
||||||
"stringify"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.6"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"side-channel": "^1.1.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"@browserify/envify": "^6.0.0",
|
|
||||||
"@browserify/uglifyify": "^6.0.0",
|
|
||||||
"@ljharb/eslint-config": "^22.2.2",
|
|
||||||
"browserify": "^16.5.2",
|
|
||||||
"bundle-collapser": "^1.4.0",
|
|
||||||
"common-shakeify": "~1.0.0",
|
|
||||||
"eclint": "^2.8.1",
|
|
||||||
"es-value-fixtures": "^1.7.1",
|
|
||||||
"eslint": "^9.39.2",
|
|
||||||
"evalmd": "^0.0.19",
|
|
||||||
"for-each": "^0.3.5",
|
|
||||||
"glob": "=10.3.7",
|
|
||||||
"has-bigints": "^1.1.0",
|
|
||||||
"has-override-mistake": "^1.0.1",
|
|
||||||
"has-property-descriptors": "^1.0.2",
|
|
||||||
"has-proto": "^1.2.0",
|
|
||||||
"has-symbols": "^1.1.0",
|
|
||||||
"iconv-lite": "^0.5.2",
|
|
||||||
"in-publish": "^2.0.1",
|
|
||||||
"jackspeak": "=2.1.1",
|
|
||||||
"jiti": "^0.0.0",
|
|
||||||
"mkdirp": "^0.5.5",
|
|
||||||
"mock-property": "^1.1.0",
|
|
||||||
"module-deps": "^6.2.3",
|
|
||||||
"npmignore": "^0.3.5",
|
|
||||||
"nyc": "^10.3.2",
|
|
||||||
"object-inspect": "^1.13.4",
|
|
||||||
"qs-iconv": "^1.0.4",
|
|
||||||
"safe-publish-latest": "^2.0.0",
|
|
||||||
"safer-buffer": "^2.1.2",
|
|
||||||
"tape": "^5.9.0",
|
|
||||||
"unassertify": "^3.0.1"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"prepack": "npmignore --auto --commentLines=autogenerated && npm run dist",
|
|
||||||
"prepublishOnly": "safe-publish-latest",
|
|
||||||
"prepublish": "not-in-publish || npm run prepublishOnly",
|
|
||||||
"pretest": "npm run --silent readme && npm run --silent lint",
|
|
||||||
"test": "npm run tests-only",
|
|
||||||
"tests-only": "nyc tape 'test/**/*.js'",
|
|
||||||
"posttest": "npx npm@'>=10.2' audit --production",
|
|
||||||
"readme": "evalmd README.md",
|
|
||||||
"postlint": "eclint check $(git ls-files | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git' | grep -v dist/)",
|
|
||||||
"lint": "eslint .",
|
|
||||||
"dist": "mkdirp dist && browserify --standalone Qs -g unassertify -g @browserify/envify -g [@browserify/uglifyify --mangle.keep_fnames --compress.keep_fnames --format.indent_level=1 --compress.arrows=false --compress.passes=4 --compress.typeofs=false] -p common-shakeify -p bundle-collapser/plugin lib/index.js > dist/qs.js"
|
|
||||||
},
|
|
||||||
"license": "BSD-3-Clause",
|
|
||||||
"publishConfig": {
|
|
||||||
"ignore": [
|
|
||||||
"!dist/*",
|
|
||||||
"bower.json",
|
|
||||||
"component.json",
|
|
||||||
".github/workflows",
|
|
||||||
"logos",
|
|
||||||
"tea.yaml"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-267
@@ -1,267 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
emptyTestCases: [
|
|
||||||
{
|
|
||||||
input: '&',
|
|
||||||
withEmptyKeys: {},
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '',
|
|
||||||
indices: '',
|
|
||||||
repeat: ''
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '&&',
|
|
||||||
withEmptyKeys: {},
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '',
|
|
||||||
indices: '',
|
|
||||||
repeat: ''
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '&=',
|
|
||||||
withEmptyKeys: { '': '' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=',
|
|
||||||
indices: '=',
|
|
||||||
repeat: '='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '&=&',
|
|
||||||
withEmptyKeys: { '': '' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=',
|
|
||||||
indices: '=',
|
|
||||||
repeat: '='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '&=&=',
|
|
||||||
withEmptyKeys: { '': ['', ''] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=&[]=',
|
|
||||||
indices: '[0]=&[1]=',
|
|
||||||
repeat: '=&='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '&=&=&',
|
|
||||||
withEmptyKeys: { '': ['', ''] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=&[]=',
|
|
||||||
indices: '[0]=&[1]=',
|
|
||||||
repeat: '=&='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=',
|
|
||||||
withEmptyKeys: { '': '' },
|
|
||||||
noEmptyKeys: {},
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=',
|
|
||||||
indices: '=',
|
|
||||||
repeat: '='
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&',
|
|
||||||
withEmptyKeys: { '': '' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=',
|
|
||||||
indices: '=',
|
|
||||||
repeat: '='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&&&',
|
|
||||||
withEmptyKeys: { '': '' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=',
|
|
||||||
indices: '=',
|
|
||||||
repeat: '='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&=&=&',
|
|
||||||
withEmptyKeys: { '': ['', '', ''] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=&[]=&[]=',
|
|
||||||
indices: '[0]=&[1]=&[2]=',
|
|
||||||
repeat: '=&=&='
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&a[]=b&a[1]=c',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=a',
|
|
||||||
withEmptyKeys: { '': 'a' },
|
|
||||||
noEmptyKeys: {},
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=a',
|
|
||||||
indices: '=a',
|
|
||||||
repeat: '=a'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a==a',
|
|
||||||
withEmptyKeys: { a: '=a' },
|
|
||||||
noEmptyKeys: { a: '=a' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: 'a==a',
|
|
||||||
indices: 'a==a',
|
|
||||||
repeat: 'a==a'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&a[]=b',
|
|
||||||
withEmptyKeys: { '': '', a: ['b'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b',
|
|
||||||
indices: '=&a[0]=b',
|
|
||||||
repeat: '=&a=b'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=&a[]=b&a[]=c&a[2]=d',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c', 'd'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c&a[]=d',
|
|
||||||
indices: '=&a[0]=b&a[1]=c&a[2]=d',
|
|
||||||
repeat: '=&a=b&a=c&a=d'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c', 'd'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=a&=b',
|
|
||||||
withEmptyKeys: { '': ['a', 'b'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=a&[]=b',
|
|
||||||
indices: '[0]=a&[1]=b',
|
|
||||||
repeat: '=a&=b'
|
|
||||||
},
|
|
||||||
noEmptyKeys: {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '=a&foo=b',
|
|
||||||
withEmptyKeys: { '': 'a', foo: 'b' },
|
|
||||||
noEmptyKeys: { foo: 'b' },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=a&foo=b',
|
|
||||||
indices: '=a&foo=b',
|
|
||||||
repeat: '=a&foo=b'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a[]=b&a=c&=',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a[]=b&a=c&=',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a[0]=b&a=c&=',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a=b&a[]=c&=',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: 'a=b&a[0]=c&=',
|
|
||||||
withEmptyKeys: { '': '', a: ['b', 'c'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '=&a[]=b&a[]=c',
|
|
||||||
indices: '=&a[0]=b&a[1]=c',
|
|
||||||
repeat: '=&a=b&a=c'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { a: ['b', 'c'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '[]=a&[]=b& []=1',
|
|
||||||
withEmptyKeys: { '': ['a', 'b'], ' ': ['1'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=a&[]=b& []=1',
|
|
||||||
indices: '[0]=a&[1]=b& [0]=1',
|
|
||||||
repeat: '=a&=b& =1'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { 0: 'a', 1: 'b', ' ': ['1'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '[0]=a&[1]=b&a[0]=1&a[1]=2',
|
|
||||||
withEmptyKeys: { '': ['a', 'b'], a: ['1', '2'] },
|
|
||||||
noEmptyKeys: { 0: 'a', 1: 'b', a: ['1', '2'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=a&[]=b&a[]=1&a[]=2',
|
|
||||||
indices: '[0]=a&[1]=b&a[0]=1&a[1]=2',
|
|
||||||
repeat: '=a&=b&a=1&a=2'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '[deep]=a&[deep]=2',
|
|
||||||
withEmptyKeys: { '': { deep: ['a', '2'] }
|
|
||||||
},
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[deep][]=a&[deep][]=2',
|
|
||||||
indices: '[deep][0]=a&[deep][1]=2',
|
|
||||||
repeat: '[deep]=a&[deep]=2'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { deep: ['a', '2'] }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: '%5B0%5D=a&%5B1%5D=b',
|
|
||||||
withEmptyKeys: { '': ['a', 'b'] },
|
|
||||||
stringifyOutput: {
|
|
||||||
brackets: '[]=a&[]=b',
|
|
||||||
indices: '[0]=a&[1]=b',
|
|
||||||
repeat: '=a&=b'
|
|
||||||
},
|
|
||||||
noEmptyKeys: { 0: 'a', 1: 'b' }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
};
|
|
||||||
-1654
File diff suppressed because it is too large
Load Diff
-1319
File diff suppressed because it is too large
Load Diff
-432
@@ -1,432 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
var test = require('tape');
|
|
||||||
var inspect = require('object-inspect');
|
|
||||||
var SaferBuffer = require('safer-buffer').Buffer;
|
|
||||||
var forEach = require('for-each');
|
|
||||||
var v = require('es-value-fixtures');
|
|
||||||
|
|
||||||
var utils = require('../lib/utils');
|
|
||||||
|
|
||||||
test('merge()', function (t) {
|
|
||||||
t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
|
|
||||||
|
|
||||||
t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
|
|
||||||
|
|
||||||
t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
|
|
||||||
|
|
||||||
var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
|
|
||||||
t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
|
|
||||||
|
|
||||||
var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
|
|
||||||
t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
|
|
||||||
|
|
||||||
var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
|
|
||||||
t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
|
|
||||||
|
|
||||||
var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
|
|
||||||
t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
|
|
||||||
|
|
||||||
var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
|
|
||||||
t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
|
|
||||||
|
|
||||||
var func = function f() {};
|
|
||||||
func();
|
|
||||||
t.deepEqual(
|
|
||||||
utils.merge(func, { foo: 'bar' }),
|
|
||||||
[func, { foo: 'bar' }],
|
|
||||||
'functions can not be merged into'
|
|
||||||
);
|
|
||||||
|
|
||||||
func.bar = 'baz';
|
|
||||||
t.deepEqual(
|
|
||||||
utils.merge({ foo: 'bar' }, func),
|
|
||||||
{ foo: 'bar', bar: 'baz' },
|
|
||||||
'functions can be merge sources'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.test(
|
|
||||||
'avoids invoking array setters unnecessarily',
|
|
||||||
{ skip: typeof Object.defineProperty !== 'function' },
|
|
||||||
function (st) {
|
|
||||||
var setCount = 0;
|
|
||||||
var getCount = 0;
|
|
||||||
var observed = [];
|
|
||||||
Object.defineProperty(observed, 0, {
|
|
||||||
get: function () {
|
|
||||||
getCount += 1;
|
|
||||||
return { bar: 'baz' };
|
|
||||||
},
|
|
||||||
set: function () { setCount += 1; }
|
|
||||||
});
|
|
||||||
utils.merge(observed, [null]);
|
|
||||||
st.equal(setCount, 0);
|
|
||||||
st.equal(getCount, 1);
|
|
||||||
observed[0] = observed[0]; // eslint-disable-line no-self-assign
|
|
||||||
st.equal(setCount, 1);
|
|
||||||
st.equal(getCount, 2);
|
|
||||||
st.end();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
t.test('with overflow objects (from arrayLimit)', function (st) {
|
|
||||||
// arrayLimit is max index, so with limit 0, max index 0 is allowed (1 element)
|
|
||||||
// To create overflow, need 2+ elements with limit 0, or 3+ with limit 1, etc.
|
|
||||||
st.test('merges primitive into overflow object at next index', function (s2t) {
|
|
||||||
// Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
|
|
||||||
var overflow = utils.combine(['a', 'b'], 'c', 0, false);
|
|
||||||
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
||||||
var merged = utils.merge(overflow, 'd');
|
|
||||||
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds primitive at next numeric index');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges primitive into regular object with numeric keys normally', function (s2t) {
|
|
||||||
var obj = { 0: 'a', 1: 'b' };
|
|
||||||
s2t.notOk(utils.isOverflow(obj), 'plain object is not marked as overflow');
|
|
||||||
var merged = utils.merge(obj, 'c');
|
|
||||||
s2t.deepEqual(merged, { 0: 'a', 1: 'b', c: true }, 'adds primitive as key (not at next index)');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges primitive into object with non-numeric keys normally', function (s2t) {
|
|
||||||
var obj = { foo: 'bar' };
|
|
||||||
var merged = utils.merge(obj, 'baz');
|
|
||||||
s2t.deepEqual(merged, { foo: 'bar', baz: true }, 'adds primitive as key with value true');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('with strictMerge, wraps object and primitive in array', function (s2t) {
|
|
||||||
var obj = { foo: 'bar' };
|
|
||||||
var merged = utils.merge(obj, 'baz', { strictMerge: true });
|
|
||||||
s2t.deepEqual(merged, [{ foo: 'bar' }, 'baz'], 'wraps in array with strictMerge');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges overflow object into primitive', function (s2t) {
|
|
||||||
// Create an overflow object via combine: 2 elements (indices 0-1) with limit 0
|
|
||||||
var overflow = utils.combine(['a'], 'b', 0, false);
|
|
||||||
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
||||||
var merged = utils.merge('c', overflow);
|
|
||||||
s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
|
|
||||||
s2t.deepEqual(merged, { 0: 'c', 1: 'a', 2: 'b' }, 'creates object with primitive at 0, source values shifted');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges overflow object into primitive with plainObjects', function (s2t) {
|
|
||||||
var overflow = utils.combine(['a'], 'b', 0, false);
|
|
||||||
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
||||||
var merged = utils.merge('c', overflow, { plainObjects: true });
|
|
||||||
s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');
|
|
||||||
s2t.deepEqual(merged, { __proto__: null, 0: 'c', 1: 'a', 2: 'b' }, 'creates null-proto object with primitive at 0');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges overflow object with multiple values into primitive', function (s2t) {
|
|
||||||
// Create an overflow object via combine: 3 elements (indices 0-2) with limit 0
|
|
||||||
var overflow = utils.combine(['b', 'c'], 'd', 0, false);
|
|
||||||
s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');
|
|
||||||
var merged = utils.merge('a', overflow);
|
|
||||||
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'shifts all source indices by 1');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges regular object into primitive as array', function (s2t) {
|
|
||||||
var obj = { foo: 'bar' };
|
|
||||||
var merged = utils.merge('a', obj);
|
|
||||||
s2t.deepEqual(merged, ['a', { foo: 'bar' }], 'creates array with primitive and object');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges primitive into array that exceeds arrayLimit', function (s2t) {
|
|
||||||
var arr = ['a', 'b', 'c'];
|
|
||||||
var merged = utils.merge(arr, 'd', { arrayLimit: 1 });
|
|
||||||
s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
|
|
||||||
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to overflow object with primitive appended');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('merges array into primitive that exceeds arrayLimit', function (s2t) {
|
|
||||||
var merged = utils.merge('a', ['b', 'c'], { arrayLimit: 1 });
|
|
||||||
s2t.ok(utils.isOverflow(merged), 'result is marked as overflow');
|
|
||||||
s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'converts to overflow object');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('assign()', function (t) {
|
|
||||||
var target = { a: 1, b: 2 };
|
|
||||||
var source = { b: 3, c: 4 };
|
|
||||||
var result = utils.assign(target, source);
|
|
||||||
|
|
||||||
t.equal(result, target, 'returns the target');
|
|
||||||
t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
|
|
||||||
t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('combine()', function (t) {
|
|
||||||
t.test('both arrays', function (st) {
|
|
||||||
var a = [1];
|
|
||||||
var b = [2];
|
|
||||||
var combined = utils.combine(a, b);
|
|
||||||
|
|
||||||
st.deepEqual(a, [1], 'a is not mutated');
|
|
||||||
st.deepEqual(b, [2], 'b is not mutated');
|
|
||||||
st.notEqual(a, combined, 'a !== combined');
|
|
||||||
st.notEqual(b, combined, 'b !== combined');
|
|
||||||
st.deepEqual(combined, [1, 2], 'combined is a + b');
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.test('one array, one non-array', function (st) {
|
|
||||||
var aN = 1;
|
|
||||||
var a = [aN];
|
|
||||||
var bN = 2;
|
|
||||||
var b = [bN];
|
|
||||||
|
|
||||||
var combinedAnB = utils.combine(aN, b);
|
|
||||||
st.deepEqual(b, [bN], 'b is not mutated');
|
|
||||||
st.notEqual(aN, combinedAnB, 'aN + b !== aN');
|
|
||||||
st.notEqual(a, combinedAnB, 'aN + b !== a');
|
|
||||||
st.notEqual(bN, combinedAnB, 'aN + b !== bN');
|
|
||||||
st.notEqual(b, combinedAnB, 'aN + b !== b');
|
|
||||||
st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
|
|
||||||
|
|
||||||
var combinedABn = utils.combine(a, bN);
|
|
||||||
st.deepEqual(a, [aN], 'a is not mutated');
|
|
||||||
st.notEqual(aN, combinedABn, 'a + bN !== aN');
|
|
||||||
st.notEqual(a, combinedABn, 'a + bN !== a');
|
|
||||||
st.notEqual(bN, combinedABn, 'a + bN !== bN');
|
|
||||||
st.notEqual(b, combinedABn, 'a + bN !== b');
|
|
||||||
st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.test('neither is an array', function (st) {
|
|
||||||
var combined = utils.combine(1, 2);
|
|
||||||
st.notEqual(1, combined, '1 + 2 !== 1');
|
|
||||||
st.notEqual(2, combined, '1 + 2 !== 2');
|
|
||||||
st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.test('with arrayLimit', function (st) {
|
|
||||||
st.test('under the limit', function (s2t) {
|
|
||||||
var combined = utils.combine(['a', 'b'], 'c', 10, false);
|
|
||||||
s2t.deepEqual(combined, ['a', 'b', 'c'], 'returns array when under limit');
|
|
||||||
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('exactly at the limit stays as array', function (s2t) {
|
|
||||||
var combined = utils.combine(['a', 'b'], 'c', 3, false);
|
|
||||||
s2t.deepEqual(combined, ['a', 'b', 'c'], 'stays as array when count equals limit');
|
|
||||||
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('over the limit', function (s2t) {
|
|
||||||
var combined = utils.combine(['a', 'b', 'c'], 'd', 3, false);
|
|
||||||
s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to object when over limit');
|
|
||||||
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('with arrayLimit 1', function (s2t) {
|
|
||||||
var combined = utils.combine([], 'a', 1, false);
|
|
||||||
s2t.deepEqual(combined, ['a'], 'stays as array when count equals limit');
|
|
||||||
s2t.ok(Array.isArray(combined), 'result is an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('with arrayLimit 0 converts single element to object', function (s2t) {
|
|
||||||
var combined = utils.combine([], 'a', 0, false);
|
|
||||||
s2t.deepEqual(combined, { 0: 'a' }, 'converts to object when count exceeds limit');
|
|
||||||
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('with arrayLimit 0 and two elements converts to object', function (s2t) {
|
|
||||||
var combined = utils.combine(['a'], 'b', 0, false);
|
|
||||||
s2t.deepEqual(combined, { 0: 'a', 1: 'b' }, 'converts to object when count exceeds limit');
|
|
||||||
s2t.notOk(Array.isArray(combined), 'result is not an array');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('with plainObjects option', function (s2t) {
|
|
||||||
var combined = utils.combine(['a', 'b'], 'c', 1, true);
|
|
||||||
var expected = { __proto__: null, 0: 'a', 1: 'b', 2: 'c' };
|
|
||||||
s2t.deepEqual(combined, expected, 'converts to object with null prototype');
|
|
||||||
s2t.equal(Object.getPrototypeOf(combined), null, 'result has null prototype when plainObjects is true');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.test('with existing overflow object', function (st) {
|
|
||||||
st.test('adds to existing overflow object at next index', function (s2t) {
|
|
||||||
// Create overflow object first via combine: 3 elements (indices 0-2) with limit 0
|
|
||||||
var overflow = utils.combine(['a', 'b'], 'c', 0, false);
|
|
||||||
s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');
|
|
||||||
|
|
||||||
var combined = utils.combine(overflow, 'd', 10, false);
|
|
||||||
s2t.equal(combined, overflow, 'returns the same object (mutated)');
|
|
||||||
s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'adds value at next numeric index');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.test('does not treat plain object with numeric keys as overflow', function (s2t) {
|
|
||||||
var plainObj = { 0: 'a', 1: 'b' };
|
|
||||||
s2t.notOk(utils.isOverflow(plainObj), 'plain object is not marked as overflow');
|
|
||||||
|
|
||||||
// combine treats this as a regular value, not an overflow object to append to
|
|
||||||
var combined = utils.combine(plainObj, 'c', 10, false);
|
|
||||||
s2t.deepEqual(combined, [{ 0: 'a', 1: 'b' }, 'c'], 'concatenates as regular values');
|
|
||||||
s2t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('decode', function (t) {
|
|
||||||
t.equal(
|
|
||||||
utils.decode('a+b'),
|
|
||||||
'a b',
|
|
||||||
'decodes + to space'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.decode('name%2Eobj'),
|
|
||||||
'name.obj',
|
|
||||||
'decodes a string'
|
|
||||||
);
|
|
||||||
t.equal(
|
|
||||||
utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),
|
|
||||||
'name.obj.foo',
|
|
||||||
'decodes a string in iso-8859-1'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('encode', function (t) {
|
|
||||||
forEach(v.nullPrimitives, function (nullish) {
|
|
||||||
t['throws'](
|
|
||||||
function () { utils.encode(nullish); },
|
|
||||||
TypeError,
|
|
||||||
inspect(nullish) + ' is not a string'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
t.equal(utils.encode(''), '', 'empty string returns itself');
|
|
||||||
t.deepEqual(utils.encode([]), [], 'empty array returns itself');
|
|
||||||
t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');
|
|
||||||
|
|
||||||
t.test('symbols', { skip: !v.hasSymbols }, function (st) {
|
|
||||||
st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');
|
|
||||||
|
|
||||||
st.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode('(abc)'),
|
|
||||||
'%28abc%29',
|
|
||||||
'encodes parentheses'
|
|
||||||
);
|
|
||||||
t.equal(
|
|
||||||
utils.encode({ toString: function () { return '(abc)'; } }),
|
|
||||||
'%28abc%29',
|
|
||||||
'toStrings and encodes parentheses'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode('abc 123 💩', null, 'iso-8859-1'),
|
|
||||||
'abc%20123%20%26%2355357%3B%26%2356489%3B',
|
|
||||||
'encodes in iso-8859-1'
|
|
||||||
);
|
|
||||||
|
|
||||||
var longString = '';
|
|
||||||
var expectedString = '';
|
|
||||||
for (var i = 0; i < 1500; i++) {
|
|
||||||
longString += ' ';
|
|
||||||
expectedString += '%20';
|
|
||||||
}
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode(longString),
|
|
||||||
expectedString,
|
|
||||||
'encodes a long string'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode('\x28\x29'),
|
|
||||||
'%28%29',
|
|
||||||
'encodes parens normally'
|
|
||||||
);
|
|
||||||
t.equal(
|
|
||||||
utils.encode('\x28\x29', null, null, null, 'RFC1738'),
|
|
||||||
'()',
|
|
||||||
'does not encode parens in RFC1738'
|
|
||||||
);
|
|
||||||
|
|
||||||
// todo RFC1738 format
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode('Āက豈'),
|
|
||||||
'%C4%80%E1%80%80%EF%A4%80',
|
|
||||||
'encodes multibyte chars'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.equal(
|
|
||||||
utils.encode('\uD83D \uDCA9'),
|
|
||||||
'%F0%9F%90%A0%F0%BA%90%80',
|
|
||||||
'encodes lone surrogates'
|
|
||||||
);
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('isBuffer()', function (t) {
|
|
||||||
var fn = function () {};
|
|
||||||
fn();
|
|
||||||
forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], fn, /a/g], function (x) {
|
|
||||||
t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
|
|
||||||
});
|
|
||||||
|
|
||||||
var fakeBuffer = { constructor: Buffer };
|
|
||||||
t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
|
|
||||||
|
|
||||||
var saferBuffer = SaferBuffer.from('abc');
|
|
||||||
t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
|
|
||||||
|
|
||||||
var buffer = SaferBuffer.from('abc');
|
|
||||||
t.notEqual(saferBuffer, buffer, 'different buffer instances');
|
|
||||||
t.equal(utils.isBuffer(buffer), true, 'another Buffer instance is a buffer');
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('isRegExp()', function (t) {
|
|
||||||
t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');
|
|
||||||
t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');
|
|
||||||
t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');
|
|
||||||
|
|
||||||
forEach(v.primitives, function (primitive) {
|
|
||||||
t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');
|
|
||||||
});
|
|
||||||
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
-55
@@ -1,55 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "body-parser",
|
|
||||||
"description": "Node.js body parsing middleware",
|
|
||||||
"version": "1.20.5",
|
|
||||||
"contributors": [
|
|
||||||
"Douglas Christopher Wilson <doug@somethingdoug.com>",
|
|
||||||
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
|
||||||
"repository": "expressjs/body-parser",
|
|
||||||
"dependencies": {
|
|
||||||
"bytes": "~3.1.2",
|
|
||||||
"content-type": "~1.0.5",
|
|
||||||
"debug": "2.6.9",
|
|
||||||
"depd": "2.0.0",
|
|
||||||
"destroy": "~1.2.0",
|
|
||||||
"http-errors": "~2.0.1",
|
|
||||||
"iconv-lite": "~0.4.24",
|
|
||||||
"on-finished": "~2.4.1",
|
|
||||||
"qs": "~6.15.1",
|
|
||||||
"raw-body": "~2.5.3",
|
|
||||||
"type-is": "~1.6.18",
|
|
||||||
"unpipe": "~1.0.0"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"eslint": "8.34.0",
|
|
||||||
"eslint-config-standard": "14.1.1",
|
|
||||||
"eslint-plugin-import": "2.27.5",
|
|
||||||
"eslint-plugin-markdown": "3.0.0",
|
|
||||||
"eslint-plugin-node": "11.1.0",
|
|
||||||
"eslint-plugin-promise": "6.1.1",
|
|
||||||
"eslint-plugin-standard": "4.1.0",
|
|
||||||
"methods": "1.1.2",
|
|
||||||
"mocha": "10.2.0",
|
|
||||||
"nyc": "15.1.0",
|
|
||||||
"safe-buffer": "5.2.1",
|
|
||||||
"supertest": "6.3.3"
|
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"lib/",
|
|
||||||
"LICENSE",
|
|
||||||
"HISTORY.md",
|
|
||||||
"index.js"
|
|
||||||
],
|
|
||||||
"engines": {
|
|
||||||
"node": ">= 0.8",
|
|
||||||
"npm": "1.2.8000 || >= 1.4.16"
|
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"lint": "eslint .",
|
|
||||||
"test": "mocha --require test/support/env --reporter spec --check-leaks --bail test/",
|
|
||||||
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
|
|
||||||
"test-cov": "nyc --reporter=html --reporter=text npm test"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-97
@@ -1,97 +0,0 @@
|
|||||||
3.1.2 / 2022-01-27
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Fix return value for un-parsable strings
|
|
||||||
|
|
||||||
3.1.1 / 2021-11-15
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Fix "thousandsSeparator" incorrecting formatting fractional part
|
|
||||||
|
|
||||||
3.1.0 / 2019-01-22
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Add petabyte (`pb`) support
|
|
||||||
|
|
||||||
3.0.0 / 2017-08-31
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Change "kB" to "KB" in format output
|
|
||||||
* Remove support for Node.js 0.6
|
|
||||||
* Remove support for ComponentJS
|
|
||||||
|
|
||||||
2.5.0 / 2017-03-24
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Add option "unit"
|
|
||||||
|
|
||||||
2.4.0 / 2016-06-01
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Add option "unitSeparator"
|
|
||||||
|
|
||||||
2.3.0 / 2016-02-15
|
|
||||||
==================
|
|
||||||
|
|
||||||
* Drop partial bytes on all parsed units
|
|
||||||
* Fix non-finite numbers to `.format` to return `null`
|
|
||||||
* Fix parsing byte string that looks like hex
|
|
||||||
* perf: hoist regular expressions
|
|
||||||
|
|
||||||
2.2.0 / 2015-11-13
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add option "decimalPlaces"
|
|
||||||
* add option "fixedDecimals"
|
|
||||||
|
|
||||||
2.1.0 / 2015-05-21
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add `.format` export
|
|
||||||
* add `.parse` export
|
|
||||||
|
|
||||||
2.0.2 / 2015-05-20
|
|
||||||
==================
|
|
||||||
|
|
||||||
* remove map recreation
|
|
||||||
* remove unnecessary object construction
|
|
||||||
|
|
||||||
2.0.1 / 2015-05-07
|
|
||||||
==================
|
|
||||||
|
|
||||||
* fix browserify require
|
|
||||||
* remove node.extend dependency
|
|
||||||
|
|
||||||
2.0.0 / 2015-04-12
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add option "case"
|
|
||||||
* add option "thousandsSeparator"
|
|
||||||
* return "null" on invalid parse input
|
|
||||||
* support proper round-trip: bytes(bytes(num)) === num
|
|
||||||
* units no longer case sensitive when parsing
|
|
||||||
|
|
||||||
1.0.0 / 2014-05-05
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add negative support. fixes #6
|
|
||||||
|
|
||||||
0.3.0 / 2014-03-19
|
|
||||||
==================
|
|
||||||
|
|
||||||
* added terabyte support
|
|
||||||
|
|
||||||
0.2.1 / 2013-04-01
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add .component
|
|
||||||
|
|
||||||
0.2.0 / 2012-10-28
|
|
||||||
==================
|
|
||||||
|
|
||||||
* bytes(200).should.eql('200b')
|
|
||||||
|
|
||||||
0.1.0 / 2012-07-04
|
|
||||||
==================
|
|
||||||
|
|
||||||
* add bytes to string conversion [yields]
|
|
||||||
-23
@@ -1,23 +0,0 @@
|
|||||||
(The MIT License)
|
|
||||||
|
|
||||||
Copyright (c) 2012-2014 TJ Holowaychuk <tj@vision-media.ca>
|
|
||||||
Copyright (c) 2015 Jed Watson <jed.watson@me.com>
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
'Software'), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
||||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
||||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
||||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
-152
@@ -1,152 +0,0 @@
|
|||||||
# Bytes utility
|
|
||||||
|
|
||||||
[![NPM Version][npm-image]][npm-url]
|
|
||||||
[![NPM Downloads][downloads-image]][downloads-url]
|
|
||||||
[![Build Status][ci-image]][ci-url]
|
|
||||||
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
||||||
|
|
||||||
Utility to parse a string bytes (ex: `1TB`) to bytes (`1099511627776`) and vice-versa.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
||||||
[npm registry](https://www.npmjs.com/). Installation is done using the
|
|
||||||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ npm install bytes
|
|
||||||
```
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```js
|
|
||||||
var bytes = require('bytes');
|
|
||||||
```
|
|
||||||
|
|
||||||
#### bytes(number|string value, [options]): number|string|null
|
|
||||||
|
|
||||||
Default export function. Delegates to either `bytes.format` or `bytes.parse` based on the type of `value`.
|
|
||||||
|
|
||||||
**Arguments**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------|----------|--------------------|
|
|
||||||
| value | `number`|`string` | Number value to format or string value to parse |
|
|
||||||
| options | `Object` | Conversion options for `format` |
|
|
||||||
|
|
||||||
**Returns**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------|------------------|-------------------------------------------------|
|
|
||||||
| results | `string`|`number`|`null` | Return null upon error. Numeric value in bytes, or string value otherwise. |
|
|
||||||
|
|
||||||
**Example**
|
|
||||||
|
|
||||||
```js
|
|
||||||
bytes(1024);
|
|
||||||
// output: '1KB'
|
|
||||||
|
|
||||||
bytes('1KB');
|
|
||||||
// output: 1024
|
|
||||||
```
|
|
||||||
|
|
||||||
#### bytes.format(number value, [options]): string|null
|
|
||||||
|
|
||||||
Format the given value in bytes into a string. If the value is negative, it is kept as such. If it is a float, it is
|
|
||||||
rounded.
|
|
||||||
|
|
||||||
**Arguments**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------|----------|--------------------|
|
|
||||||
| value | `number` | Value in bytes |
|
|
||||||
| options | `Object` | Conversion options |
|
|
||||||
|
|
||||||
**Options**
|
|
||||||
|
|
||||||
| Property | Type | Description |
|
|
||||||
|-------------------|--------|-----------------------------------------------------------------------------------------|
|
|
||||||
| decimalPlaces | `number`|`null` | Maximum number of decimal places to include in output. Default value to `2`. |
|
|
||||||
| fixedDecimals | `boolean`|`null` | Whether to always display the maximum number of decimal places. Default value to `false` |
|
|
||||||
| thousandsSeparator | `string`|`null` | Example of values: `' '`, `','` and `'.'`... Default value to `''`. |
|
|
||||||
| unit | `string`|`null` | The unit in which the result will be returned (B/KB/MB/GB/TB). Default value to `''` (which means auto detect). |
|
|
||||||
| unitSeparator | `string`|`null` | Separator to use between number and unit. Default value to `''`. |
|
|
||||||
|
|
||||||
**Returns**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------|------------------|-------------------------------------------------|
|
|
||||||
| results | `string`|`null` | Return null upon error. String value otherwise. |
|
|
||||||
|
|
||||||
**Example**
|
|
||||||
|
|
||||||
```js
|
|
||||||
bytes.format(1024);
|
|
||||||
// output: '1KB'
|
|
||||||
|
|
||||||
bytes.format(1000);
|
|
||||||
// output: '1000B'
|
|
||||||
|
|
||||||
bytes.format(1000, {thousandsSeparator: ' '});
|
|
||||||
// output: '1 000B'
|
|
||||||
|
|
||||||
bytes.format(1024 * 1.7, {decimalPlaces: 0});
|
|
||||||
// output: '2KB'
|
|
||||||
|
|
||||||
bytes.format(1024, {unitSeparator: ' '});
|
|
||||||
// output: '1 KB'
|
|
||||||
```
|
|
||||||
|
|
||||||
#### bytes.parse(string|number value): number|null
|
|
||||||
|
|
||||||
Parse the string value into an integer in bytes. If no unit is given, or `value`
|
|
||||||
is a number, it is assumed the value is in bytes.
|
|
||||||
|
|
||||||
Supported units and abbreviations are as follows and are case-insensitive:
|
|
||||||
|
|
||||||
* `b` for bytes
|
|
||||||
* `kb` for kilobytes
|
|
||||||
* `mb` for megabytes
|
|
||||||
* `gb` for gigabytes
|
|
||||||
* `tb` for terabytes
|
|
||||||
* `pb` for petabytes
|
|
||||||
|
|
||||||
The units are in powers of two, not ten. This means 1kb = 1024b according to this parser.
|
|
||||||
|
|
||||||
**Arguments**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------------|--------|--------------------|
|
|
||||||
| value | `string`|`number` | String to parse, or number in bytes. |
|
|
||||||
|
|
||||||
**Returns**
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
|---------|-------------|-------------------------|
|
|
||||||
| results | `number`|`null` | Return null upon error. Value in bytes otherwise. |
|
|
||||||
|
|
||||||
**Example**
|
|
||||||
|
|
||||||
```js
|
|
||||||
bytes.parse('1KB');
|
|
||||||
// output: 1024
|
|
||||||
|
|
||||||
bytes.parse('1024');
|
|
||||||
// output: 1024
|
|
||||||
|
|
||||||
bytes.parse(1024);
|
|
||||||
// output: 1024
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
|
||||||
|
|
||||||
[MIT](LICENSE)
|
|
||||||
|
|
||||||
[ci-image]: https://badgen.net/github/checks/visionmedia/bytes.js/master?label=ci
|
|
||||||
[ci-url]: https://github.com/visionmedia/bytes.js/actions?query=workflow%3Aci
|
|
||||||
[coveralls-image]: https://badgen.net/coveralls/c/github/visionmedia/bytes.js/master
|
|
||||||
[coveralls-url]: https://coveralls.io/r/visionmedia/bytes.js?branch=master
|
|
||||||
[downloads-image]: https://badgen.net/npm/dm/bytes
|
|
||||||
[downloads-url]: https://npmjs.org/package/bytes
|
|
||||||
[npm-image]: https://badgen.net/npm/v/bytes
|
|
||||||
[npm-url]: https://npmjs.org/package/bytes
|
|
||||||
-170
@@ -1,170 +0,0 @@
|
|||||||
/*!
|
|
||||||
* bytes
|
|
||||||
* Copyright(c) 2012-2014 TJ Holowaychuk
|
|
||||||
* Copyright(c) 2015 Jed Watson
|
|
||||||
* MIT Licensed
|
|
||||||
*/
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module exports.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = bytes;
|
|
||||||
module.exports.format = format;
|
|
||||||
module.exports.parse = parse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Module variables.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
|
|
||||||
var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;
|
|
||||||
|
|
||||||
var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;
|
|
||||||
|
|
||||||
var map = {
|
|
||||||
b: 1,
|
|
||||||
kb: 1 << 10,
|
|
||||||
mb: 1 << 20,
|
|
||||||
gb: 1 << 30,
|
|
||||||
tb: Math.pow(1024, 4),
|
|
||||||
pb: Math.pow(1024, 5),
|
|
||||||
};
|
|
||||||
|
|
||||||
var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert the given value in bytes into a string or parse to string to an integer in bytes.
|
|
||||||
*
|
|
||||||
* @param {string|number} value
|
|
||||||
* @param {{
|
|
||||||
* case: [string],
|
|
||||||
* decimalPlaces: [number]
|
|
||||||
* fixedDecimals: [boolean]
|
|
||||||
* thousandsSeparator: [string]
|
|
||||||
* unitSeparator: [string]
|
|
||||||
* }} [options] bytes options.
|
|
||||||
*
|
|
||||||
* @returns {string|number|null}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function bytes(value, options) {
|
|
||||||
if (typeof value === 'string') {
|
|
||||||
return parse(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
|
||||||
return format(value, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Format the given value in bytes into a string.
|
|
||||||
*
|
|
||||||
* If the value is negative, it is kept as such. If it is a float,
|
|
||||||
* it is rounded.
|
|
||||||
*
|
|
||||||
* @param {number} value
|
|
||||||
* @param {object} [options]
|
|
||||||
* @param {number} [options.decimalPlaces=2]
|
|
||||||
* @param {number} [options.fixedDecimals=false]
|
|
||||||
* @param {string} [options.thousandsSeparator=]
|
|
||||||
* @param {string} [options.unit=]
|
|
||||||
* @param {string} [options.unitSeparator=]
|
|
||||||
*
|
|
||||||
* @returns {string|null}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function format(value, options) {
|
|
||||||
if (!Number.isFinite(value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var mag = Math.abs(value);
|
|
||||||
var thousandsSeparator = (options && options.thousandsSeparator) || '';
|
|
||||||
var unitSeparator = (options && options.unitSeparator) || '';
|
|
||||||
var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
|
|
||||||
var fixedDecimals = Boolean(options && options.fixedDecimals);
|
|
||||||
var unit = (options && options.unit) || '';
|
|
||||||
|
|
||||||
if (!unit || !map[unit.toLowerCase()]) {
|
|
||||||
if (mag >= map.pb) {
|
|
||||||
unit = 'PB';
|
|
||||||
} else if (mag >= map.tb) {
|
|
||||||
unit = 'TB';
|
|
||||||
} else if (mag >= map.gb) {
|
|
||||||
unit = 'GB';
|
|
||||||
} else if (mag >= map.mb) {
|
|
||||||
unit = 'MB';
|
|
||||||
} else if (mag >= map.kb) {
|
|
||||||
unit = 'KB';
|
|
||||||
} else {
|
|
||||||
unit = 'B';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var val = value / map[unit.toLowerCase()];
|
|
||||||
var str = val.toFixed(decimalPlaces);
|
|
||||||
|
|
||||||
if (!fixedDecimals) {
|
|
||||||
str = str.replace(formatDecimalsRegExp, '$1');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (thousandsSeparator) {
|
|
||||||
str = str.split('.').map(function (s, i) {
|
|
||||||
return i === 0
|
|
||||||
? s.replace(formatThousandsRegExp, thousandsSeparator)
|
|
||||||
: s
|
|
||||||
}).join('.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return str + unitSeparator + unit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse the string value into an integer in bytes.
|
|
||||||
*
|
|
||||||
* If no unit is given, it is assumed the value is in bytes.
|
|
||||||
*
|
|
||||||
* @param {number|string} val
|
|
||||||
*
|
|
||||||
* @returns {number|null}
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
|
|
||||||
function parse(val) {
|
|
||||||
if (typeof val === 'number' && !isNaN(val)) {
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof val !== 'string') {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test if the string passed is valid
|
|
||||||
var results = parseRegExp.exec(val);
|
|
||||||
var floatValue;
|
|
||||||
var unit = 'b';
|
|
||||||
|
|
||||||
if (!results) {
|
|
||||||
// Nothing could be extracted from the given string
|
|
||||||
floatValue = parseInt(val, 10);
|
|
||||||
unit = 'b'
|
|
||||||
} else {
|
|
||||||
// Retrieve the value and the unit
|
|
||||||
floatValue = parseFloat(results[1]);
|
|
||||||
unit = results[4].toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNaN(floatValue)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Math.floor(map[unit] * floatValue);
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user