Skip to content

Commit 0e6a9f4

Browse files
authored
refactor: Consolidate tmpDir definitions into a package "internal" export (#11139)
## Description As noted in #10954 (comment) , the callback for [`tmp.dir`](https://www.npmjs.com/package/tmp#asynchronous-directory-creation-1) is given three arguments, which is at odds with the normal Node.js (err, value) convention and therefore not compatible with [`util.promisify`](https://nodejs.org/docs/latest/api/util.html#utilpromisifyoriginal) (hence our many open-coded implementations of a _(prefix?: string) => Promise\<[dirName: string, cleanup: () => void]>_ `tmpDir`). Further, all the call sites of those many functions use `await` anyway and aren't doing useful work until the temporary directory is available. This PR defines a single factory function in package "internal" and uses it throughout the codebase—almost entirely confined to testing, with the notable exception of `handleSwingStoreExport`. ### Security Considerations None known. ### Scaling Considerations No impact is expected, and even the non-testing change is after an `await`. ### Documentation Considerations No relevant changes. ### Testing Considerations All tests must continue to pass. ### Upgrade Considerations Nothing special.
2 parents 75e8d38 + 9047455 commit 0e6a9f4

File tree

22 files changed

+133
-191
lines changed

22 files changed

+133
-191
lines changed

a3p-integration/proposals/z:acceptance/yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ __metadata:
99
version: 0.0.0-use.local
1010
resolution: "@agoric/access-token@portal:../../agoric-sdk/packages/access-token::locator=root-workspace-0b6124%40workspace%3A."
1111
dependencies:
12+
"@agoric/internal": "npm:^0.3.2"
1213
n-readlines: "npm:^1.0.0"
1314
proper-lockfile: "npm:^4.1.2"
1415
tmp: "npm:^0.2.1"

packages/SwingSet/test/transcript/state-sync-reload.test.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import test from 'ava';
33

44
import tmp from 'tmp';
5+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
56
import { kunser } from '@agoric/kmarshal';
67
import {
78
initSwingStore,
@@ -12,20 +13,7 @@ import {
1213
import { initializeSwingset, makeSwingsetController } from '../../src/index.js';
1314
import { buildKernelBundle } from '../../src/controller/initializeSwingset.js';
1415

15-
/**
16-
* @param {string} [prefix]
17-
* @returns {Promise<[string, () => void]>}
18-
*/
19-
const tmpDir = prefix =>
20-
new Promise((resolve, reject) => {
21-
tmp.dir({ unsafeCleanup: true, prefix }, (err, name, removeCallback) => {
22-
if (err) {
23-
reject(err);
24-
} else {
25-
resolve([name, removeCallback]);
26-
}
27-
});
28-
});
16+
const tmpDir = makeTempDirFactory(tmp);
2917

3018
const bfile = name => new URL(name, import.meta.url).pathname;
3119

@@ -35,8 +23,8 @@ test.before(async t => {
3523
});
3624

3725
test('state-sync reload', async t => {
38-
const [dbDir, cleanup] = await tmpDir('testdb');
39-
const [importDbDir, cleanupImport] = await tmpDir('importtestdb');
26+
const [dbDir, cleanup] = tmpDir('testdb');
27+
const [importDbDir, cleanupImport] = tmpDir('importtestdb');
4028
t.teardown(cleanup);
4129
t.teardown(cleanupImport);
4230

packages/SwingSet/test/vat-admin/slow-termination/slow-termination.test.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,14 @@ import tmp from 'tmp';
66
import sqlite3 from 'better-sqlite3';
77
import path from 'path';
88

9+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
910
import { kser } from '@agoric/kmarshal';
1011
import { initSwingStore } from '@agoric/swing-store';
1112

1213
import { buildVatController, buildKernelBundles } from '../../../src/index.js';
1314
import { enumeratePrefixedKeys } from '../../../src/kernel/state/storageHelper.js';
1415

15-
/**
16-
* @param {string} [prefix]
17-
* @returns {Promise<[string, () => void]>}
18-
*/
19-
export const tmpDir = prefix =>
20-
new Promise((resolve, reject) => {
21-
tmp.dir({ unsafeCleanup: true, prefix }, (err, name, removeCallback) => {
22-
if (err) {
23-
reject(err);
24-
} else {
25-
resolve([name, removeCallback]);
26-
}
27-
});
28-
});
16+
const tmpDir = makeTempDirFactory(tmp);
2917

3018
test.before(async t => {
3119
const kernelBundles = await buildKernelBundles();
@@ -90,7 +78,7 @@ async function doSlowTerminate(t, mode) {
9078
},
9179
};
9280

93-
const [dbDir, cleanup] = await tmpDir('testdb');
81+
const [dbDir, cleanup] = tmpDir('testdb');
9482
t.teardown(cleanup);
9583

9684
const ss = initSwingStore(dbDir);

packages/access-token/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"lint:types": "tsc"
1919
},
2020
"dependencies": {
21+
"@agoric/internal": "^0.3.2",
2122
"n-readlines": "^1.0.0",
2223
"proper-lockfile": "^4.1.2",
2324
"tmp": "^0.2.1"

packages/access-token/test/state.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import test from 'ava';
2-
import { tmpDir } from './tmp.js';
2+
import tmp from 'tmp';
3+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
34
import {
45
initJSONStore,
56
openJSONStore,
67
getAllState,
78
isJSONStore,
89
} from '../src/json-store.js';
910

11+
const tmpDir = makeTempDirFactory(tmp);
12+
1013
function testStorage(t, storage) {
1114
t.falsy(storage.has('missing'));
1215
t.is(storage.get('missing'), undefined);
@@ -39,7 +42,7 @@ function testStorage(t, storage) {
3942
}
4043

4144
test('storageInFile', async t => {
42-
const [dbDir, cleanup] = await tmpDir('testdb');
45+
const [dbDir, cleanup] = tmpDir('testdb');
4346
t.teardown(cleanup);
4447
t.is(isJSONStore(dbDir), false);
4548
const { storage, commit, close } = await initJSONStore(dbDir);

packages/access-token/test/tmp.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/access-token/test/token.test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import test from 'ava';
2-
import { tmpDir } from './tmp.js';
3-
2+
import tmp from 'tmp';
3+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
44
import { getAccessToken } from '../src/access-token.js';
55

6+
const tmpDir = makeTempDirFactory(tmp);
7+
68
test('access tokens', async t => {
7-
const [sharedStateDir, removeCallback] = await tmpDir('access-token-test');
9+
const [sharedStateDir, removeCallback] = tmpDir('access-token-test');
810
const [a, b, c] = await Promise.all([
911
getAccessToken(1234, sharedStateDir),
1012
getAccessToken(1234, sharedStateDir),

packages/cosmic-swingset/src/chain-main.js

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';
3737
import { makeInitMsg } from '@agoric/internal/src/chain-utils.js';
3838
import * as STORAGE_PATH from '@agoric/internal/src/chain-storage-paths.js';
3939
import * as ActionType from '@agoric/internal/src/action-types.js';
40+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
4041
import { BridgeId, CosmosInitKeyToBridgeId } from '@agoric/internal';
4142
import {
4243
makeArchiveSnapshot,
@@ -64,6 +65,8 @@ import {
6465

6566
const ignore = () => {};
6667

68+
const tmpDir = makeTempDirFactory(tmp);
69+
6770
// eslint-disable-next-line no-unused-vars
6871
let whenHellFreezesOver = null;
6972

@@ -700,38 +703,24 @@ export default async function main(
700703
});
701704
stateSyncExport = exportData;
702705

703-
await new Promise((resolve, reject) => {
704-
tmp.dir(
705-
{
706-
prefix: `agd-state-sync-${blockHeight}-`,
707-
unsafeCleanup: true,
708-
},
709-
(err, exportDir, cleanup) => {
710-
if (err) {
711-
reject(err);
712-
return;
713-
}
714-
exportData.exportDir = exportDir;
715-
/** @type {Promise<void> | undefined} */
716-
let cleanupResult;
717-
exportData.cleanup = async () => {
718-
cleanupResult ||= new Promise(cleanupDone => {
719-
// If the exporter is still the same, then the retriever
720-
// is in charge of cleanups
721-
if (stateSyncExport !== exportData) {
722-
// @ts-expect-error wrong type definitions
723-
cleanup(cleanupDone);
724-
} else {
725-
console.warn('unexpected call of state-sync cleanup');
726-
cleanupDone();
727-
}
728-
});
729-
await cleanupResult;
730-
};
731-
resolve(null);
732-
},
733-
);
734-
});
706+
const [exportDir, cleanup] = tmpDir(`agd-state-sync-${blockHeight}-`);
707+
exportData.exportDir = exportDir;
708+
/** @type {Promise<void> | undefined} */
709+
let cleanupResult;
710+
exportData.cleanup = async () => {
711+
cleanupResult ||= new Promise(cleanupDone => {
712+
// If the exporter is still the same, then the retriever
713+
// is in charge of cleanups
714+
if (stateSyncExport !== exportData) {
715+
// @ts-expect-error wrong type definitions
716+
cleanup(cleanupDone);
717+
} else {
718+
console.warn('unexpected call of state-sync cleanup');
719+
cleanupDone();
720+
}
721+
});
722+
await cleanupResult;
723+
};
735724

736725
console.warn(
737726
'Initiating SwingSet state snapshot at block height',

packages/cosmic-swingset/tools/test-kit.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@agoric/internal/src/action-types.js';
1414
import * as STORAGE_PATH from '@agoric/internal/src/chain-storage-paths.js';
1515
import { deepCopyJsonable } from '@agoric/internal/src/js-utils.js';
16+
import { makeTempDirFactory } from '@agoric/internal/src/tmpDir.js';
1617
import { makeRunUtils } from '@agoric/swingset-vat/tools/run-utils.js';
1718
import { initSwingStore } from '@agoric/swing-store';
1819
import {
@@ -28,6 +29,8 @@ import { makeQueue } from '../src/helpers/make-queue.js';
2829
/** @import { ManagerType, SwingSetConfig } from '@agoric/swingset-vat' */
2930
/** @import { InboundQueue } from '../src/launch-chain.js'; */
3031

32+
const tmpDir = makeTempDirFactory(tmp);
33+
3134
/**
3235
* @template T
3336
* @typedef {(input: T) => T} Replacer
@@ -245,10 +248,7 @@ export const makeCosmicSwingsetTestKit = async (
245248
STORAGE_PATH.HIGH_PRIORITY_QUEUE,
246249
);
247250

248-
const { name: dbDir, removeCallback: cleanupDB } = tmp.dirSync({
249-
prefix: debugName || 'testdb',
250-
unsafeCleanup: true,
251-
});
251+
const [dbDir, cleanupDB] = tmpDir(debugName || 'testdb');
252252
const launchChain = makeLaunchChain(fakeAgcc, dbDir, {
253253
env,
254254
fs,

packages/internal/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * from './js-utils.js';
99
export { pureDataMarshaller } from './marshal.js';
1010
export * from './method-tools.js';
1111
export * from './ses-utils.js';
12+
export * from './tmpDir.js';
1213
export * from './typeCheck.js';
1314
export * from './typeGuards.js';
1415

0 commit comments

Comments
 (0)