Files
bun.sh/src/javascript/jsc/module.exports.js
Jarred Sumner a37f86e89d bun:sqlite (#167)
* ✂️

* Add the slow version

* draw the rest of the owl

* Fix crash when allocating lots of memory

* [Bun.Transipiler] Support passing objects

* [JS Parser] Support passing objects to macros via Bun.Transpiler

* Update JSSQLStatement.cpp

* Embed SQLite

* Add SQLite to Dockerfile

* [sqlite] Add quick one-off queries without creating a whole object

* [sqlite] Add `columnsCount`, rename raw() to `values()`, remove `rebind`

* Implement `bun:sqlite`

* return null

* Fix updating query

* Update bun.d.ts

* more tests

* Support variadic arguments, write tests and add types

* Update sqlite.d.ts

* Update sqlite.d.ts

* latest

* Implement `Database.loadExtension` and `Database.setCustomSQLite`

* Support `require.resolve`

* [napi] Improve string performance

* [bun.js] Support some of `node:module`

* another test

* [sqlite] Support serialize & deserialize

* [`bun:ffi`] Implement `CFunction` and `linkSymbols`

* [bun.js] Fix crash in `Buffer.from`

* Update sqlite.test.js

* Document linkSymbols

* docs

* Update README.md
2022-05-16 15:46:20 -07:00

105 lines
2.4 KiB
JavaScript

function resolve(request, args) {
if (typeof args === "object" && args?.paths?.length) {
return this.resolveSync(request, args);
}
return this.resolveSync(request);
}
// not implemented
resolve.paths = () => [];
function require(pathString) {
// this refers to an ImportMeta instance
const resolved = this.resolveSync(pathString);
if (
!resolved.endsWith(".node") &&
!resolved.endsWith(".json") &&
!resolved.endsWith(".toml")
) {
throw new Error(
"Dynamic require() in Bun.js currently only supports .node, .json, and .toml files.\n\tConsider using ESM import() instead."
);
}
return this.require(resolved);
}
const main = {
get() {
return Bun.main;
},
set() {
return false;
},
configurable: false,
};
export function createRequire(filename) {
var filenameString = filename;
const isURL =
typeof filename === "object" && filename && filename instanceof URL;
if (isURL) {
filenameString = filename.pathname;
}
var lastSlash = filenameString.lastIndexOf(
// TODO: WINDOWS
// windows is more complicated here
// but we don't support windows yet
process.platform !== "win32" ? "/" : "\\"
);
var customImportMeta = {
...import.meta,
path: filenameString,
file:
lastSlash > -1 ? filenameString.substring(lastSlash + 1) : filenameString,
dir: lastSlash > -1 ? filenameString.substring(0, lastSlash) : "",
};
if (isURL) {
customImportMeta.url = filename;
} else {
// lazy because URL is slow and also can throw
Object.defineProperty(customImportMeta, "url", {
get() {
const value = new URL("file://" + customImportMeta.path).href;
Object.defineProperty(customImportMeta, "url", {
value,
});
return value;
},
configurable: true,
});
}
var bound = require.bind(customImportMeta);
bound.resolve = resolve.bind(customImportMeta);
// do this one lazily
Object.defineProperty(bound, "main", main);
return bound;
}
// this isn't exhaustive
export const builtinModules = ["node:path", "node:fs", "bun:ffi", "bun:sqlite"];
// noop
export function syncBuiltinESMExports() {}
export function findSourceMap(path) {
throw new Error("findSourceMap is not implemented");
}
export function SourceMap() {
throw new Error("SourceMap is not implemented");
}
export default {
createRequire,
syncBuiltinESMExports,
findSourceMap,
SourceMap,
};