UNPKG

@tradle/aws-s3-client

Version:
174 lines 6.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const aws_sdk_1 = __importDefault(require("aws-sdk")); const tape_1 = __importDefault(require("tape")); const sinon_1 = __importDefault(require("sinon")); const lru_cache_1 = __importDefault(require("lru-cache")); const aws_client_factory_1 = require("@tradle/aws-client-factory"); const test_1 = require("@tradle/aws-common-utils/lib/test"); const aws_common_utils_1 = require("@tradle/aws-common-utils"); const __1 = require(".."); const kv_1 = require("../kv"); test_1.initTest(aws_sdk_1.default); const clients = aws_client_factory_1.createClientFactory({ AWS: aws_sdk_1.default, defaults: { region: 'us-east-1' } }); const s3 = clients.s3(); const client = __1.createClient({ client: s3 }); tape_1.default('getCacheable', async (t) => { const sandbox = sinon_1.default.createSandbox(); const bucketName = `test-${aws_common_utils_1.randomString(10)}`; const bucket = __1.wrapBucket({ bucket: bucketName, client }); try { await bucket.create(); } catch (e) { console.log(e); console.warn('Did you maybe not start a local test server?'); return; } const key = 'a'; const cacheable = bucket.getCacheable({ key, parse: JSON.parse.bind(JSON), ttl: 100 }); try { await cacheable.get(); t.fail('expected error'); } catch (err) { t.equal(err.name, 'NotFound'); } let value = { a: 1 }; await cacheable.put({ value }); const getObjectSpy = sandbox.spy(s3, 'getObject'); t.same(await cacheable.get(), value); t.equal(getObjectSpy.callCount, 0); t.same(await cacheable.get(), value); t.equal(getObjectSpy.callCount, 0); value = { a: 2 }; await bucket.putJSON(key, value); await new Promise(resolve => setTimeout(resolve, 200)); t.same(await cacheable.get(), value); t.equal(getObjectSpy.callCount, 1); t.same(await cacheable.get(), value); t.equal(getObjectSpy.callCount, 1); getObjectSpy.restore(); await bucket.del(key); await s3.deleteBucket({ Bucket: bucketName }).promise(); sandbox.restore(); }); tape_1.default('Bucket', async (t) => { const bucketName = `test-${Date.now()}-${aws_common_utils_1.randomString(10)}`; const bucket = __1.wrapBucket({ bucket: bucketName, client }); await bucket.create(); const ops = [ { method: 'exists', args: ['abc'], result: false }, { method: 'get', args: ['abc'], error: 'NotFound' }, { method: 'getJSON', args: ['abc'], error: 'NotFound' }, { method: 'put', args: ['abc', { cba: 1 }] }, { method: 'exists', args: ['abc'], result: true }, { method: 'get', args: ['abc'], body: Buffer.from(JSON.stringify({ cba: 1 })) }, { method: 'getJSON', args: ['abc'], result: { cba: 1 } }, { method: 'del', args: ['abc'] }, { method: 'exists', args: ['abc'], result: false }, { method: 'exists', args: ['abcd'], result: false }, { method: 'del', args: ['abcd'], result: undefined }, { method: 'getJSON', args: ['abc'], error: 'NotFound' } ]; for (const op of ops) { const { method, args, result, body, error } = op; try { const actualResult = await bucket[method].apply(bucket, args); const msg = `${method}(${args})`; if (error) { t.fail(`${msg} expected error: ${error}`); } else if (typeof result !== 'undefined') { t.same(actualResult, result, `${msg} ${actualResult} != ${result}`); } else if (typeof body !== 'undefined') { t.same(actualResult.Body, body, `${msg} ${actualResult.Body} != ${body}`); } } catch (err) { t.equal(err.name, error); } } await bucket.destroy(); }); tape_1.default('Bucket with cache', async (t) => { const sandbox = sinon_1.default.createSandbox(); const bucketName = `test-${Date.now()}-${aws_common_utils_1.randomString(10)}`; const bucket = __1.wrapBucketMemoized({ bucket: bucketName, client, cache: new lru_cache_1.default({ maxAge: 500 }) }); await bucket.create(); const ops = [ { method: 'exists', args: ['abc'], result: false }, { method: 'get', args: ['abc'], error: 'NotFound' }, { method: 'getJSON', args: ['abc'], error: 'NotFound' }, { method: 'putJSON', args: ['abc', { cba: 1 }] }, { method: 'exists', args: ['abc'], result: true }, { method: 'get', args: ['abc'], body: Buffer.from(JSON.stringify({ cba: 1 })) }, { method: 'getJSON', args: ['abc'], result: { cba: 1 }, cached: true }, { method: 'del', args: ['abc'] }, { method: 'exists', args: ['abc'], result: false }, { method: 'exists', args: ['abcd'], result: false }, { method: 'del', args: ['abcd'], result: undefined } ]; for (const op of ops) { await new Promise(resolve => setTimeout(resolve, 50)); const { method, args, result, body, cached, error } = op; let getObjStub; if (cached) { getObjStub = sandbox.stub(s3, 'getObject').callsFake(() => { t.fail('expected object to be cached'); throw new Error(); }); } try { const actualResult = await bucket[method].apply(bucket, args); const msg = `${method}(${args})`; if (error) { t.fail(`${msg} expected error: ${error}`); } else if (typeof result !== 'undefined') { t.same(actualResult, result, `${msg} ${actualResult} != ${result}`); } else if (typeof body !== 'undefined') { t.same(actualResult.Body, body, `${msg} ${actualResult.Body} != ${body}`); } } catch (err) { t.equal(err.name, error); } finally { if (getObjStub) { getObjStub.restore(); } } } await bucket.destroy(); sandbox.restore(); }); (async () => { const bucket = __1.wrapBucket({ bucket: `test-${aws_common_utils_1.randomString(10)}`, client }); await bucket.create(); test_1.testKV({ name: 's3 kv', create: () => kv_1.createJsonKVStore({ folder: bucket }), done: () => bucket.destroy() }); })(); //# sourceMappingURL=bucket.js.map