Skip to content

Commit 4f3a0d1

Browse files
authored
feat(@jsii/spec): add loadAssemblyFromBuffer function (#3634)
Usage is demonstrated here: cdklabs/construct-hub#920 Two implementations were considered to fit the use case in construct hub, `loadAssemblyFromBuffer` and `loadAssemblyFromTarball`. I went with loading from a buffer because it allows us to utilize prior art in construct hub for parsing tarballs, does not need an added dependency on `tar`, and causes minimal impact when refactoring the ingestion lambda to use this function. Also includes a small refactoring of the test suite. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent e13ef56 commit 4f3a0d1

File tree

113 files changed

+559
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+559
-288
lines changed

eslint-config.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins:
88
- '@typescript-eslint'
99
- import
1010
- prettier
11+
- unicorn
1112

1213
parser: '@typescript-eslint/parser'
1314
parserOptions:
@@ -43,6 +44,9 @@ rules:
4344
'prettier/prettier':
4445
- error
4546

47+
'unicorn/prefer-node-protocol':
48+
- error
49+
4650
'@typescript-eslint/array-type':
4751
- error
4852
- default: array-simple
@@ -152,7 +156,9 @@ rules:
152156

153157
'import/no-extraneous-dependencies':
154158
- error
155-
- devDependencies: ['**/test/**'] # Only allow importing devDependencies from tests
159+
- devDependencies: # Only allow importing devDependencies from tests
160+
- '**/test/**'
161+
- '**/*.test.ts'
156162
optionalDependencies: false # Disallow importing optional dependencies (those shouldn't be used here)
157163
peerDependencies: false # Disallow importing peer dependencies (those shouldn't be used here)
158164

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"eslint-import-resolver-typescript": "^2.7.1",
2828
"eslint-plugin-import": "^2.26.0",
2929
"eslint-plugin-prettier": "^4.0.0",
30+
"eslint-plugin-unicorn": "^43.0.0",
3031
"jest": "^28.1.1",
3132
"jest-circus": "^28.1.1",
3233
"jest-config": "^28.1.1",

packages/@jsii/benchmarks/bin/benchmark.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'fs-extra';
2-
import * as path from 'path';
2+
import * as path from 'node:path';
33
import * as yargs from 'yargs';
44

55
import { benchmarks } from '../lib';

packages/@jsii/benchmarks/lib/benchmark.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { Profiler, Session } from 'inspector';
2-
import { performance, PerformanceObserver, PerformanceEntry } from 'perf_hooks';
1+
import { Profiler, Session } from 'node:inspector';
2+
import {
3+
performance,
4+
PerformanceObserver,
5+
PerformanceEntry,
6+
} from 'node:perf_hooks';
37

48
/**
59
* Result of a single run of the subject

packages/@jsii/benchmarks/lib/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as path from 'path';
1+
import * as path from 'node:path';
22

33
export const fixturesDir = path.resolve(__dirname, '..', 'fixtures');
44

packages/@jsii/benchmarks/scripts/snapshot-package.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as cp from 'child_process';
21
import * as fs from 'fs-extra';
32
import * as glob from 'glob';
4-
import * as os from 'os';
5-
import * as path from 'path';
3+
import * as cp from 'node:child_process';
4+
import * as os from 'node:os';
5+
import * as path from 'node:path';
66
import * as tar from 'tar';
77
import * as ts from 'typescript';
88

packages/@jsii/check-node/src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as process from 'process';
1+
import * as process from 'node:process';
22
import { Range, SemVer } from 'semver';
33

44
const ONE_DAY_IN_MILLISECONDS = 86_400_000;

packages/@jsii/check-node/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Chalk, bgYellow, bgYellowBright, bgRed } from 'chalk';
2-
import { error } from 'console';
3-
import { version } from 'process';
2+
import { error } from 'node:console';
3+
import { version } from 'node:process';
44

55
import { NodeRelease } from './constants';
66

packages/@jsii/integ-test/test/build-cdk.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Octokit } from '@octokit/rest';
22
import * as dotenv from 'dotenv';
33
import { mkdtemp, readdir, remove } from 'fs-extra';
4-
import { tmpdir } from 'os';
5-
import * as path from 'path';
4+
import { tmpdir } from 'node:os';
5+
import * as path from 'node:path';
66

77
import {
88
downloadReleaseAsset,

packages/@jsii/integ-test/utils/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as cp from 'child_process';
2-
import { IncomingMessage } from 'http';
3-
import * as https from 'https';
4-
import { Readable } from 'stream';
1+
import * as cp from 'node:child_process';
2+
import { IncomingMessage } from 'node:http';
3+
import * as https from 'node:https';
4+
import { Readable } from 'node:stream';
55
import { extract } from 'tar';
66

77
/**

packages/@jsii/kernel/src/kernel.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import * as childProcess from 'child_process';
21
import * as fs from 'fs-extra';
3-
import * as os from 'os';
4-
import { join } from 'path';
5-
import * as path from 'path';
6-
import * as vm from 'vm';
2+
import * as childProcess from 'node:child_process';
3+
import * as os from 'node:os';
4+
import { join } from 'node:path';
5+
import * as path from 'node:path';
6+
import * as vm from 'node:vm';
77

88
import * as api from './api';
99
import {

packages/@jsii/kernel/src/kernel.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as spec from '@jsii/spec';
22
import { loadAssemblyFromPath } from '@jsii/spec';
3-
import * as cp from 'child_process';
43
import * as fs from 'fs-extra';
5-
import * as os from 'os';
6-
import * as path from 'path';
4+
import * as cp from 'node:child_process';
5+
import * as os from 'node:os';
6+
import * as path from 'node:path';
7+
import * as vm from 'node:vm';
78
import * as tar from 'tar';
8-
import * as vm from 'vm';
99

1010
import * as api from './api';
1111
import { TOKEN_REF } from './api';
@@ -49,7 +49,7 @@ export class Kernel {
4949
// I wonder if webpack has some pragma that allows opting-out at certain points
5050
// in the code.
5151
// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
52-
const moduleLoad = require('module').Module._load;
52+
const moduleLoad = require('node:module').Module._load;
5353
const nodeRequire = (p: string) => moduleLoad(p, module, false);
5454

5555
this.sandbox = vm.createContext({

packages/@jsii/kernel/src/on-exit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as fs from 'fs-extra';
2-
import * as process from 'process';
2+
import * as process from 'node:process';
33

44
const removeSyncPaths = new Array<string>();
55

packages/@jsii/runtime/bin/jsii-runtime.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import '@jsii/check-node/run';
22

3-
import { spawn } from 'child_process';
4-
import { error } from 'console';
5-
import { constants as os } from 'os';
6-
import { resolve } from 'path';
7-
import { Duplex } from 'stream';
3+
import { spawn } from 'node:child_process';
4+
import { error } from 'node:console';
5+
import { constants as os } from 'node:os';
6+
import { resolve } from 'node:path';
7+
import { Duplex } from 'node:stream';
88

99
// Spawn another node process, with the following file descriptor setup:
1010
// - No STDIN will be provided

packages/@jsii/runtime/lib/host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { api, Kernel } from '@jsii/kernel';
2-
import { EventEmitter } from 'events';
2+
import { EventEmitter } from 'node:events';
33

44
import { Input, IInputOutput } from './in-out';
55

packages/@jsii/runtime/lib/sync-stdio.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from 'fs';
1+
import * as fs from 'node:fs';
22

33
const INPUT_BUFFER_SIZE = 1_048_576; // 1MiB (aka: 1024 * 1024), not related to max line length
44

packages/@jsii/runtime/test/kernel-host.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { api } from '@jsii/kernel';
22
import * as spec from '@jsii/spec';
33
import { loadAssemblyFromPath } from '@jsii/spec';
4-
import * as child from 'child_process';
5-
import * as fs from 'fs';
6-
import * as path from 'path';
4+
import * as child from 'node:child_process';
5+
import * as fs from 'node:fs';
6+
import * as path from 'node:path';
77

88
import { KernelHost, IInputOutput, Input, Output } from '../lib';
99

packages/@jsii/runtime/test/playback.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as child from 'child_process';
2-
import * as fs from 'fs';
3-
import * as os from 'os';
4-
import * as path from 'path';
1+
import * as child from 'node:child_process';
2+
import * as fs from 'node:fs';
3+
import * as os from 'node:os';
4+
import * as path from 'node:path';
55

66
import { IInputOutput, Input, KernelHost, Output } from '../lib';
77

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
#!/bin/bash
22
set -euo pipefail
33

4-
# Input
5-
INPUT_FILE='lib/assembly.d.ts'
6-
7-
# Output
8-
OUTPUT_DIR='schema'
9-
OUTPUT_FILE="${OUTPUT_DIR}/jsii-spec.schema.json"
10-
11-
mkdir -p ${OUTPUT_DIR}
12-
13-
echo "Generating JSON schema into ${OUTPUT_FILE}"
14-
typescript-json-schema \
15-
${INPUT_FILE} 'Assembly' \
16-
--out ${OUTPUT_FILE} \
17-
--refs true \
18-
--required true \
19-
--strictNullChecks true \
20-
--topRef true
4+
{
5+
# Input
6+
INPUT_FILE='lib/assembly.d.ts'
7+
8+
# Output
9+
OUTPUT_DIR='schema'
10+
OUTPUT_FILE="${OUTPUT_DIR}/jsii-spec.schema.json"
11+
12+
mkdir -p ${OUTPUT_DIR}
13+
14+
echo "Generating JSON schema into ${OUTPUT_FILE}"
15+
typescript-json-schema \
16+
${INPUT_FILE} 'Assembly' \
17+
--out ${OUTPUT_FILE} \
18+
--refs true \
19+
--required true \
20+
--strictNullChecks true \
21+
--topRef true
22+
}
23+
24+
{
25+
# Input
26+
INPUT_FILE='lib/redirect.d.ts'
27+
28+
# Output
29+
OUTPUT_DIR='schema'
30+
OUTPUT_FILE="${OUTPUT_DIR}/assembly-redirect.schema.json"
31+
32+
mkdir -p ${OUTPUT_DIR}
33+
34+
echo "Generating JSON schema into ${OUTPUT_FILE}"
35+
typescript-json-schema \
36+
${INPUT_FILE} 'AssemblyRedirect'\
37+
--out ${OUTPUT_FILE} \
38+
--refs true \
39+
--required true \
40+
--strictNullChecks true \
41+
--topRef true
42+
}

packages/@jsii/spec/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"package": "package-js"
3232
},
3333
"dependencies": {
34-
"ajv": "^8.11.0",
35-
"fs-extra": "^10.1.0"
34+
"ajv": "^8.11.0"
3635
},
3736
"devDependencies": {
37+
"fs-extra": "^10.1.0",
3838
"jsii-build-tools": "^0.0.0",
3939
"typescript-json-schema": "^0.53.1"
4040
}

0 commit comments

Comments
 (0)