Skip to content

Commit 61da385

Browse files
committed
fix(core): add script to prevent byte array type parameter from being generated by compiler
1 parent 969d21e commit 61da385

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

.changeset/shiny-donuts-invite.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/util-stream": patch
3+
"@smithy/core": patch
4+
---
5+
6+
prevent compilation from inserting Uint8Array type parameter

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ test-types:
3939
npx tsc -p tsconfig.test.json
4040

4141
test-integration:
42+
node ./scripts/validation/no-generic-byte-arrays.js
4243
make test-browser
4344
yarn g:vitest run -c vitest.config.integ.mts
4445
make test-types

packages/core/src/submodules/cbor/cbor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { encode, resize, toUint8Array } from "./cbor-encode";
1212
* @see https://github.com/kriszyp/cbor-x
1313
*/
1414
export const cbor = {
15-
deserialize(payload: Uint8Array) {
15+
deserialize(payload: Uint8Array): any {
1616
setPayload(payload);
1717
return decode(0, payload.length);
1818
},
19-
serialize(input: any) {
19+
serialize(input: any): Uint8Array {
2020
try {
2121
encode(input);
2222
return toUint8Array();

packages/util-stream/src/ByteArrayCollector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class ByteArrayCollector {
1313
this.byteLength += byteArray.byteLength;
1414
}
1515

16-
public flush() {
16+
public flush(): Uint8Array {
1717
if (this.byteArrays.length === 1) {
1818
const bytes = this.byteArrays[0];
1919
this.reset();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Runs after a full build to assert that Uint8Array was not generated with a type parameter
5+
* by TypeScript, which is only compatible with TypeScript 5.7.
6+
*/
7+
8+
const walk = require("../utils/walk");
9+
const fs = require("node:fs");
10+
const path = require("node:path");
11+
12+
const root = path.join(__dirname, "..", "..");
13+
14+
const packages = path.join(root, "packages");
15+
16+
(async () => {
17+
const errors = [];
18+
for (const folder of fs.readdirSync(packages)) {
19+
const packagePath = path.join(packages, folder);
20+
const distTypes = path.join(packagePath, "dist-types");
21+
if (fs.existsSync(distTypes)) {
22+
for await (const file of walk(distTypes)) {
23+
const contents = fs.readFileSync(file, "utf-8");
24+
if (contents.includes("Uint8Array<")) {
25+
errors.push(file);
26+
}
27+
}
28+
}
29+
}
30+
if (errors.length > 0) {
31+
throw new Error(
32+
`The following files used Uint8Array in a generic way, only compatible with TypeScript 5.7:\n\t${errors.join(
33+
"\n\t"
34+
)}`
35+
);
36+
} else {
37+
console.log(`✅ No Uint8Arrays with type parameters.`);
38+
}
39+
})();

0 commit comments

Comments
 (0)