Skip to content

Commit d7d6220

Browse files
committed
update documentation
1 parent e3ab9f1 commit d7d6220

7 files changed

Lines changed: 432 additions & 152 deletions

File tree

.github/workflows/publish.yml

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
1-
name: Publish to NPM
1+
name: Publish to NPM and GitHub Pages
22

33
on:
44
release:
55
types: [created]
66

7+
permissions:
8+
contents: read
9+
id-token: write
10+
711
jobs:
812
build:
913
runs-on: ubuntu-latest
10-
permissions:
11-
contents: read
12-
id-token: write
1314
steps:
1415
- uses: actions/checkout@v4
15-
- uses: oven-sh/setup-bun@v1
16+
17+
- name: Setup Bun
18+
uses: oven-sh/setup-bun@v1
1619
with:
1720
bun-version: latest
18-
- uses: actions/setup-node@v4
21+
22+
- name: Setup Node
23+
uses: actions/setup-node@v4
1924
with:
2025
node-version: "20.x"
2126
registry-url: "https://registry.npmjs.org"
22-
- run: bun install
23-
- run: bun run build
24-
- run: npm publish --provenance --access public
27+
28+
- name: Install Dependencies
29+
run: bun install
30+
31+
- name: Build BinaryStream
32+
run: bun run build
33+
34+
- name: Publish to NPM
35+
run: npm publish --provenance --access public
2536
env:
2637
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38+
39+
- name: Build Docs
40+
run: bun run docs
41+
42+
- name: Upload Docs to GitHub Pages
43+
uses: actions/upload-pages-artifact@v3
44+
with:
45+
artifact-name: docs
46+
path: docs/
47+
deploy:
48+
environment:
49+
name: github-pages
50+
url: ${{ steps.deployment.outputs.page_url }}
51+
runs-on: ubuntu-latest
52+
needs: build
53+
steps:
54+
- name: Deploy to GitHub Pages
55+
id: deployment
56+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,5 @@ dist
173173

174174
# Finder (MacOS) folder config
175175
.DS_Store
176+
177+
docs/

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# BinaryStream
22

3-
`BinaryStream` is a TypeScript package designed to facilitate the reading and writing of binary data. This package leverages the `ArrayBuffer` and `DataView` interfaces, making it compatible with both Node.js and browser environments.
3+
`BinaryStream` is an Typescript module designed to facilitate the reading and writing of binary data. This package uses the [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer) and [`DataView`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView) interfaces, both of which are native to Javascript making it compatible with Node.js and browser environments.
44

55
## Features
66

77
- Supports both Node.js and browser environments.
88
- Provides an easy-to-use API for handling binary data.
99
- Methods for reading and writing various data types (integers, floats, strings, etc.).
10-
- Allows for dynamic buffer creation without worrying about resizing.
10+
- Allows for dynamic buffer creation without worrying about sizing.
1111

1212
## Installation
1313

14-
You can install the `BinaryStream` package via npm:
14+
You can install the `BinaryStream` package via npm.
1515

1616
```bash
1717
npm install @pocketnode/binarystream
@@ -27,36 +27,41 @@ bun install @pocketnode/binarystream
2727

2828
### Importing the Module
2929

30-
In a TypeScript or JavaScript file, import the `BinaryStream` module:
31-
3230
```typescript
31+
// Node.js, Bun, etc.
3332
import BinaryStream from "@pocketnode/binarystream";
34-
// or through CDN
35-
import BinaryStream from "https://unpkg.com/@pocketnode/binarystream@latest/dist/BinaryStream.js";
33+
34+
// Browser from CDN
35+
import BinaryStream from "https://esm.run/@pocketnode/binarystream@latest/dist/BinaryStream.js";
3636
```
3737

38-
### Creating a BinaryStream Instance
38+
### Creating an Instance
3939

4040
To create a new instance of `BinaryStream`, you can either provide an existing `ArrayBuffer` or specify the size of a new buffer:
4141

4242
```typescript
43-
// Create a BinaryStream with a new ArrayBuffer of 1024 bytes
44-
const stream = new BinaryStream(1024);
43+
// Create an empty BinaryStream
44+
const stream = new BinaryStream();
45+
46+
// Create a BinaryStream from an array of bytes
47+
const bytes = BinaryStream.from([0xde, 0xad, 0xbe, 0xef]);
48+
bytes.toString("hex"); // deadbeef
49+
50+
// Create a BinaryStream from a string
51+
const str = BinaryStream.from("\xde\xad\xbe\xef", "binary");
52+
str.toString("hex"); // deadbeef
4553

4654
// Create a BinaryStream from an existing ArrayBuffer
47-
const buffer = new ArrayBuffer(1024);
48-
const streamFromBuffer = new BinaryStream(buffer);
55+
const blob = new Blob(..., {type: "application/octet-stream"});
56+
const streamFromBuffer = BinaryStream.from(await blob.arrayBuffer());
4957
```
5058

5159
### Example
5260

53-
Here is a complete example demonstrating how to write and read data using `BinaryStream`:
61+
Here is an example demonstrating how to write and read data using `BinaryStream`:
5462

5563
```typescript
56-
import BinaryStream from "@pocketnode/binarystream";
57-
58-
// Create a BinaryStream with a new ArrayBuffer of 1024 bytes
59-
const stream = new BinaryStream(1024);
64+
const stream = new BinaryStream();
6065

6166
// Write data to the stream
6267
stream.writeUInt8(255);
@@ -77,12 +82,9 @@ console.log(uint8); // 255
7782
console.log(int16); // -32768
7883
console.log(float32); // 3.14
7984
console.log(str); // Hello, BinaryStream!
85+
console.log(stream.buffer); // Uint8Array (31) [255, 128, 0, 64, 72, 245, 195, 0, 0, 0, 20, 72, 101, 108, 108, 111, 44, 32, 66, 105, 110, 97, 114, 121, 83, 116, 114, 101, 97, 109, 33]
8086
```
8187

82-
## Compatibility
83-
84-
`BinaryStream` works in both Node.js and browser environments. It relies on `ArrayBuffer` and `DataView`, which are available in [modern browsers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transfer#browser_compatibility) and Node.js.
85-
8688
## License
8789

8890
This project is licensed under the GNU GPLv3 License. See the [LICENSE](LICENSE) file for details.

bun.lockb

16.3 KB
Binary file not shown.

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@
1717
"node"
1818
],
1919
"scripts": {
20-
"build": "bun run build.ts"
20+
"build": "bun run build.ts",
21+
"docs": "typedoc --plugin typedoc-plugin-mdn-links --plugin typedoc-material-theme --out docs --name BinaryStream --readme README.md --themeColor '#1B1D68' ./dist/BinaryStream.d.ts"
2122
},
2223
"license": "GPL-3.0-only",
2324
"repository": {
2425
"type": "git",
2526
"url": "git+https://github.com/pocketnode/binarystream.git"
2627
},
2728
"devDependencies": {
29+
"typescript": "^5.0.0",
2830
"bun-plugin-dts": "^0.2.3",
29-
"bun-types": "latest"
30-
},
31-
"peerDependencies": {
32-
"typescript": "^5.0.0"
31+
"bun-types": "latest",
32+
"type-doc": "^0.1.41",
33+
"typedoc-material-theme": "^1.0.2",
34+
"typedoc-plugin-mdn-links": "^3.1.28"
3335
}
3436
}

src/ArrayBufferTransferPolyfill.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare global {
2+
interface ArrayBuffer {
3+
transfer(size: number): ArrayBuffer;
4+
}
5+
}
6+
7+
if (!ArrayBuffer.prototype.transfer) {
8+
ArrayBuffer.prototype.transfer = function (size: number): ArrayBuffer {
9+
const buffer = new ArrayBuffer(size);
10+
const view = new Uint8Array(buffer);
11+
view.set(new Uint8Array(this, 0, size));
12+
return buffer;
13+
};
14+
}

0 commit comments

Comments
 (0)