Skip to content

Commit ef97e99

Browse files
committed
fixup! buffer: add endings option, remove Node.js specific encoding option
1 parent 7650688 commit ef97e99

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

doc/api/buffer.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ multiple worker threads.
459459
### `new buffer.Blob([sources[, options]])`
460460
<!-- YAML
461461
added: v15.7.0
462+
changes:
463+
- version: REPLACEME
464+
pr-url: https://github.com/nodejs/node/pull/39708
465+
description: Added the standard `endings` option to replace line-endings,
466+
and removed the non-standard `encoding` option.
462467
-->
463468

464469
* `sources` {string[]|ArrayBuffer[]|TypedArray[]|DataView[]|Blob[]} An array
@@ -477,9 +482,9 @@ Creates a new `Blob` object containing a concatenation of the given sources.
477482
{ArrayBuffer}, {TypedArray}, {DataView}, and {Buffer} sources are copied into
478483
the 'Blob' and can therefore be safely modified after the 'Blob' is created.
479484

480-
String sources are encoded as UTF-8 byte sequences and copied into the blob.
485+
String sources are encoded as UTF-8 byte sequences and copied into the Blob.
481486
Unmatched surrogate pairs within each string part will be replaced by Unicode
482-
0xFFFD replacement characters.
487+
U+FFFD replacement characters.
483488

484489
### `blob.arrayBuffer()`
485490
<!-- YAML

lib/internal/blob.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
PromiseReject,
1010
SafePromisePrototypeFinally,
1111
ReflectConstruct,
12+
RegExpPrototypeSymbolReplace,
1213
RegExpPrototypeTest,
1314
StringPrototypeToLowerCase,
1415
StringPrototypeSplit,
@@ -111,7 +112,7 @@ function getSource(source, endings) {
111112
} else if (!isArrayBufferView(source)) {
112113
source = `${source}`;
113114
if (endings === 'native')
114-
source = source.replaceAll(/(\n|\r\n)/g, lazyEOL());
115+
source = RegExpPrototypeSymbolReplace(/\n|\r\n/g, source, lazyEOL());
115116
source = enc.encode(source);
116117
}
117118

@@ -131,7 +132,7 @@ class Blob {
131132
* @param {{
132133
* endings? : string,
133134
* type? : string,
134-
* }} options
135+
* }} [options]
135136
* @returns
136137
*/
137138
constructor(sources = [], options = {}) {
@@ -148,7 +149,7 @@ class Blob {
148149
} = options;
149150

150151
endings = `${endings}`;
151-
if (endings !== 'transparent' || endings !== 'native')
152+
if (endings !== 'transparent' && endings !== 'native')
152153
throw new ERR_INVALID_ARG_VALUE('options.endings', endings);
153154

154155
let length = 0;

test/parallel/test-blob.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Flags: --no-warnings
12
'use strict';
23

34
const common = require('../common');
45
const assert = require('assert');
56
const { Blob } = require('buffer');
67
const { inspect } = require('util');
8+
const { EOL } = require('os');
79

810
{
911
const b = new Blob();
@@ -210,7 +212,7 @@ assert.throws(() => new Blob({}), {
210212

211213
{
212214
const b = new Blob(['hello\n'], { endings: 'native' });
213-
assert.strictEqual(b.size, 7);
215+
assert.strictEqual(b.size, EOL.length + 5);
214216

215217
[1, {}, 'foo'].forEach((endings) => {
216218
assert.throws(() => new Blob([], { endings }), {

0 commit comments

Comments
 (0)