@flighthq/encoding 0.5.1-edge.685.f72fc7f

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.md ADDED
@@ -0,0 +1,9 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2013-2026 Joshua Granick and other contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # @flighthq/encoding
2
+
3
+ Portable UTF-8 conversion between strings and byte arrays
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @flighthq/encoding
9
+ ```
10
+
11
+ Import the supported application-facing API from `@flighthq/encoding`. The `@flighthq/encoding/contract` export is the wider package-to-package contract used to compose Flight itself.
12
+
13
+ This package is part of the locked-version Flight SDK graph. Applications may instead install and import `@flighthq/sdk` when package-level tree shaking is sufficient.
14
+
15
+ - [Flight project](https://github.com/flighthq/flight)
16
+ - [Source for @flighthq/encoding@0.5.1-edge.685.f72fc7f](https://github.com/flighthq/flight/tree/f72fc7feaa9653d950f39c7d2fe6365f48e82e51/packages/encoding)
17
+ - [License](https://github.com/flighthq/flight/blob/f72fc7feaa9653d950f39c7d2fe6365f48e82e51/LICENSE.md)
@@ -0,0 +1,2 @@
1
+ export * from './utf8';
2
+ //# sourceMappingURL=contract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './utf8';
2
+ //# sourceMappingURL=contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract.js","sourceRoot":"","sources":["../src/contract.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from './contract';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './contract';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC"}
package/dist/utf8.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function decodeUTF8(bytes: Readonly<Uint8Array>, offset?: number, length?: number): string;
2
+ export declare function encodeUTF8(text: string): Uint8Array;
3
+ //# sourceMappingURL=utf8.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8.d.ts","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,EAC3B,MAAM,GAAE,MAAU,EAClB,MAAM,GAAE,MAA8B,GACrC,MAAM,CAqER;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAmBnD"}
package/dist/utf8.js ADDED
@@ -0,0 +1,147 @@
1
+ export function decodeUTF8(bytes, offset = 0, length = bytes.length - offset) {
2
+ assertUTF8Window(bytes.length, offset, length);
3
+ const end = offset + length;
4
+ let result = '';
5
+ let index = offset;
6
+ while (index < end) {
7
+ const first = bytes[index++];
8
+ if (first <= 0x7f) {
9
+ result += String.fromCharCode(first);
10
+ continue;
11
+ }
12
+ if (first >= 0xc2 && first <= 0xdf) {
13
+ if (index < end) {
14
+ const second = bytes[index];
15
+ if (isUTF8Continuation(second)) {
16
+ index++;
17
+ result += String.fromCharCode(((first & 0x1f) << 6) | (second & 0x3f));
18
+ continue;
19
+ }
20
+ }
21
+ result += UTF8_REPLACEMENT_CHARACTER;
22
+ continue;
23
+ }
24
+ if (first >= 0xe0 && first <= 0xef) {
25
+ const secondMinimum = first === 0xe0 ? 0xa0 : 0x80;
26
+ const secondMaximum = first === 0xed ? 0x9f : 0xbf;
27
+ if (index >= end || bytes[index] < secondMinimum || bytes[index] > secondMaximum) {
28
+ result += UTF8_REPLACEMENT_CHARACTER;
29
+ continue;
30
+ }
31
+ const second = bytes[index++];
32
+ if (index >= end || !isUTF8Continuation(bytes[index])) {
33
+ result += UTF8_REPLACEMENT_CHARACTER;
34
+ continue;
35
+ }
36
+ const third = bytes[index++];
37
+ result += String.fromCharCode(((first & 0x0f) << 12) | ((second & 0x3f) << 6) | (third & 0x3f));
38
+ continue;
39
+ }
40
+ if (first >= 0xf0 && first <= 0xf4) {
41
+ const secondMinimum = first === 0xf0 ? 0x90 : 0x80;
42
+ const secondMaximum = first === 0xf4 ? 0x8f : 0xbf;
43
+ if (index >= end || bytes[index] < secondMinimum || bytes[index] > secondMaximum) {
44
+ result += UTF8_REPLACEMENT_CHARACTER;
45
+ continue;
46
+ }
47
+ const second = bytes[index++];
48
+ if (index >= end || !isUTF8Continuation(bytes[index])) {
49
+ result += UTF8_REPLACEMENT_CHARACTER;
50
+ continue;
51
+ }
52
+ const third = bytes[index++];
53
+ if (index >= end || !isUTF8Continuation(bytes[index])) {
54
+ result += UTF8_REPLACEMENT_CHARACTER;
55
+ continue;
56
+ }
57
+ const fourth = bytes[index++];
58
+ const codePoint = ((first & 0x07) << 18) | ((second & 0x3f) << 12) | ((third & 0x3f) << 6) | (fourth & 0x3f);
59
+ const pair = codePoint - 0x10000;
60
+ result += String.fromCharCode(0xd800 | (pair >> 10), 0xdc00 | (pair & 0x3ff));
61
+ continue;
62
+ }
63
+ result += UTF8_REPLACEMENT_CHARACTER;
64
+ }
65
+ return result;
66
+ }
67
+ export function encodeUTF8(text) {
68
+ const result = new Uint8Array(measureUTF8(text));
69
+ let outputIndex = 0;
70
+ for (let index = 0; index < text.length; index++) {
71
+ let codePoint = text.charCodeAt(index);
72
+ if (codePoint >= 0xd800 && codePoint <= 0xdbff) {
73
+ const second = index + 1 < text.length ? text.charCodeAt(index + 1) : 0;
74
+ if (second >= 0xdc00 && second <= 0xdfff) {
75
+ codePoint = 0x10000 + ((codePoint - 0xd800) << 10) + (second - 0xdc00);
76
+ index++;
77
+ }
78
+ else {
79
+ codePoint = UTF8_REPLACEMENT_CODE_POINT;
80
+ }
81
+ }
82
+ else if (codePoint >= 0xdc00 && codePoint <= 0xdfff) {
83
+ codePoint = UTF8_REPLACEMENT_CODE_POINT;
84
+ }
85
+ outputIndex = writeUTF8CodePoint(result, outputIndex, codePoint);
86
+ }
87
+ return result;
88
+ }
89
+ function assertUTF8Window(byteLength, offset, length) {
90
+ if (!Number.isInteger(offset) ||
91
+ !Number.isInteger(length) ||
92
+ offset < 0 ||
93
+ length < 0 ||
94
+ offset + length > byteLength) {
95
+ throw new RangeError('decodeUTF8 window is outside the byte array');
96
+ }
97
+ }
98
+ function isUTF8Continuation(byte) {
99
+ return byte >= 0x80 && byte <= 0xbf;
100
+ }
101
+ function measureUTF8(text) {
102
+ let byteLength = 0;
103
+ for (let index = 0; index < text.length; index++) {
104
+ const codeUnit = text.charCodeAt(index);
105
+ if (codeUnit <= 0x7f)
106
+ byteLength++;
107
+ else if (codeUnit <= 0x7ff)
108
+ byteLength += 2;
109
+ else if (codeUnit >= 0xd800 && codeUnit <= 0xdbff) {
110
+ const second = index + 1 < text.length ? text.charCodeAt(index + 1) : 0;
111
+ if (second >= 0xdc00 && second <= 0xdfff) {
112
+ byteLength += 4;
113
+ index++;
114
+ }
115
+ else {
116
+ byteLength += 3;
117
+ }
118
+ }
119
+ else
120
+ byteLength += 3;
121
+ }
122
+ return byteLength;
123
+ }
124
+ function writeUTF8CodePoint(out, offset, codePoint) {
125
+ if (codePoint <= 0x7f) {
126
+ out[offset++] = codePoint;
127
+ }
128
+ else if (codePoint <= 0x7ff) {
129
+ out[offset++] = 0xc0 | (codePoint >> 6);
130
+ out[offset++] = 0x80 | (codePoint & 0x3f);
131
+ }
132
+ else if (codePoint <= 0xffff) {
133
+ out[offset++] = 0xe0 | (codePoint >> 12);
134
+ out[offset++] = 0x80 | ((codePoint >> 6) & 0x3f);
135
+ out[offset++] = 0x80 | (codePoint & 0x3f);
136
+ }
137
+ else {
138
+ out[offset++] = 0xf0 | (codePoint >> 18);
139
+ out[offset++] = 0x80 | ((codePoint >> 12) & 0x3f);
140
+ out[offset++] = 0x80 | ((codePoint >> 6) & 0x3f);
141
+ out[offset++] = 0x80 | (codePoint & 0x3f);
142
+ }
143
+ return offset;
144
+ }
145
+ const UTF8_REPLACEMENT_CHARACTER = '\ufffd';
146
+ const UTF8_REPLACEMENT_CODE_POINT = 0xfffd;
147
+ //# sourceMappingURL=utf8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utf8.js","sourceRoot":"","sources":["../src/utf8.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,CACxB,KAA2B,EAC3B,SAAiB,CAAC,EAClB,SAAiB,KAAK,CAAC,MAAM,GAAG,MAAM;IAEtC,gBAAgB,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/C,MAAM,GAAG,GAAG,MAA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,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QACjD,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC;SAAM,CAAC;QACN,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;QACzC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QAClD,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;QACjD,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,0BAA0B,GAAG,QAAQ,CAAC;AAC5C,MAAM,2BAA2B,GAAG,MAAM,CAAC"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@flighthq/encoding",
3
+ "version": "0.5.1-edge.685.f72fc7f",
4
+ "author": "Joshua Granick and other contributors",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/flighthq/flight.git",
9
+ "directory": "packages/encoding"
10
+ },
11
+ "type": "module",
12
+ "main": "dist/index.js",
13
+ "types": "dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./contract": {
20
+ "types": "./dist/contract.d.ts",
21
+ "default": "./dist/contract.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "src/**/*.test.ts",
27
+ "!dist/**/*.test.js",
28
+ "!dist/**/*.test.d.ts",
29
+ "!dist/**/*.test.js.map",
30
+ "!dist/**/*.test.d.ts.map"
31
+ ],
32
+ "scripts": {
33
+ "build": "tsc -b",
34
+ "clean": "tsc -b --clean",
35
+ "test": "vitest run --config vitest.config.ts",
36
+ "test:watch": "vitest --watch --config vitest.config.ts",
37
+ "prepack": "npm run clean && npm run clean:dist && npm run build",
38
+ "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "^5.3.0"
42
+ },
43
+ "description": "Portable UTF-8 conversion between strings and byte arrays",
44
+ "sideEffects": false
45
+ }
@@ -0,0 +1,49 @@
1
+ import { decodeUTF8, encodeUTF8 } from './utf8';
2
+
3
+ describe('decodeUTF8', () => {
4
+ it('decodes empty and ASCII input', () => {
5
+ expect(decodeUTF8(new Uint8Array())).toBe('');
6
+ expect(decodeUTF8(Uint8Array.of(0, 72, 101, 108, 108, 111, 0x7f))).toBe('\0Hello\u007f');
7
+ });
8
+
9
+ it('decodes two-, three-, and four-byte sequences', () => {
10
+ expect(decodeUTF8(Uint8Array.of(0xc2, 0xa2, 0xe2, 0x82, 0xac, 0xf0, 0x9f, 0x98, 0x80))).toBe('¢€😀');
11
+ });
12
+
13
+ it('replaces malformed and incomplete sequences without consuming the next valid byte', () => {
14
+ expect(decodeUTF8(Uint8Array.of(0x80, 0xc0, 0xaf))).toBe('���');
15
+ expect(decodeUTF8(Uint8Array.of(0xe2, 0x28, 0xa1))).toBe('�(�');
16
+ expect(decodeUTF8(Uint8Array.of(0xed, 0xa0, 0x80))).toBe('���');
17
+ expect(decodeUTF8(Uint8Array.of(0xe2, 0x82))).toBe('�');
18
+ });
19
+
20
+ it('honors offset and length windows without reading adjacent bytes', () => {
21
+ const bytes = Uint8Array.of(0xff, 72, 0xc3, 0xa9, 0xff);
22
+ expect(decodeUTF8(bytes, 1, 3)).toBe('Hé');
23
+ expect(decodeUTF8(bytes, 1, 0)).toBe('');
24
+ expect(decodeUTF8(bytes, 5)).toBe('');
25
+ expect(decodeUTF8(Uint8Array.of(0xe2, 0x82, 0xac), 0, 2)).toBe('�');
26
+ });
27
+
28
+ it('rejects windows outside the byte array', () => {
29
+ const bytes = Uint8Array.of(65);
30
+ expect(() => decodeUTF8(bytes, -1)).toThrow(RangeError);
31
+ expect(() => decodeUTF8(bytes, 0, 2)).toThrow(RangeError);
32
+ });
33
+ });
34
+
35
+ describe('encodeUTF8', () => {
36
+ it('encodes empty and ASCII input', () => {
37
+ expect(encodeUTF8('')).toEqual(new Uint8Array());
38
+ expect(encodeUTF8('\0Hello\u007f')).toEqual(Uint8Array.of(0, 72, 101, 108, 108, 111, 0x7f));
39
+ });
40
+
41
+ it('encodes two-, three-, and four-byte sequences', () => {
42
+ expect(encodeUTF8('¢€😀')).toEqual(Uint8Array.of(0xc2, 0xa2, 0xe2, 0x82, 0xac, 0xf0, 0x9f, 0x98, 0x80));
43
+ });
44
+
45
+ it('encodes unpaired surrogates as the replacement character', () => {
46
+ expect(encodeUTF8('\ud800A\udc00')).toEqual(Uint8Array.of(0xef, 0xbf, 0xbd, 65, 0xef, 0xbf, 0xbd));
47
+ expect(encodeUTF8('\ufffd')).toEqual(Uint8Array.of(0xef, 0xbf, 0xbd));
48
+ });
49
+ });