Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Goodbye, node.js Buffer #1074

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Returns, via a promise, a [*tokenizer*](#tokenizer) which can be used to parse a
```js
import * as strtok3 from 'strtok3';
import * as Token from 'token-types';

(async () => {

const tokenizer = await strtok3.fromFile("somefile.bin");
Expand All @@ -68,7 +68,7 @@ import * as Token from 'token-types';
console.log(`My number: ${myNumber}`);
} finally {
tokenizer.close(); // Close the file
}
}
})();

```
Expand Down Expand Up @@ -106,7 +106,7 @@ Returns a [*tokenizer*](#tokenizer) which can be used to parse the provided buff

```js
import * as strtok3 from 'strtok3';

const tokenizer = strtok3.fromBuffer(buffer);

tokenizer.readToken(Token.UINT8).then(myUint8Number => {
Expand All @@ -115,7 +115,7 @@ tokenizer.readToken(Token.UINT8).then(myUint8Number => {
```

## Tokenizer
The tokenizer allows us to *read* or *peek* from the *tokenizer-stream*. The *tokenizer-stream* is an abstraction of a [stream](https://nodejs.org/api/stream.html), file or [Buffer](https://nodejs.org/api/buffer.html).
The tokenizer allows us to *read* or *peek* from the *tokenizer-stream*. The *tokenizer-stream* is an abstraction of a [stream](https://nodejs.org/api/stream.html), file or [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array).
It can also be translated in chunked reads, as done in [@tokenizer/http](https://github.com/Borewit/tokenizer-http);

What is the difference with Nodejs.js stream?
Expand All @@ -133,7 +133,7 @@ Optional attribute describing the file information, see [IFileInfo](#IFileInfo)
Pointer to the current position in the [*tokenizer*](#tokenizer) stream.
If a *position* is provided to a *read* or *peek* method, is should be, at least, equal or greater than this value.

### Tokenizer methods
### Tokenizer methods

There are two kind of methods:
1. *read* methods: used to read a *token* of [Buffer](https://nodejs.org/api/buffer.html) from the [*tokenizer*](#tokenizer). The position of the *tokenizer-stream* will advance with the size of the token.
Expand Down Expand Up @@ -200,7 +200,7 @@ Return value `Promise<number>` Promise with number peeked from the *tokenizer-st

#### Method `tokenizer.ignore()`

Advanse the offset pointer with the number of bytes provided.
Advance the offset pointer with the number of bytes provided.
`ignore(length)`

| Parameter | Type | Description |
Expand Down Expand Up @@ -234,14 +234,14 @@ File information interface which describes the underlying file, each attribute i

| Attribute | Type | Description |
|-----------|---------|---------------------------------------------------------------------------------------------------|
| size | number | File size in bytes |
| size | number | File size in bytes |
| mimeType | number | [MIME-type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of file. |
| path | number | File path |
| url | boolean | File URL |

## Token

The *token* is basically a description what to read form the [*tokenizer-stream*](#tokenizer).
The *token* is basically a description what to read form the [*tokenizer-stream*](#tokenizer).
A basic set of *token types* can be found here: [*token-types*](https://github.com/Borewit/token-types).

A token is something which implements the following interface:
Expand All @@ -258,7 +258,7 @@ export interface IGetToken<T> {
* @param buf Buffer to read the decoded value from
* @param off Decode offset
*/
get(buf: Buffer, off: number): T;
get(buf: Uint8Array, off: number): T;
}
```
The *tokenizer* reads `token.len` bytes from the *tokenizer-stream* into a Buffer.
Expand Down Expand Up @@ -286,7 +286,7 @@ import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';
const response = await fetch(url);
const readableWebStream = response.body; // Web-API readable stream
const nodeStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream

const tokenizer = strtok3core.fromStream(nodeStream); // And we now have tokenizer in a web environment
})();
```
Expand Down
5 changes: 2 additions & 3 deletions lib/AbstractTokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ITokenizer, IFileInfo, IReadChunkOptions } from './types.js';
import { EndOfStreamError } from 'peek-readable';
import { IGetToken, IToken } from '@tokenizer/token';
import { Buffer } from 'node:buffer';

interface INormalizedReadChunkOptions extends IReadChunkOptions {
offset: number;
Expand Down Expand Up @@ -51,7 +50,7 @@ export abstract class AbstractTokenizer implements ITokenizer {
* @returns Promise with token data
*/
public async readToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value> {
const uint8Array = Buffer.alloc(token.len);
const uint8Array = new Uint8Array(token.len);
const len = await this.readBuffer(uint8Array, {position});
if (len < token.len)
throw new EndOfStreamError();
Expand All @@ -65,7 +64,7 @@ export abstract class AbstractTokenizer implements ITokenizer {
* @returns Promise with token data
*/
public async peekToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value> {
const uint8Array = Buffer.alloc(token.len);
const uint8Array = new Uint8Array(token.len);
const len = await this.peekBuffer(uint8Array, {position});
if (len < token.len)
throw new EndOfStreamError();
Expand Down
10 changes: 5 additions & 5 deletions lib/FsPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export async function read(fd: number, buffer: Uint8Array, offset: number, lengt
});
}

export async function writeFile(path: fs.PathLike, data: Buffer | string): Promise<void> {
export async function writeFile(path: fs.PathLike, data: Uint8Array | string, options: fs.WriteFileOptions = null): Promise<void> {
return new Promise<void>((resolve, reject) => {
fs.writeFile(path, data, err => {
fs.writeFile(path, data, options, err => {
if (err)
reject(err);
else
Expand All @@ -67,12 +67,12 @@ export async function writeFile(path: fs.PathLike, data: Buffer | string): Promi
});
}

export function writeFileSync(path: fs.PathLike, data: Buffer | string): void {
export function writeFileSync(path: fs.PathLike, data: Uint8Array | string): void {
fs.writeFileSync(path, data);
}

export async function readFile(path: fs.PathLike): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
export async function readFile(path: fs.PathLike): Promise<Uint8Array> {
return new Promise<Uint8Array>((resolve, reject) => {
fs.readFile(path, (err, buffer) => {
if (err)
reject(err);
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export interface ITokenizer {
* @param options - Read behaviour options
* @returns Promise with number of bytes read
*/
peekBuffer(buffer: Buffer, options?: IReadChunkOptions): Promise<number>;
peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;

/**
* Peek (read ahead) buffer from tokenizer
* @param buffer - Target buffer to fill with data peeked from the tokenizer-stream
* @param options - Additional read options
* @returns Promise with number of bytes read
*/
readBuffer(buffer: Buffer, options?: IReadChunkOptions): Promise<number>;
readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;

/**
* Peek a token from the tokenizer-stream.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"remark-preset-lint-recommended": "^7.0.0",
"token-types": "^5.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.2"
"typescript": "^5.5.2",
"uint8array-extras": "^1.2.0"
},
"dependencies": {
"@tokenizer/token": "^0.3.0",
Expand Down
Loading