added doga
This commit is contained in:
22
25_02_24/node_modules/steno/LICENSE
generated
vendored
Normal file
22
25_02_24/node_modules/steno/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2021 typicode
|
||||
|
||||
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.
|
||||
|
||||
50
25_02_24/node_modules/steno/README.md
generated
vendored
Normal file
50
25_02_24/node_modules/steno/README.md
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
# Steno [](https://www.npmjs.org/package/steno) [](https://github.com/typicode/steno/actions/workflows/node.js.yml)
|
||||
|
||||
> Specialized fast async file writer
|
||||
|
||||
**Steno** makes writing to the same file often/concurrently fast and safe.
|
||||
|
||||
Used in [lowdb](https://github.com/typicode/lowdb).
|
||||
|
||||
_https://en.wikipedia.org/wiki/Stenotype_
|
||||
|
||||
## Features
|
||||
|
||||
- ⚡ Fast (see benchmark)
|
||||
- 🐦 Lightweight (~6kb)
|
||||
- 👍 ⚛️ Safe: No partial writes (writes are atomic)
|
||||
- 👍 🏁 Safe: No race conditions (writes are ordered even if they're async)
|
||||
|
||||
## Usage
|
||||
|
||||
```javascript
|
||||
import { Writer } from 'steno'
|
||||
|
||||
// Create a singleton writer
|
||||
const file = new Writer('file.txt')
|
||||
|
||||
// Use it in the rest of your code
|
||||
async function save() {
|
||||
await file.write('some data')
|
||||
}
|
||||
```
|
||||
|
||||
## Benchmark
|
||||
|
||||
`npm run benchmark` (see `src/benchmark.ts`)
|
||||
|
||||
```
|
||||
Write 1KB data to the same file x 1000
|
||||
fs : 62ms
|
||||
steno : 1ms
|
||||
|
||||
Write 1MB data to the same file x 1000
|
||||
fs : 2300ms
|
||||
steno : 5ms
|
||||
```
|
||||
|
||||
_Steno uses a smart queue and avoids unnecessary writes._
|
||||
|
||||
## License
|
||||
|
||||
MIT - [Typicode](https://github.com/typicode)
|
||||
11
25_02_24/node_modules/steno/lib/index.d.ts
generated
vendored
Normal file
11
25_02_24/node_modules/steno/lib/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
/// <reference types="node" resolution-mode="require"/>
|
||||
/// <reference types="node" resolution-mode="require"/>
|
||||
import { PathLike } from 'node:fs';
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
type Data = Parameters<typeof writeFile>[1];
|
||||
export declare class Writer {
|
||||
#private;
|
||||
constructor(filename: PathLike);
|
||||
write(data: Data): Promise<void>;
|
||||
}
|
||||
export {};
|
||||
86
25_02_24/node_modules/steno/lib/index.js
generated
vendored
Normal file
86
25_02_24/node_modules/steno/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,86 @@
|
||||
import { rename, writeFile } from 'node:fs/promises';
|
||||
import { basename, dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
// Returns a temporary file
|
||||
// Example: for /some/file will return /some/.file.tmp
|
||||
function getTempFilename(file) {
|
||||
const f = file instanceof URL ? fileURLToPath(file) : file.toString();
|
||||
return join(dirname(f), `.${basename(f)}.tmp`);
|
||||
}
|
||||
// Retries an asynchronous operation with a delay between retries and a maximum retry count
|
||||
async function retryAsyncOperation(fn, maxRetries, delayMs) {
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
try {
|
||||
return await fn();
|
||||
}
|
||||
catch (error) {
|
||||
if (i < maxRetries - 1) {
|
||||
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||
}
|
||||
else {
|
||||
throw error; // Rethrow the error if max retries reached
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
export class Writer {
|
||||
#filename;
|
||||
#tempFilename;
|
||||
#locked = false;
|
||||
#prev = null;
|
||||
#next = null;
|
||||
#nextPromise = null;
|
||||
#nextData = null;
|
||||
// File is locked, add data for later
|
||||
#add(data) {
|
||||
// Only keep most recent data
|
||||
this.#nextData = data;
|
||||
// Create a singleton promise to resolve all next promises once next data is written
|
||||
this.#nextPromise ||= new Promise((resolve, reject) => {
|
||||
this.#next = [resolve, reject];
|
||||
});
|
||||
// Return a promise that will resolve at the same time as next promise
|
||||
return new Promise((resolve, reject) => {
|
||||
this.#nextPromise?.then(resolve).catch(reject);
|
||||
});
|
||||
}
|
||||
// File isn't locked, write data
|
||||
async #write(data) {
|
||||
// Lock file
|
||||
this.#locked = true;
|
||||
try {
|
||||
// Atomic write
|
||||
await writeFile(this.#tempFilename, data, 'utf-8');
|
||||
await retryAsyncOperation(async () => {
|
||||
await rename(this.#tempFilename, this.#filename);
|
||||
}, 10, 100);
|
||||
// Call resolve
|
||||
this.#prev?.[0]();
|
||||
}
|
||||
catch (err) {
|
||||
// Call reject
|
||||
if (err instanceof Error) {
|
||||
this.#prev?.[1](err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
finally {
|
||||
// Unlock file
|
||||
this.#locked = false;
|
||||
this.#prev = this.#next;
|
||||
this.#next = this.#nextPromise = null;
|
||||
if (this.#nextData !== null) {
|
||||
const nextData = this.#nextData;
|
||||
this.#nextData = null;
|
||||
await this.write(nextData);
|
||||
}
|
||||
}
|
||||
}
|
||||
constructor(filename) {
|
||||
this.#filename = filename;
|
||||
this.#tempFilename = getTempFilename(filename);
|
||||
}
|
||||
async write(data) {
|
||||
return this.#locked ? this.#add(data) : this.#write(data);
|
||||
}
|
||||
}
|
||||
62
25_02_24/node_modules/steno/package.json
generated
vendored
Normal file
62
25_02_24/node_modules/steno/package.json
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
{
|
||||
"name": "steno",
|
||||
"version": "4.0.2",
|
||||
"description": "Specialized fast async file writer",
|
||||
"keywords": [
|
||||
"fs",
|
||||
"file",
|
||||
"write",
|
||||
"writer",
|
||||
"asynchronous",
|
||||
"fast",
|
||||
"race",
|
||||
"condition",
|
||||
"atomic",
|
||||
"writing",
|
||||
"safe"
|
||||
],
|
||||
"homepage": "https://github.com/typicode/steno",
|
||||
"bugs": {
|
||||
"url": "https://github.com/typicode/steno/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/typicode/steno.git"
|
||||
},
|
||||
"funding": "https://github.com/sponsors/typicode",
|
||||
"license": "MIT",
|
||||
"author": "Typicode <typicode@gmail.com>",
|
||||
"type": "module",
|
||||
"exports": "./lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"files": [
|
||||
"lib/index.js",
|
||||
"lib/index.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node --import tsx/esm --test src/test.ts",
|
||||
"build": "del-cli lib && tsc",
|
||||
"lint": "eslint src --ext .ts --ignore-path .gitignore",
|
||||
"prepare": "husky install",
|
||||
"prepublishOnly": "npm run build",
|
||||
"postversion": "git push && git push --tags && npm publish",
|
||||
"benchmark": "npm run build && node lib/benchmark.js",
|
||||
"commit": "commit"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^17.7.2",
|
||||
"@commitlint/config-conventional": "^17.7.0",
|
||||
"@commitlint/prompt-cli": "^17.7.2",
|
||||
"@sindresorhus/tsconfig": "^5.0.0",
|
||||
"@types/async-retry": "^1.4.8",
|
||||
"@types/node": "^20.8.3",
|
||||
"@typicode/eslint-config": "^1.2.0",
|
||||
"del-cli": "^5.1.0",
|
||||
"husky": "^8.0.3",
|
||||
"tsx": "^4.7.0",
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user