@strapi/data-transfer 5.61.1 → 5.62.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.
Files changed (85) hide show
  1. package/dist/directory/providers/destination/index.d.ts +1 -1
  2. package/dist/directory/providers/destination/index.d.ts.map +1 -1
  3. package/dist/directory/providers/destination/index.js +15 -31
  4. package/dist/directory/providers/destination/index.js.map +1 -1
  5. package/dist/directory/providers/destination/index.mjs +15 -31
  6. package/dist/directory/providers/destination/index.mjs.map +1 -1
  7. package/dist/directory/providers/destination/utils.d.ts +1 -2
  8. package/dist/directory/providers/destination/utils.d.ts.map +1 -1
  9. package/dist/directory/providers/destination/utils.js +7 -36
  10. package/dist/directory/providers/destination/utils.js.map +1 -1
  11. package/dist/directory/providers/destination/utils.mjs +7 -36
  12. package/dist/directory/providers/destination/utils.mjs.map +1 -1
  13. package/dist/directory/providers/source/index.d.ts.map +1 -1
  14. package/dist/directory/providers/source/index.js +7 -7
  15. package/dist/directory/providers/source/index.js.map +1 -1
  16. package/dist/directory/providers/source/index.mjs +7 -7
  17. package/dist/directory/providers/source/index.mjs.map +1 -1
  18. package/dist/engine/index.d.ts.map +1 -1
  19. package/dist/engine/index.js +45 -32
  20. package/dist/engine/index.js.map +1 -1
  21. package/dist/engine/index.mjs +43 -30
  22. package/dist/engine/index.mjs.map +1 -1
  23. package/dist/file/providers/destination/index.d.ts +1 -1
  24. package/dist/file/providers/destination/index.d.ts.map +1 -1
  25. package/dist/file/providers/destination/index.js +46 -48
  26. package/dist/file/providers/destination/index.js.map +1 -1
  27. package/dist/file/providers/destination/index.mjs +46 -48
  28. package/dist/file/providers/destination/index.mjs.map +1 -1
  29. package/dist/file/providers/destination/utils.d.ts +1 -2
  30. package/dist/file/providers/destination/utils.d.ts.map +1 -1
  31. package/dist/file/providers/destination/utils.js +28 -37
  32. package/dist/file/providers/destination/utils.js.map +1 -1
  33. package/dist/file/providers/destination/utils.mjs +28 -37
  34. package/dist/file/providers/destination/utils.mjs.map +1 -1
  35. package/dist/file/providers/source/index.d.ts +1 -1
  36. package/dist/file/providers/source/index.d.ts.map +1 -1
  37. package/dist/file/providers/source/index.js +85 -61
  38. package/dist/file/providers/source/index.js.map +1 -1
  39. package/dist/file/providers/source/index.mjs +84 -60
  40. package/dist/file/providers/source/index.mjs.map +1 -1
  41. package/dist/strapi/providers/local-source/configuration.d.ts.map +1 -1
  42. package/dist/strapi/providers/local-source/configuration.js +4 -3
  43. package/dist/strapi/providers/local-source/configuration.js.map +1 -1
  44. package/dist/strapi/providers/local-source/configuration.mjs +2 -1
  45. package/dist/strapi/providers/local-source/configuration.mjs.map +1 -1
  46. package/dist/strapi/providers/local-source/index.d.ts.map +1 -1
  47. package/dist/strapi/providers/local-source/index.js +12 -2
  48. package/dist/strapi/providers/local-source/index.js.map +1 -1
  49. package/dist/strapi/providers/local-source/index.mjs +11 -1
  50. package/dist/strapi/providers/local-source/index.mjs.map +1 -1
  51. package/dist/strapi/remote/handlers/push.js +0 -1
  52. package/dist/strapi/remote/handlers/push.js.map +1 -1
  53. package/dist/strapi/remote/handlers/push.mjs +0 -1
  54. package/dist/strapi/remote/handlers/push.mjs.map +1 -1
  55. package/dist/utils/buffered-entry-writer.d.ts +21 -0
  56. package/dist/utils/buffered-entry-writer.d.ts.map +1 -0
  57. package/dist/utils/buffered-entry-writer.js +95 -0
  58. package/dist/utils/buffered-entry-writer.js.map +1 -0
  59. package/dist/utils/buffered-entry-writer.mjs +93 -0
  60. package/dist/utils/buffered-entry-writer.mjs.map +1 -0
  61. package/dist/utils/defer-writable.d.ts +11 -0
  62. package/dist/utils/defer-writable.d.ts.map +1 -0
  63. package/dist/utils/defer-writable.js +106 -0
  64. package/dist/utils/defer-writable.js.map +1 -0
  65. package/dist/utils/defer-writable.mjs +104 -0
  66. package/dist/utils/defer-writable.mjs.map +1 -0
  67. package/dist/utils/esm-deps.d.ts +16 -0
  68. package/dist/utils/esm-deps.d.ts.map +1 -0
  69. package/dist/utils/esm-deps.js +33 -0
  70. package/dist/utils/esm-deps.js.map +1 -0
  71. package/dist/utils/esm-deps.mjs +31 -0
  72. package/dist/utils/esm-deps.mjs.map +1 -0
  73. package/dist/utils/jsonl-write-stream.d.ts +13 -0
  74. package/dist/utils/jsonl-write-stream.d.ts.map +1 -0
  75. package/dist/utils/jsonl-write-stream.js +75 -0
  76. package/dist/utils/jsonl-write-stream.js.map +1 -0
  77. package/dist/utils/jsonl-write-stream.mjs +73 -0
  78. package/dist/utils/jsonl-write-stream.mjs.map +1 -0
  79. package/dist/utils/unknown-error.d.ts +7 -0
  80. package/dist/utils/unknown-error.d.ts.map +1 -0
  81. package/dist/utils/unknown-error.js +30 -0
  82. package/dist/utils/unknown-error.js.map +1 -0
  83. package/dist/utils/unknown-error.mjs +27 -0
  84. package/dist/utils/unknown-error.mjs.map +1 -0
  85. package/package.json +7 -10
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../src/file/providers/destination/index.ts"],"sourcesContent":["import path from 'path';\nimport zlib from 'zlib';\nimport { Readable, Writable } from 'stream';\nimport { rm, createWriteStream } from 'fs-extra';\nimport tar from 'tar-stream';\nimport { stringer } from 'stream-json/jsonl/Stringer';\nimport { chain } from 'stream-chain';\n\nimport { createEncryptionCipher } from '../../../utils/encryption';\nimport type {\n IAsset,\n IDestinationProvider,\n IDestinationProviderTransferResults,\n IMetadata,\n ProviderType,\n Stream,\n} from '../../../types';\nimport type { IDiagnosticReporter } from '../../../utils/diagnostic';\nimport { createFilePathFactory, createTarEntryStream } from './utils';\nimport { ProviderTransferError } from '../../../errors/providers';\n\nexport interface ILocalFileDestinationProviderOptions {\n encryption: {\n enabled: boolean; // if the file should be encrypted\n key?: string; // the key to use when encryption.enabled is true\n };\n\n compression: {\n enabled: boolean; // if the file should be compressed with gzip\n };\n\n file: {\n path: string; // the filename to create\n maxSize?: number; // the max size of a single backup file\n maxSizeJsonl?: number; // the max lines of each jsonl file before creating the next file\n };\n}\n\nexport interface ILocalFileDestinationProviderTransferResults extends IDestinationProviderTransferResults {\n file?: {\n path?: string;\n };\n}\n\nexport const createLocalFileDestinationProvider = (\n options: ILocalFileDestinationProviderOptions\n) => {\n return new LocalFileDestinationProvider(options);\n};\n\nclass LocalFileDestinationProvider implements IDestinationProvider {\n name = 'destination::local-file';\n\n type: ProviderType = 'destination';\n\n options: ILocalFileDestinationProviderOptions;\n\n results: ILocalFileDestinationProviderTransferResults = {};\n\n #providersMetadata: { source?: IMetadata; destination?: IMetadata } = {};\n\n #archive: { stream?: tar.Pack; pipeline?: Stream } = {};\n\n #diagnostics?: IDiagnosticReporter;\n\n #closed = false;\n\n constructor(options: ILocalFileDestinationProviderOptions) {\n this.options = options;\n }\n\n #reportInfo(message: string) {\n this.#diagnostics?.report({\n details: {\n createdAt: new Date(),\n message,\n origin: 'file-destination-provider',\n },\n kind: 'info',\n });\n }\n\n get #archivePath() {\n const { encryption, compression, file } = this.options;\n\n let filePath = `${file.path}.tar`;\n\n if (compression.enabled) {\n filePath += '.gz';\n }\n\n if (encryption.enabled) {\n filePath += '.enc';\n }\n\n return filePath;\n }\n\n setMetadata(target: ProviderType, metadata: IMetadata): IDestinationProvider {\n this.#providersMetadata[target] = metadata;\n\n return this;\n }\n\n createGzip(): zlib.Gzip {\n this.#reportInfo('creating gzip');\n return zlib.createGzip();\n }\n\n bootstrap(diagnostics: IDiagnosticReporter): void | Promise<void> {\n this.#diagnostics = diagnostics;\n this.#closed = false;\n const { compression, encryption } = this.options;\n\n if (encryption.enabled && !encryption.key) {\n throw new Error(\"Can't encrypt without a key\");\n }\n\n this.#archive.stream = tar.pack();\n\n const outStream = createWriteStream(this.#archivePath);\n\n outStream.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'ENOSPC') {\n throw new ProviderTransferError(\n \"Your server doesn't have space to proceed with the import.\"\n );\n }\n throw err;\n });\n\n const archiveTransforms: Stream[] = [];\n\n if (compression.enabled) {\n archiveTransforms.push(this.createGzip());\n }\n\n if (encryption.enabled && encryption.key) {\n archiveTransforms.push(createEncryptionCipher(encryption.key));\n }\n\n this.#archive.pipeline = chain([this.#archive.stream, ...archiveTransforms, outStream]);\n\n this.results.file = { path: this.#archivePath };\n }\n\n async close() {\n if (this.#closed) {\n return;\n }\n\n this.#closed = true;\n const { stream, pipeline } = this.#archive;\n\n if (!stream) {\n return;\n }\n\n await this.#writeMetadata();\n stream.finalize();\n\n if (pipeline && !pipeline.closed) {\n await new Promise<void>((resolve, reject) => {\n pipeline.on('close', resolve).on('error', reject);\n });\n }\n }\n\n async rollback(): Promise<void> {\n this.#reportInfo('rolling back');\n await this.close();\n await rm(this.#archivePath, { force: true });\n }\n\n getMetadata() {\n return null;\n }\n\n async #writeMetadata(): Promise<void> {\n this.#reportInfo('writing metadata');\n const metadata = this.#providersMetadata.source;\n\n if (metadata) {\n await new Promise((resolve) => {\n const outStream = this.#getMetadataStream();\n const data = JSON.stringify(metadata, null, 2);\n\n Readable.from(data).pipe(outStream).on('close', resolve);\n });\n }\n }\n\n #getMetadataStream() {\n const { stream } = this.#archive;\n\n if (!stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n return createTarEntryStream(stream, () => 'metadata.json');\n }\n\n createSchemasWriteStream() {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating schemas write stream');\n const filePathFactory = createFilePathFactory('schemas');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createEntitiesWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating entities write stream');\n const filePathFactory = createFilePathFactory('entities');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createLinksWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating links write stream');\n const filePathFactory = createFilePathFactory('links');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createConfigurationWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating configuration write stream');\n const filePathFactory = createFilePathFactory('configuration');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createAssetsWriteStream(): Writable {\n const { stream: archiveStream } = this.#archive;\n\n if (!archiveStream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo('creating assets write stream');\n return new Writable({\n objectMode: true,\n write(data: IAsset, _encoding, callback) {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n const entryPath = path.posix.join('assets', 'uploads', data.filename);\n\n const entryMetadataPath = path.posix.join('assets', 'metadata', `${data.filename}.json`);\n const stringifiedMetadata = JSON.stringify(data.metadata);\n archiveStream.entry(\n {\n name: entryMetadataPath,\n size: stringifiedMetadata.length,\n },\n stringifiedMetadata\n );\n\n const entry = archiveStream.entry({\n name: entryPath,\n size: data.stats.size,\n });\n\n if (!entry) {\n callback(new Error(`Failed to created an asset tar entry for ${entryPath}`));\n return;\n }\n\n data.stream.pipe(entry);\n\n entry\n .on('finish', () => {\n callback(null);\n })\n .on('error', (error) => {\n callback(error);\n });\n },\n });\n }\n}\n"],"names":["createLocalFileDestinationProvider","options","LocalFileDestinationProvider","setMetadata","target","metadata","createGzip","zlib","bootstrap","diagnostics","compression","encryption","enabled","key","Error","stream","tar","pack","outStream","createWriteStream","on","err","code","ProviderTransferError","archiveTransforms","push","createEncryptionCipher","pipeline","chain","results","file","path","close","finalize","closed","Promise","resolve","reject","rollback","rm","force","getMetadata","createSchemasWriteStream","filePathFactory","createFilePathFactory","entryStream","createTarEntryStream","maxSizeJsonl","stringer","createEntitiesWriteStream","createLinksWriteStream","createConfigurationWriteStream","createAssetsWriteStream","archiveStream","Writable","objectMode","write","data","_encoding","callback","entryPath","posix","join","filename","entryMetadataPath","stringifiedMetadata","JSON","stringify","entry","name","size","length","stats","pipe","error","type","message","report","details","createdAt","Date","origin","kind","filePath","source","Readable","from"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CO,MAAMA,qCAAqC,CAChDC,OAAAA,GAAAA;AAEA,IAAA,OAAO,IAAIC,4BAAAA,CAA6BD,OAAAA,CAAAA;AAC1C;AAWE,IAAA,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA,EAEA,QAAA,iBAAA,8BAAA,CAAA,UAAA,CAAA,EAEA,YAAA,iBAAA,8BAAA,CAAA,cAAA,CAAA,EAEA,OAAA,iBAAA,8BAAA,CAAA,SAAA,CAAA,EAMA,WAAA,iBAAA,8BAAA,CAAA,aAAA,CAAA,EAWI,6EAgGE,cAAA,iBAAA,8BAAA,CAAA,gBAAA,CAAA,EAcN,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA;AA9IF,MAAMC,4BAAAA,CAAAA;IAgDJC,WAAAA,CAAYC,MAAoB,EAAEC,QAAmB,EAAwB;AAC3E,QAAA,+BAAA,CAAA,IAAI,EAAC,kBAAA,CAAA,CAAA,kBAAA,CAAkB,CAACD,OAAO,GAAGC,QAAAA;AAElC,QAAA,OAAO,IAAI;AACb,IAAA;IAEAC,UAAAA,GAAwB;QACtB,+BAAA,CAAA,IAAI,EAAC,WAAA,CAAA,CAAA,WAAA,CAAA,CAAY,eAAA,CAAA;AACjB,QAAA,OAAOC,qBAAKD,UAAU,EAAA;AACxB,IAAA;AAEAE,IAAAA,SAAAA,CAAUC,WAAgC,EAAwB;QAChE,+BAAA,CAAA,IAAI,EAAC,YAAA,CAAA,CAAA,YAAA,CAAA,GAAeA,WAAAA;QACpB,+BAAA,CAAA,IAAI,EAAC,OAAA,CAAA,CAAA,OAAA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AKC,SAAS,CAAC9D,UAAU,IAAA,EAAM,CAAA,CAAA;YAE5CgF,eAAAA,CAASC,IAAI,CAAC7B,IAAAA,CAAAA,CAAMgB,IAAI,CAACvD,SAAAA,CAAAA,CAAWE,EAAE,CAAC,OAAA,EAASgB,OAAAA,CAAAA;AAClD,QAAA,CAAA,CAAA;AACF,IAAA;AACF;AAEA,SAAA,iBAAA,GAAA;AACE,IAAA,MAAM,EAAErB,MAAM,EAAE,GAAG,+BAAA,CAAA,IAAI,EAAC,QAAA,CAAA,CAAA,QAAA,CAAA;AAExB,IAAA,IAAI,CAACA,MAAAA,EAAQ;AACX,QAAA,MAAM,IAAID,KAAAA,CAAM,+BAAA,CAAA;AAClB,IAAA;IAEA,OAAOgC,0BAAAA,CAAqB/B,QAAQ,IAAM,eAAA,CAAA;AAC5C;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../src/file/providers/destination/index.ts"],"sourcesContent":["import path from 'path';\nimport zlib from 'zlib';\nimport { Readable, Writable } from 'stream';\nimport { rm, createWriteStream } from 'fs-extra';\nimport tar from 'tar-stream';\n\nimport { createEncryptionCipher } from '../../../utils/encryption';\nimport { errorFromUnknown } from '../../../utils/unknown-error';\nimport { loadStreamDeps } from '../../../utils/esm-deps';\nimport { createJsonlWriteStream } from '../../../utils/jsonl-write-stream';\nimport type {\n IAsset,\n IDestinationProvider,\n IDestinationProviderTransferResults,\n IMetadata,\n ProviderType,\n Stream,\n} from '../../../types';\nimport type { IDiagnosticReporter } from '../../../utils/diagnostic';\nimport { createFilePathFactory, createTarEntryStream } from './utils';\nimport { ProviderTransferError } from '../../../errors/providers';\n\nexport interface ILocalFileDestinationProviderOptions {\n encryption: {\n enabled: boolean; // if the file should be encrypted\n key?: string; // the key to use when encryption.enabled is true\n };\n\n compression: {\n enabled: boolean; // if the file should be compressed with gzip\n };\n\n file: {\n path: string; // the filename to create\n maxSize?: number; // the max size of a single backup file\n maxSizeJsonl?: number; // the max size in bytes of each jsonl file before creating the next file; a larger single line fails the stage\n };\n}\n\nexport interface ILocalFileDestinationProviderTransferResults extends IDestinationProviderTransferResults {\n file?: {\n path?: string;\n };\n}\n\nexport const createLocalFileDestinationProvider = (\n options: ILocalFileDestinationProviderOptions\n) => {\n return new LocalFileDestinationProvider(options);\n};\n\nclass LocalFileDestinationProvider implements IDestinationProvider {\n name = 'destination::local-file';\n\n type: ProviderType = 'destination';\n\n options: ILocalFileDestinationProviderOptions;\n\n results: ILocalFileDestinationProviderTransferResults = {};\n\n #providersMetadata: { source?: IMetadata; destination?: IMetadata } = {};\n\n #archive: { stream?: tar.Pack; pipeline?: Stream } = {};\n\n #archiveReady: Promise<void> = Promise.resolve();\n\n #archiveError?: Error;\n\n #diagnostics?: IDiagnosticReporter;\n\n #closed = false;\n\n constructor(options: ILocalFileDestinationProviderOptions) {\n this.options = options;\n }\n\n #reportInfo(message: string) {\n this.#diagnostics?.report({\n details: {\n createdAt: new Date(),\n message,\n origin: 'file-destination-provider',\n },\n kind: 'info',\n });\n }\n\n get #archivePath() {\n const { encryption, compression, file } = this.options;\n\n let filePath = `${file.path}.tar`;\n\n if (compression.enabled) {\n filePath += '.gz';\n }\n\n if (encryption.enabled) {\n filePath += '.enc';\n }\n\n return filePath;\n }\n\n setMetadata(target: ProviderType, metadata: IMetadata): IDestinationProvider {\n this.#providersMetadata[target] = metadata;\n\n return this;\n }\n\n createGzip(): zlib.Gzip {\n this.#reportInfo('creating gzip');\n return zlib.createGzip();\n }\n\n bootstrap(diagnostics: IDiagnosticReporter): void | Promise<void> {\n this.#diagnostics = diagnostics;\n this.#closed = false;\n const { compression, encryption } = this.options;\n\n if (encryption.enabled && !encryption.key) {\n throw new Error(\"Can't encrypt without a key\");\n }\n\n this.#archive.stream = tar.pack();\n\n const outStream = createWriteStream(this.#archivePath);\n\n outStream.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'ENOSPC') {\n throw new ProviderTransferError(\n \"Your server doesn't have space to proceed with the import.\"\n );\n }\n throw err;\n });\n\n const archiveTransforms: Stream[] = [];\n\n if (compression.enabled) {\n archiveTransforms.push(this.createGzip());\n }\n\n if (encryption.enabled && encryption.key) {\n archiveTransforms.push(createEncryptionCipher(encryption.key));\n }\n\n const archiveStream = this.#archive.stream;\n this.#archiveError = undefined;\n this.#archiveReady = loadStreamDeps()\n .then(({ chain }) => {\n this.#archive.pipeline = chain([archiveStream, ...archiveTransforms, outStream]);\n })\n .catch((error: unknown) => {\n this.#archiveError = errorFromUnknown(error);\n archiveStream.destroy();\n outStream.destroy();\n });\n\n this.results.file = { path: this.#archivePath };\n }\n\n async close() {\n if (this.#closed) {\n return;\n }\n\n this.#closed = true;\n await this.#archiveReady;\n if (this.#archiveError) {\n throw this.#archiveError;\n }\n\n const { stream, pipeline } = this.#archive;\n\n if (!stream) {\n return;\n }\n\n await this.#writeMetadata();\n stream.finalize();\n\n if (pipeline && !pipeline.closed) {\n await new Promise<void>((resolve, reject) => {\n pipeline.on('close', resolve).on('error', reject);\n });\n }\n }\n\n async rollback(): Promise<void> {\n this.#reportInfo('rolling back');\n await this.close();\n await rm(this.#archivePath, { force: true });\n }\n\n getMetadata() {\n return null;\n }\n\n async #writeMetadata(): Promise<void> {\n this.#reportInfo('writing metadata');\n const metadata = this.#providersMetadata.source;\n\n if (metadata) {\n await new Promise((resolve) => {\n const outStream = this.#getMetadataStream();\n const data = JSON.stringify(metadata, null, 2);\n\n Readable.from(data).pipe(outStream).on('close', resolve);\n });\n }\n }\n\n #getMetadataStream() {\n const { stream } = this.#archive;\n\n if (!stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n return createTarEntryStream(stream, () => 'metadata.json');\n }\n\n createSchemasWriteStream(): Writable {\n return this.#createJsonlWriteStream('schemas');\n }\n\n createEntitiesWriteStream(): Writable {\n return this.#createJsonlWriteStream('entities');\n }\n\n createLinksWriteStream(): Writable {\n return this.#createJsonlWriteStream('links');\n }\n\n createConfigurationWriteStream(): Writable {\n return this.#createJsonlWriteStream('configuration');\n }\n\n #createJsonlWriteStream(stage: string): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo(`creating ${stage} write stream`);\n const filePathFactory = createFilePathFactory(stage);\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return createJsonlWriteStream(entryStream);\n }\n\n createAssetsWriteStream(): Writable {\n const { stream: archiveStream } = this.#archive;\n\n if (!archiveStream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo('creating assets write stream');\n return new Writable({\n objectMode: true,\n write(data: IAsset, _encoding, callback) {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n const entryPath = path.posix.join('assets', 'uploads', data.filename);\n\n const entryMetadataPath = path.posix.join('assets', 'metadata', `${data.filename}.json`);\n const stringifiedMetadata = JSON.stringify(data.metadata);\n archiveStream.entry(\n {\n name: entryMetadataPath,\n size: stringifiedMetadata.length,\n },\n stringifiedMetadata\n );\n\n const entry = archiveStream.entry({\n name: entryPath,\n size: data.stats.size,\n });\n\n if (!entry) {\n callback(new Error(`Failed to created an asset tar entry for ${entryPath}`));\n return;\n }\n\n data.stream.pipe(entry);\n\n entry\n .on('finish', () => {\n callback(null);\n })\n .on('error', (error) => {\n callback(error);\n });\n },\n });\n }\n}\n"],"names":["createLocalFileDestinationProvider","options","LocalFileDestinationProvider","setMetadata","target","metadata","createGzip","zlib","bootstrap","diagnostics","compression","encryption","enabled","key","Error","stream","tar","pack","outStream","createWriteStream","on","err","code","ProviderTransferError","archiveTransforms","push","createEncryptionCipher","archiveStream","undefined","loadStreamDeps","then","chain","pipeline","catch","error","errorFromUnknown","destroy","results","file","path","close","finalize","closed","Promise","resolve","reject","rollback","rm","force","getMetadata","createSchemasWriteStream","createEntitiesWriteStream","createLinksWriteStream","createConfigurationWriteStream","createAssetsWriteStream","Writable","objectMode","write","data","_encoding","callback","entryPath","posix","join","filename","entryMetadataPath","stringifiedMetadata","JSON","stringify","entry","name","size","length","stats","pipe","type","message","report","details","createdAt","Date","origin","kind","filePath","source","Readable","from","createTarEntryStream","stage","filePathFactory","createFilePathFactory","entryStream","maxSizeJsonl","createJsonlWriteStream"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CO,MAAMA,qCAAqC,CAChDC,OAAAA,GAAAA;AAEA,IAAA,OAAO,IAAIC,4BAAAA,CAA6BD,OAAAA,CAAAA;AAC1C;IAWE,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA,EAEA,QAAA,iBAAA,8BAAA,CAAA,UAAA,CAAA,EAEA,+EAEA,aAAA,iBAAA,8BAAA,CAAA,eAAA,CAAA,EAEA,YAAA,iBAAA,8BAAA,CAAA,cAAA,CAAA,EAEA,mEAMA,WAAA,iBAAA,8BAAA,CAAA,aAAA,CAAA,EAWI,YAAA,iBAAA,8BAAA,CAAA,cAAA,CAAA,EA+GE,iFAcN,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA,EA0BA,uBAAA,iBAAA,8BAAA,CAAA,yBAAA,CAAA;AA3LF,MAAMC,4BAAAA,CAAAA;IAoDJC,WAAAA,CAAYC,MAAoB,EAAEC,QAAmB,EAAwB;AAC3E,QAAA,+BAAA,CAAA,IAAI,EAAC,kBAAA,CAAA,CAAA,kBAAA,CAAkB,CAACD,OAAO,GAAGC,QAAAA;AAElC,QAAA,OAAO,IAAI;AACb,IAAA;IAEAC,UAAAA,GAAwB;QACtB,+BAAA,CAAA,IAAI,EAAC,WAAA,CAAA,CAAA,WAAA,CAAA,CAAY,eAAA,CAAA;AACjB,QAAA,OAAOC,qBAAKD,UAAU,EAAA;AACxB,IAAA;AAEAE,IAAAA,SAAAA,CAAUC,WAAgC,EAAwB;QAChE,+BAAA,CAAA,IAAI,EAAC,YAAA,CAAA,CAAA,YAAA,CAAA,GAAeA,WAAAA;QACpB,+BAAA,CAAA,IAAI,EAAC,OAAA,CAAA,CAAA,OAAA,CAAA,GAAU,KAAA;QACf,MAAM,EAAE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J,0BAAAA,CAClB,+BAAA,CAAA,IAAI,EAAC,UAAA,QAAA,CAAA,CAASxE,MAAM,EACpB0E,eAAAA,EACA,IAAI,CAACxF,OAAO,CAACqC,IAAI,CAACsD,YAAY,CAAA;AAGhC,IAAA,OAAOC,uCAAAA,CAAuBF,WAAAA,CAAAA;AAChC;;;;"}
@@ -3,10 +3,11 @@ import zip from 'zlib';
3
3
  import { Writable, Readable } from 'stream';
4
4
  import { createWriteStream, rm } from 'fs-extra';
5
5
  import tar from 'tar-stream';
6
- import { stringer } from 'stream-json/jsonl/Stringer';
7
- import { chain } from 'stream-chain';
8
6
  import { createEncryptionCipher } from '../../../utils/encryption/encrypt.mjs';
9
7
  import 'crypto';
8
+ import { errorFromUnknown } from '../../../utils/unknown-error.mjs';
9
+ import { loadStreamDeps } from '../../../utils/esm-deps.mjs';
10
+ import { createJsonlWriteStream } from '../../../utils/jsonl-write-stream.mjs';
10
11
  import { createTarEntryStream, createFilePathFactory } from './utils.mjs';
11
12
  import { ProviderTransferError } from '../../../errors/providers.mjs';
12
13
 
@@ -23,7 +24,7 @@ function _class_private_field_loose_key(name) {
23
24
  const createLocalFileDestinationProvider = (options)=>{
24
25
  return new LocalFileDestinationProvider(options);
25
26
  };
26
- var _providersMetadata = /*#__PURE__*/ _class_private_field_loose_key("_providersMetadata"), _archive = /*#__PURE__*/ _class_private_field_loose_key("_archive"), _diagnostics = /*#__PURE__*/ _class_private_field_loose_key("_diagnostics"), _closed = /*#__PURE__*/ _class_private_field_loose_key("_closed"), _reportInfo = /*#__PURE__*/ _class_private_field_loose_key("_reportInfo"), _archivePath = /*#__PURE__*/ _class_private_field_loose_key("_archivePath"), _writeMetadata = /*#__PURE__*/ _class_private_field_loose_key("_writeMetadata"), _getMetadataStream = /*#__PURE__*/ _class_private_field_loose_key("_getMetadataStream");
27
+ var _providersMetadata = /*#__PURE__*/ _class_private_field_loose_key("_providersMetadata"), _archive = /*#__PURE__*/ _class_private_field_loose_key("_archive"), _archiveReady = /*#__PURE__*/ _class_private_field_loose_key("_archiveReady"), _archiveError = /*#__PURE__*/ _class_private_field_loose_key("_archiveError"), _diagnostics = /*#__PURE__*/ _class_private_field_loose_key("_diagnostics"), _closed = /*#__PURE__*/ _class_private_field_loose_key("_closed"), _reportInfo = /*#__PURE__*/ _class_private_field_loose_key("_reportInfo"), _archivePath = /*#__PURE__*/ _class_private_field_loose_key("_archivePath"), _writeMetadata = /*#__PURE__*/ _class_private_field_loose_key("_writeMetadata"), _getMetadataStream = /*#__PURE__*/ _class_private_field_loose_key("_getMetadataStream"), _createJsonlWriteStream = /*#__PURE__*/ _class_private_field_loose_key("_createJsonlWriteStream");
27
28
  class LocalFileDestinationProvider {
28
29
  setMetadata(target, metadata) {
29
30
  _class_private_field_loose_base(this, _providersMetadata)[_providersMetadata][target] = metadata;
@@ -55,11 +56,19 @@ class LocalFileDestinationProvider {
55
56
  if (encryption.enabled && encryption.key) {
56
57
  archiveTransforms.push(createEncryptionCipher(encryption.key));
57
58
  }
58
- _class_private_field_loose_base(this, _archive)[_archive].pipeline = chain([
59
- _class_private_field_loose_base(this, _archive)[_archive].stream,
60
- ...archiveTransforms,
61
- outStream
62
- ]);
59
+ const archiveStream = _class_private_field_loose_base(this, _archive)[_archive].stream;
60
+ _class_private_field_loose_base(this, _archiveError)[_archiveError] = undefined;
61
+ _class_private_field_loose_base(this, _archiveReady)[_archiveReady] = loadStreamDeps().then(({ chain })=>{
62
+ _class_private_field_loose_base(this, _archive)[_archive].pipeline = chain([
63
+ archiveStream,
64
+ ...archiveTransforms,
65
+ outStream
66
+ ]);
67
+ }).catch((error)=>{
68
+ _class_private_field_loose_base(this, _archiveError)[_archiveError] = errorFromUnknown(error);
69
+ archiveStream.destroy();
70
+ outStream.destroy();
71
+ });
63
72
  this.results.file = {
64
73
  path: _class_private_field_loose_base(this, _archivePath)[_archivePath]
65
74
  };
@@ -69,6 +78,10 @@ class LocalFileDestinationProvider {
69
78
  return;
70
79
  }
71
80
  _class_private_field_loose_base(this, _closed)[_closed] = true;
81
+ await _class_private_field_loose_base(this, _archiveReady)[_archiveReady];
82
+ if (_class_private_field_loose_base(this, _archiveError)[_archiveError]) {
83
+ throw _class_private_field_loose_base(this, _archiveError)[_archiveError];
84
+ }
72
85
  const { stream, pipeline } = _class_private_field_loose_base(this, _archive)[_archive];
73
86
  if (!stream) {
74
87
  return;
@@ -92,52 +105,16 @@ class LocalFileDestinationProvider {
92
105
  return null;
93
106
  }
94
107
  createSchemasWriteStream() {
95
- if (!_class_private_field_loose_base(this, _archive)[_archive].stream) {
96
- throw new Error('Archive stream is unavailable');
97
- }
98
- _class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating schemas write stream');
99
- const filePathFactory = createFilePathFactory('schemas');
100
- const entryStream = createTarEntryStream(_class_private_field_loose_base(this, _archive)[_archive].stream, filePathFactory, this.options.file.maxSizeJsonl);
101
- return chain([
102
- stringer(),
103
- entryStream
104
- ]);
108
+ return _class_private_field_loose_base(this, _createJsonlWriteStream)[_createJsonlWriteStream]('schemas');
105
109
  }
106
110
  createEntitiesWriteStream() {
107
- if (!_class_private_field_loose_base(this, _archive)[_archive].stream) {
108
- throw new Error('Archive stream is unavailable');
109
- }
110
- _class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating entities write stream');
111
- const filePathFactory = createFilePathFactory('entities');
112
- const entryStream = createTarEntryStream(_class_private_field_loose_base(this, _archive)[_archive].stream, filePathFactory, this.options.file.maxSizeJsonl);
113
- return chain([
114
- stringer(),
115
- entryStream
116
- ]);
111
+ return _class_private_field_loose_base(this, _createJsonlWriteStream)[_createJsonlWriteStream]('entities');
117
112
  }
118
113
  createLinksWriteStream() {
119
- if (!_class_private_field_loose_base(this, _archive)[_archive].stream) {
120
- throw new Error('Archive stream is unavailable');
121
- }
122
- _class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating links write stream');
123
- const filePathFactory = createFilePathFactory('links');
124
- const entryStream = createTarEntryStream(_class_private_field_loose_base(this, _archive)[_archive].stream, filePathFactory, this.options.file.maxSizeJsonl);
125
- return chain([
126
- stringer(),
127
- entryStream
128
- ]);
114
+ return _class_private_field_loose_base(this, _createJsonlWriteStream)[_createJsonlWriteStream]('links');
129
115
  }
130
116
  createConfigurationWriteStream() {
131
- if (!_class_private_field_loose_base(this, _archive)[_archive].stream) {
132
- throw new Error('Archive stream is unavailable');
133
- }
134
- _class_private_field_loose_base(this, _reportInfo)[_reportInfo]('creating configuration write stream');
135
- const filePathFactory = createFilePathFactory('configuration');
136
- const entryStream = createTarEntryStream(_class_private_field_loose_base(this, _archive)[_archive].stream, filePathFactory, this.options.file.maxSizeJsonl);
137
- return chain([
138
- stringer(),
139
- entryStream
140
- ]);
117
+ return _class_private_field_loose_base(this, _createJsonlWriteStream)[_createJsonlWriteStream]('configuration');
141
118
  }
142
119
  createAssetsWriteStream() {
143
120
  const { stream: archiveStream } = _class_private_field_loose_base(this, _archive)[_archive];
@@ -187,6 +164,9 @@ class LocalFileDestinationProvider {
187
164
  Object.defineProperty(this, _getMetadataStream, {
188
165
  value: getMetadataStream
189
166
  });
167
+ Object.defineProperty(this, _createJsonlWriteStream, {
168
+ value: createJsonlWriteStream1
169
+ });
190
170
  Object.defineProperty(this, _providersMetadata, {
191
171
  writable: true,
192
172
  value: void 0
@@ -195,6 +175,14 @@ class LocalFileDestinationProvider {
195
175
  writable: true,
196
176
  value: void 0
197
177
  });
178
+ Object.defineProperty(this, _archiveReady, {
179
+ writable: true,
180
+ value: void 0
181
+ });
182
+ Object.defineProperty(this, _archiveError, {
183
+ writable: true,
184
+ value: void 0
185
+ });
198
186
  Object.defineProperty(this, _diagnostics, {
199
187
  writable: true,
200
188
  value: void 0
@@ -208,6 +196,7 @@ class LocalFileDestinationProvider {
208
196
  this.results = {};
209
197
  _class_private_field_loose_base(this, _providersMetadata)[_providersMetadata] = {};
210
198
  _class_private_field_loose_base(this, _archive)[_archive] = {};
199
+ _class_private_field_loose_base(this, _archiveReady)[_archiveReady] = Promise.resolve();
211
200
  _class_private_field_loose_base(this, _closed)[_closed] = false;
212
201
  this.options = options;
213
202
  }
@@ -251,6 +240,15 @@ function getMetadataStream() {
251
240
  }
252
241
  return createTarEntryStream(stream, ()=>'metadata.json');
253
242
  }
243
+ function createJsonlWriteStream1(stage) {
244
+ if (!_class_private_field_loose_base(this, _archive)[_archive].stream) {
245
+ throw new Error('Archive stream is unavailable');
246
+ }
247
+ _class_private_field_loose_base(this, _reportInfo)[_reportInfo](`creating ${stage} write stream`);
248
+ const filePathFactory = createFilePathFactory(stage);
249
+ const entryStream = createTarEntryStream(_class_private_field_loose_base(this, _archive)[_archive].stream, filePathFactory, this.options.file.maxSizeJsonl);
250
+ return createJsonlWriteStream(entryStream);
251
+ }
254
252
 
255
253
  export { createLocalFileDestinationProvider };
256
254
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../../src/file/providers/destination/index.ts"],"sourcesContent":["import path from 'path';\nimport zlib from 'zlib';\nimport { Readable, Writable } from 'stream';\nimport { rm, createWriteStream } from 'fs-extra';\nimport tar from 'tar-stream';\nimport { stringer } from 'stream-json/jsonl/Stringer';\nimport { chain } from 'stream-chain';\n\nimport { createEncryptionCipher } from '../../../utils/encryption';\nimport type {\n IAsset,\n IDestinationProvider,\n IDestinationProviderTransferResults,\n IMetadata,\n ProviderType,\n Stream,\n} from '../../../types';\nimport type { IDiagnosticReporter } from '../../../utils/diagnostic';\nimport { createFilePathFactory, createTarEntryStream } from './utils';\nimport { ProviderTransferError } from '../../../errors/providers';\n\nexport interface ILocalFileDestinationProviderOptions {\n encryption: {\n enabled: boolean; // if the file should be encrypted\n key?: string; // the key to use when encryption.enabled is true\n };\n\n compression: {\n enabled: boolean; // if the file should be compressed with gzip\n };\n\n file: {\n path: string; // the filename to create\n maxSize?: number; // the max size of a single backup file\n maxSizeJsonl?: number; // the max lines of each jsonl file before creating the next file\n };\n}\n\nexport interface ILocalFileDestinationProviderTransferResults extends IDestinationProviderTransferResults {\n file?: {\n path?: string;\n };\n}\n\nexport const createLocalFileDestinationProvider = (\n options: ILocalFileDestinationProviderOptions\n) => {\n return new LocalFileDestinationProvider(options);\n};\n\nclass LocalFileDestinationProvider implements IDestinationProvider {\n name = 'destination::local-file';\n\n type: ProviderType = 'destination';\n\n options: ILocalFileDestinationProviderOptions;\n\n results: ILocalFileDestinationProviderTransferResults = {};\n\n #providersMetadata: { source?: IMetadata; destination?: IMetadata } = {};\n\n #archive: { stream?: tar.Pack; pipeline?: Stream } = {};\n\n #diagnostics?: IDiagnosticReporter;\n\n #closed = false;\n\n constructor(options: ILocalFileDestinationProviderOptions) {\n this.options = options;\n }\n\n #reportInfo(message: string) {\n this.#diagnostics?.report({\n details: {\n createdAt: new Date(),\n message,\n origin: 'file-destination-provider',\n },\n kind: 'info',\n });\n }\n\n get #archivePath() {\n const { encryption, compression, file } = this.options;\n\n let filePath = `${file.path}.tar`;\n\n if (compression.enabled) {\n filePath += '.gz';\n }\n\n if (encryption.enabled) {\n filePath += '.enc';\n }\n\n return filePath;\n }\n\n setMetadata(target: ProviderType, metadata: IMetadata): IDestinationProvider {\n this.#providersMetadata[target] = metadata;\n\n return this;\n }\n\n createGzip(): zlib.Gzip {\n this.#reportInfo('creating gzip');\n return zlib.createGzip();\n }\n\n bootstrap(diagnostics: IDiagnosticReporter): void | Promise<void> {\n this.#diagnostics = diagnostics;\n this.#closed = false;\n const { compression, encryption } = this.options;\n\n if (encryption.enabled && !encryption.key) {\n throw new Error(\"Can't encrypt without a key\");\n }\n\n this.#archive.stream = tar.pack();\n\n const outStream = createWriteStream(this.#archivePath);\n\n outStream.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'ENOSPC') {\n throw new ProviderTransferError(\n \"Your server doesn't have space to proceed with the import.\"\n );\n }\n throw err;\n });\n\n const archiveTransforms: Stream[] = [];\n\n if (compression.enabled) {\n archiveTransforms.push(this.createGzip());\n }\n\n if (encryption.enabled && encryption.key) {\n archiveTransforms.push(createEncryptionCipher(encryption.key));\n }\n\n this.#archive.pipeline = chain([this.#archive.stream, ...archiveTransforms, outStream]);\n\n this.results.file = { path: this.#archivePath };\n }\n\n async close() {\n if (this.#closed) {\n return;\n }\n\n this.#closed = true;\n const { stream, pipeline } = this.#archive;\n\n if (!stream) {\n return;\n }\n\n await this.#writeMetadata();\n stream.finalize();\n\n if (pipeline && !pipeline.closed) {\n await new Promise<void>((resolve, reject) => {\n pipeline.on('close', resolve).on('error', reject);\n });\n }\n }\n\n async rollback(): Promise<void> {\n this.#reportInfo('rolling back');\n await this.close();\n await rm(this.#archivePath, { force: true });\n }\n\n getMetadata() {\n return null;\n }\n\n async #writeMetadata(): Promise<void> {\n this.#reportInfo('writing metadata');\n const metadata = this.#providersMetadata.source;\n\n if (metadata) {\n await new Promise((resolve) => {\n const outStream = this.#getMetadataStream();\n const data = JSON.stringify(metadata, null, 2);\n\n Readable.from(data).pipe(outStream).on('close', resolve);\n });\n }\n }\n\n #getMetadataStream() {\n const { stream } = this.#archive;\n\n if (!stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n return createTarEntryStream(stream, () => 'metadata.json');\n }\n\n createSchemasWriteStream() {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating schemas write stream');\n const filePathFactory = createFilePathFactory('schemas');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createEntitiesWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating entities write stream');\n const filePathFactory = createFilePathFactory('entities');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createLinksWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating links write stream');\n const filePathFactory = createFilePathFactory('links');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createConfigurationWriteStream(): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n this.#reportInfo('creating configuration write stream');\n const filePathFactory = createFilePathFactory('configuration');\n\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return chain([stringer(), entryStream]);\n }\n\n createAssetsWriteStream(): Writable {\n const { stream: archiveStream } = this.#archive;\n\n if (!archiveStream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo('creating assets write stream');\n return new Writable({\n objectMode: true,\n write(data: IAsset, _encoding, callback) {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n const entryPath = path.posix.join('assets', 'uploads', data.filename);\n\n const entryMetadataPath = path.posix.join('assets', 'metadata', `${data.filename}.json`);\n const stringifiedMetadata = JSON.stringify(data.metadata);\n archiveStream.entry(\n {\n name: entryMetadataPath,\n size: stringifiedMetadata.length,\n },\n stringifiedMetadata\n );\n\n const entry = archiveStream.entry({\n name: entryPath,\n size: data.stats.size,\n });\n\n if (!entry) {\n callback(new Error(`Failed to created an asset tar entry for ${entryPath}`));\n return;\n }\n\n data.stream.pipe(entry);\n\n entry\n .on('finish', () => {\n callback(null);\n })\n .on('error', (error) => {\n callback(error);\n });\n },\n });\n }\n}\n"],"names":["createLocalFileDestinationProvider","options","LocalFileDestinationProvider","setMetadata","target","metadata","createGzip","zlib","bootstrap","diagnostics","compression","encryption","enabled","key","Error","stream","tar","pack","outStream","createWriteStream","on","err","code","ProviderTransferError","archiveTransforms","push","createEncryptionCipher","pipeline","chain","results","file","path","close","finalize","closed","Promise","resolve","reject","rollback","rm","force","getMetadata","createSchemasWriteStream","filePathFactory","createFilePathFactory","entryStream","createTarEntryStream","maxSizeJsonl","stringer","createEntitiesWriteStream","createLinksWriteStream","createConfigurationWriteStream","createAssetsWriteStream","archiveStream","Writable","objectMode","write","data","_encoding","callback","entryPath","posix","join","filename","entryMetadataPath","stringifiedMetadata","JSON","stringify","entry","name","size","length","stats","pipe","error","type","message","report","details","createdAt","Date","origin","kind","filePath","source","Readable","from"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA4CO,MAAMA,qCAAqC,CAChDC,OAAAA,GAAAA;AAEA,IAAA,OAAO,IAAIC,4BAAAA,CAA6BD,OAAAA,CAAAA;AAC1C;AAWE,IAAA,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA,EAEA,QAAA,iBAAA,8BAAA,CAAA,UAAA,CAAA,EAEA,YAAA,iBAAA,8BAAA,CAAA,cAAA,CAAA,EAEA,OAAA,iBAAA,8BAAA,CAAA,SAAA,CAAA,EAMA,WAAA,iBAAA,8BAAA,CAAA,aAAA,CAAA,EAWI,6EAgGE,cAAA,iBAAA,8BAAA,CAAA,gBAAA,CAAA,EAcN,kBAAA,iBAAA,8BAAA,CAAA,oBAAA,CAAA;AA9IF,MAAMC,4BAAAA,CAAAA;IAgDJC,WAAAA,CAAYC,MAAoB,EAAEC,QAAmB,EAAwB;AAC3E,QAAA,+BAAA,CAAA,IAAI,EAAC,kBAAA,CAAA,CAAA,kBAAA,CAAkB,CAACD,OAAO,GAAGC,QAAAA;AAElC,QAAA,OAAO,IAAI;AACb,IAAA;IAEAC,UAAAA,GAAwB;QACtB,+BAAA,CAAA,IAAI,EAAC,WAAA,CAAA,CAAA,WAAA,CAAA,CAAY,eAAA,CAAA;AACjB,QAAA,OAAOC,IAAKD,UAAU,EAAA;AACxB,IAAA;AAEAE,IAAAA,SAAAA,CAAUC,WAAgC,EAAwB;QAChE,+BAAA,CAAA,IAAI,EAAC,YAAA,CAAA,CAAA,YAAA,CAAA,GAAeA,WAAAA;QACpB,+BAAA,CAAA,IAAI,EAAC,OAAA,CAAA,CAAA,OAAA,CAAA,GAAU,KAAA;QACf,MAAM,EAAEC,WAA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gF,QAAAA,CAASC,IAAI,CAAC7B,IAAAA,CAAAA,CAAMgB,IAAI,CAACvD,SAAAA,CAAAA,CAAWE,EAAE,CAAC,OAAA,EAASgB,OAAAA,CAAAA;AAClD,QAAA,CAAA,CAAA;AACF,IAAA;AACF;AAEA,SAAA,iBAAA,GAAA;AACE,IAAA,MAAM,EAAErB,MAAM,EAAE,GAAG,+BAAA,CAAA,IAAI,EAAC,QAAA,CAAA,CAAA,QAAA,CAAA;AAExB,IAAA,IAAI,CAACA,MAAAA,EAAQ;AACX,QAAA,MAAM,IAAID,KAAAA,CAAM,+BAAA,CAAA;AAClB,IAAA;IAEA,OAAOgC,oBAAAA,CAAqB/B,QAAQ,IAAM,eAAA,CAAA;AAC5C;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../../../../src/file/providers/destination/index.ts"],"sourcesContent":["import path from 'path';\nimport zlib from 'zlib';\nimport { Readable, Writable } from 'stream';\nimport { rm, createWriteStream } from 'fs-extra';\nimport tar from 'tar-stream';\n\nimport { createEncryptionCipher } from '../../../utils/encryption';\nimport { errorFromUnknown } from '../../../utils/unknown-error';\nimport { loadStreamDeps } from '../../../utils/esm-deps';\nimport { createJsonlWriteStream } from '../../../utils/jsonl-write-stream';\nimport type {\n IAsset,\n IDestinationProvider,\n IDestinationProviderTransferResults,\n IMetadata,\n ProviderType,\n Stream,\n} from '../../../types';\nimport type { IDiagnosticReporter } from '../../../utils/diagnostic';\nimport { createFilePathFactory, createTarEntryStream } from './utils';\nimport { ProviderTransferError } from '../../../errors/providers';\n\nexport interface ILocalFileDestinationProviderOptions {\n encryption: {\n enabled: boolean; // if the file should be encrypted\n key?: string; // the key to use when encryption.enabled is true\n };\n\n compression: {\n enabled: boolean; // if the file should be compressed with gzip\n };\n\n file: {\n path: string; // the filename to create\n maxSize?: number; // the max size of a single backup file\n maxSizeJsonl?: number; // the max size in bytes of each jsonl file before creating the next file; a larger single line fails the stage\n };\n}\n\nexport interface ILocalFileDestinationProviderTransferResults extends IDestinationProviderTransferResults {\n file?: {\n path?: string;\n };\n}\n\nexport const createLocalFileDestinationProvider = (\n options: ILocalFileDestinationProviderOptions\n) => {\n return new LocalFileDestinationProvider(options);\n};\n\nclass LocalFileDestinationProvider implements IDestinationProvider {\n name = 'destination::local-file';\n\n type: ProviderType = 'destination';\n\n options: ILocalFileDestinationProviderOptions;\n\n results: ILocalFileDestinationProviderTransferResults = {};\n\n #providersMetadata: { source?: IMetadata; destination?: IMetadata } = {};\n\n #archive: { stream?: tar.Pack; pipeline?: Stream } = {};\n\n #archiveReady: Promise<void> = Promise.resolve();\n\n #archiveError?: Error;\n\n #diagnostics?: IDiagnosticReporter;\n\n #closed = false;\n\n constructor(options: ILocalFileDestinationProviderOptions) {\n this.options = options;\n }\n\n #reportInfo(message: string) {\n this.#diagnostics?.report({\n details: {\n createdAt: new Date(),\n message,\n origin: 'file-destination-provider',\n },\n kind: 'info',\n });\n }\n\n get #archivePath() {\n const { encryption, compression, file } = this.options;\n\n let filePath = `${file.path}.tar`;\n\n if (compression.enabled) {\n filePath += '.gz';\n }\n\n if (encryption.enabled) {\n filePath += '.enc';\n }\n\n return filePath;\n }\n\n setMetadata(target: ProviderType, metadata: IMetadata): IDestinationProvider {\n this.#providersMetadata[target] = metadata;\n\n return this;\n }\n\n createGzip(): zlib.Gzip {\n this.#reportInfo('creating gzip');\n return zlib.createGzip();\n }\n\n bootstrap(diagnostics: IDiagnosticReporter): void | Promise<void> {\n this.#diagnostics = diagnostics;\n this.#closed = false;\n const { compression, encryption } = this.options;\n\n if (encryption.enabled && !encryption.key) {\n throw new Error(\"Can't encrypt without a key\");\n }\n\n this.#archive.stream = tar.pack();\n\n const outStream = createWriteStream(this.#archivePath);\n\n outStream.on('error', (err: NodeJS.ErrnoException) => {\n if (err.code === 'ENOSPC') {\n throw new ProviderTransferError(\n \"Your server doesn't have space to proceed with the import.\"\n );\n }\n throw err;\n });\n\n const archiveTransforms: Stream[] = [];\n\n if (compression.enabled) {\n archiveTransforms.push(this.createGzip());\n }\n\n if (encryption.enabled && encryption.key) {\n archiveTransforms.push(createEncryptionCipher(encryption.key));\n }\n\n const archiveStream = this.#archive.stream;\n this.#archiveError = undefined;\n this.#archiveReady = loadStreamDeps()\n .then(({ chain }) => {\n this.#archive.pipeline = chain([archiveStream, ...archiveTransforms, outStream]);\n })\n .catch((error: unknown) => {\n this.#archiveError = errorFromUnknown(error);\n archiveStream.destroy();\n outStream.destroy();\n });\n\n this.results.file = { path: this.#archivePath };\n }\n\n async close() {\n if (this.#closed) {\n return;\n }\n\n this.#closed = true;\n await this.#archiveReady;\n if (this.#archiveError) {\n throw this.#archiveError;\n }\n\n const { stream, pipeline } = this.#archive;\n\n if (!stream) {\n return;\n }\n\n await this.#writeMetadata();\n stream.finalize();\n\n if (pipeline && !pipeline.closed) {\n await new Promise<void>((resolve, reject) => {\n pipeline.on('close', resolve).on('error', reject);\n });\n }\n }\n\n async rollback(): Promise<void> {\n this.#reportInfo('rolling back');\n await this.close();\n await rm(this.#archivePath, { force: true });\n }\n\n getMetadata() {\n return null;\n }\n\n async #writeMetadata(): Promise<void> {\n this.#reportInfo('writing metadata');\n const metadata = this.#providersMetadata.source;\n\n if (metadata) {\n await new Promise((resolve) => {\n const outStream = this.#getMetadataStream();\n const data = JSON.stringify(metadata, null, 2);\n\n Readable.from(data).pipe(outStream).on('close', resolve);\n });\n }\n }\n\n #getMetadataStream() {\n const { stream } = this.#archive;\n\n if (!stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n return createTarEntryStream(stream, () => 'metadata.json');\n }\n\n createSchemasWriteStream(): Writable {\n return this.#createJsonlWriteStream('schemas');\n }\n\n createEntitiesWriteStream(): Writable {\n return this.#createJsonlWriteStream('entities');\n }\n\n createLinksWriteStream(): Writable {\n return this.#createJsonlWriteStream('links');\n }\n\n createConfigurationWriteStream(): Writable {\n return this.#createJsonlWriteStream('configuration');\n }\n\n #createJsonlWriteStream(stage: string): Writable {\n if (!this.#archive.stream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo(`creating ${stage} write stream`);\n const filePathFactory = createFilePathFactory(stage);\n const entryStream = createTarEntryStream(\n this.#archive.stream,\n filePathFactory,\n this.options.file.maxSizeJsonl\n );\n\n return createJsonlWriteStream(entryStream);\n }\n\n createAssetsWriteStream(): Writable {\n const { stream: archiveStream } = this.#archive;\n\n if (!archiveStream) {\n throw new Error('Archive stream is unavailable');\n }\n\n this.#reportInfo('creating assets write stream');\n return new Writable({\n objectMode: true,\n write(data: IAsset, _encoding, callback) {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n const entryPath = path.posix.join('assets', 'uploads', data.filename);\n\n const entryMetadataPath = path.posix.join('assets', 'metadata', `${data.filename}.json`);\n const stringifiedMetadata = JSON.stringify(data.metadata);\n archiveStream.entry(\n {\n name: entryMetadataPath,\n size: stringifiedMetadata.length,\n },\n stringifiedMetadata\n );\n\n const entry = archiveStream.entry({\n name: entryPath,\n size: data.stats.size,\n });\n\n if (!entry) {\n callback(new Error(`Failed to created an asset tar entry for ${entryPath}`));\n return;\n }\n\n data.stream.pipe(entry);\n\n entry\n .on('finish', () => {\n callback(null);\n })\n .on('error', (error) => {\n callback(error);\n });\n },\n });\n }\n}\n"],"names":["createLocalFileDestinationProvider","options","LocalFileDestinationProvider","setMetadata","target","metadata","createGzip","zlib","bootstrap","diagnostics","compression","encryption","enabled","key","Error","stream","tar","pack","outStream","createWriteStream","on","err","code","ProviderTransferError","archiveTransforms","push","createEncryptionCipher","archiveStream","undefined","loadStreamDeps","then","chain","pipeline","catch","error","errorFromUnknown","destroy","results","file","path","close","finalize","closed","Promise","resolve","reject","rollback","rm","force","getMetadata","createSchemasWriteStream","createEntitiesWriteStream","createLinksWriteStream","createConfigurationWriteStream","createAssetsWriteStream","Writable","objectMode","write","data","_encoding","callback","entryPath","posix","join","filename","entryMetadataPath","stringifiedMetadata","JSON","stringify","entry","name","size","length","stats","pipe","type","message","report","details","createdAt","Date","origin","kind","filePath","source","Readable","from","createT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MAAM,EACpB0E,eAAAA,EACA,IAAI,CAACxF,OAAO,CAACqC,IAAI,CAACsD,YAAY,CAAA;AAGhC,IAAA,OAAOC,sBAAAA,CAAuBF,WAAAA,CAAAA;AAChC;;;;"}
@@ -1,9 +1,8 @@
1
- import { Writable } from 'stream';
2
1
  import tar from 'tar-stream';
3
2
  /**
4
3
  * Create a file path factory for a given path & prefix.
5
4
  * Upon being called, the factory will return a file path for a given index
6
5
  */
7
6
  export declare const createFilePathFactory: (type: string) => (fileIndex?: number) => string;
8
- export declare const createTarEntryStream: (archive: tar.Pack, pathFactory: (index?: number) => string, maxSize?: number) => Writable;
7
+ export declare const createTarEntryStream: (archive: tar.Pack, pathFactory: (index?: number) => string, maxSize?: number) => import("stream").Writable;
9
8
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/file/providers/destination/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,GAAG,MAAM,YAAY,CAAC;AAE7B;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAC/B,MAAM,MAAM,MACZ,kBAAa,KAAG,MAQhB,CAAC;AAEJ,eAAO,MAAM,oBAAoB,GAC/B,SAAS,GAAG,CAAC,IAAI,EACjB,aAAa,CAAC,KAAK,CAAC,EAAE,MAAM,KAAK,MAAM,EACvC,gBAAgB,aAsDjB,CAAC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/file/providers/destination/utils.ts"],"names":[],"mappings":"AACA,OAAO,GAAG,MAAM,YAAY,CAAC;AAI7B;;;GAGG;AACH,eAAO,MAAM,qBAAqB,GAC/B,MAAM,MAAM,MACZ,kBAAa,KAAG,MAQhB,CAAC;AAEJ,eAAO,MAAM,oBAAoB,GAC/B,SAAS,GAAG,CAAC,IAAI,EACjB,aAAa,CAAC,KAAK,CAAC,EAAE,MAAM,KAAK,MAAM,EACvC,gBAAgB,8BAmCjB,CAAC"}
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var stream = require('stream');
4
3
  var path = require('path');
4
+ var bufferedEntryWriter = require('../../../utils/buffered-entry-writer.js');
5
5
 
6
6
  /**
7
7
  * Create a file path factory for a given path & prefix.
@@ -14,47 +14,38 @@ var path = require('path');
14
14
  };
15
15
  const createTarEntryStream = (archive, pathFactory, maxSize = 2.56e8)=>{
16
16
  let fileIndex = 0;
17
- let buffer = '';
18
- const flush = async ()=>{
19
- if (!buffer) {
20
- return;
21
- }
17
+ const writeEntry = (content)=>{
22
18
  fileIndex += 1;
23
19
  const name = pathFactory(fileIndex);
24
- const size = buffer.length;
25
- await new Promise((resolve, reject)=>{
26
- archive.entry({
27
- name,
28
- size
29
- }, buffer, (err)=>{
30
- if (err) {
31
- reject(err);
32
- }
33
- resolve();
34
- });
35
- });
36
- buffer = '';
37
- };
38
- const push = (chunk)=>{
39
- buffer += chunk;
40
- };
41
- return new stream.Writable({
42
- async destroy (err, callback) {
43
- await flush();
44
- callback(err);
45
- },
46
- async write (chunk, _encoding, callback) {
47
- const size = chunk.length;
48
- if (chunk.length > maxSize) {
49
- callback(new Error(`payload too large: ${chunk.length}>${maxSize}`));
20
+ const size = content.length;
21
+ return new Promise((resolve, reject)=>{
22
+ // tar-stream 2.2.0 does not call the callback when the pack is finalized or destroyed,
23
+ // and it throws instead of calling back when another entry is still open. Either one
24
+ // would leave this promise pending, and a destroy that waits for the flush would hang.
25
+ let entry;
26
+ try {
27
+ entry = archive.entry({
28
+ name,
29
+ size
30
+ }, content, (err)=>{
31
+ if (err) {
32
+ reject(err);
33
+ return;
34
+ }
35
+ resolve();
36
+ });
37
+ } catch (err) {
38
+ reject(err instanceof Error ? err : new Error(String(err)));
50
39
  return;
51
40
  }
52
- if (buffer.length + size > maxSize) {
53
- await flush();
41
+ if (!entry) {
42
+ reject(new Error('Cannot write a tar entry after the archive has closed'));
54
43
  }
55
- push(chunk);
56
- callback(null);
57
- }
44
+ });
45
+ };
46
+ return bufferedEntryWriter.createBufferedEntryWriter({
47
+ maxSize,
48
+ writeEntry
58
49
  });
59
50
  };
60
51
 
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sources":["../../../../src/file/providers/destination/utils.ts"],"sourcesContent":["import { Writable } from 'stream';\nimport { posix } from 'path';\nimport tar from 'tar-stream';\n\n/**\n * Create a file path factory for a given path & prefix.\n * Upon being called, the factory will return a file path for a given index\n */\nexport const createFilePathFactory =\n (type: string) =>\n (fileIndex = 0): string => {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n return posix.join(\n // \"{type}\" directory\n type,\n // \"${type}_XXXXX.jsonl\" file\n `${type}_${String(fileIndex).padStart(5, '0')}.jsonl`\n );\n };\n\nexport const createTarEntryStream = (\n archive: tar.Pack,\n pathFactory: (index?: number) => string,\n maxSize = 2.56e8\n) => {\n let fileIndex = 0;\n let buffer = '';\n\n const flush = async () => {\n if (!buffer) {\n return;\n }\n\n fileIndex += 1;\n const name = pathFactory(fileIndex);\n const size = buffer.length;\n\n await new Promise<void>((resolve, reject) => {\n archive.entry({ name, size }, buffer, (err) => {\n if (err) {\n reject(err);\n }\n\n resolve();\n });\n });\n\n buffer = '';\n };\n\n const push = (chunk: string | Buffer) => {\n buffer += chunk;\n };\n\n return new Writable({\n async destroy(err, callback) {\n await flush();\n callback(err);\n },\n\n async write(chunk, _encoding, callback) {\n const size = chunk.length;\n\n if (chunk.length > maxSize) {\n callback(new Error(`payload too large: ${chunk.length}>${maxSize}`));\n return;\n }\n\n if (buffer.length + size > maxSize) {\n await flush();\n }\n\n push(chunk);\n\n callback(null);\n },\n });\n};\n"],"names":["createFilePathFactory","type","fileIndex","posix","join","String","padStart","createTarEntryStream","archive","pathFactory","maxSize","buffer","flush","name","size","length","Promise","resolve","reject","entry","err","push","chunk","Writable","destroy","callback","write","_encoding","Error"],"mappings":";;;;;AAIA;;;AAGC,IACM,MAAMA,qBAAAA,GACX,CAACC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AA;YACNa,QAAAA,CAASL,GAAAA,CAAAA;AACX,QAAA,CAAA;AAEA,QAAA,MAAMM,KAAAA,CAAAA,CAAMJ,KAAK,EAAEK,SAAS,EAAEF,QAAQ,EAAA;YACpC,MAAMX,IAAAA,GAAOQ,MAAMP,MAAM;YAEzB,IAAIO,KAAAA,CAAMP,MAAM,GAAGL,OAAAA,EAAS;gBAC1Be,QAAAA,CAAS,IAAIG,KAAAA,CAAM,CAAC,mBAAmB,EAAEN,MAAMP,MAAM,CAAC,CAAC,EAAEL,OAAAA,CAAAA,CAAS,CAAA,CAAA;AAClE,gBAAA;AACF,YAAA;AAEA,YAAA,IAAIC,MAAAA,CAAOI,MAAM,GAAGD,IAAAA,GAAOJ,OAAAA,EAAS;gBAClC,MAAME,KAAAA,EAAAA;AACR,YAAA;YAEAS,IAAAA,CAAKC,KAAAA,CAAAA;YAELG,QAAAA,CAAS,IAAA,CAAA;AACX,QAAA;AACF,KAAA,CAAA;AACF;;;;;"}
1
+ {"version":3,"file":"utils.js","sources":["../../../../src/file/providers/destination/utils.ts"],"sourcesContent":["import { posix } from 'path';\nimport tar from 'tar-stream';\n\nimport { createBufferedEntryWriter } from '../../../utils/buffered-entry-writer';\n\n/**\n * Create a file path factory for a given path & prefix.\n * Upon being called, the factory will return a file path for a given index\n */\nexport const createFilePathFactory =\n (type: string) =>\n (fileIndex = 0): string => {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n return posix.join(\n // \"{type}\" directory\n type,\n // \"${type}_XXXXX.jsonl\" file\n `${type}_${String(fileIndex).padStart(5, '0')}.jsonl`\n );\n };\n\nexport const createTarEntryStream = (\n archive: tar.Pack,\n pathFactory: (index?: number) => string,\n maxSize = 2.56e8\n) => {\n let fileIndex = 0;\n\n const writeEntry = (content: string) => {\n fileIndex += 1;\n const name = pathFactory(fileIndex);\n const size = content.length;\n\n return new Promise<void>((resolve, reject) => {\n // tar-stream 2.2.0 does not call the callback when the pack is finalized or destroyed,\n // and it throws instead of calling back when another entry is still open. Either one\n // would leave this promise pending, and a destroy that waits for the flush would hang.\n let entry: unknown;\n try {\n entry = archive.entry({ name, size }, content, (err) => {\n if (err) {\n reject(err);\n return;\n }\n\n resolve();\n });\n } catch (err) {\n reject(err instanceof Error ? err : new Error(String(err)));\n return;\n }\n\n if (!entry) {\n reject(new Error('Cannot write a tar entry after the archive has closed'));\n }\n });\n };\n\n return createBufferedEntryWriter({ maxSize, writeEntry });\n};\n"],"names":["createFilePathFactory","type","fileIndex","posix","join","String","padStart","createTarEntryStream","archive","pathFactory","maxSize","writeEntry","content","name","size","length","Promise","resolve","reject","entry","err","Error","createBufferedEntryWriter"],"mappings":";;;;;AAKA;;;AAGC,IACM,MAAMA,qBAAAA,GACX,CAACC,IAAAA,GACD,CAACC,YAAY,CAAC,GAAA;;QAEZ,OAAOC,UAAAA,CAAMC,IAAI;AAEfH,QAAAA,IAAAA;QAEA,CAAA,EAAGA,IAAAA,CAAK,CAAC,EAAEI,MAAAA,CAAOH,SAAAA,CAAAA,CAAWI,QAAQ,CAAC,CAAA,EAAG,GAAA,CAAA,CAAK,MAAM,CAAC,CAAA;IAEzD;MAEWC,oBAAAA,GAAuB,CAClCC,OAAAA,EACAC,WAAAA,EACAC,UAAU,MAAM,GAAA;AAEhB,IAAA,IAAIR,SAAAA,GAAY,CAAA;AAEhB,IAAA,MAAMS,aAAa,CAACC,OAAAA,GAAAA;QAClBV,SAAAA,IAAa,CAAA;AACb,QAAA,MAAMW,OAAOJ,WAAAA,CAAYP,SAAAA,CAAAA;QACzB,MAAMY,IAAAA,GAAOF,QAAQG,MAAM;QAE3B,OAAO,IAAIC,OAAAA,CAAc,CAACC,OAAAA,EAASC,MAAAA,GAAAA;;;;YAIjC,IAAIC,KAAAA;YACJ,IAAI;gBACFA,KAAAA,GAAQX,OAAAA,CAAQW,KAAK,CAAC;AAAEN,oBAAAA,IAAAA;AAAMC,oBAAAA;AAAK,iBAAA,EAAGF,SAAS,CAACQ,GAAAA,GAAAA;AAC9C,oBAAA,IAAIA,GAAAA,EAAK;wBACPF,MAAAA,CAAOE,GAAAA,CAAAA;AACP,wBAAA;AACF,oBAAA;AAEAH,oBAAAA,OAAAA,EAAAA;AACF,gBAAA,CAAA,CAAA;AACF,YAAA,CAAA,CAAE,OAAOG,GAAAA,EAAK;AACZF,gBAAAA,MAAAA,CAAOE,GAAAA,YAAeC,KAAAA,GAAQD,GAAAA,GAAM,IAAIC,MAAMhB,MAAAA,CAAOe,GAAAA,CAAAA,CAAAA,CAAAA;AACrD,gBAAA;AACF,YAAA;AAEA,YAAA,IAAI,CAACD,KAAAA,EAAO;AACVD,gBAAAA,MAAAA,CAAO,IAAIG,KAAAA,CAAM,uDAAA,CAAA,CAAA;AACnB,YAAA;AACF,QAAA,CAAA,CAAA;AACF,IAAA,CAAA;AAEA,IAAA,OAAOC,6CAAAA,CAA0B;AAAEZ,QAAAA,OAAAA;AAASC,QAAAA;AAAW,KAAA,CAAA;AACzD;;;;;"}
@@ -1,5 +1,5 @@
1
- import { Writable } from 'stream';
2
1
  import { posix } from 'path';
2
+ import { createBufferedEntryWriter } from '../../../utils/buffered-entry-writer.mjs';
3
3
 
4
4
  /**
5
5
  * Create a file path factory for a given path & prefix.
@@ -12,47 +12,38 @@ import { posix } from 'path';
12
12
  };
13
13
  const createTarEntryStream = (archive, pathFactory, maxSize = 2.56e8)=>{
14
14
  let fileIndex = 0;
15
- let buffer = '';
16
- const flush = async ()=>{
17
- if (!buffer) {
18
- return;
19
- }
15
+ const writeEntry = (content)=>{
20
16
  fileIndex += 1;
21
17
  const name = pathFactory(fileIndex);
22
- const size = buffer.length;
23
- await new Promise((resolve, reject)=>{
24
- archive.entry({
25
- name,
26
- size
27
- }, buffer, (err)=>{
28
- if (err) {
29
- reject(err);
30
- }
31
- resolve();
32
- });
33
- });
34
- buffer = '';
35
- };
36
- const push = (chunk)=>{
37
- buffer += chunk;
38
- };
39
- return new Writable({
40
- async destroy (err, callback) {
41
- await flush();
42
- callback(err);
43
- },
44
- async write (chunk, _encoding, callback) {
45
- const size = chunk.length;
46
- if (chunk.length > maxSize) {
47
- callback(new Error(`payload too large: ${chunk.length}>${maxSize}`));
18
+ const size = content.length;
19
+ return new Promise((resolve, reject)=>{
20
+ // tar-stream 2.2.0 does not call the callback when the pack is finalized or destroyed,
21
+ // and it throws instead of calling back when another entry is still open. Either one
22
+ // would leave this promise pending, and a destroy that waits for the flush would hang.
23
+ let entry;
24
+ try {
25
+ entry = archive.entry({
26
+ name,
27
+ size
28
+ }, content, (err)=>{
29
+ if (err) {
30
+ reject(err);
31
+ return;
32
+ }
33
+ resolve();
34
+ });
35
+ } catch (err) {
36
+ reject(err instanceof Error ? err : new Error(String(err)));
48
37
  return;
49
38
  }
50
- if (buffer.length + size > maxSize) {
51
- await flush();
39
+ if (!entry) {
40
+ reject(new Error('Cannot write a tar entry after the archive has closed'));
52
41
  }
53
- push(chunk);
54
- callback(null);
55
- }
42
+ });
43
+ };
44
+ return createBufferedEntryWriter({
45
+ maxSize,
46
+ writeEntry
56
47
  });
57
48
  };
58
49
 
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sources":["../../../../src/file/providers/destination/utils.ts"],"sourcesContent":["import { Writable } from 'stream';\nimport { posix } from 'path';\nimport tar from 'tar-stream';\n\n/**\n * Create a file path factory for a given path & prefix.\n * Upon being called, the factory will return a file path for a given index\n */\nexport const createFilePathFactory =\n (type: string) =>\n (fileIndex = 0): string => {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n return posix.join(\n // \"{type}\" directory\n type,\n // \"${type}_XXXXX.jsonl\" file\n `${type}_${String(fileIndex).padStart(5, '0')}.jsonl`\n );\n };\n\nexport const createTarEntryStream = (\n archive: tar.Pack,\n pathFactory: (index?: number) => string,\n maxSize = 2.56e8\n) => {\n let fileIndex = 0;\n let buffer = '';\n\n const flush = async () => {\n if (!buffer) {\n return;\n }\n\n fileIndex += 1;\n const name = pathFactory(fileIndex);\n const size = buffer.length;\n\n await new Promise<void>((resolve, reject) => {\n archive.entry({ name, size }, buffer, (err) => {\n if (err) {\n reject(err);\n }\n\n resolve();\n });\n });\n\n buffer = '';\n };\n\n const push = (chunk: string | Buffer) => {\n buffer += chunk;\n };\n\n return new Writable({\n async destroy(err, callback) {\n await flush();\n callback(err);\n },\n\n async write(chunk, _encoding, callback) {\n const size = chunk.length;\n\n if (chunk.length > maxSize) {\n callback(new Error(`payload too large: ${chunk.length}>${maxSize}`));\n return;\n }\n\n if (buffer.length + size > maxSize) {\n await flush();\n }\n\n push(chunk);\n\n callback(null);\n },\n });\n};\n"],"names":["createFilePathFactory","type","fileIndex","posix","join","String","padStart","createTarEntryStream","archive","pathFactory","maxSize","buffer","flush","name","size","length","Promise","resolve","reject","entry","err","push","chunk","Writable","destroy","callback","write","_encoding","Error"],"mappings":";;;AAIA;;;AAGC,IACM,MAAMA,qBAAAA,GACX,CAACC,IAAAA,GACD,CAACC,YAAY,CAAC,GAAA;;QAEZ,OAAOC,KAAAA,CAAMC,IAAI;AAEfH,QAAAA,IAAAA;QAEA,CAAA,EAAGA,IAAAA,CAAK,CAAC,EAAEI,MAAAA,CAAOH,SAAAA,CAAAA,CAAWI,QAAQ,CAAC,CAAA,EAAG,GAAA,CAAA,CAAK,MAAM,CAAC,CAAA;IAEzD;MAEWC,oBAAAA,GAAuB,CAClCC,OAAAA,EACAC,WAAAA,EACAC,UAAU,MAAM,GAAA;AAEhB,IAAA,IAAIR,SAAAA,GAAY,CAAA;AAChB,IAAA,IAAIS,MAAAA,GAAS,EAAA;AAEb,IAAA,MAAMC,KAAAA,GAAQ,UAAA;AACZ,QAAA,IAAI,CAACD,MAAAA,EAAQ;AACX,YAAA;AACF,QAAA;QAEAT,SAAAA,IAAa,CAAA;AACb,QAAA,MAAMW,OAAOJ,WAAAA,CAAYP,SAAAA,CAAAA;QACzB,MAAMY,IAAAA,GAAOH,OAAOI,MAAM;QAE1B,MAAM,IAAIC,OAAAA,CAAc,CAACC,OAAAA,EAASC,MAAAA,GAAAA;AAChCV,YAAAA,OAAAA,CAAQW,KAAK,CAAC;AAAEN,gBAAAA,IAAAA;AAAMC,gBAAAA;AAAK,aAAA,EAAGH,QAAQ,CAACS,GAAAA,GAAAA;AACrC,gBAAA,IAAIA,GAAAA,EAAK;oBACPF,MAAAA,CAAOE,GAAAA,CAAAA;AACT,gBAAA;AAEAH,gBAAAA,OAAAA,EAAAA;AACF,YAAA,CAAA,CAAA;AACF,QAAA,CAAA,CAAA;QAEAN,MAAAA,GAAS,EAAA;AACX,IAAA,CAAA;AAEA,IAAA,MAAMU,OAAO,CAACC,KAAAA,GAAAA;QACZX,MAAAA,IAAUW,KAAAA;AACZ,IAAA,CAAA;AAEA,IAAA,OAAO,IAAIC,QAAAA,CAAS;QAClB,MAAMC,OAAAA,CAAAA,CAAQJ,GAAG,EAAEK,QAAQ,EAAA;YACzB,MAAMb,KAAAA,EAAAA;YACNa,QAAAA,CAASL,GAAAA,CAAAA;AACX,QAAA,CAAA;AAEA,QAAA,MAAMM,KAAAA,CAAAA,CAAMJ,KAAK,EAAEK,SAAS,EAAEF,QAAQ,EAAA;YACpC,MAAMX,IAAAA,GAAOQ,MAAMP,MAAM;YAEzB,IAAIO,KAAAA,CAAMP,MAAM,GAAGL,OAAAA,EAAS;gBAC1Be,QAAAA,CAAS,IAAIG,KAAAA,CAAM,CAAC,mBAAmB,EAAEN,MAAMP,MAAM,CAAC,CAAC,EAAEL,OAAAA,CAAAA,CAAS,CAAA,CAAA;AAClE,gBAAA;AACF,YAAA;AAEA,YAAA,IAAIC,MAAAA,CAAOI,MAAM,GAAGD,IAAAA,GAAOJ,OAAAA,EAAS;gBAClC,MAAME,KAAAA,EAAAA;AACR,YAAA;YAEAS,IAAAA,CAAKC,KAAAA,CAAAA;YAELG,QAAAA,CAAS,IAAA,CAAA;AACX,QAAA;AACF,KAAA,CAAA;AACF;;;;"}
1
+ {"version":3,"file":"utils.mjs","sources":["../../../../src/file/providers/destination/utils.ts"],"sourcesContent":["import { posix } from 'path';\nimport tar from 'tar-stream';\n\nimport { createBufferedEntryWriter } from '../../../utils/buffered-entry-writer';\n\n/**\n * Create a file path factory for a given path & prefix.\n * Upon being called, the factory will return a file path for a given index\n */\nexport const createFilePathFactory =\n (type: string) =>\n (fileIndex = 0): string => {\n // always write tar files with posix paths so we have a standard format for paths regardless of system\n return posix.join(\n // \"{type}\" directory\n type,\n // \"${type}_XXXXX.jsonl\" file\n `${type}_${String(fileIndex).padStart(5, '0')}.jsonl`\n );\n };\n\nexport const createTarEntryStream = (\n archive: tar.Pack,\n pathFactory: (index?: number) => string,\n maxSize = 2.56e8\n) => {\n let fileIndex = 0;\n\n const writeEntry = (content: string) => {\n fileIndex += 1;\n const name = pathFactory(fileIndex);\n const size = content.length;\n\n return new Promise<void>((resolve, reject) => {\n // tar-stream 2.2.0 does not call the callback when the pack is finalized or destroyed,\n // and it throws instead of calling back when another entry is still open. Either one\n // would leave this promise pending, and a destroy that waits for the flush would hang.\n let entry: unknown;\n try {\n entry = archive.entry({ name, size }, content, (err) => {\n if (err) {\n reject(err);\n return;\n }\n\n resolve();\n });\n } catch (err) {\n reject(err instanceof Error ? err : new Error(String(err)));\n return;\n }\n\n if (!entry) {\n reject(new Error('Cannot write a tar entry after the archive has closed'));\n }\n });\n };\n\n return createBufferedEntryWriter({ maxSize, writeEntry });\n};\n"],"names":["createFilePathFactory","type","fileIndex","posix","join","String","padStart","createTarEntryStream","archive","pathFactory","maxSize","writeEntry","content","name","size","length","Promise","resolve","reject","entry","err","Error","createBufferedEntryWriter"],"mappings":";;;AAKA;;;AAGC,IACM,MAAMA,qBAAAA,GACX,CAACC,IAAAA,GACD,CAACC,YAAY,CAAC,GAAA;;QAEZ,OAAOC,KAAAA,CAAMC,IAAI;AAEfH,QAAAA,IAAAA;QAEA,CAAA,EAAGA,IAAAA,CAAK,CAAC,EAAEI,MAAAA,CAAOH,SAAAA,CAAAA,CAAWI,QAAQ,CAAC,CAAA,EAAG,GAAA,CAAA,CAAK,MAAM,CAAC,CAAA;IAEzD;MAEWC,oBAAAA,GAAuB,CAClCC,OAAAA,EACAC,WAAAA,EACAC,UAAU,MAAM,GAAA;AAEhB,IAAA,IAAIR,SAAAA,GAAY,CAAA;AAEhB,IAAA,MAAMS,aAAa,CAACC,OAAAA,GAAAA;QAClBV,SAAAA,IAAa,CAAA;AACb,QAAA,MAAMW,OAAOJ,WAAAA,CAAYP,SAAAA,CAAAA;QACzB,MAAMY,IAAAA,GAAOF,QAAQG,MAAM;QAE3B,OAAO,IAAIC,OAAAA,CAAc,CAACC,OAAAA,EAASC,MAAAA,GAAAA;;;;YAIjC,IAAIC,KAAAA;YACJ,IAAI;gBACFA,KAAAA,GAAQX,OAAAA,CAAQW,KAAK,CAAC;AAAEN,oBAAAA,IAAAA;AAAMC,oBAAAA;AAAK,iBAAA,EAAGF,SAAS,CAACQ,GAAAA,GAAAA;AAC9C,oBAAA,IAAIA,GAAAA,EAAK;wBACPF,MAAAA,CAAOE,GAAAA,CAAAA;AACP,wBAAA;AACF,oBAAA;AAEAH,oBAAAA,OAAAA,EAAAA;AACF,gBAAA,CAAA,CAAA;AACF,YAAA,CAAA,CAAE,OAAOG,GAAAA,EAAK;AACZF,gBAAAA,MAAAA,CAAOE,GAAAA,YAAeC,KAAAA,GAAQD,GAAAA,GAAM,IAAIC,MAAMhB,MAAAA,CAAOe,GAAAA,CAAAA,CAAAA,CAAAA;AACrD,gBAAA;AACF,YAAA;AAEA,YAAA,IAAI,CAACD,KAAAA,EAAO;AACVD,gBAAAA,MAAAA,CAAO,IAAIG,KAAAA,CAAM,uDAAA,CAAA,CAAA;AACnB,YAAA;AACF,QAAA,CAAA,CAAA;AACF,IAAA,CAAA;AAEA,IAAA,OAAOC,yBAAAA,CAA0B;AAAEZ,QAAAA,OAAAA;AAASC,QAAAA;AAAW,KAAA,CAAA;AACzD;;;;"}
@@ -34,7 +34,7 @@ declare class LocalFileSourceProvider implements ISourceProvider {
34
34
  createSchemasReadStream(): Readable;
35
35
  createLinksReadStream(): Readable;
36
36
  createConfigurationReadStream(): Readable;
37
- createAssetsReadStream(): Readable | Promise<Readable>;
37
+ createAssetsReadStream(): Readable;
38
38
  }
39
39
  export {};
40
40
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/file/providers/source/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAavC,OAAO,KAAK,EAEV,SAAS,EACT,eAAe,EACf,YAAY,EAEZ,aAAa,EACd,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAuBrE;;GAEG;AACH,MAAM,WAAW,+BAA+B;IAC9C,IAAI,EAAE;QACJ,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IAEF,UAAU,EAAE;QACV,OAAO,EAAE,OAAO,CAAC;QACjB,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;IAEF,WAAW,EAAE;QACX,OAAO,EAAE,OAAO,CAAC;KAClB,CAAC;CACH;AAED,eAAO,MAAM,6BAA6B,GAAI,SAAS,+BAA+B,4BAErF,CAAC;AAEF,cAAM,uBAAwB,YAAW,eAAe;;IACtD,IAAI,EAAE,YAAY,CAAY;IAE9B,IAAI,SAAwB;IAE5B,OAAO,EAAE,+BAA+B,CAAC;gBAQ7B,OAAO,EAAE,+BAA+B;IA4CpD;;OAEG;IACG,SAAS,CAAC,WAAW,EAAE,mBAAmB;IAoC1C,WAAW;IASX,UAAU;IAiBV,aAAa,CAAC,KAAK,EAAE,aAAa;IA2GxC,wBAAwB,IAAI,QAAQ;IAKpC,uBAAuB,IAAI,QAAQ;IAKnC,qBAAqB,IAAI,QAAQ;IAKjC,6BAA6B,IAAI,QAAQ;IAMzC,sBAAsB,IAAI,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;CAoMvD"}
1
+ {"version":3,"file":"index.d.ts","source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