miniflare
Advanced tools
Comparing version 3.20240620.0 to 3.20240701.0
@@ -19,2 +19,211 @@ var __defProp = Object.defineProperty; | ||
import { z } from "miniflare:zod"; | ||
// src/workers/d1/dumpSql.ts | ||
function* dumpSql(db, options) { | ||
yield "PRAGMA defer_foreign_keys=TRUE;"; | ||
let filterTables = new Set(options?.tables || []), { noData, noSchema } = options || {}, tables_cursor = db.prepare(` | ||
SELECT name, type, sql | ||
FROM sqlite_schema AS o | ||
WHERE (true) AND type=='table' | ||
AND sql NOT NULL | ||
ORDER BY tbl_name='sqlite_sequence', rowid; | ||
`)(), tables = Array.from(tables_cursor); | ||
for (let { name: table, sql } of tables) { | ||
if (filterTables.size > 0 && !filterTables.has(table)) | ||
continue; | ||
if (table === "sqlite_sequence") | ||
noSchema || (yield "DELETE FROM sqlite_sequence;"); | ||
else if (table.match(/^sqlite_stat./)) | ||
noSchema || (yield "ANALYZE sqlite_schema;"); | ||
else { | ||
if (sql.startsWith("CREATE VIRTUAL TABLE")) | ||
throw new Error( | ||
"D1 Export error: cannot export databases with Virtual Tables (fts5)" | ||
); | ||
if (table.startsWith("_cf_") || table.startsWith("sqlite_")) | ||
continue; | ||
sql.match(/CREATE TABLE ['"].*/) ? noSchema || (yield `CREATE TABLE IF NOT EXISTS ${sql.substring(13)};`) : noSchema || (yield `${sql};`); | ||
} | ||
if (noData) | ||
continue; | ||
let columns_cursor = db.exec(`PRAGMA table_info="${table}"`), columns = Array.from(columns_cursor), select = `SELECT ${columns.map((c) => c.name).join(", ")} | ||
FROM "${table}";`, rows_cursor = db.exec(select); | ||
for (let dataRow of rows_cursor.raw()) { | ||
let formattedCells = dataRow.map((cell, i) => { | ||
let colType = columns[i].type, cellType = typeof cell; | ||
return cell === null ? "NULL" : colType === "INTEGER" || cellType === "number" ? cell : colType === "TEXT" || cellType === "string" ? outputQuotedEscapedString(cell) : cell instanceof ArrayBuffer ? `X'${Array.prototype.map.call(new Uint8Array(cell), (b) => b.toString(16)).join("")}'` : (console.log({ colType, cellType, cell, column: columns[i] }), "ERROR"); | ||
}); | ||
yield `INSERT INTO ${sqliteQuote(table)} VALUES(${formattedCells.join(",")});`; | ||
} | ||
} | ||
if (!noSchema) { | ||
let rest_of_schema = db.exec( | ||
"SELECT name, sql FROM sqlite_schema AS o WHERE (true) AND sql NOT NULL AND type IN ('index', 'trigger', 'view') ORDER BY type COLLATE NOCASE /* DESC */;" | ||
); | ||
for (let { name, sql } of rest_of_schema) | ||
filterTables.size > 0 && !filterTables.has(name) || (yield `${sql};`); | ||
} | ||
} | ||
function outputQuotedEscapedString(cell) { | ||
let lfs = !1, crs = !1, quotesOrNewlinesRegexp = /'|(\n)|(\r)/g, escapeQuotesDetectingNewlines = (_, lf, cr) => lf ? (lfs = !0, "\\n") : cr ? (crs = !0, "\\r") : "''", output_string = `'${cell.replace( | ||
quotesOrNewlinesRegexp, | ||
escapeQuotesDetectingNewlines | ||
)}'`; | ||
return crs && (output_string = `replace(${output_string},'\\r',char(13))`), lfs && (output_string = `replace(${output_string},'\\n',char(10))`), output_string; | ||
} | ||
function sqliteQuote(token) { | ||
return token.length === 0 || // Doesn't start with alpha or underscore | ||
!token.match(/^[a-zA-Z_]/) || token.match(/\W/) || SQLITE_KEYWORDS.has(token.toUpperCase()) ? `"${token}"` : token; | ||
} | ||
var SQLITE_KEYWORDS = /* @__PURE__ */ new Set([ | ||
"ABORT", | ||
"ACTION", | ||
"ADD", | ||
"AFTER", | ||
"ALL", | ||
"ALTER", | ||
"ALWAYS", | ||
"ANALYZE", | ||
"AND", | ||
"AS", | ||
"ASC", | ||
"ATTACH", | ||
"AUTOINCREMENT", | ||
"BEFORE", | ||
"BEGIN", | ||
"BETWEEN", | ||
"BY", | ||
"CASCADE", | ||
"CASE", | ||
"CAST", | ||
"CHECK", | ||
"COLLATE", | ||
"COLUMN", | ||
"COMMIT", | ||
"CONFLICT", | ||
"CONSTRAINT", | ||
"CREATE", | ||
"CROSS", | ||
"CURRENT", | ||
"CURRENT_DATE", | ||
"CURRENT_TIME", | ||
"CURRENT_TIMESTAMP", | ||
"DATABASE", | ||
"DEFAULT", | ||
"DEFERRED", | ||
"DEFERRABLE", | ||
"DELETE", | ||
"DESC", | ||
"DETACH", | ||
"DISTINCT", | ||
"DO", | ||
"DROP", | ||
"END", | ||
"EACH", | ||
"ELSE", | ||
"ESCAPE", | ||
"EXCEPT", | ||
"EXCLUSIVE", | ||
"EXCLUDE", | ||
"EXISTS", | ||
"EXPLAIN", | ||
"FAIL", | ||
"FILTER", | ||
"FIRST", | ||
"FOLLOWING", | ||
"FOR", | ||
"FOREIGN", | ||
"FROM", | ||
"FULL", | ||
"GENERATED", | ||
"GLOB", | ||
"GROUP", | ||
"GROUPS", | ||
"HAVING", | ||
"IF", | ||
"IGNORE", | ||
"IMMEDIATE", | ||
"IN", | ||
"INDEX", | ||
"INDEXED", | ||
"INITIALLY", | ||
"INNER", | ||
"INSERT", | ||
"INSTEAD", | ||
"INTERSECT", | ||
"INTO", | ||
"IS", | ||
"ISNULL", | ||
"JOIN", | ||
"KEY", | ||
"LAST", | ||
"LEFT", | ||
"LIKE", | ||
"LIMIT", | ||
"MATCH", | ||
"MATERIALIZED", | ||
"NATURAL", | ||
"NO", | ||
"NOT", | ||
"NOTHING", | ||
"NOTNULL", | ||
"NULL", | ||
"NULLS", | ||
"OF", | ||
"OFFSET", | ||
"ON", | ||
"OR", | ||
"ORDER", | ||
"OTHERS", | ||
"OUTER", | ||
"OVER", | ||
"PARTITION", | ||
"PLAN", | ||
"PRAGMA", | ||
"PRECEDING", | ||
"PRIMARY", | ||
"QUERY", | ||
"RAISE", | ||
"RANGE", | ||
"RECURSIVE", | ||
"REFERENCES", | ||
"REGEXP", | ||
"REINDEX", | ||
"RELEASE", | ||
"RENAME", | ||
"REPLACE", | ||
"RESTRICT", | ||
"RETURNING", | ||
"RIGHT", | ||
"ROLLBACK", | ||
"ROW", | ||
"ROWS", | ||
"SAVEPOINT", | ||
"SELECT", | ||
"SET", | ||
"TABLE", | ||
"TEMP", | ||
"TEMPORARY", | ||
"THEN", | ||
"TIES", | ||
"TO", | ||
"TRANSACTION", | ||
"TRIGGER", | ||
"UNBOUNDED", | ||
"UNION", | ||
"UNIQUE", | ||
"UPDATE", | ||
"USING", | ||
"VACUUM", | ||
"VALUES", | ||
"VIEW", | ||
"VIRTUAL", | ||
"WHEN", | ||
"WHERE", | ||
"WINDOW", | ||
"WITH", | ||
"WITHOUT" | ||
]); | ||
// src/workers/d1/database.worker.ts | ||
var D1ValueSchema = z.union([ | ||
@@ -28,3 +237,3 @@ z.number(), | ||
params: z.array(D1ValueSchema).nullable().optional() | ||
}), D1QueriesSchema = z.union([D1QuerySchema, z.array(D1QuerySchema)]), D1ResultsFormatSchema = z.enum(["ARRAY_OF_OBJECTS", "ROWS_AND_COLUMNS", "NONE"]).catch("ARRAY_OF_OBJECTS"), served_by = "miniflare.db", D1Error = class extends HttpError { | ||
}), D1QueriesSchema = z.union([D1QuerySchema, z.array(D1QuerySchema)]), D1_EXPORT_PRAGMA = "PRAGMA miniflare_d1_export(?,?,?);", D1ResultsFormatSchema = z.enum(["ARRAY_OF_OBJECTS", "ROWS_AND_COLUMNS", "NONE"]).catch("ARRAY_OF_OBJECTS"), served_by = "miniflare.db", D1Error = class extends HttpError { | ||
constructor(cause) { | ||
@@ -112,3 +321,4 @@ super(500); | ||
let queries = D1QueriesSchema.parse(await req.json()); | ||
Array.isArray(queries) || (queries = [queries]); | ||
if (Array.isArray(queries) || (queries = [queries]), this.#isExportPragma(queries)) | ||
return this.#doExportData(queries); | ||
let { searchParams } = new URL(req.url), resultsFormat = D1ResultsFormatSchema.parse( | ||
@@ -119,2 +329,17 @@ searchParams.get("resultsFormat") | ||
}; | ||
#isExportPragma(queries) { | ||
return queries.length === 1 && queries[0].sql === D1_EXPORT_PRAGMA && (queries[0].params?.length || 0) >= 2; | ||
} | ||
#doExportData(queries) { | ||
let [noSchema, noData, ...tables] = queries[0].params, options = { | ||
noSchema: !!noSchema, | ||
noData: !!noData, | ||
tables | ||
}; | ||
return Response.json({ | ||
success: !0, | ||
results: [Array.from(dumpSql(this.state.storage.sql, options))], | ||
meta: {} | ||
}); | ||
} | ||
}; | ||
@@ -121,0 +346,0 @@ __decorateClass([ |
{ | ||
"name": "miniflare", | ||
"version": "3.20240620.0", | ||
"version": "3.20240701.0", | ||
"description": "Fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -41,4 +41,4 @@ "keywords": [ | ||
"undici": "^5.28.4", | ||
"workerd": "1.20240620.1", | ||
"ws": "^8.14.2", | ||
"workerd": "1.20240701.0", | ||
"ws": "^8.17.1", | ||
"youch": "^3.2.2", | ||
@@ -45,0 +45,0 @@ "zod": "^3.22.3" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1222065
20818
+ Added@cloudflare/workerd-darwin-64@1.20240701.0(transitive)
+ Added@cloudflare/workerd-darwin-arm64@1.20240701.0(transitive)
+ Added@cloudflare/workerd-linux-64@1.20240701.0(transitive)
+ Added@cloudflare/workerd-linux-arm64@1.20240701.0(transitive)
+ Added@cloudflare/workerd-windows-64@1.20240701.0(transitive)
+ Addedworkerd@1.20240701.0(transitive)
- Removed@cloudflare/workerd-darwin-64@1.20240620.1(transitive)
- Removed@cloudflare/workerd-darwin-arm64@1.20240620.1(transitive)
- Removed@cloudflare/workerd-linux-64@1.20240620.1(transitive)
- Removed@cloudflare/workerd-linux-arm64@1.20240620.1(transitive)
- Removed@cloudflare/workerd-windows-64@1.20240620.1(transitive)
- Removedworkerd@1.20240620.1(transitive)
Updatedworkerd@1.20240701.0
Updatedws@^8.17.1