@rsdoctor/utils 0.3.6 → 0.3.8-beta.0

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.
@@ -43,7 +43,7 @@ class FileSharding {
43
43
  /**
44
44
  * @param ext the extension name of the output file (must starts with ".")
45
45
  */
46
- createVirtualShardingFiles(ext = "") {
46
+ createVirtualShardingFiles(ext = "", index = 0) {
47
47
  const bf = Buffer.from(this.content, this.encoding);
48
48
  const res = [];
49
49
  const threshold = this.limitBytes;
@@ -52,16 +52,16 @@ class FileSharding {
52
52
  res.push(bf.subarray(tmpBytes, tmpBytes + threshold));
53
53
  tmpBytes += threshold;
54
54
  }
55
- return res.map((e, i) => ({ filename: `${i}${ext}`, content: e }));
55
+ return res.map((e, i) => ({ filename: `${i + index}${ext}`, content: e }));
56
56
  }
57
57
  /**
58
58
  * @param folder absolute path of folder which used to save string sharding files.
59
59
  * @param ext the extension name of the output file (must starts with ".")
60
60
  */
61
- async writeStringToFolder(folder, ext = "") {
61
+ async writeStringToFolder(folder, ext = "", index) {
62
62
  const dist = import_path.default.resolve(folder);
63
63
  await import_fs_extra.default.ensureDir(dist);
64
- const res = this.createVirtualShardingFiles(ext);
64
+ const res = this.createVirtualShardingFiles(ext, index);
65
65
  await Promise.all(
66
66
  res.map(
67
67
  (e) => new Promise((resolve, reject) => {
@@ -23,28 +23,53 @@ __export(json_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(json_exports);
25
25
  var import_json_stream_stringify = require("json-stream-stringify");
26
- var import_stream = require("stream");
27
26
  var import_path = require("path");
28
27
  var import_common = require("../common");
28
+ var import_stream = require("stream");
29
+ const maxFileSize = 1024 * 1024 * 400;
29
30
  function stringify(json, replacer, space, cycle) {
31
+ const jsonList = [];
30
32
  if (json && typeof json === "object") {
31
33
  return new Promise((resolve, reject) => {
32
- let res = "";
33
- const pt = new import_stream.PassThrough();
34
34
  const stream = new import_json_stream_stringify.JsonStreamStringify(json, replacer, space, cycle);
35
- pt.on("data", (chunk) => {
36
- res += chunk;
37
- });
38
- pt.on("end", () => {
39
- return resolve(res);
40
- });
41
- pt.on("error", (err) => {
42
- return reject(err);
35
+ let currentLength = 0;
36
+ let currentContent = "";
37
+ const batchProcessor = new import_stream.Transform({
38
+ readableObjectMode: true,
39
+ transform(chunk, _encoding, callback) {
40
+ const lines = chunk.toString().split("\\n");
41
+ lines.forEach((line) => {
42
+ if (currentLength + line.length > maxFileSize) {
43
+ jsonList.push(currentContent);
44
+ currentContent = "";
45
+ currentLength = 0;
46
+ }
47
+ if (line.length) {
48
+ currentContent += line;
49
+ currentLength += line.length;
50
+ }
51
+ });
52
+ callback();
53
+ }
43
54
  });
44
- stream.on("error", (err) => {
55
+ stream.pipe(batchProcessor).on("data", (line) => {
56
+ if (currentLength + line.length > maxFileSize) {
57
+ jsonList.push(currentContent);
58
+ currentContent = "";
59
+ currentLength = 0;
60
+ }
61
+ if (line.length) {
62
+ currentContent += line;
63
+ currentLength += line.length;
64
+ }
65
+ }).on("end", () => {
66
+ if (jsonList.length < 1) {
67
+ jsonList.push(currentContent);
68
+ }
69
+ resolve(jsonList);
70
+ }).on("error", (err) => {
45
71
  return reject(err);
46
72
  });
47
- stream.pipe(pt);
48
73
  });
49
74
  }
50
75
  return Promise.resolve(JSON.stringify(json, replacer, space));
@@ -10,7 +10,7 @@ class FileSharding {
10
10
  /**
11
11
  * @param ext the extension name of the output file (must starts with ".")
12
12
  */
13
- createVirtualShardingFiles(ext = "") {
13
+ createVirtualShardingFiles(ext = "", index = 0) {
14
14
  const bf = Buffer.from(this.content, this.encoding);
15
15
  const res = [];
16
16
  const threshold = this.limitBytes;
@@ -19,16 +19,16 @@ class FileSharding {
19
19
  res.push(bf.subarray(tmpBytes, tmpBytes + threshold));
20
20
  tmpBytes += threshold;
21
21
  }
22
- return res.map((e, i) => ({ filename: `${i}${ext}`, content: e }));
22
+ return res.map((e, i) => ({ filename: `${i + index}${ext}`, content: e }));
23
23
  }
24
24
  /**
25
25
  * @param folder absolute path of folder which used to save string sharding files.
26
26
  * @param ext the extension name of the output file (must starts with ".")
27
27
  */
28
- async writeStringToFolder(folder, ext = "") {
28
+ async writeStringToFolder(folder, ext = "", index) {
29
29
  const dist = path.resolve(folder);
30
30
  await fse.ensureDir(dist);
31
- const res = this.createVirtualShardingFiles(ext);
31
+ const res = this.createVirtualShardingFiles(ext, index);
32
32
  await Promise.all(
33
33
  res.map(
34
34
  (e) => new Promise((resolve, reject) => {
@@ -1,26 +1,51 @@
1
1
  import { JsonStreamStringify } from "json-stream-stringify";
2
- import { PassThrough } from "stream";
3
2
  import { dirname, join } from "path";
4
3
  import { Package } from "../common";
4
+ import { Transform } from "stream";
5
+ const maxFileSize = 1024 * 1024 * 400;
5
6
  function stringify(json, replacer, space, cycle) {
7
+ const jsonList = [];
6
8
  if (json && typeof json === "object") {
7
9
  return new Promise((resolve, reject) => {
8
- let res = "";
9
- const pt = new PassThrough();
10
10
  const stream = new JsonStreamStringify(json, replacer, space, cycle);
11
- pt.on("data", (chunk) => {
12
- res += chunk;
11
+ let currentLength = 0;
12
+ let currentContent = "";
13
+ const batchProcessor = new Transform({
14
+ readableObjectMode: true,
15
+ transform(chunk, _encoding, callback) {
16
+ const lines = chunk.toString().split("\\n");
17
+ lines.forEach((line) => {
18
+ if (currentLength + line.length > maxFileSize) {
19
+ jsonList.push(currentContent);
20
+ currentContent = "";
21
+ currentLength = 0;
22
+ }
23
+ if (line.length) {
24
+ currentContent += line;
25
+ currentLength += line.length;
26
+ }
27
+ });
28
+ callback();
29
+ }
13
30
  });
14
- pt.on("end", () => {
15
- return resolve(res);
16
- });
17
- pt.on("error", (err) => {
18
- return reject(err);
19
- });
20
- stream.on("error", (err) => {
31
+ stream.pipe(batchProcessor).on("data", (line) => {
32
+ if (currentLength + line.length > maxFileSize) {
33
+ jsonList.push(currentContent);
34
+ currentContent = "";
35
+ currentLength = 0;
36
+ }
37
+ if (line.length) {
38
+ currentContent += line;
39
+ currentLength += line.length;
40
+ }
41
+ }).on("end", () => {
42
+ if (jsonList.length < 1) {
43
+ jsonList.push(currentContent);
44
+ }
45
+ resolve(jsonList);
46
+ }).on("error", (err) => {
21
47
  return reject(err);
22
48
  });
23
- stream.pipe(pt);
24
49
  });
25
50
  }
26
51
  return Promise.resolve(JSON.stringify(json, replacer, space));
@@ -7,7 +7,7 @@ export declare class FileSharding {
7
7
  /**
8
8
  * @param ext the extension name of the output file (must starts with ".")
9
9
  */
10
- createVirtualShardingFiles(ext?: string): {
10
+ createVirtualShardingFiles(ext?: string, index?: number): {
11
11
  filename: string;
12
12
  content: Buffer;
13
13
  }[];
@@ -15,7 +15,7 @@ export declare class FileSharding {
15
15
  * @param folder absolute path of folder which used to save string sharding files.
16
16
  * @param ext the extension name of the output file (must starts with ".")
17
17
  */
18
- writeStringToFolder(folder: string, ext?: string): Promise<{
18
+ writeStringToFolder(folder: string, ext?: string, index?: number): Promise<{
19
19
  filename: string;
20
20
  content: Buffer;
21
21
  }[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"sharding.d.ts","sourceRoot":"","sources":["../../../../src/build/file/sharding.ts"],"names":[],"mappings":";AAIA,qBAAa,YAAY;IAErB,SAAS,CAAC,OAAO,EAAE,MAAM;IACzB,SAAS,CAAC,UAAU;IACpB,SAAS,CAAC,QAAQ,EAAE,cAAc;gBAFxB,OAAO,EAAE,MAAM,EACf,UAAU,SAAmB,EAC7B,QAAQ,GAAE,cAAwB;IAG9C;;OAEG;IACI,0BAA0B,CAAC,GAAG,SAAK;;;;IAc1C;;;OAGG;IACU,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,SAAK;;;;CAsB1D"}
1
+ {"version":3,"file":"sharding.d.ts","sourceRoot":"","sources":["../../../../src/build/file/sharding.ts"],"names":[],"mappings":";AAIA,qBAAa,YAAY;IAErB,SAAS,CAAC,OAAO,EAAE,MAAM;IACzB,SAAS,CAAC,UAAU;IACpB,SAAS,CAAC,QAAQ,EAAE,cAAc;gBAFxB,OAAO,EAAE,MAAM,EACf,UAAU,SAAmB,EAC7B,QAAQ,GAAE,cAAwB;IAG9C;;OAEG;IACI,0BAA0B,CAAC,GAAG,SAAK,EAAE,KAAK,SAAI;;;;IAcrD;;;OAGG;IACU,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,SAAK,EAAE,KAAK,CAAC,EAAE,MAAM;;;;CAsB1E"}
@@ -1 +1 @@
1
- {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/build/json.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAItC,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,SAAS,SAAS,GAAG,SAAS,GAAG,MAAM,EACvE,IAAI,EAAE,CAAC,EACP,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG,EACtD,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EACvB,KAAK,CAAC,EAAE,OAAO,GACd,OAAO,CAAC,CAAC,CAAC,CA4BZ;AAGD,eAAO,MAAM,eAAe,SACpB,MAAM,aACD,IAAI,cAAc,KAC5B,IAAI,gBAAgB,GAAG,SAiCzB,CAAC"}
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../src/build/json.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAOtC,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,SAAS,SAAS,GAAG,SAAS,GAAG,MAAM,EACvE,IAAI,EAAE,CAAC,EACP,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG,EACtD,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EACvB,KAAK,CAAC,EAAE,OAAO,GACd,OAAO,CAAC,CAAC,CAAC,CA6DZ;AAGD,eAAO,MAAM,eAAe,SACpB,MAAM,aACD,IAAI,cAAc,KAC5B,IAAI,gBAAgB,GAAG,SAiCzB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdoctor/utils",
3
- "version": "0.3.6",
3
+ "version": "0.3.8-beta.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/web-infra-dev/rsdoctor",
@@ -84,7 +84,7 @@
84
84
  "lodash": "^4.17.21",
85
85
  "rslog": "^1.2.0",
86
86
  "strip-ansi": "^6.0.1",
87
- "@rsdoctor/types": "0.3.6"
87
+ "@rsdoctor/types": "0.3.8-beta.0"
88
88
  },
89
89
  "devDependencies": {
90
90
  "@types/babel__code-frame": "7.0.3",