Skip to content

Commit

Permalink
Merge pull request #47 from AaronJan/feature/manual-cleanup
Browse files Browse the repository at this point in the history
feat: 允许手动决定合适清理OSS对象
  • Loading branch information
yanjixiong authored Sep 27, 2022
2 parents a65b405 + 3a1950e commit fc2312b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ const { receive, reply } = require('fc-toolkit').initReveiver(

async function handler (event, context, callback) {
try {
// `receive` 在从 OSS 收到 payload 后会自动将其删除
const body = await receive(event)
// handle the body here..
const returnValue = await doSomethingYouNeed(body)
Expand All @@ -146,6 +147,27 @@ async function handler (event, context, callback) {
}
```

或者也可以使用 `receiveManually` 来获取包含 header 在内的更多响应内容,以及手动控制 OSS 临时对象的清理:

```js
const { receiveManually, reply } = require('fc-toolkit').initReveiver(
false,
'aws'
)

async function handler (event, context, callback) {
try {
const resp = await receiveManually(event)
const returnValue = await doSomethingYouNeed(resp.body)
await reply(callback)(returnValue)
// 手动调用清理回调
await resp.cleanup()
} catch (e) {
callback(e)
}
}
```

## 使用Buffer

### initInvoker
Expand Down
5 changes: 4 additions & 1 deletion __tests__/receiver-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('receiver test cases', () => {
storeType: 'direct',
isBuffer: true,
});
await resp.cleanup();
expect(Buffer.isBuffer(resp.body)).toBeTruthy();
expect(resp.body.toString()).toBe('test message');
});
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('receiver test cases', () => {
storeType: 'oss',
ossKey: testKey,
});
await resp.cleanup();
expect(resp.body).toBe(testContent);
});

Expand All @@ -109,6 +111,7 @@ describe('receiver test cases', () => {
ossKey: testKey,
isBuffer: true,
});
await resp.cleanup();
expect(Buffer.isBuffer(resp.body)).toBeTruthy();
expect(resp.body.toString()).toBe(testContent);
});
Expand Down Expand Up @@ -163,7 +166,7 @@ describe('receiver test cases', () => {
storeType: 'oss',
ossKey: testKey,
});
expect(resp).toEqual(testBody);
expect(resp).toEqual(JSON.stringify(testBody));
});
});
});
Expand Down
37 changes: 30 additions & 7 deletions src/bufferSupport/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,53 @@ export interface IReceiveParsedPayload {
headers?: any;
isBuffer?: boolean;
}

export interface ILogger {
debug(...data: any[]): void;
info(...data: any[]): void;
warn(...data: any[]): void;
error(...data: any[]): void;
log(...data: any[]): void;
}

export interface IReceiverContext {
logger?: ILogger;
}

export interface IReplyPayload {
storeType: string;
isBuffer?: boolean;
body: string;
meta?: any;
}

export interface IReceiveResponse {
headers?: any;
body: any;
storeType?: string;
cleanup: () => Promise<void>;
}

export function initReceiver(
noOSS: boolean = false,
ossType: StorageEngine = StorageEngine.ALIYUN_OSS,
ossThreshold: number = 0
): {
receive: (
event: Buffer | string | IReceiveParsedPayload
) => Promise<{ headers?: any; body: any; storeType?: string }>;
event: Buffer | string | IReceiveParsedPayload,
context?: IReceiverContext
) => Promise<IReceiveResponse>;
reply: replyFunc;
} {
const fcConfig = loadConfigWithEnvs(ossType);
const storageOptions = fcConfig[ossType] || ({} as any);
const storageClient = getClientByType(ossType, storageOptions);

const receive = async (
event: Buffer | string | IReceiveParsedPayload
): Promise<{ headers?: any; body: string | Buffer; storeType?: string }> => {
event: Buffer | string | IReceiveParsedPayload,
context: IReceiverContext = {}
): Promise<IReceiveResponse> => {
const logger = context.logger ?? console
let storeType: string;
let ossKey: string | undefined;
let body: any;
Expand Down Expand Up @@ -101,16 +124,16 @@ export function initReceiver(
}
const content: Buffer = resp.content;

storageClient.del(ossKey as string).catch(console.error);
const cleanup = () => storageClient.del(ossKey as string).catch(logger.error);

return omitBy(
{ headers, body: isBuffer ? content : content.toString(), storeType },
{ headers, body: isBuffer ? content : content.toString(), storeType, cleanup },
(v: any) => v === undefined
);
}

return omitBy(
{ headers, body: isBuffer ? Buffer.from(body, 'base64') : body },
{ headers, body: isBuffer ? Buffer.from(body, 'base64') : body, cleanup: () => Promise.resolve(void 0) },
(v: any) => v === undefined
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export function initReceiver(
ossType: StorageEngine = StorageEngine.ALIYUN_OSS
): {
receive: (event: string | IReceiveParsedPayload) => Promise<any>;
receiveManually: (event: string | IReceiveParsedPayload) => Promise<bufferSupport.IReceiveResponse>;
reply: bufferSupport.replyFunc;
} {
const { receive, reply } = bufferSupport.initReceiver(noOSS, ossType, 0);

return {
receive: (event: string | IReceiveParsedPayload) =>
receive(event).then(res => res.body),
receive(event).then(res => res.cleanup().then(() => res.body)),
receiveManually: receive,
reply,
};
}

0 comments on commit fc2312b

Please sign in to comment.