@nice2dev/ui-graphics 1.3.14 → 1.3.16
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/CHANGELOG.md +13 -0
- package/dist/cjs/animation/AnimatedPerson.js +470 -143
- package/dist/cjs/animation/AnimationEditor.js +289 -101
- package/dist/cjs/animation/Audience.js +83 -61
- package/dist/cjs/animation/BodyRenderer.js +69 -28
- package/dist/cjs/animation/animationHelper.js +281 -288
- package/dist/cjs/animation/characterTypes.js +200 -203
- package/dist/cjs/animation/choreoDSL.js +205 -219
- package/dist/cjs/animation/choreography.js +4 -32
- package/dist/cjs/animation/karaokeIntegration.js +68 -77
- package/dist/cjs/animation/lottieExport.js +108 -114
- package/dist/cjs/animation/motionPath.js +225 -248
- package/dist/cjs/animation/physics.js +227 -254
- package/dist/cjs/animation/rig/RigPlayer.js +53 -56
- package/dist/cjs/animation/rig/RiggedBody.js +71 -26
- package/dist/cjs/animation/rig/choreoRigBridge.js +72 -77
- package/dist/cjs/animation/rig/ik.js +13 -18
- package/dist/cjs/animation/rig/poseOps.js +34 -41
- package/dist/cjs/animation/rig/presets.js +64 -72
- package/dist/cjs/animation/rig/rigMath.js +25 -32
- package/dist/cjs/animation/rig/rigTypes.js +59 -63
- package/dist/cjs/animation/shapes/arms.js +31 -13
- package/dist/cjs/animation/shapes/eyes.js +129 -47
- package/dist/cjs/animation/shapes/hair.js +72 -45
- package/dist/cjs/animation/shapes/heads.js +131 -57
- package/dist/cjs/animation/shapes/headwear.js +47 -38
- package/dist/cjs/animation/shapes/legs.js +31 -13
- package/dist/cjs/animation/shapes/mouths.js +120 -44
- package/dist/cjs/animation/shapes/noses.js +80 -25
- package/dist/cjs/animation/shapes/outfits.js +101 -39
- package/dist/cjs/animation/shapes/torsos.js +15 -11
- package/dist/cjs/animation/spineExport.js +103 -121
- package/dist/cjs/core/LocalUI.js +108 -43
- package/dist/cjs/core/NiceBrushEngine.js +758 -552
- package/dist/cjs/core/collaboration.js +249 -251
- package/dist/cjs/core/colorBlindness.js +100 -115
- package/dist/cjs/core/dragDrop.js +155 -158
- package/dist/cjs/core/editorAsk.js +51 -41
- package/dist/cjs/core/fixes.js +140 -171
- package/dist/cjs/core/gridGuides.js +166 -201
- package/dist/cjs/core/historyVisual.js +156 -135
- package/dist/cjs/core/i18n.js +13 -14
- package/dist/cjs/core/integrations.js +238 -249
- package/dist/cjs/core/minimap.js +149 -151
- package/dist/cjs/core/plugins.js +98 -113
- package/dist/cjs/core/rtl.js +71 -83
- package/dist/cjs/core/shortcuts.js +196 -225
- package/dist/cjs/core/theme.js +142 -149
- package/dist/cjs/core/yjsCollaboration.js +232 -293
- package/dist/cjs/design/design-tools.js +12 -22
- package/dist/cjs/font/NiceFontEditor.js +877 -316
- package/dist/cjs/font/fontFormatUtils.js +388 -448
- package/dist/cjs/game/GameAsset2dEditor.js +1201 -392
- package/dist/cjs/game/GameAsset2dEditor.module.css.js +146 -5
- package/dist/cjs/game/gameAssetTypes.js +7 -16
- package/dist/cjs/game/gameAssetUtils.js +390 -413
- package/dist/cjs/game/useGameAssetEditor.js +820 -763
- package/dist/cjs/index.js +81 -84
- package/dist/cjs/nice2dev-ui-graphics.css +1 -1
- package/dist/cjs/node_modules/lib0/array.js +8 -70
- package/dist/cjs/node_modules/lib0/binary.js +3 -20
- package/dist/cjs/node_modules/lib0/broadcastchannel.js +24 -86
- package/dist/cjs/node_modules/lib0/buffer.js +15 -82
- package/dist/cjs/node_modules/lib0/conditions.js +3 -16
- package/dist/cjs/node_modules/lib0/decoding.js +108 -287
- package/dist/cjs/node_modules/lib0/dom.js +14 -44
- package/dist/cjs/node_modules/lib0/encoding.js +68 -418
- package/dist/cjs/node_modules/lib0/environment.js +26 -92
- package/dist/cjs/node_modules/lib0/error.js +5 -29
- package/dist/cjs/node_modules/lib0/function.js +27 -66
- package/dist/cjs/node_modules/lib0/iterator.js +8 -33
- package/dist/cjs/node_modules/lib0/logging.common.js +12 -22
- package/dist/cjs/node_modules/lib0/logging.js +32 -71
- package/dist/cjs/node_modules/lib0/map.js +16 -86
- package/dist/cjs/node_modules/lib0/math.js +3 -32
- package/dist/cjs/node_modules/lib0/number.js +4 -15
- package/dist/cjs/node_modules/lib0/object.js +14 -88
- package/dist/cjs/node_modules/lib0/observable.js +38 -78
- package/dist/cjs/node_modules/lib0/pair.js +3 -20
- package/dist/cjs/node_modules/lib0/prng.js +8 -77
- package/dist/cjs/node_modules/lib0/promise.js +6 -30
- package/dist/cjs/node_modules/lib0/random.js +8 -23
- package/dist/cjs/node_modules/lib0/schema.js +313 -687
- package/dist/cjs/node_modules/lib0/set.js +3 -10
- package/dist/cjs/node_modules/lib0/storage.js +20 -55
- package/dist/cjs/node_modules/lib0/string.js +17 -72
- package/dist/cjs/node_modules/lib0/symbol.js +2 -12
- package/dist/cjs/node_modules/lib0/time.js +2 -15
- package/dist/cjs/node_modules/lib0/trait/equality.js +3 -28
- package/dist/cjs/node_modules/lib0/url.js +4 -18
- package/dist/cjs/node_modules/lib0/webcrypto.js +2 -5
- package/dist/cjs/node_modules/y-protocols/auth.js +5 -19
- package/dist/cjs/node_modules/y-protocols/awareness.js +53 -116
- package/dist/cjs/node_modules/y-protocols/sync.js +16 -103
- package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +77 -219
- package/dist/cjs/node_modules/yjs/dist/yjs.js +2128 -4868
- package/dist/cjs/photo/FilterThumb.js +37 -25
- package/dist/cjs/photo/NiceAITools.js +679 -467
- package/dist/cjs/photo/PhotoEditor.js +34 -20
- package/dist/cjs/photo/PhotoEditor.module.css.js +296 -5
- package/dist/cjs/photo/PhotoEditorCanvas.js +250 -91
- package/dist/cjs/photo/PhotoEditorLeftPanel.js +1086 -224
- package/dist/cjs/photo/PhotoEditorToolbar.js +135 -19
- package/dist/cjs/photo/photoEditorActions.js +89 -72
- package/dist/cjs/photo/photoEditorTypes.js +29 -32
- package/dist/cjs/photo/photoFilters.js +959 -985
- package/dist/cjs/photo/photoOverlays.js +899 -945
- package/dist/cjs/photo/photoSelection.js +427 -499
- package/dist/cjs/photo/usePhotoEditor.js +739 -741
- package/dist/cjs/pixel/HSVPicker.js +191 -175
- package/dist/cjs/pixel/PixelEditor.js +159 -48
- package/dist/cjs/pixel/PixelEditor.module.css.js +209 -5
- package/dist/cjs/pixel/PixelEditorCanvas.js +29 -16
- package/dist/cjs/pixel/PixelEditorMenuBar.js +160 -162
- package/dist/cjs/pixel/PixelEditorRightPanel.js +377 -45
- package/dist/cjs/pixel/PixelEditorStatusBar.js +47 -20
- package/dist/cjs/pixel/PixelEditorTimeline.js +106 -23
- package/dist/cjs/pixel/PixelEditorToolbar.js +99 -21
- package/dist/cjs/pixel/SpriteGeneratorPanel.js +483 -303
- package/dist/cjs/pixel/animalGeneratorPresets.js +776 -601
- package/dist/cjs/pixel/asepriteFormat.js +511 -560
- package/dist/cjs/pixel/buildingGeneratorPresets.js +693 -520
- package/dist/cjs/pixel/characterGeneratorPresets.js +644 -544
- package/dist/cjs/pixel/pixel-pro.js +52 -63
- package/dist/cjs/pixel/pixelEditorExports.js +388 -428
- package/dist/cjs/pixel/pixelEditorTypes.js +91 -108
- package/dist/cjs/pixel/pixelEditorUtils.js +519 -561
- package/dist/cjs/pixel/spriteGeneratorCore.js +206 -257
- package/dist/cjs/pixel/usePixelEditor.js +1321 -1343
- package/dist/cjs/texture/Nice3DTexturePainter.js +707 -440
- package/dist/cjs/ui/NiceUIDesigner.js +954 -527
- package/dist/cjs/vector/NiceSvgToCode.js +586 -446
- package/dist/cjs/vector/VectorEditor.js +290 -117
- package/dist/cjs/vector/VectorEditor.module.css.js +86 -5
- package/dist/cjs/vector/VectorEditorMenuBar.js +333 -275
- package/dist/cjs/vector/VectorEditorRightPanel.js +1094 -371
- package/dist/cjs/vector/VectorEditorShapeRenderer.js +198 -60
- package/dist/cjs/vector/VectorEditorStatusBar.js +39 -13
- package/dist/cjs/vector/useVectorEditor.js +848 -889
- package/dist/cjs/vector/vectorAnimation.js +110 -137
- package/dist/cjs/vector/vectorBooleanOps.js +398 -485
- package/dist/cjs/vector/vectorEditorExport.js +201 -203
- package/dist/cjs/vector/vectorEditorImport.js +173 -189
- package/dist/cjs/vector/vectorEditorTypes.js +42 -49
- package/dist/cjs/vector/vectorGradients.js +151 -174
- package/dist/cjs/vector/vectorIconExport.js +52 -59
- package/dist/cjs/vector/vectorIconImport.js +169 -219
- package/dist/esm/animation/AnimatedPerson.js +470 -141
- package/dist/esm/animation/AnimationEditor.js +290 -100
- package/dist/esm/animation/Audience.js +83 -59
- package/dist/esm/animation/BodyRenderer.js +71 -25
- package/dist/esm/animation/animationHelper.js +284 -287
- package/dist/esm/animation/characterTypes.js +204 -202
- package/dist/esm/animation/choreoDSL.js +212 -218
- package/dist/esm/animation/choreography.js +8 -31
- package/dist/esm/animation/karaokeIntegration.js +74 -76
- package/dist/esm/animation/lottieExport.js +112 -113
- package/dist/esm/animation/motionPath.js +235 -247
- package/dist/esm/animation/physics.js +235 -253
- package/dist/esm/animation/rig/RigPlayer.js +53 -54
- package/dist/esm/animation/rig/RiggedBody.js +71 -24
- package/dist/esm/animation/rig/choreoRigBridge.js +74 -76
- package/dist/esm/animation/rig/ik.js +14 -17
- package/dist/esm/animation/rig/poseOps.js +37 -40
- package/dist/esm/animation/rig/presets.js +70 -71
- package/dist/esm/animation/rig/rigMath.js +28 -31
- package/dist/esm/animation/rig/rigTypes.js +62 -62
- package/dist/esm/animation/shapes/arms.js +32 -12
- package/dist/esm/animation/shapes/eyes.js +135 -46
- package/dist/esm/animation/shapes/hair.js +80 -44
- package/dist/esm/animation/shapes/heads.js +140 -56
- package/dist/esm/animation/shapes/headwear.js +54 -37
- package/dist/esm/animation/shapes/legs.js +32 -12
- package/dist/esm/animation/shapes/mouths.js +127 -43
- package/dist/esm/animation/shapes/noses.js +87 -24
- package/dist/esm/animation/shapes/outfits.js +106 -38
- package/dist/esm/animation/shapes/torsos.js +16 -10
- package/dist/esm/animation/spineExport.js +106 -120
- package/dist/esm/core/LocalUI.js +111 -42
- package/dist/esm/core/NiceBrushEngine.js +761 -551
- package/dist/esm/core/collaboration.js +255 -250
- package/dist/esm/core/colorBlindness.js +106 -114
- package/dist/esm/core/dragDrop.js +157 -157
- package/dist/esm/core/editorAsk.js +53 -40
- package/dist/esm/core/fixes.js +145 -170
- package/dist/esm/core/gridGuides.js +178 -200
- package/dist/esm/core/historyVisual.js +158 -134
- package/dist/esm/core/i18n.js +15 -13
- package/dist/esm/core/integrations.js +244 -248
- package/dist/esm/core/minimap.js +151 -150
- package/dist/esm/core/plugins.js +102 -112
- package/dist/esm/core/rtl.js +77 -82
- package/dist/esm/core/shortcuts.js +200 -224
- package/dist/esm/core/theme.js +150 -148
- package/dist/esm/core/yjsCollaboration.js +240 -292
- package/dist/esm/design/design-tools.js +13 -21
- package/dist/esm/font/NiceFontEditor.js +880 -315
- package/dist/esm/font/fontFormatUtils.js +394 -447
- package/dist/esm/game/GameAsset2dEditor.js +1202 -391
- package/dist/esm/game/GameAsset2dEditor.module.css.js +146 -3
- package/dist/esm/game/gameAssetTypes.js +8 -15
- package/dist/esm/game/gameAssetUtils.js +408 -412
- package/dist/esm/game/useGameAssetEditor.js +821 -762
- package/dist/esm/index.js +332 -69
- package/dist/esm/nice2dev-ui-graphics.css +1 -1
- package/dist/esm/node_modules/lib0/array.js +15 -69
- package/dist/esm/node_modules/lib0/binary.js +14 -19
- package/dist/esm/node_modules/lib0/broadcastchannel.js +27 -85
- package/dist/esm/node_modules/lib0/buffer.js +20 -80
- package/dist/esm/node_modules/lib0/conditions.js +4 -15
- package/dist/esm/node_modules/lib0/decoding.js +128 -286
- package/dist/esm/node_modules/lib0/dom.js +20 -43
- package/dist/esm/node_modules/lib0/encoding.js +90 -415
- package/dist/esm/node_modules/lib0/environment.js +33 -91
- package/dist/esm/node_modules/lib0/error.js +8 -28
- package/dist/esm/node_modules/lib0/function.js +31 -65
- package/dist/esm/node_modules/lib0/iterator.js +11 -32
- package/dist/esm/node_modules/lib0/logging.common.js +22 -21
- package/dist/esm/node_modules/lib0/logging.js +44 -70
- package/dist/esm/node_modules/lib0/map.js +21 -85
- package/dist/esm/node_modules/lib0/math.js +9 -31
- package/dist/esm/node_modules/lib0/number.js +7 -14
- package/dist/esm/node_modules/lib0/object.js +26 -87
- package/dist/esm/node_modules/lib0/observable.js +40 -77
- package/dist/esm/node_modules/lib0/pair.js +5 -19
- package/dist/esm/node_modules/lib0/prng.js +15 -76
- package/dist/esm/node_modules/lib0/promise.js +7 -29
- package/dist/esm/node_modules/lib0/random.js +10 -22
- package/dist/esm/node_modules/lib0/schema.js +367 -686
- package/dist/esm/node_modules/lib0/set.js +4 -9
- package/dist/esm/node_modules/lib0/storage.js +23 -54
- package/dist/esm/node_modules/lib0/string.js +26 -71
- package/dist/esm/node_modules/lib0/symbol.js +3 -11
- package/dist/esm/node_modules/lib0/time.js +3 -14
- package/dist/esm/node_modules/lib0/trait/equality.js +5 -27
- package/dist/esm/node_modules/lib0/url.js +5 -17
- package/dist/esm/node_modules/lib0/webcrypto.js +3 -4
- package/dist/esm/node_modules/y-protocols/auth.js +7 -18
- package/dist/esm/node_modules/y-protocols/awareness.js +58 -115
- package/dist/esm/node_modules/y-protocols/sync.js +26 -102
- package/dist/esm/node_modules/y-websocket/src/y-websocket.js +82 -218
- package/dist/esm/node_modules/yjs/dist/yjs.js +2263 -4899
- package/dist/esm/photo/FilterThumb.js +37 -23
- package/dist/esm/photo/NiceAITools.js +685 -466
- package/dist/esm/photo/PhotoEditor.js +34 -18
- package/dist/esm/photo/PhotoEditor.module.css.js +296 -3
- package/dist/esm/photo/PhotoEditorCanvas.js +250 -89
- package/dist/esm/photo/PhotoEditorLeftPanel.js +1086 -222
- package/dist/esm/photo/PhotoEditorToolbar.js +135 -17
- package/dist/esm/photo/photoEditorActions.js +93 -71
- package/dist/esm/photo/photoEditorTypes.js +32 -31
- package/dist/esm/photo/photoFilters.js +967 -984
- package/dist/esm/photo/photoOverlays.js +913 -944
- package/dist/esm/photo/photoSelection.js +438 -498
- package/dist/esm/photo/usePhotoEditor.js +740 -740
- package/dist/esm/pixel/HSVPicker.js +192 -174
- package/dist/esm/pixel/PixelEditor.js +160 -47
- package/dist/esm/pixel/PixelEditor.module.css.js +209 -3
- package/dist/esm/pixel/PixelEditorCanvas.js +29 -14
- package/dist/esm/pixel/PixelEditorMenuBar.js +160 -160
- package/dist/esm/pixel/PixelEditorRightPanel.js +377 -43
- package/dist/esm/pixel/PixelEditorStatusBar.js +47 -18
- package/dist/esm/pixel/PixelEditorTimeline.js +106 -21
- package/dist/esm/pixel/PixelEditorToolbar.js +99 -19
- package/dist/esm/pixel/SpriteGeneratorPanel.js +483 -301
- package/dist/esm/pixel/animalGeneratorPresets.js +782 -600
- package/dist/esm/pixel/asepriteFormat.js +517 -559
- package/dist/esm/pixel/buildingGeneratorPresets.js +699 -519
- package/dist/esm/pixel/characterGeneratorPresets.js +650 -543
- package/dist/esm/pixel/pixel-pro.js +53 -62
- package/dist/esm/pixel/pixelEditorExports.js +397 -427
- package/dist/esm/pixel/pixelEditorTypes.js +103 -107
- package/dist/esm/pixel/pixelEditorUtils.js +551 -560
- package/dist/esm/pixel/spriteGeneratorCore.js +223 -256
- package/dist/esm/pixel/usePixelEditor.js +1322 -1342
- package/dist/esm/texture/Nice3DTexturePainter.js +709 -439
- package/dist/esm/ui/NiceUIDesigner.js +957 -526
- package/dist/esm/vector/NiceSvgToCode.js +589 -445
- package/dist/esm/vector/VectorEditor.js +291 -116
- package/dist/esm/vector/VectorEditor.module.css.js +86 -3
- package/dist/esm/vector/VectorEditorMenuBar.js +333 -273
- package/dist/esm/vector/VectorEditorRightPanel.js +1094 -369
- package/dist/esm/vector/VectorEditorShapeRenderer.js +199 -59
- package/dist/esm/vector/VectorEditorStatusBar.js +39 -11
- package/dist/esm/vector/useVectorEditor.js +849 -888
- package/dist/esm/vector/vectorAnimation.js +115 -136
- package/dist/esm/vector/vectorBooleanOps.js +401 -484
- package/dist/esm/vector/vectorEditorExport.js +207 -202
- package/dist/esm/vector/vectorEditorImport.js +174 -188
- package/dist/esm/vector/vectorEditorTypes.js +50 -48
- package/dist/esm/vector/vectorGradients.js +158 -173
- package/dist/esm/vector/vectorIconExport.js +53 -58
- package/dist/esm/vector/vectorIconImport.js +171 -218
- package/package.json +9 -15
|
@@ -1,83 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Efficient schema-less binary encoding with support for variable length encoding.
|
|
11
|
-
*
|
|
12
|
-
* Use [lib0/encoding] with [lib0/decoding]. Every encoding function has a corresponding decoding function.
|
|
13
|
-
*
|
|
14
|
-
* Encodes numbers in little-endian order (least to most significant byte order)
|
|
15
|
-
* and is compatible with Golang's binary encoding (https://golang.org/pkg/encoding/binary/)
|
|
16
|
-
* which is also used in Protocol Buffers.
|
|
17
|
-
*
|
|
18
|
-
* ```js
|
|
19
|
-
* // encoding step
|
|
20
|
-
* const encoder = encoding.createEncoder()
|
|
21
|
-
* encoding.writeVarUint(encoder, 256)
|
|
22
|
-
* encoding.writeVarString(encoder, 'Hello world!')
|
|
23
|
-
* const buf = encoding.toUint8Array(encoder)
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* ```js
|
|
27
|
-
* // decoding step
|
|
28
|
-
* const decoder = decoding.createDecoder(buf)
|
|
29
|
-
* decoding.readVarUint(decoder) // => 256
|
|
30
|
-
* decoding.readVarString(decoder) // => 'Hello world!'
|
|
31
|
-
* decoding.hasContent(decoder) // => false - all data is read
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @module encoding
|
|
35
|
-
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* A BinaryEncoder handles the encoding to an Uint8Array.
|
|
40
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const math = require("./math.js");
|
|
4
|
+
const number = require("./number.js");
|
|
5
|
+
const binary = require("./binary.js");
|
|
6
|
+
const string = require("./string.js");
|
|
7
|
+
const array = require("./array.js");
|
|
41
8
|
class Encoder {
|
|
42
|
-
constructor
|
|
9
|
+
constructor() {
|
|
43
10
|
this.cpos = 0;
|
|
44
11
|
this.cbuf = new Uint8Array(100);
|
|
45
|
-
/**
|
|
46
|
-
* @type {Array<Uint8Array>}
|
|
47
|
-
*/
|
|
48
12
|
this.bufs = [];
|
|
49
13
|
}
|
|
50
14
|
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @function
|
|
54
|
-
* @return {Encoder}
|
|
55
|
-
*/
|
|
56
15
|
const createEncoder = () => new Encoder();
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* The current length of the encoded data.
|
|
60
|
-
*
|
|
61
|
-
* @function
|
|
62
|
-
* @param {Encoder} encoder
|
|
63
|
-
* @return {number}
|
|
64
|
-
*/
|
|
65
|
-
const length = encoder => {
|
|
16
|
+
const length = (encoder) => {
|
|
66
17
|
let len = encoder.cpos;
|
|
67
18
|
for (let i = 0; i < encoder.bufs.length; i++) {
|
|
68
19
|
len += encoder.bufs[i].length;
|
|
69
20
|
}
|
|
70
|
-
return len
|
|
21
|
+
return len;
|
|
71
22
|
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Transform to Uint8Array.
|
|
75
|
-
*
|
|
76
|
-
* @function
|
|
77
|
-
* @param {Encoder} encoder
|
|
78
|
-
* @return {Uint8Array<ArrayBuffer>} The created ArrayBuffer.
|
|
79
|
-
*/
|
|
80
|
-
const toUint8Array = encoder => {
|
|
23
|
+
const toUint8Array = (encoder) => {
|
|
81
24
|
const uint8arr = new Uint8Array(length(encoder));
|
|
82
25
|
let curPos = 0;
|
|
83
26
|
for (let i = 0; i < encoder.bufs.length; i++) {
|
|
@@ -86,16 +29,8 @@ const toUint8Array = encoder => {
|
|
|
86
29
|
curPos += d.length;
|
|
87
30
|
}
|
|
88
31
|
uint8arr.set(new Uint8Array(encoder.cbuf.buffer, 0, encoder.cpos), curPos);
|
|
89
|
-
return uint8arr
|
|
32
|
+
return uint8arr;
|
|
90
33
|
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Verify that it is possible to write `len` bytes wtihout checking. If
|
|
94
|
-
* necessary, a new Buffer with the required length is attached.
|
|
95
|
-
*
|
|
96
|
-
* @param {Encoder} encoder
|
|
97
|
-
* @param {number} len
|
|
98
|
-
*/
|
|
99
34
|
const verifyLen = (encoder, len) => {
|
|
100
35
|
const bufferLen = encoder.cbuf.length;
|
|
101
36
|
if (bufferLen - encoder.cpos < len) {
|
|
@@ -104,14 +39,6 @@ const verifyLen = (encoder, len) => {
|
|
|
104
39
|
encoder.cpos = 0;
|
|
105
40
|
}
|
|
106
41
|
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Write one byte to the encoder.
|
|
110
|
-
*
|
|
111
|
-
* @function
|
|
112
|
-
* @param {Encoder} encoder
|
|
113
|
-
* @param {number} num The byte that is to be encoded.
|
|
114
|
-
*/
|
|
115
42
|
const write = (encoder, num) => {
|
|
116
43
|
const bufferLen = encoder.cbuf.length;
|
|
117
44
|
if (encoder.cpos === bufferLen) {
|
|
@@ -121,73 +48,30 @@ const write = (encoder, num) => {
|
|
|
121
48
|
}
|
|
122
49
|
encoder.cbuf[encoder.cpos++] = num;
|
|
123
50
|
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Write one byte as an unsigned integer.
|
|
127
|
-
*
|
|
128
|
-
* @function
|
|
129
|
-
* @param {Encoder} encoder
|
|
130
|
-
* @param {number} num The number that is to be encoded.
|
|
131
|
-
*/
|
|
132
51
|
const writeUint8 = write;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Write a variable length unsigned integer. Max encodable integer is 2^53.
|
|
136
|
-
*
|
|
137
|
-
* @function
|
|
138
|
-
* @param {Encoder} encoder
|
|
139
|
-
* @param {number} num The number that is to be encoded.
|
|
140
|
-
*/
|
|
141
52
|
const writeVarUint = (encoder, num) => {
|
|
142
53
|
while (num > binary.BITS7) {
|
|
143
|
-
write(encoder, binary.BIT8 |
|
|
144
|
-
num = math.floor(num / 128);
|
|
54
|
+
write(encoder, binary.BIT8 | binary.BITS7 & num);
|
|
55
|
+
num = math.floor(num / 128);
|
|
145
56
|
}
|
|
146
57
|
write(encoder, binary.BITS7 & num);
|
|
147
58
|
};
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Write a variable length integer.
|
|
151
|
-
*
|
|
152
|
-
* We use the 7th bit instead for signaling that this is a negative number.
|
|
153
|
-
*
|
|
154
|
-
* @function
|
|
155
|
-
* @param {Encoder} encoder
|
|
156
|
-
* @param {number} num The number that is to be encoded.
|
|
157
|
-
*/
|
|
158
59
|
const writeVarInt = (encoder, num) => {
|
|
159
60
|
const isNegative = math.isNegativeZero(num);
|
|
160
61
|
if (isNegative) {
|
|
161
62
|
num = -num;
|
|
162
63
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
num = math.floor(num / 64); // shift >>> 6
|
|
166
|
-
// We don't need to consider the case of num === 0 so we can use a different
|
|
167
|
-
// pattern here than above.
|
|
64
|
+
write(encoder, (num > binary.BITS6 ? binary.BIT8 : 0) | (isNegative ? binary.BIT7 : 0) | binary.BITS6 & num);
|
|
65
|
+
num = math.floor(num / 64);
|
|
168
66
|
while (num > 0) {
|
|
169
|
-
write(encoder, (num > binary.BITS7 ? binary.BIT8 : 0) |
|
|
170
|
-
num = math.floor(num / 128);
|
|
67
|
+
write(encoder, (num > binary.BITS7 ? binary.BIT8 : 0) | binary.BITS7 & num);
|
|
68
|
+
num = math.floor(num / 128);
|
|
171
69
|
}
|
|
172
70
|
};
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* A cache to store strings temporarily
|
|
176
|
-
*/
|
|
177
|
-
const _strBuffer = new Uint8Array(30000);
|
|
71
|
+
const _strBuffer = new Uint8Array(3e4);
|
|
178
72
|
const _maxStrBSize = _strBuffer.length / 3;
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Write a variable length string.
|
|
182
|
-
*
|
|
183
|
-
* @function
|
|
184
|
-
* @param {Encoder} encoder
|
|
185
|
-
* @param {String} str The string that is to be encoded.
|
|
186
|
-
*/
|
|
187
73
|
const _writeVarStringNative = (encoder, str) => {
|
|
188
74
|
if (str.length < _maxStrBSize) {
|
|
189
|
-
// We can encode the string into the existing buffer
|
|
190
|
-
/* c8 ignore next */
|
|
191
75
|
const written = string.utf8TextEncoder.encodeInto(str, _strBuffer).written || 0;
|
|
192
76
|
writeVarUint(encoder, written);
|
|
193
77
|
for (let i = 0; i < written; i++) {
|
|
@@ -197,53 +81,21 @@ const _writeVarStringNative = (encoder, str) => {
|
|
|
197
81
|
writeVarUint8Array(encoder, string.encodeUtf8(str));
|
|
198
82
|
}
|
|
199
83
|
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Write a variable length string.
|
|
203
|
-
*
|
|
204
|
-
* @function
|
|
205
|
-
* @param {Encoder} encoder
|
|
206
|
-
* @param {String} str The string that is to be encoded.
|
|
207
|
-
*/
|
|
208
84
|
const _writeVarStringPolyfill = (encoder, str) => {
|
|
209
85
|
const encodedString = unescape(encodeURIComponent(str));
|
|
210
86
|
const len = encodedString.length;
|
|
211
87
|
writeVarUint(encoder, len);
|
|
212
88
|
for (let i = 0; i < len; i++) {
|
|
213
|
-
write(
|
|
89
|
+
write(
|
|
90
|
+
encoder,
|
|
91
|
+
/** @type {number} */
|
|
92
|
+
encodedString.codePointAt(i)
|
|
93
|
+
);
|
|
214
94
|
}
|
|
215
95
|
};
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
* Write a variable length string.
|
|
219
|
-
*
|
|
220
|
-
* @function
|
|
221
|
-
* @param {Encoder} encoder
|
|
222
|
-
* @param {String} str The string that is to be encoded.
|
|
223
|
-
*/
|
|
224
|
-
/* c8 ignore next */
|
|
225
|
-
const writeVarString = (string.utf8TextEncoder && /** @type {any} */ (string.utf8TextEncoder).encodeInto) ? _writeVarStringNative : _writeVarStringPolyfill;
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Write the content of another Encoder.
|
|
229
|
-
*
|
|
230
|
-
* @TODO: can be improved!
|
|
231
|
-
* - Note: Should consider that when appending a lot of small Encoders, we should rather clone than referencing the old structure.
|
|
232
|
-
* Encoders start with a rather big initial buffer.
|
|
233
|
-
*
|
|
234
|
-
* @function
|
|
235
|
-
* @param {Encoder} encoder The enUint8Arr
|
|
236
|
-
* @param {Encoder} append The BinaryEncoder to be written.
|
|
237
|
-
*/
|
|
96
|
+
const writeVarString = string.utf8TextEncoder && /** @type {any} */
|
|
97
|
+
string.utf8TextEncoder.encodeInto ? _writeVarStringNative : _writeVarStringPolyfill;
|
|
238
98
|
const writeBinaryEncoder = (encoder, append) => writeUint8Array(encoder, toUint8Array(append));
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Append fixed-length Uint8Array to the encoder.
|
|
242
|
-
*
|
|
243
|
-
* @function
|
|
244
|
-
* @param {Encoder} encoder
|
|
245
|
-
* @param {Uint8Array} uint8Array
|
|
246
|
-
*/
|
|
247
99
|
const writeUint8Array = (encoder, uint8Array) => {
|
|
248
100
|
const bufferLen = encoder.cbuf.length;
|
|
249
101
|
const cpos = encoder.cpos;
|
|
@@ -252,172 +104,68 @@ const writeUint8Array = (encoder, uint8Array) => {
|
|
|
252
104
|
encoder.cbuf.set(uint8Array.subarray(0, leftCopyLen), cpos);
|
|
253
105
|
encoder.cpos += leftCopyLen;
|
|
254
106
|
if (rightCopyLen > 0) {
|
|
255
|
-
// Still something to write, write right half..
|
|
256
|
-
// Append new buffer
|
|
257
107
|
encoder.bufs.push(encoder.cbuf);
|
|
258
|
-
// must have at least size of remaining buffer
|
|
259
108
|
encoder.cbuf = new Uint8Array(math.max(bufferLen * 2, rightCopyLen));
|
|
260
|
-
// copy array
|
|
261
109
|
encoder.cbuf.set(uint8Array.subarray(leftCopyLen));
|
|
262
110
|
encoder.cpos = rightCopyLen;
|
|
263
111
|
}
|
|
264
112
|
};
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Append an Uint8Array to Encoder.
|
|
268
|
-
*
|
|
269
|
-
* @function
|
|
270
|
-
* @param {Encoder} encoder
|
|
271
|
-
* @param {Uint8Array} uint8Array
|
|
272
|
-
*/
|
|
273
113
|
const writeVarUint8Array = (encoder, uint8Array) => {
|
|
274
114
|
writeVarUint(encoder, uint8Array.byteLength);
|
|
275
115
|
writeUint8Array(encoder, uint8Array);
|
|
276
116
|
};
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Create an DataView of the next `len` bytes. Use it to write data after
|
|
280
|
-
* calling this function.
|
|
281
|
-
*
|
|
282
|
-
* ```js
|
|
283
|
-
* // write float32 using DataView
|
|
284
|
-
* const dv = writeOnDataView(encoder, 4)
|
|
285
|
-
* dv.setFloat32(0, 1.1)
|
|
286
|
-
* // read float32 using DataView
|
|
287
|
-
* const dv = readFromDataView(encoder, 4)
|
|
288
|
-
* dv.getFloat32(0) // => 1.100000023841858 (leaving it to the reader to find out why this is the correct result)
|
|
289
|
-
* ```
|
|
290
|
-
*
|
|
291
|
-
* @param {Encoder} encoder
|
|
292
|
-
* @param {number} len
|
|
293
|
-
* @return {DataView}
|
|
294
|
-
*/
|
|
295
117
|
const writeOnDataView = (encoder, len) => {
|
|
296
118
|
verifyLen(encoder, len);
|
|
297
119
|
const dview = new DataView(encoder.cbuf.buffer, encoder.cpos, len);
|
|
298
120
|
encoder.cpos += len;
|
|
299
|
-
return dview
|
|
121
|
+
return dview;
|
|
300
122
|
};
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* @param {Encoder} encoder
|
|
304
|
-
* @param {number} num
|
|
305
|
-
*/
|
|
306
123
|
const writeFloat32 = (encoder, num) => writeOnDataView(encoder, 4).setFloat32(0, num, false);
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* @param {Encoder} encoder
|
|
310
|
-
* @param {number} num
|
|
311
|
-
*/
|
|
312
124
|
const writeFloat64 = (encoder, num) => writeOnDataView(encoder, 8).setFloat64(0, num, false);
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
*/
|
|
318
|
-
const writeBigInt64 = (encoder, num) => /** @type {any} */ (writeOnDataView(encoder, 8)).setBigInt64(0, num, false);
|
|
319
|
-
|
|
125
|
+
const writeBigInt64 = (encoder, num) => (
|
|
126
|
+
/** @type {any} */
|
|
127
|
+
writeOnDataView(encoder, 8).setBigInt64(0, num, false)
|
|
128
|
+
);
|
|
320
129
|
const floatTestBed = new DataView(new ArrayBuffer(4));
|
|
321
|
-
|
|
322
|
-
* Check if a number can be encoded as a 32 bit float.
|
|
323
|
-
*
|
|
324
|
-
* @param {number} num
|
|
325
|
-
* @return {boolean}
|
|
326
|
-
*/
|
|
327
|
-
const isFloat32 = num => {
|
|
130
|
+
const isFloat32 = (num) => {
|
|
328
131
|
floatTestBed.setFloat32(0, num);
|
|
329
|
-
return floatTestBed.getFloat32(0) === num
|
|
132
|
+
return floatTestBed.getFloat32(0) === num;
|
|
330
133
|
};
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* @typedef {Array<AnyEncodable>} AnyEncodableArray
|
|
334
|
-
*/
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* @typedef {undefined|null|number|bigint|boolean|string|{[k:string]:AnyEncodable}|AnyEncodableArray|Uint8Array} AnyEncodable
|
|
338
|
-
*/
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* Encode data with efficient binary format.
|
|
342
|
-
*
|
|
343
|
-
* Differences to JSON:
|
|
344
|
-
* • Transforms data to a binary format (not to a string)
|
|
345
|
-
* • Encodes undefined, NaN, and ArrayBuffer (these can't be represented in JSON)
|
|
346
|
-
* • Numbers are efficiently encoded either as a variable length integer, as a
|
|
347
|
-
* 32 bit float, as a 64 bit float, or as a 64 bit bigint.
|
|
348
|
-
*
|
|
349
|
-
* Encoding table:
|
|
350
|
-
*
|
|
351
|
-
* | Data Type | Prefix | Encoding Method | Comment |
|
|
352
|
-
* | ------------------- | -------- | ------------------ | ------- |
|
|
353
|
-
* | undefined | 127 | | Functions, symbol, and everything that cannot be identified is encoded as undefined |
|
|
354
|
-
* | null | 126 | | |
|
|
355
|
-
* | integer | 125 | writeVarInt | Only encodes 32 bit signed integers |
|
|
356
|
-
* | float32 | 124 | writeFloat32 | |
|
|
357
|
-
* | float64 | 123 | writeFloat64 | |
|
|
358
|
-
* | bigint | 122 | writeBigInt64 | |
|
|
359
|
-
* | boolean (false) | 121 | | True and false are different data types so we save the following byte |
|
|
360
|
-
* | boolean (true) | 120 | | - 0b01111000 so the last bit determines whether true or false |
|
|
361
|
-
* | string | 119 | writeVarString | |
|
|
362
|
-
* | object<string,any> | 118 | custom | Writes {length} then {length} key-value pairs |
|
|
363
|
-
* | array<any> | 117 | custom | Writes {length} then {length} json values |
|
|
364
|
-
* | Uint8Array | 116 | writeVarUint8Array | We use Uint8Array for any kind of binary data |
|
|
365
|
-
*
|
|
366
|
-
* Reasons for the decreasing prefix:
|
|
367
|
-
* We need the first bit for extendability (later we may want to encode the
|
|
368
|
-
* prefix with writeVarUint). The remaining 7 bits are divided as follows:
|
|
369
|
-
* [0-30] the beginning of the data range is used for custom purposes
|
|
370
|
-
* (defined by the function that uses this library)
|
|
371
|
-
* [31-127] the end of the data range is used for data encoding by
|
|
372
|
-
* lib0/encoding.js
|
|
373
|
-
*
|
|
374
|
-
* @param {Encoder} encoder
|
|
375
|
-
* @param {AnyEncodable} data
|
|
376
|
-
*/
|
|
377
134
|
const writeAny = (encoder, data) => {
|
|
378
135
|
switch (typeof data) {
|
|
379
|
-
case
|
|
380
|
-
// TYPE 119: STRING
|
|
136
|
+
case "string":
|
|
381
137
|
write(encoder, 119);
|
|
382
138
|
writeVarString(encoder, data);
|
|
383
|
-
break
|
|
384
|
-
case
|
|
139
|
+
break;
|
|
140
|
+
case "number":
|
|
385
141
|
if (number.isInteger(data) && math.abs(data) <= binary.BITS31) {
|
|
386
|
-
// TYPE 125: INTEGER
|
|
387
142
|
write(encoder, 125);
|
|
388
143
|
writeVarInt(encoder, data);
|
|
389
144
|
} else if (isFloat32(data)) {
|
|
390
|
-
// TYPE 124: FLOAT32
|
|
391
145
|
write(encoder, 124);
|
|
392
146
|
writeFloat32(encoder, data);
|
|
393
147
|
} else {
|
|
394
|
-
// TYPE 123: FLOAT64
|
|
395
148
|
write(encoder, 123);
|
|
396
149
|
writeFloat64(encoder, data);
|
|
397
150
|
}
|
|
398
|
-
break
|
|
399
|
-
case
|
|
400
|
-
// TYPE 122: BigInt
|
|
151
|
+
break;
|
|
152
|
+
case "bigint":
|
|
401
153
|
write(encoder, 122);
|
|
402
154
|
writeBigInt64(encoder, data);
|
|
403
|
-
break
|
|
404
|
-
case
|
|
155
|
+
break;
|
|
156
|
+
case "object":
|
|
405
157
|
if (data === null) {
|
|
406
|
-
// TYPE 126: null
|
|
407
158
|
write(encoder, 126);
|
|
408
159
|
} else if (array.isArray(data)) {
|
|
409
|
-
// TYPE 117: Array
|
|
410
160
|
write(encoder, 117);
|
|
411
161
|
writeVarUint(encoder, data.length);
|
|
412
162
|
for (let i = 0; i < data.length; i++) {
|
|
413
163
|
writeAny(encoder, data[i]);
|
|
414
164
|
}
|
|
415
165
|
} else if (data instanceof Uint8Array) {
|
|
416
|
-
// TYPE 116: ArrayBuffer
|
|
417
166
|
write(encoder, 116);
|
|
418
167
|
writeVarUint8Array(encoder, data);
|
|
419
168
|
} else {
|
|
420
|
-
// TYPE 118: Object
|
|
421
169
|
write(encoder, 118);
|
|
422
170
|
const keys = Object.keys(data);
|
|
423
171
|
writeVarUint(encoder, keys.length);
|
|
@@ -427,106 +175,58 @@ const writeAny = (encoder, data) => {
|
|
|
427
175
|
writeAny(encoder, data[key]);
|
|
428
176
|
}
|
|
429
177
|
}
|
|
430
|
-
break
|
|
431
|
-
case
|
|
432
|
-
// TYPE 120/121: boolean (true/false)
|
|
178
|
+
break;
|
|
179
|
+
case "boolean":
|
|
433
180
|
write(encoder, data ? 120 : 121);
|
|
434
|
-
break
|
|
181
|
+
break;
|
|
435
182
|
default:
|
|
436
|
-
// TYPE 127: undefined
|
|
437
183
|
write(encoder, 127);
|
|
438
184
|
}
|
|
439
185
|
};
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Now come a few stateful encoder that have their own classes.
|
|
443
|
-
*/
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Basic Run Length Encoder - a basic compression implementation.
|
|
447
|
-
*
|
|
448
|
-
* Encodes [1,1,1,7] to [1,3,7,1] (3 times 1, 1 time 7). This encoder might do more harm than good if there are a lot of values that are not repeated.
|
|
449
|
-
*
|
|
450
|
-
* It was originally used for image compression. Cool .. article http://csbruce.com/cbm/transactor/pdfs/trans_v7_i06.pdf
|
|
451
|
-
*
|
|
452
|
-
* @note T must not be null!
|
|
453
|
-
*
|
|
454
|
-
* @template T
|
|
455
|
-
*/
|
|
456
186
|
class RleEncoder extends Encoder {
|
|
457
187
|
/**
|
|
458
188
|
* @param {function(Encoder, T):void} writer
|
|
459
189
|
*/
|
|
460
|
-
constructor
|
|
190
|
+
constructor(writer) {
|
|
461
191
|
super();
|
|
462
|
-
/**
|
|
463
|
-
* The writer
|
|
464
|
-
*/
|
|
465
192
|
this.w = writer;
|
|
466
|
-
/**
|
|
467
|
-
* Current state
|
|
468
|
-
* @type {T|null}
|
|
469
|
-
*/
|
|
470
193
|
this.s = null;
|
|
471
194
|
this.count = 0;
|
|
472
195
|
}
|
|
473
|
-
|
|
474
196
|
/**
|
|
475
197
|
* @param {T} v
|
|
476
198
|
*/
|
|
477
|
-
write
|
|
199
|
+
write(v) {
|
|
478
200
|
if (this.s === v) {
|
|
479
201
|
this.count++;
|
|
480
202
|
} else {
|
|
481
203
|
if (this.count > 0) {
|
|
482
|
-
|
|
483
|
-
writeVarUint(this, this.count - 1); // since count is always > 0, we can decrement by one. non-standard encoding ftw
|
|
204
|
+
writeVarUint(this, this.count - 1);
|
|
484
205
|
}
|
|
485
206
|
this.count = 1;
|
|
486
|
-
// write first value
|
|
487
207
|
this.w(this, v);
|
|
488
208
|
this.s = v;
|
|
489
209
|
}
|
|
490
210
|
}
|
|
491
211
|
}
|
|
492
|
-
|
|
493
|
-
/**
|
|
494
|
-
* @param {UintOptRleEncoder} encoder
|
|
495
|
-
*/
|
|
496
|
-
const flushUintOptRleEncoder = encoder => {
|
|
212
|
+
const flushUintOptRleEncoder = (encoder) => {
|
|
497
213
|
if (encoder.count > 0) {
|
|
498
|
-
// flush counter, unless this is the first value (count = 0)
|
|
499
|
-
// case 1: just a single value. set sign to positive
|
|
500
|
-
// case 2: write several values. set sign to negative to indicate that there is a length coming
|
|
501
214
|
writeVarInt(encoder.encoder, encoder.count === 1 ? encoder.s : -encoder.s);
|
|
502
215
|
if (encoder.count > 1) {
|
|
503
|
-
writeVarUint(encoder.encoder, encoder.count - 2);
|
|
216
|
+
writeVarUint(encoder.encoder, encoder.count - 2);
|
|
504
217
|
}
|
|
505
218
|
}
|
|
506
219
|
};
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* Optimized Rle encoder that does not suffer from the mentioned problem of the basic Rle encoder.
|
|
510
|
-
*
|
|
511
|
-
* Internally uses VarInt encoder to write unsigned integers. If the input occurs multiple times, we write
|
|
512
|
-
* write it as a negative number. The UintOptRleDecoder then understands that it needs to read a count.
|
|
513
|
-
*
|
|
514
|
-
* Encodes [1,2,3,3,3] as [1,2,-3,3] (once 1, once 2, three times 3)
|
|
515
|
-
*/
|
|
516
220
|
class UintOptRleEncoder {
|
|
517
|
-
constructor
|
|
221
|
+
constructor() {
|
|
518
222
|
this.encoder = new Encoder();
|
|
519
|
-
/**
|
|
520
|
-
* @type {number}
|
|
521
|
-
*/
|
|
522
223
|
this.s = 0;
|
|
523
224
|
this.count = 0;
|
|
524
225
|
}
|
|
525
|
-
|
|
526
226
|
/**
|
|
527
227
|
* @param {number} v
|
|
528
228
|
*/
|
|
529
|
-
write
|
|
229
|
+
write(v) {
|
|
530
230
|
if (this.s === v) {
|
|
531
231
|
this.count++;
|
|
532
232
|
} else {
|
|
@@ -535,68 +235,36 @@ class UintOptRleEncoder {
|
|
|
535
235
|
this.s = v;
|
|
536
236
|
}
|
|
537
237
|
}
|
|
538
|
-
|
|
539
238
|
/**
|
|
540
239
|
* Flush the encoded state and transform this to a Uint8Array.
|
|
541
240
|
*
|
|
542
241
|
* Note that this should only be called once.
|
|
543
242
|
*/
|
|
544
|
-
toUint8Array
|
|
243
|
+
toUint8Array() {
|
|
545
244
|
flushUintOptRleEncoder(this);
|
|
546
|
-
return toUint8Array(this.encoder)
|
|
245
|
+
return toUint8Array(this.encoder);
|
|
547
246
|
}
|
|
548
247
|
}
|
|
549
|
-
|
|
550
|
-
/**
|
|
551
|
-
* @param {IntDiffOptRleEncoder} encoder
|
|
552
|
-
*/
|
|
553
|
-
const flushIntDiffOptRleEncoder = encoder => {
|
|
248
|
+
const flushIntDiffOptRleEncoder = (encoder) => {
|
|
554
249
|
if (encoder.count > 0) {
|
|
555
|
-
// 31 bit making up the diff | wether to write the counter
|
|
556
|
-
// const encodedDiff = encoder.diff << 1 | (encoder.count === 1 ? 0 : 1)
|
|
557
250
|
const encodedDiff = encoder.diff * 2 + (encoder.count === 1 ? 0 : 1);
|
|
558
|
-
// flush counter, unless this is the first value (count = 0)
|
|
559
|
-
// case 1: just a single value. set first bit to positive
|
|
560
|
-
// case 2: write several values. set first bit to negative to indicate that there is a length coming
|
|
561
251
|
writeVarInt(encoder.encoder, encodedDiff);
|
|
562
252
|
if (encoder.count > 1) {
|
|
563
|
-
writeVarUint(encoder.encoder, encoder.count - 2);
|
|
253
|
+
writeVarUint(encoder.encoder, encoder.count - 2);
|
|
564
254
|
}
|
|
565
255
|
}
|
|
566
256
|
};
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* A combination of the IntDiffEncoder and the UintOptRleEncoder.
|
|
570
|
-
*
|
|
571
|
-
* The count approach is similar to the UintDiffOptRleEncoder, but instead of using the negative bitflag, it encodes
|
|
572
|
-
* in the LSB whether a count is to be read. Therefore this Encoder only supports 31 bit integers!
|
|
573
|
-
*
|
|
574
|
-
* Encodes [1, 2, 3, 2] as [3, 1, 6, -1] (more specifically [(1 << 1) | 1, (3 << 0) | 0, -1])
|
|
575
|
-
*
|
|
576
|
-
* Internally uses variable length encoding. Contrary to normal UintVar encoding, the first byte contains:
|
|
577
|
-
* * 1 bit that denotes whether the next value is a count (LSB)
|
|
578
|
-
* * 1 bit that denotes whether this value is negative (MSB - 1)
|
|
579
|
-
* * 1 bit that denotes whether to continue reading the variable length integer (MSB)
|
|
580
|
-
*
|
|
581
|
-
* Therefore, only five bits remain to encode diff ranges.
|
|
582
|
-
*
|
|
583
|
-
* Use this Encoder only when appropriate. In most cases, this is probably a bad idea.
|
|
584
|
-
*/
|
|
585
257
|
class IntDiffOptRleEncoder {
|
|
586
|
-
constructor
|
|
258
|
+
constructor() {
|
|
587
259
|
this.encoder = new Encoder();
|
|
588
|
-
/**
|
|
589
|
-
* @type {number}
|
|
590
|
-
*/
|
|
591
260
|
this.s = 0;
|
|
592
261
|
this.count = 0;
|
|
593
262
|
this.diff = 0;
|
|
594
263
|
}
|
|
595
|
-
|
|
596
264
|
/**
|
|
597
265
|
* @param {number} v
|
|
598
266
|
*/
|
|
599
|
-
write
|
|
267
|
+
write(v) {
|
|
600
268
|
if (this.diff === v - this.s) {
|
|
601
269
|
this.s = v;
|
|
602
270
|
this.count++;
|
|
@@ -607,60 +275,42 @@ class IntDiffOptRleEncoder {
|
|
|
607
275
|
this.s = v;
|
|
608
276
|
}
|
|
609
277
|
}
|
|
610
|
-
|
|
611
278
|
/**
|
|
612
279
|
* Flush the encoded state and transform this to a Uint8Array.
|
|
613
280
|
*
|
|
614
281
|
* Note that this should only be called once.
|
|
615
282
|
*/
|
|
616
|
-
toUint8Array
|
|
283
|
+
toUint8Array() {
|
|
617
284
|
flushIntDiffOptRleEncoder(this);
|
|
618
|
-
return toUint8Array(this.encoder)
|
|
285
|
+
return toUint8Array(this.encoder);
|
|
619
286
|
}
|
|
620
287
|
}
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* Optimized String Encoder.
|
|
624
|
-
*
|
|
625
|
-
* Encoding many small strings in a simple Encoder is not very efficient. The function call to decode a string takes some time and creates references that must be eventually deleted.
|
|
626
|
-
* In practice, when decoding several million small strings, the GC will kick in more and more often to collect orphaned string objects (or maybe there is another reason?).
|
|
627
|
-
*
|
|
628
|
-
* This string encoder solves the above problem. All strings are concatenated and written as a single string using a single encoding call.
|
|
629
|
-
*
|
|
630
|
-
* The lengths are encoded using a UintOptRleEncoder.
|
|
631
|
-
*/
|
|
632
288
|
class StringEncoder {
|
|
633
|
-
constructor
|
|
634
|
-
/**
|
|
635
|
-
* @type {Array<string>}
|
|
636
|
-
*/
|
|
289
|
+
constructor() {
|
|
637
290
|
this.sarr = [];
|
|
638
|
-
this.s =
|
|
291
|
+
this.s = "";
|
|
639
292
|
this.lensE = new UintOptRleEncoder();
|
|
640
293
|
}
|
|
641
|
-
|
|
642
294
|
/**
|
|
643
295
|
* @param {string} string
|
|
644
296
|
*/
|
|
645
|
-
write
|
|
646
|
-
this.s +=
|
|
297
|
+
write(string2) {
|
|
298
|
+
this.s += string2;
|
|
647
299
|
if (this.s.length > 19) {
|
|
648
300
|
this.sarr.push(this.s);
|
|
649
|
-
this.s =
|
|
301
|
+
this.s = "";
|
|
650
302
|
}
|
|
651
|
-
this.lensE.write(
|
|
303
|
+
this.lensE.write(string2.length);
|
|
652
304
|
}
|
|
653
|
-
|
|
654
|
-
toUint8Array () {
|
|
305
|
+
toUint8Array() {
|
|
655
306
|
const encoder = new Encoder();
|
|
656
307
|
this.sarr.push(this.s);
|
|
657
|
-
this.s =
|
|
658
|
-
writeVarString(encoder, this.sarr.join(
|
|
308
|
+
this.s = "";
|
|
309
|
+
writeVarString(encoder, this.sarr.join(""));
|
|
659
310
|
writeUint8Array(encoder, this.lensE.toUint8Array());
|
|
660
|
-
return toUint8Array(encoder)
|
|
311
|
+
return toUint8Array(encoder);
|
|
661
312
|
}
|
|
662
313
|
}
|
|
663
|
-
|
|
664
314
|
exports.Encoder = Encoder;
|
|
665
315
|
exports.IntDiffOptRleEncoder = IntDiffOptRleEncoder;
|
|
666
316
|
exports.RleEncoder = RleEncoder;
|