Skip to content
Draft
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: 20 additions & 0 deletions packages/csv-parse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ parser.write("someone:x:1022:1022::/home/someone:/bin/bash\n");
parser.end();
```

## Browser Web Streams

The `csv-parse/browser/esm/stream` entry point uses the browser's native
`TransformStream` and accepts byte chunks, such as a fetch response body:

```js
import { parse } from "csv-parse/browser/esm/stream";

const response = await fetch("data.csv");
const reader = response.body.pipeThrough(parse({ columns: true })).getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
console.log(value);
}
```

This entry point requires native Web Streams support. It does not install
stream globals or require Node.js stream modules.

## Contributors

The project is sponsored by [Adaltas](https://www.adaltas.com), an Big Data consulting firm based in Paris, France.
Expand Down
3 changes: 2 additions & 1 deletion packages/csv-parse/dist/cjs/stream.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,8 @@ const parse = (opts) => {
controller = ctr;
},
transform(chunk) {
const error = api.parse(chunk, false, enqueue, terminate);
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
const error = api.parse(buffer, false, enqueue, terminate);
if (error) {
controller.error(error);
}
Expand Down
35 changes: 35 additions & 0 deletions packages/csv-parse/dist/esm/browser/api/CsvError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { OptionsNormalized } from "../options.js";

export type CsvErrorCode =
| "CSV_INVALID_ARGUMENT"
| "CSV_INVALID_CLOSING_QUOTE"
| "CSV_INVALID_COLUMN_DEFINITION"
| "CSV_INVALID_COLUMN_MAPPING"
| "CSV_INVALID_OPTION_BOM"
| "CSV_INVALID_OPTION_CAST"
| "CSV_INVALID_OPTION_CAST_DATE"
| "CSV_INVALID_OPTION_COLUMNS"
| "CSV_INVALID_OPTION_COMMENT"
| "CSV_INVALID_OPTION_DELIMITER"
| "CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME"
| "CSV_INVALID_OPTION_ON_RECORD"
| "CSV_MAX_RECORD_SIZE"
| "CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE"
| "CSV_OPTION_COLUMNS_MISSING_NAME"
| "CSV_QUOTE_NOT_CLOSED"
| "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH"
| "CSV_RECORD_INCONSISTENT_COLUMNS"
| "CSV_UNKNOWN_ERROR"
| "INVALID_OPENING_QUOTE";

export class CsvError extends Error {
readonly code: CsvErrorCode;
[key: string]: unknown;

constructor(
code: CsvErrorCode,
message: string | string[],
options?: OptionsNormalized,
...contexts: unknown[]
);
}
18 changes: 18 additions & 0 deletions packages/csv-parse/dist/esm/browser/buffer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** The Buffer methods exposed by the browser bundle's byte-valued options. */
export interface Buffer extends Uint8Array {
equals(otherBuffer: Uint8Array): boolean;
toString(encoding?: BufferEncoding, start?: number, end?: number): string;
}

export type BufferEncoding =
| "ascii"
| "utf8"
| "utf-8"
| "utf16le"
| "utf-16le"
| "ucs2"
| "ucs-2"
| "base64"
| "latin1"
| "binary"
| "hex";
Loading