Add comprehensive tests for role middleware and fix package dependencies
Some checks are pending
Docker Test / test (push) Waiting to run

This commit is contained in:
BibaBot 2026-03-16 20:07:22 +00:00
parent 64aa924270
commit bfd432d094
1884 changed files with 384668 additions and 84 deletions

21
node_modules/sql-escaper/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Weslley Araújo, Andrey Sidorov, Douglas Wilson, and contributors.
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.

424
node_modules/sql-escaper/README.md generated vendored Normal file
View file

@ -0,0 +1,424 @@
# SQL Escaper
[![NPM Version](https://img.shields.io/npm/v/sql-escaper.svg?label=&color=70a1ff&logo=npm&logoColor=white)](https://www.npmjs.com/package/sql-escaper)
[![NPM Downloads](https://img.shields.io/npm/dm/sql-escaper.svg?label=&logo=npm&logoColor=white&color=45aaf2)](https://www.npmjs.com/package/sql-escaper)
[![Coverage](https://img.shields.io/codecov/c/github/mysqljs/sql-escaper?label=&logo=codecov&logoColor=white&color=98cc00)](https://app.codecov.io/gh/mysqljs/sql-escaper)<br />
[![GitHub Workflow Status (Node.js)](https://img.shields.io/github/actions/workflow/status/mysqljs/sql-escaper/ci_node.yml?event=push&label=&branch=main&logo=nodedotjs&logoColor=535c68&color=badc58)](https://github.com/mysqljs/sql-escaper/actions/workflows/ci_node.yml?query=branch%3Amain)
[![GitHub Workflow Status (Bun)](https://img.shields.io/github/actions/workflow/status/mysqljs/sql-escaper/ci_bun.yml?event=push&label=&branch=main&logo=bun&logoColor=ffffff&color=f368e0)](https://github.com/mysqljs/sql-escaper/actions/workflows/ci_bun.yml?query=branch%3Amain)
[![GitHub Workflow Status (Deno)](https://img.shields.io/github/actions/workflow/status/mysqljs/sql-escaper/ci_deno.yml?event=push&label=&branch=main&logo=deno&logoColor=ffffff&color=079992)](https://github.com/mysqljs/sql-escaper/actions/workflows/ci_deno.yml?query=branch%3Amain)
## Motivation
**SQL Escaper** is a rework of [**sqlstring**](https://github.com/mysqljs/sqlstring) (created by [**Douglas Wilson**](https://github.com/dougwilson)), by using an **AST**-based approach to parse and format SQL queries while maintaining its same API.
### Rework includes:
- **TypeScript** by default.
- Support for `Uint8Array` and `BigInt`.
- Support for both **CJS** and **ESM** exports.
- Up to [**~40% faster**](#performance) compared to **sqlstring**.
- Distinguishes when a keyword is used as value.
- Distinguishes when a column has a keyword name.
- Distinguishes between multiple clauses/keywords in the same query.
- Reasonable conservative support for **Node.js v12** _(**sqlstring** supports **Node.js v0.6**)_.
> [!TIP]
>
> **SQL Escaper** has the same API as the original [**sqlstring**](https://github.com/mysqljs/sqlstring), so it can be used as a drop-in replacement. If **SQL Escaper** breaks any **API** usage compared to **sqlstring**, please, report it as a bug. [Pull Requests are welcome](./CONTRIBUTING.md).
> [!IMPORTANT]
>
> 🔐 **SQL Escaper** is intended to fix a potential [**SQL Injection vulnerability**](https://flattsecurity.medium.com/finding-an-unseen-sql-injection-by-bypassing-escape-functions-in-mysqljs-mysql-90b27f6542b4) reported in 2022. By combining the original [**sqlstring**](https://github.com/mysqljs/sqlstring) with [**mysqljs/mysql**](https://github.com/mysqljs/mysql) or [**MySQL2**](https://github.com/sidorares/node-mysql2), objects passed as values could be expanded into **SQL** fragments, potentially allowing attackers to manipulate query structure. See [sidorares/node-mysql2#4051](https://github.com/sidorares/node-mysql2/issues/4051) for details.
>
> Regardless of the `stringifyObjects` value, objects used outside of `SET` or `ON DUPLICATE KEY UPDATE` contexts are always stringified as `'[object Object]'`. This is a security measure to prevent [SQL Injection](https://flattsecurity.medium.com/finding-an-unseen-sql-injection-by-bypassing-escape-functions-in-mysqljs-mysql-90b27f6542b4) and is not interpreted as a breaking change for **sqlstring** usage.
---
## Install
```bash
# Node.js
npm i sql-escaper
```
```bash
# Bun
bun add sql-escaper
```
```bash
# Deno
deno add npm:sql-escaper
```
---
### [MySQL2](https://github.com/sidorares/node-mysql2)
For **MySQL2**, it already uses **SQL Escaper** as its default escaping library since version `3.17.0`, so you just need to update it to the latest version:
```bash
npm i mysql2@latest
```
### [mysqljs/mysql](https://github.com/mysqljs/mysql)
You can use an overrides in your _package.json_:
```json
"dependencies": {
"mysql": "^2.18.1"
},
"overrides": {
"sqlstring": "npm:sql-escaper"
}
```
- Next, clean the `node_modules` and reinstall the dependencies (`npm i`).
- Please, note the minimum supported version of **Node.js** is `12`.
---
## Usage
For _up-to-date_ documentation, always follow the [**README.md**](https://github.com/mysqljs/sql-escaper?tab=readme-ov-file#readme) in the **GitHub** repository.
### Quickstart
```js
import { escape, escapeId, format, raw } from 'sql-escaper';
escape("Hello 'World'");
// => "'Hello \\'World\\''"
escapeId('table.column');
// => '`table`.`column`'
format('SELECT * FROM ?? WHERE id = ?', ['users', 42]);
// => 'SELECT * FROM `users` WHERE id = 42'
format('INSERT INTO users SET ?', [{ name: 'foo', email: 'bar@test.com' }]);
// => "INSERT INTO users SET `name` = 'foo', `email` = 'bar@test.com'"
escape(raw('NOW()'));
// => 'NOW()'
```
### Import
#### ES Modules
```js
import { escape, escapeId, format, raw } from 'sql-escaper';
```
#### CommonJS
```js
const { escape, escapeId, format, raw } = require('sql-escaper');
```
---
## API
### escape
Escapes a value for safe use in SQL queries.
```ts
escape(value: SqlValue, stringifyObjects?: boolean, timezone?: Timezone): string
```
```js
escape(undefined); // 'NULL'
escape(null); // 'NULL'
escape(true); // 'true'
escape(false); // 'false'
escape(5); // '5'
escape("Hello 'World"); // "'Hello \\'World'"
```
#### Dates
Dates are converted to `YYYY-MM-DD HH:mm:ss.sss` format:
```js
escape(new Date(2012, 4, 7, 11, 42, 3, 2));
// => "'2012-05-07 11:42:03.002'"
```
Invalid dates return `NULL`:
```js
escape(new Date(NaN)); // 'NULL'
```
You can specify a timezone:
```js
const date = new Date(Date.UTC(2012, 4, 7, 11, 42, 3, 2));
escape(date, false, 'Z'); // "'2012-05-07 11:42:03.002'"
escape(date, false, '+01'); // "'2012-05-07 12:42:03.002'"
escape(date, false, '-05:00'); // "'2012-05-07 06:42:03.002'"
```
#### Buffers
Buffers are converted to hex strings:
```js
escape(Buffer.from([0, 1, 254, 255]));
// => "X'0001feff'"
```
#### Objects
Objects with a `toSqlString` method will have that method called:
```js
escape({ toSqlString: () => 'NOW()' });
// => 'NOW()'
```
Plain objects are converted to `key = value` pairs:
```js
escape({ a: 'b', c: 'd' });
// => "`a` = 'b', `c` = 'd'"
```
Function properties in objects are ignored:
```js
escape({ a: 'b', c: () => {} });
// => "`a` = 'b'"
```
When `stringifyObjects` is set to a non-nullish value, objects are stringified instead of being expanded into key-value pairs:
```js
escape({ a: 'b' }, true);
// => "'[object Object]'"
```
#### Arrays
Arrays are turned into comma-separated lists:
```js
escape([1, 2, 'c']);
// => "1, 2, 'c'"
```
Nested arrays are turned into grouped lists (useful for bulk inserts):
```js
escape([
[1, 2, 3],
[4, 5, 6],
]);
// => '(1, 2, 3), (4, 5, 6)'
```
---
### escapeId
Escapes an identifier (database, table, or column name).
```ts
escapeId(value: SqlValue, forbidQualified?: boolean): string
```
```js
escapeId('id');
// => '`id`'
escapeId('table.column');
// => '`table`.`column`'
escapeId('i`d');
// => '`i``d`'
```
Qualified identifiers (with `.`) can be forbidden:
```js
escapeId('id1.id2', true);
// => '`id1.id2`'
```
Arrays are turned into comma-separated identifier lists:
```js
escapeId(['a', 'b', 't.c']);
// => '`a`, `b`, `t`.`c`'
```
---
### format
Formats a SQL query by replacing `?` placeholders with escaped values and `??` with escaped identifiers.
```ts
format(sql: string, values?: SqlValue | SqlValue[], stringifyObjects?: boolean, timezone?: Timezone): string
```
```js
format('SELECT * FROM ?? WHERE id = ?', ['users', 42]);
// => 'SELECT * FROM `users` WHERE id = 42'
format('? and ?', ['a', 'b']);
// => "'a' and 'b'"
```
Triple (or more) question marks are ignored:
```js
format('? or ??? and ?', ['foo', 'bar', 'fizz', 'buzz']);
// => "'foo' or ??? and 'bar'"
```
If no values are provided, the SQL is returned unchanged:
```js
format('SELECT ??');
// => 'SELECT ??'
```
#### Objects in SET clauses
When `stringifyObjects` is falsy, objects used in `SET` or `ON DUPLICATE KEY UPDATE` contexts are automatically expanded into `key = value` pairs:
```js
format('UPDATE users SET ?', [{ name: 'foo', email: 'bar@test.com' }]);
// => "UPDATE users SET `name` = 'foo', `email` = 'bar@test.com'"
format(
'INSERT INTO users (name, email) VALUES (?, ?) ON DUPLICATE KEY UPDATE ?',
['foo', 'bar@test.com', { name: 'foo', email: 'bar@test.com' }]
);
// => "INSERT INTO users (name, email) VALUES ('foo', 'bar@test.com') ON DUPLICATE KEY UPDATE `name` = 'foo', `email` = 'bar@test.com'"
```
When `stringifyObjects` is truthy, objects are always stringified:
```js
format('UPDATE users SET ?', [{ name: 'foo' }], true);
// => "UPDATE users SET '[object Object]'"
```
---
### raw
Creates a raw SQL value that will not be escaped.
```ts
raw(sql: string): Raw
```
```js
escape(raw('NOW()'));
// => 'NOW()'
escape({ id: raw('LAST_INSERT_ID()') });
// => '`id` = LAST_INSERT_ID()'
```
Only accepts strings:
```js
raw(42); // throws TypeError
```
---
### TypeScript
You can import the available types:
```ts
import type { Raw, SqlValue, Timezone } from 'sql-escaper';
```
---
## Performance
Each benchmark formats `10,000` queries using `format` with `100` mixed values (numbers, strings, `null`, and dates), comparing **SQL Escaper** against the original [**sqlstring**](https://github.com/mysqljs/sqlstring) through [**hyperfine**](https://github.com/sharkdp/hyperfine):
| Benchmark | sqlstring | SQL Escaper | Difference |
| ---------------------------------------- | --------: | ----------: | ---------------: |
| Select 100 values | 248.8 ms | 178.7 ms | **1.39x faster** |
| Insert 100 values | 247.5 ms | 196.2 ms | **1.26x faster** |
| SET with 100 values | 257.5 ms | 205.2 ms | **1.26x faster** |
| SET with 100 objects | 348.3 ms | 250.5 ms | **1.39x faster** |
| ON DUPLICATE KEY UPDATE with 100 values | 466.2 ms | 394.6 ms | **1.18x faster** |
| ON DUPLICATE KEY UPDATE with 100 objects | 558.2 ms | 433.9 ms | **1.29x faster** |
- See detailed results and how the benchmarks are run in the [**benchmark**](https://github.com/mysqljs/sql-escaper/tree/main/benchmark) directory.
> [!NOTE]
>
> Benchmarks ran on [**GitHub Actions**](https://github.com/mysqljs/sql-escaper/blob/main/.github/workflows/ci_benchmark.yml) (`ubuntu-latest`) using **Node.js LTS**.
> Results may vary depending on runner hardware and runtime version.
---
## Differences from sqlstring
- Requires **Node.js 12+** (the original [**sqlstring**](https://github.com/mysqljs/sqlstring) supports **Node.js** 0.6+)
> [!TIP]
>
> The Node.js 12+ requirement is what allows **SQL Escaper** to leverage modern engine optimizations and achieve the [performance gains](#performance) over the original.
---
## Caution
> Based on the original [**sqlstring** documentation](https://github.com/mysqljs/sqlstring#readme).
- The escaping methods in this library only work when the [`NO_BACKSLASH_ESCAPES`](https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_backslash_escapes) SQL mode is disabled (which is the default state for MySQL servers).
- This library performs **client-side escaping** to generate SQL strings. The syntax for `format` may look similar to a prepared statement, but it is not — the escaping rules from this module are used to produce the resulting SQL string.
- When using `format`, **all** `?` placeholders are replaced, including those contained in comments and strings.
- When structured user input is provided as the value to escape, care should be taken to validate the shape of the input, as the resulting escaped string may contain more than a single value.
- `NaN` and `Infinity` are left as-is. MySQL does not support these values, and trying to insert them will trigger MySQL errors.
- The string provided to `raw()` will **skip all escaping**, so be careful when passing in unvalidated input.
---
## Security Policy
[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/mysqljs/sql-escaper/ci_codeql.yml?event=push&label=&branch=main&logo=github&logoColor=white&color=f368e0)](https://github.com/mysqljs/sql-escaper/actions/workflows/ci_codeql.yml?query=branch%3Amain)
Please check the [**SECURITY.md**](https://github.com/mysqljs/sql-escaper/blob/main/SECURITY.md).
---
## Contributing
See the [**Contributing Guide**](https://github.com/mysqljs/sql-escaper/blob/main/CONTRIBUTING.md) and please follow our [**Code of Conduct**](https://github.com/mysqljs/sql-escaper/blob/main/CODE_OF_CONDUCT.md) 🚀
---
## Acknowledgements
- [![Contributors](https://img.shields.io/github/contributors/mysqljs/sql-escaper?label=Contributors)](https://github.com/mysqljs/sql-escaper/graphs/contributors)
- **SQL Escaper** is adapted from [**sqlstring**](https://github.com/mysqljs/sqlstring) ([**MIT**](https://github.com/mysqljs/sqlstring/blob/master/LICENSE)), modernizing it with high performance, TypeScript support and multi-runtime compatibility.
- Special thanks to [**Douglas Wilson**](https://github.com/dougwilson) for the original **sqlstring** project and its [**contributors**](https://github.com/mysqljs/sqlstring/graphs/contributors).
---
## License
**SQL Escaper** is under the [**MIT License**](https://github.com/mysqljs/sql-escaper/blob/main/LICENSE).

15
node_modules/sql-escaper/lib/index.d.ts generated vendored Normal file
View file

@ -0,0 +1,15 @@
/**
* Adapted from https://github.com/mysqljs/sqlstring/blob/cd528556b4b6bcf300c3db515026935dedf7cfa1/lib/SqlString.js
* MIT LICENSE: https://github.com/mysqljs/sqlstring/blob/cd528556b4b6bcf300c3db515026935dedf7cfa1/LICENSE
*/
import type { Raw, SqlValue, Timezone } from './types.js';
import { Buffer } from 'node:buffer';
export type { Raw, SqlValue, Timezone } from './types.js';
export declare const dateToString: (date: Date, timezone: Timezone) => string;
export declare const escapeId: (value: SqlValue, forbidQualified?: boolean) => string;
export declare const objectToValues: (object: Record<string, SqlValue>, timezone?: Timezone) => string;
export declare const bufferToString: (buffer: Buffer) => string;
export declare const arrayToList: (array: SqlValue[], timezone?: Timezone) => string;
export declare const escape: (value: SqlValue, stringifyObjects?: boolean, timezone?: Timezone) => string;
export declare const format: (sql: string, values?: SqlValue | SqlValue[], stringifyObjects?: boolean, timezone?: Timezone) => string;
export declare const raw: (sql: string) => Raw;

398
node_modules/sql-escaper/lib/index.js generated vendored Normal file
View file

@ -0,0 +1,398 @@
"use strict";
/**
* Adapted from https://github.com/mysqljs/sqlstring/blob/cd528556b4b6bcf300c3db515026935dedf7cfa1/lib/SqlString.js
* MIT LICENSE: https://github.com/mysqljs/sqlstring/blob/cd528556b4b6bcf300c3db515026935dedf7cfa1/LICENSE
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.raw = exports.format = exports.escape = exports.arrayToList = exports.bufferToString = exports.objectToValues = exports.escapeId = exports.dateToString = void 0;
const node_buffer_1 = require("node:buffer");
const regex = {
backtick: /`/g,
dot: /\./g,
timezone: /([+\-\s])(\d\d):?(\d\d)?/,
escapeChars: /[\0\b\t\n\r\x1a"'\\]/g,
};
const CHARS_ESCAPE_MAP = {
'\0': '\\0',
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\r': '\\r',
'\x1a': '\\Z',
'"': '\\"',
"'": "\\'",
'\\': '\\\\',
};
const charCode = {
singleQuote: 39,
backtick: 96,
backslash: 92,
dash: 45,
slash: 47,
asterisk: 42,
questionMark: 63,
newline: 10,
space: 32,
tab: 9,
carriageReturn: 13,
};
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
const isWordChar = (code) => (code >= 65 && code <= 90) ||
(code >= 97 && code <= 122) ||
(code >= 48 && code <= 57) ||
code === 95;
const isWhitespace = (code) => code === charCode.space ||
code === charCode.tab ||
code === charCode.newline ||
code === charCode.carriageReturn;
const hasOnlyWhitespaceBetween = (sql, start, end) => {
if (start >= end)
return true;
for (let i = start; i < end; i++) {
const code = sql.charCodeAt(i);
if (code !== charCode.space &&
code !== charCode.tab &&
code !== charCode.newline &&
code !== charCode.carriageReturn)
return false;
}
return true;
};
const toLower = (code) => code | 32;
const matchesWord = (sql, position, word, length) => {
for (let offset = 0; offset < word.length; offset++)
if (toLower(sql.charCodeAt(position + offset)) !== word.charCodeAt(offset))
return false;
return ((position === 0 || !isWordChar(sql.charCodeAt(position - 1))) &&
(position + word.length >= length ||
!isWordChar(sql.charCodeAt(position + word.length))));
};
const skipSqlContext = (sql, position) => {
const currentChar = sql.charCodeAt(position);
const nextChar = sql.charCodeAt(position + 1);
if (currentChar === charCode.singleQuote) {
for (let cursor = position + 1; cursor < sql.length; cursor++) {
if (sql.charCodeAt(cursor) === charCode.backslash)
cursor++;
else if (sql.charCodeAt(cursor) === charCode.singleQuote)
return cursor + 1;
}
return sql.length;
}
if (currentChar === charCode.backtick) {
const length = sql.length;
for (let cursor = position + 1; cursor < length; cursor++) {
if (sql.charCodeAt(cursor) !== charCode.backtick)
continue;
if (sql.charCodeAt(cursor + 1) === charCode.backtick) {
cursor++;
continue;
}
return cursor + 1;
}
return length;
}
if (currentChar === charCode.dash && nextChar === charCode.dash) {
const lineBreak = sql.indexOf('\n', position + 2);
return lineBreak === -1 ? sql.length : lineBreak + 1;
}
if (currentChar === charCode.slash && nextChar === charCode.asterisk) {
const commentEnd = sql.indexOf('*/', position + 2);
return commentEnd === -1 ? sql.length : commentEnd + 2;
}
return -1;
};
const findNextPlaceholder = (sql, start) => {
const sqlLength = sql.length;
for (let position = start; position < sqlLength; position++) {
const code = sql.charCodeAt(position);
if (code === charCode.questionMark)
return position;
if (code === charCode.singleQuote ||
code === charCode.backtick ||
code === charCode.dash ||
code === charCode.slash) {
const contextEnd = skipSqlContext(sql, position);
if (contextEnd !== -1)
position = contextEnd - 1;
}
}
return -1;
};
const findSetKeyword = (sql, startFrom = 0) => {
const length = sql.length;
for (let position = startFrom; position < length; position++) {
const code = sql.charCodeAt(position);
const lower = code | 32;
if (code === charCode.singleQuote ||
code === charCode.backtick ||
code === charCode.dash ||
code === charCode.slash) {
const contextEnd = skipSqlContext(sql, position);
if (contextEnd !== -1) {
position = contextEnd - 1;
continue;
}
}
if (lower === 115 && matchesWord(sql, position, 'set', length))
return position + 3;
if (lower === 107 && matchesWord(sql, position, 'key', length)) {
let cursor = position + 3;
while (cursor < length && isWhitespace(sql.charCodeAt(cursor)))
cursor++;
if (matchesWord(sql, cursor, 'update', length))
return cursor + 6;
}
}
return -1;
};
const isDate = (value) => Object.prototype.toString.call(value) === '[object Date]';
const hasSqlString = (value) => typeof value === 'object' &&
value !== null &&
'toSqlString' in value &&
typeof value.toSqlString === 'function';
const escapeString = (value) => {
regex.escapeChars.lastIndex = 0;
let chunkIndex = 0;
let escapedValue = '';
let match;
for (match = regex.escapeChars.exec(value); match !== null; match = regex.escapeChars.exec(value)) {
escapedValue += value.slice(chunkIndex, match.index);
escapedValue += CHARS_ESCAPE_MAP[match[0]];
chunkIndex = regex.escapeChars.lastIndex;
}
if (chunkIndex === 0)
return `'${value}'`;
if (chunkIndex < value.length)
return `'${escapedValue}${value.slice(chunkIndex)}'`;
return `'${escapedValue}'`;
};
const pad2 = (value) => (value < 10 ? '0' + value : '' + value);
const pad3 = (value) => value < 10 ? '00' + value : value < 100 ? '0' + value : '' + value;
const pad4 = (value) => value < 10
? '000' + value
: value < 100
? '00' + value
: value < 1000
? '0' + value
: '' + value;
const convertTimezone = (tz) => {
if (tz === 'Z')
return 0;
const timezoneMatch = tz.match(regex.timezone);
if (timezoneMatch)
return ((timezoneMatch[1] === '-' ? -1 : 1) *
(Number.parseInt(timezoneMatch[2], 10) +
(timezoneMatch[3] ? Number.parseInt(timezoneMatch[3], 10) : 0) / 60) *
60);
return false;
};
const dateToString = (date, timezone) => {
if (Number.isNaN(date.getTime()))
return 'NULL';
let year;
let month;
let day;
let hour;
let minute;
let second;
let millisecond;
if (timezone === 'local') {
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
hour = date.getHours();
minute = date.getMinutes();
second = date.getSeconds();
millisecond = date.getMilliseconds();
}
else {
const timezoneOffsetMinutes = convertTimezone(timezone);
let time = date.getTime();
if (timezoneOffsetMinutes !== false && timezoneOffsetMinutes !== 0)
time += timezoneOffsetMinutes * 60000;
const adjustedDate = new Date(time);
year = adjustedDate.getUTCFullYear();
month = adjustedDate.getUTCMonth() + 1;
day = adjustedDate.getUTCDate();
hour = adjustedDate.getUTCHours();
minute = adjustedDate.getUTCMinutes();
second = adjustedDate.getUTCSeconds();
millisecond = adjustedDate.getUTCMilliseconds();
}
// YYYY-MM-DD HH:mm:ss.mmm
return escapeString(pad4(year) +
'-' +
pad2(month) +
'-' +
pad2(day) +
' ' +
pad2(hour) +
':' +
pad2(minute) +
':' +
pad2(second) +
'.' +
pad3(millisecond));
};
exports.dateToString = dateToString;
const escapeId = (value, forbidQualified) => {
if (Array.isArray(value)) {
const length = value.length;
const parts = new Array(length);
for (let i = 0; i < length; i++)
parts[i] = (0, exports.escapeId)(value[i], forbidQualified);
return parts.join(', ');
}
const identifier = String(value);
const hasJsonOperator = identifier.indexOf('->') !== -1;
if (forbidQualified || hasJsonOperator) {
if (identifier.indexOf('`') === -1)
return `\`${identifier}\``;
return `\`${identifier.replace(regex.backtick, '``')}\``;
}
if (identifier.indexOf('`') === -1 && identifier.indexOf('.') === -1)
return `\`${identifier}\``;
return `\`${identifier
.replace(regex.backtick, '``')
.replace(regex.dot, '`.`')}\``;
};
exports.escapeId = escapeId;
const objectToValues = (object, timezone) => {
const keys = Object.keys(object);
const keysLength = keys.length;
if (keysLength === 0)
return '';
let sql = '';
for (let i = 0; i < keysLength; i++) {
const key = keys[i];
const value = object[key];
if (typeof value === 'function')
continue;
if (sql.length > 0)
sql += ', ';
sql += (0, exports.escapeId)(key);
sql += ' = ';
sql += (0, exports.escape)(value, true, timezone);
}
return sql;
};
exports.objectToValues = objectToValues;
const bufferToString = (buffer) => `X${escapeString(buffer.toString('hex'))}`;
exports.bufferToString = bufferToString;
const arrayToList = (array, timezone) => {
const length = array.length;
const parts = new Array(length);
for (let i = 0; i < length; i++) {
const value = array[i];
if (Array.isArray(value))
parts[i] = `(${(0, exports.arrayToList)(value, timezone)})`;
else
parts[i] = (0, exports.escape)(value, true, timezone);
}
return parts.join(', ');
};
exports.arrayToList = arrayToList;
const escape = (value, stringifyObjects, timezone) => {
if (value === undefined || value === null)
return 'NULL';
switch (typeof value) {
case 'boolean':
return value ? 'true' : 'false';
case 'number':
case 'bigint':
return value + '';
case 'object': {
if (isDate(value))
return (0, exports.dateToString)(value, timezone || 'local');
if (Array.isArray(value))
return (0, exports.arrayToList)(value, timezone);
if (node_buffer_1.Buffer.isBuffer(value))
return (0, exports.bufferToString)(value);
if (value instanceof Uint8Array)
return (0, exports.bufferToString)(node_buffer_1.Buffer.from(value));
if (hasSqlString(value))
return String(value.toSqlString());
if (!(stringifyObjects === undefined || stringifyObjects === null))
return escapeString(String(value));
if (isRecord(value))
return (0, exports.objectToValues)(value, timezone);
return escapeString(String(value));
}
case 'string':
return escapeString(value);
default:
return escapeString(String(value));
}
};
exports.escape = escape;
const format = (sql, values, stringifyObjects, timezone) => {
if (values === undefined || values === null)
return sql;
const valuesArray = Array.isArray(values) ? values : [values];
const length = valuesArray.length;
let setIndex = -2; // -2 = not yet computed, -1 = no SET found
let result = '';
let chunkIndex = 0;
let valuesIndex = 0;
let placeholderPosition = findNextPlaceholder(sql, 0);
while (valuesIndex < length && placeholderPosition !== -1) {
// Count consecutive question marks to detect ? vs ?? vs ???+
let placeholderEnd = placeholderPosition + 1;
let escapedValue;
while (sql.charCodeAt(placeholderEnd) === 63)
placeholderEnd++;
const placeholderLength = placeholderEnd - placeholderPosition;
const currentValue = valuesArray[valuesIndex];
if (placeholderLength > 2) {
placeholderPosition = findNextPlaceholder(sql, placeholderEnd);
continue;
}
if (placeholderLength === 2)
escapedValue = (0, exports.escapeId)(currentValue);
else if (typeof currentValue === 'number')
escapedValue = `${currentValue}`;
else if (typeof currentValue === 'object' &&
currentValue !== null &&
!stringifyObjects) {
// Lazy: compute SET position only when we first encounter an object
if (setIndex === -2)
setIndex = findSetKeyword(sql);
if (setIndex !== -1 &&
setIndex <= placeholderPosition &&
hasOnlyWhitespaceBetween(sql, setIndex, placeholderPosition) &&
!hasSqlString(currentValue) &&
!Array.isArray(currentValue) &&
!node_buffer_1.Buffer.isBuffer(currentValue) &&
!(currentValue instanceof Uint8Array) &&
!isDate(currentValue) &&
isRecord(currentValue)) {
escapedValue = (0, exports.objectToValues)(currentValue, timezone);
setIndex = findSetKeyword(sql, placeholderEnd);
}
else
escapedValue = (0, exports.escape)(currentValue, true, timezone);
}
else
escapedValue = (0, exports.escape)(currentValue, stringifyObjects, timezone);
result += sql.slice(chunkIndex, placeholderPosition);
result += escapedValue;
chunkIndex = placeholderEnd;
valuesIndex++;
placeholderPosition = findNextPlaceholder(sql, placeholderEnd);
}
if (chunkIndex === 0)
return sql;
if (chunkIndex < sql.length)
return result + sql.slice(chunkIndex);
return result;
};
exports.format = format;
const raw = (sql) => {
if (typeof sql !== 'string')
throw new TypeError('argument sql must be a string');
return {
toSqlString: () => sql,
};
};
exports.raw = raw;

305
node_modules/sql-escaper/lib/index.mjs generated vendored Normal file
View file

@ -0,0 +1,305 @@
import { Buffer } from "node:buffer";
const regex = {
backtick: /`/g,
dot: /\./g,
timezone: /([+\-\s])(\d\d):?(\d\d)?/,
escapeChars: /[\0\b\t\n\r\x1a"'\\]/g
};
const CHARS_ESCAPE_MAP = {
"\0": "\\0",
"\b": "\\b",
" ": "\\t",
"\n": "\\n",
"\r": "\\r",
"": "\\Z",
'"': '\\"',
"'": "\\'",
"\\": "\\\\"
};
const charCode = {
singleQuote: 39,
backtick: 96,
backslash: 92,
dash: 45,
slash: 47,
asterisk: 42,
questionMark: 63,
newline: 10,
space: 32,
tab: 9,
carriageReturn: 13
};
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
const isWordChar = (code) => code >= 65 && code <= 90 || code >= 97 && code <= 122 || code >= 48 && code <= 57 || code === 95;
const isWhitespace = (code) => code === charCode.space || code === charCode.tab || code === charCode.newline || code === charCode.carriageReturn;
const hasOnlyWhitespaceBetween = (sql, start, end) => {
if (start >= end) return true;
for (let i = start; i < end; i++) {
const code = sql.charCodeAt(i);
if (code !== charCode.space && code !== charCode.tab && code !== charCode.newline && code !== charCode.carriageReturn)
return false;
}
return true;
};
const toLower = (code) => code | 32;
const matchesWord = (sql, position, word, length) => {
for (let offset = 0; offset < word.length; offset++)
if (toLower(sql.charCodeAt(position + offset)) !== word.charCodeAt(offset))
return false;
return (position === 0 || !isWordChar(sql.charCodeAt(position - 1))) && (position + word.length >= length || !isWordChar(sql.charCodeAt(position + word.length)));
};
const skipSqlContext = (sql, position) => {
const currentChar = sql.charCodeAt(position);
const nextChar = sql.charCodeAt(position + 1);
if (currentChar === charCode.singleQuote) {
for (let cursor = position + 1; cursor < sql.length; cursor++) {
if (sql.charCodeAt(cursor) === charCode.backslash) cursor++;
else if (sql.charCodeAt(cursor) === charCode.singleQuote)
return cursor + 1;
}
return sql.length;
}
if (currentChar === charCode.backtick) {
const length = sql.length;
for (let cursor = position + 1; cursor < length; cursor++) {
if (sql.charCodeAt(cursor) !== charCode.backtick) continue;
if (sql.charCodeAt(cursor + 1) === charCode.backtick) {
cursor++;
continue;
}
return cursor + 1;
}
return length;
}
if (currentChar === charCode.dash && nextChar === charCode.dash) {
const lineBreak = sql.indexOf("\n", position + 2);
return lineBreak === -1 ? sql.length : lineBreak + 1;
}
if (currentChar === charCode.slash && nextChar === charCode.asterisk) {
const commentEnd = sql.indexOf("*/", position + 2);
return commentEnd === -1 ? sql.length : commentEnd + 2;
}
return -1;
};
const findNextPlaceholder = (sql, start) => {
const sqlLength = sql.length;
for (let position = start; position < sqlLength; position++) {
const code = sql.charCodeAt(position);
if (code === charCode.questionMark) return position;
if (code === charCode.singleQuote || code === charCode.backtick || code === charCode.dash || code === charCode.slash) {
const contextEnd = skipSqlContext(sql, position);
if (contextEnd !== -1) position = contextEnd - 1;
}
}
return -1;
};
const findSetKeyword = (sql, startFrom = 0) => {
const length = sql.length;
for (let position = startFrom; position < length; position++) {
const code = sql.charCodeAt(position);
const lower = code | 32;
if (code === charCode.singleQuote || code === charCode.backtick || code === charCode.dash || code === charCode.slash) {
const contextEnd = skipSqlContext(sql, position);
if (contextEnd !== -1) {
position = contextEnd - 1;
continue;
}
}
if (lower === 115 && matchesWord(sql, position, "set", length))
return position + 3;
if (lower === 107 && matchesWord(sql, position, "key", length)) {
let cursor = position + 3;
while (cursor < length && isWhitespace(sql.charCodeAt(cursor))) cursor++;
if (matchesWord(sql, cursor, "update", length)) return cursor + 6;
}
}
return -1;
};
const isDate = (value) => Object.prototype.toString.call(value) === "[object Date]";
const hasSqlString = (value) => typeof value === "object" && value !== null && "toSqlString" in value && typeof value.toSqlString === "function";
const escapeString = (value) => {
regex.escapeChars.lastIndex = 0;
let chunkIndex = 0;
let escapedValue = "";
let match;
for (match = regex.escapeChars.exec(value); match !== null; match = regex.escapeChars.exec(value)) {
escapedValue += value.slice(chunkIndex, match.index);
escapedValue += CHARS_ESCAPE_MAP[match[0]];
chunkIndex = regex.escapeChars.lastIndex;
}
if (chunkIndex === 0) return `'${value}'`;
if (chunkIndex < value.length)
return `'${escapedValue}${value.slice(chunkIndex)}'`;
return `'${escapedValue}'`;
};
const pad2 = (value) => value < 10 ? "0" + value : "" + value;
const pad3 = (value) => value < 10 ? "00" + value : value < 100 ? "0" + value : "" + value;
const pad4 = (value) => value < 10 ? "000" + value : value < 100 ? "00" + value : value < 1e3 ? "0" + value : "" + value;
const convertTimezone = (tz) => {
if (tz === "Z") return 0;
const timezoneMatch = tz.match(regex.timezone);
if (timezoneMatch)
return (timezoneMatch[1] === "-" ? -1 : 1) * (Number.parseInt(timezoneMatch[2], 10) + (timezoneMatch[3] ? Number.parseInt(timezoneMatch[3], 10) : 0) / 60) * 60;
return false;
};
const dateToString = (date, timezone) => {
if (Number.isNaN(date.getTime())) return "NULL";
let year;
let month;
let day;
let hour;
let minute;
let second;
let millisecond;
if (timezone === "local") {
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
hour = date.getHours();
minute = date.getMinutes();
second = date.getSeconds();
millisecond = date.getMilliseconds();
} else {
const timezoneOffsetMinutes = convertTimezone(timezone);
let time = date.getTime();
if (timezoneOffsetMinutes !== false && timezoneOffsetMinutes !== 0)
time += timezoneOffsetMinutes * 6e4;
const adjustedDate = new Date(time);
year = adjustedDate.getUTCFullYear();
month = adjustedDate.getUTCMonth() + 1;
day = adjustedDate.getUTCDate();
hour = adjustedDate.getUTCHours();
minute = adjustedDate.getUTCMinutes();
second = adjustedDate.getUTCSeconds();
millisecond = adjustedDate.getUTCMilliseconds();
}
return escapeString(
pad4(year) + "-" + pad2(month) + "-" + pad2(day) + " " + pad2(hour) + ":" + pad2(minute) + ":" + pad2(second) + "." + pad3(millisecond)
);
};
const escapeId = (value, forbidQualified) => {
if (Array.isArray(value)) {
const length = value.length;
const parts = new Array(length);
for (let i = 0; i < length; i++)
parts[i] = escapeId(value[i], forbidQualified);
return parts.join(", ");
}
const identifier = String(value);
const hasJsonOperator = identifier.indexOf("->") !== -1;
if (forbidQualified || hasJsonOperator) {
if (identifier.indexOf("`") === -1) return `\`${identifier}\``;
return `\`${identifier.replace(regex.backtick, "``")}\``;
}
if (identifier.indexOf("`") === -1 && identifier.indexOf(".") === -1)
return `\`${identifier}\``;
return `\`${identifier.replace(regex.backtick, "``").replace(regex.dot, "`.`")}\``;
};
const objectToValues = (object, timezone) => {
const keys = Object.keys(object);
const keysLength = keys.length;
if (keysLength === 0) return "";
let sql = "";
for (let i = 0; i < keysLength; i++) {
const key = keys[i];
const value = object[key];
if (typeof value === "function") continue;
if (sql.length > 0) sql += ", ";
sql += escapeId(key);
sql += " = ";
sql += escape(value, true, timezone);
}
return sql;
};
const bufferToString = (buffer) => `X${escapeString(buffer.toString("hex"))}`;
const arrayToList = (array, timezone) => {
const length = array.length;
const parts = new Array(length);
for (let i = 0; i < length; i++) {
const value = array[i];
if (Array.isArray(value)) parts[i] = `(${arrayToList(value, timezone)})`;
else parts[i] = escape(value, true, timezone);
}
return parts.join(", ");
};
const escape = (value, stringifyObjects, timezone) => {
if (value === void 0 || value === null) return "NULL";
switch (typeof value) {
case "boolean":
return value ? "true" : "false";
case "number":
case "bigint":
return value + "";
case "object": {
if (isDate(value)) return dateToString(value, timezone || "local");
if (Array.isArray(value)) return arrayToList(value, timezone);
if (Buffer.isBuffer(value)) return bufferToString(value);
if (value instanceof Uint8Array)
return bufferToString(Buffer.from(value));
if (hasSqlString(value)) return String(value.toSqlString());
if (!(stringifyObjects === void 0 || stringifyObjects === null))
return escapeString(String(value));
if (isRecord(value)) return objectToValues(value, timezone);
return escapeString(String(value));
}
case "string":
return escapeString(value);
default:
return escapeString(String(value));
}
};
const format = (sql, values, stringifyObjects, timezone) => {
if (values === void 0 || values === null) return sql;
const valuesArray = Array.isArray(values) ? values : [values];
const length = valuesArray.length;
let setIndex = -2;
let result = "";
let chunkIndex = 0;
let valuesIndex = 0;
let placeholderPosition = findNextPlaceholder(sql, 0);
while (valuesIndex < length && placeholderPosition !== -1) {
let placeholderEnd = placeholderPosition + 1;
let escapedValue;
while (sql.charCodeAt(placeholderEnd) === 63) placeholderEnd++;
const placeholderLength = placeholderEnd - placeholderPosition;
const currentValue = valuesArray[valuesIndex];
if (placeholderLength > 2) {
placeholderPosition = findNextPlaceholder(sql, placeholderEnd);
continue;
}
if (placeholderLength === 2) escapedValue = escapeId(currentValue);
else if (typeof currentValue === "number") escapedValue = `${currentValue}`;
else if (typeof currentValue === "object" && currentValue !== null && !stringifyObjects) {
if (setIndex === -2) setIndex = findSetKeyword(sql);
if (setIndex !== -1 && setIndex <= placeholderPosition && hasOnlyWhitespaceBetween(sql, setIndex, placeholderPosition) && !hasSqlString(currentValue) && !Array.isArray(currentValue) && !Buffer.isBuffer(currentValue) && !(currentValue instanceof Uint8Array) && !isDate(currentValue) && isRecord(currentValue)) {
escapedValue = objectToValues(currentValue, timezone);
setIndex = findSetKeyword(sql, placeholderEnd);
} else escapedValue = escape(currentValue, true, timezone);
} else escapedValue = escape(currentValue, stringifyObjects, timezone);
result += sql.slice(chunkIndex, placeholderPosition);
result += escapedValue;
chunkIndex = placeholderEnd;
valuesIndex++;
placeholderPosition = findNextPlaceholder(sql, placeholderEnd);
}
if (chunkIndex === 0) return sql;
if (chunkIndex < sql.length) return result + sql.slice(chunkIndex);
return result;
};
const raw = (sql) => {
if (typeof sql !== "string")
throw new TypeError("argument sql must be a string");
return {
toSqlString: () => sql
};
};
export {
arrayToList,
bufferToString,
dateToString,
escape,
escapeId,
format,
objectToValues,
raw
};

5
node_modules/sql-escaper/lib/types.d.ts generated vendored Normal file
View file

@ -0,0 +1,5 @@
export type Raw = {
toSqlString(): string;
};
export type SqlValue = string | number | bigint | boolean | Date | Buffer | Uint8Array | Raw | Record<string, unknown> | SqlValue[] | null | undefined;
export type Timezone = 'local' | 'Z' | (string & NonNullable<unknown>);

2
node_modules/sql-escaper/lib/types.js generated vendored Normal file
View file

@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

81
node_modules/sql-escaper/package.json generated vendored Normal file
View file

@ -0,0 +1,81 @@
{
"name": "sql-escaper",
"version": "1.3.3",
"description": "🛡️ Faster SQL escape and format for JavaScript (Node.js, Bun, and Deno).",
"main": "./lib/index.js",
"module": "./lib/index.mjs",
"types": "./lib/index.d.ts",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/mysqljs/sql-escaper.git"
},
"bugs": {
"url": "https://github.com/mysqljs/sql-escaper/issues"
},
"author": "https://github.com/mysqljs",
"funding": {
"type": "github",
"url": "https://github.com/mysqljs/sql-escaper?sponsor=1"
},
"files": [
"lib"
],
"engines": {
"node": ">=12.0.0",
"bun": ">=1.0.0",
"deno": ">=2.0.0"
},
"scripts": {
"benchmark": "cd benchmark && npm ci && bash index.sh",
"build:esm": "esbuild src/index.ts --outfile=lib/index.mjs --platform=node --target=node12 --format=esm",
"build": "rm -rf ./lib && tsc && npm run build:esm",
"test:node": "poku",
"test:bun": "bun poku",
"test:deno": "deno run -A npm:poku",
"test:coverage": "mcr --import tsx --config mcr.config.ts npm run test:node",
"lint": "biome lint && prettier --check .",
"lint:fix": "biome lint --write && prettier --write .github/workflows/*.yml .",
"update": "pu minor && npm i && (npm audit fix || true) && npm run lint:fix"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@ianvs/prettier-plugin-sort-imports": "^4.7.0",
"@types/node": "^25.2.0",
"esbuild": "^0.27.2",
"monocart-coverage-reports": "^2.12.9",
"packages-update": "^2.0.0",
"poku": "^3.0.3-canary.13a996a9",
"prettier": "^3.8.1",
"tsx": "^4.21.0",
"typescript": "^5.9.3"
},
"exports": {
".": {
"import": {
"types": "./lib/index.d.ts",
"default": "./lib/index.mjs"
},
"require": {
"types": "./lib/index.d.ts",
"default": "./lib/index.js"
}
}
},
"keywords": [
"sql",
"escape",
"format",
"sqlstring",
"sql-injection",
"sanitize",
"query",
"mysql",
"node",
"nodejs",
"bun",
"deno",
"typescript",
"fast"
]
}