pw-buffer 3.0.1 → 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 +1 -1
- package/README.md +2 -0
- package/dist/MppcCompressorTransform.d.ts +1 -2
- package/dist/MppcCompressorTransform.js +6 -1
- package/dist/MppcDecompressorTransform.d.ts +1 -2
- package/dist/MppcDecompressorTransform.js +6 -1
- package/dist/PwBuffer.d.ts +3 -5
- package/dist/PwBuffer.js +84 -37
- package/dist/errors/PwBufferError.d.ts +3 -1
- package/dist/errors/PwBufferError.js +9 -1
- package/dist/errors/PwBufferRangeError.d.ts +3 -0
- package/dist/errors/PwBufferRangeError.js +7 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +3 -1
- package/dist/mppc.d.ts +0 -1
- package/dist/mppc.js +4 -4
- package/dist/utils/write-cuint-to-pw-buffer.js +7 -14
- package/package.json +5 -3
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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'));
|
package/dist/PwBuffer.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { ExtendedBuffer } from 'extended-buffer';
|
|
3
1
|
import type { PwBufferOptions } from './PwBufferOptions';
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
54
|
-
|
|
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
|
-
|
|
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
|
|
61
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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,6 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PwBufferError = void 0;
|
|
4
|
-
|
|
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,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;
|
package/dist/errors/index.d.ts
CHANGED
package/dist/errors/index.js
CHANGED
|
@@ -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
package/dist/mppc.js
CHANGED
|
@@ -61,7 +61,7 @@ class BitWriter {
|
|
|
61
61
|
}
|
|
62
62
|
class MppcDecompressor {
|
|
63
63
|
constructor() {
|
|
64
|
-
this.history =
|
|
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
|
|
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
|
|
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 =
|
|
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 =
|
|
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
|
|
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
|
-
|
|
21
|
-
|
|
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
|
-
|
|
30
|
-
buffer.
|
|
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
|
|
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
|
|
52
|
+
"extended-buffer": "^7.3.0"
|
|
51
53
|
},
|
|
52
54
|
"devDependencies": {
|
|
53
55
|
"@types/node": "^6.14.13",
|
|
54
|
-
"typescript": "^
|
|
56
|
+
"typescript": "^5.9.3"
|
|
55
57
|
}
|
|
56
58
|
}
|