bun-types 1.3.2-canary.20251106T140813 → 1.3.2-canary.20251108T140624
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hashing
|
|
3
|
+
description: Bun provides a set of utility functions for hashing and verifying passwords with various cryptographically secure algorithms
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
<Note>
|
|
7
|
+
Bun implements the `createHash` and `createHmac` functions from [`node:crypto`](https://nodejs.org/api/crypto.html) in
|
|
8
|
+
addition to the Bun-native APIs documented below.
|
|
9
|
+
</Note>
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## `Bun.password`
|
|
14
|
+
|
|
15
|
+
`Bun.password` is a collection of utility functions for hashing and verifying passwords with various cryptographically secure algorithms.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const password = "super-secure-pa$$word";
|
|
19
|
+
|
|
20
|
+
const hash = await Bun.password.hash(password);
|
|
21
|
+
// => $argon2id$v=19$m=65536,t=2,p=1$tFq+9AVr1bfPxQdh6E8DQRhEXg/M/SqYCNu6gVdRRNs$GzJ8PuBi+K+BVojzPfS5mjnC8OpLGtv8KJqF99eP6a4
|
|
22
|
+
|
|
23
|
+
const isMatch = await Bun.password.verify(password, hash);
|
|
24
|
+
// => true
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The second argument to `Bun.password.hash` accepts a params object that lets you pick and configure the hashing algorithm.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const password = "super-secure-pa$$word";
|
|
31
|
+
|
|
32
|
+
// use argon2 (default)
|
|
33
|
+
const argonHash = await Bun.password.hash(password, {
|
|
34
|
+
algorithm: "argon2id", // "argon2id" | "argon2i" | "argon2d"
|
|
35
|
+
memoryCost: 4, // memory usage in kibibytes
|
|
36
|
+
timeCost: 3, // the number of iterations
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// use bcrypt
|
|
40
|
+
const bcryptHash = await Bun.password.hash(password, {
|
|
41
|
+
algorithm: "bcrypt",
|
|
42
|
+
cost: 4, // number between 4-31
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The algorithm used to create the hash is stored in the hash itself. When using `bcrypt`, the returned hash is encoded in [Modular Crypt Format](https://passlib.readthedocs.io/en/stable/modular_crypt_format.html) for compatibility with most existing `bcrypt` implementations; with `argon2` the result is encoded in the newer [PHC format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md).
|
|
47
|
+
|
|
48
|
+
The `verify` function automatically detects the algorithm based on the input hash and use the correct verification method. It can correctly infer the algorithm from both PHC- or MCF-encoded hashes.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const password = "super-secure-pa$$word";
|
|
52
|
+
|
|
53
|
+
const hash = await Bun.password.hash(password, {
|
|
54
|
+
/* config */
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const isMatch = await Bun.password.verify(password, hash);
|
|
58
|
+
// => true
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Synchronous versions of all functions are also available. Keep in mind that these functions are computationally expensive, so using a blocking API may degrade application performance.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const password = "super-secure-pa$$word";
|
|
65
|
+
|
|
66
|
+
const hash = Bun.password.hashSync(password, {
|
|
67
|
+
/* config */
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const isMatch = Bun.password.verifySync(password, hash);
|
|
71
|
+
// => true
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Salt
|
|
75
|
+
|
|
76
|
+
When you use `Bun.password.hash`, a salt is automatically generated and included in the hash.
|
|
77
|
+
|
|
78
|
+
### bcrypt - Modular Crypt Format
|
|
79
|
+
|
|
80
|
+
In the following [Modular Crypt Format](https://passlib.readthedocs.io/en/stable/modular_crypt_format.html) hash (used by `bcrypt`):
|
|
81
|
+
|
|
82
|
+
Input:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
await Bun.password.hash("hello", {
|
|
86
|
+
algorithm: "bcrypt",
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Output:
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
$2b$10$Lyj9kHYZtiyfxh2G60TEfeqs7xkkGiEFFDi3iJGc50ZG/XJ1sxIFi;
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The format is composed of:
|
|
97
|
+
|
|
98
|
+
- `bcrypt`: `$2b`
|
|
99
|
+
- `rounds`: `$10` - rounds (log10 of the actual number of rounds)
|
|
100
|
+
- `salt`: `$Lyj9kHYZtiyfxh2G60TEfeqs7xkkGiEFFDi3iJGc50ZG/XJ1sxIFi`
|
|
101
|
+
- `hash`: `$GzJ8PuBi+K+BVojzPfS5mjnC8OpLGtv8KJqF99eP6a4`
|
|
102
|
+
|
|
103
|
+
By default, the bcrypt library truncates passwords longer than 72 bytes. In Bun, if you pass `Bun.password.hash` a password longer than 72 bytes and use the `bcrypt` algorithm, the password will be hashed via SHA-512 before being passed to bcrypt.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
await Bun.password.hash("hello".repeat(100), {
|
|
107
|
+
algorithm: "bcrypt",
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
So instead of sending bcrypt a 500-byte password silently truncated to 72 bytes, Bun will hash the password using SHA-512 and send the hashed password to bcrypt (only if it exceeds 72 bytes). This is a more secure default behavior.
|
|
112
|
+
|
|
113
|
+
### argon2 - PHC format
|
|
114
|
+
|
|
115
|
+
In the following [PHC format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md) hash (used by `argon2`):
|
|
116
|
+
|
|
117
|
+
Input:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
await Bun.password.hash("hello", {
|
|
121
|
+
algorithm: "argon2id",
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Output:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
$argon2id$v=19$m=65536,t=2,p=1$xXnlSvPh4ym5KYmxKAuuHVlDvy2QGHBNuI6bJJrRDOs$2YY6M48XmHn+s5NoBaL+ficzXajq2Yj8wut3r0vnrwI
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The format is composed of:
|
|
132
|
+
|
|
133
|
+
- `algorithm`: `$argon2id`
|
|
134
|
+
- `version`: `$v=19`
|
|
135
|
+
- `memory cost`: `65536`
|
|
136
|
+
- `iterations`: `t=2`
|
|
137
|
+
- `parallelism`: `p=1`
|
|
138
|
+
- `salt`: `$xXnlSvPh4ym5KYmxKAuuHVlDvy2QGHBNuI6bJJrRDOs`
|
|
139
|
+
- `hash`: `$2YY6M48XmHn+s5NoBaL+ficzXajq2Yj8wut3r0vnrwI`
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## `Bun.hash`
|
|
144
|
+
|
|
145
|
+
`Bun.hash` is a collection of utilities for _non-cryptographic_ hashing. Non-cryptographic hashing algorithms are optimized for speed of computation over collision-resistance or security.
|
|
146
|
+
|
|
147
|
+
The standard `Bun.hash` functions uses [Wyhash](https://github.com/wangyi-fudan/wyhash) to generate a 64-bit hash from an input of arbitrary size.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
Bun.hash("some data here");
|
|
151
|
+
// 11562320457524636935n
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
The input can be a string, `TypedArray`, `DataView`, `ArrayBuffer`, or `SharedArrayBuffer`.
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const arr = new Uint8Array([1, 2, 3, 4]);
|
|
158
|
+
|
|
159
|
+
Bun.hash("some data here");
|
|
160
|
+
Bun.hash(arr);
|
|
161
|
+
Bun.hash(arr.buffer);
|
|
162
|
+
Bun.hash(new DataView(arr.buffer));
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Optionally, an integer seed can be specified as the second parameter. For 64-bit hashes seeds above `Number.MAX_SAFE_INTEGER` should be given as BigInt to avoid loss of precision.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
Bun.hash("some data here", 1234);
|
|
169
|
+
// 15724820720172937558n
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Additional hashing algorithms are available as properties on `Bun.hash`. The API is the same for each, only changing the return type from number for 32-bit hashes to bigint for 64-bit hashes.
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
Bun.hash.wyhash("data", 1234); // equivalent to Bun.hash()
|
|
176
|
+
Bun.hash.crc32("data", 1234);
|
|
177
|
+
Bun.hash.adler32("data", 1234);
|
|
178
|
+
Bun.hash.cityHash32("data", 1234);
|
|
179
|
+
Bun.hash.cityHash64("data", 1234);
|
|
180
|
+
Bun.hash.xxHash32("data", 1234);
|
|
181
|
+
Bun.hash.xxHash64("data", 1234);
|
|
182
|
+
Bun.hash.xxHash3("data", 1234);
|
|
183
|
+
Bun.hash.murmur32v3("data", 1234);
|
|
184
|
+
Bun.hash.murmur32v2("data", 1234);
|
|
185
|
+
Bun.hash.murmur64v2("data", 1234);
|
|
186
|
+
Bun.hash.rapidhash("data", 1234);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## `Bun.CryptoHasher`
|
|
192
|
+
|
|
193
|
+
`Bun.CryptoHasher` is a general-purpose utility class that lets you incrementally compute a hash of string or binary data using a range of cryptographic hash algorithms. The following algorithms are supported:
|
|
194
|
+
|
|
195
|
+
- `"blake2b256"`
|
|
196
|
+
- `"blake2b512"`
|
|
197
|
+
- `"md4"`
|
|
198
|
+
- `"md5"`
|
|
199
|
+
- `"ripemd160"`
|
|
200
|
+
- `"sha1"`
|
|
201
|
+
- `"sha224"`
|
|
202
|
+
- `"sha256"`
|
|
203
|
+
- `"sha384"`
|
|
204
|
+
- `"sha512"`
|
|
205
|
+
- `"sha512-224"`
|
|
206
|
+
- `"sha512-256"`
|
|
207
|
+
- `"sha3-224"`
|
|
208
|
+
- `"sha3-256"`
|
|
209
|
+
- `"sha3-384"`
|
|
210
|
+
- `"sha3-512"`
|
|
211
|
+
- `"shake128"`
|
|
212
|
+
- `"shake256"`
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
216
|
+
hasher.update("hello world");
|
|
217
|
+
hasher.digest();
|
|
218
|
+
// Uint8Array(32) [ <byte>, <byte>, ... ]
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Once initialized, data can be incrementally fed to to the hasher using `.update()`. This method accepts `string`, `TypedArray`, and `ArrayBuffer`.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
225
|
+
|
|
226
|
+
hasher.update("hello world");
|
|
227
|
+
hasher.update(new Uint8Array([1, 2, 3]));
|
|
228
|
+
hasher.update(new ArrayBuffer(10));
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
If a `string` is passed, an optional second parameter can be used to specify the encoding (default `'utf-8'`). The following encodings are supported:
|
|
232
|
+
|
|
233
|
+
| Category | Encodings |
|
|
234
|
+
| -------------------------- | ------------------------------------------- |
|
|
235
|
+
| Binary encodings | `"base64"` `"base64url"` `"hex"` `"binary"` |
|
|
236
|
+
| Character encodings | `"utf8"` `"utf-8"` `"utf16le"` `"latin1"` |
|
|
237
|
+
| Legacy character encodings | `"ascii"` `"binary"` `"ucs2"` `"ucs-2"` |
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
hasher.update("hello world"); // defaults to utf8
|
|
241
|
+
hasher.update("hello world", "hex");
|
|
242
|
+
hasher.update("hello world", "base64");
|
|
243
|
+
hasher.update("hello world", "latin1");
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
After the data has been feed into the hasher, a final hash can be computed using `.digest()`. By default, this method returns a `Uint8Array` containing the hash.
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
250
|
+
hasher.update("hello world");
|
|
251
|
+
|
|
252
|
+
hasher.digest();
|
|
253
|
+
// => Uint8Array(32) [ 185, 77, 39, 185, 147, ... ]
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The `.digest()` method can optionally return the hash as a string. To do so, specify an encoding:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
hasher.digest("base64");
|
|
260
|
+
// => "uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="
|
|
261
|
+
|
|
262
|
+
hasher.digest("hex");
|
|
263
|
+
// => "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Alternatively, the method can write the hash into a pre-existing `TypedArray` instance. This may be desirable in some performance-sensitive applications.
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
const arr = new Uint8Array(32);
|
|
270
|
+
|
|
271
|
+
hasher.digest(arr);
|
|
272
|
+
|
|
273
|
+
console.log(arr);
|
|
274
|
+
// => Uint8Array(32) [ 185, 77, 39, 185, 147, ... ]
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### HMAC in `Bun.CryptoHasher`
|
|
278
|
+
|
|
279
|
+
`Bun.CryptoHasher` can be used to compute HMAC digests. To do so, pass the key to the constructor.
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
const hasher = new Bun.CryptoHasher("sha256", "secret-key");
|
|
283
|
+
hasher.update("hello world");
|
|
284
|
+
console.log(hasher.digest("hex"));
|
|
285
|
+
// => "095d5a21fe6d0646db223fdf3de6436bb8dfb2fab0b51677ecf6441fcf5f2a67"
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
When using HMAC, a more limited set of algorithms are supported:
|
|
289
|
+
|
|
290
|
+
- `"blake2b512"`
|
|
291
|
+
- `"md5"`
|
|
292
|
+
- `"sha1"`
|
|
293
|
+
- `"sha224"`
|
|
294
|
+
- `"sha256"`
|
|
295
|
+
- `"sha384"`
|
|
296
|
+
- `"sha512-224"`
|
|
297
|
+
- `"sha512-256"`
|
|
298
|
+
- `"sha512"`
|
|
299
|
+
|
|
300
|
+
Unlike the non-HMAC `Bun.CryptoHasher`, the HMAC `Bun.CryptoHasher` instance is not reset after `.digest()` is called, and attempting to use the same instance again will throw an error.
|
|
301
|
+
|
|
302
|
+
Other methods like `.copy()` and `.update()` are supported (as long as it's before `.digest()`), but methods like `.digest()` that finalize the hasher are not.
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
const hasher = new Bun.CryptoHasher("sha256", "secret-key");
|
|
306
|
+
hasher.update("hello world");
|
|
307
|
+
|
|
308
|
+
const copy = hasher.copy();
|
|
309
|
+
copy.update("!");
|
|
310
|
+
console.log(copy.digest("hex"));
|
|
311
|
+
// => "3840176c3d8923f59ac402b7550404b28ab11cb0ef1fa199130a5c37864b5497"
|
|
312
|
+
|
|
313
|
+
console.log(hasher.digest("hex"));
|
|
314
|
+
// => "095d5a21fe6d0646db223fdf3de6436bb8dfb2fab0b51677ecf6441fcf5f2a67"
|
|
315
|
+
```
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: HTMLRewriter
|
|
3
|
+
description: Use Bun's HTMLRewriter to transform HTML documents with CSS selectors
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
HTMLRewriter lets you use CSS selectors to transform HTML documents. It works with `Request`, `Response`, as well as `string`. Bun's implementation is based on Cloudflare's [lol-html](https://github.com/cloudflare/lol-html).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
A common usecase is rewriting URLs in HTML content. Here's an example that rewrites image sources and link URLs to use a CDN domain:
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// Replace all images with a rickroll
|
|
16
|
+
const rewriter = new HTMLRewriter().on("img", {
|
|
17
|
+
element(img) {
|
|
18
|
+
// Famous rickroll video thumbnail
|
|
19
|
+
img.setAttribute("src", "https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg");
|
|
20
|
+
|
|
21
|
+
// Wrap the image in a link to the video
|
|
22
|
+
img.before('<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank">', {
|
|
23
|
+
html: true,
|
|
24
|
+
});
|
|
25
|
+
img.after("</a>", { html: true });
|
|
26
|
+
|
|
27
|
+
// Add some fun alt text
|
|
28
|
+
img.setAttribute("alt", "Definitely not a rickroll");
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// An example HTML document
|
|
33
|
+
const html = `
|
|
34
|
+
<html>
|
|
35
|
+
<body>
|
|
36
|
+
<img src="/cat.jpg">
|
|
37
|
+
<img src="dog.png">
|
|
38
|
+
<img src="https://example.com/bird.webp">
|
|
39
|
+
</body>
|
|
40
|
+
</html>
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
const result = rewriter.transform(html);
|
|
44
|
+
console.log(result);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
This replaces all images with a thumbnail of Rick Astley and wraps each `<img>` in a link, producing a diff like this:
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<html>
|
|
51
|
+
<body>
|
|
52
|
+
<img src="/cat.jpg" /> // [!code --] <img src="dog.png" /> // [!code --]
|
|
53
|
+
<img src="https://example.com/bird.webp" /> // [!code --]
|
|
54
|
+
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank">
|
|
55
|
+
// [!code ++]
|
|
56
|
+
<img src="https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg" alt="Definitely not a rickroll" />
|
|
57
|
+
// [!code ++]
|
|
58
|
+
</a>
|
|
59
|
+
// [!code ++]
|
|
60
|
+
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank">
|
|
61
|
+
// [!code ++]
|
|
62
|
+
<img src="https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg" alt="Definitely not a rickroll" />
|
|
63
|
+
// [!code ++]
|
|
64
|
+
</a>
|
|
65
|
+
// [!code ++]
|
|
66
|
+
<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" target="_blank">
|
|
67
|
+
// [!code ++]
|
|
68
|
+
<img src="https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg" alt="Definitely not a rickroll" />
|
|
69
|
+
// [!code ++]
|
|
70
|
+
</a>
|
|
71
|
+
// [!code ++]
|
|
72
|
+
</body>
|
|
73
|
+
</html>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Now every image on the page will be replaced with a thumbnail of Rick Astley, and clicking any image will lead to [a very famous video](https://www.youtube.com/watch?v=dQw4w9WgXcQ).
|
|
77
|
+
|
|
78
|
+
### Input types
|
|
79
|
+
|
|
80
|
+
HTMLRewriter can transform HTML from various sources. The input is automatically handled based on its type:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
// From Response
|
|
84
|
+
rewriter.transform(new Response("<div>content</div>"));
|
|
85
|
+
|
|
86
|
+
// From string
|
|
87
|
+
rewriter.transform("<div>content</div>");
|
|
88
|
+
|
|
89
|
+
// From ArrayBuffer
|
|
90
|
+
rewriter.transform(new TextEncoder().encode("<div>content</div>").buffer);
|
|
91
|
+
|
|
92
|
+
// From Blob
|
|
93
|
+
rewriter.transform(new Blob(["<div>content</div>"]));
|
|
94
|
+
|
|
95
|
+
// From File
|
|
96
|
+
rewriter.transform(Bun.file("index.html"));
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Note that Cloudflare Workers implementation of HTMLRewriter only supports `Response` objects.
|
|
100
|
+
|
|
101
|
+
### Element Handlers
|
|
102
|
+
|
|
103
|
+
The `on(selector, handlers)` method allows you to register handlers for HTML elements that match a CSS selector. The handlers are called for each matching element during parsing:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
rewriter.on("div.content", {
|
|
107
|
+
// Handle elements
|
|
108
|
+
element(element) {
|
|
109
|
+
element.setAttribute("class", "new-content");
|
|
110
|
+
element.append("<p>New content</p>", { html: true });
|
|
111
|
+
},
|
|
112
|
+
// Handle text nodes
|
|
113
|
+
text(text) {
|
|
114
|
+
text.replace("new text");
|
|
115
|
+
},
|
|
116
|
+
// Handle comments
|
|
117
|
+
comments(comment) {
|
|
118
|
+
comment.remove();
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The handlers can be asynchronous and return a Promise. Note that async operations will block the transformation until they complete:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
rewriter.on("div", {
|
|
127
|
+
async element(element) {
|
|
128
|
+
await Bun.sleep(1000);
|
|
129
|
+
element.setInnerContent("<span>replace</span>", { html: true });
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### CSS Selector Support
|
|
135
|
+
|
|
136
|
+
The `on()` method supports a wide range of CSS selectors:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
// Tag selectors
|
|
140
|
+
rewriter.on("p", handler);
|
|
141
|
+
|
|
142
|
+
// Class selectors
|
|
143
|
+
rewriter.on("p.red", handler);
|
|
144
|
+
|
|
145
|
+
// ID selectors
|
|
146
|
+
rewriter.on("h1#header", handler);
|
|
147
|
+
|
|
148
|
+
// Attribute selectors
|
|
149
|
+
rewriter.on("p[data-test]", handler); // Has attribute
|
|
150
|
+
rewriter.on('p[data-test="one"]', handler); // Exact match
|
|
151
|
+
rewriter.on('p[data-test="one" i]', handler); // Case-insensitive
|
|
152
|
+
rewriter.on('p[data-test="one" s]', handler); // Case-sensitive
|
|
153
|
+
rewriter.on('p[data-test~="two"]', handler); // Word match
|
|
154
|
+
rewriter.on('p[data-test^="a"]', handler); // Starts with
|
|
155
|
+
rewriter.on('p[data-test$="1"]', handler); // Ends with
|
|
156
|
+
rewriter.on('p[data-test*="b"]', handler); // Contains
|
|
157
|
+
rewriter.on('p[data-test|="a"]', handler); // Dash-separated
|
|
158
|
+
|
|
159
|
+
// Combinators
|
|
160
|
+
rewriter.on("div span", handler); // Descendant
|
|
161
|
+
rewriter.on("div > span", handler); // Direct child
|
|
162
|
+
|
|
163
|
+
// Pseudo-classes
|
|
164
|
+
rewriter.on("p:nth-child(2)", handler);
|
|
165
|
+
rewriter.on("p:first-child", handler);
|
|
166
|
+
rewriter.on("p:nth-of-type(2)", handler);
|
|
167
|
+
rewriter.on("p:first-of-type", handler);
|
|
168
|
+
rewriter.on("p:not(:first-child)", handler);
|
|
169
|
+
|
|
170
|
+
// Universal selector
|
|
171
|
+
rewriter.on("*", handler);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Element Operations
|
|
175
|
+
|
|
176
|
+
Elements provide various methods for manipulation. All modification methods return the element instance for chaining:
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
rewriter.on("div", {
|
|
180
|
+
element(el) {
|
|
181
|
+
// Attributes
|
|
182
|
+
el.setAttribute("class", "new-class").setAttribute("data-id", "123");
|
|
183
|
+
|
|
184
|
+
const classAttr = el.getAttribute("class"); // "new-class"
|
|
185
|
+
const hasId = el.hasAttribute("id"); // boolean
|
|
186
|
+
el.removeAttribute("class");
|
|
187
|
+
|
|
188
|
+
// Content manipulation
|
|
189
|
+
el.setInnerContent("New content"); // Escapes HTML by default
|
|
190
|
+
el.setInnerContent("<p>HTML content</p>", { html: true }); // Parses HTML
|
|
191
|
+
el.setInnerContent(""); // Clear content
|
|
192
|
+
|
|
193
|
+
// Position manipulation
|
|
194
|
+
el.before("Content before").after("Content after").prepend("First child").append("Last child");
|
|
195
|
+
|
|
196
|
+
// HTML content insertion
|
|
197
|
+
el.before("<span>before</span>", { html: true })
|
|
198
|
+
.after("<span>after</span>", { html: true })
|
|
199
|
+
.prepend("<span>first</span>", { html: true })
|
|
200
|
+
.append("<span>last</span>", { html: true });
|
|
201
|
+
|
|
202
|
+
// Removal
|
|
203
|
+
el.remove(); // Remove element and contents
|
|
204
|
+
el.removeAndKeepContent(); // Remove only the element tags
|
|
205
|
+
|
|
206
|
+
// Properties
|
|
207
|
+
console.log(el.tagName); // Lowercase tag name
|
|
208
|
+
console.log(el.namespaceURI); // Element's namespace URI
|
|
209
|
+
console.log(el.selfClosing); // Whether element is self-closing (e.g. <div />)
|
|
210
|
+
console.log(el.canHaveContent); // Whether element can contain content (false for void elements like <br>)
|
|
211
|
+
console.log(el.removed); // Whether element was removed
|
|
212
|
+
|
|
213
|
+
// Attributes iteration
|
|
214
|
+
for (const [name, value] of el.attributes) {
|
|
215
|
+
console.log(name, value);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// End tag handling
|
|
219
|
+
el.onEndTag(endTag => {
|
|
220
|
+
endTag.before("Before end tag");
|
|
221
|
+
endTag.after("After end tag");
|
|
222
|
+
endTag.remove(); // Remove the end tag
|
|
223
|
+
console.log(endTag.name); // Tag name in lowercase
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Text Operations
|
|
230
|
+
|
|
231
|
+
Text handlers provide methods for text manipulation. Text chunks represent portions of text content and provide information about their position in the text node:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
rewriter.on("p", {
|
|
235
|
+
text(text) {
|
|
236
|
+
// Content
|
|
237
|
+
console.log(text.text); // Text content
|
|
238
|
+
console.log(text.lastInTextNode); // Whether this is the last chunk
|
|
239
|
+
console.log(text.removed); // Whether text was removed
|
|
240
|
+
|
|
241
|
+
// Manipulation
|
|
242
|
+
text.before("Before text").after("After text").replace("New text").remove();
|
|
243
|
+
|
|
244
|
+
// HTML content insertion
|
|
245
|
+
text
|
|
246
|
+
.before("<span>before</span>", { html: true })
|
|
247
|
+
.after("<span>after</span>", { html: true })
|
|
248
|
+
.replace("<span>replace</span>", { html: true });
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Comment Operations
|
|
254
|
+
|
|
255
|
+
Comment handlers allow comment manipulation with similar methods to text nodes:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
rewriter.on("*", {
|
|
259
|
+
comments(comment) {
|
|
260
|
+
// Content
|
|
261
|
+
console.log(comment.text); // Comment text
|
|
262
|
+
comment.text = "New comment text"; // Set comment text
|
|
263
|
+
console.log(comment.removed); // Whether comment was removed
|
|
264
|
+
|
|
265
|
+
// Manipulation
|
|
266
|
+
comment.before("Before comment").after("After comment").replace("New comment").remove();
|
|
267
|
+
|
|
268
|
+
// HTML content insertion
|
|
269
|
+
comment
|
|
270
|
+
.before("<span>before</span>", { html: true })
|
|
271
|
+
.after("<span>after</span>", { html: true })
|
|
272
|
+
.replace("<span>replace</span>", { html: true });
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Document Handlers
|
|
278
|
+
|
|
279
|
+
The `onDocument(handlers)` method allows you to handle document-level events. These handlers are called for events that occur at the document level rather than within specific elements:
|
|
280
|
+
|
|
281
|
+
```ts
|
|
282
|
+
rewriter.onDocument({
|
|
283
|
+
// Handle doctype
|
|
284
|
+
doctype(doctype) {
|
|
285
|
+
console.log(doctype.name); // "html"
|
|
286
|
+
console.log(doctype.publicId); // public identifier if present
|
|
287
|
+
console.log(doctype.systemId); // system identifier if present
|
|
288
|
+
},
|
|
289
|
+
// Handle text nodes
|
|
290
|
+
text(text) {
|
|
291
|
+
console.log(text.text);
|
|
292
|
+
},
|
|
293
|
+
// Handle comments
|
|
294
|
+
comments(comment) {
|
|
295
|
+
console.log(comment.text);
|
|
296
|
+
},
|
|
297
|
+
// Handle document end
|
|
298
|
+
end(end) {
|
|
299
|
+
end.append("<!-- Footer -->", { html: true });
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Response Handling
|
|
305
|
+
|
|
306
|
+
When transforming a Response:
|
|
307
|
+
|
|
308
|
+
- The status code, headers, and other response properties are preserved
|
|
309
|
+
- The body is transformed while maintaining streaming capabilities
|
|
310
|
+
- Content-encoding (like gzip) is handled automatically
|
|
311
|
+
- The original response body is marked as used after transformation
|
|
312
|
+
- Headers are cloned to the new response
|
|
313
|
+
|
|
314
|
+
## Error Handling
|
|
315
|
+
|
|
316
|
+
HTMLRewriter operations can throw errors in several cases:
|
|
317
|
+
|
|
318
|
+
- Invalid selector syntax in `on()` method
|
|
319
|
+
- Invalid HTML content in transformation methods
|
|
320
|
+
- Stream errors when processing Response bodies
|
|
321
|
+
- Memory allocation failures
|
|
322
|
+
- Invalid input types (e.g., passing Symbol)
|
|
323
|
+
- Body already used errors
|
|
324
|
+
|
|
325
|
+
Errors should be caught and handled appropriately:
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
try {
|
|
329
|
+
const result = rewriter.transform(input);
|
|
330
|
+
// Process result
|
|
331
|
+
} catch (error) {
|
|
332
|
+
console.error("HTMLRewriter error:", error);
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
## See also
|
|
339
|
+
|
|
340
|
+
You can also read the [Cloudflare documentation](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/), which this API is intended to be compatible with.
|