Skip to content

Commit

Permalink
fix: Address TypeScript recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Feb 16, 2022
1 parent cd5f084 commit 2d1e1e0
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 63 deletions.
19 changes: 10 additions & 9 deletions packages/compartment-mapper/src/parse-archive-cjs.js
@@ -1,13 +1,12 @@
// @ts-check

/** @typedef {import('ses').ThirdPartyStaticModuleInterface} ThirdPartyStaticModuleInterface */

import { analyzeCommonJS } from '@endo/cjs-module-analyzer';

const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();

const { freeze } = Object;
/** @type {<T>(value: T) => T} */
const freeze = Object.freeze;

const noopExecute = () => {};
freeze(noopExecute);
Expand Down Expand Up @@ -38,11 +37,13 @@ export const parseArchiveCjs = async (
return {
parser: 'pre-cjs-json',
bytes: pre,
record: /** @type {ThirdPartyStaticModuleInterface} */ (freeze({
imports: freeze(imports),
exports: freeze(exports),
reexports: freeze(reexports),
execute: noopExecute,
})),
record: /** @type {import('ses').ThirdPartyStaticModuleInterface} */ (freeze(
{
imports: freeze(imports),
exports: freeze(exports),
reexports: freeze(reexports),
execute: noopExecute,
},
)),
};
};
2 changes: 1 addition & 1 deletion packages/daemon/src/connection.js
Expand Up @@ -30,7 +30,7 @@ const makeCapTPWithStreams = (name, writer, reader, cancelled, bootstrap) => {
}
})();

const closed = cancelled.finally(async () => {
const closed = cancelled.catch(async () => {
abort();
await Promise.all([writer.return(undefined), drained]);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/daemon.js
Expand Up @@ -72,9 +72,9 @@ const makeWorker = async locator => {

const terminated = Promise.all([exited, closed]);

const { resolve: cancelWorker, promise: workerCancelled } = makePromiseKit();
const { reject: cancelWorker, promise: workerCancelled } = makePromiseKit();

cancelled.catch(async () => cancelWorker());
cancelled.catch(async error => cancelWorker(error));

workerCancelled.then(async () => {
const responded = E(bootstrap).terminate();
Expand All @@ -83,7 +83,7 @@ const makeWorker = async locator => {
});

const terminate = () => {
cancelWorker();
cancelWorker(new Error('Terminated'));
};

return harden({
Expand Down
4 changes: 2 additions & 2 deletions packages/daemon/test/test-endo.js
Expand Up @@ -35,7 +35,7 @@ const locator = {
};

test.serial('lifecycle', async t => {
const { resolve: cancel, promise: cancelled } = makePromiseKit();
const { reject: cancel, promise: cancelled } = makePromiseKit();

await reset(locator);
await clean(locator);
Expand All @@ -51,7 +51,7 @@ test.serial('lifecycle', async t => {
const bootstrap = getBootstrap();
const worker = await E(E.get(bootstrap).privateFacet).makeWorker();
await E(E.get(worker).actions).terminate();
cancel();
cancel(new Error('Cancelled'));
await closed;

await stop(locator);
Expand Down
2 changes: 1 addition & 1 deletion packages/netstring/jsconfig.json
Expand Up @@ -7,5 +7,5 @@
"strictNullChecks": true,
"moduleResolution": "node"
},
"include": ["src/**/*.js", "index.d.ts"]
"include": ["*.js", "src/**/*.js", "*/.ts", "src/**/*.ts"]
}
1 change: 0 additions & 1 deletion packages/netstring/package.json
Expand Up @@ -38,7 +38,6 @@
},
"devDependencies": {
"@endo/eslint-config": "^0.4.2",
"@endo/stream": "^0.1.0",
"ava": "^3.12.1",
"babel-eslint": "^10.0.3",
"c8": "^7.7.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/netstring/reader.js
Expand Up @@ -67,6 +67,8 @@ async function* makeNetstringIterator(
`Unexpected dangling message at offset ${offset} of ${name}`,
);
}

return undefined;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/netstring/writer.js
Expand Up @@ -48,7 +48,7 @@ export const makeNetstringWriter = output => {
return output.next(buffer.subarray(0, messageLength));
},
async return() {
return output.return();
return output.return(undefined);
},
async throw(error) {
return output.throw(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/promise-kit/jsconfig.json
Expand Up @@ -7,5 +7,5 @@
"strictNullChecks": true,
"moduleResolution": "node"
},
"include": ["src/**/*.js", "index.d.ts"]
"include": ["**/*.js", "**/*.ts"]
}
6 changes: 4 additions & 2 deletions packages/ses-ava/src/ses-ava-test.js
Expand Up @@ -110,8 +110,10 @@ const wrapTester = (testerFunc, logger = defaultLogger) => {
* @returns {TesterInterface}
*/
const wrapTest = (avaTest, logger = defaultLogger) => {
/** @type {TesterInterface} */
const testerWrapper = wrapTester(avaTest, logger);
const testerWrapper = /** @type {TesterInterface} */ (wrapTester(
avaTest,
logger,
));
for (const methodName of testerMethodsWhitelist) {
if (methodName in avaTest) {
/** @type {TesterFunc} */
Expand Down
4 changes: 3 additions & 1 deletion packages/ses-ava/src/types.js
Expand Up @@ -57,7 +57,7 @@
* @param {ImplFunc} implFunc
* @returns {void}
*
* @typedef {TesterFunc} TesterInterface
* @typedef {Object} TesterProperties
* @property {TesterFunc} after
* @property {TesterFunc} afterEach
* @property {TesterFunc} before
Expand All @@ -67,4 +67,6 @@
* @property {TesterFunc} serial
* @property {TesterFunc} only
* @property {TesterFunc} skip
*
* @typedef {TesterFunc & TesterProperties} TesterInterface
*/
2 changes: 2 additions & 0 deletions packages/zip/index.js
@@ -1,2 +1,4 @@
// eslint-disable-next-line import/export
export * from './src/types.js';
export { ZipReader, readZip } from './src/reader.js';
export { ZipWriter, writeZip } from './src/writer.js';
28 changes: 0 additions & 28 deletions packages/zip/src/exported.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/zip/src/format-reader.js
Expand Up @@ -2,6 +2,11 @@
/* eslint no-bitwise: ["off"], max-lines: ["off"] */

/**
* @typedef {import('./types.js').ArchiveHeaders} ArchiveHeaders
* @typedef {import('./types.js').CompressedFile} CompressedFile
* @typedef {import('./types.js').UncompressedFile} UncompressedFile
* @typedef {import('./types.js').ArchivedFile} ArchivedFile
*
* @typedef {{
* name: Uint8Array,
* version: number,
Expand Down
14 changes: 7 additions & 7 deletions packages/zip/src/format-writer.js
Expand Up @@ -18,7 +18,7 @@
* externalFileAttributes: number,
* content: Uint8Array,
* comment: Uint8Array,
* } & ArchiveHeaders} FileRecord
* } & import('./types.js').ArchiveHeaders} FileRecord
*
* @typedef {{
* index: number,
Expand Down Expand Up @@ -172,8 +172,8 @@ export function writeZipRecords(writer, records, comment = '') {
}

/**
* @param {ArchivedFile} file
* @returns {UncompressedFile}
* @param {import('./types.js').ArchivedFile} file
* @returns {import('./types.js').UncompressedFile}
*/
function encodeFile(file) {
const name = textEncoder.encode(file.name.replace(/\\/g, '/'));
Expand All @@ -188,8 +188,8 @@ function encodeFile(file) {
}

/**
* @param {UncompressedFile} file
* @returns {CompressedFile}
* @param {import('./types.js').UncompressedFile} file
* @returns {import('./types.js').CompressedFile}
*/
function compressFileWithStore(file) {
return {
Expand Down Expand Up @@ -226,7 +226,7 @@ function externalFileAttributes(mode) {
// }

/**
* @param {CompressedFile} file
* @param {import('./types.js').CompressedFile} file
* @returns {FileRecord}
*/
function makeFileRecord(file) {
Expand All @@ -252,7 +252,7 @@ function makeFileRecord(file) {

/**
* @param {BufferWriter} writer
* @param {Array<ArchivedFile>} files
* @param {Array<import('./types.js').ArchivedFile>} files
* @param {string} comment
*/
export function writeZip(writer, files, comment = '') {
Expand Down
6 changes: 3 additions & 3 deletions packages/zip/src/reader.js
Expand Up @@ -30,7 +30,7 @@ export class ZipReader {

/**
* @param {string} name
* @returns {ArchivedStat=}
* @returns {import('./types.js').ArchivedStat=}
*/
stat(name) {
const file = this.files.get(name);
Expand All @@ -49,11 +49,11 @@ export class ZipReader {
/**
* @param {Uint8Array} data
* @param {string} location
* @returns {Promise<ArchiveReader>}
* @returns {Promise<import('./types.js').ArchiveReader>}
*/
export const readZip = async (data, location) => {
const reader = new ZipReader(data, { name: location });
/** @type {ReadFn} */
/** @type {import('./types.js').ReadFn} */
const read = async path => reader.read(path);
return { read };
};
31 changes: 31 additions & 0 deletions packages/zip/src/types.js
@@ -1,5 +1,7 @@
// @ts-check

export {};

/**
* @typedef {{
* mode: number,
Expand Down Expand Up @@ -43,3 +45,32 @@
* uncompressedLength: number,
* }} ArchiveHeaders
*/

/**
* @typedef {Object} ArchiveReader
* @property {ReadFn} read
*/

/**
* @callback ReadFn
* @param {string} name
* @returns {Promise<Uint8Array>} bytes
*/

/**
* @typedef {Object} ArchiveWriter
* @property {WriteFn} write
* @property {SnapshotFn} snapshot
*/

/**
* @callback WriteFn
* @param {string} name
* @param {Uint8Array} bytes
* @returns {Promise<void>}
*/

/**
* @callback SnapshotFn
* @returns {Promise<Uint8Array>}
*/
6 changes: 3 additions & 3 deletions packages/zip/src/writer.js
Expand Up @@ -50,15 +50,15 @@ export class ZipWriter {
}

/**
* @returns {ArchiveWriter}
* @returns {import('./types.js').ArchiveWriter}
*/
export const writeZip = () => {
const writer = new ZipWriter();
/** @type {WriteFn} */
/** @type {import('./types.js').WriteFn} */
const write = async (path, data) => {
writer.write(path, data);
};
/** @type {SnapshotFn} */
/** @type {import('./types.js').SnapshotFn} */
const snapshot = async () => writer.snapshot();
return { write, snapshot };
};

0 comments on commit 2d1e1e0

Please sign in to comment.