Skip to content

Fix string_decoder import #548

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

Merged
merged 5 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion build/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { aliases, skippedSources, sources } from './files.mjs'
import { footers } from './footers.mjs'
import { headers } from './headers.mjs'
import { replacements } from './replacements.mjs'
import pkg from '../package.json' with { type: "json" };

const baseMatcher = /^(?:lib|test)/
const strictMatcher = /^(['"']use strict.+)/
Expand Down Expand Up @@ -162,7 +163,7 @@ async function main() {
return process.exit(1)
}

const nodeVersion = process.argv[2]
const nodeVersion = process.argv[2] || pkg.config.node.version;

if (!nodeVersion?.match(/^\d+\.\d+\.\d+/)) {
console.error('Usage: build.js xx.yy.zz [node.tar.gz]')
Expand Down
5 changes: 4 additions & 1 deletion build/replacements.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ const streamsRequireInternal = ["require\\('internal/(.+)'\\)", "require('../int

const streamsRequirePrimordials = ['= primordials', "= require('../ours/primordials')"]

const stringDecoderRequirePackage = ["require\\('string_decoder'\\)", "require('string_decoder/')"]

const testCommonKnownGlobals = [
'let knownGlobals = \\[(\\n\\s+)',
`
Expand Down Expand Up @@ -312,7 +314,8 @@ export const replacements = {
],
'lib/internal/streams/readable.js': [
removefromWebReadableMethod,
removetoWebReadableMethod
removetoWebReadableMethod,
stringDecoderRequirePackage
],
'lib/internal/streams/.+': [
internalStreamsRequireErrors,
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default [
'space-before-function-paren': 0,
curly: [2, 'all'],
'local/no-big-int': 'error',
'no-undef': 'warn',
},
},
]

2 changes: 1 addition & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const {
} = require('../../ours/errors')
const { validateObject } = require('../validators')
const kPaused = Symbol('kPaused')
const { StringDecoder } = require('string_decoder')
const { StringDecoder } = require('string_decoder/')
const from = require('./from')
ObjectSetPrototypeOf(Readable.prototype, Stream.prototype)
ObjectSetPrototypeOf(Readable, Stream)
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function isReadableNodeStream(obj, strict = false) {
) // Writable has .pipe.
)
}

function isWritableNodeStream(obj) {
var _obj$_writableState
return !!(
Expand All @@ -45,7 +44,6 @@ function isWritableNodeStream(obj) {
) // Duplex
)
}

function isDuplexNodeStream(obj) {
return !!(
obj &&
Expand Down
3 changes: 2 additions & 1 deletion lib/ours/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ E(
received = addNumericalSeparator(String(input))
} else if (typeof input === 'bigint') {
received = String(input)
if (input > 2n ** 32n || input < -(2n ** 32n)) {
const limit = BigInt(2) ** BigInt(32)
if (input > limit || input < -limit) {
received = addNumericalSeparator(received)
}
received += 'n'
Expand Down
2 changes: 1 addition & 1 deletion lib/ours/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ module.exports = {
TypedArrayPrototypeSet(self, buf, len) {
return self.set(buf, len)
},
Boolean: Boolean,
Boolean,
Uint8Array
}
4 changes: 3 additions & 1 deletion lib/ours/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const validateAbortSignal = (signal, name) => {
}
}
const validateFunction = (value, name) => {
if (typeof value !== 'function') throw new ERR_INVALID_ARG_TYPE(name, 'Function', value)
if (typeof value !== 'function') {
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value)
}
}

// This is a simplified version of AggregateError
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,10 @@
},
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"config": {
"node": {
"version": "18.20.5"
}
}
}
2 changes: 1 addition & 1 deletion src/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ module.exports = {
TypedArrayPrototypeSet(self, buf, len) {
return self.set(buf, len)
},
Boolean: Boolean,
Boolean,
Uint8Array
}
31 changes: 15 additions & 16 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ const validateAbortSignal = (signal, name) => {
(signal === null ||
typeof signal !== 'object' ||
!('aborted' in signal))) {
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal);
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal)
}
};
}
const validateFunction = (value, name) => {
if (typeof value !== 'function')
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
};
if (typeof value !== 'function') { throw new ERR_INVALID_ARG_TYPE(name, 'Function', value) }
}

// This is a simplified version of AggregateError
class AggregateError extends Error {
Expand Down Expand Up @@ -155,26 +154,26 @@ module.exports = {
},
addAbortListener: require('events').addAbortListener || function addAbortListener(signal, listener) {
if (signal === undefined) {
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal);
throw new ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal)
}
validateAbortSignal(signal, 'signal');
validateFunction(listener, 'listener');
validateAbortSignal(signal, 'signal')
validateFunction(listener, 'listener')

let removeEventListener;
let removeEventListener
if (signal.aborted) {
queueMicrotask(() => listener());
queueMicrotask(() => listener())
} else {
signal.addEventListener('abort', listener, { __proto__: null, once: true, [kResistStopPropagation]: true });
signal.addEventListener('abort', listener, { __proto__: null, once: true, [kResistStopPropagation]: true })
removeEventListener = () => {
signal.removeEventListener('abort', listener);
};
signal.removeEventListener('abort', listener)
}
}
return {
__proto__: null,
[SymbolDispose]() {
removeEventListener?.();
},
};
removeEventListener?.()
}
}
},
AbortSignalAny: AbortSignal.any || function AbortSignalAny(signals) {
// Fast path if there is only one signal.
Expand Down
1 change: 0 additions & 1 deletion test/browser/test-stream2-readable-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ module.exports = function (test) {
old.emit('data', item)
// console.log('after emit', chunks, flowing);
}

if (chunks <= 0) {
oldEnded = true
// console.log('old end', chunks, flowing);
Expand Down
18 changes: 3 additions & 15 deletions test/common/fixtures.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import fixtures from './fixtures.js';
import fixtures from './fixtures.js'

const {
fixturesDir,
path,
fileURL,
readSync,
readKey,
} = fixtures;
const { fixturesDir, path, fileURL, readSync, readKey } = fixtures

export {
fixturesDir,
path,
fileURL,
readSync,
readKey,
};
export { fixturesDir, path, fileURL, readSync, readKey }
1 change: 0 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,6 @@ const common = {
get enoughTestMem() {
return require('os').totalmem() > 0x70000000 /* 1.75 Gb */
},

get hasFipsCrypto() {
return hasCrypto && require('crypto').getFips()
},
Expand Down
16 changes: 8 additions & 8 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequire } from 'module';
import { createRequire } from 'module'

const require = createRequire(import.meta.url);
const common = require('./index.js');
const require = createRequire(import.meta.url)
const common = require('./index.js')

const {
allowGlobals,
Expand Down Expand Up @@ -50,10 +50,10 @@ const {
skipIfDumbTerminal,
skipIfEslintMissing,
skipIfInspectorDisabled,
spawnPromisified,
} = common;
spawnPromisified
} = common

const getPort = () => common.PORT;
const getPort = () => common.PORT

export {
allowGlobals,
Expand Down Expand Up @@ -104,5 +104,5 @@ export {
skipIfDumbTerminal,
skipIfEslintMissing,
skipIfInspectorDisabled,
spawnPromisified,
};
spawnPromisified
}
100 changes: 59 additions & 41 deletions test/parallel/test-stream-asIndexedPairs.mjs
Original file line number Diff line number Diff line change
@@ -1,64 +1,82 @@
import '../common/index.mjs';
import { Readable }from '../../lib/ours/index.js';
import { deepStrictEqual, rejects, throws } from 'assert';
import tap from 'tap';
import '../common/index.mjs'
import { Readable } from '../../lib/ours/index.js'
import { deepStrictEqual, rejects, throws } from 'assert'
import tap from 'tap'

{
// asIndexedPairs with a synchronous stream
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray();
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
const empty = await Readable.from([]).asIndexedPairs().toArray();
deepStrictEqual(empty, []);
const pairs = await Readable.from([1, 2, 3]).asIndexedPairs().toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 2],
[2, 3]
])
const empty = await Readable.from([]).asIndexedPairs().toArray()
deepStrictEqual(empty, [])
}

{
// asIndexedPairs works an asynchronous streams
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x);
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray();
deepStrictEqual(pairs, [[0, 1], [1, 2], [2, 3]]);
const empty = await asyncFrom([]).asIndexedPairs().toArray();
deepStrictEqual(empty, []);
const asyncFrom = (...args) => Readable.from(...args).map(async (x) => x)
const pairs = await asyncFrom([1, 2, 3]).asIndexedPairs().toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 2],
[2, 3]
])
const empty = await asyncFrom([]).asIndexedPairs().toArray()
deepStrictEqual(empty, [])
}

{
// Does not enumerate an infinite stream
const infinite = () => Readable.from(async function* () {
while (true) yield 1;
}());
const pairs = await infinite().asIndexedPairs().take(3).toArray();
deepStrictEqual(pairs, [[0, 1], [1, 1], [2, 1]]);
const empty = await infinite().asIndexedPairs().take(0).toArray();
deepStrictEqual(empty, []);
const infinite = () =>
Readable.from(
(async function* () {
while (true) yield 1
})()
)
const pairs = await infinite().asIndexedPairs().take(3).toArray()
deepStrictEqual(pairs, [
[0, 1],
[1, 1],
[2, 1]
])
const empty = await infinite().asIndexedPairs().take(0).toArray()
deepStrictEqual(empty, [])
}

{
// AbortSignal
await rejects(async () => {
const ac = new AbortController();
const { signal } = ac;
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
ac.abort();
await p;
}, { name: 'AbortError' });
await rejects(
async () => {
const ac = new AbortController()
const { signal } = ac
const p = Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
ac.abort()
await p
},
{ name: 'AbortError' }
)

await rejects(async () => {
const signal = AbortSignal.abort();
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray();
}, /AbortError/);
const signal = AbortSignal.abort()
await Readable.from([1, 2, 3]).asIndexedPairs({ signal }).toArray()
}, /AbortError/)
}

{
// Error cases
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/);
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/);
throws(() => Readable.from([1]).asIndexedPairs(1), /ERR_INVALID_ARG_TYPE/)
throws(() => Readable.from([1]).asIndexedPairs({ signal: true }), /ERR_INVALID_ARG_TYPE/)
}

/* replacement start */
process.on('beforeExit', (code) => {
if(code === 0) {
tap.pass('test succeeded');
} else {
tap.fail(`test failed - exited code ${code}`);
}
});
/* replacement end */
/* replacement start */
process.on('beforeExit', (code) => {
if (code === 0) {
tap.pass('test succeeded')
} else {
tap.fail(`test failed - exited code ${code}`)
}
})
/* replacement end */
Loading
Loading