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