@8ms/helpers 1.1.41 → 1.1.44
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.
|
@@ -7,7 +7,7 @@ export declare const timeZoneLondon = "Europe/London";
|
|
|
7
7
|
* We can't use getDate here as it becomes a circular issue.
|
|
8
8
|
* Returns a new Date instance.
|
|
9
9
|
*/
|
|
10
|
-
declare const getTimeZoned: ({ input, setTimeZone }: GetTimeZonedType & {
|
|
10
|
+
declare const getTimeZoned: ({ input, setTimeZone, }: GetTimeZonedType & {
|
|
11
11
|
input: Date;
|
|
12
12
|
}) => Date;
|
|
13
13
|
export default getTimeZoned;
|
|
@@ -16,7 +16,22 @@ var getTimeZoned = function (_a) {
|
|
|
16
16
|
var input = _a.input, _b = _a.setTimeZone, setTimeZone = _b === void 0 ? exports.timeZoneLondon : _b;
|
|
17
17
|
var dateInstance = input;
|
|
18
18
|
if (setTimeZone) {
|
|
19
|
-
|
|
19
|
+
// Cannot convert YYYY-MM-DD into new Date as Safari doesn't support this
|
|
20
|
+
var dateString = (0, date_fns_tz_1.formatInTimeZone)(dateInstance, setTimeZone, 'yyyy-MM-dd HH:mm:ss');
|
|
21
|
+
var year = Number(dateString.substring(0, 4));
|
|
22
|
+
var month = Number(dateString.substring(5, 7)) - 1;
|
|
23
|
+
var day = Number(dateString.substring(8, 10));
|
|
24
|
+
var hours = Number(dateString.substring(11, 13));
|
|
25
|
+
var minutes = Number(dateString.substring(14, 16));
|
|
26
|
+
var seconds = Number(dateString.substring(17, 19));
|
|
27
|
+
dateInstance = new Date();
|
|
28
|
+
// Reset the date to the 1st initially at 1am
|
|
29
|
+
dateInstance.setDate(1);
|
|
30
|
+
dateInstance.setHours(1, 0, 0, 0);
|
|
31
|
+
dateInstance.setFullYear(year);
|
|
32
|
+
dateInstance.setMonth(month);
|
|
33
|
+
dateInstance.setDate(day);
|
|
34
|
+
dateInstance.setHours(hours, minutes, seconds);
|
|
20
35
|
// Fixes the timezone offset due to daylight savings
|
|
21
36
|
dateInstance = (0, addMinutes_1.default)(dateInstance, dateInstance.getTimezoneOffset() * -1);
|
|
22
37
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.writeFile = void 0;
|
|
7
|
+
var writeFile_1 = __importDefault(require("./writeFile"));
|
|
8
|
+
exports.writeFile = writeFile_1.default;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write a file to Google Cloud Storage.
|
|
3
|
+
*/
|
|
4
|
+
export declare const writeFile: ({ GoogleStorageLib, bucket, credentials, data, key }: {
|
|
5
|
+
GoogleStorageLib: any;
|
|
6
|
+
bucket: string;
|
|
7
|
+
credentials: any;
|
|
8
|
+
data: any;
|
|
9
|
+
key: string;
|
|
10
|
+
}) => Promise<any>;
|
|
11
|
+
export default writeFile;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.writeFile = void 0;
|
|
43
|
+
var stream_1 = __importDefault(require("stream"));
|
|
44
|
+
/**
|
|
45
|
+
* Write a file to Google Cloud Storage.
|
|
46
|
+
*/
|
|
47
|
+
var writeFile = function (_a) {
|
|
48
|
+
var GoogleStorageLib = _a.GoogleStorageLib, bucket = _a.bucket, credentials = _a.credentials, data = _a.data, key = _a.key;
|
|
49
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
50
|
+
var Storage, storage, data_stream, google_file;
|
|
51
|
+
return __generator(this, function (_b) {
|
|
52
|
+
switch (_b.label) {
|
|
53
|
+
case 0:
|
|
54
|
+
Storage = GoogleStorageLib.Storage;
|
|
55
|
+
storage = new Storage({
|
|
56
|
+
credentials: credentials,
|
|
57
|
+
});
|
|
58
|
+
data_stream = new stream_1.default.PassThrough();
|
|
59
|
+
google_file = storage.bucket(bucket)
|
|
60
|
+
.file(key);
|
|
61
|
+
data_stream.push(data);
|
|
62
|
+
data_stream.push(null);
|
|
63
|
+
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
64
|
+
data_stream.pipe(google_file.createWriteStream({
|
|
65
|
+
contentType: 'auto',
|
|
66
|
+
resumable: false,
|
|
67
|
+
validation: false,
|
|
68
|
+
metadata: {},
|
|
69
|
+
}))
|
|
70
|
+
.on('error', function (error) {
|
|
71
|
+
reject(error);
|
|
72
|
+
})
|
|
73
|
+
.on('finish', function () {
|
|
74
|
+
console.log("File uploaded successfully.");
|
|
75
|
+
resolve(true);
|
|
76
|
+
});
|
|
77
|
+
})];
|
|
78
|
+
case 1: return [2 /*return*/, _b.sent()];
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
exports.writeFile = writeFile;
|
|
84
|
+
exports.default = exports.writeFile;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@8ms/helpers",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.44",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"@babel/preset-env": "7.18.2",
|
|
22
22
|
"@babel/preset-flow": "7.17.12",
|
|
23
23
|
"@babel/preset-typescript": "7.17.12",
|
|
24
|
+
"@google-cloud/storage": "6.1.0",
|
|
24
25
|
"@types/jest": "27.5.1",
|
|
25
26
|
"@types/lodash": "4.14.182",
|
|
26
27
|
"@types/node": "17.0.35",
|