Skip to content

Commit

Permalink
Merge branch 'main' into don/feat/bunx-no-install
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Jan 11, 2025
2 parents 0d03265 + b04ce67 commit eed6da0
Show file tree
Hide file tree
Showing 19 changed files with 1,120 additions and 53 deletions.
18 changes: 3 additions & 15 deletions docs/guides/write-file/unlink.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@
name: Delete a file
---

To synchronously delete a file with Bun, use the `unlinkSync` function from the [`node:fs`](https://nodejs.org/api/fs.html#fs_fs_unlink_path_callback) module. (Currently, there is no `Bun` API for deleting files.)
To delete a file in Bun, use the `delete` method.

```ts
import { unlinkSync } from "node:fs";
import { file } from "bun";

const path = "/path/to/file.txt";
unlinkSync(path);
```

---

To remove a file asynchronously, use the `unlink` function from the [`node:fs/promises`](https://nodejs.org/api/fs.html#fs_fspromises_unlink_path) module.

```ts
import { unlink } from "node:fs/promises";

const path = "/path/to/file.txt";
await unlink(path);
await file("./path-to-file.txt").delete();
```
10 changes: 10 additions & 0 deletions packages/bun-types/bun.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,16 @@ declare module "bun" {
* Deletes the file.
*/
unlink(): Promise<void>;

/**
* Deletes the file. ( same as unlink )
*/
delete(): Promise<void>

/**
* Provides useful information about the file.
*/
stat(): Promise<Stats>
}
interface NetworkSink extends FileSink {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/bun-types/html-rewriter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ declare namespace HTMLRewriterTypes {

interface Element {
tagName: string;
readonly attributes: IterableIterator<string[]>;
readonly attributes: IterableIterator<[string, string]>;
readonly removed: boolean;
/** Whether the element is explicitly self-closing, e.g. `<foo />` */
readonly selfClosing: boolean;
Expand Down
8 changes: 6 additions & 2 deletions src/bun.js/bindings/JSPropertyIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ extern "C" JSPropertyIterator* Bun__JSPropertyIterator__create(JSC::JSGlobalObje
auto scope = DECLARE_THROW_SCOPE(vm);
JSC::PropertyNameArray array(vm, PropertyNameMode::StringsAndSymbols, PrivateSymbolMode::Exclude);

if (UNLIKELY(object->hasNonReifiedStaticProperties())) {
object->reifyAllStaticProperties(globalObject);
}

#if OS(WINDOWS)
if (UNLIKELY(object->type() == JSC::ProxyObjectType)) {
// Check if we're actually iterating through the JSEnvironmentVariableMap's proxy.
Expand Down Expand Up @@ -75,7 +79,7 @@ extern "C" JSPropertyIterator* Bun__JSPropertyIterator__create(JSC::JSGlobalObje
if (only_non_index_properties) {
object->getOwnNonIndexPropertyNames(globalObject, array, DontEnumPropertiesMode::Exclude);
} else {
object->getOwnPropertyNames(object, globalObject, array, DontEnumPropertiesMode::Exclude);
object->methodTable()->getOwnPropertyNames(object, globalObject, array, DontEnumPropertiesMode::Exclude);
}
} else {
object->getPropertyNames(globalObject, array, DontEnumPropertiesMode::Exclude);
Expand Down Expand Up @@ -161,7 +165,7 @@ extern "C" EncodedJSValue Bun__JSPropertyIterator__getNameAndValue(JSPropertyIte
auto& vm = iter->vm;
auto scope = DECLARE_THROW_SCOPE(vm);
PropertySlot slot(object, PropertySlot::InternalMethodType::GetOwnProperty);
if (!object->getOwnPropertySlot(object, globalObject, prop, slot)) {
if (!object->methodTable()->getOwnPropertySlot(object, globalObject, prop, slot)) {
return {};
}
RETURN_IF_EXCEPTION(scope, {});
Expand Down
16 changes: 10 additions & 6 deletions src/js/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2607,14 +2607,18 @@ function getStringWidth(str, removeControlChars = true) {
}

// Regex used for ansi escape code splitting
// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
// Ref: https://github.com/chalk/ansi-regex/blob/f338e1814144efb950276aac84135ff86b72dc8e/index.js
// License: MIT by Sindre Sorhus <[email protected]>
// Matches all ansi escape code sequences in a string
const ansiPattern =
const ansiPattern = new RegExp(
"[\\u001B\\u009B][[\\]()#;?]*" +
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*" +
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" +
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))";
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]+)*" +
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]*)*)?" +
"(?:\\u0007|\\u001B\\u005C|\\u009C))" +
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?" +
"[\\dA-PR-TZcf-nq-uy=><~]))",
"g",
);
const ansi = new RegExp(ansiPattern, "g");
/** Remove all VT control characters. Use to estimate displayed string width. */
function stripVTControlCharacters(str) {
Expand Down
72 changes: 49 additions & 23 deletions src/js/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ const formatWithOptions = utl.formatWithOptions;
const format = utl.format;
const stripVTControlCharacters = utl.stripVTControlCharacters;

const codesWarned = new Set();
function deprecate(fn, msg, code) {
if (process.noDeprecation === true) {
return fn;
}
if (code !== undefined) validateString(code, "code");

var warned = false;
function deprecated() {
Expand All @@ -46,7 +48,15 @@ function deprecate(fn, msg, code) {
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
if (code !== undefined) {
// only warn for each code once
if (codesWarned.has(code)) {
process.emitWarning(msg, "DeprecationWarning", code);
}
codesWarned.add(code);
} else {
process.emitWarning(msg, "DeprecationWarning");
}
}
warned = true;
}
Expand Down Expand Up @@ -149,7 +159,13 @@ var inherits = function inherits(ctor, superCtor) {
if (superCtor.prototype === undefined) {
throw $ERR_INVALID_ARG_TYPE("superCtor.prototype", "object", superCtor.prototype);
}
ctor.super_ = superCtor;
Object.defineProperty(ctor, "super_", {
// @ts-ignore
__proto__: null,
value: superCtor,
writable: true,
configurable: true,
});
Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
};
var _extend = function (origin, add) {
Expand All @@ -172,30 +188,40 @@ function callbackifyOnRejected(reason, cb) {
return cb(reason);
}
function callbackify(original) {
if (typeof original !== "function") {
throw new TypeError('The "original" argument must be of type Function');
}
function callbackified() {
var args = Array.prototype.slice.$call(arguments);
var maybeCb = args.pop();
if (typeof maybeCb !== "function") {
throw new TypeError("The last argument must be of type Function");
}
var self = this;
var cb = function () {
return maybeCb.$apply(self, arguments);
};
const { validateFunction } = require("internal/validators");
validateFunction(original, "original");

// We DO NOT return the promise as it gives the user a false sense that
// the promise is actually somehow related to the callback's execution
// and that the callback throwing will reject the promise.
function callbackified(...args) {
const maybeCb = Array.prototype.pop.$call(args);
validateFunction(maybeCb, "last argument");
const cb = Function.prototype.bind.$call(maybeCb, this);
// In true node style we process the callback on `nextTick` with all the
// implications (stack, `uncaughtException`, `async_hooks`)
original.$apply(this, args).then(
function (ret) {
process.nextTick(cb, null, ret);
},
function (rej) {
process.nextTick(callbackifyOnRejected, rej, cb);
},
ret => process.nextTick(cb, null, ret),
rej => process.nextTick(callbackifyOnRejected, rej, cb),
);
}
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original));
Object.defineProperties(callbackified, getOwnPropertyDescriptors(original));

const descriptors = Object.getOwnPropertyDescriptors(original);
// It is possible to manipulate a functions `length` or `name` property. This
// guards against the manipulation.
if (typeof descriptors.length.value === "number") {
descriptors.length.value++;
}
if (typeof descriptors.name.value === "string") {
descriptors.name.value += "Callbackified";
}
const propertiesValues = Object.values(descriptors);
for (let i = 0; i < propertiesValues.length; i++) {
// We want to use null-prototype objects to not rely on globally mutable
// %Object.prototype%.
Object.setPrototypeOf(propertiesValues[i], null);
}
Object.defineProperties(callbackified, descriptors);
return callbackified;
}
var toUSVString = input => {
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
Loading

0 comments on commit eed6da0

Please sign in to comment.