Improve use of files as input. Add dish to repl
This commit is contained in:
parent
431f1d4be0
commit
25e0585742
@ -23,6 +23,14 @@ class SyncDish extends Dish {
|
|||||||
* @param {String|Number} - The dish type, as enum or string
|
* @param {String|Number} - The dish type, as enum or string
|
||||||
*/
|
*/
|
||||||
constructor(inputOrDish=null, type=null) {
|
constructor(inputOrDish=null, type=null) {
|
||||||
|
|
||||||
|
// Allow `fs` file input:
|
||||||
|
// Any node fs Buffers transformed to array buffer
|
||||||
|
// NOT Buffer.buff, as this makes a buffer of the whole object.
|
||||||
|
if (Buffer.isBuffer(inputOrDish)) {
|
||||||
|
inputOrDish = new Uint8Array(inputOrDish).buffer;
|
||||||
|
}
|
||||||
|
|
||||||
super(inputOrDish, type);
|
super(inputOrDish, type);
|
||||||
|
|
||||||
// Add operations to make it composable
|
// Add operations to make it composable
|
||||||
|
@ -8,8 +8,6 @@
|
|||||||
|
|
||||||
/*eslint no-console: ["off"] */
|
/*eslint no-console: ["off"] */
|
||||||
|
|
||||||
|
|
||||||
import Dish from "../core/Dish";
|
|
||||||
import SyncDish from "./SyncDish";
|
import SyncDish from "./SyncDish";
|
||||||
import NodeRecipe from "./NodeRecipe";
|
import NodeRecipe from "./NodeRecipe";
|
||||||
import OperationConfig from "./config/OperationConfig.json";
|
import OperationConfig from "./config/OperationConfig.json";
|
||||||
@ -107,15 +105,11 @@ function ensureIsDish(input) {
|
|||||||
return new SyncDish();
|
return new SyncDish();
|
||||||
}
|
}
|
||||||
|
|
||||||
let dish;
|
|
||||||
if (input instanceof SyncDish) {
|
if (input instanceof SyncDish) {
|
||||||
dish = input;
|
return input;
|
||||||
} else {
|
} else {
|
||||||
dish = new SyncDish();
|
return new SyncDish(input);
|
||||||
const type = Dish.typeEnum(input.constructor.name);
|
|
||||||
dish.set(input, type);
|
|
||||||
}
|
}
|
||||||
return dish;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,10 +120,6 @@ function ensureIsDish(input) {
|
|||||||
* @param args - operation args
|
* @param args - operation args
|
||||||
*/
|
*/
|
||||||
function prepareOp(opInstance, input, args) {
|
function prepareOp(opInstance, input, args) {
|
||||||
// convert any Buffers into ArrayBuffers.
|
|
||||||
if (input instanceof Buffer) {
|
|
||||||
input = input.buffer;
|
|
||||||
}
|
|
||||||
const dish = ensureIsDish(input);
|
const dish = ensureIsDish(input);
|
||||||
let transformedArgs;
|
let transformedArgs;
|
||||||
// Transform object-style args to original args array
|
// Transform object-style args to original args array
|
||||||
|
@ -34,3 +34,4 @@ operations.forEach((op) => {
|
|||||||
|
|
||||||
replServer.context.help = chef.help;
|
replServer.context.help = chef.help;
|
||||||
replServer.context.bake = chef.bake;
|
replServer.context.bake = chef.bake;
|
||||||
|
replServer.context.Dish = chef.Dish;
|
||||||
|
@ -15,6 +15,7 @@ import it from "../assertionHandler";
|
|||||||
import chef from "../../../src/node/index";
|
import chef from "../../../src/node/index";
|
||||||
import OperationError from "../../../src/core/errors/OperationError";
|
import OperationError from "../../../src/core/errors/OperationError";
|
||||||
import SyncDish from "../../../src/node/SyncDish";
|
import SyncDish from "../../../src/node/SyncDish";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
import { toBase32, Dish } from "../../../src/node/index";
|
import { toBase32, Dish } from "../../../src/node/index";
|
||||||
import TestRegister from "../../TestRegister";
|
import TestRegister from "../../TestRegister";
|
||||||
@ -349,6 +350,13 @@ TestRegister.addApiTests([
|
|||||||
assert.strictEqual(JSONDish.type, 6);
|
assert.strictEqual(JSONDish.type, 6);
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
it("Composable dish: Buffer type dishes should be converted to strings", () => {
|
||||||
|
fs.writeFileSync("test.txt", "abc");
|
||||||
|
const dish = new Dish(fs.readFileSync("test.txt"));
|
||||||
|
assert.strictEqual(dish.type, 4);
|
||||||
|
fs.unlinkSync("test.txt");
|
||||||
|
}),
|
||||||
|
|
||||||
it("Excluded operations: throw a sensible error when you try and call one", () => {
|
it("Excluded operations: throw a sensible error when you try and call one", () => {
|
||||||
try {
|
try {
|
||||||
chef.fork();
|
chef.fork();
|
||||||
|
Loading…
Reference in New Issue
Block a user