@angadie/chittie-transport 0.1.0
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/LICENSE +24 -0
- package/README.md +44 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.mjs +31 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Asyncdot Engineering
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
Portions of this software are vendored from MIT-licensed projects and retain
|
|
16
|
+
their original copyright notices; see VENDOR.md.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
24
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @angadie/chittie-transport
|
|
2
|
+
|
|
3
|
+
The transport contract for chittie, plus helpers. A transport ships ESC/POS bytes to a printer — discovery, permissions, and connection details live in the platform adapter; this package is just the **send contract**. Zero runtime dependencies.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @angadie/chittie-transport
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## The contract
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
interface Transport {
|
|
15
|
+
connect?(): Promise<void>; // open the port / connect BLE / open socket
|
|
16
|
+
write(data: Uint8Array): Promise<void>; // the one required method
|
|
17
|
+
disconnect?(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Implement it directly, or use a ready-made adapter:
|
|
22
|
+
[`@angadie/chittie-transport-web`](../chittie-transport-web) ·
|
|
23
|
+
[`@angadie/chittie-transport-react-native`](../chittie-transport-react-native).
|
|
24
|
+
|
|
25
|
+
## Helpers
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { print, writeBytes, chunk } from '@angadie/chittie-transport';
|
|
29
|
+
|
|
30
|
+
// connect (if supported) then write:
|
|
31
|
+
await print(transport, bytes);
|
|
32
|
+
|
|
33
|
+
// write in fixed-size chunks (BLE MTU), with an optional inter-chunk delay:
|
|
34
|
+
await writeBytes(transport, bytes, { chunkSize: 180, chunkDelayMs: 20 });
|
|
35
|
+
|
|
36
|
+
// split bytes yourself:
|
|
37
|
+
const parts = chunk(bytes, 180); // Uint8Array[]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`print` does not disconnect — the caller owns connection lifetime.
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A transport ships ESC/POS bytes to a printer. Deliberately tiny and
|
|
4
|
+
* library-agnostic: discovery/permissions/connection details live in the
|
|
5
|
+
* concrete adapter (web / react-native / node); this is just the send contract.
|
|
6
|
+
*/
|
|
7
|
+
interface Transport {
|
|
8
|
+
/** Optional: open the connection (request port, connect BLE, open socket…). */
|
|
9
|
+
connect?(): Promise<void>;
|
|
10
|
+
/** Write raw bytes to the printer. The one required method. */
|
|
11
|
+
write(data: Uint8Array): Promise<void>;
|
|
12
|
+
/** Optional: close the connection. */
|
|
13
|
+
disconnect?(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
/** Split bytes into fixed-size chunks (e.g. for a BLE MTU). */
|
|
16
|
+
declare function chunk(data: Uint8Array, size: number): Uint8Array[];
|
|
17
|
+
interface WriteOptions {
|
|
18
|
+
/** If set, write in chunks of this many bytes (BLE/serial buffer limits). */
|
|
19
|
+
chunkSize?: number;
|
|
20
|
+
/** Optional delay (ms) between chunks, for slow links. */
|
|
21
|
+
chunkDelayMs?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Write bytes, optionally chunked with an inter-chunk delay. */
|
|
24
|
+
declare function writeBytes(transport: Transport, data: Uint8Array, options?: WriteOptions): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Convenience: connect (if the transport supports it) then write the bytes.
|
|
27
|
+
* Does not disconnect — the caller owns connection lifetime.
|
|
28
|
+
*/
|
|
29
|
+
declare function print(transport: Transport, data: Uint8Array, options?: WriteOptions): Promise<void>;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { Transport, WriteOptions, chunk, print, writeBytes };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
/** Split bytes into fixed-size chunks (e.g. for a BLE MTU). */
|
|
3
|
+
function chunk(data, size) {
|
|
4
|
+
if (size <= 0) throw new RangeError("chunk size must be > 0");
|
|
5
|
+
const out = [];
|
|
6
|
+
for (let offset = 0; offset < data.length; offset += size) out.push(data.subarray(offset, Math.min(offset + size, data.length)));
|
|
7
|
+
return out;
|
|
8
|
+
}
|
|
9
|
+
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
10
|
+
/** Write bytes, optionally chunked with an inter-chunk delay. */
|
|
11
|
+
async function writeBytes(transport, data, options = {}) {
|
|
12
|
+
if (!options.chunkSize) {
|
|
13
|
+
await transport.write(data);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const chunks = chunk(data, options.chunkSize);
|
|
17
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
18
|
+
await transport.write(chunks[i]);
|
|
19
|
+
if (options.chunkDelayMs && i < chunks.length - 1) await delay(options.chunkDelayMs);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Convenience: connect (if the transport supports it) then write the bytes.
|
|
24
|
+
* Does not disconnect — the caller owns connection lifetime.
|
|
25
|
+
*/
|
|
26
|
+
async function print(transport, data, options = {}) {
|
|
27
|
+
if (transport.connect) await transport.connect();
|
|
28
|
+
await writeBytes(transport, data, options);
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { chunk, print, writeBytes };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@angadie/chittie-transport",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Transport contract for chittie: { connect; write; disconnect } + chunk()/writeBytes/print helpers. Zero runtime deps.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsdown",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"test": "tsx spikes/transport.spike.ts"
|
|
24
|
+
}
|
|
25
|
+
}
|