From 3e2bb605582c2426154d2e9c1189496008c8b85d Mon Sep 17 00:00:00 2001 From: Xavier Stouder Date: Wed, 17 May 2023 00:16:06 +0200 Subject: [PATCH 1/7] doc: document undocumented method on buffer prototype Fixes: https://github.com/nodejs/node/issues/46467 --- doc/api/buffer.md | 375 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 375 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 27fcd7d804555c..8c9ae594558b44 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -3515,6 +3515,381 @@ console.log(buf.toString()); // Also prints: cuffer (!) ``` + +### `buf.asciiSlice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according ASCII character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +In most cases, `buf.toString()` is preferable, especially if encoding is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('ascii', 0, 3) +console.log(buf.asciiSlice(0, 3)); +// Prints: buf +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('ascii', 0, 3) +console.log(buf.asciiSlice(0, 3)); +// Prints: buf +``` + +### `buf.base64Slice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to base64 character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('base64', 0, 3) +console.log(buf.base64Slice(0, 3)); +// Prints: YnVm +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('base64', 0, 3) +console.log(buf.base64Slice(0, 3)); +// Prints: YnVm +``` + +### `buf.base64urlSlice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to base64url character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('base64url', 0, 3) +console.log(buf.base64urlSlice(0, 3)); +// Prints: YnVm +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('base64url', 0, 3) +console.log(buf.base64urlSlice(0, 3)); +// Prints: YnVm +``` + +### `buf.hexSlice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to hex character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('hex', 0, 3) +console.log(buf.hexSlice(0, 3)); +// Prints: 627566 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('hex', 0, 3) +console.log(buf.hexSlice(0, 3)); +// Prints: 627566 +``` + +### `buf.latin1Slice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to latin1 character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('latin1', 0, 3) +console.log(buf.latin1Slice(0, 3)); +// Prints: buf +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('latin1', 0, 3) +console.log(buf.latin1Slice(0, 3)); +// Prints: buf +``` + +### `buf.ucs2Slice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to UCS-2 character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('ucs2', 0, 3) +console.log(buf.ucs2Slice(0, 3)); +// Prints: 畢 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('ucs2', 0, 3) +console.log(buf.ucs2Slice(0, 3)); +// Prints: 畢 +``` + +### `buf.utf8Slice([start[, end]])` + +* `start` {integer} The byte offset to start slicing at. **Default**: 0. +* `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. +* Returns: {string} + +Decodes buf to a string according to UTF-8 character encoding. `start` and `end` may be passed to decode only a subset of `buf`. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('utf8', 0, 3) +console.log(buf.utf8Slice(0, 3)); +// Prints: buf +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.from('buffer'); + +// Equivalent to buf.toString('utf8', 0, 3) +console.log(buf.utf8Slice(0, 3)); +// Prints: buf +``` + +### `buf.asciiWrite(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. + +Writes `string` to `buf` at `offset` according to the ASCII character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.asciiWrite('buffer'); +// Prints: 6 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.asciiWrite('buffer'); +console.log(bytesWritten); +// Prints: 6 +``` + +### `buf.base64Write(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the base64 character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.base64Write('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.base64Write('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + +### `buf.base64urlWrite(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the base64url character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.base64urlWrite('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.base64urlWrite('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + +### `buf.hexWrite(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the hex character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.hexWrite('ab'); +console.log(bytesWritten); +// Prints: 1 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.hexWrite('ab'); +console.log(bytesWritten); +// Prints: 1 +``` + +### `buf.latin1Write(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the latin1 character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.latin1Write('buffer'); +console.log(bytesWritten); +// Prints: 2 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.latin1Write('buffer'); +console.log(bytesWritten); +// Prints: 2 +``` + +### `buf.utf8Write(string[, offset[, length]])` + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf.length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the utf8 character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.utf8Write('buffer'); +console.log(bytesWritten); +// Prints: 2 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.utf8Write('buffer'); +console.log(bytesWritten); +// Prints: 2 +``` + ### `buf.swap16()` + * `start` {integer} The byte offset to start slicing at. **Default**: 0. * `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. @@ -3576,6 +3580,10 @@ console.log(buf.base64Slice(0, 3)); ### `buf.base64urlSlice([start[, end]])` + + * `start` {integer} The byte offset to start slicing at. **Default**: 0. * `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. @@ -3604,6 +3612,10 @@ console.log(buf.base64urlSlice(0, 3)); ### `buf.hexSlice([start[, end]])` + + * `start` {integer} The byte offset to start slicing at. **Default**: 0. * `end` {integer} The byte offset to stop decoding at (not inclusive). **Default**: `buf.length`. @@ -3632,6 +3644,10 @@ console.log(buf.hexSlice(0, 3)); ### `buf.latin1Slice([start[, end]])` + + * `start` {integer} The byte offset to start slicing at. **Default**: 0. * `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. @@ -3660,6 +3676,10 @@ console.log(buf.latin1Slice(0, 3)); ### `buf.ucs2Slice([start[, end]])` + + * `start` {integer} The byte offset to start slicing at. **Default**: 0. * `end` {integer} The byte offset to stop slicing at (not inclusive). **Default**: `buf.length`. @@ -3748,6 +3768,10 @@ console.log(bytesWritten); ### `buf.base64Write(string[, offset[, length]])` + + * `string` {string} String to write to `buf`. * `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. @@ -3782,6 +3806,10 @@ console.log(bytesWritten); ### `buf.base64urlWrite(string[, offset[, length]])` + + * `string` {string} String to write to `buf`. * `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. @@ -3816,6 +3844,10 @@ console.log(bytesWritten); ### `buf.hexWrite(string[, offset[, length]])` + + * `string` {string} String to write to `buf`. * `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. @@ -3850,6 +3882,10 @@ console.log(bytesWritten); ### `buf.latin1Write(string[, offset[, length]])` + + * `string` {string} String to write to `buf`. * `offset` {integer} Number of bytes to skip before starting to write `string`. **Default**: 0. @@ -3882,6 +3918,44 @@ console.log(bytesWritten); // Prints: 2 ``` +### `buf.ucs2Write([start[, end]])` + + + +* `string` {string} String to write to `buf`. +* `offset` {integer} Number of bytes to skip before starting to write + `string`. **Default**: 0. +* `length` {integer} Maximum number of bytes to write. **Default**: `buf. + length - offset`. +* Returns: {integer} The number of bytes written. + +Writes `string` to `buf` at `offset` according to the UCS-2 character encoding +and returns the number of bytes written. If `buf` did not contain enough space +to fit the entire string, only part of `string` will be written. However, +partially encoded characters will not be written. + +In most cases, `buf.write` is preferable, especially if `encoding` is variable. + +```mjs +import { Buffer } from 'node:buffer'; +const buf = Buffer.alloc(10); + +const bytesWritten = buf.ucs2Write('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + +```cjs +const { Buffer } = require('node:buffer'); +const buf = Buffer.alloc(10); + +const bytesWritten = buf.ucs2Write('buffer'); +console.log(bytesWritten); +// Prints: 4 +``` + ### `buf.utf8Write(string[, offset[, length]])` * `string` {string} String to write to `buf`. From 737bc2e16d3e3d5105841f9b65903eb4c0fd8d32 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 27 Jul 2023 00:36:39 -0700 Subject: [PATCH 7/7] doc: add missing console.log(bytesWritten) --- doc/api/buffer.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 5fd1f23118c6a3..764350e2a653a4 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -3754,6 +3754,7 @@ import { Buffer } from 'node:buffer'; const buf = Buffer.alloc(10); const bytesWritten = buf.asciiWrite('buffer'); +console.log(bytesWritten); // Prints: 6 ```