pw-buffer 3.0.2 → 3.1.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017 Sality
3
+ Copyright (c) 2025 mvcbox
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![npm version](https://badge.fury.io/js/pw-buffer.svg?flush_cache=v3_0_2)](https://badge.fury.io/js/pw-buffer)
1
+ [![npm version](https://badge.fury.io/js/pw-buffer.svg)](https://badge.fury.io/js/pw-buffer)
2
2
 
3
3
  # Perfect World binary utils
4
4
 
@@ -81,6 +81,8 @@ export { MppcDecompressorTransform } from './MppcDecompressorTransform';
81
81
 
82
82
  - `capacity?: number` – initial capacity (bytes)
83
83
  - `capacityStep?: number` – resize step (bytes)
84
+ - `nativeAllocSlow?: boolean` – using Buffer.allocUnsafeSlow() when initializing
85
+ - `nativeReallocSlow?: boolean` – using Buffer.allocUnsafeSlow() for further reallocations
84
86
 
85
87
  Example:
86
88
 
@@ -1,8 +1,7 @@
1
- /// <reference types="node" />
2
1
  import { MppcCompressor } from './mppc';
3
2
  import { Transform, TransformOptions } from 'stream';
4
3
  export declare class MppcCompressorTransform extends Transform {
5
4
  protected readonly _mppcCompressor: MppcCompressor;
6
5
  constructor(transformOptions?: TransformOptions);
7
- _transform(chunk: Buffer, encoding: string, callback: Function): void;
6
+ _transform(chunk: Buffer | string, encoding: string, callback: Function): void;
8
7
  }
@@ -14,7 +14,12 @@ class MppcCompressorTransform extends stream_1.Transform {
14
14
  _transform(chunk, encoding, callback) {
15
15
  try {
16
16
  if (chunk instanceof Buffer) {
17
- callback(null, this._mppcCompressor.update(chunk));
17
+ this.push(this._mppcCompressor.update(chunk));
18
+ callback();
19
+ }
20
+ else if (typeof chunk === 'string') {
21
+ this.push(this._mppcCompressor.update(Buffer.from(chunk, encoding)));
22
+ callback();
18
23
  }
19
24
  else {
20
25
  callback(new errors_1.PwBufferTypeError('INVALID_CHUNK_TYPE'));
@@ -1,8 +1,7 @@
1
- /// <reference types="node" />
2
1
  import { MppcDecompressor } from './mppc';
3
2
  import { Transform, TransformOptions } from 'stream';
4
3
  export declare class MppcDecompressorTransform extends Transform {
5
4
  protected readonly _mppcDecompressor: MppcDecompressor;
6
5
  constructor(transformOptions?: TransformOptions);
7
- _transform(chunk: Buffer, encoding: string, callback: Function): void;
6
+ _transform(chunk: Buffer | string, encoding: string, callback: Function): void;
8
7
  }
@@ -14,7 +14,12 @@ class MppcDecompressorTransform extends stream_1.Transform {
14
14
  _transform(chunk, encoding, callback) {
15
15
  try {
16
16
  if (chunk instanceof Buffer) {
17
- callback(null, this._mppcDecompressor.update(chunk));
17
+ this.push(this._mppcDecompressor.update(chunk));
18
+ callback();
19
+ }
20
+ else if (typeof chunk === 'string') {
21
+ this.push(this._mppcDecompressor.update(Buffer.from(chunk, encoding)));
22
+ callback();
18
23
  }
19
24
  else {
20
25
  callback(new errors_1.PwBufferTypeError('INVALID_CHUNK_TYPE'));
@@ -1,13 +1,11 @@
1
- /// <reference types="node" />
2
- import { ExtendedBuffer } from 'extended-buffer';
3
1
  import type { PwBufferOptions } from './PwBufferOptions';
4
- export declare class PwBuffer extends ExtendedBuffer {
5
- protected createInstance(options?: PwBufferOptions): this;
2
+ import { ExtendedBuffer, ExtendedBufferOptions } from 'extended-buffer';
3
+ export declare class PwBuffer<PBO extends PwBufferOptions = PwBufferOptions> extends ExtendedBuffer<PBO> {
6
4
  isReadableCUInt(): boolean;
7
5
  readCUInt(): number;
8
6
  writeCUInt(value: number, unshift?: boolean): this;
9
7
  readPwString(): string;
10
8
  writePwString(string: string, unshift?: boolean): this;
11
9
  readPwOctets(): this;
12
- writePwOctets(octets: ExtendedBuffer | Buffer, unshift?: boolean): this;
10
+ writePwOctets(octets: ExtendedBuffer<ExtendedBufferOptions> | Buffer, unshift?: boolean): this;
13
11
  }
package/dist/PwBuffer.js CHANGED
@@ -1,14 +1,43 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.PwBuffer = void 0;
4
37
  const buffer_1 = require("buffer");
5
- const utils = require("./utils");
38
+ const utils = __importStar(require("./utils"));
6
39
  const extended_buffer_1 = require("extended-buffer");
7
40
  class PwBuffer extends extended_buffer_1.ExtendedBuffer {
8
- createInstance(options) {
9
- const ThisClass = this.constructor;
10
- return new ThisClass(options);
11
- }
12
41
  isReadableCUInt() {
13
42
  if (!this.isReadable(1)) {
14
43
  return false;
@@ -27,54 +56,72 @@ class PwBuffer extends extended_buffer_1.ExtendedBuffer {
27
56
  return true;
28
57
  }
29
58
  readCUInt() {
30
- let value = this.readUIntBE(1);
31
- switch (value & 0xE0) {
32
- case 0xE0: {
33
- try {
59
+ const savedPointer = this.getPointer();
60
+ try {
61
+ const value = this.readUIntBE(1);
62
+ switch (value & 0xE0) {
63
+ case 0xE0: {
34
64
  return this.readUIntBE(4);
35
65
  }
36
- catch (e) {
66
+ case 0xC0: {
37
67
  this._pointer--;
68
+ return this.readUIntBE(4) & 0x1FFFFFFF;
69
+ }
70
+ case 0x80:
71
+ case 0xA0: {
72
+ this._pointer--;
73
+ return this.readUIntBE(2) & 0x3FFF;
38
74
  }
39
75
  }
40
- case 0xC0: {
41
- this._pointer--;
42
- return this.readUIntBE(4) & 0x1FFFFFFF;
43
- }
44
- case 0x80:
45
- case 0xA0: {
46
- this._pointer--;
47
- return this.readUIntBE(2) & 0x3FFF;
48
- }
76
+ return value;
77
+ }
78
+ catch (e) {
79
+ this.setPointer(savedPointer);
80
+ throw e;
49
81
  }
50
- return value;
51
82
  }
52
83
  writeCUInt(value, unshift) {
53
- utils.writeCUIntToPwBuffer(this, value, unshift);
54
- return this;
84
+ const data = new PwBuffer({
85
+ capacity: 5,
86
+ capacityStep: 0
87
+ });
88
+ utils.writeCUIntToPwBuffer(data, value);
89
+ return this.writeNativeBuffer(data.nativeBufferView, unshift);
55
90
  }
56
91
  readPwString() {
57
- return this.readString(this.readCUInt(), 'utf16le');
92
+ const savedPointer = this.getPointer();
93
+ try {
94
+ return this.readString(this.readCUInt(), 'utf16le');
95
+ }
96
+ catch (e) {
97
+ this.setPointer(savedPointer);
98
+ throw e;
99
+ }
58
100
  }
59
101
  writePwString(string, unshift) {
60
- const bytes = buffer_1.Buffer.from(string, 'utf16le');
61
- if (unshift) {
62
- return this.writeNativeBuffer(bytes, true).writeCUInt(bytes.length, true);
63
- }
64
- return this.writeCUInt(bytes.length).writeNativeBuffer(bytes);
102
+ const octets = buffer_1.Buffer.from(string, 'utf16le');
103
+ return this.writePwOctets(octets, unshift);
65
104
  }
66
105
  readPwOctets() {
67
- let byteLength = this.readCUInt();
68
- return this.readBuffer(byteLength, false, {
69
- capacity: byteLength,
70
- capacityStep: 0
71
- });
106
+ const savedPointer = this.getPointer();
107
+ try {
108
+ const byteLength = this.readCUInt();
109
+ return this.readBuffer(byteLength, false, {
110
+ capacity: byteLength,
111
+ capacityStep: 0
112
+ });
113
+ }
114
+ catch (e) {
115
+ this.setPointer(savedPointer);
116
+ throw e;
117
+ }
72
118
  }
73
119
  writePwOctets(octets, unshift) {
74
- if (unshift) {
75
- return this.writeBuffer(octets, true).writeCUInt(octets.length, true);
76
- }
77
- return this.writeCUInt(octets.length).writeBuffer(octets);
120
+ const data = (new PwBuffer({
121
+ capacity: octets.length + 5,
122
+ capacityStep: 0
123
+ })).writeCUInt(octets.length).writeBuffer(octets);
124
+ return this.writeNativeBuffer(data.nativeBufferView, unshift);
78
125
  }
79
126
  }
80
127
  exports.PwBuffer = PwBuffer;
@@ -1,2 +1,4 @@
1
- export declare class PwBufferError extends Error {
1
+ import { ExtendedBufferError } from 'extended-buffer';
2
+ export declare class PwBufferError extends ExtendedBufferError {
3
+ constructor(message?: string);
2
4
  }
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PwBufferError = void 0;
4
- class PwBufferError extends Error {
4
+ const extended_buffer_1 = require("extended-buffer");
5
+ class PwBufferError extends extended_buffer_1.ExtendedBufferError {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = new.target.name;
9
+ if (Error.captureStackTrace) {
10
+ Error.captureStackTrace(this, new.target);
11
+ }
12
+ }
5
13
  }
6
14
  exports.PwBufferError = PwBufferError;
@@ -0,0 +1,3 @@
1
+ import { PwBufferError } from './PwBufferError';
2
+ export declare class PwBufferRangeError extends PwBufferError {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PwBufferRangeError = void 0;
4
+ const PwBufferError_1 = require("./PwBufferError");
5
+ class PwBufferRangeError extends PwBufferError_1.PwBufferError {
6
+ }
7
+ exports.PwBufferRangeError = PwBufferRangeError;
@@ -1,2 +1,3 @@
1
1
  export { PwBufferError } from './PwBufferError';
2
2
  export { PwBufferTypeError } from './PwBufferTypeError';
3
+ export { PwBufferRangeError } from './PwBufferRangeError';
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PwBufferTypeError = exports.PwBufferError = void 0;
3
+ exports.PwBufferRangeError = exports.PwBufferTypeError = exports.PwBufferError = void 0;
4
4
  var PwBufferError_1 = require("./PwBufferError");
5
5
  Object.defineProperty(exports, "PwBufferError", { enumerable: true, get: function () { return PwBufferError_1.PwBufferError; } });
6
6
  var PwBufferTypeError_1 = require("./PwBufferTypeError");
7
7
  Object.defineProperty(exports, "PwBufferTypeError", { enumerable: true, get: function () { return PwBufferTypeError_1.PwBufferTypeError; } });
8
+ var PwBufferRangeError_1 = require("./PwBufferRangeError");
9
+ Object.defineProperty(exports, "PwBufferRangeError", { enumerable: true, get: function () { return PwBufferRangeError_1.PwBufferRangeError; } });
package/dist/mppc.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export declare class MppcDecompressor {
3
2
  private history;
4
3
  private histPtr;
package/dist/mppc.js CHANGED
@@ -61,7 +61,7 @@ class BitWriter {
61
61
  }
62
62
  class MppcDecompressor {
63
63
  constructor() {
64
- this.history = new Uint8Array(MPPC_HIST_LEN);
64
+ this.history = buffer_1.Buffer.alloc(MPPC_HIST_LEN);
65
65
  this.histPtr = 0;
66
66
  this.bitOffset = 0;
67
67
  this.legacy = buffer_1.Buffer.alloc(0);
@@ -125,7 +125,7 @@ class MppcDecompressor {
125
125
  }
126
126
  }
127
127
  if (this.histPtr > histHead) {
128
- outParts.push(buffer_1.Buffer.from(this.history.subarray(histHead, this.histPtr)));
128
+ outParts.push(buffer_1.Buffer.from((0, extended_buffer_1.nativeBufferSubarray)(this.history, histHead, this.histPtr)));
129
129
  }
130
130
  if (this.histPtr === MPPC_HIST_LEN) {
131
131
  this.histPtr = 0;
@@ -236,7 +236,7 @@ class MppcDecompressor {
236
236
  this.histPtr = dstEnd;
237
237
  }
238
238
  if (this.histPtr > histHead) {
239
- outParts.push(buffer_1.Buffer.from(this.history.subarray(histHead, this.histPtr)));
239
+ outParts.push(buffer_1.Buffer.from((0, extended_buffer_1.nativeBufferSubarray)(this.history, histHead, this.histPtr)));
240
240
  }
241
241
  this.legacy = (0, extended_buffer_1.nativeBufferSubarray)(this.legacy, rptr);
242
242
  this.bitOffset = bitShift;
@@ -246,7 +246,7 @@ class MppcDecompressor {
246
246
  exports.MppcDecompressor = MppcDecompressor;
247
247
  class MppcCompressor {
248
248
  constructor() {
249
- this.history = new Uint8Array(MPPC_HIST_LEN);
249
+ this.history = buffer_1.Buffer.alloc(MPPC_HIST_LEN);
250
250
  this.histPtr = 0;
251
251
  this.bw = new BitWriter();
252
252
  this.dict = new Map();
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeCUIntToPwBuffer = void 0;
3
+ exports.writeCUIntToPwBuffer = writeCUIntToPwBuffer;
4
+ const errors_1 = require("../errors");
4
5
  const extended_buffer_1 = require("extended-buffer");
5
6
  function writeCUIntToPwBuffer(buffer, value, unshift) {
6
7
  (0, extended_buffer_1.assertInteger)(value);
7
8
  if (value < 0 || value > 0xFFFFFFFF) {
8
- throw new extended_buffer_1.ExtendedBufferRangeError('CUINT_VALUE_OUT_OF_RANGE');
9
+ throw new errors_1.PwBufferRangeError('CUINT_VALUE_OUT_OF_RANGE');
9
10
  }
10
11
  if (value < 0x80) {
11
12
  buffer.writeUIntBE(value & 0xFF, 1, unshift);
@@ -17,19 +18,11 @@ function writeCUIntToPwBuffer(buffer, value, unshift) {
17
18
  buffer.writeUIntBE((value | 0xC0000000) >>> 0, 4, unshift);
18
19
  }
19
20
  else {
20
- const savedPointer = buffer.getPointer();
21
- try {
22
- if (unshift) {
23
- buffer.allocStart(5).writeUIntBE(value >>> 0, 4, true).writeUIntBE(0xE0, 1, true);
24
- }
25
- else {
26
- buffer.allocEnd(5).writeUIntBE(0xE0, 1).writeUIntBE(value >>> 0, 4);
27
- }
21
+ if (unshift) {
22
+ buffer.allocStart(5).writeUIntBE(value >>> 0, 4, true).writeUIntBE(0xE0, 1, true);
28
23
  }
29
- catch (e) {
30
- buffer.setPointer(savedPointer);
31
- throw e;
24
+ else {
25
+ buffer.allocEnd(5).writeUIntBE(0xE0, 1).writeUIntBE(value >>> 0, 4);
32
26
  }
33
27
  }
34
28
  }
35
- exports.writeCUIntToPwBuffer = writeCUIntToPwBuffer;
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "pw-buffer",
3
- "version": "3.0.2",
3
+ "version": "3.1.0",
4
4
  "description": "PW Buffer, Perfect World, Beijing Perfect World",
5
+ "type": "commonjs",
5
6
  "main": "./dist/index.js",
6
7
  "types": "./dist/index.d.ts",
7
8
  "exports": {
8
9
  ".": {
9
10
  "types": "./dist/index.d.ts",
10
11
  "require": "./dist/index.js",
12
+ "import": "./dist/index.js",
11
13
  "default": "./dist/index.js"
12
14
  }
13
15
  },
@@ -47,10 +49,10 @@
47
49
  "README.md"
48
50
  ],
49
51
  "dependencies": {
50
- "extended-buffer": "^7.0.2"
52
+ "extended-buffer": "^7.3.0"
51
53
  },
52
54
  "devDependencies": {
53
55
  "@types/node": "^6.14.13",
54
- "typescript": "^4.7.2"
56
+ "typescript": "^5.9.3"
55
57
  }
56
58
  }