Skip to content

Commit

Permalink
chore: Remove unused special case for JSON input TDE-1030 (#900)
Browse files Browse the repository at this point in the history
#### Motivation

Simplify JSON file reading.

#### Checklist

- [ ] Tests updated (untested code)
- [ ] Docs updated (N/A)
- [x] Issue linked in Title
  • Loading branch information
l0b0 authored Mar 15, 2024
1 parent 125cfd5 commit 96ae5cf
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/commands/copy/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { fsa } from '@chunkd/fs';
import { WorkerRpcPool } from '@wtrpc/core';
import { boolean, command, flag, number, option, restPositionals, string } from 'cmd-ts';
import { performance } from 'perf_hooks';
import { gunzipSync } from 'zlib';
import * as z from 'zod';

import { CliInfo } from '../../cli.info.js';
Expand All @@ -14,22 +13,6 @@ import { CopyContract } from './copy-rpc.js';
const CopyValidator = z.object({ source: z.string(), target: z.string() });
const CopyManifest = z.array(CopyValidator);

/**
* Attempt to figure out how the configuration is pass to us
* - Could be a path to a S3 location s3://foo/bar.json
* - Could be a JSON document "[{}]"
* - Could be a Base64'd Gzipped document
*/
async function tryParse(x: string): Promise<unknown> {
if (x.startsWith('s3://') || x.startsWith('./') || x.startsWith('/')) {
const json = await fsa.readJson<ActionCopy>(x);
if (json.action !== 'copy') throw new Error('Invalid action: ' + json.action + ' from:' + x);
return json.parameters.manifest;
}
if (x.startsWith('[') || x.startsWith('{')) return JSON.parse(x);
return JSON.parse(gunzipSync(Buffer.from(x, 'base64url')).toString());
}

export const commandCopy = command({
name: 'copy',
description: 'Copy a manifest of files',
Expand Down Expand Up @@ -87,7 +70,9 @@ export const commandCopy = command({
const chunks = [];
const startTime = performance.now();
for (const m of args.manifest) {
const data = await tryParse(m);
const json = await fsa.readJson<ActionCopy>(m);
if (json.action !== 'copy') throw new Error('Invalid action: ' + json.action + ' from:' + m);
const data = json.parameters.manifest;
const manifest = CopyManifest.parse(data);

const chunkSize = Math.ceil(manifest.length / args.concurrency);
Expand Down

0 comments on commit 96ae5cf

Please sign in to comment.