1 | # Base64 Encoding
|
2 |
|
3 | Fast Base64 encoding and decoding powered by WebAssembly.
|
4 |
|
5 | This library is modeled after the WHATWG `TextEncoder` and `TextDecoder` API,
|
6 | providing a `Base64Encoder` and `Base64Decoder` class.
|
7 |
|
8 | The C implementation was chosen based on benchmarks provided by [`gaspardpetit/base64`](https://github.com/gaspardpetit/base64).
|
9 |
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```js
|
14 | const encoder = await new Base64Encoder().optimize();
|
15 | encoder.encode(new TextEncoder().encode('foobar')) // => Zm9vYmFy
|
16 |
|
17 | const decoder = await new Base64Decoder().optimize();
|
18 | new TextDecoder().decode(decoder.decode("Zm9vYmFy")) // => foobar
|
19 | ```
|
20 |
|
21 | For one-shot usage, you can use the JS implementation without instantiating a WASM instance:
|
22 |
|
23 | ```js
|
24 | new Base64Encoder().encode(new TextEncoder().encode('foobar')) // => Zm9vYmFy
|
25 | new TextDecoder().decode(new Base64Decoder().decode("Zm9vYmFy")) // => foobar
|
26 | ```
|
27 |
|
28 | ### URL-friendly Encoding
|
29 | This implementation also supports a URL-friendly variant of Base64, where
|
30 |
|
31 | - all `'+'` are mapped to `'-'`
|
32 | - all `'/'` are mapped to `'_'`
|
33 | - the padding characters `'='` are omitted
|
34 |
|
35 | To use this variant, provide the `url` setting when creating the encoder.
|
36 |
|
37 | ```js
|
38 | const encoder = await new Base64Encoder({ url: true }).optimize();
|
39 | ```
|
40 |
|
41 | For decoding URL-friendly Base64 no extra steps are required.
|
42 |
|
43 |
|
44 | ## Performance
|
45 |
|
46 | TBD
|
47 |
|
48 | Currently only the encoder provides a significant performance improvement over the pure JS implementation.
|
49 |
|
50 |
|
51 | ## Distribution
|
52 |
|
53 | This module is published on npm under the [`base64-encoding`](https://www.npmjs.com/package/base64-encoding) tag.
|
54 | The package contains the following:
|
55 |
|
56 | - The root folder ([Browse](https://unpkg.com/browse/base64-encoding/)) exports ES modules in ES2018 syntax.
|
57 | All internal module paths are fully qualified, so they can be imported in Deno or the browser directly:
|
58 |
|
59 | ```ts
|
60 | import * as b64 from 'https://unpkg.com/base64-encoding?module';
|
61 | ```
|
62 |
|
63 | - The `module` folder ([Browse](https://unpkg.com/browse/base64-encoding/module/)) contains a rolled-up version of the above.
|
64 |
|
65 | ```ts
|
66 | import * as b64 from 'https://unpkg.com/base64-encoding/module';
|
67 | ```
|
68 |
|
69 | - The `cjs` folder ([Browse](https://unpkg.com/browse/base64-encoding/cjs/)) exports CommonJS modules in ES2015 syntax for use in node.
|
70 |
|
71 | ```ts
|
72 | require('base64-encoding')
|
73 | ```
|
74 |
|
75 | - The `src` folder ([Browse](https://unpkg.com/browse/base64-encoding/src/)) contains the TypeScript source code.
|
76 |
|
77 |
|
78 | The root and `cjs` folder include type declarations and source maps, so that IntelliSense works out of the box in VSCode.
|
79 |
|
80 | The `package.json` properly sets the `main`, `module`, `type` and `exports` keys, so that `package.json-`based tools will pick the right version.
|
81 |
|
82 |
|
83 | ## License
|
84 |
|
85 | TBD
|
86 |
|
87 | Currently the C code is licensed under an ancient Apache 1.0 license that comes with some pretty old-school requirements, such as including the following in all promotional materials:
|
88 |
|
89 | > This product includes software
|
90 | > developed by the Apache Group for use in the Apache HTTP server project
|
91 | > (http://www.apache.org/).
|
92 |
|
93 | It is very likely that [`ap_base64.c`](https://github.com/dhamidi/apache-httpd-1.3.42/blob/master/src/ap/ap_base64.c) has been shipped under a Apache-2.0 license somewhere.
|
94 | Once I locate it, this requirement will go away.
|
95 |
|
96 | ## TODO
|
97 |
|
98 | - Figure out why decoding is slow
|
99 | - License
|
100 |
|
101 |
|
\ | No newline at end of file |