Skip to content

Commit

Permalink
Merge branch 'main' into zack/fix-glob
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Apr 16, 2024
2 parents 4a96c74 + 3df202f commit a1c2bd5
Show file tree
Hide file tree
Showing 57 changed files with 1,590 additions and 734 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.22)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0067 NEW)

set(Bun_VERSION "1.1.3")
set(Bun_VERSION "1.1.4")
set(WEBKIT_TAG e3a2d89a0b1644cc8d5c245bd2ffee4d4bd6c1d5)

set(BUN_WORKDIR "${CMAKE_CURRENT_BINARY_DIR}")
Expand Down Expand Up @@ -555,6 +555,7 @@ else()
add_compile_definitions("BUN_DEBUG=1")
set(ASSERT_ENABLED "1")
endif()

message(STATUS "Using WebKit from ${WEBKIT_DIR}")
else()
if(NOT EXISTS "${WEBKIT_DIR}/lib/${libWTF}.${STATIC_LIB_EXT}" OR NOT EXISTS "${WEBKIT_DIR}/lib/${libJavaScriptCore}.${STATIC_LIB_EXT}")
Expand Down
54 changes: 49 additions & 5 deletions docs/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const db = new Database("mydb.sqlite", { create: true });
You can also use an import attribute to load a database.

```ts
import db from "./mydb.sqlite" with {"type": "sqlite"};
import db from "./mydb.sqlite" with { "type": "sqlite" };

console.log(db.query("select * from users LIMIT 1").get());
```
Expand All @@ -74,16 +74,39 @@ import { Database } from "bun:sqlite";
const db = new Database("./mydb.sqlite");
```

### `.close()`
### `.close(throwOnError: boolean = false)`

To close a database:
To close a database connection, but allow existing queries to finish, call `.close(false)`:

```ts
const db = new Database();
db.close();
// ... do stuff
db.close(false);
```

Note: `close()` is called automatically when the database is garbage collected. It is safe to call multiple times but has no effect after the first.
To close the database and throw an error if there are any pending queries, call `.close(true)`:

```ts
const db = new Database();
// ... do stuff
db.close(true);
```

Note: `close(false)` is called automatically when the database is garbage collected. It is safe to call multiple times but has no effect after the first.

### `using` statement

You can use the `using` statement to ensure that a database connection is closed when the `using` block is exited.

```ts
import { Database } from "bun:sqlite";

{
using db = new Database("mydb.sqlite");
using query = db.query("select 'Hello world' as message;");
console.log(query.get()); // => { message: "Hello world" }
}
```

### `.serialize()`

Expand Down Expand Up @@ -128,6 +151,8 @@ db.exec("PRAGMA journal_mode = WAL;");

{% details summary="What is WAL mode" %}
In WAL mode, writes to the database are written directly to a separate file called the "WAL file" (write-ahead log). This file will be later integrated into the main database file. Think of it as a buffer for pending writes. Refer to the [SQLite docs](https://www.sqlite.org/wal.html) for a more detailed overview.

On macOS, WAL files may be persistent by default. This is not a bug, it is how macOS configured the system version of SQLite.
{% /details %}

## Statements
Expand Down Expand Up @@ -387,6 +412,25 @@ db.loadExtension("myext");

{% /details %}

### .fileControl(cmd: number, value: any)

To use the advanced `sqlite3_file_control` API, call `.fileControl(cmd, value)` on your `Database` instance.

```ts
import { Database, constants } from "bun:sqlite";

const db = new Database();
// Ensure WAL mode is NOT persistent
// this prevents wal files from lingering after the database is closed
db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0);
```

`value` can be:

- `number`
- `TypedArray`
- `undefined` or `null`

## Reference

```ts
Expand Down
4 changes: 4 additions & 0 deletions packages/bun-internal-test/src/banned.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"@import(\"root\").bun.": "Only import 'bun' once",
"std.mem.indexOfAny": "Use bun.strings.indexAny or bun.strings.indexAnyComptime",
"std.debug.print": "Don't let this be committed",
" == undefined": "This is by definition Undefined Behavior.",
" != undefined": "This is by definition Undefined Behavior.",
"undefined == ": "This is by definition Undefined Behavior.",
"undefined != ": "This is by definition Undefined Behavior.",
"": ""
}
218 changes: 215 additions & 3 deletions packages/bun-types/sqlite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* | `null` | `NULL` |
*/
declare module "bun:sqlite" {
export class Database {
export class Database implements Disposable {
/**
* Open or create a SQLite3 database
*
Expand Down Expand Up @@ -257,7 +257,20 @@ declare module "bun:sqlite" {
*
* Internally, this calls `sqlite3_close_v2`.
*/
close(): void;
close(
/**
* If `true`, then the database will throw an error if it is in use
* @default false
*
* When true, this calls `sqlite3_close` instead of `sqlite3_close_v2`.
*
* Learn more about this in the [sqlite3 documentation](https://www.sqlite.org/c3ref/close.html).
*
* Bun will automatically call close by default when the database instance is garbage collected.
* In The future, Bun may default `throwOnError` to be true but for backwards compatibility, it is false by default.
*/
throwOnError?: boolean,
): void;

/**
* The filename passed when `new Database()` was called
Expand Down Expand Up @@ -304,6 +317,8 @@ declare module "bun:sqlite" {
*/
static setCustomSQLite(path: string): boolean;

[Symbol.dispose](): void;

/**
* Creates a function that always runs inside a transaction. When the
* function is invoked, it will begin a new transaction. When the function
Expand Down Expand Up @@ -427,6 +442,17 @@ declare module "bun:sqlite" {
* ```
*/
static deserialize(serialized: NodeJS.TypedArray | ArrayBufferLike, isReadOnly?: boolean): Database;

/**
* See `sqlite3_file_control` for more information.
* @link https://www.sqlite.org/c3ref/file_control.html
*/
fileControl(op: number, arg?: ArrayBufferView | number): number;
/**
* See `sqlite3_file_control` for more information.
* @link https://www.sqlite.org/c3ref/file_control.html
*/
fileControl(zDbName: string, op: number, arg?: ArrayBufferView | number): number;
}

/**
Expand Down Expand Up @@ -455,7 +481,7 @@ declare module "bun:sqlite" {
* // => undefined
* ```
*/
export class Statement<ReturnType = unknown, ParamsType extends SQLQueryBindings[] = any[]> {
export class Statement<ReturnType = unknown, ParamsType extends SQLQueryBindings[] = any[]> implements Disposable {
/**
* Creates a new prepared statement from native code.
*
Expand Down Expand Up @@ -633,6 +659,11 @@ declare module "bun:sqlite" {
*/
finalize(): void;

/**
* Calls {@link finalize} if it wasn't already called.
*/
[Symbol.dispose](): void;

/**
* Return the expanded SQL string for the prepared statement.
*
Expand Down Expand Up @@ -766,6 +797,187 @@ declare module "bun:sqlite" {
* @constant 0x04
*/
SQLITE_PREPARE_NO_VTAB: number;

/**
* @constant 1
*/
SQLITE_FCNTL_LOCKSTATE: number;
/**
* @constant 2
*/
SQLITE_FCNTL_GET_LOCKPROXYFILE: number;
/**
* @constant 3
*/
SQLITE_FCNTL_SET_LOCKPROXYFILE: number;
/**
* @constant 4
*/
SQLITE_FCNTL_LAST_ERRNO: number;
/**
* @constant 5
*/
SQLITE_FCNTL_SIZE_HINT: number;
/**
* @constant 6
*/
SQLITE_FCNTL_CHUNK_SIZE: number;
/**
* @constant 7
*/
SQLITE_FCNTL_FILE_POINTER: number;
/**
* @constant 8
*/
SQLITE_FCNTL_SYNC_OMITTED: number;
/**
* @constant 9
*/
SQLITE_FCNTL_WIN32_AV_RETRY: number;
/**
* @constant 10
*
* Control whether or not the WAL is persisted
* Some versions of macOS configure WAL to be persistent by default.
*
* You can change this with code like the below:
* ```ts
* import { Database } from "bun:sqlite";
*
* const db = Database.open("mydb.sqlite");
* db.fileControl(constants.SQLITE_FCNTL_PERSIST_WAL, 0);
* // enable WAL
* db.exec("PRAGMA journal_mode = WAL");
* // .. do some work
* db.close();
* ```
*
*/
SQLITE_FCNTL_PERSIST_WAL: number;
/**
* @constant 11
*/
SQLITE_FCNTL_OVERWRITE: number;
/**
* @constant 12
*/
SQLITE_FCNTL_VFSNAME: number;
/**
* @constant 13
*/
SQLITE_FCNTL_POWERSAFE_OVERWRITE: number;
/**
* @constant 14
*/
SQLITE_FCNTL_PRAGMA: number;
/**
* @constant 15
*/
SQLITE_FCNTL_BUSYHANDLER: number;
/**
* @constant 16
*/
SQLITE_FCNTL_TEMPFILENAME: number;
/**
* @constant 18
*/
SQLITE_FCNTL_MMAP_SIZE: number;
/**
* @constant 19
*/
SQLITE_FCNTL_TRACE: number;
/**
* @constant 20
*/
SQLITE_FCNTL_HAS_MOVED: number;
/**
* @constant 21
*/
SQLITE_FCNTL_SYNC: number;
/**
* @constant 22
*/
SQLITE_FCNTL_COMMIT_PHASETWO: number;
/**
* @constant 23
*/
SQLITE_FCNTL_WIN32_SET_HANDLE: number;
/**
* @constant 24
*/
SQLITE_FCNTL_WAL_BLOCK: number;
/**
* @constant 25
*/
SQLITE_FCNTL_ZIPVFS: number;
/**
* @constant 26
*/
SQLITE_FCNTL_RBU: number;
/**
* @constant 27
*/
SQLITE_FCNTL_VFS_POINTER: number;
/**
* @constant 28
*/
SQLITE_FCNTL_JOURNAL_POINTER: number;
/**
* @constant 29
*/
SQLITE_FCNTL_WIN32_GET_HANDLE: number;
/**
* @constant 30
*/
SQLITE_FCNTL_PDB: number;
/**
* @constant 31
*/
SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: number;
/**
* @constant 32
*/
SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: number;
/**
* @constant 33
*/
SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: number;
/**
* @constant 34
*/
SQLITE_FCNTL_LOCK_TIMEOUT: number;
/**
* @constant 35
*/
SQLITE_FCNTL_DATA_VERSION: number;
/**
* @constant 36
*/
SQLITE_FCNTL_SIZE_LIMIT: number;
/**
* @constant 37
*/
SQLITE_FCNTL_CKPT_DONE: number;
/**
* @constant 38
*/
SQLITE_FCNTL_RESERVE_BYTES: number;
/**
* @constant 39
*/
SQLITE_FCNTL_CKPT_START: number;
/**
* @constant 40
*/
SQLITE_FCNTL_EXTERNAL_READER: number;
/**
* @constant 41
*/
SQLITE_FCNTL_CKSM_FILE: number;
/**
* @constant 42
*/
SQLITE_FCNTL_RESET_CACHE: number;
};

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/bun-usockets/src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ void us_internal_on_ssl_handshake(
struct us_internal_ssl_socket_t *
us_internal_ssl_socket_close(struct us_internal_ssl_socket_t *s, int code,
void *reason) {
struct us_internal_ssl_socket_context_t *context =
(struct us_internal_ssl_socket_context_t *)us_socket_context(0, &s->s);

if (s->handshake_state != HANDSHAKE_COMPLETED) {
// if we have some pending handshake we cancel it and try to check the
Expand Down
Loading

0 comments on commit a1c2bd5

Please sign in to comment.