wirejs-deploy-amplify-basic 0.0.12-alpha → 0.0.14-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,22 +1,75 @@
|
|
|
1
1
|
import { env } from 'process';
|
|
2
|
+
import { LambdaFunctionURLHandler, APIGatewayProxyEventV2 } from 'aws-lambda';
|
|
3
|
+
import { CookieJar, Context, requiresContext } from 'wirejs-resources';
|
|
4
|
+
import * as api from '../../../api/index';
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
function createContext(event: APIGatewayProxyEventV2) {
|
|
7
|
+
const baseUrl = `https://${event.requestContext.domainName}${event.rawPath}`;
|
|
8
|
+
const queryString = event.queryStringParameters
|
|
9
|
+
? `?${new URLSearchParams(event.queryStringParameters as any).toString()}`
|
|
10
|
+
: '';
|
|
11
|
+
const location = new URL(`${baseUrl}${queryString}`);
|
|
4
12
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// PutObjectCommand,
|
|
9
|
-
// GetObjectCommand
|
|
10
|
-
// } from '@aws-sdk/client-s3';
|
|
13
|
+
const cookies = new CookieJar(event.headers['cookie']);
|
|
14
|
+
return new Context({ cookies, location });
|
|
15
|
+
}
|
|
11
16
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
async function callApiMethod(api: any, call: any, context: any) {
|
|
18
|
+
try {
|
|
19
|
+
const [scope, ...rest] = call.method;
|
|
20
|
+
console.log('api method parsed', { scope, rest });
|
|
21
|
+
if (rest.length === 0) {
|
|
22
|
+
console.log('api method resolved. invoking...');
|
|
23
|
+
if (requiresContext(api[scope])) {
|
|
24
|
+
return {
|
|
25
|
+
data: await (api[scope] as any)(context, ...call.args.slice(1))
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
return {
|
|
29
|
+
data: await api[scope](...call.args)
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
console.log('nested scope found');
|
|
34
|
+
return callApiMethod(api[scope], {
|
|
35
|
+
...call,
|
|
36
|
+
method: rest,
|
|
37
|
+
}, context);
|
|
38
|
+
}
|
|
39
|
+
} catch (error: any) {
|
|
40
|
+
console.log(error);
|
|
41
|
+
return { error: error.message };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
15
44
|
|
|
16
45
|
export const handler: LambdaFunctionURLHandler = async (event, context) => {
|
|
46
|
+
const calls = JSON.parse(event.body!);
|
|
47
|
+
const responses = [];
|
|
48
|
+
|
|
49
|
+
for (const call of calls) {
|
|
50
|
+
console.log('handling API call', call);
|
|
51
|
+
responses.push(await callApiMethod(api, call, context));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const wjsContext = createContext(event);
|
|
55
|
+
|
|
56
|
+
console.log('setting cookies', wjsContext.cookies.getSetCookies());
|
|
57
|
+
|
|
58
|
+
const cookies: string[] = [];
|
|
59
|
+
for (const cookie of wjsContext.cookies.getSetCookies()) {
|
|
60
|
+
const cookieOptions = [];
|
|
61
|
+
if (cookie.maxAge) cookieOptions.push(`Max-Age=${cookie.maxAge}`);
|
|
62
|
+
if (cookie.httpOnly) cookieOptions.push('HttpOnly');
|
|
63
|
+
if (cookie.secure) cookieOptions.push('Secure');
|
|
64
|
+
cookies.push(`${cookie.name}=${cookie.value}; ${cookieOptions.join('; ')}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
17
67
|
return {
|
|
18
68
|
statusCode: 200,
|
|
19
|
-
cookies
|
|
20
|
-
|
|
69
|
+
cookies,
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
72
|
+
},
|
|
73
|
+
body: JSON.stringify(responses)
|
|
21
74
|
}
|
|
22
75
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-deploy-amplify-basic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14-alpha",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"wirejs-deploy-amplify-basic": "./build.js"
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"recursive-copy": "^2.0.14",
|
|
13
13
|
"rimraf": "^6.0.1",
|
|
14
14
|
"wirejs-dom": "^1.0.34",
|
|
15
|
-
"wirejs-resources": "^0.1.
|
|
15
|
+
"wirejs-resources": "^0.1.8-alpha"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@aws-amplify/backend": "^1.14.0",
|
|
19
|
-
"@aws-sdk/client-s3": "^3.
|
|
19
|
+
"@aws-sdk/client-s3": "^3.738.0"
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { env } from 'process';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
S3Client,
|
|
5
|
+
ListObjectsCommand,
|
|
6
|
+
PutObjectCommand,
|
|
7
|
+
GetObjectCommand,
|
|
8
|
+
DeleteObjectCommand
|
|
9
|
+
} from '@aws-sdk/client-s3';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
overrides,
|
|
13
|
+
Resource,
|
|
14
|
+
} from 'wirejs-resources';
|
|
3
15
|
|
|
4
16
|
export {
|
|
5
17
|
AuthenticationService,
|
|
@@ -7,40 +19,98 @@ export {
|
|
|
7
19
|
requiresContext,
|
|
8
20
|
Context,
|
|
9
21
|
CookieJar,
|
|
22
|
+
FileService,
|
|
10
23
|
Resource,
|
|
11
24
|
overrides,
|
|
12
25
|
} from 'wirejs-resources';
|
|
13
26
|
|
|
27
|
+
const Bucket = env['BUCKET'];
|
|
28
|
+
const s3 = new S3Client();
|
|
29
|
+
|
|
14
30
|
export class FileService extends Resource {
|
|
15
31
|
constructor(scope, id) {
|
|
16
32
|
super(scope, id);
|
|
17
33
|
addResource('FileService', { absoluteId: this.absoluteId });
|
|
18
34
|
}
|
|
19
35
|
|
|
20
|
-
|
|
21
|
-
|
|
36
|
+
/**
|
|
37
|
+
* @param {string} filename
|
|
38
|
+
* @param {BufferEncoding} [encoding]
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
async read(filename, encoding = 'utf8') {
|
|
42
|
+
const Key = `${this.absoluteId}/${filename}`;
|
|
43
|
+
const command = new GetObjectCommand({ Bucket, Key });
|
|
44
|
+
const result = await s3.send(command);
|
|
45
|
+
return result.Body.transformToString(encoding);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {string} filename
|
|
50
|
+
* @param {string} data
|
|
51
|
+
* @param {{
|
|
52
|
+
* onlyIfNotExists: boolean
|
|
53
|
+
* }} [param2]
|
|
54
|
+
*/
|
|
55
|
+
async write(filename, data, { onlyIfNotExists = false} = {}) {
|
|
56
|
+
const Key = `${this.absoluteId}/${filename}`;
|
|
57
|
+
const Body = data;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @type {ConstructorParameters<typeof PutObjectCommand>[0]}
|
|
61
|
+
*/
|
|
62
|
+
const commandDetails = { Bucket, Key, Body };
|
|
63
|
+
if (onlyIfNotExists) {
|
|
64
|
+
commandDetails['IfNoneMatch'] = '*';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const command = new PutObjectCommand();
|
|
68
|
+
return s3.send(command);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {string} filename
|
|
73
|
+
*/
|
|
74
|
+
async delete(filename) {
|
|
75
|
+
const Key = `${this.absoluteId}/${filename}`;
|
|
76
|
+
const command = new DeleteObjectCommand({
|
|
77
|
+
Bucket,
|
|
78
|
+
Key
|
|
79
|
+
});
|
|
80
|
+
return s3.send(command);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async * list({ prefix = '' } = {}) {
|
|
84
|
+
const Prefix = `${this.absoluteId}/${prefix}`;
|
|
85
|
+
let Marker = null;
|
|
86
|
+
|
|
87
|
+
while (true) {
|
|
88
|
+
const command = new ListObjectsCommand({
|
|
89
|
+
Bucket,
|
|
90
|
+
Prefix,
|
|
91
|
+
MaxKeys: 1000,
|
|
92
|
+
Marker
|
|
93
|
+
});
|
|
94
|
+
const result = await s3.send(command);
|
|
95
|
+
Marker = result.NextMarker;
|
|
96
|
+
|
|
97
|
+
for (const o of result.Contents || []) {
|
|
98
|
+
yield o.Key;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!Marker) break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
isAlreadyExistsError(error) {
|
|
106
|
+
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html
|
|
107
|
+
return error?.$metadata?.httpStatusCode === 412;
|
|
22
108
|
}
|
|
23
109
|
}
|
|
24
110
|
|
|
25
111
|
// expose resources to other resources that might depend on it.
|
|
26
112
|
overrides.FileService = FileService;
|
|
27
113
|
|
|
28
|
-
// export class AuthenticationService {
|
|
29
|
-
// constructor(id) {
|
|
30
|
-
// addResource('AuthenticationService', { id });
|
|
31
|
-
// }
|
|
32
|
-
|
|
33
|
-
// buildApi(...args) {
|
|
34
|
-
// // console.log('AuthService.buildApi', [args]);
|
|
35
|
-
// }
|
|
36
|
-
// }
|
|
37
|
-
|
|
38
|
-
// export class Secret {
|
|
39
|
-
// constructor(id) {
|
|
40
|
-
// addResource('Secret', { id });
|
|
41
|
-
// }
|
|
42
|
-
// }
|
|
43
|
-
|
|
44
114
|
globalThis.wirejsResources = [];
|
|
45
115
|
|
|
46
116
|
function addResource(type, options) {
|