extended-buffer 7.5.0 → 7.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -30,8 +30,17 @@ If your bundler does not expose `Buffer` globally, add a small shim in your app
30
30
  ```ts
31
31
  import { Buffer } from "buffer";
32
32
 
33
- if (!globalThis.Buffer) {
34
- globalThis.Buffer = Buffer;
33
+ const globalScope: any =
34
+ typeof globalThis !== "undefined"
35
+ ? globalThis
36
+ : typeof self !== "undefined"
37
+ ? self
38
+ : typeof window !== "undefined"
39
+ ? window
40
+ : undefined;
41
+
42
+ if (globalScope && !globalScope.Buffer) {
43
+ globalScope.Buffer = Buffer;
35
44
  }
36
45
  ```
37
46
 
@@ -64,7 +73,7 @@ import { defineConfig } from "vite";
64
73
  export default defineConfig({
65
74
  resolve: {
66
75
  alias: {
67
- buffer: "buffer"
76
+ buffer: "buffer/"
68
77
  }
69
78
  },
70
79
  optimizeDeps: {
@@ -400,6 +409,7 @@ Rules:
400
409
 
401
410
  - If the callback **returns normally**, changes are kept (committed).
402
411
  - If the callback **throws**, the buffer is restored (rolled back) and the error is re-thrown.
412
+ - The callback must be **synchronous** (returned Promises are not awaited).
403
413
  - Transactions are **re-entrant**: nested `transaction()` calls do not create extra snapshots.
404
414
 
405
415
  What gets rolled back:
@@ -510,7 +520,7 @@ Common error codes you may see:
510
520
  - `VALUE_MUST_BE_AN_BIG_INTEGER`: value is not a `bigint`
511
521
  - `VALUE_MUST_BE_AN_UNSIGNED_BIG_INTEGER`: value is not a `bigint` or less than 0
512
522
  - `EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT`: BigInt methods are not supported in the current runtime
513
- - `EXCEEDING_MAXIMUM_BUFFER_SIZE`: allocation exceeds Node’s `kMaxLength` or `os.totalmem()`
523
+ - `EXCEEDING_MAXIMUM_BUFFER_SIZE`: allocation exceeds Node’s maximum buffer size (`kMaxLength` or `os.totalmem()` when available)
514
524
 
515
525
  ---
516
526
 
@@ -530,8 +540,8 @@ b.writeUInt16BE(123, true);
530
540
 
531
541
  ### `nodeGc()` is Node-specific
532
542
 
533
- `nodeGc()` calls `global.gc()` if it exists. In Node.js it requires starting the process with `--expose-gc`.
534
- In non-Node runtimes, `global` may not exist.
543
+ `nodeGc()` calls `gc()` on the detected global object if it exists. In Node.js it requires starting the process with `--expose-gc`.
544
+ In browsers/non-Node runtimes it simply no-ops.
535
545
 
536
546
  ---
537
547
 
@@ -5,13 +5,35 @@ exports.allocNativeBuffer = allocNativeBuffer;
5
5
  const buffer_1 = require("buffer");
6
6
  const errors_1 = require("../errors");
7
7
  const assert_unsigned_integer_1 = require("./assert-unsigned-integer");
8
- const maxBufferSize = typeof buffer_1.kMaxLength === 'number'
8
+ const totalMem = (function () {
9
+ try {
10
+ const req = typeof module !== 'undefined' && typeof module.require === 'function'
11
+ ? module.require.bind(module)
12
+ : typeof require === 'function'
13
+ ? require
14
+ : undefined;
15
+ if (!req) {
16
+ return undefined;
17
+ }
18
+ const os = req('os');
19
+ if (typeof (os === null || os === void 0 ? void 0 : os.totalmem) !== 'function') {
20
+ return undefined;
21
+ }
22
+ const result = os.totalmem();
23
+ return Number.isSafeInteger(result) && result > 0 ? result : undefined;
24
+ }
25
+ catch (_a) {
26
+ return undefined;
27
+ }
28
+ })();
29
+ const fallbackMaxLength = typeof buffer_1.kMaxLength === 'number'
9
30
  ? buffer_1.kMaxLength
10
31
  : typeof buffer_1.Buffer.kMaxLength === 'number'
11
32
  ? buffer_1.Buffer.kMaxLength
12
33
  : typeof ((_a = buffer_1.Buffer.constants) === null || _a === void 0 ? void 0 : _a.MAX_LENGTH) === 'number'
13
34
  ? buffer_1.Buffer.constants.MAX_LENGTH
14
35
  : Number.MAX_SAFE_INTEGER;
36
+ const maxBufferSize = typeof totalMem === 'number' ? Math.min(fallbackMaxLength, totalMem) : fallbackMaxLength;
15
37
  (0, assert_unsigned_integer_1.assertUnsignedInteger)(maxBufferSize);
16
38
  function allocNativeBuffer(size, allocSlow) {
17
39
  (0, assert_unsigned_integer_1.assertUnsignedInteger)(size);
@@ -3,13 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.assertSupportBigInteger = assertSupportBigInteger;
4
4
  const buffer_1 = require("buffer");
5
5
  const errors_1 = require("../errors");
6
+ const requiredMethods = [
7
+ 'readBigUInt64LE',
8
+ 'readBigUInt64BE',
9
+ 'readBigInt64LE',
10
+ 'readBigInt64BE',
11
+ 'writeBigUInt64LE',
12
+ 'writeBigUInt64BE',
13
+ 'writeBigInt64LE',
14
+ 'writeBigInt64BE'
15
+ ];
16
+ const isSupported = typeof BigInt === 'function' &&
17
+ requiredMethods.every(method => typeof buffer_1.Buffer.prototype[method] === 'function');
6
18
  function assertSupportBigInteger() {
7
- let isSupported = false;
8
- try {
9
- isSupported = typeof BigInt(0) === 'bigint';
10
- }
11
- catch (e) { }
12
- if (!isSupported || typeof buffer_1.Buffer.prototype.readBigUInt64LE !== 'function') {
19
+ if (!isSupported) {
13
20
  throw new errors_1.ExtendedBufferUnsupportedError('EXECUTION_ENVIRONMENT_NOT_SUPPORT_BIG_INT');
14
21
  }
15
22
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extended-buffer",
3
- "version": "7.5.0",
3
+ "version": "7.5.1",
4
4
  "description": "Node JS extended Buffer",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,7 @@
18
18
  "test": "node ./node_modules/.bin/mocha",
19
19
  "coverage": "node ./node_modules/.bin/nyc --reporter=lcov --reporter=html --reporter=text ./node_modules/.bin/mocha",
20
20
  "clean": "node -e \"['./dist', './coverage', './.nyc_output'].forEach(item => require('fs').rmSync(item, {recursive:true,force:true}));\"",
21
- "prepack": "npm run build"
21
+ "prepack": "npm run build && npm run test"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=6.0.0"