Skip to content

Commit

Permalink
feat(@jsii/spec): add loadAssemblyFromBuffer function (#3634)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kaizencc committed Jul 6, 2022
1 parent e13ef56 commit 4f3a0d1
Show file tree
Hide file tree
Showing 113 changed files with 559 additions and 288 deletions.
8 changes: 7 additions & 1 deletion eslint-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins:
- '@typescript-eslint'
- import
- prettier
- unicorn

parser: '@typescript-eslint/parser'
parserOptions:
Expand Down Expand Up @@ -43,6 +44,9 @@ rules:
'prettier/prettier':
- error

'unicorn/prefer-node-protocol':
- error

'@typescript-eslint/array-type':
- error
- default: array-simple
Expand Down Expand Up @@ -152,7 +156,9 @@ rules:

'import/no-extraneous-dependencies':
- error
- devDependencies: ['**/test/**'] # Only allow importing devDependencies from tests
- devDependencies: # Only allow importing devDependencies from tests
- '**/test/**'
- '**/*.test.ts'
optionalDependencies: false # Disallow importing optional dependencies (those shouldn't be used here)
peerDependencies: false # Disallow importing peer dependencies (those shouldn't be used here)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unicorn": "^43.0.0",
"jest": "^28.1.1",
"jest-circus": "^28.1.1",
"jest-config": "^28.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/benchmarks/bin/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as path from 'node:path';
import * as yargs from 'yargs';

import { benchmarks } from '../lib';
Expand Down
8 changes: 6 additions & 2 deletions packages/@jsii/benchmarks/lib/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Profiler, Session } from 'inspector';
import { performance, PerformanceObserver, PerformanceEntry } from 'perf_hooks';
import { Profiler, Session } from 'node:inspector';
import {
performance,
PerformanceObserver,
PerformanceEntry,
} from 'node:perf_hooks';

/**
* Result of a single run of the subject
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/benchmarks/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import * as path from 'node:path';

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

Expand Down
6 changes: 3 additions & 3 deletions packages/@jsii/benchmarks/scripts/snapshot-package.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as cp from 'child_process';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as os from 'os';
import * as path from 'path';
import * as cp from 'node:child_process';
import * as os from 'node:os';
import * as path from 'node:path';
import * as tar from 'tar';
import * as ts from 'typescript';

Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/check-node/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as process from 'process';
import * as process from 'node:process';
import { Range, SemVer } from 'semver';

const ONE_DAY_IN_MILLISECONDS = 86_400_000;
Expand Down
4 changes: 2 additions & 2 deletions packages/@jsii/check-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Chalk, bgYellow, bgYellowBright, bgRed } from 'chalk';
import { error } from 'console';
import { version } from 'process';
import { error } from 'node:console';
import { version } from 'node:process';

import { NodeRelease } from './constants';

Expand Down
4 changes: 2 additions & 2 deletions packages/@jsii/integ-test/test/build-cdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Octokit } from '@octokit/rest';
import * as dotenv from 'dotenv';
import { mkdtemp, readdir, remove } from 'fs-extra';
import { tmpdir } from 'os';
import * as path from 'path';
import { tmpdir } from 'node:os';
import * as path from 'node:path';

import {
downloadReleaseAsset,
Expand Down
8 changes: 4 additions & 4 deletions packages/@jsii/integ-test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cp from 'child_process';
import { IncomingMessage } from 'http';
import * as https from 'https';
import { Readable } from 'stream';
import * as cp from 'node:child_process';
import { IncomingMessage } from 'node:http';
import * as https from 'node:https';
import { Readable } from 'node:stream';
import { extract } from 'tar';

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/@jsii/kernel/src/kernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as childProcess from 'child_process';
import * as fs from 'fs-extra';
import * as os from 'os';
import { join } from 'path';
import * as path from 'path';
import * as vm from 'vm';
import * as childProcess from 'node:child_process';
import * as os from 'node:os';
import { join } from 'node:path';
import * as path from 'node:path';
import * as vm from 'node:vm';

import * as api from './api';
import {
Expand Down
10 changes: 5 additions & 5 deletions packages/@jsii/kernel/src/kernel.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as spec from '@jsii/spec';
import { loadAssemblyFromPath } from '@jsii/spec';
import * as cp from 'child_process';
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as cp from 'node:child_process';
import * as os from 'node:os';
import * as path from 'node:path';
import * as vm from 'node:vm';
import * as tar from 'tar';
import * as vm from 'vm';

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

this.sandbox = vm.createContext({
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/kernel/src/on-exit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs-extra';
import * as process from 'process';
import * as process from 'node:process';

const removeSyncPaths = new Array<string>();

Expand Down
10 changes: 5 additions & 5 deletions packages/@jsii/runtime/bin/jsii-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import '@jsii/check-node/run';

import { spawn } from 'child_process';
import { error } from 'console';
import { constants as os } from 'os';
import { resolve } from 'path';
import { Duplex } from 'stream';
import { spawn } from 'node:child_process';
import { error } from 'node:console';
import { constants as os } from 'node:os';
import { resolve } from 'node:path';
import { Duplex } from 'node:stream';

// Spawn another node process, with the following file descriptor setup:
// - No STDIN will be provided
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/runtime/lib/host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { api, Kernel } from '@jsii/kernel';
import { EventEmitter } from 'events';
import { EventEmitter } from 'node:events';

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

Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/runtime/lib/sync-stdio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from 'fs';
import * as fs from 'node:fs';

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

Expand Down
6 changes: 3 additions & 3 deletions packages/@jsii/runtime/test/kernel-host.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { api } from '@jsii/kernel';
import * as spec from '@jsii/spec';
import { loadAssemblyFromPath } from '@jsii/spec';
import * as child from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as child from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';

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

Expand Down
8 changes: 4 additions & 4 deletions packages/@jsii/runtime/test/playback.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as child from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as child from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';

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

Expand Down
56 changes: 39 additions & 17 deletions packages/@jsii/spec/build-tools/generate-json-schema.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
#!/bin/bash
set -euo pipefail

# Input
INPUT_FILE='lib/assembly.d.ts'

# Output
OUTPUT_DIR='schema'
OUTPUT_FILE="${OUTPUT_DIR}/jsii-spec.schema.json"

mkdir -p ${OUTPUT_DIR}

echo "Generating JSON schema into ${OUTPUT_FILE}"
typescript-json-schema \
${INPUT_FILE} 'Assembly' \
--out ${OUTPUT_FILE} \
--refs true \
--required true \
--strictNullChecks true \
--topRef true
{
# Input
INPUT_FILE='lib/assembly.d.ts'

# Output
OUTPUT_DIR='schema'
OUTPUT_FILE="${OUTPUT_DIR}/jsii-spec.schema.json"

mkdir -p ${OUTPUT_DIR}

echo "Generating JSON schema into ${OUTPUT_FILE}"
typescript-json-schema \
${INPUT_FILE} 'Assembly' \
--out ${OUTPUT_FILE} \
--refs true \
--required true \
--strictNullChecks true \
--topRef true
}

{
# Input
INPUT_FILE='lib/redirect.d.ts'

# Output
OUTPUT_DIR='schema'
OUTPUT_FILE="${OUTPUT_DIR}/assembly-redirect.schema.json"

mkdir -p ${OUTPUT_DIR}

echo "Generating JSON schema into ${OUTPUT_FILE}"
typescript-json-schema \
${INPUT_FILE} 'AssemblyRedirect'\
--out ${OUTPUT_FILE} \
--refs true \
--required true \
--strictNullChecks true \
--topRef true
}
4 changes: 2 additions & 2 deletions packages/@jsii/spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"package": "package-js"
},
"dependencies": {
"ajv": "^8.11.0",
"fs-extra": "^10.1.0"
"ajv": "^8.11.0"
},
"devDependencies": {
"fs-extra": "^10.1.0",
"jsii-build-tools": "^0.0.0",
"typescript-json-schema": "^0.53.1"
}
Expand Down
Loading

0 comments on commit 4f3a0d1

Please sign in to comment.